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