]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinview.cpp
restore the x- and y-position of the view if the current view is part of the history
[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(closeRequest()),
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 itemView()->horizontalScrollBar()->value();
323 }
324
325 int DolphinView::contentsY() const
326 {
327 return itemView()->verticalScrollBar()->value();
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::declareViewActive()
559 {
560 mainWindow()->setActiveView( this );
561 }
562
563 void DolphinView::mouseReleaseEvent(QMouseEvent* event)
564 {
565 QWidget::mouseReleaseEvent(event);
566 mainWindow()->setActiveView(this);
567 }
568
569 DolphinMainWindow* DolphinView::mainWindow() const
570 {
571 return m_mainWindow;
572 }
573
574 void DolphinView::loadDirectory(const KUrl& url)
575 {
576 const ViewProperties props(url);
577
578 const Mode mode = props.viewMode();
579 if (m_mode != mode) {
580 m_mode = mode;
581 createView();
582 emit modeChanged();
583 }
584
585 const bool showHiddenFiles = props.showHiddenFiles();
586 if (showHiddenFiles != m_dirLister->showingDotFiles()) {
587 m_dirLister->setShowingDotFiles(showHiddenFiles);
588 emit showHiddenFilesChanged();
589 }
590
591 const DolphinView::Sorting sorting = props.sorting();
592 if (sorting != m_proxyModel->sorting()) {
593 m_proxyModel->setSorting(sorting);
594 emit sortingChanged(sorting);
595 }
596
597 const Qt::SortOrder sortOrder = props.sortOrder();
598 if (sortOrder != m_proxyModel->sortOrder()) {
599 m_proxyModel->setSortOrder(sortOrder);
600 emit sortOrderChanged(sortOrder);
601 }
602
603 // TODO: handle previews (props.showPreview())
604
605 startDirLister(url);
606 emit urlChanged(url);
607
608 m_statusBar->clear();
609 }
610
611 void DolphinView::triggerItem(const QModelIndex& index)
612 {
613 KFileItem* item = m_dirModel->itemForIndex(m_proxyModel->mapToSource(index));
614 if (item == 0) {
615 return;
616 }
617
618 if (item->isDir()) {
619 // Prefer the local path over the Url. This assures that the
620 // volume space information is correct. Assuming that the Url is media:/sda1,
621 // and the local path is /windows/C: For the Url the space info is related
622 // to the root partition (and hence wrong) and for the local path the space
623 // info is related to the windows partition (-> correct).
624 const QString localPath(item->localPath());
625 if (localPath.isEmpty()) {
626 setUrl(item->url());
627 }
628 else {
629 setUrl(KUrl(localPath));
630 }
631 }
632 else {
633 item->run();
634 }
635 }
636
637 void DolphinView::updateProgress(int percent)
638 {
639 if (m_showProgress) {
640 m_statusBar->setProgress(percent);
641 }
642 }
643
644 void DolphinView::updateItemCount()
645 {
646 if (m_showProgress) {
647 m_statusBar->setProgressText(QString::null);
648 m_statusBar->setProgress(100);
649 m_showProgress = false;
650 }
651
652 KFileItemList items(m_dirLister->items());
653 KFileItemList::const_iterator it = items.begin();
654 const KFileItemList::const_iterator end = items.end();
655
656 m_fileCount = 0;
657 m_folderCount = 0;
658
659 while (it != end) {
660 KFileItem* item = *it;
661 if (item->isDir()) {
662 ++m_folderCount;
663 }
664 else {
665 ++m_fileCount;
666 }
667 ++it;
668 }
669
670 updateStatusBar();
671
672 QTimer::singleShot(0, this, SLOT(restoreContentsPos()));
673 }
674
675 void DolphinView::restoreContentsPos()
676 {
677 int index = 0;
678 const QLinkedList<UrlNavigator::HistoryElem> history = urlHistory(index);
679 if (!history.isEmpty()) {
680 QAbstractItemView* view = itemView();
681 // TODO: view->setCurrentItem(history[index].currentFileName());
682
683 QLinkedList<UrlNavigator::HistoryElem>::const_iterator it = history.begin();
684 it += index;
685 view->horizontalScrollBar()->setValue((*it).contentsX());
686 view->verticalScrollBar()->setValue((*it).contentsY());
687 }
688 }
689
690 void DolphinView::showInfoMessage(const QString& msg)
691 {
692 m_statusBar->setMessage(msg, DolphinStatusBar::Information);
693 }
694
695 void DolphinView::showErrorMessage(const QString& msg)
696 {
697 m_statusBar->setMessage(msg, DolphinStatusBar::Error);
698 }
699
700 void DolphinView::emitSelectionChangedSignal()
701 {
702 emit selectionChanged();
703 }
704
705 void DolphinView::closeFilterBar()
706 {
707 m_filterBar->hide();
708 emit showFilterBarChanged(false);
709 }
710
711 void DolphinView::startDirLister(const KUrl& url, bool reload)
712 {
713 if (!url.isValid()) {
714 const QString location(url.pathOrUrl());
715 if (location.isEmpty()) {
716 m_statusBar->setMessage(i18n("The location is empty."), DolphinStatusBar::Error);
717 }
718 else {
719 m_statusBar->setMessage(i18n("The location '%1' is invalid.",location),
720 DolphinStatusBar::Error);
721 }
722 return;
723 }
724
725 // Only show the directory loading progress if the status bar does
726 // not contain another progress information. This means that
727 // the directory loading progress information has the lowest priority.
728 const QString progressText(m_statusBar->progressText());
729 m_showProgress = progressText.isEmpty() ||
730 (progressText == i18n("Loading directory..."));
731 if (m_showProgress) {
732 m_statusBar->setProgressText(i18n("Loading directory..."));
733 m_statusBar->setProgress(0);
734 }
735
736 m_dirLister->stop();
737 m_dirLister->openUrl(url, false, reload);
738 }
739
740 QString DolphinView::defaultStatusBarText() const
741 {
742 // TODO: the following code is not suitable for languages where multiple forms
743 // of plurals are given (e. g. in Poland three forms of plurals exist).
744 const int itemCount = m_folderCount + m_fileCount;
745
746 QString text;
747 if (itemCount == 1) {
748 text = i18n("1 Item");
749 }
750 else {
751 text = i18n("%1 Items",itemCount);
752 }
753
754 text += " (";
755
756 if (m_folderCount == 1) {
757 text += i18n("1 Folder");
758 }
759 else {
760 text += i18n("%1 Folders",m_folderCount);
761 }
762
763 text += ", ";
764
765 if (m_fileCount == 1) {
766 text += i18n("1 File");
767 }
768 else {
769 text += i18n("%1 Files",m_fileCount);
770 }
771
772 text += ")";
773
774 return text;
775 }
776
777 QString DolphinView::selectionStatusBarText() const
778 {
779 // TODO: the following code is not suitable for languages where multiple forms
780 // of plurals are given (e. g. in Poland three forms of plurals exist).
781 QString text;
782 const KFileItemList list = selectedItems();
783 if (list.isEmpty()) {
784 // TODO: assert(!list.isEmpty()) should be used, as this method is only invoked if
785 // DolphinView::hasSelection() is true. Inconsistent behavior?
786 return QString();
787 }
788
789 int fileCount = 0;
790 int folderCount = 0;
791 KIO::filesize_t byteSize = 0;
792 KFileItemList::const_iterator it = list.begin();
793 const KFileItemList::const_iterator end = list.end();
794 while (it != end){
795 KFileItem* item = *it;
796 if (item->isDir()) {
797 ++folderCount;
798 }
799 else {
800 ++fileCount;
801 byteSize += item->size();
802 }
803 ++it;
804 }
805
806 if (folderCount == 1) {
807 text = i18n("1 Folder selected");
808 }
809 else if (folderCount > 1) {
810 text = i18n("%1 Folders selected",folderCount);
811 }
812
813 if ((fileCount > 0) && (folderCount > 0)) {
814 text += ", ";
815 }
816
817 const QString sizeText(KIO::convertSize(byteSize));
818 if (fileCount == 1) {
819 text += i18n("1 File selected (%1)",sizeText);
820 }
821 else if (fileCount > 1) {
822 text += i18n("%1 Files selected (%1)",fileCount,sizeText);
823 }
824
825 return text;
826 }
827
828 void DolphinView::showFilterBar(bool show)
829 {
830 assert(m_filterBar != 0);
831 if (show) {
832 m_filterBar->show();
833 }
834 else {
835 m_filterBar->hide();
836 }
837 }
838
839 void DolphinView::updateStatusBar()
840 {
841 // As the item count information is less important
842 // in comparison with other messages, it should only
843 // be shown if:
844 // - the status bar is empty or
845 // - shows already the item count information or
846 // - shows only a not very important information
847 // - if any progress is given don't show the item count info at all
848 const QString msg(m_statusBar->message());
849 const bool updateStatusBarMsg = (msg.isEmpty() ||
850 (msg == m_statusBar->defaultText()) ||
851 (m_statusBar->type() == DolphinStatusBar::Information)) &&
852 (m_statusBar->progress() == 100);
853
854 const QString text(hasSelection() ? selectionStatusBarText() : defaultStatusBarText());
855 m_statusBar->setDefaultText(text);
856
857 if (updateStatusBarMsg) {
858 m_statusBar->setMessage(text, DolphinStatusBar::Default);
859 }
860 }
861
862 void DolphinView::changeNameFilter(const QString& nameFilter)
863 {
864 // The name filter of KDirLister does a 'hard' filtering, which
865 // means that only the items are shown where the names match
866 // exactly the filter. This is non-transparent for the user, which
867 // just wants to have a 'soft' filtering: does the name contain
868 // the filter string?
869 QString adjustedFilter(nameFilter);
870 adjustedFilter.insert(0, '*');
871 adjustedFilter.append('*');
872
873 // Use the ProxyModel to filter:
874 // This code is #ifdefed as setNameFilter behaves
875 // slightly different than the QSortFilterProxyModel
876 // as it will not remove directories. I will ask
877 // our beloved usability experts for input
878 // -- z.
879 #if 0
880 m_dirLister->setNameFilter(adjustedFilter);
881 m_dirLister->emitChanges();
882 #else
883 m_proxyModel->setFilterRegExp( nameFilter );
884 #endif
885 }
886
887 void DolphinView::createView()
888 {
889 // delete current view
890 QAbstractItemView* view = itemView();
891 if (view != 0) {
892 m_topLayout->removeWidget(view);
893 view->close();
894 view->deleteLater();
895 m_iconsView = 0;
896 m_detailsView = 0;
897 }
898
899 assert(m_iconsView == 0);
900 assert(m_detailsView == 0);
901
902 // ... and recreate it representing the current mode
903 switch (m_mode) {
904 case IconsView:
905 m_iconsView = new DolphinIconsView(this);
906 m_iconsView->setViewMode(QListView::IconMode);
907 m_iconsView->setSpacing(32);
908 view = m_iconsView;
909 // TODO: read out view settings
910 break;
911
912 case DetailsView:
913 m_detailsView = new DolphinDetailsView(this);
914 view = m_detailsView;
915 // TODO: read out view settings
916 break;
917 }
918
919 view->setModel(m_proxyModel);
920
921 view->setSelectionMode(QAbstractItemView::ExtendedSelection);
922
923 KFileItemDelegate* delegate = new KFileItemDelegate(this);
924 delegate->setAdditionalInformation(KFileItemDelegate::FriendlyMimeType);
925 view->setItemDelegate(delegate);
926
927 new KMimeTypeResolver(view, m_dirModel);
928
929 connect(view, SIGNAL(clicked(const QModelIndex&)),
930 this, SLOT(triggerItem(const QModelIndex&)));
931 connect(view->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
932 this, SLOT(emitSelectionChangedSignal()));
933
934 m_topLayout->insertWidget(1, view);
935 }
936
937 int DolphinView::columnIndex(Sorting sorting) const
938 {
939 int index = 0;
940 switch (sorting) {
941 case SortByName: index = KDirModel::Name; break;
942 case SortBySize: index = KDirModel::Size; break;
943 case SortByDate: index = KDirModel::ModifiedTime; break;
944 default: assert(false);
945 }
946 return index;
947 }
948
949 void DolphinView::selectAll(QItemSelectionModel::SelectionFlags flags)
950 {
951 QItemSelectionModel* selectionModel = itemView()->selectionModel();
952 const QAbstractItemModel* itemModel = selectionModel->model();
953
954 const QModelIndex topLeft = itemModel->index(0, 0);
955 const QModelIndex bottomRight = itemModel->index(itemModel->rowCount() - 1,
956 itemModel->columnCount() - 1);
957
958 QItemSelection selection(topLeft, bottomRight);
959 selectionModel->select(selection, flags);
960 }
961
962 QAbstractItemView* DolphinView::itemView() const
963 {
964 assert((m_iconsView == 0) || (m_detailsView == 0));
965 if (m_detailsView != 0) {
966 return m_detailsView;
967 }
968 return m_iconsView;
969 }
970
971 #include "dolphinview.moc"