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