]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinview.cpp
If the root directory lister is different from the internally used directory lister...
[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::setContentsPosition(int x, int y)
351 {
352 QAbstractItemView* view = m_viewAccessor.itemView();
353 Q_ASSERT(view != 0);
354 view->horizontalScrollBar()->setValue(x);
355 view->verticalScrollBar()->setValue(y);
356
357 m_loadingDirectory = false;
358 }
359
360 void DolphinView::setRestoredContentsPosition(const QPoint& pos)
361 {
362 // TODO: This function is called by DolphinViewContainer.
363 // If it makes use of DolphinView::restoreState(...) to restore the
364 // view state in KDE 4.5, this function can be removed.
365 m_restoredContentsPosition = pos;
366 }
367
368 QPoint DolphinView::contentsPosition() const
369 {
370 // TODO: If DolphinViewContainer uses DolphinView::saveState(...) to save the
371 // view state in KDE 4.5, this code can be moved to DolphinView::saveState.
372 QAbstractItemView* view = m_viewAccessor.itemView();
373 Q_ASSERT(view != 0);
374 const int x = view->horizontalScrollBar()->value();
375 const int y = view->verticalScrollBar()->value();
376 return QPoint(x, y);
377 }
378
379 void DolphinView::setZoomLevel(int level)
380 {
381 if (level < ZoomLevelInfo::minimumLevel()) {
382 level = ZoomLevelInfo::minimumLevel();
383 } else if (level > ZoomLevelInfo::maximumLevel()) {
384 level = ZoomLevelInfo::maximumLevel();
385 }
386
387 if (level != zoomLevel()) {
388 m_controller->setZoomLevel(level);
389 emit zoomLevelChanged(level);
390 }
391 }
392
393 int DolphinView::zoomLevel() const
394 {
395 return m_controller->zoomLevel();
396 }
397
398 void DolphinView::setSorting(Sorting sorting)
399 {
400 if (sorting != this->sorting()) {
401 updateSorting(sorting);
402 }
403 }
404
405 DolphinView::Sorting DolphinView::sorting() const
406 {
407 return m_viewAccessor.proxyModel()->sorting();
408 }
409
410 void DolphinView::setSortOrder(Qt::SortOrder order)
411 {
412 if (sortOrder() != order) {
413 updateSortOrder(order);
414 }
415 }
416
417 Qt::SortOrder DolphinView::sortOrder() const
418 {
419 return m_viewAccessor.proxyModel()->sortOrder();
420 }
421
422 void DolphinView::setSortFoldersFirst(bool foldersFirst)
423 {
424 if (sortFoldersFirst() != foldersFirst) {
425 updateSortFoldersFirst(foldersFirst);
426 }
427 }
428
429 bool DolphinView::sortFoldersFirst() const
430 {
431 return m_viewAccessor.proxyModel()->sortFoldersFirst();
432 }
433
434 void DolphinView::setAdditionalInfo(KFileItemDelegate::InformationList info)
435 {
436 const KUrl viewPropsUrl = rootUrl();
437 ViewProperties props(viewPropsUrl);
438 props.setAdditionalInfo(info);
439 m_viewAccessor.itemDelegate()->setShowInformation(info);
440
441 emit additionalInfoChanged();
442
443 if (m_viewAccessor.reloadOnAdditionalInfoChange()) {
444 loadDirectory(viewPropsUrl);
445 }
446 }
447
448 KFileItemDelegate::InformationList DolphinView::additionalInfo() const
449 {
450 return m_viewAccessor.itemDelegate()->showInformation();
451 }
452
453 void DolphinView::reload()
454 {
455 QByteArray viewState;
456 QDataStream saveStream(&viewState, QIODevice::WriteOnly);
457 saveState(saveStream);
458 m_selectedItems= selectedItems();
459
460 setUrl(url());
461 loadDirectory(url(), true);
462
463 QDataStream restoreStream(viewState);
464 restoreState(restoreStream);
465 }
466
467 void DolphinView::refresh()
468 {
469 m_ignoreViewProperties = false;
470
471 const bool oldActivationState = m_active;
472 const int oldZoomLevel = m_controller->zoomLevel();
473 m_active = true;
474
475 createView();
476 applyViewProperties();
477 reload();
478
479 setActive(oldActivationState);
480 updateZoomLevel(oldZoomLevel);
481 }
482
483 void DolphinView::updateView(const KUrl& url, const KUrl& rootUrl)
484 {
485 Q_UNUSED(rootUrl); // TODO: remove after columnview-cleanup has been finished
486
487 if (m_controller->url() == url) {
488 return;
489 }
490
491 m_controller->setUrl(url); // emits urlChanged, which we forward
492 m_viewAccessor.prepareUrlChange(url);
493 applyViewProperties();
494 loadDirectory(url);
495
496 // When changing the URL there is no need to keep the version
497 // data of the previous URL.
498 m_viewAccessor.dirModel()->clearVersionData();
499
500 emit startedPathLoading(url);
501 }
502
503 void DolphinView::setNameFilter(const QString& nameFilter)
504 {
505 m_controller->setNameFilter(nameFilter);
506 }
507
508 void DolphinView::calculateItemCount(int& fileCount,
509 int& folderCount,
510 KIO::filesize_t& totalFileSize) const
511 {
512 foreach (const KFileItem& item, m_viewAccessor.dirLister()->items()) {
513 if (item.isDir()) {
514 ++folderCount;
515 } else {
516 ++fileCount;
517 totalFileSize += item.size();
518 }
519 }
520 }
521
522 QString DolphinView::statusBarText() const
523 {
524 QString text;
525 int folderCount = 0;
526 int fileCount = 0;
527 KIO::filesize_t totalFileSize = 0;
528
529 if (hasSelection()) {
530 // give a summary of the status of the selected files
531 const KFileItemList list = selectedItems();
532 if (list.isEmpty()) {
533 // when an item is triggered, it is temporary selected but selectedItems()
534 // will return an empty list
535 return text;
536 }
537
538 KFileItemList::const_iterator it = list.begin();
539 const KFileItemList::const_iterator end = list.end();
540 while (it != end) {
541 const KFileItem& item = *it;
542 if (item.isDir()) {
543 ++folderCount;
544 } else {
545 ++fileCount;
546 totalFileSize += item.size();
547 }
548 ++it;
549 }
550
551 if (folderCount + fileCount == 1) {
552 // if only one item is selected, show the filename
553 const QString name = list.first().name();
554 text = (folderCount == 1) ? i18nc("@info:status", "<filename>%1</filename> selected", name) :
555 i18nc("@info:status", "<filename>%1</filename> selected (%2)",
556 name, KIO::convertSize(totalFileSize));
557 } else {
558 // at least 2 items are selected
559 const QString foldersText = i18ncp("@info:status", "1 Folder selected", "%1 Folders selected", folderCount);
560 const QString filesText = i18ncp("@info:status", "1 File selected", "%1 Files selected", fileCount);
561 if ((folderCount > 0) && (fileCount > 0)) {
562 text = i18nc("@info:status folders, files (size)", "%1, %2 (%3)",
563 foldersText, filesText, KIO::convertSize(totalFileSize));
564 } else if (fileCount > 0) {
565 text = i18nc("@info:status files (size)", "%1 (%2)", filesText, KIO::convertSize(totalFileSize));
566 } else {
567 Q_ASSERT(folderCount > 0);
568 text = foldersText;
569 }
570 }
571 } else {
572 calculateItemCount(fileCount, folderCount, totalFileSize);
573 text = KIO::itemsSummaryString(fileCount + folderCount,
574 fileCount, folderCount,
575 totalFileSize, true);
576 }
577
578 return text;
579 }
580
581 QList<QAction*> DolphinView::versionControlActions(const KFileItemList& items) const
582 {
583 return m_controller->versionControlActions(items);
584 }
585
586 void DolphinView::setUrl(const KUrl& url)
587 {
588 m_newFileNames.clear();
589 updateView(url, KUrl());
590 }
591
592 void DolphinView::selectAll()
593 {
594 m_viewAccessor.itemView()->selectAll();
595 }
596
597 void DolphinView::invertSelection()
598 {
599 QItemSelectionModel* selectionModel = m_viewAccessor.itemView()->selectionModel();
600 const QAbstractItemModel* itemModel = selectionModel->model();
601
602 const QModelIndex topLeft = itemModel->index(0, 0);
603 const QModelIndex bottomRight = itemModel->index(itemModel->rowCount() - 1,
604 itemModel->columnCount() - 1);
605
606 const QItemSelection selection(topLeft, bottomRight);
607 selectionModel->select(selection, QItemSelectionModel::Toggle);
608 }
609
610 void DolphinView::clearSelection()
611 {
612 m_viewAccessor.itemView()->clearSelection();
613 }
614
615 void DolphinView::renameSelectedItems()
616 {
617 KFileItemList items = selectedItems();
618 const int itemCount = items.count();
619 if (itemCount < 1) {
620 return;
621 }
622
623 if (itemCount > 1) {
624 // More than one item has been selected for renaming. Open
625 // a rename dialog and rename all items afterwards.
626 QPointer<RenameDialog> dialog = new RenameDialog(this, items);
627 if (dialog->exec() == QDialog::Rejected) {
628 delete dialog;
629 return;
630 }
631
632 const QString newName = dialog->newName();
633 if (newName.isEmpty()) {
634 emit errorMessage(dialog->errorString());
635 delete dialog;
636 return;
637 }
638 delete dialog;
639
640 // the selection would be invalid after renaming the items, so just clear
641 // it before
642 clearSelection();
643
644 // TODO: check how this can be integrated into KIO::FileUndoManager/KonqOperations
645 // as one operation instead of n rename operations like it is done now...
646 Q_ASSERT(newName.contains('#'));
647
648 // currently the items are sorted by the selection order, resort
649 // them by the file name
650 qSort(items.begin(), items.end(), lessThan);
651
652 // iterate through all selected items and rename them...
653 int index = 1;
654 foreach (const KFileItem& item, items) {
655 const KUrl& oldUrl = item.url();
656 QString number;
657 number.setNum(index++);
658
659 QString name = newName;
660 name.replace('#', number);
661
662 if (oldUrl.fileName() != name) {
663 KUrl newUrl = oldUrl;
664 newUrl.setFileName(name);
665 KonqOperations::rename(this, oldUrl, newUrl);
666 }
667 }
668 } else if (DolphinSettings::instance().generalSettings()->renameInline()) {
669 Q_ASSERT(itemCount == 1);
670 const QModelIndex dirIndex = m_viewAccessor.dirModel()->indexForItem(items.first());
671 const QModelIndex proxyIndex = m_viewAccessor.proxyModel()->mapFromSource(dirIndex);
672 m_viewAccessor.itemView()->edit(proxyIndex);
673 } else {
674 Q_ASSERT(itemCount == 1);
675
676 QPointer<RenameDialog> dialog = new RenameDialog(this, items);
677 if (dialog->exec() == QDialog::Rejected) {
678 delete dialog;
679 return;
680 }
681
682 const QString newName = dialog->newName();
683 if (newName.isEmpty()) {
684 emit errorMessage(dialog->errorString());
685 delete dialog;
686 return;
687 }
688 delete dialog;
689
690 const KUrl& oldUrl = items.first().url();
691 KUrl newUrl = oldUrl;
692 newUrl.setFileName(newName);
693 KonqOperations::rename(this, oldUrl, newUrl);
694 }
695
696 // assure that the current index remains visible when KDirLister
697 // will notify the view about changed items
698 m_assureVisibleCurrentIndex = true;
699 }
700
701 void DolphinView::trashSelectedItems()
702 {
703 const KUrl::List list = simplifiedSelectedUrls();
704 KonqOperations::del(this, KonqOperations::TRASH, list);
705 }
706
707 void DolphinView::deleteSelectedItems()
708 {
709 const KUrl::List list = simplifiedSelectedUrls();
710 const bool del = KonqOperations::askDeleteConfirmation(list,
711 KonqOperations::DEL,
712 KonqOperations::DEFAULT_CONFIRMATION,
713 this);
714
715 if (del) {
716 KIO::Job* job = KIO::del(list);
717 connect(job, SIGNAL(result(KJob*)),
718 this, SLOT(slotDeleteFileFinished(KJob*)));
719 }
720 }
721
722 void DolphinView::cutSelectedItems()
723 {
724 QMimeData* mimeData = selectionMimeData();
725 KonqMimeData::addIsCutSelection(mimeData, true);
726 QApplication::clipboard()->setMimeData(mimeData);
727 }
728
729 void DolphinView::copySelectedItems()
730 {
731 QMimeData* mimeData = selectionMimeData();
732 QApplication::clipboard()->setMimeData(mimeData);
733 }
734
735 void DolphinView::paste()
736 {
737 pasteToUrl(url());
738 }
739
740 void DolphinView::pasteIntoFolder()
741 {
742 const KFileItemList items = selectedItems();
743 if ((items.count() == 1) && items.first().isDir()) {
744 pasteToUrl(items.first().url());
745 }
746 }
747
748 void DolphinView::setShowPreview(bool show)
749 {
750 if (m_showPreview == show) {
751 return;
752 }
753
754 const KUrl viewPropsUrl = rootUrl();
755 ViewProperties props(viewPropsUrl);
756 props.setShowPreview(show);
757
758 m_showPreview = show;
759 const int oldZoomLevel = m_controller->zoomLevel();
760 emit showPreviewChanged();
761
762 // Enabling or disabling the preview might change the icon size of the view.
763 // As the view does not emit a signal when the icon size has been changed,
764 // the used zoom level of the controller must be adjusted manually:
765 updateZoomLevel(oldZoomLevel);
766 }
767
768 void DolphinView::setShowHiddenFiles(bool show)
769 {
770 if (m_viewAccessor.dirLister()->showingDotFiles() == show) {
771 return;
772 }
773
774 const KUrl viewPropsUrl = rootUrl();
775 ViewProperties props(viewPropsUrl);
776 props.setShowHiddenFiles(show);
777
778 m_viewAccessor.dirLister()->setShowingDotFiles(show);
779 emit showHiddenFilesChanged();
780 }
781
782 void DolphinView::setCategorizedSorting(bool categorized)
783 {
784 if (categorized == categorizedSorting()) {
785 return;
786 }
787
788 // setCategorizedSorting(true) may only get invoked
789 // if the view supports categorized sorting
790 Q_ASSERT(!categorized || supportsCategorizedSorting());
791
792 ViewProperties props(rootUrl());
793 props.setCategorizedSorting(categorized);
794 props.save();
795
796 m_storedCategorizedSorting = categorized;
797 m_viewAccessor.proxyModel()->setCategorizedModel(categorized);
798
799 emit categorizedSortingChanged();
800 }
801
802 void DolphinView::toggleSortOrder()
803 {
804 const Qt::SortOrder order = (sortOrder() == Qt::AscendingOrder) ?
805 Qt::DescendingOrder :
806 Qt::AscendingOrder;
807 setSortOrder(order);
808 }
809
810 void DolphinView::toggleSortFoldersFirst()
811 {
812 setSortFoldersFirst(!sortFoldersFirst());
813 }
814
815 void DolphinView::toggleAdditionalInfo(QAction* action)
816 {
817 const KFileItemDelegate::Information info =
818 static_cast<KFileItemDelegate::Information>(action->data().toInt());
819
820 KFileItemDelegate::InformationList list = additionalInfo();
821
822 const bool show = action->isChecked();
823
824 const int index = list.indexOf(info);
825 const bool containsInfo = (index >= 0);
826 if (show && !containsInfo) {
827 list.append(info);
828 setAdditionalInfo(list);
829 } else if (!show && containsInfo) {
830 list.removeAt(index);
831 setAdditionalInfo(list);
832 Q_ASSERT(list.indexOf(info) < 0);
833 }
834 }
835
836 void DolphinView::mouseReleaseEvent(QMouseEvent* event)
837 {
838 QWidget::mouseReleaseEvent(event);
839 setActive(true);
840 }
841
842 bool DolphinView::eventFilter(QObject* watched, QEvent* event)
843 {
844 switch (event->type()) {
845 case QEvent::FocusIn:
846 if (watched == m_viewAccessor.itemView()) {
847 m_controller->requestActivation();
848 }
849 break;
850
851 case QEvent::DragEnter:
852 if (watched == m_viewAccessor.itemView()->viewport()) {
853 setActive(true);
854 }
855 break;
856
857 case QEvent::KeyPress:
858 if (watched == m_viewAccessor.itemView()) {
859 // clear the selection when Escape has been pressed
860 QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
861 if (keyEvent->key() == Qt::Key_Escape) {
862 clearSelection();
863 }
864 }
865 break;
866
867 case QEvent::Wheel:
868 if (watched == m_viewAccessor.itemView()->viewport()) {
869 // Ctrl+wheel events should cause icon zooming, but not if the left mouse button is pressed
870 // (the user is probably trying to scroll during a selection in that case)
871 QWheelEvent* wheelEvent = static_cast<QWheelEvent*>(event);
872 if (wheelEvent->modifiers() & Qt::ControlModifier && !(wheelEvent->buttons() & Qt::LeftButton)) {
873 const int delta = wheelEvent->delta();
874 const int level = zoomLevel();
875 if (delta > 0) {
876 setZoomLevel(level + 1);
877 } else if (delta < 0) {
878 setZoomLevel(level - 1);
879 }
880 return true;
881 }
882 }
883 break;
884
885 default:
886 break;
887 }
888
889 return QWidget::eventFilter(watched, event);
890 }
891
892 void DolphinView::activate()
893 {
894 setActive(true);
895 }
896
897 void DolphinView::triggerItem(const KFileItem& item)
898 {
899 const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
900 if ((modifier & Qt::ShiftModifier) || (modifier & Qt::ControlModifier)) {
901 // items are selected by the user, hence don't trigger the
902 // item specified by 'index'
903 return;
904 }
905
906 // TODO: the m_isContextMenuOpen check is a workaround for Qt-issue 207192
907 if (item.isNull() || m_isContextMenuOpen) {
908 return;
909 }
910
911 emit itemTriggered(item); // caught by DolphinViewContainer or DolphinPart
912 }
913
914 void DolphinView::slotSelectionChanged(const QItemSelection& selected, const QItemSelection& deselected)
915 {
916 const int count = selectedItemsCount();
917 const bool selectionStateChanged = ((count > 0) && (selected.count() == count)) ||
918 ((count == 0) && !deselected.isEmpty());
919
920 // If nothing has been selected before and something got selected (or if something
921 // was selected before and now nothing is selected) the selectionChangedSignal must
922 // be emitted asynchronously as fast as possible to update the edit-actions.
923 m_selectionChangedTimer->setInterval(selectionStateChanged ? 0 : 300);
924 m_selectionChangedTimer->start();
925 }
926
927 void DolphinView::emitSelectionChangedSignal()
928 {
929 emit selectionChanged(DolphinView::selectedItems());
930 }
931
932 void DolphinView::openContextMenu(const QPoint& pos,
933 const QList<QAction*>& customActions)
934 {
935 KFileItem item;
936 const QModelIndex index = m_viewAccessor.itemView()->indexAt(pos);
937 if (index.isValid() && (index.column() == DolphinModel::Name)) {
938 const QModelIndex dolphinModelIndex = m_viewAccessor.proxyModel()->mapToSource(index);
939 item = m_viewAccessor.dirModel()->itemForIndex(dolphinModelIndex);
940 }
941
942 m_isContextMenuOpen = true; // TODO: workaround for Qt-issue 207192
943 emit requestContextMenu(item, url(), customActions);
944 m_isContextMenuOpen = false;
945 }
946
947 void DolphinView::dropUrls(const KFileItem& destItem,
948 const KUrl& destPath,
949 QDropEvent* event)
950 {
951 addNewFileNames(event->mimeData());
952 DragAndDropHelper::instance().dropUrls(destItem, destPath, event, this);
953 }
954
955 void DolphinView::updateSorting(DolphinView::Sorting sorting)
956 {
957 ViewProperties props(rootUrl());
958 props.setSorting(sorting);
959
960 m_viewAccessor.proxyModel()->setSorting(sorting);
961
962 emit sortingChanged(sorting);
963 }
964
965 void DolphinView::updateSortOrder(Qt::SortOrder order)
966 {
967 ViewProperties props(rootUrl());
968 props.setSortOrder(order);
969
970 m_viewAccessor.proxyModel()->setSortOrder(order);
971
972 emit sortOrderChanged(order);
973 }
974
975 void DolphinView::updateSortFoldersFirst(bool foldersFirst)
976 {
977 ViewProperties props(rootUrl());
978 props.setSortFoldersFirst(foldersFirst);
979
980 m_viewAccessor.proxyModel()->setSortFoldersFirst(foldersFirst);
981
982 emit sortFoldersFirstChanged(foldersFirst);
983 }
984
985 void DolphinView::updateAdditionalInfo(const KFileItemDelegate::InformationList& info)
986 {
987 ViewProperties props(rootUrl());
988 props.setAdditionalInfo(info);
989 props.save();
990
991 m_viewAccessor.itemDelegate()->setShowInformation(info);
992
993 emit additionalInfoChanged();
994 }
995
996 void DolphinView::updateAdditionalInfoActions(KActionCollection* collection)
997 {
998 const bool enable = (m_mode == DolphinView::DetailsView) ||
999 (m_mode == DolphinView::IconsView);
1000
1001 QAction* showSizeInfo = collection->action("show_size_info");
1002 QAction* showDateInfo = collection->action("show_date_info");
1003 QAction* showPermissionsInfo = collection->action("show_permissions_info");
1004 QAction* showOwnerInfo = collection->action("show_owner_info");
1005 QAction* showGroupInfo = collection->action("show_group_info");
1006 QAction* showMimeInfo = collection->action("show_mime_info");
1007
1008 showSizeInfo->setChecked(false);
1009 showDateInfo->setChecked(false);
1010 showPermissionsInfo->setChecked(false);
1011 showOwnerInfo->setChecked(false);
1012 showGroupInfo->setChecked(false);
1013 showMimeInfo->setChecked(false);
1014
1015 showSizeInfo->setEnabled(enable);
1016 showDateInfo->setEnabled(enable);
1017 showPermissionsInfo->setEnabled(enable);
1018 showOwnerInfo->setEnabled(enable);
1019 showGroupInfo->setEnabled(enable);
1020 showMimeInfo->setEnabled(enable);
1021
1022 foreach (KFileItemDelegate::Information info, m_viewAccessor.itemDelegate()->showInformation()) {
1023 switch (info) {
1024 case KFileItemDelegate::Size:
1025 showSizeInfo->setChecked(true);
1026 break;
1027 case KFileItemDelegate::ModificationTime:
1028 showDateInfo->setChecked(true);
1029 break;
1030 case KFileItemDelegate::Permissions:
1031 showPermissionsInfo->setChecked(true);
1032 break;
1033 case KFileItemDelegate::Owner:
1034 showOwnerInfo->setChecked(true);
1035 break;
1036 case KFileItemDelegate::OwnerAndGroup:
1037 showGroupInfo->setChecked(true);
1038 break;
1039 case KFileItemDelegate::FriendlyMimeType:
1040 showMimeInfo->setChecked(true);
1041 break;
1042 default:
1043 break;
1044 }
1045 }
1046 }
1047
1048 QPair<bool, QString> DolphinView::pasteInfo() const
1049 {
1050 return KonqOperations::pasteInfo(url());
1051 }
1052
1053 void DolphinView::setTabsForFilesEnabled(bool tabsForFiles)
1054 {
1055 m_tabsForFiles = tabsForFiles;
1056 }
1057
1058 bool DolphinView::isTabsForFilesEnabled() const
1059 {
1060 return m_tabsForFiles;
1061 }
1062
1063 void DolphinView::activateItem(const KUrl& url)
1064 {
1065 // TODO: If DolphinViewContainer uses DolphinView::restoreState(...) to restore the
1066 // view state in KDE 4.5, this function can be removed.
1067 m_activeItemUrl = url;
1068 }
1069
1070 bool DolphinView::itemsExpandable() const
1071 {
1072 return m_viewAccessor.itemsExpandable();
1073 }
1074
1075 void DolphinView::restoreState(QDataStream &stream)
1076 {
1077 // current item
1078 stream >> m_activeItemUrl;
1079
1080 // view position
1081 stream >> m_restoredContentsPosition;
1082
1083 // expanded folders (only relevant for the details view - will be ignored by the view in other view modes)
1084 QSet<KUrl> urlsToExpand;
1085 stream >> urlsToExpand;
1086 const DolphinDetailsViewExpander* expander = m_viewAccessor.setExpandedUrls(urlsToExpand);
1087
1088 if (expander) {
1089 m_expanderActive = true;
1090 connect (expander, SIGNAL(completed()), this, SLOT(slotLoadingCompleted()));
1091 }
1092 else {
1093 m_expanderActive = false;
1094 }
1095 }
1096
1097 void DolphinView::saveState(QDataStream &stream)
1098 {
1099 // current item
1100 KFileItem currentItem;
1101 const QAbstractItemView* view = m_viewAccessor.itemView();
1102
1103 if(view) {
1104 const QModelIndex proxyIndex = view->currentIndex();
1105 const QModelIndex dirModelIndex = m_viewAccessor.proxyModel()->mapToSource(proxyIndex);
1106 currentItem = m_viewAccessor.dirModel()->itemForIndex(dirModelIndex);
1107 }
1108
1109 KUrl currentUrl;
1110 if (!currentItem.isNull())
1111 currentUrl = currentItem.url();
1112
1113 stream << currentUrl;
1114
1115 // view position
1116 stream << contentsPosition();
1117
1118 // expanded folders (only relevant for the details view - the set will be empty in other view modes)
1119 stream << m_viewAccessor.expandedUrls();
1120 }
1121
1122 void DolphinView::observeCreatedItem(const KUrl& url)
1123 {
1124 m_createdItemUrl = url;
1125 connect(m_viewAccessor.dirModel(), SIGNAL(rowsInserted(const QModelIndex&, int, int)),
1126 this, SLOT(selectAndScrollToCreatedItem()));
1127 }
1128
1129 void DolphinView::selectAndScrollToCreatedItem()
1130 {
1131 const QModelIndex dirIndex = m_viewAccessor.dirModel()->indexForUrl(m_createdItemUrl);
1132 if (dirIndex.isValid()) {
1133 const QModelIndex proxyIndex = m_viewAccessor.proxyModel()->mapFromSource(dirIndex);
1134 m_viewAccessor.itemView()->setCurrentIndex(proxyIndex);
1135 }
1136
1137 disconnect(m_viewAccessor.dirModel(), SIGNAL(rowsInserted(const QModelIndex&, int, int)),
1138 this, SLOT(selectAndScrollToCreatedItem()));
1139 m_createdItemUrl = KUrl();
1140 }
1141
1142 void DolphinView::emitContentsMoved()
1143 {
1144 // TODO: If DolphinViewContainer uses DolphinView::saveState(...) to save the
1145 // view state in KDE 4.5, the contentsMoved signal might not be needed anymore,
1146 // depending on how the implementation is done.
1147 // In that case, the code in contentsPosition() can be moved to saveState().
1148
1149 // only emit the contents moved signal if no directory loading is ongoing
1150 // (this would reset the contents position always to (0, 0))
1151 if (!m_loadingDirectory) {
1152 const QPoint pos(contentsPosition());
1153 emit contentsMoved(pos.x(), pos.y());
1154 }
1155 }
1156
1157 void DolphinView::showHoverInformation(const KFileItem& item)
1158 {
1159 emit requestItemInfo(item);
1160 }
1161
1162 void DolphinView::clearHoverInformation()
1163 {
1164 emit requestItemInfo(KFileItem());
1165 }
1166
1167 void DolphinView::slotDeleteFileFinished(KJob* job)
1168 {
1169 if (job->error() == 0) {
1170 emit operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
1171 } else if (job->error() != KIO::ERR_USER_CANCELED) {
1172 emit errorMessage(job->errorString());
1173 }
1174 }
1175
1176 void DolphinView::slotRequestUrlChange(const KUrl& url)
1177 {
1178 emit requestUrlChange(url);
1179 m_controller->setUrl(url);
1180 }
1181
1182 void DolphinView::slotDirListerCompleted()
1183 {
1184 if (!m_expanderActive) {
1185 slotLoadingCompleted();
1186 }
1187
1188 if (!m_newFileNames.isEmpty()) {
1189 // select all newly added items created by a paste operation or
1190 // a drag & drop operation
1191 const int rowCount = m_viewAccessor.proxyModel()->rowCount();
1192 QItemSelection selection;
1193 for (int row = 0; row < rowCount; ++row) {
1194 const QModelIndex proxyIndex = m_viewAccessor.proxyModel()->index(row, 0);
1195 const QModelIndex dirIndex = m_viewAccessor.proxyModel()->mapToSource(proxyIndex);
1196 const KUrl url = m_viewAccessor.dirModel()->itemForIndex(dirIndex).url();
1197 if (m_newFileNames.contains(url.fileName())) {
1198 selection.merge(QItemSelection(proxyIndex, proxyIndex), QItemSelectionModel::Select);
1199 }
1200 }
1201 m_viewAccessor.itemView()->selectionModel()->select(selection, QItemSelectionModel::Select);
1202
1203 m_newFileNames.clear();
1204 }
1205 }
1206
1207 void DolphinView::slotLoadingCompleted()
1208 {
1209 m_expanderActive = false;
1210 m_loadingDirectory = false;
1211
1212 if (!m_activeItemUrl.isEmpty()) {
1213 // assure that the current item remains visible
1214 const QModelIndex dirIndex = m_viewAccessor.dirModel()->indexForUrl(m_activeItemUrl);
1215 if (dirIndex.isValid()) {
1216 const QModelIndex proxyIndex = m_viewAccessor.proxyModel()->mapFromSource(dirIndex);
1217 QAbstractItemView* view = m_viewAccessor.itemView();
1218 const bool clearSelection = !hasSelection();
1219 view->setCurrentIndex(proxyIndex);
1220 if (clearSelection) {
1221 view->clearSelection();
1222 }
1223 m_activeItemUrl.clear();
1224 }
1225 }
1226
1227 if (!m_selectedItems.isEmpty()) {
1228 const KUrl& baseUrl = url();
1229 KUrl url;
1230 QItemSelection newSelection;
1231 foreach(const KFileItem& item, m_selectedItems) {
1232 url = item.url().upUrl();
1233 if (baseUrl.equals(url, KUrl::CompareWithoutTrailingSlash)) {
1234 QModelIndex index = m_viewAccessor.proxyModel()->mapFromSource(m_viewAccessor.dirModel()->indexForItem(item));
1235 newSelection.select(index, index);
1236 }
1237 }
1238 m_viewAccessor.itemView()->selectionModel()->select(newSelection,
1239 QItemSelectionModel::ClearAndSelect
1240 | QItemSelectionModel::Current);
1241 m_selectedItems.clear();
1242 }
1243
1244 // Restore the contents position. This has to be done using a Qt::QueuedConnection
1245 // because the view might not be in its final state yet.
1246 QMetaObject::invokeMethod(this, "restoreContentsPosition", Qt::QueuedConnection);
1247 }
1248
1249 void DolphinView::slotRefreshItems()
1250 {
1251 if (m_assureVisibleCurrentIndex) {
1252 m_assureVisibleCurrentIndex = false;
1253 m_viewAccessor.itemView()->scrollTo(m_viewAccessor.itemView()->currentIndex());
1254 }
1255 }
1256
1257 void DolphinView::loadDirectory(const KUrl& url, bool reload)
1258 {
1259 if (!url.isValid()) {
1260 const QString location(url.pathOrUrl());
1261 if (location.isEmpty()) {
1262 emit errorMessage(i18nc("@info:status", "The location is empty."));
1263 } else {
1264 emit errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location));
1265 }
1266 return;
1267 }
1268
1269 m_loadingDirectory = true;
1270 m_expanderActive = false;
1271
1272 KDirLister* dirLister = m_viewAccessor.dirLister();
1273 dirLister->openUrl(url, reload ? KDirLister::Reload : KDirLister::NoFlags);
1274
1275 KDirLister* rootDirLister = m_viewAccessor.rootDirLister();
1276 if (dirLister != rootDirLister) {
1277 // In the case of the column view the root directory lister can be different. Assure
1278 // that it gets synchronized (clients from DolphinView are not aware that internally
1279 // different directory listers are used).
1280 rootDirLister->openUrl(url, reload ? KDirLister::Reload : KDirLister::NoFlags);
1281 }
1282 }
1283
1284 void DolphinView::applyViewProperties()
1285 {
1286 if (m_ignoreViewProperties) {
1287 return;
1288 }
1289
1290 const ViewProperties props(rootUrl());
1291
1292 const Mode mode = props.viewMode();
1293 if (m_mode != mode) {
1294 const int oldZoomLevel = m_controller->zoomLevel();
1295
1296 m_mode = mode;
1297 createView();
1298 emit modeChanged();
1299
1300 updateZoomLevel(oldZoomLevel);
1301 }
1302 if (m_viewAccessor.itemView() == 0) {
1303 createView();
1304 }
1305 Q_ASSERT(m_viewAccessor.itemView() != 0);
1306 Q_ASSERT(m_viewAccessor.itemDelegate() != 0);
1307
1308 const bool showHiddenFiles = props.showHiddenFiles();
1309 if (showHiddenFiles != m_viewAccessor.dirLister()->showingDotFiles()) {
1310 m_viewAccessor.dirLister()->setShowingDotFiles(showHiddenFiles);
1311 emit showHiddenFilesChanged();
1312 }
1313
1314 m_storedCategorizedSorting = props.categorizedSorting();
1315 const bool categorized = m_storedCategorizedSorting && supportsCategorizedSorting();
1316 if (categorized != m_viewAccessor.proxyModel()->isCategorizedModel()) {
1317 m_viewAccessor.proxyModel()->setCategorizedModel(categorized);
1318 emit categorizedSortingChanged();
1319 }
1320
1321 const DolphinView::Sorting sorting = props.sorting();
1322 if (sorting != m_viewAccessor.proxyModel()->sorting()) {
1323 m_viewAccessor.proxyModel()->setSorting(sorting);
1324 emit sortingChanged(sorting);
1325 }
1326
1327 const Qt::SortOrder sortOrder = props.sortOrder();
1328 if (sortOrder != m_viewAccessor.proxyModel()->sortOrder()) {
1329 m_viewAccessor.proxyModel()->setSortOrder(sortOrder);
1330 emit sortOrderChanged(sortOrder);
1331 }
1332
1333 const bool sortFoldersFirst = props.sortFoldersFirst();
1334 if (sortFoldersFirst != m_viewAccessor.proxyModel()->sortFoldersFirst()) {
1335 m_viewAccessor.proxyModel()->setSortFoldersFirst(sortFoldersFirst);
1336 emit sortFoldersFirstChanged(sortFoldersFirst);
1337 }
1338
1339 KFileItemDelegate::InformationList info = props.additionalInfo();
1340 if (info != m_viewAccessor.itemDelegate()->showInformation()) {
1341 m_viewAccessor.itemDelegate()->setShowInformation(info);
1342 emit additionalInfoChanged();
1343 }
1344
1345 const bool showPreview = props.showPreview();
1346 if (showPreview != m_showPreview) {
1347 m_showPreview = showPreview;
1348 const int oldZoomLevel = m_controller->zoomLevel();
1349 emit showPreviewChanged();
1350
1351 // Enabling or disabling the preview might change the icon size of the view.
1352 // As the view does not emit a signal when the icon size has been changed,
1353 // the used zoom level of the controller must be adjusted manually:
1354 updateZoomLevel(oldZoomLevel);
1355 }
1356
1357 if (DolphinSettings::instance().generalSettings()->globalViewProps()) {
1358 // During the lifetime of a DolphinView instance the global view properties
1359 // should not be changed. This allows e. g. to split a view and use different
1360 // view properties for each view.
1361 m_ignoreViewProperties = true;
1362 }
1363 }
1364
1365 void DolphinView::createView()
1366 {
1367 deleteView();
1368
1369 Q_ASSERT(m_viewAccessor.itemView() == 0);
1370 m_viewAccessor.createView(this, m_controller, m_mode);
1371
1372 QAbstractItemView* view = m_viewAccessor.itemView();
1373 Q_ASSERT(view != 0);
1374 view->installEventFilter(this);
1375 view->viewport()->installEventFilter(this);
1376
1377 m_controller->setItemView(view);
1378
1379 // When changing the view mode, the selection is lost due to reinstantiating
1380 // a new item view with a custom selection model. Pass the ownership of the
1381 // selection model to DolphinView, so that it can be shared by all item views.
1382 if (m_selectionModel != 0) {
1383 view->setSelectionModel(m_selectionModel);
1384 } else {
1385 m_selectionModel = view->selectionModel();
1386 }
1387 m_selectionModel->setParent(this);
1388 connect(view->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
1389 this, SLOT(slotSelectionChanged(const QItemSelection&, const QItemSelection&)));
1390
1391 connect(view->verticalScrollBar(), SIGNAL(valueChanged(int)),
1392 this, SLOT(emitContentsMoved()));
1393 connect(view->horizontalScrollBar(), SIGNAL(valueChanged(int)),
1394 this, SLOT(emitContentsMoved()));
1395
1396 setFocusProxy(m_viewAccessor.layoutTarget());
1397 m_topLayout->insertWidget(1, m_viewAccessor.layoutTarget());
1398 }
1399
1400 void DolphinView::deleteView()
1401 {
1402 QAbstractItemView* view = m_viewAccessor.itemView();
1403 if (view != 0) {
1404 // It's important to set the keyboard focus to the parent
1405 // before deleting the view: Otherwise when having a split
1406 // view the other view will get the focus and will request
1407 // an activation (see DolphinView::eventFilter()).
1408 setFocusProxy(0);
1409 setFocus();
1410
1411 m_topLayout->removeWidget(view);
1412 view->close();
1413
1414 // disconnect all signal/slots
1415 disconnect(view);
1416 m_controller->disconnect(view);
1417 view->disconnect();
1418 disconnect(view->verticalScrollBar(), SIGNAL(valueChanged(int)),
1419 this, SLOT(emitContentsMoved()));
1420 disconnect(view->horizontalScrollBar(), SIGNAL(valueChanged(int)),
1421 this, SLOT(emitContentsMoved()));
1422
1423 m_viewAccessor.deleteView();
1424 }
1425 }
1426
1427 void DolphinView::pasteToUrl(const KUrl& url)
1428 {
1429 addNewFileNames(QApplication::clipboard()->mimeData());
1430 KonqOperations::doPaste(this, url);
1431 }
1432
1433 void DolphinView::updateZoomLevel(int oldZoomLevel)
1434 {
1435 const int newZoomLevel = ZoomLevelInfo::zoomLevelForIconSize(m_viewAccessor.itemView()->iconSize());
1436 if (oldZoomLevel != newZoomLevel) {
1437 m_controller->setZoomLevel(newZoomLevel);
1438 emit zoomLevelChanged(newZoomLevel);
1439 }
1440 }
1441
1442 KUrl::List DolphinView::simplifiedSelectedUrls() const
1443 {
1444 KUrl::List list = selectedUrls();
1445 if (itemsExpandable() ) {
1446 list = KDirModel::simplifiedUrlList(list);
1447 }
1448 return list;
1449 }
1450
1451 QMimeData* DolphinView::selectionMimeData() const
1452 {
1453 const QAbstractItemView* view = m_viewAccessor.itemView();
1454 Q_ASSERT((view != 0) && (view->selectionModel() != 0));
1455 const QItemSelection selection = m_viewAccessor.proxyModel()->mapSelectionToSource(view->selectionModel()->selection());
1456 return m_viewAccessor.dirModel()->mimeData(selection.indexes());
1457 }
1458
1459 void DolphinView::addNewFileNames(const QMimeData* mimeData)
1460 {
1461 const KUrl::List urls = KUrl::List::fromMimeData(mimeData);
1462 foreach (const KUrl& url, urls) {
1463 m_newFileNames.insert(url.fileName());
1464 }
1465 }
1466
1467 DolphinView::ViewAccessor::ViewAccessor(DolphinSortFilterProxyModel* proxyModel) :
1468 m_iconsView(0),
1469 m_detailsView(0),
1470 m_columnsContainer(0),
1471 m_proxyModel(proxyModel),
1472 m_dragSource(0)
1473 {
1474 }
1475
1476 DolphinView::ViewAccessor::~ViewAccessor()
1477 {
1478 delete m_dragSource;
1479 m_dragSource = 0;
1480 }
1481
1482 void DolphinView::ViewAccessor::createView(QWidget* parent,
1483 DolphinController* controller,
1484 Mode mode)
1485 {
1486 Q_ASSERT(itemView() == 0);
1487
1488 switch (mode) {
1489 case IconsView:
1490 m_iconsView = new DolphinIconsView(parent, controller, m_proxyModel);
1491 break;
1492
1493 case DetailsView:
1494 m_detailsView = new DolphinDetailsView(parent, controller, m_proxyModel);
1495 break;
1496
1497 case ColumnView:
1498 m_columnsContainer = new DolphinColumnViewContainer(parent, controller);
1499 break;
1500
1501 default:
1502 Q_ASSERT(false);
1503 }
1504 }
1505
1506 void DolphinView::ViewAccessor::deleteView()
1507 {
1508 QAbstractItemView* view = itemView();
1509 if (view != 0) {
1510 if (DragAndDropHelper::instance().isDragSource(view)) {
1511 // The view is a drag source (the feature "Open folders
1512 // during drag operations" is used). Deleting the view
1513 // during an ongoing drag operation is not allowed, so
1514 // this will postponed.
1515 if (m_dragSource != 0) {
1516 // the old stored view is obviously not the drag source anymore
1517 m_dragSource->deleteLater();
1518 m_dragSource = 0;
1519 }
1520 view->hide();
1521 m_dragSource = view;
1522 } else {
1523 view->deleteLater();
1524 }
1525 }
1526
1527 m_iconsView = 0;
1528 m_detailsView = 0;
1529
1530 if (m_columnsContainer != 0) {
1531 m_columnsContainer->deleteLater();
1532 }
1533 m_columnsContainer = 0;
1534 }
1535
1536
1537 void DolphinView::ViewAccessor::prepareUrlChange(const KUrl& url)
1538 {
1539 if (m_columnsContainer != 0) {
1540 m_columnsContainer->showColumn(url);
1541 }
1542
1543 if(!m_detailsViewExpander.isNull()) {
1544 // stop expanding items in the current folder
1545 m_detailsViewExpander->stop();
1546 }
1547 }
1548
1549 QAbstractItemView* DolphinView::ViewAccessor::itemView() const
1550 {
1551 if (m_iconsView != 0) {
1552 return m_iconsView;
1553 }
1554
1555 if (m_detailsView != 0) {
1556 return m_detailsView;
1557 }
1558
1559 if (m_columnsContainer != 0) {
1560 return m_columnsContainer->activeColumn();
1561 }
1562
1563 return 0;
1564 }
1565
1566 KFileItemDelegate* DolphinView::ViewAccessor::itemDelegate() const
1567 {
1568 return static_cast<KFileItemDelegate*>(itemView()->itemDelegate());
1569 }
1570
1571 QWidget* DolphinView::ViewAccessor::layoutTarget() const
1572 {
1573 if (m_columnsContainer != 0) {
1574 return m_columnsContainer;
1575 }
1576 return itemView();
1577 }
1578
1579 KUrl DolphinView::ViewAccessor::rootUrl() const
1580 {
1581 return (m_columnsContainer != 0) ? m_columnsContainer->rootUrl() : KUrl();
1582 }
1583
1584 KDirLister* DolphinView::ViewAccessor::rootDirLister() const
1585 {
1586 return static_cast<DolphinModel*>(m_proxyModel->sourceModel())->dirLister();
1587 }
1588
1589 bool DolphinView::ViewAccessor::supportsCategorizedSorting() const
1590 {
1591 return m_iconsView != 0;
1592 }
1593
1594 bool DolphinView::ViewAccessor::itemsExpandable() const
1595 {
1596 return (m_detailsView != 0) && m_detailsView->itemsExpandable();
1597 }
1598
1599
1600 QSet<KUrl> DolphinView::ViewAccessor::expandedUrls() const
1601 {
1602 if (m_detailsView != 0) {
1603 return m_detailsView->expandedUrls();
1604 }
1605
1606 return QSet<KUrl>();
1607 }
1608
1609 const DolphinDetailsViewExpander* DolphinView::ViewAccessor::setExpandedUrls(const QSet<KUrl>& urlsToExpand)
1610 {
1611 if ((m_detailsView != 0) && m_detailsView->itemsExpandable() && !urlsToExpand.isEmpty()) {
1612 m_detailsViewExpander = new DolphinDetailsViewExpander(m_detailsView, urlsToExpand);
1613 return m_detailsViewExpander;
1614 }
1615 else {
1616 return 0;
1617 }
1618 }
1619
1620 bool DolphinView::ViewAccessor::reloadOnAdditionalInfoChange() const
1621 {
1622 // the details view requires no reloading of the directory, as it maps
1623 // the file item delegate info to its columns internally
1624 return m_detailsView != 0;
1625 }
1626
1627 DolphinModel* DolphinView::ViewAccessor::dirModel() const
1628 {
1629 return static_cast<DolphinModel*>(proxyModel()->sourceModel());
1630 }
1631
1632 DolphinSortFilterProxyModel* DolphinView::ViewAccessor::proxyModel() const
1633 {
1634 if (m_columnsContainer != 0) {
1635 return static_cast<DolphinSortFilterProxyModel*>(m_columnsContainer->activeColumn()->model());
1636 }
1637 return m_proxyModel;
1638 }
1639
1640 KDirLister* DolphinView::ViewAccessor::dirLister() const
1641 {
1642 return dirModel()->dirLister();
1643 }
1644
1645 void DolphinView::slotRedirection(const KUrl& oldUrl, const KUrl& newUrl)
1646 {
1647 emit redirection(oldUrl, newUrl);
1648 m_controller->redirectToUrl(newUrl); // #186947
1649 }
1650
1651 void DolphinView::restoreContentsPosition()
1652 {
1653 if (!m_restoredContentsPosition.isNull()) {
1654 setContentsPosition(m_restoredContentsPosition.x(), m_restoredContentsPosition.y());
1655 m_restoredContentsPosition = QPoint();
1656 }
1657 }
1658
1659 #include "dolphinview.moc"