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