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