]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincontroller.cpp
b128c90335e2c89804458b7a0ac88f1b86f9b47d
[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 const QList<QAction*>& customActions)
79 {
80 emit activated();
81 emit requestContextMenu(pos, customActions);
82 }
83
84 void DolphinController::requestActivation()
85 {
86 emit activated();
87 }
88
89 void DolphinController::indicateDroppedUrls(const KFileItem& destItem,
90 const KUrl& destPath,
91 QDropEvent* event)
92 {
93 emit urlsDropped(destItem, destPath, event);
94 }
95
96
97 void DolphinController::indicateSortingChange(DolphinView::Sorting sorting)
98 {
99 emit sortingChanged(sorting);
100 }
101
102 void DolphinController::indicateSortOrderChange(Qt::SortOrder order)
103 {
104 emit sortOrderChanged(order);
105 }
106
107 void DolphinController::indicateAdditionalInfoChange(const KFileItemDelegate::InformationList& info)
108 {
109 emit additionalInfoChanged(info);
110 }
111
112 void DolphinController::indicateActivationChange(bool active)
113 {
114 emit activationChanged(active);
115 }
116
117 void DolphinController::setZoomLevel(int level)
118 {
119 Q_ASSERT(level >= ZoomLevelInfo::minimumLevel());
120 Q_ASSERT(level <= ZoomLevelInfo::maximumLevel());
121 if (level != m_zoomLevel) {
122 m_zoomLevel = level;
123 emit zoomLevelChanged(m_zoomLevel);
124 }
125 }
126
127 void DolphinController::handleKeyPressEvent(QKeyEvent* event)
128 {
129 Q_ASSERT(m_itemView != 0);
130
131 const QItemSelectionModel* selModel = m_itemView->selectionModel();
132 const QModelIndex currentIndex = selModel->currentIndex();
133 const bool trigger = currentIndex.isValid()
134 && ((event->key() == Qt::Key_Return)
135 || (event->key() == Qt::Key_Enter))
136 && (selModel->selectedIndexes().count() > 0);
137 if (trigger) {
138 const QModelIndexList indexList = selModel->selectedIndexes();
139 foreach (const QModelIndex& index, indexList) {
140 emit itemTriggered(itemForIndex(index));
141 }
142 }
143 }
144
145 void DolphinController::replaceUrlByClipboard()
146 {
147 const QClipboard* clipboard = QApplication::clipboard();
148 QString text;
149 if (clipboard->mimeData(QClipboard::Selection)->hasText()) {
150 text = clipboard->mimeData(QClipboard::Selection)->text();
151 } else if (clipboard->mimeData(QClipboard::Clipboard)->hasText()) {
152 text = clipboard->mimeData(QClipboard::Clipboard)->text();
153 }
154 if (!text.isEmpty() && QDir::isAbsolutePath(text)) {
155 m_dolphinView->setUrl(KUrl(text));
156 }
157 }
158
159 void DolphinController::emitHideToolTip()
160 {
161 emit hideToolTip();
162 }
163
164 void DolphinController::emitItemTriggered(const KFileItem& item)
165 {
166 emit itemTriggered(item);
167 }
168
169 KFileItem DolphinController::itemForIndex(const QModelIndex& index) const
170 {
171 Q_ASSERT(m_itemView != 0);
172
173 QAbstractProxyModel* proxyModel = static_cast<QAbstractProxyModel*>(m_itemView->model());
174 KDirModel* dirModel = static_cast<KDirModel*>(proxyModel->sourceModel());
175 const QModelIndex dirIndex = proxyModel->mapToSource(index);
176 return dirModel->itemForIndex(dirIndex);
177 }
178
179 void DolphinController::triggerItem(const QModelIndex& index)
180 {
181 if (m_mouseButtons & Qt::LeftButton) {
182 const KFileItem item = itemForIndex(index);
183 if (index.isValid() && (index.column() == KDirModel::Name)) {
184 emit itemTriggered(item);
185 } else {
186 m_itemView->clearSelection();
187 emit itemEntered(KFileItem());
188 }
189 }
190 }
191
192 void DolphinController::requestTab(const QModelIndex& index)
193 {
194 if (m_mouseButtons & Qt::MidButton) {
195 const KFileItem item = itemForIndex(index);
196 const bool validRequest = index.isValid() &&
197 (index.column() == KDirModel::Name) &&
198 (item.isDir() || m_dolphinView->isTabsForFilesEnabled());
199 if (validRequest) {
200 emit tabRequested(item.url());
201 }
202 }
203 }
204
205 void DolphinController::emitItemEntered(const QModelIndex& index)
206 {
207 KFileItem item = itemForIndex(index);
208 if (!item.isNull()) {
209 emit itemEntered(item);
210 }
211 }
212
213 void DolphinController::emitViewportEntered()
214 {
215 emit viewportEntered();
216 }
217
218 void DolphinController::updateMouseButtonState()
219 {
220 m_mouseButtons = QApplication::mouseButtons();
221 }
222
223 #include "dolphincontroller.moc"