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