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 // Emit the signal itemContextMenuRequested() in case if at least one
380 // item is selected. Otherwise the signal viewContextMenuRequested() will be emitted.
381 const QSet
<int> selectedItems
= m_selectionManager
->selectedItems();
383 if (selectedItems
.count() >= 2) {
384 const int currentItemIndex
= m_selectionManager
->currentItem();
385 index
= selectedItems
.contains(currentItemIndex
)
386 ? currentItemIndex
: selectedItems
.toList().first();
387 } else if (selectedItems
.count() == 1) {
388 index
= selectedItems
.toList().first();
392 const QRectF contextRect
= m_view
->itemContextRect(index
);
393 const QPointF
pos(m_view
->scene()->views().first()->mapToGlobal(contextRect
.bottomRight().toPoint()));
394 emit
itemContextMenuRequested(index
, pos
);
396 emit
viewContextMenuRequested(QCursor::pos());
402 if (m_selectionBehavior
!= SingleSelection
) {
403 m_selectionManager
->clearSelection();
405 m_keyboardManager
->cancelSearch();
409 if (m_selectionBehavior
== MultiSelection
) {
410 if (controlPressed
) {
411 // Toggle the selection state of the current item.
412 m_selectionManager
->endAnchoredSelection();
413 m_selectionManager
->setSelected(index
, 1, KItemListSelectionManager::Toggle
);
414 m_selectionManager
->beginAnchoredSelection(index
);
417 // Select the current item if it is not selected yet.
418 const int current
= m_selectionManager
->currentItem();
419 if (!m_selectionManager
->isSelected(current
)) {
420 m_selectionManager
->setSelected(current
);
425 // Fall through to the default case and add the Space to the current search string.
428 m_keyboardManager
->addKeys(event
->text());
429 // Make sure unconsumed events get propagated up the chain. #302329
434 if (m_selectionManager
->currentItem() != index
) {
435 switch (m_selectionBehavior
) {
437 m_selectionManager
->setCurrentItem(index
);
440 case SingleSelection
:
441 m_selectionManager
->setCurrentItem(index
);
442 m_selectionManager
->clearSelection();
443 m_selectionManager
->setSelected(index
, 1);
447 if (controlPressed
) {
448 m_selectionManager
->endAnchoredSelection();
451 m_selectionManager
->setCurrentItem(index
);
453 if (!shiftOrControlPressed
) {
454 m_selectionManager
->clearSelection();
455 m_selectionManager
->setSelected(index
, 1);
459 m_selectionManager
->beginAnchoredSelection(index
);
464 m_view
->scrollToItem(index
);
469 void KItemListController::slotChangeCurrentItem(const QString
& text
, bool searchFromNextItem
)
471 if (!m_model
|| m_model
->count() == 0) {
474 const int currentIndex
= m_selectionManager
->currentItem();
476 if (searchFromNextItem
) {
477 index
= m_model
->indexForKeyboardSearch(text
, (currentIndex
+ 1) % m_model
->count());
479 index
= m_model
->indexForKeyboardSearch(text
, currentIndex
);
482 m_selectionManager
->setCurrentItem(index
);
484 if (m_selectionBehavior
!= NoSelection
) {
485 m_selectionManager
->clearSelection();
486 m_selectionManager
->setSelected(index
, 1);
487 m_selectionManager
->beginAnchoredSelection(index
);
490 m_view
->scrollToItem(index
);
494 void KItemListController::slotAutoActivationTimeout()
496 if (!m_model
|| !m_view
) {
500 const int index
= m_autoActivationTimer
->property("index").toInt();
501 if (index
< 0 || index
>= m_model
->count()) {
505 /* m_view->isUnderMouse() fixes a bug in the Folder-View-Panel and in the
508 * Bug: When you drag a file onto a Folder-View-Item or a Places-Item and
509 * then move away before the auto-activation timeout triggers, than the
510 * item still becomes activated/expanded.
512 * See Bug 293200 and 305783
514 if (m_model
->supportsDropping(index
) && m_view
->isUnderMouse()) {
515 if (m_view
->supportsItemExpanding() && m_model
->isExpandable(index
)) {
516 const bool expanded
= m_model
->isExpanded(index
);
517 m_model
->setExpanded(index
, !expanded
);
518 } else if (m_autoActivationBehavior
!= ExpansionOnly
) {
519 emit
itemActivated(index
);
524 bool KItemListController::inputMethodEvent(QInputMethodEvent
* event
)
530 bool KItemListController::mousePressEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
536 m_pressedMousePos
= transform
.map(event
->pos());
537 m_pressedIndex
= m_view
->itemAt(m_pressedMousePos
);
538 emit
mouseButtonPressed(m_pressedIndex
, event
->buttons());
540 if (m_view
->isAboveExpansionToggle(m_pressedIndex
, m_pressedMousePos
)) {
541 m_selectionManager
->endAnchoredSelection();
542 m_selectionManager
->setCurrentItem(m_pressedIndex
);
543 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
547 m_selectionTogglePressed
= m_view
->isAboveSelectionToggle(m_pressedIndex
, m_pressedMousePos
);
548 if (m_selectionTogglePressed
) {
549 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
550 // The previous anchored selection has been finished already in
551 // KItemListSelectionManager::setSelected(). We can safely change
552 // the current item and start a new anchored selection now.
553 m_selectionManager
->setCurrentItem(m_pressedIndex
);
554 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
558 const bool shiftPressed
= event
->modifiers() & Qt::ShiftModifier
;
559 const bool controlPressed
= event
->modifiers() & Qt::ControlModifier
;
561 // The previous selection is cleared if either
562 // 1. The selection mode is SingleSelection, or
563 // 2. the selection mode is MultiSelection, and *none* of the following conditions are met:
564 // a) Shift or Control are pressed.
565 // b) The clicked item is selected already. In that case, the user might want to:
566 // - start dragging multiple items, or
567 // - open the context menu and perform an action for all selected items.
568 const bool shiftOrControlPressed
= shiftPressed
|| controlPressed
;
569 const bool pressedItemAlreadySelected
= m_pressedIndex
>= 0 && m_selectionManager
->isSelected(m_pressedIndex
);
570 const bool clearSelection
= m_selectionBehavior
== SingleSelection
||
571 (!shiftOrControlPressed
&& !pressedItemAlreadySelected
);
572 if (clearSelection
) {
573 m_selectionManager
->clearSelection();
574 } else if (pressedItemAlreadySelected
&& !shiftOrControlPressed
&& (event
->buttons() & Qt::LeftButton
)) {
575 // The user might want to start dragging multiple items, but if he clicks the item
576 // in order to trigger it instead, the other selected items must be deselected.
577 // However, we do not know yet what the user is going to do.
578 // -> remember that the user pressed an item which had been selected already and
579 // clear the selection in mouseReleaseEvent(), unless the items are dragged.
580 m_clearSelectionIfItemsAreNotDragged
= true;
584 // Finish the anchored selection before the current index is changed
585 m_selectionManager
->endAnchoredSelection();
588 if (m_pressedIndex
>= 0) {
589 m_selectionManager
->setCurrentItem(m_pressedIndex
);
591 switch (m_selectionBehavior
) {
595 case SingleSelection
:
596 m_selectionManager
->setSelected(m_pressedIndex
);
600 if (controlPressed
&& !shiftPressed
) {
601 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
602 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
603 } else if (!shiftPressed
|| !m_selectionManager
->isAnchoredSelectionActive()) {
604 // Select the pressed item and start a new anchored selection
605 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Select
);
606 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
615 if (event
->buttons() & Qt::RightButton
) {
616 emit
itemContextMenuRequested(m_pressedIndex
, event
->screenPos());
622 if (event
->buttons() & Qt::RightButton
) {
623 const QRectF headerBounds
= m_view
->headerBoundaries();
624 if (headerBounds
.contains(event
->pos())) {
625 emit
headerContextMenuRequested(event
->screenPos());
627 emit
viewContextMenuRequested(event
->screenPos());
632 if (m_selectionBehavior
== MultiSelection
) {
633 QPointF startPos
= m_pressedMousePos
;
634 if (m_view
->scrollOrientation() == Qt::Vertical
) {
635 startPos
.ry() += m_view
->scrollOffset();
636 if (m_view
->itemSize().width() < 0) {
637 // Use a special rubberband for views that have only one column and
638 // expand the rubberband to use the whole width of the view.
642 startPos
.rx() += m_view
->scrollOffset();
645 m_oldSelection
= m_selectionManager
->selectedItems();
646 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
647 rubberBand
->setStartPosition(startPos
);
648 rubberBand
->setEndPosition(startPos
);
649 rubberBand
->setActive(true);
650 connect(rubberBand
, SIGNAL(endPositionChanged(QPointF
,QPointF
)), this, SLOT(slotRubberBandChanged()));
651 m_view
->setAutoScroll(true);
657 bool KItemListController::mouseMoveEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
663 if (m_pressedIndex
>= 0) {
664 // Check whether a dragging should be started
665 if (event
->buttons() & Qt::LeftButton
) {
666 const QPointF pos
= transform
.map(event
->pos());
667 if ((pos
- m_pressedMousePos
).manhattanLength() >= QApplication::startDragDistance()) {
668 if (!m_selectionManager
->isSelected(m_pressedIndex
)) {
669 // Always assure that the dragged item gets selected. Usually this is already
670 // done on the mouse-press event, but when using the selection-toggle on a
671 // selected item the dragged item is not selected yet.
672 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
674 // A selected item has been clicked to drag all selected items
675 // -> the selection should not be cleared when the mouse button is released.
676 m_clearSelectionIfItemsAreNotDragged
= false;
683 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
684 if (rubberBand
->isActive()) {
685 QPointF endPos
= transform
.map(event
->pos());
687 // Update the current item.
688 const int newCurrent
= m_view
->itemAt(endPos
);
689 if (newCurrent
>= 0) {
690 // It's expected that the new current index is also the new anchor (bug 163451).
691 m_selectionManager
->endAnchoredSelection();
692 m_selectionManager
->setCurrentItem(newCurrent
);
693 m_selectionManager
->beginAnchoredSelection(newCurrent
);
696 if (m_view
->scrollOrientation() == Qt::Vertical
) {
697 endPos
.ry() += m_view
->scrollOffset();
698 if (m_view
->itemSize().width() < 0) {
699 // Use a special rubberband for views that have only one column and
700 // expand the rubberband to use the whole width of the view.
701 endPos
.setX(m_view
->size().width());
704 endPos
.rx() += m_view
->scrollOffset();
706 rubberBand
->setEndPosition(endPos
);
713 bool KItemListController::mouseReleaseEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
719 emit
mouseButtonReleased(m_pressedIndex
, event
->buttons());
721 const bool isAboveSelectionToggle
= m_view
->isAboveSelectionToggle(m_pressedIndex
, m_pressedMousePos
);
722 if (isAboveSelectionToggle
) {
723 m_selectionTogglePressed
= false;
727 if (!isAboveSelectionToggle
&& m_selectionTogglePressed
) {
728 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
729 m_selectionTogglePressed
= false;
733 const bool shiftOrControlPressed
= event
->modifiers() & Qt::ShiftModifier
||
734 event
->modifiers() & Qt::ControlModifier
;
736 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
737 if (rubberBand
->isActive()) {
738 disconnect(rubberBand
, SIGNAL(endPositionChanged(QPointF
,QPointF
)), this, SLOT(slotRubberBandChanged()));
739 rubberBand
->setActive(false);
740 m_oldSelection
.clear();
741 m_view
->setAutoScroll(false);
744 const QPointF pos
= transform
.map(event
->pos());
745 const int index
= m_view
->itemAt(pos
);
747 if (index
>= 0 && index
== m_pressedIndex
) {
748 // The release event is done above the same item as the press event
750 if (m_clearSelectionIfItemsAreNotDragged
) {
751 // A selected item has been clicked, but no drag operation has been started
752 // -> clear the rest of the selection.
753 m_selectionManager
->clearSelection();
754 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Select
);
755 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
758 if (event
->button() & Qt::LeftButton
) {
759 bool emitItemActivated
= true;
760 if (m_view
->isAboveExpansionToggle(index
, pos
)) {
761 const bool expanded
= m_model
->isExpanded(index
);
762 m_model
->setExpanded(index
, !expanded
);
764 emit
itemExpansionToggleClicked(index
);
765 emitItemActivated
= false;
766 } else if (shiftOrControlPressed
) {
767 // The mouse click should only update the selection, not trigger the item
768 emitItemActivated
= false;
769 } else if (!(KGlobalSettings::singleClick() || m_singleClickActivationEnforced
)) {
770 emitItemActivated
= false;
772 if (emitItemActivated
) {
773 emit
itemActivated(index
);
775 } else if (event
->button() & Qt::MidButton
) {
776 emit
itemMiddleClicked(index
);
780 m_pressedMousePos
= QPointF();
782 m_clearSelectionIfItemsAreNotDragged
= false;
786 bool KItemListController::mouseDoubleClickEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
788 const QPointF pos
= transform
.map(event
->pos());
789 const int index
= m_view
->itemAt(pos
);
791 // Expand item if desired - See Bug 295573
792 if (m_mouseDoubleClickAction
!= ActivateItemOnly
) {
793 if (m_view
&& m_model
&& m_view
->supportsItemExpanding() && m_model
->isExpandable(index
)) {
794 const bool expanded
= m_model
->isExpanded(index
);
795 m_model
->setExpanded(index
, !expanded
);
799 bool emitItemActivated
= !(KGlobalSettings::singleClick() || m_singleClickActivationEnforced
) &&
800 (event
->button() & Qt::LeftButton
) &&
801 index
>= 0 && index
< m_model
->count();
802 if (emitItemActivated
) {
803 emit
itemActivated(index
);
808 bool KItemListController::dragEnterEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
815 bool KItemListController::dragLeaveEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
820 m_view
->setAutoScroll(false);
821 m_view
->hideDropIndicator();
823 KItemListWidget
* widget
= hoveredWidget();
825 widget
->setHovered(false);
826 emit
itemUnhovered(widget
->index());
831 bool KItemListController::dragMoveEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
833 if (!m_model
|| !m_view
) {
837 event
->acceptProposedAction();
839 KItemListWidget
* oldHoveredWidget
= hoveredWidget();
841 const QPointF pos
= transform
.map(event
->pos());
842 KItemListWidget
* newHoveredWidget
= widgetForPos(pos
);
844 if (oldHoveredWidget
!= newHoveredWidget
) {
845 m_autoActivationTimer
->stop();
847 if (oldHoveredWidget
) {
848 oldHoveredWidget
->setHovered(false);
849 emit
itemUnhovered(oldHoveredWidget
->index());
853 if (newHoveredWidget
) {
854 bool droppingBetweenItems
= false;
855 if (m_model
->sortRole().isEmpty()) {
856 // The model supports inserting items between other items.
857 droppingBetweenItems
= (m_view
->showDropIndicator(pos
) >= 0);
860 const int index
= newHoveredWidget
->index();
861 if (!droppingBetweenItems
) {
862 if (m_model
->supportsDropping(index
)) {
863 // Something has been dragged on an item.
864 m_view
->hideDropIndicator();
865 if (!newHoveredWidget
->isHovered()) {
866 newHoveredWidget
->setHovered(true);
867 emit
itemHovered(index
);
870 if (!m_autoActivationTimer
->isActive() && m_autoActivationTimer
->interval() >= 0) {
871 m_autoActivationTimer
->setProperty("index", index
);
872 m_autoActivationTimer
->start();
876 m_autoActivationTimer
->stop();
877 if (newHoveredWidget
&& newHoveredWidget
->isHovered()) {
878 newHoveredWidget
->setHovered(false);
879 emit
itemUnhovered(index
);
883 m_view
->hideDropIndicator();
889 bool KItemListController::dropEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
895 m_autoActivationTimer
->stop();
896 m_view
->setAutoScroll(false);
898 const QPointF pos
= transform
.map(event
->pos());
900 int dropAboveIndex
= -1;
901 if (m_model
->sortRole().isEmpty()) {
902 // The model supports inserting of items between other items.
903 dropAboveIndex
= m_view
->showDropIndicator(pos
);
906 if (dropAboveIndex
>= 0) {
907 // Something has been dropped between two items.
908 m_view
->hideDropIndicator();
909 emit
aboveItemDropEvent(dropAboveIndex
, event
);
911 // Something has been dropped on an item or on an empty part of the view.
912 emit
itemDropEvent(m_view
->itemAt(pos
), event
);
915 QAccessible::updateAccessibility(view(), 0, QAccessible::DragDropEnd
);
920 bool KItemListController::hoverEnterEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
927 bool KItemListController::hoverMoveEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
930 if (!m_model
|| !m_view
) {
934 KItemListWidget
* oldHoveredWidget
= hoveredWidget();
935 const QPointF pos
= transform
.map(event
->pos());
936 KItemListWidget
* newHoveredWidget
= widgetForPos(pos
);
938 if (oldHoveredWidget
!= newHoveredWidget
) {
939 if (oldHoveredWidget
) {
940 oldHoveredWidget
->setHovered(false);
941 emit
itemUnhovered(oldHoveredWidget
->index());
944 if (newHoveredWidget
) {
945 newHoveredWidget
->setHovered(true);
946 emit
itemHovered(newHoveredWidget
->index());
953 bool KItemListController::hoverLeaveEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
958 if (!m_model
|| !m_view
) {
962 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
963 if (widget
->isHovered()) {
964 widget
->setHovered(false);
965 emit
itemUnhovered(widget
->index());
971 bool KItemListController::wheelEvent(QGraphicsSceneWheelEvent
* event
, const QTransform
& transform
)
978 bool KItemListController::resizeEvent(QGraphicsSceneResizeEvent
* event
, const QTransform
& transform
)
985 bool KItemListController::processEvent(QEvent
* event
, const QTransform
& transform
)
991 switch (event
->type()) {
992 case QEvent::KeyPress
:
993 return keyPressEvent(static_cast<QKeyEvent
*>(event
));
994 case QEvent::InputMethod
:
995 return inputMethodEvent(static_cast<QInputMethodEvent
*>(event
));
996 case QEvent::GraphicsSceneMousePress
:
997 return mousePressEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
998 case QEvent::GraphicsSceneMouseMove
:
999 return mouseMoveEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
1000 case QEvent::GraphicsSceneMouseRelease
:
1001 return mouseReleaseEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
1002 case QEvent::GraphicsSceneMouseDoubleClick
:
1003 return mouseDoubleClickEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
1004 case QEvent::GraphicsSceneWheel
:
1005 return wheelEvent(static_cast<QGraphicsSceneWheelEvent
*>(event
), QTransform());
1006 case QEvent::GraphicsSceneDragEnter
:
1007 return dragEnterEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
1008 case QEvent::GraphicsSceneDragLeave
:
1009 return dragLeaveEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
1010 case QEvent::GraphicsSceneDragMove
:
1011 return dragMoveEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
1012 case QEvent::GraphicsSceneDrop
:
1013 return dropEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
1014 case QEvent::GraphicsSceneHoverEnter
:
1015 return hoverEnterEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
1016 case QEvent::GraphicsSceneHoverMove
:
1017 return hoverMoveEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
1018 case QEvent::GraphicsSceneHoverLeave
:
1019 return hoverLeaveEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
1020 case QEvent::GraphicsSceneResize
:
1021 return resizeEvent(static_cast<QGraphicsSceneResizeEvent
*>(event
), transform
);
1029 void KItemListController::slotViewScrollOffsetChanged(qreal current
, qreal previous
)
1035 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
1036 if (rubberBand
->isActive()) {
1037 const qreal diff
= current
- previous
;
1038 // TODO: Ideally just QCursor::pos() should be used as
1039 // new end-position but it seems there is no easy way
1040 // to have something like QWidget::mapFromGlobal() for QGraphicsWidget
1041 // (... or I just missed an easy way to do the mapping)
1042 QPointF endPos
= rubberBand
->endPosition();
1043 if (m_view
->scrollOrientation() == Qt::Vertical
) {
1044 endPos
.ry() += diff
;
1046 endPos
.rx() += diff
;
1049 rubberBand
->setEndPosition(endPos
);
1053 void KItemListController::slotRubberBandChanged()
1055 if (!m_view
|| !m_model
|| m_model
->count() <= 0) {
1059 const KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
1060 const QPointF startPos
= rubberBand
->startPosition();
1061 const QPointF endPos
= rubberBand
->endPosition();
1062 QRectF rubberBandRect
= QRectF(startPos
, endPos
).normalized();
1064 const bool scrollVertical
= (m_view
->scrollOrientation() == Qt::Vertical
);
1065 if (scrollVertical
) {
1066 rubberBandRect
.translate(0, -m_view
->scrollOffset());
1068 rubberBandRect
.translate(-m_view
->scrollOffset(), 0);
1071 if (!m_oldSelection
.isEmpty()) {
1072 // Clear the old selection that was available before the rubberband has
1073 // been activated in case if no Shift- or Control-key are pressed
1074 const bool shiftOrControlPressed
= QApplication::keyboardModifiers() & Qt::ShiftModifier
||
1075 QApplication::keyboardModifiers() & Qt::ControlModifier
;
1076 if (!shiftOrControlPressed
) {
1077 m_oldSelection
.clear();
1081 QSet
<int> selectedItems
;
1083 // Select all visible items that intersect with the rubberband
1084 foreach (const KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
1085 const int index
= widget
->index();
1087 const QRectF widgetRect
= m_view
->itemRect(index
);
1088 if (widgetRect
.intersects(rubberBandRect
)) {
1089 const QRectF iconRect
= widget
->iconRect().translated(widgetRect
.topLeft());
1090 const QRectF textRect
= widget
->textRect().translated(widgetRect
.topLeft());
1091 if (iconRect
.intersects(rubberBandRect
) || textRect
.intersects(rubberBandRect
)) {
1092 selectedItems
.insert(index
);
1097 // Select all invisible items that intersect with the rubberband. Instead of
1098 // iterating all items only the area which might be touched by the rubberband
1100 const bool increaseIndex
= scrollVertical
?
1101 startPos
.y() > endPos
.y(): startPos
.x() > endPos
.x();
1103 int index
= increaseIndex
? m_view
->lastVisibleIndex() + 1 : m_view
->firstVisibleIndex() - 1;
1104 bool selectionFinished
= false;
1106 const QRectF widgetRect
= m_view
->itemRect(index
);
1107 if (widgetRect
.intersects(rubberBandRect
)) {
1108 selectedItems
.insert(index
);
1111 if (increaseIndex
) {
1113 selectionFinished
= (index
>= m_model
->count()) ||
1114 ( scrollVertical
&& widgetRect
.top() > rubberBandRect
.bottom()) ||
1115 (!scrollVertical
&& widgetRect
.left() > rubberBandRect
.right());
1118 selectionFinished
= (index
< 0) ||
1119 ( scrollVertical
&& widgetRect
.bottom() < rubberBandRect
.top()) ||
1120 (!scrollVertical
&& widgetRect
.right() < rubberBandRect
.left());
1122 } while (!selectionFinished
);
1124 if (QApplication::keyboardModifiers() & Qt::ControlModifier
) {
1125 // If Control is pressed, the selection state of all items in the rubberband is toggled.
1126 // Therefore, the new selection contains:
1127 // 1. All previously selected items which are not inside the rubberband, and
1128 // 2. all items inside the rubberband which have not been selected previously.
1129 m_selectionManager
->setSelectedItems((m_oldSelection
- selectedItems
) + (selectedItems
- m_oldSelection
));
1132 m_selectionManager
->setSelectedItems(selectedItems
+ m_oldSelection
);
1136 void KItemListController::startDragging()
1138 if (!m_view
|| !m_model
) {
1142 const QSet
<int> selectedItems
= m_selectionManager
->selectedItems();
1143 if (selectedItems
.isEmpty()) {
1147 QMimeData
* data
= m_model
->createMimeData(selectedItems
);
1152 // The created drag object will be owned and deleted
1153 // by QApplication::activeWindow().
1154 QDrag
* drag
= new QDrag(QApplication::activeWindow());
1155 drag
->setMimeData(data
);
1157 const QPixmap pixmap
= m_view
->createDragPixmap(selectedItems
);
1158 drag
->setPixmap(pixmap
);
1160 const QPoint
hotSpot(pixmap
.width() / 2, 0);
1161 drag
->setHotSpot(hotSpot
);
1163 drag
->exec(Qt::MoveAction
| Qt::CopyAction
| Qt::LinkAction
, Qt::CopyAction
);
1164 QAccessible::updateAccessibility(view(), 0, QAccessible::DragDropStart
);
1167 KItemListWidget
* KItemListController::hoveredWidget() const
1171 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
1172 if (widget
->isHovered()) {
1180 KItemListWidget
* KItemListController::widgetForPos(const QPointF
& pos
) const
1184 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
1185 const QPointF mappedPos
= widget
->mapFromItem(m_view
, pos
);
1187 const bool hovered
= widget
->contains(mappedPos
) &&
1188 !widget
->expansionToggleRect().contains(mappedPos
);
1197 void KItemListController::updateKeyboardAnchor()
1199 const bool validAnchor
= m_keyboardAnchorIndex
>= 0 &&
1200 m_keyboardAnchorIndex
< m_model
->count() &&
1201 keyboardAnchorPos(m_keyboardAnchorIndex
) == m_keyboardAnchorPos
;
1203 const int index
= m_selectionManager
->currentItem();
1204 m_keyboardAnchorIndex
= index
;
1205 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
1209 int KItemListController::nextRowIndex(int index
) const
1211 if (m_keyboardAnchorIndex
< 0) {
1215 const int maxIndex
= m_model
->count() - 1;
1216 if (index
== maxIndex
) {
1220 // Calculate the index of the last column inside the row of the current index
1221 int lastColumnIndex
= index
;
1222 while (keyboardAnchorPos(lastColumnIndex
+ 1) > keyboardAnchorPos(lastColumnIndex
)) {
1224 if (lastColumnIndex
>= maxIndex
) {
1229 // Based on the last column index go to the next row and calculate the nearest index
1230 // that is below the current index
1231 int nextRowIndex
= lastColumnIndex
+ 1;
1232 int searchIndex
= nextRowIndex
;
1233 qreal minDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(nextRowIndex
));
1234 while (searchIndex
< maxIndex
&& keyboardAnchorPos(searchIndex
+ 1) > keyboardAnchorPos(searchIndex
)) {
1236 const qreal searchDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(searchIndex
));
1237 if (searchDiff
< minDiff
) {
1238 minDiff
= searchDiff
;
1239 nextRowIndex
= searchIndex
;
1243 return nextRowIndex
;
1246 int KItemListController::previousRowIndex(int index
) const
1248 if (m_keyboardAnchorIndex
< 0 || index
== 0) {
1252 // Calculate the index of the first column inside the row of the current index
1253 int firstColumnIndex
= index
;
1254 while (keyboardAnchorPos(firstColumnIndex
- 1) < keyboardAnchorPos(firstColumnIndex
)) {
1256 if (firstColumnIndex
<= 0) {
1261 // Based on the first column index go to the previous row and calculate the nearest index
1262 // that is above the current index
1263 int previousRowIndex
= firstColumnIndex
- 1;
1264 int searchIndex
= previousRowIndex
;
1265 qreal minDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(previousRowIndex
));
1266 while (searchIndex
> 0 && keyboardAnchorPos(searchIndex
- 1) < keyboardAnchorPos(searchIndex
)) {
1268 const qreal searchDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(searchIndex
));
1269 if (searchDiff
< minDiff
) {
1270 minDiff
= searchDiff
;
1271 previousRowIndex
= searchIndex
;
1275 return previousRowIndex
;
1278 qreal
KItemListController::keyboardAnchorPos(int index
) const
1280 const QRectF itemRect
= m_view
->itemRect(index
);
1281 if (!itemRect
.isEmpty()) {
1282 return (m_view
->scrollOrientation() == Qt::Vertical
) ? itemRect
.x() : itemRect
.y();
1288 void KItemListController::updateExtendedSelectionRegion()
1291 const bool extend
= (m_selectionBehavior
!= MultiSelection
);
1292 KItemListStyleOption option
= m_view
->styleOption();
1293 if (option
.extendedSelectionRegion
!= extend
) {
1294 option
.extendedSelectionRegion
= extend
;
1295 m_view
->setStyleOption(option
);
1300 #include "kitemlistcontroller.moc"