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