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