]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincontroller.cpp
- The selection changed timer only needs to be created for a DolphinView instance.
[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_nameFilter(),
35 m_url(),
36 m_dolphinView(dolphinView),
37 m_itemView(0)
38 {
39 }
40
41 DolphinController::~DolphinController()
42 {
43 }
44
45 void DolphinController::setUrl(const KUrl& url)
46 {
47 if (m_url != url) {
48 m_url = url;
49 emit cancelPreviews();
50 emit urlChanged(url);
51 }
52 }
53
54 void DolphinController::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 m_zoomLevel = ZoomLevelInfo::zoomLevelForIconSize(m_itemView->iconSize());
65
66 // TODO: this is a workaround until Qt-issue 176832 has been fixed
67 connect(m_itemView, SIGNAL(pressed(const QModelIndex&)),
68 this, SLOT(updateMouseButtonState()));
69 }
70 }
71
72 void DolphinController::triggerUrlChangeRequest(const KUrl& url)
73 {
74 if (m_url != url) {
75 emit requestUrlChange(url);
76 }
77 }
78
79 void DolphinController::triggerContextMenuRequest(const QPoint& pos,
80 const QList<QAction*>& customActions)
81 {
82 emit activated();
83 emit requestContextMenu(pos, customActions);
84 }
85
86 void DolphinController::requestActivation()
87 {
88 emit activated();
89 }
90
91 void DolphinController::indicateDroppedUrls(const KFileItem& destItem,
92 const KUrl& destPath,
93 QDropEvent* event)
94 {
95 emit urlsDropped(destItem, destPath, event);
96 }
97
98
99 void DolphinController::indicateSortingChange(DolphinView::Sorting sorting)
100 {
101 emit sortingChanged(sorting);
102 }
103
104 void DolphinController::indicateSortOrderChange(Qt::SortOrder order)
105 {
106 emit sortOrderChanged(order);
107 }
108
109 void DolphinController::indicateSortFoldersFirstChange(bool foldersFirst)
110 {
111 emit sortFoldersFirstChanged(foldersFirst);
112 }
113
114 void DolphinController::indicateAdditionalInfoChange(const KFileItemDelegate::InformationList& info)
115 {
116 emit additionalInfoChanged(info);
117 }
118
119 void DolphinController::indicateActivationChange(bool active)
120 {
121 emit activationChanged(active);
122 }
123
124 void DolphinController::setNameFilter(const QString& nameFilter)
125 {
126 if (nameFilter != m_nameFilter) {
127 m_nameFilter = nameFilter;
128 emit nameFilterChanged(nameFilter);
129 }
130 }
131
132 QString DolphinController::nameFilter() const
133 {
134 return m_nameFilter;
135 }
136
137 void DolphinController::setZoomLevel(int level)
138 {
139 Q_ASSERT(level >= ZoomLevelInfo::minimumLevel());
140 Q_ASSERT(level <= ZoomLevelInfo::maximumLevel());
141 if (level != m_zoomLevel) {
142 m_zoomLevel = level;
143 emit zoomLevelChanged(m_zoomLevel);
144 }
145 }
146
147 void DolphinController::handleKeyPressEvent(QKeyEvent* event)
148 {
149 Q_ASSERT(m_itemView != 0);
150
151 const QItemSelectionModel* selModel = m_itemView->selectionModel();
152 const QModelIndex currentIndex = selModel->currentIndex();
153 const bool trigger = currentIndex.isValid()
154 && ((event->key() == Qt::Key_Return)
155 || (event->key() == Qt::Key_Enter))
156 && !selModel->selectedIndexes().isEmpty();
157 if (trigger) {
158 const QModelIndexList indexList = selModel->selectedIndexes();
159 foreach (const QModelIndex& index, indexList) {
160 emit itemTriggered(itemForIndex(index));
161 }
162 }
163 }
164
165 void DolphinController::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 DolphinController::emitHideToolTip()
180 {
181 emit hideToolTip();
182 }
183
184 void DolphinController::emitItemTriggered(const KFileItem& item)
185 {
186 emit itemTriggered(item);
187 }
188
189 KFileItem DolphinController::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 DolphinController::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 DolphinController::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 DolphinController::emitItemEntered(const QModelIndex& index)
226 {
227 KFileItem item = itemForIndex(index);
228 if (!item.isNull()) {
229 emit itemEntered(item);
230 }
231 }
232
233 void DolphinController::emitViewportEntered()
234 {
235 emit viewportEntered();
236 }
237
238 void DolphinController::updateMouseButtonState()
239 {
240 m_mouseButtons = QApplication::mouseButtons();
241 }
242
243 #include "dolphincontroller.moc"