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"
27 #include "kitemlistview.h"
28 #include "kitemlistselectionmanager.h"
30 #include "private/kitemlistrubberband.h"
31 #include "private/kitemlistkeyboardsearchmanager.h"
33 #include <QApplication>
35 #include <QGraphicsScene>
36 #include <QGraphicsSceneEvent>
37 #include <QGraphicsView>
40 #include <QAccessible>
41 #include <views/draganddrophelper.h>
43 KItemListController::KItemListController(KItemModelBase
* model
, KItemListView
* view
, QObject
* parent
) :
45 m_singleClickActivationEnforced(false),
46 m_selectionTogglePressed(false),
47 m_clearSelectionIfItemsAreNotDragged(false),
48 m_selectionBehavior(NoSelection
),
49 m_autoActivationBehavior(ActivationAndExpansion
),
50 m_mouseDoubleClickAction(ActivateItemOnly
),
53 m_selectionManager(new KItemListSelectionManager(this)),
54 m_keyboardManager(new KItemListKeyboardSearchManager(this)),
57 m_autoActivationTimer(nullptr),
59 m_keyboardAnchorIndex(-1),
60 m_keyboardAnchorPos(0)
62 connect(m_keyboardManager
, &KItemListKeyboardSearchManager::changeCurrentItem
,
63 this, &KItemListController::slotChangeCurrentItem
);
64 connect(m_selectionManager
, &KItemListSelectionManager::currentChanged
,
65 m_keyboardManager
, &KItemListKeyboardSearchManager::slotCurrentChanged
);
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::showEvent(QShowEvent
* event
)
221 bool KItemListController::hideEvent(QHideEvent
* event
)
227 bool KItemListController::keyPressEvent(QKeyEvent
* event
)
229 int index
= m_selectionManager
->currentItem();
230 int key
= event
->key();
232 // Handle the expanding/collapsing of items
233 if (m_view
->supportsItemExpanding() && m_model
->isExpandable(index
)) {
234 if (key
== Qt::Key_Right
) {
235 if (m_model
->setExpanded(index
, true)) {
238 } else if (key
== Qt::Key_Left
) {
239 if (m_model
->setExpanded(index
, false)) {
245 const bool shiftPressed
= event
->modifiers() & Qt::ShiftModifier
;
246 const bool controlPressed
= event
->modifiers() & Qt::ControlModifier
;
247 const bool shiftOrControlPressed
= shiftPressed
|| controlPressed
;
249 const int itemCount
= m_model
->count();
251 // For horizontal scroll orientation, transform
252 // the arrow keys to simplify the event handling.
253 if (m_view
->scrollOrientation() == Qt::Horizontal
) {
255 case Qt::Key_Up
: key
= Qt::Key_Left
; break;
256 case Qt::Key_Down
: key
= Qt::Key_Right
; break;
257 case Qt::Key_Left
: key
= Qt::Key_Up
; break;
258 case Qt::Key_Right
: key
= Qt::Key_Down
; break;
263 const bool selectSingleItem
= m_selectionBehavior
!= NoSelection
&&
265 (key
== Qt::Key_Home
|| key
== Qt::Key_End
||
266 key
== Qt::Key_Up
|| key
== Qt::Key_Down
||
267 key
== Qt::Key_Left
|| key
== Qt::Key_Right
);
268 if (selectSingleItem
) {
269 const int current
= m_selectionManager
->currentItem();
270 m_selectionManager
->setSelected(current
);
277 m_keyboardAnchorIndex
= index
;
278 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
282 index
= itemCount
- 1;
283 m_keyboardAnchorIndex
= index
;
284 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
289 const int expandedParentsCount
= m_model
->expandedParentsCount(index
);
290 if (expandedParentsCount
== 0) {
293 // Go to the parent of the current item.
296 } while (index
> 0 && m_model
->expandedParentsCount(index
) == expandedParentsCount
);
298 m_keyboardAnchorIndex
= index
;
299 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
304 if (index
< itemCount
- 1) {
306 m_keyboardAnchorIndex
= index
;
307 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
312 updateKeyboardAnchor();
313 index
= previousRowIndex(index
);
317 updateKeyboardAnchor();
318 index
= nextRowIndex(index
);
322 if (m_view
->scrollOrientation() == Qt::Horizontal
) {
323 // The new current index should correspond to the first item in the current column.
324 int newIndex
= qMax(index
- 1, 0);
325 while (newIndex
!= index
&& m_view
->itemRect(newIndex
).topLeft().y() < m_view
->itemRect(index
).topLeft().y()) {
327 newIndex
= qMax(index
- 1, 0);
329 m_keyboardAnchorIndex
= index
;
330 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
332 const qreal currentItemBottom
= m_view
->itemRect(index
).bottomLeft().y();
333 const qreal height
= m_view
->geometry().height();
335 // The new current item should be the first item in the current
336 // column whose itemRect's top coordinate is larger than targetY.
337 const qreal targetY
= currentItemBottom
- height
;
339 updateKeyboardAnchor();
340 int newIndex
= previousRowIndex(index
);
343 updateKeyboardAnchor();
344 newIndex
= previousRowIndex(index
);
345 } while (m_view
->itemRect(newIndex
).topLeft().y() > targetY
&& newIndex
!= index
);
349 case Qt::Key_PageDown
:
350 if (m_view
->scrollOrientation() == Qt::Horizontal
) {
351 // The new current index should correspond to the last item in the current column.
352 int newIndex
= qMin(index
+ 1, m_model
->count() - 1);
353 while (newIndex
!= index
&& m_view
->itemRect(newIndex
).topLeft().y() > m_view
->itemRect(index
).topLeft().y()) {
355 newIndex
= qMin(index
+ 1, m_model
->count() - 1);
357 m_keyboardAnchorIndex
= index
;
358 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
360 const qreal currentItemTop
= m_view
->itemRect(index
).topLeft().y();
361 const qreal height
= m_view
->geometry().height();
363 // The new current item should be the last item in the current
364 // column whose itemRect's bottom coordinate is smaller than targetY.
365 const qreal targetY
= currentItemTop
+ height
;
367 updateKeyboardAnchor();
368 int newIndex
= nextRowIndex(index
);
371 updateKeyboardAnchor();
372 newIndex
= nextRowIndex(index
);
373 } while (m_view
->itemRect(newIndex
).bottomLeft().y() < targetY
&& newIndex
!= index
);
378 case Qt::Key_Return
: {
379 const KItemSet selectedItems
= m_selectionManager
->selectedItems();
380 if (selectedItems
.count() >= 2) {
381 emit
itemsActivated(selectedItems
);
382 } else if (selectedItems
.count() == 1) {
383 emit
itemActivated(selectedItems
.first());
385 emit
itemActivated(index
);
391 // Emit the signal itemContextMenuRequested() in case if at least one
392 // item is selected. Otherwise the signal viewContextMenuRequested() will be emitted.
393 const KItemSet selectedItems
= m_selectionManager
->selectedItems();
395 if (selectedItems
.count() >= 2) {
396 const int currentItemIndex
= m_selectionManager
->currentItem();
397 index
= selectedItems
.contains(currentItemIndex
)
398 ? currentItemIndex
: selectedItems
.first();
399 } else if (selectedItems
.count() == 1) {
400 index
= selectedItems
.first();
404 const QRectF contextRect
= m_view
->itemContextRect(index
);
405 const QPointF
pos(m_view
->scene()->views().first()->mapToGlobal(contextRect
.bottomRight().toPoint()));
406 emit
itemContextMenuRequested(index
, pos
);
408 emit
viewContextMenuRequested(QCursor::pos());
414 if (m_selectionBehavior
!= SingleSelection
) {
415 m_selectionManager
->clearSelection();
417 m_keyboardManager
->cancelSearch();
418 emit
escapePressed();
422 if (m_selectionBehavior
== MultiSelection
) {
423 if (controlPressed
) {
424 // Toggle the selection state of the current item.
425 m_selectionManager
->endAnchoredSelection();
426 m_selectionManager
->setSelected(index
, 1, KItemListSelectionManager::Toggle
);
427 m_selectionManager
->beginAnchoredSelection(index
);
430 // Select the current item if it is not selected yet.
431 const int current
= m_selectionManager
->currentItem();
432 if (!m_selectionManager
->isSelected(current
)) {
433 m_selectionManager
->setSelected(current
);
439 // to the default case and add the Space to the current search string.
441 m_keyboardManager
->addKeys(event
->text());
442 // Make sure unconsumed events get propagated up the chain. #302329
447 if (m_selectionManager
->currentItem() != index
) {
448 switch (m_selectionBehavior
) {
450 m_selectionManager
->setCurrentItem(index
);
453 case SingleSelection
:
454 m_selectionManager
->setCurrentItem(index
);
455 m_selectionManager
->clearSelection();
456 m_selectionManager
->setSelected(index
, 1);
460 if (controlPressed
) {
461 m_selectionManager
->endAnchoredSelection();
464 m_selectionManager
->setCurrentItem(index
);
466 if (!shiftOrControlPressed
) {
467 m_selectionManager
->clearSelection();
468 m_selectionManager
->setSelected(index
, 1);
472 m_selectionManager
->beginAnchoredSelection(index
);
477 m_view
->scrollToItem(index
);
482 void KItemListController::slotChangeCurrentItem(const QString
& text
, bool searchFromNextItem
)
484 if (!m_model
|| m_model
->count() == 0) {
487 const int currentIndex
= m_selectionManager
->currentItem();
489 if (searchFromNextItem
) {
490 index
= m_model
->indexForKeyboardSearch(text
, (currentIndex
+ 1) % m_model
->count());
492 index
= m_model
->indexForKeyboardSearch(text
, currentIndex
);
495 m_selectionManager
->setCurrentItem(index
);
497 if (m_selectionBehavior
!= NoSelection
) {
498 m_selectionManager
->clearSelection();
499 m_selectionManager
->setSelected(index
, 1);
500 m_selectionManager
->beginAnchoredSelection(index
);
503 m_view
->scrollToItem(index
);
507 void KItemListController::slotAutoActivationTimeout()
509 if (!m_model
|| !m_view
) {
513 const int index
= m_autoActivationTimer
->property("index").toInt();
514 if (index
< 0 || index
>= m_model
->count()) {
518 /* m_view->isUnderMouse() fixes a bug in the Folder-View-Panel and in the
521 * Bug: When you drag a file onto a Folder-View-Item or a Places-Item and
522 * then move away before the auto-activation timeout triggers, than the
523 * item still becomes activated/expanded.
525 * See Bug 293200 and 305783
527 if (m_model
->supportsDropping(index
) && m_view
->isUnderMouse()) {
528 if (m_view
->supportsItemExpanding() && m_model
->isExpandable(index
)) {
529 const bool expanded
= m_model
->isExpanded(index
);
530 m_model
->setExpanded(index
, !expanded
);
531 } else if (m_autoActivationBehavior
!= ExpansionOnly
) {
532 emit
itemActivated(index
);
537 bool KItemListController::inputMethodEvent(QInputMethodEvent
* event
)
543 bool KItemListController::mousePressEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
549 m_pressedMousePos
= transform
.map(event
->pos());
550 m_pressedIndex
= m_view
->itemAt(m_pressedMousePos
);
551 emit
mouseButtonPressed(m_pressedIndex
, event
->buttons());
553 if (event
->buttons() & (Qt::BackButton
| Qt::ForwardButton
)) {
554 // Do not select items when clicking the back/forward buttons, see
555 // https://bugs.kde.org/show_bug.cgi?id=327412.
559 if (m_view
->isAboveExpansionToggle(m_pressedIndex
, m_pressedMousePos
)) {
560 m_selectionManager
->endAnchoredSelection();
561 m_selectionManager
->setCurrentItem(m_pressedIndex
);
562 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
566 m_selectionTogglePressed
= m_view
->isAboveSelectionToggle(m_pressedIndex
, m_pressedMousePos
);
567 if (m_selectionTogglePressed
) {
568 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
569 // The previous anchored selection has been finished already in
570 // KItemListSelectionManager::setSelected(). We can safely change
571 // the current item and start a new anchored selection now.
572 m_selectionManager
->setCurrentItem(m_pressedIndex
);
573 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
577 const bool shiftPressed
= event
->modifiers() & Qt::ShiftModifier
;
578 const bool controlPressed
= event
->modifiers() & Qt::ControlModifier
;
580 // The previous selection is cleared if either
581 // 1. The selection mode is SingleSelection, or
582 // 2. the selection mode is MultiSelection, and *none* of the following conditions are met:
583 // a) Shift or Control are pressed.
584 // b) The clicked item is selected already. In that case, the user might want to:
585 // - start dragging multiple items, or
586 // - open the context menu and perform an action for all selected items.
587 const bool shiftOrControlPressed
= shiftPressed
|| controlPressed
;
588 const bool pressedItemAlreadySelected
= m_pressedIndex
>= 0 && m_selectionManager
->isSelected(m_pressedIndex
);
589 const bool clearSelection
= m_selectionBehavior
== SingleSelection
||
590 (!shiftOrControlPressed
&& !pressedItemAlreadySelected
);
591 if (clearSelection
) {
592 m_selectionManager
->clearSelection();
593 } else if (pressedItemAlreadySelected
&& !shiftOrControlPressed
&& (event
->buttons() & Qt::LeftButton
)) {
594 // The user might want to start dragging multiple items, but if he clicks the item
595 // in order to trigger it instead, the other selected items must be deselected.
596 // However, we do not know yet what the user is going to do.
597 // -> remember that the user pressed an item which had been selected already and
598 // clear the selection in mouseReleaseEvent(), unless the items are dragged.
599 m_clearSelectionIfItemsAreNotDragged
= true;
601 if (m_selectionManager
->selectedItems().count() == 1 && m_view
->isAboveText(m_pressedIndex
, m_pressedMousePos
)) {
602 emit
selectedItemTextPressed(m_pressedIndex
);
607 // Finish the anchored selection before the current index is changed
608 m_selectionManager
->endAnchoredSelection();
611 if (m_pressedIndex
>= 0) {
612 m_selectionManager
->setCurrentItem(m_pressedIndex
);
614 switch (m_selectionBehavior
) {
618 case SingleSelection
:
619 m_selectionManager
->setSelected(m_pressedIndex
);
623 if (controlPressed
&& !shiftPressed
) {
624 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
625 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
626 } else if (!shiftPressed
|| !m_selectionManager
->isAnchoredSelectionActive()) {
627 // Select the pressed item and start a new anchored selection
628 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Select
);
629 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
638 if (event
->buttons() & Qt::RightButton
) {
639 emit
itemContextMenuRequested(m_pressedIndex
, event
->screenPos());
645 if (event
->buttons() & Qt::RightButton
) {
646 const QRectF headerBounds
= m_view
->headerBoundaries();
647 if (headerBounds
.contains(event
->pos())) {
648 emit
headerContextMenuRequested(event
->screenPos());
650 emit
viewContextMenuRequested(event
->screenPos());
655 if (m_selectionBehavior
== MultiSelection
) {
656 QPointF startPos
= m_pressedMousePos
;
657 if (m_view
->scrollOrientation() == Qt::Vertical
) {
658 startPos
.ry() += m_view
->scrollOffset();
659 if (m_view
->itemSize().width() < 0) {
660 // Use a special rubberband for views that have only one column and
661 // expand the rubberband to use the whole width of the view.
665 startPos
.rx() += m_view
->scrollOffset();
668 m_oldSelection
= m_selectionManager
->selectedItems();
669 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
670 rubberBand
->setStartPosition(startPos
);
671 rubberBand
->setEndPosition(startPos
);
672 rubberBand
->setActive(true);
673 connect(rubberBand
, &KItemListRubberBand::endPositionChanged
, this, &KItemListController::slotRubberBandChanged
);
674 m_view
->setAutoScroll(true);
680 bool KItemListController::mouseMoveEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
686 if (m_pressedIndex
>= 0) {
687 // Check whether a dragging should be started
688 if (event
->buttons() & Qt::LeftButton
) {
689 const QPointF pos
= transform
.map(event
->pos());
690 if ((pos
- m_pressedMousePos
).manhattanLength() >= QApplication::startDragDistance()) {
691 if (!m_selectionManager
->isSelected(m_pressedIndex
)) {
692 // Always assure that the dragged item gets selected. Usually this is already
693 // done on the mouse-press event, but when using the selection-toggle on a
694 // selected item the dragged item is not selected yet.
695 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
697 // A selected item has been clicked to drag all selected items
698 // -> the selection should not be cleared when the mouse button is released.
699 m_clearSelectionIfItemsAreNotDragged
= false;
706 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
707 if (rubberBand
->isActive()) {
708 QPointF endPos
= transform
.map(event
->pos());
710 // Update the current item.
711 const int newCurrent
= m_view
->itemAt(endPos
);
712 if (newCurrent
>= 0) {
713 // It's expected that the new current index is also the new anchor (bug 163451).
714 m_selectionManager
->endAnchoredSelection();
715 m_selectionManager
->setCurrentItem(newCurrent
);
716 m_selectionManager
->beginAnchoredSelection(newCurrent
);
719 if (m_view
->scrollOrientation() == Qt::Vertical
) {
720 endPos
.ry() += m_view
->scrollOffset();
721 if (m_view
->itemSize().width() < 0) {
722 // Use a special rubberband for views that have only one column and
723 // expand the rubberband to use the whole width of the view.
724 endPos
.setX(m_view
->size().width());
727 endPos
.rx() += m_view
->scrollOffset();
729 rubberBand
->setEndPosition(endPos
);
736 bool KItemListController::mouseReleaseEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
742 emit
mouseButtonReleased(m_pressedIndex
, event
->buttons());
744 const bool isAboveSelectionToggle
= m_view
->isAboveSelectionToggle(m_pressedIndex
, m_pressedMousePos
);
745 if (isAboveSelectionToggle
) {
746 m_selectionTogglePressed
= false;
750 if (!isAboveSelectionToggle
&& m_selectionTogglePressed
) {
751 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
752 m_selectionTogglePressed
= false;
756 const bool shiftOrControlPressed
= event
->modifiers() & Qt::ShiftModifier
||
757 event
->modifiers() & Qt::ControlModifier
;
759 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
760 if (rubberBand
->isActive()) {
761 disconnect(rubberBand
, &KItemListRubberBand::endPositionChanged
, this, &KItemListController::slotRubberBandChanged
);
762 rubberBand
->setActive(false);
763 m_oldSelection
.clear();
764 m_view
->setAutoScroll(false);
767 const QPointF pos
= transform
.map(event
->pos());
768 const int index
= m_view
->itemAt(pos
);
770 if (index
>= 0 && index
== m_pressedIndex
) {
771 // The release event is done above the same item as the press event
773 if (m_clearSelectionIfItemsAreNotDragged
) {
774 // A selected item has been clicked, but no drag operation has been started
775 // -> clear the rest of the selection.
776 m_selectionManager
->clearSelection();
777 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Select
);
778 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
781 if (event
->button() & Qt::LeftButton
) {
782 bool emitItemActivated
= true;
783 if (m_view
->isAboveExpansionToggle(index
, pos
)) {
784 const bool expanded
= m_model
->isExpanded(index
);
785 m_model
->setExpanded(index
, !expanded
);
787 emit
itemExpansionToggleClicked(index
);
788 emitItemActivated
= false;
789 } else if (shiftOrControlPressed
) {
790 // The mouse click should only update the selection, not trigger the item
791 emitItemActivated
= false;
792 } else if (!(m_view
->style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick
) || m_singleClickActivationEnforced
)) {
793 emitItemActivated
= false;
795 if (emitItemActivated
) {
796 emit
itemActivated(index
);
798 } else if (event
->button() & Qt::MidButton
) {
799 emit
itemMiddleClicked(index
);
803 m_pressedMousePos
= QPointF();
805 m_clearSelectionIfItemsAreNotDragged
= false;
809 bool KItemListController::mouseDoubleClickEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
811 const QPointF pos
= transform
.map(event
->pos());
812 const int index
= m_view
->itemAt(pos
);
814 // Expand item if desired - See Bug 295573
815 if (m_mouseDoubleClickAction
!= ActivateItemOnly
) {
816 if (m_view
&& m_model
&& m_view
->supportsItemExpanding() && m_model
->isExpandable(index
)) {
817 const bool expanded
= m_model
->isExpanded(index
);
818 m_model
->setExpanded(index
, !expanded
);
822 if (event
->button() & Qt::RightButton
) {
823 m_selectionManager
->clearSelection();
825 m_selectionManager
->setSelected(index
);
826 emit
itemContextMenuRequested(index
, event
->screenPos());
828 const QRectF headerBounds
= m_view
->headerBoundaries();
829 if (headerBounds
.contains(event
->pos())) {
830 emit
headerContextMenuRequested(event
->screenPos());
832 emit
viewContextMenuRequested(event
->screenPos());
838 bool emitItemActivated
= !(m_view
->style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick
) || m_singleClickActivationEnforced
) &&
839 (event
->button() & Qt::LeftButton
) &&
840 index
>= 0 && index
< m_model
->count();
841 if (emitItemActivated
) {
842 emit
itemActivated(index
);
847 bool KItemListController::dragEnterEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
852 DragAndDropHelper::clearUrlListMatchesUrlCache();
857 bool KItemListController::dragLeaveEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
862 m_autoActivationTimer
->stop();
863 m_view
->setAutoScroll(false);
864 m_view
->hideDropIndicator();
866 KItemListWidget
* widget
= hoveredWidget();
868 widget
->setHovered(false);
869 emit
itemUnhovered(widget
->index());
874 bool KItemListController::dragMoveEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
876 if (!m_model
|| !m_view
) {
881 QUrl hoveredDir
= m_model
->directory();
882 KItemListWidget
* oldHoveredWidget
= hoveredWidget();
884 const QPointF pos
= transform
.map(event
->pos());
885 KItemListWidget
* newHoveredWidget
= widgetForPos(pos
);
887 if (oldHoveredWidget
!= newHoveredWidget
) {
888 m_autoActivationTimer
->stop();
890 if (oldHoveredWidget
) {
891 oldHoveredWidget
->setHovered(false);
892 emit
itemUnhovered(oldHoveredWidget
->index());
896 if (newHoveredWidget
) {
897 bool droppingBetweenItems
= false;
898 if (m_model
->sortRole().isEmpty()) {
899 // The model supports inserting items between other items.
900 droppingBetweenItems
= (m_view
->showDropIndicator(pos
) >= 0);
903 const int index
= newHoveredWidget
->index();
905 if (m_model
->isDir(index
)) {
906 hoveredDir
= m_model
->url(index
);
909 if (!droppingBetweenItems
) {
910 if (m_model
->supportsDropping(index
)) {
911 // Something has been dragged on an item.
912 m_view
->hideDropIndicator();
913 if (!newHoveredWidget
->isHovered()) {
914 newHoveredWidget
->setHovered(true);
915 emit
itemHovered(index
);
918 if (!m_autoActivationTimer
->isActive() && m_autoActivationTimer
->interval() >= 0) {
919 m_autoActivationTimer
->setProperty("index", index
);
920 m_autoActivationTimer
->start();
924 m_autoActivationTimer
->stop();
925 if (newHoveredWidget
&& newHoveredWidget
->isHovered()) {
926 newHoveredWidget
->setHovered(false);
927 emit
itemUnhovered(index
);
931 m_view
->hideDropIndicator();
934 event
->setAccepted(!DragAndDropHelper::urlListMatchesUrl(event
->mimeData()->urls(), hoveredDir
));
939 bool KItemListController::dropEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
945 m_autoActivationTimer
->stop();
946 m_view
->setAutoScroll(false);
948 const QPointF pos
= transform
.map(event
->pos());
950 int dropAboveIndex
= -1;
951 if (m_model
->sortRole().isEmpty()) {
952 // The model supports inserting of items between other items.
953 dropAboveIndex
= m_view
->showDropIndicator(pos
);
956 if (dropAboveIndex
>= 0) {
957 // Something has been dropped between two items.
958 m_view
->hideDropIndicator();
959 emit
aboveItemDropEvent(dropAboveIndex
, event
);
960 } else if (!event
->mimeData()->hasFormat(m_model
->blacklistItemDropEventMimeType())) {
961 // Something has been dropped on an item or on an empty part of the view.
962 emit
itemDropEvent(m_view
->itemAt(pos
), event
);
965 QAccessibleEvent
accessibilityEvent(view(), QAccessible::DragDropEnd
);
966 QAccessible::updateAccessibility(&accessibilityEvent
);
971 bool KItemListController::hoverEnterEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
978 bool KItemListController::hoverMoveEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
981 if (!m_model
|| !m_view
) {
985 KItemListWidget
* oldHoveredWidget
= hoveredWidget();
986 const QPointF pos
= transform
.map(event
->pos());
987 KItemListWidget
* newHoveredWidget
= widgetForPos(pos
);
989 if (oldHoveredWidget
!= newHoveredWidget
) {
990 if (oldHoveredWidget
) {
991 oldHoveredWidget
->setHovered(false);
992 emit
itemUnhovered(oldHoveredWidget
->index());
995 if (newHoveredWidget
) {
996 newHoveredWidget
->setHovered(true);
997 const QPointF mappedPos
= newHoveredWidget
->mapFromItem(m_view
, pos
);
998 newHoveredWidget
->setHoverPosition(mappedPos
);
999 emit
itemHovered(newHoveredWidget
->index());
1001 } else if (oldHoveredWidget
) {
1002 const QPointF mappedPos
= oldHoveredWidget
->mapFromItem(m_view
, pos
);
1003 oldHoveredWidget
->setHoverPosition(mappedPos
);
1009 bool KItemListController::hoverLeaveEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
1012 Q_UNUSED(transform
);
1014 if (!m_model
|| !m_view
) {
1018 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
1019 if (widget
->isHovered()) {
1020 widget
->setHovered(false);
1021 emit
itemUnhovered(widget
->index());
1027 bool KItemListController::wheelEvent(QGraphicsSceneWheelEvent
* event
, const QTransform
& transform
)
1030 Q_UNUSED(transform
);
1034 bool KItemListController::resizeEvent(QGraphicsSceneResizeEvent
* event
, const QTransform
& transform
)
1037 Q_UNUSED(transform
);
1041 bool KItemListController::processEvent(QEvent
* event
, const QTransform
& transform
)
1047 switch (event
->type()) {
1048 case QEvent::KeyPress
:
1049 return keyPressEvent(static_cast<QKeyEvent
*>(event
));
1050 case QEvent::InputMethod
:
1051 return inputMethodEvent(static_cast<QInputMethodEvent
*>(event
));
1052 case QEvent::GraphicsSceneMousePress
:
1053 return mousePressEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
1054 case QEvent::GraphicsSceneMouseMove
:
1055 return mouseMoveEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
1056 case QEvent::GraphicsSceneMouseRelease
:
1057 return mouseReleaseEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
1058 case QEvent::GraphicsSceneMouseDoubleClick
:
1059 return mouseDoubleClickEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
1060 case QEvent::GraphicsSceneWheel
:
1061 return wheelEvent(static_cast<QGraphicsSceneWheelEvent
*>(event
), QTransform());
1062 case QEvent::GraphicsSceneDragEnter
:
1063 return dragEnterEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
1064 case QEvent::GraphicsSceneDragLeave
:
1065 return dragLeaveEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
1066 case QEvent::GraphicsSceneDragMove
:
1067 return dragMoveEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
1068 case QEvent::GraphicsSceneDrop
:
1069 return dropEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
1070 case QEvent::GraphicsSceneHoverEnter
:
1071 return hoverEnterEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
1072 case QEvent::GraphicsSceneHoverMove
:
1073 return hoverMoveEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
1074 case QEvent::GraphicsSceneHoverLeave
:
1075 return hoverLeaveEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
1076 case QEvent::GraphicsSceneResize
:
1077 return resizeEvent(static_cast<QGraphicsSceneResizeEvent
*>(event
), transform
);
1085 void KItemListController::slotViewScrollOffsetChanged(qreal current
, qreal previous
)
1091 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
1092 if (rubberBand
->isActive()) {
1093 const qreal diff
= current
- previous
;
1094 // TODO: Ideally just QCursor::pos() should be used as
1095 // new end-position but it seems there is no easy way
1096 // to have something like QWidget::mapFromGlobal() for QGraphicsWidget
1097 // (... or I just missed an easy way to do the mapping)
1098 QPointF endPos
= rubberBand
->endPosition();
1099 if (m_view
->scrollOrientation() == Qt::Vertical
) {
1100 endPos
.ry() += diff
;
1102 endPos
.rx() += diff
;
1105 rubberBand
->setEndPosition(endPos
);
1109 void KItemListController::slotRubberBandChanged()
1111 if (!m_view
|| !m_model
|| m_model
->count() <= 0) {
1115 const KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
1116 const QPointF startPos
= rubberBand
->startPosition();
1117 const QPointF endPos
= rubberBand
->endPosition();
1118 QRectF rubberBandRect
= QRectF(startPos
, endPos
).normalized();
1120 const bool scrollVertical
= (m_view
->scrollOrientation() == Qt::Vertical
);
1121 if (scrollVertical
) {
1122 rubberBandRect
.translate(0, -m_view
->scrollOffset());
1124 rubberBandRect
.translate(-m_view
->scrollOffset(), 0);
1127 if (!m_oldSelection
.isEmpty()) {
1128 // Clear the old selection that was available before the rubberband has
1129 // been activated in case if no Shift- or Control-key are pressed
1130 const bool shiftOrControlPressed
= QApplication::keyboardModifiers() & Qt::ShiftModifier
||
1131 QApplication::keyboardModifiers() & Qt::ControlModifier
;
1132 if (!shiftOrControlPressed
) {
1133 m_oldSelection
.clear();
1137 KItemSet selectedItems
;
1139 // Select all visible items that intersect with the rubberband
1140 foreach (const KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
1141 const int index
= widget
->index();
1143 const QRectF widgetRect
= m_view
->itemRect(index
);
1144 if (widgetRect
.intersects(rubberBandRect
)) {
1145 const QRectF iconRect
= widget
->iconRect().translated(widgetRect
.topLeft());
1146 const QRectF textRect
= widget
->textRect().translated(widgetRect
.topLeft());
1147 if (iconRect
.intersects(rubberBandRect
) || textRect
.intersects(rubberBandRect
)) {
1148 selectedItems
.insert(index
);
1153 // Select all invisible items that intersect with the rubberband. Instead of
1154 // iterating all items only the area which might be touched by the rubberband
1156 const bool increaseIndex
= scrollVertical
?
1157 startPos
.y() > endPos
.y(): startPos
.x() > endPos
.x();
1159 int index
= increaseIndex
? m_view
->lastVisibleIndex() + 1 : m_view
->firstVisibleIndex() - 1;
1160 bool selectionFinished
= false;
1162 const QRectF widgetRect
= m_view
->itemRect(index
);
1163 if (widgetRect
.intersects(rubberBandRect
)) {
1164 selectedItems
.insert(index
);
1167 if (increaseIndex
) {
1169 selectionFinished
= (index
>= m_model
->count()) ||
1170 ( scrollVertical
&& widgetRect
.top() > rubberBandRect
.bottom()) ||
1171 (!scrollVertical
&& widgetRect
.left() > rubberBandRect
.right());
1174 selectionFinished
= (index
< 0) ||
1175 ( scrollVertical
&& widgetRect
.bottom() < rubberBandRect
.top()) ||
1176 (!scrollVertical
&& widgetRect
.right() < rubberBandRect
.left());
1178 } while (!selectionFinished
);
1180 if (QApplication::keyboardModifiers() & Qt::ControlModifier
) {
1181 // If Control is pressed, the selection state of all items in the rubberband is toggled.
1182 // Therefore, the new selection contains:
1183 // 1. All previously selected items which are not inside the rubberband, and
1184 // 2. all items inside the rubberband which have not been selected previously.
1185 m_selectionManager
->setSelectedItems(m_oldSelection
^ selectedItems
);
1188 m_selectionManager
->setSelectedItems(selectedItems
+ m_oldSelection
);
1192 void KItemListController::startDragging()
1194 if (!m_view
|| !m_model
) {
1198 const KItemSet selectedItems
= m_selectionManager
->selectedItems();
1199 if (selectedItems
.isEmpty()) {
1203 QMimeData
* data
= m_model
->createMimeData(selectedItems
);
1208 // The created drag object will be owned and deleted
1209 // by QApplication::activeWindow().
1210 QDrag
* drag
= new QDrag(QApplication::activeWindow());
1211 drag
->setMimeData(data
);
1213 const QPixmap pixmap
= m_view
->createDragPixmap(selectedItems
);
1214 drag
->setPixmap(pixmap
);
1216 const QPoint
hotSpot((pixmap
.width() / pixmap
.devicePixelRatio()) / 2, 0);
1217 drag
->setHotSpot(hotSpot
);
1219 drag
->exec(Qt::MoveAction
| Qt::CopyAction
| Qt::LinkAction
, Qt::CopyAction
);
1221 QAccessibleEvent
accessibilityEvent(view(), QAccessible::DragDropStart
);
1222 QAccessible::updateAccessibility(&accessibilityEvent
);
1225 KItemListWidget
* KItemListController::hoveredWidget() const
1229 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
1230 if (widget
->isHovered()) {
1238 KItemListWidget
* KItemListController::widgetForPos(const QPointF
& pos
) const
1242 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
1243 const QPointF mappedPos
= widget
->mapFromItem(m_view
, pos
);
1245 const bool hovered
= widget
->contains(mappedPos
) &&
1246 !widget
->expansionToggleRect().contains(mappedPos
);
1255 void KItemListController::updateKeyboardAnchor()
1257 const bool validAnchor
= m_keyboardAnchorIndex
>= 0 &&
1258 m_keyboardAnchorIndex
< m_model
->count() &&
1259 keyboardAnchorPos(m_keyboardAnchorIndex
) == m_keyboardAnchorPos
;
1261 const int index
= m_selectionManager
->currentItem();
1262 m_keyboardAnchorIndex
= index
;
1263 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
1267 int KItemListController::nextRowIndex(int index
) const
1269 if (m_keyboardAnchorIndex
< 0) {
1273 const int maxIndex
= m_model
->count() - 1;
1274 if (index
== maxIndex
) {
1278 // Calculate the index of the last column inside the row of the current index
1279 int lastColumnIndex
= index
;
1280 while (keyboardAnchorPos(lastColumnIndex
+ 1) > keyboardAnchorPos(lastColumnIndex
)) {
1282 if (lastColumnIndex
>= maxIndex
) {
1287 // Based on the last column index go to the next row and calculate the nearest index
1288 // that is below the current index
1289 int nextRowIndex
= lastColumnIndex
+ 1;
1290 int searchIndex
= nextRowIndex
;
1291 qreal minDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(nextRowIndex
));
1292 while (searchIndex
< maxIndex
&& keyboardAnchorPos(searchIndex
+ 1) > keyboardAnchorPos(searchIndex
)) {
1294 const qreal searchDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(searchIndex
));
1295 if (searchDiff
< minDiff
) {
1296 minDiff
= searchDiff
;
1297 nextRowIndex
= searchIndex
;
1301 return nextRowIndex
;
1304 int KItemListController::previousRowIndex(int index
) const
1306 if (m_keyboardAnchorIndex
< 0 || index
== 0) {
1310 // Calculate the index of the first column inside the row of the current index
1311 int firstColumnIndex
= index
;
1312 while (keyboardAnchorPos(firstColumnIndex
- 1) < keyboardAnchorPos(firstColumnIndex
)) {
1314 if (firstColumnIndex
<= 0) {
1319 // Based on the first column index go to the previous row and calculate the nearest index
1320 // that is above the current index
1321 int previousRowIndex
= firstColumnIndex
- 1;
1322 int searchIndex
= previousRowIndex
;
1323 qreal minDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(previousRowIndex
));
1324 while (searchIndex
> 0 && keyboardAnchorPos(searchIndex
- 1) < keyboardAnchorPos(searchIndex
)) {
1326 const qreal searchDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(searchIndex
));
1327 if (searchDiff
< minDiff
) {
1328 minDiff
= searchDiff
;
1329 previousRowIndex
= searchIndex
;
1333 return previousRowIndex
;
1336 qreal
KItemListController::keyboardAnchorPos(int index
) const
1338 const QRectF itemRect
= m_view
->itemRect(index
);
1339 if (!itemRect
.isEmpty()) {
1340 return (m_view
->scrollOrientation() == Qt::Vertical
) ? itemRect
.x() : itemRect
.y();
1346 void KItemListController::updateExtendedSelectionRegion()
1349 const bool extend
= (m_selectionBehavior
!= MultiSelection
);
1350 KItemListStyleOption option
= m_view
->styleOption();
1351 if (option
.extendedSelectionRegion
!= extend
) {
1352 option
.extendedSelectionRegion
= extend
;
1353 m_view
->setStyleOption(option
);