]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/folders/treeviewcontextmenu.cpp
port to QPushButton
[dolphin.git] / src / panels / folders / treeviewcontextmenu.cpp
1 /***************************************************************************
2 * Copyright (C) 2006-2010 by Peter Penz <peter.penz19@gmail.com> *
3 * Copyright (C) 2006 by Cvetoslav Ludmiloff <ludmiloff@gmail.com> *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 ***************************************************************************/
20
21 #include "treeviewcontextmenu.h"
22
23 #include <KFileItem>
24 #include <KIconLoader>
25 #include <KIO/CopyJob>
26 #include <KIO/DeleteJob>
27 #include <KIO/JobUiDelegate>
28 #include <QMenu>
29 #include <QIcon>
30 #include <KJobWidgets>
31 #include <KSharedConfig>
32 #include <KConfigGroup>
33 #include <kurlmimedata.h>
34 #include <KFileItemListProperties>
35 #include <konq_operations.h>
36 #include <KLocalizedString>
37 #include <KIO/Paste>
38 #include <KIO/FileUndoManager>
39 #include <KPropertiesDialog>
40
41 #include "folderspanel.h"
42
43 #include <QApplication>
44 #include <QClipboard>
45 #include <QMimeData>
46
47 TreeViewContextMenu::TreeViewContextMenu(FoldersPanel* parent,
48 const KFileItem& fileInfo) :
49 QObject(parent),
50 m_parent(parent),
51 m_fileItem(fileInfo)
52 {
53 }
54
55 TreeViewContextMenu::~TreeViewContextMenu()
56 {
57 }
58
59 void TreeViewContextMenu::open()
60 {
61 QMenu* popup = new QMenu(m_parent);
62
63 if (!m_fileItem.isNull()) {
64 KFileItemListProperties capabilities(KFileItemList() << m_fileItem);
65
66 // insert 'Cut', 'Copy' and 'Paste'
67 QAction* cutAction = new QAction(QIcon::fromTheme("edit-cut"), i18nc("@action:inmenu", "Cut"), this);
68 cutAction->setEnabled(capabilities.supportsMoving());
69 connect(cutAction, &QAction::triggered, this, &TreeViewContextMenu::cut);
70
71 QAction* copyAction = new QAction(QIcon::fromTheme("edit-copy"), i18nc("@action:inmenu", "Copy"), this);
72 connect(copyAction, &QAction::triggered, this, &TreeViewContextMenu::copy);
73
74 const QPair<bool, QString> pasteInfo = KonqOperations::pasteInfo(m_fileItem.url());
75 QAction* pasteAction = new QAction(QIcon::fromTheme("edit-paste"), pasteInfo.second, this);
76 connect(pasteAction, &QAction::triggered, this, &TreeViewContextMenu::paste);
77 pasteAction->setEnabled(pasteInfo.first);
78
79 popup->addAction(cutAction);
80 popup->addAction(copyAction);
81 popup->addAction(pasteAction);
82 popup->addSeparator();
83
84 // insert 'Rename'
85 QAction* renameAction = new QAction(i18nc("@action:inmenu", "Rename..."), this);
86 renameAction->setEnabled(capabilities.supportsMoving());
87 renameAction->setIcon(QIcon::fromTheme("edit-rename"));
88 connect(renameAction, &QAction::triggered, this, &TreeViewContextMenu::rename);
89 popup->addAction(renameAction);
90
91 // insert 'Move to Trash' and (optionally) 'Delete'
92 KSharedConfig::Ptr globalConfig = KSharedConfig::openConfig("kdeglobals", KConfig::IncludeGlobals);
93 KConfigGroup configGroup(globalConfig, "KDE");
94 bool showDeleteCommand = configGroup.readEntry("ShowDeleteCommand", false);
95
96 const KUrl url = m_fileItem.url();
97 if (url.isLocalFile()) {
98 QAction* moveToTrashAction = new QAction(QIcon::fromTheme("user-trash"),
99 i18nc("@action:inmenu", "Move to Trash"), this);
100 const bool enableMoveToTrash = capabilities.isLocal() && capabilities.supportsMoving();
101 moveToTrashAction->setEnabled(enableMoveToTrash);
102 connect(moveToTrashAction, &QAction::triggered, this, &TreeViewContextMenu::moveToTrash);
103 popup->addAction(moveToTrashAction);
104 } else {
105 showDeleteCommand = true;
106 }
107
108 if (showDeleteCommand) {
109 QAction* deleteAction = new QAction(QIcon::fromTheme("edit-delete"), i18nc("@action:inmenu", "Delete"), this);
110 deleteAction->setEnabled(capabilities.supportsDeleting());
111 connect(deleteAction, &QAction::triggered, this, &TreeViewContextMenu::deleteItem);
112 popup->addAction(deleteAction);
113 }
114
115 popup->addSeparator();
116 }
117
118 // insert 'Show Hidden Files'
119 QAction* showHiddenFilesAction = new QAction(i18nc("@action:inmenu", "Show Hidden Files"), this);
120 showHiddenFilesAction->setCheckable(true);
121 showHiddenFilesAction->setChecked(m_parent->showHiddenFiles());
122 popup->addAction(showHiddenFilesAction);
123 connect(showHiddenFilesAction, &QAction::toggled, this, &TreeViewContextMenu::setShowHiddenFiles);
124
125 // insert 'Automatic Scrolling'
126 QAction* autoScrollingAction = new QAction(i18nc("@action:inmenu", "Automatic Scrolling"), this);
127 autoScrollingAction->setCheckable(true);
128 autoScrollingAction->setChecked(m_parent->autoScrolling());
129 // TODO: Temporary disabled. Horizontal autoscrolling will be implemented later either
130 // in KItemViews or manually as part of the FoldersPanel
131 //popup->addAction(autoScrollingAction);
132 connect(autoScrollingAction, &QAction::toggled, this, &TreeViewContextMenu::setAutoScrolling);
133
134 if (!m_fileItem.isNull()) {
135 // insert 'Properties' entry
136 QAction* propertiesAction = new QAction(i18nc("@action:inmenu", "Properties"), this);
137 propertiesAction->setIcon(QIcon::fromTheme("document-properties"));
138 connect(propertiesAction, &QAction::triggered, this, &TreeViewContextMenu::showProperties);
139 popup->addAction(propertiesAction);
140 }
141
142 QList<QAction*> customActions = m_parent->customContextMenuActions();
143 if (!customActions.isEmpty()) {
144 popup->addSeparator();
145 foreach (QAction* action, customActions) {
146 popup->addAction(action);
147 }
148 }
149
150 QWeakPointer<QMenu> popupPtr = popup;
151 popup->exec(QCursor::pos());
152 if (popupPtr.data()) {
153 popupPtr.data()->deleteLater();
154 }
155 }
156
157 void TreeViewContextMenu::populateMimeData(QMimeData* mimeData, bool cut)
158 {
159 KUrl::List kdeUrls;
160 kdeUrls.append(m_fileItem.url());
161 KUrl::List mostLocalUrls;
162 bool dummy;
163 mostLocalUrls.append(m_fileItem.mostLocalUrl(dummy));
164 KIO::setClipboardDataCut(mimeData, cut);
165 KUrlMimeData::setUrls(kdeUrls, mostLocalUrls, mimeData);
166 }
167
168 void TreeViewContextMenu::cut()
169 {
170 QMimeData* mimeData = new QMimeData();
171 populateMimeData(mimeData, true);
172 QApplication::clipboard()->setMimeData(mimeData);
173 }
174
175 void TreeViewContextMenu::copy()
176 {
177 QMimeData* mimeData = new QMimeData();
178 populateMimeData(mimeData, false);
179 QApplication::clipboard()->setMimeData(mimeData);
180 }
181
182 void TreeViewContextMenu::paste()
183 {
184 KonqOperations::doPaste(m_parent, m_fileItem.url());
185 }
186
187 void TreeViewContextMenu::rename()
188 {
189 m_parent->rename(m_fileItem);
190 }
191
192 void TreeViewContextMenu::moveToTrash()
193 {
194 KUrl::List list = KUrl::List() << m_fileItem.url();
195 KIO::JobUiDelegate uiDelegate;
196 uiDelegate.setWindow(m_parent);
197 if (uiDelegate.askDeleteConfirmation(list, KIO::JobUiDelegate::Trash, KIO::JobUiDelegate::DefaultConfirmation)) {
198 KIO::Job* job = KIO::trash(list);
199 KIO::FileUndoManager::self()->recordJob(KIO::FileUndoManager::Trash, list, KUrl("trash:/"), job);
200 KJobWidgets::setWindow(job, m_parent);
201 job->ui()->setAutoErrorHandlingEnabled(true);
202 }
203 }
204
205 void TreeViewContextMenu::deleteItem()
206 {
207 KUrl::List list = KUrl::List() << m_fileItem.url();
208 KIO::JobUiDelegate uiDelegate;
209 uiDelegate.setWindow(m_parent);
210 if (uiDelegate.askDeleteConfirmation(list, KIO::JobUiDelegate::Delete, KIO::JobUiDelegate::DefaultConfirmation)) {
211 KIO::Job* job = KIO::del(list);
212 KJobWidgets::setWindow(job, m_parent);
213 job->ui()->setAutoErrorHandlingEnabled(true);
214 }
215 }
216
217 void TreeViewContextMenu::showProperties()
218 {
219 KPropertiesDialog* dialog = new KPropertiesDialog(m_fileItem.url(), m_parent);
220 dialog->setAttribute(Qt::WA_DeleteOnClose);
221 dialog->show();
222 }
223
224 void TreeViewContextMenu::setShowHiddenFiles(bool show)
225 {
226 m_parent->setShowHiddenFiles(show);
227 }
228
229 void TreeViewContextMenu::setAutoScrolling(bool enable)
230 {
231 m_parent->setAutoScrolling(enable);
232 }
233