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 "views/draganddrophelper.h"
18 #include <QAccessible>
19 #include <QApplication>
21 #include <QGraphicsScene>
22 #include <QGraphicsSceneEvent>
23 #include <QGraphicsView>
27 KItemListController::KItemListController(KItemModelBase
* model
, KItemListView
* view
, QObject
* parent
) :
29 m_singleClickActivationEnforced(false),
30 m_selectionTogglePressed(false),
31 m_clearSelectionIfItemsAreNotDragged(false),
32 m_selectionBehavior(NoSelection
),
33 m_autoActivationBehavior(ActivationAndExpansion
),
34 m_mouseDoubleClickAction(ActivateItemOnly
),
37 m_selectionManager(new KItemListSelectionManager(this)),
38 m_keyboardManager(new KItemListKeyboardSearchManager(this)),
41 m_autoActivationTimer(nullptr),
43 m_keyboardAnchorIndex(-1),
44 m_keyboardAnchorPos(0)
46 connect(m_keyboardManager
, &KItemListKeyboardSearchManager::changeCurrentItem
,
47 this, &KItemListController::slotChangeCurrentItem
);
48 connect(m_selectionManager
, &KItemListSelectionManager::currentChanged
,
49 m_keyboardManager
, &KItemListKeyboardSearchManager::slotCurrentChanged
);
50 connect(m_selectionManager
, &KItemListSelectionManager::selectionChanged
,
51 m_keyboardManager
, &KItemListKeyboardSearchManager::slotSelectionChanged
);
53 m_autoActivationTimer
= new QTimer(this);
54 m_autoActivationTimer
->setSingleShot(true);
55 m_autoActivationTimer
->setInterval(-1);
56 connect(m_autoActivationTimer
, &QTimer::timeout
, this, &KItemListController::slotAutoActivationTimeout
);
62 KItemListController::~KItemListController()
71 void KItemListController::setModel(KItemModelBase
* model
)
73 if (m_model
== model
) {
77 KItemModelBase
* oldModel
= m_model
;
79 oldModel
->deleteLater();
84 m_model
->setParent(this);
88 m_view
->setModel(m_model
);
91 m_selectionManager
->setModel(m_model
);
93 emit
modelChanged(m_model
, oldModel
);
96 KItemModelBase
* KItemListController::model() const
101 KItemListSelectionManager
* KItemListController::selectionManager() const
103 return m_selectionManager
;
106 void KItemListController::setView(KItemListView
* view
)
108 if (m_view
== view
) {
112 KItemListView
* oldView
= m_view
;
114 disconnect(oldView
, &KItemListView::scrollOffsetChanged
, this, &KItemListController::slotViewScrollOffsetChanged
);
115 oldView
->deleteLater();
121 m_view
->setParent(this);
122 m_view
->setController(this);
123 m_view
->setModel(m_model
);
124 connect(m_view
, &KItemListView::scrollOffsetChanged
, this, &KItemListController::slotViewScrollOffsetChanged
);
125 updateExtendedSelectionRegion();
128 emit
viewChanged(m_view
, oldView
);
131 KItemListView
* KItemListController::view() const
136 void KItemListController::setSelectionBehavior(SelectionBehavior behavior
)
138 m_selectionBehavior
= behavior
;
139 updateExtendedSelectionRegion();
142 KItemListController::SelectionBehavior
KItemListController::selectionBehavior() const
144 return m_selectionBehavior
;
147 void KItemListController::setAutoActivationBehavior(AutoActivationBehavior behavior
)
149 m_autoActivationBehavior
= behavior
;
152 KItemListController::AutoActivationBehavior
KItemListController::autoActivationBehavior() const
154 return m_autoActivationBehavior
;
157 void KItemListController::setMouseDoubleClickAction(MouseDoubleClickAction action
)
159 m_mouseDoubleClickAction
= action
;
162 KItemListController::MouseDoubleClickAction
KItemListController::mouseDoubleClickAction() const
164 return m_mouseDoubleClickAction
;
167 int KItemListController::indexCloseToMousePressedPosition() const
169 QHashIterator
<KItemListWidget
*, KItemListGroupHeader
*> it(m_view
->m_visibleGroups
);
170 while (it
.hasNext()) {
172 KItemListGroupHeader
*groupHeader
= it
.value();
173 const QPointF mappedToGroup
= groupHeader
->mapFromItem(nullptr, m_pressedMousePos
);
174 if (groupHeader
->contains(mappedToGroup
)) {
175 return it
.key()->index();
181 void KItemListController::setAutoActivationDelay(int delay
)
183 m_autoActivationTimer
->setInterval(delay
);
186 int KItemListController::autoActivationDelay() const
188 return m_autoActivationTimer
->interval();
191 void KItemListController::setSingleClickActivationEnforced(bool singleClick
)
193 m_singleClickActivationEnforced
= singleClick
;
196 bool KItemListController::singleClickActivationEnforced() const
198 return m_singleClickActivationEnforced
;
201 bool KItemListController::keyPressEvent(QKeyEvent
* event
)
203 int index
= m_selectionManager
->currentItem();
204 int key
= event
->key();
206 // Handle the expanding/collapsing of items
207 if (m_view
->supportsItemExpanding() && m_model
->isExpandable(index
)) {
208 if (key
== Qt::Key_Right
) {
209 if (m_model
->setExpanded(index
, true)) {
212 } else if (key
== Qt::Key_Left
) {
213 if (m_model
->setExpanded(index
, false)) {
219 const bool shiftPressed
= event
->modifiers() & Qt::ShiftModifier
;
220 const bool controlPressed
= event
->modifiers() & Qt::ControlModifier
;
221 const bool shiftOrControlPressed
= shiftPressed
|| controlPressed
;
222 const bool navigationPressed
= key
== Qt::Key_Home
|| key
== Qt::Key_End
||
223 key
== Qt::Key_PageUp
|| key
== Qt::Key_PageDown
||
224 key
== Qt::Key_Up
|| key
== Qt::Key_Down
||
225 key
== Qt::Key_Left
|| key
== Qt::Key_Right
;
227 const int itemCount
= m_model
->count();
229 // For horizontal scroll orientation, transform
230 // the arrow keys to simplify the event handling.
231 if (m_view
->scrollOrientation() == Qt::Horizontal
) {
233 case Qt::Key_Up
: key
= Qt::Key_Left
; break;
234 case Qt::Key_Down
: key
= Qt::Key_Right
; break;
235 case Qt::Key_Left
: key
= Qt::Key_Up
; break;
236 case Qt::Key_Right
: key
= Qt::Key_Down
; break;
241 const bool selectSingleItem
= m_selectionBehavior
!= NoSelection
&& itemCount
== 1 && navigationPressed
;
243 if (selectSingleItem
) {
244 const int current
= m_selectionManager
->currentItem();
245 m_selectionManager
->setSelected(current
);
252 m_keyboardAnchorIndex
= index
;
253 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
257 index
= itemCount
- 1;
258 m_keyboardAnchorIndex
= index
;
259 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
264 const int expandedParentsCount
= m_model
->expandedParentsCount(index
);
265 if (expandedParentsCount
== 0) {
268 // Go to the parent of the current item.
271 } while (index
> 0 && m_model
->expandedParentsCount(index
) == expandedParentsCount
);
273 m_keyboardAnchorIndex
= index
;
274 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
279 if (index
< itemCount
- 1) {
281 m_keyboardAnchorIndex
= index
;
282 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
287 updateKeyboardAnchor();
288 index
= previousRowIndex(index
);
292 updateKeyboardAnchor();
293 index
= nextRowIndex(index
);
297 if (m_view
->scrollOrientation() == Qt::Horizontal
) {
298 // The new current index should correspond to the first item in the current column.
299 int newIndex
= qMax(index
- 1, 0);
300 while (newIndex
!= index
&& m_view
->itemRect(newIndex
).topLeft().y() < m_view
->itemRect(index
).topLeft().y()) {
302 newIndex
= qMax(index
- 1, 0);
304 m_keyboardAnchorIndex
= index
;
305 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
307 const qreal currentItemBottom
= m_view
->itemRect(index
).bottomLeft().y();
308 const qreal height
= m_view
->geometry().height();
310 // The new current item should be the first item in the current
311 // column whose itemRect's top coordinate is larger than targetY.
312 const qreal targetY
= currentItemBottom
- height
;
314 updateKeyboardAnchor();
315 int newIndex
= previousRowIndex(index
);
318 updateKeyboardAnchor();
319 newIndex
= previousRowIndex(index
);
320 } while (m_view
->itemRect(newIndex
).topLeft().y() > targetY
&& newIndex
!= index
);
324 case Qt::Key_PageDown
:
325 if (m_view
->scrollOrientation() == Qt::Horizontal
) {
326 // The new current index should correspond to the last item in the current column.
327 int newIndex
= qMin(index
+ 1, m_model
->count() - 1);
328 while (newIndex
!= index
&& m_view
->itemRect(newIndex
).topLeft().y() > m_view
->itemRect(index
).topLeft().y()) {
330 newIndex
= qMin(index
+ 1, m_model
->count() - 1);
332 m_keyboardAnchorIndex
= index
;
333 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
335 const qreal currentItemTop
= m_view
->itemRect(index
).topLeft().y();
336 const qreal height
= m_view
->geometry().height();
338 // The new current item should be the last item in the current
339 // column whose itemRect's bottom coordinate is smaller than targetY.
340 const qreal targetY
= currentItemTop
+ height
;
342 updateKeyboardAnchor();
343 int newIndex
= nextRowIndex(index
);
346 updateKeyboardAnchor();
347 newIndex
= nextRowIndex(index
);
348 } while (m_view
->itemRect(newIndex
).bottomLeft().y() < targetY
&& newIndex
!= index
);
353 case Qt::Key_Return
: {
354 const KItemSet selectedItems
= m_selectionManager
->selectedItems();
355 if (selectedItems
.count() >= 2) {
356 emit
itemsActivated(selectedItems
);
357 } else if (selectedItems
.count() == 1) {
358 emit
itemActivated(selectedItems
.first());
360 emit
itemActivated(index
);
366 // Emit the signal itemContextMenuRequested() in case if at least one
367 // item is selected. Otherwise the signal viewContextMenuRequested() will be emitted.
368 const KItemSet selectedItems
= m_selectionManager
->selectedItems();
370 if (selectedItems
.count() >= 2) {
371 const int currentItemIndex
= m_selectionManager
->currentItem();
372 index
= selectedItems
.contains(currentItemIndex
)
373 ? currentItemIndex
: selectedItems
.first();
374 } else if (selectedItems
.count() == 1) {
375 index
= selectedItems
.first();
379 const QRectF contextRect
= m_view
->itemContextRect(index
);
380 const QPointF
pos(m_view
->scene()->views().first()->mapToGlobal(contextRect
.bottomRight().toPoint()));
381 emit
itemContextMenuRequested(index
, pos
);
383 emit
viewContextMenuRequested(QCursor::pos());
389 if (m_selectionBehavior
!= SingleSelection
) {
390 m_selectionManager
->clearSelection();
392 m_keyboardManager
->cancelSearch();
393 emit
escapePressed();
397 if (m_selectionBehavior
== MultiSelection
) {
398 if (controlPressed
) {
399 // Toggle the selection state of the current item.
400 m_selectionManager
->endAnchoredSelection();
401 m_selectionManager
->setSelected(index
, 1, KItemListSelectionManager::Toggle
);
402 m_selectionManager
->beginAnchoredSelection(index
);
405 // Select the current item if it is not selected yet.
406 const int current
= m_selectionManager
->currentItem();
407 if (!m_selectionManager
->isSelected(current
)) {
408 m_selectionManager
->setSelected(current
);
413 Q_FALLTHROUGH(); // fall through to the default case and add the Space to the current search string.
415 m_keyboardManager
->addKeys(event
->text());
416 // Make sure unconsumed events get propagated up the chain. #302329
421 if (m_selectionManager
->currentItem() != index
) {
422 switch (m_selectionBehavior
) {
424 m_selectionManager
->setCurrentItem(index
);
427 case SingleSelection
:
428 m_selectionManager
->setCurrentItem(index
);
429 m_selectionManager
->clearSelection();
430 m_selectionManager
->setSelected(index
, 1);
434 if (controlPressed
) {
435 m_selectionManager
->endAnchoredSelection();
438 m_selectionManager
->setCurrentItem(index
);
440 if (!shiftOrControlPressed
) {
441 m_selectionManager
->clearSelection();
442 m_selectionManager
->setSelected(index
, 1);
446 m_selectionManager
->beginAnchoredSelection(index
);
452 if (navigationPressed
) {
453 m_view
->scrollToItem(index
);
458 void KItemListController::slotChangeCurrentItem(const QString
& text
, bool searchFromNextItem
)
460 if (!m_model
|| m_model
->count() == 0) {
464 if (searchFromNextItem
) {
465 const int currentIndex
= m_selectionManager
->currentItem();
466 index
= m_model
->indexForKeyboardSearch(text
, (currentIndex
+ 1) % m_model
->count());
468 index
= m_model
->indexForKeyboardSearch(text
, 0);
471 m_selectionManager
->setCurrentItem(index
);
473 if (m_selectionBehavior
!= NoSelection
) {
474 m_selectionManager
->replaceSelection(index
);
475 m_selectionManager
->beginAnchoredSelection(index
);
478 m_view
->scrollToItem(index
);
482 void KItemListController::slotAutoActivationTimeout()
484 if (!m_model
|| !m_view
) {
488 const int index
= m_autoActivationTimer
->property("index").toInt();
489 if (index
< 0 || index
>= m_model
->count()) {
493 /* m_view->isUnderMouse() fixes a bug in the Folder-View-Panel and in the
496 * Bug: When you drag a file onto a Folder-View-Item or a Places-Item and
497 * then move away before the auto-activation timeout triggers, than the
498 * item still becomes activated/expanded.
500 * See Bug 293200 and 305783
502 if (m_model
->supportsDropping(index
) && m_view
->isUnderMouse()) {
503 if (m_view
->supportsItemExpanding() && m_model
->isExpandable(index
)) {
504 const bool expanded
= m_model
->isExpanded(index
);
505 m_model
->setExpanded(index
, !expanded
);
506 } else if (m_autoActivationBehavior
!= ExpansionOnly
) {
507 emit
itemActivated(index
);
512 bool KItemListController::inputMethodEvent(QInputMethodEvent
* event
)
518 bool KItemListController::mousePressEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
524 m_pressedMousePos
= transform
.map(event
->pos());
525 m_pressedIndex
= m_view
->itemAt(m_pressedMousePos
);
526 emit
mouseButtonPressed(m_pressedIndex
, event
->buttons());
528 if (event
->buttons() & (Qt::BackButton
| Qt::ForwardButton
)) {
529 // Do not select items when clicking the back/forward buttons, see
530 // https://bugs.kde.org/show_bug.cgi?id=327412.
534 if (m_view
->isAboveExpansionToggle(m_pressedIndex
, m_pressedMousePos
)) {
535 m_selectionManager
->endAnchoredSelection();
536 m_selectionManager
->setCurrentItem(m_pressedIndex
);
537 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
541 m_selectionTogglePressed
= m_view
->isAboveSelectionToggle(m_pressedIndex
, m_pressedMousePos
);
542 if (m_selectionTogglePressed
) {
543 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
544 // The previous anchored selection has been finished already in
545 // KItemListSelectionManager::setSelected(). We can safely change
546 // the current item and start a new anchored selection now.
547 m_selectionManager
->setCurrentItem(m_pressedIndex
);
548 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
552 const bool shiftPressed
= event
->modifiers() & Qt::ShiftModifier
;
553 const bool controlPressed
= event
->modifiers() & Qt::ControlModifier
;
555 // The previous selection is cleared if either
556 // 1. The selection mode is SingleSelection, or
557 // 2. the selection mode is MultiSelection, and *none* of the following conditions are met:
558 // a) Shift or Control are pressed.
559 // b) The clicked item is selected already. In that case, the user might want to:
560 // - start dragging multiple items, or
561 // - open the context menu and perform an action for all selected items.
562 const bool shiftOrControlPressed
= shiftPressed
|| controlPressed
;
563 const bool pressedItemAlreadySelected
= m_pressedIndex
>= 0 && m_selectionManager
->isSelected(m_pressedIndex
);
564 const bool clearSelection
= m_selectionBehavior
== SingleSelection
||
565 (!shiftOrControlPressed
&& !pressedItemAlreadySelected
);
566 if (clearSelection
) {
567 m_selectionManager
->clearSelection();
568 } else if (pressedItemAlreadySelected
&& !shiftOrControlPressed
&& (event
->buttons() & Qt::LeftButton
)) {
569 // The user might want to start dragging multiple items, but if he clicks the item
570 // in order to trigger it instead, the other selected items must be deselected.
571 // However, we do not know yet what the user is going to do.
572 // -> remember that the user pressed an item which had been selected already and
573 // clear the selection in mouseReleaseEvent(), unless the items are dragged.
574 m_clearSelectionIfItemsAreNotDragged
= true;
576 if (m_selectionManager
->selectedItems().count() == 1 && m_view
->isAboveText(m_pressedIndex
, m_pressedMousePos
)) {
577 emit
selectedItemTextPressed(m_pressedIndex
);
582 // Finish the anchored selection before the current index is changed
583 m_selectionManager
->endAnchoredSelection();
586 if (event
->buttons() & Qt::RightButton
) {
587 // Stop rubber band from persisting after right-clicks
588 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
589 if (rubberBand
->isActive()) {
590 disconnect(rubberBand
, &KItemListRubberBand::endPositionChanged
, this, &KItemListController::slotRubberBandChanged
);
591 rubberBand
->setActive(false);
592 m_view
->setAutoScroll(false);
596 if (m_pressedIndex
>= 0) {
597 m_selectionManager
->setCurrentItem(m_pressedIndex
);
599 switch (m_selectionBehavior
) {
603 case SingleSelection
:
604 m_selectionManager
->setSelected(m_pressedIndex
);
608 if (controlPressed
&& !shiftPressed
) {
609 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
610 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
611 } else if (!shiftPressed
|| !m_selectionManager
->isAnchoredSelectionActive()) {
612 // Select the pressed item and start a new anchored selection
613 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Select
);
614 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
623 if (event
->buttons() & Qt::RightButton
) {
624 emit
itemContextMenuRequested(m_pressedIndex
, event
->screenPos());
630 if (event
->buttons() & Qt::RightButton
) {
631 const QRectF headerBounds
= m_view
->headerBoundaries();
632 if (headerBounds
.contains(event
->pos())) {
633 emit
headerContextMenuRequested(event
->screenPos());
635 emit
viewContextMenuRequested(event
->screenPos());
640 if (m_selectionBehavior
== MultiSelection
) {
641 QPointF startPos
= m_pressedMousePos
;
642 if (m_view
->scrollOrientation() == Qt::Vertical
) {
643 startPos
.ry() += m_view
->scrollOffset();
644 if (m_view
->itemSize().width() < 0) {
645 // Use a special rubberband for views that have only one column and
646 // expand the rubberband to use the whole width of the view.
650 startPos
.rx() += m_view
->scrollOffset();
653 m_oldSelection
= m_selectionManager
->selectedItems();
654 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
655 rubberBand
->setStartPosition(startPos
);
656 rubberBand
->setEndPosition(startPos
);
657 rubberBand
->setActive(true);
658 connect(rubberBand
, &KItemListRubberBand::endPositionChanged
, this, &KItemListController::slotRubberBandChanged
);
659 m_view
->setAutoScroll(true);
665 bool KItemListController::mouseMoveEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
671 if (m_pressedIndex
>= 0) {
672 // Check whether a dragging should be started
673 if (event
->buttons() & Qt::LeftButton
) {
674 const QPointF pos
= transform
.map(event
->pos());
675 if ((pos
- m_pressedMousePos
).manhattanLength() >= QApplication::startDragDistance()) {
676 if (!m_selectionManager
->isSelected(m_pressedIndex
)) {
677 // Always assure that the dragged item gets selected. Usually this is already
678 // done on the mouse-press event, but when using the selection-toggle on a
679 // selected item the dragged item is not selected yet.
680 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
682 // A selected item has been clicked to drag all selected items
683 // -> the selection should not be cleared when the mouse button is released.
684 m_clearSelectionIfItemsAreNotDragged
= false;
691 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
692 if (rubberBand
->isActive()) {
693 QPointF endPos
= transform
.map(event
->pos());
695 // Update the current item.
696 const int newCurrent
= m_view
->itemAt(endPos
);
697 if (newCurrent
>= 0) {
698 // It's expected that the new current index is also the new anchor (bug 163451).
699 m_selectionManager
->endAnchoredSelection();
700 m_selectionManager
->setCurrentItem(newCurrent
);
701 m_selectionManager
->beginAnchoredSelection(newCurrent
);
704 if (m_view
->scrollOrientation() == Qt::Vertical
) {
705 endPos
.ry() += m_view
->scrollOffset();
706 if (m_view
->itemSize().width() < 0) {
707 // Use a special rubberband for views that have only one column and
708 // expand the rubberband to use the whole width of the view.
709 endPos
.setX(m_view
->size().width());
712 endPos
.rx() += m_view
->scrollOffset();
714 rubberBand
->setEndPosition(endPos
);
721 bool KItemListController::mouseReleaseEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
727 emit
mouseButtonReleased(m_pressedIndex
, event
->buttons());
729 const bool isAboveSelectionToggle
= m_view
->isAboveSelectionToggle(m_pressedIndex
, m_pressedMousePos
);
730 if (isAboveSelectionToggle
) {
731 m_selectionTogglePressed
= false;
735 if (!isAboveSelectionToggle
&& m_selectionTogglePressed
) {
736 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
737 m_selectionTogglePressed
= false;
741 const bool shiftOrControlPressed
= event
->modifiers() & Qt::ShiftModifier
||
742 event
->modifiers() & Qt::ControlModifier
;
744 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
745 if (rubberBand
->isActive()) {
746 disconnect(rubberBand
, &KItemListRubberBand::endPositionChanged
, this, &KItemListController::slotRubberBandChanged
);
747 rubberBand
->setActive(false);
748 m_oldSelection
.clear();
749 m_view
->setAutoScroll(false);
752 const QPointF pos
= transform
.map(event
->pos());
753 const int index
= m_view
->itemAt(pos
);
755 if (index
>= 0 && index
== m_pressedIndex
) {
756 // The release event is done above the same item as the press event
758 if (m_clearSelectionIfItemsAreNotDragged
) {
759 // A selected item has been clicked, but no drag operation has been started
760 // -> clear the rest of the selection.
761 m_selectionManager
->clearSelection();
762 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Select
);
763 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
766 if (event
->button() & Qt::LeftButton
) {
767 bool emitItemActivated
= true;
768 if (m_view
->isAboveExpansionToggle(index
, pos
)) {
769 const bool expanded
= m_model
->isExpanded(index
);
770 m_model
->setExpanded(index
, !expanded
);
772 emit
itemExpansionToggleClicked(index
);
773 emitItemActivated
= false;
774 } else if (shiftOrControlPressed
) {
775 // The mouse click should only update the selection, not trigger the item
776 emitItemActivated
= false;
777 } else if (!(m_view
->style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick
) || m_singleClickActivationEnforced
)) {
778 emitItemActivated
= false;
780 if (emitItemActivated
) {
781 emit
itemActivated(index
);
783 } else if (event
->button() & Qt::MiddleButton
) {
784 emit
itemMiddleClicked(index
);
788 m_pressedMousePos
= QPointF();
790 m_clearSelectionIfItemsAreNotDragged
= false;
794 bool KItemListController::mouseDoubleClickEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
796 const QPointF pos
= transform
.map(event
->pos());
797 const int index
= m_view
->itemAt(pos
);
799 // Expand item if desired - See Bug 295573
800 if (m_mouseDoubleClickAction
!= ActivateItemOnly
) {
801 if (m_view
&& m_model
&& m_view
->supportsItemExpanding() && m_model
->isExpandable(index
)) {
802 const bool expanded
= m_model
->isExpanded(index
);
803 m_model
->setExpanded(index
, !expanded
);
807 if (event
->button() & Qt::RightButton
) {
808 m_selectionManager
->clearSelection();
810 m_selectionManager
->setSelected(index
);
811 emit
itemContextMenuRequested(index
, event
->screenPos());
813 const QRectF headerBounds
= m_view
->headerBoundaries();
814 if (headerBounds
.contains(event
->pos())) {
815 emit
headerContextMenuRequested(event
->screenPos());
817 emit
viewContextMenuRequested(event
->screenPos());
823 bool emitItemActivated
= !(m_view
->style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick
) || m_singleClickActivationEnforced
) &&
824 (event
->button() & Qt::LeftButton
) &&
825 index
>= 0 && index
< m_model
->count();
826 if (emitItemActivated
) {
827 emit
itemActivated(index
);
832 bool KItemListController::dragEnterEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
837 DragAndDropHelper::clearUrlListMatchesUrlCache();
842 bool KItemListController::dragLeaveEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
847 m_autoActivationTimer
->stop();
848 m_view
->setAutoScroll(false);
849 m_view
->hideDropIndicator();
851 KItemListWidget
* widget
= hoveredWidget();
853 widget
->setHovered(false);
854 emit
itemUnhovered(widget
->index());
859 bool KItemListController::dragMoveEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
861 if (!m_model
|| !m_view
) {
866 QUrl hoveredDir
= m_model
->directory();
867 KItemListWidget
* oldHoveredWidget
= hoveredWidget();
869 const QPointF pos
= transform
.map(event
->pos());
870 KItemListWidget
* newHoveredWidget
= widgetForPos(pos
);
872 if (oldHoveredWidget
!= newHoveredWidget
) {
873 m_autoActivationTimer
->stop();
875 if (oldHoveredWidget
) {
876 oldHoveredWidget
->setHovered(false);
877 emit
itemUnhovered(oldHoveredWidget
->index());
881 if (newHoveredWidget
) {
882 bool droppingBetweenItems
= false;
883 if (m_model
->sortRole().isEmpty()) {
884 // The model supports inserting items between other items.
885 droppingBetweenItems
= (m_view
->showDropIndicator(pos
) >= 0);
888 const int index
= newHoveredWidget
->index();
890 if (m_model
->isDir(index
)) {
891 hoveredDir
= m_model
->url(index
);
894 if (!droppingBetweenItems
) {
895 if (m_model
->supportsDropping(index
)) {
896 // Something has been dragged on an item.
897 m_view
->hideDropIndicator();
898 if (!newHoveredWidget
->isHovered()) {
899 newHoveredWidget
->setHovered(true);
900 emit
itemHovered(index
);
903 if (!m_autoActivationTimer
->isActive() && m_autoActivationTimer
->interval() >= 0) {
904 m_autoActivationTimer
->setProperty("index", index
);
905 m_autoActivationTimer
->start();
909 m_autoActivationTimer
->stop();
910 if (newHoveredWidget
&& newHoveredWidget
->isHovered()) {
911 newHoveredWidget
->setHovered(false);
912 emit
itemUnhovered(index
);
916 m_view
->hideDropIndicator();
919 if (DragAndDropHelper::urlListMatchesUrl(event
->mimeData()->urls(), hoveredDir
)) {
920 event
->setDropAction(Qt::IgnoreAction
);
923 event
->setDropAction(event
->proposedAction());
929 bool KItemListController::dropEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
935 m_autoActivationTimer
->stop();
936 m_view
->setAutoScroll(false);
938 const QPointF pos
= transform
.map(event
->pos());
940 int dropAboveIndex
= -1;
941 if (m_model
->sortRole().isEmpty()) {
942 // The model supports inserting of items between other items.
943 dropAboveIndex
= m_view
->showDropIndicator(pos
);
946 if (dropAboveIndex
>= 0) {
947 // Something has been dropped between two items.
948 m_view
->hideDropIndicator();
949 emit
aboveItemDropEvent(dropAboveIndex
, event
);
950 } else if (!event
->mimeData()->hasFormat(m_model
->blacklistItemDropEventMimeType())) {
951 // Something has been dropped on an item or on an empty part of the view.
952 emit
itemDropEvent(m_view
->itemAt(pos
), event
);
955 QAccessibleEvent
accessibilityEvent(view(), QAccessible::DragDropEnd
);
956 QAccessible::updateAccessibility(&accessibilityEvent
);
961 bool KItemListController::hoverEnterEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
968 bool KItemListController::hoverMoveEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
971 if (!m_model
|| !m_view
) {
975 KItemListWidget
* oldHoveredWidget
= hoveredWidget();
976 const QPointF pos
= transform
.map(event
->pos());
977 KItemListWidget
* newHoveredWidget
= widgetForPos(pos
);
979 if (oldHoveredWidget
!= newHoveredWidget
) {
980 if (oldHoveredWidget
) {
981 oldHoveredWidget
->setHovered(false);
982 emit
itemUnhovered(oldHoveredWidget
->index());
985 if (newHoveredWidget
) {
986 newHoveredWidget
->setHovered(true);
987 const QPointF mappedPos
= newHoveredWidget
->mapFromItem(m_view
, pos
);
988 newHoveredWidget
->setHoverPosition(mappedPos
);
989 emit
itemHovered(newHoveredWidget
->index());
991 } else if (oldHoveredWidget
) {
992 const QPointF mappedPos
= oldHoveredWidget
->mapFromItem(m_view
, pos
);
993 oldHoveredWidget
->setHoverPosition(mappedPos
);
999 bool KItemListController::hoverLeaveEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
1004 if (!m_model
|| !m_view
) {
1008 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
1009 if (widget
->isHovered()) {
1010 widget
->setHovered(false);
1011 emit
itemUnhovered(widget
->index());
1017 bool KItemListController::wheelEvent(QGraphicsSceneWheelEvent
* event
, const QTransform
& transform
)
1024 bool KItemListController::resizeEvent(QGraphicsSceneResizeEvent
* event
, const QTransform
& transform
)
1031 bool KItemListController::processEvent(QEvent
* event
, const QTransform
& transform
)
1037 switch (event
->type()) {
1038 case QEvent::KeyPress
:
1039 return keyPressEvent(static_cast<QKeyEvent
*>(event
));
1040 case QEvent::InputMethod
:
1041 return inputMethodEvent(static_cast<QInputMethodEvent
*>(event
));
1042 case QEvent::GraphicsSceneMousePress
:
1043 return mousePressEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
1044 case QEvent::GraphicsSceneMouseMove
:
1045 return mouseMoveEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
1046 case QEvent::GraphicsSceneMouseRelease
:
1047 return mouseReleaseEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
1048 case QEvent::GraphicsSceneMouseDoubleClick
:
1049 return mouseDoubleClickEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
1050 case QEvent::GraphicsSceneWheel
:
1051 return wheelEvent(static_cast<QGraphicsSceneWheelEvent
*>(event
), QTransform());
1052 case QEvent::GraphicsSceneDragEnter
:
1053 return dragEnterEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
1054 case QEvent::GraphicsSceneDragLeave
:
1055 return dragLeaveEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
1056 case QEvent::GraphicsSceneDragMove
:
1057 return dragMoveEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
1058 case QEvent::GraphicsSceneDrop
:
1059 return dropEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
1060 case QEvent::GraphicsSceneHoverEnter
:
1061 return hoverEnterEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
1062 case QEvent::GraphicsSceneHoverMove
:
1063 return hoverMoveEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
1064 case QEvent::GraphicsSceneHoverLeave
:
1065 return hoverLeaveEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
1066 case QEvent::GraphicsSceneResize
:
1067 return resizeEvent(static_cast<QGraphicsSceneResizeEvent
*>(event
), transform
);
1075 void KItemListController::slotViewScrollOffsetChanged(qreal current
, qreal previous
)
1081 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
1082 if (rubberBand
->isActive()) {
1083 const qreal diff
= current
- previous
;
1084 // TODO: Ideally just QCursor::pos() should be used as
1085 // new end-position but it seems there is no easy way
1086 // to have something like QWidget::mapFromGlobal() for QGraphicsWidget
1087 // (... or I just missed an easy way to do the mapping)
1088 QPointF endPos
= rubberBand
->endPosition();
1089 if (m_view
->scrollOrientation() == Qt::Vertical
) {
1090 endPos
.ry() += diff
;
1092 endPos
.rx() += diff
;
1095 rubberBand
->setEndPosition(endPos
);
1099 void KItemListController::slotRubberBandChanged()
1101 if (!m_view
|| !m_model
|| m_model
->count() <= 0) {
1105 const KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
1106 const QPointF startPos
= rubberBand
->startPosition();
1107 const QPointF endPos
= rubberBand
->endPosition();
1108 QRectF rubberBandRect
= QRectF(startPos
, endPos
).normalized();
1110 const bool scrollVertical
= (m_view
->scrollOrientation() == Qt::Vertical
);
1111 if (scrollVertical
) {
1112 rubberBandRect
.translate(0, -m_view
->scrollOffset());
1114 rubberBandRect
.translate(-m_view
->scrollOffset(), 0);
1117 if (!m_oldSelection
.isEmpty()) {
1118 // Clear the old selection that was available before the rubberband has
1119 // been activated in case if no Shift- or Control-key are pressed
1120 const bool shiftOrControlPressed
= QApplication::keyboardModifiers() & Qt::ShiftModifier
||
1121 QApplication::keyboardModifiers() & Qt::ControlModifier
;
1122 if (!shiftOrControlPressed
) {
1123 m_oldSelection
.clear();
1127 KItemSet selectedItems
;
1129 // Select all visible items that intersect with the rubberband
1130 foreach (const KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
1131 const int index
= widget
->index();
1133 const QRectF widgetRect
= m_view
->itemRect(index
);
1134 if (widgetRect
.intersects(rubberBandRect
)) {
1135 const QRectF iconRect
= widget
->iconRect().translated(widgetRect
.topLeft());
1136 const QRectF textRect
= widget
->textRect().translated(widgetRect
.topLeft());
1137 if (iconRect
.intersects(rubberBandRect
) || textRect
.intersects(rubberBandRect
)) {
1138 selectedItems
.insert(index
);
1143 // Select all invisible items that intersect with the rubberband. Instead of
1144 // iterating all items only the area which might be touched by the rubberband
1146 const bool increaseIndex
= scrollVertical
?
1147 startPos
.y() > endPos
.y(): startPos
.x() > endPos
.x();
1149 int index
= increaseIndex
? m_view
->lastVisibleIndex() + 1 : m_view
->firstVisibleIndex() - 1;
1150 bool selectionFinished
= false;
1152 const QRectF widgetRect
= m_view
->itemRect(index
);
1153 if (widgetRect
.intersects(rubberBandRect
)) {
1154 selectedItems
.insert(index
);
1157 if (increaseIndex
) {
1159 selectionFinished
= (index
>= m_model
->count()) ||
1160 ( scrollVertical
&& widgetRect
.top() > rubberBandRect
.bottom()) ||
1161 (!scrollVertical
&& widgetRect
.left() > rubberBandRect
.right());
1164 selectionFinished
= (index
< 0) ||
1165 ( scrollVertical
&& widgetRect
.bottom() < rubberBandRect
.top()) ||
1166 (!scrollVertical
&& widgetRect
.right() < rubberBandRect
.left());
1168 } while (!selectionFinished
);
1170 if (QApplication::keyboardModifiers() & Qt::ControlModifier
) {
1171 // If Control is pressed, the selection state of all items in the rubberband is toggled.
1172 // Therefore, the new selection contains:
1173 // 1. All previously selected items which are not inside the rubberband, and
1174 // 2. all items inside the rubberband which have not been selected previously.
1175 m_selectionManager
->setSelectedItems(m_oldSelection
^ selectedItems
);
1178 m_selectionManager
->setSelectedItems(selectedItems
+ m_oldSelection
);
1182 void KItemListController::startDragging()
1184 if (!m_view
|| !m_model
) {
1188 const KItemSet selectedItems
= m_selectionManager
->selectedItems();
1189 if (selectedItems
.isEmpty()) {
1193 QMimeData
* data
= m_model
->createMimeData(selectedItems
);
1198 // The created drag object will be owned and deleted
1199 // by QApplication::activeWindow().
1200 QDrag
* drag
= new QDrag(QApplication::activeWindow());
1201 drag
->setMimeData(data
);
1203 const QPixmap pixmap
= m_view
->createDragPixmap(selectedItems
);
1204 drag
->setPixmap(pixmap
);
1206 const QPoint
hotSpot((pixmap
.width() / pixmap
.devicePixelRatio()) / 2, 0);
1207 drag
->setHotSpot(hotSpot
);
1209 drag
->exec(Qt::MoveAction
| Qt::CopyAction
| Qt::LinkAction
, Qt::CopyAction
);
1211 QAccessibleEvent
accessibilityEvent(view(), QAccessible::DragDropStart
);
1212 QAccessible::updateAccessibility(&accessibilityEvent
);
1215 KItemListWidget
* KItemListController::hoveredWidget() const
1219 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
1220 if (widget
->isHovered()) {
1228 KItemListWidget
* KItemListController::widgetForPos(const QPointF
& pos
) const
1232 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
1233 const QPointF mappedPos
= widget
->mapFromItem(m_view
, pos
);
1235 const bool hovered
= widget
->contains(mappedPos
) &&
1236 !widget
->expansionToggleRect().contains(mappedPos
);
1245 void KItemListController::updateKeyboardAnchor()
1247 const bool validAnchor
= m_keyboardAnchorIndex
>= 0 &&
1248 m_keyboardAnchorIndex
< m_model
->count() &&
1249 keyboardAnchorPos(m_keyboardAnchorIndex
) == m_keyboardAnchorPos
;
1251 const int index
= m_selectionManager
->currentItem();
1252 m_keyboardAnchorIndex
= index
;
1253 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
1257 int KItemListController::nextRowIndex(int index
) const
1259 if (m_keyboardAnchorIndex
< 0) {
1263 const int maxIndex
= m_model
->count() - 1;
1264 if (index
== maxIndex
) {
1268 // Calculate the index of the last column inside the row of the current index
1269 int lastColumnIndex
= index
;
1270 while (keyboardAnchorPos(lastColumnIndex
+ 1) > keyboardAnchorPos(lastColumnIndex
)) {
1272 if (lastColumnIndex
>= maxIndex
) {
1277 // Based on the last column index go to the next row and calculate the nearest index
1278 // that is below the current index
1279 int nextRowIndex
= lastColumnIndex
+ 1;
1280 int searchIndex
= nextRowIndex
;
1281 qreal minDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(nextRowIndex
));
1282 while (searchIndex
< maxIndex
&& keyboardAnchorPos(searchIndex
+ 1) > keyboardAnchorPos(searchIndex
)) {
1284 const qreal searchDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(searchIndex
));
1285 if (searchDiff
< minDiff
) {
1286 minDiff
= searchDiff
;
1287 nextRowIndex
= searchIndex
;
1291 return nextRowIndex
;
1294 int KItemListController::previousRowIndex(int index
) const
1296 if (m_keyboardAnchorIndex
< 0 || index
== 0) {
1300 // Calculate the index of the first column inside the row of the current index
1301 int firstColumnIndex
= index
;
1302 while (keyboardAnchorPos(firstColumnIndex
- 1) < keyboardAnchorPos(firstColumnIndex
)) {
1304 if (firstColumnIndex
<= 0) {
1309 // Based on the first column index go to the previous row and calculate the nearest index
1310 // that is above the current index
1311 int previousRowIndex
= firstColumnIndex
- 1;
1312 int searchIndex
= previousRowIndex
;
1313 qreal minDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(previousRowIndex
));
1314 while (searchIndex
> 0 && keyboardAnchorPos(searchIndex
- 1) < keyboardAnchorPos(searchIndex
)) {
1316 const qreal searchDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(searchIndex
));
1317 if (searchDiff
< minDiff
) {
1318 minDiff
= searchDiff
;
1319 previousRowIndex
= searchIndex
;
1323 return previousRowIndex
;
1326 qreal
KItemListController::keyboardAnchorPos(int index
) const
1328 const QRectF itemRect
= m_view
->itemRect(index
);
1329 if (!itemRect
.isEmpty()) {
1330 return (m_view
->scrollOrientation() == Qt::Vertical
) ? itemRect
.x() : itemRect
.y();
1336 void KItemListController::updateExtendedSelectionRegion()
1339 const bool extend
= (m_selectionBehavior
!= MultiSelection
);
1340 KItemListStyleOption option
= m_view
->styleOption();
1341 if (option
.extendedSelectionRegion
!= extend
) {
1342 option
.extendedSelectionRegion
= extend
;
1343 m_view
->setStyleOption(option
);