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