]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincontroller.cpp
Now the IconManager does not depend anymore from Dolphin classes. If at least a secon...
[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 switch (m_itemView->iconSize().height()) {
61 case KIconLoader::SizeSmallMedium: m_zoomLevel = 0; break;
62 case KIconLoader::SizeMedium: m_zoomLevel = 1; break;
63 case KIconLoader::SizeLarge: m_zoomLevel = 2; break;
64 case KIconLoader::SizeHuge: m_zoomLevel = 3; break;
65 case KIconLoader::SizeEnormous: m_zoomLevel = 4; break;
66 case KIconLoader::SizeEnormous * 3 / 2: m_zoomLevel = 5; break;
67 case KIconLoader::SizeEnormous * 2: m_zoomLevel = 6; break;
68 default: Q_ASSERT(false); m_zoomLevel = 2; break;
69 }
70
71 // TODO: this is a workaround until Qt-issue 176832 has been fixed
72 connect(m_itemView, SIGNAL(pressed(const QModelIndex&)),
73 this, SLOT(updateOpenTabState()));
74 }
75 }
76
77 void DolphinController::triggerUrlChangeRequest(const KUrl& url)
78 {
79 if (m_url != url) {
80 emit requestUrlChange(url);
81 }
82 }
83
84 void DolphinController::triggerContextMenuRequest(const QPoint& pos)
85 {
86 emit activated();
87 emit requestContextMenu(pos);
88 }
89
90 void DolphinController::requestActivation()
91 {
92 emit activated();
93 }
94
95 void DolphinController::indicateDroppedUrls(const KUrl::List& urls,
96 const KUrl& destPath,
97 const KFileItem& destItem)
98 {
99 emit urlsDropped(urls, destPath, destItem);
100 }
101
102
103 void DolphinController::indicateSortingChange(DolphinView::Sorting sorting)
104 {
105 emit sortingChanged(sorting);
106 }
107
108 void DolphinController::indicateSortOrderChange(Qt::SortOrder order)
109 {
110 emit sortOrderChanged(order);
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::setZoomLevel(int level)
124 {
125 Q_ASSERT(level >= zoomLevelMinimum());
126 Q_ASSERT(level <= zoomLevelMaximum());
127 if (level != m_zoomLevel) {
128 m_zoomLevel = level;
129 emit zoomLevelChanged(m_zoomLevel);
130 }
131 }
132
133 int DolphinController::iconSizeForZoomLevel(int level)
134 {
135 int size = KIconLoader::SizeMedium;
136 switch (level) {
137 case 0: size = KIconLoader::SizeSmallMedium; break;
138 case 1: size = KIconLoader::SizeMedium; break;
139 case 2: size = KIconLoader::SizeLarge; break;
140 case 3: size = KIconLoader::SizeHuge; break;
141 case 4: size = KIconLoader::SizeEnormous; break;
142 case 5: size = KIconLoader::SizeEnormous * 3 / 2; break;
143 case 6: size = KIconLoader::SizeEnormous * 2; break;
144 default: Q_ASSERT(false); break;
145 }
146 return size;
147 }
148
149 void DolphinController::handleKeyPressEvent(QKeyEvent* event)
150 {
151 Q_ASSERT(m_itemView != 0);
152
153 const QItemSelectionModel* selModel = m_itemView->selectionModel();
154 const QModelIndex currentIndex = selModel->currentIndex();
155 const bool trigger = currentIndex.isValid()
156 && (event->key() == Qt::Key_Return)
157 && (selModel->selectedIndexes().count() > 0);
158 if (trigger) {
159 const QModelIndexList indexList = selModel->selectedIndexes();
160 foreach (const QModelIndex& index, indexList) {
161 triggerItem(index);
162 }
163 }
164 }
165
166 void DolphinController::replaceUrlByClipboard()
167 {
168 const QClipboard* clipboard = QApplication::clipboard();
169 QString text;
170 if (clipboard->mimeData(QClipboard::Selection)->hasText()) {
171 text = clipboard->mimeData(QClipboard::Selection)->text();
172 } else if (clipboard->mimeData(QClipboard::Clipboard)->hasText()) {
173 text = clipboard->mimeData(QClipboard::Clipboard)->text();
174 }
175 if (!text.isEmpty() && QDir::isAbsolutePath(text)) {
176 m_dolphinView->setUrl(KUrl(text));
177 }
178 }
179
180 KFileItem DolphinController::itemForIndex(const QModelIndex& index) const
181 {
182 Q_ASSERT(m_itemView != 0);
183
184 QAbstractProxyModel* proxyModel = static_cast<QAbstractProxyModel*>(m_itemView->model());
185 KDirModel* dirModel = static_cast<KDirModel*>(proxyModel->sourceModel());
186 const QModelIndex dirIndex = proxyModel->mapToSource(index);
187 return dirModel->itemForIndex(dirIndex);
188 }
189
190 void DolphinController::triggerItem(const QModelIndex& index)
191 {
192 const bool openTab = m_openTab;
193 m_openTab = false;
194
195 const KFileItem item = itemForIndex(index);
196 if (index.isValid() && (index.column() == KDirModel::Name)) {
197 if (openTab && (item.isDir() || m_dolphinView->isTabsForFilesEnabled())) {
198 emit tabRequested(item.url());
199 } else {
200 emit itemTriggered(item);
201 }
202 } else {
203 m_itemView->clearSelection();
204 if (!openTab) {
205 emit itemEntered(KFileItem());
206 }
207 }
208 }
209
210 void DolphinController::emitItemEntered(const QModelIndex& index)
211 {
212 KFileItem item = itemForIndex(index);
213 if (!item.isNull()) {
214 emit itemEntered(item);
215 }
216 }
217
218 void DolphinController::emitViewportEntered()
219 {
220 emit viewportEntered();
221 }
222
223 void DolphinController::updateOpenTabState()
224 {
225 m_openTab = QApplication::mouseButtons() & Qt::MidButton;
226 }
227
228 #include "dolphincontroller.moc"