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