X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/blobdiff_plain/2f47863c68bb0ac0417eed76f713ced651a4908a..ed0df8dc1fc9576e36c920882e7f4b00a2811113:/src/dolphinview.cpp diff --git a/src/dolphinview.cpp b/src/dolphinview.cpp index 4fc3248c8..f2f23d273 100644 --- a/src/dolphinview.cpp +++ b/src/dolphinview.cpp @@ -52,17 +52,18 @@ #include "dolphinmodel.h" #include "dolphincolumnview.h" #include "dolphincontroller.h" +#include "dolphindetailsview.h" #include "dolphinfileitemdelegate.h" +#include "dolphinnewmenuobserver.h" #include "dolphinsortfilterproxymodel.h" -#include "dolphindetailsview.h" #include "dolphin_detailsmodesettings.h" #include "dolphiniconsview.h" -#include "settings/dolphinsettings.h" #include "dolphin_generalsettings.h" #include "draganddrophelper.h" #include "folderexpander.h" #include "renamedialog.h" #include "tooltips/tooltipmanager.h" +#include "settings/dolphinsettings.h" #include "viewproperties.h" #include "zoomlevelinfo.h" @@ -88,6 +89,7 @@ DolphinView::DolphinView(QWidget* parent, m_tabsForFiles(false), m_isContextMenuOpen(false), m_ignoreViewProperties(false), + m_assureVisibleCurrentIndex(false), m_mode(DolphinView::IconsView), m_topLayout(0), m_controller(0), @@ -103,6 +105,8 @@ DolphinView::DolphinView(QWidget* parent, m_toolTipManager(0), m_rootUrl(), m_currentItemUrl(), + m_createdItemUrl(), + m_selectedItems(), m_expandedDragSource(0) { m_topLayout = new QVBoxLayout(this); @@ -125,6 +129,8 @@ DolphinView::DolphinView(QWidget* parent, this, SLOT(updateSorting(DolphinView::Sorting))); connect(m_controller, SIGNAL(sortOrderChanged(Qt::SortOrder)), this, SLOT(updateSortOrder(Qt::SortOrder))); + connect(m_controller, SIGNAL(sortFoldersFirstChanged(bool)), + this, SLOT(updateSortFoldersFirst(bool))); connect(m_controller, SIGNAL(additionalInfoChanged(const KFileItemDelegate::InformationList&)), this, SLOT(updateAdditionalInfo(const KFileItemDelegate::InformationList&))); connect(m_controller, SIGNAL(itemTriggered(const KFileItem&)), @@ -142,6 +148,14 @@ DolphinView::DolphinView(QWidget* parent, this, SIGNAL(redirection(KUrl, KUrl))); connect(m_dirLister, SIGNAL(completed()), this, SLOT(restoreCurrentItem())); + connect(m_dirLister, SIGNAL(refreshItems(const QList>&)), + this, SLOT(slotRefreshItems())); + + // When a new item has been created by the "Create New..." menu, the item should + // get selected and it must be assured that the item will get visible. As the + // creation is done asynchronously, several signals must be checked: + connect(&DolphinNewMenuObserver::instance(), SIGNAL(itemCreated(const KUrl&)), + this, SLOT(observeCreatedItem(const KUrl&))); applyViewProperties(url); m_topLayout->addWidget(itemView()); @@ -149,7 +163,6 @@ DolphinView::DolphinView(QWidget* parent, DolphinView::~DolphinView() { - kDebug() << "Deleted view " << m_expandedDragSource; delete m_expandedDragSource; m_expandedDragSource = 0; } @@ -315,7 +328,10 @@ bool DolphinView::hasSelection() const void DolphinView::clearSelection() { - itemView()->selectionModel()->clear(); + QItemSelectionModel* selModel = itemView()->selectionModel(); + const QModelIndex currentIndex = selModel->currentIndex(); + selModel->setCurrentIndex(currentIndex, QItemSelectionModel::Current | + QItemSelectionModel::Clear); } KFileItemList DolphinView::selectedItems() const @@ -395,7 +411,7 @@ void DolphinView::setZoomLevel(int level) if (level != zoomLevel()) { m_controller->setZoomLevel(level); - m_previewGenerator->updatePreviews(); + m_previewGenerator->updateIcons(); emit zoomLevelChanged(level); } } @@ -429,6 +445,18 @@ Qt::SortOrder DolphinView::sortOrder() const return m_proxyModel->sortOrder(); } +void DolphinView::setSortFoldersFirst(bool foldersFirst) +{ + if (sortFoldersFirst() != foldersFirst) { + updateSortFoldersFirst(foldersFirst); + } +} + +bool DolphinView::sortFoldersFirst() const +{ + return m_proxyModel->sortFoldersFirst(); +} + void DolphinView::setAdditionalInfo(KFileItemDelegate::InformationList info) { const KUrl viewPropsUrl = viewPropertiesUrl(); @@ -595,15 +623,15 @@ void DolphinView::changeSelection(const KFileItemList& selection) } const KUrl& baseUrl = url(); KUrl url; - QItemSelection new_selection; + QItemSelection newSelection; foreach(const KFileItem& item, selection) { url = item.url().upUrl(); if (baseUrl.equals(url, KUrl::CompareWithoutTrailingSlash)) { QModelIndex index = m_proxyModel->mapFromSource(m_dolphinModel->indexForItem(item)); - new_selection.select(index, index); + newSelection.select(index, index); } } - itemView()->selectionModel()->select(new_selection, + itemView()->selectionModel()->select(newSelection, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Current); } @@ -627,30 +655,31 @@ void DolphinView::renameSelectedItems() const QString newName = dialog.newName(); if (newName.isEmpty()) { emit errorMessage(dialog.errorString()); - } else { - // TODO: check how this can be integrated into KIO::FileUndoManager/KonqOperations - // as one operation instead of n rename operations like it is done now... - Q_ASSERT(newName.contains('#')); - - // currently the items are sorted by the selection order, resort - // them by the file name - qSort(items.begin(), items.end(), lessThan); - - // iterate through all selected items and rename them... - int index = 1; - foreach (const KFileItem& item, items) { - const KUrl& oldUrl = item.url(); - QString number; - number.setNum(index++); - - QString name = newName; - name.replace('#', number); - - if (oldUrl.fileName() != name) { - KUrl newUrl = oldUrl; - newUrl.setFileName(name); - KonqOperations::rename(this, oldUrl, newUrl); - } + return; + } + + // TODO: check how this can be integrated into KIO::FileUndoManager/KonqOperations + // as one operation instead of n rename operations like it is done now... + Q_ASSERT(newName.contains('#')); + + // currently the items are sorted by the selection order, resort + // them by the file name + qSort(items.begin(), items.end(), lessThan); + + // iterate through all selected items and rename them... + int index = 1; + foreach (const KFileItem& item, items) { + const KUrl& oldUrl = item.url(); + QString number; + number.setNum(index++); + + QString name = newName; + name.replace('#', number); + + if (oldUrl.fileName() != name) { + KUrl newUrl = oldUrl; + newUrl.setFileName(name); + KonqOperations::rename(this, oldUrl, newUrl); } } } else if (DolphinSettings::instance().generalSettings()->renameInline()) { @@ -674,13 +703,18 @@ void DolphinView::renameSelectedItems() const QString& newName = dialog.newName(); if (newName.isEmpty()) { emit errorMessage(dialog.errorString()); - } else { - const KUrl& oldUrl = items.first().url(); - KUrl newUrl = oldUrl; - newUrl.setFileName(newName); - KonqOperations::rename(this, oldUrl, newUrl); + return; } + + const KUrl& oldUrl = items.first().url(); + KUrl newUrl = oldUrl; + newUrl.setFileName(newName); + KonqOperations::rename(this, oldUrl, newUrl); } + + // assure that the current index remains visible when KDirLister + // will notify the view about changed items + m_assureVisibleCurrentIndex = true; } void DolphinView::trashSelectedItems() @@ -798,6 +832,11 @@ void DolphinView::toggleSortOrder() setSortOrder(order); } +void DolphinView::toggleSortFoldersFirst() +{ + setSortFoldersFirst(!sortFoldersFirst()); +} + void DolphinView::toggleAdditionalInfo(QAction* action) { const KFileItemDelegate::Information info = @@ -849,14 +888,12 @@ bool DolphinView::eventFilter(QObject* watched, QEvent* event) break; case QEvent::MouseButtonPress: - kDebug() << "m_expandedDragSource = " << m_expandedDragSource; if ((watched == itemView()->viewport()) && (m_expandedDragSource != 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. - kDebug() << "Deleted view " << m_expandedDragSource; m_expandedDragSource->deleteLater(); m_expandedDragSource = 0; } @@ -869,8 +906,16 @@ bool DolphinView::eventFilter(QObject* watched, QEvent* event) break; case QEvent::KeyPress: - if ((watched == itemView()) && (m_toolTipManager != 0)) { - m_toolTipManager->hideTip(); + if (watched == itemView()) { + if (m_toolTipManager != 0) { + m_toolTipManager->hideTip(); + } + + // clear the selection when Escape has been pressed + QKeyEvent* keyEvent = static_cast(event); + if (keyEvent->key() == Qt::Key_Escape) { + clearSelection(); + } } break; @@ -961,6 +1006,16 @@ void DolphinView::updateSortOrder(Qt::SortOrder order) emit sortOrderChanged(order); } +void DolphinView::updateSortFoldersFirst(bool foldersFirst) +{ + ViewProperties props(viewPropertiesUrl()); + props.setSortFoldersFirst(foldersFirst); + + m_proxyModel->setSortFoldersFirst(foldersFirst); + + emit sortFoldersFirstChanged(foldersFirst); +} + void DolphinView::updateAdditionalInfo(const KFileItemDelegate::InformationList& info) { ViewProperties props(viewPropertiesUrl()); @@ -1026,30 +1081,7 @@ void DolphinView::updateAdditionalInfoActions(KActionCollection* collection) QPair DolphinView::pasteInfo() const { - QPair ret; - QClipboard* clipboard = QApplication::clipboard(); - const QMimeData* mimeData = clipboard->mimeData(); - - KUrl::List urls = KUrl::List::fromMimeData(mimeData); - if (!urls.isEmpty()) { - // disable the paste action if no writing is supported - KFileItem item(KFileItem::Unknown, KFileItem::Unknown, url()); - ret.first = KonqFileItemCapabilities(KFileItemList() << item).supportsWriting(); - - if (urls.count() == 1) { - const KFileItem item(KFileItem::Unknown, KFileItem::Unknown, urls.first(), true); - ret.second = item.isDir() ? i18nc("@action:inmenu", "Paste One Folder") : - i18nc("@action:inmenu", "Paste One File"); - - } else { - ret.second = i18ncp("@action:inmenu", "Paste One Item", "Paste %1 Items", urls.count()); - } - } else { - ret.first = false; - ret.second = i18nc("@action:inmenu", "Paste"); - } - - return ret; + return KonqOperations::pasteInfo(url()); } void DolphinView::setTabsForFilesEnabled(bool tabsForFiles) @@ -1073,11 +1105,9 @@ void DolphinView::deleteWhenNotDragSource(QAbstractItemView *view) return; if (DragAndDropHelper::instance().isDragSource(view)) { - kDebug() << "Is current drag source"; // We must store for later deletion. if (m_expandedDragSource != 0) { // The old stored view is obviously not the drag source anymore. - kDebug() << "Deleted old view " << m_expandedDragSource; m_expandedDragSource->deleteLater(); m_expandedDragSource = 0; } @@ -1085,11 +1115,36 @@ void DolphinView::deleteWhenNotDragSource(QAbstractItemView *view) m_expandedDragSource = view; } else { - kDebug() << "Deleted new view " << view; view->deleteLater(); } } +void DolphinView::observeCreatedItem(const KUrl& url) +{ + m_createdItemUrl = url; + connect(m_dolphinModel, SIGNAL(rowsInserted(const QModelIndex&, int, int)), + this, SLOT(selectAndScrollToCreatedItem())); +} + +void DolphinView::selectAndScrollToCreatedItem() +{ + const QModelIndex dirIndex = m_dolphinModel->indexForUrl(m_createdItemUrl); + if (dirIndex.isValid()) { + const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex); + itemView()->setCurrentIndex(proxyIndex); + } + + disconnect(m_dolphinModel, SIGNAL(rowsInserted(const QModelIndex&, int, int)), + this, SLOT(selectAndScrollToCreatedItem())); + m_createdItemUrl = KUrl(); +} + +void DolphinView::restoreSelection() +{ + disconnect(m_dirLister, SIGNAL(completed()), this, SLOT(restoreSelection())); + changeSelection(m_selectedItems); +} + void DolphinView::emitContentsMoved() { // only emit the contents moved signal if: @@ -1130,15 +1185,26 @@ void DolphinView::slotRequestUrlChange(const KUrl& url) void DolphinView::restoreCurrentItem() { - const QModelIndex dirIndex = m_dolphinModel->indexForUrl(m_currentItemUrl); - if (dirIndex.isValid()) { - const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex); - QAbstractItemView* view = itemView(); - const bool clearSelection = !hasSelection(); - view->setCurrentIndex(proxyIndex); - if (clearSelection) { - view->clearSelection(); + if (!m_currentItemUrl.isEmpty()) { + const QModelIndex dirIndex = m_dolphinModel->indexForUrl(m_currentItemUrl); + if (dirIndex.isValid()) { + const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex); + QAbstractItemView* view = itemView(); + const bool clearSelection = !hasSelection(); + view->setCurrentIndex(proxyIndex); + if (clearSelection) { + view->clearSelection(); + } } + m_currentItemUrl.clear(); + } +} + +void DolphinView::slotRefreshItems() +{ + if (m_assureVisibleCurrentIndex) { + m_assureVisibleCurrentIndex = false; + itemView()->scrollTo(itemView()->currentIndex()); } } @@ -1156,6 +1222,11 @@ void DolphinView::loadDirectory(const KUrl& url, bool reload) m_loadingDirectory = true; + if (reload) { + m_selectedItems = selectedItems(); + connect(m_dirLister, SIGNAL(completed()), this, SLOT(restoreSelection())); + } + m_dirLister->stop(); m_dirLister->openUrl(url, reload ? KDirLister::Reload : KDirLister::NoFlags); @@ -1235,6 +1306,12 @@ void DolphinView::applyViewProperties(const KUrl& url) emit sortOrderChanged(sortOrder); } + const bool sortFoldersFirst = props.sortFoldersFirst(); + if (sortFoldersFirst != m_proxyModel->sortFoldersFirst()) { + m_proxyModel->setSortFoldersFirst(sortFoldersFirst); + emit sortFoldersFirstChanged(sortFoldersFirst); + } + KFileItemDelegate::InformationList info = props.additionalInfo(); if (info != m_fileItemDelegate->showInformation()) { m_fileItemDelegate->setShowInformation(info); @@ -1417,16 +1494,7 @@ bool DolphinView::isCutItem(const KFileItem& item) const void DolphinView::pasteToUrl(const KUrl& url) { - QClipboard* clipboard = QApplication::clipboard(); - const QMimeData* mimeData = clipboard->mimeData(); - - const KUrl::List sourceUrls = KUrl::List::fromMimeData(mimeData); - if (KonqMimeData::decodeIsCutSelection(mimeData)) { - KonqOperations::copy(this, KonqOperations::MOVE, sourceUrls, url); - clipboard->clear(); - } else { - KonqOperations::copy(this, KonqOperations::COPY, sourceUrls, url); - } + KonqOperations::doPaste(this, url); } void DolphinView::updateZoomLevel(int oldZoomLevel) @@ -1459,4 +1527,5 @@ QMimeData* DolphinView::selectionMimeData() const return m_dolphinModel->mimeData(selection.indexes()); } + #include "dolphinview.moc"