X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/blobdiff_plain/7b6ace6466cf349a4ce7a080d5e978aa0a7043e2..232dcda76c486a8d4f0b021bd32b8eef01880595:/src/dolphinview.cpp diff --git a/src/dolphinview.cpp b/src/dolphinview.cpp index 638e6e7cb..e59f9b282 100644 --- a/src/dolphinview.cpp +++ b/src/dolphinview.cpp @@ -52,20 +52,23 @@ #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" +#include + /** * Helper function for sorting items with qSort() in * DolphinView::renameSelectedItems(). @@ -104,6 +107,9 @@ DolphinView::DolphinView(QWidget* parent, m_toolTipManager(0), m_rootUrl(), m_currentItemUrl(), + m_createdItemUrl(), + m_selectedItems(), + m_newFileNames(), m_expandedDragSource(0) { m_topLayout = new QVBoxLayout(this); @@ -126,6 +132,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,10 +150,16 @@ DolphinView::DolphinView(QWidget* parent, connect(m_dirLister, SIGNAL(redirection(KUrl, KUrl)), this, SIGNAL(redirection(KUrl, KUrl))); connect(m_dirLister, SIGNAL(completed()), - this, SLOT(restoreCurrentItem())); + this, SLOT(slotDirListerCompleted())); 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()); } @@ -176,10 +190,6 @@ void DolphinView::setActive(bool active) QColor color = KColorScheme(QPalette::Active, KColorScheme::View).background().color(); if (active) { - // TODO: emitting urlChanged() is a hack, as the URL hasn't really changed. It - // bypasses the problem when having a split view and changing the active view to - // update the some URL dependent states. A nicer approach should be no big deal... - emit urlChanged(url()); emit selectionChanged(selectedItems()); } else { color.setAlpha(150); @@ -400,7 +410,7 @@ void DolphinView::setZoomLevel(int level) if (level != zoomLevel()) { m_controller->setZoomLevel(level); - m_previewGenerator->updatePreviews(); + m_previewGenerator->updateIcons(); emit zoomLevelChanged(level); } } @@ -434,6 +444,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(); @@ -587,7 +609,9 @@ QString DolphinView::statusBarText() const void DolphinView::setUrl(const KUrl& url) { - // remember current item candidate (see restoreCurrentItem()) + m_newFileNames.clear(); + + // remember current item candidate (see slotDirListerCompleted()) m_currentItemUrl = url; updateView(url, KUrl()); } @@ -600,15 +624,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); } @@ -809,6 +833,11 @@ void DolphinView::toggleSortOrder() setSortOrder(order); } +void DolphinView::toggleSortFoldersFirst() +{ + setSortFoldersFirst(!sortFoldersFirst()); +} + void DolphinView::toggleAdditionalInfo(QAction* action) { const KFileItemDelegate::Information info = @@ -955,6 +984,7 @@ void DolphinView::dropUrls(const KFileItem& destItem, const KUrl& destPath, QDropEvent* event) { + addNewFileNames(event->mimeData()); DragAndDropHelper::instance().dropUrls(destItem, destPath, event, this); } @@ -978,6 +1008,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()); @@ -1043,30 +1083,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) @@ -1104,6 +1121,32 @@ void DolphinView::deleteWhenNotDragSource(QAbstractItemView *view) } } +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: @@ -1142,17 +1185,38 @@ void DolphinView::slotRequestUrlChange(const KUrl& url) m_controller->setUrl(url); } -void DolphinView::restoreCurrentItem() +void DolphinView::slotDirListerCompleted() { - 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()) { + // assure that the current item remains visible + 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(); + } + + if (!m_newFileNames.isEmpty()) { + // select all newly added items created by a paste operation or + // a drag & drop operation + QItemSelectionModel* selectionModel = itemView()->selectionModel(); + const int rowCount = m_proxyModel->rowCount(); + for (int row = 0; row < rowCount; ++row) { + const QModelIndex proxyIndex = m_proxyModel->index(row, 0); + const QModelIndex dirIndex = m_proxyModel->mapToSource(proxyIndex); + const KUrl url = m_dolphinModel->itemForIndex(dirIndex).url(); + if (m_newFileNames.contains(url.fileName())) { + selectionModel->select(proxyIndex, QItemSelectionModel::Select); + } + } + + m_newFileNames.clear(); } } @@ -1160,10 +1224,7 @@ void DolphinView::slotRefreshItems() { if (m_assureVisibleCurrentIndex) { m_assureVisibleCurrentIndex = false; - // Invoking itemView()->scrollTo(itemView()->currentIndex()) is - // not sufficient, as QListView and QTreeView have an inconsistent - // default behavior. - m_controller->triggerScrollToCurrentItem(); + itemView()->scrollTo(itemView()->currentIndex()); } } @@ -1181,6 +1242,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); @@ -1260,6 +1326,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); @@ -1422,36 +1494,10 @@ QAbstractItemView* DolphinView::itemView() const return m_iconsView; } -bool DolphinView::isCutItem(const KFileItem& item) const -{ - const QMimeData* mimeData = QApplication::clipboard()->mimeData(); - const KUrl::List cutUrls = KUrl::List::fromMimeData(mimeData); - - const KUrl& itemUrl = item.url(); - KUrl::List::const_iterator it = cutUrls.begin(); - const KUrl::List::const_iterator end = cutUrls.end(); - while (it != end) { - if (*it == itemUrl) { - return true; - } - ++it; - } - - return false; -} - 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); - } + addNewFileNames(QApplication::clipboard()->mimeData()); + KonqOperations::doPaste(this, url); } void DolphinView::updateZoomLevel(int oldZoomLevel) @@ -1484,4 +1530,12 @@ QMimeData* DolphinView::selectionMimeData() const return m_dolphinModel->mimeData(selection.indexes()); } +void DolphinView::addNewFileNames(const QMimeData* mimeData) +{ + const KUrl::List urls = KUrl::List::fromMimeData(mimeData); + foreach (const KUrl& url, urls) { + m_newFileNames.insert(url.fileName()); + } +} + #include "dolphinview.moc"