]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphiniconsview.cpp
Restore filtering of items. The DolphinView just tells the controller about the filte...
[dolphin.git] / src / dolphiniconsview.cpp
1 /***************************************************************************
2 * Copyright (C) 2006-2009 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 "settings/dolphinsettings.h"
25 #include "dolphinsortfilterproxymodel.h"
26 #include "dolphinviewautoscroller.h"
27 #include "dolphin_iconsmodesettings.h"
28 #include "dolphin_generalsettings.h"
29 #include "draganddrophelper.h"
30 #include "selectionmanager.h"
31 #include "zoomlevelinfo.h"
32
33 #include <kcategorizedsortfilterproxymodel.h>
34 #include <kdialog.h>
35 #include <kdirmodel.h>
36 #include <kfileitemdelegate.h>
37
38 #include <QAbstractProxyModel>
39 #include <QApplication>
40 #include <QScrollBar>
41
42 DolphinIconsView::DolphinIconsView(QWidget* parent, DolphinController* controller) :
43 KCategorizedView(parent),
44 m_controller(controller),
45 m_selectionManager(0),
46 m_autoScroller(0),
47 m_categoryDrawer(0),
48 m_font(),
49 m_decorationSize(),
50 m_decorationPosition(QStyleOptionViewItem::Top),
51 m_displayAlignment(Qt::AlignHCenter),
52 m_itemSize(),
53 m_dropRect()
54 {
55 Q_ASSERT(controller != 0);
56 setLayoutDirection(Qt::LeftToRight);
57 setViewMode(QListView::IconMode);
58 setResizeMode(QListView::Adjust);
59 setMovement(QListView::Static);
60 setDragEnabled(true);
61 setEditTriggers(QAbstractItemView::NoEditTriggers);
62 viewport()->setAcceptDrops(true);
63
64 setMouseTracking(true);
65 m_autoScroller = new DolphinViewAutoScroller(this);
66
67 connect(this, SIGNAL(clicked(const QModelIndex&)),
68 controller, SLOT(requestTab(const QModelIndex&)));
69 if (KGlobalSettings::singleClick()) {
70 connect(this, SIGNAL(clicked(const QModelIndex&)),
71 controller, SLOT(triggerItem(const QModelIndex&)));
72 } else {
73 connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
74 controller, SLOT(triggerItem(const QModelIndex&)));
75 }
76
77 if (DolphinSettings::instance().generalSettings()->showSelectionToggle()) {
78 m_selectionManager = new SelectionManager(this);
79 connect(m_selectionManager, SIGNAL(selectionChanged()),
80 this, SLOT(requestActivation()));
81 connect(m_controller, SIGNAL(urlChanged(const KUrl&)),
82 m_selectionManager, SLOT(reset()));
83 }
84
85 connect(this, SIGNAL(entered(const QModelIndex&)),
86 controller, SLOT(emitItemEntered(const QModelIndex&)));
87 connect(this, SIGNAL(viewportEntered()),
88 controller, SLOT(emitViewportEntered()));
89 connect(controller, SIGNAL(nameFilterChanged(const QString&)),
90 this, SLOT(setNameFilter(const QString&)));
91 connect(controller, SIGNAL(zoomLevelChanged(int)),
92 this, SLOT(setZoomLevel(int)));
93
94 const DolphinView* view = controller->dolphinView();
95 connect(view, SIGNAL(showPreviewChanged()),
96 this, SLOT(slotShowPreviewChanged()));
97 connect(view, SIGNAL(additionalInfoChanged()),
98 this, SLOT(slotAdditionalInfoChanged()));
99
100 // apply the icons mode settings to the widget
101 const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
102 Q_ASSERT(settings != 0);
103
104 if (settings->useSystemFont()) {
105 m_font = KGlobalSettings::generalFont();
106 } else {
107 m_font = QFont(settings->fontFamily(),
108 settings->fontSize(),
109 settings->fontWeight(),
110 settings->italicFont());
111 }
112
113 setWordWrap(settings->numberOfTextlines() > 1);
114 updateGridSize(view->showPreview(), 0);
115
116 if (settings->arrangement() == QListView::TopToBottom) {
117 setFlow(QListView::LeftToRight);
118 m_decorationPosition = QStyleOptionViewItem::Top;
119 m_displayAlignment = Qt::AlignHCenter;
120 } else {
121 setFlow(QListView::TopToBottom);
122 m_decorationPosition = QStyleOptionViewItem::Left;
123 m_displayAlignment = Qt::AlignLeft | Qt::AlignVCenter;
124 }
125
126 m_categoryDrawer = new DolphinCategoryDrawer();
127 setCategoryDrawer(m_categoryDrawer);
128
129 setFocus();
130
131 connect(KGlobalSettings::self(), SIGNAL(settingsChanged(int)),
132 this, SLOT(slotGlobalSettingsChanged(int)));
133 }
134
135 DolphinIconsView::~DolphinIconsView()
136 {
137 delete m_categoryDrawer;
138 m_categoryDrawer = 0;
139 }
140
141 void DolphinIconsView::dataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight)
142 {
143 KCategorizedView::dataChanged(topLeft, bottomRight);
144
145 KCategorizedSortFilterProxyModel* proxyModel = dynamic_cast<KCategorizedSortFilterProxyModel*>(model());
146 if (!proxyModel->isCategorizedModel()) {
147 // bypass a QListView issue that items are not layout correctly if the decoration size of
148 // an index changes
149 scheduleDelayedItemsLayout();
150 }
151 }
152
153 QStyleOptionViewItem DolphinIconsView::viewOptions() const
154 {
155 QStyleOptionViewItem viewOptions = KCategorizedView::viewOptions();
156 viewOptions.font = m_font;
157 viewOptions.decorationPosition = m_decorationPosition;
158 viewOptions.decorationSize = m_decorationSize;
159 viewOptions.displayAlignment = m_displayAlignment;
160 viewOptions.showDecorationSelected = true;
161 return viewOptions;
162 }
163
164 void DolphinIconsView::contextMenuEvent(QContextMenuEvent* event)
165 {
166 KCategorizedView::contextMenuEvent(event);
167 m_controller->triggerContextMenuRequest(event->pos());
168 }
169
170 void DolphinIconsView::mousePressEvent(QMouseEvent* event)
171 {
172 m_controller->requestActivation();
173 const QModelIndex index = indexAt(event->pos());
174 if (index.isValid() && (event->button() == Qt::LeftButton)) {
175 // TODO: It should not be necessary to manually set the dragging state, but I could
176 // not reproduce this issue with a Qt-only example yet to find the root cause.
177 // Issue description: start Dolphin, split the view and drag an item from the
178 // inactive view to the active view by a very fast mouse movement. Result:
179 // the item gets selected instead of being dragged...
180 setState(QAbstractItemView::DraggingState);
181 }
182
183 if (!index.isValid()) {
184 if (QApplication::mouseButtons() & Qt::MidButton) {
185 m_controller->replaceUrlByClipboard();
186 }
187 const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
188 if (!(modifier & Qt::ShiftModifier) && !(modifier & Qt::ControlModifier)) {
189 clearSelection();
190 }
191 }
192
193 KCategorizedView::mousePressEvent(event);
194 }
195
196 void DolphinIconsView::startDrag(Qt::DropActions supportedActions)
197 {
198 DragAndDropHelper::instance().startDrag(this, supportedActions, m_controller);
199 }
200
201 void DolphinIconsView::dragEnterEvent(QDragEnterEvent* event)
202 {
203 if (DragAndDropHelper::instance().isMimeDataSupported(event->mimeData())) {
204 event->acceptProposedAction();
205 }
206 }
207
208 void DolphinIconsView::dragLeaveEvent(QDragLeaveEvent* event)
209 {
210 Q_UNUSED(event);
211 setDirtyRegion(m_dropRect);
212 }
213
214 void DolphinIconsView::dragMoveEvent(QDragMoveEvent* event)
215 {
216 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
217 const QModelIndex index = indexAt(event->pos());
218 setDirtyRegion(m_dropRect);
219
220 m_dropRect.setSize(QSize()); // set as invalid
221 if (index.isValid()) {
222 const KFileItem item = m_controller->itemForIndex(index);
223 if (!item.isNull() && item.isDir()) {
224 m_dropRect = visualRect(index);
225 } else {
226 m_dropRect.setSize(QSize()); // set as invalid
227 }
228 }
229 if (DragAndDropHelper::instance().isMimeDataSupported(event->mimeData())) {
230 // accept url drops, independently from the destination item
231 event->acceptProposedAction();
232 }
233
234 setDirtyRegion(m_dropRect);
235 }
236
237 void DolphinIconsView::dropEvent(QDropEvent* event)
238 {
239 const QModelIndex index = indexAt(event->pos());
240 const KFileItem item = m_controller->itemForIndex(index);
241 m_controller->indicateDroppedUrls(item, m_controller->url(), event);
242 }
243
244 QModelIndex DolphinIconsView::moveCursor(CursorAction cursorAction, Qt::KeyboardModifiers modifiers)
245 {
246 const QModelIndex oldCurrent = currentIndex();
247
248 QModelIndex newCurrent = KCategorizedView::moveCursor(cursorAction, modifiers);
249 if (newCurrent != oldCurrent) {
250 return newCurrent;
251 }
252
253 // The cursor has not been moved by the base implementation. Provide a
254 // wrap behavior, so that the cursor will go to the next item when reaching
255 // the border.
256 const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
257 if (settings->arrangement() == QListView::LeftToRight) {
258 switch (cursorAction) {
259 case MoveUp:
260 if (newCurrent.row() == 0) {
261 return newCurrent;
262 }
263 newCurrent = KCategorizedView::moveCursor(MoveLeft, modifiers);
264 selectionModel()->setCurrentIndex(newCurrent, QItemSelectionModel::NoUpdate);
265 newCurrent = KCategorizedView::moveCursor(MovePageDown, modifiers);
266 break;
267
268 case MoveDown:
269 if (newCurrent.row() == (model()->rowCount() - 1)) {
270 return newCurrent;
271 }
272 newCurrent = KCategorizedView::moveCursor(MovePageUp, modifiers);
273 selectionModel()->setCurrentIndex(newCurrent, QItemSelectionModel::NoUpdate);
274 newCurrent = KCategorizedView::moveCursor(MoveRight, modifiers);
275 break;
276
277 default:
278 break;
279 }
280 } else {
281 QModelIndex current = oldCurrent;
282 switch (cursorAction) {
283 case MoveLeft:
284 if (newCurrent.row() == 0) {
285 return newCurrent;
286 }
287 newCurrent = KCategorizedView::moveCursor(MoveUp, modifiers);
288 do {
289 selectionModel()->setCurrentIndex(newCurrent, QItemSelectionModel::NoUpdate);
290 current = newCurrent;
291 newCurrent = KCategorizedView::moveCursor(MoveRight, modifiers);
292 } while (newCurrent != current);
293 break;
294
295 case MoveRight:
296 if (newCurrent.row() == (model()->rowCount() - 1)) {
297 return newCurrent;
298 }
299 do {
300 selectionModel()->setCurrentIndex(newCurrent, QItemSelectionModel::NoUpdate);
301 current = newCurrent;
302 newCurrent = KCategorizedView::moveCursor(MoveLeft, modifiers);
303 } while (newCurrent != current);
304 newCurrent = KCategorizedView::moveCursor(MoveDown, modifiers);
305 break;
306
307 default:
308 break;
309 }
310 }
311
312 // Revert all changes of the current item to make sure that item selection works correctly
313 selectionModel()->setCurrentIndex(oldCurrent, QItemSelectionModel::NoUpdate);
314 return newCurrent;
315 }
316
317 void DolphinIconsView::keyPressEvent(QKeyEvent* event)
318 {
319 KCategorizedView::keyPressEvent(event);
320 m_controller->handleKeyPressEvent(event);
321 }
322
323 void DolphinIconsView::wheelEvent(QWheelEvent* event)
324 {
325 if (m_selectionManager != 0) {
326 m_selectionManager->reset();
327 }
328
329 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
330 if (event->modifiers() & Qt::ControlModifier) {
331 event->ignore();
332 return;
333 }
334
335 horizontalScrollBar()->setSingleStep(m_itemSize.width() / 10);
336 verticalScrollBar()->setSingleStep(m_itemSize.height() / 10);
337
338 KCategorizedView::wheelEvent(event);
339 // if the icons are aligned left to right, the vertical wheel event should
340 // be applied to the horizontal scrollbar
341 const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
342 const bool scrollHorizontal = (event->orientation() == Qt::Vertical) &&
343 (settings->arrangement() == QListView::LeftToRight);
344 if (scrollHorizontal) {
345 QWheelEvent horizEvent(event->pos(),
346 event->delta(),
347 event->buttons(),
348 event->modifiers(),
349 Qt::Horizontal);
350 QApplication::sendEvent(horizontalScrollBar(), &horizEvent);
351 }
352 }
353
354 void DolphinIconsView::showEvent(QShowEvent* event)
355 {
356 KFileItemDelegate* delegate = dynamic_cast<KFileItemDelegate*>(itemDelegate());
357 delegate->setMaximumSize(m_itemSize);
358
359 KCategorizedView::showEvent(event);
360 }
361
362 void DolphinIconsView::leaveEvent(QEvent* event)
363 {
364 KCategorizedView::leaveEvent(event);
365 // if the mouse is above an item and moved very fast outside the widget,
366 // no viewportEntered() signal might be emitted although the mouse has been moved
367 // above the viewport
368 m_controller->emitViewportEntered();
369 }
370
371 void DolphinIconsView::currentChanged(const QModelIndex& current, const QModelIndex& previous)
372 {
373 KCategorizedView::currentChanged(current, previous);
374 m_autoScroller->handleCurrentIndexChange(current, previous);
375 }
376
377 void DolphinIconsView::resizeEvent(QResizeEvent* event)
378 {
379 KCategorizedView::resizeEvent(event);
380 const DolphinView* view = m_controller->dolphinView();
381 updateGridSize(view->showPreview(), view->additionalInfo().count());
382 }
383
384 void DolphinIconsView::slotShowPreviewChanged()
385 {
386 const DolphinView* view = m_controller->dolphinView();
387 updateGridSize(view->showPreview(), additionalInfoCount());
388 }
389
390 void DolphinIconsView::slotAdditionalInfoChanged()
391 {
392 const DolphinView* view = m_controller->dolphinView();
393 const bool showPreview = view->showPreview();
394 updateGridSize(showPreview, view->additionalInfo().count());
395 }
396
397 void DolphinIconsView::setNameFilter(const QString& nameFilter)
398 {
399 DolphinSortFilterProxyModel* proxyModel = static_cast<DolphinSortFilterProxyModel*>(model());
400 proxyModel->setFilterRegExp(nameFilter);
401 }
402
403 void DolphinIconsView::setZoomLevel(int level)
404 {
405 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
406
407 const int oldIconSize = settings->iconSize();
408 int newIconSize = oldIconSize;
409
410 const bool showPreview = m_controller->dolphinView()->showPreview();
411 if (showPreview) {
412 const int previewSize = ZoomLevelInfo::iconSizeForZoomLevel(level);
413 settings->setPreviewSize(previewSize);
414 } else {
415 newIconSize = ZoomLevelInfo::iconSizeForZoomLevel(level);
416 settings->setIconSize(newIconSize);
417 }
418
419 // increase also the grid size
420 const int diff = newIconSize - oldIconSize;
421 settings->setItemWidth(settings->itemWidth() + diff);
422 settings->setItemHeight(settings->itemHeight() + diff);
423
424 updateGridSize(showPreview, additionalInfoCount());
425 }
426
427 void DolphinIconsView::requestActivation()
428 {
429 m_controller->requestActivation();
430 }
431
432 void DolphinIconsView::slotGlobalSettingsChanged(int category)
433 {
434 Q_UNUSED(category);
435
436 const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
437 Q_ASSERT(settings != 0);
438 if (settings->useSystemFont()) {
439 m_font = KGlobalSettings::generalFont();
440 }
441
442 disconnect(this, SIGNAL(clicked(QModelIndex)), m_controller, SLOT(triggerItem(QModelIndex)));
443 disconnect(this, SIGNAL(doubleClicked(QModelIndex)), m_controller, SLOT(triggerItem(QModelIndex)));
444 if (KGlobalSettings::singleClick()) {
445 connect(this, SIGNAL(clicked(QModelIndex)), m_controller, SLOT(triggerItem(QModelIndex)));
446 } else {
447 connect(this, SIGNAL(doubleClicked(QModelIndex)), m_controller, SLOT(triggerItem(QModelIndex)));
448 }
449 }
450
451 void DolphinIconsView::updateGridSize(bool showPreview, int additionalInfoCount)
452 {
453 const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
454 Q_ASSERT(settings != 0);
455
456 int itemWidth = settings->itemWidth();
457 int itemHeight = settings->itemHeight();
458 int size = settings->iconSize();
459
460 if (showPreview) {
461 const int previewSize = settings->previewSize();
462 const int diff = previewSize - size;
463 itemWidth += diff;
464 itemHeight += diff;
465
466 size = previewSize;
467 }
468
469 Q_ASSERT(additionalInfoCount >= 0);
470 itemHeight += additionalInfoCount * m_font.pointSize() * 2;
471
472 // Optimize the item size of the grid in a way to prevent large gaps on the
473 // right border (= row arrangement) or the bottom border (= column arrangement).
474 // There is no public API in QListView to find out the used width of the viewport
475 // for the layout. The following calculation of 'contentWidth'/'contentHeight'
476 // is based on QListViewPrivate::prepareItemsLayout() (Copyright (C) 2009 Nokia Corporation).
477 int frameAroundContents = 0;
478 if (style()->styleHint(QStyle::SH_ScrollView_FrameOnlyAroundContents)) {
479 frameAroundContents = style()->pixelMetric(QStyle::PM_DefaultFrameWidth) * 2;
480 }
481 const int spacing = settings->gridSpacing();
482 if (settings->arrangement() == QListView::TopToBottom) {
483 const int contentWidth = viewport()->width() - 1
484 - frameAroundContents
485 - style()->pixelMetric(QStyle::PM_ScrollBarExtent, 0, horizontalScrollBar());
486 const int gridWidth = itemWidth + spacing * 2;
487 const int horizItemCount = contentWidth / gridWidth;
488 if (horizItemCount > 0) {
489 itemWidth += (contentWidth - horizItemCount * gridWidth) / horizItemCount;
490 }
491
492 // The decoration width indirectly defines the maximum
493 // width for the text wrapping. To use the maximum item width
494 // for text wrapping, it is used as decoration width.
495 m_decorationSize = QSize(itemWidth, size);
496 setIconSize(QSize(itemWidth, size));
497 } else {
498 const int contentHeight = viewport()->height() - 1
499 - frameAroundContents
500 - style()->pixelMetric(QStyle::PM_ScrollBarExtent, 0, verticalScrollBar());
501 const int gridHeight = itemHeight + spacing;
502 const int vertItemCount = contentHeight / gridHeight;
503 if (vertItemCount > 0) {
504 itemHeight += (contentHeight - vertItemCount * gridHeight) / vertItemCount;
505 }
506
507 m_decorationSize = QSize(size, size);
508 setIconSize(QSize(size, size));
509 }
510
511 m_itemSize = QSize(itemWidth, itemHeight);
512 setGridSizeOwn(QSize(itemWidth + spacing * 2, itemHeight + spacing));
513
514 KFileItemDelegate* delegate = dynamic_cast<KFileItemDelegate*>(itemDelegate());
515 if (delegate != 0) {
516 delegate->setMaximumSize(m_itemSize);
517 }
518
519 if (m_selectionManager != 0) {
520 m_selectionManager->reset();
521 }
522 }
523
524 int DolphinIconsView::additionalInfoCount() const
525 {
526 const DolphinView* view = m_controller->dolphinView();
527 return view->additionalInfo().count();
528 }
529
530 #include "dolphiniconsview.moc"