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"
27 #include "kitemlistview.h"
28 #include "kitemlistselectionmanager.h"
30 #include "private/kitemlistrubberband.h"
31 #include "private/kitemlistkeyboardsearchmanager.h"
33 #include <QApplication>
36 #include <QGraphicsScene>
37 #include <QGraphicsSceneEvent>
38 #include <QGraphicsView>
41 #include <QAccessible>
42 #include <views/draganddrophelper.h>
44 KItemListController::KItemListController(KItemModelBase
* model
, KItemListView
* view
, QObject
* parent
) :
46 m_singleClickActivationEnforced(false),
47 m_selectionTogglePressed(false),
48 m_clearSelectionIfItemsAreNotDragged(false),
49 m_selectionBehavior(NoSelection
),
50 m_autoActivationBehavior(ActivationAndExpansion
),
51 m_mouseDoubleClickAction(ActivateItemOnly
),
54 m_selectionManager(new KItemListSelectionManager(this)),
55 m_keyboardManager(new KItemListKeyboardSearchManager(this)),
58 m_autoActivationTimer(nullptr),
60 m_keyboardAnchorIndex(-1),
61 m_keyboardAnchorPos(0)
63 connect(m_keyboardManager
, &KItemListKeyboardSearchManager::changeCurrentItem
,
64 this, &KItemListController::slotChangeCurrentItem
);
65 connect(m_selectionManager
, &KItemListSelectionManager::currentChanged
,
66 m_keyboardManager
, &KItemListKeyboardSearchManager::slotCurrentChanged
);
68 m_autoActivationTimer
= new QTimer(this);
69 m_autoActivationTimer
->setSingleShot(true);
70 m_autoActivationTimer
->setInterval(-1);
71 connect(m_autoActivationTimer
, &QTimer::timeout
, this, &KItemListController::slotAutoActivationTimeout
);
77 KItemListController::~KItemListController()
86 void KItemListController::setModel(KItemModelBase
* model
)
88 if (m_model
== model
) {
92 KItemModelBase
* oldModel
= m_model
;
94 oldModel
->deleteLater();
99 m_model
->setParent(this);
103 m_view
->setModel(m_model
);
106 m_selectionManager
->setModel(m_model
);
108 emit
modelChanged(m_model
, oldModel
);
111 KItemModelBase
* KItemListController::model() const
116 KItemListSelectionManager
* KItemListController::selectionManager() const
118 return m_selectionManager
;
121 void KItemListController::setView(KItemListView
* view
)
123 if (m_view
== view
) {
127 KItemListView
* oldView
= m_view
;
129 disconnect(oldView
, &KItemListView::scrollOffsetChanged
, this, &KItemListController::slotViewScrollOffsetChanged
);
130 oldView
->deleteLater();
136 m_view
->setParent(this);
137 m_view
->setController(this);
138 m_view
->setModel(m_model
);
139 connect(m_view
, &KItemListView::scrollOffsetChanged
, this, &KItemListController::slotViewScrollOffsetChanged
);
140 updateExtendedSelectionRegion();
143 emit
viewChanged(m_view
, oldView
);
146 KItemListView
* KItemListController::view() const
151 void KItemListController::setSelectionBehavior(SelectionBehavior behavior
)
153 m_selectionBehavior
= behavior
;
154 updateExtendedSelectionRegion();
157 KItemListController::SelectionBehavior
KItemListController::selectionBehavior() const
159 return m_selectionBehavior
;
162 void KItemListController::setAutoActivationBehavior(AutoActivationBehavior behavior
)
164 m_autoActivationBehavior
= behavior
;
167 KItemListController::AutoActivationBehavior
KItemListController::autoActivationBehavior() const
169 return m_autoActivationBehavior
;
172 void KItemListController::setMouseDoubleClickAction(MouseDoubleClickAction action
)
174 m_mouseDoubleClickAction
= action
;
177 KItemListController::MouseDoubleClickAction
KItemListController::mouseDoubleClickAction() const
179 return m_mouseDoubleClickAction
;
182 int KItemListController::indexCloseToMousePressedPosition() const
184 QHashIterator
<KItemListWidget
*, KItemListGroupHeader
*> it(m_view
->m_visibleGroups
);
185 while (it
.hasNext()) {
187 KItemListGroupHeader
*groupHeader
= it
.value();
188 const QPointF mappedToGroup
= groupHeader
->mapFromItem(nullptr, m_pressedMousePos
);
189 if (groupHeader
->contains(mappedToGroup
)) {
190 return it
.key()->index();
196 void KItemListController::setAutoActivationDelay(int delay
)
198 m_autoActivationTimer
->setInterval(delay
);
201 int KItemListController::autoActivationDelay() const
203 return m_autoActivationTimer
->interval();
206 void KItemListController::setSingleClickActivationEnforced(bool singleClick
)
208 m_singleClickActivationEnforced
= singleClick
;
211 bool KItemListController::singleClickActivationEnforced() const
213 return m_singleClickActivationEnforced
;
216 bool KItemListController::showEvent(QShowEvent
* event
)
222 bool KItemListController::hideEvent(QHideEvent
* event
)
228 bool KItemListController::keyPressEvent(QKeyEvent
* event
)
230 int index
= m_selectionManager
->currentItem();
231 int key
= event
->key();
233 // Handle the expanding/collapsing of items
234 if (m_view
->supportsItemExpanding() && m_model
->isExpandable(index
)) {
235 if (key
== Qt::Key_Right
) {
236 if (m_model
->setExpanded(index
, true)) {
239 } else if (key
== Qt::Key_Left
) {
240 if (m_model
->setExpanded(index
, false)) {
246 const bool shiftPressed
= event
->modifiers() & Qt::ShiftModifier
;
247 const bool controlPressed
= event
->modifiers() & Qt::ControlModifier
;
248 const bool shiftOrControlPressed
= shiftPressed
|| controlPressed
;
250 const int itemCount
= m_model
->count();
252 // For horizontal scroll orientation, transform
253 // the arrow keys to simplify the event handling.
254 if (m_view
->scrollOrientation() == Qt::Horizontal
) {
256 case Qt::Key_Up
: key
= Qt::Key_Left
; break;
257 case Qt::Key_Down
: key
= Qt::Key_Right
; break;
258 case Qt::Key_Left
: key
= Qt::Key_Up
; break;
259 case Qt::Key_Right
: key
= Qt::Key_Down
; break;
264 const bool selectSingleItem
= m_selectionBehavior
!= NoSelection
&&
266 (key
== Qt::Key_Home
|| key
== Qt::Key_End
||
267 key
== Qt::Key_Up
|| key
== Qt::Key_Down
||
268 key
== Qt::Key_Left
|| key
== Qt::Key_Right
);
269 if (selectSingleItem
) {
270 const int current
= m_selectionManager
->currentItem();
271 m_selectionManager
->setSelected(current
);
278 m_keyboardAnchorIndex
= index
;
279 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
283 index
= itemCount
- 1;
284 m_keyboardAnchorIndex
= index
;
285 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
290 const int expandedParentsCount
= m_model
->expandedParentsCount(index
);
291 if (expandedParentsCount
== 0) {
294 // Go to the parent of the current item.
297 } while (index
> 0 && m_model
->expandedParentsCount(index
) == expandedParentsCount
);
299 m_keyboardAnchorIndex
= index
;
300 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
305 if (index
< itemCount
- 1) {
307 m_keyboardAnchorIndex
= index
;
308 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
313 updateKeyboardAnchor();
314 index
= previousRowIndex(index
);
318 updateKeyboardAnchor();
319 index
= nextRowIndex(index
);
323 if (m_view
->scrollOrientation() == Qt::Horizontal
) {
324 // The new current index should correspond to the first item in the current column.
325 int newIndex
= qMax(index
- 1, 0);
326 while (newIndex
!= index
&& m_view
->itemRect(newIndex
).topLeft().y() < m_view
->itemRect(index
).topLeft().y()) {
328 newIndex
= qMax(index
- 1, 0);
330 m_keyboardAnchorIndex
= index
;
331 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
333 const qreal currentItemBottom
= m_view
->itemRect(index
).bottomLeft().y();
334 const qreal height
= m_view
->geometry().height();
336 // The new current item should be the first item in the current
337 // column whose itemRect's top coordinate is larger than targetY.
338 const qreal targetY
= currentItemBottom
- height
;
340 updateKeyboardAnchor();
341 int newIndex
= previousRowIndex(index
);
344 updateKeyboardAnchor();
345 newIndex
= previousRowIndex(index
);
346 } while (m_view
->itemRect(newIndex
).topLeft().y() > targetY
&& newIndex
!= index
);
350 case Qt::Key_PageDown
:
351 if (m_view
->scrollOrientation() == Qt::Horizontal
) {
352 // The new current index should correspond to the last item in the current column.
353 int newIndex
= qMin(index
+ 1, m_model
->count() - 1);
354 while (newIndex
!= index
&& m_view
->itemRect(newIndex
).topLeft().y() > m_view
->itemRect(index
).topLeft().y()) {
356 newIndex
= qMin(index
+ 1, m_model
->count() - 1);
358 m_keyboardAnchorIndex
= index
;
359 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
361 const qreal currentItemTop
= m_view
->itemRect(index
).topLeft().y();
362 const qreal height
= m_view
->geometry().height();
364 // The new current item should be the last item in the current
365 // column whose itemRect's bottom coordinate is smaller than targetY.
366 const qreal targetY
= currentItemTop
+ height
;
368 updateKeyboardAnchor();
369 int newIndex
= nextRowIndex(index
);
372 updateKeyboardAnchor();
373 newIndex
= nextRowIndex(index
);
374 } while (m_view
->itemRect(newIndex
).bottomLeft().y() < targetY
&& newIndex
!= index
);
379 case Qt::Key_Return
: {
380 const KItemSet selectedItems
= m_selectionManager
->selectedItems();
381 if (selectedItems
.count() >= 2) {
382 emit
itemsActivated(selectedItems
);
383 } else if (selectedItems
.count() == 1) {
384 emit
itemActivated(selectedItems
.first());
386 emit
itemActivated(index
);
392 // Emit the signal itemContextMenuRequested() in case if at least one
393 // item is selected. Otherwise the signal viewContextMenuRequested() will be emitted.
394 const KItemSet selectedItems
= m_selectionManager
->selectedItems();
396 if (selectedItems
.count() >= 2) {
397 const int currentItemIndex
= m_selectionManager
->currentItem();
398 index
= selectedItems
.contains(currentItemIndex
)
399 ? currentItemIndex
: selectedItems
.first();
400 } else if (selectedItems
.count() == 1) {
401 index
= selectedItems
.first();
405 const QRectF contextRect
= m_view
->itemContextRect(index
);
406 const QPointF
pos(m_view
->scene()->views().first()->mapToGlobal(contextRect
.bottomRight().toPoint()));
407 emit
itemContextMenuRequested(index
, pos
);
409 emit
viewContextMenuRequested(QCursor::pos());
415 if (m_selectionBehavior
!= SingleSelection
) {
416 m_selectionManager
->clearSelection();
418 m_keyboardManager
->cancelSearch();
419 emit
escapePressed();
423 if (m_selectionBehavior
== MultiSelection
) {
424 if (controlPressed
) {
425 // Toggle the selection state of the current item.
426 m_selectionManager
->endAnchoredSelection();
427 m_selectionManager
->setSelected(index
, 1, KItemListSelectionManager::Toggle
);
428 m_selectionManager
->beginAnchoredSelection(index
);
431 // Select the current item if it is not selected yet.
432 const int current
= m_selectionManager
->currentItem();
433 if (!m_selectionManager
->isSelected(current
)) {
434 m_selectionManager
->setSelected(current
);
439 // Fall through to the default case and add the Space to the current search string.
442 m_keyboardManager
->addKeys(event
->text());
443 // Make sure unconsumed events get propagated up the chain. #302329
448 if (m_selectionManager
->currentItem() != index
) {
449 switch (m_selectionBehavior
) {
451 m_selectionManager
->setCurrentItem(index
);
454 case SingleSelection
:
455 m_selectionManager
->setCurrentItem(index
);
456 m_selectionManager
->clearSelection();
457 m_selectionManager
->setSelected(index
, 1);
461 if (controlPressed
) {
462 m_selectionManager
->endAnchoredSelection();
465 m_selectionManager
->setCurrentItem(index
);
467 if (!shiftOrControlPressed
) {
468 m_selectionManager
->clearSelection();
469 m_selectionManager
->setSelected(index
, 1);
473 m_selectionManager
->beginAnchoredSelection(index
);
478 m_view
->scrollToItem(index
);
483 void KItemListController::slotChangeCurrentItem(const QString
& text
, bool searchFromNextItem
)
485 if (!m_model
|| m_model
->count() == 0) {
488 const int currentIndex
= m_selectionManager
->currentItem();
490 if (searchFromNextItem
) {
491 index
= m_model
->indexForKeyboardSearch(text
, (currentIndex
+ 1) % m_model
->count());
493 index
= m_model
->indexForKeyboardSearch(text
, currentIndex
);
496 m_selectionManager
->setCurrentItem(index
);
498 if (m_selectionBehavior
!= NoSelection
) {
499 m_selectionManager
->clearSelection();
500 m_selectionManager
->setSelected(index
, 1);
501 m_selectionManager
->beginAnchoredSelection(index
);
504 m_view
->scrollToItem(index
);
508 void KItemListController::slotAutoActivationTimeout()
510 if (!m_model
|| !m_view
) {
514 const int index
= m_autoActivationTimer
->property("index").toInt();
515 if (index
< 0 || index
>= m_model
->count()) {
519 /* m_view->isUnderMouse() fixes a bug in the Folder-View-Panel and in the
522 * Bug: When you drag a file onto a Folder-View-Item or a Places-Item and
523 * then move away before the auto-activation timeout triggers, than the
524 * item still becomes activated/expanded.
526 * See Bug 293200 and 305783
528 if (m_model
->supportsDropping(index
) && m_view
->isUnderMouse()) {
529 if (m_view
->supportsItemExpanding() && m_model
->isExpandable(index
)) {
530 const bool expanded
= m_model
->isExpanded(index
);
531 m_model
->setExpanded(index
, !expanded
);
532 } else if (m_autoActivationBehavior
!= ExpansionOnly
) {
533 emit
itemActivated(index
);
538 bool KItemListController::inputMethodEvent(QInputMethodEvent
* event
)
544 bool KItemListController::mousePressEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
550 m_pressedMousePos
= transform
.map(event
->pos());
551 m_pressedIndex
= m_view
->itemAt(m_pressedMousePos
);
552 emit
mouseButtonPressed(m_pressedIndex
, event
->buttons());
554 if (event
->buttons() & (Qt::BackButton
| Qt::ForwardButton
)) {
555 // Do not select items when clicking the back/forward buttons, see
556 // https://bugs.kde.org/show_bug.cgi?id=327412.
560 if (m_view
->isAboveExpansionToggle(m_pressedIndex
, m_pressedMousePos
)) {
561 m_selectionManager
->endAnchoredSelection();
562 m_selectionManager
->setCurrentItem(m_pressedIndex
);
563 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
567 m_selectionTogglePressed
= m_view
->isAboveSelectionToggle(m_pressedIndex
, m_pressedMousePos
);
568 if (m_selectionTogglePressed
) {
569 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
570 // The previous anchored selection has been finished already in
571 // KItemListSelectionManager::setSelected(). We can safely change
572 // the current item and start a new anchored selection now.
573 m_selectionManager
->setCurrentItem(m_pressedIndex
);
574 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
578 const bool shiftPressed
= event
->modifiers() & Qt::ShiftModifier
;
579 const bool controlPressed
= event
->modifiers() & Qt::ControlModifier
;
581 // The previous selection is cleared if either
582 // 1. The selection mode is SingleSelection, or
583 // 2. the selection mode is MultiSelection, and *none* of the following conditions are met:
584 // a) Shift or Control are pressed.
585 // b) The clicked item is selected already. In that case, the user might want to:
586 // - start dragging multiple items, or
587 // - open the context menu and perform an action for all selected items.
588 const bool shiftOrControlPressed
= shiftPressed
|| controlPressed
;
589 const bool pressedItemAlreadySelected
= m_pressedIndex
>= 0 && m_selectionManager
->isSelected(m_pressedIndex
);
590 const bool clearSelection
= m_selectionBehavior
== SingleSelection
||
591 (!shiftOrControlPressed
&& !pressedItemAlreadySelected
);
592 if (clearSelection
) {
593 m_selectionManager
->clearSelection();
594 } else if (pressedItemAlreadySelected
&& !shiftOrControlPressed
&& (event
->buttons() & Qt::LeftButton
)) {
595 // The user might want to start dragging multiple items, but if he clicks the item
596 // in order to trigger it instead, the other selected items must be deselected.
597 // However, we do not know yet what the user is going to do.
598 // -> remember that the user pressed an item which had been selected already and
599 // clear the selection in mouseReleaseEvent(), unless the items are dragged.
600 m_clearSelectionIfItemsAreNotDragged
= true;
602 if (m_selectionManager
->selectedItems().count() == 1 && m_view
->isAboveText(m_pressedIndex
, m_pressedMousePos
)) {
603 emit
selectedItemTextPressed(m_pressedIndex
);
608 // Finish the anchored selection before the current index is changed
609 m_selectionManager
->endAnchoredSelection();
612 if (m_pressedIndex
>= 0) {
613 m_selectionManager
->setCurrentItem(m_pressedIndex
);
615 switch (m_selectionBehavior
) {
619 case SingleSelection
:
620 m_selectionManager
->setSelected(m_pressedIndex
);
624 if (controlPressed
&& !shiftPressed
) {
625 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
626 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
627 } else if (!shiftPressed
|| !m_selectionManager
->isAnchoredSelectionActive()) {
628 // Select the pressed item and start a new anchored selection
629 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Select
);
630 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
639 if (event
->buttons() & Qt::RightButton
) {
640 emit
itemContextMenuRequested(m_pressedIndex
, event
->screenPos());
646 if (event
->buttons() & Qt::RightButton
) {
647 const QRectF headerBounds
= m_view
->headerBoundaries();
648 if (headerBounds
.contains(event
->pos())) {
649 emit
headerContextMenuRequested(event
->screenPos());
651 emit
viewContextMenuRequested(event
->screenPos());
656 if (m_selectionBehavior
== MultiSelection
) {
657 QPointF startPos
= m_pressedMousePos
;
658 if (m_view
->scrollOrientation() == Qt::Vertical
) {
659 startPos
.ry() += m_view
->scrollOffset();
660 if (m_view
->itemSize().width() < 0) {
661 // Use a special rubberband for views that have only one column and
662 // expand the rubberband to use the whole width of the view.
666 startPos
.rx() += m_view
->scrollOffset();
669 m_oldSelection
= m_selectionManager
->selectedItems();
670 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
671 rubberBand
->setStartPosition(startPos
);
672 rubberBand
->setEndPosition(startPos
);
673 rubberBand
->setActive(true);
674 connect(rubberBand
, &KItemListRubberBand::endPositionChanged
, this, &KItemListController::slotRubberBandChanged
);
675 m_view
->setAutoScroll(true);
681 bool KItemListController::mouseMoveEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
687 if (m_pressedIndex
>= 0) {
688 // Check whether a dragging should be started
689 if (event
->buttons() & Qt::LeftButton
) {
690 const QPointF pos
= transform
.map(event
->pos());
691 if ((pos
- m_pressedMousePos
).manhattanLength() >= QApplication::startDragDistance()) {
692 if (!m_selectionManager
->isSelected(m_pressedIndex
)) {
693 // Always assure that the dragged item gets selected. Usually this is already
694 // done on the mouse-press event, but when using the selection-toggle on a
695 // selected item the dragged item is not selected yet.
696 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
698 // A selected item has been clicked to drag all selected items
699 // -> the selection should not be cleared when the mouse button is released.
700 m_clearSelectionIfItemsAreNotDragged
= false;
707 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
708 if (rubberBand
->isActive()) {
709 QPointF endPos
= transform
.map(event
->pos());
711 // Update the current item.
712 const int newCurrent
= m_view
->itemAt(endPos
);
713 if (newCurrent
>= 0) {
714 // It's expected that the new current index is also the new anchor (bug 163451).
715 m_selectionManager
->endAnchoredSelection();
716 m_selectionManager
->setCurrentItem(newCurrent
);
717 m_selectionManager
->beginAnchoredSelection(newCurrent
);
720 if (m_view
->scrollOrientation() == Qt::Vertical
) {
721 endPos
.ry() += m_view
->scrollOffset();
722 if (m_view
->itemSize().width() < 0) {
723 // Use a special rubberband for views that have only one column and
724 // expand the rubberband to use the whole width of the view.
725 endPos
.setX(m_view
->size().width());
728 endPos
.rx() += m_view
->scrollOffset();
730 rubberBand
->setEndPosition(endPos
);
737 bool KItemListController::mouseReleaseEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
743 emit
mouseButtonReleased(m_pressedIndex
, event
->buttons());
745 const bool isAboveSelectionToggle
= m_view
->isAboveSelectionToggle(m_pressedIndex
, m_pressedMousePos
);
746 if (isAboveSelectionToggle
) {
747 m_selectionTogglePressed
= false;
751 if (!isAboveSelectionToggle
&& m_selectionTogglePressed
) {
752 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
753 m_selectionTogglePressed
= false;
757 const bool shiftOrControlPressed
= event
->modifiers() & Qt::ShiftModifier
||
758 event
->modifiers() & Qt::ControlModifier
;
760 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
761 if (rubberBand
->isActive()) {
762 disconnect(rubberBand
, &KItemListRubberBand::endPositionChanged
, this, &KItemListController::slotRubberBandChanged
);
763 rubberBand
->setActive(false);
764 m_oldSelection
.clear();
765 m_view
->setAutoScroll(false);
768 const QPointF pos
= transform
.map(event
->pos());
769 const int index
= m_view
->itemAt(pos
);
771 if (index
>= 0 && index
== m_pressedIndex
) {
772 // The release event is done above the same item as the press event
774 if (m_clearSelectionIfItemsAreNotDragged
) {
775 // A selected item has been clicked, but no drag operation has been started
776 // -> clear the rest of the selection.
777 m_selectionManager
->clearSelection();
778 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Select
);
779 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
782 if (event
->button() & Qt::LeftButton
) {
783 bool emitItemActivated
= true;
784 if (m_view
->isAboveExpansionToggle(index
, pos
)) {
785 const bool expanded
= m_model
->isExpanded(index
);
786 m_model
->setExpanded(index
, !expanded
);
788 emit
itemExpansionToggleClicked(index
);
789 emitItemActivated
= false;
790 } else if (shiftOrControlPressed
) {
791 // The mouse click should only update the selection, not trigger the item
792 emitItemActivated
= false;
793 } else if (!(m_view
->style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick
) || m_singleClickActivationEnforced
)) {
794 emitItemActivated
= false;
796 if (emitItemActivated
) {
797 emit
itemActivated(index
);
799 } else if (event
->button() & Qt::MidButton
) {
800 emit
itemMiddleClicked(index
);
804 m_pressedMousePos
= QPointF();
806 m_clearSelectionIfItemsAreNotDragged
= false;
810 bool KItemListController::mouseDoubleClickEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
812 const QPointF pos
= transform
.map(event
->pos());
813 const int index
= m_view
->itemAt(pos
);
815 // Expand item if desired - See Bug 295573
816 if (m_mouseDoubleClickAction
!= ActivateItemOnly
) {
817 if (m_view
&& m_model
&& m_view
->supportsItemExpanding() && m_model
->isExpandable(index
)) {
818 const bool expanded
= m_model
->isExpanded(index
);
819 m_model
->setExpanded(index
, !expanded
);
823 if (event
->button() & Qt::RightButton
) {
824 m_selectionManager
->clearSelection();
826 m_selectionManager
->setSelected(index
);
827 emit
itemContextMenuRequested(index
, event
->screenPos());
829 const QRectF headerBounds
= m_view
->headerBoundaries();
830 if (headerBounds
.contains(event
->pos())) {
831 emit
headerContextMenuRequested(event
->screenPos());
833 emit
viewContextMenuRequested(event
->screenPos());
839 bool emitItemActivated
= !(m_view
->style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick
) || m_singleClickActivationEnforced
) &&
840 (event
->button() & Qt::LeftButton
) &&
841 index
>= 0 && index
< m_model
->count();
842 if (emitItemActivated
) {
843 emit
itemActivated(index
);
848 bool KItemListController::dragEnterEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
855 bool KItemListController::dragLeaveEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
860 m_autoActivationTimer
->stop();
861 m_view
->setAutoScroll(false);
862 m_view
->hideDropIndicator();
864 KItemListWidget
* widget
= hoveredWidget();
866 widget
->setHovered(false);
867 emit
itemUnhovered(widget
->index());
872 bool KItemListController::dragMoveEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
874 if (!m_model
|| !m_view
) {
879 QUrl hoveredDir
= m_model
->directory();
880 KItemListWidget
* oldHoveredWidget
= hoveredWidget();
882 const QPointF pos
= transform
.map(event
->pos());
883 KItemListWidget
* newHoveredWidget
= widgetForPos(pos
);
885 if (oldHoveredWidget
!= newHoveredWidget
) {
886 m_autoActivationTimer
->stop();
888 if (oldHoveredWidget
) {
889 oldHoveredWidget
->setHovered(false);
890 emit
itemUnhovered(oldHoveredWidget
->index());
894 if (newHoveredWidget
) {
895 bool droppingBetweenItems
= false;
896 if (m_model
->sortRole().isEmpty()) {
897 // The model supports inserting items between other items.
898 droppingBetweenItems
= (m_view
->showDropIndicator(pos
) >= 0);
901 const int index
= newHoveredWidget
->index();
903 if (m_model
->isDir(index
)) {
904 hoveredDir
= m_model
->url(index
);
907 if (!droppingBetweenItems
) {
908 if (m_model
->supportsDropping(index
)) {
909 // Something has been dragged on an item.
910 m_view
->hideDropIndicator();
911 if (!newHoveredWidget
->isHovered()) {
912 newHoveredWidget
->setHovered(true);
913 emit
itemHovered(index
);
916 if (!m_autoActivationTimer
->isActive() && m_autoActivationTimer
->interval() >= 0) {
917 m_autoActivationTimer
->setProperty("index", index
);
918 m_autoActivationTimer
->start();
922 m_autoActivationTimer
->stop();
923 if (newHoveredWidget
&& newHoveredWidget
->isHovered()) {
924 newHoveredWidget
->setHovered(false);
925 emit
itemUnhovered(index
);
929 m_view
->hideDropIndicator();
932 event
->setAccepted(!DragAndDropHelper::urlListMatchesUrl(event
->mimeData()->urls(), hoveredDir
));
937 bool KItemListController::dropEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
943 m_autoActivationTimer
->stop();
944 m_view
->setAutoScroll(false);
946 const QPointF pos
= transform
.map(event
->pos());
948 int dropAboveIndex
= -1;
949 if (m_model
->sortRole().isEmpty()) {
950 // The model supports inserting of items between other items.
951 dropAboveIndex
= m_view
->showDropIndicator(pos
);
954 if (dropAboveIndex
>= 0) {
955 // Something has been dropped between two items.
956 m_view
->hideDropIndicator();
957 emit
aboveItemDropEvent(dropAboveIndex
, event
);
958 } else if (!event
->mimeData()->hasFormat(m_model
->blacklistItemDropEventMimeType())) {
959 // Something has been dropped on an item or on an empty part of the view.
960 emit
itemDropEvent(m_view
->itemAt(pos
), event
);
963 QAccessibleEvent
accessibilityEvent(view(), QAccessible::DragDropEnd
);
964 QAccessible::updateAccessibility(&accessibilityEvent
);
969 bool KItemListController::hoverEnterEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
976 bool KItemListController::hoverMoveEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
979 if (!m_model
|| !m_view
) {
983 KItemListWidget
* oldHoveredWidget
= hoveredWidget();
984 const QPointF pos
= transform
.map(event
->pos());
985 KItemListWidget
* newHoveredWidget
= widgetForPos(pos
);
987 if (oldHoveredWidget
!= newHoveredWidget
) {
988 if (oldHoveredWidget
) {
989 oldHoveredWidget
->setHovered(false);
990 emit
itemUnhovered(oldHoveredWidget
->index());
993 if (newHoveredWidget
) {
994 newHoveredWidget
->setHovered(true);
995 const QPointF mappedPos
= newHoveredWidget
->mapFromItem(m_view
, pos
);
996 newHoveredWidget
->setHoverPosition(mappedPos
);
997 emit
itemHovered(newHoveredWidget
->index());
999 } else if (oldHoveredWidget
) {
1000 const QPointF mappedPos
= oldHoveredWidget
->mapFromItem(m_view
, pos
);
1001 oldHoveredWidget
->setHoverPosition(mappedPos
);
1007 bool KItemListController::hoverLeaveEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
1010 Q_UNUSED(transform
);
1012 if (!m_model
|| !m_view
) {
1016 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
1017 if (widget
->isHovered()) {
1018 widget
->setHovered(false);
1019 emit
itemUnhovered(widget
->index());
1025 bool KItemListController::wheelEvent(QGraphicsSceneWheelEvent
* event
, const QTransform
& transform
)
1028 Q_UNUSED(transform
);
1032 bool KItemListController::resizeEvent(QGraphicsSceneResizeEvent
* event
, const QTransform
& transform
)
1035 Q_UNUSED(transform
);
1039 bool KItemListController::processEvent(QEvent
* event
, const QTransform
& transform
)
1045 switch (event
->type()) {
1046 case QEvent::KeyPress
:
1047 return keyPressEvent(static_cast<QKeyEvent
*>(event
));
1048 case QEvent::InputMethod
:
1049 return inputMethodEvent(static_cast<QInputMethodEvent
*>(event
));
1050 case QEvent::GraphicsSceneMousePress
:
1051 return mousePressEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
1052 case QEvent::GraphicsSceneMouseMove
:
1053 return mouseMoveEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
1054 case QEvent::GraphicsSceneMouseRelease
:
1055 return mouseReleaseEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
1056 case QEvent::GraphicsSceneMouseDoubleClick
:
1057 return mouseDoubleClickEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
1058 case QEvent::GraphicsSceneWheel
:
1059 return wheelEvent(static_cast<QGraphicsSceneWheelEvent
*>(event
), QTransform());
1060 case QEvent::GraphicsSceneDragEnter
:
1061 return dragEnterEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
1062 case QEvent::GraphicsSceneDragLeave
:
1063 return dragLeaveEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
1064 case QEvent::GraphicsSceneDragMove
:
1065 return dragMoveEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
1066 case QEvent::GraphicsSceneDrop
:
1067 return dropEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
1068 case QEvent::GraphicsSceneHoverEnter
:
1069 return hoverEnterEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
1070 case QEvent::GraphicsSceneHoverMove
:
1071 return hoverMoveEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
1072 case QEvent::GraphicsSceneHoverLeave
:
1073 return hoverLeaveEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
1074 case QEvent::GraphicsSceneResize
:
1075 return resizeEvent(static_cast<QGraphicsSceneResizeEvent
*>(event
), transform
);
1083 void KItemListController::slotViewScrollOffsetChanged(qreal current
, qreal previous
)
1089 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
1090 if (rubberBand
->isActive()) {
1091 const qreal diff
= current
- previous
;
1092 // TODO: Ideally just QCursor::pos() should be used as
1093 // new end-position but it seems there is no easy way
1094 // to have something like QWidget::mapFromGlobal() for QGraphicsWidget
1095 // (... or I just missed an easy way to do the mapping)
1096 QPointF endPos
= rubberBand
->endPosition();
1097 if (m_view
->scrollOrientation() == Qt::Vertical
) {
1098 endPos
.ry() += diff
;
1100 endPos
.rx() += diff
;
1103 rubberBand
->setEndPosition(endPos
);
1107 void KItemListController::slotRubberBandChanged()
1109 if (!m_view
|| !m_model
|| m_model
->count() <= 0) {
1113 const KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
1114 const QPointF startPos
= rubberBand
->startPosition();
1115 const QPointF endPos
= rubberBand
->endPosition();
1116 QRectF rubberBandRect
= QRectF(startPos
, endPos
).normalized();
1118 const bool scrollVertical
= (m_view
->scrollOrientation() == Qt::Vertical
);
1119 if (scrollVertical
) {
1120 rubberBandRect
.translate(0, -m_view
->scrollOffset());
1122 rubberBandRect
.translate(-m_view
->scrollOffset(), 0);
1125 if (!m_oldSelection
.isEmpty()) {
1126 // Clear the old selection that was available before the rubberband has
1127 // been activated in case if no Shift- or Control-key are pressed
1128 const bool shiftOrControlPressed
= QApplication::keyboardModifiers() & Qt::ShiftModifier
||
1129 QApplication::keyboardModifiers() & Qt::ControlModifier
;
1130 if (!shiftOrControlPressed
) {
1131 m_oldSelection
.clear();
1135 KItemSet selectedItems
;
1137 // Select all visible items that intersect with the rubberband
1138 foreach (const KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
1139 const int index
= widget
->index();
1141 const QRectF widgetRect
= m_view
->itemRect(index
);
1142 if (widgetRect
.intersects(rubberBandRect
)) {
1143 const QRectF iconRect
= widget
->iconRect().translated(widgetRect
.topLeft());
1144 const QRectF textRect
= widget
->textRect().translated(widgetRect
.topLeft());
1145 if (iconRect
.intersects(rubberBandRect
) || textRect
.intersects(rubberBandRect
)) {
1146 selectedItems
.insert(index
);
1151 // Select all invisible items that intersect with the rubberband. Instead of
1152 // iterating all items only the area which might be touched by the rubberband
1154 const bool increaseIndex
= scrollVertical
?
1155 startPos
.y() > endPos
.y(): startPos
.x() > endPos
.x();
1157 int index
= increaseIndex
? m_view
->lastVisibleIndex() + 1 : m_view
->firstVisibleIndex() - 1;
1158 bool selectionFinished
= false;
1160 const QRectF widgetRect
= m_view
->itemRect(index
);
1161 if (widgetRect
.intersects(rubberBandRect
)) {
1162 selectedItems
.insert(index
);
1165 if (increaseIndex
) {
1167 selectionFinished
= (index
>= m_model
->count()) ||
1168 ( scrollVertical
&& widgetRect
.top() > rubberBandRect
.bottom()) ||
1169 (!scrollVertical
&& widgetRect
.left() > rubberBandRect
.right());
1172 selectionFinished
= (index
< 0) ||
1173 ( scrollVertical
&& widgetRect
.bottom() < rubberBandRect
.top()) ||
1174 (!scrollVertical
&& widgetRect
.right() < rubberBandRect
.left());
1176 } while (!selectionFinished
);
1178 if (QApplication::keyboardModifiers() & Qt::ControlModifier
) {
1179 // If Control is pressed, the selection state of all items in the rubberband is toggled.
1180 // Therefore, the new selection contains:
1181 // 1. All previously selected items which are not inside the rubberband, and
1182 // 2. all items inside the rubberband which have not been selected previously.
1183 m_selectionManager
->setSelectedItems(m_oldSelection
^ selectedItems
);
1186 m_selectionManager
->setSelectedItems(selectedItems
+ m_oldSelection
);
1190 void KItemListController::startDragging()
1192 if (!m_view
|| !m_model
) {
1196 const KItemSet selectedItems
= m_selectionManager
->selectedItems();
1197 if (selectedItems
.isEmpty()) {
1201 QMimeData
* data
= m_model
->createMimeData(selectedItems
);
1206 // The created drag object will be owned and deleted
1207 // by QApplication::activeWindow().
1208 QDrag
* drag
= new QDrag(QApplication::activeWindow());
1209 drag
->setMimeData(data
);
1211 const QPixmap pixmap
= m_view
->createDragPixmap(selectedItems
);
1212 drag
->setPixmap(pixmap
);
1214 const QPoint
hotSpot((pixmap
.width() / pixmap
.devicePixelRatio()) / 2, 0);
1215 drag
->setHotSpot(hotSpot
);
1217 drag
->exec(Qt::MoveAction
| Qt::CopyAction
| Qt::LinkAction
, Qt::CopyAction
);
1219 QAccessibleEvent
accessibilityEvent(view(), QAccessible::DragDropStart
);
1220 QAccessible::updateAccessibility(&accessibilityEvent
);
1223 KItemListWidget
* KItemListController::hoveredWidget() const
1227 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
1228 if (widget
->isHovered()) {
1236 KItemListWidget
* KItemListController::widgetForPos(const QPointF
& pos
) const
1240 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
1241 const QPointF mappedPos
= widget
->mapFromItem(m_view
, pos
);
1243 const bool hovered
= widget
->contains(mappedPos
) &&
1244 !widget
->expansionToggleRect().contains(mappedPos
);
1253 void KItemListController::updateKeyboardAnchor()
1255 const bool validAnchor
= m_keyboardAnchorIndex
>= 0 &&
1256 m_keyboardAnchorIndex
< m_model
->count() &&
1257 keyboardAnchorPos(m_keyboardAnchorIndex
) == m_keyboardAnchorPos
;
1259 const int index
= m_selectionManager
->currentItem();
1260 m_keyboardAnchorIndex
= index
;
1261 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
1265 int KItemListController::nextRowIndex(int index
) const
1267 if (m_keyboardAnchorIndex
< 0) {
1271 const int maxIndex
= m_model
->count() - 1;
1272 if (index
== maxIndex
) {
1276 // Calculate the index of the last column inside the row of the current index
1277 int lastColumnIndex
= index
;
1278 while (keyboardAnchorPos(lastColumnIndex
+ 1) > keyboardAnchorPos(lastColumnIndex
)) {
1280 if (lastColumnIndex
>= maxIndex
) {
1285 // Based on the last column index go to the next row and calculate the nearest index
1286 // that is below the current index
1287 int nextRowIndex
= lastColumnIndex
+ 1;
1288 int searchIndex
= nextRowIndex
;
1289 qreal minDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(nextRowIndex
));
1290 while (searchIndex
< maxIndex
&& keyboardAnchorPos(searchIndex
+ 1) > keyboardAnchorPos(searchIndex
)) {
1292 const qreal searchDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(searchIndex
));
1293 if (searchDiff
< minDiff
) {
1294 minDiff
= searchDiff
;
1295 nextRowIndex
= searchIndex
;
1299 return nextRowIndex
;
1302 int KItemListController::previousRowIndex(int index
) const
1304 if (m_keyboardAnchorIndex
< 0 || index
== 0) {
1308 // Calculate the index of the first column inside the row of the current index
1309 int firstColumnIndex
= index
;
1310 while (keyboardAnchorPos(firstColumnIndex
- 1) < keyboardAnchorPos(firstColumnIndex
)) {
1312 if (firstColumnIndex
<= 0) {
1317 // Based on the first column index go to the previous row and calculate the nearest index
1318 // that is above the current index
1319 int previousRowIndex
= firstColumnIndex
- 1;
1320 int searchIndex
= previousRowIndex
;
1321 qreal minDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(previousRowIndex
));
1322 while (searchIndex
> 0 && keyboardAnchorPos(searchIndex
- 1) < keyboardAnchorPos(searchIndex
)) {
1324 const qreal searchDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(searchIndex
));
1325 if (searchDiff
< minDiff
) {
1326 minDiff
= searchDiff
;
1327 previousRowIndex
= searchIndex
;
1331 return previousRowIndex
;
1334 qreal
KItemListController::keyboardAnchorPos(int index
) const
1336 const QRectF itemRect
= m_view
->itemRect(index
);
1337 if (!itemRect
.isEmpty()) {
1338 return (m_view
->scrollOrientation() == Qt::Vertical
) ? itemRect
.x() : itemRect
.y();
1344 void KItemListController::updateExtendedSelectionRegion()
1347 const bool extend
= (m_selectionBehavior
!= MultiSelection
);
1348 KItemListStyleOption option
= m_view
->styleOption();
1349 if (option
.extendedSelectionRegion
!= extend
) {
1350 option
.extendedSelectionRegion
= extend
;
1351 m_view
->setStyleOption(option
);