]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincontroller.cpp
use KAction::setShortcut() instead of QAction::setShortcut()
[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) || (event->key() == Qt::Key_Enter))
175 && !selModel->selectedIndexes().isEmpty();
176 if (!trigger) {
177 return;
178 }
179
180 // Emit the signal itemTriggered() for all selected files.
181 // Several selected directories are opened in separate tabs,
182 // one selected directory will get opened in the view.
183 QModelIndexList dirQueue;
184 const QModelIndexList indexList = selModel->selectedIndexes();
185 foreach (const QModelIndex& index, indexList) {
186 if (itemForIndex(index).isDir()) {
187 dirQueue << index;
188 } else {
189 emit itemTriggered(itemForIndex(index));
190 }
191 }
192
193 if (dirQueue.length() == 1) {
194 // open directory in the view
195 emit itemTriggered(itemForIndex(dirQueue[0]));
196 } else {
197 // open directories in separate tabs
198 foreach(const QModelIndex& dir, dirQueue) {
199 emit tabRequested(itemForIndex(dir).url());
200 }
201 }
202 }
203
204 void DolphinController::replaceUrlByClipboard()
205 {
206 const QClipboard* clipboard = QApplication::clipboard();
207 QString text;
208 if (clipboard->mimeData(QClipboard::Selection)->hasText()) {
209 text = clipboard->mimeData(QClipboard::Selection)->text();
210 } else if (clipboard->mimeData(QClipboard::Clipboard)->hasText()) {
211 text = clipboard->mimeData(QClipboard::Clipboard)->text();
212 }
213 if (!text.isEmpty() && QDir::isAbsolutePath(text)) {
214 m_dolphinView->setUrl(KUrl(text));
215 }
216 }
217
218 void DolphinController::emitHideToolTip()
219 {
220 emit hideToolTip();
221 }
222
223 void DolphinController::emitItemTriggered(const KFileItem& item)
224 {
225 emit itemTriggered(item);
226 }
227
228 KFileItem DolphinController::itemForIndex(const QModelIndex& index) const
229 {
230 Q_ASSERT(m_itemView != 0);
231
232 QAbstractProxyModel* proxyModel = static_cast<QAbstractProxyModel*>(m_itemView->model());
233 KDirModel* dirModel = static_cast<KDirModel*>(proxyModel->sourceModel());
234 const QModelIndex dirIndex = proxyModel->mapToSource(index);
235 return dirModel->itemForIndex(dirIndex);
236 }
237
238 void DolphinController::triggerItem(const QModelIndex& index)
239 {
240 if (m_mouseButtons & Qt::LeftButton) {
241 const KFileItem item = itemForIndex(index);
242 if (index.isValid() && (index.column() == KDirModel::Name)) {
243 emit itemTriggered(item);
244 } else {
245 m_itemView->clearSelection();
246 emit itemEntered(KFileItem());
247 }
248 }
249 }
250
251 void DolphinController::requestTab(const QModelIndex& index)
252 {
253 if (m_mouseButtons & Qt::MidButton) {
254 const KFileItem item = itemForIndex(index);
255 const bool validRequest = index.isValid() &&
256 (index.column() == KDirModel::Name) &&
257 (item.isDir() || m_dolphinView->isTabsForFilesEnabled());
258 if (validRequest) {
259 emit tabRequested(item.url());
260 }
261 }
262 }
263
264 void DolphinController::emitItemEntered(const QModelIndex& index)
265 {
266 KFileItem item = itemForIndex(index);
267 if (!item.isNull()) {
268 emit itemEntered(item);
269 }
270 }
271
272 void DolphinController::emitViewportEntered()
273 {
274 emit viewportEntered();
275 }
276
277 void DolphinController::updateMouseButtonState()
278 {
279 m_mouseButtons = QApplication::mouseButtons();
280 }
281
282 #include "dolphincontroller.moc"