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