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
;
234 const bool navigationPressed
= key
== Qt::Key_Home
|| key
== Qt::Key_End
||
235 key
== Qt::Key_PageUp
|| key
== Qt::Key_PageDown
||
236 key
== Qt::Key_Up
|| key
== Qt::Key_Down
||
237 key
== Qt::Key_Left
|| key
== Qt::Key_Right
;
239 const int itemCount
= m_model
->count();
241 // For horizontal scroll orientation, transform
242 // the arrow keys to simplify the event handling.
243 if (m_view
->scrollOrientation() == Qt::Horizontal
) {
245 case Qt::Key_Up
: key
= Qt::Key_Left
; break;
246 case Qt::Key_Down
: key
= Qt::Key_Right
; break;
247 case Qt::Key_Left
: key
= Qt::Key_Up
; break;
248 case Qt::Key_Right
: key
= Qt::Key_Down
; break;
253 const bool selectSingleItem
= m_selectionBehavior
!= NoSelection
&& itemCount
== 1 && navigationPressed
;
255 if (selectSingleItem
) {
256 const int current
= m_selectionManager
->currentItem();
257 m_selectionManager
->setSelected(current
);
264 m_keyboardAnchorIndex
= index
;
265 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
269 index
= itemCount
- 1;
270 m_keyboardAnchorIndex
= index
;
271 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
276 const int expandedParentsCount
= m_model
->expandedParentsCount(index
);
277 if (expandedParentsCount
== 0) {
280 // Go to the parent of the current item.
283 } while (index
> 0 && m_model
->expandedParentsCount(index
) == expandedParentsCount
);
285 m_keyboardAnchorIndex
= index
;
286 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
291 if (index
< itemCount
- 1) {
293 m_keyboardAnchorIndex
= index
;
294 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
299 updateKeyboardAnchor();
300 index
= previousRowIndex(index
);
304 updateKeyboardAnchor();
305 index
= nextRowIndex(index
);
309 if (m_view
->scrollOrientation() == Qt::Horizontal
) {
310 // The new current index should correspond to the first item in the current column.
311 int newIndex
= qMax(index
- 1, 0);
312 while (newIndex
!= index
&& m_view
->itemRect(newIndex
).topLeft().y() < m_view
->itemRect(index
).topLeft().y()) {
314 newIndex
= qMax(index
- 1, 0);
316 m_keyboardAnchorIndex
= index
;
317 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
319 const qreal currentItemBottom
= m_view
->itemRect(index
).bottomLeft().y();
320 const qreal height
= m_view
->geometry().height();
322 // The new current item should be the first item in the current
323 // column whose itemRect's top coordinate is larger than targetY.
324 const qreal targetY
= currentItemBottom
- height
;
326 updateKeyboardAnchor();
327 int newIndex
= previousRowIndex(index
);
330 updateKeyboardAnchor();
331 newIndex
= previousRowIndex(index
);
332 } while (m_view
->itemRect(newIndex
).topLeft().y() > targetY
&& newIndex
!= index
);
336 case Qt::Key_PageDown
:
337 if (m_view
->scrollOrientation() == Qt::Horizontal
) {
338 // The new current index should correspond to the last item in the current column.
339 int newIndex
= qMin(index
+ 1, m_model
->count() - 1);
340 while (newIndex
!= index
&& m_view
->itemRect(newIndex
).topLeft().y() > m_view
->itemRect(index
).topLeft().y()) {
342 newIndex
= qMin(index
+ 1, m_model
->count() - 1);
344 m_keyboardAnchorIndex
= index
;
345 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
347 const qreal currentItemTop
= m_view
->itemRect(index
).topLeft().y();
348 const qreal height
= m_view
->geometry().height();
350 // The new current item should be the last item in the current
351 // column whose itemRect's bottom coordinate is smaller than targetY.
352 const qreal targetY
= currentItemTop
+ height
;
354 updateKeyboardAnchor();
355 int newIndex
= nextRowIndex(index
);
358 updateKeyboardAnchor();
359 newIndex
= nextRowIndex(index
);
360 } while (m_view
->itemRect(newIndex
).bottomLeft().y() < targetY
&& newIndex
!= index
);
365 case Qt::Key_Return
: {
366 const KItemSet selectedItems
= m_selectionManager
->selectedItems();
367 if (selectedItems
.count() >= 2) {
368 emit
itemsActivated(selectedItems
);
369 } else if (selectedItems
.count() == 1) {
370 emit
itemActivated(selectedItems
.first());
372 emit
itemActivated(index
);
378 // Emit the signal itemContextMenuRequested() in case if at least one
379 // item is selected. Otherwise the signal viewContextMenuRequested() will be emitted.
380 const KItemSet selectedItems
= m_selectionManager
->selectedItems();
382 if (selectedItems
.count() >= 2) {
383 const int currentItemIndex
= m_selectionManager
->currentItem();
384 index
= selectedItems
.contains(currentItemIndex
)
385 ? currentItemIndex
: selectedItems
.first();
386 } else if (selectedItems
.count() == 1) {
387 index
= selectedItems
.first();
391 const QRectF contextRect
= m_view
->itemContextRect(index
);
392 const QPointF
pos(m_view
->scene()->views().first()->mapToGlobal(contextRect
.bottomRight().toPoint()));
393 emit
itemContextMenuRequested(index
, pos
);
395 emit
viewContextMenuRequested(QCursor::pos());
401 if (m_selectionBehavior
!= SingleSelection
) {
402 m_selectionManager
->clearSelection();
404 m_keyboardManager
->cancelSearch();
405 emit
escapePressed();
409 if (m_selectionBehavior
== MultiSelection
) {
410 if (controlPressed
) {
411 // Toggle the selection state of the current item.
412 m_selectionManager
->endAnchoredSelection();
413 m_selectionManager
->setSelected(index
, 1, KItemListSelectionManager::Toggle
);
414 m_selectionManager
->beginAnchoredSelection(index
);
417 // Select the current item if it is not selected yet.
418 const int current
= m_selectionManager
->currentItem();
419 if (!m_selectionManager
->isSelected(current
)) {
420 m_selectionManager
->setSelected(current
);
425 Q_FALLTHROUGH(); // 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
);
464 if (navigationPressed
) {
465 m_view
->scrollToItem(index
);
470 void KItemListController::slotChangeCurrentItem(const QString
& text
, bool searchFromNextItem
)
472 if (!m_model
|| m_model
->count() == 0) {
475 const int currentIndex
= m_selectionManager
->currentItem();
477 if (searchFromNextItem
) {
478 index
= m_model
->indexForKeyboardSearch(text
, (currentIndex
+ 1) % m_model
->count());
480 index
= m_model
->indexForKeyboardSearch(text
, currentIndex
);
483 m_selectionManager
->setCurrentItem(index
);
485 if (m_selectionBehavior
!= NoSelection
) {
486 m_selectionManager
->clearSelection();
487 m_selectionManager
->setSelected(index
, 1);
488 m_selectionManager
->beginAnchoredSelection(index
);
491 m_view
->scrollToItem(index
);
495 void KItemListController::slotAutoActivationTimeout()
497 if (!m_model
|| !m_view
) {
501 const int index
= m_autoActivationTimer
->property("index").toInt();
502 if (index
< 0 || index
>= m_model
->count()) {
506 /* m_view->isUnderMouse() fixes a bug in the Folder-View-Panel and in the
509 * Bug: When you drag a file onto a Folder-View-Item or a Places-Item and
510 * then move away before the auto-activation timeout triggers, than the
511 * item still becomes activated/expanded.
513 * See Bug 293200 and 305783
515 if (m_model
->supportsDropping(index
) && m_view
->isUnderMouse()) {
516 if (m_view
->supportsItemExpanding() && m_model
->isExpandable(index
)) {
517 const bool expanded
= m_model
->isExpanded(index
);
518 m_model
->setExpanded(index
, !expanded
);
519 } else if (m_autoActivationBehavior
!= ExpansionOnly
) {
520 emit
itemActivated(index
);
525 bool KItemListController::inputMethodEvent(QInputMethodEvent
* event
)
531 bool KItemListController::mousePressEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
537 m_pressedMousePos
= transform
.map(event
->pos());
538 m_pressedIndex
= m_view
->itemAt(m_pressedMousePos
);
539 emit
mouseButtonPressed(m_pressedIndex
, event
->buttons());
541 if (event
->buttons() & (Qt::BackButton
| Qt::ForwardButton
)) {
542 // Do not select items when clicking the back/forward buttons, see
543 // https://bugs.kde.org/show_bug.cgi?id=327412.
547 if (m_view
->isAboveExpansionToggle(m_pressedIndex
, m_pressedMousePos
)) {
548 m_selectionManager
->endAnchoredSelection();
549 m_selectionManager
->setCurrentItem(m_pressedIndex
);
550 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
554 m_selectionTogglePressed
= m_view
->isAboveSelectionToggle(m_pressedIndex
, m_pressedMousePos
);
555 if (m_selectionTogglePressed
) {
556 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
557 // The previous anchored selection has been finished already in
558 // KItemListSelectionManager::setSelected(). We can safely change
559 // the current item and start a new anchored selection now.
560 m_selectionManager
->setCurrentItem(m_pressedIndex
);
561 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
565 const bool shiftPressed
= event
->modifiers() & Qt::ShiftModifier
;
566 const bool controlPressed
= event
->modifiers() & Qt::ControlModifier
;
568 // The previous selection is cleared if either
569 // 1. The selection mode is SingleSelection, or
570 // 2. the selection mode is MultiSelection, and *none* of the following conditions are met:
571 // a) Shift or Control are pressed.
572 // b) The clicked item is selected already. In that case, the user might want to:
573 // - start dragging multiple items, or
574 // - open the context menu and perform an action for all selected items.
575 const bool shiftOrControlPressed
= shiftPressed
|| controlPressed
;
576 const bool pressedItemAlreadySelected
= m_pressedIndex
>= 0 && m_selectionManager
->isSelected(m_pressedIndex
);
577 const bool clearSelection
= m_selectionBehavior
== SingleSelection
||
578 (!shiftOrControlPressed
&& !pressedItemAlreadySelected
);
579 if (clearSelection
) {
580 m_selectionManager
->clearSelection();
581 } else if (pressedItemAlreadySelected
&& !shiftOrControlPressed
&& (event
->buttons() & Qt::LeftButton
)) {
582 // The user might want to start dragging multiple items, but if he clicks the item
583 // in order to trigger it instead, the other selected items must be deselected.
584 // However, we do not know yet what the user is going to do.
585 // -> remember that the user pressed an item which had been selected already and
586 // clear the selection in mouseReleaseEvent(), unless the items are dragged.
587 m_clearSelectionIfItemsAreNotDragged
= true;
589 if (m_selectionManager
->selectedItems().count() == 1 && m_view
->isAboveText(m_pressedIndex
, m_pressedMousePos
)) {
590 emit
selectedItemTextPressed(m_pressedIndex
);
595 // Finish the anchored selection before the current index is changed
596 m_selectionManager
->endAnchoredSelection();
599 if (m_pressedIndex
>= 0) {
600 m_selectionManager
->setCurrentItem(m_pressedIndex
);
602 switch (m_selectionBehavior
) {
606 case SingleSelection
:
607 m_selectionManager
->setSelected(m_pressedIndex
);
611 if (controlPressed
&& !shiftPressed
) {
612 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
613 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
614 } else if (!shiftPressed
|| !m_selectionManager
->isAnchoredSelectionActive()) {
615 // Select the pressed item and start a new anchored selection
616 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Select
);
617 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
626 if (event
->buttons() & Qt::RightButton
) {
627 emit
itemContextMenuRequested(m_pressedIndex
, event
->screenPos());
633 if (event
->buttons() & Qt::RightButton
) {
634 const QRectF headerBounds
= m_view
->headerBoundaries();
635 if (headerBounds
.contains(event
->pos())) {
636 emit
headerContextMenuRequested(event
->screenPos());
638 emit
viewContextMenuRequested(event
->screenPos());
643 if (m_selectionBehavior
== MultiSelection
) {
644 QPointF startPos
= m_pressedMousePos
;
645 if (m_view
->scrollOrientation() == Qt::Vertical
) {
646 startPos
.ry() += m_view
->scrollOffset();
647 if (m_view
->itemSize().width() < 0) {
648 // Use a special rubberband for views that have only one column and
649 // expand the rubberband to use the whole width of the view.
653 startPos
.rx() += m_view
->scrollOffset();
656 m_oldSelection
= m_selectionManager
->selectedItems();
657 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
658 rubberBand
->setStartPosition(startPos
);
659 rubberBand
->setEndPosition(startPos
);
660 rubberBand
->setActive(true);
661 connect(rubberBand
, &KItemListRubberBand::endPositionChanged
, this, &KItemListController::slotRubberBandChanged
);
662 m_view
->setAutoScroll(true);
668 bool KItemListController::mouseMoveEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
674 if (m_pressedIndex
>= 0) {
675 // Check whether a dragging should be started
676 if (event
->buttons() & Qt::LeftButton
) {
677 const QPointF pos
= transform
.map(event
->pos());
678 if ((pos
- m_pressedMousePos
).manhattanLength() >= QApplication::startDragDistance()) {
679 if (!m_selectionManager
->isSelected(m_pressedIndex
)) {
680 // Always assure that the dragged item gets selected. Usually this is already
681 // done on the mouse-press event, but when using the selection-toggle on a
682 // selected item the dragged item is not selected yet.
683 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
685 // A selected item has been clicked to drag all selected items
686 // -> the selection should not be cleared when the mouse button is released.
687 m_clearSelectionIfItemsAreNotDragged
= false;
694 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
695 if (rubberBand
->isActive()) {
696 QPointF endPos
= transform
.map(event
->pos());
698 // Update the current item.
699 const int newCurrent
= m_view
->itemAt(endPos
);
700 if (newCurrent
>= 0) {
701 // It's expected that the new current index is also the new anchor (bug 163451).
702 m_selectionManager
->endAnchoredSelection();
703 m_selectionManager
->setCurrentItem(newCurrent
);
704 m_selectionManager
->beginAnchoredSelection(newCurrent
);
707 if (m_view
->scrollOrientation() == Qt::Vertical
) {
708 endPos
.ry() += m_view
->scrollOffset();
709 if (m_view
->itemSize().width() < 0) {
710 // Use a special rubberband for views that have only one column and
711 // expand the rubberband to use the whole width of the view.
712 endPos
.setX(m_view
->size().width());
715 endPos
.rx() += m_view
->scrollOffset();
717 rubberBand
->setEndPosition(endPos
);
724 bool KItemListController::mouseReleaseEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
730 emit
mouseButtonReleased(m_pressedIndex
, event
->buttons());
732 const bool isAboveSelectionToggle
= m_view
->isAboveSelectionToggle(m_pressedIndex
, m_pressedMousePos
);
733 if (isAboveSelectionToggle
) {
734 m_selectionTogglePressed
= false;
738 if (!isAboveSelectionToggle
&& m_selectionTogglePressed
) {
739 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
740 m_selectionTogglePressed
= false;
744 const bool shiftOrControlPressed
= event
->modifiers() & Qt::ShiftModifier
||
745 event
->modifiers() & Qt::ControlModifier
;
747 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
748 if (rubberBand
->isActive()) {
749 disconnect(rubberBand
, &KItemListRubberBand::endPositionChanged
, this, &KItemListController::slotRubberBandChanged
);
750 rubberBand
->setActive(false);
751 m_oldSelection
.clear();
752 m_view
->setAutoScroll(false);
755 const QPointF pos
= transform
.map(event
->pos());
756 const int index
= m_view
->itemAt(pos
);
758 if (index
>= 0 && index
== m_pressedIndex
) {
759 // The release event is done above the same item as the press event
761 if (m_clearSelectionIfItemsAreNotDragged
) {
762 // A selected item has been clicked, but no drag operation has been started
763 // -> clear the rest of the selection.
764 m_selectionManager
->clearSelection();
765 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Select
);
766 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
769 if (event
->button() & Qt::LeftButton
) {
770 bool emitItemActivated
= true;
771 if (m_view
->isAboveExpansionToggle(index
, pos
)) {
772 const bool expanded
= m_model
->isExpanded(index
);
773 m_model
->setExpanded(index
, !expanded
);
775 emit
itemExpansionToggleClicked(index
);
776 emitItemActivated
= false;
777 } else if (shiftOrControlPressed
) {
778 // The mouse click should only update the selection, not trigger the item
779 emitItemActivated
= false;
780 } else if (!(m_view
->style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick
) || m_singleClickActivationEnforced
)) {
781 emitItemActivated
= false;
783 if (emitItemActivated
) {
784 emit
itemActivated(index
);
786 } else if (event
->button() & Qt::MidButton
) {
787 emit
itemMiddleClicked(index
);
791 m_pressedMousePos
= QPointF();
793 m_clearSelectionIfItemsAreNotDragged
= false;
797 bool KItemListController::mouseDoubleClickEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
799 const QPointF pos
= transform
.map(event
->pos());
800 const int index
= m_view
->itemAt(pos
);
802 // Expand item if desired - See Bug 295573
803 if (m_mouseDoubleClickAction
!= ActivateItemOnly
) {
804 if (m_view
&& m_model
&& m_view
->supportsItemExpanding() && m_model
->isExpandable(index
)) {
805 const bool expanded
= m_model
->isExpanded(index
);
806 m_model
->setExpanded(index
, !expanded
);
810 if (event
->button() & Qt::RightButton
) {
811 m_selectionManager
->clearSelection();
813 m_selectionManager
->setSelected(index
);
814 emit
itemContextMenuRequested(index
, event
->screenPos());
816 const QRectF headerBounds
= m_view
->headerBoundaries();
817 if (headerBounds
.contains(event
->pos())) {
818 emit
headerContextMenuRequested(event
->screenPos());
820 emit
viewContextMenuRequested(event
->screenPos());
826 bool emitItemActivated
= !(m_view
->style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick
) || m_singleClickActivationEnforced
) &&
827 (event
->button() & Qt::LeftButton
) &&
828 index
>= 0 && index
< m_model
->count();
829 if (emitItemActivated
) {
830 emit
itemActivated(index
);
835 bool KItemListController::dragEnterEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
840 DragAndDropHelper::clearUrlListMatchesUrlCache();
845 bool KItemListController::dragLeaveEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
850 m_autoActivationTimer
->stop();
851 m_view
->setAutoScroll(false);
852 m_view
->hideDropIndicator();
854 KItemListWidget
* widget
= hoveredWidget();
856 widget
->setHovered(false);
857 emit
itemUnhovered(widget
->index());
862 bool KItemListController::dragMoveEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
864 if (!m_model
|| !m_view
) {
869 QUrl hoveredDir
= m_model
->directory();
870 KItemListWidget
* oldHoveredWidget
= hoveredWidget();
872 const QPointF pos
= transform
.map(event
->pos());
873 KItemListWidget
* newHoveredWidget
= widgetForPos(pos
);
875 if (oldHoveredWidget
!= newHoveredWidget
) {
876 m_autoActivationTimer
->stop();
878 if (oldHoveredWidget
) {
879 oldHoveredWidget
->setHovered(false);
880 emit
itemUnhovered(oldHoveredWidget
->index());
884 if (newHoveredWidget
) {
885 bool droppingBetweenItems
= false;
886 if (m_model
->sortRole().isEmpty()) {
887 // The model supports inserting items between other items.
888 droppingBetweenItems
= (m_view
->showDropIndicator(pos
) >= 0);
891 const int index
= newHoveredWidget
->index();
893 if (m_model
->isDir(index
)) {
894 hoveredDir
= m_model
->url(index
);
897 if (!droppingBetweenItems
) {
898 if (m_model
->supportsDropping(index
)) {
899 // Something has been dragged on an item.
900 m_view
->hideDropIndicator();
901 if (!newHoveredWidget
->isHovered()) {
902 newHoveredWidget
->setHovered(true);
903 emit
itemHovered(index
);
906 if (!m_autoActivationTimer
->isActive() && m_autoActivationTimer
->interval() >= 0) {
907 m_autoActivationTimer
->setProperty("index", index
);
908 m_autoActivationTimer
->start();
912 m_autoActivationTimer
->stop();
913 if (newHoveredWidget
&& newHoveredWidget
->isHovered()) {
914 newHoveredWidget
->setHovered(false);
915 emit
itemUnhovered(index
);
919 m_view
->hideDropIndicator();
922 event
->setAccepted(!DragAndDropHelper::urlListMatchesUrl(event
->mimeData()->urls(), hoveredDir
));
927 bool KItemListController::dropEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
933 m_autoActivationTimer
->stop();
934 m_view
->setAutoScroll(false);
936 const QPointF pos
= transform
.map(event
->pos());
938 int dropAboveIndex
= -1;
939 if (m_model
->sortRole().isEmpty()) {
940 // The model supports inserting of items between other items.
941 dropAboveIndex
= m_view
->showDropIndicator(pos
);
944 if (dropAboveIndex
>= 0) {
945 // Something has been dropped between two items.
946 m_view
->hideDropIndicator();
947 emit
aboveItemDropEvent(dropAboveIndex
, event
);
948 } else if (!event
->mimeData()->hasFormat(m_model
->blacklistItemDropEventMimeType())) {
949 // Something has been dropped on an item or on an empty part of the view.
950 emit
itemDropEvent(m_view
->itemAt(pos
), event
);
953 QAccessibleEvent
accessibilityEvent(view(), QAccessible::DragDropEnd
);
954 QAccessible::updateAccessibility(&accessibilityEvent
);
959 bool KItemListController::hoverEnterEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
966 bool KItemListController::hoverMoveEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
969 if (!m_model
|| !m_view
) {
973 KItemListWidget
* oldHoveredWidget
= hoveredWidget();
974 const QPointF pos
= transform
.map(event
->pos());
975 KItemListWidget
* newHoveredWidget
= widgetForPos(pos
);
977 if (oldHoveredWidget
!= newHoveredWidget
) {
978 if (oldHoveredWidget
) {
979 oldHoveredWidget
->setHovered(false);
980 emit
itemUnhovered(oldHoveredWidget
->index());
983 if (newHoveredWidget
) {
984 newHoveredWidget
->setHovered(true);
985 const QPointF mappedPos
= newHoveredWidget
->mapFromItem(m_view
, pos
);
986 newHoveredWidget
->setHoverPosition(mappedPos
);
987 emit
itemHovered(newHoveredWidget
->index());
989 } else if (oldHoveredWidget
) {
990 const QPointF mappedPos
= oldHoveredWidget
->mapFromItem(m_view
, pos
);
991 oldHoveredWidget
->setHoverPosition(mappedPos
);
997 bool KItemListController::hoverLeaveEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
1000 Q_UNUSED(transform
);
1002 if (!m_model
|| !m_view
) {
1006 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
1007 if (widget
->isHovered()) {
1008 widget
->setHovered(false);
1009 emit
itemUnhovered(widget
->index());
1015 bool KItemListController::wheelEvent(QGraphicsSceneWheelEvent
* event
, const QTransform
& transform
)
1018 Q_UNUSED(transform
);
1022 bool KItemListController::resizeEvent(QGraphicsSceneResizeEvent
* event
, const QTransform
& transform
)
1025 Q_UNUSED(transform
);
1029 bool KItemListController::processEvent(QEvent
* event
, const QTransform
& transform
)
1035 switch (event
->type()) {
1036 case QEvent::KeyPress
:
1037 return keyPressEvent(static_cast<QKeyEvent
*>(event
));
1038 case QEvent::InputMethod
:
1039 return inputMethodEvent(static_cast<QInputMethodEvent
*>(event
));
1040 case QEvent::GraphicsSceneMousePress
:
1041 return mousePressEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
1042 case QEvent::GraphicsSceneMouseMove
:
1043 return mouseMoveEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
1044 case QEvent::GraphicsSceneMouseRelease
:
1045 return mouseReleaseEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
1046 case QEvent::GraphicsSceneMouseDoubleClick
:
1047 return mouseDoubleClickEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
1048 case QEvent::GraphicsSceneWheel
:
1049 return wheelEvent(static_cast<QGraphicsSceneWheelEvent
*>(event
), QTransform());
1050 case QEvent::GraphicsSceneDragEnter
:
1051 return dragEnterEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
1052 case QEvent::GraphicsSceneDragLeave
:
1053 return dragLeaveEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
1054 case QEvent::GraphicsSceneDragMove
:
1055 return dragMoveEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
1056 case QEvent::GraphicsSceneDrop
:
1057 return dropEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
1058 case QEvent::GraphicsSceneHoverEnter
:
1059 return hoverEnterEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
1060 case QEvent::GraphicsSceneHoverMove
:
1061 return hoverMoveEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
1062 case QEvent::GraphicsSceneHoverLeave
:
1063 return hoverLeaveEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
1064 case QEvent::GraphicsSceneResize
:
1065 return resizeEvent(static_cast<QGraphicsSceneResizeEvent
*>(event
), transform
);
1073 void KItemListController::slotViewScrollOffsetChanged(qreal current
, qreal previous
)
1079 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
1080 if (rubberBand
->isActive()) {
1081 const qreal diff
= current
- previous
;
1082 // TODO: Ideally just QCursor::pos() should be used as
1083 // new end-position but it seems there is no easy way
1084 // to have something like QWidget::mapFromGlobal() for QGraphicsWidget
1085 // (... or I just missed an easy way to do the mapping)
1086 QPointF endPos
= rubberBand
->endPosition();
1087 if (m_view
->scrollOrientation() == Qt::Vertical
) {
1088 endPos
.ry() += diff
;
1090 endPos
.rx() += diff
;
1093 rubberBand
->setEndPosition(endPos
);
1097 void KItemListController::slotRubberBandChanged()
1099 if (!m_view
|| !m_model
|| m_model
->count() <= 0) {
1103 const KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
1104 const QPointF startPos
= rubberBand
->startPosition();
1105 const QPointF endPos
= rubberBand
->endPosition();
1106 QRectF rubberBandRect
= QRectF(startPos
, endPos
).normalized();
1108 const bool scrollVertical
= (m_view
->scrollOrientation() == Qt::Vertical
);
1109 if (scrollVertical
) {
1110 rubberBandRect
.translate(0, -m_view
->scrollOffset());
1112 rubberBandRect
.translate(-m_view
->scrollOffset(), 0);
1115 if (!m_oldSelection
.isEmpty()) {
1116 // Clear the old selection that was available before the rubberband has
1117 // been activated in case if no Shift- or Control-key are pressed
1118 const bool shiftOrControlPressed
= QApplication::keyboardModifiers() & Qt::ShiftModifier
||
1119 QApplication::keyboardModifiers() & Qt::ControlModifier
;
1120 if (!shiftOrControlPressed
) {
1121 m_oldSelection
.clear();
1125 KItemSet selectedItems
;
1127 // Select all visible items that intersect with the rubberband
1128 foreach (const KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
1129 const int index
= widget
->index();
1131 const QRectF widgetRect
= m_view
->itemRect(index
);
1132 if (widgetRect
.intersects(rubberBandRect
)) {
1133 const QRectF iconRect
= widget
->iconRect().translated(widgetRect
.topLeft());
1134 const QRectF textRect
= widget
->textRect().translated(widgetRect
.topLeft());
1135 if (iconRect
.intersects(rubberBandRect
) || textRect
.intersects(rubberBandRect
)) {
1136 selectedItems
.insert(index
);
1141 // Select all invisible items that intersect with the rubberband. Instead of
1142 // iterating all items only the area which might be touched by the rubberband
1144 const bool increaseIndex
= scrollVertical
?
1145 startPos
.y() > endPos
.y(): startPos
.x() > endPos
.x();
1147 int index
= increaseIndex
? m_view
->lastVisibleIndex() + 1 : m_view
->firstVisibleIndex() - 1;
1148 bool selectionFinished
= false;
1150 const QRectF widgetRect
= m_view
->itemRect(index
);
1151 if (widgetRect
.intersects(rubberBandRect
)) {
1152 selectedItems
.insert(index
);
1155 if (increaseIndex
) {
1157 selectionFinished
= (index
>= m_model
->count()) ||
1158 ( scrollVertical
&& widgetRect
.top() > rubberBandRect
.bottom()) ||
1159 (!scrollVertical
&& widgetRect
.left() > rubberBandRect
.right());
1162 selectionFinished
= (index
< 0) ||
1163 ( scrollVertical
&& widgetRect
.bottom() < rubberBandRect
.top()) ||
1164 (!scrollVertical
&& widgetRect
.right() < rubberBandRect
.left());
1166 } while (!selectionFinished
);
1168 if (QApplication::keyboardModifiers() & Qt::ControlModifier
) {
1169 // If Control is pressed, the selection state of all items in the rubberband is toggled.
1170 // Therefore, the new selection contains:
1171 // 1. All previously selected items which are not inside the rubberband, and
1172 // 2. all items inside the rubberband which have not been selected previously.
1173 m_selectionManager
->setSelectedItems(m_oldSelection
^ selectedItems
);
1176 m_selectionManager
->setSelectedItems(selectedItems
+ m_oldSelection
);
1180 void KItemListController::startDragging()
1182 if (!m_view
|| !m_model
) {
1186 const KItemSet selectedItems
= m_selectionManager
->selectedItems();
1187 if (selectedItems
.isEmpty()) {
1191 QMimeData
* data
= m_model
->createMimeData(selectedItems
);
1196 // The created drag object will be owned and deleted
1197 // by QApplication::activeWindow().
1198 QDrag
* drag
= new QDrag(QApplication::activeWindow());
1199 drag
->setMimeData(data
);
1201 const QPixmap pixmap
= m_view
->createDragPixmap(selectedItems
);
1202 drag
->setPixmap(pixmap
);
1204 const QPoint
hotSpot((pixmap
.width() / pixmap
.devicePixelRatio()) / 2, 0);
1205 drag
->setHotSpot(hotSpot
);
1207 drag
->exec(Qt::MoveAction
| Qt::CopyAction
| Qt::LinkAction
, Qt::CopyAction
);
1209 QAccessibleEvent
accessibilityEvent(view(), QAccessible::DragDropStart
);
1210 QAccessible::updateAccessibility(&accessibilityEvent
);
1213 KItemListWidget
* KItemListController::hoveredWidget() const
1217 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
1218 if (widget
->isHovered()) {
1226 KItemListWidget
* KItemListController::widgetForPos(const QPointF
& pos
) const
1230 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
1231 const QPointF mappedPos
= widget
->mapFromItem(m_view
, pos
);
1233 const bool hovered
= widget
->contains(mappedPos
) &&
1234 !widget
->expansionToggleRect().contains(mappedPos
);
1243 void KItemListController::updateKeyboardAnchor()
1245 const bool validAnchor
= m_keyboardAnchorIndex
>= 0 &&
1246 m_keyboardAnchorIndex
< m_model
->count() &&
1247 keyboardAnchorPos(m_keyboardAnchorIndex
) == m_keyboardAnchorPos
;
1249 const int index
= m_selectionManager
->currentItem();
1250 m_keyboardAnchorIndex
= index
;
1251 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
1255 int KItemListController::nextRowIndex(int index
) const
1257 if (m_keyboardAnchorIndex
< 0) {
1261 const int maxIndex
= m_model
->count() - 1;
1262 if (index
== maxIndex
) {
1266 // Calculate the index of the last column inside the row of the current index
1267 int lastColumnIndex
= index
;
1268 while (keyboardAnchorPos(lastColumnIndex
+ 1) > keyboardAnchorPos(lastColumnIndex
)) {
1270 if (lastColumnIndex
>= maxIndex
) {
1275 // Based on the last column index go to the next row and calculate the nearest index
1276 // that is below the current index
1277 int nextRowIndex
= lastColumnIndex
+ 1;
1278 int searchIndex
= nextRowIndex
;
1279 qreal minDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(nextRowIndex
));
1280 while (searchIndex
< maxIndex
&& keyboardAnchorPos(searchIndex
+ 1) > keyboardAnchorPos(searchIndex
)) {
1282 const qreal searchDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(searchIndex
));
1283 if (searchDiff
< minDiff
) {
1284 minDiff
= searchDiff
;
1285 nextRowIndex
= searchIndex
;
1289 return nextRowIndex
;
1292 int KItemListController::previousRowIndex(int index
) const
1294 if (m_keyboardAnchorIndex
< 0 || index
== 0) {
1298 // Calculate the index of the first column inside the row of the current index
1299 int firstColumnIndex
= index
;
1300 while (keyboardAnchorPos(firstColumnIndex
- 1) < keyboardAnchorPos(firstColumnIndex
)) {
1302 if (firstColumnIndex
<= 0) {
1307 // Based on the first column index go to the previous row and calculate the nearest index
1308 // that is above the current index
1309 int previousRowIndex
= firstColumnIndex
- 1;
1310 int searchIndex
= previousRowIndex
;
1311 qreal minDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(previousRowIndex
));
1312 while (searchIndex
> 0 && keyboardAnchorPos(searchIndex
- 1) < keyboardAnchorPos(searchIndex
)) {
1314 const qreal searchDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(searchIndex
));
1315 if (searchDiff
< minDiff
) {
1316 minDiff
= searchDiff
;
1317 previousRowIndex
= searchIndex
;
1321 return previousRowIndex
;
1324 qreal
KItemListController::keyboardAnchorPos(int index
) const
1326 const QRectF itemRect
= m_view
->itemRect(index
);
1327 if (!itemRect
.isEmpty()) {
1328 return (m_view
->scrollOrientation() == Qt::Vertical
) ? itemRect
.x() : itemRect
.y();
1334 void KItemListController::updateExtendedSelectionRegion()
1337 const bool extend
= (m_selectionBehavior
!= MultiSelection
);
1338 KItemListStyleOption option
= m_view
->styleOption();
1339 if (option
.extendedSelectionRegion
!= extend
) {
1340 option
.extendedSelectionRegion
= extend
;
1341 m_view
->setStyleOption(option
);