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