]>
cloud.milkyroute.net Git - dolphin.git/blob - 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> *
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. *
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. *
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 ***************************************************************************/
21 #include "treeviewcontextmenu.h"
24 #include <KIconLoader>
25 #include <KIO/DeleteJob>
28 #include <KSharedConfig>
29 #include <KConfigGroup>
30 #include <konqmimedata.h>
31 #include <KFileItemListProperties>
32 #include <konq_operations.h>
34 #include <KPropertiesDialog>
36 #include "folderspanel.h"
38 #include <QApplication>
42 TreeViewContextMenu::TreeViewContextMenu(FoldersPanel
* parent
,
43 const KFileItem
& fileInfo
) :
50 TreeViewContextMenu::~TreeViewContextMenu()
54 void TreeViewContextMenu::open()
56 KMenu
* popup
= new KMenu(m_parent
);
58 if (!m_fileItem
.isNull()) {
59 KFileItemListProperties
capabilities(KFileItemList() << m_fileItem
);
61 // insert 'Cut', 'Copy' and 'Paste'
62 QAction
* cutAction
= new QAction(KIcon("edit-cut"), i18nc("@action:inmenu", "Cut"), this);
63 cutAction
->setEnabled(capabilities
.supportsMoving());
64 connect(cutAction
, &QAction::triggered
, this, &TreeViewContextMenu::cut
);
66 QAction
* copyAction
= new QAction(KIcon("edit-copy"), i18nc("@action:inmenu", "Copy"), this);
67 connect(copyAction
, &QAction::triggered
, this, &TreeViewContextMenu::copy
);
69 const QPair
<bool, QString
> pasteInfo
= KonqOperations::pasteInfo(m_fileItem
.url());
70 QAction
* pasteAction
= new QAction(KIcon("edit-paste"), pasteInfo
.second
, this);
71 connect(pasteAction
, &QAction::triggered
, this, &TreeViewContextMenu::paste
);
72 pasteAction
->setEnabled(pasteInfo
.first
);
74 popup
->addAction(cutAction
);
75 popup
->addAction(copyAction
);
76 popup
->addAction(pasteAction
);
77 popup
->addSeparator();
80 QAction
* renameAction
= new QAction(i18nc("@action:inmenu", "Rename..."), this);
81 renameAction
->setEnabled(capabilities
.supportsMoving());
82 renameAction
->setIcon(KIcon("edit-rename"));
83 connect(renameAction
, &QAction::triggered
, this, &TreeViewContextMenu::rename
);
84 popup
->addAction(renameAction
);
86 // insert 'Move to Trash' and (optionally) 'Delete'
87 KSharedConfig::Ptr globalConfig
= KSharedConfig::openConfig("kdeglobals", KConfig::IncludeGlobals
);
88 KConfigGroup
configGroup(globalConfig
, "KDE");
89 bool showDeleteCommand
= configGroup
.readEntry("ShowDeleteCommand", false);
91 const KUrl url
= m_fileItem
.url();
92 if (url
.isLocalFile()) {
93 QAction
* moveToTrashAction
= new QAction(KIcon("user-trash"),
94 i18nc("@action:inmenu", "Move to Trash"), this);
95 const bool enableMoveToTrash
= capabilities
.isLocal() && capabilities
.supportsMoving();
96 moveToTrashAction
->setEnabled(enableMoveToTrash
);
97 connect(moveToTrashAction
, &QAction::triggered
, this, &TreeViewContextMenu::moveToTrash
);
98 popup
->addAction(moveToTrashAction
);
100 showDeleteCommand
= true;
103 if (showDeleteCommand
) {
104 QAction
* deleteAction
= new QAction(KIcon("edit-delete"), i18nc("@action:inmenu", "Delete"), this);
105 deleteAction
->setEnabled(capabilities
.supportsDeleting());
106 connect(deleteAction
, &QAction::triggered
, this, &TreeViewContextMenu::deleteItem
);
107 popup
->addAction(deleteAction
);
110 popup
->addSeparator();
113 // insert 'Show Hidden Files'
114 QAction
* showHiddenFilesAction
= new QAction(i18nc("@action:inmenu", "Show Hidden Files"), this);
115 showHiddenFilesAction
->setCheckable(true);
116 showHiddenFilesAction
->setChecked(m_parent
->showHiddenFiles());
117 popup
->addAction(showHiddenFilesAction
);
118 connect(showHiddenFilesAction
, &QAction::toggled
, this, &TreeViewContextMenu::setShowHiddenFiles
);
120 // insert 'Automatic Scrolling'
121 QAction
* autoScrollingAction
= new QAction(i18nc("@action:inmenu", "Automatic Scrolling"), this);
122 autoScrollingAction
->setCheckable(true);
123 autoScrollingAction
->setChecked(m_parent
->autoScrolling());
124 // TODO: Temporary disabled. Horizontal autoscrolling will be implemented later either
125 // in KItemViews or manually as part of the FoldersPanel
126 //popup->addAction(autoScrollingAction);
127 connect(autoScrollingAction
, &QAction::toggled
, this, &TreeViewContextMenu::setAutoScrolling
);
129 if (!m_fileItem
.isNull()) {
130 // insert 'Properties' entry
131 QAction
* propertiesAction
= new QAction(i18nc("@action:inmenu", "Properties"), this);
132 propertiesAction
->setIcon(KIcon("document-properties"));
133 connect(propertiesAction
, &QAction::triggered
, this, &TreeViewContextMenu::showProperties
);
134 popup
->addAction(propertiesAction
);
137 QList
<QAction
*> customActions
= m_parent
->customContextMenuActions();
138 if (!customActions
.isEmpty()) {
139 popup
->addSeparator();
140 foreach (QAction
* action
, customActions
) {
141 popup
->addAction(action
);
145 QWeakPointer
<KMenu
> popupPtr
= popup
;
146 popup
->exec(QCursor::pos());
147 if (popupPtr
.data()) {
148 popupPtr
.data()->deleteLater();
152 void TreeViewContextMenu::populateMimeData(QMimeData
* mimeData
, bool cut
)
155 kdeUrls
.append(m_fileItem
.url());
156 KUrl::List mostLocalUrls
;
158 mostLocalUrls
.append(m_fileItem
.mostLocalUrl(dummy
));
159 KonqMimeData::populateMimeData(mimeData
, kdeUrls
, mostLocalUrls
, cut
);
162 void TreeViewContextMenu::cut()
164 QMimeData
* mimeData
= new QMimeData();
165 populateMimeData(mimeData
, true);
166 QApplication::clipboard()->setMimeData(mimeData
);
169 void TreeViewContextMenu::copy()
171 QMimeData
* mimeData
= new QMimeData();
172 populateMimeData(mimeData
, false);
173 QApplication::clipboard()->setMimeData(mimeData
);
176 void TreeViewContextMenu::paste()
178 KonqOperations::doPaste(m_parent
, m_fileItem
.url());
181 void TreeViewContextMenu::rename()
183 m_parent
->rename(m_fileItem
);
186 void TreeViewContextMenu::moveToTrash()
188 KonqOperations::del(m_parent
, KonqOperations::TRASH
, KUrl::List() << m_fileItem
.url());
191 void TreeViewContextMenu::deleteItem()
193 KonqOperations::del(m_parent
, KonqOperations::DEL
, KUrl::List() << m_fileItem
.url());
196 void TreeViewContextMenu::showProperties()
198 KPropertiesDialog
* dialog
= new KPropertiesDialog(m_fileItem
.url(), m_parent
);
199 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
203 void TreeViewContextMenu::setShowHiddenFiles(bool show
)
205 m_parent
->setShowHiddenFiles(show
);
208 void TreeViewContextMenu::setAutoScrolling(bool enable
)
210 m_parent
->setAutoScrolling(enable
);