]> cloud.milkyroute.net Git - dolphin.git/commitdiff
Implement cut/copy/paste in dolphinpart.
authorDavid Faure <faure@kde.org>
Thu, 15 Nov 2007 20:04:05 +0000 (20:04 +0000)
committerDavid Faure <faure@kde.org>
Thu, 15 Nov 2007 20:04:05 +0000 (20:04 +0000)
Had to move more code to DolphinView, to use it from the part.

svn path=/trunk/KDE/kdebase/apps/; revision=737203

src/dolphinmainwindow.cpp
src/dolphinmainwindow.h
src/dolphinpart.cpp
src/dolphinpart.h
src/dolphinpart.rc
src/dolphinview.cpp
src/dolphinview.h

index 77cb13c0233f8197e12b902db75826762d30da7a..c38cc5ea3b8f58b964dc7a444d0423d572687305 100644 (file)
@@ -122,10 +122,10 @@ void DolphinMainWindow::toggleViews()
     m_viewContainer[SecondaryView] = container;
 }
 
-void DolphinMainWindow::slotRenaming()
+void DolphinMainWindow::slotDoingOperation(KonqFileUndoManager::CommandType commandType)
 {
     clearStatusBar();
-    m_undoCommandTypes.append(KonqFileUndoManager::RENAME);
+    m_undoCommandTypes.append(commandType);
 }
 
 void DolphinMainWindow::refreshViews()
@@ -611,55 +611,17 @@ void DolphinMainWindow::undo()
 
 void DolphinMainWindow::cut()
 {
-    QMimeData* mimeData = new QMimeData();
-    const KUrl::List kdeUrls = m_activeViewContainer->view()->selectedUrls();
-    const KUrl::List mostLocalUrls;
-    KonqMimeData::populateMimeData(mimeData, kdeUrls, mostLocalUrls, true);
-    QApplication::clipboard()->setMimeData(mimeData);
+    m_activeViewContainer->view()->cutSelectedItems();
 }
 
 void DolphinMainWindow::copy()
 {
-    QMimeData* mimeData = new QMimeData();
-    const KUrl::List kdeUrls = m_activeViewContainer->view()->selectedUrls();
-    const KUrl::List mostLocalUrls;
-    KonqMimeData::populateMimeData(mimeData, kdeUrls, mostLocalUrls, false);
-
-    QApplication::clipboard()->setMimeData(mimeData);
+    m_activeViewContainer->view()->copySelectedItems();
 }
 
 void DolphinMainWindow::paste()
 {
-    QClipboard* clipboard = QApplication::clipboard();
-    const QMimeData* mimeData = clipboard->mimeData();
-
-    clearStatusBar();
-
-    const KUrl::List sourceUrls = KUrl::List::fromMimeData(mimeData);
-
-    // per default the pasting is done into the current Url of the view
-    KUrl destUrl(m_activeViewContainer->url());
-
-    // check whether the pasting should be done into a selected directory
-    KUrl::List selectedUrls = m_activeViewContainer->view()->selectedUrls();
-    if (selectedUrls.count() == 1) {
-        const KFileItem fileItem(S_IFDIR,
-                                 KFileItem::Unknown,
-                                 selectedUrls.first(),
-                                 true);
-        if (fileItem.isDir()) {
-            // only one item is selected which is a directory, hence paste
-            // into this directory
-            destUrl = selectedUrls.first();
-        }
-    }
-
-    if (KonqMimeData::decodeIsCutSelection(mimeData)) {
-        moveUrls(sourceUrls, destUrl);
-        clipboard->clear();
-    } else {
-        copyUrls(sourceUrls, destUrl);
-    }
+    m_activeViewContainer->view()->paste();
 }
 
 void DolphinMainWindow::updatePasteAction()
@@ -669,39 +631,9 @@ void DolphinMainWindow::updatePasteAction()
         return;
     }
 
-    QString text(i18nc("@action:inmenu", "Paste"));
-    QClipboard* clipboard = QApplication::clipboard();
-    const QMimeData* mimeData = clipboard->mimeData();
-
-    KUrl::List urls = KUrl::List::fromMimeData(mimeData);
-    if (!urls.isEmpty()) {
-        pasteAction->setEnabled(true);
-
-        pasteAction->setText(i18ncp("@action:inmenu", "Paste One File", "Paste %1 Files", urls.count()));
-    } else {
-        pasteAction->setEnabled(false);
-        pasteAction->setText(i18nc("@action:inmenu", "Paste"));
-    }
-
-    if (pasteAction->isEnabled()) {
-        KUrl::List urls = m_activeViewContainer->view()->selectedUrls();
-        const uint count = urls.count();
-        if (count > 1) {
-            // pasting should not be allowed when more than one file
-            // is selected
-            pasteAction->setEnabled(false);
-        } else if (count == 1) {
-            // Only one file is selected. Pasting is only allowed if this
-            // file is a directory.
-            // TODO: this doesn't work with remote protocols; instead we need a
-            // m_activeViewContainer->selectedFileItems() to get the real KFileItems
-            const KFileItem fileItem(S_IFDIR,
-                                     KFileItem::Unknown,
-                                     urls.first(),
-                                     true);
-            pasteAction->setEnabled(fileItem.isDir());
-        }
-    }
+    QPair<bool, QString> pasteInfo = m_activeViewContainer->view()->pasteInfo();
+    pasteAction->setEnabled(pasteInfo.first);
+    pasteAction->setText(pasteInfo.second);
 }
 
 void DolphinMainWindow::selectAll()
@@ -1581,8 +1513,8 @@ void DolphinMainWindow::connectViewSignals(int viewIndex)
             this, SLOT(slotRequestItemInfo(KFileItem)));
     connect(view, SIGNAL(activated()),
             this, SLOT(toggleActiveView()));
-    connect(view, SIGNAL(renaming()),
-            this, SLOT(slotRenaming()));
+    connect(view, SIGNAL(doingOperation(KonqFileUndoManager::CommandType)),
+            this, SLOT(slotDoingOperation(KonqFileUndoManager::CommandType)));
 
     const KUrlNavigator* navigator = container->urlNavigator();
     connect(navigator, SIGNAL(urlChanged(const KUrl&)),
index 29ec377445c1248241363da4e3e5a21bcd1e02b4..1471b44e900e3da2c3e8d1c5c809d15bd0f84f41 100644 (file)
@@ -422,8 +422,8 @@ private slots:
     /** Toggles the active view if two views are shown within the main window. */
     void toggleActiveView();
 
-    /** Called when the view is renaming a file. */
-    void slotRenaming();
+    /** Called when the view is doing a file operation, like renaming, copying, moving etc. */
+    void slotDoingOperation(KonqFileUndoManager::CommandType type);
 
 private:
     DolphinMainWindow(int id);
index f278edddf4c3380912e9bfc02f564ac6e829ccb9..a0871ce066c3dc2d740591f995ae398982b0f16e 100644 (file)
 */
 
 #include "dolphinpart.h"
-#include <kactioncollection.h>
-#include <ktoggleaction.h>
-#include <QActionGroup>
 #include "dolphinsortfilterproxymodel.h"
 #include "dolphinview.h"
 #include "dolphinmodel.h"
 
+#include <kactioncollection.h>
 #include <kdirlister.h>
 #include <kmessagebox.h>
-#include <kparts/browserextension.h>
 #include <kparts/genericfactory.h>
+#include <ktoggleaction.h>
+
+#include <QActionGroup>
 #include <QApplication>
+#include <QClipboard>
 
 typedef KParts::GenericFactory<DolphinPart> DolphinPartFactory;
 K_EXPORT_COMPONENT_FACTORY(dolphinpart, DolphinPartFactory)
 
-class DolphinPartBrowserExtension : public KParts::BrowserExtension
-{
-public:
-    DolphinPartBrowserExtension( KParts::ReadOnlyPart* part )
-        : KParts::BrowserExtension( part ) {}
-};
-
 DolphinPart::DolphinPart(QWidget* parentWidget, QObject* parent, const QStringList& args)
     : KParts::ReadOnlyPart(parent)
 {
@@ -91,6 +85,10 @@ DolphinPart::DolphinPart(QWidget* parentWidget, QObject* parent, const QStringLi
     connect(m_view, SIGNAL(modeChanged()),
             this, SLOT(updateViewActions()));
 
+    QClipboard* clipboard = QApplication::clipboard();
+    connect(clipboard, SIGNAL(dataChanged()),
+            this, SLOT(updatePasteAction()));
+
     createActions();
     updateViewActions();
 
@@ -107,10 +105,6 @@ DolphinPart::DolphinPart(QWidget* parentWidget, QObject* parent, const QStringLi
 
     // TODO MMB-click should do something like KonqDirPart::mmbClicked
 
-    // TODO updating the paste action
-    // if (paste) emit m_extension->setActionText( "paste", actionText );
-    // emit m_extension->enableAction( "paste", paste );
-
     // TODO updating the trash and del actions too - or removing special handling of those from konq?
 }
 
@@ -137,7 +131,8 @@ void DolphinPart::createActions()
 void DolphinPart::slotSelectionChanged(const KFileItemList& selection)
 {
     // Yes, DolphinMainWindow has very similar code :/
-    if (selection.isEmpty()) {
+    const bool hasSelection = !selection.isEmpty();
+    if (!hasSelection) {
         stateChanged("has_no_selection");
     } else {
         stateChanged("has_selection");
@@ -148,6 +143,15 @@ void DolphinPart::slotSelectionChanged(const KFileItemList& selection)
             renameAction->setEnabled(true);
         }
     }
+    emit m_extension->enableAction("cut", hasSelection);
+    emit m_extension->enableAction("copy", hasSelection);
+}
+
+void DolphinPart::updatePasteAction()
+{
+    QPair<bool, QString> pasteInfo = m_view->pasteInfo();
+    emit m_extension->enableAction( "paste", pasteInfo.first );
+    emit m_extension->setActionText( "paste", pasteInfo.second );
 }
 
 void DolphinPart::updateViewActions()
@@ -274,4 +278,21 @@ void DolphinPart::slotUrlChanged(const KUrl& url)
     }
 }
 
+////
+
+void DolphinPartBrowserExtension::cut()
+{
+    m_part->view()->cutSelectedItems();
+}
+
+void DolphinPartBrowserExtension::copy()
+{
+    m_part->view()->copySelectedItems();
+}
+
+void DolphinPartBrowserExtension::paste()
+{
+    m_part->view()->paste();
+}
+
 #include "dolphinpart.moc"
index bf81b55f48d4b8d71b2304fd67c71096f24ca25c..114c21d3178ffb3725f1218441109f99da29a96a 100644 (file)
@@ -21,6 +21,7 @@
 #define DOLPHINPART_H
 
 #include <kparts/part.h>
+#include <kparts/browserextension.h>
 class KFileItemList;
 class KFileItem;
 class DolphinPartBrowserExtension;
@@ -50,6 +51,8 @@ public:
     /// see the supportsUndo property
     bool supportsUndo() const { return true; }
 
+    DolphinView* view() { return m_view; }
+
 protected:
     /**
      * We reimplement openUrl so no need to implement openFile.
@@ -99,6 +102,12 @@ private Q_SLOTS:
      */
     void updateViewActions();
 
+    /**
+     * Updates the text of the paste action dependent from
+     * the number of items which are in the clipboard.
+     */
+    void updatePasteAction();
+
 private:
     void createActions();
 
@@ -111,4 +120,20 @@ private:
     Q_DISABLE_COPY(DolphinPart)
 };
 
+class DolphinPartBrowserExtension : public KParts::BrowserExtension
+{
+    Q_OBJECT
+public:
+    DolphinPartBrowserExtension( DolphinPart* part )
+        : KParts::BrowserExtension( part ), m_part(part) {}
+
+public Q_SLOTS:
+    void cut();
+    void copy();
+    void paste();
+
+private:
+    DolphinPart* m_part;
+};
+
 #endif /* DOLPHINPART_H */
index e5a9ff63dc398490b1a31c541b8b4e885d339f06..f1d4652e55897a7e8747184a13f215e8ee2aeaaf 100644 (file)
@@ -1,8 +1,7 @@
 <!DOCTYPE kpartgui SYSTEM "kpartgui.dtd">
 <kpartgui name="dolphinpart" version="4" >
  <MenuBar>
-   <Menu name="edit">
-   <!-- TODO cut, copy, paste -->
+  <Menu name="edit">
    <Action name="rename"/>
    <!-- TODO trash, del -->
    <Action name="select_all" />
@@ -51,8 +50,6 @@
 </ToolBar>
 <State name="has_selection" >
   <enable>
-   <Action name="edit_cut" />
-   <Action name="edit_copy" />
    <Action name="move_to_trash" />
    <Action name="delete" />
    <Action name="properties" />
@@ -60,8 +57,6 @@
  </State>
  <State name="has_no_selection" >
   <disable>
-   <Action name="edit_cut" />
-   <Action name="edit_copy" />
    <Action name="rename" />
    <Action name="move_to_trash" />
    <Action name="delete" />
index 590a813a87d0e3ff7d111e08ca66204cf52b4085..30b07ce4222422ae1e4d8c7c06db5ac0bd1c61d4 100644 (file)
@@ -29,6 +29,7 @@
 #include <QBoxLayout>
 #include <QTimer>
 #include <QScrollBar>
+#include <QClipboard>
 
 #include <kcolorscheme.h>
 #include <kdirlister.h>
@@ -1035,7 +1036,7 @@ void DolphinView::renameSelectedItems()
                     KUrl newUrl = oldUrl;
                     newUrl.setFileName(name);
                     KonqOperations::rename(this, oldUrl, newUrl);
-                    emit renaming();
+                    emit doingOperation(KonqFileUndoManager::RENAME);
                 }
                 ++it;
             }
@@ -1061,9 +1062,98 @@ void DolphinView::renameSelectedItems()
             KUrl newUrl = oldUrl;
             newUrl.setFileName(newName);
             KonqOperations::rename(this, oldUrl, newUrl);
-            emit renaming();
+            emit doingOperation(KonqFileUndoManager::RENAME);
+        }
+    }
+}
+
+void DolphinView::cutSelectedItems()
+{
+    QMimeData* mimeData = new QMimeData();
+    const KUrl::List kdeUrls = selectedUrls();
+    const KUrl::List mostLocalUrls;
+    KonqMimeData::populateMimeData(mimeData, kdeUrls, mostLocalUrls, true);
+    QApplication::clipboard()->setMimeData(mimeData);
+}
+
+void DolphinView::copySelectedItems()
+{
+    QMimeData* mimeData = new QMimeData();
+    const KUrl::List kdeUrls = selectedUrls();
+    const KUrl::List mostLocalUrls;
+    KonqMimeData::populateMimeData(mimeData, kdeUrls, mostLocalUrls, false);
+    QApplication::clipboard()->setMimeData(mimeData);
+}
+
+void DolphinView::paste()
+{
+    QClipboard* clipboard = QApplication::clipboard();
+    const QMimeData* mimeData = clipboard->mimeData();
+
+    const KUrl::List sourceUrls = KUrl::List::fromMimeData(mimeData);
+
+    // per default the pasting is done into the current Url of the view
+    KUrl destUrl(url());
+
+    // check whether the pasting should be done into a selected directory
+    const KUrl::List selectedUrls = this->selectedUrls();
+    if (selectedUrls.count() == 1) {
+        const KFileItem fileItem(S_IFDIR,
+                                 KFileItem::Unknown,
+                                 selectedUrls.first(),
+                                 true);
+        if (fileItem.isDir()) {
+            // only one item is selected which is a directory, hence paste
+            // into this directory
+            destUrl = selectedUrls.first();
+        }
+    }
+
+    if (KonqMimeData::decodeIsCutSelection(mimeData)) {
+        KonqOperations::copy(this, KonqOperations::MOVE, sourceUrls, destUrl);
+        emit doingOperation(KonqFileUndoManager::MOVE);
+        clipboard->clear();
+    } else {
+        KonqOperations::copy(this, KonqOperations::COPY, sourceUrls, destUrl);
+        emit doingOperation(KonqFileUndoManager::COPY);
+    }
+}
+
+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()) {
+        ret.first = true;
+        ret.second = i18ncp("@action:inmenu", "Paste One File", "Paste %1 Files", urls.count());
+    } else {
+        ret.first = false;
+        ret.second = i18nc("@action:inmenu", "Paste");
+    }
+
+    if (ret.first) {
+        const KUrl::List urls = selectedUrls();
+        const uint count = urls.count();
+        if (count > 1) {
+            // pasting should not be allowed when more than one file
+            // is selected
+            ret.first = false;
+        } else if (count == 1) {
+            // Only one file is selected. Pasting is only allowed if this
+            // file is a directory.
+            // TODO: this doesn't work with remote protocols; instead we need a
+            // m_activeViewContainer->selectedFileItems() to get the real KFileItems
+            const KFileItem fileItem(S_IFDIR,
+                                     KFileItem::Unknown,
+                                     urls.first(),
+                                     true);
+            ret.first = fileItem.isDir();
         }
     }
+    return ret;
 }
 
 #include "dolphinview.moc"
index ff72bbaa76d482f17760ed98c5de6297cf98c254..c20ea10d2ca30f30a33b9d69d3d98fa0838885ba 100644 (file)
@@ -29,6 +29,7 @@
 #include <kparts/part.h>
 #include <kfileitem.h>
 #include <kfileitemdelegate.h>
+#include <konq_fileundomanager.h>
 #include <kio/job.h>
 
 #include <QBoxLayout>
@@ -343,6 +344,13 @@ public:
      */
     QString currentViewModeActionName() const;
 
+    /**
+     * Returns the state of the paste action:
+     * first is whether the action should be enabled
+     * second is the text for the action
+     */
+    QPair<bool, QString> pasteInfo() const;
+
 public slots:
     /**
      * Changes the directory to \a url. If the current directory is equal to
@@ -364,6 +372,18 @@ public slots:
      */
     void renameSelectedItems();
 
+    /**
+     * Copies all selected items to the clipboard and marks
+     * the items as cutted.
+     */
+    void cutSelectedItems();
+
+    /** Copies all selected items to the clipboard. */
+    void copySelectedItems();
+
+    /** Pastes the clipboard data to this view. */
+    void paste();
+
 signals:
     /**
      * Is emitted if the view has been activated by e. g. a mouse click.
@@ -452,10 +472,10 @@ signals:
     void startedPathLoading(const KUrl& url);
 
     /**
-     * Is emitted when renaming one or more items.
+     * Is emitted when renaming, copying, moving, linking etc.
      * Used for feedback in the mainwindow.
      */
-    void renaming();
+    void doingOperation(KonqFileUndoManager::CommandType type);
 
 protected:
     /** @see QWidget::mouseReleaseEvent */