]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincontroller.cpp
Restore the "Edit->Selection" menu from Konqueror 3 for file
[dolphin.git] / src / dolphincontroller.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz (peter.penz@gmx.at) *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
19
20 #include "dolphincontroller.h"
21 #include "zoomlevelinfo.h"
22
23 #include <kdirmodel.h>
24 #include <QAbstractProxyModel>
25 #include <QApplication>
26 #include <QClipboard>
27 #include <QDir>
28
29 Qt::MouseButtons DolphinController::m_mouseButtons = Qt::NoButton;
30
31 DolphinController::DolphinController(DolphinView* dolphinView) :
32 QObject(dolphinView),
33 m_zoomLevel(0),
34 m_nameFilter(),
35 m_url(),
36 m_dolphinView(dolphinView),
37 m_itemView(0),
38 m_versionControlActions()
39 {
40 }
41
42 DolphinController::~DolphinController()
43 {
44 }
45
46 void DolphinController::setUrl(const KUrl& url)
47 {
48 if (m_url != url) {
49 m_url = url;
50 emit cancelPreviews();
51 emit urlChanged(url);
52 }
53 }
54
55 void DolphinController::redirectToUrl(const KUrl& url)
56 {
57 m_url = url;
58 }
59
60 void DolphinController::setItemView(QAbstractItemView* view)
61 {
62 if (m_itemView != 0) {
63 disconnect(m_itemView, SIGNAL(pressed(const QModelIndex&)),
64 this, SLOT(updateMouseButtonState()));
65 }
66
67 m_itemView = view;
68
69 if (m_itemView != 0) {
70 m_zoomLevel = ZoomLevelInfo::zoomLevelForIconSize(m_itemView->iconSize());
71
72 // TODO: this is a workaround until Qt-issue 176832 has been fixed
73 connect(m_itemView, SIGNAL(pressed(const QModelIndex&)),
74 this, SLOT(updateMouseButtonState()));
75 }
76 }
77
78 void DolphinController::triggerUrlChangeRequest(const KUrl& url)
79 {
80 if (m_url != url) {
81 emit requestUrlChange(url);
82 }
83 }
84
85 void DolphinController::triggerContextMenuRequest(const QPoint& pos,
86 const QList<QAction*>& customActions)
87 {
88 emit activated();
89 emit requestContextMenu(pos, customActions);
90 }
91
92 void DolphinController::requestActivation()
93 {
94 emit activated();
95 }
96
97 void DolphinController::indicateDroppedUrls(const KFileItem& destItem,
98 const KUrl& destPath,
99 QDropEvent* event)
100 {
101 emit urlsDropped(destItem, destPath, event);
102 }
103
104
105 void DolphinController::indicateSortingChange(DolphinView::Sorting sorting)
106 {
107 emit sortingChanged(sorting);
108 }
109
110 void DolphinController::indicateSortOrderChange(Qt::SortOrder order)
111 {
112 emit sortOrderChanged(order);
113 }
114
115 void DolphinController::indicateSortFoldersFirstChange(bool foldersFirst)
116 {
117 emit sortFoldersFirstChanged(foldersFirst);
118 }
119
120 void DolphinController::indicateAdditionalInfoChange(const KFileItemDelegate::InformationList& info)
121 {
122 emit additionalInfoChanged(info);
123 }
124
125 void DolphinController::indicateActivationChange(bool active)
126 {
127 emit activationChanged(active);
128 }
129
130 void DolphinController::setNameFilter(const QString& nameFilter)
131 {
132 if (nameFilter != m_nameFilter) {
133 m_nameFilter = nameFilter;
134 emit nameFilterChanged(nameFilter);
135 }
136 }
137
138 QString DolphinController::nameFilter() const
139 {
140 return m_nameFilter;
141 }
142
143 void DolphinController::setZoomLevel(int level)
144 {
145 Q_ASSERT(level >= ZoomLevelInfo::minimumLevel());
146 Q_ASSERT(level <= ZoomLevelInfo::maximumLevel());
147 if (level != m_zoomLevel) {
148 m_zoomLevel = level;
149 emit zoomLevelChanged(m_zoomLevel);
150 }
151 }
152
153 void DolphinController::setVersionControlActions(QList<QAction*> actions)
154 {
155 m_versionControlActions = actions;
156 }
157
158 QList<QAction*> DolphinController::versionControlActions(const KFileItemList& items)
159 {
160 emit requestVersionControlActions(items);
161 // All view implementations are connected with the signal requestVersionControlActions()
162 // (see ViewExtensionFactory) and will invoke DolphinController::setVersionControlActions(),
163 // so that the context dependent actions can be returned.
164 return m_versionControlActions;
165 }
166
167 void DolphinController::handleKeyPressEvent(QKeyEvent* event)
168 {
169 Q_ASSERT(m_itemView != 0);
170
171 const QItemSelectionModel* selModel = m_itemView->selectionModel();
172 const QModelIndex currentIndex = selModel->currentIndex();
173 const bool trigger = currentIndex.isValid()
174 && ((event->key() == Qt::Key_Return)
175 || (event->key() == Qt::Key_Enter))
176 && !selModel->selectedIndexes().isEmpty();
177 if (trigger) {
178 const QModelIndexList indexList = selModel->selectedIndexes();
179 foreach (const QModelIndex& index, indexList) {
180 emit itemTriggered(itemForIndex(index));
181 }
182 }
183 }
184
185 void DolphinController::replaceUrlByClipboard()
186 {
187 const QClipboard* clipboard = QApplication::clipboard();
188 QString text;
189 if (clipboard->mimeData(QClipboard::Selection)->hasText()) {
190 text = clipboard->mimeData(QClipboard::Selection)->text();
191 } else if (clipboard->mimeData(QClipboard::Clipboard)->hasText()) {
192 text = clipboard->mimeData(QClipboard::Clipboard)->text();
193 }
194 if (!text.isEmpty() && QDir::isAbsolutePath(text)) {
195 m_dolphinView->setUrl(KUrl(text));
196 }
197 }
198
199 void DolphinController::emitHideToolTip()
200 {
201 emit hideToolTip();
202 }
203
204 void DolphinController::emitItemTriggered(const KFileItem& item)
205 {
206 emit itemTriggered(item);
207 }
208
209 KFileItem DolphinController::itemForIndex(const QModelIndex& index) const
210 {
211 Q_ASSERT(m_itemView != 0);
212
213 QAbstractProxyModel* proxyModel = static_cast<QAbstractProxyModel*>(m_itemView->model());
214 KDirModel* dirModel = static_cast<KDirModel*>(proxyModel->sourceModel());
215 const QModelIndex dirIndex = proxyModel->mapToSource(index);
216 return dirModel->itemForIndex(dirIndex);
217 }
218
219 void DolphinController::triggerItem(const QModelIndex& index)
220 {
221 if (m_mouseButtons & Qt::LeftButton) {
222 const KFileItem item = itemForIndex(index);
223 if (index.isValid() && (index.column() == KDirModel::Name)) {
224 emit itemTriggered(item);
225 } else {
226 m_itemView->clearSelection();
227 emit itemEntered(KFileItem());
228 }
229 }
230 }
231
232 void DolphinController::requestTab(const QModelIndex& index)
233 {
234 if (m_mouseButtons & Qt::MidButton) {
235 const KFileItem item = itemForIndex(index);
236 const bool validRequest = index.isValid() &&
237 (index.column() == KDirModel::Name) &&
238 (item.isDir() || m_dolphinView->isTabsForFilesEnabled());
239 if (validRequest) {
240 emit tabRequested(item.url());
241 }
242 }
243 }
244
245 void DolphinController::emitItemEntered(const QModelIndex& index)
246 {
247 KFileItem item = itemForIndex(index);
248 if (!item.isNull()) {
249 emit itemEntered(item);
250 }
251 }
252
253 void DolphinController::emitViewportEntered()
254 {
255 emit viewportEntered();
256 }
257
258 void DolphinController::emitSelectionChanged()
259 {
260 emit selectionChanged();
261 }
262
263 void DolphinController::updateMouseButtonState()
264 {
265 m_mouseButtons = QApplication::mouseButtons();
266 }
267
268 #include "dolphincontroller.moc"