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::showEvent(QShowEvent
* event
)
219 bool KItemListController::hideEvent(QHideEvent
* event
)
225 bool KItemListController::keyPressEvent(QKeyEvent
* event
)
227 int index
= m_selectionManager
->currentItem();
228 int key
= event
->key();
230 // Handle the expanding/collapsing of items
231 if (m_view
->supportsItemExpanding() && m_model
->isExpandable(index
)) {
232 if (key
== Qt::Key_Right
) {
233 if (m_model
->setExpanded(index
, true)) {
236 } else if (key
== Qt::Key_Left
) {
237 if (m_model
->setExpanded(index
, false)) {
243 const bool shiftPressed
= event
->modifiers() & Qt::ShiftModifier
;
244 const bool controlPressed
= event
->modifiers() & Qt::ControlModifier
;
245 const bool shiftOrControlPressed
= shiftPressed
|| controlPressed
;
247 const int itemCount
= m_model
->count();
249 // For horizontal scroll orientation, transform
250 // the arrow keys to simplify the event handling.
251 if (m_view
->scrollOrientation() == Qt::Horizontal
) {
253 case Qt::Key_Up
: key
= Qt::Key_Left
; break;
254 case Qt::Key_Down
: key
= Qt::Key_Right
; break;
255 case Qt::Key_Left
: key
= Qt::Key_Up
; break;
256 case Qt::Key_Right
: key
= Qt::Key_Down
; break;
261 const bool selectSingleItem
= m_selectionBehavior
!= NoSelection
&&
263 (key
== Qt::Key_Home
|| key
== Qt::Key_End
||
264 key
== Qt::Key_Up
|| key
== Qt::Key_Down
||
265 key
== Qt::Key_Left
|| key
== Qt::Key_Right
);
266 if (selectSingleItem
) {
267 const int current
= m_selectionManager
->currentItem();
268 m_selectionManager
->setSelected(current
);
275 m_keyboardAnchorIndex
= index
;
276 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
280 index
= itemCount
- 1;
281 m_keyboardAnchorIndex
= index
;
282 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
287 const int expandedParentsCount
= m_model
->expandedParentsCount(index
);
288 if (expandedParentsCount
== 0) {
291 // Go to the parent of the current item.
294 } while (index
> 0 && m_model
->expandedParentsCount(index
) == expandedParentsCount
);
296 m_keyboardAnchorIndex
= index
;
297 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
302 if (index
< itemCount
- 1) {
304 m_keyboardAnchorIndex
= index
;
305 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
310 updateKeyboardAnchor();
311 index
= previousRowIndex(index
);
315 updateKeyboardAnchor();
316 index
= nextRowIndex(index
);
320 if (m_view
->scrollOrientation() == Qt::Horizontal
) {
321 // The new current index should correspond to the first item in the current column.
322 int newIndex
= qMax(index
- 1, 0);
323 while (newIndex
!= index
&& m_view
->itemRect(newIndex
).topLeft().y() < m_view
->itemRect(index
).topLeft().y()) {
325 newIndex
= qMax(index
- 1, 0);
327 m_keyboardAnchorIndex
= index
;
328 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
330 const qreal currentItemBottom
= m_view
->itemRect(index
).bottomLeft().y();
331 const qreal height
= m_view
->geometry().height();
333 // The new current item should be the first item in the current
334 // column whose itemRect's top coordinate is larger than targetY.
335 const qreal targetY
= currentItemBottom
- height
;
337 updateKeyboardAnchor();
338 int newIndex
= previousRowIndex(index
);
341 updateKeyboardAnchor();
342 newIndex
= previousRowIndex(index
);
343 } while (m_view
->itemRect(newIndex
).topLeft().y() > targetY
&& newIndex
!= index
);
347 case Qt::Key_PageDown
:
348 if (m_view
->scrollOrientation() == Qt::Horizontal
) {
349 // The new current index should correspond to the last item in the current column.
350 int newIndex
= qMin(index
+ 1, m_model
->count() - 1);
351 while (newIndex
!= index
&& m_view
->itemRect(newIndex
).topLeft().y() > m_view
->itemRect(index
).topLeft().y()) {
353 newIndex
= qMin(index
+ 1, m_model
->count() - 1);
355 m_keyboardAnchorIndex
= index
;
356 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
358 const qreal currentItemTop
= m_view
->itemRect(index
).topLeft().y();
359 const qreal height
= m_view
->geometry().height();
361 // The new current item should be the last item in the current
362 // column whose itemRect's bottom coordinate is smaller than targetY.
363 const qreal targetY
= currentItemTop
+ height
;
365 updateKeyboardAnchor();
366 int newIndex
= nextRowIndex(index
);
369 updateKeyboardAnchor();
370 newIndex
= nextRowIndex(index
);
371 } while (m_view
->itemRect(newIndex
).bottomLeft().y() < targetY
&& newIndex
!= index
);
376 case Qt::Key_Return
: {
377 const KItemSet selectedItems
= m_selectionManager
->selectedItems();
378 if (selectedItems
.count() >= 2) {
379 emit
itemsActivated(selectedItems
);
380 } else if (selectedItems
.count() == 1) {
381 emit
itemActivated(selectedItems
.first());
383 emit
itemActivated(index
);
389 // Emit the signal itemContextMenuRequested() in case if at least one
390 // item is selected. Otherwise the signal viewContextMenuRequested() will be emitted.
391 const KItemSet selectedItems
= m_selectionManager
->selectedItems();
393 if (selectedItems
.count() >= 2) {
394 const int currentItemIndex
= m_selectionManager
->currentItem();
395 index
= selectedItems
.contains(currentItemIndex
)
396 ? currentItemIndex
: selectedItems
.first();
397 } else if (selectedItems
.count() == 1) {
398 index
= selectedItems
.first();
402 const QRectF contextRect
= m_view
->itemContextRect(index
);
403 const QPointF
pos(m_view
->scene()->views().first()->mapToGlobal(contextRect
.bottomRight().toPoint()));
404 emit
itemContextMenuRequested(index
, pos
);
406 emit
viewContextMenuRequested(QCursor::pos());
412 if (m_selectionBehavior
!= SingleSelection
) {
413 m_selectionManager
->clearSelection();
415 m_keyboardManager
->cancelSearch();
416 emit
escapePressed();
420 if (m_selectionBehavior
== MultiSelection
) {
421 if (controlPressed
) {
422 // Toggle the selection state of the current item.
423 m_selectionManager
->endAnchoredSelection();
424 m_selectionManager
->setSelected(index
, 1, KItemListSelectionManager::Toggle
);
425 m_selectionManager
->beginAnchoredSelection(index
);
428 // Select the current item if it is not selected yet.
429 const int current
= m_selectionManager
->currentItem();
430 if (!m_selectionManager
->isSelected(current
)) {
431 m_selectionManager
->setSelected(current
);
436 Q_FALLTHROUGH(); // fall through to the default case and add the Space to the current search string.
438 m_keyboardManager
->addKeys(event
->text());
439 // Make sure unconsumed events get propagated up the chain. #302329
444 if (m_selectionManager
->currentItem() != index
) {
445 switch (m_selectionBehavior
) {
447 m_selectionManager
->setCurrentItem(index
);
450 case SingleSelection
:
451 m_selectionManager
->setCurrentItem(index
);
452 m_selectionManager
->clearSelection();
453 m_selectionManager
->setSelected(index
, 1);
457 if (controlPressed
) {
458 m_selectionManager
->endAnchoredSelection();
461 m_selectionManager
->setCurrentItem(index
);
463 if (!shiftOrControlPressed
) {
464 m_selectionManager
->clearSelection();
465 m_selectionManager
->setSelected(index
, 1);
469 m_selectionManager
->beginAnchoredSelection(index
);
474 m_view
->scrollToItem(index
);
479 void KItemListController::slotChangeCurrentItem(const QString
& text
, bool searchFromNextItem
)
481 if (!m_model
|| m_model
->count() == 0) {
484 const int currentIndex
= m_selectionManager
->currentItem();
486 if (searchFromNextItem
) {
487 index
= m_model
->indexForKeyboardSearch(text
, (currentIndex
+ 1) % m_model
->count());
489 index
= m_model
->indexForKeyboardSearch(text
, currentIndex
);
492 m_selectionManager
->setCurrentItem(index
);
494 if (m_selectionBehavior
!= NoSelection
) {
495 m_selectionManager
->clearSelection();
496 m_selectionManager
->setSelected(index
, 1);
497 m_selectionManager
->beginAnchoredSelection(index
);
500 m_view
->scrollToItem(index
);
504 void KItemListController::slotAutoActivationTimeout()
506 if (!m_model
|| !m_view
) {
510 const int index
= m_autoActivationTimer
->property("index").toInt();
511 if (index
< 0 || index
>= m_model
->count()) {
515 /* m_view->isUnderMouse() fixes a bug in the Folder-View-Panel and in the
518 * Bug: When you drag a file onto a Folder-View-Item or a Places-Item and
519 * then move away before the auto-activation timeout triggers, than the
520 * item still becomes activated/expanded.
522 * See Bug 293200 and 305783
524 if (m_model
->supportsDropping(index
) && m_view
->isUnderMouse()) {
525 if (m_view
->supportsItemExpanding() && m_model
->isExpandable(index
)) {
526 const bool expanded
= m_model
->isExpanded(index
);
527 m_model
->setExpanded(index
, !expanded
);
528 } else if (m_autoActivationBehavior
!= ExpansionOnly
) {
529 emit
itemActivated(index
);
534 bool KItemListController::inputMethodEvent(QInputMethodEvent
* event
)
540 bool KItemListController::mousePressEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
546 m_pressedMousePos
= transform
.map(event
->pos());
547 m_pressedIndex
= m_view
->itemAt(m_pressedMousePos
);
548 emit
mouseButtonPressed(m_pressedIndex
, event
->buttons());
550 if (event
->buttons() & (Qt::BackButton
| Qt::ForwardButton
)) {
551 // Do not select items when clicking the back/forward buttons, see
552 // https://bugs.kde.org/show_bug.cgi?id=327412.
556 if (m_view
->isAboveExpansionToggle(m_pressedIndex
, m_pressedMousePos
)) {
557 m_selectionManager
->endAnchoredSelection();
558 m_selectionManager
->setCurrentItem(m_pressedIndex
);
559 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
563 m_selectionTogglePressed
= m_view
->isAboveSelectionToggle(m_pressedIndex
, m_pressedMousePos
);
564 if (m_selectionTogglePressed
) {
565 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
566 // The previous anchored selection has been finished already in
567 // KItemListSelectionManager::setSelected(). We can safely change
568 // the current item and start a new anchored selection now.
569 m_selectionManager
->setCurrentItem(m_pressedIndex
);
570 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
574 const bool shiftPressed
= event
->modifiers() & Qt::ShiftModifier
;
575 const bool controlPressed
= event
->modifiers() & Qt::ControlModifier
;
577 // The previous selection is cleared if either
578 // 1. The selection mode is SingleSelection, or
579 // 2. the selection mode is MultiSelection, and *none* of the following conditions are met:
580 // a) Shift or Control are pressed.
581 // b) The clicked item is selected already. In that case, the user might want to:
582 // - start dragging multiple items, or
583 // - open the context menu and perform an action for all selected items.
584 const bool shiftOrControlPressed
= shiftPressed
|| controlPressed
;
585 const bool pressedItemAlreadySelected
= m_pressedIndex
>= 0 && m_selectionManager
->isSelected(m_pressedIndex
);
586 const bool clearSelection
= m_selectionBehavior
== SingleSelection
||
587 (!shiftOrControlPressed
&& !pressedItemAlreadySelected
);
588 if (clearSelection
) {
589 m_selectionManager
->clearSelection();
590 } else if (pressedItemAlreadySelected
&& !shiftOrControlPressed
&& (event
->buttons() & Qt::LeftButton
)) {
591 // The user might want to start dragging multiple items, but if he clicks the item
592 // in order to trigger it instead, the other selected items must be deselected.
593 // However, we do not know yet what the user is going to do.
594 // -> remember that the user pressed an item which had been selected already and
595 // clear the selection in mouseReleaseEvent(), unless the items are dragged.
596 m_clearSelectionIfItemsAreNotDragged
= true;
598 if (m_selectionManager
->selectedItems().count() == 1 && m_view
->isAboveText(m_pressedIndex
, m_pressedMousePos
)) {
599 emit
selectedItemTextPressed(m_pressedIndex
);
604 // Finish the anchored selection before the current index is changed
605 m_selectionManager
->endAnchoredSelection();
608 if (m_pressedIndex
>= 0) {
609 m_selectionManager
->setCurrentItem(m_pressedIndex
);
611 switch (m_selectionBehavior
) {
615 case SingleSelection
:
616 m_selectionManager
->setSelected(m_pressedIndex
);
620 if (controlPressed
&& !shiftPressed
) {
621 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
622 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
623 } else if (!shiftPressed
|| !m_selectionManager
->isAnchoredSelectionActive()) {
624 // Select the pressed item and start a new anchored selection
625 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Select
);
626 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
635 if (event
->buttons() & Qt::RightButton
) {
636 emit
itemContextMenuRequested(m_pressedIndex
, event
->screenPos());
642 if (event
->buttons() & Qt::RightButton
) {
643 const QRectF headerBounds
= m_view
->headerBoundaries();
644 if (headerBounds
.contains(event
->pos())) {
645 emit
headerContextMenuRequested(event
->screenPos());
647 emit
viewContextMenuRequested(event
->screenPos());
652 if (m_selectionBehavior
== MultiSelection
) {
653 QPointF startPos
= m_pressedMousePos
;
654 if (m_view
->scrollOrientation() == Qt::Vertical
) {
655 startPos
.ry() += m_view
->scrollOffset();
656 if (m_view
->itemSize().width() < 0) {
657 // Use a special rubberband for views that have only one column and
658 // expand the rubberband to use the whole width of the view.
662 startPos
.rx() += m_view
->scrollOffset();
665 m_oldSelection
= m_selectionManager
->selectedItems();
666 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
667 rubberBand
->setStartPosition(startPos
);
668 rubberBand
->setEndPosition(startPos
);
669 rubberBand
->setActive(true);
670 connect(rubberBand
, &KItemListRubberBand::endPositionChanged
, this, &KItemListController::slotRubberBandChanged
);
671 m_view
->setAutoScroll(true);
677 bool KItemListController::mouseMoveEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
683 if (m_pressedIndex
>= 0) {
684 // Check whether a dragging should be started
685 if (event
->buttons() & Qt::LeftButton
) {
686 const QPointF pos
= transform
.map(event
->pos());
687 if ((pos
- m_pressedMousePos
).manhattanLength() >= QApplication::startDragDistance()) {
688 if (!m_selectionManager
->isSelected(m_pressedIndex
)) {
689 // Always assure that the dragged item gets selected. Usually this is already
690 // done on the mouse-press event, but when using the selection-toggle on a
691 // selected item the dragged item is not selected yet.
692 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
694 // A selected item has been clicked to drag all selected items
695 // -> the selection should not be cleared when the mouse button is released.
696 m_clearSelectionIfItemsAreNotDragged
= false;
703 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
704 if (rubberBand
->isActive()) {
705 QPointF endPos
= transform
.map(event
->pos());
707 // Update the current item.
708 const int newCurrent
= m_view
->itemAt(endPos
);
709 if (newCurrent
>= 0) {
710 // It's expected that the new current index is also the new anchor (bug 163451).
711 m_selectionManager
->endAnchoredSelection();
712 m_selectionManager
->setCurrentItem(newCurrent
);
713 m_selectionManager
->beginAnchoredSelection(newCurrent
);
716 if (m_view
->scrollOrientation() == Qt::Vertical
) {
717 endPos
.ry() += m_view
->scrollOffset();
718 if (m_view
->itemSize().width() < 0) {
719 // Use a special rubberband for views that have only one column and
720 // expand the rubberband to use the whole width of the view.
721 endPos
.setX(m_view
->size().width());
724 endPos
.rx() += m_view
->scrollOffset();
726 rubberBand
->setEndPosition(endPos
);
733 bool KItemListController::mouseReleaseEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
739 emit
mouseButtonReleased(m_pressedIndex
, event
->buttons());
741 const bool isAboveSelectionToggle
= m_view
->isAboveSelectionToggle(m_pressedIndex
, m_pressedMousePos
);
742 if (isAboveSelectionToggle
) {
743 m_selectionTogglePressed
= false;
747 if (!isAboveSelectionToggle
&& m_selectionTogglePressed
) {
748 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
749 m_selectionTogglePressed
= false;
753 const bool shiftOrControlPressed
= event
->modifiers() & Qt::ShiftModifier
||
754 event
->modifiers() & Qt::ControlModifier
;
756 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
757 if (rubberBand
->isActive()) {
758 disconnect(rubberBand
, &KItemListRubberBand::endPositionChanged
, this, &KItemListController::slotRubberBandChanged
);
759 rubberBand
->setActive(false);
760 m_oldSelection
.clear();
761 m_view
->setAutoScroll(false);
764 const QPointF pos
= transform
.map(event
->pos());
765 const int index
= m_view
->itemAt(pos
);
767 if (index
>= 0 && index
== m_pressedIndex
) {
768 // The release event is done above the same item as the press event
770 if (m_clearSelectionIfItemsAreNotDragged
) {
771 // A selected item has been clicked, but no drag operation has been started
772 // -> clear the rest of the selection.
773 m_selectionManager
->clearSelection();
774 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Select
);
775 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
778 if (event
->button() & Qt::LeftButton
) {
779 bool emitItemActivated
= true;
780 if (m_view
->isAboveExpansionToggle(index
, pos
)) {
781 const bool expanded
= m_model
->isExpanded(index
);
782 m_model
->setExpanded(index
, !expanded
);
784 emit
itemExpansionToggleClicked(index
);
785 emitItemActivated
= false;
786 } else if (shiftOrControlPressed
) {
787 // The mouse click should only update the selection, not trigger the item
788 emitItemActivated
= false;
789 } else if (!(m_view
->style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick
) || m_singleClickActivationEnforced
)) {
790 emitItemActivated
= false;
792 if (emitItemActivated
) {
793 emit
itemActivated(index
);
795 } else if (event
->button() & Qt::MidButton
) {
796 emit
itemMiddleClicked(index
);
800 m_pressedMousePos
= QPointF();
802 m_clearSelectionIfItemsAreNotDragged
= false;
806 bool KItemListController::mouseDoubleClickEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
808 const QPointF pos
= transform
.map(event
->pos());
809 const int index
= m_view
->itemAt(pos
);
811 // Expand item if desired - See Bug 295573
812 if (m_mouseDoubleClickAction
!= ActivateItemOnly
) {
813 if (m_view
&& m_model
&& m_view
->supportsItemExpanding() && m_model
->isExpandable(index
)) {
814 const bool expanded
= m_model
->isExpanded(index
);
815 m_model
->setExpanded(index
, !expanded
);
819 if (event
->button() & Qt::RightButton
) {
820 m_selectionManager
->clearSelection();
822 m_selectionManager
->setSelected(index
);
823 emit
itemContextMenuRequested(index
, event
->screenPos());
825 const QRectF headerBounds
= m_view
->headerBoundaries();
826 if (headerBounds
.contains(event
->pos())) {
827 emit
headerContextMenuRequested(event
->screenPos());
829 emit
viewContextMenuRequested(event
->screenPos());
835 bool emitItemActivated
= !(m_view
->style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick
) || m_singleClickActivationEnforced
) &&
836 (event
->button() & Qt::LeftButton
) &&
837 index
>= 0 && index
< m_model
->count();
838 if (emitItemActivated
) {
839 emit
itemActivated(index
);
844 bool KItemListController::dragEnterEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
849 DragAndDropHelper::clearUrlListMatchesUrlCache();
854 bool KItemListController::dragLeaveEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
859 m_autoActivationTimer
->stop();
860 m_view
->setAutoScroll(false);
861 m_view
->hideDropIndicator();
863 KItemListWidget
* widget
= hoveredWidget();
865 widget
->setHovered(false);
866 emit
itemUnhovered(widget
->index());
871 bool KItemListController::dragMoveEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
873 if (!m_model
|| !m_view
) {
878 QUrl hoveredDir
= m_model
->directory();
879 KItemListWidget
* oldHoveredWidget
= hoveredWidget();
881 const QPointF pos
= transform
.map(event
->pos());
882 KItemListWidget
* newHoveredWidget
= widgetForPos(pos
);
884 if (oldHoveredWidget
!= newHoveredWidget
) {
885 m_autoActivationTimer
->stop();
887 if (oldHoveredWidget
) {
888 oldHoveredWidget
->setHovered(false);
889 emit
itemUnhovered(oldHoveredWidget
->index());
893 if (newHoveredWidget
) {
894 bool droppingBetweenItems
= false;
895 if (m_model
->sortRole().isEmpty()) {
896 // The model supports inserting items between other items.
897 droppingBetweenItems
= (m_view
->showDropIndicator(pos
) >= 0);
900 const int index
= newHoveredWidget
->index();
902 if (m_model
->isDir(index
)) {
903 hoveredDir
= m_model
->url(index
);
906 if (!droppingBetweenItems
) {
907 if (m_model
->supportsDropping(index
)) {
908 // Something has been dragged on an item.
909 m_view
->hideDropIndicator();
910 if (!newHoveredWidget
->isHovered()) {
911 newHoveredWidget
->setHovered(true);
912 emit
itemHovered(index
);
915 if (!m_autoActivationTimer
->isActive() && m_autoActivationTimer
->interval() >= 0) {
916 m_autoActivationTimer
->setProperty("index", index
);
917 m_autoActivationTimer
->start();
921 m_autoActivationTimer
->stop();
922 if (newHoveredWidget
&& newHoveredWidget
->isHovered()) {
923 newHoveredWidget
->setHovered(false);
924 emit
itemUnhovered(index
);
928 m_view
->hideDropIndicator();
931 event
->setAccepted(!DragAndDropHelper::urlListMatchesUrl(event
->mimeData()->urls(), hoveredDir
));
936 bool KItemListController::dropEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
942 m_autoActivationTimer
->stop();
943 m_view
->setAutoScroll(false);
945 const QPointF pos
= transform
.map(event
->pos());
947 int dropAboveIndex
= -1;
948 if (m_model
->sortRole().isEmpty()) {
949 // The model supports inserting of items between other items.
950 dropAboveIndex
= m_view
->showDropIndicator(pos
);
953 if (dropAboveIndex
>= 0) {
954 // Something has been dropped between two items.
955 m_view
->hideDropIndicator();
956 emit
aboveItemDropEvent(dropAboveIndex
, event
);
957 } else if (!event
->mimeData()->hasFormat(m_model
->blacklistItemDropEventMimeType())) {
958 // Something has been dropped on an item or on an empty part of the view.
959 emit
itemDropEvent(m_view
->itemAt(pos
), event
);
962 QAccessibleEvent
accessibilityEvent(view(), QAccessible::DragDropEnd
);
963 QAccessible::updateAccessibility(&accessibilityEvent
);
968 bool KItemListController::hoverEnterEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
975 bool KItemListController::hoverMoveEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
978 if (!m_model
|| !m_view
) {
982 KItemListWidget
* oldHoveredWidget
= hoveredWidget();
983 const QPointF pos
= transform
.map(event
->pos());
984 KItemListWidget
* newHoveredWidget
= widgetForPos(pos
);
986 if (oldHoveredWidget
!= newHoveredWidget
) {
987 if (oldHoveredWidget
) {
988 oldHoveredWidget
->setHovered(false);
989 emit
itemUnhovered(oldHoveredWidget
->index());
992 if (newHoveredWidget
) {
993 newHoveredWidget
->setHovered(true);
994 const QPointF mappedPos
= newHoveredWidget
->mapFromItem(m_view
, pos
);
995 newHoveredWidget
->setHoverPosition(mappedPos
);
996 emit
itemHovered(newHoveredWidget
->index());
998 } else if (oldHoveredWidget
) {
999 const QPointF mappedPos
= oldHoveredWidget
->mapFromItem(m_view
, pos
);
1000 oldHoveredWidget
->setHoverPosition(mappedPos
);
1006 bool KItemListController::hoverLeaveEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
1009 Q_UNUSED(transform
);
1011 if (!m_model
|| !m_view
) {
1015 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
1016 if (widget
->isHovered()) {
1017 widget
->setHovered(false);
1018 emit
itemUnhovered(widget
->index());
1024 bool KItemListController::wheelEvent(QGraphicsSceneWheelEvent
* event
, const QTransform
& transform
)
1027 Q_UNUSED(transform
);
1031 bool KItemListController::resizeEvent(QGraphicsSceneResizeEvent
* event
, const QTransform
& transform
)
1034 Q_UNUSED(transform
);
1038 bool KItemListController::processEvent(QEvent
* event
, const QTransform
& transform
)
1044 switch (event
->type()) {
1045 case QEvent::KeyPress
:
1046 return keyPressEvent(static_cast<QKeyEvent
*>(event
));
1047 case QEvent::InputMethod
:
1048 return inputMethodEvent(static_cast<QInputMethodEvent
*>(event
));
1049 case QEvent::GraphicsSceneMousePress
:
1050 return mousePressEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
1051 case QEvent::GraphicsSceneMouseMove
:
1052 return mouseMoveEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
1053 case QEvent::GraphicsSceneMouseRelease
:
1054 return mouseReleaseEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
1055 case QEvent::GraphicsSceneMouseDoubleClick
:
1056 return mouseDoubleClickEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
1057 case QEvent::GraphicsSceneWheel
:
1058 return wheelEvent(static_cast<QGraphicsSceneWheelEvent
*>(event
), QTransform());
1059 case QEvent::GraphicsSceneDragEnter
:
1060 return dragEnterEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
1061 case QEvent::GraphicsSceneDragLeave
:
1062 return dragLeaveEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
1063 case QEvent::GraphicsSceneDragMove
:
1064 return dragMoveEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
1065 case QEvent::GraphicsSceneDrop
:
1066 return dropEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
1067 case QEvent::GraphicsSceneHoverEnter
:
1068 return hoverEnterEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
1069 case QEvent::GraphicsSceneHoverMove
:
1070 return hoverMoveEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
1071 case QEvent::GraphicsSceneHoverLeave
:
1072 return hoverLeaveEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
1073 case QEvent::GraphicsSceneResize
:
1074 return resizeEvent(static_cast<QGraphicsSceneResizeEvent
*>(event
), transform
);
1082 void KItemListController::slotViewScrollOffsetChanged(qreal current
, qreal previous
)
1088 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
1089 if (rubberBand
->isActive()) {
1090 const qreal diff
= current
- previous
;
1091 // TODO: Ideally just QCursor::pos() should be used as
1092 // new end-position but it seems there is no easy way
1093 // to have something like QWidget::mapFromGlobal() for QGraphicsWidget
1094 // (... or I just missed an easy way to do the mapping)
1095 QPointF endPos
= rubberBand
->endPosition();
1096 if (m_view
->scrollOrientation() == Qt::Vertical
) {
1097 endPos
.ry() += diff
;
1099 endPos
.rx() += diff
;
1102 rubberBand
->setEndPosition(endPos
);
1106 void KItemListController::slotRubberBandChanged()
1108 if (!m_view
|| !m_model
|| m_model
->count() <= 0) {
1112 const KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
1113 const QPointF startPos
= rubberBand
->startPosition();
1114 const QPointF endPos
= rubberBand
->endPosition();
1115 QRectF rubberBandRect
= QRectF(startPos
, endPos
).normalized();
1117 const bool scrollVertical
= (m_view
->scrollOrientation() == Qt::Vertical
);
1118 if (scrollVertical
) {
1119 rubberBandRect
.translate(0, -m_view
->scrollOffset());
1121 rubberBandRect
.translate(-m_view
->scrollOffset(), 0);
1124 if (!m_oldSelection
.isEmpty()) {
1125 // Clear the old selection that was available before the rubberband has
1126 // been activated in case if no Shift- or Control-key are pressed
1127 const bool shiftOrControlPressed
= QApplication::keyboardModifiers() & Qt::ShiftModifier
||
1128 QApplication::keyboardModifiers() & Qt::ControlModifier
;
1129 if (!shiftOrControlPressed
) {
1130 m_oldSelection
.clear();
1134 KItemSet selectedItems
;
1136 // Select all visible items that intersect with the rubberband
1137 foreach (const KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
1138 const int index
= widget
->index();
1140 const QRectF widgetRect
= m_view
->itemRect(index
);
1141 if (widgetRect
.intersects(rubberBandRect
)) {
1142 const QRectF iconRect
= widget
->iconRect().translated(widgetRect
.topLeft());
1143 const QRectF textRect
= widget
->textRect().translated(widgetRect
.topLeft());
1144 if (iconRect
.intersects(rubberBandRect
) || textRect
.intersects(rubberBandRect
)) {
1145 selectedItems
.insert(index
);
1150 // Select all invisible items that intersect with the rubberband. Instead of
1151 // iterating all items only the area which might be touched by the rubberband
1153 const bool increaseIndex
= scrollVertical
?
1154 startPos
.y() > endPos
.y(): startPos
.x() > endPos
.x();
1156 int index
= increaseIndex
? m_view
->lastVisibleIndex() + 1 : m_view
->firstVisibleIndex() - 1;
1157 bool selectionFinished
= false;
1159 const QRectF widgetRect
= m_view
->itemRect(index
);
1160 if (widgetRect
.intersects(rubberBandRect
)) {
1161 selectedItems
.insert(index
);
1164 if (increaseIndex
) {
1166 selectionFinished
= (index
>= m_model
->count()) ||
1167 ( scrollVertical
&& widgetRect
.top() > rubberBandRect
.bottom()) ||
1168 (!scrollVertical
&& widgetRect
.left() > rubberBandRect
.right());
1171 selectionFinished
= (index
< 0) ||
1172 ( scrollVertical
&& widgetRect
.bottom() < rubberBandRect
.top()) ||
1173 (!scrollVertical
&& widgetRect
.right() < rubberBandRect
.left());
1175 } while (!selectionFinished
);
1177 if (QApplication::keyboardModifiers() & Qt::ControlModifier
) {
1178 // If Control is pressed, the selection state of all items in the rubberband is toggled.
1179 // Therefore, the new selection contains:
1180 // 1. All previously selected items which are not inside the rubberband, and
1181 // 2. all items inside the rubberband which have not been selected previously.
1182 m_selectionManager
->setSelectedItems(m_oldSelection
^ selectedItems
);
1185 m_selectionManager
->setSelectedItems(selectedItems
+ m_oldSelection
);
1189 void KItemListController::startDragging()
1191 if (!m_view
|| !m_model
) {
1195 const KItemSet selectedItems
= m_selectionManager
->selectedItems();
1196 if (selectedItems
.isEmpty()) {
1200 QMimeData
* data
= m_model
->createMimeData(selectedItems
);
1205 // The created drag object will be owned and deleted
1206 // by QApplication::activeWindow().
1207 QDrag
* drag
= new QDrag(QApplication::activeWindow());
1208 drag
->setMimeData(data
);
1210 const QPixmap pixmap
= m_view
->createDragPixmap(selectedItems
);
1211 drag
->setPixmap(pixmap
);
1213 const QPoint
hotSpot((pixmap
.width() / pixmap
.devicePixelRatio()) / 2, 0);
1214 drag
->setHotSpot(hotSpot
);
1216 drag
->exec(Qt::MoveAction
| Qt::CopyAction
| Qt::LinkAction
, Qt::CopyAction
);
1218 QAccessibleEvent
accessibilityEvent(view(), QAccessible::DragDropStart
);
1219 QAccessible::updateAccessibility(&accessibilityEvent
);
1222 KItemListWidget
* KItemListController::hoveredWidget() const
1226 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
1227 if (widget
->isHovered()) {
1235 KItemListWidget
* KItemListController::widgetForPos(const QPointF
& pos
) const
1239 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
1240 const QPointF mappedPos
= widget
->mapFromItem(m_view
, pos
);
1242 const bool hovered
= widget
->contains(mappedPos
) &&
1243 !widget
->expansionToggleRect().contains(mappedPos
);
1252 void KItemListController::updateKeyboardAnchor()
1254 const bool validAnchor
= m_keyboardAnchorIndex
>= 0 &&
1255 m_keyboardAnchorIndex
< m_model
->count() &&
1256 keyboardAnchorPos(m_keyboardAnchorIndex
) == m_keyboardAnchorPos
;
1258 const int index
= m_selectionManager
->currentItem();
1259 m_keyboardAnchorIndex
= index
;
1260 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
1264 int KItemListController::nextRowIndex(int index
) const
1266 if (m_keyboardAnchorIndex
< 0) {
1270 const int maxIndex
= m_model
->count() - 1;
1271 if (index
== maxIndex
) {
1275 // Calculate the index of the last column inside the row of the current index
1276 int lastColumnIndex
= index
;
1277 while (keyboardAnchorPos(lastColumnIndex
+ 1) > keyboardAnchorPos(lastColumnIndex
)) {
1279 if (lastColumnIndex
>= maxIndex
) {
1284 // Based on the last column index go to the next row and calculate the nearest index
1285 // that is below the current index
1286 int nextRowIndex
= lastColumnIndex
+ 1;
1287 int searchIndex
= nextRowIndex
;
1288 qreal minDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(nextRowIndex
));
1289 while (searchIndex
< maxIndex
&& keyboardAnchorPos(searchIndex
+ 1) > keyboardAnchorPos(searchIndex
)) {
1291 const qreal searchDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(searchIndex
));
1292 if (searchDiff
< minDiff
) {
1293 minDiff
= searchDiff
;
1294 nextRowIndex
= searchIndex
;
1298 return nextRowIndex
;
1301 int KItemListController::previousRowIndex(int index
) const
1303 if (m_keyboardAnchorIndex
< 0 || index
== 0) {
1307 // Calculate the index of the first column inside the row of the current index
1308 int firstColumnIndex
= index
;
1309 while (keyboardAnchorPos(firstColumnIndex
- 1) < keyboardAnchorPos(firstColumnIndex
)) {
1311 if (firstColumnIndex
<= 0) {
1316 // Based on the first column index go to the previous row and calculate the nearest index
1317 // that is above the current index
1318 int previousRowIndex
= firstColumnIndex
- 1;
1319 int searchIndex
= previousRowIndex
;
1320 qreal minDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(previousRowIndex
));
1321 while (searchIndex
> 0 && keyboardAnchorPos(searchIndex
- 1) < keyboardAnchorPos(searchIndex
)) {
1323 const qreal searchDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(searchIndex
));
1324 if (searchDiff
< minDiff
) {
1325 minDiff
= searchDiff
;
1326 previousRowIndex
= searchIndex
;
1330 return previousRowIndex
;
1333 qreal
KItemListController::keyboardAnchorPos(int index
) const
1335 const QRectF itemRect
= m_view
->itemRect(index
);
1336 if (!itemRect
.isEmpty()) {
1337 return (m_view
->scrollOrientation() == Qt::Vertical
) ? itemRect
.x() : itemRect
.y();
1343 void KItemListController::updateExtendedSelectionRegion()
1346 const bool extend
= (m_selectionBehavior
!= MultiSelection
);
1347 KItemListStyleOption option
= m_view
->styleOption();
1348 if (option
.extendedSelectionRegion
!= extend
) {
1349 option
.extendedSelectionRegion
= extend
;
1350 m_view
->setStyleOption(option
);