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 <KGlobalSettings>
29 #include "kitemlistview.h"
30 #include "kitemlistselectionmanager.h"
32 #include "private/kitemlistrubberband.h"
33 #include "private/kitemlistkeyboardsearchmanager.h"
35 #include <QApplication>
38 #include <QGraphicsScene>
39 #include <QGraphicsSceneEvent>
40 #include <QGraphicsView>
43 #include <QAccessible>
45 KItemListController::KItemListController(KItemModelBase
* model
, KItemListView
* view
, QObject
* parent
) :
47 m_singleClickActivationEnforced(false),
48 m_selectionTogglePressed(false),
49 m_clearSelectionIfItemsAreNotDragged(false),
50 m_selectionBehavior(NoSelection
),
51 m_autoActivationBehavior(ActivationAndExpansion
),
52 m_mouseDoubleClickAction(ActivateItemOnly
),
55 m_selectionManager(new KItemListSelectionManager(this)),
56 m_keyboardManager(new KItemListKeyboardSearchManager(this)),
59 m_autoActivationTimer(0),
61 m_keyboardAnchorIndex(-1),
62 m_keyboardAnchorPos(0)
64 connect(m_keyboardManager
, SIGNAL(changeCurrentItem(QString
,bool)),
65 this, SLOT(slotChangeCurrentItem(QString
,bool)));
66 connect(m_selectionManager
, SIGNAL(currentChanged(int,int)),
67 m_keyboardManager
, SLOT(slotCurrentChanged(int,int)));
69 m_autoActivationTimer
= new QTimer(this);
70 m_autoActivationTimer
->setSingleShot(true);
71 m_autoActivationTimer
->setInterval(-1);
72 connect(m_autoActivationTimer
, SIGNAL(timeout()), this, SLOT(slotAutoActivationTimeout()));
78 KItemListController::~KItemListController()
87 void KItemListController::setModel(KItemModelBase
* model
)
89 if (m_model
== model
) {
93 KItemModelBase
* oldModel
= m_model
;
95 oldModel
->deleteLater();
100 m_model
->setParent(this);
104 m_view
->setModel(m_model
);
107 m_selectionManager
->setModel(m_model
);
109 emit
modelChanged(m_model
, oldModel
);
112 KItemModelBase
* KItemListController::model() const
117 KItemListSelectionManager
* KItemListController::selectionManager() const
119 return m_selectionManager
;
122 void KItemListController::setView(KItemListView
* view
)
124 if (m_view
== view
) {
128 KItemListView
* oldView
= m_view
;
130 disconnect(oldView
, SIGNAL(scrollOffsetChanged(qreal
,qreal
)), this, SLOT(slotViewScrollOffsetChanged(qreal
,qreal
)));
131 oldView
->deleteLater();
137 m_view
->setParent(this);
138 m_view
->setController(this);
139 m_view
->setModel(m_model
);
140 connect(m_view
, SIGNAL(scrollOffsetChanged(qreal
,qreal
)), this, SLOT(slotViewScrollOffsetChanged(qreal
,qreal
)));
141 updateExtendedSelectionRegion();
144 emit
viewChanged(m_view
, oldView
);
147 KItemListView
* KItemListController::view() const
152 void KItemListController::setSelectionBehavior(SelectionBehavior behavior
)
154 m_selectionBehavior
= behavior
;
155 updateExtendedSelectionRegion();
158 KItemListController::SelectionBehavior
KItemListController::selectionBehavior() const
160 return m_selectionBehavior
;
163 void KItemListController::setAutoActivationBehavior(AutoActivationBehavior behavior
)
165 m_autoActivationBehavior
= behavior
;
168 KItemListController::AutoActivationBehavior
KItemListController::autoActivationBehavior() const
170 return m_autoActivationBehavior
;
173 void KItemListController::setMouseDoubleClickAction(MouseDoubleClickAction action
)
175 m_mouseDoubleClickAction
= action
;
178 KItemListController::MouseDoubleClickAction
KItemListController::mouseDoubleClickAction() const
180 return m_mouseDoubleClickAction
;
183 void KItemListController::setAutoActivationDelay(int delay
)
185 m_autoActivationTimer
->setInterval(delay
);
188 int KItemListController::autoActivationDelay() const
190 return m_autoActivationTimer
->interval();
193 void KItemListController::setSingleClickActivationEnforced(bool singleClick
)
195 m_singleClickActivationEnforced
= singleClick
;
198 bool KItemListController::singleClickActivationEnforced() const
200 return m_singleClickActivationEnforced
;
203 bool KItemListController::showEvent(QShowEvent
* event
)
209 bool KItemListController::hideEvent(QHideEvent
* event
)
215 bool KItemListController::keyPressEvent(QKeyEvent
* event
)
217 int index
= m_selectionManager
->currentItem();
218 int key
= event
->key();
220 // Handle the expanding/collapsing of items
221 if (m_view
->supportsItemExpanding() && m_model
->isExpandable(index
)) {
222 if (key
== Qt::Key_Right
) {
223 if (m_model
->setExpanded(index
, true)) {
226 } else if (key
== Qt::Key_Left
) {
227 if (m_model
->setExpanded(index
, false)) {
233 const bool shiftPressed
= event
->modifiers() & Qt::ShiftModifier
;
234 const bool controlPressed
= event
->modifiers() & Qt::ControlModifier
;
235 const bool shiftOrControlPressed
= shiftPressed
|| controlPressed
;
237 const int itemCount
= m_model
->count();
239 // For horizontal scroll orientation, transform
240 // the arrow keys to simplify the event handling.
241 if (m_view
->scrollOrientation() == Qt::Horizontal
) {
243 case Qt::Key_Up
: key
= Qt::Key_Left
; break;
244 case Qt::Key_Down
: key
= Qt::Key_Right
; break;
245 case Qt::Key_Left
: key
= Qt::Key_Up
; break;
246 case Qt::Key_Right
: key
= Qt::Key_Down
; break;
251 const bool selectSingleItem
= m_selectionBehavior
!= NoSelection
&&
253 (key
== Qt::Key_Home
|| key
== Qt::Key_End
||
254 key
== Qt::Key_Up
|| key
== Qt::Key_Down
||
255 key
== Qt::Key_Left
|| key
== Qt::Key_Right
);
256 if (selectSingleItem
) {
257 const int current
= m_selectionManager
->currentItem();
258 m_selectionManager
->setSelected(current
);
265 m_keyboardAnchorIndex
= index
;
266 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
270 index
= itemCount
- 1;
271 m_keyboardAnchorIndex
= index
;
272 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
277 const int expandedParentsCount
= m_model
->expandedParentsCount(index
);
278 if (expandedParentsCount
== 0) {
281 // Go to the parent of the current item.
284 } while (index
> 0 && m_model
->expandedParentsCount(index
) == expandedParentsCount
);
286 m_keyboardAnchorIndex
= index
;
287 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
292 if (index
< itemCount
- 1) {
294 m_keyboardAnchorIndex
= index
;
295 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
300 updateKeyboardAnchor();
301 index
= previousRowIndex(index
);
305 updateKeyboardAnchor();
306 index
= nextRowIndex(index
);
310 if (m_view
->scrollOrientation() == Qt::Horizontal
) {
311 // The new current index should correspond to the first item in the current column.
312 int newIndex
= qMax(index
- 1, 0);
313 while (newIndex
!= index
&& m_view
->itemRect(newIndex
).topLeft().y() < m_view
->itemRect(index
).topLeft().y()) {
315 newIndex
= qMax(index
- 1, 0);
317 m_keyboardAnchorIndex
= index
;
318 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
320 const qreal currentItemBottom
= m_view
->itemRect(index
).bottomLeft().y();
321 const qreal height
= m_view
->geometry().height();
323 // The new current item should be the first item in the current
324 // column whose itemRect's top coordinate is larger than targetY.
325 const qreal targetY
= currentItemBottom
- height
;
327 updateKeyboardAnchor();
328 int newIndex
= previousRowIndex(index
);
331 updateKeyboardAnchor();
332 newIndex
= previousRowIndex(index
);
333 } while (m_view
->itemRect(newIndex
).topLeft().y() > targetY
&& newIndex
!= index
);
337 case Qt::Key_PageDown
:
338 if (m_view
->scrollOrientation() == Qt::Horizontal
) {
339 // The new current index should correspond to the last item in the current column.
340 int newIndex
= qMin(index
+ 1, m_model
->count() - 1);
341 while (newIndex
!= index
&& m_view
->itemRect(newIndex
).topLeft().y() > m_view
->itemRect(index
).topLeft().y()) {
343 newIndex
= qMin(index
+ 1, m_model
->count() - 1);
345 m_keyboardAnchorIndex
= index
;
346 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
348 const qreal currentItemTop
= m_view
->itemRect(index
).topLeft().y();
349 const qreal height
= m_view
->geometry().height();
351 // The new current item should be the last item in the current
352 // column whose itemRect's bottom coordinate is smaller than targetY.
353 const qreal targetY
= currentItemTop
+ height
;
355 updateKeyboardAnchor();
356 int newIndex
= nextRowIndex(index
);
359 updateKeyboardAnchor();
360 newIndex
= nextRowIndex(index
);
361 } while (m_view
->itemRect(newIndex
).bottomLeft().y() < targetY
&& newIndex
!= index
);
366 case Qt::Key_Return
: {
367 const QSet
<int> selectedItems
= m_selectionManager
->selectedItems();
368 if (selectedItems
.count() >= 2) {
369 emit
itemsActivated(selectedItems
);
370 } else if (selectedItems
.count() == 1) {
371 emit
itemActivated(selectedItems
.toList().first());
373 emit
itemActivated(index
);
379 if (m_selectionBehavior
== MultiSelection
) {
380 if (controlPressed
) {
381 m_selectionManager
->endAnchoredSelection();
382 m_selectionManager
->setSelected(index
, 1, KItemListSelectionManager::Toggle
);
383 m_selectionManager
->beginAnchoredSelection(index
);
385 const int current
= m_selectionManager
->currentItem();
386 m_selectionManager
->setSelected(current
);
392 // Emit the signal itemContextMenuRequested() in case if at least one
393 // item is selected. Otherwise the signal viewContextMenuRequested() will be emitted.
394 const QSet
<int> selectedItems
= m_selectionManager
->selectedItems();
396 if (selectedItems
.count() >= 2) {
397 const int currentItemIndex
= m_selectionManager
->currentItem();
398 index
= selectedItems
.contains(currentItemIndex
)
399 ? currentItemIndex
: selectedItems
.toList().first();
400 } else if (selectedItems
.count() == 1) {
401 index
= selectedItems
.toList().first();
405 const QRectF contextRect
= m_view
->itemContextRect(index
);
406 const QPointF
pos(m_view
->scene()->views().first()->mapToGlobal(contextRect
.bottomRight().toPoint()));
407 emit
itemContextMenuRequested(index
, pos
);
409 emit
viewContextMenuRequested(QCursor::pos());
415 if (m_selectionBehavior
!= SingleSelection
) {
416 m_selectionManager
->clearSelection();
418 m_keyboardManager
->cancelSearch();
422 m_keyboardManager
->addKeys(event
->text());
423 // Make sure unconsumed events get propagated up the chain. #302329
428 if (m_selectionManager
->currentItem() != index
) {
429 switch (m_selectionBehavior
) {
431 m_selectionManager
->setCurrentItem(index
);
434 case SingleSelection
:
435 m_selectionManager
->setCurrentItem(index
);
436 m_selectionManager
->clearSelection();
437 m_selectionManager
->setSelected(index
, 1);
441 if (controlPressed
) {
442 m_selectionManager
->endAnchoredSelection();
445 m_selectionManager
->setCurrentItem(index
);
447 if (!shiftOrControlPressed
) {
448 m_selectionManager
->clearSelection();
449 m_selectionManager
->setSelected(index
, 1);
453 m_selectionManager
->beginAnchoredSelection(index
);
458 m_view
->scrollToItem(index
);
463 void KItemListController::slotChangeCurrentItem(const QString
& text
, bool searchFromNextItem
)
465 if (!m_model
|| m_model
->count() == 0) {
468 const int currentIndex
= m_selectionManager
->currentItem();
470 if (searchFromNextItem
) {
471 index
= m_model
->indexForKeyboardSearch(text
, (currentIndex
+ 1) % m_model
->count());
473 index
= m_model
->indexForKeyboardSearch(text
, currentIndex
);
476 m_selectionManager
->setCurrentItem(index
);
477 m_selectionManager
->clearSelection();
478 m_selectionManager
->setSelected(index
, 1);
479 m_selectionManager
->beginAnchoredSelection(index
);
480 m_view
->scrollToItem(index
);
484 void KItemListController::slotAutoActivationTimeout()
486 if (!m_model
|| !m_view
) {
490 const int index
= m_autoActivationTimer
->property("index").toInt();
491 if (index
< 0 || index
>= m_model
->count()) {
495 /* m_view->isUnderMouse() fixes a bug in the Folder-View-Panel and in the
498 * Bug: When you drag a file onto a Folder-View-Item or a Places-Item and
499 * then move away before the auto-activation timeout triggers, than the
500 * item still becomes activated/expanded.
502 * See Bug 293200 and 305783
504 if (m_model
->supportsDropping(index
) && m_view
->isUnderMouse()) {
505 if (m_view
->supportsItemExpanding() && m_model
->isExpandable(index
)) {
506 const bool expanded
= m_model
->isExpanded(index
);
507 m_model
->setExpanded(index
, !expanded
);
508 } else if (m_autoActivationBehavior
!= ExpansionOnly
) {
509 emit
itemActivated(index
);
514 bool KItemListController::inputMethodEvent(QInputMethodEvent
* event
)
520 bool KItemListController::mousePressEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
526 m_pressedMousePos
= transform
.map(event
->pos());
527 m_pressedIndex
= m_view
->itemAt(m_pressedMousePos
);
528 emit
mouseButtonPressed(m_pressedIndex
, event
->buttons());
530 if (m_view
->isAboveExpansionToggle(m_pressedIndex
, m_pressedMousePos
)) {
531 m_selectionManager
->endAnchoredSelection();
532 m_selectionManager
->setCurrentItem(m_pressedIndex
);
533 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
537 m_selectionTogglePressed
= m_view
->isAboveSelectionToggle(m_pressedIndex
, m_pressedMousePos
);
538 if (m_selectionTogglePressed
) {
539 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
540 // The previous anchored selection has been finished already in
541 // KItemListSelectionManager::setSelected(). We can safely change
542 // the current item and start a new anchored selection now.
543 m_selectionManager
->setCurrentItem(m_pressedIndex
);
544 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
548 const bool shiftPressed
= event
->modifiers() & Qt::ShiftModifier
;
549 const bool controlPressed
= event
->modifiers() & Qt::ControlModifier
;
551 // The previous selection is cleared if either
552 // 1. The selection mode is SingleSelection, or
553 // 2. the selection mode is MultiSelection, and *none* of the following conditions are met:
554 // a) Shift or Control are pressed.
555 // b) The clicked item is selected already. In that case, the user might want to:
556 // - start dragging multiple items, or
557 // - open the context menu and perform an action for all selected items.
558 const bool shiftOrControlPressed
= shiftPressed
|| controlPressed
;
559 const bool pressedItemAlreadySelected
= m_pressedIndex
>= 0 && m_selectionManager
->isSelected(m_pressedIndex
);
560 const bool clearSelection
= m_selectionBehavior
== SingleSelection
||
561 (!shiftOrControlPressed
&& !pressedItemAlreadySelected
);
562 if (clearSelection
) {
563 m_selectionManager
->clearSelection();
564 } else if (pressedItemAlreadySelected
&& !shiftOrControlPressed
&& (event
->buttons() & Qt::LeftButton
)) {
565 // The user might want to start dragging multiple items, but if he clicks the item
566 // in order to trigger it instead, the other selected items must be deselected.
567 // However, we do not know yet what the user is going to do.
568 // -> remember that the user pressed an item which had been selected already and
569 // clear the selection in mouseReleaseEvent(), unless the items are dragged.
570 m_clearSelectionIfItemsAreNotDragged
= true;
574 // Finish the anchored selection before the current index is changed
575 m_selectionManager
->endAnchoredSelection();
578 if (m_pressedIndex
>= 0) {
579 m_selectionManager
->setCurrentItem(m_pressedIndex
);
581 switch (m_selectionBehavior
) {
585 case SingleSelection
:
586 m_selectionManager
->setSelected(m_pressedIndex
);
590 if (controlPressed
&& !shiftPressed
) {
591 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
592 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
593 } else if (!shiftPressed
|| !m_selectionManager
->isAnchoredSelectionActive()) {
594 // Select the pressed item and start a new anchored selection
595 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Select
);
596 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
605 if (event
->buttons() & Qt::RightButton
) {
606 emit
itemContextMenuRequested(m_pressedIndex
, event
->screenPos());
612 if (event
->buttons() & Qt::RightButton
) {
613 const QRectF headerBounds
= m_view
->headerBoundaries();
614 if (headerBounds
.contains(event
->pos())) {
615 emit
headerContextMenuRequested(event
->screenPos());
617 emit
viewContextMenuRequested(event
->screenPos());
622 if (m_selectionBehavior
== MultiSelection
) {
623 QPointF startPos
= m_pressedMousePos
;
624 if (m_view
->scrollOrientation() == Qt::Vertical
) {
625 startPos
.ry() += m_view
->scrollOffset();
626 if (m_view
->itemSize().width() < 0) {
627 // Use a special rubberband for views that have only one column and
628 // expand the rubberband to use the whole width of the view.
632 startPos
.rx() += m_view
->scrollOffset();
635 m_oldSelection
= m_selectionManager
->selectedItems();
636 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
637 rubberBand
->setStartPosition(startPos
);
638 rubberBand
->setEndPosition(startPos
);
639 rubberBand
->setActive(true);
640 connect(rubberBand
, SIGNAL(endPositionChanged(QPointF
,QPointF
)), this, SLOT(slotRubberBandChanged()));
641 m_view
->setAutoScroll(true);
647 bool KItemListController::mouseMoveEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
653 if (m_pressedIndex
>= 0) {
654 // Check whether a dragging should be started
655 if (event
->buttons() & Qt::LeftButton
) {
656 const QPointF pos
= transform
.map(event
->pos());
657 if ((pos
- m_pressedMousePos
).manhattanLength() >= QApplication::startDragDistance()) {
658 if (!m_selectionManager
->isSelected(m_pressedIndex
)) {
659 // Always assure that the dragged item gets selected. Usually this is already
660 // done on the mouse-press event, but when using the selection-toggle on a
661 // selected item the dragged item is not selected yet.
662 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
664 // A selected item has been clicked to drag all selected items
665 // -> the selection should not be cleared when the mouse button is released.
666 m_clearSelectionIfItemsAreNotDragged
= false;
673 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
674 if (rubberBand
->isActive()) {
675 QPointF endPos
= transform
.map(event
->pos());
677 // Update the current item.
678 const int newCurrent
= m_view
->itemAt(endPos
);
679 if (newCurrent
>= 0) {
680 // It's expected that the new current index is also the new anchor (bug 163451).
681 m_selectionManager
->endAnchoredSelection();
682 m_selectionManager
->setCurrentItem(newCurrent
);
683 m_selectionManager
->beginAnchoredSelection(newCurrent
);
686 if (m_view
->scrollOrientation() == Qt::Vertical
) {
687 endPos
.ry() += m_view
->scrollOffset();
688 if (m_view
->itemSize().width() < 0) {
689 // Use a special rubberband for views that have only one column and
690 // expand the rubberband to use the whole width of the view.
691 endPos
.setX(m_view
->size().width());
694 endPos
.rx() += m_view
->scrollOffset();
696 rubberBand
->setEndPosition(endPos
);
703 bool KItemListController::mouseReleaseEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
709 emit
mouseButtonReleased(m_pressedIndex
, event
->buttons());
711 const bool isAboveSelectionToggle
= m_view
->isAboveSelectionToggle(m_pressedIndex
, m_pressedMousePos
);
712 if (isAboveSelectionToggle
) {
713 m_selectionTogglePressed
= false;
717 if (!isAboveSelectionToggle
&& m_selectionTogglePressed
) {
718 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
719 m_selectionTogglePressed
= false;
723 const bool shiftOrControlPressed
= event
->modifiers() & Qt::ShiftModifier
||
724 event
->modifiers() & Qt::ControlModifier
;
726 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
727 if (rubberBand
->isActive()) {
728 disconnect(rubberBand
, SIGNAL(endPositionChanged(QPointF
,QPointF
)), this, SLOT(slotRubberBandChanged()));
729 rubberBand
->setActive(false);
730 m_oldSelection
.clear();
731 m_view
->setAutoScroll(false);
734 const QPointF pos
= transform
.map(event
->pos());
735 const int index
= m_view
->itemAt(pos
);
737 if (index
>= 0 && index
== m_pressedIndex
) {
738 // The release event is done above the same item as the press event
740 if (m_clearSelectionIfItemsAreNotDragged
) {
741 // A selected item has been clicked, but no drag operation has been started
742 // -> clear the rest of the selection.
743 m_selectionManager
->clearSelection();
744 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Select
);
745 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
748 if (event
->button() & Qt::LeftButton
) {
749 bool emitItemActivated
= true;
750 if (m_view
->isAboveExpansionToggle(index
, pos
)) {
751 const bool expanded
= m_model
->isExpanded(index
);
752 m_model
->setExpanded(index
, !expanded
);
754 emit
itemExpansionToggleClicked(index
);
755 emitItemActivated
= false;
756 } else if (shiftOrControlPressed
) {
757 // The mouse click should only update the selection, not trigger the item
758 emitItemActivated
= false;
759 } else if (!(KGlobalSettings::singleClick() || m_singleClickActivationEnforced
)) {
760 emitItemActivated
= false;
762 if (emitItemActivated
) {
763 emit
itemActivated(index
);
765 } else if (event
->button() & Qt::MidButton
) {
766 emit
itemMiddleClicked(index
);
770 m_pressedMousePos
= QPointF();
772 m_clearSelectionIfItemsAreNotDragged
= false;
776 bool KItemListController::mouseDoubleClickEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
778 const QPointF pos
= transform
.map(event
->pos());
779 const int index
= m_view
->itemAt(pos
);
781 // Expand item if desired - See Bug 295573
782 if (m_mouseDoubleClickAction
!= ActivateItemOnly
) {
783 if (m_view
&& m_model
&& m_view
->supportsItemExpanding() && m_model
->isExpandable(index
)) {
784 const bool expanded
= m_model
->isExpanded(index
);
785 m_model
->setExpanded(index
, !expanded
);
789 bool emitItemActivated
= !(KGlobalSettings::singleClick() || m_singleClickActivationEnforced
) &&
790 (event
->button() & Qt::LeftButton
) &&
791 index
>= 0 && index
< m_model
->count();
792 if (emitItemActivated
) {
793 emit
itemActivated(index
);
798 bool KItemListController::dragEnterEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
805 bool KItemListController::dragLeaveEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
810 m_view
->setAutoScroll(false);
811 m_view
->hideDropIndicator();
813 KItemListWidget
* widget
= hoveredWidget();
815 widget
->setHovered(false);
816 emit
itemUnhovered(widget
->index());
821 bool KItemListController::dragMoveEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
823 if (!m_model
|| !m_view
) {
827 event
->acceptProposedAction();
829 KItemListWidget
* oldHoveredWidget
= hoveredWidget();
831 const QPointF pos
= transform
.map(event
->pos());
832 KItemListWidget
* newHoveredWidget
= widgetForPos(pos
);
834 if (oldHoveredWidget
!= newHoveredWidget
) {
835 m_autoActivationTimer
->stop();
837 if (oldHoveredWidget
) {
838 oldHoveredWidget
->setHovered(false);
839 emit
itemUnhovered(oldHoveredWidget
->index());
842 if (newHoveredWidget
) {
843 bool droppingBetweenItems
= false;
844 if (m_model
->sortRole().isEmpty()) {
845 // The model supports inserting items between other items.
846 droppingBetweenItems
= (m_view
->showDropIndicator(pos
) >= 0);
849 const int index
= newHoveredWidget
->index();
850 if (!droppingBetweenItems
&& m_model
->supportsDropping(index
)) {
851 // Something has been dragged on an item.
852 m_view
->hideDropIndicator();
853 newHoveredWidget
->setHovered(true);
854 emit
itemHovered(index
);
856 if (m_autoActivationTimer
->interval() >= 0) {
857 m_autoActivationTimer
->setProperty("index", index
);
858 m_autoActivationTimer
->start();
867 bool KItemListController::dropEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
873 m_autoActivationTimer
->stop();
874 m_view
->setAutoScroll(false);
876 const QPointF pos
= transform
.map(event
->pos());
878 int dropAboveIndex
= -1;
879 if (m_model
->sortRole().isEmpty()) {
880 // The model supports inserting of items between other items.
881 dropAboveIndex
= m_view
->showDropIndicator(pos
);
884 if (dropAboveIndex
>= 0) {
885 // Something has been dropped between two items.
886 m_view
->hideDropIndicator();
887 emit
aboveItemDropEvent(dropAboveIndex
, event
);
889 // Something has been dropped on an item or on an empty part of the view.
890 emit
itemDropEvent(m_view
->itemAt(pos
), event
);
893 QAccessible::updateAccessibility(view(), 0, QAccessible::DragDropEnd
);
898 bool KItemListController::hoverEnterEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
905 bool KItemListController::hoverMoveEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
908 if (!m_model
|| !m_view
) {
912 KItemListWidget
* oldHoveredWidget
= hoveredWidget();
913 const QPointF pos
= transform
.map(event
->pos());
914 KItemListWidget
* newHoveredWidget
= widgetForPos(pos
);
916 if (oldHoveredWidget
!= newHoveredWidget
) {
917 if (oldHoveredWidget
) {
918 oldHoveredWidget
->setHovered(false);
919 emit
itemUnhovered(oldHoveredWidget
->index());
922 if (newHoveredWidget
) {
923 newHoveredWidget
->setHovered(true);
924 emit
itemHovered(newHoveredWidget
->index());
931 bool KItemListController::hoverLeaveEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
936 if (!m_model
|| !m_view
) {
940 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
941 if (widget
->isHovered()) {
942 widget
->setHovered(false);
943 emit
itemUnhovered(widget
->index());
949 bool KItemListController::wheelEvent(QGraphicsSceneWheelEvent
* event
, const QTransform
& transform
)
956 bool KItemListController::resizeEvent(QGraphicsSceneResizeEvent
* event
, const QTransform
& transform
)
963 bool KItemListController::processEvent(QEvent
* event
, const QTransform
& transform
)
969 switch (event
->type()) {
970 case QEvent::KeyPress
:
971 return keyPressEvent(static_cast<QKeyEvent
*>(event
));
972 case QEvent::InputMethod
:
973 return inputMethodEvent(static_cast<QInputMethodEvent
*>(event
));
974 case QEvent::GraphicsSceneMousePress
:
975 return mousePressEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
976 case QEvent::GraphicsSceneMouseMove
:
977 return mouseMoveEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
978 case QEvent::GraphicsSceneMouseRelease
:
979 return mouseReleaseEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
980 case QEvent::GraphicsSceneMouseDoubleClick
:
981 return mouseDoubleClickEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
982 case QEvent::GraphicsSceneWheel
:
983 return wheelEvent(static_cast<QGraphicsSceneWheelEvent
*>(event
), QTransform());
984 case QEvent::GraphicsSceneDragEnter
:
985 return dragEnterEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
986 case QEvent::GraphicsSceneDragLeave
:
987 return dragLeaveEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
988 case QEvent::GraphicsSceneDragMove
:
989 return dragMoveEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
990 case QEvent::GraphicsSceneDrop
:
991 return dropEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
992 case QEvent::GraphicsSceneHoverEnter
:
993 return hoverEnterEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
994 case QEvent::GraphicsSceneHoverMove
:
995 return hoverMoveEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
996 case QEvent::GraphicsSceneHoverLeave
:
997 return hoverLeaveEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
998 case QEvent::GraphicsSceneResize
:
999 return resizeEvent(static_cast<QGraphicsSceneResizeEvent
*>(event
), transform
);
1007 void KItemListController::slotViewScrollOffsetChanged(qreal current
, qreal previous
)
1013 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
1014 if (rubberBand
->isActive()) {
1015 const qreal diff
= current
- previous
;
1016 // TODO: Ideally just QCursor::pos() should be used as
1017 // new end-position but it seems there is no easy way
1018 // to have something like QWidget::mapFromGlobal() for QGraphicsWidget
1019 // (... or I just missed an easy way to do the mapping)
1020 QPointF endPos
= rubberBand
->endPosition();
1021 if (m_view
->scrollOrientation() == Qt::Vertical
) {
1022 endPos
.ry() += diff
;
1024 endPos
.rx() += diff
;
1027 rubberBand
->setEndPosition(endPos
);
1031 void KItemListController::slotRubberBandChanged()
1033 if (!m_view
|| !m_model
|| m_model
->count() <= 0) {
1037 const KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
1038 const QPointF startPos
= rubberBand
->startPosition();
1039 const QPointF endPos
= rubberBand
->endPosition();
1040 QRectF rubberBandRect
= QRectF(startPos
, endPos
).normalized();
1042 const bool scrollVertical
= (m_view
->scrollOrientation() == Qt::Vertical
);
1043 if (scrollVertical
) {
1044 rubberBandRect
.translate(0, -m_view
->scrollOffset());
1046 rubberBandRect
.translate(-m_view
->scrollOffset(), 0);
1049 if (!m_oldSelection
.isEmpty()) {
1050 // Clear the old selection that was available before the rubberband has
1051 // been activated in case if no Shift- or Control-key are pressed
1052 const bool shiftOrControlPressed
= QApplication::keyboardModifiers() & Qt::ShiftModifier
||
1053 QApplication::keyboardModifiers() & Qt::ControlModifier
;
1054 if (!shiftOrControlPressed
) {
1055 m_oldSelection
.clear();
1059 QSet
<int> selectedItems
;
1061 // Select all visible items that intersect with the rubberband
1062 foreach (const KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
1063 const int index
= widget
->index();
1065 const QRectF widgetRect
= m_view
->itemRect(index
);
1066 if (widgetRect
.intersects(rubberBandRect
)) {
1067 const QRectF iconRect
= widget
->iconRect().translated(widgetRect
.topLeft());
1068 const QRectF textRect
= widget
->textRect().translated(widgetRect
.topLeft());
1069 if (iconRect
.intersects(rubberBandRect
) || textRect
.intersects(rubberBandRect
)) {
1070 selectedItems
.insert(index
);
1075 // Select all invisible items that intersect with the rubberband. Instead of
1076 // iterating all items only the area which might be touched by the rubberband
1078 const bool increaseIndex
= scrollVertical
?
1079 startPos
.y() > endPos
.y(): startPos
.x() > endPos
.x();
1081 int index
= increaseIndex
? m_view
->lastVisibleIndex() + 1 : m_view
->firstVisibleIndex() - 1;
1082 bool selectionFinished
= false;
1084 const QRectF widgetRect
= m_view
->itemRect(index
);
1085 if (widgetRect
.intersects(rubberBandRect
)) {
1086 selectedItems
.insert(index
);
1089 if (increaseIndex
) {
1091 selectionFinished
= (index
>= m_model
->count()) ||
1092 ( scrollVertical
&& widgetRect
.top() > rubberBandRect
.bottom()) ||
1093 (!scrollVertical
&& widgetRect
.left() > rubberBandRect
.right());
1096 selectionFinished
= (index
< 0) ||
1097 ( scrollVertical
&& widgetRect
.bottom() < rubberBandRect
.top()) ||
1098 (!scrollVertical
&& widgetRect
.right() < rubberBandRect
.left());
1100 } while (!selectionFinished
);
1102 if (QApplication::keyboardModifiers() & Qt::ControlModifier
) {
1103 // If Control is pressed, the selection state of all items in the rubberband is toggled.
1104 // Therefore, the new selection contains:
1105 // 1. All previously selected items which are not inside the rubberband, and
1106 // 2. all items inside the rubberband which have not been selected previously.
1107 m_selectionManager
->setSelectedItems((m_oldSelection
- selectedItems
) + (selectedItems
- m_oldSelection
));
1110 m_selectionManager
->setSelectedItems(selectedItems
+ m_oldSelection
);
1114 void KItemListController::startDragging()
1116 if (!m_view
|| !m_model
) {
1120 const QSet
<int> selectedItems
= m_selectionManager
->selectedItems();
1121 if (selectedItems
.isEmpty()) {
1125 QMimeData
* data
= m_model
->createMimeData(selectedItems
);
1130 // The created drag object will be owned and deleted
1131 // by QApplication::activeWindow().
1132 QDrag
* drag
= new QDrag(QApplication::activeWindow());
1133 drag
->setMimeData(data
);
1135 const QPixmap pixmap
= m_view
->createDragPixmap(selectedItems
);
1136 drag
->setPixmap(pixmap
);
1138 const QPoint
hotSpot(pixmap
.width() / 2, 0);
1139 drag
->setHotSpot(hotSpot
);
1141 drag
->exec(Qt::MoveAction
| Qt::CopyAction
| Qt::LinkAction
, Qt::CopyAction
);
1142 QAccessible::updateAccessibility(view(), 0, QAccessible::DragDropStart
);
1145 KItemListWidget
* KItemListController::hoveredWidget() const
1149 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
1150 if (widget
->isHovered()) {
1158 KItemListWidget
* KItemListController::widgetForPos(const QPointF
& pos
) const
1162 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
1163 const QPointF mappedPos
= widget
->mapFromItem(m_view
, pos
);
1165 const bool hovered
= widget
->contains(mappedPos
) &&
1166 !widget
->expansionToggleRect().contains(mappedPos
);
1175 void KItemListController::updateKeyboardAnchor()
1177 const bool validAnchor
= m_keyboardAnchorIndex
>= 0 &&
1178 m_keyboardAnchorIndex
< m_model
->count() &&
1179 keyboardAnchorPos(m_keyboardAnchorIndex
) == m_keyboardAnchorPos
;
1181 const int index
= m_selectionManager
->currentItem();
1182 m_keyboardAnchorIndex
= index
;
1183 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
1187 int KItemListController::nextRowIndex(int index
) const
1189 if (m_keyboardAnchorIndex
< 0) {
1193 const int maxIndex
= m_model
->count() - 1;
1194 if (index
== maxIndex
) {
1198 // Calculate the index of the last column inside the row of the current index
1199 int lastColumnIndex
= index
;
1200 while (keyboardAnchorPos(lastColumnIndex
+ 1) > keyboardAnchorPos(lastColumnIndex
)) {
1202 if (lastColumnIndex
>= maxIndex
) {
1207 // Based on the last column index go to the next row and calculate the nearest index
1208 // that is below the current index
1209 int nextRowIndex
= lastColumnIndex
+ 1;
1210 int searchIndex
= nextRowIndex
;
1211 qreal minDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(nextRowIndex
));
1212 while (searchIndex
< maxIndex
&& keyboardAnchorPos(searchIndex
+ 1) > keyboardAnchorPos(searchIndex
)) {
1214 const qreal searchDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(searchIndex
));
1215 if (searchDiff
< minDiff
) {
1216 minDiff
= searchDiff
;
1217 nextRowIndex
= searchIndex
;
1221 return nextRowIndex
;
1224 int KItemListController::previousRowIndex(int index
) const
1226 if (m_keyboardAnchorIndex
< 0 || index
== 0) {
1230 // Calculate the index of the first column inside the row of the current index
1231 int firstColumnIndex
= index
;
1232 while (keyboardAnchorPos(firstColumnIndex
- 1) < keyboardAnchorPos(firstColumnIndex
)) {
1234 if (firstColumnIndex
<= 0) {
1239 // Based on the first column index go to the previous row and calculate the nearest index
1240 // that is above the current index
1241 int previousRowIndex
= firstColumnIndex
- 1;
1242 int searchIndex
= previousRowIndex
;
1243 qreal minDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(previousRowIndex
));
1244 while (searchIndex
> 0 && keyboardAnchorPos(searchIndex
- 1) < keyboardAnchorPos(searchIndex
)) {
1246 const qreal searchDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(searchIndex
));
1247 if (searchDiff
< minDiff
) {
1248 minDiff
= searchDiff
;
1249 previousRowIndex
= searchIndex
;
1253 return previousRowIndex
;
1256 qreal
KItemListController::keyboardAnchorPos(int index
) const
1258 const QRectF itemRect
= m_view
->itemRect(index
);
1259 if (!itemRect
.isEmpty()) {
1260 return (m_view
->scrollOrientation() == Qt::Vertical
) ? itemRect
.x() : itemRect
.y();
1266 void KItemListController::updateExtendedSelectionRegion()
1269 const bool extend
= (m_selectionBehavior
!= MultiSelection
);
1270 KItemListStyleOption option
= m_view
->styleOption();
1271 if (option
.extendedSelectionRegion
!= extend
) {
1272 option
.extendedSelectionRegion
= extend
;
1273 m_view
->setStyleOption(option
);
1278 #include "kitemlistcontroller.moc"