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