]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/dolphinview.cpp
remember which service menus should be shown in the context menu
[dolphin.git] / src / dolphinview.cpp
index 0bd24ecb3a5a73f8f291efd5e9a2397864113984..8b914de4079819ab9cdcdf265526cd8c5153f3bd 100644 (file)
 #include "dolphindetailsview.h"
 #include "dolphin_detailsmodesettings.h"
 #include "dolphiniconsview.h"
-#include "dolphinsettings.h"
+#include "settings/dolphinsettings.h"
 #include "dolphin_generalsettings.h"
 #include "draganddrophelper.h"
 #include "folderexpander.h"
 #include "renamedialog.h"
-#include "tooltipmanager.h"
+#include "tooltips/tooltipmanager.h"
 #include "viewproperties.h"
 #include "zoomlevelinfo.h"
 
@@ -88,6 +88,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,7 +104,7 @@ DolphinView::DolphinView(QWidget* parent,
     m_toolTipManager(0),
     m_rootUrl(),
     m_currentItemUrl(),
-    m_expandedViews()
+    m_expandedDragSource(0)
 {
     m_topLayout = new QVBoxLayout(this);
     m_topLayout->setSpacing(0);
@@ -117,8 +118,8 @@ DolphinView::DolphinView(QWidget* parent,
     connect(m_controller, SIGNAL(requestUrlChange(const KUrl&)),
             this, SLOT(slotRequestUrlChange(const KUrl&)));
 
-    connect(m_controller, SIGNAL(requestContextMenu(const QPoint&)),
-            this, SLOT(openContextMenu(const QPoint&)));
+    connect(m_controller, SIGNAL(requestContextMenu(const QPoint&, const QList<QAction*>&)),
+            this, SLOT(openContextMenu(const QPoint&, const QList<QAction*>&)));
     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)),
@@ -142,6 +143,8 @@ DolphinView::DolphinView(QWidget* parent,
             this, SIGNAL(redirection(KUrl, KUrl)));
     connect(m_dirLister, SIGNAL(completed()),
             this, SLOT(restoreCurrentItem()));
+    connect(m_dirLister, SIGNAL(refreshItems(const QList<QPair<KFileItem,KFileItem>>&)),
+            this, SLOT(slotRefreshItems()));
 
     applyViewProperties(url);
     m_topLayout->addWidget(itemView());
@@ -149,7 +152,8 @@ DolphinView::DolphinView(QWidget* parent,
 
 DolphinView::~DolphinView()
 {
-    deleteExpandedViews();
+    delete m_expandedDragSource;
+    m_expandedDragSource = 0;
 }
 
 const KUrl& DolphinView::url() const
@@ -313,7 +317,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
@@ -625,30 +632,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()) {
@@ -672,13 +680,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()
@@ -847,13 +860,14 @@ bool DolphinView::eventFilter(QObject* watched, QEvent* event)
         break;
 
     case QEvent::MouseButtonPress:
-        if ((watched == itemView()->viewport()) && (m_expandedViews.count() > 0)) {
+        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.
-            deleteExpandedViews();
+            m_expandedDragSource->deleteLater();
+            m_expandedDragSource = 0;
         }
         break;
 
@@ -863,6 +877,20 @@ bool DolphinView::eventFilter(QObject* watched, QEvent* event)
         }
         break;
 
+    case QEvent::KeyPress:
+        if (watched == itemView()) {
+            if (m_toolTipManager != 0) {
+                m_toolTipManager->hideTip();
+            }
+
+            // clear the selection when Escape has been pressed
+            QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
+            if (keyEvent->key() == Qt::Key_Escape) {
+                clearSelection();
+            }
+        }
+        break;
+
     default:
         break;
     }
@@ -900,7 +928,8 @@ void DolphinView::emitSelectionChangedSignal()
     emit selectionChanged(DolphinView::selectedItems());
 }
 
-void DolphinView::openContextMenu(const QPoint& pos)
+void DolphinView::openContextMenu(const QPoint& pos,
+                                  const QList<QAction*>& customActions)
 {
     KFileItem item;
     if (isColumnViewActive()) {
@@ -918,7 +947,7 @@ void DolphinView::openContextMenu(const QPoint& pos)
     }
 
     m_isContextMenuOpen = true; // TODO: workaround for Qt-issue 207192
-    emit requestContextMenu(item, url());
+    emit requestContextMenu(item, url(), customActions);
     m_isContextMenuOpen = false;
 }
 
@@ -1014,30 +1043,7 @@ void DolphinView::updateAdditionalInfoActions(KActionCollection* collection)
 
 QPair<bool, QString> DolphinView::pasteInfo() const
 {
-    QPair<bool, QString> 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)
@@ -1055,6 +1061,26 @@ bool DolphinView::itemsExpandable() const
     return (m_detailsView != 0) && m_detailsView->itemsExpandable();
 }
 
+void DolphinView::deleteWhenNotDragSource(QAbstractItemView *view)
+{
+    if (view == 0)
+        return;
+
+    if (DragAndDropHelper::instance().isDragSource(view)) {
+        // We must store for later deletion.
+        if (m_expandedDragSource != 0) {
+            // The old stored view is obviously not the drag source anymore.
+            m_expandedDragSource->deleteLater();
+            m_expandedDragSource = 0;
+        }
+        view->hide();
+        m_expandedDragSource = view;
+    }
+    else {
+        view->deleteLater();
+    }
+}
+
 void DolphinView::emitContentsMoved()
 {
     // only emit the contents moved signal if:
@@ -1107,14 +1133,12 @@ void DolphinView::restoreCurrentItem()
     }
 }
 
-void DolphinView::enterDir(const QModelIndex& index, QAbstractItemView* view)
+void DolphinView::slotRefreshItems()
 {
-    // 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);
+    if (m_assureVisibleCurrentIndex) {
+        m_assureVisibleCurrentIndex = false;
+        itemView()->scrollTo(itemView()->currentIndex());
+    }
 }
 
 void DolphinView::loadDirectory(const KUrl& url, bool reload)
@@ -1279,8 +1303,13 @@ void DolphinView::createView()
 
         FolderExpander* folderExpander = new FolderExpander(view, m_proxyModel);
         folderExpander->setEnabled(enabled);
-        connect(folderExpander, SIGNAL(enterDir(const QModelIndex&, QAbstractItemView*)),
-                this, SLOT(enterDir(const QModelIndex&, QAbstractItemView*)));
+        connect(folderExpander, SIGNAL(enterDir(const QModelIndex&)),
+                m_controller, SLOT(triggerItem(const QModelIndex&)));
+    }
+    else {
+        // Listen out for requests to delete the current column.
+        connect(m_columnView, SIGNAL(requestColumnDeletion(QAbstractItemView*)),
+                this, SLOT(deleteWhenNotDragSource(QAbstractItemView*)));
     }
 
     m_controller->setItemView(view);
@@ -1336,29 +1365,22 @@ void DolphinView::deleteView()
         m_topLayout->removeWidget(view);
         view->close();
 
+        // m_previewGenerator's parent is not always destroyed, and we
+        // don't want two active at once - manually delete.
+        delete m_previewGenerator;
+        m_previewGenerator = 0;
+
         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();
-        }
+        deleteWhenNotDragSource(view);
         view = 0;
 
         m_iconsView = 0;
         m_detailsView = 0;
         m_columnView = 0;
         m_fileItemDelegate = 0;
-        m_previewGenerator = 0;
         m_toolTipManager = 0;
     }
 }
@@ -1394,16 +1416,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)
@@ -1424,17 +1437,6 @@ KUrl::List DolphinView::simplifiedSelectedUrls() const
     return list;
 }
 
-void DolphinView::deleteExpandedViews()
-{
-    const QAbstractItemView* view = itemView();
-    foreach (QAbstractItemView* expandedView, m_expandedViews) {
-        if (expandedView != view) {
-            expandedView->deleteLater();
-        }
-    }
-    m_expandedViews.clear();
-}
-
 QMimeData* DolphinView::selectionMimeData() const
 {
     if (isColumnViewActive()) {