]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistcontroller.cpp
When Esc is pressed, clear the selection and cancel the keyboard search
[dolphin.git] / src / kitemviews / kitemlistcontroller.cpp
1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
3 * Copyright (C) 2012 by Frank Reininghaus <frank78ac@googlemail.com> *
4 * *
5 * Based on the Itemviews NG project from Trolltech Labs: *
6 * http://qt.gitorious.org/qt-labs/itemviews-ng *
7 * *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
12 * *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, write to the *
20 * Free Software Foundation, Inc., *
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
22 ***************************************************************************/
23
24 #include "kitemlistcontroller.h"
25
26 #include <KGlobalSettings>
27 #include <KDebug>
28
29 #include "kitemlistview.h"
30 #include "kitemlistselectionmanager.h"
31
32 #include "private/kitemlistrubberband.h"
33 #include "private/kitemlistkeyboardsearchmanager.h"
34
35 #include <QApplication>
36 #include <QDrag>
37 #include <QEvent>
38 #include <QGraphicsScene>
39 #include <QGraphicsSceneEvent>
40 #include <QGraphicsView>
41 #include <QMimeData>
42 #include <QTimer>
43
44 KItemListController::KItemListController(KItemModelBase* model, KItemListView* view, QObject* parent) :
45 QObject(parent),
46 m_singleClickActivation(KGlobalSettings::singleClick()),
47 m_selectionTogglePressed(false),
48 m_clearSelectionIfItemsAreNotDragged(false),
49 m_selectionBehavior(NoSelection),
50 m_model(0),
51 m_view(0),
52 m_selectionManager(new KItemListSelectionManager(this)),
53 m_keyboardManager(new KItemListKeyboardSearchManager(this)),
54 m_pressedIndex(-1),
55 m_pressedMousePos(),
56 m_autoActivationTimer(0),
57 m_oldSelection(),
58 m_keyboardAnchorIndex(-1),
59 m_keyboardAnchorPos(0)
60 {
61 connect(m_keyboardManager, SIGNAL(changeCurrentItem(QString,bool)),
62 this, SLOT(slotChangeCurrentItem(QString,bool)));
63
64 m_autoActivationTimer = new QTimer(this);
65 m_autoActivationTimer->setSingleShot(true);
66 m_autoActivationTimer->setInterval(-1);
67 connect(m_autoActivationTimer, SIGNAL(timeout()), this, SLOT(slotAutoActivationTimeout()));
68
69 setModel(model);
70 setView(view);
71 }
72
73 KItemListController::~KItemListController()
74 {
75 setView(0);
76 delete m_view;
77 m_view = 0;
78
79 setModel(0);
80 delete m_model;
81 m_model = 0;
82 }
83
84 void KItemListController::setModel(KItemModelBase* model)
85 {
86 if (m_model == model) {
87 return;
88 }
89
90 KItemModelBase* oldModel = m_model;
91 m_model = model;
92 if (m_model) {
93 m_model->setParent(this);
94 }
95
96 if (m_view) {
97 m_view->setModel(m_model);
98 }
99
100 m_selectionManager->setModel(m_model);
101
102 emit modelChanged(m_model, oldModel);
103 }
104
105 KItemModelBase* KItemListController::model() const
106 {
107 return m_model;
108 }
109
110 KItemListSelectionManager* KItemListController::selectionManager() const
111 {
112 return m_selectionManager;
113 }
114
115 void KItemListController::setView(KItemListView* view)
116 {
117 if (m_view == view) {
118 return;
119 }
120
121 KItemListView* oldView = m_view;
122 if (oldView) {
123 disconnect(oldView, SIGNAL(scrollOffsetChanged(qreal,qreal)), this, SLOT(slotViewScrollOffsetChanged(qreal,qreal)));
124 }
125
126 m_view = view;
127
128 if (m_view) {
129 m_view->setController(this);
130 m_view->setModel(m_model);
131 connect(m_view, SIGNAL(scrollOffsetChanged(qreal,qreal)), this, SLOT(slotViewScrollOffsetChanged(qreal,qreal)));
132 updateExtendedSelectionRegion();
133 }
134
135 emit viewChanged(m_view, oldView);
136 }
137
138 KItemListView* KItemListController::view() const
139 {
140 return m_view;
141 }
142
143 void KItemListController::setSelectionBehavior(SelectionBehavior behavior)
144 {
145 m_selectionBehavior = behavior;
146 updateExtendedSelectionRegion();
147 }
148
149 KItemListController::SelectionBehavior KItemListController::selectionBehavior() const
150 {
151 return m_selectionBehavior;
152 }
153
154 void KItemListController::setAutoActivationDelay(int delay)
155 {
156 m_autoActivationTimer->setInterval(delay);
157 }
158
159 int KItemListController::autoActivationDelay() const
160 {
161 return m_autoActivationTimer->interval();
162 }
163
164 void KItemListController::setSingleClickActivation(bool singleClick)
165 {
166 m_singleClickActivation = singleClick;
167 }
168
169 bool KItemListController::singleClickActivation() const
170 {
171 return m_singleClickActivation;
172 }
173
174 bool KItemListController::showEvent(QShowEvent* event)
175 {
176 Q_UNUSED(event);
177 return false;
178 }
179
180 bool KItemListController::hideEvent(QHideEvent* event)
181 {
182 Q_UNUSED(event);
183 return false;
184 }
185
186 bool KItemListController::keyPressEvent(QKeyEvent* event)
187 {
188 int index = m_selectionManager->currentItem();
189 int key = event->key();
190
191 // Handle the expanding/collapsing of items
192 if (m_view->supportsItemExpanding() && m_model->isExpandable(index)) {
193 if (key == Qt::Key_Right) {
194 if (m_model->setExpanded(index, true)) {
195 return true;
196 }
197 } else if (key == Qt::Key_Left) {
198 if (m_model->setExpanded(index, false)) {
199 return true;
200 }
201 }
202 }
203
204 const bool shiftPressed = event->modifiers() & Qt::ShiftModifier;
205 const bool controlPressed = event->modifiers() & Qt::ControlModifier;
206 const bool shiftOrControlPressed = shiftPressed || controlPressed;
207
208 const int itemCount = m_model->count();
209
210 // For horizontal scroll orientation, transform
211 // the arrow keys to simplify the event handling.
212 if (m_view->scrollOrientation() == Qt::Horizontal) {
213 switch (key) {
214 case Qt::Key_Up: key = Qt::Key_Left; break;
215 case Qt::Key_Down: key = Qt::Key_Right; break;
216 case Qt::Key_Left: key = Qt::Key_Up; break;
217 case Qt::Key_Right: key = Qt::Key_Down; break;
218 default: break;
219 }
220 }
221
222 const bool selectSingleItem = m_selectionBehavior != NoSelection &&
223 itemCount == 1 &&
224 (key == Qt::Key_Home || key == Qt::Key_End ||
225 key == Qt::Key_Up || key == Qt::Key_Down ||
226 key == Qt::Key_Left || key == Qt::Key_Right);
227 if (selectSingleItem) {
228 const int current = m_selectionManager->currentItem();
229 m_selectionManager->setSelected(current);
230 return true;
231 }
232
233 switch (key) {
234 case Qt::Key_Home:
235 index = 0;
236 m_keyboardAnchorIndex = index;
237 m_keyboardAnchorPos = keyboardAnchorPos(index);
238 break;
239
240 case Qt::Key_End:
241 index = itemCount - 1;
242 m_keyboardAnchorIndex = index;
243 m_keyboardAnchorPos = keyboardAnchorPos(index);
244 break;
245
246 case Qt::Key_Left:
247 if (index > 0) {
248 --index;
249 m_keyboardAnchorIndex = index;
250 m_keyboardAnchorPos = keyboardAnchorPos(index);
251 }
252 break;
253
254 case Qt::Key_Right:
255 if (index < itemCount - 1) {
256 ++index;
257 m_keyboardAnchorIndex = index;
258 m_keyboardAnchorPos = keyboardAnchorPos(index);
259 }
260 break;
261
262 case Qt::Key_Up:
263 updateKeyboardAnchor();
264 index = previousRowIndex(index);
265 break;
266
267 case Qt::Key_Down:
268 updateKeyboardAnchor();
269 index = nextRowIndex(index);
270 break;
271
272 case Qt::Key_PageUp:
273 if (m_view->scrollOrientation() == Qt::Horizontal) {
274 // The new current index should correspond to the first item in the current column.
275 int newIndex = qMax(index - 1, 0);
276 while (newIndex != index && m_view->itemRect(newIndex).topLeft().y() < m_view->itemRect(index).topLeft().y()) {
277 index = newIndex;
278 newIndex = qMax(index - 1, 0);
279 }
280 m_keyboardAnchorIndex = index;
281 m_keyboardAnchorPos = keyboardAnchorPos(index);
282 } else {
283 const qreal currentItemBottom = m_view->itemRect(index).bottomLeft().y();
284 const qreal height = m_view->geometry().height();
285
286 // The new current item should be the first item in the current
287 // column whose itemRect's top coordinate is larger than targetY.
288 const qreal targetY = currentItemBottom - height;
289
290 updateKeyboardAnchor();
291 int newIndex = previousRowIndex(index);
292 do {
293 index = newIndex;
294 updateKeyboardAnchor();
295 newIndex = previousRowIndex(index);
296 } while (m_view->itemRect(newIndex).topLeft().y() > targetY && newIndex != index);
297 }
298 break;
299
300 case Qt::Key_PageDown:
301 if (m_view->scrollOrientation() == Qt::Horizontal) {
302 // The new current index should correspond to the last item in the current column.
303 int newIndex = qMin(index + 1, m_model->count() - 1);
304 while (newIndex != index && m_view->itemRect(newIndex).topLeft().y() > m_view->itemRect(index).topLeft().y()) {
305 index = newIndex;
306 newIndex = qMin(index + 1, m_model->count() - 1);
307 }
308 m_keyboardAnchorIndex = index;
309 m_keyboardAnchorPos = keyboardAnchorPos(index);
310 } else {
311 const qreal currentItemTop = m_view->itemRect(index).topLeft().y();
312 const qreal height = m_view->geometry().height();
313
314 // The new current item should be the last item in the current
315 // column whose itemRect's bottom coordinate is smaller than targetY.
316 const qreal targetY = currentItemTop + height;
317
318 updateKeyboardAnchor();
319 int newIndex = nextRowIndex(index);
320 do {
321 index = newIndex;
322 updateKeyboardAnchor();
323 newIndex = nextRowIndex(index);
324 } while (m_view->itemRect(newIndex).bottomLeft().y() < targetY && newIndex != index);
325 }
326 break;
327
328 case Qt::Key_Enter:
329 case Qt::Key_Return: {
330 const QSet<int> selectedItems = m_selectionManager->selectedItems();
331 if (selectedItems.count() >= 2) {
332 emit itemsActivated(selectedItems);
333 } else if (selectedItems.count() == 1) {
334 emit itemActivated(selectedItems.toList().first());
335 } else {
336 emit itemActivated(index);
337 }
338 break;
339 }
340
341 case Qt::Key_Space:
342 if (m_selectionBehavior == MultiSelection) {
343 if (controlPressed) {
344 m_selectionManager->endAnchoredSelection();
345 m_selectionManager->setSelected(index, 1, KItemListSelectionManager::Toggle);
346 m_selectionManager->beginAnchoredSelection(index);
347 } else {
348 const int current = m_selectionManager->currentItem();
349 m_selectionManager->setSelected(current);
350 }
351 }
352 break;
353
354 case Qt::Key_Menu: {
355 // Emit the signal itemContextMenuRequested() in case if at least one
356 // item is selected. Otherwise the signal viewContextMenuRequested() will be emitted.
357 const QSet<int> selectedItems = m_selectionManager->selectedItems();
358 int index = -1;
359 if (selectedItems.count() >= 2) {
360 const int currentItemIndex = m_selectionManager->currentItem();
361 index = selectedItems.contains(currentItemIndex)
362 ? currentItemIndex : selectedItems.toList().first();
363 } else if (selectedItems.count() == 1) {
364 index = selectedItems.toList().first();
365 }
366
367 if (index >= 0) {
368 const QRectF contextRect = m_view->itemContextRect(index);
369 const QPointF pos(m_view->scene()->views().first()->mapToGlobal(contextRect.bottomRight().toPoint()));
370 emit itemContextMenuRequested(index, pos);
371 } else {
372 emit viewContextMenuRequested(QCursor::pos());
373 }
374 break;
375 }
376
377 case Qt::Key_Escape:
378 if (m_selectionBehavior != SingleSelection) {
379 m_selectionManager->clearSelection();
380 }
381 m_keyboardManager->cancelSearch();
382 break;
383
384 default:
385 m_keyboardManager->addKeys(event->text());
386 return false;
387 }
388
389 if (m_selectionManager->currentItem() != index) {
390 switch (m_selectionBehavior) {
391 case NoSelection:
392 m_selectionManager->setCurrentItem(index);
393 break;
394
395 case SingleSelection:
396 m_selectionManager->setCurrentItem(index);
397 m_selectionManager->clearSelection();
398 m_selectionManager->setSelected(index, 1);
399 break;
400
401 case MultiSelection:
402 if (controlPressed) {
403 m_selectionManager->endAnchoredSelection();
404 }
405
406 m_selectionManager->setCurrentItem(index);
407
408 if (!shiftOrControlPressed) {
409 m_selectionManager->clearSelection();
410 m_selectionManager->setSelected(index, 1);
411 }
412
413 if (!shiftPressed) {
414 m_selectionManager->beginAnchoredSelection(index);
415 }
416 break;
417 }
418
419 m_view->scrollToItem(index);
420 }
421 return true;
422 }
423
424 void KItemListController::slotChangeCurrentItem(const QString& text, bool searchFromNextItem)
425 {
426 if (!m_model || m_model->count() == 0) {
427 return;
428 }
429 const int currentIndex = m_selectionManager->currentItem();
430 int index;
431 if (searchFromNextItem) {
432 index = m_model->indexForKeyboardSearch(text, (currentIndex + 1) % m_model->count());
433 } else {
434 index = m_model->indexForKeyboardSearch(text, currentIndex);
435 }
436 if (index >= 0) {
437 m_selectionManager->setCurrentItem(index);
438 m_selectionManager->clearSelection();
439 m_selectionManager->setSelected(index, 1);
440 m_selectionManager->beginAnchoredSelection(index);
441 m_view->scrollToItem(index);
442 }
443 }
444
445 void KItemListController::slotAutoActivationTimeout()
446 {
447 if (!m_model || !m_view) {
448 return;
449 }
450
451 const int index = m_autoActivationTimer->property("index").toInt();
452 if (index < 0 || index >= m_model->count()) {
453 return;
454 }
455
456 if (m_model->supportsDropping(index)) {
457 if (m_view->supportsItemExpanding() && m_model->isExpandable(index)) {
458 const bool expanded = m_model->isExpanded(index);
459 m_model->setExpanded(index, !expanded);
460 } else {
461 emit itemActivated(index);
462 }
463 }
464 }
465
466 bool KItemListController::inputMethodEvent(QInputMethodEvent* event)
467 {
468 Q_UNUSED(event);
469 return false;
470 }
471
472 bool KItemListController::mousePressEvent(QGraphicsSceneMouseEvent* event, const QTransform& transform)
473 {
474 if (!m_view) {
475 return false;
476 }
477
478 m_pressedMousePos = transform.map(event->pos());
479 m_pressedIndex = m_view->itemAt(m_pressedMousePos);
480 emit mouseButtonPressed(m_pressedIndex, event->buttons());
481
482 if (m_view->isAboveExpansionToggle(m_pressedIndex, m_pressedMousePos)) {
483 m_selectionManager->endAnchoredSelection();
484 m_selectionManager->setCurrentItem(m_pressedIndex);
485 m_selectionManager->beginAnchoredSelection(m_pressedIndex);
486 return true;
487 }
488
489 m_selectionTogglePressed = m_view->isAboveSelectionToggle(m_pressedIndex, m_pressedMousePos);
490 if (m_selectionTogglePressed) {
491 m_selectionManager->setSelected(m_pressedIndex, 1, KItemListSelectionManager::Toggle);
492 // The previous anchored selection has been finished already in
493 // KItemListSelectionManager::setSelected(). We can safely change
494 // the current item and start a new anchored selection now.
495 m_selectionManager->setCurrentItem(m_pressedIndex);
496 m_selectionManager->beginAnchoredSelection(m_pressedIndex);
497 return true;
498 }
499
500 const bool shiftPressed = event->modifiers() & Qt::ShiftModifier;
501 const bool controlPressed = event->modifiers() & Qt::ControlModifier;
502
503 // The previous selection is cleared if either
504 // 1. The selection mode is SingleSelection, or
505 // 2. the selection mode is MultiSelection, and *none* of the following conditions are met:
506 // a) Shift or Control are pressed.
507 // b) The clicked item is selected already. In that case, the user might want to:
508 // - start dragging multiple items, or
509 // - open the context menu and perform an action for all selected items.
510 const bool shiftOrControlPressed = shiftPressed || controlPressed;
511 const bool pressedItemAlreadySelected = m_pressedIndex >= 0 && m_selectionManager->isSelected(m_pressedIndex);
512 const bool clearSelection = m_selectionBehavior == SingleSelection ||
513 (!shiftOrControlPressed && !pressedItemAlreadySelected);
514 if (clearSelection) {
515 m_selectionManager->clearSelection();
516 } else if (pressedItemAlreadySelected && !shiftOrControlPressed && (event->buttons() & Qt::LeftButton)) {
517 // The user might want to start dragging multiple items, but if he clicks the item
518 // in order to trigger it instead, the other selected items must be deselected.
519 // However, we do not know yet what the user is going to do.
520 // -> remember that the user pressed an item which had been selected already and
521 // clear the selection in mouseReleaseEvent(), unless the items are dragged.
522 m_clearSelectionIfItemsAreNotDragged = true;
523 }
524
525 if (!shiftPressed) {
526 // Finish the anchored selection before the current index is changed
527 m_selectionManager->endAnchoredSelection();
528 }
529
530 if (m_pressedIndex >= 0) {
531 m_selectionManager->setCurrentItem(m_pressedIndex);
532
533 switch (m_selectionBehavior) {
534 case NoSelection:
535 break;
536
537 case SingleSelection:
538 m_selectionManager->setSelected(m_pressedIndex);
539 break;
540
541 case MultiSelection:
542 if (controlPressed) {
543 m_selectionManager->setSelected(m_pressedIndex, 1, KItemListSelectionManager::Toggle);
544 m_selectionManager->beginAnchoredSelection(m_pressedIndex);
545 } else if (!shiftPressed || !m_selectionManager->isAnchoredSelectionActive()) {
546 // Select the pressed item and start a new anchored selection
547 m_selectionManager->setSelected(m_pressedIndex, 1, KItemListSelectionManager::Select);
548 m_selectionManager->beginAnchoredSelection(m_pressedIndex);
549 }
550 break;
551
552 default:
553 Q_ASSERT(false);
554 break;
555 }
556
557 if (event->buttons() & Qt::RightButton) {
558 emit itemContextMenuRequested(m_pressedIndex, event->screenPos());
559 }
560
561 return true;
562 }
563
564 if (event->buttons() & Qt::RightButton) {
565 const QRectF headerBounds = m_view->headerBoundaries();
566 if (headerBounds.contains(event->pos())) {
567 emit headerContextMenuRequested(event->screenPos());
568 } else {
569 emit viewContextMenuRequested(event->screenPos());
570 }
571 return true;
572 }
573
574 if (m_selectionBehavior == MultiSelection) {
575 QPointF startPos = m_pressedMousePos;
576 if (m_view->scrollOrientation() == Qt::Vertical) {
577 startPos.ry() += m_view->scrollOffset();
578 if (m_view->itemSize().width() < 0) {
579 // Use a special rubberband for views that have only one column and
580 // expand the rubberband to use the whole width of the view.
581 startPos.setX(0);
582 }
583 } else {
584 startPos.rx() += m_view->scrollOffset();
585 }
586
587 m_oldSelection = m_selectionManager->selectedItems();
588 KItemListRubberBand* rubberBand = m_view->rubberBand();
589 rubberBand->setStartPosition(startPos);
590 rubberBand->setEndPosition(startPos);
591 rubberBand->setActive(true);
592 connect(rubberBand, SIGNAL(endPositionChanged(QPointF,QPointF)), this, SLOT(slotRubberBandChanged()));
593 m_view->setAutoScroll(true);
594 }
595
596 return false;
597 }
598
599 bool KItemListController::mouseMoveEvent(QGraphicsSceneMouseEvent* event, const QTransform& transform)
600 {
601 if (!m_view) {
602 return false;
603 }
604
605 if (m_pressedIndex >= 0) {
606 // Check whether a dragging should be started
607 if (event->buttons() & Qt::LeftButton) {
608 const QPointF pos = transform.map(event->pos());
609 if ((pos - m_pressedMousePos).manhattanLength() >= QApplication::startDragDistance()) {
610 if (!m_selectionManager->isSelected(m_pressedIndex)) {
611 // Always assure that the dragged item gets selected. Usually this is already
612 // done on the mouse-press event, but when using the selection-toggle on a
613 // selected item the dragged item is not selected yet.
614 m_selectionManager->setSelected(m_pressedIndex, 1, KItemListSelectionManager::Toggle);
615 } else {
616 // A selected item has been clicked to drag all selected items
617 // -> the selection should not be cleared when the mouse button is released.
618 m_clearSelectionIfItemsAreNotDragged = false;
619 }
620
621 startDragging();
622 }
623 }
624 } else {
625 KItemListRubberBand* rubberBand = m_view->rubberBand();
626 if (rubberBand->isActive()) {
627 QPointF endPos = transform.map(event->pos());
628
629 // Update the current item.
630 const int newCurrent = m_view->itemAt(endPos);
631 if (newCurrent >= 0) {
632 // It's expected that the new current index is also the new anchor (bug 163451).
633 m_selectionManager->endAnchoredSelection();
634 m_selectionManager->setCurrentItem(newCurrent);
635 m_selectionManager->beginAnchoredSelection(newCurrent);
636 }
637
638 if (m_view->scrollOrientation() == Qt::Vertical) {
639 endPos.ry() += m_view->scrollOffset();
640 if (m_view->itemSize().width() < 0) {
641 // Use a special rubberband for views that have only one column and
642 // expand the rubberband to use the whole width of the view.
643 endPos.setX(m_view->size().width());
644 }
645 } else {
646 endPos.rx() += m_view->scrollOffset();
647 }
648 rubberBand->setEndPosition(endPos);
649 }
650 }
651
652 return false;
653 }
654
655 bool KItemListController::mouseReleaseEvent(QGraphicsSceneMouseEvent* event, const QTransform& transform)
656 {
657 if (!m_view) {
658 return false;
659 }
660
661 emit mouseButtonReleased(m_pressedIndex, event->buttons());
662
663 const bool isAboveSelectionToggle = m_view->isAboveSelectionToggle(m_pressedIndex, m_pressedMousePos);
664 if (isAboveSelectionToggle) {
665 m_selectionTogglePressed = false;
666 return true;
667 }
668
669 if (!isAboveSelectionToggle && m_selectionTogglePressed) {
670 m_selectionManager->setSelected(m_pressedIndex, 1, KItemListSelectionManager::Toggle);
671 m_selectionTogglePressed = false;
672 return true;
673 }
674
675 const bool shiftOrControlPressed = event->modifiers() & Qt::ShiftModifier ||
676 event->modifiers() & Qt::ControlModifier;
677
678 KItemListRubberBand* rubberBand = m_view->rubberBand();
679 if (rubberBand->isActive()) {
680 disconnect(rubberBand, SIGNAL(endPositionChanged(QPointF,QPointF)), this, SLOT(slotRubberBandChanged()));
681 rubberBand->setActive(false);
682 m_oldSelection.clear();
683 m_view->setAutoScroll(false);
684 }
685
686 const QPointF pos = transform.map(event->pos());
687 const int index = m_view->itemAt(pos);
688
689 if (index >= 0 && index == m_pressedIndex) {
690 // The release event is done above the same item as the press event
691
692 if (m_clearSelectionIfItemsAreNotDragged) {
693 // A selected item has been clicked, but no drag operation has been started
694 // -> clear the rest of the selection.
695 m_selectionManager->clearSelection();
696 m_selectionManager->setSelected(m_pressedIndex, 1, KItemListSelectionManager::Select);
697 m_selectionManager->beginAnchoredSelection(m_pressedIndex);
698 }
699
700 if (event->button() & Qt::LeftButton) {
701 bool emitItemActivated = true;
702 if (m_view->isAboveExpansionToggle(index, pos)) {
703 const bool expanded = m_model->isExpanded(index);
704 m_model->setExpanded(index, !expanded);
705
706 emit itemExpansionToggleClicked(index);
707 emitItemActivated = false;
708 } else if (shiftOrControlPressed) {
709 // The mouse click should only update the selection, not trigger the item
710 emitItemActivated = false;
711 } else if (!m_singleClickActivation) {
712 emitItemActivated = false;
713 }
714 if (emitItemActivated) {
715 emit itemActivated(index);
716 }
717 } else if (event->button() & Qt::MidButton) {
718 emit itemMiddleClicked(index);
719 }
720 }
721
722 m_pressedMousePos = QPointF();
723 m_pressedIndex = -1;
724 m_clearSelectionIfItemsAreNotDragged = false;
725 return false;
726 }
727
728 bool KItemListController::mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event, const QTransform& transform)
729 {
730 const QPointF pos = transform.map(event->pos());
731 const int index = m_view->itemAt(pos);
732
733 bool emitItemActivated = !m_singleClickActivation &&
734 (event->button() & Qt::LeftButton) &&
735 index >= 0 && index < m_model->count();
736 if (emitItemActivated) {
737 emit itemActivated(index);
738 }
739 return false;
740 }
741
742 bool KItemListController::dragEnterEvent(QGraphicsSceneDragDropEvent* event, const QTransform& transform)
743 {
744 Q_UNUSED(event);
745 Q_UNUSED(transform);
746 return false;
747 }
748
749 bool KItemListController::dragLeaveEvent(QGraphicsSceneDragDropEvent* event, const QTransform& transform)
750 {
751 Q_UNUSED(event);
752 Q_UNUSED(transform);
753
754 m_view->setAutoScroll(false);
755
756 KItemListWidget* widget = hoveredWidget();
757 if (widget) {
758 widget->setHovered(false);
759 emit itemUnhovered(widget->index());
760 }
761 return false;
762 }
763
764 bool KItemListController::dragMoveEvent(QGraphicsSceneDragDropEvent* event, const QTransform& transform)
765 {
766 Q_UNUSED(transform);
767 if (!m_model || !m_view) {
768 return false;
769 }
770
771 event->acceptProposedAction();
772
773 KItemListWidget* oldHoveredWidget = hoveredWidget();
774
775 const QPointF pos = transform.map(event->pos());
776 KItemListWidget* newHoveredWidget = widgetForPos(pos);
777
778 if (oldHoveredWidget != newHoveredWidget) {
779 m_autoActivationTimer->stop();
780
781 if (oldHoveredWidget) {
782 oldHoveredWidget->setHovered(false);
783 emit itemUnhovered(oldHoveredWidget->index());
784 }
785
786 if (newHoveredWidget) {
787 const int index = newHoveredWidget->index();
788 if (m_model->supportsDropping(index)) {
789 newHoveredWidget->setHovered(true);
790 }
791 emit itemHovered(index);
792
793 if (m_autoActivationTimer->interval() >= 0) {
794 m_autoActivationTimer->setProperty("index", index);
795 m_autoActivationTimer->start();
796 }
797 }
798 }
799
800 return false;
801 }
802
803 bool KItemListController::dropEvent(QGraphicsSceneDragDropEvent* event, const QTransform& transform)
804 {
805 Q_UNUSED(transform)
806 if (!m_view) {
807 return false;
808 }
809
810 m_autoActivationTimer->stop();
811 m_view->setAutoScroll(false);
812
813 const QPointF pos = transform.map(event->pos());
814 const int index = m_view->itemAt(pos);
815 emit itemDropEvent(index, event);
816
817 return true;
818 }
819
820 bool KItemListController::hoverEnterEvent(QGraphicsSceneHoverEvent* event, const QTransform& transform)
821 {
822 Q_UNUSED(event);
823 Q_UNUSED(transform);
824 return false;
825 }
826
827 bool KItemListController::hoverMoveEvent(QGraphicsSceneHoverEvent* event, const QTransform& transform)
828 {
829 Q_UNUSED(transform);
830 if (!m_model || !m_view) {
831 return false;
832 }
833
834 KItemListWidget* oldHoveredWidget = hoveredWidget();
835 const QPointF pos = transform.map(event->pos());
836 KItemListWidget* newHoveredWidget = widgetForPos(pos);
837
838 if (oldHoveredWidget != newHoveredWidget) {
839 if (oldHoveredWidget) {
840 oldHoveredWidget->setHovered(false);
841 emit itemUnhovered(oldHoveredWidget->index());
842 }
843
844 if (newHoveredWidget) {
845 newHoveredWidget->setHovered(true);
846 emit itemHovered(newHoveredWidget->index());
847 }
848 }
849
850 return false;
851 }
852
853 bool KItemListController::hoverLeaveEvent(QGraphicsSceneHoverEvent* event, const QTransform& transform)
854 {
855 Q_UNUSED(event);
856 Q_UNUSED(transform);
857
858 if (!m_model || !m_view) {
859 return false;
860 }
861
862 foreach (KItemListWidget* widget, m_view->visibleItemListWidgets()) {
863 if (widget->isHovered()) {
864 widget->setHovered(false);
865 emit itemUnhovered(widget->index());
866 }
867 }
868 return false;
869 }
870
871 bool KItemListController::wheelEvent(QGraphicsSceneWheelEvent* event, const QTransform& transform)
872 {
873 Q_UNUSED(event);
874 Q_UNUSED(transform);
875 return false;
876 }
877
878 bool KItemListController::resizeEvent(QGraphicsSceneResizeEvent* event, const QTransform& transform)
879 {
880 Q_UNUSED(event);
881 Q_UNUSED(transform);
882 return false;
883 }
884
885 bool KItemListController::processEvent(QEvent* event, const QTransform& transform)
886 {
887 if (!event) {
888 return false;
889 }
890
891 switch (event->type()) {
892 case QEvent::KeyPress:
893 return keyPressEvent(static_cast<QKeyEvent*>(event));
894 case QEvent::InputMethod:
895 return inputMethodEvent(static_cast<QInputMethodEvent*>(event));
896 case QEvent::GraphicsSceneMousePress:
897 return mousePressEvent(static_cast<QGraphicsSceneMouseEvent*>(event), QTransform());
898 case QEvent::GraphicsSceneMouseMove:
899 return mouseMoveEvent(static_cast<QGraphicsSceneMouseEvent*>(event), QTransform());
900 case QEvent::GraphicsSceneMouseRelease:
901 return mouseReleaseEvent(static_cast<QGraphicsSceneMouseEvent*>(event), QTransform());
902 case QEvent::GraphicsSceneMouseDoubleClick:
903 return mouseDoubleClickEvent(static_cast<QGraphicsSceneMouseEvent*>(event), QTransform());
904 case QEvent::GraphicsSceneWheel:
905 return wheelEvent(static_cast<QGraphicsSceneWheelEvent*>(event), QTransform());
906 case QEvent::GraphicsSceneDragEnter:
907 return dragEnterEvent(static_cast<QGraphicsSceneDragDropEvent*>(event), QTransform());
908 case QEvent::GraphicsSceneDragLeave:
909 return dragLeaveEvent(static_cast<QGraphicsSceneDragDropEvent*>(event), QTransform());
910 case QEvent::GraphicsSceneDragMove:
911 return dragMoveEvent(static_cast<QGraphicsSceneDragDropEvent*>(event), QTransform());
912 case QEvent::GraphicsSceneDrop:
913 return dropEvent(static_cast<QGraphicsSceneDragDropEvent*>(event), QTransform());
914 case QEvent::GraphicsSceneHoverEnter:
915 return hoverEnterEvent(static_cast<QGraphicsSceneHoverEvent*>(event), QTransform());
916 case QEvent::GraphicsSceneHoverMove:
917 return hoverMoveEvent(static_cast<QGraphicsSceneHoverEvent*>(event), QTransform());
918 case QEvent::GraphicsSceneHoverLeave:
919 return hoverLeaveEvent(static_cast<QGraphicsSceneHoverEvent*>(event), QTransform());
920 case QEvent::GraphicsSceneResize:
921 return resizeEvent(static_cast<QGraphicsSceneResizeEvent*>(event), transform);
922 default:
923 break;
924 }
925
926 return false;
927 }
928
929 void KItemListController::slotViewScrollOffsetChanged(qreal current, qreal previous)
930 {
931 if (!m_view) {
932 return;
933 }
934
935 KItemListRubberBand* rubberBand = m_view->rubberBand();
936 if (rubberBand->isActive()) {
937 const qreal diff = current - previous;
938 // TODO: Ideally just QCursor::pos() should be used as
939 // new end-position but it seems there is no easy way
940 // to have something like QWidget::mapFromGlobal() for QGraphicsWidget
941 // (... or I just missed an easy way to do the mapping)
942 QPointF endPos = rubberBand->endPosition();
943 if (m_view->scrollOrientation() == Qt::Vertical) {
944 endPos.ry() += diff;
945 } else {
946 endPos.rx() += diff;
947 }
948
949 rubberBand->setEndPosition(endPos);
950 }
951 }
952
953 void KItemListController::slotRubberBandChanged()
954 {
955 if (!m_view || !m_model || m_model->count() <= 0) {
956 return;
957 }
958
959 const KItemListRubberBand* rubberBand = m_view->rubberBand();
960 const QPointF startPos = rubberBand->startPosition();
961 const QPointF endPos = rubberBand->endPosition();
962 QRectF rubberBandRect = QRectF(startPos, endPos).normalized();
963
964 const bool scrollVertical = (m_view->scrollOrientation() == Qt::Vertical);
965 if (scrollVertical) {
966 rubberBandRect.translate(0, -m_view->scrollOffset());
967 } else {
968 rubberBandRect.translate(-m_view->scrollOffset(), 0);
969 }
970
971 if (!m_oldSelection.isEmpty()) {
972 // Clear the old selection that was available before the rubberband has
973 // been activated in case if no Shift- or Control-key are pressed
974 const bool shiftOrControlPressed = QApplication::keyboardModifiers() & Qt::ShiftModifier ||
975 QApplication::keyboardModifiers() & Qt::ControlModifier;
976 if (!shiftOrControlPressed) {
977 m_oldSelection.clear();
978 }
979 }
980
981 QSet<int> selectedItems;
982
983 // Select all visible items that intersect with the rubberband
984 foreach (const KItemListWidget* widget, m_view->visibleItemListWidgets()) {
985 const int index = widget->index();
986
987 const QRectF widgetRect = m_view->itemRect(index);
988 if (widgetRect.intersects(rubberBandRect)) {
989 const QRectF iconRect = widget->iconRect().translated(widgetRect.topLeft());
990 const QRectF textRect = widget->textRect().translated(widgetRect.topLeft());
991 if (iconRect.intersects(rubberBandRect) || textRect.intersects(rubberBandRect)) {
992 selectedItems.insert(index);
993 }
994 }
995 }
996
997 // Select all invisible items that intersect with the rubberband. Instead of
998 // iterating all items only the area which might be touched by the rubberband
999 // will be checked.
1000 const bool increaseIndex = scrollVertical ?
1001 startPos.y() > endPos.y(): startPos.x() > endPos.x();
1002
1003 int index = increaseIndex ? m_view->lastVisibleIndex() + 1 : m_view->firstVisibleIndex() - 1;
1004 bool selectionFinished = false;
1005 do {
1006 const QRectF widgetRect = m_view->itemRect(index);
1007 if (widgetRect.intersects(rubberBandRect)) {
1008 selectedItems.insert(index);
1009 }
1010
1011 if (increaseIndex) {
1012 ++index;
1013 selectionFinished = (index >= m_model->count()) ||
1014 ( scrollVertical && widgetRect.top() > rubberBandRect.bottom()) ||
1015 (!scrollVertical && widgetRect.left() > rubberBandRect.right());
1016 } else {
1017 --index;
1018 selectionFinished = (index < 0) ||
1019 ( scrollVertical && widgetRect.bottom() < rubberBandRect.top()) ||
1020 (!scrollVertical && widgetRect.right() < rubberBandRect.left());
1021 }
1022 } while (!selectionFinished);
1023
1024 if (QApplication::keyboardModifiers() & Qt::ControlModifier) {
1025 // If Control is pressed, the selection state of all items in the rubberband is toggled.
1026 // Therefore, the new selection contains:
1027 // 1. All previously selected items which are not inside the rubberband, and
1028 // 2. all items inside the rubberband which have not been selected previously.
1029 m_selectionManager->setSelectedItems((m_oldSelection - selectedItems) + (selectedItems - m_oldSelection));
1030 }
1031 else {
1032 m_selectionManager->setSelectedItems(selectedItems + m_oldSelection);
1033 }
1034 }
1035
1036 void KItemListController::startDragging()
1037 {
1038 if (!m_view || !m_model) {
1039 return;
1040 }
1041
1042 const QSet<int> selectedItems = m_selectionManager->selectedItems();
1043 if (selectedItems.isEmpty()) {
1044 return;
1045 }
1046
1047 QMimeData* data = m_model->createMimeData(selectedItems);
1048 if (!data) {
1049 return;
1050 }
1051
1052 // The created drag object will be owned and deleted
1053 // by QApplication::activeWindow().
1054 QDrag* drag = new QDrag(QApplication::activeWindow());
1055 drag->setMimeData(data);
1056
1057 const QPixmap pixmap = m_view->createDragPixmap(selectedItems);
1058 drag->setPixmap(pixmap);
1059
1060 drag->exec(Qt::MoveAction | Qt::CopyAction | Qt::LinkAction, Qt::CopyAction);
1061 }
1062
1063 KItemListWidget* KItemListController::hoveredWidget() const
1064 {
1065 Q_ASSERT(m_view);
1066
1067 foreach (KItemListWidget* widget, m_view->visibleItemListWidgets()) {
1068 if (widget->isHovered()) {
1069 return widget;
1070 }
1071 }
1072
1073 return 0;
1074 }
1075
1076 KItemListWidget* KItemListController::widgetForPos(const QPointF& pos) const
1077 {
1078 Q_ASSERT(m_view);
1079
1080 foreach (KItemListWidget* widget, m_view->visibleItemListWidgets()) {
1081 const QPointF mappedPos = widget->mapFromItem(m_view, pos);
1082
1083 const bool hovered = widget->contains(mappedPos) &&
1084 !widget->expansionToggleRect().contains(mappedPos);
1085 if (hovered) {
1086 return widget;
1087 }
1088 }
1089
1090 return 0;
1091 }
1092
1093 void KItemListController::updateKeyboardAnchor()
1094 {
1095 const bool validAnchor = m_keyboardAnchorIndex >= 0 &&
1096 m_keyboardAnchorIndex < m_model->count() &&
1097 keyboardAnchorPos(m_keyboardAnchorIndex) == m_keyboardAnchorPos;
1098 if (!validAnchor) {
1099 const int index = m_selectionManager->currentItem();
1100 m_keyboardAnchorIndex = index;
1101 m_keyboardAnchorPos = keyboardAnchorPos(index);
1102 }
1103 }
1104
1105 int KItemListController::nextRowIndex(int index) const
1106 {
1107 if (m_keyboardAnchorIndex < 0) {
1108 return index;
1109 }
1110
1111 const int maxIndex = m_model->count() - 1;
1112 if (index == maxIndex) {
1113 return index;
1114 }
1115
1116 // Calculate the index of the last column inside the row of the current index
1117 int lastColumnIndex = index;
1118 while (keyboardAnchorPos(lastColumnIndex + 1) > keyboardAnchorPos(lastColumnIndex)) {
1119 ++lastColumnIndex;
1120 if (lastColumnIndex >= maxIndex) {
1121 return index;
1122 }
1123 }
1124
1125 // Based on the last column index go to the next row and calculate the nearest index
1126 // that is below the current index
1127 int nextRowIndex = lastColumnIndex + 1;
1128 int searchIndex = nextRowIndex;
1129 qreal minDiff = qAbs(m_keyboardAnchorPos - keyboardAnchorPos(nextRowIndex));
1130 while (searchIndex < maxIndex && keyboardAnchorPos(searchIndex + 1) > keyboardAnchorPos(searchIndex)) {
1131 ++searchIndex;
1132 const qreal searchDiff = qAbs(m_keyboardAnchorPos - keyboardAnchorPos(searchIndex));
1133 if (searchDiff < minDiff) {
1134 minDiff = searchDiff;
1135 nextRowIndex = searchIndex;
1136 }
1137 }
1138
1139 return nextRowIndex;
1140 }
1141
1142 int KItemListController::previousRowIndex(int index) const
1143 {
1144 if (m_keyboardAnchorIndex < 0 || index == 0) {
1145 return index;
1146 }
1147
1148 // Calculate the index of the first column inside the row of the current index
1149 int firstColumnIndex = index;
1150 while (keyboardAnchorPos(firstColumnIndex - 1) < keyboardAnchorPos(firstColumnIndex)) {
1151 --firstColumnIndex;
1152 if (firstColumnIndex <= 0) {
1153 return index;
1154 }
1155 }
1156
1157 // Based on the first column index go to the previous row and calculate the nearest index
1158 // that is above the current index
1159 int previousRowIndex = firstColumnIndex - 1;
1160 int searchIndex = previousRowIndex;
1161 qreal minDiff = qAbs(m_keyboardAnchorPos - keyboardAnchorPos(previousRowIndex));
1162 while (searchIndex > 0 && keyboardAnchorPos(searchIndex - 1) < keyboardAnchorPos(searchIndex)) {
1163 --searchIndex;
1164 const qreal searchDiff = qAbs(m_keyboardAnchorPos - keyboardAnchorPos(searchIndex));
1165 if (searchDiff < minDiff) {
1166 minDiff = searchDiff;
1167 previousRowIndex = searchIndex;
1168 }
1169 }
1170
1171 return previousRowIndex;
1172 }
1173
1174 qreal KItemListController::keyboardAnchorPos(int index) const
1175 {
1176 const QRectF itemRect = m_view->itemRect(index);
1177 if (!itemRect.isEmpty()) {
1178 return (m_view->scrollOrientation() == Qt::Vertical) ? itemRect.x() : itemRect.y();
1179 }
1180
1181 return 0;
1182 }
1183
1184 void KItemListController::updateExtendedSelectionRegion()
1185 {
1186 if (m_view) {
1187 const bool extend = (m_selectionBehavior != MultiSelection);
1188 KItemListStyleOption option = m_view->styleOption();
1189 if (option.extendedSelectionRegion != extend) {
1190 option.extendedSelectionRegion = extend;
1191 m_view->setStyleOption(option);
1192 }
1193 }
1194 }
1195
1196 #include "kitemlistcontroller.moc"