X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/blobdiff_plain/b8a4e0fac2820ec8fc442c2abeea700ae4947e01..4e8e54e6dbfe507b6513b10f383d8aab42460a64:/src/dolphinview.cpp diff --git a/src/dolphinview.cpp b/src/dolphinview.cpp index 221d20376..5ef00d60a 100644 --- a/src/dolphinview.cpp +++ b/src/dolphinview.cpp @@ -31,7 +31,7 @@ #include #include #include -#include +#include #include #include #include @@ -47,19 +47,22 @@ #include #include -#include "dolphindropcontroller.h" #include "dolphinmodel.h" #include "dolphincolumnview.h" #include "dolphincontroller.h" +#include "dolphinfileitemdelegate.h" #include "dolphinsortfilterproxymodel.h" #include "dolphindetailsview.h" +#include "dolphin_detailsmodesettings.h" #include "dolphiniconsview.h" #include "dolphinsettings.h" #include "dolphin_generalsettings.h" -#include "iconmanager.h" +#include "draganddrophelper.h" +#include "folderexpander.h" #include "renamedialog.h" #include "tooltipmanager.h" #include "viewproperties.h" +#include "zoomlevelinfo.h" DolphinView::DolphinView(QWidget* parent, const KUrl& url, @@ -84,10 +87,11 @@ DolphinView::DolphinView(QWidget* parent, m_dolphinModel(dolphinModel), m_dirLister(dirLister), m_proxyModel(proxyModel), - m_iconManager(0), + m_previewGenerator(0), m_toolTipManager(0), m_rootUrl(), - m_currentItemUrl() + m_currentItemUrl(), + m_expandedViews() { m_topLayout = new QVBoxLayout(this); m_topLayout->setSpacing(0); @@ -103,8 +107,8 @@ DolphinView::DolphinView(QWidget* parent, connect(m_controller, SIGNAL(requestContextMenu(const QPoint&)), this, SLOT(openContextMenu(const QPoint&))); - connect(m_controller, SIGNAL(urlsDropped(const KUrl::List&, const KUrl&, const KFileItem&)), - this, SLOT(dropUrls(const KUrl::List&, const KUrl&, const KFileItem&))); + connect(m_controller, SIGNAL(urlsDropped(const KFileItem&, const KUrl&, QDropEvent*)), + this, SLOT(dropUrls(const KFileItem&, const KUrl&, QDropEvent*))); connect(m_controller, SIGNAL(sortingChanged(DolphinView::Sorting)), this, SLOT(updateSorting(DolphinView::Sorting))); connect(m_controller, SIGNAL(sortOrderChanged(Qt::SortOrder)), @@ -123,7 +127,7 @@ DolphinView::DolphinView(QWidget* parent, this, SLOT(clearHoverInformation())); connect(m_dirLister, SIGNAL(redirection(KUrl, KUrl)), - this, SLOT(slotRedirection(KUrl, KUrl))); + this, SIGNAL(redirection(KUrl, KUrl))); connect(m_dirLister, SIGNAL(completed()), this, SLOT(restoreCurrentItem())); @@ -133,6 +137,7 @@ DolphinView::DolphinView(QWidget* parent, DolphinView::~DolphinView() { + deleteExpandedViews(); } const KUrl& DolphinView::url() const @@ -190,6 +195,7 @@ void DolphinView::setMode(Mode mode) return; // the wished mode is already set } + const int oldZoomLevel = m_controller->zoomLevel(); m_mode = mode; deleteView(); @@ -216,6 +222,7 @@ void DolphinView::setMode(Mode mode) } emit modeChanged(); + updateZoomLevel(oldZoomLevel); } DolphinView::Mode DolphinView::mode() const @@ -329,6 +336,17 @@ KUrl::List DolphinView::selectedUrls() const return urls; } +int DolphinView::selectedItemsCount() const +{ + if (isColumnViewActive()) { + // TODO: get rid of this special case by adjusting the dir lister + // to the current column + return m_columnView->selectedItems().count(); + } + + return itemView()->selectionModel()->selection().count(); +} + void DolphinView::setContentsPosition(int x, int y) { QAbstractItemView* view = itemView(); @@ -349,26 +367,24 @@ QPoint DolphinView::contentsPosition() const return QPoint(x, y); } -void DolphinView::zoomIn() -{ - m_controller->triggerZoomIn(); - m_iconManager->updatePreviews(); -} - -void DolphinView::zoomOut() +void DolphinView::setZoomLevel(int level) { - m_controller->triggerZoomOut(); - m_iconManager->updatePreviews(); -} - -bool DolphinView::isZoomInPossible() const -{ - return m_controller->isZoomInPossible(); + if (level < ZoomLevelInfo::minimumLevel()) { + level = ZoomLevelInfo::minimumLevel(); + } else if (level > ZoomLevelInfo::maximumLevel()) { + level = ZoomLevelInfo::maximumLevel(); + } + + if (level != zoomLevel()) { + m_controller->setZoomLevel(level); + m_previewGenerator->updatePreviews(); + emit zoomLevelChanged(level); + } } -bool DolphinView::isZoomOutPossible() const +int DolphinView::zoomLevel() const { - return m_controller->isZoomOutPossible(); + return m_controller->zoomLevel(); } void DolphinView::setSorting(Sorting sorting) @@ -425,6 +441,7 @@ void DolphinView::reload() void DolphinView::refresh() { const bool oldActivationState = m_active; + const int oldZoomLevel = m_controller->zoomLevel(); m_active = true; createView(); @@ -432,6 +449,7 @@ void DolphinView::refresh() reload(); setActive(oldActivationState); + updateZoomLevel(oldZoomLevel); } void DolphinView::updateView(const KUrl& url, const KUrl& rootUrl) @@ -440,7 +458,7 @@ void DolphinView::updateView(const KUrl& url, const KUrl& rootUrl) return; } - m_iconManager->cancelPreviews(); + m_previewGenerator->cancelPreviews(); m_controller->setUrl(url); // emits urlChanged, which we forward if (!rootUrl.isEmpty() && rootUrl.isParentOf(url)) { @@ -469,17 +487,70 @@ void DolphinView::setNameFilter(const QString& nameFilter) } } -void DolphinView::calculateItemCount(int& fileCount, int& folderCount) +void DolphinView::calculateItemCount(int& fileCount, + int& folderCount, + KIO::filesize_t& totalFileSize) const { - foreach (const KFileItem &item, m_dirLister->items()) { + foreach (const KFileItem& item, m_dirLister->items()) { if (item.isDir()) { ++folderCount; } else { ++fileCount; + totalFileSize += item.size(); } } } +QString DolphinView::statusBarText() const +{ + QString text; + int folderCount = 0; + int fileCount = 0; + KIO::filesize_t totalFileSize = 0; + + if (hasSelection()) { + // give a summary of the status of the selected files + const KFileItemList list = selectedItems(); + if (list.isEmpty()) { + // when an item is triggered, it is temporary selected but selectedItems() + // will return an empty list + return text; + } + + KFileItemList::const_iterator it = list.begin(); + const KFileItemList::const_iterator end = list.end(); + while (it != end) { + const KFileItem& item = *it; + if (item.isDir()) { + ++folderCount; + } else { + ++fileCount; + totalFileSize += item.size(); + } + ++it; + } + + const QString foldersText = i18ncp("@info:status", "1 Folder selected", "%1 Folders selected", folderCount); + const QString filesText = i18ncp("@info:status", "1 File selected", "%1 Files selected", fileCount); + if ((folderCount > 0) && (fileCount > 0)) { + text = i18nc("@info:status folders, files (size)", "%1, %2 (%3)", + foldersText, filesText, KIO::convertSize(totalFileSize)); + } else if (fileCount > 0) { + text = i18nc("@info:status files (size)", "%1 (%2)", filesText, KIO::convertSize(totalFileSize)); + } else { + Q_ASSERT(folderCount > 0); + text = foldersText; + } + } else { + calculateItemCount(fileCount, folderCount, totalFileSize); + text = KIO::itemsSummaryString(fileCount + folderCount, + fileCount, folderCount, + totalFileSize, true); + } + + return text; +} + void DolphinView::setUrl(const KUrl& url) { // remember current item candidate (see restoreCurrentItem()) @@ -511,7 +582,12 @@ void DolphinView::changeSelection(const KFileItemList& selection) void DolphinView::renameSelectedItems() { const KFileItemList items = selectedItems(); - if (items.count() > 1) { + const int itemCount = items.count(); + if (itemCount < 1) { + return; + } + + if (itemCount > 1) { // More than one item has been selected for renaming. Open // a rename dialog and rename all items afterwards. RenameDialog dialog(this, items); @@ -541,13 +617,12 @@ void DolphinView::renameSelectedItems() KUrl newUrl = oldUrl; newUrl.setFileName(name); KonqOperations::rename(this, oldUrl, newUrl); - emit doingOperation(KIO::FileUndoManager::Rename); } } } } else if (DolphinSettings::instance().generalSettings()->renameInline()) { - Q_ASSERT(items.count() == 1); - + Q_ASSERT(itemCount == 1); + if (isColumnViewActive()) { m_columnView->editItem(items.first()); } else { @@ -556,8 +631,8 @@ void DolphinView::renameSelectedItems() itemView()->edit(proxyIndex); } } else { - Q_ASSERT(items.count() == 1); - + Q_ASSERT(itemCount == 1); + RenameDialog dialog(this, items); if (dialog.exec() == QDialog::Rejected) { return; @@ -571,20 +646,19 @@ void DolphinView::renameSelectedItems() KUrl newUrl = oldUrl; newUrl.setFileName(newName); KonqOperations::rename(this, oldUrl, newUrl); - emit doingOperation(KIO::FileUndoManager::Rename); } } } void DolphinView::trashSelectedItems() { - emit doingOperation(KIO::FileUndoManager::Trash); - KonqOperations::del(this, KonqOperations::TRASH, selectedUrls()); + const KUrl::List list = simplifiedSelectedUrls(); + KonqOperations::del(this, KonqOperations::TRASH, list); } void DolphinView::deleteSelectedItems() { - const KUrl::List list = selectedUrls(); + const KUrl::List list = simplifiedSelectedUrls(); const bool del = KonqOperations::askDeleteConfirmation(list, KonqOperations::DEL, KonqOperations::DEFAULT_CONFIRMATION, @@ -600,7 +674,7 @@ void DolphinView::deleteSelectedItems() void DolphinView::cutSelectedItems() { QMimeData* mimeData = new QMimeData(); - const KUrl::List kdeUrls = selectedUrls(); + const KUrl::List kdeUrls = simplifiedSelectedUrls(); const KUrl::List mostLocalUrls; KonqMimeData::populateMimeData(mimeData, kdeUrls, mostLocalUrls, true); QApplication::clipboard()->setMimeData(mimeData); @@ -639,8 +713,15 @@ void DolphinView::setShowPreview(bool show) props.setShowPreview(show); m_showPreview = show; - m_iconManager->setShowPreview(show); + m_previewGenerator->setPreviewShown(show); + + const int oldZoomLevel = m_controller->zoomLevel(); emit showPreviewChanged(); + + // Enabling or disabling the preview might change the icon size of the view. + // As the view does not emit a signal when the icon size has been changed, + // the used zoom level of the controller must be adjusted manually: + updateZoomLevel(oldZoomLevel); loadDirectory(viewPropsUrl); } @@ -710,7 +791,6 @@ void DolphinView::toggleAdditionalInfo(QAction* action) } } - void DolphinView::mouseReleaseEvent(QMouseEvent* event) { QWidget::mouseReleaseEvent(event); @@ -721,10 +801,11 @@ void DolphinView::wheelEvent(QWheelEvent* event) { if (event->modifiers() & Qt::ControlModifier) { const int delta = event->delta(); - if ((delta > 0) && isZoomInPossible()) { - zoomIn(); - } else if ((delta < 0) && isZoomOutPossible()) { - zoomOut(); + const int level = zoomLevel(); + if (delta > 0) { + setZoomLevel(level + 1); + } else if (delta < 0) { + setZoomLevel(level - 1); } event->accept(); } @@ -732,8 +813,32 @@ void DolphinView::wheelEvent(QWheelEvent* event) bool DolphinView::eventFilter(QObject* watched, QEvent* event) { - if ((watched == itemView()) && (event->type() == QEvent::FocusIn)) { - m_controller->requestActivation(); + switch (event->type()) { + case QEvent::FocusIn: + if (watched == itemView()) { + m_controller->requestActivation(); + } + break; + + case QEvent::MouseButtonPress: + if ((watched == itemView()->viewport()) && (m_expandedViews.count() > 0)) { + // Listening to a mousebutton press event to delete expanded views is a + // workaround, as it seems impossible for the FolderExpander to know when + // a dragging outside a view has been finished. However it works quite well: + // A mousebutton press event indicates that a drag operation must be + // finished already. + deleteExpandedViews(); + } + break; + + case QEvent::DragEnter: + if (watched == itemView()->viewport()) { + setActive(true); + } + break; + + default: + break; } return QWidget::eventFilter(watched, event); @@ -791,27 +896,11 @@ void DolphinView::openContextMenu(const QPoint& pos) m_isContextMenuOpen = false; } -void DolphinView::dropUrls(const KUrl::List& urls, +void DolphinView::dropUrls(const KFileItem& destItem, const KUrl& destPath, - const KFileItem& destItem) + QDropEvent* event) { - Q_ASSERT(!urls.isEmpty()); - const KUrl& destination = !destItem.isNull() && destItem.isDir() ? - destItem.url() : destPath; - const KUrl sourceDir = KUrl(urls.first().directory()); - if (sourceDir != destination) { - dropUrls(urls, destination); - } -} - -void DolphinView::dropUrls(const KUrl::List& urls, - const KUrl& destination) -{ - DolphinDropController dropController(this); - // forward doingOperation signal up to the mainwindow - connect(&dropController, SIGNAL(doingOperation(KIO::FileUndoManager::CommandType)), - this, SIGNAL(doingOperation(KIO::FileUndoManager::CommandType))); - dropController.dropUrls(urls, destination); + DragAndDropHelper::instance().dropUrls(destItem, destPath, event, this); } void DolphinView::updateSorting(DolphinView::Sorting sorting) @@ -962,11 +1051,16 @@ void DolphinView::slotDeleteFileFinished(KJob* job) { if (job->error() == 0) { emit operationCompletedMessage(i18nc("@info:status", "Delete operation completed.")); - } else { + } else if (job->error() != KIO::ERR_USER_CANCELED) { emit errorMessage(job->errorString()); } } +void DolphinView::slotRequestUrlChange(const KUrl& url) +{ + emit requestUrlChange(url); + m_controller->setUrl(url); +} void DolphinView::restoreCurrentItem() { @@ -982,6 +1076,16 @@ void DolphinView::restoreCurrentItem() } } +void DolphinView::enterDir(const QModelIndex& index, QAbstractItemView* view) +{ + // Deleting a view that is the root of a drag operation is not allowed, otherwise + // the dragging gets automatically cancelled by Qt. So before entering a new + // directory, the current view is remembered in m_expandedViews and deleted + // later when the drag operation has been finished (see DolphinView::eventFilter()). + m_expandedViews.append(view); + m_controller->triggerItem(index); +} + void DolphinView::loadDirectory(const KUrl& url, bool reload) { if (!url.isValid()) { @@ -1032,9 +1136,13 @@ void DolphinView::applyViewProperties(const KUrl& url) const Mode mode = props.viewMode(); if (m_mode != mode) { + const int oldZoomLevel = m_controller->zoomLevel(); + m_mode = mode; createView(); emit modeChanged(); + + updateZoomLevel(oldZoomLevel); } if (itemView() == 0) { createView(); @@ -1076,8 +1184,15 @@ void DolphinView::applyViewProperties(const KUrl& url) const bool showPreview = props.showPreview(); if (showPreview != m_showPreview) { m_showPreview = showPreview; - m_iconManager->setShowPreview(showPreview); + m_previewGenerator->setPreviewShown(showPreview); + + const int oldZoomLevel = m_controller->zoomLevel(); emit showPreviewChanged(); + + // Enabling or disabling the preview might change the icon size of the view. + // As the view does not emit a signal when the icon size has been changed, + // the used zoom level of the controller must be adjusted manually: + updateZoomLevel(oldZoomLevel); } } @@ -1109,10 +1224,27 @@ void DolphinView::createView() Q_ASSERT(view != 0); view->installEventFilter(this); + view->viewport()->installEventFilter(this); + + if (m_mode != ColumnView) { + // Give the view the ability to auto-expand its directories on hovering + // (the column view takes care about this itself). If the details view + // uses expandable folders, the auto-expanding should be used always. + DolphinSettings& settings = DolphinSettings::instance(); + const bool enabled = settings.generalSettings()->autoExpandFolders() || + ((m_detailsView != 0) && settings.detailsModeSettings()->expandableFolders()); + + FolderExpander* folderExpander = new FolderExpander(view, m_proxyModel); + folderExpander->setEnabled(enabled); + connect(folderExpander, SIGNAL(enterDir(const QModelIndex&, QAbstractItemView*)), + this, SLOT(enterDir(const QModelIndex&, QAbstractItemView*))); + } m_controller->setItemView(view); - m_fileItemDelegate = new KFileItemDelegate(view); + m_fileItemDelegate = new DolphinFileItemDelegate(view); + m_fileItemDelegate->setShowToolTipWhenElided(false); + m_fileItemDelegate->setMinimizedNameColumn(m_mode == DetailsView); view->setItemDelegate(m_fileItemDelegate); view->setModel(m_proxyModel); @@ -1128,11 +1260,13 @@ void DolphinView::createView() view->setSelectionMode(QAbstractItemView::ExtendedSelection); - m_iconManager = new IconManager(view, m_proxyModel); - m_iconManager->setShowPreview(m_showPreview); + m_previewGenerator = new KFilePreviewGenerator(view); + m_previewGenerator->setPreviewShown(m_showPreview); if (DolphinSettings::instance().generalSettings()->showToolTips()) { m_toolTipManager = new ToolTipManager(view, m_proxyModel); + connect(m_controller, SIGNAL(hideToolTip()), + m_toolTipManager, SLOT(hideTip())); } m_topLayout->insertWidget(1, view); @@ -1149,15 +1283,38 @@ void DolphinView::deleteView() { QAbstractItemView* view = itemView(); if (view != 0) { + // It's important to set the keyboard focus to the parent + // before deleting the view: Otherwise when having a split + // view the other view will get the focus and will request + // an activation (see DolphinView::eventFilter()). + setFocus(); + m_topLayout->removeWidget(view); view->close(); - view->deleteLater(); + + disconnect(view); + m_controller->disconnect(view); + view->disconnect(); + + bool deleteView = true; + foreach (const QAbstractItemView* expandedView, m_expandedViews) { + if (view == expandedView) { + // the current view got already expanded and must stay alive + // until the dragging has been completed + deleteView = false; + break; + } + } + if (deleteView) { + view->deleteLater(); + } view = 0; + m_iconsView = 0; m_detailsView = 0; m_columnView = 0; m_fileItemDelegate = 0; - m_iconManager = 0; + m_previewGenerator = 0; m_toolTipManager = 0; } } @@ -1199,25 +1356,44 @@ void DolphinView::pasteToUrl(const KUrl& url) const KUrl::List sourceUrls = KUrl::List::fromMimeData(mimeData); if (KonqMimeData::decodeIsCutSelection(mimeData)) { KonqOperations::copy(this, KonqOperations::MOVE, sourceUrls, url); - emit doingOperation(KIO::FileUndoManager::Move); clipboard->clear(); } else { KonqOperations::copy(this, KonqOperations::COPY, sourceUrls, url); - emit doingOperation(KIO::FileUndoManager::Copy); } } -void DolphinView::slotRequestUrlChange(const KUrl& url) +void DolphinView::updateZoomLevel(int oldZoomLevel) { - emit requestUrlChange(url); - m_controller->setUrl(url); + const int newZoomLevel = ZoomLevelInfo::zoomLevelForIconSize(itemView()->iconSize()); + if (oldZoomLevel != newZoomLevel) { + m_controller->setZoomLevel(newZoomLevel); + emit zoomLevelChanged(newZoomLevel); + } } -void DolphinView::slotRedirection(const KUrl& oldUrl, const KUrl& newUrl) +KUrl::List DolphinView::simplifiedSelectedUrls() const { - if (oldUrl == m_controller->url()) { - m_controller->setUrl(newUrl); + KUrl::List list = selectedUrls(); + if (itemsExpandable() ) { + list = KonqOperations::simplifiedUrlList(list); } + return list; +} + +void DolphinView::deleteExpandedViews() +{ + const QAbstractItemView* view = itemView(); + foreach (QAbstractItemView* expandedView, m_expandedViews) { + if (expandedView != view) { + expandedView->deleteLater(); + } + } + m_expandedViews.clear(); +} + +bool DolphinView::itemsExpandable() const +{ + return (m_detailsView != 0) && m_detailsView->itemsExpandable(); } #include "dolphinview.moc"