]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincontroller.cpp
Remove all code that is related to DolphinController's
[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::triggerContextMenuRequest(const QPoint& pos,
79 const QList<QAction*>& customActions)
80 {
81 emit activated();
82 emit requestContextMenu(pos, customActions);
83 }
84
85 void DolphinController::requestActivation()
86 {
87 emit activated();
88 }
89
90 void DolphinController::indicateDroppedUrls(const KFileItem& destItem,
91 const KUrl& destPath,
92 QDropEvent* event)
93 {
94 emit urlsDropped(destItem, destPath, event);
95 }
96
97
98 void DolphinController::indicateSortingChange(DolphinView::Sorting sorting)
99 {
100 emit sortingChanged(sorting);
101 }
102
103 void DolphinController::indicateSortOrderChange(Qt::SortOrder order)
104 {
105 emit sortOrderChanged(order);
106 }
107
108 void DolphinController::indicateSortFoldersFirstChange(bool foldersFirst)
109 {
110 emit sortFoldersFirstChanged(foldersFirst);
111 }
112
113 void DolphinController::indicateAdditionalInfoChange(const KFileItemDelegate::InformationList& info)
114 {
115 emit additionalInfoChanged(info);
116 }
117
118 void DolphinController::indicateActivationChange(bool active)
119 {
120 emit activationChanged(active);
121 }
122
123 void DolphinController::setNameFilter(const QString& nameFilter)
124 {
125 if (nameFilter != m_nameFilter) {
126 m_nameFilter = nameFilter;
127 emit nameFilterChanged(nameFilter);
128 }
129 }
130
131 QString DolphinController::nameFilter() const
132 {
133 return m_nameFilter;
134 }
135
136 void DolphinController::setZoomLevel(int level)
137 {
138 Q_ASSERT(level >= ZoomLevelInfo::minimumLevel());
139 Q_ASSERT(level <= ZoomLevelInfo::maximumLevel());
140 if (level != m_zoomLevel) {
141 m_zoomLevel = level;
142 emit zoomLevelChanged(m_zoomLevel);
143 }
144 }
145
146 void DolphinController::setVersionControlActions(QList<QAction*> actions)
147 {
148 m_versionControlActions = actions;
149 }
150
151 QList<QAction*> DolphinController::versionControlActions(const KFileItemList& items)
152 {
153 emit requestVersionControlActions(items);
154 // All view implementations are connected with the signal requestVersionControlActions()
155 // (see ViewExtensionFactory) and will invoke DolphinController::setVersionControlActions(),
156 // so that the context dependent actions can be returned.
157 return m_versionControlActions;
158 }
159
160 void DolphinController::handleKeyPressEvent(QKeyEvent* event)
161 {
162 Q_ASSERT(m_itemView != 0);
163
164 const QItemSelectionModel* selModel = m_itemView->selectionModel();
165 const QModelIndex currentIndex = selModel->currentIndex();
166 const bool trigger = currentIndex.isValid()
167 && ((event->key() == Qt::Key_Return) || (event->key() == Qt::Key_Enter))
168 && !selModel->selectedIndexes().isEmpty();
169 if (!trigger) {
170 return;
171 }
172
173 // Emit the signal itemTriggered() for all selected files.
174 // Several selected directories are opened in separate tabs,
175 // one selected directory will get opened in the view.
176 QModelIndexList dirQueue;
177 const QModelIndexList indexList = selModel->selectedIndexes();
178 foreach (const QModelIndex& index, indexList) {
179 if (itemForIndex(index).isDir()) {
180 dirQueue << index;
181 } else {
182 emit itemTriggered(itemForIndex(index));
183 }
184 }
185
186 if (dirQueue.length() == 1) {
187 // open directory in the view
188 emit itemTriggered(itemForIndex(dirQueue[0]));
189 } else {
190 // open directories in separate tabs
191 foreach(const QModelIndex& dir, dirQueue) {
192 emit tabRequested(itemForIndex(dir).url());
193 }
194 }
195 }
196
197 void DolphinController::replaceUrlByClipboard()
198 {
199 const QClipboard* clipboard = QApplication::clipboard();
200 QString text;
201 if (clipboard->mimeData(QClipboard::Selection)->hasText()) {
202 text = clipboard->mimeData(QClipboard::Selection)->text();
203 } else if (clipboard->mimeData(QClipboard::Clipboard)->hasText()) {
204 text = clipboard->mimeData(QClipboard::Clipboard)->text();
205 }
206 if (!text.isEmpty() && QDir::isAbsolutePath(text)) {
207 m_dolphinView->setUrl(KUrl(text));
208 }
209 }
210
211 void DolphinController::emitHideToolTip()
212 {
213 emit hideToolTip();
214 }
215
216 void DolphinController::emitItemTriggered(const KFileItem& item)
217 {
218 emit itemTriggered(item);
219 }
220
221 KFileItem DolphinController::itemForIndex(const QModelIndex& index) const
222 {
223 Q_ASSERT(m_itemView != 0);
224
225 QAbstractProxyModel* proxyModel = static_cast<QAbstractProxyModel*>(m_itemView->model());
226 KDirModel* dirModel = static_cast<KDirModel*>(proxyModel->sourceModel());
227 const QModelIndex dirIndex = proxyModel->mapToSource(index);
228 return dirModel->itemForIndex(dirIndex);
229 }
230
231 void DolphinController::triggerItem(const QModelIndex& index)
232 {
233 if (m_mouseButtons & Qt::LeftButton) {
234 const KFileItem item = itemForIndex(index);
235 if (index.isValid() && (index.column() == KDirModel::Name)) {
236 emit itemTriggered(item);
237 } else {
238 m_itemView->clearSelection();
239 emit itemEntered(KFileItem());
240 }
241 }
242 }
243
244 void DolphinController::requestTab(const QModelIndex& index)
245 {
246 if (m_mouseButtons & Qt::MidButton) {
247 const KFileItem item = itemForIndex(index);
248 const bool validRequest = index.isValid() &&
249 (index.column() == KDirModel::Name) &&
250 (item.isDir() || m_dolphinView->isTabsForFilesEnabled());
251 if (validRequest) {
252 emit tabRequested(item.url());
253 }
254 }
255 }
256
257 void DolphinController::emitItemEntered(const QModelIndex& index)
258 {
259 KFileItem item = itemForIndex(index);
260 if (!item.isNull()) {
261 emit itemEntered(item);
262 }
263 }
264
265 void DolphinController::emitViewportEntered()
266 {
267 emit viewportEntered();
268 }
269
270 void DolphinController::updateMouseButtonState()
271 {
272 m_mouseButtons = QApplication::mouseButtons();
273 }
274
275 #include "dolphincontroller.moc"