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