]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphiniconsview.cpp
fixed regression because of disconnecting non-available slots - now files don't get...
[dolphin.git] / src / dolphiniconsview.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 "dolphiniconsview.h"
21
22 #include "dolphincategorydrawer.h"
23 #include "dolphincontroller.h"
24 #include "dolphinfileitemdelegate.h"
25 #include "dolphinsettings.h"
26 #include "dolphin_iconsmodesettings.h"
27 #include "dolphin_generalsettings.h"
28 #include "draganddrophelper.h"
29 #include "selectionmanager.h"
30
31 #include <kcategorizedsortfilterproxymodel.h>
32 #include <kdialog.h>
33 #include <kdirmodel.h>
34
35 #include <QAbstractProxyModel>
36 #include <QApplication>
37 #include <QPainter>
38 #include <QPoint>
39 #include <QScrollBar>
40
41 DolphinIconsView::DolphinIconsView(QWidget* parent, DolphinController* controller) :
42 KCategorizedView(parent),
43 m_controller(controller),
44 m_categoryDrawer(0),
45 m_font(),
46 m_decorationSize(),
47 m_decorationPosition(QStyleOptionViewItem::Top),
48 m_displayAlignment(Qt::AlignHCenter),
49 m_itemSize(),
50 m_dropRect()
51 {
52 Q_ASSERT(controller != 0);
53 setViewMode(QListView::IconMode);
54 setResizeMode(QListView::Adjust);
55 setSpacing(KDialog::spacingHint());
56 setMovement(QListView::Static);
57 setDragEnabled(true);
58 viewport()->setAcceptDrops(true);
59
60 setMouseTracking(true);
61
62 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
63 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
64 // necessary connecting the signal 'singleClick()' or 'doubleClick' and to handle the
65 // RETURN-key in keyPressEvent().
66 if (KGlobalSettings::singleClick()) {
67 connect(this, SIGNAL(clicked(const QModelIndex&)),
68 controller, SLOT(triggerItem(const QModelIndex&)));
69 if (DolphinSettings::instance().generalSettings()->showSelectionToggle()) {
70 SelectionManager* selManager = new SelectionManager(this);
71 connect(selManager, SIGNAL(selectionChanged()),
72 this, SLOT(requestActivation()));
73 connect(m_controller, SIGNAL(urlChanged(const KUrl&)),
74 selManager, SLOT(reset()));
75 }
76 } else {
77 connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
78 controller, SLOT(triggerItem(const QModelIndex&)));
79 }
80 connect(this, SIGNAL(entered(const QModelIndex&)),
81 controller, SLOT(emitItemEntered(const QModelIndex&)));
82 connect(this, SIGNAL(viewportEntered()),
83 controller, SLOT(emitViewportEntered()));
84 connect(controller, SIGNAL(zoomIn()),
85 this, SLOT(zoomIn()));
86 connect(controller, SIGNAL(zoomOut()),
87 this, SLOT(zoomOut()));
88
89 const DolphinView* view = controller->dolphinView();
90 connect(view, SIGNAL(showPreviewChanged()),
91 this, SLOT(slotShowPreviewChanged()));
92 connect(view, SIGNAL(additionalInfoChanged()),
93 this, SLOT(slotAdditionalInfoChanged()));
94
95 // apply the icons mode settings to the widget
96 const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
97 Q_ASSERT(settings != 0);
98
99 if (settings->useSystemFont()) {
100 m_font = KGlobalSettings::generalFont();
101 } else {
102 m_font = QFont(settings->fontFamily(),
103 settings->fontSize(),
104 settings->fontWeight(),
105 settings->italicFont());
106 }
107
108 setWordWrap(settings->numberOfTextlines() > 1);
109 updateGridSize(view->showPreview(), 0);
110
111 if (settings->arrangement() == QListView::TopToBottom) {
112 setFlow(QListView::LeftToRight);
113 m_decorationPosition = QStyleOptionViewItem::Top;
114 m_displayAlignment = Qt::AlignHCenter;
115 } else {
116 setFlow(QListView::TopToBottom);
117 m_decorationPosition = QStyleOptionViewItem::Left;
118 m_displayAlignment = Qt::AlignLeft | Qt::AlignVCenter;
119 }
120
121 m_categoryDrawer = new DolphinCategoryDrawer();
122 setCategoryDrawer(m_categoryDrawer);
123
124 setFocus();
125
126 connect(KGlobalSettings::self(), SIGNAL(kdisplayFontChanged()),
127 this, SLOT(updateFont()));
128 }
129
130 DolphinIconsView::~DolphinIconsView()
131 {
132 delete m_categoryDrawer;
133 m_categoryDrawer = 0;
134 }
135
136 QStyleOptionViewItem DolphinIconsView::viewOptions() const
137 {
138 QStyleOptionViewItem viewOptions = KCategorizedView::viewOptions();
139 viewOptions.font = m_font;
140 viewOptions.decorationPosition = m_decorationPosition;
141 viewOptions.decorationSize = m_decorationSize;
142 viewOptions.displayAlignment = m_displayAlignment;
143 viewOptions.showDecorationSelected = true;
144 return viewOptions;
145 }
146
147 void DolphinIconsView::contextMenuEvent(QContextMenuEvent* event)
148 {
149 KCategorizedView::contextMenuEvent(event);
150 m_controller->triggerContextMenuRequest(event->pos());
151 }
152
153 void DolphinIconsView::mousePressEvent(QMouseEvent* event)
154 {
155 m_controller->requestActivation();
156 if (!indexAt(event->pos()).isValid()) {
157 const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
158 if (!(modifier & Qt::ShiftModifier) && !(modifier & Qt::ControlModifier)) {
159 clearSelection();
160 }
161 }
162
163 KCategorizedView::mousePressEvent(event);
164 }
165
166 void DolphinIconsView::startDrag(Qt::DropActions supportedActions)
167 {
168 // TODO: invoking KCategorizedView::startDrag() should not be necessary, we'll
169 // fix this in KDE 4.1
170 KCategorizedView::startDrag(supportedActions);
171 DragAndDropHelper::startDrag(this, supportedActions);
172 }
173
174 void DolphinIconsView::dragEnterEvent(QDragEnterEvent* event)
175 {
176 if (event->mimeData()->hasUrls()) {
177 event->acceptProposedAction();
178 }
179 }
180
181 void DolphinIconsView::dragLeaveEvent(QDragLeaveEvent* event)
182 {
183 KCategorizedView::dragLeaveEvent(event);
184 setDirtyRegion(m_dropRect);
185 }
186
187 void DolphinIconsView::dragMoveEvent(QDragMoveEvent* event)
188 {
189 KCategorizedView::dragMoveEvent(event);
190
191 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
192 const QModelIndex index = indexAt(event->pos());
193 setDirtyRegion(m_dropRect);
194
195 m_dropRect.setSize(QSize()); // set as invalid
196 if (index.isValid()) {
197 const KFileItem item = m_controller->itemForIndex(index);
198 if (!item.isNull() && item.isDir()) {
199 m_dropRect = visualRect(index);
200 } else {
201 m_dropRect.setSize(QSize()); // set as invalid
202 }
203 }
204 if (event->mimeData()->hasUrls()) {
205 // accept url drops, independently from the destination item
206 event->acceptProposedAction();
207 }
208
209 setDirtyRegion(m_dropRect);
210 }
211
212 void DolphinIconsView::dropEvent(QDropEvent* event)
213 {
214 if (!selectionModel()->isSelected(indexAt(event->pos()))) {
215 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
216 if (!urls.isEmpty()) {
217 const QModelIndex index = indexAt(event->pos());
218 const KFileItem item = m_controller->itemForIndex(index);
219 m_controller->indicateDroppedUrls(urls,
220 m_controller->url(),
221 item);
222 event->acceptProposedAction();
223 }
224 }
225
226 KCategorizedView::dropEvent(event);
227 }
228
229 void DolphinIconsView::keyPressEvent(QKeyEvent* event)
230 {
231 KCategorizedView::keyPressEvent(event);
232 m_controller->handleKeyPressEvent(event);
233 }
234
235 void DolphinIconsView::wheelEvent(QWheelEvent* event)
236 {
237 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
238 if (event->modifiers() & Qt::ControlModifier) {
239 event->ignore();
240 return;
241 }
242 KCategorizedView::wheelEvent(event);
243 // if the icons are aligned left to right, the vertical wheel event should
244 // be applied to the horizontal scrollbar
245 const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
246 const bool scrollHorizontal = (event->orientation() == Qt::Vertical) &&
247 (settings->arrangement() == QListView::LeftToRight);
248 if (scrollHorizontal) {
249 QWheelEvent horizEvent(event->pos(),
250 event->delta(),
251 event->buttons(),
252 event->modifiers(),
253 Qt::Horizontal);
254 QApplication::sendEvent(horizontalScrollBar(), &horizEvent);
255 }
256 }
257
258 void DolphinIconsView::slotShowPreviewChanged()
259 {
260 const DolphinView* view = m_controller->dolphinView();
261 updateGridSize(view->showPreview(), additionalInfoCount());
262 }
263
264 void DolphinIconsView::slotAdditionalInfoChanged()
265 {
266 const DolphinView* view = m_controller->dolphinView();
267 const bool showPreview = view->showPreview();
268 updateGridSize(showPreview, view->additionalInfo().count());
269 }
270
271 void DolphinIconsView::zoomIn()
272 {
273 if (isZoomInPossible()) {
274 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
275
276 const int oldIconSize = settings->iconSize();
277 int newIconSize = oldIconSize;
278
279 const bool showPreview = m_controller->dolphinView()->showPreview();
280 if (showPreview) {
281 const int previewSize = increasedIconSize(settings->previewSize());
282 settings->setPreviewSize(previewSize);
283 } else {
284 newIconSize = increasedIconSize(oldIconSize);
285 settings->setIconSize(newIconSize);
286 }
287
288 // increase also the grid size
289 const int diff = newIconSize - oldIconSize;
290 settings->setItemWidth(settings->itemWidth() + diff);
291 settings->setItemHeight(settings->itemHeight() + diff);
292
293 updateGridSize(showPreview, additionalInfoCount());
294 }
295 }
296
297 void DolphinIconsView::zoomOut()
298 {
299 if (isZoomOutPossible()) {
300 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
301
302 const int oldIconSize = settings->iconSize();
303 int newIconSize = oldIconSize;
304
305 const bool showPreview = m_controller->dolphinView()->showPreview();
306 if (showPreview) {
307 const int previewSize = decreasedIconSize(settings->previewSize());
308 settings->setPreviewSize(previewSize);
309 } else {
310 newIconSize = decreasedIconSize(settings->iconSize());
311 settings->setIconSize(newIconSize);
312 }
313
314 // decrease also the grid size
315 const int diff = oldIconSize - newIconSize;
316 settings->setItemWidth(settings->itemWidth() - diff);
317 settings->setItemHeight(settings->itemHeight() - diff);
318
319 updateGridSize(showPreview, additionalInfoCount());
320 }
321 }
322
323 void DolphinIconsView::requestActivation()
324 {
325 m_controller->requestActivation();
326 }
327
328 void DolphinIconsView::updateFont()
329 {
330 const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
331 Q_ASSERT(settings != 0);
332
333 if (settings->useSystemFont()) {
334 m_font = KGlobalSettings::generalFont();
335 }
336 }
337
338 bool DolphinIconsView::isZoomInPossible() const
339 {
340 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
341 const bool showPreview = m_controller->dolphinView()->showPreview();
342 const int size = showPreview ? settings->previewSize() : settings->iconSize();
343 return size < KIconLoader::SizeEnormous;
344 }
345
346 bool DolphinIconsView::isZoomOutPossible() const
347 {
348 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
349 const bool showPreview = m_controller->dolphinView()->showPreview();
350 const int size = showPreview ? settings->previewSize() : settings->iconSize();
351 return size > KIconLoader::SizeSmall;
352 }
353
354 int DolphinIconsView::increasedIconSize(int size) const
355 {
356 int incSize = 0;
357 switch (size) {
358 case KIconLoader::SizeSmall: incSize = KIconLoader::SizeSmallMedium; break;
359 case KIconLoader::SizeSmallMedium: incSize = KIconLoader::SizeMedium; break;
360 case KIconLoader::SizeMedium: incSize = KIconLoader::SizeLarge; break;
361 case KIconLoader::SizeLarge: incSize = KIconLoader::SizeHuge; break;
362 case KIconLoader::SizeHuge: incSize = KIconLoader::SizeEnormous; break;
363 default: Q_ASSERT(false); break;
364 }
365 return incSize;
366 }
367
368 int DolphinIconsView::decreasedIconSize(int size) const
369 {
370 int decSize = 0;
371 switch (size) {
372 case KIconLoader::SizeSmallMedium: decSize = KIconLoader::SizeSmall; break;
373 case KIconLoader::SizeMedium: decSize = KIconLoader::SizeSmallMedium; break;
374 case KIconLoader::SizeLarge: decSize = KIconLoader::SizeMedium; break;
375 case KIconLoader::SizeHuge: decSize = KIconLoader::SizeLarge; break;
376 case KIconLoader::SizeEnormous: decSize = KIconLoader::SizeHuge; break;
377 default: Q_ASSERT(false); break;
378 }
379 return decSize;
380 }
381
382 void DolphinIconsView::updateGridSize(bool showPreview, int additionalInfoCount)
383 {
384 const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
385 Q_ASSERT(settings != 0);
386
387 int itemWidth = settings->itemWidth();
388 int itemHeight = settings->itemHeight();
389 int size = settings->iconSize();
390
391 if (showPreview) {
392 const int previewSize = settings->previewSize();
393 const int diff = previewSize - size;
394 itemWidth += diff;
395 itemHeight += diff;
396
397 size = previewSize;
398 }
399
400 Q_ASSERT(additionalInfoCount >= 0);
401 itemHeight += additionalInfoCount * m_font.pointSize() * 2;
402
403 if (settings->arrangement() == QListView::TopToBottom) {
404 // The decoration width indirectly defines the maximum
405 // width for the text wrapping. To use the maximum item width
406 // for text wrapping, it is used as decoration width.
407 m_decorationSize = QSize(itemWidth, size);
408 setIconSize(QSize(itemWidth, size));
409 } else {
410 m_decorationSize = QSize(size, size);
411 setIconSize(QSize(size, size));
412 }
413
414 m_itemSize = QSize(itemWidth, itemHeight);
415
416 const int spacing = settings->gridSpacing();
417 setGridSize(QSize(itemWidth + spacing * 2, itemHeight + spacing));
418
419 m_controller->setZoomInPossible(isZoomInPossible());
420 m_controller->setZoomOutPossible(isZoomOutPossible());
421
422 DolphinFileItemDelegate* delegate = qobject_cast<DolphinFileItemDelegate*>(itemDelegate());
423 if (delegate != 0) {
424 delegate->setMaximumSize(m_itemSize);
425 }
426 }
427
428 int DolphinIconsView::additionalInfoCount() const
429 {
430 const DolphinView* view = m_controller->dolphinView();
431 return view->additionalInfo().count();
432 }
433
434 #include "dolphiniconsview.moc"