1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
3 * Copyright (C) 2012 by Frank Reininghaus <frank78ac@googlemail.com> *
5 * Based on the Itemviews NG project from Trolltech Labs: *
6 * http://qt.gitorious.org/qt-labs/itemviews-ng *
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. *
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. *
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 ***************************************************************************/
24 #include "kitemlistcontroller.h"
26 #include <KGlobalSettings>
29 #include "kitemlistview.h"
30 #include "kitemlistselectionmanager.h"
32 #include "private/kitemlistrubberband.h"
33 #include "private/kitemlistkeyboardsearchmanager.h"
35 #include <QApplication>
38 #include <QGraphicsScene>
39 #include <QGraphicsSceneEvent>
40 #include <QGraphicsView>
44 KItemListController::KItemListController(KItemModelBase
* model
, KItemListView
* view
, QObject
* parent
) :
46 m_singleClickActivation(KGlobalSettings::singleClick()),
47 m_selectionTogglePressed(false),
48 m_clearSelectionIfItemsAreNotDragged(false),
49 m_selectionBehavior(NoSelection
),
52 m_selectionManager(new KItemListSelectionManager(this)),
53 m_keyboardManager(new KItemListKeyboardSearchManager(this)),
56 m_autoActivationTimer(0),
58 m_keyboardAnchorIndex(-1),
59 m_keyboardAnchorPos(0)
61 connect(m_keyboardManager
, SIGNAL(changeCurrentItem(QString
,bool)),
62 this, SLOT(slotChangeCurrentItem(QString
,bool)));
63 connect(m_selectionManager
, SIGNAL(currentChanged(int,int)),
64 m_keyboardManager
, SLOT(slotCurrentChanged(int,int)));
66 m_autoActivationTimer
= new QTimer(this);
67 m_autoActivationTimer
->setSingleShot(true);
68 m_autoActivationTimer
->setInterval(-1);
69 connect(m_autoActivationTimer
, SIGNAL(timeout()), this, SLOT(slotAutoActivationTimeout()));
75 KItemListController::~KItemListController()
84 void KItemListController::setModel(KItemModelBase
* model
)
86 if (m_model
== model
) {
90 KItemModelBase
* oldModel
= m_model
;
92 oldModel
->deleteLater();
97 m_model
->setParent(this);
101 m_view
->setModel(m_model
);
104 m_selectionManager
->setModel(m_model
);
106 emit
modelChanged(m_model
, oldModel
);
109 KItemModelBase
* KItemListController::model() const
114 KItemListSelectionManager
* KItemListController::selectionManager() const
116 return m_selectionManager
;
119 void KItemListController::setView(KItemListView
* view
)
121 if (m_view
== view
) {
125 KItemListView
* oldView
= m_view
;
127 disconnect(oldView
, SIGNAL(scrollOffsetChanged(qreal
,qreal
)), this, SLOT(slotViewScrollOffsetChanged(qreal
,qreal
)));
128 oldView
->deleteLater();
134 m_view
->setParent(this);
135 m_view
->setController(this);
136 m_view
->setModel(m_model
);
137 connect(m_view
, SIGNAL(scrollOffsetChanged(qreal
,qreal
)), this, SLOT(slotViewScrollOffsetChanged(qreal
,qreal
)));
138 updateExtendedSelectionRegion();
141 emit
viewChanged(m_view
, oldView
);
144 KItemListView
* KItemListController::view() const
149 void KItemListController::setSelectionBehavior(SelectionBehavior behavior
)
151 m_selectionBehavior
= behavior
;
152 updateExtendedSelectionRegion();
155 KItemListController::SelectionBehavior
KItemListController::selectionBehavior() const
157 return m_selectionBehavior
;
160 void KItemListController::setAutoActivationDelay(int delay
)
162 m_autoActivationTimer
->setInterval(delay
);
165 int KItemListController::autoActivationDelay() const
167 return m_autoActivationTimer
->interval();
170 void KItemListController::setSingleClickActivation(bool singleClick
)
172 m_singleClickActivation
= singleClick
;
175 bool KItemListController::singleClickActivation() const
177 return m_singleClickActivation
;
180 bool KItemListController::showEvent(QShowEvent
* event
)
186 bool KItemListController::hideEvent(QHideEvent
* event
)
192 bool KItemListController::keyPressEvent(QKeyEvent
* event
)
194 int index
= m_selectionManager
->currentItem();
195 int key
= event
->key();
197 // Handle the expanding/collapsing of items
198 if (m_view
->supportsItemExpanding() && m_model
->isExpandable(index
)) {
199 if (key
== Qt::Key_Right
) {
200 if (m_model
->setExpanded(index
, true)) {
203 } else if (key
== Qt::Key_Left
) {
204 if (m_model
->setExpanded(index
, false)) {
210 const bool shiftPressed
= event
->modifiers() & Qt::ShiftModifier
;
211 const bool controlPressed
= event
->modifiers() & Qt::ControlModifier
;
212 const bool shiftOrControlPressed
= shiftPressed
|| controlPressed
;
214 const int itemCount
= m_model
->count();
216 // For horizontal scroll orientation, transform
217 // the arrow keys to simplify the event handling.
218 if (m_view
->scrollOrientation() == Qt::Horizontal
) {
220 case Qt::Key_Up
: key
= Qt::Key_Left
; break;
221 case Qt::Key_Down
: key
= Qt::Key_Right
; break;
222 case Qt::Key_Left
: key
= Qt::Key_Up
; break;
223 case Qt::Key_Right
: key
= Qt::Key_Down
; break;
228 const bool selectSingleItem
= m_selectionBehavior
!= NoSelection
&&
230 (key
== Qt::Key_Home
|| key
== Qt::Key_End
||
231 key
== Qt::Key_Up
|| key
== Qt::Key_Down
||
232 key
== Qt::Key_Left
|| key
== Qt::Key_Right
);
233 if (selectSingleItem
) {
234 const int current
= m_selectionManager
->currentItem();
235 m_selectionManager
->setSelected(current
);
242 m_keyboardAnchorIndex
= index
;
243 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
247 index
= itemCount
- 1;
248 m_keyboardAnchorIndex
= index
;
249 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
255 m_keyboardAnchorIndex
= index
;
256 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
261 if (index
< itemCount
- 1) {
263 m_keyboardAnchorIndex
= index
;
264 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
269 updateKeyboardAnchor();
270 index
= previousRowIndex(index
);
274 updateKeyboardAnchor();
275 index
= nextRowIndex(index
);
279 if (m_view
->scrollOrientation() == Qt::Horizontal
) {
280 // The new current index should correspond to the first item in the current column.
281 int newIndex
= qMax(index
- 1, 0);
282 while (newIndex
!= index
&& m_view
->itemRect(newIndex
).topLeft().y() < m_view
->itemRect(index
).topLeft().y()) {
284 newIndex
= qMax(index
- 1, 0);
286 m_keyboardAnchorIndex
= index
;
287 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
289 const qreal currentItemBottom
= m_view
->itemRect(index
).bottomLeft().y();
290 const qreal height
= m_view
->geometry().height();
292 // The new current item should be the first item in the current
293 // column whose itemRect's top coordinate is larger than targetY.
294 const qreal targetY
= currentItemBottom
- height
;
296 updateKeyboardAnchor();
297 int newIndex
= previousRowIndex(index
);
300 updateKeyboardAnchor();
301 newIndex
= previousRowIndex(index
);
302 } while (m_view
->itemRect(newIndex
).topLeft().y() > targetY
&& newIndex
!= index
);
306 case Qt::Key_PageDown
:
307 if (m_view
->scrollOrientation() == Qt::Horizontal
) {
308 // The new current index should correspond to the last item in the current column.
309 int newIndex
= qMin(index
+ 1, m_model
->count() - 1);
310 while (newIndex
!= index
&& m_view
->itemRect(newIndex
).topLeft().y() > m_view
->itemRect(index
).topLeft().y()) {
312 newIndex
= qMin(index
+ 1, m_model
->count() - 1);
314 m_keyboardAnchorIndex
= index
;
315 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
317 const qreal currentItemTop
= m_view
->itemRect(index
).topLeft().y();
318 const qreal height
= m_view
->geometry().height();
320 // The new current item should be the last item in the current
321 // column whose itemRect's bottom coordinate is smaller than targetY.
322 const qreal targetY
= currentItemTop
+ height
;
324 updateKeyboardAnchor();
325 int newIndex
= nextRowIndex(index
);
328 updateKeyboardAnchor();
329 newIndex
= nextRowIndex(index
);
330 } while (m_view
->itemRect(newIndex
).bottomLeft().y() < targetY
&& newIndex
!= index
);
335 case Qt::Key_Return
: {
336 const QSet
<int> selectedItems
= m_selectionManager
->selectedItems();
337 if (selectedItems
.count() >= 2) {
338 emit
itemsActivated(selectedItems
);
339 } else if (selectedItems
.count() == 1) {
340 emit
itemActivated(selectedItems
.toList().first());
342 emit
itemActivated(index
);
348 if (m_selectionBehavior
== MultiSelection
) {
349 if (controlPressed
) {
350 m_selectionManager
->endAnchoredSelection();
351 m_selectionManager
->setSelected(index
, 1, KItemListSelectionManager::Toggle
);
352 m_selectionManager
->beginAnchoredSelection(index
);
354 const int current
= m_selectionManager
->currentItem();
355 m_selectionManager
->setSelected(current
);
361 // Emit the signal itemContextMenuRequested() in case if at least one
362 // item is selected. Otherwise the signal viewContextMenuRequested() will be emitted.
363 const QSet
<int> selectedItems
= m_selectionManager
->selectedItems();
365 if (selectedItems
.count() >= 2) {
366 const int currentItemIndex
= m_selectionManager
->currentItem();
367 index
= selectedItems
.contains(currentItemIndex
)
368 ? currentItemIndex
: selectedItems
.toList().first();
369 } else if (selectedItems
.count() == 1) {
370 index
= selectedItems
.toList().first();
374 const QRectF contextRect
= m_view
->itemContextRect(index
);
375 const QPointF
pos(m_view
->scene()->views().first()->mapToGlobal(contextRect
.bottomRight().toPoint()));
376 emit
itemContextMenuRequested(index
, pos
);
378 emit
viewContextMenuRequested(QCursor::pos());
384 if (m_selectionBehavior
!= SingleSelection
) {
385 m_selectionManager
->clearSelection();
387 m_keyboardManager
->cancelSearch();
391 m_keyboardManager
->addKeys(event
->text());
395 if (m_selectionManager
->currentItem() != index
) {
396 switch (m_selectionBehavior
) {
398 m_selectionManager
->setCurrentItem(index
);
401 case SingleSelection
:
402 m_selectionManager
->setCurrentItem(index
);
403 m_selectionManager
->clearSelection();
404 m_selectionManager
->setSelected(index
, 1);
408 if (controlPressed
) {
409 m_selectionManager
->endAnchoredSelection();
412 m_selectionManager
->setCurrentItem(index
);
414 if (!shiftOrControlPressed
) {
415 m_selectionManager
->clearSelection();
416 m_selectionManager
->setSelected(index
, 1);
420 m_selectionManager
->beginAnchoredSelection(index
);
425 m_view
->scrollToItem(index
);
430 void KItemListController::slotChangeCurrentItem(const QString
& text
, bool searchFromNextItem
)
432 if (!m_model
|| m_model
->count() == 0) {
435 const int currentIndex
= m_selectionManager
->currentItem();
437 if (searchFromNextItem
) {
438 index
= m_model
->indexForKeyboardSearch(text
, (currentIndex
+ 1) % m_model
->count());
440 index
= m_model
->indexForKeyboardSearch(text
, currentIndex
);
443 m_selectionManager
->setCurrentItem(index
);
444 m_selectionManager
->clearSelection();
445 m_selectionManager
->setSelected(index
, 1);
446 m_selectionManager
->beginAnchoredSelection(index
);
447 m_view
->scrollToItem(index
);
451 void KItemListController::slotAutoActivationTimeout()
453 if (!m_model
|| !m_view
) {
457 const int index
= m_autoActivationTimer
->property("index").toInt();
458 if (index
< 0 || index
>= m_model
->count()) {
462 if (m_model
->supportsDropping(index
)) {
463 if (m_view
->supportsItemExpanding() && m_model
->isExpandable(index
)) {
464 const bool expanded
= m_model
->isExpanded(index
);
465 m_model
->setExpanded(index
, !expanded
);
467 emit
itemActivated(index
);
472 bool KItemListController::inputMethodEvent(QInputMethodEvent
* event
)
478 bool KItemListController::mousePressEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
484 m_pressedMousePos
= transform
.map(event
->pos());
485 m_pressedIndex
= m_view
->itemAt(m_pressedMousePos
);
486 emit
mouseButtonPressed(m_pressedIndex
, event
->buttons());
488 if (m_view
->isAboveExpansionToggle(m_pressedIndex
, m_pressedMousePos
)) {
489 m_selectionManager
->endAnchoredSelection();
490 m_selectionManager
->setCurrentItem(m_pressedIndex
);
491 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
495 m_selectionTogglePressed
= m_view
->isAboveSelectionToggle(m_pressedIndex
, m_pressedMousePos
);
496 if (m_selectionTogglePressed
) {
497 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
498 // The previous anchored selection has been finished already in
499 // KItemListSelectionManager::setSelected(). We can safely change
500 // the current item and start a new anchored selection now.
501 m_selectionManager
->setCurrentItem(m_pressedIndex
);
502 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
506 const bool shiftPressed
= event
->modifiers() & Qt::ShiftModifier
;
507 const bool controlPressed
= event
->modifiers() & Qt::ControlModifier
;
509 // The previous selection is cleared if either
510 // 1. The selection mode is SingleSelection, or
511 // 2. the selection mode is MultiSelection, and *none* of the following conditions are met:
512 // a) Shift or Control are pressed.
513 // b) The clicked item is selected already. In that case, the user might want to:
514 // - start dragging multiple items, or
515 // - open the context menu and perform an action for all selected items.
516 const bool shiftOrControlPressed
= shiftPressed
|| controlPressed
;
517 const bool pressedItemAlreadySelected
= m_pressedIndex
>= 0 && m_selectionManager
->isSelected(m_pressedIndex
);
518 const bool clearSelection
= m_selectionBehavior
== SingleSelection
||
519 (!shiftOrControlPressed
&& !pressedItemAlreadySelected
);
520 if (clearSelection
) {
521 m_selectionManager
->clearSelection();
522 } else if (pressedItemAlreadySelected
&& !shiftOrControlPressed
&& (event
->buttons() & Qt::LeftButton
)) {
523 // The user might want to start dragging multiple items, but if he clicks the item
524 // in order to trigger it instead, the other selected items must be deselected.
525 // However, we do not know yet what the user is going to do.
526 // -> remember that the user pressed an item which had been selected already and
527 // clear the selection in mouseReleaseEvent(), unless the items are dragged.
528 m_clearSelectionIfItemsAreNotDragged
= true;
532 // Finish the anchored selection before the current index is changed
533 m_selectionManager
->endAnchoredSelection();
536 if (m_pressedIndex
>= 0) {
537 m_selectionManager
->setCurrentItem(m_pressedIndex
);
539 switch (m_selectionBehavior
) {
543 case SingleSelection
:
544 m_selectionManager
->setSelected(m_pressedIndex
);
548 if (controlPressed
) {
549 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
550 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
551 } else if (!shiftPressed
|| !m_selectionManager
->isAnchoredSelectionActive()) {
552 // Select the pressed item and start a new anchored selection
553 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Select
);
554 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
563 if (event
->buttons() & Qt::RightButton
) {
564 emit
itemContextMenuRequested(m_pressedIndex
, event
->screenPos());
570 if (event
->buttons() & Qt::RightButton
) {
571 const QRectF headerBounds
= m_view
->headerBoundaries();
572 if (headerBounds
.contains(event
->pos())) {
573 emit
headerContextMenuRequested(event
->screenPos());
575 emit
viewContextMenuRequested(event
->screenPos());
580 if (m_selectionBehavior
== MultiSelection
) {
581 QPointF startPos
= m_pressedMousePos
;
582 if (m_view
->scrollOrientation() == Qt::Vertical
) {
583 startPos
.ry() += m_view
->scrollOffset();
584 if (m_view
->itemSize().width() < 0) {
585 // Use a special rubberband for views that have only one column and
586 // expand the rubberband to use the whole width of the view.
590 startPos
.rx() += m_view
->scrollOffset();
593 m_oldSelection
= m_selectionManager
->selectedItems();
594 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
595 rubberBand
->setStartPosition(startPos
);
596 rubberBand
->setEndPosition(startPos
);
597 rubberBand
->setActive(true);
598 connect(rubberBand
, SIGNAL(endPositionChanged(QPointF
,QPointF
)), this, SLOT(slotRubberBandChanged()));
599 m_view
->setAutoScroll(true);
605 bool KItemListController::mouseMoveEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
611 if (m_pressedIndex
>= 0) {
612 // Check whether a dragging should be started
613 if (event
->buttons() & Qt::LeftButton
) {
614 const QPointF pos
= transform
.map(event
->pos());
615 if ((pos
- m_pressedMousePos
).manhattanLength() >= QApplication::startDragDistance()) {
616 if (!m_selectionManager
->isSelected(m_pressedIndex
)) {
617 // Always assure that the dragged item gets selected. Usually this is already
618 // done on the mouse-press event, but when using the selection-toggle on a
619 // selected item the dragged item is not selected yet.
620 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
622 // A selected item has been clicked to drag all selected items
623 // -> the selection should not be cleared when the mouse button is released.
624 m_clearSelectionIfItemsAreNotDragged
= false;
631 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
632 if (rubberBand
->isActive()) {
633 QPointF endPos
= transform
.map(event
->pos());
635 // Update the current item.
636 const int newCurrent
= m_view
->itemAt(endPos
);
637 if (newCurrent
>= 0) {
638 // It's expected that the new current index is also the new anchor (bug 163451).
639 m_selectionManager
->endAnchoredSelection();
640 m_selectionManager
->setCurrentItem(newCurrent
);
641 m_selectionManager
->beginAnchoredSelection(newCurrent
);
644 if (m_view
->scrollOrientation() == Qt::Vertical
) {
645 endPos
.ry() += m_view
->scrollOffset();
646 if (m_view
->itemSize().width() < 0) {
647 // Use a special rubberband for views that have only one column and
648 // expand the rubberband to use the whole width of the view.
649 endPos
.setX(m_view
->size().width());
652 endPos
.rx() += m_view
->scrollOffset();
654 rubberBand
->setEndPosition(endPos
);
661 bool KItemListController::mouseReleaseEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
667 emit
mouseButtonReleased(m_pressedIndex
, event
->buttons());
669 const bool isAboveSelectionToggle
= m_view
->isAboveSelectionToggle(m_pressedIndex
, m_pressedMousePos
);
670 if (isAboveSelectionToggle
) {
671 m_selectionTogglePressed
= false;
675 if (!isAboveSelectionToggle
&& m_selectionTogglePressed
) {
676 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
677 m_selectionTogglePressed
= false;
681 const bool shiftOrControlPressed
= event
->modifiers() & Qt::ShiftModifier
||
682 event
->modifiers() & Qt::ControlModifier
;
684 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
685 if (rubberBand
->isActive()) {
686 disconnect(rubberBand
, SIGNAL(endPositionChanged(QPointF
,QPointF
)), this, SLOT(slotRubberBandChanged()));
687 rubberBand
->setActive(false);
688 m_oldSelection
.clear();
689 m_view
->setAutoScroll(false);
692 const QPointF pos
= transform
.map(event
->pos());
693 const int index
= m_view
->itemAt(pos
);
695 if (index
>= 0 && index
== m_pressedIndex
) {
696 // The release event is done above the same item as the press event
698 if (m_clearSelectionIfItemsAreNotDragged
) {
699 // A selected item has been clicked, but no drag operation has been started
700 // -> clear the rest of the selection.
701 m_selectionManager
->clearSelection();
702 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Select
);
703 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
706 if (event
->button() & Qt::LeftButton
) {
707 bool emitItemActivated
= true;
708 if (m_view
->isAboveExpansionToggle(index
, pos
)) {
709 const bool expanded
= m_model
->isExpanded(index
);
710 m_model
->setExpanded(index
, !expanded
);
712 emit
itemExpansionToggleClicked(index
);
713 emitItemActivated
= false;
714 } else if (shiftOrControlPressed
) {
715 // The mouse click should only update the selection, not trigger the item
716 emitItemActivated
= false;
717 } else if (!m_singleClickActivation
) {
718 emitItemActivated
= false;
720 if (emitItemActivated
) {
721 emit
itemActivated(index
);
723 } else if (event
->button() & Qt::MidButton
) {
724 emit
itemMiddleClicked(index
);
728 m_pressedMousePos
= QPointF();
730 m_clearSelectionIfItemsAreNotDragged
= false;
734 bool KItemListController::mouseDoubleClickEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
736 const QPointF pos
= transform
.map(event
->pos());
737 const int index
= m_view
->itemAt(pos
);
739 bool emitItemActivated
= !m_singleClickActivation
&&
740 (event
->button() & Qt::LeftButton
) &&
741 index
>= 0 && index
< m_model
->count();
742 if (emitItemActivated
) {
743 emit
itemActivated(index
);
748 bool KItemListController::dragEnterEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
755 bool KItemListController::dragLeaveEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
760 m_view
->setAutoScroll(false);
761 m_view
->hideDropIndicator();
763 KItemListWidget
* widget
= hoveredWidget();
765 widget
->setHovered(false);
766 emit
itemUnhovered(widget
->index());
771 bool KItemListController::dragMoveEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
774 if (!m_model
|| !m_view
) {
778 event
->acceptProposedAction();
780 KItemListWidget
* oldHoveredWidget
= hoveredWidget();
782 const QPointF pos
= transform
.map(event
->pos());
783 KItemListWidget
* newHoveredWidget
= widgetForPos(pos
);
785 if (oldHoveredWidget
!= newHoveredWidget
) {
786 m_autoActivationTimer
->stop();
788 if (oldHoveredWidget
) {
789 oldHoveredWidget
->setHovered(false);
790 emit
itemUnhovered(oldHoveredWidget
->index());
793 if (newHoveredWidget
) {
794 const int index
= newHoveredWidget
->index();
795 if (m_model
->supportsDropping(index
)) {
796 newHoveredWidget
->setHovered(true);
797 } else if (m_model
->sortRole().isEmpty()) {
798 // The model supports inserting of items on
799 // the given index. Assure that a drop-indicator
800 // is shown by the view.
801 m_view
->showDropIndicator(pos
);
803 emit
itemHovered(index
);
805 if (m_autoActivationTimer
->interval() >= 0) {
806 m_autoActivationTimer
->setProperty("index", index
);
807 m_autoActivationTimer
->start();
815 bool KItemListController::dropEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
822 m_autoActivationTimer
->stop();
823 m_view
->setAutoScroll(false);
825 const QPointF pos
= transform
.map(event
->pos());
826 if (m_model
->sortRole().isEmpty()) {
827 // The model supports inserting of items on
829 const int dropIndex
= m_view
->showDropIndicator(pos
);
830 m_view
->hideDropIndicator();
831 emit
itemDropEvent(dropIndex
, event
);
833 emit
itemDropEvent(m_view
->itemAt(pos
), event
);
839 bool KItemListController::hoverEnterEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
846 bool KItemListController::hoverMoveEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
849 if (!m_model
|| !m_view
) {
853 KItemListWidget
* oldHoveredWidget
= hoveredWidget();
854 const QPointF pos
= transform
.map(event
->pos());
855 KItemListWidget
* newHoveredWidget
= widgetForPos(pos
);
857 if (oldHoveredWidget
!= newHoveredWidget
) {
858 if (oldHoveredWidget
) {
859 oldHoveredWidget
->setHovered(false);
860 emit
itemUnhovered(oldHoveredWidget
->index());
863 if (newHoveredWidget
) {
864 newHoveredWidget
->setHovered(true);
865 emit
itemHovered(newHoveredWidget
->index());
872 bool KItemListController::hoverLeaveEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
877 if (!m_model
|| !m_view
) {
881 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
882 if (widget
->isHovered()) {
883 widget
->setHovered(false);
884 emit
itemUnhovered(widget
->index());
890 bool KItemListController::wheelEvent(QGraphicsSceneWheelEvent
* event
, const QTransform
& transform
)
897 bool KItemListController::resizeEvent(QGraphicsSceneResizeEvent
* event
, const QTransform
& transform
)
904 bool KItemListController::processEvent(QEvent
* event
, const QTransform
& transform
)
910 switch (event
->type()) {
911 case QEvent::KeyPress
:
912 return keyPressEvent(static_cast<QKeyEvent
*>(event
));
913 case QEvent::InputMethod
:
914 return inputMethodEvent(static_cast<QInputMethodEvent
*>(event
));
915 case QEvent::GraphicsSceneMousePress
:
916 return mousePressEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
917 case QEvent::GraphicsSceneMouseMove
:
918 return mouseMoveEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
919 case QEvent::GraphicsSceneMouseRelease
:
920 return mouseReleaseEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
921 case QEvent::GraphicsSceneMouseDoubleClick
:
922 return mouseDoubleClickEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
923 case QEvent::GraphicsSceneWheel
:
924 return wheelEvent(static_cast<QGraphicsSceneWheelEvent
*>(event
), QTransform());
925 case QEvent::GraphicsSceneDragEnter
:
926 return dragEnterEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
927 case QEvent::GraphicsSceneDragLeave
:
928 return dragLeaveEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
929 case QEvent::GraphicsSceneDragMove
:
930 return dragMoveEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
931 case QEvent::GraphicsSceneDrop
:
932 return dropEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
933 case QEvent::GraphicsSceneHoverEnter
:
934 return hoverEnterEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
935 case QEvent::GraphicsSceneHoverMove
:
936 return hoverMoveEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
937 case QEvent::GraphicsSceneHoverLeave
:
938 return hoverLeaveEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
939 case QEvent::GraphicsSceneResize
:
940 return resizeEvent(static_cast<QGraphicsSceneResizeEvent
*>(event
), transform
);
948 void KItemListController::slotViewScrollOffsetChanged(qreal current
, qreal previous
)
954 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
955 if (rubberBand
->isActive()) {
956 const qreal diff
= current
- previous
;
957 // TODO: Ideally just QCursor::pos() should be used as
958 // new end-position but it seems there is no easy way
959 // to have something like QWidget::mapFromGlobal() for QGraphicsWidget
960 // (... or I just missed an easy way to do the mapping)
961 QPointF endPos
= rubberBand
->endPosition();
962 if (m_view
->scrollOrientation() == Qt::Vertical
) {
968 rubberBand
->setEndPosition(endPos
);
972 void KItemListController::slotRubberBandChanged()
974 if (!m_view
|| !m_model
|| m_model
->count() <= 0) {
978 const KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
979 const QPointF startPos
= rubberBand
->startPosition();
980 const QPointF endPos
= rubberBand
->endPosition();
981 QRectF rubberBandRect
= QRectF(startPos
, endPos
).normalized();
983 const bool scrollVertical
= (m_view
->scrollOrientation() == Qt::Vertical
);
984 if (scrollVertical
) {
985 rubberBandRect
.translate(0, -m_view
->scrollOffset());
987 rubberBandRect
.translate(-m_view
->scrollOffset(), 0);
990 if (!m_oldSelection
.isEmpty()) {
991 // Clear the old selection that was available before the rubberband has
992 // been activated in case if no Shift- or Control-key are pressed
993 const bool shiftOrControlPressed
= QApplication::keyboardModifiers() & Qt::ShiftModifier
||
994 QApplication::keyboardModifiers() & Qt::ControlModifier
;
995 if (!shiftOrControlPressed
) {
996 m_oldSelection
.clear();
1000 QSet
<int> selectedItems
;
1002 // Select all visible items that intersect with the rubberband
1003 foreach (const KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
1004 const int index
= widget
->index();
1006 const QRectF widgetRect
= m_view
->itemRect(index
);
1007 if (widgetRect
.intersects(rubberBandRect
)) {
1008 const QRectF iconRect
= widget
->iconRect().translated(widgetRect
.topLeft());
1009 const QRectF textRect
= widget
->textRect().translated(widgetRect
.topLeft());
1010 if (iconRect
.intersects(rubberBandRect
) || textRect
.intersects(rubberBandRect
)) {
1011 selectedItems
.insert(index
);
1016 // Select all invisible items that intersect with the rubberband. Instead of
1017 // iterating all items only the area which might be touched by the rubberband
1019 const bool increaseIndex
= scrollVertical
?
1020 startPos
.y() > endPos
.y(): startPos
.x() > endPos
.x();
1022 int index
= increaseIndex
? m_view
->lastVisibleIndex() + 1 : m_view
->firstVisibleIndex() - 1;
1023 bool selectionFinished
= false;
1025 const QRectF widgetRect
= m_view
->itemRect(index
);
1026 if (widgetRect
.intersects(rubberBandRect
)) {
1027 selectedItems
.insert(index
);
1030 if (increaseIndex
) {
1032 selectionFinished
= (index
>= m_model
->count()) ||
1033 ( scrollVertical
&& widgetRect
.top() > rubberBandRect
.bottom()) ||
1034 (!scrollVertical
&& widgetRect
.left() > rubberBandRect
.right());
1037 selectionFinished
= (index
< 0) ||
1038 ( scrollVertical
&& widgetRect
.bottom() < rubberBandRect
.top()) ||
1039 (!scrollVertical
&& widgetRect
.right() < rubberBandRect
.left());
1041 } while (!selectionFinished
);
1043 if (QApplication::keyboardModifiers() & Qt::ControlModifier
) {
1044 // If Control is pressed, the selection state of all items in the rubberband is toggled.
1045 // Therefore, the new selection contains:
1046 // 1. All previously selected items which are not inside the rubberband, and
1047 // 2. all items inside the rubberband which have not been selected previously.
1048 m_selectionManager
->setSelectedItems((m_oldSelection
- selectedItems
) + (selectedItems
- m_oldSelection
));
1051 m_selectionManager
->setSelectedItems(selectedItems
+ m_oldSelection
);
1055 void KItemListController::startDragging()
1057 if (!m_view
|| !m_model
) {
1061 const QSet
<int> selectedItems
= m_selectionManager
->selectedItems();
1062 if (selectedItems
.isEmpty()) {
1066 QMimeData
* data
= m_model
->createMimeData(selectedItems
);
1071 // The created drag object will be owned and deleted
1072 // by QApplication::activeWindow().
1073 QDrag
* drag
= new QDrag(QApplication::activeWindow());
1074 drag
->setMimeData(data
);
1076 const QPixmap pixmap
= m_view
->createDragPixmap(selectedItems
);
1077 drag
->setPixmap(pixmap
);
1079 // TODO: The vertical hotspot of -24 should be replaced by the
1080 // height of the QCursor-pixmap.
1081 const QPoint
hotSpot(pixmap
.width() / 2, -24);
1082 drag
->setHotSpot(hotSpot
);
1084 drag
->exec(Qt::MoveAction
| Qt::CopyAction
| Qt::LinkAction
, Qt::CopyAction
);
1087 KItemListWidget
* KItemListController::hoveredWidget() const
1091 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
1092 if (widget
->isHovered()) {
1100 KItemListWidget
* KItemListController::widgetForPos(const QPointF
& pos
) const
1104 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
1105 const QPointF mappedPos
= widget
->mapFromItem(m_view
, pos
);
1107 const bool hovered
= widget
->contains(mappedPos
) &&
1108 !widget
->expansionToggleRect().contains(mappedPos
);
1117 void KItemListController::updateKeyboardAnchor()
1119 const bool validAnchor
= m_keyboardAnchorIndex
>= 0 &&
1120 m_keyboardAnchorIndex
< m_model
->count() &&
1121 keyboardAnchorPos(m_keyboardAnchorIndex
) == m_keyboardAnchorPos
;
1123 const int index
= m_selectionManager
->currentItem();
1124 m_keyboardAnchorIndex
= index
;
1125 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
1129 int KItemListController::nextRowIndex(int index
) const
1131 if (m_keyboardAnchorIndex
< 0) {
1135 const int maxIndex
= m_model
->count() - 1;
1136 if (index
== maxIndex
) {
1140 // Calculate the index of the last column inside the row of the current index
1141 int lastColumnIndex
= index
;
1142 while (keyboardAnchorPos(lastColumnIndex
+ 1) > keyboardAnchorPos(lastColumnIndex
)) {
1144 if (lastColumnIndex
>= maxIndex
) {
1149 // Based on the last column index go to the next row and calculate the nearest index
1150 // that is below the current index
1151 int nextRowIndex
= lastColumnIndex
+ 1;
1152 int searchIndex
= nextRowIndex
;
1153 qreal minDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(nextRowIndex
));
1154 while (searchIndex
< maxIndex
&& keyboardAnchorPos(searchIndex
+ 1) > keyboardAnchorPos(searchIndex
)) {
1156 const qreal searchDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(searchIndex
));
1157 if (searchDiff
< minDiff
) {
1158 minDiff
= searchDiff
;
1159 nextRowIndex
= searchIndex
;
1163 return nextRowIndex
;
1166 int KItemListController::previousRowIndex(int index
) const
1168 if (m_keyboardAnchorIndex
< 0 || index
== 0) {
1172 // Calculate the index of the first column inside the row of the current index
1173 int firstColumnIndex
= index
;
1174 while (keyboardAnchorPos(firstColumnIndex
- 1) < keyboardAnchorPos(firstColumnIndex
)) {
1176 if (firstColumnIndex
<= 0) {
1181 // Based on the first column index go to the previous row and calculate the nearest index
1182 // that is above the current index
1183 int previousRowIndex
= firstColumnIndex
- 1;
1184 int searchIndex
= previousRowIndex
;
1185 qreal minDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(previousRowIndex
));
1186 while (searchIndex
> 0 && keyboardAnchorPos(searchIndex
- 1) < keyboardAnchorPos(searchIndex
)) {
1188 const qreal searchDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(searchIndex
));
1189 if (searchDiff
< minDiff
) {
1190 minDiff
= searchDiff
;
1191 previousRowIndex
= searchIndex
;
1195 return previousRowIndex
;
1198 qreal
KItemListController::keyboardAnchorPos(int index
) const
1200 const QRectF itemRect
= m_view
->itemRect(index
);
1201 if (!itemRect
.isEmpty()) {
1202 return (m_view
->scrollOrientation() == Qt::Vertical
) ? itemRect
.x() : itemRect
.y();
1208 void KItemListController::updateExtendedSelectionRegion()
1211 const bool extend
= (m_selectionBehavior
!= MultiSelection
);
1212 KItemListStyleOption option
= m_view
->styleOption();
1213 if (option
.extendedSelectionRegion
!= extend
) {
1214 option
.extendedSelectionRegion
= extend
;
1215 m_view
->setStyleOption(option
);
1220 #include "kitemlistcontroller.moc"