]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/dolphinview.cpp
Initialize the zoom slider correctly on startup (regression introduced in 4.5.0)
[dolphin.git] / src / views / dolphinview.cpp
1 /***************************************************************************
2 * Copyright (C) 2006-2009 by Peter Penz <peter.penz@gmx.at> *
3 * Copyright (C) 2006 by Gregor Kališnik <gregor@podnapisi.net> *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 ***************************************************************************/
20
21 #include "dolphinview.h"
22
23 #include <QAbstractItemView>
24 #include <QApplication>
25 #include <QClipboard>
26 #include <QKeyEvent>
27 #include <QItemSelection>
28 #include <QBoxLayout>
29 #include <QTimer>
30 #include <QScrollBar>
31
32 #include <kactioncollection.h>
33 #include <kcolorscheme.h>
34 #include <kdirlister.h>
35 #include <kiconeffect.h>
36 #include <kfileitem.h>
37 #include <klocale.h>
38 #include <kio/deletejob.h>
39 #include <kio/netaccess.h>
40 #include <kio/previewjob.h>
41 #include <kjob.h>
42 #include <kmenu.h>
43 #include <kmessagebox.h>
44 #include <kmimetyperesolver.h>
45 #include <konq_fileitemcapabilities.h>
46 #include <konq_operations.h>
47 #include <konqmimedata.h>
48 #include <kstringhandler.h>
49 #include <ktoggleaction.h>
50 #include <kurl.h>
51
52 #include "additionalinfoaccessor.h"
53 #include "dolphinmodel.h"
54 #include "dolphincolumnviewcontainer.h"
55 #include "dolphinviewcontroller.h"
56 #include "dolphindetailsview.h"
57 #include "dolphinfileitemdelegate.h"
58 #include "dolphinnewmenuobserver.h"
59 #include "dolphinsortfilterproxymodel.h"
60 #include "dolphin_detailsmodesettings.h"
61 #include "dolphiniconsview.h"
62 #include "dolphin_generalsettings.h"
63 #include "draganddrophelper.h"
64 #include "renamedialog.h"
65 #include "settings/dolphinsettings.h"
66 #include "viewmodecontroller.h"
67 #include "viewproperties.h"
68 #include "zoomlevelinfo.h"
69 #include "dolphindetailsviewexpander.h"
70
71 /**
72 * Helper function for sorting items with qSort() in
73 * DolphinView::renameSelectedItems().
74 */
75 bool lessThan(const KFileItem& item1, const KFileItem& item2)
76 {
77 return KStringHandler::naturalCompare(item1.name(), item2.name()) < 0;
78 }
79
80 DolphinView::DolphinView(QWidget* parent,
81 const KUrl& url,
82 DolphinSortFilterProxyModel* proxyModel) :
83 QWidget(parent),
84 m_active(true),
85 m_showPreview(false),
86 m_storedCategorizedSorting(false),
87 m_tabsForFiles(false),
88 m_isContextMenuOpen(false),
89 m_ignoreViewProperties(false),
90 m_assureVisibleCurrentIndex(false),
91 m_mode(DolphinView::IconsView),
92 m_topLayout(0),
93 m_dolphinViewController(0),
94 m_viewModeController(0),
95 m_viewAccessor(proxyModel),
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 return;
544 }
545
546 // The selection model might change in the case of the column view. Disconnect
547 // from the current selection model and reconnect later after the URL switch.
548 QAbstractItemView* view = m_viewAccessor.itemView();
549 disconnect(view->selectionModel(), SIGNAL(selectionChanged(QItemSelection, QItemSelection)),
550 this, SLOT(slotSelectionChanged(QItemSelection, QItemSelection)));
551
552 m_newFileNames.clear();
553
554 m_viewModeController->setUrl(url); // emits urlChanged, which we forward
555 m_viewAccessor.prepareUrlChange(url);
556 applyViewProperties();
557 loadDirectory(url);
558
559 // When changing the URL there is no need to keep the version
560 // data of the previous URL.
561 m_viewAccessor.dirModel()->clearVersionData();
562
563 emit startedPathLoading(url);
564
565 // Reconnect to the (probably) new selection model
566 view = m_viewAccessor.itemView();
567 connect(view->selectionModel(), SIGNAL(selectionChanged(QItemSelection, QItemSelection)),
568 this, SLOT(slotSelectionChanged(QItemSelection, QItemSelection)));
569 }
570
571 void DolphinView::selectAll()
572 {
573 m_viewAccessor.itemView()->selectAll();
574 }
575
576 void DolphinView::invertSelection()
577 {
578 QItemSelectionModel* selectionModel = m_viewAccessor.itemView()->selectionModel();
579 const QAbstractItemModel* itemModel = selectionModel->model();
580
581 const QModelIndex topLeft = itemModel->index(0, 0);
582 const QModelIndex bottomRight = itemModel->index(itemModel->rowCount() - 1,
583 itemModel->columnCount() - 1);
584
585 const QItemSelection selection(topLeft, bottomRight);
586 selectionModel->select(selection, QItemSelectionModel::Toggle);
587 }
588
589 void DolphinView::clearSelection()
590 {
591 m_viewAccessor.itemView()->clearSelection();
592 }
593
594 void DolphinView::renameSelectedItems()
595 {
596 KFileItemList items = selectedItems();
597 const int itemCount = items.count();
598 if (itemCount < 1) {
599 return;
600 }
601
602 if (itemCount > 1) {
603 // More than one item has been selected for renaming. Open
604 // a rename dialog and rename all items afterwards.
605 QPointer<RenameDialog> dialog = new RenameDialog(this, items);
606 if (dialog->exec() == QDialog::Rejected) {
607 delete dialog;
608 return;
609 }
610
611 const QString newName = dialog->newName();
612 if (newName.isEmpty()) {
613 emit errorMessage(dialog->errorString());
614 delete dialog;
615 return;
616 }
617 delete dialog;
618
619 // the selection would be invalid after renaming the items, so just clear
620 // it before
621 clearSelection();
622
623 // TODO: check how this can be integrated into KIO::FileUndoManager/KonqOperations
624 // as one operation instead of n rename operations like it is done now...
625 Q_ASSERT(newName.contains('#'));
626
627 // currently the items are sorted by the selection order, resort
628 // them by the file name
629 qSort(items.begin(), items.end(), lessThan);
630
631 // iterate through all selected items and rename them...
632 int index = 1;
633 foreach (const KFileItem& item, items) {
634 const KUrl& oldUrl = item.url();
635 QString number;
636 number.setNum(index++);
637
638 QString name = newName;
639 name.replace('#', number);
640
641 if (oldUrl.fileName() != name) {
642 KUrl newUrl = oldUrl;
643 newUrl.setFileName(name);
644 KonqOperations::rename(this, oldUrl, newUrl);
645 }
646 }
647 } else if (DolphinSettings::instance().generalSettings()->renameInline()) {
648 Q_ASSERT(itemCount == 1);
649 const QModelIndex dirIndex = m_viewAccessor.dirModel()->indexForItem(items.first());
650 const QModelIndex proxyIndex = m_viewAccessor.proxyModel()->mapFromSource(dirIndex);
651 m_viewAccessor.itemView()->edit(proxyIndex);
652 } else {
653 Q_ASSERT(itemCount == 1);
654
655 QPointer<RenameDialog> dialog = new RenameDialog(this, items);
656 if (dialog->exec() == QDialog::Rejected) {
657 delete dialog;
658 return;
659 }
660
661 const QString newName = dialog->newName();
662 if (newName.isEmpty()) {
663 emit errorMessage(dialog->errorString());
664 delete dialog;
665 return;
666 }
667 delete dialog;
668
669 const KUrl& oldUrl = items.first().url();
670 KUrl newUrl = oldUrl;
671 newUrl.setFileName(newName);
672 KonqOperations::rename(this, oldUrl, newUrl);
673 }
674
675 // assure that the current index remains visible when KDirLister
676 // will notify the view about changed items
677 m_assureVisibleCurrentIndex = true;
678 }
679
680 void DolphinView::trashSelectedItems()
681 {
682 const KUrl::List list = simplifiedSelectedUrls();
683 KonqOperations::del(this, KonqOperations::TRASH, list);
684 }
685
686 void DolphinView::deleteSelectedItems()
687 {
688 const KUrl::List list = simplifiedSelectedUrls();
689 const bool del = KonqOperations::askDeleteConfirmation(list,
690 KonqOperations::DEL,
691 KonqOperations::DEFAULT_CONFIRMATION,
692 this);
693
694 if (del) {
695 KIO::Job* job = KIO::del(list);
696 connect(job, SIGNAL(result(KJob*)),
697 this, SLOT(slotDeleteFileFinished(KJob*)));
698 }
699 }
700
701 void DolphinView::cutSelectedItems()
702 {
703 QMimeData* mimeData = selectionMimeData();
704 KonqMimeData::addIsCutSelection(mimeData, true);
705 QApplication::clipboard()->setMimeData(mimeData);
706 }
707
708 void DolphinView::copySelectedItems()
709 {
710 QMimeData* mimeData = selectionMimeData();
711 QApplication::clipboard()->setMimeData(mimeData);
712 }
713
714 void DolphinView::paste()
715 {
716 pasteToUrl(url());
717 }
718
719 void DolphinView::pasteIntoFolder()
720 {
721 const KFileItemList items = selectedItems();
722 if ((items.count() == 1) && items.first().isDir()) {
723 pasteToUrl(items.first().url());
724 }
725 }
726
727 void DolphinView::setShowPreview(bool show)
728 {
729 if (m_showPreview == show) {
730 return;
731 }
732
733 const KUrl viewPropsUrl = rootUrl();
734 ViewProperties props(viewPropsUrl);
735 props.setShowPreview(show);
736
737 m_showPreview = show;
738 const int oldZoomLevel = m_viewModeController->zoomLevel();
739 emit showPreviewChanged();
740
741 // Enabling or disabling the preview might change the icon size of the view.
742 // As the view does not emit a signal when the icon size has been changed,
743 // the used zoom level of the controller must be adjusted manually:
744 updateZoomLevel(oldZoomLevel);
745 }
746
747 void DolphinView::setShowHiddenFiles(bool show)
748 {
749 if (m_viewAccessor.dirLister()->showingDotFiles() == show) {
750 return;
751 }
752
753 const KUrl viewPropsUrl = rootUrl();
754 ViewProperties props(viewPropsUrl);
755 props.setShowHiddenFiles(show);
756
757 m_viewAccessor.dirLister()->setShowingDotFiles(show);
758 emit showHiddenFilesChanged();
759 }
760
761 void DolphinView::setCategorizedSorting(bool categorized)
762 {
763 if (categorized == categorizedSorting()) {
764 return;
765 }
766
767 // setCategorizedSorting(true) may only get invoked
768 // if the view supports categorized sorting
769 Q_ASSERT(!categorized || supportsCategorizedSorting());
770
771 ViewProperties props(rootUrl());
772 props.setCategorizedSorting(categorized);
773 props.save();
774
775 m_storedCategorizedSorting = categorized;
776 m_viewAccessor.proxyModel()->setCategorizedModel(categorized);
777
778 emit categorizedSortingChanged();
779 }
780
781 void DolphinView::toggleSortOrder()
782 {
783 const Qt::SortOrder order = (sortOrder() == Qt::AscendingOrder) ?
784 Qt::DescendingOrder :
785 Qt::AscendingOrder;
786 setSortOrder(order);
787 }
788
789 void DolphinView::toggleSortFoldersFirst()
790 {
791 setSortFoldersFirst(!sortFoldersFirst());
792 }
793
794 void DolphinView::toggleAdditionalInfo(QAction* action)
795 {
796 const KFileItemDelegate::Information info =
797 static_cast<KFileItemDelegate::Information>(action->data().toInt());
798
799 KFileItemDelegate::InformationList list = additionalInfo();
800
801 const bool show = action->isChecked();
802
803 const int index = list.indexOf(info);
804 const bool containsInfo = (index >= 0);
805 if (show && !containsInfo) {
806 list.append(info);
807 setAdditionalInfo(list);
808 } else if (!show && containsInfo) {
809 list.removeAt(index);
810 setAdditionalInfo(list);
811 Q_ASSERT(list.indexOf(info) < 0);
812 }
813 }
814
815 void DolphinView::mouseReleaseEvent(QMouseEvent* event)
816 {
817 QWidget::mouseReleaseEvent(event);
818 setActive(true);
819 }
820
821 bool DolphinView::eventFilter(QObject* watched, QEvent* event)
822 {
823 switch (event->type()) {
824 case QEvent::FocusIn:
825 if (watched == m_viewAccessor.itemView()) {
826 m_dolphinViewController->requestActivation();
827 }
828 break;
829
830 case QEvent::DragEnter:
831 if (watched == m_viewAccessor.itemView()->viewport()) {
832 setActive(true);
833 }
834 break;
835
836 case QEvent::KeyPress:
837 if (watched == m_viewAccessor.itemView()) {
838 // clear the selection when Escape has been pressed
839 QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
840 if (keyEvent->key() == Qt::Key_Escape) {
841 clearSelection();
842 }
843 }
844 break;
845
846 case QEvent::Wheel:
847 if (watched == m_viewAccessor.itemView()->viewport()) {
848 // Ctrl+wheel events should cause icon zooming, but not if the left mouse button is pressed
849 // (the user is probably trying to scroll during a selection in that case)
850 QWheelEvent* wheelEvent = static_cast<QWheelEvent*>(event);
851 if (wheelEvent->modifiers() & Qt::ControlModifier && !(wheelEvent->buttons() & Qt::LeftButton)) {
852 const int delta = wheelEvent->delta();
853 const int level = zoomLevel();
854 if (delta > 0) {
855 setZoomLevel(level + 1);
856 } else if (delta < 0) {
857 setZoomLevel(level - 1);
858 }
859 return true;
860 }
861 }
862 break;
863
864 default:
865 break;
866 }
867
868 return QWidget::eventFilter(watched, event);
869 }
870
871 void DolphinView::activate()
872 {
873 setActive(true);
874 }
875
876 void DolphinView::triggerItem(const KFileItem& item)
877 {
878 const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
879 if ((modifier & Qt::ShiftModifier) || (modifier & Qt::ControlModifier)) {
880 // items are selected by the user, hence don't trigger the
881 // item specified by 'index'
882 return;
883 }
884
885 // TODO: the m_isContextMenuOpen check is a workaround for Qt-issue 207192
886 if (item.isNull() || m_isContextMenuOpen) {
887 return;
888 }
889
890 emit itemTriggered(item); // caught by DolphinViewContainer or DolphinPart
891 }
892
893 void DolphinView::slotSelectionChanged(const QItemSelection& selected, const QItemSelection& deselected)
894 {
895 const int count = selectedItemsCount();
896 const bool selectionStateChanged = ((count > 0) && (selected.count() == count)) ||
897 ((count == 0) && !deselected.isEmpty());
898
899 // If nothing has been selected before and something got selected (or if something
900 // was selected before and now nothing is selected) the selectionChangedSignal must
901 // be emitted asynchronously as fast as possible to update the edit-actions.
902 m_selectionChangedTimer->setInterval(selectionStateChanged ? 0 : 300);
903 m_selectionChangedTimer->start();
904 }
905
906 void DolphinView::emitSelectionChangedSignal()
907 {
908 emit selectionChanged(DolphinView::selectedItems());
909 }
910
911 void DolphinView::openContextMenu(const QPoint& pos,
912 const QList<QAction*>& customActions)
913 {
914 KFileItem item;
915 const QModelIndex index = m_viewAccessor.itemView()->indexAt(pos);
916 if (index.isValid() && (index.column() == DolphinModel::Name)) {
917 const QModelIndex dolphinModelIndex = m_viewAccessor.proxyModel()->mapToSource(index);
918 item = m_viewAccessor.dirModel()->itemForIndex(dolphinModelIndex);
919 }
920
921 m_isContextMenuOpen = true; // TODO: workaround for Qt-issue 207192
922 emit requestContextMenu(item, url(), customActions);
923 m_isContextMenuOpen = false;
924 }
925
926 void DolphinView::dropUrls(const KFileItem& destItem,
927 const KUrl& destPath,
928 QDropEvent* event)
929 {
930 addNewFileNames(event->mimeData());
931 DragAndDropHelper::instance().dropUrls(destItem, destPath, event, this);
932 }
933
934 void DolphinView::updateSorting(DolphinView::Sorting sorting)
935 {
936 ViewProperties props(rootUrl());
937 props.setSorting(sorting);
938
939 m_viewAccessor.proxyModel()->setSorting(sorting);
940
941 emit sortingChanged(sorting);
942 }
943
944 void DolphinView::updateSortOrder(Qt::SortOrder order)
945 {
946 ViewProperties props(rootUrl());
947 props.setSortOrder(order);
948
949 m_viewAccessor.proxyModel()->setSortOrder(order);
950
951 emit sortOrderChanged(order);
952 }
953
954 void DolphinView::updateSortFoldersFirst(bool foldersFirst)
955 {
956 ViewProperties props(rootUrl());
957 props.setSortFoldersFirst(foldersFirst);
958
959 m_viewAccessor.proxyModel()->setSortFoldersFirst(foldersFirst);
960
961 emit sortFoldersFirstChanged(foldersFirst);
962 }
963
964 void DolphinView::updateAdditionalInfo(const KFileItemDelegate::InformationList& info)
965 {
966 ViewProperties props(rootUrl());
967 props.setAdditionalInfo(info);
968 props.save();
969
970 m_viewAccessor.itemDelegate()->setShowInformation(info);
971
972 emit additionalInfoChanged();
973 }
974
975 void DolphinView::updateAdditionalInfoActions(KActionCollection* collection)
976 {
977 const AdditionalInfoAccessor& infoAccessor = AdditionalInfoAccessor::instance();
978
979 const KFileItemDelegate::InformationList checkedInfo = m_viewAccessor.itemDelegate()->showInformation();
980 const KFileItemDelegate::InformationList infoKeys = infoAccessor.keys();
981
982 const bool enable = (m_mode == DolphinView::DetailsView) ||
983 (m_mode == DolphinView::IconsView);
984
985 foreach (const KFileItemDelegate::Information& info, infoKeys) {
986 const QString name = infoAccessor.actionCollectionName(info, AdditionalInfoAccessor::AdditionalInfoType);
987 QAction* action = collection->action(name);
988 Q_ASSERT(action != 0);
989 action->setEnabled(enable);
990 action->setChecked(checkedInfo.contains(info));
991 }
992 }
993
994 QPair<bool, QString> DolphinView::pasteInfo() const
995 {
996 return KonqOperations::pasteInfo(url());
997 }
998
999 void DolphinView::setTabsForFilesEnabled(bool tabsForFiles)
1000 {
1001 m_tabsForFiles = tabsForFiles;
1002 }
1003
1004 bool DolphinView::isTabsForFilesEnabled() const
1005 {
1006 return m_tabsForFiles;
1007 }
1008
1009 bool DolphinView::itemsExpandable() const
1010 {
1011 return m_viewAccessor.itemsExpandable();
1012 }
1013
1014 void DolphinView::restoreState(QDataStream& stream)
1015 {
1016 // current item
1017 stream >> m_activeItemUrl;
1018
1019 // view position
1020 stream >> m_restoredContentsPosition;
1021
1022 // expanded folders (only relevant for the details view - will be ignored by the view in other view modes)
1023 QSet<KUrl> urlsToExpand;
1024 stream >> urlsToExpand;
1025 const DolphinDetailsViewExpander* expander = m_viewAccessor.setExpandedUrls(urlsToExpand);
1026 if (expander != 0) {
1027 m_expanderActive = true;
1028 connect (expander, SIGNAL(completed()), this, SLOT(slotLoadingCompleted()));
1029 }
1030 else {
1031 m_expanderActive = false;
1032 }
1033 }
1034
1035 void DolphinView::saveState(QDataStream& stream)
1036 {
1037 // current item
1038 KFileItem currentItem;
1039 const QAbstractItemView* view = m_viewAccessor.itemView();
1040
1041 if (view != 0) {
1042 const QModelIndex proxyIndex = view->currentIndex();
1043 const QModelIndex dirModelIndex = m_viewAccessor.proxyModel()->mapToSource(proxyIndex);
1044 currentItem = m_viewAccessor.dirModel()->itemForIndex(dirModelIndex);
1045 }
1046
1047 KUrl currentUrl;
1048 if (!currentItem.isNull()) {
1049 currentUrl = currentItem.url();
1050 }
1051
1052 stream << currentUrl;
1053
1054 // view position
1055 const int x = view->horizontalScrollBar()->value();
1056 const int y = view->verticalScrollBar()->value();
1057 stream << QPoint(x, y);
1058
1059 // expanded folders (only relevant for the details view - the set will be empty in other view modes)
1060 stream << m_viewAccessor.expandedUrls();
1061 }
1062
1063 void DolphinView::observeCreatedItem(const KUrl& url)
1064 {
1065 m_createdItemUrl = url;
1066 connect(m_viewAccessor.dirModel(), SIGNAL(rowsInserted(const QModelIndex&, int, int)),
1067 this, SLOT(selectAndScrollToCreatedItem()));
1068 }
1069
1070 void DolphinView::selectAndScrollToCreatedItem()
1071 {
1072 const QModelIndex dirIndex = m_viewAccessor.dirModel()->indexForUrl(m_createdItemUrl);
1073 if (dirIndex.isValid()) {
1074 const QModelIndex proxyIndex = m_viewAccessor.proxyModel()->mapFromSource(dirIndex);
1075 m_viewAccessor.itemView()->setCurrentIndex(proxyIndex);
1076 }
1077
1078 disconnect(m_viewAccessor.dirModel(), SIGNAL(rowsInserted(const QModelIndex&, int, int)),
1079 this, SLOT(selectAndScrollToCreatedItem()));
1080 m_createdItemUrl = KUrl();
1081 }
1082
1083 void DolphinView::showHoverInformation(const KFileItem& item)
1084 {
1085 emit requestItemInfo(item);
1086 }
1087
1088 void DolphinView::clearHoverInformation()
1089 {
1090 emit requestItemInfo(KFileItem());
1091 }
1092
1093 void DolphinView::slotDeleteFileFinished(KJob* job)
1094 {
1095 if (job->error() == 0) {
1096 emit operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
1097 } else if (job->error() != KIO::ERR_USER_CANCELED) {
1098 emit errorMessage(job->errorString());
1099 }
1100 }
1101
1102 void DolphinView::slotDirListerCompleted()
1103 {
1104 if (!m_expanderActive) {
1105 slotLoadingCompleted();
1106 }
1107
1108 if (!m_newFileNames.isEmpty()) {
1109 // select all newly added items created by a paste operation or
1110 // a drag & drop operation, and clear the previous selection
1111 m_viewAccessor.itemView()->clearSelection();
1112 const int rowCount = m_viewAccessor.proxyModel()->rowCount();
1113 QItemSelection selection;
1114 for (int row = 0; row < rowCount; ++row) {
1115 const QModelIndex proxyIndex = m_viewAccessor.proxyModel()->index(row, 0);
1116 const QModelIndex dirIndex = m_viewAccessor.proxyModel()->mapToSource(proxyIndex);
1117 const KUrl url = m_viewAccessor.dirModel()->itemForIndex(dirIndex).url();
1118 if (m_newFileNames.contains(url.fileName())) {
1119 selection.merge(QItemSelection(proxyIndex, proxyIndex), QItemSelectionModel::Select);
1120 }
1121 }
1122 m_viewAccessor.itemView()->selectionModel()->select(selection, QItemSelectionModel::Select);
1123
1124 m_newFileNames.clear();
1125 }
1126 }
1127
1128 void DolphinView::slotLoadingCompleted()
1129 {
1130 m_expanderActive = false;
1131
1132 if (!m_activeItemUrl.isEmpty()) {
1133 // assure that the current item remains visible
1134 const QModelIndex dirIndex = m_viewAccessor.dirModel()->indexForUrl(m_activeItemUrl);
1135 if (dirIndex.isValid()) {
1136 const QModelIndex proxyIndex = m_viewAccessor.proxyModel()->mapFromSource(dirIndex);
1137 QAbstractItemView* view = m_viewAccessor.itemView();
1138 const bool clearSelection = !hasSelection();
1139 view->setCurrentIndex(proxyIndex);
1140 if (clearSelection) {
1141 view->clearSelection();
1142 }
1143 m_activeItemUrl.clear();
1144 }
1145 }
1146
1147 if (!m_selectedItems.isEmpty()) {
1148 const KUrl& baseUrl = url();
1149 KUrl url;
1150 QItemSelection newSelection;
1151 foreach(const KFileItem& item, m_selectedItems) {
1152 url = item.url().upUrl();
1153 if (baseUrl.equals(url, KUrl::CompareWithoutTrailingSlash)) {
1154 QModelIndex index = m_viewAccessor.proxyModel()->mapFromSource(m_viewAccessor.dirModel()->indexForItem(item));
1155 newSelection.select(index, index);
1156 }
1157 }
1158 m_viewAccessor.itemView()->selectionModel()->select(newSelection,
1159 QItemSelectionModel::ClearAndSelect
1160 | QItemSelectionModel::Current);
1161 m_selectedItems.clear();
1162 }
1163
1164 // Restore the contents position. This has to be done using a Qt::QueuedConnection
1165 // because the view might not be in its final state yet.
1166 QMetaObject::invokeMethod(this, "restoreContentsPosition", Qt::QueuedConnection);
1167 }
1168
1169 void DolphinView::slotRefreshItems()
1170 {
1171 if (m_assureVisibleCurrentIndex) {
1172 m_assureVisibleCurrentIndex = false;
1173 m_viewAccessor.itemView()->scrollTo(m_viewAccessor.itemView()->currentIndex());
1174 }
1175 }
1176
1177 void DolphinView::loadDirectory(const KUrl& url, bool reload)
1178 {
1179 if (!url.isValid()) {
1180 const QString location(url.pathOrUrl());
1181 if (location.isEmpty()) {
1182 emit errorMessage(i18nc("@info:status", "The location is empty."));
1183 } else {
1184 emit errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location));
1185 }
1186 return;
1187 }
1188
1189 KDirLister* dirLister = m_viewAccessor.dirLister();
1190 dirLister->openUrl(url, reload ? KDirLister::Reload : KDirLister::NoFlags);
1191 }
1192
1193 void DolphinView::applyViewProperties()
1194 {
1195 if (m_ignoreViewProperties) {
1196 return;
1197 }
1198
1199 const ViewProperties props(rootUrl());
1200
1201 const Mode mode = props.viewMode();
1202 if (m_mode != mode) {
1203 const int oldZoomLevel = m_viewModeController->zoomLevel();
1204
1205 m_mode = mode;
1206 createView();
1207 emit modeChanged();
1208
1209 updateZoomLevel(oldZoomLevel);
1210 }
1211 if (m_viewAccessor.itemView() == 0) {
1212 createView();
1213 }
1214 Q_ASSERT(m_viewAccessor.itemView() != 0);
1215 Q_ASSERT(m_viewAccessor.itemDelegate() != 0);
1216
1217 const bool showHiddenFiles = props.showHiddenFiles();
1218 if (showHiddenFiles != m_viewAccessor.dirLister()->showingDotFiles()) {
1219 m_viewAccessor.dirLister()->setShowingDotFiles(showHiddenFiles);
1220 emit showHiddenFilesChanged();
1221 }
1222
1223 m_storedCategorizedSorting = props.categorizedSorting();
1224 const bool categorized = m_storedCategorizedSorting && supportsCategorizedSorting();
1225 if (categorized != m_viewAccessor.proxyModel()->isCategorizedModel()) {
1226 m_viewAccessor.proxyModel()->setCategorizedModel(categorized);
1227 emit categorizedSortingChanged();
1228 }
1229
1230 const DolphinView::Sorting sorting = props.sorting();
1231 if (sorting != m_viewAccessor.proxyModel()->sorting()) {
1232 m_viewAccessor.proxyModel()->setSorting(sorting);
1233 emit sortingChanged(sorting);
1234 }
1235
1236 const Qt::SortOrder sortOrder = props.sortOrder();
1237 if (sortOrder != m_viewAccessor.proxyModel()->sortOrder()) {
1238 m_viewAccessor.proxyModel()->setSortOrder(sortOrder);
1239 emit sortOrderChanged(sortOrder);
1240 }
1241
1242 const bool sortFoldersFirst = props.sortFoldersFirst();
1243 if (sortFoldersFirst != m_viewAccessor.proxyModel()->sortFoldersFirst()) {
1244 m_viewAccessor.proxyModel()->setSortFoldersFirst(sortFoldersFirst);
1245 emit sortFoldersFirstChanged(sortFoldersFirst);
1246 }
1247
1248 KFileItemDelegate::InformationList info = props.additionalInfo();
1249 if (info != m_viewAccessor.itemDelegate()->showInformation()) {
1250 m_viewAccessor.itemDelegate()->setShowInformation(info);
1251 emit additionalInfoChanged();
1252 }
1253
1254 const bool showPreview = props.showPreview();
1255 if (showPreview != m_showPreview) {
1256 m_showPreview = showPreview;
1257 const int oldZoomLevel = m_viewModeController->zoomLevel();
1258 emit showPreviewChanged();
1259
1260 // Enabling or disabling the preview might change the icon size of the view.
1261 // As the view does not emit a signal when the icon size has been changed,
1262 // the used zoom level of the controller must be adjusted manually:
1263 updateZoomLevel(oldZoomLevel);
1264 }
1265
1266 if (DolphinSettings::instance().generalSettings()->globalViewProps()) {
1267 // During the lifetime of a DolphinView instance the global view properties
1268 // should not be changed. This allows e. g. to split a view and use different
1269 // view properties for each view.
1270 m_ignoreViewProperties = true;
1271 }
1272 }
1273
1274 void DolphinView::createView()
1275 {
1276 QAbstractItemView* view = m_viewAccessor.itemView();
1277 if ((view != 0) && (view->selectionModel() != 0)) {
1278 disconnect(view->selectionModel(), SIGNAL(selectionChanged(QItemSelection, QItemSelection)),
1279 this, SLOT(slotSelectionChanged(QItemSelection, QItemSelection)));
1280 }
1281
1282 deleteView();
1283
1284 Q_ASSERT(m_viewAccessor.itemView() == 0);
1285 m_viewAccessor.createView(this, m_dolphinViewController, m_viewModeController, m_mode);
1286
1287 view = m_viewAccessor.itemView();
1288 Q_ASSERT(view != 0);
1289 view->installEventFilter(this);
1290 view->viewport()->installEventFilter(this);
1291
1292 m_dolphinViewController->setItemView(view);
1293
1294 const int zoomLevel = ZoomLevelInfo::zoomLevelForIconSize(view->iconSize());
1295 m_viewModeController->setZoomLevel(zoomLevel);
1296
1297 connect(view->selectionModel(), SIGNAL(selectionChanged(QItemSelection, QItemSelection)),
1298 this, SLOT(slotSelectionChanged(QItemSelection, QItemSelection)));
1299
1300 setFocusProxy(m_viewAccessor.layoutTarget());
1301 m_topLayout->insertWidget(1, m_viewAccessor.layoutTarget());
1302 }
1303
1304 void DolphinView::deleteView()
1305 {
1306 QAbstractItemView* view = m_viewAccessor.itemView();
1307 if (view != 0) {
1308 // It's important to set the keyboard focus to the parent
1309 // before deleting the view: Otherwise when having a split
1310 // view the other view will get the focus and will request
1311 // an activation (see DolphinView::eventFilter()).
1312 setFocusProxy(0);
1313 setFocus();
1314
1315 m_topLayout->removeWidget(view);
1316 view->close();
1317
1318 // disconnect all signal/slots
1319 disconnect(view);
1320 m_viewModeController->disconnect(view);
1321 view->disconnect();
1322
1323 m_viewAccessor.deleteView();
1324 }
1325 }
1326
1327 void DolphinView::pasteToUrl(const KUrl& url)
1328 {
1329 addNewFileNames(QApplication::clipboard()->mimeData());
1330 KonqOperations::doPaste(this, url);
1331 }
1332
1333 void DolphinView::updateZoomLevel(int oldZoomLevel)
1334 {
1335 const int newZoomLevel = ZoomLevelInfo::zoomLevelForIconSize(m_viewAccessor.itemView()->iconSize());
1336 if (oldZoomLevel != newZoomLevel) {
1337 m_viewModeController->setZoomLevel(newZoomLevel);
1338 emit zoomLevelChanged(newZoomLevel);
1339 }
1340 }
1341
1342 KUrl::List DolphinView::simplifiedSelectedUrls() const
1343 {
1344 KUrl::List list = selectedUrls();
1345 if (itemsExpandable() ) {
1346 list = KDirModel::simplifiedUrlList(list);
1347 }
1348 return list;
1349 }
1350
1351 QMimeData* DolphinView::selectionMimeData() const
1352 {
1353 const QAbstractItemView* view = m_viewAccessor.itemView();
1354 Q_ASSERT((view != 0) && (view->selectionModel() != 0));
1355 const QItemSelection selection = m_viewAccessor.proxyModel()->mapSelectionToSource(view->selectionModel()->selection());
1356 return m_viewAccessor.dirModel()->mimeData(selection.indexes());
1357 }
1358
1359 void DolphinView::addNewFileNames(const QMimeData* mimeData)
1360 {
1361 const KUrl::List urls = KUrl::List::fromMimeData(mimeData);
1362 foreach (const KUrl& url, urls) {
1363 m_newFileNames.insert(url.fileName());
1364 }
1365 }
1366
1367 DolphinView::ViewAccessor::ViewAccessor(DolphinSortFilterProxyModel* proxyModel) :
1368 m_iconsView(0),
1369 m_detailsView(0),
1370 m_columnsContainer(0),
1371 m_proxyModel(proxyModel),
1372 m_dragSource(0)
1373 {
1374 }
1375
1376 DolphinView::ViewAccessor::~ViewAccessor()
1377 {
1378 delete m_dragSource;
1379 m_dragSource = 0;
1380 }
1381
1382 void DolphinView::ViewAccessor::createView(QWidget* parent,
1383 DolphinViewController* dolphinViewController,
1384 const ViewModeController* viewModeController,
1385 Mode mode)
1386 {
1387 Q_ASSERT(itemView() == 0);
1388
1389 switch (mode) {
1390 case IconsView:
1391 m_iconsView = new DolphinIconsView(parent,
1392 dolphinViewController,
1393 viewModeController,
1394 m_proxyModel);
1395 break;
1396
1397 case DetailsView:
1398 m_detailsView = new DolphinDetailsView(parent,
1399 dolphinViewController,
1400 viewModeController,
1401 m_proxyModel);
1402 break;
1403
1404 case ColumnView:
1405 m_columnsContainer = new DolphinColumnViewContainer(parent,
1406 dolphinViewController,
1407 viewModeController);
1408 break;
1409
1410 default:
1411 Q_ASSERT(false);
1412 }
1413 }
1414
1415 void DolphinView::ViewAccessor::deleteView()
1416 {
1417 QAbstractItemView* view = itemView();
1418 if (view != 0) {
1419 if (DragAndDropHelper::instance().isDragSource(view)) {
1420 // The view is a drag source (the feature "Open folders
1421 // during drag operations" is used). Deleting the view
1422 // during an ongoing drag operation is not allowed, so
1423 // this will postponed.
1424 if (m_dragSource != 0) {
1425 // the old stored view is obviously not the drag source anymore
1426 m_dragSource->deleteLater();
1427 m_dragSource = 0;
1428 }
1429 view->hide();
1430 m_dragSource = view;
1431 } else {
1432 view->deleteLater();
1433 }
1434 }
1435
1436 m_iconsView = 0;
1437 m_detailsView = 0;
1438
1439 if (m_columnsContainer != 0) {
1440 m_columnsContainer->deleteLater();
1441 }
1442 m_columnsContainer = 0;
1443 }
1444
1445
1446 void DolphinView::ViewAccessor::prepareUrlChange(const KUrl& url)
1447 {
1448 if (m_columnsContainer != 0) {
1449 m_columnsContainer->showColumn(url);
1450 }
1451 }
1452
1453 QAbstractItemView* DolphinView::ViewAccessor::itemView() const
1454 {
1455 if (m_iconsView != 0) {
1456 return m_iconsView;
1457 }
1458
1459 if (m_detailsView != 0) {
1460 return m_detailsView;
1461 }
1462
1463 if (m_columnsContainer != 0) {
1464 return m_columnsContainer->activeColumn();
1465 }
1466
1467 return 0;
1468 }
1469
1470 KFileItemDelegate* DolphinView::ViewAccessor::itemDelegate() const
1471 {
1472 return static_cast<KFileItemDelegate*>(itemView()->itemDelegate());
1473 }
1474
1475 QWidget* DolphinView::ViewAccessor::layoutTarget() const
1476 {
1477 if (m_columnsContainer != 0) {
1478 return m_columnsContainer;
1479 }
1480 return itemView();
1481 }
1482
1483 KUrl DolphinView::ViewAccessor::rootUrl() const
1484 {
1485 return (m_columnsContainer != 0) ? m_columnsContainer->rootUrl() : KUrl();
1486 }
1487
1488 bool DolphinView::ViewAccessor::supportsCategorizedSorting() const
1489 {
1490 return m_iconsView != 0;
1491 }
1492
1493 bool DolphinView::ViewAccessor::itemsExpandable() const
1494 {
1495 return (m_detailsView != 0) && m_detailsView->itemsExpandable();
1496 }
1497
1498
1499 QSet<KUrl> DolphinView::ViewAccessor::expandedUrls() const
1500 {
1501 if (m_detailsView != 0) {
1502 return m_detailsView->expandedUrls();
1503 }
1504
1505 return QSet<KUrl>();
1506 }
1507
1508 const DolphinDetailsViewExpander* DolphinView::ViewAccessor::setExpandedUrls(const QSet<KUrl>& urlsToExpand)
1509 {
1510 if ((m_detailsView != 0) && m_detailsView->itemsExpandable() && !urlsToExpand.isEmpty()) {
1511 // Check if another expander is already active and stop it if necessary.
1512 if(!m_detailsViewExpander.isNull()) {
1513 m_detailsViewExpander->stop();
1514 }
1515
1516 m_detailsViewExpander = new DolphinDetailsViewExpander(m_detailsView, urlsToExpand);
1517 return m_detailsViewExpander;
1518 }
1519 else {
1520 return 0;
1521 }
1522 }
1523
1524 bool DolphinView::ViewAccessor::reloadOnAdditionalInfoChange() const
1525 {
1526 // the details view requires no reloading of the directory, as it maps
1527 // the file item delegate info to its columns internally
1528 return m_detailsView != 0;
1529 }
1530
1531 DolphinModel* DolphinView::ViewAccessor::dirModel() const
1532 {
1533 return static_cast<DolphinModel*>(proxyModel()->sourceModel());
1534 }
1535
1536 DolphinSortFilterProxyModel* DolphinView::ViewAccessor::proxyModel() const
1537 {
1538 if (m_columnsContainer != 0) {
1539 return static_cast<DolphinSortFilterProxyModel*>(m_columnsContainer->activeColumn()->model());
1540 }
1541 return m_proxyModel;
1542 }
1543
1544 KDirLister* DolphinView::ViewAccessor::dirLister() const
1545 {
1546 return dirModel()->dirLister();
1547 }
1548
1549 void DolphinView::slotRedirection(const KUrl& oldUrl, const KUrl& newUrl)
1550 {
1551 if (oldUrl.equals(url(), KUrl::CompareWithoutTrailingSlash)) {
1552 emit redirection(oldUrl, newUrl);
1553 m_viewModeController->redirectToUrl(newUrl); // #186947
1554 }
1555 }
1556
1557 void DolphinView::restoreContentsPosition()
1558 {
1559 if (!m_restoredContentsPosition.isNull()) {
1560 const int x = m_restoredContentsPosition.x();
1561 const int y = m_restoredContentsPosition.y();
1562 m_restoredContentsPosition = QPoint();
1563
1564 QAbstractItemView* view = m_viewAccessor.itemView();
1565 Q_ASSERT(view != 0);
1566 view->horizontalScrollBar()->setValue(x);
1567 view->verticalScrollBar()->setValue(y);
1568 }
1569 }
1570
1571 #include "dolphinview.moc"