]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinviewcontroller.cpp
Split the class DolphinController into the two classes DolphinViewController and...
[dolphin.git] / src / dolphinviewcontroller.cpp
1 /***************************************************************************
2 * Copyright (C) 2010 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 "dolphinviewcontroller.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 DolphinViewController::m_mouseButtons = Qt::NoButton;
30
31 DolphinViewController::DolphinViewController(DolphinView* dolphinView) :
32 QObject(dolphinView),
33 m_dolphinView(dolphinView),
34 m_itemView(0),
35 m_versionControlActions()
36 {
37 }
38
39 DolphinViewController::~DolphinViewController()
40 {
41 }
42
43 const DolphinView* DolphinViewController::view() const
44 {
45 return m_dolphinView;
46 }
47
48 void DolphinViewController::requestUrlChange(const KUrl& url)
49 {
50 emit urlChangeRequested(url);
51 }
52
53 void DolphinViewController::setItemView(QAbstractItemView* view)
54 {
55 if (m_itemView != 0) {
56 disconnect(m_itemView, SIGNAL(pressed(const QModelIndex&)),
57 this, SLOT(updateMouseButtonState()));
58 }
59
60 m_itemView = view;
61
62 if (m_itemView != 0) {
63 // TODO: this is a workaround until Qt-issue 176832 has been fixed
64 connect(m_itemView, SIGNAL(pressed(const QModelIndex&)),
65 this, SLOT(updateMouseButtonState()));
66 }
67 }
68
69 QAbstractItemView* DolphinViewController::itemView() const
70 {
71 return m_itemView;
72 }
73
74 void DolphinViewController::triggerContextMenuRequest(const QPoint& pos,
75 const QList<QAction*>& customActions)
76 {
77 emit activated();
78 emit requestContextMenu(pos, customActions);
79 }
80
81 void DolphinViewController::requestActivation()
82 {
83 emit activated();
84 }
85
86 void DolphinViewController::indicateDroppedUrls(const KFileItem& destItem,
87 const KUrl& destPath,
88 QDropEvent* event)
89 {
90 emit urlsDropped(destItem, destPath, event);
91 }
92
93
94 void DolphinViewController::indicateSortingChange(DolphinView::Sorting sorting)
95 {
96 emit sortingChanged(sorting);
97 }
98
99 void DolphinViewController::indicateSortOrderChange(Qt::SortOrder order)
100 {
101 emit sortOrderChanged(order);
102 }
103
104 void DolphinViewController::indicateSortFoldersFirstChange(bool foldersFirst)
105 {
106 emit sortFoldersFirstChanged(foldersFirst);
107 }
108
109 void DolphinViewController::indicateAdditionalInfoChange(const KFileItemDelegate::InformationList& info)
110 {
111 emit additionalInfoChanged(info);
112 }
113
114 void DolphinViewController::setVersionControlActions(QList<QAction*> actions)
115 {
116 m_versionControlActions = actions;
117 }
118
119 QList<QAction*> DolphinViewController::versionControlActions(const KFileItemList& items)
120 {
121 emit requestVersionControlActions(items);
122 // All view implementations are connected with the signal requestVersionControlActions()
123 // (see ViewExtensionFactory) and will invoke DolphinViewController::setVersionControlActions(),
124 // so that the context dependent actions can be returned.
125 return m_versionControlActions;
126 }
127
128 void DolphinViewController::handleKeyPressEvent(QKeyEvent* event)
129 {
130 Q_ASSERT(m_itemView != 0);
131
132 const QItemSelectionModel* selModel = m_itemView->selectionModel();
133 const QModelIndex currentIndex = selModel->currentIndex();
134 const bool trigger = currentIndex.isValid()
135 && ((event->key() == Qt::Key_Return) || (event->key() == Qt::Key_Enter))
136 && !selModel->selectedIndexes().isEmpty();
137 if (!trigger) {
138 return;
139 }
140
141 // Emit the signal itemTriggered() for all selected files.
142 // Several selected directories are opened in separate tabs,
143 // one selected directory will get opened in the view.
144 QModelIndexList dirQueue;
145 const QModelIndexList indexList = selModel->selectedIndexes();
146 foreach (const QModelIndex& index, indexList) {
147 if (itemForIndex(index).isDir()) {
148 dirQueue << index;
149 } else {
150 emit itemTriggered(itemForIndex(index));
151 }
152 }
153
154 if (dirQueue.length() == 1) {
155 // open directory in the view
156 emit itemTriggered(itemForIndex(dirQueue[0]));
157 } else {
158 // open directories in separate tabs
159 foreach(const QModelIndex& dir, dirQueue) {
160 emit tabRequested(itemForIndex(dir).url());
161 }
162 }
163 }
164
165 void DolphinViewController::replaceUrlByClipboard()
166 {
167 const QClipboard* clipboard = QApplication::clipboard();
168 QString text;
169 if (clipboard->mimeData(QClipboard::Selection)->hasText()) {
170 text = clipboard->mimeData(QClipboard::Selection)->text();
171 } else if (clipboard->mimeData(QClipboard::Clipboard)->hasText()) {
172 text = clipboard->mimeData(QClipboard::Clipboard)->text();
173 }
174 if (!text.isEmpty() && QDir::isAbsolutePath(text)) {
175 m_dolphinView->setUrl(KUrl(text));
176 }
177 }
178
179 void DolphinViewController::requestToolTipHiding()
180 {
181 emit hideToolTip();
182 }
183
184 void DolphinViewController::emitItemTriggered(const KFileItem& item)
185 {
186 emit itemTriggered(item);
187 }
188
189 KFileItem DolphinViewController::itemForIndex(const QModelIndex& index) const
190 {
191 Q_ASSERT(m_itemView != 0);
192
193 QAbstractProxyModel* proxyModel = static_cast<QAbstractProxyModel*>(m_itemView->model());
194 KDirModel* dirModel = static_cast<KDirModel*>(proxyModel->sourceModel());
195 const QModelIndex dirIndex = proxyModel->mapToSource(index);
196 return dirModel->itemForIndex(dirIndex);
197 }
198
199 void DolphinViewController::triggerItem(const QModelIndex& index)
200 {
201 if (m_mouseButtons & Qt::LeftButton) {
202 const KFileItem item = itemForIndex(index);
203 if (index.isValid() && (index.column() == KDirModel::Name)) {
204 emit itemTriggered(item);
205 } else {
206 m_itemView->clearSelection();
207 emit itemEntered(KFileItem());
208 }
209 }
210 }
211
212 void DolphinViewController::requestTab(const QModelIndex& index)
213 {
214 if (m_mouseButtons & Qt::MidButton) {
215 const KFileItem item = itemForIndex(index);
216 const bool validRequest = index.isValid() &&
217 (index.column() == KDirModel::Name) &&
218 (item.isDir() || m_dolphinView->isTabsForFilesEnabled());
219 if (validRequest) {
220 emit tabRequested(item.url());
221 }
222 }
223 }
224
225 void DolphinViewController::emitItemEntered(const QModelIndex& index)
226 {
227 KFileItem item = itemForIndex(index);
228 if (!item.isNull()) {
229 emit itemEntered(item);
230 }
231 }
232
233 void DolphinViewController::emitViewportEntered()
234 {
235 emit viewportEntered();
236 }
237
238 void DolphinViewController::updateMouseButtonState()
239 {
240 m_mouseButtons = QApplication::mouseButtons();
241 }
242
243 #include "dolphinviewcontroller.moc"