2 * SPDX-FileCopyrightText: 2011 Peter Penz <peter.penz19@gmail.com>
3 * SPDX-FileCopyrightText: 2012 Frank Reininghaus <frank78ac@googlemail.com>
5 * Based on the Itemviews NG project from Trolltech Labs
7 * SPDX-License-Identifier: GPL-2.0-or-later
10 #include "kitemlistcontroller.h"
12 #include "kitemlistselectionmanager.h"
13 #include "kitemlistview.h"
14 #include "private/kitemlistkeyboardsearchmanager.h"
15 #include "private/kitemlistrubberband.h"
16 #include "private/ktwofingerswipe.h"
17 #include "private/ktwofingertap.h"
18 #include "views/draganddrophelper.h"
20 #include <QAccessible>
21 #include <QApplication>
24 #include <QGraphicsScene>
25 #include <QGraphicsSceneEvent>
26 #include <QGraphicsView>
30 KItemListController::KItemListController(KItemModelBase
* model
, KItemListView
* view
, QObject
* parent
) :
32 m_singleClickActivationEnforced(false),
33 m_selectionTogglePressed(false),
34 m_clearSelectionIfItemsAreNotDragged(false),
35 m_isSwipeGesture(false),
36 m_dragActionOrRightClick(false),
37 m_scrollerIsScrolling(false),
38 m_pinchGestureInProgress(false),
40 m_selectionBehavior(NoSelection
),
41 m_autoActivationBehavior(ActivationAndExpansion
),
42 m_mouseDoubleClickAction(ActivateItemOnly
),
45 m_selectionManager(new KItemListSelectionManager(this)),
46 m_keyboardManager(new KItemListKeyboardSearchManager(this)),
49 m_autoActivationTimer(nullptr),
50 m_swipeGesture(Qt::CustomGesture
),
51 m_twoFingerTapGesture(Qt::CustomGesture
),
52 m_lastSource(Qt::MouseEventNotSynthesized
),
54 m_keyboardAnchorIndex(-1),
55 m_keyboardAnchorPos(0)
57 connect(m_keyboardManager
, &KItemListKeyboardSearchManager::changeCurrentItem
,
58 this, &KItemListController::slotChangeCurrentItem
);
59 connect(m_selectionManager
, &KItemListSelectionManager::currentChanged
,
60 m_keyboardManager
, &KItemListKeyboardSearchManager::slotCurrentChanged
);
61 connect(m_selectionManager
, &KItemListSelectionManager::selectionChanged
,
62 m_keyboardManager
, &KItemListKeyboardSearchManager::slotSelectionChanged
);
64 m_autoActivationTimer
= new QTimer(this);
65 m_autoActivationTimer
->setSingleShot(true);
66 m_autoActivationTimer
->setInterval(-1);
67 connect(m_autoActivationTimer
, &QTimer::timeout
, this, &KItemListController::slotAutoActivationTimeout
);
72 m_swipeGesture
= QGestureRecognizer::registerRecognizer(new KTwoFingerSwipeRecognizer());
73 m_twoFingerTapGesture
= QGestureRecognizer::registerRecognizer(new KTwoFingerTapRecognizer());
74 view
->grabGesture(m_swipeGesture
);
75 view
->grabGesture(m_twoFingerTapGesture
);
76 view
->grabGesture(Qt::TapGesture
);
77 view
->grabGesture(Qt::TapAndHoldGesture
);
78 view
->grabGesture(Qt::PinchGesture
);
81 KItemListController::~KItemListController()
90 void KItemListController::setModel(KItemModelBase
* model
)
92 if (m_model
== model
) {
96 KItemModelBase
* oldModel
= m_model
;
98 oldModel
->deleteLater();
103 m_model
->setParent(this);
107 m_view
->setModel(m_model
);
110 m_selectionManager
->setModel(m_model
);
112 emit
modelChanged(m_model
, oldModel
);
115 KItemModelBase
* KItemListController::model() const
120 KItemListSelectionManager
* KItemListController::selectionManager() const
122 return m_selectionManager
;
125 void KItemListController::setView(KItemListView
* view
)
127 if (m_view
== view
) {
131 KItemListView
* oldView
= m_view
;
133 disconnect(oldView
, &KItemListView::scrollOffsetChanged
, this, &KItemListController::slotViewScrollOffsetChanged
);
134 oldView
->deleteLater();
140 m_view
->setParent(this);
141 m_view
->setController(this);
142 m_view
->setModel(m_model
);
143 connect(m_view
, &KItemListView::scrollOffsetChanged
, this, &KItemListController::slotViewScrollOffsetChanged
);
144 updateExtendedSelectionRegion();
147 emit
viewChanged(m_view
, oldView
);
150 KItemListView
* KItemListController::view() const
155 void KItemListController::setSelectionBehavior(SelectionBehavior behavior
)
157 m_selectionBehavior
= behavior
;
158 updateExtendedSelectionRegion();
161 KItemListController::SelectionBehavior
KItemListController::selectionBehavior() const
163 return m_selectionBehavior
;
166 void KItemListController::setAutoActivationBehavior(AutoActivationBehavior behavior
)
168 m_autoActivationBehavior
= behavior
;
171 KItemListController::AutoActivationBehavior
KItemListController::autoActivationBehavior() const
173 return m_autoActivationBehavior
;
176 void KItemListController::setMouseDoubleClickAction(MouseDoubleClickAction action
)
178 m_mouseDoubleClickAction
= action
;
181 KItemListController::MouseDoubleClickAction
KItemListController::mouseDoubleClickAction() const
183 return m_mouseDoubleClickAction
;
186 int KItemListController::indexCloseToMousePressedPosition() const
188 QHashIterator
<KItemListWidget
*, KItemListGroupHeader
*> it(m_view
->m_visibleGroups
);
189 while (it
.hasNext()) {
191 KItemListGroupHeader
*groupHeader
= it
.value();
192 const QPointF mappedToGroup
= groupHeader
->mapFromItem(nullptr, m_pressedMousePos
);
193 if (groupHeader
->contains(mappedToGroup
)) {
194 return it
.key()->index();
200 void KItemListController::setAutoActivationDelay(int delay
)
202 m_autoActivationTimer
->setInterval(delay
);
205 int KItemListController::autoActivationDelay() const
207 return m_autoActivationTimer
->interval();
210 void KItemListController::setSingleClickActivationEnforced(bool singleClick
)
212 m_singleClickActivationEnforced
= singleClick
;
215 bool KItemListController::singleClickActivationEnforced() const
217 return m_singleClickActivationEnforced
;
220 bool KItemListController::keyPressEvent(QKeyEvent
* event
)
222 int index
= m_selectionManager
->currentItem();
223 int key
= event
->key();
225 // Handle the expanding/collapsing of items
226 if (m_view
->supportsItemExpanding() && m_model
->isExpandable(index
)) {
227 if (key
== Qt::Key_Right
) {
228 if (m_model
->setExpanded(index
, true)) {
231 } else if (key
== Qt::Key_Left
) {
232 if (m_model
->setExpanded(index
, false)) {
238 const bool shiftPressed
= event
->modifiers() & Qt::ShiftModifier
;
239 const bool controlPressed
= event
->modifiers() & Qt::ControlModifier
;
240 const bool shiftOrControlPressed
= shiftPressed
|| controlPressed
;
241 const bool navigationPressed
= key
== Qt::Key_Home
|| key
== Qt::Key_End
||
242 key
== Qt::Key_PageUp
|| key
== Qt::Key_PageDown
||
243 key
== Qt::Key_Up
|| key
== Qt::Key_Down
||
244 key
== Qt::Key_Left
|| key
== Qt::Key_Right
;
246 const int itemCount
= m_model
->count();
248 // For horizontal scroll orientation, transform
249 // the arrow keys to simplify the event handling.
250 if (m_view
->scrollOrientation() == Qt::Horizontal
) {
252 case Qt::Key_Up
: key
= Qt::Key_Left
; break;
253 case Qt::Key_Down
: key
= Qt::Key_Right
; break;
254 case Qt::Key_Left
: key
= Qt::Key_Up
; break;
255 case Qt::Key_Right
: key
= Qt::Key_Down
; break;
260 const bool selectSingleItem
= m_selectionBehavior
!= NoSelection
&& itemCount
== 1 && navigationPressed
;
262 if (selectSingleItem
) {
263 const int current
= m_selectionManager
->currentItem();
264 m_selectionManager
->setSelected(current
);
271 m_keyboardAnchorIndex
= index
;
272 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
276 index
= itemCount
- 1;
277 m_keyboardAnchorIndex
= index
;
278 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
283 const int expandedParentsCount
= m_model
->expandedParentsCount(index
);
284 if (expandedParentsCount
== 0) {
287 // Go to the parent of the current item.
290 } while (index
> 0 && m_model
->expandedParentsCount(index
) == expandedParentsCount
);
292 m_keyboardAnchorIndex
= index
;
293 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
298 if (index
< itemCount
- 1) {
300 m_keyboardAnchorIndex
= index
;
301 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
306 updateKeyboardAnchor();
307 index
= previousRowIndex(index
);
311 updateKeyboardAnchor();
312 index
= nextRowIndex(index
);
316 if (m_view
->scrollOrientation() == Qt::Horizontal
) {
317 // The new current index should correspond to the first item in the current column.
318 int newIndex
= qMax(index
- 1, 0);
319 while (newIndex
!= index
&& m_view
->itemRect(newIndex
).topLeft().y() < m_view
->itemRect(index
).topLeft().y()) {
321 newIndex
= qMax(index
- 1, 0);
323 m_keyboardAnchorIndex
= index
;
324 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
326 const qreal currentItemBottom
= m_view
->itemRect(index
).bottomLeft().y();
327 const qreal height
= m_view
->geometry().height();
329 // The new current item should be the first item in the current
330 // column whose itemRect's top coordinate is larger than targetY.
331 const qreal targetY
= currentItemBottom
- height
;
333 updateKeyboardAnchor();
334 int newIndex
= previousRowIndex(index
);
337 updateKeyboardAnchor();
338 newIndex
= previousRowIndex(index
);
339 } while (m_view
->itemRect(newIndex
).topLeft().y() > targetY
&& newIndex
!= index
);
343 case Qt::Key_PageDown
:
344 if (m_view
->scrollOrientation() == Qt::Horizontal
) {
345 // The new current index should correspond to the last item in the current column.
346 int newIndex
= qMin(index
+ 1, m_model
->count() - 1);
347 while (newIndex
!= index
&& m_view
->itemRect(newIndex
).topLeft().y() > m_view
->itemRect(index
).topLeft().y()) {
349 newIndex
= qMin(index
+ 1, m_model
->count() - 1);
351 m_keyboardAnchorIndex
= index
;
352 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
354 const qreal currentItemTop
= m_view
->itemRect(index
).topLeft().y();
355 const qreal height
= m_view
->geometry().height();
357 // The new current item should be the last item in the current
358 // column whose itemRect's bottom coordinate is smaller than targetY.
359 const qreal targetY
= currentItemTop
+ height
;
361 updateKeyboardAnchor();
362 int newIndex
= nextRowIndex(index
);
365 updateKeyboardAnchor();
366 newIndex
= nextRowIndex(index
);
367 } while (m_view
->itemRect(newIndex
).bottomLeft().y() < targetY
&& newIndex
!= index
);
372 case Qt::Key_Return
: {
373 const KItemSet selectedItems
= m_selectionManager
->selectedItems();
374 if (selectedItems
.count() >= 2) {
375 emit
itemsActivated(selectedItems
);
376 } else if (selectedItems
.count() == 1) {
377 emit
itemActivated(selectedItems
.first());
379 emit
itemActivated(index
);
385 // Emit the signal itemContextMenuRequested() in case if at least one
386 // item is selected. Otherwise the signal viewContextMenuRequested() will be emitted.
387 const KItemSet selectedItems
= m_selectionManager
->selectedItems();
389 if (selectedItems
.count() >= 2) {
390 const int currentItemIndex
= m_selectionManager
->currentItem();
391 index
= selectedItems
.contains(currentItemIndex
)
392 ? currentItemIndex
: selectedItems
.first();
393 } else if (selectedItems
.count() == 1) {
394 index
= selectedItems
.first();
398 const QRectF contextRect
= m_view
->itemContextRect(index
);
399 const QPointF
pos(m_view
->scene()->views().first()->mapToGlobal(contextRect
.bottomRight().toPoint()));
400 emit
itemContextMenuRequested(index
, pos
);
402 emit
viewContextMenuRequested(QCursor::pos());
408 if (m_selectionBehavior
!= SingleSelection
) {
409 m_selectionManager
->clearSelection();
411 m_keyboardManager
->cancelSearch();
412 emit
escapePressed();
416 if (m_selectionBehavior
== MultiSelection
) {
417 if (controlPressed
) {
418 // Toggle the selection state of the current item.
419 m_selectionManager
->endAnchoredSelection();
420 m_selectionManager
->setSelected(index
, 1, KItemListSelectionManager::Toggle
);
421 m_selectionManager
->beginAnchoredSelection(index
);
424 // Select the current item if it is not selected yet.
425 const int current
= m_selectionManager
->currentItem();
426 if (!m_selectionManager
->isSelected(current
)) {
427 m_selectionManager
->setSelected(current
);
432 Q_FALLTHROUGH(); // fall through to the default case and add the Space to the current search string.
434 m_keyboardManager
->addKeys(event
->text());
435 // Make sure unconsumed events get propagated up the chain. #302329
440 if (m_selectionManager
->currentItem() != index
) {
441 switch (m_selectionBehavior
) {
443 m_selectionManager
->setCurrentItem(index
);
446 case SingleSelection
:
447 m_selectionManager
->setCurrentItem(index
);
448 m_selectionManager
->clearSelection();
449 m_selectionManager
->setSelected(index
, 1);
453 if (controlPressed
) {
454 m_selectionManager
->endAnchoredSelection();
457 m_selectionManager
->setCurrentItem(index
);
459 if (!shiftOrControlPressed
) {
460 m_selectionManager
->clearSelection();
461 m_selectionManager
->setSelected(index
, 1);
465 m_selectionManager
->beginAnchoredSelection(index
);
471 if (navigationPressed
) {
472 m_view
->scrollToItem(index
);
477 void KItemListController::slotChangeCurrentItem(const QString
& text
, bool searchFromNextItem
)
479 if (!m_model
|| m_model
->count() == 0) {
483 if (searchFromNextItem
) {
484 const int currentIndex
= m_selectionManager
->currentItem();
485 index
= m_model
->indexForKeyboardSearch(text
, (currentIndex
+ 1) % m_model
->count());
487 index
= m_model
->indexForKeyboardSearch(text
, 0);
490 m_selectionManager
->setCurrentItem(index
);
492 if (m_selectionBehavior
!= NoSelection
) {
493 m_selectionManager
->replaceSelection(index
);
494 m_selectionManager
->beginAnchoredSelection(index
);
497 m_view
->scrollToItem(index
);
501 void KItemListController::slotAutoActivationTimeout()
503 if (!m_model
|| !m_view
) {
507 const int index
= m_autoActivationTimer
->property("index").toInt();
508 if (index
< 0 || index
>= m_model
->count()) {
512 /* m_view->isUnderMouse() fixes a bug in the Folder-View-Panel and in the
515 * Bug: When you drag a file onto a Folder-View-Item or a Places-Item and
516 * then move away before the auto-activation timeout triggers, than the
517 * item still becomes activated/expanded.
519 * See Bug 293200 and 305783
521 if (m_model
->supportsDropping(index
) && m_view
->isUnderMouse()) {
522 if (m_view
->supportsItemExpanding() && m_model
->isExpandable(index
)) {
523 const bool expanded
= m_model
->isExpanded(index
);
524 m_model
->setExpanded(index
, !expanded
);
525 } else if (m_autoActivationBehavior
!= ExpansionOnly
) {
526 emit
itemActivated(index
);
531 bool KItemListController::inputMethodEvent(QInputMethodEvent
* event
)
537 bool KItemListController::mousePressEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
540 m_lastSource
= event
->source();
542 if (event
->source() == Qt::MouseEventSynthesizedByQt
) {
550 m_pressedMousePos
= transform
.map(event
->pos());
551 m_pressedIndex
= m_view
->itemAt(m_pressedMousePos
);
553 const Qt::MouseButtons buttons
= event
->buttons();
555 if (!onPress(event
->screenPos(), event
->pos(), event
->modifiers(), buttons
)) {
560 if (buttons
& (Qt::BackButton
| Qt::ForwardButton
)) {
561 // Do not select items when clicking the back/forward buttons, see
562 // https://bugs.kde.org/show_bug.cgi?id=327412.
569 bool KItemListController::mouseMoveEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
575 if (m_view
->m_tapAndHoldIndicator
->isActive()) {
576 m_view
->m_tapAndHoldIndicator
->setActive(false);
579 if (event
->source() == Qt::MouseEventSynthesizedByQt
&& !m_dragActionOrRightClick
) {
583 if (m_pressedIndex
>= 0) {
584 // Check whether a dragging should be started
585 if (event
->buttons() & Qt::LeftButton
) {
586 const QPointF pos
= transform
.map(event
->pos());
587 if ((pos
- m_pressedMousePos
).manhattanLength() >= QApplication::startDragDistance()) {
588 if (!m_selectionManager
->isSelected(m_pressedIndex
)) {
589 // Always assure that the dragged item gets selected. Usually this is already
590 // done on the mouse-press event, but when using the selection-toggle on a
591 // selected item the dragged item is not selected yet.
592 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
594 // A selected item has been clicked to drag all selected items
595 // -> the selection should not be cleared when the mouse button is released.
596 m_clearSelectionIfItemsAreNotDragged
= false;
599 m_mousePress
= false;
603 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
604 if (rubberBand
->isActive()) {
605 QPointF endPos
= transform
.map(event
->pos());
607 // Update the current item.
608 const int newCurrent
= m_view
->itemAt(endPos
);
609 if (newCurrent
>= 0) {
610 // It's expected that the new current index is also the new anchor (bug 163451).
611 m_selectionManager
->endAnchoredSelection();
612 m_selectionManager
->setCurrentItem(newCurrent
);
613 m_selectionManager
->beginAnchoredSelection(newCurrent
);
616 if (m_view
->scrollOrientation() == Qt::Vertical
) {
617 endPos
.ry() += m_view
->scrollOffset();
618 if (m_view
->itemSize().width() < 0) {
619 // Use a special rubberband for views that have only one column and
620 // expand the rubberband to use the whole width of the view.
621 endPos
.setX(m_view
->size().width());
624 endPos
.rx() += m_view
->scrollOffset();
626 rubberBand
->setEndPosition(endPos
);
633 bool KItemListController::mouseReleaseEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
635 m_mousePress
= false;
641 if (m_view
->m_tapAndHoldIndicator
->isActive()) {
642 m_view
->m_tapAndHoldIndicator
->setActive(false);
645 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
646 if (event
->source() == Qt::MouseEventSynthesizedByQt
&& !rubberBand
->isActive()) {
650 emit
mouseButtonReleased(m_pressedIndex
, event
->buttons());
652 return onRelease(transform
.map(event
->pos()), event
->modifiers(), event
->button(), false);
655 bool KItemListController::mouseDoubleClickEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
657 const QPointF pos
= transform
.map(event
->pos());
658 const int index
= m_view
->itemAt(pos
);
660 // Expand item if desired - See Bug 295573
661 if (m_mouseDoubleClickAction
!= ActivateItemOnly
) {
662 if (m_view
&& m_model
&& m_view
->supportsItemExpanding() && m_model
->isExpandable(index
)) {
663 const bool expanded
= m_model
->isExpanded(index
);
664 m_model
->setExpanded(index
, !expanded
);
668 if (event
->button() & Qt::RightButton
) {
669 m_selectionManager
->clearSelection();
671 m_selectionManager
->setSelected(index
);
672 emit
itemContextMenuRequested(index
, event
->screenPos());
674 const QRectF headerBounds
= m_view
->headerBoundaries();
675 if (headerBounds
.contains(event
->pos())) {
676 emit
headerContextMenuRequested(event
->screenPos());
678 emit
viewContextMenuRequested(event
->screenPos());
684 bool emitItemActivated
= !(m_view
->style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick
) || m_singleClickActivationEnforced
) &&
685 (event
->button() & Qt::LeftButton
) &&
686 index
>= 0 && index
< m_model
->count();
687 if (emitItemActivated
) {
688 emit
itemActivated(index
);
693 bool KItemListController::dragEnterEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
698 DragAndDropHelper::clearUrlListMatchesUrlCache();
703 bool KItemListController::dragLeaveEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
708 m_autoActivationTimer
->stop();
709 m_view
->setAutoScroll(false);
710 m_view
->hideDropIndicator();
712 KItemListWidget
* widget
= hoveredWidget();
714 widget
->setHovered(false);
715 emit
itemUnhovered(widget
->index());
720 bool KItemListController::dragMoveEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
722 if (!m_model
|| !m_view
) {
727 QUrl hoveredDir
= m_model
->directory();
728 KItemListWidget
* oldHoveredWidget
= hoveredWidget();
730 const QPointF pos
= transform
.map(event
->pos());
731 KItemListWidget
* newHoveredWidget
= widgetForPos(pos
);
733 if (oldHoveredWidget
!= newHoveredWidget
) {
734 m_autoActivationTimer
->stop();
736 if (oldHoveredWidget
) {
737 oldHoveredWidget
->setHovered(false);
738 emit
itemUnhovered(oldHoveredWidget
->index());
742 if (newHoveredWidget
) {
743 bool droppingBetweenItems
= false;
744 if (m_model
->sortRole().isEmpty()) {
745 // The model supports inserting items between other items.
746 droppingBetweenItems
= (m_view
->showDropIndicator(pos
) >= 0);
749 const int index
= newHoveredWidget
->index();
751 if (m_model
->isDir(index
)) {
752 hoveredDir
= m_model
->url(index
);
755 if (!droppingBetweenItems
) {
756 if (m_model
->supportsDropping(index
)) {
757 // Something has been dragged on an item.
758 m_view
->hideDropIndicator();
759 if (!newHoveredWidget
->isHovered()) {
760 newHoveredWidget
->setHovered(true);
761 emit
itemHovered(index
);
764 if (!m_autoActivationTimer
->isActive() && m_autoActivationTimer
->interval() >= 0) {
765 m_autoActivationTimer
->setProperty("index", index
);
766 m_autoActivationTimer
->start();
770 m_autoActivationTimer
->stop();
771 if (newHoveredWidget
&& newHoveredWidget
->isHovered()) {
772 newHoveredWidget
->setHovered(false);
773 emit
itemUnhovered(index
);
777 m_view
->hideDropIndicator();
780 if (DragAndDropHelper::urlListMatchesUrl(event
->mimeData()->urls(), hoveredDir
)) {
781 event
->setDropAction(Qt::IgnoreAction
);
784 event
->setDropAction(event
->proposedAction());
790 bool KItemListController::dropEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
796 m_autoActivationTimer
->stop();
797 m_view
->setAutoScroll(false);
799 const QPointF pos
= transform
.map(event
->pos());
801 int dropAboveIndex
= -1;
802 if (m_model
->sortRole().isEmpty()) {
803 // The model supports inserting of items between other items.
804 dropAboveIndex
= m_view
->showDropIndicator(pos
);
807 if (dropAboveIndex
>= 0) {
808 // Something has been dropped between two items.
809 m_view
->hideDropIndicator();
810 emit
aboveItemDropEvent(dropAboveIndex
, event
);
811 } else if (!event
->mimeData()->hasFormat(m_model
->blacklistItemDropEventMimeType())) {
812 // Something has been dropped on an item or on an empty part of the view.
813 emit
itemDropEvent(m_view
->itemAt(pos
), event
);
816 QAccessibleEvent
accessibilityEvent(view(), QAccessible::DragDropEnd
);
817 QAccessible::updateAccessibility(&accessibilityEvent
);
822 bool KItemListController::hoverEnterEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
829 bool KItemListController::hoverMoveEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
832 if (!m_model
|| !m_view
) {
836 KItemListWidget
* oldHoveredWidget
= hoveredWidget();
837 const QPointF pos
= transform
.map(event
->pos());
838 KItemListWidget
* newHoveredWidget
= widgetForPos(pos
);
840 if (oldHoveredWidget
!= newHoveredWidget
) {
841 if (oldHoveredWidget
) {
842 oldHoveredWidget
->setHovered(false);
843 emit
itemUnhovered(oldHoveredWidget
->index());
846 if (newHoveredWidget
) {
847 newHoveredWidget
->setHovered(true);
848 const QPointF mappedPos
= newHoveredWidget
->mapFromItem(m_view
, pos
);
849 newHoveredWidget
->setHoverPosition(mappedPos
);
850 emit
itemHovered(newHoveredWidget
->index());
852 } else if (oldHoveredWidget
) {
853 const QPointF mappedPos
= oldHoveredWidget
->mapFromItem(m_view
, pos
);
854 oldHoveredWidget
->setHoverPosition(mappedPos
);
860 bool KItemListController::hoverLeaveEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
865 m_mousePress
= false;
867 if (!m_model
|| !m_view
) {
871 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
872 if (widget
->isHovered()) {
873 widget
->setHovered(false);
874 emit
itemUnhovered(widget
->index());
880 bool KItemListController::wheelEvent(QGraphicsSceneWheelEvent
* event
, const QTransform
& transform
)
887 bool KItemListController::resizeEvent(QGraphicsSceneResizeEvent
* event
, const QTransform
& transform
)
894 bool KItemListController::gestureEvent(QGestureEvent
* event
, const QTransform
& transform
)
900 //you can touch on different views at the same time, but only one QWidget gets a mousePressEvent
901 //we use this to get the right QWidget
902 //the only exception is a tap gesture with state GestureStarted, we need to reset some variable
904 if (QGesture
* tap
= event
->gesture(Qt::TapGesture
)) {
905 QTapGesture
* tapGesture
= static_cast<QTapGesture
*>(tap
);
906 if (tapGesture
->state() == Qt::GestureStarted
) {
907 tapTriggered(tapGesture
, transform
);
913 bool accepted
= false;
915 if (QGesture
* tap
= event
->gesture(Qt::TapGesture
)) {
916 tapTriggered(static_cast<QTapGesture
*>(tap
), transform
);
919 if (event
->gesture(Qt::TapAndHoldGesture
)) {
920 tapAndHoldTriggered(event
, transform
);
923 if (event
->gesture(Qt::PinchGesture
)) {
924 pinchTriggered(event
, transform
);
927 if (event
->gesture(m_swipeGesture
)) {
928 swipeTriggered(event
, transform
);
931 if (event
->gesture(m_twoFingerTapGesture
)) {
932 twoFingerTapTriggered(event
, transform
);
938 void KItemListController::tapTriggered(QTapGesture
* tap
, const QTransform
& transform
)
940 static bool scrollerWasActive
= false;
942 if (tap
->state() == Qt::GestureStarted
) {
943 m_dragActionOrRightClick
= false;
944 m_isSwipeGesture
= false;
945 m_pinchGestureInProgress
= false;
946 m_lastSource
= Qt::MouseEventSynthesizedByQt
;
947 scrollerWasActive
= m_scrollerIsScrolling
;
950 if (tap
->state() == Qt::GestureFinished
) {
951 m_mousePress
= false;
953 //if at the moment of the gesture start the QScroller was active, the user made the tap
954 //to stop the QScroller and not to tap on an item
955 if (scrollerWasActive
) {
959 if (m_view
->m_tapAndHoldIndicator
->isActive()) {
960 m_view
->m_tapAndHoldIndicator
->setActive(false);
963 m_pressedMousePos
= transform
.map(tap
->position());
964 m_pressedIndex
= m_view
->itemAt(m_pressedMousePos
);
966 if (m_dragActionOrRightClick
) {
967 onPress(tap
->hotSpot().toPoint(), tap
->position().toPoint(), Qt::NoModifier
, Qt::RightButton
);
968 onRelease(transform
.map(tap
->position()), Qt::NoModifier
, Qt::RightButton
, false);
969 m_dragActionOrRightClick
= false;
972 onPress(tap
->hotSpot().toPoint(), tap
->position().toPoint(), Qt::NoModifier
, Qt::LeftButton
);
973 onRelease(transform
.map(tap
->position()), Qt::NoModifier
, Qt::LeftButton
, true);
978 void KItemListController::tapAndHoldTriggered(QGestureEvent
* event
, const QTransform
& transform
)
981 //the Qt TabAndHold gesture is triggerable with a mouse click, we don't want this
982 if (m_lastSource
== Qt::MouseEventNotSynthesized
) {
986 const QTapAndHoldGesture
* tap
= static_cast<QTapAndHoldGesture
*>(event
->gesture(Qt::TapAndHoldGesture
));
987 if (tap
->state() == Qt::GestureFinished
) {
988 //if a pinch gesture is in progress we don't want a TabAndHold gesture
989 if (m_pinchGestureInProgress
) {
992 m_pressedMousePos
= transform
.map(event
->mapToGraphicsScene(tap
->position()));
993 m_pressedIndex
= m_view
->itemAt(m_pressedMousePos
);
995 if (m_pressedIndex
>= 0 && !m_selectionManager
->isSelected(m_pressedIndex
)) {
996 m_selectionManager
->clearSelection();
997 m_selectionManager
->setSelected(m_pressedIndex
);
998 } else if (m_pressedIndex
== -1) {
999 m_selectionManager
->clearSelection();
1003 emit
scrollerStop();
1005 m_view
->m_tapAndHoldIndicator
->setStartPosition(m_pressedMousePos
);
1006 m_view
->m_tapAndHoldIndicator
->setActive(true);
1008 m_dragActionOrRightClick
= true;
1012 void KItemListController::pinchTriggered(QGestureEvent
* event
, const QTransform
& transform
)
1016 const QPinchGesture
* pinch
= static_cast<QPinchGesture
*>(event
->gesture(Qt::PinchGesture
));
1017 const qreal sensitivityModifier
= 0.2;
1018 static qreal counter
= 0;
1020 if (pinch
->state() == Qt::GestureStarted
) {
1021 m_pinchGestureInProgress
= true;
1024 if (pinch
->state() == Qt::GestureUpdated
) {
1025 //if a swipe gesture was recognized or in progress, we don't want a pinch gesture to change the zoom
1026 if (m_isSwipeGesture
) {
1029 counter
= counter
+ (pinch
->scaleFactor() - 1);
1030 if (counter
>= sensitivityModifier
) {
1031 emit
increaseZoom();
1033 } else if (counter
<= -sensitivityModifier
) {
1034 emit
decreaseZoom();
1040 void KItemListController::swipeTriggered(QGestureEvent
* event
, const QTransform
& transform
)
1044 const KTwoFingerSwipe
* swipe
= static_cast<KTwoFingerSwipe
*>(event
->gesture(m_swipeGesture
));
1049 if (swipe
->state() == Qt::GestureStarted
) {
1050 m_isSwipeGesture
= true;
1053 if (swipe
->state() == Qt::GestureCanceled
) {
1054 m_isSwipeGesture
= false;
1057 if (swipe
->state() == Qt::GestureFinished
) {
1058 emit
scrollerStop();
1060 if (swipe
->swipeAngle() <= 20 || swipe
->swipeAngle() >= 340) {
1061 emit
mouseButtonPressed(m_pressedIndex
, Qt::BackButton
);
1062 } else if (swipe
->swipeAngle() <= 200 && swipe
->swipeAngle() >= 160) {
1063 emit
mouseButtonPressed(m_pressedIndex
, Qt::ForwardButton
);
1064 } else if (swipe
->swipeAngle() <= 110 && swipe
->swipeAngle() >= 60) {
1067 m_isSwipeGesture
= true;
1071 void KItemListController::twoFingerTapTriggered(QGestureEvent
* event
, const QTransform
& transform
)
1073 const KTwoFingerTap
* twoTap
= static_cast<KTwoFingerTap
*>(event
->gesture(m_twoFingerTapGesture
));
1079 if (twoTap
->state() == Qt::GestureStarted
) {
1080 m_pressedMousePos
= transform
.map(twoTap
->pos());
1081 m_pressedIndex
= m_view
->itemAt(m_pressedMousePos
);
1082 if (m_pressedIndex
>= 0) {
1083 onPress(twoTap
->screenPos().toPoint(), twoTap
->pos().toPoint(), Qt::ControlModifier
, Qt::LeftButton
);
1084 onRelease(transform
.map(twoTap
->pos()), Qt::ControlModifier
, Qt::LeftButton
, false);
1090 bool KItemListController::processEvent(QEvent
* event
, const QTransform
& transform
)
1096 switch (event
->type()) {
1097 case QEvent::KeyPress
:
1098 return keyPressEvent(static_cast<QKeyEvent
*>(event
));
1099 case QEvent::InputMethod
:
1100 return inputMethodEvent(static_cast<QInputMethodEvent
*>(event
));
1101 case QEvent::GraphicsSceneMousePress
:
1102 return mousePressEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
1103 case QEvent::GraphicsSceneMouseMove
:
1104 return mouseMoveEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
1105 case QEvent::GraphicsSceneMouseRelease
:
1106 return mouseReleaseEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
1107 case QEvent::GraphicsSceneMouseDoubleClick
:
1108 return mouseDoubleClickEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
1109 case QEvent::GraphicsSceneWheel
:
1110 return wheelEvent(static_cast<QGraphicsSceneWheelEvent
*>(event
), QTransform());
1111 case QEvent::GraphicsSceneDragEnter
:
1112 return dragEnterEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
1113 case QEvent::GraphicsSceneDragLeave
:
1114 return dragLeaveEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
1115 case QEvent::GraphicsSceneDragMove
:
1116 return dragMoveEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
1117 case QEvent::GraphicsSceneDrop
:
1118 return dropEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
1119 case QEvent::GraphicsSceneHoverEnter
:
1120 return hoverEnterEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
1121 case QEvent::GraphicsSceneHoverMove
:
1122 return hoverMoveEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
1123 case QEvent::GraphicsSceneHoverLeave
:
1124 return hoverLeaveEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
1125 case QEvent::GraphicsSceneResize
:
1126 return resizeEvent(static_cast<QGraphicsSceneResizeEvent
*>(event
), transform
);
1127 case QEvent::Gesture
:
1128 return gestureEvent(static_cast<QGestureEvent
*>(event
), transform
);
1136 void KItemListController::slotViewScrollOffsetChanged(qreal current
, qreal previous
)
1142 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
1143 if (rubberBand
->isActive()) {
1144 const qreal diff
= current
- previous
;
1145 // TODO: Ideally just QCursor::pos() should be used as
1146 // new end-position but it seems there is no easy way
1147 // to have something like QWidget::mapFromGlobal() for QGraphicsWidget
1148 // (... or I just missed an easy way to do the mapping)
1149 QPointF endPos
= rubberBand
->endPosition();
1150 if (m_view
->scrollOrientation() == Qt::Vertical
) {
1151 endPos
.ry() += diff
;
1153 endPos
.rx() += diff
;
1156 rubberBand
->setEndPosition(endPos
);
1160 void KItemListController::slotRubberBandChanged()
1162 if (!m_view
|| !m_model
|| m_model
->count() <= 0) {
1166 const KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
1167 const QPointF startPos
= rubberBand
->startPosition();
1168 const QPointF endPos
= rubberBand
->endPosition();
1169 QRectF rubberBandRect
= QRectF(startPos
, endPos
).normalized();
1171 const bool scrollVertical
= (m_view
->scrollOrientation() == Qt::Vertical
);
1172 if (scrollVertical
) {
1173 rubberBandRect
.translate(0, -m_view
->scrollOffset());
1175 rubberBandRect
.translate(-m_view
->scrollOffset(), 0);
1178 if (!m_oldSelection
.isEmpty()) {
1179 // Clear the old selection that was available before the rubberband has
1180 // been activated in case if no Shift- or Control-key are pressed
1181 const bool shiftOrControlPressed
= QApplication::keyboardModifiers() & Qt::ShiftModifier
||
1182 QApplication::keyboardModifiers() & Qt::ControlModifier
;
1183 if (!shiftOrControlPressed
) {
1184 m_oldSelection
.clear();
1188 KItemSet selectedItems
;
1190 // Select all visible items that intersect with the rubberband
1191 foreach (const KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
1192 const int index
= widget
->index();
1194 const QRectF widgetRect
= m_view
->itemRect(index
);
1195 if (widgetRect
.intersects(rubberBandRect
)) {
1196 const QRectF iconRect
= widget
->iconRect().translated(widgetRect
.topLeft());
1197 const QRectF textRect
= widget
->textRect().translated(widgetRect
.topLeft());
1198 if (iconRect
.intersects(rubberBandRect
) || textRect
.intersects(rubberBandRect
)) {
1199 selectedItems
.insert(index
);
1204 // Select all invisible items that intersect with the rubberband. Instead of
1205 // iterating all items only the area which might be touched by the rubberband
1207 const bool increaseIndex
= scrollVertical
?
1208 startPos
.y() > endPos
.y(): startPos
.x() > endPos
.x();
1210 int index
= increaseIndex
? m_view
->lastVisibleIndex() + 1 : m_view
->firstVisibleIndex() - 1;
1211 bool selectionFinished
= false;
1213 const QRectF widgetRect
= m_view
->itemRect(index
);
1214 if (widgetRect
.intersects(rubberBandRect
)) {
1215 selectedItems
.insert(index
);
1218 if (increaseIndex
) {
1220 selectionFinished
= (index
>= m_model
->count()) ||
1221 ( scrollVertical
&& widgetRect
.top() > rubberBandRect
.bottom()) ||
1222 (!scrollVertical
&& widgetRect
.left() > rubberBandRect
.right());
1225 selectionFinished
= (index
< 0) ||
1226 ( scrollVertical
&& widgetRect
.bottom() < rubberBandRect
.top()) ||
1227 (!scrollVertical
&& widgetRect
.right() < rubberBandRect
.left());
1229 } while (!selectionFinished
);
1231 if (QApplication::keyboardModifiers() & Qt::ControlModifier
) {
1232 // If Control is pressed, the selection state of all items in the rubberband is toggled.
1233 // Therefore, the new selection contains:
1234 // 1. All previously selected items which are not inside the rubberband, and
1235 // 2. all items inside the rubberband which have not been selected previously.
1236 m_selectionManager
->setSelectedItems(m_oldSelection
^ selectedItems
);
1239 m_selectionManager
->setSelectedItems(selectedItems
+ m_oldSelection
);
1243 void KItemListController::startDragging()
1245 if (!m_view
|| !m_model
) {
1249 const KItemSet selectedItems
= m_selectionManager
->selectedItems();
1250 if (selectedItems
.isEmpty()) {
1254 QMimeData
* data
= m_model
->createMimeData(selectedItems
);
1259 // The created drag object will be owned and deleted
1260 // by QApplication::activeWindow().
1261 QDrag
* drag
= new QDrag(QApplication::activeWindow());
1262 drag
->setMimeData(data
);
1264 const QPixmap pixmap
= m_view
->createDragPixmap(selectedItems
);
1265 drag
->setPixmap(pixmap
);
1267 const QPoint
hotSpot((pixmap
.width() / pixmap
.devicePixelRatio()) / 2, 0);
1268 drag
->setHotSpot(hotSpot
);
1270 drag
->exec(Qt::MoveAction
| Qt::CopyAction
| Qt::LinkAction
, Qt::CopyAction
);
1272 QAccessibleEvent
accessibilityEvent(view(), QAccessible::DragDropStart
);
1273 QAccessible::updateAccessibility(&accessibilityEvent
);
1276 KItemListWidget
* KItemListController::hoveredWidget() const
1280 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
1281 if (widget
->isHovered()) {
1289 KItemListWidget
* KItemListController::widgetForPos(const QPointF
& pos
) const
1293 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
1294 const QPointF mappedPos
= widget
->mapFromItem(m_view
, pos
);
1296 const bool hovered
= widget
->contains(mappedPos
) &&
1297 !widget
->expansionToggleRect().contains(mappedPos
);
1306 void KItemListController::updateKeyboardAnchor()
1308 const bool validAnchor
= m_keyboardAnchorIndex
>= 0 &&
1309 m_keyboardAnchorIndex
< m_model
->count() &&
1310 keyboardAnchorPos(m_keyboardAnchorIndex
) == m_keyboardAnchorPos
;
1312 const int index
= m_selectionManager
->currentItem();
1313 m_keyboardAnchorIndex
= index
;
1314 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
1318 int KItemListController::nextRowIndex(int index
) const
1320 if (m_keyboardAnchorIndex
< 0) {
1324 const int maxIndex
= m_model
->count() - 1;
1325 if (index
== maxIndex
) {
1329 // Calculate the index of the last column inside the row of the current index
1330 int lastColumnIndex
= index
;
1331 while (keyboardAnchorPos(lastColumnIndex
+ 1) > keyboardAnchorPos(lastColumnIndex
)) {
1333 if (lastColumnIndex
>= maxIndex
) {
1338 // Based on the last column index go to the next row and calculate the nearest index
1339 // that is below the current index
1340 int nextRowIndex
= lastColumnIndex
+ 1;
1341 int searchIndex
= nextRowIndex
;
1342 qreal minDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(nextRowIndex
));
1343 while (searchIndex
< maxIndex
&& keyboardAnchorPos(searchIndex
+ 1) > keyboardAnchorPos(searchIndex
)) {
1345 const qreal searchDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(searchIndex
));
1346 if (searchDiff
< minDiff
) {
1347 minDiff
= searchDiff
;
1348 nextRowIndex
= searchIndex
;
1352 return nextRowIndex
;
1355 int KItemListController::previousRowIndex(int index
) const
1357 if (m_keyboardAnchorIndex
< 0 || index
== 0) {
1361 // Calculate the index of the first column inside the row of the current index
1362 int firstColumnIndex
= index
;
1363 while (keyboardAnchorPos(firstColumnIndex
- 1) < keyboardAnchorPos(firstColumnIndex
)) {
1365 if (firstColumnIndex
<= 0) {
1370 // Based on the first column index go to the previous row and calculate the nearest index
1371 // that is above the current index
1372 int previousRowIndex
= firstColumnIndex
- 1;
1373 int searchIndex
= previousRowIndex
;
1374 qreal minDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(previousRowIndex
));
1375 while (searchIndex
> 0 && keyboardAnchorPos(searchIndex
- 1) < keyboardAnchorPos(searchIndex
)) {
1377 const qreal searchDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(searchIndex
));
1378 if (searchDiff
< minDiff
) {
1379 minDiff
= searchDiff
;
1380 previousRowIndex
= searchIndex
;
1384 return previousRowIndex
;
1387 qreal
KItemListController::keyboardAnchorPos(int index
) const
1389 const QRectF itemRect
= m_view
->itemRect(index
);
1390 if (!itemRect
.isEmpty()) {
1391 return (m_view
->scrollOrientation() == Qt::Vertical
) ? itemRect
.x() : itemRect
.y();
1397 void KItemListController::updateExtendedSelectionRegion()
1400 const bool extend
= (m_selectionBehavior
!= MultiSelection
);
1401 KItemListStyleOption option
= m_view
->styleOption();
1402 if (option
.extendedSelectionRegion
!= extend
) {
1403 option
.extendedSelectionRegion
= extend
;
1404 m_view
->setStyleOption(option
);
1409 bool KItemListController::onPress(const QPoint
& screenPos
, const QPointF
& pos
, const Qt::KeyboardModifiers modifiers
, const Qt::MouseButtons buttons
)
1411 emit
mouseButtonPressed(m_pressedIndex
, buttons
);
1413 if (m_view
->isAboveExpansionToggle(m_pressedIndex
, m_pressedMousePos
)) {
1414 m_selectionManager
->endAnchoredSelection();
1415 m_selectionManager
->setCurrentItem(m_pressedIndex
);
1416 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
1420 m_selectionTogglePressed
= m_view
->isAboveSelectionToggle(m_pressedIndex
, m_pressedMousePos
);
1421 if (m_selectionTogglePressed
) {
1422 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
1423 // The previous anchored selection has been finished already in
1424 // KItemListSelectionManager::setSelected(). We can safely change
1425 // the current item and start a new anchored selection now.
1426 m_selectionManager
->setCurrentItem(m_pressedIndex
);
1427 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
1431 const bool shiftPressed
= modifiers
& Qt::ShiftModifier
;
1432 const bool controlPressed
= modifiers
& Qt::ControlModifier
;
1434 // The previous selection is cleared if either
1435 // 1. The selection mode is SingleSelection, or
1436 // 2. the selection mode is MultiSelection, and *none* of the following conditions are met:
1437 // a) Shift or Control are pressed.
1438 // b) The clicked item is selected already. In that case, the user might want to:
1439 // - start dragging multiple items, or
1440 // - open the context menu and perform an action for all selected items.
1441 const bool shiftOrControlPressed
= shiftPressed
|| controlPressed
;
1442 const bool pressedItemAlreadySelected
= m_pressedIndex
>= 0 && m_selectionManager
->isSelected(m_pressedIndex
);
1443 const bool clearSelection
= m_selectionBehavior
== SingleSelection
||
1444 (!shiftOrControlPressed
&& !pressedItemAlreadySelected
);
1445 if (clearSelection
) {
1446 m_selectionManager
->clearSelection();
1447 } else if (pressedItemAlreadySelected
&& !shiftOrControlPressed
&& (buttons
& Qt::LeftButton
)) {
1448 // The user might want to start dragging multiple items, but if he clicks the item
1449 // in order to trigger it instead, the other selected items must be deselected.
1450 // However, we do not know yet what the user is going to do.
1451 // -> remember that the user pressed an item which had been selected already and
1452 // clear the selection in mouseReleaseEvent(), unless the items are dragged.
1453 m_clearSelectionIfItemsAreNotDragged
= true;
1455 if (m_selectionManager
->selectedItems().count() == 1 && m_view
->isAboveText(m_pressedIndex
, m_pressedMousePos
)) {
1456 emit
selectedItemTextPressed(m_pressedIndex
);
1460 if (!shiftPressed
) {
1461 // Finish the anchored selection before the current index is changed
1462 m_selectionManager
->endAnchoredSelection();
1465 if (buttons
& Qt::RightButton
) {
1466 // Stop rubber band from persisting after right-clicks
1467 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
1468 if (rubberBand
->isActive()) {
1469 disconnect(rubberBand
, &KItemListRubberBand::endPositionChanged
, this, &KItemListController::slotRubberBandChanged
);
1470 rubberBand
->setActive(false);
1471 m_view
->setAutoScroll(false);
1475 if (m_pressedIndex
>= 0) {
1476 m_selectionManager
->setCurrentItem(m_pressedIndex
);
1478 switch (m_selectionBehavior
) {
1482 case SingleSelection
:
1483 m_selectionManager
->setSelected(m_pressedIndex
);
1486 case MultiSelection
:
1487 if (controlPressed
&& !shiftPressed
) {
1488 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
1489 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
1490 } else if (!shiftPressed
|| !m_selectionManager
->isAnchoredSelectionActive()) {
1491 // Select the pressed item and start a new anchored selection
1492 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Select
);
1493 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
1502 if (buttons
& Qt::RightButton
) {
1503 emit
itemContextMenuRequested(m_pressedIndex
, screenPos
);
1509 if (buttons
& Qt::RightButton
) {
1510 const QRectF headerBounds
= m_view
->headerBoundaries();
1511 if (headerBounds
.contains(pos
)) {
1512 emit
headerContextMenuRequested(screenPos
);
1514 emit
viewContextMenuRequested(screenPos
);
1522 bool KItemListController::onRelease(const QPointF
& pos
, const Qt::KeyboardModifiers modifiers
, const Qt::MouseButtons buttons
, bool touch
)
1524 const bool isAboveSelectionToggle
= m_view
->isAboveSelectionToggle(m_pressedIndex
, m_pressedMousePos
);
1525 if (isAboveSelectionToggle
) {
1526 m_selectionTogglePressed
= false;
1530 if (!isAboveSelectionToggle
&& m_selectionTogglePressed
) {
1531 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
1532 m_selectionTogglePressed
= false;
1536 const bool shiftOrControlPressed
= modifiers
& Qt::ShiftModifier
||
1537 modifiers
& Qt::ControlModifier
;
1539 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
1540 if (rubberBand
->isActive()) {
1541 disconnect(rubberBand
, &KItemListRubberBand::endPositionChanged
, this, &KItemListController::slotRubberBandChanged
);
1542 rubberBand
->setActive(false);
1543 m_oldSelection
.clear();
1544 m_view
->setAutoScroll(false);
1547 const int index
= m_view
->itemAt(pos
);
1549 if (index
>= 0 && index
== m_pressedIndex
) {
1550 // The release event is done above the same item as the press event
1552 if (m_clearSelectionIfItemsAreNotDragged
) {
1553 // A selected item has been clicked, but no drag operation has been started
1554 // -> clear the rest of the selection.
1555 m_selectionManager
->clearSelection();
1556 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Select
);
1557 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
1560 if (buttons
& Qt::LeftButton
) {
1561 bool emitItemActivated
= true;
1562 if (m_view
->isAboveExpansionToggle(index
, pos
)) {
1563 const bool expanded
= m_model
->isExpanded(index
);
1564 m_model
->setExpanded(index
, !expanded
);
1566 emit
itemExpansionToggleClicked(index
);
1567 emitItemActivated
= false;
1568 } else if (shiftOrControlPressed
) {
1569 // The mouse click should only update the selection, not trigger the item
1570 emitItemActivated
= false;
1571 } else if (!(m_view
->style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick
) || m_singleClickActivationEnforced
)) {
1573 emitItemActivated
= true;
1575 emitItemActivated
= false;
1578 if (emitItemActivated
) {
1579 emit
itemActivated(index
);
1581 } else if (buttons
& Qt::MiddleButton
) {
1582 emit
itemMiddleClicked(index
);
1586 m_pressedMousePos
= QPointF();
1587 m_pressedIndex
= -1;
1588 m_clearSelectionIfItemsAreNotDragged
= false;
1592 void KItemListController::startRubberBand()
1594 if (m_selectionBehavior
== MultiSelection
) {
1595 QPointF startPos
= m_pressedMousePos
;
1596 if (m_view
->scrollOrientation() == Qt::Vertical
) {
1597 startPos
.ry() += m_view
->scrollOffset();
1598 if (m_view
->itemSize().width() < 0) {
1599 // Use a special rubberband for views that have only one column and
1600 // expand the rubberband to use the whole width of the view.
1604 startPos
.rx() += m_view
->scrollOffset();
1607 m_oldSelection
= m_selectionManager
->selectedItems();
1608 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
1609 rubberBand
->setStartPosition(startPos
);
1610 rubberBand
->setEndPosition(startPos
);
1611 rubberBand
->setActive(true);
1612 connect(rubberBand
, &KItemListRubberBand::endPositionChanged
, this, &KItemListController::slotRubberBandChanged
);
1613 m_view
->setAutoScroll(true);
1617 void KItemListController::slotStateChanged(QScroller::State newState
)
1619 if (newState
== QScroller::Scrolling
) {
1620 m_scrollerIsScrolling
= true;
1621 } else if (newState
== QScroller::Inactive
) {
1622 m_scrollerIsScrolling
= false;