]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincontroller.cpp
Allow to enable/disable the "Expandable Folders" setting of the details-view by the...
[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 const bool trigger = currentIndex.isValid()
133 && ((event->key() == Qt::Key_Return)
134 || (event->key() == Qt::Key_Enter))
135 && (selModel->selectedIndexes().count() > 0);
136 if (trigger) {
137 const QModelIndexList indexList = selModel->selectedIndexes();
138 foreach (const QModelIndex& index, indexList) {
139 emit itemTriggered(itemForIndex(index));
140 }
141 }
142 }
143
144 void DolphinController::replaceUrlByClipboard()
145 {
146 const QClipboard* clipboard = QApplication::clipboard();
147 QString text;
148 if (clipboard->mimeData(QClipboard::Selection)->hasText()) {
149 text = clipboard->mimeData(QClipboard::Selection)->text();
150 } else if (clipboard->mimeData(QClipboard::Clipboard)->hasText()) {
151 text = clipboard->mimeData(QClipboard::Clipboard)->text();
152 }
153 if (!text.isEmpty() && QDir::isAbsolutePath(text)) {
154 m_dolphinView->setUrl(KUrl(text));
155 }
156 }
157
158 void DolphinController::emitHideToolTip()
159 {
160 emit hideToolTip();
161 }
162
163 KFileItem DolphinController::itemForIndex(const QModelIndex& index) const
164 {
165 Q_ASSERT(m_itemView != 0);
166
167 QAbstractProxyModel* proxyModel = static_cast<QAbstractProxyModel*>(m_itemView->model());
168 KDirModel* dirModel = static_cast<KDirModel*>(proxyModel->sourceModel());
169 const QModelIndex dirIndex = proxyModel->mapToSource(index);
170 return dirModel->itemForIndex(dirIndex);
171 }
172
173 void DolphinController::triggerItem(const QModelIndex& index)
174 {
175 if (m_mouseButtons & Qt::LeftButton) {
176 const KFileItem item = itemForIndex(index);
177 if (index.isValid() && (index.column() == KDirModel::Name)) {
178 emit itemTriggered(item);
179 } else {
180 m_itemView->clearSelection();
181 emit itemEntered(KFileItem());
182 }
183 }
184 }
185
186 void DolphinController::requestTab(const QModelIndex& index)
187 {
188 if (m_mouseButtons & Qt::MidButton) {
189 const KFileItem item = itemForIndex(index);
190 const bool validRequest = index.isValid() &&
191 (index.column() == KDirModel::Name) &&
192 (item.isDir() || m_dolphinView->isTabsForFilesEnabled());
193 if (validRequest) {
194 emit tabRequested(item.url());
195 }
196 }
197 }
198
199 void DolphinController::emitItemEntered(const QModelIndex& index)
200 {
201 KFileItem item = itemForIndex(index);
202 if (!item.isNull()) {
203 emit itemEntered(item);
204 }
205 }
206
207 void DolphinController::emitViewportEntered()
208 {
209 emit viewportEntered();
210 }
211
212 void DolphinController::updateMouseButtonState()
213 {
214 m_mouseButtons = QApplication::mouseButtons();
215 }
216
217 #include "dolphincontroller.moc"