]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincontroller.cpp
assure correct update of the enabled/disabled state of the zoom actions (no backport...
[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
22 #include <kdirmodel.h>
23 #include <QAbstractProxyModel>
24 #include <QApplication>
25 #include <QClipboard>
26 #include <QDir>
27
28 DolphinController::DolphinController(DolphinView* dolphinView) :
29 QObject(dolphinView),
30 m_zoomLevel(0),
31 m_openTab(false),
32 m_url(),
33 m_dolphinView(dolphinView),
34 m_itemView(0)
35 {
36 }
37
38 DolphinController::~DolphinController()
39 {
40 }
41
42 void DolphinController::setUrl(const KUrl& url)
43 {
44 if (m_url != url) {
45 m_url = url;
46 emit urlChanged(url);
47 }
48 }
49
50 void DolphinController::setItemView(QAbstractItemView* view)
51 {
52 if (m_itemView != 0) {
53 disconnect(m_itemView, SIGNAL(pressed(const QModelIndex&)),
54 this, SLOT(updateOpenTabState()));
55 }
56
57 m_itemView = view;
58
59 if (m_itemView != 0) {
60 m_zoomLevel = zoomLevelForIconSize(m_itemView->iconSize());
61
62 // TODO: this is a workaround until Qt-issue 176832 has been fixed
63 connect(m_itemView, SIGNAL(pressed(const QModelIndex&)),
64 this, SLOT(updateOpenTabState()));
65 }
66 }
67
68 void DolphinController::triggerUrlChangeRequest(const KUrl& url)
69 {
70 if (m_url != url) {
71 emit requestUrlChange(url);
72 }
73 }
74
75 void DolphinController::triggerContextMenuRequest(const QPoint& pos)
76 {
77 emit activated();
78 emit requestContextMenu(pos);
79 }
80
81 void DolphinController::requestActivation()
82 {
83 emit activated();
84 }
85
86 void DolphinController::indicateDroppedUrls(const KUrl::List& urls,
87 const KUrl& destPath,
88 const KFileItem& destItem)
89 {
90 emit urlsDropped(urls, destPath, destItem);
91 }
92
93
94 void DolphinController::indicateSortingChange(DolphinView::Sorting sorting)
95 {
96 emit sortingChanged(sorting);
97 }
98
99 void DolphinController::indicateSortOrderChange(Qt::SortOrder order)
100 {
101 emit sortOrderChanged(order);
102 }
103
104 void DolphinController::indicateAdditionalInfoChange(const KFileItemDelegate::InformationList& info)
105 {
106 emit additionalInfoChanged(info);
107 }
108
109 void DolphinController::indicateActivationChange(bool active)
110 {
111 emit activationChanged(active);
112 }
113
114 void DolphinController::setZoomLevel(int level)
115 {
116 Q_ASSERT(level >= zoomLevelMinimum());
117 Q_ASSERT(level <= zoomLevelMaximum());
118 if (level != m_zoomLevel) {
119 m_zoomLevel = level;
120 emit zoomLevelChanged(m_zoomLevel);
121 }
122 }
123
124 int DolphinController::iconSizeForZoomLevel(int level)
125 {
126 int size = KIconLoader::SizeMedium;
127 switch (level) {
128 case 0: size = KIconLoader::SizeSmall; break;
129 case 1: size = KIconLoader::SizeSmallMedium; break;
130 case 2: size = KIconLoader::SizeMedium; break;
131 case 3: size = KIconLoader::SizeLarge; break;
132 case 4: size = KIconLoader::SizeHuge; break;
133 case 5: size = KIconLoader::SizeEnormous; break;
134 case 6: size = KIconLoader::SizeEnormous * 3 / 2; break;
135 case 7: size = KIconLoader::SizeEnormous * 2; break;
136 default: Q_ASSERT(false); break;
137 }
138 return size;
139 }
140
141 int DolphinController::zoomLevelForIconSize(const QSize& size)
142 {
143 int level = 0;
144 switch (size.height()) {
145 case KIconLoader::SizeSmall: level = 0; break;
146 case KIconLoader::SizeSmallMedium: level = 1; break;
147 case KIconLoader::SizeMedium: level = 2; break;
148 case KIconLoader::SizeLarge: level = 3; break;
149 case KIconLoader::SizeHuge: level = 4; break;
150 case KIconLoader::SizeEnormous: level = 5; break;
151 case KIconLoader::SizeEnormous * 3 / 2: level = 6; break;
152 case KIconLoader::SizeEnormous * 2: level = 7; break;
153 default: Q_ASSERT(false); level = 3; break;
154 }
155 return level;
156 }
157
158 void DolphinController::handleKeyPressEvent(QKeyEvent* event)
159 {
160 Q_ASSERT(m_itemView != 0);
161
162 const QItemSelectionModel* selModel = m_itemView->selectionModel();
163 const QModelIndex currentIndex = selModel->currentIndex();
164 const bool trigger = currentIndex.isValid()
165 && (event->key() == Qt::Key_Return)
166 && (selModel->selectedIndexes().count() > 0);
167 if (trigger) {
168 const QModelIndexList indexList = selModel->selectedIndexes();
169 foreach (const QModelIndex& index, indexList) {
170 triggerItem(index);
171 }
172 }
173 }
174
175 void DolphinController::replaceUrlByClipboard()
176 {
177 const QClipboard* clipboard = QApplication::clipboard();
178 QString text;
179 if (clipboard->mimeData(QClipboard::Selection)->hasText()) {
180 text = clipboard->mimeData(QClipboard::Selection)->text();
181 } else if (clipboard->mimeData(QClipboard::Clipboard)->hasText()) {
182 text = clipboard->mimeData(QClipboard::Clipboard)->text();
183 }
184 if (!text.isEmpty() && QDir::isAbsolutePath(text)) {
185 m_dolphinView->setUrl(KUrl(text));
186 }
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 const bool openTab = m_openTab;
202 m_openTab = false;
203
204 const KFileItem item = itemForIndex(index);
205 if (index.isValid() && (index.column() == KDirModel::Name)) {
206 if (openTab && (item.isDir() || m_dolphinView->isTabsForFilesEnabled())) {
207 emit tabRequested(item.url());
208 } else {
209 emit itemTriggered(item);
210 }
211 } else {
212 m_itemView->clearSelection();
213 if (!openTab) {
214 emit itemEntered(KFileItem());
215 }
216 }
217 }
218
219 void DolphinController::emitItemEntered(const QModelIndex& index)
220 {
221 KFileItem item = itemForIndex(index);
222 if (!item.isNull()) {
223 emit itemEntered(item);
224 }
225 }
226
227 void DolphinController::emitViewportEntered()
228 {
229 emit viewportEntered();
230 }
231
232 void DolphinController::updateOpenTabState()
233 {
234 m_openTab = QApplication::mouseButtons() & Qt::MidButton;
235 }
236
237 #include "dolphincontroller.moc"