]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincontroller.cpp
Restore filtering of items. The DolphinView just tells the controller about the filte...
[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 urlChanged(url);
50 }
51 }
52
53 void DolphinController::setItemView(QAbstractItemView* view)
54 {
55 if (m_itemView != 0) {
56 disconnect(m_itemView, SIGNAL(pressed(const QModelIndex&)),
57 this, SLOT(updateMouseButtonState()));
58 }
59
60 m_itemView = view;
61
62 if (m_itemView != 0) {
63 m_zoomLevel = ZoomLevelInfo::zoomLevelForIconSize(m_itemView->iconSize());
64
65 // TODO: this is a workaround until Qt-issue 176832 has been fixed
66 connect(m_itemView, SIGNAL(pressed(const QModelIndex&)),
67 this, SLOT(updateMouseButtonState()));
68 }
69 }
70
71 void DolphinController::triggerUrlChangeRequest(const KUrl& url)
72 {
73 if (m_url != url) {
74 emit requestUrlChange(url);
75 }
76 }
77
78 void DolphinController::triggerContextMenuRequest(const QPoint& pos,
79 const QList<QAction*>& customActions)
80 {
81 emit activated();
82 emit requestContextMenu(pos, customActions);
83 }
84
85 void DolphinController::requestActivation()
86 {
87 emit activated();
88 }
89
90 void DolphinController::indicateDroppedUrls(const KFileItem& destItem,
91 const KUrl& destPath,
92 QDropEvent* event)
93 {
94 emit urlsDropped(destItem, destPath, event);
95 }
96
97
98 void DolphinController::indicateSortingChange(DolphinView::Sorting sorting)
99 {
100 emit sortingChanged(sorting);
101 }
102
103 void DolphinController::indicateSortOrderChange(Qt::SortOrder order)
104 {
105 emit sortOrderChanged(order);
106 }
107
108 void DolphinController::indicateSortFoldersFirstChange(bool foldersFirst)
109 {
110 emit sortFoldersFirstChanged(foldersFirst);
111 }
112
113 void DolphinController::indicateAdditionalInfoChange(const KFileItemDelegate::InformationList& info)
114 {
115 emit additionalInfoChanged(info);
116 }
117
118 void DolphinController::indicateActivationChange(bool active)
119 {
120 emit activationChanged(active);
121 }
122
123 void DolphinController::setNameFilter(const QString& nameFilter)
124 {
125 if (nameFilter != m_nameFilter) {
126 m_nameFilter = nameFilter;
127 emit nameFilterChanged(nameFilter);
128 }
129 }
130
131 QString DolphinController::nameFilter() const
132 {
133 return m_nameFilter;
134 }
135
136 void DolphinController::setZoomLevel(int level)
137 {
138 Q_ASSERT(level >= ZoomLevelInfo::minimumLevel());
139 Q_ASSERT(level <= ZoomLevelInfo::maximumLevel());
140 if (level != m_zoomLevel) {
141 m_zoomLevel = level;
142 emit zoomLevelChanged(m_zoomLevel);
143 }
144 }
145
146 void DolphinController::handleKeyPressEvent(QKeyEvent* event)
147 {
148 Q_ASSERT(m_itemView != 0);
149
150 const QItemSelectionModel* selModel = m_itemView->selectionModel();
151 const QModelIndex currentIndex = selModel->currentIndex();
152 const bool trigger = currentIndex.isValid()
153 && ((event->key() == Qt::Key_Return)
154 || (event->key() == Qt::Key_Enter))
155 && !selModel->selectedIndexes().isEmpty();
156 if (trigger) {
157 const QModelIndexList indexList = selModel->selectedIndexes();
158 foreach (const QModelIndex& index, indexList) {
159 emit itemTriggered(itemForIndex(index));
160 }
161 }
162 }
163
164 void DolphinController::replaceUrlByClipboard()
165 {
166 const QClipboard* clipboard = QApplication::clipboard();
167 QString text;
168 if (clipboard->mimeData(QClipboard::Selection)->hasText()) {
169 text = clipboard->mimeData(QClipboard::Selection)->text();
170 } else if (clipboard->mimeData(QClipboard::Clipboard)->hasText()) {
171 text = clipboard->mimeData(QClipboard::Clipboard)->text();
172 }
173 if (!text.isEmpty() && QDir::isAbsolutePath(text)) {
174 m_dolphinView->setUrl(KUrl(text));
175 }
176 }
177
178 void DolphinController::emitHideToolTip()
179 {
180 emit hideToolTip();
181 }
182
183 void DolphinController::emitItemTriggered(const KFileItem& item)
184 {
185 emit itemTriggered(item);
186 }
187
188 KFileItem DolphinController::itemForIndex(const QModelIndex& index) const
189 {
190 Q_ASSERT(m_itemView != 0);
191
192 QAbstractProxyModel* proxyModel = static_cast<QAbstractProxyModel*>(m_itemView->model());
193 KDirModel* dirModel = static_cast<KDirModel*>(proxyModel->sourceModel());
194 const QModelIndex dirIndex = proxyModel->mapToSource(index);
195 return dirModel->itemForIndex(dirIndex);
196 }
197
198 void DolphinController::triggerItem(const QModelIndex& index)
199 {
200 if (m_mouseButtons & Qt::LeftButton) {
201 const KFileItem item = itemForIndex(index);
202 if (index.isValid() && (index.column() == KDirModel::Name)) {
203 emit itemTriggered(item);
204 } else {
205 m_itemView->clearSelection();
206 emit itemEntered(KFileItem());
207 }
208 }
209 }
210
211 void DolphinController::requestTab(const QModelIndex& index)
212 {
213 if (m_mouseButtons & Qt::MidButton) {
214 const KFileItem item = itemForIndex(index);
215 const bool validRequest = index.isValid() &&
216 (index.column() == KDirModel::Name) &&
217 (item.isDir() || m_dolphinView->isTabsForFilesEnabled());
218 if (validRequest) {
219 emit tabRequested(item.url());
220 }
221 }
222 }
223
224 void DolphinController::emitItemEntered(const QModelIndex& index)
225 {
226 KFileItem item = itemForIndex(index);
227 if (!item.isNull()) {
228 emit itemEntered(item);
229 }
230 }
231
232 void DolphinController::emitViewportEntered()
233 {
234 emit viewportEntered();
235 }
236
237 void DolphinController::updateMouseButtonState()
238 {
239 m_mouseButtons = QApplication::mouseButtons();
240 }
241
242 #include "dolphincontroller.moc"