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