]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincontroller.cpp
Added right arrow key usage when in the Column View mode. When a directory is selecte...
[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_url(),
35 m_dolphinView(dolphinView),
36 m_itemView(0)
37 {
38 }
39
40 DolphinController::~DolphinController()
41 {
42 }
43
44 void DolphinController::setUrl(const KUrl& url)
45 {
46 if (m_url != url) {
47 m_url = url;
48 emit urlChanged(url);
49 }
50 }
51
52 void DolphinController::setItemView(QAbstractItemView* view)
53 {
54 if (m_itemView != 0) {
55 disconnect(m_itemView, SIGNAL(pressed(const QModelIndex&)),
56 this, SLOT(updateMouseButtonState()));
57 }
58
59 m_itemView = view;
60
61 if (m_itemView != 0) {
62 m_zoomLevel = ZoomLevelInfo::zoomLevelForIconSize(m_itemView->iconSize());
63
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 void DolphinController::triggerUrlChangeRequest(const KUrl& url)
71 {
72 if (m_url != url) {
73 emit requestUrlChange(url);
74 }
75 }
76
77 void DolphinController::triggerContextMenuRequest(const QPoint& pos)
78 {
79 emit activated();
80 emit requestContextMenu(pos);
81 }
82
83 void DolphinController::requestActivation()
84 {
85 emit activated();
86 }
87
88 void DolphinController::indicateDroppedUrls(const KFileItem& destItem,
89 const KUrl& destPath,
90 QDropEvent* event)
91 {
92 emit urlsDropped(destItem, destPath, event);
93 }
94
95
96 void DolphinController::indicateSortingChange(DolphinView::Sorting sorting)
97 {
98 emit sortingChanged(sorting);
99 }
100
101 void DolphinController::indicateSortOrderChange(Qt::SortOrder order)
102 {
103 emit sortOrderChanged(order);
104 }
105
106 void DolphinController::indicateAdditionalInfoChange(const KFileItemDelegate::InformationList& info)
107 {
108 emit additionalInfoChanged(info);
109 }
110
111 void DolphinController::indicateActivationChange(bool active)
112 {
113 emit activationChanged(active);
114 }
115
116 void DolphinController::setZoomLevel(int level)
117 {
118 Q_ASSERT(level >= ZoomLevelInfo::minimumLevel());
119 Q_ASSERT(level <= ZoomLevelInfo::maximumLevel());
120 if (level != m_zoomLevel) {
121 m_zoomLevel = level;
122 emit zoomLevelChanged(m_zoomLevel);
123 }
124 }
125
126 void DolphinController::handleKeyPressEvent(QKeyEvent* event)
127 {
128 Q_ASSERT(m_itemView != 0);
129
130 const QItemSelectionModel* selModel = m_itemView->selectionModel();
131 const QModelIndex currentIndex = selModel->currentIndex();
132
133 if (currentIndex.isValid() && selModel->selectedIndexes().count() > 0) {
134 const int key = event->key();
135
136 if ((key == Qt::Key_Return) || (key == Qt::Key_Enter) || (key == Qt::Key_Right)) {
137
138 const QModelIndexList indexList = selModel->selectedIndexes();
139 const bool isColumnView = m_dolphinView->mode() == m_dolphinView->ColumnView;
140
141 if (key == Qt::Key_Right) {
142 if (isColumnView) {
143 // If it is the right arrow key and in the column view-only.
144 KFileItem curFileItem;
145 foreach(const QModelIndex& index, indexList) {
146 curFileItem = itemForIndex(index);
147 if (!curFileItem.isFile()) {
148 /* We want
149 * to make sure that the selected item
150 * is only a folder. If we did not have this check, it would be possible to use
151 * the right arrow to open a file when in the column view */
152 emit itemTriggered(curFileItem);
153 }
154 }
155 }
156 } else {
157 //Else it is Return or Enter keypress, so it is okay to perform the action of triggering, on files also.
158 foreach(const QModelIndex& index, indexList) {
159 emit itemTriggered(itemForIndex(index));
160 }
161 }
162 }
163 }
164 }
165
166 void DolphinController::replaceUrlByClipboard()
167 {
168 const QClipboard* clipboard = QApplication::clipboard();
169 QString text;
170 if (clipboard->mimeData(QClipboard::Selection)->hasText()) {
171 text = clipboard->mimeData(QClipboard::Selection)->text();
172 } else if (clipboard->mimeData(QClipboard::Clipboard)->hasText()) {
173 text = clipboard->mimeData(QClipboard::Clipboard)->text();
174 }
175 if (!text.isEmpty() && QDir::isAbsolutePath(text)) {
176 m_dolphinView->setUrl(KUrl(text));
177 }
178 }
179
180 void DolphinController::emitHideToolTip()
181 {
182 emit hideToolTip();
183 }
184
185 KFileItem DolphinController::itemForIndex(const QModelIndex& index) const
186 {
187 Q_ASSERT(m_itemView != 0);
188
189 QAbstractProxyModel* proxyModel = static_cast<QAbstractProxyModel*>(m_itemView->model());
190 KDirModel* dirModel = static_cast<KDirModel*>(proxyModel->sourceModel());
191 const QModelIndex dirIndex = proxyModel->mapToSource(index);
192 return dirModel->itemForIndex(dirIndex);
193 }
194
195 void DolphinController::triggerItem(const QModelIndex& index)
196 {
197 if (m_mouseButtons & Qt::LeftButton) {
198 const KFileItem item = itemForIndex(index);
199 if (index.isValid() && (index.column() == KDirModel::Name)) {
200 emit itemTriggered(item);
201 } else {
202 m_itemView->clearSelection();
203 emit itemEntered(KFileItem());
204 }
205 }
206 }
207
208 void DolphinController::requestTab(const QModelIndex& index)
209 {
210 if (m_mouseButtons & Qt::MidButton) {
211 const KFileItem item = itemForIndex(index);
212 const bool validRequest = index.isValid() &&
213 (index.column() == KDirModel::Name) &&
214 (item.isDir() || m_dolphinView->isTabsForFilesEnabled());
215 if (validRequest) {
216 emit tabRequested(item.url());
217 }
218 }
219 }
220
221 void DolphinController::emitItemEntered(const QModelIndex& index)
222 {
223 KFileItem item = itemForIndex(index);
224 if (!item.isNull()) {
225 emit itemEntered(item);
226 }
227 }
228
229 void DolphinController::emitViewportEntered()
230 {
231 emit viewportEntered();
232 }
233
234 void DolphinController::updateMouseButtonState()
235 {
236 m_mouseButtons = QApplication::mouseButtons();
237 }
238
239 #include "dolphincontroller.moc"