]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinview.cpp
Make it (almost) possible to have more than one Dolphin KMainWindow
[dolphin.git] / src / dolphinview.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz <peter.penz@gmx.at> *
3 * Copyright (C) 2006 by Gregor Kališnik <gregor@podnapisi.net> *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20
21 #include "dolphinview.h"
22
23 #include <kdirmodel.h>
24
25
26 #include <qlayout.h>
27 //Added by qt3to4:
28 #include <Q3ValueList>
29 #include <QDropEvent>
30 #include <QMouseEvent>
31 #include <Q3VBoxLayout>
32 #include <kurl.h>
33 #include <klocale.h>
34 #include <kio/netaccess.h>
35 #include <kio/renamedlg.h>
36 #include <kmimetyperesolver.h>
37 #include <assert.h>
38
39 #include "urlnavigator.h"
40 #include "dolphinstatusbar.h"
41 #include "dolphinmainwindow.h"
42 #include "dolphindirlister.h"
43 #include "viewproperties.h"
44 #include "dolphindetailsview.h"
45 #include "dolphiniconsview.h"
46 #include "dolphincontextmenu.h"
47 #include "undomanager.h"
48 #include "renamedialog.h"
49 #include "progressindicator.h"
50
51 #include "filterbar.h"
52
53 DolphinView::DolphinView(DolphinMainWindow *mainWindow,
54 QWidget *parent,
55 const KUrl& url,
56 Mode mode,
57 bool showHiddenFiles) :
58 QWidget(parent),
59 m_mainWindow(mainWindow),
60 m_refreshing(false),
61 m_showProgress(false),
62 m_mode(mode),
63 m_statusBar(0),
64 m_iconSize(0),
65 m_folderCount(0),
66 m_fileCount(0),
67 m_filterBar(0)
68 {
69 setFocusPolicy(Qt::StrongFocus);
70 m_topLayout = new Q3VBoxLayout(this);
71
72 connect(this, SIGNAL(signalModeChanged()),
73 mainWindow, SLOT(slotViewModeChanged()));
74 connect(this, SIGNAL(signalShowHiddenFilesChanged()),
75 mainWindow, SLOT(slotShowHiddenFilesChanged()));
76 connect(this, SIGNAL(signalSortingChanged(DolphinView::Sorting)),
77 mainWindow, SLOT(slotSortingChanged(DolphinView::Sorting)));
78 connect(this, SIGNAL(signalSortOrderChanged(Qt::SortOrder)),
79 mainWindow, SLOT(slotSortOrderChanged(Qt::SortOrder)));
80
81 m_urlNavigator = new UrlNavigator(url, this);
82 connect(m_urlNavigator, SIGNAL(urlChanged(const KUrl&)),
83 this, SLOT(slotUrlChanged(const KUrl&)));
84 connect(m_urlNavigator, SIGNAL(urlChanged(const KUrl&)),
85 mainWindow, SLOT(slotUrlChanged(const KUrl&)));
86 connect(m_urlNavigator, SIGNAL(historyChanged()),
87 mainWindow, SLOT(slotHistoryChanged()));
88
89 m_statusBar = new DolphinStatusBar(this);
90
91 m_dirLister = new DolphinDirLister();
92 m_dirLister->setAutoUpdate(true);
93 m_dirLister->setMainWindow(this);
94 m_dirLister->setShowingDotFiles(showHiddenFiles);
95 connect(m_dirLister, SIGNAL(clear()),
96 this, SLOT(slotClear()));
97 connect(m_dirLister, SIGNAL(percent(int)),
98 this, SLOT(slotPercent(int)));
99 connect(m_dirLister, SIGNAL(deleteItem(KFileItem*)),
100 this, SLOT(slotDeleteItem(KFileItem*)));
101 connect(m_dirLister, SIGNAL(completed()),
102 this, SLOT(slotCompleted()));
103 connect(m_dirLister, SIGNAL(infoMessage(const QString&)),
104 this, SLOT(slotInfoMessage(const QString&)));
105 connect(m_dirLister, SIGNAL(errorMessage(const QString&)),
106 this, SLOT(slotErrorMessage(const QString&)));
107
108 m_iconsView = new DolphinIconsView(this);
109 connect(m_iconsView, SIGNAL(clicked(const QModelIndex&)),
110 this, SLOT(triggerItem(const QModelIndex&)));
111 applyModeToView();
112
113 KDirModel* model = new KDirModel();
114 model->setDirLister(m_dirLister);
115 m_iconsView->setModel(model);
116
117 m_dirLister->setDelayedMimeTypes(true);
118 new KMimeTypeResolver( m_iconsView, model );
119
120 m_iconSize = K3Icon::SizeMedium;
121
122 m_filterBar = new FilterBar(mainWindow, this);
123 m_filterBar->hide();
124 connect(m_filterBar, SIGNAL(signalFilterChanged(const QString&)),
125 this, SLOT(slotChangeNameFilter(const QString&)));
126
127 m_topLayout->addWidget(m_urlNavigator);
128 m_topLayout->addWidget(m_iconsView);
129 m_topLayout->addWidget(m_filterBar);
130 m_topLayout->addWidget(m_statusBar);
131
132 startDirLister(m_urlNavigator->url());
133 }
134
135 DolphinView::~DolphinView()
136 {
137 delete m_dirLister;
138 m_dirLister = 0;
139 }
140
141 void DolphinView::setUrl(const KUrl& url)
142 {
143 m_urlNavigator->setUrl(url);
144 }
145
146 const KUrl& DolphinView::url() const
147 {
148 return m_urlNavigator->url();
149 }
150
151 void DolphinView::requestActivation()
152 {
153 mainWindow()->setActiveView(this);
154 }
155
156 bool DolphinView::isActive() const
157 {
158 return (mainWindow()->activeView() == this);
159 }
160
161 void DolphinView::setMode(Mode mode)
162 {
163 if (mode == m_mode) {
164 return; // the wished mode is already set
165 }
166
167 m_mode = mode;
168
169 ViewProperties props(m_urlNavigator->url());
170 props.setViewMode(m_mode);
171
172 applyModeToView();
173 startDirLister(m_urlNavigator->url());
174
175 emit signalModeChanged();
176 }
177
178 DolphinView::Mode DolphinView::mode() const
179 {
180 return m_mode;
181 }
182
183 void DolphinView::setShowHiddenFilesEnabled(bool show)
184 {
185 if (m_dirLister->showingDotFiles() == show) {
186 return;
187 }
188
189 ViewProperties props(m_urlNavigator->url());
190 props.setShowHiddenFilesEnabled(show);
191 props.save();
192
193 m_dirLister->setShowingDotFiles(show);
194
195 emit signalShowHiddenFilesChanged();
196
197 reload();
198 }
199
200 bool DolphinView::isShowHiddenFilesEnabled() const
201 {
202 return m_dirLister->showingDotFiles();
203 }
204
205 void DolphinView::setViewProperties(const ViewProperties& props)
206 {
207 setMode(props.viewMode());
208 setSorting(props.sorting());
209 setSortOrder(props.sortOrder());
210 setShowHiddenFilesEnabled(props.isShowHiddenFilesEnabled());
211 }
212
213 void DolphinView::renameSelectedItems()
214 {
215 const KUrl::List urls = selectedUrls();
216 if (urls.count() > 1) {
217 // More than one item has been selected for renaming. Open
218 // a rename dialog and rename all items afterwards.
219 RenameDialog dialog(urls);
220 if (dialog.exec() == QDialog::Rejected) {
221 return;
222 }
223
224 DolphinView* view = mainWindow()->activeView();
225 const QString& newName = dialog.newName();
226 if (newName.isEmpty()) {
227 view->statusBar()->setMessage(i18n("The new item name is invalid."),
228 DolphinStatusBar::Error);
229 }
230 else {
231 UndoManager& undoMan = UndoManager::instance();
232 undoMan.beginMacro();
233
234 assert(newName.contains('#'));
235
236 const int urlsCount = urls.count();
237 ProgressIndicator* progressIndicator =
238 new ProgressIndicator(mainWindow(),
239 i18n("Renaming items..."),
240 i18n("Renaming finished."),
241 urlsCount);
242
243 // iterate through all selected items and rename them...
244 const int replaceIndex = newName.find('#');
245 assert(replaceIndex >= 0);
246 for (int i = 0; i < urlsCount; ++i) {
247 const KUrl& source = urls[i];
248 QString name(newName);
249 name.replace(replaceIndex, 1, renameIndexPresentation(i + 1, urlsCount));
250
251 if (source.fileName() != name) {
252 KUrl dest(source.upUrl());
253 dest.addPath(name);
254
255 const bool destExists = KIO::NetAccess::exists(dest, false, view);
256 if (destExists) {
257 delete progressIndicator;
258 progressIndicator = 0;
259 view->statusBar()->setMessage(i18n("Renaming failed (item '%1' already exists).",name),
260 DolphinStatusBar::Error);
261 break;
262 }
263 else if (KIO::NetAccess::file_move(source, dest)) {
264 // TODO: From the users point of view he executed one 'rename n files' operation,
265 // but internally we store it as n 'rename 1 file' operations for the undo mechanism.
266 DolphinCommand command(DolphinCommand::Rename, source, dest, mainWindow());
267 undoMan.addCommand(command);
268 }
269 }
270
271 progressIndicator->execOperation();
272 }
273 delete progressIndicator;
274 progressIndicator = 0;
275
276 undoMan.endMacro();
277 }
278 }
279 else {
280 // Only one item has been selected for renaming. Use the custom
281 // renaming mechanism from the views.
282 assert(urls.count() == 1);
283 // TODO:
284 /*if (m_mode == DetailsView) {
285 Q3ListViewItem* item = m_iconsView->firstChild();
286 while (item != 0) {
287 if (item->isSelected()) {
288 m_iconsView->rename(item, DolphinDetailsView::NameColumn);
289 break;
290 }
291 item = item->nextSibling();
292 }
293 }
294 else {
295 KFileIconViewItem* item = static_cast<KFileIconViewItem*>(m_iconsView->firstItem());
296 while (item != 0) {
297 if (item->isSelected()) {
298 item->rename();
299 break;
300 }
301 item = static_cast<KFileIconViewItem*>(item->nextItem());
302 }
303 }*/
304 }
305 }
306
307 void DolphinView::selectAll()
308 {
309 //fileView()->selectAll();
310 }
311
312 void DolphinView::invertSelection()
313 {
314 //fileView()->invertSelection();
315 }
316
317 DolphinStatusBar* DolphinView::statusBar() const
318 {
319 return m_statusBar;
320 }
321
322 int DolphinView::contentsX() const
323 {
324
325 return 0; //scrollView()->contentsX();
326 }
327
328 int DolphinView::contentsY() const
329 {
330 return 0; //scrollView()->contentsY();
331 }
332
333 void DolphinView::refreshSettings()
334 {
335 startDirLister(m_urlNavigator->url());
336 }
337
338 void DolphinView::updateStatusBar()
339 {
340 // As the item count information is less important
341 // in comparison with other messages, it should only
342 // be shown if:
343 // - the status bar is empty or
344 // - shows already the item count information or
345 // - shows only a not very important information
346 // - if any progress is given don't show the item count info at all
347 const QString msg(m_statusBar->message());
348 const bool updateStatusBarMsg = (msg.isEmpty() ||
349 (msg == m_statusBar->defaultText()) ||
350 (m_statusBar->type() == DolphinStatusBar::Information)) &&
351 (m_statusBar->progress() == 100);
352
353 const QString text(hasSelection() ? selectionStatusBarText() : defaultStatusBarText());
354 m_statusBar->setDefaultText(text);
355
356 if (updateStatusBarMsg) {
357 m_statusBar->setMessage(text, DolphinStatusBar::Default);
358 }
359 }
360
361 void DolphinView::requestItemInfo(const KUrl& url)
362 {
363 emit signalRequestItemInfo(url);
364 }
365
366 bool DolphinView::isUrlEditable() const
367 {
368 return m_urlNavigator->isUrlEditable();
369 }
370
371 void DolphinView::zoomIn()
372 {
373 //itemEffectsManager()->zoomIn();
374 }
375
376 void DolphinView::zoomOut()
377 {
378 //itemEffectsManager()->zoomOut();
379 }
380
381 bool DolphinView::isZoomInPossible() const
382 {
383 return false; //itemEffectsManager()->isZoomInPossible();
384 }
385
386 bool DolphinView::isZoomOutPossible() const
387 {
388 return false; //itemEffectsManager()->isZoomOutPossible();
389 }
390
391 void DolphinView::setSorting(Sorting sorting)
392 {
393 if (sorting != this->sorting()) {
394 /*KFileView* view = fileView();
395 int spec = view->sorting() & ~QDir::Name & ~QDir::Size & ~QDir::Time & ~QDir::Unsorted;
396
397 switch (sorting) {
398 case SortByName: spec = spec | QDir::Name; break;
399 case SortBySize: spec = spec | QDir::Size; break;
400 case SortByDate: spec = spec | QDir::Time; break;
401 default: break;
402 }
403
404 ViewProperties props(url());
405 props.setSorting(sorting);
406
407 view->setSorting(static_cast<QDir::SortFlags>(spec));
408
409 emit signalSortingChanged(sorting);*/
410 }
411 }
412
413 DolphinView::Sorting DolphinView::sorting() const
414 {
415 /*const QDir::SortFlags spec = fileView()->sorting();
416
417 if (spec & QDir::Time) {
418 return SortByDate;
419 }
420
421 if (spec & QDir::Size) {
422 return SortBySize;
423 }*/
424
425 return SortByName;
426 }
427
428 void DolphinView::setSortOrder(Qt::SortOrder order)
429 {
430 if (sortOrder() != order) {
431 /*KFileView* view = fileView();
432 int sorting = view->sorting();
433 sorting = (order == Qt::Ascending) ? (sorting & ~QDir::Reversed) :
434 (sorting | QDir::Reversed);
435
436 ViewProperties props(url());
437 props.setSortOrder(order);
438
439 view->setSorting(static_cast<QDir::SortFlags>(sorting));
440
441 emit signalSortOrderChanged(order);*/
442 }
443 }
444
445 Qt::SortOrder DolphinView::sortOrder() const
446 {
447 //return fileView()->isReversed() ? Qt::Descending : Qt::Ascending;
448 return Qt::Descending;
449 }
450
451 void DolphinView::goBack()
452 {
453 m_urlNavigator->goBack();
454 }
455
456 void DolphinView::goForward()
457 {
458 m_urlNavigator->goForward();
459 }
460
461 void DolphinView::goUp()
462 {
463 m_urlNavigator->goUp();
464 }
465
466 void DolphinView::goHome()
467 {
468 m_urlNavigator->goHome();
469 }
470
471 void DolphinView::setUrlEditable(bool editable)
472 {
473 m_urlNavigator->editUrl(editable);
474 }
475
476 const Q3ValueList<UrlNavigator::HistoryElem> DolphinView::urlHistory(int& index) const
477 {
478 return m_urlNavigator->history(index);
479 }
480
481 bool DolphinView::hasSelection() const
482 {
483 const KFileItemList* list = selectedItems();
484 return (list != 0) && !list->isEmpty();
485 }
486
487 const KFileItemList* DolphinView::selectedItems() const
488 {
489 return 0; //fileView()->selectedItems();
490 }
491
492 KUrl::List DolphinView::selectedUrls() const
493 {
494 KUrl::List urls;
495
496 /*const KFileItemList* list = fileView()->selectedItems();
497 if (list != 0) {
498 KFileItemList::const_iterator it = list->begin();
499 const KFileItemList::const_iterator end = list->end();
500 while (it != end) {
501 KFileItem* item = *it;
502 urls.append(item->url());
503 ++it;
504 }
505 }*/
506
507 return urls;
508 }
509
510 const KFileItem* DolphinView::currentFileItem() const
511 {
512 return 0; // fileView()->currentFileItem();
513 }
514
515 void DolphinView::openContextMenu(KFileItem* fileInfo, const QPoint& pos)
516 {
517 DolphinContextMenu contextMenu(this, fileInfo, pos);
518 contextMenu.open();
519 }
520
521 void DolphinView::rename(const KUrl& source, const QString& newName)
522 {
523 bool ok = false;
524
525 if (newName.isEmpty() || (source.fileName() == newName)) {
526 return;
527 }
528
529 KUrl dest(source.upUrl());
530 dest.addPath(newName);
531
532 const bool destExists = KIO::NetAccess::exists(dest,
533 false,
534 mainWindow()->activeView());
535 if (destExists) {
536 // the destination already exists, hence ask the user
537 // how to proceed...
538 KIO::RenameDlg renameDialog(this,
539 i18n("File Already Exists"),
540 source.path(),
541 dest.path(),
542 KIO::M_OVERWRITE);
543 switch (renameDialog.exec()) {
544 case KIO::R_OVERWRITE:
545 // the destination should be overwritten
546 ok = KIO::NetAccess::file_move(source, dest, -1, true);
547 break;
548
549 case KIO::R_RENAME: {
550 // a new name for the destination has been used
551 KUrl newDest(renameDialog.newDestUrl());
552 ok = KIO::NetAccess::file_move(source, newDest);
553 break;
554 }
555
556 default:
557 // the renaming operation has been canceled
558 reload();
559 return;
560 }
561 }
562 else {
563 // no destination exists, hence just move the file to
564 // do the renaming
565 ok = KIO::NetAccess::file_move(source, dest);
566 }
567
568 if (ok) {
569 m_statusBar->setMessage(i18n("Renamed file '%1' to '%2'.",source.fileName(), dest.fileName()),
570 DolphinStatusBar::OperationCompleted);
571
572 DolphinCommand command(DolphinCommand::Rename, source, dest, mainWindow());
573 UndoManager::instance().addCommand(command);
574 }
575 else {
576 m_statusBar->setMessage(i18n("Renaming of file '%1' to '%2' failed.",source.fileName(), dest.fileName()),
577 DolphinStatusBar::Error);
578 reload();
579 }
580 }
581
582 void DolphinView::reload()
583 {
584 startDirLister(m_urlNavigator->url(), true);
585 }
586
587 void DolphinView::slotUrlListDropped(QDropEvent* /* event */,
588 const KUrl::List& urls,
589 const KUrl& url)
590 {
591 KUrl destination(url);
592 if (destination.isEmpty()) {
593 destination = m_urlNavigator->url();
594 }
595 else {
596 // Check whether the destination Url is a directory. If this is not the
597 // case, use the navigator Url as destination (otherwise the destination,
598 // which represents a file, would be replaced by a copy- or move-operation).
599 KFileItem fileItem(KFileItem::Unknown, KFileItem::Unknown, destination);
600 if (!fileItem.isDir()) {
601 destination = m_urlNavigator->url();
602 }
603 }
604
605 mainWindow()->dropUrls(urls, destination);
606 }
607
608 void DolphinView::mouseReleaseEvent(QMouseEvent* event)
609 {
610 QWidget::mouseReleaseEvent(event);
611 mainWindow()->setActiveView(this);
612 }
613
614 DolphinMainWindow* DolphinView::mainWindow() const
615 {
616 return m_mainWindow;
617 }
618
619 void DolphinView::slotUrlChanged(const KUrl& url)
620 {
621 const ViewProperties props(url);
622 setMode(props.viewMode());
623
624 const bool showHiddenFiles = props.isShowHiddenFilesEnabled();
625 setShowHiddenFilesEnabled(showHiddenFiles);
626 m_dirLister->setShowingDotFiles(showHiddenFiles);
627
628 setSorting(props.sorting());
629 setSortOrder(props.sortOrder());
630
631 startDirLister(url);
632
633 // The selectionChanged signal is not emitted when a new view object is
634 // created. The application does not care whether a view is represented by a
635 // different instance, hence inform the application that the selection might have
636 // changed so that it can update it's actions.
637 mainWindow()->slotSelectionChanged();
638
639 emit signalUrlChanged(url);
640 }
641
642 void DolphinView::triggerIconsViewItem(Q3IconViewItem* item)
643 {
644 /* KDE4-TODO:
645 const Qt::ButtonState keyboardState = KApplication::keyboardMouseState();
646 const bool isSelectionActive = ((keyboardState & Qt::ShiftModifier) > 0) ||
647 ((keyboardState & Qt::ControlModifier) > 0);*/
648 const bool isSelectionActive = false;
649 if ((item != 0) && !isSelectionActive) {
650 // Updating the Url must be done outside the scope of this slot,
651 // as iconview items will get deleted.
652 QTimer::singleShot(0, this, SLOT(updateUrl()));
653 mainWindow()->setActiveView(this);
654 }
655 }
656
657 void DolphinView::triggerItem(const QModelIndex& index)
658 {
659 KDirModel* dirModel = static_cast<KDirModel*>(m_iconsView->model());
660 KFileItem* item = dirModel->itemForIndex(index);
661 if (item == 0) {
662 return;
663 }
664
665 if (item->isDir()) {
666 // Prefer the local path over the Url. This assures that the
667 // volume space information is correct. Assuming that the Url is media:/sda1,
668 // and the local path is /windows/C: For the Url the space info is related
669 // to the root partition (and hence wrong) and for the local path the space
670 // info is related to the windows partition (-> correct).
671 //m_dirLister->stop();
672 //m_dirLister->openUrl(item->url());
673 //return;
674
675 const QString localPath(item->localPath());
676 if (localPath.isEmpty()) {
677 setUrl(item->url());
678 }
679 else {
680 setUrl(KUrl(localPath));
681 }
682 }
683 else {
684 item->run();
685 }
686 }
687
688 void DolphinView::updateUrl()
689 {
690 //KFileView* fileView = (m_iconsView != 0) ? static_cast<KFileView*>(m_iconsView) :
691 // static_cast<KFileView*>(m_iconsView);
692
693 KFileItem* fileItem = 0; // TODO: fileView->currentFileItem();
694 if (fileItem == 0) {
695 return;
696 }
697
698 if (fileItem->isDir()) {
699 // Prefer the local path over the Url. This assures that the
700 // volume space information is correct. Assuming that the Url is media:/sda1,
701 // and the local path is /windows/C: For the Url the space info is related
702 // to the root partition (and hence wrong) and for the local path the space
703 // info is related to the windows partition (-> correct).
704 const QString localPath(fileItem->localPath());
705 if (localPath.isEmpty()) {
706 setUrl(fileItem->url());
707 }
708 else {
709 setUrl(KUrl(localPath));
710 }
711 }
712 else {
713 fileItem->run();
714 }
715 }
716
717 void DolphinView::slotPercent(int percent)
718 {
719 if (m_showProgress) {
720 m_statusBar->setProgress(percent);
721 }
722 }
723
724 void DolphinView::slotClear()
725 {
726 //fileView()->clearView();
727 updateStatusBar();
728 }
729
730 void DolphinView::slotDeleteItem(KFileItem* item)
731 {
732 //fileView()->removeItem(item);
733 updateStatusBar();
734 }
735
736 void DolphinView::slotCompleted()
737 {
738 m_refreshing = true;
739
740 //KFileView* view = fileView();
741 //view->clearView();
742
743 // TODO: in Qt4 the code should get a lot
744 // simpler and nicer due to Interview...
745 /*if (m_iconsView != 0) {
746 m_iconsView->beginItemUpdates();
747 }
748 if (m_iconsView != 0) {
749 m_iconsView->beginItemUpdates();
750 }*/
751
752 if (m_showProgress) {
753 m_statusBar->setProgressText(QString::null);
754 m_statusBar->setProgress(100);
755 m_showProgress = false;
756 }
757
758 KFileItemList items(m_dirLister->items());
759 KFileItemList::const_iterator it = items.begin();
760 const KFileItemList::const_iterator end = items.end();
761
762 m_fileCount = 0;
763 m_folderCount = 0;
764
765 while (it != end) {
766 KFileItem* item = *it;
767 //view->insertItem(item);
768 if (item->isDir()) {
769 ++m_folderCount;
770 }
771 else {
772 ++m_fileCount;
773 }
774 ++it;
775 }
776
777 updateStatusBar();
778
779 /*if (m_iconsView != 0) {
780 // Prevent a flickering of the icon view widget by giving a small
781 // timeslot to swallow asynchronous update events.
782 m_iconsView->setUpdatesEnabled(false);
783 QTimer::singleShot(10, this, SLOT(slotDelayedUpdate()));
784 }
785
786 if (m_iconsView != 0) {
787 m_iconsView->endItemUpdates();
788 m_refreshing = false;
789 }*/
790 }
791
792 void DolphinView::slotInfoMessage(const QString& msg)
793 {
794 m_statusBar->setMessage(msg, DolphinStatusBar::Information);
795 }
796
797 void DolphinView::slotErrorMessage(const QString& msg)
798 {
799 m_statusBar->setMessage(msg, DolphinStatusBar::Error);
800 }
801
802 void DolphinView::slotGrabActivation()
803 {
804 mainWindow()->setActiveView(this);
805 }
806
807 void DolphinView::slotContentsMoving(int x, int y)
808 {
809 if (!m_refreshing) {
810 // Only emit a 'contents moved' signal if the user
811 // moved the content by adjusting the sliders. Adjustments
812 // resulted by refreshing a directory should not be respected.
813 emit contentsMoved(x, y);
814 }
815 }
816
817 /*KFileView* DolphinView::fileView() const
818 {
819 return (m_mode == DetailsView) ? static_cast<KFileView*>(m_iconsView) :
820 static_cast<KFileView*>(m_iconsView);
821 }*/
822
823 Q3ScrollView* DolphinView::scrollView() const
824 {
825 return 0; //(m_mode == DetailsView) ? static_cast<Q3ScrollView*>(m_iconsView) :
826 // static_cast<Q3ScrollView*>(m_iconsView);
827 }
828
829 ItemEffectsManager* DolphinView::itemEffectsManager() const
830 {
831 return 0;
832 }
833
834 void DolphinView::startDirLister(const KUrl& url, bool reload)
835 {
836 if (!url.isValid()) {
837 const QString location(url.pathOrUrl());
838 if (location.isEmpty()) {
839 m_statusBar->setMessage(i18n("The location is empty."), DolphinStatusBar::Error);
840 }
841 else {
842 m_statusBar->setMessage(i18n("The location '%1' is invalid.",location),
843 DolphinStatusBar::Error);
844 }
845 return;
846 }
847
848 // Only show the directory loading progress if the status bar does
849 // not contain another progress information. This means that
850 // the directory loading progress information has the lowest priority.
851 const QString progressText(m_statusBar->progressText());
852 m_showProgress = progressText.isEmpty() ||
853 (progressText == i18n("Loading directory..."));
854 if (m_showProgress) {
855 m_statusBar->setProgressText(i18n("Loading directory..."));
856 m_statusBar->setProgress(0);
857 }
858
859 m_refreshing = true;
860 m_dirLister->stop();
861 m_dirLister->openUrl(url, false, reload);
862 }
863
864 QString DolphinView::defaultStatusBarText() const
865 {
866 // TODO: the following code is not suitable for languages where multiple forms
867 // of plurals are given (e. g. in Poland three forms of plurals exist).
868 const int itemCount = m_folderCount + m_fileCount;
869
870 QString text;
871 if (itemCount == 1) {
872 text = i18n("1 Item");
873 }
874 else {
875 text = i18n("%1 Items",itemCount);
876 }
877
878 text += " (";
879
880 if (m_folderCount == 1) {
881 text += i18n("1 Folder");
882 }
883 else {
884 text += i18n("%1 Folders",m_folderCount);
885 }
886
887 text += ", ";
888
889 if (m_fileCount == 1) {
890 text += i18n("1 File");
891 }
892 else {
893 text += i18n("%1 Files",m_fileCount);
894 }
895
896 text += ")";
897
898 return text;
899 }
900
901 QString DolphinView::selectionStatusBarText() const
902 {
903 // TODO: the following code is not suitable for languages where multiple forms
904 // of plurals are given (e. g. in Poland three forms of plurals exist).
905 QString text;
906 const KFileItemList* list = selectedItems();
907 assert((list != 0) && !list->isEmpty());
908
909 int fileCount = 0;
910 int folderCount = 0;
911 KIO::filesize_t byteSize = 0;
912 KFileItemList::const_iterator it = list->begin();
913 const KFileItemList::const_iterator end = list->end();
914 while (it != end){
915 KFileItem* item = *it;
916 if (item->isDir()) {
917 ++folderCount;
918 }
919 else {
920 ++fileCount;
921 byteSize += item->size();
922 }
923 ++it;
924 }
925
926 if (folderCount == 1) {
927 text = i18n("1 Folder selected");
928 }
929 else if (folderCount > 1) {
930 text = i18n("%1 Folders selected",folderCount);
931 }
932
933 if ((fileCount > 0) && (folderCount > 0)) {
934 text += ", ";
935 }
936
937 const QString sizeText(KIO::convertSize(byteSize));
938 if (fileCount == 1) {
939 text += i18n("1 File selected (%1)",sizeText);
940 }
941 else if (fileCount > 1) {
942 text += i18n("%1 Files selected (%1)",fileCount,sizeText);
943 }
944
945 return text;
946 }
947
948 QString DolphinView::renameIndexPresentation(int index, int itemCount) const
949 {
950 // assure that the string reprentation for all indicess have the same
951 // number of characters based on the given number of items
952 QString str(QString::number(index));
953 int chrCount = 1;
954 while (itemCount >= 10) {
955 ++chrCount;
956 itemCount /= 10;
957 }
958 str.reserve(chrCount);
959
960 const int insertCount = chrCount - str.length();
961 for (int i = 0; i < insertCount; ++i) {
962 str.insert(0, '0');
963 }
964 return str;
965 }
966
967 void DolphinView::slotShowFilterBar(bool show)
968 {
969 assert(m_filterBar != 0);
970 if (show) {
971 m_filterBar->show();
972 }
973 else {
974 m_filterBar->hide();
975 }
976 }
977
978 void DolphinView::declareViewActive()
979 {
980 mainWindow()->setActiveView( this );
981 }
982
983 void DolphinView::slotChangeNameFilter(const QString& nameFilter)
984 {
985 // The name filter of KDirLister does a 'hard' filtering, which
986 // means that only the items are shown where the names match
987 // exactly the filter. This is non-transparent for the user, which
988 // just wants to have a 'soft' filtering: does the name contain
989 // the filter string?
990 QString adjustedFilter(nameFilter);
991 adjustedFilter.insert(0, '*');
992 adjustedFilter.append('*');
993
994 m_dirLister->setNameFilter(adjustedFilter);
995 m_dirLister->emitChanges();
996
997 // TODO: this is a workaround for QIconView: the item position
998 // stay as they are by filtering, only an inserting of an item
999 // results to an automatic adjusting of the item position. In Qt4/KDE4
1000 // this workaround should get obsolete due to Interview.
1001 /*KFileView* view = fileView();
1002 if (view == m_iconsView) {
1003 KFileItem* first = view->firstFileItem();
1004 if (first != 0) {
1005 view->removeItem(first);
1006 view->insertItem(first);
1007 }
1008 }*/
1009 }
1010
1011 bool DolphinView::isFilterBarVisible()
1012 {
1013 return m_filterBar->isVisible();
1014 }
1015
1016 void DolphinView::applyModeToView()
1017 {
1018 // TODO: the following code just tries to test some QListView capabilities
1019 switch (m_mode) {
1020 case IconsView:
1021 m_iconsView->setViewMode(QListView::IconMode);
1022 m_iconsView->setGridSize(QSize(128, 64));
1023 break;
1024
1025 case DetailsView:
1026 m_iconsView->setViewMode(QListView::ListMode);
1027 m_iconsView->setGridSize(QSize(256, 24));
1028 break;
1029
1030 case PreviewsView:
1031 m_iconsView->setViewMode(QListView::IconMode);
1032 m_iconsView->setGridSize(QSize(128, 128));
1033 break;
1034 }
1035 }
1036
1037 #include "dolphinview.moc"