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
)
853 DragAndDropHelper::clearUrlListMatchesUrlCache();
858 bool KItemListController::dragLeaveEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
863 m_autoActivationTimer
->stop();
864 m_view
->setAutoScroll(false);
865 m_view
->hideDropIndicator();
867 KItemListWidget
* widget
= hoveredWidget();
869 widget
->setHovered(false);
870 emit
itemUnhovered(widget
->index());
875 bool KItemListController::dragMoveEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
877 if (!m_model
|| !m_view
) {
882 QUrl hoveredDir
= m_model
->directory();
883 KItemListWidget
* oldHoveredWidget
= hoveredWidget();
885 const QPointF pos
= transform
.map(event
->pos());
886 KItemListWidget
* newHoveredWidget
= widgetForPos(pos
);
888 if (oldHoveredWidget
!= newHoveredWidget
) {
889 m_autoActivationTimer
->stop();
891 if (oldHoveredWidget
) {
892 oldHoveredWidget
->setHovered(false);
893 emit
itemUnhovered(oldHoveredWidget
->index());
897 if (newHoveredWidget
) {
898 bool droppingBetweenItems
= false;
899 if (m_model
->sortRole().isEmpty()) {
900 // The model supports inserting items between other items.
901 droppingBetweenItems
= (m_view
->showDropIndicator(pos
) >= 0);
904 const int index
= newHoveredWidget
->index();
906 if (m_model
->isDir(index
)) {
907 hoveredDir
= m_model
->url(index
);
910 if (!droppingBetweenItems
) {
911 if (m_model
->supportsDropping(index
)) {
912 // Something has been dragged on an item.
913 m_view
->hideDropIndicator();
914 if (!newHoveredWidget
->isHovered()) {
915 newHoveredWidget
->setHovered(true);
916 emit
itemHovered(index
);
919 if (!m_autoActivationTimer
->isActive() && m_autoActivationTimer
->interval() >= 0) {
920 m_autoActivationTimer
->setProperty("index", index
);
921 m_autoActivationTimer
->start();
925 m_autoActivationTimer
->stop();
926 if (newHoveredWidget
&& newHoveredWidget
->isHovered()) {
927 newHoveredWidget
->setHovered(false);
928 emit
itemUnhovered(index
);
932 m_view
->hideDropIndicator();
935 event
->setAccepted(!DragAndDropHelper::urlListMatchesUrl(event
->mimeData()->urls(), hoveredDir
));
940 bool KItemListController::dropEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
946 m_autoActivationTimer
->stop();
947 m_view
->setAutoScroll(false);
949 const QPointF pos
= transform
.map(event
->pos());
951 int dropAboveIndex
= -1;
952 if (m_model
->sortRole().isEmpty()) {
953 // The model supports inserting of items between other items.
954 dropAboveIndex
= m_view
->showDropIndicator(pos
);
957 if (dropAboveIndex
>= 0) {
958 // Something has been dropped between two items.
959 m_view
->hideDropIndicator();
960 emit
aboveItemDropEvent(dropAboveIndex
, event
);
961 } else if (!event
->mimeData()->hasFormat(m_model
->blacklistItemDropEventMimeType())) {
962 // Something has been dropped on an item or on an empty part of the view.
963 emit
itemDropEvent(m_view
->itemAt(pos
), event
);
966 QAccessibleEvent
accessibilityEvent(view(), QAccessible::DragDropEnd
);
967 QAccessible::updateAccessibility(&accessibilityEvent
);
972 bool KItemListController::hoverEnterEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
979 bool KItemListController::hoverMoveEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
982 if (!m_model
|| !m_view
) {
986 KItemListWidget
* oldHoveredWidget
= hoveredWidget();
987 const QPointF pos
= transform
.map(event
->pos());
988 KItemListWidget
* newHoveredWidget
= widgetForPos(pos
);
990 if (oldHoveredWidget
!= newHoveredWidget
) {
991 if (oldHoveredWidget
) {
992 oldHoveredWidget
->setHovered(false);
993 emit
itemUnhovered(oldHoveredWidget
->index());
996 if (newHoveredWidget
) {
997 newHoveredWidget
->setHovered(true);
998 const QPointF mappedPos
= newHoveredWidget
->mapFromItem(m_view
, pos
);
999 newHoveredWidget
->setHoverPosition(mappedPos
);
1000 emit
itemHovered(newHoveredWidget
->index());
1002 } else if (oldHoveredWidget
) {
1003 const QPointF mappedPos
= oldHoveredWidget
->mapFromItem(m_view
, pos
);
1004 oldHoveredWidget
->setHoverPosition(mappedPos
);
1010 bool KItemListController::hoverLeaveEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
1013 Q_UNUSED(transform
);
1015 if (!m_model
|| !m_view
) {
1019 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
1020 if (widget
->isHovered()) {
1021 widget
->setHovered(false);
1022 emit
itemUnhovered(widget
->index());
1028 bool KItemListController::wheelEvent(QGraphicsSceneWheelEvent
* event
, const QTransform
& transform
)
1031 Q_UNUSED(transform
);
1035 bool KItemListController::resizeEvent(QGraphicsSceneResizeEvent
* event
, const QTransform
& transform
)
1038 Q_UNUSED(transform
);
1042 bool KItemListController::processEvent(QEvent
* event
, const QTransform
& transform
)
1048 switch (event
->type()) {
1049 case QEvent::KeyPress
:
1050 return keyPressEvent(static_cast<QKeyEvent
*>(event
));
1051 case QEvent::InputMethod
:
1052 return inputMethodEvent(static_cast<QInputMethodEvent
*>(event
));
1053 case QEvent::GraphicsSceneMousePress
:
1054 return mousePressEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
1055 case QEvent::GraphicsSceneMouseMove
:
1056 return mouseMoveEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
1057 case QEvent::GraphicsSceneMouseRelease
:
1058 return mouseReleaseEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
1059 case QEvent::GraphicsSceneMouseDoubleClick
:
1060 return mouseDoubleClickEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
1061 case QEvent::GraphicsSceneWheel
:
1062 return wheelEvent(static_cast<QGraphicsSceneWheelEvent
*>(event
), QTransform());
1063 case QEvent::GraphicsSceneDragEnter
:
1064 return dragEnterEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
1065 case QEvent::GraphicsSceneDragLeave
:
1066 return dragLeaveEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
1067 case QEvent::GraphicsSceneDragMove
:
1068 return dragMoveEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
1069 case QEvent::GraphicsSceneDrop
:
1070 return dropEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
1071 case QEvent::GraphicsSceneHoverEnter
:
1072 return hoverEnterEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
1073 case QEvent::GraphicsSceneHoverMove
:
1074 return hoverMoveEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
1075 case QEvent::GraphicsSceneHoverLeave
:
1076 return hoverLeaveEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
1077 case QEvent::GraphicsSceneResize
:
1078 return resizeEvent(static_cast<QGraphicsSceneResizeEvent
*>(event
), transform
);
1086 void KItemListController::slotViewScrollOffsetChanged(qreal current
, qreal previous
)
1092 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
1093 if (rubberBand
->isActive()) {
1094 const qreal diff
= current
- previous
;
1095 // TODO: Ideally just QCursor::pos() should be used as
1096 // new end-position but it seems there is no easy way
1097 // to have something like QWidget::mapFromGlobal() for QGraphicsWidget
1098 // (... or I just missed an easy way to do the mapping)
1099 QPointF endPos
= rubberBand
->endPosition();
1100 if (m_view
->scrollOrientation() == Qt::Vertical
) {
1101 endPos
.ry() += diff
;
1103 endPos
.rx() += diff
;
1106 rubberBand
->setEndPosition(endPos
);
1110 void KItemListController::slotRubberBandChanged()
1112 if (!m_view
|| !m_model
|| m_model
->count() <= 0) {
1116 const KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
1117 const QPointF startPos
= rubberBand
->startPosition();
1118 const QPointF endPos
= rubberBand
->endPosition();
1119 QRectF rubberBandRect
= QRectF(startPos
, endPos
).normalized();
1121 const bool scrollVertical
= (m_view
->scrollOrientation() == Qt::Vertical
);
1122 if (scrollVertical
) {
1123 rubberBandRect
.translate(0, -m_view
->scrollOffset());
1125 rubberBandRect
.translate(-m_view
->scrollOffset(), 0);
1128 if (!m_oldSelection
.isEmpty()) {
1129 // Clear the old selection that was available before the rubberband has
1130 // been activated in case if no Shift- or Control-key are pressed
1131 const bool shiftOrControlPressed
= QApplication::keyboardModifiers() & Qt::ShiftModifier
||
1132 QApplication::keyboardModifiers() & Qt::ControlModifier
;
1133 if (!shiftOrControlPressed
) {
1134 m_oldSelection
.clear();
1138 KItemSet selectedItems
;
1140 // Select all visible items that intersect with the rubberband
1141 foreach (const KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
1142 const int index
= widget
->index();
1144 const QRectF widgetRect
= m_view
->itemRect(index
);
1145 if (widgetRect
.intersects(rubberBandRect
)) {
1146 const QRectF iconRect
= widget
->iconRect().translated(widgetRect
.topLeft());
1147 const QRectF textRect
= widget
->textRect().translated(widgetRect
.topLeft());
1148 if (iconRect
.intersects(rubberBandRect
) || textRect
.intersects(rubberBandRect
)) {
1149 selectedItems
.insert(index
);
1154 // Select all invisible items that intersect with the rubberband. Instead of
1155 // iterating all items only the area which might be touched by the rubberband
1157 const bool increaseIndex
= scrollVertical
?
1158 startPos
.y() > endPos
.y(): startPos
.x() > endPos
.x();
1160 int index
= increaseIndex
? m_view
->lastVisibleIndex() + 1 : m_view
->firstVisibleIndex() - 1;
1161 bool selectionFinished
= false;
1163 const QRectF widgetRect
= m_view
->itemRect(index
);
1164 if (widgetRect
.intersects(rubberBandRect
)) {
1165 selectedItems
.insert(index
);
1168 if (increaseIndex
) {
1170 selectionFinished
= (index
>= m_model
->count()) ||
1171 ( scrollVertical
&& widgetRect
.top() > rubberBandRect
.bottom()) ||
1172 (!scrollVertical
&& widgetRect
.left() > rubberBandRect
.right());
1175 selectionFinished
= (index
< 0) ||
1176 ( scrollVertical
&& widgetRect
.bottom() < rubberBandRect
.top()) ||
1177 (!scrollVertical
&& widgetRect
.right() < rubberBandRect
.left());
1179 } while (!selectionFinished
);
1181 if (QApplication::keyboardModifiers() & Qt::ControlModifier
) {
1182 // If Control is pressed, the selection state of all items in the rubberband is toggled.
1183 // Therefore, the new selection contains:
1184 // 1. All previously selected items which are not inside the rubberband, and
1185 // 2. all items inside the rubberband which have not been selected previously.
1186 m_selectionManager
->setSelectedItems(m_oldSelection
^ selectedItems
);
1189 m_selectionManager
->setSelectedItems(selectedItems
+ m_oldSelection
);
1193 void KItemListController::startDragging()
1195 if (!m_view
|| !m_model
) {
1199 const KItemSet selectedItems
= m_selectionManager
->selectedItems();
1200 if (selectedItems
.isEmpty()) {
1204 QMimeData
* data
= m_model
->createMimeData(selectedItems
);
1209 // The created drag object will be owned and deleted
1210 // by QApplication::activeWindow().
1211 QDrag
* drag
= new QDrag(QApplication::activeWindow());
1212 drag
->setMimeData(data
);
1214 const QPixmap pixmap
= m_view
->createDragPixmap(selectedItems
);
1215 drag
->setPixmap(pixmap
);
1217 const QPoint
hotSpot((pixmap
.width() / pixmap
.devicePixelRatio()) / 2, 0);
1218 drag
->setHotSpot(hotSpot
);
1220 drag
->exec(Qt::MoveAction
| Qt::CopyAction
| Qt::LinkAction
, Qt::CopyAction
);
1222 QAccessibleEvent
accessibilityEvent(view(), QAccessible::DragDropStart
);
1223 QAccessible::updateAccessibility(&accessibilityEvent
);
1226 KItemListWidget
* KItemListController::hoveredWidget() const
1230 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
1231 if (widget
->isHovered()) {
1239 KItemListWidget
* KItemListController::widgetForPos(const QPointF
& pos
) const
1243 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
1244 const QPointF mappedPos
= widget
->mapFromItem(m_view
, pos
);
1246 const bool hovered
= widget
->contains(mappedPos
) &&
1247 !widget
->expansionToggleRect().contains(mappedPos
);
1256 void KItemListController::updateKeyboardAnchor()
1258 const bool validAnchor
= m_keyboardAnchorIndex
>= 0 &&
1259 m_keyboardAnchorIndex
< m_model
->count() &&
1260 keyboardAnchorPos(m_keyboardAnchorIndex
) == m_keyboardAnchorPos
;
1262 const int index
= m_selectionManager
->currentItem();
1263 m_keyboardAnchorIndex
= index
;
1264 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
1268 int KItemListController::nextRowIndex(int index
) const
1270 if (m_keyboardAnchorIndex
< 0) {
1274 const int maxIndex
= m_model
->count() - 1;
1275 if (index
== maxIndex
) {
1279 // Calculate the index of the last column inside the row of the current index
1280 int lastColumnIndex
= index
;
1281 while (keyboardAnchorPos(lastColumnIndex
+ 1) > keyboardAnchorPos(lastColumnIndex
)) {
1283 if (lastColumnIndex
>= maxIndex
) {
1288 // Based on the last column index go to the next row and calculate the nearest index
1289 // that is below the current index
1290 int nextRowIndex
= lastColumnIndex
+ 1;
1291 int searchIndex
= nextRowIndex
;
1292 qreal minDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(nextRowIndex
));
1293 while (searchIndex
< maxIndex
&& keyboardAnchorPos(searchIndex
+ 1) > keyboardAnchorPos(searchIndex
)) {
1295 const qreal searchDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(searchIndex
));
1296 if (searchDiff
< minDiff
) {
1297 minDiff
= searchDiff
;
1298 nextRowIndex
= searchIndex
;
1302 return nextRowIndex
;
1305 int KItemListController::previousRowIndex(int index
) const
1307 if (m_keyboardAnchorIndex
< 0 || index
== 0) {
1311 // Calculate the index of the first column inside the row of the current index
1312 int firstColumnIndex
= index
;
1313 while (keyboardAnchorPos(firstColumnIndex
- 1) < keyboardAnchorPos(firstColumnIndex
)) {
1315 if (firstColumnIndex
<= 0) {
1320 // Based on the first column index go to the previous row and calculate the nearest index
1321 // that is above the current index
1322 int previousRowIndex
= firstColumnIndex
- 1;
1323 int searchIndex
= previousRowIndex
;
1324 qreal minDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(previousRowIndex
));
1325 while (searchIndex
> 0 && keyboardAnchorPos(searchIndex
- 1) < keyboardAnchorPos(searchIndex
)) {
1327 const qreal searchDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(searchIndex
));
1328 if (searchDiff
< minDiff
) {
1329 minDiff
= searchDiff
;
1330 previousRowIndex
= searchIndex
;
1334 return previousRowIndex
;
1337 qreal
KItemListController::keyboardAnchorPos(int index
) const
1339 const QRectF itemRect
= m_view
->itemRect(index
);
1340 if (!itemRect
.isEmpty()) {
1341 return (m_view
->scrollOrientation() == Qt::Vertical
) ? itemRect
.x() : itemRect
.y();
1347 void KItemListController::updateExtendedSelectionRegion()
1350 const bool extend
= (m_selectionBehavior
!= MultiSelection
);
1351 KItemListStyleOption option
= m_view
->styleOption();
1352 if (option
.extendedSelectionRegion
!= extend
) {
1353 option
.extendedSelectionRegion
= extend
;
1354 m_view
->setStyleOption(option
);