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"
26 #include "kitemlistselectionmanager.h"
27 #include "kitemlistview.h"
28 #include "private/kitemlistkeyboardsearchmanager.h"
29 #include "private/kitemlistrubberband.h"
30 #include "views/draganddrophelper.h"
32 #include <QAccessible>
33 #include <QApplication>
35 #include <QGraphicsScene>
36 #include <QGraphicsSceneEvent>
37 #include <QGraphicsView>
41 KItemListController::KItemListController(KItemModelBase
* model
, KItemListView
* view
, QObject
* parent
) :
43 m_singleClickActivationEnforced(false),
44 m_selectionTogglePressed(false),
45 m_clearSelectionIfItemsAreNotDragged(false),
46 m_selectionBehavior(NoSelection
),
47 m_autoActivationBehavior(ActivationAndExpansion
),
48 m_mouseDoubleClickAction(ActivateItemOnly
),
51 m_selectionManager(new KItemListSelectionManager(this)),
52 m_keyboardManager(new KItemListKeyboardSearchManager(this)),
55 m_autoActivationTimer(nullptr),
57 m_keyboardAnchorIndex(-1),
58 m_keyboardAnchorPos(0)
60 connect(m_keyboardManager
, &KItemListKeyboardSearchManager::changeCurrentItem
,
61 this, &KItemListController::slotChangeCurrentItem
);
62 connect(m_selectionManager
, &KItemListSelectionManager::currentChanged
,
63 m_keyboardManager
, &KItemListKeyboardSearchManager::slotCurrentChanged
);
65 m_autoActivationTimer
= new QTimer(this);
66 m_autoActivationTimer
->setSingleShot(true);
67 m_autoActivationTimer
->setInterval(-1);
68 connect(m_autoActivationTimer
, &QTimer::timeout
, this, &KItemListController::slotAutoActivationTimeout
);
74 KItemListController::~KItemListController()
83 void KItemListController::setModel(KItemModelBase
* model
)
85 if (m_model
== model
) {
89 KItemModelBase
* oldModel
= m_model
;
91 oldModel
->deleteLater();
96 m_model
->setParent(this);
100 m_view
->setModel(m_model
);
103 m_selectionManager
->setModel(m_model
);
105 emit
modelChanged(m_model
, oldModel
);
108 KItemModelBase
* KItemListController::model() const
113 KItemListSelectionManager
* KItemListController::selectionManager() const
115 return m_selectionManager
;
118 void KItemListController::setView(KItemListView
* view
)
120 if (m_view
== view
) {
124 KItemListView
* oldView
= m_view
;
126 disconnect(oldView
, &KItemListView::scrollOffsetChanged
, this, &KItemListController::slotViewScrollOffsetChanged
);
127 oldView
->deleteLater();
133 m_view
->setParent(this);
134 m_view
->setController(this);
135 m_view
->setModel(m_model
);
136 connect(m_view
, &KItemListView::scrollOffsetChanged
, this, &KItemListController::slotViewScrollOffsetChanged
);
137 updateExtendedSelectionRegion();
140 emit
viewChanged(m_view
, oldView
);
143 KItemListView
* KItemListController::view() const
148 void KItemListController::setSelectionBehavior(SelectionBehavior behavior
)
150 m_selectionBehavior
= behavior
;
151 updateExtendedSelectionRegion();
154 KItemListController::SelectionBehavior
KItemListController::selectionBehavior() const
156 return m_selectionBehavior
;
159 void KItemListController::setAutoActivationBehavior(AutoActivationBehavior behavior
)
161 m_autoActivationBehavior
= behavior
;
164 KItemListController::AutoActivationBehavior
KItemListController::autoActivationBehavior() const
166 return m_autoActivationBehavior
;
169 void KItemListController::setMouseDoubleClickAction(MouseDoubleClickAction action
)
171 m_mouseDoubleClickAction
= action
;
174 KItemListController::MouseDoubleClickAction
KItemListController::mouseDoubleClickAction() const
176 return m_mouseDoubleClickAction
;
179 int KItemListController::indexCloseToMousePressedPosition() const
181 QHashIterator
<KItemListWidget
*, KItemListGroupHeader
*> it(m_view
->m_visibleGroups
);
182 while (it
.hasNext()) {
184 KItemListGroupHeader
*groupHeader
= it
.value();
185 const QPointF mappedToGroup
= groupHeader
->mapFromItem(nullptr, m_pressedMousePos
);
186 if (groupHeader
->contains(mappedToGroup
)) {
187 return it
.key()->index();
193 void KItemListController::setAutoActivationDelay(int delay
)
195 m_autoActivationTimer
->setInterval(delay
);
198 int KItemListController::autoActivationDelay() const
200 return m_autoActivationTimer
->interval();
203 void KItemListController::setSingleClickActivationEnforced(bool singleClick
)
205 m_singleClickActivationEnforced
= singleClick
;
208 bool KItemListController::singleClickActivationEnforced() const
210 return m_singleClickActivationEnforced
;
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 Q_FALLTHROUGH(); // fall through to the default case and add the Space to the current search string.
426 m_keyboardManager
->addKeys(event
->text());
427 // Make sure unconsumed events get propagated up the chain. #302329
432 if (m_selectionManager
->currentItem() != index
) {
433 switch (m_selectionBehavior
) {
435 m_selectionManager
->setCurrentItem(index
);
438 case SingleSelection
:
439 m_selectionManager
->setCurrentItem(index
);
440 m_selectionManager
->clearSelection();
441 m_selectionManager
->setSelected(index
, 1);
445 if (controlPressed
) {
446 m_selectionManager
->endAnchoredSelection();
449 m_selectionManager
->setCurrentItem(index
);
451 if (!shiftOrControlPressed
) {
452 m_selectionManager
->clearSelection();
453 m_selectionManager
->setSelected(index
, 1);
457 m_selectionManager
->beginAnchoredSelection(index
);
462 m_view
->scrollToItem(index
);
467 void KItemListController::slotChangeCurrentItem(const QString
& text
, bool searchFromNextItem
)
469 if (!m_model
|| m_model
->count() == 0) {
472 const int currentIndex
= m_selectionManager
->currentItem();
474 if (searchFromNextItem
) {
475 index
= m_model
->indexForKeyboardSearch(text
, (currentIndex
+ 1) % m_model
->count());
477 index
= m_model
->indexForKeyboardSearch(text
, currentIndex
);
480 m_selectionManager
->setCurrentItem(index
);
482 if (m_selectionBehavior
!= NoSelection
) {
483 m_selectionManager
->clearSelection();
484 m_selectionManager
->setSelected(index
, 1);
485 m_selectionManager
->beginAnchoredSelection(index
);
488 m_view
->scrollToItem(index
);
492 void KItemListController::slotAutoActivationTimeout()
494 if (!m_model
|| !m_view
) {
498 const int index
= m_autoActivationTimer
->property("index").toInt();
499 if (index
< 0 || index
>= m_model
->count()) {
503 /* m_view->isUnderMouse() fixes a bug in the Folder-View-Panel and in the
506 * Bug: When you drag a file onto a Folder-View-Item or a Places-Item and
507 * then move away before the auto-activation timeout triggers, than the
508 * item still becomes activated/expanded.
510 * See Bug 293200 and 305783
512 if (m_model
->supportsDropping(index
) && m_view
->isUnderMouse()) {
513 if (m_view
->supportsItemExpanding() && m_model
->isExpandable(index
)) {
514 const bool expanded
= m_model
->isExpanded(index
);
515 m_model
->setExpanded(index
, !expanded
);
516 } else if (m_autoActivationBehavior
!= ExpansionOnly
) {
517 emit
itemActivated(index
);
522 bool KItemListController::inputMethodEvent(QInputMethodEvent
* event
)
528 bool KItemListController::mousePressEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
534 m_pressedMousePos
= transform
.map(event
->pos());
535 m_pressedIndex
= m_view
->itemAt(m_pressedMousePos
);
536 emit
mouseButtonPressed(m_pressedIndex
, event
->buttons());
538 if (event
->buttons() & (Qt::BackButton
| Qt::ForwardButton
)) {
539 // Do not select items when clicking the back/forward buttons, see
540 // https://bugs.kde.org/show_bug.cgi?id=327412.
544 if (m_view
->isAboveExpansionToggle(m_pressedIndex
, m_pressedMousePos
)) {
545 m_selectionManager
->endAnchoredSelection();
546 m_selectionManager
->setCurrentItem(m_pressedIndex
);
547 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
551 m_selectionTogglePressed
= m_view
->isAboveSelectionToggle(m_pressedIndex
, m_pressedMousePos
);
552 if (m_selectionTogglePressed
) {
553 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
554 // The previous anchored selection has been finished already in
555 // KItemListSelectionManager::setSelected(). We can safely change
556 // the current item and start a new anchored selection now.
557 m_selectionManager
->setCurrentItem(m_pressedIndex
);
558 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
562 const bool shiftPressed
= event
->modifiers() & Qt::ShiftModifier
;
563 const bool controlPressed
= event
->modifiers() & Qt::ControlModifier
;
565 // The previous selection is cleared if either
566 // 1. The selection mode is SingleSelection, or
567 // 2. the selection mode is MultiSelection, and *none* of the following conditions are met:
568 // a) Shift or Control are pressed.
569 // b) The clicked item is selected already. In that case, the user might want to:
570 // - start dragging multiple items, or
571 // - open the context menu and perform an action for all selected items.
572 const bool shiftOrControlPressed
= shiftPressed
|| controlPressed
;
573 const bool pressedItemAlreadySelected
= m_pressedIndex
>= 0 && m_selectionManager
->isSelected(m_pressedIndex
);
574 const bool clearSelection
= m_selectionBehavior
== SingleSelection
||
575 (!shiftOrControlPressed
&& !pressedItemAlreadySelected
);
576 if (clearSelection
) {
577 m_selectionManager
->clearSelection();
578 } else if (pressedItemAlreadySelected
&& !shiftOrControlPressed
&& (event
->buttons() & Qt::LeftButton
)) {
579 // The user might want to start dragging multiple items, but if he clicks the item
580 // in order to trigger it instead, the other selected items must be deselected.
581 // However, we do not know yet what the user is going to do.
582 // -> remember that the user pressed an item which had been selected already and
583 // clear the selection in mouseReleaseEvent(), unless the items are dragged.
584 m_clearSelectionIfItemsAreNotDragged
= true;
586 if (m_selectionManager
->selectedItems().count() == 1 && m_view
->isAboveText(m_pressedIndex
, m_pressedMousePos
)) {
587 emit
selectedItemTextPressed(m_pressedIndex
);
592 // Finish the anchored selection before the current index is changed
593 m_selectionManager
->endAnchoredSelection();
596 if (m_pressedIndex
>= 0) {
597 m_selectionManager
->setCurrentItem(m_pressedIndex
);
599 switch (m_selectionBehavior
) {
603 case SingleSelection
:
604 m_selectionManager
->setSelected(m_pressedIndex
);
608 if (controlPressed
&& !shiftPressed
) {
609 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
610 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
611 } else if (!shiftPressed
|| !m_selectionManager
->isAnchoredSelectionActive()) {
612 // Select the pressed item and start a new anchored selection
613 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Select
);
614 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
623 if (event
->buttons() & Qt::RightButton
) {
624 emit
itemContextMenuRequested(m_pressedIndex
, event
->screenPos());
630 if (event
->buttons() & Qt::RightButton
) {
631 const QRectF headerBounds
= m_view
->headerBoundaries();
632 if (headerBounds
.contains(event
->pos())) {
633 emit
headerContextMenuRequested(event
->screenPos());
635 emit
viewContextMenuRequested(event
->screenPos());
640 if (m_selectionBehavior
== MultiSelection
) {
641 QPointF startPos
= m_pressedMousePos
;
642 if (m_view
->scrollOrientation() == Qt::Vertical
) {
643 startPos
.ry() += m_view
->scrollOffset();
644 if (m_view
->itemSize().width() < 0) {
645 // Use a special rubberband for views that have only one column and
646 // expand the rubberband to use the whole width of the view.
650 startPos
.rx() += m_view
->scrollOffset();
653 m_oldSelection
= m_selectionManager
->selectedItems();
654 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
655 rubberBand
->setStartPosition(startPos
);
656 rubberBand
->setEndPosition(startPos
);
657 rubberBand
->setActive(true);
658 connect(rubberBand
, &KItemListRubberBand::endPositionChanged
, this, &KItemListController::slotRubberBandChanged
);
659 m_view
->setAutoScroll(true);
665 bool KItemListController::mouseMoveEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
671 if (m_pressedIndex
>= 0) {
672 // Check whether a dragging should be started
673 if (event
->buttons() & Qt::LeftButton
) {
674 const QPointF pos
= transform
.map(event
->pos());
675 if ((pos
- m_pressedMousePos
).manhattanLength() >= QApplication::startDragDistance()) {
676 if (!m_selectionManager
->isSelected(m_pressedIndex
)) {
677 // Always assure that the dragged item gets selected. Usually this is already
678 // done on the mouse-press event, but when using the selection-toggle on a
679 // selected item the dragged item is not selected yet.
680 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
682 // A selected item has been clicked to drag all selected items
683 // -> the selection should not be cleared when the mouse button is released.
684 m_clearSelectionIfItemsAreNotDragged
= false;
691 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
692 if (rubberBand
->isActive()) {
693 QPointF endPos
= transform
.map(event
->pos());
695 // Update the current item.
696 const int newCurrent
= m_view
->itemAt(endPos
);
697 if (newCurrent
>= 0) {
698 // It's expected that the new current index is also the new anchor (bug 163451).
699 m_selectionManager
->endAnchoredSelection();
700 m_selectionManager
->setCurrentItem(newCurrent
);
701 m_selectionManager
->beginAnchoredSelection(newCurrent
);
704 if (m_view
->scrollOrientation() == Qt::Vertical
) {
705 endPos
.ry() += m_view
->scrollOffset();
706 if (m_view
->itemSize().width() < 0) {
707 // Use a special rubberband for views that have only one column and
708 // expand the rubberband to use the whole width of the view.
709 endPos
.setX(m_view
->size().width());
712 endPos
.rx() += m_view
->scrollOffset();
714 rubberBand
->setEndPosition(endPos
);
721 bool KItemListController::mouseReleaseEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
727 emit
mouseButtonReleased(m_pressedIndex
, event
->buttons());
729 const bool isAboveSelectionToggle
= m_view
->isAboveSelectionToggle(m_pressedIndex
, m_pressedMousePos
);
730 if (isAboveSelectionToggle
) {
731 m_selectionTogglePressed
= false;
735 if (!isAboveSelectionToggle
&& m_selectionTogglePressed
) {
736 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
737 m_selectionTogglePressed
= false;
741 const bool shiftOrControlPressed
= event
->modifiers() & Qt::ShiftModifier
||
742 event
->modifiers() & Qt::ControlModifier
;
744 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
745 if (rubberBand
->isActive()) {
746 disconnect(rubberBand
, &KItemListRubberBand::endPositionChanged
, this, &KItemListController::slotRubberBandChanged
);
747 rubberBand
->setActive(false);
748 m_oldSelection
.clear();
749 m_view
->setAutoScroll(false);
752 const QPointF pos
= transform
.map(event
->pos());
753 const int index
= m_view
->itemAt(pos
);
755 if (index
>= 0 && index
== m_pressedIndex
) {
756 // The release event is done above the same item as the press event
758 if (m_clearSelectionIfItemsAreNotDragged
) {
759 // A selected item has been clicked, but no drag operation has been started
760 // -> clear the rest of the selection.
761 m_selectionManager
->clearSelection();
762 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Select
);
763 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
766 if (event
->button() & Qt::LeftButton
) {
767 bool emitItemActivated
= true;
768 if (m_view
->isAboveExpansionToggle(index
, pos
)) {
769 const bool expanded
= m_model
->isExpanded(index
);
770 m_model
->setExpanded(index
, !expanded
);
772 emit
itemExpansionToggleClicked(index
);
773 emitItemActivated
= false;
774 } else if (shiftOrControlPressed
) {
775 // The mouse click should only update the selection, not trigger the item
776 emitItemActivated
= false;
777 } else if (!(m_view
->style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick
) || m_singleClickActivationEnforced
)) {
778 emitItemActivated
= false;
780 if (emitItemActivated
) {
781 emit
itemActivated(index
);
783 } else if (event
->button() & Qt::MidButton
) {
784 emit
itemMiddleClicked(index
);
788 m_pressedMousePos
= QPointF();
790 m_clearSelectionIfItemsAreNotDragged
= false;
794 bool KItemListController::mouseDoubleClickEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
796 const QPointF pos
= transform
.map(event
->pos());
797 const int index
= m_view
->itemAt(pos
);
799 // Expand item if desired - See Bug 295573
800 if (m_mouseDoubleClickAction
!= ActivateItemOnly
) {
801 if (m_view
&& m_model
&& m_view
->supportsItemExpanding() && m_model
->isExpandable(index
)) {
802 const bool expanded
= m_model
->isExpanded(index
);
803 m_model
->setExpanded(index
, !expanded
);
807 if (event
->button() & Qt::RightButton
) {
808 m_selectionManager
->clearSelection();
810 m_selectionManager
->setSelected(index
);
811 emit
itemContextMenuRequested(index
, event
->screenPos());
813 const QRectF headerBounds
= m_view
->headerBoundaries();
814 if (headerBounds
.contains(event
->pos())) {
815 emit
headerContextMenuRequested(event
->screenPos());
817 emit
viewContextMenuRequested(event
->screenPos());
823 bool emitItemActivated
= !(m_view
->style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick
) || m_singleClickActivationEnforced
) &&
824 (event
->button() & Qt::LeftButton
) &&
825 index
>= 0 && index
< m_model
->count();
826 if (emitItemActivated
) {
827 emit
itemActivated(index
);
832 bool KItemListController::dragEnterEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
837 DragAndDropHelper::clearUrlListMatchesUrlCache();
842 bool KItemListController::dragLeaveEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
847 m_autoActivationTimer
->stop();
848 m_view
->setAutoScroll(false);
849 m_view
->hideDropIndicator();
851 KItemListWidget
* widget
= hoveredWidget();
853 widget
->setHovered(false);
854 emit
itemUnhovered(widget
->index());
859 bool KItemListController::dragMoveEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
861 if (!m_model
|| !m_view
) {
866 QUrl hoveredDir
= m_model
->directory();
867 KItemListWidget
* oldHoveredWidget
= hoveredWidget();
869 const QPointF pos
= transform
.map(event
->pos());
870 KItemListWidget
* newHoveredWidget
= widgetForPos(pos
);
872 if (oldHoveredWidget
!= newHoveredWidget
) {
873 m_autoActivationTimer
->stop();
875 if (oldHoveredWidget
) {
876 oldHoveredWidget
->setHovered(false);
877 emit
itemUnhovered(oldHoveredWidget
->index());
881 if (newHoveredWidget
) {
882 bool droppingBetweenItems
= false;
883 if (m_model
->sortRole().isEmpty()) {
884 // The model supports inserting items between other items.
885 droppingBetweenItems
= (m_view
->showDropIndicator(pos
) >= 0);
888 const int index
= newHoveredWidget
->index();
890 if (m_model
->isDir(index
)) {
891 hoveredDir
= m_model
->url(index
);
894 if (!droppingBetweenItems
) {
895 if (m_model
->supportsDropping(index
)) {
896 // Something has been dragged on an item.
897 m_view
->hideDropIndicator();
898 if (!newHoveredWidget
->isHovered()) {
899 newHoveredWidget
->setHovered(true);
900 emit
itemHovered(index
);
903 if (!m_autoActivationTimer
->isActive() && m_autoActivationTimer
->interval() >= 0) {
904 m_autoActivationTimer
->setProperty("index", index
);
905 m_autoActivationTimer
->start();
909 m_autoActivationTimer
->stop();
910 if (newHoveredWidget
&& newHoveredWidget
->isHovered()) {
911 newHoveredWidget
->setHovered(false);
912 emit
itemUnhovered(index
);
916 m_view
->hideDropIndicator();
919 event
->setAccepted(!DragAndDropHelper::urlListMatchesUrl(event
->mimeData()->urls(), hoveredDir
));
924 bool KItemListController::dropEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
930 m_autoActivationTimer
->stop();
931 m_view
->setAutoScroll(false);
933 const QPointF pos
= transform
.map(event
->pos());
935 int dropAboveIndex
= -1;
936 if (m_model
->sortRole().isEmpty()) {
937 // The model supports inserting of items between other items.
938 dropAboveIndex
= m_view
->showDropIndicator(pos
);
941 if (dropAboveIndex
>= 0) {
942 // Something has been dropped between two items.
943 m_view
->hideDropIndicator();
944 emit
aboveItemDropEvent(dropAboveIndex
, event
);
945 } else if (!event
->mimeData()->hasFormat(m_model
->blacklistItemDropEventMimeType())) {
946 // Something has been dropped on an item or on an empty part of the view.
947 emit
itemDropEvent(m_view
->itemAt(pos
), event
);
950 QAccessibleEvent
accessibilityEvent(view(), QAccessible::DragDropEnd
);
951 QAccessible::updateAccessibility(&accessibilityEvent
);
956 bool KItemListController::hoverEnterEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
963 bool KItemListController::hoverMoveEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
966 if (!m_model
|| !m_view
) {
970 KItemListWidget
* oldHoveredWidget
= hoveredWidget();
971 const QPointF pos
= transform
.map(event
->pos());
972 KItemListWidget
* newHoveredWidget
= widgetForPos(pos
);
974 if (oldHoveredWidget
!= newHoveredWidget
) {
975 if (oldHoveredWidget
) {
976 oldHoveredWidget
->setHovered(false);
977 emit
itemUnhovered(oldHoveredWidget
->index());
980 if (newHoveredWidget
) {
981 newHoveredWidget
->setHovered(true);
982 const QPointF mappedPos
= newHoveredWidget
->mapFromItem(m_view
, pos
);
983 newHoveredWidget
->setHoverPosition(mappedPos
);
984 emit
itemHovered(newHoveredWidget
->index());
986 } else if (oldHoveredWidget
) {
987 const QPointF mappedPos
= oldHoveredWidget
->mapFromItem(m_view
, pos
);
988 oldHoveredWidget
->setHoverPosition(mappedPos
);
994 bool KItemListController::hoverLeaveEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
999 if (!m_model
|| !m_view
) {
1003 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
1004 if (widget
->isHovered()) {
1005 widget
->setHovered(false);
1006 emit
itemUnhovered(widget
->index());
1012 bool KItemListController::wheelEvent(QGraphicsSceneWheelEvent
* event
, const QTransform
& transform
)
1015 Q_UNUSED(transform
);
1019 bool KItemListController::resizeEvent(QGraphicsSceneResizeEvent
* event
, const QTransform
& transform
)
1022 Q_UNUSED(transform
);
1026 bool KItemListController::processEvent(QEvent
* event
, const QTransform
& transform
)
1032 switch (event
->type()) {
1033 case QEvent::KeyPress
:
1034 return keyPressEvent(static_cast<QKeyEvent
*>(event
));
1035 case QEvent::InputMethod
:
1036 return inputMethodEvent(static_cast<QInputMethodEvent
*>(event
));
1037 case QEvent::GraphicsSceneMousePress
:
1038 return mousePressEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
1039 case QEvent::GraphicsSceneMouseMove
:
1040 return mouseMoveEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
1041 case QEvent::GraphicsSceneMouseRelease
:
1042 return mouseReleaseEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
1043 case QEvent::GraphicsSceneMouseDoubleClick
:
1044 return mouseDoubleClickEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
1045 case QEvent::GraphicsSceneWheel
:
1046 return wheelEvent(static_cast<QGraphicsSceneWheelEvent
*>(event
), QTransform());
1047 case QEvent::GraphicsSceneDragEnter
:
1048 return dragEnterEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
1049 case QEvent::GraphicsSceneDragLeave
:
1050 return dragLeaveEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
1051 case QEvent::GraphicsSceneDragMove
:
1052 return dragMoveEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
1053 case QEvent::GraphicsSceneDrop
:
1054 return dropEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
1055 case QEvent::GraphicsSceneHoverEnter
:
1056 return hoverEnterEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
1057 case QEvent::GraphicsSceneHoverMove
:
1058 return hoverMoveEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
1059 case QEvent::GraphicsSceneHoverLeave
:
1060 return hoverLeaveEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
1061 case QEvent::GraphicsSceneResize
:
1062 return resizeEvent(static_cast<QGraphicsSceneResizeEvent
*>(event
), transform
);
1070 void KItemListController::slotViewScrollOffsetChanged(qreal current
, qreal previous
)
1076 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
1077 if (rubberBand
->isActive()) {
1078 const qreal diff
= current
- previous
;
1079 // TODO: Ideally just QCursor::pos() should be used as
1080 // new end-position but it seems there is no easy way
1081 // to have something like QWidget::mapFromGlobal() for QGraphicsWidget
1082 // (... or I just missed an easy way to do the mapping)
1083 QPointF endPos
= rubberBand
->endPosition();
1084 if (m_view
->scrollOrientation() == Qt::Vertical
) {
1085 endPos
.ry() += diff
;
1087 endPos
.rx() += diff
;
1090 rubberBand
->setEndPosition(endPos
);
1094 void KItemListController::slotRubberBandChanged()
1096 if (!m_view
|| !m_model
|| m_model
->count() <= 0) {
1100 const KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
1101 const QPointF startPos
= rubberBand
->startPosition();
1102 const QPointF endPos
= rubberBand
->endPosition();
1103 QRectF rubberBandRect
= QRectF(startPos
, endPos
).normalized();
1105 const bool scrollVertical
= (m_view
->scrollOrientation() == Qt::Vertical
);
1106 if (scrollVertical
) {
1107 rubberBandRect
.translate(0, -m_view
->scrollOffset());
1109 rubberBandRect
.translate(-m_view
->scrollOffset(), 0);
1112 if (!m_oldSelection
.isEmpty()) {
1113 // Clear the old selection that was available before the rubberband has
1114 // been activated in case if no Shift- or Control-key are pressed
1115 const bool shiftOrControlPressed
= QApplication::keyboardModifiers() & Qt::ShiftModifier
||
1116 QApplication::keyboardModifiers() & Qt::ControlModifier
;
1117 if (!shiftOrControlPressed
) {
1118 m_oldSelection
.clear();
1122 KItemSet selectedItems
;
1124 // Select all visible items that intersect with the rubberband
1125 foreach (const KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
1126 const int index
= widget
->index();
1128 const QRectF widgetRect
= m_view
->itemRect(index
);
1129 if (widgetRect
.intersects(rubberBandRect
)) {
1130 const QRectF iconRect
= widget
->iconRect().translated(widgetRect
.topLeft());
1131 const QRectF textRect
= widget
->textRect().translated(widgetRect
.topLeft());
1132 if (iconRect
.intersects(rubberBandRect
) || textRect
.intersects(rubberBandRect
)) {
1133 selectedItems
.insert(index
);
1138 // Select all invisible items that intersect with the rubberband. Instead of
1139 // iterating all items only the area which might be touched by the rubberband
1141 const bool increaseIndex
= scrollVertical
?
1142 startPos
.y() > endPos
.y(): startPos
.x() > endPos
.x();
1144 int index
= increaseIndex
? m_view
->lastVisibleIndex() + 1 : m_view
->firstVisibleIndex() - 1;
1145 bool selectionFinished
= false;
1147 const QRectF widgetRect
= m_view
->itemRect(index
);
1148 if (widgetRect
.intersects(rubberBandRect
)) {
1149 selectedItems
.insert(index
);
1152 if (increaseIndex
) {
1154 selectionFinished
= (index
>= m_model
->count()) ||
1155 ( scrollVertical
&& widgetRect
.top() > rubberBandRect
.bottom()) ||
1156 (!scrollVertical
&& widgetRect
.left() > rubberBandRect
.right());
1159 selectionFinished
= (index
< 0) ||
1160 ( scrollVertical
&& widgetRect
.bottom() < rubberBandRect
.top()) ||
1161 (!scrollVertical
&& widgetRect
.right() < rubberBandRect
.left());
1163 } while (!selectionFinished
);
1165 if (QApplication::keyboardModifiers() & Qt::ControlModifier
) {
1166 // If Control is pressed, the selection state of all items in the rubberband is toggled.
1167 // Therefore, the new selection contains:
1168 // 1. All previously selected items which are not inside the rubberband, and
1169 // 2. all items inside the rubberband which have not been selected previously.
1170 m_selectionManager
->setSelectedItems(m_oldSelection
^ selectedItems
);
1173 m_selectionManager
->setSelectedItems(selectedItems
+ m_oldSelection
);
1177 void KItemListController::startDragging()
1179 if (!m_view
|| !m_model
) {
1183 const KItemSet selectedItems
= m_selectionManager
->selectedItems();
1184 if (selectedItems
.isEmpty()) {
1188 QMimeData
* data
= m_model
->createMimeData(selectedItems
);
1193 // The created drag object will be owned and deleted
1194 // by QApplication::activeWindow().
1195 QDrag
* drag
= new QDrag(QApplication::activeWindow());
1196 drag
->setMimeData(data
);
1198 const QPixmap pixmap
= m_view
->createDragPixmap(selectedItems
);
1199 drag
->setPixmap(pixmap
);
1201 const QPoint
hotSpot((pixmap
.width() / pixmap
.devicePixelRatio()) / 2, 0);
1202 drag
->setHotSpot(hotSpot
);
1204 drag
->exec(Qt::MoveAction
| Qt::CopyAction
| Qt::LinkAction
, Qt::CopyAction
);
1206 QAccessibleEvent
accessibilityEvent(view(), QAccessible::DragDropStart
);
1207 QAccessible::updateAccessibility(&accessibilityEvent
);
1210 KItemListWidget
* KItemListController::hoveredWidget() const
1214 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
1215 if (widget
->isHovered()) {
1223 KItemListWidget
* KItemListController::widgetForPos(const QPointF
& pos
) const
1227 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
1228 const QPointF mappedPos
= widget
->mapFromItem(m_view
, pos
);
1230 const bool hovered
= widget
->contains(mappedPos
) &&
1231 !widget
->expansionToggleRect().contains(mappedPos
);
1240 void KItemListController::updateKeyboardAnchor()
1242 const bool validAnchor
= m_keyboardAnchorIndex
>= 0 &&
1243 m_keyboardAnchorIndex
< m_model
->count() &&
1244 keyboardAnchorPos(m_keyboardAnchorIndex
) == m_keyboardAnchorPos
;
1246 const int index
= m_selectionManager
->currentItem();
1247 m_keyboardAnchorIndex
= index
;
1248 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
1252 int KItemListController::nextRowIndex(int index
) const
1254 if (m_keyboardAnchorIndex
< 0) {
1258 const int maxIndex
= m_model
->count() - 1;
1259 if (index
== maxIndex
) {
1263 // Calculate the index of the last column inside the row of the current index
1264 int lastColumnIndex
= index
;
1265 while (keyboardAnchorPos(lastColumnIndex
+ 1) > keyboardAnchorPos(lastColumnIndex
)) {
1267 if (lastColumnIndex
>= maxIndex
) {
1272 // Based on the last column index go to the next row and calculate the nearest index
1273 // that is below the current index
1274 int nextRowIndex
= lastColumnIndex
+ 1;
1275 int searchIndex
= nextRowIndex
;
1276 qreal minDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(nextRowIndex
));
1277 while (searchIndex
< maxIndex
&& keyboardAnchorPos(searchIndex
+ 1) > keyboardAnchorPos(searchIndex
)) {
1279 const qreal searchDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(searchIndex
));
1280 if (searchDiff
< minDiff
) {
1281 minDiff
= searchDiff
;
1282 nextRowIndex
= searchIndex
;
1286 return nextRowIndex
;
1289 int KItemListController::previousRowIndex(int index
) const
1291 if (m_keyboardAnchorIndex
< 0 || index
== 0) {
1295 // Calculate the index of the first column inside the row of the current index
1296 int firstColumnIndex
= index
;
1297 while (keyboardAnchorPos(firstColumnIndex
- 1) < keyboardAnchorPos(firstColumnIndex
)) {
1299 if (firstColumnIndex
<= 0) {
1304 // Based on the first column index go to the previous row and calculate the nearest index
1305 // that is above the current index
1306 int previousRowIndex
= firstColumnIndex
- 1;
1307 int searchIndex
= previousRowIndex
;
1308 qreal minDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(previousRowIndex
));
1309 while (searchIndex
> 0 && keyboardAnchorPos(searchIndex
- 1) < keyboardAnchorPos(searchIndex
)) {
1311 const qreal searchDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(searchIndex
));
1312 if (searchDiff
< minDiff
) {
1313 minDiff
= searchDiff
;
1314 previousRowIndex
= searchIndex
;
1318 return previousRowIndex
;
1321 qreal
KItemListController::keyboardAnchorPos(int index
) const
1323 const QRectF itemRect
= m_view
->itemRect(index
);
1324 if (!itemRect
.isEmpty()) {
1325 return (m_view
->scrollOrientation() == Qt::Vertical
) ? itemRect
.x() : itemRect
.y();
1331 void KItemListController::updateExtendedSelectionRegion()
1334 const bool extend
= (m_selectionBehavior
!= MultiSelection
);
1335 KItemListStyleOption option
= m_view
->styleOption();
1336 if (option
.extendedSelectionRegion
!= extend
) {
1337 option
.extendedSelectionRegion
= extend
;
1338 m_view
->setStyleOption(option
);