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_Up
|| key
== Qt::Key_Down
||
236 key
== Qt::Key_Left
|| key
== Qt::Key_Right
;
238 const int itemCount
= m_model
->count();
240 // For horizontal scroll orientation, transform
241 // the arrow keys to simplify the event handling.
242 if (m_view
->scrollOrientation() == Qt::Horizontal
) {
244 case Qt::Key_Up
: key
= Qt::Key_Left
; break;
245 case Qt::Key_Down
: key
= Qt::Key_Right
; break;
246 case Qt::Key_Left
: key
= Qt::Key_Up
; break;
247 case Qt::Key_Right
: key
= Qt::Key_Down
; break;
252 const bool selectSingleItem
= m_selectionBehavior
!= NoSelection
&& itemCount
== 1 && navigationPressed
;
254 if (selectSingleItem
) {
255 const int current
= m_selectionManager
->currentItem();
256 m_selectionManager
->setSelected(current
);
263 m_keyboardAnchorIndex
= index
;
264 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
268 index
= itemCount
- 1;
269 m_keyboardAnchorIndex
= index
;
270 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
275 const int expandedParentsCount
= m_model
->expandedParentsCount(index
);
276 if (expandedParentsCount
== 0) {
279 // Go to the parent of the current item.
282 } while (index
> 0 && m_model
->expandedParentsCount(index
) == expandedParentsCount
);
284 m_keyboardAnchorIndex
= index
;
285 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
290 if (index
< itemCount
- 1) {
292 m_keyboardAnchorIndex
= index
;
293 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
298 updateKeyboardAnchor();
299 index
= previousRowIndex(index
);
303 updateKeyboardAnchor();
304 index
= nextRowIndex(index
);
308 if (m_view
->scrollOrientation() == Qt::Horizontal
) {
309 // The new current index should correspond to the first item in the current column.
310 int newIndex
= qMax(index
- 1, 0);
311 while (newIndex
!= index
&& m_view
->itemRect(newIndex
).topLeft().y() < m_view
->itemRect(index
).topLeft().y()) {
313 newIndex
= qMax(index
- 1, 0);
315 m_keyboardAnchorIndex
= index
;
316 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
318 const qreal currentItemBottom
= m_view
->itemRect(index
).bottomLeft().y();
319 const qreal height
= m_view
->geometry().height();
321 // The new current item should be the first item in the current
322 // column whose itemRect's top coordinate is larger than targetY.
323 const qreal targetY
= currentItemBottom
- height
;
325 updateKeyboardAnchor();
326 int newIndex
= previousRowIndex(index
);
329 updateKeyboardAnchor();
330 newIndex
= previousRowIndex(index
);
331 } while (m_view
->itemRect(newIndex
).topLeft().y() > targetY
&& newIndex
!= index
);
335 case Qt::Key_PageDown
:
336 if (m_view
->scrollOrientation() == Qt::Horizontal
) {
337 // The new current index should correspond to the last item in the current column.
338 int newIndex
= qMin(index
+ 1, m_model
->count() - 1);
339 while (newIndex
!= index
&& m_view
->itemRect(newIndex
).topLeft().y() > m_view
->itemRect(index
).topLeft().y()) {
341 newIndex
= qMin(index
+ 1, m_model
->count() - 1);
343 m_keyboardAnchorIndex
= index
;
344 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
346 const qreal currentItemTop
= m_view
->itemRect(index
).topLeft().y();
347 const qreal height
= m_view
->geometry().height();
349 // The new current item should be the last item in the current
350 // column whose itemRect's bottom coordinate is smaller than targetY.
351 const qreal targetY
= currentItemTop
+ height
;
353 updateKeyboardAnchor();
354 int newIndex
= nextRowIndex(index
);
357 updateKeyboardAnchor();
358 newIndex
= nextRowIndex(index
);
359 } while (m_view
->itemRect(newIndex
).bottomLeft().y() < targetY
&& newIndex
!= index
);
364 case Qt::Key_Return
: {
365 const KItemSet selectedItems
= m_selectionManager
->selectedItems();
366 if (selectedItems
.count() >= 2) {
367 emit
itemsActivated(selectedItems
);
368 } else if (selectedItems
.count() == 1) {
369 emit
itemActivated(selectedItems
.first());
371 emit
itemActivated(index
);
377 // Emit the signal itemContextMenuRequested() in case if at least one
378 // item is selected. Otherwise the signal viewContextMenuRequested() will be emitted.
379 const KItemSet selectedItems
= m_selectionManager
->selectedItems();
381 if (selectedItems
.count() >= 2) {
382 const int currentItemIndex
= m_selectionManager
->currentItem();
383 index
= selectedItems
.contains(currentItemIndex
)
384 ? currentItemIndex
: selectedItems
.first();
385 } else if (selectedItems
.count() == 1) {
386 index
= selectedItems
.first();
390 const QRectF contextRect
= m_view
->itemContextRect(index
);
391 const QPointF
pos(m_view
->scene()->views().first()->mapToGlobal(contextRect
.bottomRight().toPoint()));
392 emit
itemContextMenuRequested(index
, pos
);
394 emit
viewContextMenuRequested(QCursor::pos());
400 if (m_selectionBehavior
!= SingleSelection
) {
401 m_selectionManager
->clearSelection();
403 m_keyboardManager
->cancelSearch();
404 emit
escapePressed();
408 if (m_selectionBehavior
== MultiSelection
) {
409 if (controlPressed
) {
410 // Toggle the selection state of the current item.
411 m_selectionManager
->endAnchoredSelection();
412 m_selectionManager
->setSelected(index
, 1, KItemListSelectionManager::Toggle
);
413 m_selectionManager
->beginAnchoredSelection(index
);
416 // Select the current item if it is not selected yet.
417 const int current
= m_selectionManager
->currentItem();
418 if (!m_selectionManager
->isSelected(current
)) {
419 m_selectionManager
->setSelected(current
);
424 Q_FALLTHROUGH(); // fall through to the default case and add the Space to the current search string.
426 m_keyboardManager
->addKeys(event
->text());
427 // Make sure unconsumed events get propagated up the chain. #302329
432 if (m_selectionManager
->currentItem() != index
) {
433 switch (m_selectionBehavior
) {
435 m_selectionManager
->setCurrentItem(index
);
438 case SingleSelection
:
439 m_selectionManager
->setCurrentItem(index
);
440 m_selectionManager
->clearSelection();
441 m_selectionManager
->setSelected(index
, 1);
445 if (controlPressed
) {
446 m_selectionManager
->endAnchoredSelection();
449 m_selectionManager
->setCurrentItem(index
);
451 if (!shiftOrControlPressed
) {
452 m_selectionManager
->clearSelection();
453 m_selectionManager
->setSelected(index
, 1);
457 m_selectionManager
->beginAnchoredSelection(index
);
463 if (navigationPressed
) {
464 if (index
< m_view
->firstVisibleIndex() || index
> m_view
->lastVisibleIndex()) {
465 m_view
->scrollToItem(index
);
471 void KItemListController::slotChangeCurrentItem(const QString
& text
, bool searchFromNextItem
)
473 if (!m_model
|| m_model
->count() == 0) {
476 const int currentIndex
= m_selectionManager
->currentItem();
478 if (searchFromNextItem
) {
479 index
= m_model
->indexForKeyboardSearch(text
, (currentIndex
+ 1) % m_model
->count());
481 index
= m_model
->indexForKeyboardSearch(text
, currentIndex
);
484 m_selectionManager
->setCurrentItem(index
);
486 if (m_selectionBehavior
!= NoSelection
) {
487 m_selectionManager
->clearSelection();
488 m_selectionManager
->setSelected(index
, 1);
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 (m_pressedIndex
>= 0) {
601 m_selectionManager
->setCurrentItem(m_pressedIndex
);
603 switch (m_selectionBehavior
) {
607 case SingleSelection
:
608 m_selectionManager
->setSelected(m_pressedIndex
);
612 if (controlPressed
&& !shiftPressed
) {
613 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
614 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
615 } else if (!shiftPressed
|| !m_selectionManager
->isAnchoredSelectionActive()) {
616 // Select the pressed item and start a new anchored selection
617 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Select
);
618 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
627 if (event
->buttons() & Qt::RightButton
) {
628 emit
itemContextMenuRequested(m_pressedIndex
, event
->screenPos());
634 if (event
->buttons() & Qt::RightButton
) {
635 const QRectF headerBounds
= m_view
->headerBoundaries();
636 if (headerBounds
.contains(event
->pos())) {
637 emit
headerContextMenuRequested(event
->screenPos());
639 emit
viewContextMenuRequested(event
->screenPos());
644 if (m_selectionBehavior
== MultiSelection
) {
645 QPointF startPos
= m_pressedMousePos
;
646 if (m_view
->scrollOrientation() == Qt::Vertical
) {
647 startPos
.ry() += m_view
->scrollOffset();
648 if (m_view
->itemSize().width() < 0) {
649 // Use a special rubberband for views that have only one column and
650 // expand the rubberband to use the whole width of the view.
654 startPos
.rx() += m_view
->scrollOffset();
657 m_oldSelection
= m_selectionManager
->selectedItems();
658 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
659 rubberBand
->setStartPosition(startPos
);
660 rubberBand
->setEndPosition(startPos
);
661 rubberBand
->setActive(true);
662 connect(rubberBand
, &KItemListRubberBand::endPositionChanged
, this, &KItemListController::slotRubberBandChanged
);
663 m_view
->setAutoScroll(true);
669 bool KItemListController::mouseMoveEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
675 if (m_pressedIndex
>= 0) {
676 // Check whether a dragging should be started
677 if (event
->buttons() & Qt::LeftButton
) {
678 const QPointF pos
= transform
.map(event
->pos());
679 if ((pos
- m_pressedMousePos
).manhattanLength() >= QApplication::startDragDistance()) {
680 if (!m_selectionManager
->isSelected(m_pressedIndex
)) {
681 // Always assure that the dragged item gets selected. Usually this is already
682 // done on the mouse-press event, but when using the selection-toggle on a
683 // selected item the dragged item is not selected yet.
684 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
686 // A selected item has been clicked to drag all selected items
687 // -> the selection should not be cleared when the mouse button is released.
688 m_clearSelectionIfItemsAreNotDragged
= false;
695 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
696 if (rubberBand
->isActive()) {
697 QPointF endPos
= transform
.map(event
->pos());
699 // Update the current item.
700 const int newCurrent
= m_view
->itemAt(endPos
);
701 if (newCurrent
>= 0) {
702 // It's expected that the new current index is also the new anchor (bug 163451).
703 m_selectionManager
->endAnchoredSelection();
704 m_selectionManager
->setCurrentItem(newCurrent
);
705 m_selectionManager
->beginAnchoredSelection(newCurrent
);
708 if (m_view
->scrollOrientation() == Qt::Vertical
) {
709 endPos
.ry() += m_view
->scrollOffset();
710 if (m_view
->itemSize().width() < 0) {
711 // Use a special rubberband for views that have only one column and
712 // expand the rubberband to use the whole width of the view.
713 endPos
.setX(m_view
->size().width());
716 endPos
.rx() += m_view
->scrollOffset();
718 rubberBand
->setEndPosition(endPos
);
725 bool KItemListController::mouseReleaseEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
731 emit
mouseButtonReleased(m_pressedIndex
, event
->buttons());
733 const bool isAboveSelectionToggle
= m_view
->isAboveSelectionToggle(m_pressedIndex
, m_pressedMousePos
);
734 if (isAboveSelectionToggle
) {
735 m_selectionTogglePressed
= false;
739 if (!isAboveSelectionToggle
&& m_selectionTogglePressed
) {
740 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
741 m_selectionTogglePressed
= false;
745 const bool shiftOrControlPressed
= event
->modifiers() & Qt::ShiftModifier
||
746 event
->modifiers() & Qt::ControlModifier
;
748 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
749 if (rubberBand
->isActive()) {
750 disconnect(rubberBand
, &KItemListRubberBand::endPositionChanged
, this, &KItemListController::slotRubberBandChanged
);
751 rubberBand
->setActive(false);
752 m_oldSelection
.clear();
753 m_view
->setAutoScroll(false);
756 const QPointF pos
= transform
.map(event
->pos());
757 const int index
= m_view
->itemAt(pos
);
759 if (index
>= 0 && index
== m_pressedIndex
) {
760 // The release event is done above the same item as the press event
762 if (m_clearSelectionIfItemsAreNotDragged
) {
763 // A selected item has been clicked, but no drag operation has been started
764 // -> clear the rest of the selection.
765 m_selectionManager
->clearSelection();
766 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Select
);
767 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
770 if (event
->button() & Qt::LeftButton
) {
771 bool emitItemActivated
= true;
772 if (m_view
->isAboveExpansionToggle(index
, pos
)) {
773 const bool expanded
= m_model
->isExpanded(index
);
774 m_model
->setExpanded(index
, !expanded
);
776 emit
itemExpansionToggleClicked(index
);
777 emitItemActivated
= false;
778 } else if (shiftOrControlPressed
) {
779 // The mouse click should only update the selection, not trigger the item
780 emitItemActivated
= false;
781 } else if (!(m_view
->style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick
) || m_singleClickActivationEnforced
)) {
782 emitItemActivated
= false;
784 if (emitItemActivated
) {
785 emit
itemActivated(index
);
787 } else if (event
->button() & Qt::MidButton
) {
788 emit
itemMiddleClicked(index
);
792 m_pressedMousePos
= QPointF();
794 m_clearSelectionIfItemsAreNotDragged
= false;
798 bool KItemListController::mouseDoubleClickEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
800 const QPointF pos
= transform
.map(event
->pos());
801 const int index
= m_view
->itemAt(pos
);
803 // Expand item if desired - See Bug 295573
804 if (m_mouseDoubleClickAction
!= ActivateItemOnly
) {
805 if (m_view
&& m_model
&& m_view
->supportsItemExpanding() && m_model
->isExpandable(index
)) {
806 const bool expanded
= m_model
->isExpanded(index
);
807 m_model
->setExpanded(index
, !expanded
);
811 if (event
->button() & Qt::RightButton
) {
812 m_selectionManager
->clearSelection();
814 m_selectionManager
->setSelected(index
);
815 emit
itemContextMenuRequested(index
, event
->screenPos());
817 const QRectF headerBounds
= m_view
->headerBoundaries();
818 if (headerBounds
.contains(event
->pos())) {
819 emit
headerContextMenuRequested(event
->screenPos());
821 emit
viewContextMenuRequested(event
->screenPos());
827 bool emitItemActivated
= !(m_view
->style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick
) || m_singleClickActivationEnforced
) &&
828 (event
->button() & Qt::LeftButton
) &&
829 index
>= 0 && index
< m_model
->count();
830 if (emitItemActivated
) {
831 emit
itemActivated(index
);
836 bool KItemListController::dragEnterEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
841 DragAndDropHelper::clearUrlListMatchesUrlCache();
846 bool KItemListController::dragLeaveEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
851 m_autoActivationTimer
->stop();
852 m_view
->setAutoScroll(false);
853 m_view
->hideDropIndicator();
855 KItemListWidget
* widget
= hoveredWidget();
857 widget
->setHovered(false);
858 emit
itemUnhovered(widget
->index());
863 bool KItemListController::dragMoveEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
865 if (!m_model
|| !m_view
) {
870 QUrl hoveredDir
= m_model
->directory();
871 KItemListWidget
* oldHoveredWidget
= hoveredWidget();
873 const QPointF pos
= transform
.map(event
->pos());
874 KItemListWidget
* newHoveredWidget
= widgetForPos(pos
);
876 if (oldHoveredWidget
!= newHoveredWidget
) {
877 m_autoActivationTimer
->stop();
879 if (oldHoveredWidget
) {
880 oldHoveredWidget
->setHovered(false);
881 emit
itemUnhovered(oldHoveredWidget
->index());
885 if (newHoveredWidget
) {
886 bool droppingBetweenItems
= false;
887 if (m_model
->sortRole().isEmpty()) {
888 // The model supports inserting items between other items.
889 droppingBetweenItems
= (m_view
->showDropIndicator(pos
) >= 0);
892 const int index
= newHoveredWidget
->index();
894 if (m_model
->isDir(index
)) {
895 hoveredDir
= m_model
->url(index
);
898 if (!droppingBetweenItems
) {
899 if (m_model
->supportsDropping(index
)) {
900 // Something has been dragged on an item.
901 m_view
->hideDropIndicator();
902 if (!newHoveredWidget
->isHovered()) {
903 newHoveredWidget
->setHovered(true);
904 emit
itemHovered(index
);
907 if (!m_autoActivationTimer
->isActive() && m_autoActivationTimer
->interval() >= 0) {
908 m_autoActivationTimer
->setProperty("index", index
);
909 m_autoActivationTimer
->start();
913 m_autoActivationTimer
->stop();
914 if (newHoveredWidget
&& newHoveredWidget
->isHovered()) {
915 newHoveredWidget
->setHovered(false);
916 emit
itemUnhovered(index
);
920 m_view
->hideDropIndicator();
923 event
->setAccepted(!DragAndDropHelper::urlListMatchesUrl(event
->mimeData()->urls(), hoveredDir
));
928 bool KItemListController::dropEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
934 m_autoActivationTimer
->stop();
935 m_view
->setAutoScroll(false);
937 const QPointF pos
= transform
.map(event
->pos());
939 int dropAboveIndex
= -1;
940 if (m_model
->sortRole().isEmpty()) {
941 // The model supports inserting of items between other items.
942 dropAboveIndex
= m_view
->showDropIndicator(pos
);
945 if (dropAboveIndex
>= 0) {
946 // Something has been dropped between two items.
947 m_view
->hideDropIndicator();
948 emit
aboveItemDropEvent(dropAboveIndex
, event
);
949 } else if (!event
->mimeData()->hasFormat(m_model
->blacklistItemDropEventMimeType())) {
950 // Something has been dropped on an item or on an empty part of the view.
951 emit
itemDropEvent(m_view
->itemAt(pos
), event
);
954 QAccessibleEvent
accessibilityEvent(view(), QAccessible::DragDropEnd
);
955 QAccessible::updateAccessibility(&accessibilityEvent
);
960 bool KItemListController::hoverEnterEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
967 bool KItemListController::hoverMoveEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
970 if (!m_model
|| !m_view
) {
974 KItemListWidget
* oldHoveredWidget
= hoveredWidget();
975 const QPointF pos
= transform
.map(event
->pos());
976 KItemListWidget
* newHoveredWidget
= widgetForPos(pos
);
978 if (oldHoveredWidget
!= newHoveredWidget
) {
979 if (oldHoveredWidget
) {
980 oldHoveredWidget
->setHovered(false);
981 emit
itemUnhovered(oldHoveredWidget
->index());
984 if (newHoveredWidget
) {
985 newHoveredWidget
->setHovered(true);
986 const QPointF mappedPos
= newHoveredWidget
->mapFromItem(m_view
, pos
);
987 newHoveredWidget
->setHoverPosition(mappedPos
);
988 emit
itemHovered(newHoveredWidget
->index());
990 } else if (oldHoveredWidget
) {
991 const QPointF mappedPos
= oldHoveredWidget
->mapFromItem(m_view
, pos
);
992 oldHoveredWidget
->setHoverPosition(mappedPos
);
998 bool KItemListController::hoverLeaveEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
1001 Q_UNUSED(transform
);
1003 if (!m_model
|| !m_view
) {
1007 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
1008 if (widget
->isHovered()) {
1009 widget
->setHovered(false);
1010 emit
itemUnhovered(widget
->index());
1016 bool KItemListController::wheelEvent(QGraphicsSceneWheelEvent
* event
, const QTransform
& transform
)
1019 Q_UNUSED(transform
);
1023 bool KItemListController::resizeEvent(QGraphicsSceneResizeEvent
* event
, const QTransform
& transform
)
1026 Q_UNUSED(transform
);
1030 bool KItemListController::processEvent(QEvent
* event
, const QTransform
& transform
)
1036 switch (event
->type()) {
1037 case QEvent::KeyPress
:
1038 return keyPressEvent(static_cast<QKeyEvent
*>(event
));
1039 case QEvent::InputMethod
:
1040 return inputMethodEvent(static_cast<QInputMethodEvent
*>(event
));
1041 case QEvent::GraphicsSceneMousePress
:
1042 return mousePressEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
1043 case QEvent::GraphicsSceneMouseMove
:
1044 return mouseMoveEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
1045 case QEvent::GraphicsSceneMouseRelease
:
1046 return mouseReleaseEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
1047 case QEvent::GraphicsSceneMouseDoubleClick
:
1048 return mouseDoubleClickEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
1049 case QEvent::GraphicsSceneWheel
:
1050 return wheelEvent(static_cast<QGraphicsSceneWheelEvent
*>(event
), QTransform());
1051 case QEvent::GraphicsSceneDragEnter
:
1052 return dragEnterEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
1053 case QEvent::GraphicsSceneDragLeave
:
1054 return dragLeaveEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
1055 case QEvent::GraphicsSceneDragMove
:
1056 return dragMoveEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
1057 case QEvent::GraphicsSceneDrop
:
1058 return dropEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
1059 case QEvent::GraphicsSceneHoverEnter
:
1060 return hoverEnterEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
1061 case QEvent::GraphicsSceneHoverMove
:
1062 return hoverMoveEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
1063 case QEvent::GraphicsSceneHoverLeave
:
1064 return hoverLeaveEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
1065 case QEvent::GraphicsSceneResize
:
1066 return resizeEvent(static_cast<QGraphicsSceneResizeEvent
*>(event
), transform
);
1074 void KItemListController::slotViewScrollOffsetChanged(qreal current
, qreal previous
)
1080 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
1081 if (rubberBand
->isActive()) {
1082 const qreal diff
= current
- previous
;
1083 // TODO: Ideally just QCursor::pos() should be used as
1084 // new end-position but it seems there is no easy way
1085 // to have something like QWidget::mapFromGlobal() for QGraphicsWidget
1086 // (... or I just missed an easy way to do the mapping)
1087 QPointF endPos
= rubberBand
->endPosition();
1088 if (m_view
->scrollOrientation() == Qt::Vertical
) {
1089 endPos
.ry() += diff
;
1091 endPos
.rx() += diff
;
1094 rubberBand
->setEndPosition(endPos
);
1098 void KItemListController::slotRubberBandChanged()
1100 if (!m_view
|| !m_model
|| m_model
->count() <= 0) {
1104 const KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
1105 const QPointF startPos
= rubberBand
->startPosition();
1106 const QPointF endPos
= rubberBand
->endPosition();
1107 QRectF rubberBandRect
= QRectF(startPos
, endPos
).normalized();
1109 const bool scrollVertical
= (m_view
->scrollOrientation() == Qt::Vertical
);
1110 if (scrollVertical
) {
1111 rubberBandRect
.translate(0, -m_view
->scrollOffset());
1113 rubberBandRect
.translate(-m_view
->scrollOffset(), 0);
1116 if (!m_oldSelection
.isEmpty()) {
1117 // Clear the old selection that was available before the rubberband has
1118 // been activated in case if no Shift- or Control-key are pressed
1119 const bool shiftOrControlPressed
= QApplication::keyboardModifiers() & Qt::ShiftModifier
||
1120 QApplication::keyboardModifiers() & Qt::ControlModifier
;
1121 if (!shiftOrControlPressed
) {
1122 m_oldSelection
.clear();
1126 KItemSet selectedItems
;
1128 // Select all visible items that intersect with the rubberband
1129 foreach (const KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
1130 const int index
= widget
->index();
1132 const QRectF widgetRect
= m_view
->itemRect(index
);
1133 if (widgetRect
.intersects(rubberBandRect
)) {
1134 const QRectF iconRect
= widget
->iconRect().translated(widgetRect
.topLeft());
1135 const QRectF textRect
= widget
->textRect().translated(widgetRect
.topLeft());
1136 if (iconRect
.intersects(rubberBandRect
) || textRect
.intersects(rubberBandRect
)) {
1137 selectedItems
.insert(index
);
1142 // Select all invisible items that intersect with the rubberband. Instead of
1143 // iterating all items only the area which might be touched by the rubberband
1145 const bool increaseIndex
= scrollVertical
?
1146 startPos
.y() > endPos
.y(): startPos
.x() > endPos
.x();
1148 int index
= increaseIndex
? m_view
->lastVisibleIndex() + 1 : m_view
->firstVisibleIndex() - 1;
1149 bool selectionFinished
= false;
1151 const QRectF widgetRect
= m_view
->itemRect(index
);
1152 if (widgetRect
.intersects(rubberBandRect
)) {
1153 selectedItems
.insert(index
);
1156 if (increaseIndex
) {
1158 selectionFinished
= (index
>= m_model
->count()) ||
1159 ( scrollVertical
&& widgetRect
.top() > rubberBandRect
.bottom()) ||
1160 (!scrollVertical
&& widgetRect
.left() > rubberBandRect
.right());
1163 selectionFinished
= (index
< 0) ||
1164 ( scrollVertical
&& widgetRect
.bottom() < rubberBandRect
.top()) ||
1165 (!scrollVertical
&& widgetRect
.right() < rubberBandRect
.left());
1167 } while (!selectionFinished
);
1169 if (QApplication::keyboardModifiers() & Qt::ControlModifier
) {
1170 // If Control is pressed, the selection state of all items in the rubberband is toggled.
1171 // Therefore, the new selection contains:
1172 // 1. All previously selected items which are not inside the rubberband, and
1173 // 2. all items inside the rubberband which have not been selected previously.
1174 m_selectionManager
->setSelectedItems(m_oldSelection
^ selectedItems
);
1177 m_selectionManager
->setSelectedItems(selectedItems
+ m_oldSelection
);
1181 void KItemListController::startDragging()
1183 if (!m_view
|| !m_model
) {
1187 const KItemSet selectedItems
= m_selectionManager
->selectedItems();
1188 if (selectedItems
.isEmpty()) {
1192 QMimeData
* data
= m_model
->createMimeData(selectedItems
);
1197 // The created drag object will be owned and deleted
1198 // by QApplication::activeWindow().
1199 QDrag
* drag
= new QDrag(QApplication::activeWindow());
1200 drag
->setMimeData(data
);
1202 const QPixmap pixmap
= m_view
->createDragPixmap(selectedItems
);
1203 drag
->setPixmap(pixmap
);
1205 const QPoint
hotSpot((pixmap
.width() / pixmap
.devicePixelRatio()) / 2, 0);
1206 drag
->setHotSpot(hotSpot
);
1208 drag
->exec(Qt::MoveAction
| Qt::CopyAction
| Qt::LinkAction
, Qt::CopyAction
);
1210 QAccessibleEvent
accessibilityEvent(view(), QAccessible::DragDropStart
);
1211 QAccessible::updateAccessibility(&accessibilityEvent
);
1214 KItemListWidget
* KItemListController::hoveredWidget() const
1218 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
1219 if (widget
->isHovered()) {
1227 KItemListWidget
* KItemListController::widgetForPos(const QPointF
& pos
) const
1231 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
1232 const QPointF mappedPos
= widget
->mapFromItem(m_view
, pos
);
1234 const bool hovered
= widget
->contains(mappedPos
) &&
1235 !widget
->expansionToggleRect().contains(mappedPos
);
1244 void KItemListController::updateKeyboardAnchor()
1246 const bool validAnchor
= m_keyboardAnchorIndex
>= 0 &&
1247 m_keyboardAnchorIndex
< m_model
->count() &&
1248 keyboardAnchorPos(m_keyboardAnchorIndex
) == m_keyboardAnchorPos
;
1250 const int index
= m_selectionManager
->currentItem();
1251 m_keyboardAnchorIndex
= index
;
1252 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
1256 int KItemListController::nextRowIndex(int index
) const
1258 if (m_keyboardAnchorIndex
< 0) {
1262 const int maxIndex
= m_model
->count() - 1;
1263 if (index
== maxIndex
) {
1267 // Calculate the index of the last column inside the row of the current index
1268 int lastColumnIndex
= index
;
1269 while (keyboardAnchorPos(lastColumnIndex
+ 1) > keyboardAnchorPos(lastColumnIndex
)) {
1271 if (lastColumnIndex
>= maxIndex
) {
1276 // Based on the last column index go to the next row and calculate the nearest index
1277 // that is below the current index
1278 int nextRowIndex
= lastColumnIndex
+ 1;
1279 int searchIndex
= nextRowIndex
;
1280 qreal minDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(nextRowIndex
));
1281 while (searchIndex
< maxIndex
&& keyboardAnchorPos(searchIndex
+ 1) > keyboardAnchorPos(searchIndex
)) {
1283 const qreal searchDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(searchIndex
));
1284 if (searchDiff
< minDiff
) {
1285 minDiff
= searchDiff
;
1286 nextRowIndex
= searchIndex
;
1290 return nextRowIndex
;
1293 int KItemListController::previousRowIndex(int index
) const
1295 if (m_keyboardAnchorIndex
< 0 || index
== 0) {
1299 // Calculate the index of the first column inside the row of the current index
1300 int firstColumnIndex
= index
;
1301 while (keyboardAnchorPos(firstColumnIndex
- 1) < keyboardAnchorPos(firstColumnIndex
)) {
1303 if (firstColumnIndex
<= 0) {
1308 // Based on the first column index go to the previous row and calculate the nearest index
1309 // that is above the current index
1310 int previousRowIndex
= firstColumnIndex
- 1;
1311 int searchIndex
= previousRowIndex
;
1312 qreal minDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(previousRowIndex
));
1313 while (searchIndex
> 0 && keyboardAnchorPos(searchIndex
- 1) < keyboardAnchorPos(searchIndex
)) {
1315 const qreal searchDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(searchIndex
));
1316 if (searchDiff
< minDiff
) {
1317 minDiff
= searchDiff
;
1318 previousRowIndex
= searchIndex
;
1322 return previousRowIndex
;
1325 qreal
KItemListController::keyboardAnchorPos(int index
) const
1327 const QRectF itemRect
= m_view
->itemRect(index
);
1328 if (!itemRect
.isEmpty()) {
1329 return (m_view
->scrollOrientation() == Qt::Vertical
) ? itemRect
.x() : itemRect
.y();
1335 void KItemListController::updateExtendedSelectionRegion()
1338 const bool extend
= (m_selectionBehavior
!= MultiSelection
);
1339 KItemListStyleOption option
= m_view
->styleOption();
1340 if (option
.extendedSelectionRegion
!= extend
) {
1341 option
.extendedSelectionRegion
= extend
;
1342 m_view
->setStyleOption(option
);