]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinview.cpp
Get rid of some KDE 3 relicts.
[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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 ***************************************************************************/
20
21 #include "dolphinview.h"
22
23 #include <assert.h>
24
25 #include <QDropEvent>
26 #include <QItemSelectionModel>
27 #include <QMouseEvent>
28 #include <QVBoxLayout>
29
30 #include <kdirmodel.h>
31 #include <kfileitemdelegate.h>
32 #include <klocale.h>
33 #include <kio/netaccess.h>
34 #include <kio/renamedialog.h>
35 #include <kmimetyperesolver.h>
36 #include <konq_operations.h>
37 #include <kurl.h>
38
39 #include "dolphinstatusbar.h"
40 #include "dolphinmainwindow.h"
41 #include "dolphindirlister.h"
42 #include "dolphinsortfilterproxymodel.h"
43 #include "dolphindetailsview.h"
44 #include "dolphiniconsview.h"
45 #include "dolphincontextmenu.h"
46 #include "filterbar.h"
47 #include "renamedialog.h"
48 #include "urlnavigator.h"
49 #include "viewproperties.h"
50
51 DolphinView::DolphinView(DolphinMainWindow* mainWindow,
52 QWidget* parent,
53 const KUrl& url,
54 Mode mode,
55 bool showHiddenFiles) :
56 QWidget(parent),
57 m_showProgress(false),
58 m_mode(mode),
59 m_iconSize(0),
60 m_folderCount(0),
61 m_fileCount(0),
62 m_mainWindow(mainWindow),
63 m_topLayout(0),
64 m_urlNavigator(0),
65 m_iconsView(0),
66 m_detailsView(0),
67 m_filterBar(0),
68 m_statusBar(0),
69 m_dirModel(0),
70 m_dirLister(0),
71 m_proxyModel(0)
72 {
73 hide();
74 setFocusPolicy(Qt::StrongFocus);
75 m_topLayout = new QVBoxLayout(this);
76 m_topLayout->setSpacing(0);
77 m_topLayout->setMargin(0);
78
79 m_urlNavigator = new UrlNavigator(url, this);
80 connect(m_urlNavigator, SIGNAL(urlChanged(const KUrl&)),
81 this, SLOT(loadDirectory(const KUrl&)));
82
83 m_statusBar = new DolphinStatusBar(this);
84
85 m_dirLister = new DolphinDirLister();
86 m_dirLister->setAutoUpdate(true);
87 m_dirLister->setMainWindow(this);
88 m_dirLister->setShowingDotFiles(showHiddenFiles);
89 m_dirLister->setDelayedMimeTypes(true);
90
91 connect(m_dirLister, SIGNAL(clear()),
92 this, SLOT(updateStatusBar()));
93 connect(m_dirLister, SIGNAL(percent(int)),
94 this, SLOT(updateProgress(int)));
95 connect(m_dirLister, SIGNAL(deleteItem(KFileItem*)),
96 this, SLOT(updateStatusBar()));
97 connect(m_dirLister, SIGNAL(completed()),
98 this, SLOT(updateItemCount()));
99 connect(m_dirLister, SIGNAL(infoMessage(const QString&)),
100 this, SLOT(showInfoMessage(const QString&)));
101 connect(m_dirLister, SIGNAL(errorMessage(const QString&)),
102 this, SLOT(showErrorMessage(const QString&)));
103
104 m_dirModel = new KDirModel();
105 m_dirModel->setDirLister(m_dirLister);
106 m_dirModel->setDropsAllowed(KDirModel::DropOnDirectory);
107
108 m_proxyModel = new DolphinSortFilterProxyModel(this);
109 m_proxyModel->setSourceModel(m_dirModel);
110
111 createView();
112
113 m_iconSize = K3Icon::SizeMedium;
114
115 m_filterBar = new FilterBar(this);
116 m_filterBar->hide();
117 connect(m_filterBar, SIGNAL(filterChanged(const QString&)),
118 this, SLOT(changeNameFilter(const QString&)));
119 connect(m_filterBar, SIGNAL(closed()),
120 this, SLOT(closeFilterBar()));
121
122 m_topLayout->addWidget(m_urlNavigator);
123 m_topLayout->addWidget(itemView());
124 m_topLayout->addWidget(m_filterBar);
125 m_topLayout->addWidget(m_statusBar);
126
127 loadDirectory(m_urlNavigator->url());
128 }
129
130 DolphinView::~DolphinView()
131 {
132 delete m_dirLister;
133 m_dirLister = 0;
134 }
135
136 void DolphinView::setUrl(const KUrl& url)
137 {
138 m_urlNavigator->setUrl(url);
139 }
140
141 const KUrl& DolphinView::url() const
142 {
143 return m_urlNavigator->url();
144 }
145
146 void DolphinView::requestActivation()
147 {
148 mainWindow()->setActiveView(this);
149 }
150
151 bool DolphinView::isActive() const
152 {
153 return (mainWindow()->activeView() == this);
154 }
155
156 void DolphinView::setMode(Mode mode)
157 {
158 if (mode == m_mode) {
159 return; // the wished mode is already set
160 }
161
162 m_mode = mode;
163
164 ViewProperties props(m_urlNavigator->url());
165 props.setViewMode(m_mode);
166
167 createView();
168 startDirLister(m_urlNavigator->url());
169
170 emit modeChanged();
171 }
172
173 DolphinView::Mode DolphinView::mode() const
174 {
175 return m_mode;
176 }
177
178 void DolphinView::setShowPreview(bool show)
179 {
180 ViewProperties props(m_urlNavigator->url());
181 props.setShowPreview(show);
182
183 // TODO: wait until previews are possible with KFileItemDelegate
184
185 emit showPreviewChanged();
186 }
187
188 bool DolphinView::showPreview() const
189 {
190 // TODO: wait until previews are possible with KFileItemDelegate
191 return true;
192 }
193
194 void DolphinView::setShowHiddenFiles(bool show)
195 {
196 if (m_dirLister->showingDotFiles() == show) {
197 return;
198 }
199
200 ViewProperties props(m_urlNavigator->url());
201 props.setShowHiddenFiles(show);
202 props.save();
203
204 m_dirLister->setShowingDotFiles(show);
205
206 emit showHiddenFilesChanged();
207
208 reload();
209 }
210
211 bool DolphinView::showHiddenFiles() const
212 {
213 return m_dirLister->showingDotFiles();
214 }
215
216 void DolphinView::renameSelectedItems()
217 {
218 const KUrl::List urls = selectedUrls();
219 if (urls.count() > 1) {
220 // More than one item has been selected for renaming. Open
221 // a rename dialog and rename all items afterwards.
222 RenameDialog dialog(urls);
223 if (dialog.exec() == QDialog::Rejected) {
224 return;
225 }
226
227 DolphinView* view = mainWindow()->activeView();
228 const QString& newName = dialog.newName();
229 if (newName.isEmpty()) {
230 view->statusBar()->setMessage(i18n("The new item name is invalid."),
231 DolphinStatusBar::Error);
232 }
233 else {
234 // TODO: check how this can be integrated into KonqUndoManager/KonqOperations
235
236 //UndoManager& undoMan = UndoManager::instance();
237 //undoMan.beginMacro();
238
239 assert(newName.contains('#'));
240
241 const int urlsCount = urls.count();
242
243 // iterate through all selected items and rename them...
244 const int replaceIndex = newName.indexOf('#');
245 assert(replaceIndex >= 0);
246 for (int i = 0; i < urlsCount; ++i) {
247 const KUrl& source = urls[i];
248 QString number;
249 number.setNum(i + 1);
250
251 QString name(newName);
252 name.replace(replaceIndex, 1, number);
253
254 if (source.fileName() != name) {
255 KUrl dest(source.upUrl());
256 dest.addPath(name);
257
258 const bool destExists = KIO::NetAccess::exists(dest, false, view);
259 if (destExists) {
260 view->statusBar()->setMessage(i18n("Renaming failed (item '%1' already exists).",name),
261 DolphinStatusBar::Error);
262 break;
263 }
264 else if (KIO::NetAccess::file_move(source, dest)) {
265 // TODO: From the users point of view he executed one 'rename n files' operation,
266 // but internally we store it as n 'rename 1 file' operations for the undo mechanism.
267 //DolphinCommand command(DolphinCommand::Rename, source, dest);
268 //undoMan.addCommand(command);
269 }
270 }
271 }
272
273 //undoMan.endMacro();
274 }
275 }
276 else {
277 // Only one item has been selected for renaming. Use the custom
278 // renaming mechanism from the views.
279 assert(urls.count() == 1);
280 // TODO:
281 /*if (m_mode == DetailsView) {
282 Q3ListViewItem* item = m_iconsView->firstChild();
283 while (item != 0) {
284 if (item->isSelected()) {
285 m_iconsView->rename(item, DolphinDetailsView::NameColumn);
286 break;
287 }
288 item = item->nextSibling();
289 }
290 }
291 else {
292 KFileIconViewItem* item = static_cast<KFileIconViewItem*>(m_iconsView->firstItem());
293 while (item != 0) {
294 if (item->isSelected()) {
295 item->rename();
296 break;
297 }
298 item = static_cast<KFileIconViewItem*>(item->nextItem());
299 }
300 }*/
301 }
302 }
303
304 void DolphinView::selectAll()
305 {
306 selectAll(QItemSelectionModel::Select);
307 }
308
309 void DolphinView::invertSelection()
310 {
311 selectAll(QItemSelectionModel::Toggle);
312 }
313
314 DolphinStatusBar* DolphinView::statusBar() const
315 {
316 return m_statusBar;
317 }
318
319 int DolphinView::contentsX() const
320 {
321
322 return 0; //scrollView()->contentsX();
323 }
324
325 int DolphinView::contentsY() const
326 {
327 return 0; //scrollView()->contentsY();
328 }
329
330 void DolphinView::refreshSettings()
331 {
332 startDirLister(m_urlNavigator->url());
333 }
334
335 void DolphinView::emitRequestItemInfo(const KUrl& url)
336 {
337 emit requestItemInfo(url);
338 }
339
340 bool DolphinView::isFilterBarVisible() const
341 {
342 return m_filterBar->isVisible();
343 }
344
345 bool DolphinView::isUrlEditable() const
346 {
347 return m_urlNavigator->isUrlEditable();
348 }
349
350 void DolphinView::zoomIn()
351 {
352 //itemEffectsManager()->zoomIn();
353 }
354
355 void DolphinView::zoomOut()
356 {
357 //itemEffectsManager()->zoomOut();
358 }
359
360 bool DolphinView::isZoomInPossible() const
361 {
362 return false; //itemEffectsManager()->isZoomInPossible();
363 }
364
365 bool DolphinView::isZoomOutPossible() const
366 {
367 return false; //itemEffectsManager()->isZoomOutPossible();
368 }
369
370 void DolphinView::setSorting(Sorting sorting)
371 {
372 if (sorting != this->sorting()) {
373 ViewProperties props(url());
374 props.setSorting(sorting);
375
376 m_proxyModel->setSorting(sorting);
377
378 emit sortingChanged(sorting);
379 }
380 }
381
382 DolphinView::Sorting DolphinView::sorting() const
383 {
384 return m_proxyModel->sorting();
385 }
386
387 void DolphinView::setSortOrder(Qt::SortOrder order)
388 {
389 if (sortOrder() != order) {
390 ViewProperties props(url());
391 props.setSortOrder(order);
392
393 m_proxyModel->setSortOrder(order);
394
395 emit sortOrderChanged(order);
396 }
397 }
398
399 Qt::SortOrder DolphinView::sortOrder() const
400 {
401 return m_proxyModel->sortOrder();
402 }
403
404 void DolphinView::goBack()
405 {
406 m_urlNavigator->goBack();
407 }
408
409 void DolphinView::goForward()
410 {
411 m_urlNavigator->goForward();
412 }
413
414 void DolphinView::goUp()
415 {
416 m_urlNavigator->goUp();
417 }
418
419 void DolphinView::goHome()
420 {
421 m_urlNavigator->goHome();
422 }
423
424 void DolphinView::setUrlEditable(bool editable)
425 {
426 m_urlNavigator->editUrl(editable);
427 }
428
429 const QLinkedList<UrlNavigator::HistoryElem> DolphinView::urlHistory(int& index) const
430 {
431 return m_urlNavigator->history(index);
432 }
433
434 bool DolphinView::hasSelection() const
435 {
436 return itemView()->selectionModel()->hasSelection();
437 }
438
439 KFileItemList DolphinView::selectedItems() const
440 {
441 const QAbstractItemView* view = itemView();
442
443 // Our view has a selection, we will map them back to the DirModel
444 // and then fill the KFileItemList.
445 assert((view != 0) && (view->selectionModel() != 0));
446
447 const QItemSelection selection = m_proxyModel->mapSelectionToSource(view->selectionModel()->selection());
448 KFileItemList itemList;
449
450 const QModelIndexList indexList = selection.indexes();
451 QModelIndexList::const_iterator end = indexList.end();
452 for (QModelIndexList::const_iterator it = indexList.begin(); it != end; ++it) {
453 assert((*it).isValid());
454
455 KFileItem* item = m_dirModel->itemForIndex(*it);
456 if (item != 0) {
457 itemList.append(item);
458 }
459 }
460
461 return itemList;
462 }
463
464 KUrl::List DolphinView::selectedUrls() const
465 {
466 KUrl::List urls;
467
468 const KFileItemList list = selectedItems();
469 KFileItemList::const_iterator it = list.begin();
470 const KFileItemList::const_iterator end = list.end();
471 while (it != end) {
472 KFileItem* item = *it;
473 urls.append(item->url());
474 ++it;
475 }
476
477 return urls;
478 }
479
480 KFileItem* DolphinView::fileItem(const QModelIndex index) const
481 {
482 const QModelIndex dirModelIndex = m_proxyModel->mapToSource(index);
483 return m_dirModel->itemForIndex(dirModelIndex);
484 }
485
486 void DolphinView::openContextMenu(KFileItem* fileInfo, const QPoint& pos)
487 {
488 DolphinContextMenu contextMenu(this, fileInfo, pos);
489 contextMenu.open();
490 }
491
492 void DolphinView::rename(const KUrl& source, const QString& newName)
493 {
494 bool ok = false;
495
496 if (newName.isEmpty() || (source.fileName() == newName)) {
497 return;
498 }
499
500 KUrl dest(source.upUrl());
501 dest.addPath(newName);
502
503 const bool destExists = KIO::NetAccess::exists(dest,
504 false,
505 mainWindow()->activeView());
506 if (destExists) {
507 // the destination already exists, hence ask the user
508 // how to proceed...
509 KIO::RenameDialog renameDialog(this,
510 i18n("File Already Exists"),
511 source.path(),
512 dest.path(),
513 KIO::M_OVERWRITE);
514 switch (renameDialog.exec()) {
515 case KIO::R_OVERWRITE:
516 // the destination should be overwritten
517 ok = KIO::NetAccess::file_move(source, dest, -1, true);
518 break;
519
520 case KIO::R_RENAME: {
521 // a new name for the destination has been used
522 KUrl newDest(renameDialog.newDestUrl());
523 ok = KIO::NetAccess::file_move(source, newDest);
524 break;
525 }
526
527 default:
528 // the renaming operation has been canceled
529 reload();
530 return;
531 }
532 }
533 else {
534 // no destination exists, hence just move the file to
535 // do the renaming
536 ok = KIO::NetAccess::file_move(source, dest);
537 }
538
539 const QString destFileName = dest.fileName();
540 if (ok) {
541 m_statusBar->setMessage(i18n("Renamed file '%1' to '%2'.",source.fileName(), destFileName),
542 DolphinStatusBar::OperationCompleted);
543
544 KonqOperations::rename(this, source, destFileName);
545 }
546 else {
547 m_statusBar->setMessage(i18n("Renaming of file '%1' to '%2' failed.",source.fileName(), destFileName),
548 DolphinStatusBar::Error);
549 reload();
550 }
551 }
552
553 void DolphinView::reload()
554 {
555 startDirLister(m_urlNavigator->url(), true);
556 }
557
558 void DolphinView::slotUrlListDropped(QDropEvent* /* event */,
559 const KUrl::List& urls,
560 const KUrl& url)
561 {
562 KUrl destination(url);
563 if (destination.isEmpty()) {
564 destination = m_urlNavigator->url();
565 }
566 else {
567 // Check whether the destination Url is a directory. If this is not the
568 // case, use the navigator Url as destination (otherwise the destination,
569 // which represents a file, would be replaced by a copy- or move-operation).
570 KFileItem fileItem(KFileItem::Unknown, KFileItem::Unknown, destination);
571 if (!fileItem.isDir()) {
572 destination = m_urlNavigator->url();
573 }
574 }
575
576 mainWindow()->dropUrls(urls, destination);
577 }
578
579 void DolphinView::mouseReleaseEvent(QMouseEvent* event)
580 {
581 QWidget::mouseReleaseEvent(event);
582 mainWindow()->setActiveView(this);
583 }
584
585 DolphinMainWindow* DolphinView::mainWindow() const
586 {
587 return m_mainWindow;
588 }
589
590 void DolphinView::loadDirectory(const KUrl& url)
591 {
592 const ViewProperties props(url);
593
594 const Mode mode = props.viewMode();
595 if (m_mode != mode) {
596 m_mode = mode;
597 createView();
598 emit modeChanged();
599 }
600
601 const bool showHiddenFiles = props.showHiddenFiles();
602 if (showHiddenFiles != m_dirLister->showingDotFiles()) {
603 m_dirLister->setShowingDotFiles(showHiddenFiles);
604 emit showHiddenFilesChanged();
605 }
606
607 const DolphinView::Sorting sorting = props.sorting();
608 if (sorting != m_proxyModel->sorting()) {
609 m_proxyModel->setSorting(sorting);
610 emit sortingChanged(sorting);
611 }
612
613 const Qt::SortOrder sortOrder = props.sortOrder();
614 if (sortOrder != m_proxyModel->sortOrder()) {
615 m_proxyModel->setSortOrder(sortOrder);
616 emit sortOrderChanged(sortOrder);
617 }
618
619 // TODO: handle previews (props.showPreview())
620
621 startDirLister(url);
622 emit urlChanged(url);
623 }
624
625 void DolphinView::triggerItem(const QModelIndex& index)
626 {
627 KFileItem* item = m_dirModel->itemForIndex(m_proxyModel->mapToSource(index));
628 if (item == 0) {
629 return;
630 }
631
632 if (item->isDir()) {
633 // Prefer the local path over the Url. This assures that the
634 // volume space information is correct. Assuming that the Url is media:/sda1,
635 // and the local path is /windows/C: For the Url the space info is related
636 // to the root partition (and hence wrong) and for the local path the space
637 // info is related to the windows partition (-> correct).
638 const QString localPath(item->localPath());
639 if (localPath.isEmpty()) {
640 setUrl(item->url());
641 }
642 else {
643 setUrl(KUrl(localPath));
644 }
645 }
646 else {
647 item->run();
648 }
649 }
650
651 void DolphinView::updateProgress(int percent)
652 {
653 if (m_showProgress) {
654 m_statusBar->setProgress(percent);
655 }
656 }
657
658 void DolphinView::updateItemCount()
659 {
660 if (m_showProgress) {
661 m_statusBar->setProgressText(QString::null);
662 m_statusBar->setProgress(100);
663 m_showProgress = false;
664 }
665
666 KFileItemList items(m_dirLister->items());
667 KFileItemList::const_iterator it = items.begin();
668 const KFileItemList::const_iterator end = items.end();
669
670 m_fileCount = 0;
671 m_folderCount = 0;
672
673 while (it != end) {
674 KFileItem* item = *it;
675 if (item->isDir()) {
676 ++m_folderCount;
677 }
678 else {
679 ++m_fileCount;
680 }
681 ++it;
682 }
683
684 updateStatusBar();
685 }
686
687 void DolphinView::showInfoMessage(const QString& msg)
688 {
689 m_statusBar->setMessage(msg, DolphinStatusBar::Information);
690 }
691
692 void DolphinView::showErrorMessage(const QString& msg)
693 {
694 m_statusBar->setMessage(msg, DolphinStatusBar::Error);
695 }
696
697 void DolphinView::emitSelectionChangedSignal()
698 {
699 emit selectionChanged();
700 }
701
702 void DolphinView::closeFilterBar()
703 {
704 m_filterBar->hide();
705 emit showFilterBarChanged(false);
706 }
707
708 void DolphinView::startDirLister(const KUrl& url, bool reload)
709 {
710 if (!url.isValid()) {
711 const QString location(url.pathOrUrl());
712 if (location.isEmpty()) {
713 m_statusBar->setMessage(i18n("The location is empty."), DolphinStatusBar::Error);
714 }
715 else {
716 m_statusBar->setMessage(i18n("The location '%1' is invalid.",location),
717 DolphinStatusBar::Error);
718 }
719 return;
720 }
721
722 // Only show the directory loading progress if the status bar does
723 // not contain another progress information. This means that
724 // the directory loading progress information has the lowest priority.
725 const QString progressText(m_statusBar->progressText());
726 m_showProgress = progressText.isEmpty() ||
727 (progressText == i18n("Loading directory..."));
728 if (m_showProgress) {
729 m_statusBar->setProgressText(i18n("Loading directory..."));
730 m_statusBar->setProgress(0);
731 }
732
733 m_dirLister->stop();
734 m_dirLister->openUrl(url, false, reload);
735 }
736
737 QString DolphinView::defaultStatusBarText() const
738 {
739 // TODO: the following code is not suitable for languages where multiple forms
740 // of plurals are given (e. g. in Poland three forms of plurals exist).
741 const int itemCount = m_folderCount + m_fileCount;
742
743 QString text;
744 if (itemCount == 1) {
745 text = i18n("1 Item");
746 }
747 else {
748 text = i18n("%1 Items",itemCount);
749 }
750
751 text += " (";
752
753 if (m_folderCount == 1) {
754 text += i18n("1 Folder");
755 }
756 else {
757 text += i18n("%1 Folders",m_folderCount);
758 }
759
760 text += ", ";
761
762 if (m_fileCount == 1) {
763 text += i18n("1 File");
764 }
765 else {
766 text += i18n("%1 Files",m_fileCount);
767 }
768
769 text += ")";
770
771 return text;
772 }
773
774 QString DolphinView::selectionStatusBarText() const
775 {
776 // TODO: the following code is not suitable for languages where multiple forms
777 // of plurals are given (e. g. in Poland three forms of plurals exist).
778 QString text;
779 const KFileItemList list = selectedItems();
780 if (list.isEmpty()) {
781 // TODO: assert(!list.isEmpty()) should be used, as this method is only invoked if
782 // DolphinView::hasSelection() is true. Inconsistent behavior?
783 return QString();
784 }
785
786 int fileCount = 0;
787 int folderCount = 0;
788 KIO::filesize_t byteSize = 0;
789 KFileItemList::const_iterator it = list.begin();
790 const KFileItemList::const_iterator end = list.end();
791 while (it != end){
792 KFileItem* item = *it;
793 if (item->isDir()) {
794 ++folderCount;
795 }
796 else {
797 ++fileCount;
798 byteSize += item->size();
799 }
800 ++it;
801 }
802
803 if (folderCount == 1) {
804 text = i18n("1 Folder selected");
805 }
806 else if (folderCount > 1) {
807 text = i18n("%1 Folders selected",folderCount);
808 }
809
810 if ((fileCount > 0) && (folderCount > 0)) {
811 text += ", ";
812 }
813
814 const QString sizeText(KIO::convertSize(byteSize));
815 if (fileCount == 1) {
816 text += i18n("1 File selected (%1)",sizeText);
817 }
818 else if (fileCount > 1) {
819 text += i18n("%1 Files selected (%1)",fileCount,sizeText);
820 }
821
822 return text;
823 }
824
825 void DolphinView::showFilterBar(bool show)
826 {
827 assert(m_filterBar != 0);
828 if (show) {
829 m_filterBar->show();
830 }
831 else {
832 m_filterBar->hide();
833 }
834 }
835
836 void DolphinView::declareViewActive()
837 {
838 mainWindow()->setActiveView( this );
839 }
840
841 void DolphinView::updateStatusBar()
842 {
843 // As the item count information is less important
844 // in comparison with other messages, it should only
845 // be shown if:
846 // - the status bar is empty or
847 // - shows already the item count information or
848 // - shows only a not very important information
849 // - if any progress is given don't show the item count info at all
850 const QString msg(m_statusBar->message());
851 const bool updateStatusBarMsg = (msg.isEmpty() ||
852 (msg == m_statusBar->defaultText()) ||
853 (m_statusBar->type() == DolphinStatusBar::Information)) &&
854 (m_statusBar->progress() == 100);
855
856 const QString text(hasSelection() ? selectionStatusBarText() : defaultStatusBarText());
857 m_statusBar->setDefaultText(text);
858
859 if (updateStatusBarMsg) {
860 m_statusBar->setMessage(text, DolphinStatusBar::Default);
861 }
862 }
863
864 void DolphinView::changeNameFilter(const QString& nameFilter)
865 {
866 // The name filter of KDirLister does a 'hard' filtering, which
867 // means that only the items are shown where the names match
868 // exactly the filter. This is non-transparent for the user, which
869 // just wants to have a 'soft' filtering: does the name contain
870 // the filter string?
871 QString adjustedFilter(nameFilter);
872 adjustedFilter.insert(0, '*');
873 adjustedFilter.append('*');
874
875 // Use the ProxyModel to filter:
876 // This code is #ifdefed as setNameFilter behaves
877 // slightly different than the QSortFilterProxyModel
878 // as it will not remove directories. I will ask
879 // our beloved usability experts for input
880 // -- z.
881 #if 0
882 m_dirLister->setNameFilter(adjustedFilter);
883 m_dirLister->emitChanges();
884 #else
885 m_proxyModel->setFilterRegExp( nameFilter );
886 #endif
887 }
888
889 void DolphinView::createView()
890 {
891 // delete current view
892 QAbstractItemView* view = itemView();
893 if (view != 0) {
894 m_topLayout->remove(view);
895 view->close();
896 view->deleteLater();
897 m_iconsView = 0;
898 m_detailsView = 0;
899 }
900
901 assert(m_iconsView == 0);
902 assert(m_detailsView == 0);
903
904 // ... and recreate it representing the current mode
905 switch (m_mode) {
906 case IconsView:
907 m_iconsView = new DolphinIconsView(this);
908 m_iconsView->setViewMode(QListView::IconMode);
909 m_iconsView->setSpacing(32);
910 view = m_iconsView;
911 // TODO: read out view settings
912 break;
913
914 case DetailsView:
915 m_detailsView = new DolphinDetailsView(this);
916 view = m_detailsView;
917 // TODO: read out view settings
918 break;
919 }
920
921 view->setModel(m_proxyModel);
922
923 view->setSelectionMode(QAbstractItemView::ExtendedSelection);
924
925 KFileItemDelegate* delegate = new KFileItemDelegate(this);
926 delegate->setAdditionalInformation(KFileItemDelegate::FriendlyMimeType);
927 view->setItemDelegate(delegate);
928
929 new KMimeTypeResolver(view, m_dirModel);
930
931 connect(view, SIGNAL(clicked(const QModelIndex&)),
932 this, SLOT(triggerItem(const QModelIndex&)));
933 connect(view->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
934 this, SLOT(emitSelectionChangedSignal()));
935
936 m_topLayout->insertWidget(1, view);
937 }
938
939 int DolphinView::columnIndex(Sorting sorting) const
940 {
941 int index = 0;
942 switch (sorting) {
943 case SortByName: index = KDirModel::Name; break;
944 case SortBySize: index = KDirModel::Size; break;
945 case SortByDate: index = KDirModel::ModifiedTime; break;
946 default: assert(false);
947 }
948 return index;
949 }
950
951 void DolphinView::selectAll(QItemSelectionModel::SelectionFlags flags)
952 {
953 QItemSelectionModel* selectionModel = itemView()->selectionModel();
954 const QAbstractItemModel* itemModel = selectionModel->model();
955
956 const QModelIndex topLeft = itemModel->index(0, 0);
957 const QModelIndex bottomRight = itemModel->index(itemModel->rowCount() - 1,
958 itemModel->columnCount() - 1);
959
960 QItemSelection selection(topLeft, bottomRight);
961 selectionModel->select(selection, flags);
962 }
963
964 QAbstractItemView* DolphinView::itemView() const
965 {
966 assert((m_iconsView == 0) || (m_detailsView == 0));
967 if (m_detailsView != 0) {
968 return m_detailsView;
969 }
970 return m_iconsView;
971 }
972
973 #include "dolphinview.moc"