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
);
64 connect(m_selectionManager
, &KItemListSelectionManager::selectionChanged
,
65 m_keyboardManager
, &KItemListKeyboardSearchManager::slotSelectionChanged
);
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 int KItemListController::indexCloseToMousePressedPosition() const
183 QHashIterator
<KItemListWidget
*, KItemListGroupHeader
*> it(m_view
->m_visibleGroups
);
184 while (it
.hasNext()) {
186 KItemListGroupHeader
*groupHeader
= it
.value();
187 const QPointF mappedToGroup
= groupHeader
->mapFromItem(nullptr, m_pressedMousePos
);
188 if (groupHeader
->contains(mappedToGroup
)) {
189 return it
.key()->index();
195 void KItemListController::setAutoActivationDelay(int delay
)
197 m_autoActivationTimer
->setInterval(delay
);
200 int KItemListController::autoActivationDelay() const
202 return m_autoActivationTimer
->interval();
205 void KItemListController::setSingleClickActivationEnforced(bool singleClick
)
207 m_singleClickActivationEnforced
= singleClick
;
210 bool KItemListController::singleClickActivationEnforced() const
212 return m_singleClickActivationEnforced
;
215 bool KItemListController::keyPressEvent(QKeyEvent
* event
)
217 int index
= m_selectionManager
->currentItem();
218 int key
= event
->key();
220 // Handle the expanding/collapsing of items
221 if (m_view
->supportsItemExpanding() && m_model
->isExpandable(index
)) {
222 if (key
== Qt::Key_Right
) {
223 if (m_model
->setExpanded(index
, true)) {
226 } else if (key
== Qt::Key_Left
) {
227 if (m_model
->setExpanded(index
, false)) {
233 const bool shiftPressed
= event
->modifiers() & Qt::ShiftModifier
;
234 const bool controlPressed
= event
->modifiers() & Qt::ControlModifier
;
235 const bool shiftOrControlPressed
= shiftPressed
|| controlPressed
;
236 const bool navigationPressed
= key
== Qt::Key_Home
|| key
== Qt::Key_End
||
237 key
== Qt::Key_PageUp
|| key
== Qt::Key_PageDown
||
238 key
== Qt::Key_Up
|| key
== Qt::Key_Down
||
239 key
== Qt::Key_Left
|| key
== Qt::Key_Right
;
241 const int itemCount
= m_model
->count();
243 // For horizontal scroll orientation, transform
244 // the arrow keys to simplify the event handling.
245 if (m_view
->scrollOrientation() == Qt::Horizontal
) {
247 case Qt::Key_Up
: key
= Qt::Key_Left
; break;
248 case Qt::Key_Down
: key
= Qt::Key_Right
; break;
249 case Qt::Key_Left
: key
= Qt::Key_Up
; break;
250 case Qt::Key_Right
: key
= Qt::Key_Down
; break;
255 const bool selectSingleItem
= m_selectionBehavior
!= NoSelection
&& itemCount
== 1 && navigationPressed
;
257 if (selectSingleItem
) {
258 const int current
= m_selectionManager
->currentItem();
259 m_selectionManager
->setSelected(current
);
266 m_keyboardAnchorIndex
= index
;
267 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
271 index
= itemCount
- 1;
272 m_keyboardAnchorIndex
= index
;
273 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
278 const int expandedParentsCount
= m_model
->expandedParentsCount(index
);
279 if (expandedParentsCount
== 0) {
282 // Go to the parent of the current item.
285 } while (index
> 0 && m_model
->expandedParentsCount(index
) == expandedParentsCount
);
287 m_keyboardAnchorIndex
= index
;
288 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
293 if (index
< itemCount
- 1) {
295 m_keyboardAnchorIndex
= index
;
296 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
301 updateKeyboardAnchor();
302 index
= previousRowIndex(index
);
306 updateKeyboardAnchor();
307 index
= nextRowIndex(index
);
311 if (m_view
->scrollOrientation() == Qt::Horizontal
) {
312 // The new current index should correspond to the first item in the current column.
313 int newIndex
= qMax(index
- 1, 0);
314 while (newIndex
!= index
&& m_view
->itemRect(newIndex
).topLeft().y() < m_view
->itemRect(index
).topLeft().y()) {
316 newIndex
= qMax(index
- 1, 0);
318 m_keyboardAnchorIndex
= index
;
319 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
321 const qreal currentItemBottom
= m_view
->itemRect(index
).bottomLeft().y();
322 const qreal height
= m_view
->geometry().height();
324 // The new current item should be the first item in the current
325 // column whose itemRect's top coordinate is larger than targetY.
326 const qreal targetY
= currentItemBottom
- height
;
328 updateKeyboardAnchor();
329 int newIndex
= previousRowIndex(index
);
332 updateKeyboardAnchor();
333 newIndex
= previousRowIndex(index
);
334 } while (m_view
->itemRect(newIndex
).topLeft().y() > targetY
&& newIndex
!= index
);
338 case Qt::Key_PageDown
:
339 if (m_view
->scrollOrientation() == Qt::Horizontal
) {
340 // The new current index should correspond to the last item in the current column.
341 int newIndex
= qMin(index
+ 1, m_model
->count() - 1);
342 while (newIndex
!= index
&& m_view
->itemRect(newIndex
).topLeft().y() > m_view
->itemRect(index
).topLeft().y()) {
344 newIndex
= qMin(index
+ 1, m_model
->count() - 1);
346 m_keyboardAnchorIndex
= index
;
347 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
349 const qreal currentItemTop
= m_view
->itemRect(index
).topLeft().y();
350 const qreal height
= m_view
->geometry().height();
352 // The new current item should be the last item in the current
353 // column whose itemRect's bottom coordinate is smaller than targetY.
354 const qreal targetY
= currentItemTop
+ height
;
356 updateKeyboardAnchor();
357 int newIndex
= nextRowIndex(index
);
360 updateKeyboardAnchor();
361 newIndex
= nextRowIndex(index
);
362 } while (m_view
->itemRect(newIndex
).bottomLeft().y() < targetY
&& newIndex
!= index
);
367 case Qt::Key_Return
: {
368 const KItemSet selectedItems
= m_selectionManager
->selectedItems();
369 if (selectedItems
.count() >= 2) {
370 emit
itemsActivated(selectedItems
);
371 } else if (selectedItems
.count() == 1) {
372 emit
itemActivated(selectedItems
.first());
374 emit
itemActivated(index
);
380 // Emit the signal itemContextMenuRequested() in case if at least one
381 // item is selected. Otherwise the signal viewContextMenuRequested() will be emitted.
382 const KItemSet selectedItems
= m_selectionManager
->selectedItems();
384 if (selectedItems
.count() >= 2) {
385 const int currentItemIndex
= m_selectionManager
->currentItem();
386 index
= selectedItems
.contains(currentItemIndex
)
387 ? currentItemIndex
: selectedItems
.first();
388 } else if (selectedItems
.count() == 1) {
389 index
= selectedItems
.first();
393 const QRectF contextRect
= m_view
->itemContextRect(index
);
394 const QPointF
pos(m_view
->scene()->views().first()->mapToGlobal(contextRect
.bottomRight().toPoint()));
395 emit
itemContextMenuRequested(index
, pos
);
397 emit
viewContextMenuRequested(QCursor::pos());
403 if (m_selectionBehavior
!= SingleSelection
) {
404 m_selectionManager
->clearSelection();
406 m_keyboardManager
->cancelSearch();
407 emit
escapePressed();
411 if (m_selectionBehavior
== MultiSelection
) {
412 if (controlPressed
) {
413 // Toggle the selection state of the current item.
414 m_selectionManager
->endAnchoredSelection();
415 m_selectionManager
->setSelected(index
, 1, KItemListSelectionManager::Toggle
);
416 m_selectionManager
->beginAnchoredSelection(index
);
419 // Select the current item if it is not selected yet.
420 const int current
= m_selectionManager
->currentItem();
421 if (!m_selectionManager
->isSelected(current
)) {
422 m_selectionManager
->setSelected(current
);
427 Q_FALLTHROUGH(); // fall through to the default case and add the Space to the current search string.
429 m_keyboardManager
->addKeys(event
->text());
430 // Make sure unconsumed events get propagated up the chain. #302329
435 if (m_selectionManager
->currentItem() != index
) {
436 switch (m_selectionBehavior
) {
438 m_selectionManager
->setCurrentItem(index
);
441 case SingleSelection
:
442 m_selectionManager
->setCurrentItem(index
);
443 m_selectionManager
->clearSelection();
444 m_selectionManager
->setSelected(index
, 1);
448 if (controlPressed
) {
449 m_selectionManager
->endAnchoredSelection();
452 m_selectionManager
->setCurrentItem(index
);
454 if (!shiftOrControlPressed
) {
455 m_selectionManager
->clearSelection();
456 m_selectionManager
->setSelected(index
, 1);
460 m_selectionManager
->beginAnchoredSelection(index
);
466 if (navigationPressed
) {
467 m_view
->scrollToItem(index
);
472 void KItemListController::slotChangeCurrentItem(const QString
& text
, bool searchFromNextItem
)
474 if (!m_model
|| m_model
->count() == 0) {
478 if (searchFromNextItem
) {
479 const int currentIndex
= m_selectionManager
->currentItem();
480 index
= m_model
->indexForKeyboardSearch(text
, (currentIndex
+ 1) % m_model
->count());
482 index
= m_model
->indexForKeyboardSearch(text
, 0);
485 m_selectionManager
->setCurrentItem(index
);
487 if (m_selectionBehavior
!= NoSelection
) {
488 m_selectionManager
->replaceSelection(index
);
489 m_selectionManager
->beginAnchoredSelection(index
);
492 m_view
->scrollToItem(index
);
496 void KItemListController::slotAutoActivationTimeout()
498 if (!m_model
|| !m_view
) {
502 const int index
= m_autoActivationTimer
->property("index").toInt();
503 if (index
< 0 || index
>= m_model
->count()) {
507 /* m_view->isUnderMouse() fixes a bug in the Folder-View-Panel and in the
510 * Bug: When you drag a file onto a Folder-View-Item or a Places-Item and
511 * then move away before the auto-activation timeout triggers, than the
512 * item still becomes activated/expanded.
514 * See Bug 293200 and 305783
516 if (m_model
->supportsDropping(index
) && m_view
->isUnderMouse()) {
517 if (m_view
->supportsItemExpanding() && m_model
->isExpandable(index
)) {
518 const bool expanded
= m_model
->isExpanded(index
);
519 m_model
->setExpanded(index
, !expanded
);
520 } else if (m_autoActivationBehavior
!= ExpansionOnly
) {
521 emit
itemActivated(index
);
526 bool KItemListController::inputMethodEvent(QInputMethodEvent
* event
)
532 bool KItemListController::mousePressEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
538 m_pressedMousePos
= transform
.map(event
->pos());
539 m_pressedIndex
= m_view
->itemAt(m_pressedMousePos
);
540 emit
mouseButtonPressed(m_pressedIndex
, event
->buttons());
542 if (event
->buttons() & (Qt::BackButton
| Qt::ForwardButton
)) {
543 // Do not select items when clicking the back/forward buttons, see
544 // https://bugs.kde.org/show_bug.cgi?id=327412.
548 if (m_view
->isAboveExpansionToggle(m_pressedIndex
, m_pressedMousePos
)) {
549 m_selectionManager
->endAnchoredSelection();
550 m_selectionManager
->setCurrentItem(m_pressedIndex
);
551 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
555 m_selectionTogglePressed
= m_view
->isAboveSelectionToggle(m_pressedIndex
, m_pressedMousePos
);
556 if (m_selectionTogglePressed
) {
557 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
558 // The previous anchored selection has been finished already in
559 // KItemListSelectionManager::setSelected(). We can safely change
560 // the current item and start a new anchored selection now.
561 m_selectionManager
->setCurrentItem(m_pressedIndex
);
562 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
566 const bool shiftPressed
= event
->modifiers() & Qt::ShiftModifier
;
567 const bool controlPressed
= event
->modifiers() & Qt::ControlModifier
;
569 // The previous selection is cleared if either
570 // 1. The selection mode is SingleSelection, or
571 // 2. the selection mode is MultiSelection, and *none* of the following conditions are met:
572 // a) Shift or Control are pressed.
573 // b) The clicked item is selected already. In that case, the user might want to:
574 // - start dragging multiple items, or
575 // - open the context menu and perform an action for all selected items.
576 const bool shiftOrControlPressed
= shiftPressed
|| controlPressed
;
577 const bool pressedItemAlreadySelected
= m_pressedIndex
>= 0 && m_selectionManager
->isSelected(m_pressedIndex
);
578 const bool clearSelection
= m_selectionBehavior
== SingleSelection
||
579 (!shiftOrControlPressed
&& !pressedItemAlreadySelected
);
580 if (clearSelection
) {
581 m_selectionManager
->clearSelection();
582 } else if (pressedItemAlreadySelected
&& !shiftOrControlPressed
&& (event
->buttons() & Qt::LeftButton
)) {
583 // The user might want to start dragging multiple items, but if he clicks the item
584 // in order to trigger it instead, the other selected items must be deselected.
585 // However, we do not know yet what the user is going to do.
586 // -> remember that the user pressed an item which had been selected already and
587 // clear the selection in mouseReleaseEvent(), unless the items are dragged.
588 m_clearSelectionIfItemsAreNotDragged
= true;
590 if (m_selectionManager
->selectedItems().count() == 1 && m_view
->isAboveText(m_pressedIndex
, m_pressedMousePos
)) {
591 emit
selectedItemTextPressed(m_pressedIndex
);
596 // Finish the anchored selection before the current index is changed
597 m_selectionManager
->endAnchoredSelection();
600 if (event
->buttons() & Qt::RightButton
) {
601 // Stop rubber band from persisting after right-clicks
602 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
603 if (rubberBand
->isActive()) {
604 disconnect(rubberBand
, &KItemListRubberBand::endPositionChanged
, this, &KItemListController::slotRubberBandChanged
);
605 rubberBand
->setActive(false);
606 m_view
->setAutoScroll(false);
610 if (m_pressedIndex
>= 0) {
611 m_selectionManager
->setCurrentItem(m_pressedIndex
);
613 switch (m_selectionBehavior
) {
617 case SingleSelection
:
618 m_selectionManager
->setSelected(m_pressedIndex
);
622 if (controlPressed
&& !shiftPressed
) {
623 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
624 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
625 } else if (!shiftPressed
|| !m_selectionManager
->isAnchoredSelectionActive()) {
626 // Select the pressed item and start a new anchored selection
627 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Select
);
628 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
637 if (event
->buttons() & Qt::RightButton
) {
638 emit
itemContextMenuRequested(m_pressedIndex
, event
->screenPos());
644 if (event
->buttons() & Qt::RightButton
) {
645 const QRectF headerBounds
= m_view
->headerBoundaries();
646 if (headerBounds
.contains(event
->pos())) {
647 emit
headerContextMenuRequested(event
->screenPos());
649 emit
viewContextMenuRequested(event
->screenPos());
654 if (m_selectionBehavior
== MultiSelection
) {
655 QPointF startPos
= m_pressedMousePos
;
656 if (m_view
->scrollOrientation() == Qt::Vertical
) {
657 startPos
.ry() += m_view
->scrollOffset();
658 if (m_view
->itemSize().width() < 0) {
659 // Use a special rubberband for views that have only one column and
660 // expand the rubberband to use the whole width of the view.
664 startPos
.rx() += m_view
->scrollOffset();
667 m_oldSelection
= m_selectionManager
->selectedItems();
668 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
669 rubberBand
->setStartPosition(startPos
);
670 rubberBand
->setEndPosition(startPos
);
671 rubberBand
->setActive(true);
672 connect(rubberBand
, &KItemListRubberBand::endPositionChanged
, this, &KItemListController::slotRubberBandChanged
);
673 m_view
->setAutoScroll(true);
679 bool KItemListController::mouseMoveEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
685 if (m_pressedIndex
>= 0) {
686 // Check whether a dragging should be started
687 if (event
->buttons() & Qt::LeftButton
) {
688 const QPointF pos
= transform
.map(event
->pos());
689 if ((pos
- m_pressedMousePos
).manhattanLength() >= QApplication::startDragDistance()) {
690 if (!m_selectionManager
->isSelected(m_pressedIndex
)) {
691 // Always assure that the dragged item gets selected. Usually this is already
692 // done on the mouse-press event, but when using the selection-toggle on a
693 // selected item the dragged item is not selected yet.
694 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
696 // A selected item has been clicked to drag all selected items
697 // -> the selection should not be cleared when the mouse button is released.
698 m_clearSelectionIfItemsAreNotDragged
= false;
705 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
706 if (rubberBand
->isActive()) {
707 QPointF endPos
= transform
.map(event
->pos());
709 // Update the current item.
710 const int newCurrent
= m_view
->itemAt(endPos
);
711 if (newCurrent
>= 0) {
712 // It's expected that the new current index is also the new anchor (bug 163451).
713 m_selectionManager
->endAnchoredSelection();
714 m_selectionManager
->setCurrentItem(newCurrent
);
715 m_selectionManager
->beginAnchoredSelection(newCurrent
);
718 if (m_view
->scrollOrientation() == Qt::Vertical
) {
719 endPos
.ry() += m_view
->scrollOffset();
720 if (m_view
->itemSize().width() < 0) {
721 // Use a special rubberband for views that have only one column and
722 // expand the rubberband to use the whole width of the view.
723 endPos
.setX(m_view
->size().width());
726 endPos
.rx() += m_view
->scrollOffset();
728 rubberBand
->setEndPosition(endPos
);
735 bool KItemListController::mouseReleaseEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
741 emit
mouseButtonReleased(m_pressedIndex
, event
->buttons());
743 const bool isAboveSelectionToggle
= m_view
->isAboveSelectionToggle(m_pressedIndex
, m_pressedMousePos
);
744 if (isAboveSelectionToggle
) {
745 m_selectionTogglePressed
= false;
749 if (!isAboveSelectionToggle
&& m_selectionTogglePressed
) {
750 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
751 m_selectionTogglePressed
= false;
755 const bool shiftOrControlPressed
= event
->modifiers() & Qt::ShiftModifier
||
756 event
->modifiers() & Qt::ControlModifier
;
758 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
759 if (rubberBand
->isActive()) {
760 disconnect(rubberBand
, &KItemListRubberBand::endPositionChanged
, this, &KItemListController::slotRubberBandChanged
);
761 rubberBand
->setActive(false);
762 m_oldSelection
.clear();
763 m_view
->setAutoScroll(false);
766 const QPointF pos
= transform
.map(event
->pos());
767 const int index
= m_view
->itemAt(pos
);
769 if (index
>= 0 && index
== m_pressedIndex
) {
770 // The release event is done above the same item as the press event
772 if (m_clearSelectionIfItemsAreNotDragged
) {
773 // A selected item has been clicked, but no drag operation has been started
774 // -> clear the rest of the selection.
775 m_selectionManager
->clearSelection();
776 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Select
);
777 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
780 if (event
->button() & Qt::LeftButton
) {
781 bool emitItemActivated
= true;
782 if (m_view
->isAboveExpansionToggle(index
, pos
)) {
783 const bool expanded
= m_model
->isExpanded(index
);
784 m_model
->setExpanded(index
, !expanded
);
786 emit
itemExpansionToggleClicked(index
);
787 emitItemActivated
= false;
788 } else if (shiftOrControlPressed
) {
789 // The mouse click should only update the selection, not trigger the item
790 emitItemActivated
= false;
791 } else if (!(m_view
->style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick
) || m_singleClickActivationEnforced
)) {
792 emitItemActivated
= false;
794 if (emitItemActivated
) {
795 emit
itemActivated(index
);
797 } else if (event
->button() & Qt::MidButton
) {
798 emit
itemMiddleClicked(index
);
802 m_pressedMousePos
= QPointF();
804 m_clearSelectionIfItemsAreNotDragged
= false;
808 bool KItemListController::mouseDoubleClickEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
810 const QPointF pos
= transform
.map(event
->pos());
811 const int index
= m_view
->itemAt(pos
);
813 // Expand item if desired - See Bug 295573
814 if (m_mouseDoubleClickAction
!= ActivateItemOnly
) {
815 if (m_view
&& m_model
&& m_view
->supportsItemExpanding() && m_model
->isExpandable(index
)) {
816 const bool expanded
= m_model
->isExpanded(index
);
817 m_model
->setExpanded(index
, !expanded
);
821 if (event
->button() & Qt::RightButton
) {
822 m_selectionManager
->clearSelection();
824 m_selectionManager
->setSelected(index
);
825 emit
itemContextMenuRequested(index
, event
->screenPos());
827 const QRectF headerBounds
= m_view
->headerBoundaries();
828 if (headerBounds
.contains(event
->pos())) {
829 emit
headerContextMenuRequested(event
->screenPos());
831 emit
viewContextMenuRequested(event
->screenPos());
837 bool emitItemActivated
= !(m_view
->style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick
) || m_singleClickActivationEnforced
) &&
838 (event
->button() & Qt::LeftButton
) &&
839 index
>= 0 && index
< m_model
->count();
840 if (emitItemActivated
) {
841 emit
itemActivated(index
);
846 bool KItemListController::dragEnterEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
851 DragAndDropHelper::clearUrlListMatchesUrlCache();
856 bool KItemListController::dragLeaveEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
861 m_autoActivationTimer
->stop();
862 m_view
->setAutoScroll(false);
863 m_view
->hideDropIndicator();
865 KItemListWidget
* widget
= hoveredWidget();
867 widget
->setHovered(false);
868 emit
itemUnhovered(widget
->index());
873 bool KItemListController::dragMoveEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
875 if (!m_model
|| !m_view
) {
880 QUrl hoveredDir
= m_model
->directory();
881 KItemListWidget
* oldHoveredWidget
= hoveredWidget();
883 const QPointF pos
= transform
.map(event
->pos());
884 KItemListWidget
* newHoveredWidget
= widgetForPos(pos
);
886 if (oldHoveredWidget
!= newHoveredWidget
) {
887 m_autoActivationTimer
->stop();
889 if (oldHoveredWidget
) {
890 oldHoveredWidget
->setHovered(false);
891 emit
itemUnhovered(oldHoveredWidget
->index());
895 if (newHoveredWidget
) {
896 bool droppingBetweenItems
= false;
897 if (m_model
->sortRole().isEmpty()) {
898 // The model supports inserting items between other items.
899 droppingBetweenItems
= (m_view
->showDropIndicator(pos
) >= 0);
902 const int index
= newHoveredWidget
->index();
904 if (m_model
->isDir(index
)) {
905 hoveredDir
= m_model
->url(index
);
908 if (!droppingBetweenItems
) {
909 if (m_model
->supportsDropping(index
)) {
910 // Something has been dragged on an item.
911 m_view
->hideDropIndicator();
912 if (!newHoveredWidget
->isHovered()) {
913 newHoveredWidget
->setHovered(true);
914 emit
itemHovered(index
);
917 if (!m_autoActivationTimer
->isActive() && m_autoActivationTimer
->interval() >= 0) {
918 m_autoActivationTimer
->setProperty("index", index
);
919 m_autoActivationTimer
->start();
923 m_autoActivationTimer
->stop();
924 if (newHoveredWidget
&& newHoveredWidget
->isHovered()) {
925 newHoveredWidget
->setHovered(false);
926 emit
itemUnhovered(index
);
930 m_view
->hideDropIndicator();
933 event
->setAccepted(!DragAndDropHelper::urlListMatchesUrl(event
->mimeData()->urls(), hoveredDir
));
938 bool KItemListController::dropEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
944 m_autoActivationTimer
->stop();
945 m_view
->setAutoScroll(false);
947 const QPointF pos
= transform
.map(event
->pos());
949 int dropAboveIndex
= -1;
950 if (m_model
->sortRole().isEmpty()) {
951 // The model supports inserting of items between other items.
952 dropAboveIndex
= m_view
->showDropIndicator(pos
);
955 if (dropAboveIndex
>= 0) {
956 // Something has been dropped between two items.
957 m_view
->hideDropIndicator();
958 emit
aboveItemDropEvent(dropAboveIndex
, event
);
959 } else if (!event
->mimeData()->hasFormat(m_model
->blacklistItemDropEventMimeType())) {
960 // Something has been dropped on an item or on an empty part of the view.
961 emit
itemDropEvent(m_view
->itemAt(pos
), event
);
964 QAccessibleEvent
accessibilityEvent(view(), QAccessible::DragDropEnd
);
965 QAccessible::updateAccessibility(&accessibilityEvent
);
970 bool KItemListController::hoverEnterEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
977 bool KItemListController::hoverMoveEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
980 if (!m_model
|| !m_view
) {
984 KItemListWidget
* oldHoveredWidget
= hoveredWidget();
985 const QPointF pos
= transform
.map(event
->pos());
986 KItemListWidget
* newHoveredWidget
= widgetForPos(pos
);
988 if (oldHoveredWidget
!= newHoveredWidget
) {
989 if (oldHoveredWidget
) {
990 oldHoveredWidget
->setHovered(false);
991 emit
itemUnhovered(oldHoveredWidget
->index());
994 if (newHoveredWidget
) {
995 newHoveredWidget
->setHovered(true);
996 const QPointF mappedPos
= newHoveredWidget
->mapFromItem(m_view
, pos
);
997 newHoveredWidget
->setHoverPosition(mappedPos
);
998 emit
itemHovered(newHoveredWidget
->index());
1000 } else if (oldHoveredWidget
) {
1001 const QPointF mappedPos
= oldHoveredWidget
->mapFromItem(m_view
, pos
);
1002 oldHoveredWidget
->setHoverPosition(mappedPos
);
1008 bool KItemListController::hoverLeaveEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
1011 Q_UNUSED(transform
);
1013 if (!m_model
|| !m_view
) {
1017 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
1018 if (widget
->isHovered()) {
1019 widget
->setHovered(false);
1020 emit
itemUnhovered(widget
->index());
1026 bool KItemListController::wheelEvent(QGraphicsSceneWheelEvent
* event
, const QTransform
& transform
)
1029 Q_UNUSED(transform
);
1033 bool KItemListController::resizeEvent(QGraphicsSceneResizeEvent
* event
, const QTransform
& transform
)
1036 Q_UNUSED(transform
);
1040 bool KItemListController::processEvent(QEvent
* event
, const QTransform
& transform
)
1046 switch (event
->type()) {
1047 case QEvent::KeyPress
:
1048 return keyPressEvent(static_cast<QKeyEvent
*>(event
));
1049 case QEvent::InputMethod
:
1050 return inputMethodEvent(static_cast<QInputMethodEvent
*>(event
));
1051 case QEvent::GraphicsSceneMousePress
:
1052 return mousePressEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
1053 case QEvent::GraphicsSceneMouseMove
:
1054 return mouseMoveEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
1055 case QEvent::GraphicsSceneMouseRelease
:
1056 return mouseReleaseEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
1057 case QEvent::GraphicsSceneMouseDoubleClick
:
1058 return mouseDoubleClickEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
1059 case QEvent::GraphicsSceneWheel
:
1060 return wheelEvent(static_cast<QGraphicsSceneWheelEvent
*>(event
), QTransform());
1061 case QEvent::GraphicsSceneDragEnter
:
1062 return dragEnterEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
1063 case QEvent::GraphicsSceneDragLeave
:
1064 return dragLeaveEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
1065 case QEvent::GraphicsSceneDragMove
:
1066 return dragMoveEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
1067 case QEvent::GraphicsSceneDrop
:
1068 return dropEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
1069 case QEvent::GraphicsSceneHoverEnter
:
1070 return hoverEnterEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
1071 case QEvent::GraphicsSceneHoverMove
:
1072 return hoverMoveEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
1073 case QEvent::GraphicsSceneHoverLeave
:
1074 return hoverLeaveEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
1075 case QEvent::GraphicsSceneResize
:
1076 return resizeEvent(static_cast<QGraphicsSceneResizeEvent
*>(event
), transform
);
1084 void KItemListController::slotViewScrollOffsetChanged(qreal current
, qreal previous
)
1090 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
1091 if (rubberBand
->isActive()) {
1092 const qreal diff
= current
- previous
;
1093 // TODO: Ideally just QCursor::pos() should be used as
1094 // new end-position but it seems there is no easy way
1095 // to have something like QWidget::mapFromGlobal() for QGraphicsWidget
1096 // (... or I just missed an easy way to do the mapping)
1097 QPointF endPos
= rubberBand
->endPosition();
1098 if (m_view
->scrollOrientation() == Qt::Vertical
) {
1099 endPos
.ry() += diff
;
1101 endPos
.rx() += diff
;
1104 rubberBand
->setEndPosition(endPos
);
1108 void KItemListController::slotRubberBandChanged()
1110 if (!m_view
|| !m_model
|| m_model
->count() <= 0) {
1114 const KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
1115 const QPointF startPos
= rubberBand
->startPosition();
1116 const QPointF endPos
= rubberBand
->endPosition();
1117 QRectF rubberBandRect
= QRectF(startPos
, endPos
).normalized();
1119 const bool scrollVertical
= (m_view
->scrollOrientation() == Qt::Vertical
);
1120 if (scrollVertical
) {
1121 rubberBandRect
.translate(0, -m_view
->scrollOffset());
1123 rubberBandRect
.translate(-m_view
->scrollOffset(), 0);
1126 if (!m_oldSelection
.isEmpty()) {
1127 // Clear the old selection that was available before the rubberband has
1128 // been activated in case if no Shift- or Control-key are pressed
1129 const bool shiftOrControlPressed
= QApplication::keyboardModifiers() & Qt::ShiftModifier
||
1130 QApplication::keyboardModifiers() & Qt::ControlModifier
;
1131 if (!shiftOrControlPressed
) {
1132 m_oldSelection
.clear();
1136 KItemSet selectedItems
;
1138 // Select all visible items that intersect with the rubberband
1139 foreach (const KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
1140 const int index
= widget
->index();
1142 const QRectF widgetRect
= m_view
->itemRect(index
);
1143 if (widgetRect
.intersects(rubberBandRect
)) {
1144 const QRectF iconRect
= widget
->iconRect().translated(widgetRect
.topLeft());
1145 const QRectF textRect
= widget
->textRect().translated(widgetRect
.topLeft());
1146 if (iconRect
.intersects(rubberBandRect
) || textRect
.intersects(rubberBandRect
)) {
1147 selectedItems
.insert(index
);
1152 // Select all invisible items that intersect with the rubberband. Instead of
1153 // iterating all items only the area which might be touched by the rubberband
1155 const bool increaseIndex
= scrollVertical
?
1156 startPos
.y() > endPos
.y(): startPos
.x() > endPos
.x();
1158 int index
= increaseIndex
? m_view
->lastVisibleIndex() + 1 : m_view
->firstVisibleIndex() - 1;
1159 bool selectionFinished
= false;
1161 const QRectF widgetRect
= m_view
->itemRect(index
);
1162 if (widgetRect
.intersects(rubberBandRect
)) {
1163 selectedItems
.insert(index
);
1166 if (increaseIndex
) {
1168 selectionFinished
= (index
>= m_model
->count()) ||
1169 ( scrollVertical
&& widgetRect
.top() > rubberBandRect
.bottom()) ||
1170 (!scrollVertical
&& widgetRect
.left() > rubberBandRect
.right());
1173 selectionFinished
= (index
< 0) ||
1174 ( scrollVertical
&& widgetRect
.bottom() < rubberBandRect
.top()) ||
1175 (!scrollVertical
&& widgetRect
.right() < rubberBandRect
.left());
1177 } while (!selectionFinished
);
1179 if (QApplication::keyboardModifiers() & Qt::ControlModifier
) {
1180 // If Control is pressed, the selection state of all items in the rubberband is toggled.
1181 // Therefore, the new selection contains:
1182 // 1. All previously selected items which are not inside the rubberband, and
1183 // 2. all items inside the rubberband which have not been selected previously.
1184 m_selectionManager
->setSelectedItems(m_oldSelection
^ selectedItems
);
1187 m_selectionManager
->setSelectedItems(selectedItems
+ m_oldSelection
);
1191 void KItemListController::startDragging()
1193 if (!m_view
|| !m_model
) {
1197 const KItemSet selectedItems
= m_selectionManager
->selectedItems();
1198 if (selectedItems
.isEmpty()) {
1202 QMimeData
* data
= m_model
->createMimeData(selectedItems
);
1207 // The created drag object will be owned and deleted
1208 // by QApplication::activeWindow().
1209 QDrag
* drag
= new QDrag(QApplication::activeWindow());
1210 drag
->setMimeData(data
);
1212 const QPixmap pixmap
= m_view
->createDragPixmap(selectedItems
);
1213 drag
->setPixmap(pixmap
);
1215 const QPoint
hotSpot((pixmap
.width() / pixmap
.devicePixelRatio()) / 2, 0);
1216 drag
->setHotSpot(hotSpot
);
1218 drag
->exec(Qt::MoveAction
| Qt::CopyAction
| Qt::LinkAction
, Qt::CopyAction
);
1220 QAccessibleEvent
accessibilityEvent(view(), QAccessible::DragDropStart
);
1221 QAccessible::updateAccessibility(&accessibilityEvent
);
1224 KItemListWidget
* KItemListController::hoveredWidget() const
1228 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
1229 if (widget
->isHovered()) {
1237 KItemListWidget
* KItemListController::widgetForPos(const QPointF
& pos
) const
1241 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
1242 const QPointF mappedPos
= widget
->mapFromItem(m_view
, pos
);
1244 const bool hovered
= widget
->contains(mappedPos
) &&
1245 !widget
->expansionToggleRect().contains(mappedPos
);
1254 void KItemListController::updateKeyboardAnchor()
1256 const bool validAnchor
= m_keyboardAnchorIndex
>= 0 &&
1257 m_keyboardAnchorIndex
< m_model
->count() &&
1258 keyboardAnchorPos(m_keyboardAnchorIndex
) == m_keyboardAnchorPos
;
1260 const int index
= m_selectionManager
->currentItem();
1261 m_keyboardAnchorIndex
= index
;
1262 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
1266 int KItemListController::nextRowIndex(int index
) const
1268 if (m_keyboardAnchorIndex
< 0) {
1272 const int maxIndex
= m_model
->count() - 1;
1273 if (index
== maxIndex
) {
1277 // Calculate the index of the last column inside the row of the current index
1278 int lastColumnIndex
= index
;
1279 while (keyboardAnchorPos(lastColumnIndex
+ 1) > keyboardAnchorPos(lastColumnIndex
)) {
1281 if (lastColumnIndex
>= maxIndex
) {
1286 // Based on the last column index go to the next row and calculate the nearest index
1287 // that is below the current index
1288 int nextRowIndex
= lastColumnIndex
+ 1;
1289 int searchIndex
= nextRowIndex
;
1290 qreal minDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(nextRowIndex
));
1291 while (searchIndex
< maxIndex
&& keyboardAnchorPos(searchIndex
+ 1) > keyboardAnchorPos(searchIndex
)) {
1293 const qreal searchDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(searchIndex
));
1294 if (searchDiff
< minDiff
) {
1295 minDiff
= searchDiff
;
1296 nextRowIndex
= searchIndex
;
1300 return nextRowIndex
;
1303 int KItemListController::previousRowIndex(int index
) const
1305 if (m_keyboardAnchorIndex
< 0 || index
== 0) {
1309 // Calculate the index of the first column inside the row of the current index
1310 int firstColumnIndex
= index
;
1311 while (keyboardAnchorPos(firstColumnIndex
- 1) < keyboardAnchorPos(firstColumnIndex
)) {
1313 if (firstColumnIndex
<= 0) {
1318 // Based on the first column index go to the previous row and calculate the nearest index
1319 // that is above the current index
1320 int previousRowIndex
= firstColumnIndex
- 1;
1321 int searchIndex
= previousRowIndex
;
1322 qreal minDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(previousRowIndex
));
1323 while (searchIndex
> 0 && keyboardAnchorPos(searchIndex
- 1) < keyboardAnchorPos(searchIndex
)) {
1325 const qreal searchDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(searchIndex
));
1326 if (searchDiff
< minDiff
) {
1327 minDiff
= searchDiff
;
1328 previousRowIndex
= searchIndex
;
1332 return previousRowIndex
;
1335 qreal
KItemListController::keyboardAnchorPos(int index
) const
1337 const QRectF itemRect
= m_view
->itemRect(index
);
1338 if (!itemRect
.isEmpty()) {
1339 return (m_view
->scrollOrientation() == Qt::Vertical
) ? itemRect
.x() : itemRect
.y();
1345 void KItemListController::updateExtendedSelectionRegion()
1348 const bool extend
= (m_selectionBehavior
!= MultiSelection
);
1349 KItemListStyleOption option
= m_view
->styleOption();
1350 if (option
.extendedSelectionRegion
!= extend
) {
1351 option
.extendedSelectionRegion
= extend
;
1352 m_view
->setStyleOption(option
);