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>
44 KItemListController::KItemListController(KItemModelBase
* model
, KItemListView
* view
, QObject
* parent
) :
46 m_singleClickActivation(KGlobalSettings::singleClick()),
47 m_selectionTogglePressed(false),
48 m_clearSelectionIfItemsAreNotDragged(false),
49 m_selectionBehavior(NoSelection
),
52 m_selectionManager(new KItemListSelectionManager(this)),
53 m_keyboardManager(new KItemListKeyboardSearchManager(this)),
56 m_autoActivationTimer(0),
58 m_keyboardAnchorIndex(-1),
59 m_keyboardAnchorPos(0)
61 connect(m_keyboardManager
, SIGNAL(changeCurrentItem(QString
,bool)),
62 this, SLOT(slotChangeCurrentItem(QString
,bool)));
63 connect(m_selectionManager
, SIGNAL(currentChanged(int,int)),
64 m_keyboardManager
, SLOT(slotCurrentChanged(int,int)));
66 m_autoActivationTimer
= new QTimer(this);
67 m_autoActivationTimer
->setSingleShot(true);
68 m_autoActivationTimer
->setInterval(-1);
69 connect(m_autoActivationTimer
, SIGNAL(timeout()), this, SLOT(slotAutoActivationTimeout()));
75 KItemListController::~KItemListController()
86 void KItemListController::setModel(KItemModelBase
* model
)
88 if (m_model
== model
) {
92 KItemModelBase
* oldModel
= m_model
;
95 m_model
->setParent(this);
99 m_view
->setModel(m_model
);
102 m_selectionManager
->setModel(m_model
);
104 emit
modelChanged(m_model
, oldModel
);
107 KItemModelBase
* KItemListController::model() const
112 KItemListSelectionManager
* KItemListController::selectionManager() const
114 return m_selectionManager
;
117 void KItemListController::setView(KItemListView
* view
)
119 if (m_view
== view
) {
123 KItemListView
* oldView
= m_view
;
125 disconnect(oldView
, SIGNAL(scrollOffsetChanged(qreal
,qreal
)), this, SLOT(slotViewScrollOffsetChanged(qreal
,qreal
)));
131 m_view
->setController(this);
132 m_view
->setModel(m_model
);
133 connect(m_view
, SIGNAL(scrollOffsetChanged(qreal
,qreal
)), this, SLOT(slotViewScrollOffsetChanged(qreal
,qreal
)));
134 updateExtendedSelectionRegion();
137 emit
viewChanged(m_view
, oldView
);
140 KItemListView
* KItemListController::view() const
145 void KItemListController::setSelectionBehavior(SelectionBehavior behavior
)
147 m_selectionBehavior
= behavior
;
148 updateExtendedSelectionRegion();
151 KItemListController::SelectionBehavior
KItemListController::selectionBehavior() const
153 return m_selectionBehavior
;
156 void KItemListController::setAutoActivationDelay(int delay
)
158 m_autoActivationTimer
->setInterval(delay
);
161 int KItemListController::autoActivationDelay() const
163 return m_autoActivationTimer
->interval();
166 void KItemListController::setSingleClickActivation(bool singleClick
)
168 m_singleClickActivation
= singleClick
;
171 bool KItemListController::singleClickActivation() const
173 return m_singleClickActivation
;
176 bool KItemListController::showEvent(QShowEvent
* event
)
182 bool KItemListController::hideEvent(QHideEvent
* event
)
188 bool KItemListController::keyPressEvent(QKeyEvent
* event
)
190 int index
= m_selectionManager
->currentItem();
191 int key
= event
->key();
193 // Handle the expanding/collapsing of items
194 if (m_view
->supportsItemExpanding() && m_model
->isExpandable(index
)) {
195 if (key
== Qt::Key_Right
) {
196 if (m_model
->setExpanded(index
, true)) {
199 } else if (key
== Qt::Key_Left
) {
200 if (m_model
->setExpanded(index
, false)) {
206 const bool shiftPressed
= event
->modifiers() & Qt::ShiftModifier
;
207 const bool controlPressed
= event
->modifiers() & Qt::ControlModifier
;
208 const bool shiftOrControlPressed
= shiftPressed
|| controlPressed
;
210 const int itemCount
= m_model
->count();
212 // For horizontal scroll orientation, transform
213 // the arrow keys to simplify the event handling.
214 if (m_view
->scrollOrientation() == Qt::Horizontal
) {
216 case Qt::Key_Up
: key
= Qt::Key_Left
; break;
217 case Qt::Key_Down
: key
= Qt::Key_Right
; break;
218 case Qt::Key_Left
: key
= Qt::Key_Up
; break;
219 case Qt::Key_Right
: key
= Qt::Key_Down
; break;
224 const bool selectSingleItem
= m_selectionBehavior
!= NoSelection
&&
226 (key
== Qt::Key_Home
|| key
== Qt::Key_End
||
227 key
== Qt::Key_Up
|| key
== Qt::Key_Down
||
228 key
== Qt::Key_Left
|| key
== Qt::Key_Right
);
229 if (selectSingleItem
) {
230 const int current
= m_selectionManager
->currentItem();
231 m_selectionManager
->setSelected(current
);
238 m_keyboardAnchorIndex
= index
;
239 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
243 index
= itemCount
- 1;
244 m_keyboardAnchorIndex
= index
;
245 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
251 m_keyboardAnchorIndex
= index
;
252 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
257 if (index
< itemCount
- 1) {
259 m_keyboardAnchorIndex
= index
;
260 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
265 updateKeyboardAnchor();
266 index
= previousRowIndex(index
);
270 updateKeyboardAnchor();
271 index
= nextRowIndex(index
);
275 if (m_view
->scrollOrientation() == Qt::Horizontal
) {
276 // The new current index should correspond to the first item in the current column.
277 int newIndex
= qMax(index
- 1, 0);
278 while (newIndex
!= index
&& m_view
->itemRect(newIndex
).topLeft().y() < m_view
->itemRect(index
).topLeft().y()) {
280 newIndex
= qMax(index
- 1, 0);
282 m_keyboardAnchorIndex
= index
;
283 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
285 const qreal currentItemBottom
= m_view
->itemRect(index
).bottomLeft().y();
286 const qreal height
= m_view
->geometry().height();
288 // The new current item should be the first item in the current
289 // column whose itemRect's top coordinate is larger than targetY.
290 const qreal targetY
= currentItemBottom
- height
;
292 updateKeyboardAnchor();
293 int newIndex
= previousRowIndex(index
);
296 updateKeyboardAnchor();
297 newIndex
= previousRowIndex(index
);
298 } while (m_view
->itemRect(newIndex
).topLeft().y() > targetY
&& newIndex
!= index
);
302 case Qt::Key_PageDown
:
303 if (m_view
->scrollOrientation() == Qt::Horizontal
) {
304 // The new current index should correspond to the last item in the current column.
305 int newIndex
= qMin(index
+ 1, m_model
->count() - 1);
306 while (newIndex
!= index
&& m_view
->itemRect(newIndex
).topLeft().y() > m_view
->itemRect(index
).topLeft().y()) {
308 newIndex
= qMin(index
+ 1, m_model
->count() - 1);
310 m_keyboardAnchorIndex
= index
;
311 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
313 const qreal currentItemTop
= m_view
->itemRect(index
).topLeft().y();
314 const qreal height
= m_view
->geometry().height();
316 // The new current item should be the last item in the current
317 // column whose itemRect's bottom coordinate is smaller than targetY.
318 const qreal targetY
= currentItemTop
+ height
;
320 updateKeyboardAnchor();
321 int newIndex
= nextRowIndex(index
);
324 updateKeyboardAnchor();
325 newIndex
= nextRowIndex(index
);
326 } while (m_view
->itemRect(newIndex
).bottomLeft().y() < targetY
&& newIndex
!= index
);
331 case Qt::Key_Return
: {
332 const QSet
<int> selectedItems
= m_selectionManager
->selectedItems();
333 if (selectedItems
.count() >= 2) {
334 emit
itemsActivated(selectedItems
);
335 } else if (selectedItems
.count() == 1) {
336 emit
itemActivated(selectedItems
.toList().first());
338 emit
itemActivated(index
);
344 if (m_selectionBehavior
== MultiSelection
) {
345 if (controlPressed
) {
346 m_selectionManager
->endAnchoredSelection();
347 m_selectionManager
->setSelected(index
, 1, KItemListSelectionManager::Toggle
);
348 m_selectionManager
->beginAnchoredSelection(index
);
350 const int current
= m_selectionManager
->currentItem();
351 m_selectionManager
->setSelected(current
);
357 // Emit the signal itemContextMenuRequested() in case if at least one
358 // item is selected. Otherwise the signal viewContextMenuRequested() will be emitted.
359 const QSet
<int> selectedItems
= m_selectionManager
->selectedItems();
361 if (selectedItems
.count() >= 2) {
362 const int currentItemIndex
= m_selectionManager
->currentItem();
363 index
= selectedItems
.contains(currentItemIndex
)
364 ? currentItemIndex
: selectedItems
.toList().first();
365 } else if (selectedItems
.count() == 1) {
366 index
= selectedItems
.toList().first();
370 const QRectF contextRect
= m_view
->itemContextRect(index
);
371 const QPointF
pos(m_view
->scene()->views().first()->mapToGlobal(contextRect
.bottomRight().toPoint()));
372 emit
itemContextMenuRequested(index
, pos
);
374 emit
viewContextMenuRequested(QCursor::pos());
380 if (m_selectionBehavior
!= SingleSelection
) {
381 m_selectionManager
->clearSelection();
383 m_keyboardManager
->cancelSearch();
387 m_keyboardManager
->addKeys(event
->text());
391 if (m_selectionManager
->currentItem() != index
) {
392 switch (m_selectionBehavior
) {
394 m_selectionManager
->setCurrentItem(index
);
397 case SingleSelection
:
398 m_selectionManager
->setCurrentItem(index
);
399 m_selectionManager
->clearSelection();
400 m_selectionManager
->setSelected(index
, 1);
404 if (controlPressed
) {
405 m_selectionManager
->endAnchoredSelection();
408 m_selectionManager
->setCurrentItem(index
);
410 if (!shiftOrControlPressed
) {
411 m_selectionManager
->clearSelection();
412 m_selectionManager
->setSelected(index
, 1);
416 m_selectionManager
->beginAnchoredSelection(index
);
421 m_view
->scrollToItem(index
);
426 void KItemListController::slotChangeCurrentItem(const QString
& text
, bool searchFromNextItem
)
428 if (!m_model
|| m_model
->count() == 0) {
431 const int currentIndex
= m_selectionManager
->currentItem();
433 if (searchFromNextItem
) {
434 index
= m_model
->indexForKeyboardSearch(text
, (currentIndex
+ 1) % m_model
->count());
436 index
= m_model
->indexForKeyboardSearch(text
, currentIndex
);
439 m_selectionManager
->setCurrentItem(index
);
440 m_selectionManager
->clearSelection();
441 m_selectionManager
->setSelected(index
, 1);
442 m_selectionManager
->beginAnchoredSelection(index
);
443 m_view
->scrollToItem(index
);
447 void KItemListController::slotAutoActivationTimeout()
449 if (!m_model
|| !m_view
) {
453 const int index
= m_autoActivationTimer
->property("index").toInt();
454 if (index
< 0 || index
>= m_model
->count()) {
458 if (m_model
->supportsDropping(index
)) {
459 if (m_view
->supportsItemExpanding() && m_model
->isExpandable(index
)) {
460 const bool expanded
= m_model
->isExpanded(index
);
461 m_model
->setExpanded(index
, !expanded
);
463 emit
itemActivated(index
);
468 bool KItemListController::inputMethodEvent(QInputMethodEvent
* event
)
474 bool KItemListController::mousePressEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
480 m_pressedMousePos
= transform
.map(event
->pos());
481 m_pressedIndex
= m_view
->itemAt(m_pressedMousePos
);
482 emit
mouseButtonPressed(m_pressedIndex
, event
->buttons());
484 if (m_view
->isAboveExpansionToggle(m_pressedIndex
, m_pressedMousePos
)) {
485 m_selectionManager
->endAnchoredSelection();
486 m_selectionManager
->setCurrentItem(m_pressedIndex
);
487 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
491 m_selectionTogglePressed
= m_view
->isAboveSelectionToggle(m_pressedIndex
, m_pressedMousePos
);
492 if (m_selectionTogglePressed
) {
493 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
494 // The previous anchored selection has been finished already in
495 // KItemListSelectionManager::setSelected(). We can safely change
496 // the current item and start a new anchored selection now.
497 m_selectionManager
->setCurrentItem(m_pressedIndex
);
498 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
502 const bool shiftPressed
= event
->modifiers() & Qt::ShiftModifier
;
503 const bool controlPressed
= event
->modifiers() & Qt::ControlModifier
;
505 // The previous selection is cleared if either
506 // 1. The selection mode is SingleSelection, or
507 // 2. the selection mode is MultiSelection, and *none* of the following conditions are met:
508 // a) Shift or Control are pressed.
509 // b) The clicked item is selected already. In that case, the user might want to:
510 // - start dragging multiple items, or
511 // - open the context menu and perform an action for all selected items.
512 const bool shiftOrControlPressed
= shiftPressed
|| controlPressed
;
513 const bool pressedItemAlreadySelected
= m_pressedIndex
>= 0 && m_selectionManager
->isSelected(m_pressedIndex
);
514 const bool clearSelection
= m_selectionBehavior
== SingleSelection
||
515 (!shiftOrControlPressed
&& !pressedItemAlreadySelected
);
516 if (clearSelection
) {
517 m_selectionManager
->clearSelection();
518 } else if (pressedItemAlreadySelected
&& !shiftOrControlPressed
&& (event
->buttons() & Qt::LeftButton
)) {
519 // The user might want to start dragging multiple items, but if he clicks the item
520 // in order to trigger it instead, the other selected items must be deselected.
521 // However, we do not know yet what the user is going to do.
522 // -> remember that the user pressed an item which had been selected already and
523 // clear the selection in mouseReleaseEvent(), unless the items are dragged.
524 m_clearSelectionIfItemsAreNotDragged
= true;
528 // Finish the anchored selection before the current index is changed
529 m_selectionManager
->endAnchoredSelection();
532 if (m_pressedIndex
>= 0) {
533 m_selectionManager
->setCurrentItem(m_pressedIndex
);
535 switch (m_selectionBehavior
) {
539 case SingleSelection
:
540 m_selectionManager
->setSelected(m_pressedIndex
);
544 if (controlPressed
) {
545 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
546 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
547 } else if (!shiftPressed
|| !m_selectionManager
->isAnchoredSelectionActive()) {
548 // Select the pressed item and start a new anchored selection
549 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Select
);
550 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
559 if (event
->buttons() & Qt::RightButton
) {
560 emit
itemContextMenuRequested(m_pressedIndex
, event
->screenPos());
566 if (event
->buttons() & Qt::RightButton
) {
567 const QRectF headerBounds
= m_view
->headerBoundaries();
568 if (headerBounds
.contains(event
->pos())) {
569 emit
headerContextMenuRequested(event
->screenPos());
571 emit
viewContextMenuRequested(event
->screenPos());
576 if (m_selectionBehavior
== MultiSelection
) {
577 QPointF startPos
= m_pressedMousePos
;
578 if (m_view
->scrollOrientation() == Qt::Vertical
) {
579 startPos
.ry() += m_view
->scrollOffset();
580 if (m_view
->itemSize().width() < 0) {
581 // Use a special rubberband for views that have only one column and
582 // expand the rubberband to use the whole width of the view.
586 startPos
.rx() += m_view
->scrollOffset();
589 m_oldSelection
= m_selectionManager
->selectedItems();
590 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
591 rubberBand
->setStartPosition(startPos
);
592 rubberBand
->setEndPosition(startPos
);
593 rubberBand
->setActive(true);
594 connect(rubberBand
, SIGNAL(endPositionChanged(QPointF
,QPointF
)), this, SLOT(slotRubberBandChanged()));
595 m_view
->setAutoScroll(true);
601 bool KItemListController::mouseMoveEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
607 if (m_pressedIndex
>= 0) {
608 // Check whether a dragging should be started
609 if (event
->buttons() & Qt::LeftButton
) {
610 const QPointF pos
= transform
.map(event
->pos());
611 if ((pos
- m_pressedMousePos
).manhattanLength() >= QApplication::startDragDistance()) {
612 if (!m_selectionManager
->isSelected(m_pressedIndex
)) {
613 // Always assure that the dragged item gets selected. Usually this is already
614 // done on the mouse-press event, but when using the selection-toggle on a
615 // selected item the dragged item is not selected yet.
616 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
618 // A selected item has been clicked to drag all selected items
619 // -> the selection should not be cleared when the mouse button is released.
620 m_clearSelectionIfItemsAreNotDragged
= false;
627 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
628 if (rubberBand
->isActive()) {
629 QPointF endPos
= transform
.map(event
->pos());
631 // Update the current item.
632 const int newCurrent
= m_view
->itemAt(endPos
);
633 if (newCurrent
>= 0) {
634 // It's expected that the new current index is also the new anchor (bug 163451).
635 m_selectionManager
->endAnchoredSelection();
636 m_selectionManager
->setCurrentItem(newCurrent
);
637 m_selectionManager
->beginAnchoredSelection(newCurrent
);
640 if (m_view
->scrollOrientation() == Qt::Vertical
) {
641 endPos
.ry() += m_view
->scrollOffset();
642 if (m_view
->itemSize().width() < 0) {
643 // Use a special rubberband for views that have only one column and
644 // expand the rubberband to use the whole width of the view.
645 endPos
.setX(m_view
->size().width());
648 endPos
.rx() += m_view
->scrollOffset();
650 rubberBand
->setEndPosition(endPos
);
657 bool KItemListController::mouseReleaseEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
663 emit
mouseButtonReleased(m_pressedIndex
, event
->buttons());
665 const bool isAboveSelectionToggle
= m_view
->isAboveSelectionToggle(m_pressedIndex
, m_pressedMousePos
);
666 if (isAboveSelectionToggle
) {
667 m_selectionTogglePressed
= false;
671 if (!isAboveSelectionToggle
&& m_selectionTogglePressed
) {
672 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
673 m_selectionTogglePressed
= false;
677 const bool shiftOrControlPressed
= event
->modifiers() & Qt::ShiftModifier
||
678 event
->modifiers() & Qt::ControlModifier
;
680 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
681 if (rubberBand
->isActive()) {
682 disconnect(rubberBand
, SIGNAL(endPositionChanged(QPointF
,QPointF
)), this, SLOT(slotRubberBandChanged()));
683 rubberBand
->setActive(false);
684 m_oldSelection
.clear();
685 m_view
->setAutoScroll(false);
688 const QPointF pos
= transform
.map(event
->pos());
689 const int index
= m_view
->itemAt(pos
);
691 if (index
>= 0 && index
== m_pressedIndex
) {
692 // The release event is done above the same item as the press event
694 if (m_clearSelectionIfItemsAreNotDragged
) {
695 // A selected item has been clicked, but no drag operation has been started
696 // -> clear the rest of the selection.
697 m_selectionManager
->clearSelection();
698 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Select
);
699 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
702 if (event
->button() & Qt::LeftButton
) {
703 bool emitItemActivated
= true;
704 if (m_view
->isAboveExpansionToggle(index
, pos
)) {
705 const bool expanded
= m_model
->isExpanded(index
);
706 m_model
->setExpanded(index
, !expanded
);
708 emit
itemExpansionToggleClicked(index
);
709 emitItemActivated
= false;
710 } else if (shiftOrControlPressed
) {
711 // The mouse click should only update the selection, not trigger the item
712 emitItemActivated
= false;
713 } else if (!m_singleClickActivation
) {
714 emitItemActivated
= false;
716 if (emitItemActivated
) {
717 emit
itemActivated(index
);
719 } else if (event
->button() & Qt::MidButton
) {
720 emit
itemMiddleClicked(index
);
724 m_pressedMousePos
= QPointF();
726 m_clearSelectionIfItemsAreNotDragged
= false;
730 bool KItemListController::mouseDoubleClickEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
732 const QPointF pos
= transform
.map(event
->pos());
733 const int index
= m_view
->itemAt(pos
);
735 bool emitItemActivated
= !m_singleClickActivation
&&
736 (event
->button() & Qt::LeftButton
) &&
737 index
>= 0 && index
< m_model
->count();
738 if (emitItemActivated
) {
739 emit
itemActivated(index
);
744 bool KItemListController::dragEnterEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
751 bool KItemListController::dragLeaveEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
756 m_view
->setAutoScroll(false);
758 KItemListWidget
* widget
= hoveredWidget();
760 widget
->setHovered(false);
761 emit
itemUnhovered(widget
->index());
766 bool KItemListController::dragMoveEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
769 if (!m_model
|| !m_view
) {
773 event
->acceptProposedAction();
775 KItemListWidget
* oldHoveredWidget
= hoveredWidget();
777 const QPointF pos
= transform
.map(event
->pos());
778 KItemListWidget
* newHoveredWidget
= widgetForPos(pos
);
780 if (oldHoveredWidget
!= newHoveredWidget
) {
781 m_autoActivationTimer
->stop();
783 if (oldHoveredWidget
) {
784 oldHoveredWidget
->setHovered(false);
785 emit
itemUnhovered(oldHoveredWidget
->index());
788 if (newHoveredWidget
) {
789 const int index
= newHoveredWidget
->index();
790 if (m_model
->supportsDropping(index
)) {
791 newHoveredWidget
->setHovered(true);
793 emit
itemHovered(index
);
795 if (m_autoActivationTimer
->interval() >= 0) {
796 m_autoActivationTimer
->setProperty("index", index
);
797 m_autoActivationTimer
->start();
805 bool KItemListController::dropEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
812 m_autoActivationTimer
->stop();
813 m_view
->setAutoScroll(false);
815 const QPointF pos
= transform
.map(event
->pos());
816 const int index
= m_view
->itemAt(pos
);
817 emit
itemDropEvent(index
, event
);
822 bool KItemListController::hoverEnterEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
829 bool KItemListController::hoverMoveEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
832 if (!m_model
|| !m_view
) {
836 KItemListWidget
* oldHoveredWidget
= hoveredWidget();
837 const QPointF pos
= transform
.map(event
->pos());
838 KItemListWidget
* newHoveredWidget
= widgetForPos(pos
);
840 if (oldHoveredWidget
!= newHoveredWidget
) {
841 if (oldHoveredWidget
) {
842 oldHoveredWidget
->setHovered(false);
843 emit
itemUnhovered(oldHoveredWidget
->index());
846 if (newHoveredWidget
) {
847 newHoveredWidget
->setHovered(true);
848 emit
itemHovered(newHoveredWidget
->index());
855 bool KItemListController::hoverLeaveEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
860 if (!m_model
|| !m_view
) {
864 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
865 if (widget
->isHovered()) {
866 widget
->setHovered(false);
867 emit
itemUnhovered(widget
->index());
873 bool KItemListController::wheelEvent(QGraphicsSceneWheelEvent
* event
, const QTransform
& transform
)
880 bool KItemListController::resizeEvent(QGraphicsSceneResizeEvent
* event
, const QTransform
& transform
)
887 bool KItemListController::processEvent(QEvent
* event
, const QTransform
& transform
)
893 switch (event
->type()) {
894 case QEvent::KeyPress
:
895 return keyPressEvent(static_cast<QKeyEvent
*>(event
));
896 case QEvent::InputMethod
:
897 return inputMethodEvent(static_cast<QInputMethodEvent
*>(event
));
898 case QEvent::GraphicsSceneMousePress
:
899 return mousePressEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
900 case QEvent::GraphicsSceneMouseMove
:
901 return mouseMoveEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
902 case QEvent::GraphicsSceneMouseRelease
:
903 return mouseReleaseEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
904 case QEvent::GraphicsSceneMouseDoubleClick
:
905 return mouseDoubleClickEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
906 case QEvent::GraphicsSceneWheel
:
907 return wheelEvent(static_cast<QGraphicsSceneWheelEvent
*>(event
), QTransform());
908 case QEvent::GraphicsSceneDragEnter
:
909 return dragEnterEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
910 case QEvent::GraphicsSceneDragLeave
:
911 return dragLeaveEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
912 case QEvent::GraphicsSceneDragMove
:
913 return dragMoveEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
914 case QEvent::GraphicsSceneDrop
:
915 return dropEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
916 case QEvent::GraphicsSceneHoverEnter
:
917 return hoverEnterEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
918 case QEvent::GraphicsSceneHoverMove
:
919 return hoverMoveEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
920 case QEvent::GraphicsSceneHoverLeave
:
921 return hoverLeaveEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
922 case QEvent::GraphicsSceneResize
:
923 return resizeEvent(static_cast<QGraphicsSceneResizeEvent
*>(event
), transform
);
931 void KItemListController::slotViewScrollOffsetChanged(qreal current
, qreal previous
)
937 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
938 if (rubberBand
->isActive()) {
939 const qreal diff
= current
- previous
;
940 // TODO: Ideally just QCursor::pos() should be used as
941 // new end-position but it seems there is no easy way
942 // to have something like QWidget::mapFromGlobal() for QGraphicsWidget
943 // (... or I just missed an easy way to do the mapping)
944 QPointF endPos
= rubberBand
->endPosition();
945 if (m_view
->scrollOrientation() == Qt::Vertical
) {
951 rubberBand
->setEndPosition(endPos
);
955 void KItemListController::slotRubberBandChanged()
957 if (!m_view
|| !m_model
|| m_model
->count() <= 0) {
961 const KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
962 const QPointF startPos
= rubberBand
->startPosition();
963 const QPointF endPos
= rubberBand
->endPosition();
964 QRectF rubberBandRect
= QRectF(startPos
, endPos
).normalized();
966 const bool scrollVertical
= (m_view
->scrollOrientation() == Qt::Vertical
);
967 if (scrollVertical
) {
968 rubberBandRect
.translate(0, -m_view
->scrollOffset());
970 rubberBandRect
.translate(-m_view
->scrollOffset(), 0);
973 if (!m_oldSelection
.isEmpty()) {
974 // Clear the old selection that was available before the rubberband has
975 // been activated in case if no Shift- or Control-key are pressed
976 const bool shiftOrControlPressed
= QApplication::keyboardModifiers() & Qt::ShiftModifier
||
977 QApplication::keyboardModifiers() & Qt::ControlModifier
;
978 if (!shiftOrControlPressed
) {
979 m_oldSelection
.clear();
983 QSet
<int> selectedItems
;
985 // Select all visible items that intersect with the rubberband
986 foreach (const KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
987 const int index
= widget
->index();
989 const QRectF widgetRect
= m_view
->itemRect(index
);
990 if (widgetRect
.intersects(rubberBandRect
)) {
991 const QRectF iconRect
= widget
->iconRect().translated(widgetRect
.topLeft());
992 const QRectF textRect
= widget
->textRect().translated(widgetRect
.topLeft());
993 if (iconRect
.intersects(rubberBandRect
) || textRect
.intersects(rubberBandRect
)) {
994 selectedItems
.insert(index
);
999 // Select all invisible items that intersect with the rubberband. Instead of
1000 // iterating all items only the area which might be touched by the rubberband
1002 const bool increaseIndex
= scrollVertical
?
1003 startPos
.y() > endPos
.y(): startPos
.x() > endPos
.x();
1005 int index
= increaseIndex
? m_view
->lastVisibleIndex() + 1 : m_view
->firstVisibleIndex() - 1;
1006 bool selectionFinished
= false;
1008 const QRectF widgetRect
= m_view
->itemRect(index
);
1009 if (widgetRect
.intersects(rubberBandRect
)) {
1010 selectedItems
.insert(index
);
1013 if (increaseIndex
) {
1015 selectionFinished
= (index
>= m_model
->count()) ||
1016 ( scrollVertical
&& widgetRect
.top() > rubberBandRect
.bottom()) ||
1017 (!scrollVertical
&& widgetRect
.left() > rubberBandRect
.right());
1020 selectionFinished
= (index
< 0) ||
1021 ( scrollVertical
&& widgetRect
.bottom() < rubberBandRect
.top()) ||
1022 (!scrollVertical
&& widgetRect
.right() < rubberBandRect
.left());
1024 } while (!selectionFinished
);
1026 if (QApplication::keyboardModifiers() & Qt::ControlModifier
) {
1027 // If Control is pressed, the selection state of all items in the rubberband is toggled.
1028 // Therefore, the new selection contains:
1029 // 1. All previously selected items which are not inside the rubberband, and
1030 // 2. all items inside the rubberband which have not been selected previously.
1031 m_selectionManager
->setSelectedItems((m_oldSelection
- selectedItems
) + (selectedItems
- m_oldSelection
));
1034 m_selectionManager
->setSelectedItems(selectedItems
+ m_oldSelection
);
1038 void KItemListController::startDragging()
1040 if (!m_view
|| !m_model
) {
1044 const QSet
<int> selectedItems
= m_selectionManager
->selectedItems();
1045 if (selectedItems
.isEmpty()) {
1049 QMimeData
* data
= m_model
->createMimeData(selectedItems
);
1054 // The created drag object will be owned and deleted
1055 // by QApplication::activeWindow().
1056 QDrag
* drag
= new QDrag(QApplication::activeWindow());
1057 drag
->setMimeData(data
);
1059 const QPixmap pixmap
= m_view
->createDragPixmap(selectedItems
);
1060 drag
->setPixmap(pixmap
);
1062 drag
->exec(Qt::MoveAction
| Qt::CopyAction
| Qt::LinkAction
, Qt::CopyAction
);
1065 KItemListWidget
* KItemListController::hoveredWidget() const
1069 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
1070 if (widget
->isHovered()) {
1078 KItemListWidget
* KItemListController::widgetForPos(const QPointF
& pos
) const
1082 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
1083 const QPointF mappedPos
= widget
->mapFromItem(m_view
, pos
);
1085 const bool hovered
= widget
->contains(mappedPos
) &&
1086 !widget
->expansionToggleRect().contains(mappedPos
);
1095 void KItemListController::updateKeyboardAnchor()
1097 const bool validAnchor
= m_keyboardAnchorIndex
>= 0 &&
1098 m_keyboardAnchorIndex
< m_model
->count() &&
1099 keyboardAnchorPos(m_keyboardAnchorIndex
) == m_keyboardAnchorPos
;
1101 const int index
= m_selectionManager
->currentItem();
1102 m_keyboardAnchorIndex
= index
;
1103 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
1107 int KItemListController::nextRowIndex(int index
) const
1109 if (m_keyboardAnchorIndex
< 0) {
1113 const int maxIndex
= m_model
->count() - 1;
1114 if (index
== maxIndex
) {
1118 // Calculate the index of the last column inside the row of the current index
1119 int lastColumnIndex
= index
;
1120 while (keyboardAnchorPos(lastColumnIndex
+ 1) > keyboardAnchorPos(lastColumnIndex
)) {
1122 if (lastColumnIndex
>= maxIndex
) {
1127 // Based on the last column index go to the next row and calculate the nearest index
1128 // that is below the current index
1129 int nextRowIndex
= lastColumnIndex
+ 1;
1130 int searchIndex
= nextRowIndex
;
1131 qreal minDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(nextRowIndex
));
1132 while (searchIndex
< maxIndex
&& keyboardAnchorPos(searchIndex
+ 1) > keyboardAnchorPos(searchIndex
)) {
1134 const qreal searchDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(searchIndex
));
1135 if (searchDiff
< minDiff
) {
1136 minDiff
= searchDiff
;
1137 nextRowIndex
= searchIndex
;
1141 return nextRowIndex
;
1144 int KItemListController::previousRowIndex(int index
) const
1146 if (m_keyboardAnchorIndex
< 0 || index
== 0) {
1150 // Calculate the index of the first column inside the row of the current index
1151 int firstColumnIndex
= index
;
1152 while (keyboardAnchorPos(firstColumnIndex
- 1) < keyboardAnchorPos(firstColumnIndex
)) {
1154 if (firstColumnIndex
<= 0) {
1159 // Based on the first column index go to the previous row and calculate the nearest index
1160 // that is above the current index
1161 int previousRowIndex
= firstColumnIndex
- 1;
1162 int searchIndex
= previousRowIndex
;
1163 qreal minDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(previousRowIndex
));
1164 while (searchIndex
> 0 && keyboardAnchorPos(searchIndex
- 1) < keyboardAnchorPos(searchIndex
)) {
1166 const qreal searchDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(searchIndex
));
1167 if (searchDiff
< minDiff
) {
1168 minDiff
= searchDiff
;
1169 previousRowIndex
= searchIndex
;
1173 return previousRowIndex
;
1176 qreal
KItemListController::keyboardAnchorPos(int index
) const
1178 const QRectF itemRect
= m_view
->itemRect(index
);
1179 if (!itemRect
.isEmpty()) {
1180 return (m_view
->scrollOrientation() == Qt::Vertical
) ? itemRect
.x() : itemRect
.y();
1186 void KItemListController::updateExtendedSelectionRegion()
1189 const bool extend
= (m_selectionBehavior
!= MultiSelection
);
1190 KItemListStyleOption option
= m_view
->styleOption();
1191 if (option
.extendedSelectionRegion
!= extend
) {
1192 option
.extendedSelectionRegion
= extend
;
1193 m_view
->setStyleOption(option
);
1198 #include "kitemlistcontroller.moc"