delay a few things on startup, such as setting the visibility mode, which ensures...
[personal-kdebase.git] / apps / dolphin / src / dolphinview.cpp
blob6681fe1895421eca8dbfd26bff0da8909a89f6f2
1 /***************************************************************************
2 * Copyright (C) 2006 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 ***************************************************************************/
21 #include "dolphinview.h"
23 #include <QApplication>
24 #include <QClipboard>
25 #include <QKeyEvent>
26 #include <QItemSelection>
27 #include <QBoxLayout>
28 #include <QTimer>
29 #include <QScrollBar>
31 #include <kactioncollection.h>
32 #include <kcolorscheme.h>
33 #include <kdirlister.h>
34 #include <kfilepreviewgenerator.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>
52 #include "dolphinmodel.h"
53 #include "dolphincolumnview.h"
54 #include "dolphincontroller.h"
55 #include "dolphinfileitemdelegate.h"
56 #include "dolphinsortfilterproxymodel.h"
57 #include "dolphindetailsview.h"
58 #include "dolphin_detailsmodesettings.h"
59 #include "dolphiniconsview.h"
60 #include "settings/dolphinsettings.h"
61 #include "dolphin_generalsettings.h"
62 #include "draganddrophelper.h"
63 #include "folderexpander.h"
64 #include "renamedialog.h"
65 #include "tooltips/tooltipmanager.h"
66 #include "viewproperties.h"
67 #include "zoomlevelinfo.h"
69 /**
70 * Helper function for sorting items with qSort() in
71 * DolphinView::renameSelectedItems().
73 bool lessThan(const KFileItem& item1, const KFileItem& item2)
75 return KStringHandler::naturalCompare(item1.name(), item2.name()) < 0;
78 DolphinView::DolphinView(QWidget* parent,
79 const KUrl& url,
80 KDirLister* dirLister,
81 DolphinModel* dolphinModel,
82 DolphinSortFilterProxyModel* proxyModel) :
83 QWidget(parent),
84 m_active(true),
85 m_showPreview(false),
86 m_loadingDirectory(false),
87 m_storedCategorizedSorting(false),
88 m_tabsForFiles(false),
89 m_isContextMenuOpen(false),
90 m_ignoreViewProperties(false),
91 m_mode(DolphinView::IconsView),
92 m_topLayout(0),
93 m_controller(0),
94 m_iconsView(0),
95 m_detailsView(0),
96 m_columnView(0),
97 m_fileItemDelegate(0),
98 m_selectionModel(0),
99 m_dolphinModel(dolphinModel),
100 m_dirLister(dirLister),
101 m_proxyModel(proxyModel),
102 m_previewGenerator(0),
103 m_toolTipManager(0),
104 m_rootUrl(),
105 m_currentItemUrl(),
106 m_expandedViews()
108 m_topLayout = new QVBoxLayout(this);
109 m_topLayout->setSpacing(0);
110 m_topLayout->setMargin(0);
112 m_controller = new DolphinController(this);
113 m_controller->setUrl(url);
115 connect(m_controller, SIGNAL(urlChanged(const KUrl&)),
116 this, SIGNAL(urlChanged(const KUrl&)));
117 connect(m_controller, SIGNAL(requestUrlChange(const KUrl&)),
118 this, SLOT(slotRequestUrlChange(const KUrl&)));
120 connect(m_controller, SIGNAL(requestContextMenu(const QPoint&)),
121 this, SLOT(openContextMenu(const QPoint&)));
122 connect(m_controller, SIGNAL(urlsDropped(const KFileItem&, const KUrl&, QDropEvent*)),
123 this, SLOT(dropUrls(const KFileItem&, const KUrl&, QDropEvent*)));
124 connect(m_controller, SIGNAL(sortingChanged(DolphinView::Sorting)),
125 this, SLOT(updateSorting(DolphinView::Sorting)));
126 connect(m_controller, SIGNAL(sortOrderChanged(Qt::SortOrder)),
127 this, SLOT(updateSortOrder(Qt::SortOrder)));
128 connect(m_controller, SIGNAL(additionalInfoChanged(const KFileItemDelegate::InformationList&)),
129 this, SLOT(updateAdditionalInfo(const KFileItemDelegate::InformationList&)));
130 connect(m_controller, SIGNAL(itemTriggered(const KFileItem&)),
131 this, SLOT(triggerItem(const KFileItem&)));
132 connect(m_controller, SIGNAL(tabRequested(const KUrl&)),
133 this, SIGNAL(tabRequested(const KUrl&)));
134 connect(m_controller, SIGNAL(activated()),
135 this, SLOT(activate()));
136 connect(m_controller, SIGNAL(itemEntered(const KFileItem&)),
137 this, SLOT(showHoverInformation(const KFileItem&)));
138 connect(m_controller, SIGNAL(viewportEntered()),
139 this, SLOT(clearHoverInformation()));
141 connect(m_dirLister, SIGNAL(redirection(KUrl, KUrl)),
142 this, SIGNAL(redirection(KUrl, KUrl)));
143 connect(m_dirLister, SIGNAL(completed()),
144 this, SLOT(restoreCurrentItem()));
146 applyViewProperties(url);
147 m_topLayout->addWidget(itemView());
150 DolphinView::~DolphinView()
152 deleteExpandedViews();
155 const KUrl& DolphinView::url() const
157 return m_controller->url();
160 KUrl DolphinView::rootUrl() const
162 return isColumnViewActive() ? m_columnView->rootUrl() : url();
165 void DolphinView::setActive(bool active)
167 if (active == m_active) {
168 return;
171 m_active = active;
173 QColor color = KColorScheme(QPalette::Active, KColorScheme::View).background().color();
174 if (active) {
175 // TODO: emitting urlChanged() is a hack, as the URL hasn't really changed. It
176 // bypasses the problem when having a split view and changing the active view to
177 // update the some URL dependent states. A nicer approach should be no big deal...
178 emit urlChanged(url());
179 emit selectionChanged(selectedItems());
180 } else {
181 color.setAlpha(150);
184 QWidget* viewport = itemView()->viewport();
185 QPalette palette;
186 palette.setColor(viewport->backgroundRole(), color);
187 viewport->setPalette(palette);
189 update();
191 if (active) {
192 itemView()->setFocus();
193 emit activated();
196 m_controller->indicateActivationChange(active);
199 bool DolphinView::isActive() const
201 return m_active;
204 void DolphinView::setMode(Mode mode)
206 if (mode == m_mode) {
207 return; // the wished mode is already set
210 const int oldZoomLevel = m_controller->zoomLevel();
211 m_mode = mode;
213 deleteView();
215 const KUrl viewPropsUrl = viewPropertiesUrl();
216 ViewProperties props(viewPropsUrl);
217 props.setViewMode(m_mode);
218 createView();
220 // the file item delegate has been recreated, apply the current
221 // additional information manually
222 const KFileItemDelegate::InformationList infoList = props.additionalInfo();
223 m_fileItemDelegate->setShowInformation(infoList);
224 emit additionalInfoChanged();
226 // Not all view modes support categorized sorting. Adjust the sorting model
227 // if changing the view mode results in a change of the categorized sorting
228 // capabilities.
229 m_storedCategorizedSorting = props.categorizedSorting();
230 const bool categorized = m_storedCategorizedSorting && supportsCategorizedSorting();
231 if (categorized != m_proxyModel->isCategorizedModel()) {
232 m_proxyModel->setCategorizedModel(categorized);
233 emit categorizedSortingChanged();
236 emit modeChanged();
238 updateZoomLevel(oldZoomLevel);
239 if (m_showPreview) {
240 loadDirectory(viewPropsUrl);
244 DolphinView::Mode DolphinView::mode() const
246 return m_mode;
249 bool DolphinView::showPreview() const
251 return m_showPreview;
254 bool DolphinView::showHiddenFiles() const
256 return m_dirLister->showingDotFiles();
259 bool DolphinView::categorizedSorting() const
261 // If all view modes would support categorized sorting, returning
262 // m_proxyModel->isCategorizedModel() would be the way to go. As
263 // currently only the icons view supports caterized sorting, we remember
264 // the stored view properties state in m_storedCategorizedSorting and
265 // return this state. The application takes care to disable the corresponding
266 // checkbox by checking DolphinView::supportsCategorizedSorting() to indicate
267 // that this setting is not applied to the current view mode.
268 return m_storedCategorizedSorting;
271 bool DolphinView::supportsCategorizedSorting() const
273 return m_iconsView != 0;
276 void DolphinView::selectAll()
278 QAbstractItemView* view = itemView();
279 // TODO: there seems to be a bug in QAbstractItemView::selectAll(); if
280 // the Ctrl-key is pressed (e. g. for Ctrl+A), selectAll() inverts the
281 // selection instead of selecting all items. This is bypassed for KDE 4.0
282 // by invoking clearSelection() first.
283 view->clearSelection();
284 view->selectAll();
287 void DolphinView::invertSelection()
289 if (isColumnViewActive()) {
290 // QAbstractItemView does not offer a virtual method invertSelection()
291 // as counterpart to QAbstractItemView::selectAll(). This makes it
292 // necessary to delegate the inverting of the selection to the
293 // column view, as only the selection of the active column should
294 // get inverted.
295 m_columnView->invertSelection();
296 } else {
297 QItemSelectionModel* selectionModel = itemView()->selectionModel();
298 const QAbstractItemModel* itemModel = selectionModel->model();
300 const QModelIndex topLeft = itemModel->index(0, 0);
301 const QModelIndex bottomRight = itemModel->index(itemModel->rowCount() - 1,
302 itemModel->columnCount() - 1);
304 const QItemSelection selection(topLeft, bottomRight);
305 selectionModel->select(selection, QItemSelectionModel::Toggle);
309 bool DolphinView::hasSelection() const
311 return itemView()->selectionModel()->hasSelection();
314 void DolphinView::clearSelection()
316 itemView()->selectionModel()->clear();
319 KFileItemList DolphinView::selectedItems() const
321 if (isColumnViewActive()) {
322 return m_columnView->selectedItems();
325 const QAbstractItemView* view = itemView();
327 // Our view has a selection, we will map them back to the DolphinModel
328 // and then fill the KFileItemList.
329 Q_ASSERT((view != 0) && (view->selectionModel() != 0));
331 const QItemSelection selection = m_proxyModel->mapSelectionToSource(view->selectionModel()->selection());
332 KFileItemList itemList;
334 const QModelIndexList indexList = selection.indexes();
335 foreach (const QModelIndex &index, indexList) {
336 KFileItem item = m_dolphinModel->itemForIndex(index);
337 if (!item.isNull()) {
338 itemList.append(item);
342 return itemList;
345 KUrl::List DolphinView::selectedUrls() const
347 KUrl::List urls;
348 const KFileItemList list = selectedItems();
349 foreach (const KFileItem &item, list) {
350 urls.append(item.url());
352 return urls;
355 int DolphinView::selectedItemsCount() const
357 if (isColumnViewActive()) {
358 // TODO: get rid of this special case by adjusting the dir lister
359 // to the current column
360 return m_columnView->selectedItems().count();
363 return itemView()->selectionModel()->selection().count();
366 void DolphinView::setContentsPosition(int x, int y)
368 QAbstractItemView* view = itemView();
370 // the ColumnView takes care itself for the horizontal scrolling
371 if (!isColumnViewActive()) {
372 view->horizontalScrollBar()->setValue(x);
374 view->verticalScrollBar()->setValue(y);
376 m_loadingDirectory = false;
379 QPoint DolphinView::contentsPosition() const
381 const int x = itemView()->horizontalScrollBar()->value();
382 const int y = itemView()->verticalScrollBar()->value();
383 return QPoint(x, y);
386 void DolphinView::setZoomLevel(int level)
388 if (level < ZoomLevelInfo::minimumLevel()) {
389 level = ZoomLevelInfo::minimumLevel();
390 } else if (level > ZoomLevelInfo::maximumLevel()) {
391 level = ZoomLevelInfo::maximumLevel();
394 if (level != zoomLevel()) {
395 m_controller->setZoomLevel(level);
396 m_previewGenerator->updatePreviews();
397 emit zoomLevelChanged(level);
401 int DolphinView::zoomLevel() const
403 return m_controller->zoomLevel();
406 void DolphinView::setSorting(Sorting sorting)
408 if (sorting != this->sorting()) {
409 updateSorting(sorting);
413 DolphinView::Sorting DolphinView::sorting() const
415 return m_proxyModel->sorting();
418 void DolphinView::setSortOrder(Qt::SortOrder order)
420 if (sortOrder() != order) {
421 updateSortOrder(order);
425 Qt::SortOrder DolphinView::sortOrder() const
427 return m_proxyModel->sortOrder();
430 void DolphinView::setAdditionalInfo(KFileItemDelegate::InformationList info)
432 const KUrl viewPropsUrl = viewPropertiesUrl();
433 ViewProperties props(viewPropsUrl);
434 props.setAdditionalInfo(info);
435 m_fileItemDelegate->setShowInformation(info);
437 emit additionalInfoChanged();
439 if (itemView() != m_detailsView) {
440 // the details view requires no reloading of the directory, as it maps
441 // the file item delegate info to its columns internally
442 loadDirectory(viewPropsUrl);
446 KFileItemDelegate::InformationList DolphinView::additionalInfo() const
448 return m_fileItemDelegate->showInformation();
451 void DolphinView::reload()
453 setUrl(url());
454 loadDirectory(url(), true);
457 void DolphinView::refresh()
459 m_ignoreViewProperties = false;
461 const bool oldActivationState = m_active;
462 const int oldZoomLevel = m_controller->zoomLevel();
463 m_active = true;
465 createView();
466 applyViewProperties(m_controller->url());
467 reload();
469 setActive(oldActivationState);
470 updateZoomLevel(oldZoomLevel);
473 void DolphinView::updateView(const KUrl& url, const KUrl& rootUrl)
475 if (m_controller->url() == url) {
476 return;
479 m_previewGenerator->cancelPreviews();
480 m_controller->setUrl(url); // emits urlChanged, which we forward
482 if (!rootUrl.isEmpty() && rootUrl.isParentOf(url)) {
483 applyViewProperties(rootUrl);
484 loadDirectory(rootUrl);
485 if (itemView() == m_columnView) {
486 m_columnView->setRootUrl(rootUrl);
487 m_columnView->showColumn(url);
489 } else {
490 applyViewProperties(url);
491 loadDirectory(url);
494 emit startedPathLoading(url);
497 void DolphinView::setNameFilter(const QString& nameFilter)
499 m_proxyModel->setFilterRegExp(nameFilter);
501 if (isColumnViewActive()) {
502 // adjusting the directory lister is not enough in the case of the
503 // column view, as each column has its own directory lister internally...
504 m_columnView->setNameFilter(nameFilter);
508 void DolphinView::calculateItemCount(int& fileCount,
509 int& folderCount,
510 KIO::filesize_t& totalFileSize) const
512 foreach (const KFileItem& item, m_dirLister->items()) {
513 if (item.isDir()) {
514 ++folderCount;
515 } else {
516 ++fileCount;
517 totalFileSize += item.size();
522 QString DolphinView::statusBarText() const
524 QString text;
525 int folderCount = 0;
526 int fileCount = 0;
527 KIO::filesize_t totalFileSize = 0;
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;
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();
548 ++it;
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;
571 } else {
572 calculateItemCount(fileCount, folderCount, totalFileSize);
573 text = KIO::itemsSummaryString(fileCount + folderCount,
574 fileCount, folderCount,
575 totalFileSize, true);
578 return text;
581 void DolphinView::setUrl(const KUrl& url)
583 // remember current item candidate (see restoreCurrentItem())
584 m_currentItemUrl = url;
585 updateView(url, KUrl());
588 void DolphinView::changeSelection(const KFileItemList& selection)
590 clearSelection();
591 if (selection.isEmpty()) {
592 return;
594 const KUrl& baseUrl = url();
595 KUrl url;
596 QItemSelection new_selection;
597 foreach(const KFileItem& item, selection) {
598 url = item.url().upUrl();
599 if (baseUrl.equals(url, KUrl::CompareWithoutTrailingSlash)) {
600 QModelIndex index = m_proxyModel->mapFromSource(m_dolphinModel->indexForItem(item));
601 new_selection.select(index, index);
604 itemView()->selectionModel()->select(new_selection,
605 QItemSelectionModel::ClearAndSelect
606 | QItemSelectionModel::Current);
609 void DolphinView::renameSelectedItems()
611 KFileItemList items = selectedItems();
612 const int itemCount = items.count();
613 if (itemCount < 1) {
614 return;
617 if (itemCount > 1) {
618 // More than one item has been selected for renaming. Open
619 // a rename dialog and rename all items afterwards.
620 RenameDialog dialog(this, items);
621 if (dialog.exec() == QDialog::Rejected) {
622 return;
625 const QString newName = dialog.newName();
626 if (newName.isEmpty()) {
627 emit errorMessage(dialog.errorString());
628 } else {
629 // TODO: check how this can be integrated into KIO::FileUndoManager/KonqOperations
630 // as one operation instead of n rename operations like it is done now...
631 Q_ASSERT(newName.contains('#'));
633 // currently the items are sorted by the selection order, resort
634 // them by the file name
635 qSort(items.begin(), items.end(), lessThan);
637 // iterate through all selected items and rename them...
638 int index = 1;
639 foreach (const KFileItem& item, items) {
640 const KUrl& oldUrl = item.url();
641 QString number;
642 number.setNum(index++);
644 QString name = newName;
645 name.replace('#', number);
647 if (oldUrl.fileName() != name) {
648 KUrl newUrl = oldUrl;
649 newUrl.setFileName(name);
650 KonqOperations::rename(this, oldUrl, newUrl);
654 } else if (DolphinSettings::instance().generalSettings()->renameInline()) {
655 Q_ASSERT(itemCount == 1);
657 if (isColumnViewActive()) {
658 m_columnView->editItem(items.first());
659 } else {
660 const QModelIndex dirIndex = m_dolphinModel->indexForItem(items.first());
661 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
662 itemView()->edit(proxyIndex);
664 } else {
665 Q_ASSERT(itemCount == 1);
667 RenameDialog dialog(this, items);
668 if (dialog.exec() == QDialog::Rejected) {
669 return;
672 const QString& newName = dialog.newName();
673 if (newName.isEmpty()) {
674 emit errorMessage(dialog.errorString());
675 } else {
676 const KUrl& oldUrl = items.first().url();
677 KUrl newUrl = oldUrl;
678 newUrl.setFileName(newName);
679 KonqOperations::rename(this, oldUrl, newUrl);
684 void DolphinView::trashSelectedItems()
686 const KUrl::List list = simplifiedSelectedUrls();
687 KonqOperations::del(this, KonqOperations::TRASH, list);
690 void DolphinView::deleteSelectedItems()
692 const KUrl::List list = simplifiedSelectedUrls();
693 const bool del = KonqOperations::askDeleteConfirmation(list,
694 KonqOperations::DEL,
695 KonqOperations::DEFAULT_CONFIRMATION,
696 this);
698 if (del) {
699 KIO::Job* job = KIO::del(list);
700 connect(job, SIGNAL(result(KJob*)),
701 this, SLOT(slotDeleteFileFinished(KJob*)));
705 void DolphinView::cutSelectedItems()
707 QMimeData* mimeData = selectionMimeData();
708 KonqMimeData::addIsCutSelection(mimeData, true);
709 QApplication::clipboard()->setMimeData(mimeData);
712 void DolphinView::copySelectedItems()
714 QMimeData* mimeData = selectionMimeData();
715 QApplication::clipboard()->setMimeData(mimeData);
718 void DolphinView::paste()
720 pasteToUrl(url());
723 void DolphinView::pasteIntoFolder()
725 const KFileItemList items = selectedItems();
726 if ((items.count() == 1) && items.first().isDir()) {
727 pasteToUrl(items.first().url());
731 void DolphinView::setShowPreview(bool show)
733 if (m_showPreview == show) {
734 return;
737 const KUrl viewPropsUrl = viewPropertiesUrl();
738 ViewProperties props(viewPropsUrl);
739 props.setShowPreview(show);
741 m_showPreview = show;
742 m_previewGenerator->setPreviewShown(show);
744 const int oldZoomLevel = m_controller->zoomLevel();
745 emit showPreviewChanged();
747 // Enabling or disabling the preview might change the icon size of the view.
748 // As the view does not emit a signal when the icon size has been changed,
749 // the used zoom level of the controller must be adjusted manually:
750 updateZoomLevel(oldZoomLevel);
752 loadDirectory(viewPropsUrl);
755 void DolphinView::setShowHiddenFiles(bool show)
757 if (m_dirLister->showingDotFiles() == show) {
758 return;
761 const KUrl viewPropsUrl = viewPropertiesUrl();
762 ViewProperties props(viewPropsUrl);
763 props.setShowHiddenFiles(show);
765 m_dirLister->setShowingDotFiles(show);
766 emit showHiddenFilesChanged();
768 loadDirectory(viewPropsUrl);
771 void DolphinView::setCategorizedSorting(bool categorized)
773 if (categorized == categorizedSorting()) {
774 return;
777 // setCategorizedSorting(true) may only get invoked
778 // if the view supports categorized sorting
779 Q_ASSERT(!categorized || supportsCategorizedSorting());
781 ViewProperties props(viewPropertiesUrl());
782 props.setCategorizedSorting(categorized);
783 props.save();
785 m_storedCategorizedSorting = categorized;
786 m_proxyModel->setCategorizedModel(categorized);
788 emit categorizedSortingChanged();
791 void DolphinView::toggleSortOrder()
793 const Qt::SortOrder order = (sortOrder() == Qt::AscendingOrder) ?
794 Qt::DescendingOrder :
795 Qt::AscendingOrder;
796 setSortOrder(order);
799 void DolphinView::toggleAdditionalInfo(QAction* action)
801 const KFileItemDelegate::Information info =
802 static_cast<KFileItemDelegate::Information>(action->data().toInt());
804 KFileItemDelegate::InformationList list = additionalInfo();
806 const bool show = action->isChecked();
808 const int index = list.indexOf(info);
809 const bool containsInfo = (index >= 0);
810 if (show && !containsInfo) {
811 list.append(info);
812 setAdditionalInfo(list);
813 } else if (!show && containsInfo) {
814 list.removeAt(index);
815 setAdditionalInfo(list);
816 Q_ASSERT(list.indexOf(info) < 0);
820 void DolphinView::mouseReleaseEvent(QMouseEvent* event)
822 QWidget::mouseReleaseEvent(event);
823 setActive(true);
826 void DolphinView::wheelEvent(QWheelEvent* event)
828 if (event->modifiers() & Qt::ControlModifier) {
829 const int delta = event->delta();
830 const int level = zoomLevel();
831 if (delta > 0) {
832 setZoomLevel(level + 1);
833 } else if (delta < 0) {
834 setZoomLevel(level - 1);
836 event->accept();
840 bool DolphinView::eventFilter(QObject* watched, QEvent* event)
842 switch (event->type()) {
843 case QEvent::FocusIn:
844 if (watched == itemView()) {
845 m_controller->requestActivation();
847 break;
849 case QEvent::MouseButtonPress:
850 if ((watched == itemView()->viewport()) && (m_expandedViews.count() > 0)) {
851 // Listening to a mousebutton press event to delete expanded views is a
852 // workaround, as it seems impossible for the FolderExpander to know when
853 // a dragging outside a view has been finished. However it works quite well:
854 // A mousebutton press event indicates that a drag operation must be
855 // finished already.
856 deleteExpandedViews();
858 break;
860 case QEvent::DragEnter:
861 if (watched == itemView()->viewport()) {
862 setActive(true);
864 break;
866 default:
867 break;
870 return QWidget::eventFilter(watched, event);
873 void DolphinView::activate()
875 setActive(true);
878 void DolphinView::triggerItem(const KFileItem& item)
880 const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
881 if ((modifier & Qt::ShiftModifier) || (modifier & Qt::ControlModifier)) {
882 // items are selected by the user, hence don't trigger the
883 // item specified by 'index'
884 return;
887 // TODO: the m_isContextMenuOpen check is a workaround for Qt-issue 207192
888 if (item.isNull() || m_isContextMenuOpen) {
889 return;
892 if (m_toolTipManager != 0) {
893 m_toolTipManager->hideTip();
895 emit itemTriggered(item); // caught by DolphinViewContainer or DolphinPart
898 void DolphinView::emitSelectionChangedSignal()
900 emit selectionChanged(DolphinView::selectedItems());
903 void DolphinView::openContextMenu(const QPoint& pos)
905 KFileItem item;
906 if (isColumnViewActive()) {
907 item = m_columnView->itemAt(pos);
908 } else {
909 const QModelIndex index = itemView()->indexAt(pos);
910 if (index.isValid() && (index.column() == DolphinModel::Name)) {
911 const QModelIndex dolphinModelIndex = m_proxyModel->mapToSource(index);
912 item = m_dolphinModel->itemForIndex(dolphinModelIndex);
916 if (m_toolTipManager != 0) {
917 m_toolTipManager->hideTip();
920 m_isContextMenuOpen = true; // TODO: workaround for Qt-issue 207192
921 emit requestContextMenu(item, url());
922 m_isContextMenuOpen = false;
925 void DolphinView::dropUrls(const KFileItem& destItem,
926 const KUrl& destPath,
927 QDropEvent* event)
929 DragAndDropHelper::instance().dropUrls(destItem, destPath, event, this);
932 void DolphinView::updateSorting(DolphinView::Sorting sorting)
934 ViewProperties props(viewPropertiesUrl());
935 props.setSorting(sorting);
937 m_proxyModel->setSorting(sorting);
939 emit sortingChanged(sorting);
942 void DolphinView::updateSortOrder(Qt::SortOrder order)
944 ViewProperties props(viewPropertiesUrl());
945 props.setSortOrder(order);
947 m_proxyModel->setSortOrder(order);
949 emit sortOrderChanged(order);
952 void DolphinView::updateAdditionalInfo(const KFileItemDelegate::InformationList& info)
954 ViewProperties props(viewPropertiesUrl());
955 props.setAdditionalInfo(info);
956 props.save();
958 m_fileItemDelegate->setShowInformation(info);
960 emit additionalInfoChanged();
963 void DolphinView::updateAdditionalInfoActions(KActionCollection* collection)
965 const bool enable = (m_mode == DolphinView::DetailsView) ||
966 (m_mode == DolphinView::IconsView);
968 QAction* showSizeInfo = collection->action("show_size_info");
969 QAction* showDateInfo = collection->action("show_date_info");
970 QAction* showPermissionsInfo = collection->action("show_permissions_info");
971 QAction* showOwnerInfo = collection->action("show_owner_info");
972 QAction* showGroupInfo = collection->action("show_group_info");
973 QAction* showMimeInfo = collection->action("show_mime_info");
975 showSizeInfo->setChecked(false);
976 showDateInfo->setChecked(false);
977 showPermissionsInfo->setChecked(false);
978 showOwnerInfo->setChecked(false);
979 showGroupInfo->setChecked(false);
980 showMimeInfo->setChecked(false);
982 showSizeInfo->setEnabled(enable);
983 showDateInfo->setEnabled(enable);
984 showPermissionsInfo->setEnabled(enable);
985 showOwnerInfo->setEnabled(enable);
986 showGroupInfo->setEnabled(enable);
987 showMimeInfo->setEnabled(enable);
989 foreach (KFileItemDelegate::Information info, m_fileItemDelegate->showInformation()) {
990 switch (info) {
991 case KFileItemDelegate::Size:
992 showSizeInfo->setChecked(true);
993 break;
994 case KFileItemDelegate::ModificationTime:
995 showDateInfo->setChecked(true);
996 break;
997 case KFileItemDelegate::Permissions:
998 showPermissionsInfo->setChecked(true);
999 break;
1000 case KFileItemDelegate::Owner:
1001 showOwnerInfo->setChecked(true);
1002 break;
1003 case KFileItemDelegate::OwnerAndGroup:
1004 showGroupInfo->setChecked(true);
1005 break;
1006 case KFileItemDelegate::FriendlyMimeType:
1007 showMimeInfo->setChecked(true);
1008 break;
1009 default:
1010 break;
1015 QPair<bool, QString> DolphinView::pasteInfo() const
1017 QPair<bool, QString> ret;
1018 QClipboard* clipboard = QApplication::clipboard();
1019 const QMimeData* mimeData = clipboard->mimeData();
1021 KUrl::List urls = KUrl::List::fromMimeData(mimeData);
1022 if (!urls.isEmpty()) {
1023 // disable the paste action if no writing is supported
1024 KFileItem item(KFileItem::Unknown, KFileItem::Unknown, url());
1025 ret.first = KonqFileItemCapabilities(KFileItemList() << item).supportsWriting();
1027 if (urls.count() == 1) {
1028 const KFileItem item(KFileItem::Unknown, KFileItem::Unknown, urls.first(), true);
1029 ret.second = item.isDir() ? i18nc("@action:inmenu", "Paste One Folder") :
1030 i18nc("@action:inmenu", "Paste One File");
1032 } else {
1033 ret.second = i18ncp("@action:inmenu", "Paste One Item", "Paste %1 Items", urls.count());
1035 } else {
1036 ret.first = false;
1037 ret.second = i18nc("@action:inmenu", "Paste");
1040 return ret;
1043 void DolphinView::setTabsForFilesEnabled(bool tabsForFiles)
1045 m_tabsForFiles = tabsForFiles;
1048 bool DolphinView::isTabsForFilesEnabled() const
1050 return m_tabsForFiles;
1053 bool DolphinView::itemsExpandable() const
1055 return (m_detailsView != 0) && m_detailsView->itemsExpandable();
1058 void DolphinView::emitContentsMoved()
1060 // only emit the contents moved signal if:
1061 // - no directory loading is ongoing (this would reset the contents position
1062 // always to (0, 0))
1063 // - if the Column View is active: the column view does an automatic
1064 // positioning during the loading operation, which must be remembered
1065 if (!m_loadingDirectory || isColumnViewActive()) {
1066 const QPoint pos(contentsPosition());
1067 emit contentsMoved(pos.x(), pos.y());
1071 void DolphinView::showHoverInformation(const KFileItem& item)
1073 emit requestItemInfo(item);
1076 void DolphinView::clearHoverInformation()
1078 emit requestItemInfo(KFileItem());
1081 void DolphinView::slotDeleteFileFinished(KJob* job)
1083 if (job->error() == 0) {
1084 emit operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
1085 } else if (job->error() != KIO::ERR_USER_CANCELED) {
1086 emit errorMessage(job->errorString());
1090 void DolphinView::slotRequestUrlChange(const KUrl& url)
1092 emit requestUrlChange(url);
1093 m_controller->setUrl(url);
1096 void DolphinView::restoreCurrentItem()
1098 const QModelIndex dirIndex = m_dolphinModel->indexForUrl(m_currentItemUrl);
1099 if (dirIndex.isValid()) {
1100 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
1101 QAbstractItemView* view = itemView();
1102 const bool clearSelection = !hasSelection();
1103 view->setCurrentIndex(proxyIndex);
1104 if (clearSelection) {
1105 view->clearSelection();
1110 void DolphinView::enterDir(const QModelIndex& index, QAbstractItemView* view)
1112 // Deleting a view that is the root of a drag operation is not allowed, otherwise
1113 // the dragging gets automatically cancelled by Qt. So before entering a new
1114 // directory, the current view is remembered in m_expandedViews and deleted
1115 // later when the drag operation has been finished (see DolphinView::eventFilter()).
1116 m_expandedViews.append(view);
1117 m_controller->triggerItem(index);
1120 void DolphinView::loadDirectory(const KUrl& url, bool reload)
1122 if (!url.isValid()) {
1123 const QString location(url.pathOrUrl());
1124 if (location.isEmpty()) {
1125 emit errorMessage(i18nc("@info:status", "The location is empty."));
1126 } else {
1127 emit errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location));
1129 return;
1132 m_loadingDirectory = true;
1134 m_dirLister->stop();
1135 m_dirLister->openUrl(url, reload ? KDirLister::Reload : KDirLister::NoFlags);
1137 if (isColumnViewActive()) {
1138 // adjusting the directory lister is not enough in the case of the
1139 // column view, as each column has its own directory lister internally...
1140 if (reload) {
1141 m_columnView->reload();
1142 } else {
1143 m_columnView->showColumn(url);
1148 KUrl DolphinView::viewPropertiesUrl() const
1150 if (isColumnViewActive()) {
1151 return m_columnView->rootUrl();
1154 return url();
1157 void DolphinView::applyViewProperties(const KUrl& url)
1159 if (m_ignoreViewProperties) {
1160 return;
1163 if (isColumnViewActive() && rootUrl().isParentOf(url)) {
1164 // The column view is active, hence don't apply the view properties
1165 // of sub directories (represented by columns) to the view. The
1166 // view always represents the properties of the first column.
1167 return;
1170 const ViewProperties props(url);
1172 const Mode mode = props.viewMode();
1173 if (m_mode != mode) {
1174 const int oldZoomLevel = m_controller->zoomLevel();
1176 m_mode = mode;
1177 createView();
1178 emit modeChanged();
1180 updateZoomLevel(oldZoomLevel);
1182 if (itemView() == 0) {
1183 createView();
1185 Q_ASSERT(itemView() != 0);
1186 Q_ASSERT(m_fileItemDelegate != 0);
1188 const bool showHiddenFiles = props.showHiddenFiles();
1189 if (showHiddenFiles != m_dirLister->showingDotFiles()) {
1190 m_dirLister->setShowingDotFiles(showHiddenFiles);
1191 emit showHiddenFilesChanged();
1194 m_storedCategorizedSorting = props.categorizedSorting();
1195 const bool categorized = m_storedCategorizedSorting && supportsCategorizedSorting();
1196 if (categorized != m_proxyModel->isCategorizedModel()) {
1197 m_proxyModel->setCategorizedModel(categorized);
1198 emit categorizedSortingChanged();
1201 const DolphinView::Sorting sorting = props.sorting();
1202 if (sorting != m_proxyModel->sorting()) {
1203 m_proxyModel->setSorting(sorting);
1204 emit sortingChanged(sorting);
1207 const Qt::SortOrder sortOrder = props.sortOrder();
1208 if (sortOrder != m_proxyModel->sortOrder()) {
1209 m_proxyModel->setSortOrder(sortOrder);
1210 emit sortOrderChanged(sortOrder);
1213 KFileItemDelegate::InformationList info = props.additionalInfo();
1214 if (info != m_fileItemDelegate->showInformation()) {
1215 m_fileItemDelegate->setShowInformation(info);
1216 emit additionalInfoChanged();
1219 const bool showPreview = props.showPreview();
1220 if (showPreview != m_showPreview) {
1221 m_showPreview = showPreview;
1222 m_previewGenerator->setPreviewShown(showPreview);
1224 const int oldZoomLevel = m_controller->zoomLevel();
1225 emit showPreviewChanged();
1227 // Enabling or disabling the preview might change the icon size of the view.
1228 // As the view does not emit a signal when the icon size has been changed,
1229 // the used zoom level of the controller must be adjusted manually:
1230 updateZoomLevel(oldZoomLevel);
1233 if (DolphinSettings::instance().generalSettings()->globalViewProps()) {
1234 // During the lifetime of a DolphinView instance the global view properties
1235 // should not be changed. This allows e. g. to split a view and use different
1236 // view properties for each view.
1237 m_ignoreViewProperties = true;
1241 void DolphinView::createView()
1243 deleteView();
1244 Q_ASSERT(m_iconsView == 0);
1245 Q_ASSERT(m_detailsView == 0);
1246 Q_ASSERT(m_columnView == 0);
1248 QAbstractItemView* view = 0;
1249 switch (m_mode) {
1250 case IconsView: {
1251 m_iconsView = new DolphinIconsView(this, m_controller);
1252 view = m_iconsView;
1253 break;
1256 case DetailsView:
1257 m_detailsView = new DolphinDetailsView(this, m_controller);
1258 view = m_detailsView;
1259 break;
1261 case ColumnView:
1262 m_columnView = new DolphinColumnView(this, m_controller);
1263 view = m_columnView;
1264 break;
1267 Q_ASSERT(view != 0);
1268 view->installEventFilter(this);
1269 view->viewport()->installEventFilter(this);
1270 setFocusProxy(view);
1272 if (m_mode != ColumnView) {
1273 // Give the view the ability to auto-expand its directories on hovering
1274 // (the column view takes care about this itself). If the details view
1275 // uses expandable folders, the auto-expanding should be used always.
1276 DolphinSettings& settings = DolphinSettings::instance();
1277 const bool enabled = settings.generalSettings()->autoExpandFolders() ||
1278 ((m_detailsView != 0) && settings.detailsModeSettings()->expandableFolders());
1280 FolderExpander* folderExpander = new FolderExpander(view, m_proxyModel);
1281 folderExpander->setEnabled(enabled);
1282 connect(folderExpander, SIGNAL(enterDir(const QModelIndex&, QAbstractItemView*)),
1283 this, SLOT(enterDir(const QModelIndex&, QAbstractItemView*)));
1286 m_controller->setItemView(view);
1288 m_fileItemDelegate = new DolphinFileItemDelegate(view);
1289 m_fileItemDelegate->setShowToolTipWhenElided(false);
1290 m_fileItemDelegate->setMinimizedNameColumn(m_mode == DetailsView);
1291 view->setItemDelegate(m_fileItemDelegate);
1293 view->setModel(m_proxyModel);
1294 if (m_selectionModel != 0) {
1295 view->setSelectionModel(m_selectionModel);
1296 } else {
1297 m_selectionModel = view->selectionModel();
1300 // reparent the selection model, as it should not be deleted
1301 // when deleting the model
1302 m_selectionModel->setParent(this);
1304 view->setSelectionMode(QAbstractItemView::ExtendedSelection);
1306 m_previewGenerator = new KFilePreviewGenerator(view);
1307 m_previewGenerator->setPreviewShown(m_showPreview);
1309 if (DolphinSettings::instance().generalSettings()->showToolTips()) {
1310 m_toolTipManager = new ToolTipManager(view, m_proxyModel);
1311 connect(m_controller, SIGNAL(hideToolTip()),
1312 m_toolTipManager, SLOT(hideTip()));
1315 m_topLayout->insertWidget(1, view);
1317 connect(view->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
1318 this, SLOT(emitSelectionChangedSignal()));
1319 connect(view->verticalScrollBar(), SIGNAL(valueChanged(int)),
1320 this, SLOT(emitContentsMoved()));
1321 connect(view->horizontalScrollBar(), SIGNAL(valueChanged(int)),
1322 this, SLOT(emitContentsMoved()));
1325 void DolphinView::deleteView()
1327 QAbstractItemView* view = itemView();
1328 if (view != 0) {
1329 // It's important to set the keyboard focus to the parent
1330 // before deleting the view: Otherwise when having a split
1331 // view the other view will get the focus and will request
1332 // an activation (see DolphinView::eventFilter()).
1333 setFocusProxy(0);
1334 setFocus();
1336 m_topLayout->removeWidget(view);
1337 view->close();
1339 disconnect(view);
1340 m_controller->disconnect(view);
1341 view->disconnect();
1343 bool deleteView = true;
1344 foreach (const QAbstractItemView* expandedView, m_expandedViews) {
1345 if (view == expandedView) {
1346 // the current view got already expanded and must stay alive
1347 // until the dragging has been completed
1348 deleteView = false;
1349 break;
1352 if (deleteView) {
1353 view->deleteLater();
1355 view = 0;
1357 m_iconsView = 0;
1358 m_detailsView = 0;
1359 m_columnView = 0;
1360 m_fileItemDelegate = 0;
1361 m_previewGenerator = 0;
1362 m_toolTipManager = 0;
1366 QAbstractItemView* DolphinView::itemView() const
1368 if (m_detailsView != 0) {
1369 return m_detailsView;
1370 } else if (m_columnView != 0) {
1371 return m_columnView;
1374 return m_iconsView;
1377 bool DolphinView::isCutItem(const KFileItem& item) const
1379 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
1380 const KUrl::List cutUrls = KUrl::List::fromMimeData(mimeData);
1382 const KUrl& itemUrl = item.url();
1383 KUrl::List::const_iterator it = cutUrls.begin();
1384 const KUrl::List::const_iterator end = cutUrls.end();
1385 while (it != end) {
1386 if (*it == itemUrl) {
1387 return true;
1389 ++it;
1392 return false;
1395 void DolphinView::pasteToUrl(const KUrl& url)
1397 QClipboard* clipboard = QApplication::clipboard();
1398 const QMimeData* mimeData = clipboard->mimeData();
1400 const KUrl::List sourceUrls = KUrl::List::fromMimeData(mimeData);
1401 if (KonqMimeData::decodeIsCutSelection(mimeData)) {
1402 KonqOperations::copy(this, KonqOperations::MOVE, sourceUrls, url);
1403 clipboard->clear();
1404 } else {
1405 KonqOperations::copy(this, KonqOperations::COPY, sourceUrls, url);
1409 void DolphinView::updateZoomLevel(int oldZoomLevel)
1411 const int newZoomLevel = ZoomLevelInfo::zoomLevelForIconSize(itemView()->iconSize());
1412 if (oldZoomLevel != newZoomLevel) {
1413 m_controller->setZoomLevel(newZoomLevel);
1414 emit zoomLevelChanged(newZoomLevel);
1418 KUrl::List DolphinView::simplifiedSelectedUrls() const
1420 KUrl::List list = selectedUrls();
1421 if (itemsExpandable() ) {
1422 list = KDirModel::simplifiedUrlList(list);
1424 return list;
1427 void DolphinView::deleteExpandedViews()
1429 const QAbstractItemView* view = itemView();
1430 foreach (QAbstractItemView* expandedView, m_expandedViews) {
1431 if (expandedView != view) {
1432 expandedView->deleteLater();
1435 m_expandedViews.clear();
1438 QMimeData* DolphinView::selectionMimeData() const
1440 if (isColumnViewActive()) {
1441 return m_columnView->selectionMimeData();
1444 const QAbstractItemView* view = itemView();
1445 Q_ASSERT((view != 0) && (view->selectionModel() != 0));
1446 const QItemSelection selection = m_proxyModel->mapSelectionToSource(view->selectionModel()->selection());
1447 return m_dolphinModel->mimeData(selection.indexes());
1450 #include "dolphinview.moc"