]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/dolphinview.cpp
SVN_SILENT made messages (.desktop file)
[dolphin.git] / src / views / dolphinview.cpp
1 /***************************************************************************
2 * Copyright (C) 2006-2009 by Peter Penz <peter.penz19@gmail.com> *
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 <QAbstractItemView>
24 #include <QApplication>
25 #include <QClipboard>
26 #include <QKeyEvent>
27 #include <QItemSelection>
28 #include <QBoxLayout>
29 #include <QTimer>
30 #include <QScrollBar>
31
32 #include <KActionCollection>
33 #include <KColorScheme>
34 #include <KDirLister>
35 #include <KIconEffect>
36 #include <KFileItem>
37 #include <KFileItemListProperties>
38 #include <KLocale>
39 #include <KIO/DeleteJob>
40 #include <KIO/NetAccess>
41 #include <KIO/PreviewJob>
42 #include <KJob>
43 #include <KMenu>
44 #include <KMessageBox>
45 #include <konq_fileitemcapabilities.h>
46 #include <konq_operations.h>
47 #include <konqmimedata.h>
48 #include <KToggleAction>
49 #include <KUrl>
50
51 #include "additionalinfoaccessor.h"
52 #include "dolphindirlister.h"
53 #include "dolphinmodel.h"
54 #include "dolphincolumnviewcontainer.h"
55 #include "dolphinviewcontroller.h"
56 #include "dolphindetailsview.h"
57 #include "dolphinfileitemdelegate.h"
58 #include "dolphinnewfilemenuobserver.h"
59 #include "dolphinsortfilterproxymodel.h"
60 #include "dolphin_detailsmodesettings.h"
61 #include "dolphiniconsview.h"
62 #include "dolphin_generalsettings.h"
63 #include "draganddrophelper.h"
64 #include "renamedialog.h"
65 #include "settings/dolphinsettings.h"
66 #include "viewmodecontroller.h"
67 #include "viewproperties.h"
68 #include "zoomlevelinfo.h"
69 #include "dolphindetailsviewexpander.h"
70
71 DolphinView::DolphinView(const KUrl& url, QWidget* parent) :
72 QWidget(parent),
73 m_active(true),
74 m_showPreview(false),
75 m_storedCategorizedSorting(false),
76 m_tabsForFiles(false),
77 m_isContextMenuOpen(false),
78 m_assureVisibleCurrentIndex(false),
79 m_expanderActive(false),
80 m_isFolderWritable(true),
81 m_mode(DolphinView::IconsView),
82 m_topLayout(0),
83 m_dolphinViewController(0),
84 m_viewModeController(0),
85 m_viewAccessor(),
86 m_selectionChangedTimer(0),
87 m_activeItemUrl(),
88 m_restoredContentsPosition(),
89 m_createdItemUrl(),
90 m_selectedItems(),
91 m_newFileNames()
92 {
93 m_topLayout = new QVBoxLayout(this);
94 m_topLayout->setSpacing(0);
95 m_topLayout->setMargin(0);
96
97 m_dolphinViewController = new DolphinViewController(this);
98
99 m_viewModeController = new ViewModeController(this);
100 m_viewModeController->setUrl(url);
101
102 connect(m_viewModeController, SIGNAL(urlChanged(const KUrl&)),
103 this, SIGNAL(urlChanged(const KUrl&)));
104
105 connect(m_dolphinViewController, SIGNAL(requestContextMenu(const QPoint&, const QList<QAction*>&)),
106 this, SLOT(openContextMenu(const QPoint&, const QList<QAction*>&)));
107 connect(m_dolphinViewController, SIGNAL(urlsDropped(const KFileItem&, const KUrl&, QDropEvent*)),
108 this, SLOT(dropUrls(const KFileItem&, const KUrl&, QDropEvent*)));
109 connect(m_dolphinViewController, SIGNAL(sortingChanged(DolphinView::Sorting)),
110 this, SLOT(updateSorting(DolphinView::Sorting)));
111 connect(m_dolphinViewController, SIGNAL(sortOrderChanged(Qt::SortOrder)),
112 this, SLOT(updateSortOrder(Qt::SortOrder)));
113 connect(m_dolphinViewController, SIGNAL(sortFoldersFirstChanged(bool)),
114 this, SLOT(updateSortFoldersFirst(bool)));
115 connect(m_dolphinViewController, SIGNAL(additionalInfoChanged(const KFileItemDelegate::InformationList&)),
116 this, SLOT(updateAdditionalInfo(const KFileItemDelegate::InformationList&)));
117 connect(m_dolphinViewController, SIGNAL(itemTriggered(const KFileItem&)),
118 this, SLOT(triggerItem(const KFileItem&)));
119 connect(m_dolphinViewController, SIGNAL(tabRequested(const KUrl&)),
120 this, SIGNAL(tabRequested(const KUrl&)));
121 connect(m_dolphinViewController, SIGNAL(activated()),
122 this, SLOT(activate()));
123 connect(m_dolphinViewController, SIGNAL(itemEntered(const KFileItem&)),
124 this, SLOT(showHoverInformation(const KFileItem&)));
125 connect(m_dolphinViewController, SIGNAL(viewportEntered()),
126 this, SLOT(clearHoverInformation()));
127 connect(m_dolphinViewController, SIGNAL(urlChangeRequested(KUrl)),
128 this, SLOT(slotUrlChangeRequested(KUrl)));
129
130 // When a new item has been created by the "Create New..." menu, the item should
131 // get selected and it must be assured that the item will get visible. As the
132 // creation is done asynchronously, several signals must be checked:
133 connect(&DolphinNewFileMenuObserver::instance(), SIGNAL(itemCreated(const KUrl&)),
134 this, SLOT(observeCreatedItem(const KUrl&)));
135
136 m_selectionChangedTimer = new QTimer(this);
137 m_selectionChangedTimer->setSingleShot(true);
138 m_selectionChangedTimer->setInterval(300);
139 connect(m_selectionChangedTimer, SIGNAL(timeout()),
140 this, SLOT(emitSelectionChangedSignal()));
141
142 applyViewProperties();
143 m_topLayout->addWidget(m_viewAccessor.layoutTarget());
144 }
145
146 DolphinView::~DolphinView()
147 {
148 }
149
150 KUrl DolphinView::url() const
151 {
152 return m_viewModeController->url();
153 }
154
155 KUrl DolphinView::rootUrl() const
156 {
157 const KUrl viewUrl = url();
158 const KUrl root = m_viewAccessor.rootUrl();
159 if (root.isEmpty() || !root.isParentOf(viewUrl)) {
160 return viewUrl;
161 }
162 return root;
163 }
164
165 void DolphinView::setActive(bool active)
166 {
167 if (active == m_active) {
168 return;
169 }
170
171 m_active = active;
172
173 QColor color = KColorScheme(QPalette::Active, KColorScheme::View).background().color();
174 if (!active) {
175 color.setAlpha(150);
176 }
177
178 QWidget* viewport = m_viewAccessor.itemView()->viewport();
179 QPalette palette;
180 palette.setColor(viewport->backgroundRole(), color);
181 viewport->setPalette(palette);
182
183 update();
184
185 if (active) {
186 m_viewAccessor.itemView()->setFocus();
187 emit activated();
188 emitSelectionChangedSignal();
189 emit writeStateChanged(m_isFolderWritable);
190 }
191
192 m_viewModeController->indicateActivationChange(active);
193 }
194
195 bool DolphinView::isActive() const
196 {
197 return m_active;
198 }
199
200 void DolphinView::setMode(Mode mode)
201 {
202 if (mode == m_mode) {
203 return; // the wished mode is already set
204 }
205
206 const int oldZoomLevel = m_viewModeController->zoomLevel();
207 m_mode = mode;
208
209 // remember the currently selected items, so that they will
210 // be restored after reloading the directory
211 m_selectedItems = selectedItems();
212
213 const bool hasFocus = m_viewAccessor.itemView()->hasFocus();
214 deleteView();
215
216 const KUrl viewPropsUrl = rootUrl();
217 ViewProperties props(viewPropsUrl);
218 props.setViewMode(m_mode);
219 createView();
220
221 if (hasFocus) {
222 m_viewAccessor.itemView()->setFocus();
223 }
224
225 // the file item delegate has been recreated, apply the current
226 // additional information manually
227 const KFileItemDelegate::InformationList infoList = props.additionalInfo();
228 m_viewAccessor.itemDelegate()->setShowInformation(infoList);
229 emit additionalInfoChanged();
230
231 // Not all view modes support categorized sorting. Adjust the sorting model
232 // if changing the view mode results in a change of the categorized sorting
233 // capabilities.
234 m_storedCategorizedSorting = props.categorizedSorting();
235 const bool categorized = m_storedCategorizedSorting && supportsCategorizedSorting();
236 if (categorized != m_viewAccessor.proxyModel()->isCategorizedModel()) {
237 m_viewAccessor.proxyModel()->setCategorizedModel(categorized);
238 emit categorizedSortingChanged();
239 }
240
241 emit modeChanged();
242
243 updateZoomLevel(oldZoomLevel);
244 loadDirectory(viewPropsUrl);
245 }
246
247 DolphinView::Mode DolphinView::mode() const
248 {
249 return m_mode;
250 }
251
252 bool DolphinView::showPreview() const
253 {
254 return m_showPreview;
255 }
256
257 bool DolphinView::showHiddenFiles() const
258 {
259 return m_viewAccessor.dirLister()->showingDotFiles();
260 }
261
262 bool DolphinView::categorizedSorting() const
263 {
264 // If all view modes would support categorized sorting, returning
265 // m_viewAccessor.proxyModel()->isCategorizedModel() would be the way to go. As
266 // currently only the icons view supports caterized sorting, we remember
267 // the stored view properties state in m_storedCategorizedSorting and
268 // return this state. The application takes care to disable the corresponding
269 // checkbox by checking DolphinView::supportsCategorizedSorting() to indicate
270 // that this setting is not applied to the current view mode.
271 return m_storedCategorizedSorting;
272 }
273
274 bool DolphinView::supportsCategorizedSorting() const
275 {
276 return m_viewAccessor.supportsCategorizedSorting();
277 }
278
279 KFileItem DolphinView::rootItem() const
280 {
281 return m_viewAccessor.dirLister()->rootItem();
282 }
283
284 KFileItemList DolphinView::items() const
285 {
286 return m_viewAccessor.dirLister()->items();
287 }
288
289 KFileItemList DolphinView::selectedItems() const
290 {
291 KFileItemList itemList;
292 const QAbstractItemView* view = m_viewAccessor.itemView();
293 if (!view) {
294 return itemList;
295 }
296
297 const QItemSelection selection = m_viewAccessor.proxyModel()->mapSelectionToSource(view->selectionModel()->selection());
298
299 const QModelIndexList indexList = selection.indexes();
300 foreach (const QModelIndex &index, indexList) {
301 KFileItem item = m_viewAccessor.dirModel()->itemForIndex(index);
302 if (!item.isNull()) {
303 itemList.append(item);
304 }
305 }
306
307 return itemList;
308 }
309
310 int DolphinView::selectedItemsCount() const
311 {
312 const QAbstractItemView* view = m_viewAccessor.itemView();
313 if (!view) {
314 return 0;
315 }
316
317 return view->selectionModel()->selectedIndexes().count();
318 }
319
320 void DolphinView::markUrlsAsSelected(const QList<KUrl>& urls)
321 {
322 foreach (const KUrl& url, urls) {
323 KFileItem item(KFileItem::Unknown, KFileItem::Unknown, url);
324 m_selectedItems.append(item);
325 }
326 }
327
328 void DolphinView::setItemSelectionEnabled(const QRegExp& pattern, bool enabled)
329 {
330 const QItemSelection matchingIndexes = childrenMatchingPattern(QModelIndex(), pattern);
331 const QItemSelectionModel::SelectionFlags command = enabled
332 ? QItemSelectionModel::Select
333 : QItemSelectionModel::Deselect;
334 m_viewAccessor.itemView()->selectionModel()->select(matchingIndexes, command);
335 }
336
337 void DolphinView::setZoomLevel(int level)
338 {
339 if (level < ZoomLevelInfo::minimumLevel()) {
340 level = ZoomLevelInfo::minimumLevel();
341 } else if (level > ZoomLevelInfo::maximumLevel()) {
342 level = ZoomLevelInfo::maximumLevel();
343 }
344
345 if (level != zoomLevel()) {
346 m_viewModeController->setZoomLevel(level);
347 emit zoomLevelChanged(level);
348 }
349 }
350
351 int DolphinView::zoomLevel() const
352 {
353 return m_viewModeController->zoomLevel();
354 }
355
356 void DolphinView::setSorting(Sorting sorting)
357 {
358 if (sorting != this->sorting()) {
359 updateSorting(sorting);
360 }
361 }
362
363 DolphinView::Sorting DolphinView::sorting() const
364 {
365 return m_viewAccessor.proxyModel()->sorting();
366 }
367
368 void DolphinView::setSortOrder(Qt::SortOrder order)
369 {
370 if (sortOrder() != order) {
371 updateSortOrder(order);
372 }
373 }
374
375 Qt::SortOrder DolphinView::sortOrder() const
376 {
377 return m_viewAccessor.proxyModel()->sortOrder();
378 }
379
380 void DolphinView::setSortFoldersFirst(bool foldersFirst)
381 {
382 if (sortFoldersFirst() != foldersFirst) {
383 updateSortFoldersFirst(foldersFirst);
384 }
385 }
386
387 bool DolphinView::sortFoldersFirst() const
388 {
389 return m_viewAccessor.proxyModel()->sortFoldersFirst();
390 }
391
392 void DolphinView::setAdditionalInfo(KFileItemDelegate::InformationList info)
393 {
394 const KUrl viewPropsUrl = rootUrl();
395 ViewProperties props(viewPropsUrl);
396 props.setAdditionalInfo(info);
397 m_viewAccessor.itemDelegate()->setShowInformation(info);
398
399 emit additionalInfoChanged();
400
401 if (m_viewAccessor.reloadOnAdditionalInfoChange()) {
402 loadDirectory(viewPropsUrl);
403 }
404 }
405
406 KFileItemDelegate::InformationList DolphinView::additionalInfo() const
407 {
408 return m_viewAccessor.itemDelegate()->showInformation();
409 }
410
411 void DolphinView::reload()
412 {
413 QByteArray viewState;
414 QDataStream saveStream(&viewState, QIODevice::WriteOnly);
415 saveState(saveStream);
416 m_selectedItems= selectedItems();
417
418 setUrl(url());
419 loadDirectory(url(), true);
420
421 QDataStream restoreStream(viewState);
422 restoreState(restoreStream);
423 }
424
425 void DolphinView::stopLoading()
426 {
427 m_viewAccessor.dirLister()->stop();
428 }
429
430 void DolphinView::refresh()
431 {
432 const bool oldActivationState = m_active;
433 const int oldZoomLevel = m_viewModeController->zoomLevel();
434 m_active = true;
435
436 createView();
437 applyViewProperties();
438 reload();
439
440 setActive(oldActivationState);
441 updateZoomLevel(oldZoomLevel);
442 }
443
444 void DolphinView::setNameFilter(const QString& nameFilter)
445 {
446 m_viewModeController->setNameFilter(nameFilter);
447 }
448
449 QString DolphinView::nameFilter() const
450 {
451 return m_viewModeController->nameFilter();
452 }
453
454 void DolphinView::calculateItemCount(int& fileCount,
455 int& folderCount,
456 KIO::filesize_t& totalFileSize) const
457 {
458 foreach (const KFileItem& item, m_viewAccessor.dirLister()->items()) {
459 if (item.isDir()) {
460 ++folderCount;
461 } else {
462 ++fileCount;
463 totalFileSize += item.size();
464 }
465 }
466 }
467
468 QString DolphinView::statusBarText() const
469 {
470 QString text;
471 int folderCount = 0;
472 int fileCount = 0;
473 KIO::filesize_t totalFileSize = 0;
474
475 if (hasSelection()) {
476 // give a summary of the status of the selected files
477 const KFileItemList list = selectedItems();
478 if (list.isEmpty()) {
479 // when an item is triggered, it is temporary selected but selectedItems()
480 // will return an empty list
481 return text;
482 }
483
484 KFileItemList::const_iterator it = list.begin();
485 const KFileItemList::const_iterator end = list.end();
486 while (it != end) {
487 const KFileItem& item = *it;
488 if (item.isDir()) {
489 ++folderCount;
490 } else {
491 ++fileCount;
492 totalFileSize += item.size();
493 }
494 ++it;
495 }
496
497 if (folderCount + fileCount == 1) {
498 // if only one item is selected, show the filename
499 const QString name = list.first().text();
500 text = (folderCount == 1) ? i18nc("@info:status", "<filename>%1</filename> selected", name) :
501 i18nc("@info:status", "<filename>%1</filename> selected (%2)",
502 name, KIO::convertSize(totalFileSize));
503 } else {
504 // at least 2 items are selected
505 const QString foldersText = i18ncp("@info:status", "1 Folder selected", "%1 Folders selected", folderCount);
506 const QString filesText = i18ncp("@info:status", "1 File selected", "%1 Files selected", fileCount);
507 if ((folderCount > 0) && (fileCount > 0)) {
508 text = i18nc("@info:status folders, files (size)", "%1, %2 (%3)",
509 foldersText, filesText, KIO::convertSize(totalFileSize));
510 } else if (fileCount > 0) {
511 text = i18nc("@info:status files (size)", "%1 (%2)", filesText, KIO::convertSize(totalFileSize));
512 } else {
513 Q_ASSERT(folderCount > 0);
514 text = foldersText;
515 }
516 }
517 } else {
518 calculateItemCount(fileCount, folderCount, totalFileSize);
519 text = KIO::itemsSummaryString(fileCount + folderCount,
520 fileCount, folderCount,
521 totalFileSize, true);
522 }
523
524 return text;
525 }
526
527 QList<QAction*> DolphinView::versionControlActions(const KFileItemList& items) const
528 {
529 return m_dolphinViewController->versionControlActions(items);
530 }
531
532 void DolphinView::setUrl(const KUrl& url)
533 {
534 if (m_viewModeController->url() == url) {
535 return;
536 }
537
538 const bool hadSelection = hasSelection();
539
540 // The selection model and directory lister might change in the case of the column view:
541 disconnectViewAccessor();
542
543 m_newFileNames.clear();
544
545 m_viewModeController->setUrl(url); // emits urlChanged, which we forward
546 m_viewAccessor.prepareUrlChange(url);
547 applyViewProperties();
548
549 // When changing the URL there is no need to keep the version
550 // data of the previous URL.
551 m_viewAccessor.dirModel()->clearVersionData();
552
553 // Reconnect to the (probably) new selection model and directory lister
554 connectViewAccessor();
555 loadDirectory(url);
556
557 if (hadSelection || hasSelection()) {
558 emitSelectionChangedSignal();
559 }
560 }
561
562 void DolphinView::selectAll()
563 {
564 m_viewAccessor.itemView()->selectAll();
565 }
566
567 void DolphinView::invertSelection()
568 {
569 // Implementation note: Using selectionModel->select(selection, QItemSelectionModel::Toggle) does not
570 // work, as QItemSelectionModel::hasSelection() provides invalid values in this case. This might be a Qt-issue -
571 // when changing the implementation with an updated Qt-version don't forget to run the Dolphin-unit-tests that
572 // verify this usecase.
573 const KFileItemList selItems = selectedItems();
574 clearSelection();
575
576 QItemSelection invertedSelection;
577 foreach (const KFileItem& item, items()) {
578 if (!selItems.contains(item)) {
579 const QModelIndex index = m_viewAccessor.proxyModel()->mapFromSource(m_viewAccessor.dirModel()->indexForItem(item));
580 invertedSelection.select(index, index);
581 }
582 }
583
584 QItemSelectionModel* selectionModel = m_viewAccessor.itemView()->selectionModel();
585 selectionModel->select(invertedSelection, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Current);
586 }
587
588 void DolphinView::clearSelection()
589 {
590 m_viewAccessor.itemView()->clearSelection();
591 }
592
593 void DolphinView::renameSelectedItems()
594 {
595 KFileItemList items = selectedItems();
596 const int itemCount = items.count();
597 if (itemCount < 1) {
598 return;
599 }
600
601 if ((itemCount == 1) && DolphinSettings::instance().generalSettings()->renameInline()) {
602 const QModelIndex dirIndex = m_viewAccessor.dirModel()->indexForItem(items.first());
603 const QModelIndex proxyIndex = m_viewAccessor.proxyModel()->mapFromSource(dirIndex);
604 m_viewAccessor.itemView()->edit(proxyIndex);
605 } else {
606 RenameDialog* dialog = new RenameDialog(this, items);
607 dialog->setAttribute(Qt::WA_DeleteOnClose);
608 dialog->show();
609 dialog->raise();
610 dialog->activateWindow();
611 }
612
613 // assure that the current index remains visible when KDirLister
614 // will notify the view about changed items
615 m_assureVisibleCurrentIndex = true;
616 }
617
618 void DolphinView::trashSelectedItems()
619 {
620 const KUrl::List list = simplifiedSelectedUrls();
621 KonqOperations::del(this, KonqOperations::TRASH, list);
622 }
623
624 void DolphinView::deleteSelectedItems()
625 {
626 const KUrl::List list = simplifiedSelectedUrls();
627 const bool del = KonqOperations::askDeleteConfirmation(list,
628 KonqOperations::DEL,
629 KonqOperations::DEFAULT_CONFIRMATION,
630 this);
631
632 if (del) {
633 KIO::Job* job = KIO::del(list);
634 connect(job, SIGNAL(result(KJob*)),
635 this, SLOT(slotDeleteFileFinished(KJob*)));
636 }
637 }
638
639 void DolphinView::cutSelectedItems()
640 {
641 QMimeData* mimeData = selectionMimeData();
642 KonqMimeData::addIsCutSelection(mimeData, true);
643 QApplication::clipboard()->setMimeData(mimeData);
644 }
645
646 void DolphinView::copySelectedItems()
647 {
648 QMimeData* mimeData = selectionMimeData();
649 QApplication::clipboard()->setMimeData(mimeData);
650 }
651
652 void DolphinView::paste()
653 {
654 pasteToUrl(url());
655 }
656
657 void DolphinView::pasteIntoFolder()
658 {
659 const KFileItemList items = selectedItems();
660 if ((items.count() == 1) && items.first().isDir()) {
661 pasteToUrl(items.first().url());
662 }
663 }
664
665 void DolphinView::setShowPreview(bool show)
666 {
667 if (m_showPreview == show) {
668 return;
669 }
670
671 const KUrl viewPropsUrl = rootUrl();
672 ViewProperties props(viewPropsUrl);
673 props.setShowPreview(show);
674
675 m_showPreview = show;
676 const int oldZoomLevel = m_viewModeController->zoomLevel();
677 emit showPreviewChanged();
678
679 // Enabling or disabling the preview might change the icon size of the view.
680 // As the view does not emit a signal when the icon size has been changed,
681 // the used zoom level of the controller must be adjusted manually:
682 updateZoomLevel(oldZoomLevel);
683 }
684
685 void DolphinView::setShowHiddenFiles(bool show)
686 {
687 if (m_viewAccessor.dirLister()->showingDotFiles() == show) {
688 return;
689 }
690
691 m_selectedItems = selectedItems();
692
693 const KUrl viewPropsUrl = rootUrl();
694 ViewProperties props(viewPropsUrl);
695 props.setShowHiddenFiles(show);
696
697 m_viewAccessor.dirLister()->setShowingDotFiles(show);
698 emit showHiddenFilesChanged();
699 }
700
701 void DolphinView::setCategorizedSorting(bool categorized)
702 {
703 if (categorized == categorizedSorting()) {
704 return;
705 }
706
707 // setCategorizedSorting(true) may only get invoked
708 // if the view supports categorized sorting
709 Q_ASSERT(!categorized || supportsCategorizedSorting());
710
711 ViewProperties props(rootUrl());
712 props.setCategorizedSorting(categorized);
713 props.save();
714
715 m_storedCategorizedSorting = categorized;
716 m_viewAccessor.proxyModel()->setCategorizedModel(categorized);
717
718 emit categorizedSortingChanged();
719 }
720
721 void DolphinView::mouseReleaseEvent(QMouseEvent* event)
722 {
723 QWidget::mouseReleaseEvent(event);
724 setActive(true);
725 }
726
727 bool DolphinView::eventFilter(QObject* watched, QEvent* event)
728 {
729 switch (event->type()) {
730 case QEvent::FocusIn:
731 if (watched == m_viewAccessor.itemView()) {
732 m_dolphinViewController->requestActivation();
733 }
734 break;
735
736 case QEvent::DragEnter:
737 if (watched == m_viewAccessor.itemView()->viewport()) {
738 setActive(true);
739 }
740 break;
741
742 case QEvent::KeyPress:
743 if (watched == m_viewAccessor.itemView()) {
744 // clear the selection when Escape has been pressed
745 QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
746 if (keyEvent->key() == Qt::Key_Escape) {
747 clearSelection();
748 }
749 }
750 break;
751
752 case QEvent::Wheel:
753 if (watched == m_viewAccessor.itemView()->viewport()) {
754 // Ctrl+wheel events should cause icon zooming, but not if the left mouse button is pressed
755 // (the user is probably trying to scroll during a selection in that case)
756 QWheelEvent* wheelEvent = static_cast<QWheelEvent*>(event);
757 if (wheelEvent->modifiers() & Qt::ControlModifier && !(wheelEvent->buttons() & Qt::LeftButton)) {
758 const int delta = wheelEvent->delta();
759 const int level = zoomLevel();
760 if (delta > 0) {
761 setZoomLevel(level + 1);
762 } else if (delta < 0) {
763 setZoomLevel(level - 1);
764 }
765 return true;
766 }
767 }
768 break;
769
770 default:
771 break;
772 }
773
774 return QWidget::eventFilter(watched, event);
775 }
776
777 void DolphinView::showEvent(QShowEvent* event)
778 {
779 QWidget::showEvent(event);
780 if (!event->spontaneous()) {
781 loadDirectory(url());
782 }
783 }
784
785 void DolphinView::activate()
786 {
787 setActive(true);
788 }
789
790 void DolphinView::triggerItem(const KFileItem& item)
791 {
792 const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
793 if ((modifier & Qt::ShiftModifier) || (modifier & Qt::ControlModifier)) {
794 // items are selected by the user, hence don't trigger the
795 // item specified by 'index'
796 return;
797 }
798
799 // TODO: the m_isContextMenuOpen check is a workaround for Qt-issue 207192
800 if (item.isNull() || m_isContextMenuOpen) {
801 return;
802 }
803
804 emit itemTriggered(item); // caught by DolphinViewContainer or DolphinPart
805 }
806
807 void DolphinView::slotSelectionChanged(const QItemSelection& selected, const QItemSelection& deselected)
808 {
809 const int count = selectedItemsCount();
810 const bool selectionStateChanged = ((count > 0) && (selected.count() == count)) ||
811 ((count == 0) && !deselected.isEmpty());
812
813 // If nothing has been selected before and something got selected (or if something
814 // was selected before and now nothing is selected) the selectionChangedSignal must
815 // be emitted asynchronously as fast as possible to update the edit-actions.
816 m_selectionChangedTimer->setInterval(selectionStateChanged ? 0 : 300);
817 m_selectionChangedTimer->start();
818 }
819
820 void DolphinView::emitSelectionChangedSignal()
821 {
822 m_selectionChangedTimer->stop();
823 emit selectionChanged(selectedItems());
824 }
825
826 void DolphinView::openContextMenu(const QPoint& pos,
827 const QList<QAction*>& customActions)
828 {
829 KFileItem item;
830 const QModelIndex index = m_viewAccessor.itemView()->indexAt(pos);
831 if (index.isValid() && (index.column() == DolphinModel::Name)) {
832 const QModelIndex dolphinModelIndex = m_viewAccessor.proxyModel()->mapToSource(index);
833 item = m_viewAccessor.dirModel()->itemForIndex(dolphinModelIndex);
834 }
835
836 m_isContextMenuOpen = true; // TODO: workaround for Qt-issue 207192
837 emit requestContextMenu(item, url(), customActions);
838 m_isContextMenuOpen = false;
839 }
840
841 void DolphinView::dropUrls(const KFileItem& destItem,
842 const KUrl& destPath,
843 QDropEvent* event)
844 {
845 addNewFileNames(event->mimeData());
846 DragAndDropHelper::instance().dropUrls(destItem, destPath, event, this);
847 }
848
849 void DolphinView::updateSorting(DolphinView::Sorting sorting)
850 {
851 ViewProperties props(rootUrl());
852 props.setSorting(sorting);
853
854 m_viewAccessor.proxyModel()->setSorting(sorting);
855
856 emit sortingChanged(sorting);
857 }
858
859 void DolphinView::updateSortOrder(Qt::SortOrder order)
860 {
861 ViewProperties props(rootUrl());
862 props.setSortOrder(order);
863
864 m_viewAccessor.proxyModel()->setSortOrder(order);
865
866 emit sortOrderChanged(order);
867 }
868
869 void DolphinView::updateSortFoldersFirst(bool foldersFirst)
870 {
871 ViewProperties props(rootUrl());
872 props.setSortFoldersFirst(foldersFirst);
873
874 m_viewAccessor.proxyModel()->setSortFoldersFirst(foldersFirst);
875
876 emit sortFoldersFirstChanged(foldersFirst);
877 }
878
879 void DolphinView::updateAdditionalInfo(const KFileItemDelegate::InformationList& info)
880 {
881 ViewProperties props(rootUrl());
882 props.setAdditionalInfo(info);
883 props.save();
884
885 m_viewAccessor.itemDelegate()->setShowInformation(info);
886
887 emit additionalInfoChanged();
888 }
889
890 void DolphinView::updateAdditionalInfoActions(KActionCollection* collection)
891 {
892 const AdditionalInfoAccessor& infoAccessor = AdditionalInfoAccessor::instance();
893
894 const KFileItemDelegate::InformationList checkedInfo = m_viewAccessor.itemDelegate()->showInformation();
895 const KFileItemDelegate::InformationList infoKeys = infoAccessor.keys();
896
897 const bool enable = (m_mode == DolphinView::DetailsView) ||
898 (m_mode == DolphinView::IconsView);
899
900 foreach (const KFileItemDelegate::Information& info, infoKeys) {
901 const QString name = infoAccessor.actionCollectionName(info, AdditionalInfoAccessor::AdditionalInfoType);
902 QAction* action = collection->action(name);
903 Q_ASSERT(action);
904 action->setEnabled(enable);
905 action->setChecked(checkedInfo.contains(info));
906 }
907 }
908
909 QPair<bool, QString> DolphinView::pasteInfo() const
910 {
911 return KonqOperations::pasteInfo(url());
912 }
913
914 void DolphinView::setTabsForFilesEnabled(bool tabsForFiles)
915 {
916 m_tabsForFiles = tabsForFiles;
917 }
918
919 bool DolphinView::isTabsForFilesEnabled() const
920 {
921 return m_tabsForFiles;
922 }
923
924 bool DolphinView::itemsExpandable() const
925 {
926 return m_viewAccessor.itemsExpandable();
927 }
928
929 void DolphinView::restoreState(QDataStream& stream)
930 {
931 // Restore the URL of the current item that had the keyboard focus
932 stream >> m_activeItemUrl;
933
934 // Restore the root URL
935 KUrl rootUrl;
936 stream >> rootUrl;
937 m_viewAccessor.setRootUrl(rootUrl);
938
939 // Restore the view position
940 stream >> m_restoredContentsPosition;
941
942 // Restore expanded folders (only relevant for the details view - will be ignored by the view in other view modes)
943 QSet<KUrl> urlsToExpand;
944 stream >> urlsToExpand;
945 const DolphinDetailsViewExpander* expander = m_viewAccessor.setExpandedUrls(urlsToExpand);
946 if (expander) {
947 m_expanderActive = true;
948 connect (expander, SIGNAL(completed()), this, SLOT(slotLoadingCompleted()));
949 }
950 else {
951 m_expanderActive = false;
952 }
953 }
954
955 void DolphinView::saveState(QDataStream& stream)
956 {
957 // Save the URL of the current item that has the keyboard focus
958 KFileItem currentItem;
959 const QAbstractItemView* view = m_viewAccessor.itemView();
960
961 if (view) {
962 const QModelIndex proxyIndex = view->currentIndex();
963 const QModelIndex dirModelIndex = m_viewAccessor.proxyModel()->mapToSource(proxyIndex);
964 currentItem = m_viewAccessor.dirModel()->itemForIndex(dirModelIndex);
965 }
966
967 KUrl currentItemUrl;
968 if (!currentItem.isNull()) {
969 currentItemUrl = currentItem.url();
970 }
971
972 stream << currentItemUrl;
973
974 // Save the root URL
975 stream << m_viewAccessor.rootUrl();
976
977 // Save view position
978 const int x = view->horizontalScrollBar()->value();
979 const int y = view->verticalScrollBar()->value();
980 stream << QPoint(x, y);
981
982 // Save expanded folders (only relevant for the details view - the set will be empty in other view modes)
983 stream << m_viewAccessor.expandedUrls();
984 }
985
986 bool DolphinView::hasSelection() const
987 {
988 const QAbstractItemView* view = m_viewAccessor.itemView();
989 return view && view->selectionModel()->hasSelection();
990 }
991
992 void DolphinView::observeCreatedItem(const KUrl& url)
993 {
994 m_createdItemUrl = url;
995 connect(m_viewAccessor.dirModel(), SIGNAL(rowsInserted(const QModelIndex&, int, int)),
996 this, SLOT(selectAndScrollToCreatedItem()));
997 }
998
999 void DolphinView::selectAndScrollToCreatedItem()
1000 {
1001 const QModelIndex dirIndex = m_viewAccessor.dirModel()->indexForUrl(m_createdItemUrl);
1002 if (dirIndex.isValid()) {
1003 const QModelIndex proxyIndex = m_viewAccessor.proxyModel()->mapFromSource(dirIndex);
1004 m_viewAccessor.itemView()->setCurrentIndex(proxyIndex);
1005 }
1006
1007 disconnect(m_viewAccessor.dirModel(), SIGNAL(rowsInserted(const QModelIndex&, int, int)),
1008 this, SLOT(selectAndScrollToCreatedItem()));
1009 m_createdItemUrl = KUrl();
1010 }
1011
1012 void DolphinView::slotRedirection(const KUrl& oldUrl, const KUrl& newUrl)
1013 {
1014 if (oldUrl.equals(url(), KUrl::CompareWithoutTrailingSlash)) {
1015 emit redirection(oldUrl, newUrl);
1016 m_viewModeController->redirectToUrl(newUrl); // #186947
1017 }
1018 }
1019
1020 void DolphinView::restoreContentsPosition()
1021 {
1022 if (!m_restoredContentsPosition.isNull()) {
1023 const int x = m_restoredContentsPosition.x();
1024 const int y = m_restoredContentsPosition.y();
1025 m_restoredContentsPosition = QPoint();
1026
1027 QAbstractItemView* view = m_viewAccessor.itemView();
1028 Q_ASSERT(view);
1029 view->horizontalScrollBar()->setValue(x);
1030 view->verticalScrollBar()->setValue(y);
1031 }
1032 }
1033
1034 void DolphinView::slotUrlChangeRequested(const KUrl& url)
1035 {
1036 m_viewModeController->setUrl(url);
1037 updateWritableState();
1038 }
1039
1040 void DolphinView::showHoverInformation(const KFileItem& item)
1041 {
1042 emit requestItemInfo(item);
1043 }
1044
1045 void DolphinView::clearHoverInformation()
1046 {
1047 emit requestItemInfo(KFileItem());
1048 }
1049
1050 void DolphinView::slotDeleteFileFinished(KJob* job)
1051 {
1052 if (job->error() == 0) {
1053 emit operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
1054 } else if (job->error() != KIO::ERR_USER_CANCELED) {
1055 emit errorMessage(job->errorString());
1056 }
1057 }
1058
1059 void DolphinView::slotDirListerStarted(const KUrl& url)
1060 {
1061 // Disable the writestate temporary until it can be determined in a fast way
1062 // in DolphinView::slotDirListerCompleted()
1063 if (m_isFolderWritable) {
1064 m_isFolderWritable = false;
1065 emit writeStateChanged(m_isFolderWritable);
1066 }
1067
1068 emit startedPathLoading(url);
1069 }
1070
1071 void DolphinView::slotDirListerCompleted()
1072 {
1073 if (!m_expanderActive) {
1074 slotLoadingCompleted();
1075 }
1076
1077 if (!m_newFileNames.isEmpty()) {
1078 // select all newly added items created by a paste operation or
1079 // a drag & drop operation, and clear the previous selection
1080 m_viewAccessor.itemView()->clearSelection();
1081 const int rowCount = m_viewAccessor.proxyModel()->rowCount();
1082 QItemSelection selection;
1083 for (int row = 0; row < rowCount; ++row) {
1084 const QModelIndex proxyIndex = m_viewAccessor.proxyModel()->index(row, 0);
1085 const QModelIndex dirIndex = m_viewAccessor.proxyModel()->mapToSource(proxyIndex);
1086 const KUrl url = m_viewAccessor.dirModel()->itemForIndex(dirIndex).url();
1087 if (m_newFileNames.contains(url.fileName())) {
1088 selection.merge(QItemSelection(proxyIndex, proxyIndex), QItemSelectionModel::Select);
1089 }
1090 }
1091 m_viewAccessor.itemView()->selectionModel()->select(selection, QItemSelectionModel::Select);
1092
1093 m_newFileNames.clear();
1094 }
1095
1096 updateWritableState();
1097 }
1098
1099 void DolphinView::slotLoadingCompleted()
1100 {
1101 m_expanderActive = false;
1102
1103 if (!m_activeItemUrl.isEmpty()) {
1104 // assure that the current item remains visible
1105 const QModelIndex dirIndex = m_viewAccessor.dirModel()->indexForUrl(m_activeItemUrl);
1106 if (dirIndex.isValid()) {
1107 const QModelIndex proxyIndex = m_viewAccessor.proxyModel()->mapFromSource(dirIndex);
1108 QAbstractItemView* view = m_viewAccessor.itemView();
1109 const bool clearSelection = !hasSelection();
1110 view->setCurrentIndex(proxyIndex);
1111 if (clearSelection) {
1112 view->clearSelection();
1113 }
1114 m_activeItemUrl.clear();
1115 }
1116 }
1117
1118 if (!m_selectedItems.isEmpty()) {
1119 const KUrl& baseUrl = url();
1120 KUrl url;
1121 QItemSelection newSelection;
1122 foreach(const KFileItem& item, m_selectedItems) {
1123 url = item.url().upUrl();
1124 if (baseUrl.equals(url, KUrl::CompareWithoutTrailingSlash)) {
1125 const QModelIndex index = m_viewAccessor.proxyModel()->mapFromSource(m_viewAccessor.dirModel()->indexForItem(item));
1126 newSelection.select(index, index);
1127 }
1128 }
1129 m_viewAccessor.itemView()->selectionModel()->select(newSelection,
1130 QItemSelectionModel::ClearAndSelect
1131 | QItemSelectionModel::Current);
1132 m_selectedItems.clear();
1133 }
1134
1135 // Restore the contents position. This has to be done using a Qt::QueuedConnection
1136 // because the view might not be in its final state yet.
1137 QMetaObject::invokeMethod(this, "restoreContentsPosition", Qt::QueuedConnection);
1138
1139 emit finishedPathLoading(url());
1140 }
1141
1142 void DolphinView::slotRefreshItems()
1143 {
1144 if (m_assureVisibleCurrentIndex) {
1145 m_assureVisibleCurrentIndex = false;
1146 m_viewAccessor.itemView()->scrollTo(m_viewAccessor.itemView()->currentIndex());
1147 }
1148 }
1149
1150 void DolphinView::loadDirectory(const KUrl& url, bool reload)
1151 {
1152 if (!url.isValid()) {
1153 const QString location(url.pathOrUrl());
1154 if (location.isEmpty()) {
1155 emit errorMessage(i18nc("@info:status", "The location is empty."));
1156 } else {
1157 emit errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location));
1158 }
1159 return;
1160 }
1161
1162 KDirLister* dirLister = m_viewAccessor.dirLister();
1163 dirLister->openUrl(url, reload ? KDirLister::Reload : KDirLister::NoFlags);
1164 }
1165
1166 void DolphinView::applyViewProperties()
1167 {
1168 const ViewProperties props(rootUrl());
1169
1170 const Mode mode = props.viewMode();
1171 if (m_mode != mode) {
1172 const int oldZoomLevel = m_viewModeController->zoomLevel();
1173
1174 m_mode = mode;
1175 createView();
1176 emit modeChanged();
1177
1178 updateZoomLevel(oldZoomLevel);
1179 }
1180 if (!m_viewAccessor.itemView()) {
1181 createView();
1182 }
1183
1184 Q_ASSERT(m_viewAccessor.itemView());
1185 Q_ASSERT(m_viewAccessor.itemDelegate());
1186
1187 const bool showHiddenFiles = props.showHiddenFiles();
1188 if (showHiddenFiles != m_viewAccessor.dirLister()->showingDotFiles()) {
1189 m_viewAccessor.dirLister()->setShowingDotFiles(showHiddenFiles);
1190 emit showHiddenFilesChanged();
1191 }
1192
1193 m_storedCategorizedSorting = props.categorizedSorting();
1194 const bool categorized = m_storedCategorizedSorting && supportsCategorizedSorting();
1195 if (categorized != m_viewAccessor.proxyModel()->isCategorizedModel()) {
1196 m_viewAccessor.proxyModel()->setCategorizedModel(categorized);
1197 emit categorizedSortingChanged();
1198 }
1199
1200 const DolphinView::Sorting sorting = props.sorting();
1201 if (sorting != m_viewAccessor.proxyModel()->sorting()) {
1202 m_viewAccessor.proxyModel()->setSorting(sorting);
1203 emit sortingChanged(sorting);
1204 }
1205
1206 const Qt::SortOrder sortOrder = props.sortOrder();
1207 if (sortOrder != m_viewAccessor.proxyModel()->sortOrder()) {
1208 m_viewAccessor.proxyModel()->setSortOrder(sortOrder);
1209 emit sortOrderChanged(sortOrder);
1210 }
1211
1212 const bool sortFoldersFirst = props.sortFoldersFirst();
1213 if (sortFoldersFirst != m_viewAccessor.proxyModel()->sortFoldersFirst()) {
1214 m_viewAccessor.proxyModel()->setSortFoldersFirst(sortFoldersFirst);
1215 emit sortFoldersFirstChanged(sortFoldersFirst);
1216 }
1217
1218 KFileItemDelegate::InformationList info = props.additionalInfo();
1219 if (info != m_viewAccessor.itemDelegate()->showInformation()) {
1220 m_viewAccessor.itemDelegate()->setShowInformation(info);
1221 emit additionalInfoChanged();
1222 }
1223
1224 const bool showPreview = props.showPreview();
1225 if (showPreview != m_showPreview) {
1226 m_showPreview = showPreview;
1227 const int oldZoomLevel = m_viewModeController->zoomLevel();
1228 emit showPreviewChanged();
1229
1230 // Enabling or disabling the preview might change the icon size of the view.
1231 // As the view does not emit a signal when the icon size has been changed,
1232 // the used zoom level of the controller must be adjusted manually:
1233 updateZoomLevel(oldZoomLevel);
1234 }
1235 }
1236
1237 void DolphinView::createView()
1238 {
1239 deleteView();
1240
1241 Q_ASSERT(!m_viewAccessor.itemView());
1242 Q_ASSERT(!m_dolphinViewController->itemView());
1243 m_viewAccessor.createView(this, m_dolphinViewController, m_viewModeController, m_mode);
1244
1245 QAbstractItemView* view = m_viewAccessor.itemView();
1246 Q_ASSERT(view);
1247 view->installEventFilter(this);
1248 view->viewport()->installEventFilter(this);
1249
1250 m_dolphinViewController->setItemView(view);
1251
1252 const int zoomLevel = ZoomLevelInfo::zoomLevelForIconSize(view->iconSize());
1253 m_viewModeController->setZoomLevel(zoomLevel);
1254
1255 connectViewAccessor();
1256
1257 setFocusProxy(m_viewAccessor.layoutTarget());
1258 m_topLayout->insertWidget(1, m_viewAccessor.layoutTarget());
1259 }
1260
1261 void DolphinView::deleteView()
1262 {
1263 QAbstractItemView* view = m_viewAccessor.itemView();
1264 Q_ASSERT(!m_dolphinViewController->itemView() || (m_dolphinViewController->itemView() == view));
1265 m_dolphinViewController->setItemView(0);
1266
1267 if (view) {
1268 disconnectViewAccessor();
1269
1270 if (hasFocus()) {
1271 // It's important to set the keyboard focus to the parent
1272 // before deleting the view: Otherwise when having a split
1273 // view the other view will get the focus and will request
1274 // an activation (see DolphinView::eventFilter()).
1275 setFocusProxy(0);
1276 setFocus();
1277 }
1278
1279 m_viewModeController->disconnect(view);
1280
1281 m_viewAccessor.deleteView();
1282 }
1283 }
1284
1285 void DolphinView::pasteToUrl(const KUrl& url)
1286 {
1287 addNewFileNames(QApplication::clipboard()->mimeData());
1288 KonqOperations::doPaste(this, url);
1289 }
1290
1291 void DolphinView::updateZoomLevel(int oldZoomLevel)
1292 {
1293 const int newZoomLevel = ZoomLevelInfo::zoomLevelForIconSize(m_viewAccessor.itemView()->iconSize());
1294 if (oldZoomLevel != newZoomLevel) {
1295 m_viewModeController->setZoomLevel(newZoomLevel);
1296 emit zoomLevelChanged(newZoomLevel);
1297 }
1298 }
1299
1300 KUrl::List DolphinView::simplifiedSelectedUrls() const
1301 {
1302 KUrl::List urls;
1303
1304 const KFileItemList items = selectedItems();
1305 foreach (const KFileItem &item, items) {
1306 urls.append(item.url());
1307 }
1308
1309
1310 if (itemsExpandable()) {
1311 urls = KDirModel::simplifiedUrlList(urls);
1312 }
1313
1314 return urls;
1315 }
1316
1317 QMimeData* DolphinView::selectionMimeData() const
1318 {
1319 const QAbstractItemView* view = m_viewAccessor.itemView();
1320 Q_ASSERT((view) && (view->selectionModel()));
1321 const QItemSelection selection = m_viewAccessor.proxyModel()->mapSelectionToSource(view->selectionModel()->selection());
1322 return m_viewAccessor.dirModel()->mimeData(selection.indexes());
1323 }
1324
1325 void DolphinView::addNewFileNames(const QMimeData* mimeData)
1326 {
1327 const KUrl::List urls = KUrl::List::fromMimeData(mimeData);
1328 foreach (const KUrl& url, urls) {
1329 m_newFileNames.insert(url.fileName());
1330 }
1331 }
1332
1333 QItemSelection DolphinView::childrenMatchingPattern(const QModelIndex& parent, const QRegExp& pattern) const
1334 {
1335 QItemSelection matchingIndexes;
1336 const DolphinSortFilterProxyModel* proxyModel = m_viewAccessor.proxyModel();
1337 const DolphinModel* dolphinModel = m_viewAccessor.dirModel();
1338
1339 const int rowCount = proxyModel->rowCount(parent);
1340
1341 for (int row = 0; row < rowCount; ++row) {
1342 QModelIndex index = proxyModel->index(row, 0, parent);
1343 QModelIndex sourceIndex = proxyModel->mapToSource(index);
1344
1345 if (sourceIndex.isValid() && pattern.exactMatch(dolphinModel->data(sourceIndex).toString())) {
1346 matchingIndexes += QItemSelectionRange(index);
1347 }
1348
1349 if (proxyModel->hasChildren(index)) {
1350 matchingIndexes += childrenMatchingPattern(index, pattern);
1351 }
1352 }
1353
1354 return matchingIndexes;
1355 }
1356
1357 void DolphinView::connectViewAccessor()
1358 {
1359 KDirLister* dirLister = m_viewAccessor.dirLister();
1360 connect(dirLister, SIGNAL(redirection(KUrl,KUrl)), this, SLOT(slotRedirection(KUrl,KUrl)));
1361 connect(dirLister, SIGNAL(started(KUrl)), this, SLOT(slotDirListerStarted(KUrl)));
1362 connect(dirLister, SIGNAL(completed()), this, SLOT(slotDirListerCompleted()));
1363 connect(dirLister, SIGNAL(refreshItems(const QList<QPair<KFileItem,KFileItem>>&)),
1364 this, SLOT(slotRefreshItems()));
1365
1366 connect(dirLister, SIGNAL(clear()), this, SIGNAL(itemCountChanged()));
1367 connect(dirLister, SIGNAL(newItems(KFileItemList)), this, SIGNAL(itemCountChanged()));
1368 connect(dirLister, SIGNAL(infoMessage(const QString&)), this, SIGNAL(infoMessage(const QString&)));
1369 connect(dirLister, SIGNAL(errorMessage(const QString&)), this, SIGNAL(infoMessage(const QString&)));
1370 connect(dirLister, SIGNAL(percent(int)), this, SIGNAL(pathLoadingProgress(int)));
1371 connect(dirLister, SIGNAL(urlIsFileError(const KUrl&)), this, SIGNAL(urlIsFileError(const KUrl&)));
1372 connect(dirLister, SIGNAL(itemsDeleted(const KFileItemList&)), this, SIGNAL(itemCountChanged()));
1373
1374 QAbstractItemView* view = m_viewAccessor.itemView();
1375 connect(view->selectionModel(), SIGNAL(selectionChanged(QItemSelection, QItemSelection)),
1376 this, SLOT(slotSelectionChanged(QItemSelection, QItemSelection)));
1377 }
1378
1379 void DolphinView::disconnectViewAccessor()
1380 {
1381 KDirLister* dirLister = m_viewAccessor.dirLister();
1382 disconnect(dirLister, SIGNAL(redirection(KUrl,KUrl)), this, SLOT(slotRedirection(KUrl,KUrl)));
1383 disconnect(dirLister, SIGNAL(started(KUrl)), this, SLOT(slotDirListerStarted(KUrl)));
1384 disconnect(dirLister, SIGNAL(completed()), this, SLOT(slotDirListerCompleted()));
1385 disconnect(dirLister, SIGNAL(refreshItems(const QList<QPair<KFileItem,KFileItem>>&)),
1386 this, SLOT(slotRefreshItems()));
1387
1388 disconnect(dirLister, SIGNAL(clear()), this, SIGNAL(itemCountChanged()));
1389 disconnect(dirLister, SIGNAL(newItems(KFileItemList)), this, SIGNAL(itemCountChanged()));
1390 disconnect(dirLister, SIGNAL(infoMessage(const QString&)), this, SIGNAL(infoMessage(const QString&)));
1391 disconnect(dirLister, SIGNAL(errorMessage(const QString&)), this, SIGNAL(errorMessage(const QString&)));
1392 disconnect(dirLister, SIGNAL(percent(int)), this, SIGNAL(pathLoadingProgress(int)));
1393 disconnect(dirLister, SIGNAL(urlIsFileError(const KUrl&)), this, SIGNAL(urlIsFileError(const KUrl&)));
1394 disconnect(dirLister, SIGNAL(itemsDeleted(const KFileItemList&)), this, SIGNAL(itemCountChanged()));
1395
1396 QAbstractItemView* view = m_viewAccessor.itemView();
1397 disconnect(view->selectionModel(), SIGNAL(selectionChanged(QItemSelection, QItemSelection)),
1398 this, SLOT(slotSelectionChanged(QItemSelection, QItemSelection)));
1399 }
1400
1401 void DolphinView::updateWritableState()
1402 {
1403 const bool wasFolderWritable = m_isFolderWritable;
1404 m_isFolderWritable = true;
1405
1406 const KFileItem item = m_viewAccessor.dirLister()->rootItem();
1407 if (!item.isNull()) {
1408 KFileItemListProperties capabilities(KFileItemList() << item);
1409 m_isFolderWritable = capabilities.supportsWriting();
1410 }
1411 if (m_isFolderWritable != wasFolderWritable) {
1412 emit writeStateChanged(m_isFolderWritable);
1413 }
1414 }
1415
1416 DolphinView::ViewAccessor::ViewAccessor() :
1417 m_rootUrl(),
1418 m_iconsView(0),
1419 m_detailsView(0),
1420 m_columnsContainer(0),
1421 m_dolphinModel(0),
1422 m_proxyModel(0),
1423 m_dragSource(0)
1424 {
1425 DolphinDirLister* dirLister = new DolphinDirLister();
1426 dirLister->setAutoUpdate(true);
1427 dirLister->setDelayedMimeTypes(true);
1428
1429 m_dolphinModel = new DolphinModel();
1430 m_dolphinModel->setDirLister(dirLister); // m_dolphinModel takes ownership of dirLister
1431 m_dolphinModel->setDropsAllowed(DolphinModel::DropOnDirectory);
1432
1433 m_proxyModel = new DolphinSortFilterProxyModel();
1434 m_proxyModel->setSourceModel(m_dolphinModel);
1435 m_proxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
1436 }
1437
1438 DolphinView::ViewAccessor::~ViewAccessor()
1439 {
1440 delete m_proxyModel;
1441 m_proxyModel = 0;
1442
1443 delete m_dolphinModel;
1444 m_dolphinModel = 0;
1445
1446 delete m_dragSource;
1447 m_dragSource = 0;
1448 }
1449
1450 void DolphinView::ViewAccessor::createView(QWidget* parent,
1451 DolphinViewController* dolphinViewController,
1452 const ViewModeController* viewModeController,
1453 Mode mode)
1454 {
1455 Q_ASSERT(!itemView());
1456
1457 switch (mode) {
1458 case IconsView:
1459 m_iconsView = new DolphinIconsView(parent,
1460 dolphinViewController,
1461 viewModeController,
1462 m_proxyModel);
1463 break;
1464
1465 case DetailsView:
1466 m_detailsView = new DolphinDetailsView(parent,
1467 dolphinViewController,
1468 viewModeController,
1469 m_proxyModel);
1470 break;
1471
1472 case ColumnView:
1473 m_columnsContainer = new DolphinColumnViewContainer(parent,
1474 dolphinViewController,
1475 viewModeController);
1476 if (!m_rootUrl.isEmpty() && m_rootUrl.isParentOf(viewModeController->url())) {
1477 // The column-view must show several columns starting with m_rootUrl as
1478 // first column and viewModeController->url() as last column.
1479 m_columnsContainer->showColumn(m_rootUrl);
1480 m_columnsContainer->showColumn(viewModeController->url());
1481 }
1482 break;
1483
1484 default:
1485 Q_ASSERT(false);
1486 }
1487
1488 KDirLister* lister = dirLister();
1489 if (lister) {
1490 lister->setMainWindow(parent->window());
1491 }
1492 }
1493
1494 void DolphinView::ViewAccessor::deleteView()
1495 {
1496 if (m_columnsContainer) {
1497 m_columnsContainer->close();
1498 m_columnsContainer->disconnect();
1499 m_columnsContainer->deleteLater();
1500 m_columnsContainer = 0;
1501 } else {
1502 QAbstractItemView* view = itemView();
1503 if (view) {
1504 view->close();
1505 view->disconnect();
1506
1507 if (DragAndDropHelper::instance().isDragSource(view)) {
1508 // The view is a drag source (the feature "Open folders
1509 // during drag operations" is used). Deleting the view
1510 // during an ongoing drag operation is not allowed, so
1511 // this will postponed.
1512 if (m_dragSource) {
1513 // the old stored view is obviously not the drag source anymore
1514 m_dragSource->deleteLater();
1515 m_dragSource = 0;
1516 }
1517 view->hide();
1518 m_dragSource = view;
1519 } else {
1520 view->deleteLater();
1521 view = 0;
1522 }
1523 }
1524
1525 m_iconsView = 0;
1526 m_detailsView = 0;
1527 }
1528 }
1529
1530 void DolphinView::ViewAccessor::prepareUrlChange(const KUrl& url)
1531 {
1532 if (m_columnsContainer) {
1533 m_columnsContainer->showColumn(url);
1534 }
1535 }
1536
1537 QAbstractItemView* DolphinView::ViewAccessor::itemView() const
1538 {
1539 if (m_iconsView) {
1540 return m_iconsView;
1541 }
1542
1543 if (m_detailsView) {
1544 return m_detailsView;
1545 }
1546
1547 if (m_columnsContainer) {
1548 return m_columnsContainer->activeColumn();
1549 }
1550
1551 return 0;
1552 }
1553
1554 KFileItemDelegate* DolphinView::ViewAccessor::itemDelegate() const
1555 {
1556 return static_cast<KFileItemDelegate*>(itemView()->itemDelegate());
1557 }
1558
1559 QWidget* DolphinView::ViewAccessor::layoutTarget() const
1560 {
1561 if (m_columnsContainer) {
1562 return m_columnsContainer;
1563 }
1564 return itemView();
1565 }
1566
1567 void DolphinView::ViewAccessor::setRootUrl(const KUrl& rootUrl)
1568 {
1569 m_rootUrl = rootUrl;
1570 }
1571
1572 KUrl DolphinView::ViewAccessor::rootUrl() const
1573 {
1574 return m_columnsContainer ? m_columnsContainer->rootUrl() : m_rootUrl;
1575 }
1576
1577 bool DolphinView::ViewAccessor::supportsCategorizedSorting() const
1578 {
1579 return m_iconsView != 0;
1580 }
1581
1582 bool DolphinView::ViewAccessor::itemsExpandable() const
1583 {
1584 return m_detailsView && m_detailsView->itemsExpandable();
1585 }
1586
1587 QSet<KUrl> DolphinView::ViewAccessor::expandedUrls() const
1588 {
1589 if (m_detailsView) {
1590 return m_detailsView->expandedUrls();
1591 }
1592
1593 return QSet<KUrl>();
1594 }
1595
1596 const DolphinDetailsViewExpander* DolphinView::ViewAccessor::setExpandedUrls(const QSet<KUrl>& urlsToExpand)
1597 {
1598 if (m_detailsView && m_detailsView->itemsExpandable() && !urlsToExpand.isEmpty()) {
1599 // Check if another expander is already active and stop it if necessary.
1600 if(!m_detailsViewExpander.isNull()) {
1601 m_detailsViewExpander->stop();
1602 }
1603
1604 m_detailsViewExpander = new DolphinDetailsViewExpander(m_detailsView, urlsToExpand);
1605 return m_detailsViewExpander;
1606 }
1607 else {
1608 return 0;
1609 }
1610 }
1611
1612 bool DolphinView::ViewAccessor::reloadOnAdditionalInfoChange() const
1613 {
1614 // the details view requires no reloading of the directory, as it maps
1615 // the file item delegate info to its columns internally
1616 return m_detailsView != 0;
1617 }
1618
1619 DolphinModel* DolphinView::ViewAccessor::dirModel() const
1620 {
1621 return static_cast<DolphinModel*>(proxyModel()->sourceModel());
1622 }
1623
1624 DolphinSortFilterProxyModel* DolphinView::ViewAccessor::proxyModel() const
1625 {
1626 if (m_columnsContainer) {
1627 return static_cast<DolphinSortFilterProxyModel*>(m_columnsContainer->activeColumn()->model());
1628 }
1629 return m_proxyModel;
1630 }
1631
1632 KDirLister* DolphinView::ViewAccessor::dirLister() const
1633 {
1634 return dirModel()->dirLister();
1635 }
1636
1637 #include "dolphinview.moc"