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>
43 KItemListController::KItemListController(KItemModelBase
* model
, KItemListView
* view
, QObject
* parent
) :
45 m_singleClickActivationEnforced(false),
46 m_selectionTogglePressed(false),
47 m_clearSelectionIfItemsAreNotDragged(false),
48 m_selectionBehavior(NoSelection
),
49 m_autoActivationBehavior(ActivationAndExpansion
),
50 m_mouseDoubleClickAction(ActivateItemOnly
),
53 m_selectionManager(new KItemListSelectionManager(this)),
54 m_keyboardManager(new KItemListKeyboardSearchManager(this)),
57 m_autoActivationTimer(0),
59 m_keyboardAnchorIndex(-1),
60 m_keyboardAnchorPos(0)
62 connect(m_keyboardManager
, &KItemListKeyboardSearchManager::changeCurrentItem
,
63 this, &KItemListController::slotChangeCurrentItem
);
64 connect(m_selectionManager
, &KItemListSelectionManager::currentChanged
,
65 m_keyboardManager
, &KItemListKeyboardSearchManager::slotCurrentChanged
);
67 m_autoActivationTimer
= new QTimer(this);
68 m_autoActivationTimer
->setSingleShot(true);
69 m_autoActivationTimer
->setInterval(-1);
70 connect(m_autoActivationTimer
, &QTimer::timeout
, this, &KItemListController::slotAutoActivationTimeout
);
76 KItemListController::~KItemListController()
85 void KItemListController::setModel(KItemModelBase
* model
)
87 if (m_model
== model
) {
91 KItemModelBase
* oldModel
= m_model
;
93 oldModel
->deleteLater();
98 m_model
->setParent(this);
102 m_view
->setModel(m_model
);
105 m_selectionManager
->setModel(m_model
);
107 emit
modelChanged(m_model
, oldModel
);
110 KItemModelBase
* KItemListController::model() const
115 KItemListSelectionManager
* KItemListController::selectionManager() const
117 return m_selectionManager
;
120 void KItemListController::setView(KItemListView
* view
)
122 if (m_view
== view
) {
126 KItemListView
* oldView
= m_view
;
128 disconnect(oldView
, &KItemListView::scrollOffsetChanged
, this, &KItemListController::slotViewScrollOffsetChanged
);
129 oldView
->deleteLater();
135 m_view
->setParent(this);
136 m_view
->setController(this);
137 m_view
->setModel(m_model
);
138 connect(m_view
, &KItemListView::scrollOffsetChanged
, this, &KItemListController::slotViewScrollOffsetChanged
);
139 updateExtendedSelectionRegion();
142 emit
viewChanged(m_view
, oldView
);
145 KItemListView
* KItemListController::view() const
150 void KItemListController::setSelectionBehavior(SelectionBehavior behavior
)
152 m_selectionBehavior
= behavior
;
153 updateExtendedSelectionRegion();
156 KItemListController::SelectionBehavior
KItemListController::selectionBehavior() const
158 return m_selectionBehavior
;
161 void KItemListController::setAutoActivationBehavior(AutoActivationBehavior behavior
)
163 m_autoActivationBehavior
= behavior
;
166 KItemListController::AutoActivationBehavior
KItemListController::autoActivationBehavior() const
168 return m_autoActivationBehavior
;
171 void KItemListController::setMouseDoubleClickAction(MouseDoubleClickAction action
)
173 m_mouseDoubleClickAction
= action
;
176 KItemListController::MouseDoubleClickAction
KItemListController::mouseDoubleClickAction() const
178 return m_mouseDoubleClickAction
;
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::showEvent(QShowEvent
* event
)
207 bool KItemListController::hideEvent(QHideEvent
* event
)
213 bool KItemListController::keyPressEvent(QKeyEvent
* event
)
215 int index
= m_selectionManager
->currentItem();
216 int key
= event
->key();
218 // Handle the expanding/collapsing of items
219 if (m_view
->supportsItemExpanding() && m_model
->isExpandable(index
)) {
220 if (key
== Qt::Key_Right
) {
221 if (m_model
->setExpanded(index
, true)) {
224 } else if (key
== Qt::Key_Left
) {
225 if (m_model
->setExpanded(index
, false)) {
231 const bool shiftPressed
= event
->modifiers() & Qt::ShiftModifier
;
232 const bool controlPressed
= event
->modifiers() & Qt::ControlModifier
;
233 const bool shiftOrControlPressed
= shiftPressed
|| controlPressed
;
235 const int itemCount
= m_model
->count();
237 // For horizontal scroll orientation, transform
238 // the arrow keys to simplify the event handling.
239 if (m_view
->scrollOrientation() == Qt::Horizontal
) {
241 case Qt::Key_Up
: key
= Qt::Key_Left
; break;
242 case Qt::Key_Down
: key
= Qt::Key_Right
; break;
243 case Qt::Key_Left
: key
= Qt::Key_Up
; break;
244 case Qt::Key_Right
: key
= Qt::Key_Down
; break;
249 const bool selectSingleItem
= m_selectionBehavior
!= NoSelection
&&
251 (key
== Qt::Key_Home
|| key
== Qt::Key_End
||
252 key
== Qt::Key_Up
|| key
== Qt::Key_Down
||
253 key
== Qt::Key_Left
|| key
== Qt::Key_Right
);
254 if (selectSingleItem
) {
255 const int current
= m_selectionManager
->currentItem();
256 m_selectionManager
->setSelected(current
);
263 m_keyboardAnchorIndex
= index
;
264 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
268 index
= itemCount
- 1;
269 m_keyboardAnchorIndex
= index
;
270 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
275 const int expandedParentsCount
= m_model
->expandedParentsCount(index
);
276 if (expandedParentsCount
== 0) {
279 // Go to the parent of the current item.
282 } while (index
> 0 && m_model
->expandedParentsCount(index
) == expandedParentsCount
);
284 m_keyboardAnchorIndex
= index
;
285 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
290 if (index
< itemCount
- 1) {
292 m_keyboardAnchorIndex
= index
;
293 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
298 updateKeyboardAnchor();
299 index
= previousRowIndex(index
);
303 updateKeyboardAnchor();
304 index
= nextRowIndex(index
);
308 if (m_view
->scrollOrientation() == Qt::Horizontal
) {
309 // The new current index should correspond to the first item in the current column.
310 int newIndex
= qMax(index
- 1, 0);
311 while (newIndex
!= index
&& m_view
->itemRect(newIndex
).topLeft().y() < m_view
->itemRect(index
).topLeft().y()) {
313 newIndex
= qMax(index
- 1, 0);
315 m_keyboardAnchorIndex
= index
;
316 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
318 const qreal currentItemBottom
= m_view
->itemRect(index
).bottomLeft().y();
319 const qreal height
= m_view
->geometry().height();
321 // The new current item should be the first item in the current
322 // column whose itemRect's top coordinate is larger than targetY.
323 const qreal targetY
= currentItemBottom
- height
;
325 updateKeyboardAnchor();
326 int newIndex
= previousRowIndex(index
);
329 updateKeyboardAnchor();
330 newIndex
= previousRowIndex(index
);
331 } while (m_view
->itemRect(newIndex
).topLeft().y() > targetY
&& newIndex
!= index
);
335 case Qt::Key_PageDown
:
336 if (m_view
->scrollOrientation() == Qt::Horizontal
) {
337 // The new current index should correspond to the last item in the current column.
338 int newIndex
= qMin(index
+ 1, m_model
->count() - 1);
339 while (newIndex
!= index
&& m_view
->itemRect(newIndex
).topLeft().y() > m_view
->itemRect(index
).topLeft().y()) {
341 newIndex
= qMin(index
+ 1, m_model
->count() - 1);
343 m_keyboardAnchorIndex
= index
;
344 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
346 const qreal currentItemTop
= m_view
->itemRect(index
).topLeft().y();
347 const qreal height
= m_view
->geometry().height();
349 // The new current item should be the last item in the current
350 // column whose itemRect's bottom coordinate is smaller than targetY.
351 const qreal targetY
= currentItemTop
+ height
;
353 updateKeyboardAnchor();
354 int newIndex
= nextRowIndex(index
);
357 updateKeyboardAnchor();
358 newIndex
= nextRowIndex(index
);
359 } while (m_view
->itemRect(newIndex
).bottomLeft().y() < targetY
&& newIndex
!= index
);
364 case Qt::Key_Return
: {
365 const KItemSet selectedItems
= m_selectionManager
->selectedItems();
366 if (selectedItems
.count() >= 2) {
367 emit
itemsActivated(selectedItems
);
368 } else if (selectedItems
.count() == 1) {
369 emit
itemActivated(selectedItems
.first());
371 emit
itemActivated(index
);
377 // Emit the signal itemContextMenuRequested() in case if at least one
378 // item is selected. Otherwise the signal viewContextMenuRequested() will be emitted.
379 const KItemSet selectedItems
= m_selectionManager
->selectedItems();
381 if (selectedItems
.count() >= 2) {
382 const int currentItemIndex
= m_selectionManager
->currentItem();
383 index
= selectedItems
.contains(currentItemIndex
)
384 ? currentItemIndex
: selectedItems
.first();
385 } else if (selectedItems
.count() == 1) {
386 index
= selectedItems
.first();
390 const QRectF contextRect
= m_view
->itemContextRect(index
);
391 const QPointF
pos(m_view
->scene()->views().first()->mapToGlobal(contextRect
.bottomRight().toPoint()));
392 emit
itemContextMenuRequested(index
, pos
);
394 emit
viewContextMenuRequested(QCursor::pos());
400 if (m_selectionBehavior
!= SingleSelection
) {
401 m_selectionManager
->clearSelection();
403 m_keyboardManager
->cancelSearch();
404 emit
escapePressed();
408 if (m_selectionBehavior
== MultiSelection
) {
409 if (controlPressed
) {
410 // Toggle the selection state of the current item.
411 m_selectionManager
->endAnchoredSelection();
412 m_selectionManager
->setSelected(index
, 1, KItemListSelectionManager::Toggle
);
413 m_selectionManager
->beginAnchoredSelection(index
);
416 // Select the current item if it is not selected yet.
417 const int current
= m_selectionManager
->currentItem();
418 if (!m_selectionManager
->isSelected(current
)) {
419 m_selectionManager
->setSelected(current
);
424 // Fall through to the default case and add the Space to the current search string.
427 m_keyboardManager
->addKeys(event
->text());
428 // Make sure unconsumed events get propagated up the chain. #302329
433 if (m_selectionManager
->currentItem() != index
) {
434 switch (m_selectionBehavior
) {
436 m_selectionManager
->setCurrentItem(index
);
439 case SingleSelection
:
440 m_selectionManager
->setCurrentItem(index
);
441 m_selectionManager
->clearSelection();
442 m_selectionManager
->setSelected(index
, 1);
446 if (controlPressed
) {
447 m_selectionManager
->endAnchoredSelection();
450 m_selectionManager
->setCurrentItem(index
);
452 if (!shiftOrControlPressed
) {
453 m_selectionManager
->clearSelection();
454 m_selectionManager
->setSelected(index
, 1);
458 m_selectionManager
->beginAnchoredSelection(index
);
463 m_view
->scrollToItem(index
);
468 void KItemListController::slotChangeCurrentItem(const QString
& text
, bool searchFromNextItem
)
470 if (!m_model
|| m_model
->count() == 0) {
473 const int currentIndex
= m_selectionManager
->currentItem();
475 if (searchFromNextItem
) {
476 index
= m_model
->indexForKeyboardSearch(text
, (currentIndex
+ 1) % m_model
->count());
478 index
= m_model
->indexForKeyboardSearch(text
, currentIndex
);
481 m_selectionManager
->setCurrentItem(index
);
483 if (m_selectionBehavior
!= NoSelection
) {
484 m_selectionManager
->clearSelection();
485 m_selectionManager
->setSelected(index
, 1);
486 m_selectionManager
->beginAnchoredSelection(index
);
489 m_view
->scrollToItem(index
);
493 void KItemListController::slotAutoActivationTimeout()
495 if (!m_model
|| !m_view
) {
499 const int index
= m_autoActivationTimer
->property("index").toInt();
500 if (index
< 0 || index
>= m_model
->count()) {
504 /* m_view->isUnderMouse() fixes a bug in the Folder-View-Panel and in the
507 * Bug: When you drag a file onto a Folder-View-Item or a Places-Item and
508 * then move away before the auto-activation timeout triggers, than the
509 * item still becomes activated/expanded.
511 * See Bug 293200 and 305783
513 if (m_model
->supportsDropping(index
) && m_view
->isUnderMouse()) {
514 if (m_view
->supportsItemExpanding() && m_model
->isExpandable(index
)) {
515 const bool expanded
= m_model
->isExpanded(index
);
516 m_model
->setExpanded(index
, !expanded
);
517 } else if (m_autoActivationBehavior
!= ExpansionOnly
) {
518 emit
itemActivated(index
);
523 bool KItemListController::inputMethodEvent(QInputMethodEvent
* event
)
529 bool KItemListController::mousePressEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
535 m_pressedMousePos
= transform
.map(event
->pos());
536 m_pressedIndex
= m_view
->itemAt(m_pressedMousePos
);
537 emit
mouseButtonPressed(m_pressedIndex
, event
->buttons());
539 if (event
->buttons() & (Qt::BackButton
| Qt::ForwardButton
)) {
540 // Do not select items when clicking the back/forward buttons, see
541 // https://bugs.kde.org/show_bug.cgi?id=327412.
545 if (m_view
->isAboveExpansionToggle(m_pressedIndex
, m_pressedMousePos
)) {
546 m_selectionManager
->endAnchoredSelection();
547 m_selectionManager
->setCurrentItem(m_pressedIndex
);
548 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
552 m_selectionTogglePressed
= m_view
->isAboveSelectionToggle(m_pressedIndex
, m_pressedMousePos
);
553 if (m_selectionTogglePressed
) {
554 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
555 // The previous anchored selection has been finished already in
556 // KItemListSelectionManager::setSelected(). We can safely change
557 // the current item and start a new anchored selection now.
558 m_selectionManager
->setCurrentItem(m_pressedIndex
);
559 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
563 const bool shiftPressed
= event
->modifiers() & Qt::ShiftModifier
;
564 const bool controlPressed
= event
->modifiers() & Qt::ControlModifier
;
566 // The previous selection is cleared if either
567 // 1. The selection mode is SingleSelection, or
568 // 2. the selection mode is MultiSelection, and *none* of the following conditions are met:
569 // a) Shift or Control are pressed.
570 // b) The clicked item is selected already. In that case, the user might want to:
571 // - start dragging multiple items, or
572 // - open the context menu and perform an action for all selected items.
573 const bool shiftOrControlPressed
= shiftPressed
|| controlPressed
;
574 const bool pressedItemAlreadySelected
= m_pressedIndex
>= 0 && m_selectionManager
->isSelected(m_pressedIndex
);
575 const bool clearSelection
= m_selectionBehavior
== SingleSelection
||
576 (!shiftOrControlPressed
&& !pressedItemAlreadySelected
);
577 if (clearSelection
) {
578 m_selectionManager
->clearSelection();
579 } else if (pressedItemAlreadySelected
&& !shiftOrControlPressed
&& (event
->buttons() & Qt::LeftButton
)) {
580 // The user might want to start dragging multiple items, but if he clicks the item
581 // in order to trigger it instead, the other selected items must be deselected.
582 // However, we do not know yet what the user is going to do.
583 // -> remember that the user pressed an item which had been selected already and
584 // clear the selection in mouseReleaseEvent(), unless the items are dragged.
585 m_clearSelectionIfItemsAreNotDragged
= true;
589 // Finish the anchored selection before the current index is changed
590 m_selectionManager
->endAnchoredSelection();
593 if (m_pressedIndex
>= 0) {
594 m_selectionManager
->setCurrentItem(m_pressedIndex
);
596 switch (m_selectionBehavior
) {
600 case SingleSelection
:
601 m_selectionManager
->setSelected(m_pressedIndex
);
605 if (controlPressed
&& !shiftPressed
) {
606 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
607 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
608 } else if (!shiftPressed
|| !m_selectionManager
->isAnchoredSelectionActive()) {
609 // Select the pressed item and start a new anchored selection
610 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Select
);
611 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
620 if (event
->buttons() & Qt::RightButton
) {
621 emit
itemContextMenuRequested(m_pressedIndex
, event
->screenPos());
627 if (event
->buttons() & Qt::RightButton
) {
628 const QRectF headerBounds
= m_view
->headerBoundaries();
629 if (headerBounds
.contains(event
->pos())) {
630 emit
headerContextMenuRequested(event
->screenPos());
632 emit
viewContextMenuRequested(event
->screenPos());
637 if (m_selectionBehavior
== MultiSelection
) {
638 QPointF startPos
= m_pressedMousePos
;
639 if (m_view
->scrollOrientation() == Qt::Vertical
) {
640 startPos
.ry() += m_view
->scrollOffset();
641 if (m_view
->itemSize().width() < 0) {
642 // Use a special rubberband for views that have only one column and
643 // expand the rubberband to use the whole width of the view.
647 startPos
.rx() += m_view
->scrollOffset();
650 m_oldSelection
= m_selectionManager
->selectedItems();
651 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
652 rubberBand
->setStartPosition(startPos
);
653 rubberBand
->setEndPosition(startPos
);
654 rubberBand
->setActive(true);
655 connect(rubberBand
, &KItemListRubberBand::endPositionChanged
, this, &KItemListController::slotRubberBandChanged
);
656 m_view
->setAutoScroll(true);
662 bool KItemListController::mouseMoveEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
668 if (m_pressedIndex
>= 0) {
669 // Check whether a dragging should be started
670 if (event
->buttons() & Qt::LeftButton
) {
671 const QPointF pos
= transform
.map(event
->pos());
672 if ((pos
- m_pressedMousePos
).manhattanLength() >= QApplication::startDragDistance()) {
673 if (!m_selectionManager
->isSelected(m_pressedIndex
)) {
674 // Always assure that the dragged item gets selected. Usually this is already
675 // done on the mouse-press event, but when using the selection-toggle on a
676 // selected item the dragged item is not selected yet.
677 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
679 // A selected item has been clicked to drag all selected items
680 // -> the selection should not be cleared when the mouse button is released.
681 m_clearSelectionIfItemsAreNotDragged
= false;
688 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
689 if (rubberBand
->isActive()) {
690 QPointF endPos
= transform
.map(event
->pos());
692 // Update the current item.
693 const int newCurrent
= m_view
->itemAt(endPos
);
694 if (newCurrent
>= 0) {
695 // It's expected that the new current index is also the new anchor (bug 163451).
696 m_selectionManager
->endAnchoredSelection();
697 m_selectionManager
->setCurrentItem(newCurrent
);
698 m_selectionManager
->beginAnchoredSelection(newCurrent
);
701 if (m_view
->scrollOrientation() == Qt::Vertical
) {
702 endPos
.ry() += m_view
->scrollOffset();
703 if (m_view
->itemSize().width() < 0) {
704 // Use a special rubberband for views that have only one column and
705 // expand the rubberband to use the whole width of the view.
706 endPos
.setX(m_view
->size().width());
709 endPos
.rx() += m_view
->scrollOffset();
711 rubberBand
->setEndPosition(endPos
);
718 bool KItemListController::mouseReleaseEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
724 emit
mouseButtonReleased(m_pressedIndex
, event
->buttons());
726 const bool isAboveSelectionToggle
= m_view
->isAboveSelectionToggle(m_pressedIndex
, m_pressedMousePos
);
727 if (isAboveSelectionToggle
) {
728 m_selectionTogglePressed
= false;
732 if (!isAboveSelectionToggle
&& m_selectionTogglePressed
) {
733 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
734 m_selectionTogglePressed
= false;
738 const bool shiftOrControlPressed
= event
->modifiers() & Qt::ShiftModifier
||
739 event
->modifiers() & Qt::ControlModifier
;
741 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
742 if (rubberBand
->isActive()) {
743 disconnect(rubberBand
, &KItemListRubberBand::endPositionChanged
, this, &KItemListController::slotRubberBandChanged
);
744 rubberBand
->setActive(false);
745 m_oldSelection
.clear();
746 m_view
->setAutoScroll(false);
749 const QPointF pos
= transform
.map(event
->pos());
750 const int index
= m_view
->itemAt(pos
);
752 if (index
>= 0 && index
== m_pressedIndex
) {
753 // The release event is done above the same item as the press event
755 if (m_clearSelectionIfItemsAreNotDragged
) {
756 // A selected item has been clicked, but no drag operation has been started
757 // -> clear the rest of the selection.
758 m_selectionManager
->clearSelection();
759 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Select
);
760 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
763 if (event
->button() & Qt::LeftButton
) {
764 bool emitItemActivated
= true;
765 if (m_view
->isAboveExpansionToggle(index
, pos
)) {
766 const bool expanded
= m_model
->isExpanded(index
);
767 m_model
->setExpanded(index
, !expanded
);
769 emit
itemExpansionToggleClicked(index
);
770 emitItemActivated
= false;
771 } else if (shiftOrControlPressed
) {
772 // The mouse click should only update the selection, not trigger the item
773 emitItemActivated
= false;
774 } else if (!(m_view
->style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick
) || m_singleClickActivationEnforced
)) {
775 emitItemActivated
= false;
777 if (emitItemActivated
) {
778 emit
itemActivated(index
);
780 } else if (event
->button() & Qt::MidButton
) {
781 emit
itemMiddleClicked(index
);
785 m_pressedMousePos
= QPointF();
787 m_clearSelectionIfItemsAreNotDragged
= false;
791 bool KItemListController::mouseDoubleClickEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
793 const QPointF pos
= transform
.map(event
->pos());
794 const int index
= m_view
->itemAt(pos
);
796 // Expand item if desired - See Bug 295573
797 if (m_mouseDoubleClickAction
!= ActivateItemOnly
) {
798 if (m_view
&& m_model
&& m_view
->supportsItemExpanding() && m_model
->isExpandable(index
)) {
799 const bool expanded
= m_model
->isExpanded(index
);
800 m_model
->setExpanded(index
, !expanded
);
804 bool emitItemActivated
= !(m_view
->style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick
) || m_singleClickActivationEnforced
) &&
805 (event
->button() & Qt::LeftButton
) &&
806 index
>= 0 && index
< m_model
->count();
807 if (emitItemActivated
) {
808 emit
itemActivated(index
);
813 bool KItemListController::dragEnterEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
820 bool KItemListController::dragLeaveEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
825 m_view
->setAutoScroll(false);
826 m_view
->hideDropIndicator();
828 KItemListWidget
* widget
= hoveredWidget();
830 widget
->setHovered(false);
831 emit
itemUnhovered(widget
->index());
836 bool KItemListController::dragMoveEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
838 if (!m_model
|| !m_view
) {
842 event
->acceptProposedAction();
844 KItemListWidget
* oldHoveredWidget
= hoveredWidget();
846 const QPointF pos
= transform
.map(event
->pos());
847 KItemListWidget
* newHoveredWidget
= widgetForPos(pos
);
849 if (oldHoveredWidget
!= newHoveredWidget
) {
850 m_autoActivationTimer
->stop();
852 if (oldHoveredWidget
) {
853 oldHoveredWidget
->setHovered(false);
854 emit
itemUnhovered(oldHoveredWidget
->index());
858 if (newHoveredWidget
) {
859 bool droppingBetweenItems
= false;
860 if (m_model
->sortRole().isEmpty()) {
861 // The model supports inserting items between other items.
862 droppingBetweenItems
= (m_view
->showDropIndicator(pos
) >= 0);
865 const int index
= newHoveredWidget
->index();
866 if (!droppingBetweenItems
) {
867 if (m_model
->supportsDropping(index
)) {
868 // Something has been dragged on an item.
869 m_view
->hideDropIndicator();
870 if (!newHoveredWidget
->isHovered()) {
871 newHoveredWidget
->setHovered(true);
872 emit
itemHovered(index
);
875 if (!m_autoActivationTimer
->isActive() && m_autoActivationTimer
->interval() >= 0) {
876 m_autoActivationTimer
->setProperty("index", index
);
877 m_autoActivationTimer
->start();
881 m_autoActivationTimer
->stop();
882 if (newHoveredWidget
&& newHoveredWidget
->isHovered()) {
883 newHoveredWidget
->setHovered(false);
884 emit
itemUnhovered(index
);
888 m_view
->hideDropIndicator();
894 bool KItemListController::dropEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
900 m_autoActivationTimer
->stop();
901 m_view
->setAutoScroll(false);
903 const QPointF pos
= transform
.map(event
->pos());
905 int dropAboveIndex
= -1;
906 if (m_model
->sortRole().isEmpty()) {
907 // The model supports inserting of items between other items.
908 dropAboveIndex
= m_view
->showDropIndicator(pos
);
911 if (dropAboveIndex
>= 0) {
912 // Something has been dropped between two items.
913 m_view
->hideDropIndicator();
914 emit
aboveItemDropEvent(dropAboveIndex
, event
);
916 // Something has been dropped on an item or on an empty part of the view.
917 emit
itemDropEvent(m_view
->itemAt(pos
), event
);
920 QAccessibleEvent
accessibilityEvent(view(), QAccessible::DragDropEnd
);
921 QAccessible::updateAccessibility(&accessibilityEvent
);
926 bool KItemListController::hoverEnterEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
933 bool KItemListController::hoverMoveEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
936 if (!m_model
|| !m_view
) {
940 KItemListWidget
* oldHoveredWidget
= hoveredWidget();
941 const QPointF pos
= transform
.map(event
->pos());
942 KItemListWidget
* newHoveredWidget
= widgetForPos(pos
);
944 if (oldHoveredWidget
!= newHoveredWidget
) {
945 if (oldHoveredWidget
) {
946 oldHoveredWidget
->setHovered(false);
947 emit
itemUnhovered(oldHoveredWidget
->index());
950 if (newHoveredWidget
) {
951 newHoveredWidget
->setHovered(true);
952 const QPointF mappedPos
= newHoveredWidget
->mapFromItem(m_view
, pos
);
953 newHoveredWidget
->setHoverPosition(mappedPos
);
954 emit
itemHovered(newHoveredWidget
->index());
956 } else if (oldHoveredWidget
) {
957 const QPointF mappedPos
= oldHoveredWidget
->mapFromItem(m_view
, pos
);
958 oldHoveredWidget
->setHoverPosition(mappedPos
);
964 bool KItemListController::hoverLeaveEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
969 if (!m_model
|| !m_view
) {
973 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
974 if (widget
->isHovered()) {
975 widget
->setHovered(false);
976 emit
itemUnhovered(widget
->index());
982 bool KItemListController::wheelEvent(QGraphicsSceneWheelEvent
* event
, const QTransform
& transform
)
989 bool KItemListController::resizeEvent(QGraphicsSceneResizeEvent
* event
, const QTransform
& transform
)
996 bool KItemListController::processEvent(QEvent
* event
, const QTransform
& transform
)
1002 switch (event
->type()) {
1003 case QEvent::KeyPress
:
1004 return keyPressEvent(static_cast<QKeyEvent
*>(event
));
1005 case QEvent::InputMethod
:
1006 return inputMethodEvent(static_cast<QInputMethodEvent
*>(event
));
1007 case QEvent::GraphicsSceneMousePress
:
1008 return mousePressEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
1009 case QEvent::GraphicsSceneMouseMove
:
1010 return mouseMoveEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
1011 case QEvent::GraphicsSceneMouseRelease
:
1012 return mouseReleaseEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
1013 case QEvent::GraphicsSceneMouseDoubleClick
:
1014 return mouseDoubleClickEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
1015 case QEvent::GraphicsSceneWheel
:
1016 return wheelEvent(static_cast<QGraphicsSceneWheelEvent
*>(event
), QTransform());
1017 case QEvent::GraphicsSceneDragEnter
:
1018 return dragEnterEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
1019 case QEvent::GraphicsSceneDragLeave
:
1020 return dragLeaveEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
1021 case QEvent::GraphicsSceneDragMove
:
1022 return dragMoveEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
1023 case QEvent::GraphicsSceneDrop
:
1024 return dropEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
1025 case QEvent::GraphicsSceneHoverEnter
:
1026 return hoverEnterEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
1027 case QEvent::GraphicsSceneHoverMove
:
1028 return hoverMoveEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
1029 case QEvent::GraphicsSceneHoverLeave
:
1030 return hoverLeaveEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
1031 case QEvent::GraphicsSceneResize
:
1032 return resizeEvent(static_cast<QGraphicsSceneResizeEvent
*>(event
), transform
);
1040 void KItemListController::slotViewScrollOffsetChanged(qreal current
, qreal previous
)
1046 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
1047 if (rubberBand
->isActive()) {
1048 const qreal diff
= current
- previous
;
1049 // TODO: Ideally just QCursor::pos() should be used as
1050 // new end-position but it seems there is no easy way
1051 // to have something like QWidget::mapFromGlobal() for QGraphicsWidget
1052 // (... or I just missed an easy way to do the mapping)
1053 QPointF endPos
= rubberBand
->endPosition();
1054 if (m_view
->scrollOrientation() == Qt::Vertical
) {
1055 endPos
.ry() += diff
;
1057 endPos
.rx() += diff
;
1060 rubberBand
->setEndPosition(endPos
);
1064 void KItemListController::slotRubberBandChanged()
1066 if (!m_view
|| !m_model
|| m_model
->count() <= 0) {
1070 const KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
1071 const QPointF startPos
= rubberBand
->startPosition();
1072 const QPointF endPos
= rubberBand
->endPosition();
1073 QRectF rubberBandRect
= QRectF(startPos
, endPos
).normalized();
1075 const bool scrollVertical
= (m_view
->scrollOrientation() == Qt::Vertical
);
1076 if (scrollVertical
) {
1077 rubberBandRect
.translate(0, -m_view
->scrollOffset());
1079 rubberBandRect
.translate(-m_view
->scrollOffset(), 0);
1082 if (!m_oldSelection
.isEmpty()) {
1083 // Clear the old selection that was available before the rubberband has
1084 // been activated in case if no Shift- or Control-key are pressed
1085 const bool shiftOrControlPressed
= QApplication::keyboardModifiers() & Qt::ShiftModifier
||
1086 QApplication::keyboardModifiers() & Qt::ControlModifier
;
1087 if (!shiftOrControlPressed
) {
1088 m_oldSelection
.clear();
1092 KItemSet selectedItems
;
1094 // Select all visible items that intersect with the rubberband
1095 foreach (const KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
1096 const int index
= widget
->index();
1098 const QRectF widgetRect
= m_view
->itemRect(index
);
1099 if (widgetRect
.intersects(rubberBandRect
)) {
1100 const QRectF iconRect
= widget
->iconRect().translated(widgetRect
.topLeft());
1101 const QRectF textRect
= widget
->textRect().translated(widgetRect
.topLeft());
1102 if (iconRect
.intersects(rubberBandRect
) || textRect
.intersects(rubberBandRect
)) {
1103 selectedItems
.insert(index
);
1108 // Select all invisible items that intersect with the rubberband. Instead of
1109 // iterating all items only the area which might be touched by the rubberband
1111 const bool increaseIndex
= scrollVertical
?
1112 startPos
.y() > endPos
.y(): startPos
.x() > endPos
.x();
1114 int index
= increaseIndex
? m_view
->lastVisibleIndex() + 1 : m_view
->firstVisibleIndex() - 1;
1115 bool selectionFinished
= false;
1117 const QRectF widgetRect
= m_view
->itemRect(index
);
1118 if (widgetRect
.intersects(rubberBandRect
)) {
1119 selectedItems
.insert(index
);
1122 if (increaseIndex
) {
1124 selectionFinished
= (index
>= m_model
->count()) ||
1125 ( scrollVertical
&& widgetRect
.top() > rubberBandRect
.bottom()) ||
1126 (!scrollVertical
&& widgetRect
.left() > rubberBandRect
.right());
1129 selectionFinished
= (index
< 0) ||
1130 ( scrollVertical
&& widgetRect
.bottom() < rubberBandRect
.top()) ||
1131 (!scrollVertical
&& widgetRect
.right() < rubberBandRect
.left());
1133 } while (!selectionFinished
);
1135 if (QApplication::keyboardModifiers() & Qt::ControlModifier
) {
1136 // If Control is pressed, the selection state of all items in the rubberband is toggled.
1137 // Therefore, the new selection contains:
1138 // 1. All previously selected items which are not inside the rubberband, and
1139 // 2. all items inside the rubberband which have not been selected previously.
1140 m_selectionManager
->setSelectedItems(m_oldSelection
^ selectedItems
);
1143 m_selectionManager
->setSelectedItems(selectedItems
+ m_oldSelection
);
1147 void KItemListController::startDragging()
1149 if (!m_view
|| !m_model
) {
1153 const KItemSet selectedItems
= m_selectionManager
->selectedItems();
1154 if (selectedItems
.isEmpty()) {
1158 QMimeData
* data
= m_model
->createMimeData(selectedItems
);
1163 // The created drag object will be owned and deleted
1164 // by QApplication::activeWindow().
1165 QDrag
* drag
= new QDrag(QApplication::activeWindow());
1166 drag
->setMimeData(data
);
1168 const QPixmap pixmap
= m_view
->createDragPixmap(selectedItems
);
1169 drag
->setPixmap(pixmap
);
1171 const QPoint
hotSpot((pixmap
.width() / pixmap
.devicePixelRatio()) / 2, 0);
1172 drag
->setHotSpot(hotSpot
);
1174 drag
->exec(Qt::MoveAction
| Qt::CopyAction
| Qt::LinkAction
, Qt::CopyAction
);
1176 QAccessibleEvent
accessibilityEvent(view(), QAccessible::DragDropStart
);
1177 QAccessible::updateAccessibility(&accessibilityEvent
);
1180 KItemListWidget
* KItemListController::hoveredWidget() const
1184 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
1185 if (widget
->isHovered()) {
1193 KItemListWidget
* KItemListController::widgetForPos(const QPointF
& pos
) const
1197 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
1198 const QPointF mappedPos
= widget
->mapFromItem(m_view
, pos
);
1200 const bool hovered
= widget
->contains(mappedPos
) &&
1201 !widget
->expansionToggleRect().contains(mappedPos
);
1210 void KItemListController::updateKeyboardAnchor()
1212 const bool validAnchor
= m_keyboardAnchorIndex
>= 0 &&
1213 m_keyboardAnchorIndex
< m_model
->count() &&
1214 keyboardAnchorPos(m_keyboardAnchorIndex
) == m_keyboardAnchorPos
;
1216 const int index
= m_selectionManager
->currentItem();
1217 m_keyboardAnchorIndex
= index
;
1218 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
1222 int KItemListController::nextRowIndex(int index
) const
1224 if (m_keyboardAnchorIndex
< 0) {
1228 const int maxIndex
= m_model
->count() - 1;
1229 if (index
== maxIndex
) {
1233 // Calculate the index of the last column inside the row of the current index
1234 int lastColumnIndex
= index
;
1235 while (keyboardAnchorPos(lastColumnIndex
+ 1) > keyboardAnchorPos(lastColumnIndex
)) {
1237 if (lastColumnIndex
>= maxIndex
) {
1242 // Based on the last column index go to the next row and calculate the nearest index
1243 // that is below the current index
1244 int nextRowIndex
= lastColumnIndex
+ 1;
1245 int searchIndex
= nextRowIndex
;
1246 qreal minDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(nextRowIndex
));
1247 while (searchIndex
< maxIndex
&& keyboardAnchorPos(searchIndex
+ 1) > keyboardAnchorPos(searchIndex
)) {
1249 const qreal searchDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(searchIndex
));
1250 if (searchDiff
< minDiff
) {
1251 minDiff
= searchDiff
;
1252 nextRowIndex
= searchIndex
;
1256 return nextRowIndex
;
1259 int KItemListController::previousRowIndex(int index
) const
1261 if (m_keyboardAnchorIndex
< 0 || index
== 0) {
1265 // Calculate the index of the first column inside the row of the current index
1266 int firstColumnIndex
= index
;
1267 while (keyboardAnchorPos(firstColumnIndex
- 1) < keyboardAnchorPos(firstColumnIndex
)) {
1269 if (firstColumnIndex
<= 0) {
1274 // Based on the first column index go to the previous row and calculate the nearest index
1275 // that is above the current index
1276 int previousRowIndex
= firstColumnIndex
- 1;
1277 int searchIndex
= previousRowIndex
;
1278 qreal minDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(previousRowIndex
));
1279 while (searchIndex
> 0 && keyboardAnchorPos(searchIndex
- 1) < keyboardAnchorPos(searchIndex
)) {
1281 const qreal searchDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(searchIndex
));
1282 if (searchDiff
< minDiff
) {
1283 minDiff
= searchDiff
;
1284 previousRowIndex
= searchIndex
;
1288 return previousRowIndex
;
1291 qreal
KItemListController::keyboardAnchorPos(int index
) const
1293 const QRectF itemRect
= m_view
->itemRect(index
);
1294 if (!itemRect
.isEmpty()) {
1295 return (m_view
->scrollOrientation() == Qt::Vertical
) ? itemRect
.x() : itemRect
.y();
1301 void KItemListController::updateExtendedSelectionRegion()
1304 const bool extend
= (m_selectionBehavior
!= MultiSelection
);
1305 KItemListStyleOption option
= m_view
->styleOption();
1306 if (option
.extendedSelectionRegion
!= extend
) {
1307 option
.extendedSelectionRegion
= extend
;
1308 m_view
->setStyleOption(option
);