From: David Faure Date: Thu, 15 Nov 2007 20:04:05 +0000 (+0000) Subject: Implement cut/copy/paste in dolphinpart. X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/commitdiff_plain/f8302197daca5667aa3d4963528ce09e344eb27f Implement cut/copy/paste in dolphinpart. Had to move more code to DolphinView, to use it from the part. svn path=/trunk/KDE/kdebase/apps/; revision=737203 --- diff --git a/src/dolphinmainwindow.cpp b/src/dolphinmainwindow.cpp index 77cb13c02..c38cc5ea3 100644 --- a/src/dolphinmainwindow.cpp +++ b/src/dolphinmainwindow.cpp @@ -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 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&)), diff --git a/src/dolphinmainwindow.h b/src/dolphinmainwindow.h index 29ec37744..1471b44e9 100644 --- a/src/dolphinmainwindow.h +++ b/src/dolphinmainwindow.h @@ -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); diff --git a/src/dolphinpart.cpp b/src/dolphinpart.cpp index f278edddf..a0871ce06 100644 --- a/src/dolphinpart.cpp +++ b/src/dolphinpart.cpp @@ -18,29 +18,23 @@ */ #include "dolphinpart.h" -#include -#include -#include #include "dolphinsortfilterproxymodel.h" #include "dolphinview.h" #include "dolphinmodel.h" +#include #include #include -#include #include +#include + +#include #include +#include typedef KParts::GenericFactory 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 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" diff --git a/src/dolphinpart.h b/src/dolphinpart.h index bf81b55f4..114c21d31 100644 --- a/src/dolphinpart.h +++ b/src/dolphinpart.h @@ -21,6 +21,7 @@ #define DOLPHINPART_H #include +#include 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 */ diff --git a/src/dolphinpart.rc b/src/dolphinpart.rc index e5a9ff63d..f1d4652e5 100644 --- a/src/dolphinpart.rc +++ b/src/dolphinpart.rc @@ -1,8 +1,7 @@ - - + @@ -51,8 +50,6 @@ - - @@ -60,8 +57,6 @@ - - diff --git a/src/dolphinview.cpp b/src/dolphinview.cpp index 590a813a8..30b07ce42 100644 --- a/src/dolphinview.cpp +++ b/src/dolphinview.cpp @@ -29,6 +29,7 @@ #include #include #include +#include #include #include @@ -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 DolphinView::pasteInfo() const +{ + QPair 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" diff --git a/src/dolphinview.h b/src/dolphinview.h index ff72bbaa7..c20ea10d2 100644 --- a/src/dolphinview.h +++ b/src/dolphinview.h @@ -29,6 +29,7 @@ #include #include #include +#include #include #include @@ -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 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 */