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