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