]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincontroller.cpp
Fixed regression that triggering of items with the keyboard did not work anymore...
[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 DolphinController::DolphinController(DolphinView* dolphinView) :
30 QObject(dolphinView),
31 m_zoomLevel(0),
32 m_mouseButtons(Qt::NoButton),
33 m_url(),
34 m_dolphinView(dolphinView),
35 m_itemView(0)
36 {
37 }
38
39 DolphinController::~DolphinController()
40 {
41 }
42
43 void DolphinController::setUrl(const KUrl& url)
44 {
45 if (m_url != url) {
46 m_url = url;
47 emit urlChanged(url);
48 }
49 }
50
51 void DolphinController::setItemView(QAbstractItemView* view)
52 {
53 if (m_itemView != 0) {
54 disconnect(m_itemView, SIGNAL(pressed(const QModelIndex&)),
55 this, SLOT(updateMouseButtonState()));
56 }
57
58 m_itemView = view;
59
60 if (m_itemView != 0) {
61 m_zoomLevel = ZoomLevelInfo::zoomLevelForIconSize(m_itemView->iconSize());
62
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 void DolphinController::triggerUrlChangeRequest(const KUrl& url)
70 {
71 if (m_url != url) {
72 emit requestUrlChange(url);
73 }
74 }
75
76 void DolphinController::triggerContextMenuRequest(const QPoint& pos)
77 {
78 emit activated();
79 emit requestContextMenu(pos);
80 }
81
82 void DolphinController::requestActivation()
83 {
84 emit activated();
85 }
86
87 void DolphinController::indicateDroppedUrls(const KFileItem& destItem,
88 const KUrl& destPath,
89 QDropEvent* event)
90 {
91 emit urlsDropped(destItem, destPath, event);
92 }
93
94
95 void DolphinController::indicateSortingChange(DolphinView::Sorting sorting)
96 {
97 emit sortingChanged(sorting);
98 }
99
100 void DolphinController::indicateSortOrderChange(Qt::SortOrder order)
101 {
102 emit sortOrderChanged(order);
103 }
104
105 void DolphinController::indicateAdditionalInfoChange(const KFileItemDelegate::InformationList& info)
106 {
107 emit additionalInfoChanged(info);
108 }
109
110 void DolphinController::indicateActivationChange(bool active)
111 {
112 emit activationChanged(active);
113 }
114
115 void DolphinController::setZoomLevel(int level)
116 {
117 Q_ASSERT(level >= ZoomLevelInfo::minimumLevel());
118 Q_ASSERT(level <= ZoomLevelInfo::maximumLevel());
119 if (level != m_zoomLevel) {
120 m_zoomLevel = level;
121 emit zoomLevelChanged(m_zoomLevel);
122 }
123 }
124
125 void DolphinController::handleKeyPressEvent(QKeyEvent* event)
126 {
127 Q_ASSERT(m_itemView != 0);
128
129 const QItemSelectionModel* selModel = m_itemView->selectionModel();
130 const QModelIndex currentIndex = selModel->currentIndex();
131 const bool trigger = currentIndex.isValid()
132 && (event->key() == Qt::Key_Return)
133 && (selModel->selectedIndexes().count() > 0);
134 if (trigger) {
135 const QModelIndexList indexList = selModel->selectedIndexes();
136 foreach (const QModelIndex& index, indexList) {
137 emit itemTriggered(itemForIndex(index));
138 }
139 }
140 }
141
142 void DolphinController::replaceUrlByClipboard()
143 {
144 const QClipboard* clipboard = QApplication::clipboard();
145 QString text;
146 if (clipboard->mimeData(QClipboard::Selection)->hasText()) {
147 text = clipboard->mimeData(QClipboard::Selection)->text();
148 } else if (clipboard->mimeData(QClipboard::Clipboard)->hasText()) {
149 text = clipboard->mimeData(QClipboard::Clipboard)->text();
150 }
151 if (!text.isEmpty() && QDir::isAbsolutePath(text)) {
152 m_dolphinView->setUrl(KUrl(text));
153 }
154 }
155
156 void DolphinController::emitHideToolTip()
157 {
158 emit hideToolTip();
159 }
160
161 KFileItem DolphinController::itemForIndex(const QModelIndex& index) const
162 {
163 Q_ASSERT(m_itemView != 0);
164
165 QAbstractProxyModel* proxyModel = static_cast<QAbstractProxyModel*>(m_itemView->model());
166 KDirModel* dirModel = static_cast<KDirModel*>(proxyModel->sourceModel());
167 const QModelIndex dirIndex = proxyModel->mapToSource(index);
168 return dirModel->itemForIndex(dirIndex);
169 }
170
171 void DolphinController::triggerItem(const QModelIndex& index)
172 {
173 if (m_mouseButtons & Qt::LeftButton) {
174 const KFileItem item = itemForIndex(index);
175 if (index.isValid() && (index.column() == KDirModel::Name)) {
176 emit itemTriggered(item);
177 } else {
178 m_itemView->clearSelection();
179 emit itemEntered(KFileItem());
180 }
181 m_mouseButtons = Qt::NoButton;
182 } else if (m_mouseButtons & Qt::RightButton) {
183 m_mouseButtons = Qt::NoButton;
184 }
185 }
186
187 void DolphinController::requestTab(const QModelIndex& index)
188 {
189 if (m_mouseButtons & Qt::MidButton) {
190 const KFileItem item = itemForIndex(index);
191 const bool validRequest = index.isValid() &&
192 (index.column() == KDirModel::Name) &&
193 (item.isDir() || m_dolphinView->isTabsForFilesEnabled());
194 if (validRequest) {
195 emit tabRequested(item.url());
196 }
197 m_mouseButtons = Qt::NoButton;
198 } else if (m_mouseButtons & Qt::RightButton) {
199 m_mouseButtons = Qt::NoButton;
200 }
201 }
202
203 void DolphinController::emitItemEntered(const QModelIndex& index)
204 {
205 KFileItem item = itemForIndex(index);
206 if (!item.isNull()) {
207 emit itemEntered(item);
208 }
209 }
210
211 void DolphinController::emitViewportEntered()
212 {
213 emit viewportEntered();
214 }
215
216 void DolphinController::updateMouseButtonState()
217 {
218 m_mouseButtons = QApplication::mouseButtons();
219 }
220
221 #include "dolphincontroller.moc"