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