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)));
64 m_autoActivationTimer
= new QTimer(this);
65 m_autoActivationTimer
->setSingleShot(true);
66 m_autoActivationTimer
->setInterval(-1);
67 connect(m_autoActivationTimer
, SIGNAL(timeout()), this, SLOT(slotAutoActivationTimeout()));
73 KItemListController::~KItemListController()
84 void KItemListController::setModel(KItemModelBase
* model
)
86 if (m_model
== model
) {
90 KItemModelBase
* oldModel
= m_model
;
93 m_model
->setParent(this);
97 m_view
->setModel(m_model
);
100 m_selectionManager
->setModel(m_model
);
102 emit
modelChanged(m_model
, oldModel
);
105 KItemModelBase
* KItemListController::model() const
110 KItemListSelectionManager
* KItemListController::selectionManager() const
112 return m_selectionManager
;
115 void KItemListController::setView(KItemListView
* view
)
117 if (m_view
== view
) {
121 KItemListView
* oldView
= m_view
;
123 disconnect(oldView
, SIGNAL(scrollOffsetChanged(qreal
,qreal
)), this, SLOT(slotViewScrollOffsetChanged(qreal
,qreal
)));
129 m_view
->setController(this);
130 m_view
->setModel(m_model
);
131 connect(m_view
, SIGNAL(scrollOffsetChanged(qreal
,qreal
)), this, SLOT(slotViewScrollOffsetChanged(qreal
,qreal
)));
132 updateExtendedSelectionRegion();
135 emit
viewChanged(m_view
, oldView
);
138 KItemListView
* KItemListController::view() const
143 void KItemListController::setSelectionBehavior(SelectionBehavior behavior
)
145 m_selectionBehavior
= behavior
;
146 updateExtendedSelectionRegion();
149 KItemListController::SelectionBehavior
KItemListController::selectionBehavior() const
151 return m_selectionBehavior
;
154 void KItemListController::setAutoActivationDelay(int delay
)
156 m_autoActivationTimer
->setInterval(delay
);
159 int KItemListController::autoActivationDelay() const
161 return m_autoActivationTimer
->interval();
164 void KItemListController::setSingleClickActivation(bool singleClick
)
166 m_singleClickActivation
= singleClick
;
169 bool KItemListController::singleClickActivation() const
171 return m_singleClickActivation
;
174 bool KItemListController::showEvent(QShowEvent
* event
)
180 bool KItemListController::hideEvent(QHideEvent
* event
)
186 bool KItemListController::keyPressEvent(QKeyEvent
* event
)
188 int index
= m_selectionManager
->currentItem();
189 int key
= event
->key();
191 // Handle the expanding/collapsing of items
192 if (m_view
->supportsItemExpanding() && m_model
->isExpandable(index
)) {
193 if (key
== Qt::Key_Right
) {
194 if (m_model
->setExpanded(index
, true)) {
197 } else if (key
== Qt::Key_Left
) {
198 if (m_model
->setExpanded(index
, false)) {
204 const bool shiftPressed
= event
->modifiers() & Qt::ShiftModifier
;
205 const bool controlPressed
= event
->modifiers() & Qt::ControlModifier
;
206 const bool shiftOrControlPressed
= shiftPressed
|| controlPressed
;
208 const int itemCount
= m_model
->count();
210 // For horizontal scroll orientation, transform
211 // the arrow keys to simplify the event handling.
212 if (m_view
->scrollOrientation() == Qt::Horizontal
) {
214 case Qt::Key_Up
: key
= Qt::Key_Left
; break;
215 case Qt::Key_Down
: key
= Qt::Key_Right
; break;
216 case Qt::Key_Left
: key
= Qt::Key_Up
; break;
217 case Qt::Key_Right
: key
= Qt::Key_Down
; break;
222 const bool selectSingleItem
= m_selectionBehavior
!= NoSelection
&&
224 (key
== Qt::Key_Home
|| key
== Qt::Key_End
||
225 key
== Qt::Key_Up
|| key
== Qt::Key_Down
||
226 key
== Qt::Key_Left
|| key
== Qt::Key_Right
);
227 if (selectSingleItem
) {
228 const int current
= m_selectionManager
->currentItem();
229 m_selectionManager
->setSelected(current
);
236 m_keyboardAnchorIndex
= index
;
237 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
241 index
= itemCount
- 1;
242 m_keyboardAnchorIndex
= index
;
243 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
249 m_keyboardAnchorIndex
= index
;
250 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
255 if (index
< itemCount
- 1) {
257 m_keyboardAnchorIndex
= index
;
258 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
263 updateKeyboardAnchor();
264 index
= previousRowIndex(index
);
268 updateKeyboardAnchor();
269 index
= nextRowIndex(index
);
273 if (m_view
->scrollOrientation() == Qt::Horizontal
) {
274 // The new current index should correspond to the first item in the current column.
275 int newIndex
= qMax(index
- 1, 0);
276 while (newIndex
!= index
&& m_view
->itemRect(newIndex
).topLeft().y() < m_view
->itemRect(index
).topLeft().y()) {
278 newIndex
= qMax(index
- 1, 0);
280 m_keyboardAnchorIndex
= index
;
281 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
283 const qreal currentItemBottom
= m_view
->itemRect(index
).bottomLeft().y();
284 const qreal height
= m_view
->geometry().height();
286 // The new current item should be the first item in the current
287 // column whose itemRect's top coordinate is larger than targetY.
288 const qreal targetY
= currentItemBottom
- height
;
290 updateKeyboardAnchor();
291 int newIndex
= previousRowIndex(index
);
294 updateKeyboardAnchor();
295 newIndex
= previousRowIndex(index
);
296 } while (m_view
->itemRect(newIndex
).topLeft().y() > targetY
&& newIndex
!= index
);
300 case Qt::Key_PageDown
:
301 if (m_view
->scrollOrientation() == Qt::Horizontal
) {
302 // The new current index should correspond to the last item in the current column.
303 int newIndex
= qMin(index
+ 1, m_model
->count() - 1);
304 while (newIndex
!= index
&& m_view
->itemRect(newIndex
).topLeft().y() > m_view
->itemRect(index
).topLeft().y()) {
306 newIndex
= qMin(index
+ 1, m_model
->count() - 1);
308 m_keyboardAnchorIndex
= index
;
309 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
311 const qreal currentItemTop
= m_view
->itemRect(index
).topLeft().y();
312 const qreal height
= m_view
->geometry().height();
314 // The new current item should be the last item in the current
315 // column whose itemRect's bottom coordinate is smaller than targetY.
316 const qreal targetY
= currentItemTop
+ height
;
318 updateKeyboardAnchor();
319 int newIndex
= nextRowIndex(index
);
322 updateKeyboardAnchor();
323 newIndex
= nextRowIndex(index
);
324 } while (m_view
->itemRect(newIndex
).bottomLeft().y() < targetY
&& newIndex
!= index
);
329 case Qt::Key_Return
: {
330 const QSet
<int> selectedItems
= m_selectionManager
->selectedItems();
331 if (selectedItems
.count() >= 2) {
332 emit
itemsActivated(selectedItems
);
333 } else if (selectedItems
.count() == 1) {
334 emit
itemActivated(selectedItems
.toList().first());
336 emit
itemActivated(index
);
342 if (m_selectionBehavior
== MultiSelection
) {
343 if (controlPressed
) {
344 m_selectionManager
->endAnchoredSelection();
345 m_selectionManager
->setSelected(index
, 1, KItemListSelectionManager::Toggle
);
346 m_selectionManager
->beginAnchoredSelection(index
);
348 const int current
= m_selectionManager
->currentItem();
349 m_selectionManager
->setSelected(current
);
355 // Emit the signal itemContextMenuRequested() in case if at least one
356 // item is selected. Otherwise the signal viewContextMenuRequested() will be emitted.
357 const QSet
<int> selectedItems
= m_selectionManager
->selectedItems();
359 if (selectedItems
.count() >= 2) {
360 const int currentItemIndex
= m_selectionManager
->currentItem();
361 index
= selectedItems
.contains(currentItemIndex
)
362 ? currentItemIndex
: selectedItems
.toList().first();
363 } else if (selectedItems
.count() == 1) {
364 index
= selectedItems
.toList().first();
368 const QRectF contextRect
= m_view
->itemContextRect(index
);
369 const QPointF
pos(m_view
->scene()->views().first()->mapToGlobal(contextRect
.bottomRight().toPoint()));
370 emit
itemContextMenuRequested(index
, pos
);
372 emit
viewContextMenuRequested(QCursor::pos());
378 if (m_selectionBehavior
!= SingleSelection
) {
379 m_selectionManager
->clearSelection();
381 m_keyboardManager
->cancelSearch();
385 m_keyboardManager
->addKeys(event
->text());
389 if (m_selectionManager
->currentItem() != index
) {
390 switch (m_selectionBehavior
) {
392 m_selectionManager
->setCurrentItem(index
);
395 case SingleSelection
:
396 m_selectionManager
->setCurrentItem(index
);
397 m_selectionManager
->clearSelection();
398 m_selectionManager
->setSelected(index
, 1);
402 if (controlPressed
) {
403 m_selectionManager
->endAnchoredSelection();
406 m_selectionManager
->setCurrentItem(index
);
408 if (!shiftOrControlPressed
) {
409 m_selectionManager
->clearSelection();
410 m_selectionManager
->setSelected(index
, 1);
414 m_selectionManager
->beginAnchoredSelection(index
);
419 m_view
->scrollToItem(index
);
424 void KItemListController::slotChangeCurrentItem(const QString
& text
, bool searchFromNextItem
)
426 if (!m_model
|| m_model
->count() == 0) {
429 const int currentIndex
= m_selectionManager
->currentItem();
431 if (searchFromNextItem
) {
432 index
= m_model
->indexForKeyboardSearch(text
, (currentIndex
+ 1) % m_model
->count());
434 index
= m_model
->indexForKeyboardSearch(text
, currentIndex
);
437 m_selectionManager
->setCurrentItem(index
);
438 m_selectionManager
->clearSelection();
439 m_selectionManager
->setSelected(index
, 1);
440 m_selectionManager
->beginAnchoredSelection(index
);
441 m_view
->scrollToItem(index
);
445 void KItemListController::slotAutoActivationTimeout()
447 if (!m_model
|| !m_view
) {
451 const int index
= m_autoActivationTimer
->property("index").toInt();
452 if (index
< 0 || index
>= m_model
->count()) {
456 if (m_model
->supportsDropping(index
)) {
457 if (m_view
->supportsItemExpanding() && m_model
->isExpandable(index
)) {
458 const bool expanded
= m_model
->isExpanded(index
);
459 m_model
->setExpanded(index
, !expanded
);
461 emit
itemActivated(index
);
466 bool KItemListController::inputMethodEvent(QInputMethodEvent
* event
)
472 bool KItemListController::mousePressEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
478 m_pressedMousePos
= transform
.map(event
->pos());
479 m_pressedIndex
= m_view
->itemAt(m_pressedMousePos
);
480 emit
mouseButtonPressed(m_pressedIndex
, event
->buttons());
482 if (m_view
->isAboveExpansionToggle(m_pressedIndex
, m_pressedMousePos
)) {
483 m_selectionManager
->endAnchoredSelection();
484 m_selectionManager
->setCurrentItem(m_pressedIndex
);
485 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
489 m_selectionTogglePressed
= m_view
->isAboveSelectionToggle(m_pressedIndex
, m_pressedMousePos
);
490 if (m_selectionTogglePressed
) {
491 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
492 // The previous anchored selection has been finished already in
493 // KItemListSelectionManager::setSelected(). We can safely change
494 // the current item and start a new anchored selection now.
495 m_selectionManager
->setCurrentItem(m_pressedIndex
);
496 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
500 const bool shiftPressed
= event
->modifiers() & Qt::ShiftModifier
;
501 const bool controlPressed
= event
->modifiers() & Qt::ControlModifier
;
503 // The previous selection is cleared if either
504 // 1. The selection mode is SingleSelection, or
505 // 2. the selection mode is MultiSelection, and *none* of the following conditions are met:
506 // a) Shift or Control are pressed.
507 // b) The clicked item is selected already. In that case, the user might want to:
508 // - start dragging multiple items, or
509 // - open the context menu and perform an action for all selected items.
510 const bool shiftOrControlPressed
= shiftPressed
|| controlPressed
;
511 const bool pressedItemAlreadySelected
= m_pressedIndex
>= 0 && m_selectionManager
->isSelected(m_pressedIndex
);
512 const bool clearSelection
= m_selectionBehavior
== SingleSelection
||
513 (!shiftOrControlPressed
&& !pressedItemAlreadySelected
);
514 if (clearSelection
) {
515 m_selectionManager
->clearSelection();
516 } else if (pressedItemAlreadySelected
&& !shiftOrControlPressed
&& (event
->buttons() & Qt::LeftButton
)) {
517 // The user might want to start dragging multiple items, but if he clicks the item
518 // in order to trigger it instead, the other selected items must be deselected.
519 // However, we do not know yet what the user is going to do.
520 // -> remember that the user pressed an item which had been selected already and
521 // clear the selection in mouseReleaseEvent(), unless the items are dragged.
522 m_clearSelectionIfItemsAreNotDragged
= true;
526 // Finish the anchored selection before the current index is changed
527 m_selectionManager
->endAnchoredSelection();
530 if (m_pressedIndex
>= 0) {
531 m_selectionManager
->setCurrentItem(m_pressedIndex
);
533 switch (m_selectionBehavior
) {
537 case SingleSelection
:
538 m_selectionManager
->setSelected(m_pressedIndex
);
542 if (controlPressed
) {
543 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
544 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
545 } else if (!shiftPressed
|| !m_selectionManager
->isAnchoredSelectionActive()) {
546 // Select the pressed item and start a new anchored selection
547 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Select
);
548 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
557 if (event
->buttons() & Qt::RightButton
) {
558 emit
itemContextMenuRequested(m_pressedIndex
, event
->screenPos());
564 if (event
->buttons() & Qt::RightButton
) {
565 const QRectF headerBounds
= m_view
->headerBoundaries();
566 if (headerBounds
.contains(event
->pos())) {
567 emit
headerContextMenuRequested(event
->screenPos());
569 emit
viewContextMenuRequested(event
->screenPos());
574 if (m_selectionBehavior
== MultiSelection
) {
575 QPointF startPos
= m_pressedMousePos
;
576 if (m_view
->scrollOrientation() == Qt::Vertical
) {
577 startPos
.ry() += m_view
->scrollOffset();
578 if (m_view
->itemSize().width() < 0) {
579 // Use a special rubberband for views that have only one column and
580 // expand the rubberband to use the whole width of the view.
584 startPos
.rx() += m_view
->scrollOffset();
587 m_oldSelection
= m_selectionManager
->selectedItems();
588 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
589 rubberBand
->setStartPosition(startPos
);
590 rubberBand
->setEndPosition(startPos
);
591 rubberBand
->setActive(true);
592 connect(rubberBand
, SIGNAL(endPositionChanged(QPointF
,QPointF
)), this, SLOT(slotRubberBandChanged()));
593 m_view
->setAutoScroll(true);
599 bool KItemListController::mouseMoveEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
605 if (m_pressedIndex
>= 0) {
606 // Check whether a dragging should be started
607 if (event
->buttons() & Qt::LeftButton
) {
608 const QPointF pos
= transform
.map(event
->pos());
609 if ((pos
- m_pressedMousePos
).manhattanLength() >= QApplication::startDragDistance()) {
610 if (!m_selectionManager
->isSelected(m_pressedIndex
)) {
611 // Always assure that the dragged item gets selected. Usually this is already
612 // done on the mouse-press event, but when using the selection-toggle on a
613 // selected item the dragged item is not selected yet.
614 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
616 // A selected item has been clicked to drag all selected items
617 // -> the selection should not be cleared when the mouse button is released.
618 m_clearSelectionIfItemsAreNotDragged
= false;
625 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
626 if (rubberBand
->isActive()) {
627 QPointF endPos
= transform
.map(event
->pos());
629 // Update the current item.
630 const int newCurrent
= m_view
->itemAt(endPos
);
631 if (newCurrent
>= 0) {
632 // It's expected that the new current index is also the new anchor (bug 163451).
633 m_selectionManager
->endAnchoredSelection();
634 m_selectionManager
->setCurrentItem(newCurrent
);
635 m_selectionManager
->beginAnchoredSelection(newCurrent
);
638 if (m_view
->scrollOrientation() == Qt::Vertical
) {
639 endPos
.ry() += m_view
->scrollOffset();
640 if (m_view
->itemSize().width() < 0) {
641 // Use a special rubberband for views that have only one column and
642 // expand the rubberband to use the whole width of the view.
643 endPos
.setX(m_view
->size().width());
646 endPos
.rx() += m_view
->scrollOffset();
648 rubberBand
->setEndPosition(endPos
);
655 bool KItemListController::mouseReleaseEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
661 emit
mouseButtonReleased(m_pressedIndex
, event
->buttons());
663 const bool isAboveSelectionToggle
= m_view
->isAboveSelectionToggle(m_pressedIndex
, m_pressedMousePos
);
664 if (isAboveSelectionToggle
) {
665 m_selectionTogglePressed
= false;
669 if (!isAboveSelectionToggle
&& m_selectionTogglePressed
) {
670 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
671 m_selectionTogglePressed
= false;
675 const bool shiftOrControlPressed
= event
->modifiers() & Qt::ShiftModifier
||
676 event
->modifiers() & Qt::ControlModifier
;
678 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
679 if (rubberBand
->isActive()) {
680 disconnect(rubberBand
, SIGNAL(endPositionChanged(QPointF
,QPointF
)), this, SLOT(slotRubberBandChanged()));
681 rubberBand
->setActive(false);
682 m_oldSelection
.clear();
683 m_view
->setAutoScroll(false);
686 const QPointF pos
= transform
.map(event
->pos());
687 const int index
= m_view
->itemAt(pos
);
689 if (index
>= 0 && index
== m_pressedIndex
) {
690 // The release event is done above the same item as the press event
692 if (m_clearSelectionIfItemsAreNotDragged
) {
693 // A selected item has been clicked, but no drag operation has been started
694 // -> clear the rest of the selection.
695 m_selectionManager
->clearSelection();
696 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Select
);
697 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
700 if (event
->button() & Qt::LeftButton
) {
701 bool emitItemActivated
= true;
702 if (m_view
->isAboveExpansionToggle(index
, pos
)) {
703 const bool expanded
= m_model
->isExpanded(index
);
704 m_model
->setExpanded(index
, !expanded
);
706 emit
itemExpansionToggleClicked(index
);
707 emitItemActivated
= false;
708 } else if (shiftOrControlPressed
) {
709 // The mouse click should only update the selection, not trigger the item
710 emitItemActivated
= false;
711 } else if (!m_singleClickActivation
) {
712 emitItemActivated
= false;
714 if (emitItemActivated
) {
715 emit
itemActivated(index
);
717 } else if (event
->button() & Qt::MidButton
) {
718 emit
itemMiddleClicked(index
);
722 m_pressedMousePos
= QPointF();
724 m_clearSelectionIfItemsAreNotDragged
= false;
728 bool KItemListController::mouseDoubleClickEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
730 const QPointF pos
= transform
.map(event
->pos());
731 const int index
= m_view
->itemAt(pos
);
733 bool emitItemActivated
= !m_singleClickActivation
&&
734 (event
->button() & Qt::LeftButton
) &&
735 index
>= 0 && index
< m_model
->count();
736 if (emitItemActivated
) {
737 emit
itemActivated(index
);
742 bool KItemListController::dragEnterEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
749 bool KItemListController::dragLeaveEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
754 m_view
->setAutoScroll(false);
756 KItemListWidget
* widget
= hoveredWidget();
758 widget
->setHovered(false);
759 emit
itemUnhovered(widget
->index());
764 bool KItemListController::dragMoveEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
767 if (!m_model
|| !m_view
) {
771 event
->acceptProposedAction();
773 KItemListWidget
* oldHoveredWidget
= hoveredWidget();
775 const QPointF pos
= transform
.map(event
->pos());
776 KItemListWidget
* newHoveredWidget
= widgetForPos(pos
);
778 if (oldHoveredWidget
!= newHoveredWidget
) {
779 m_autoActivationTimer
->stop();
781 if (oldHoveredWidget
) {
782 oldHoveredWidget
->setHovered(false);
783 emit
itemUnhovered(oldHoveredWidget
->index());
786 if (newHoveredWidget
) {
787 const int index
= newHoveredWidget
->index();
788 if (m_model
->supportsDropping(index
)) {
789 newHoveredWidget
->setHovered(true);
791 emit
itemHovered(index
);
793 if (m_autoActivationTimer
->interval() >= 0) {
794 m_autoActivationTimer
->setProperty("index", index
);
795 m_autoActivationTimer
->start();
803 bool KItemListController::dropEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
810 m_autoActivationTimer
->stop();
811 m_view
->setAutoScroll(false);
813 const QPointF pos
= transform
.map(event
->pos());
814 const int index
= m_view
->itemAt(pos
);
815 emit
itemDropEvent(index
, event
);
820 bool KItemListController::hoverEnterEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
827 bool KItemListController::hoverMoveEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
830 if (!m_model
|| !m_view
) {
834 KItemListWidget
* oldHoveredWidget
= hoveredWidget();
835 const QPointF pos
= transform
.map(event
->pos());
836 KItemListWidget
* newHoveredWidget
= widgetForPos(pos
);
838 if (oldHoveredWidget
!= newHoveredWidget
) {
839 if (oldHoveredWidget
) {
840 oldHoveredWidget
->setHovered(false);
841 emit
itemUnhovered(oldHoveredWidget
->index());
844 if (newHoveredWidget
) {
845 newHoveredWidget
->setHovered(true);
846 emit
itemHovered(newHoveredWidget
->index());
853 bool KItemListController::hoverLeaveEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
858 if (!m_model
|| !m_view
) {
862 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
863 if (widget
->isHovered()) {
864 widget
->setHovered(false);
865 emit
itemUnhovered(widget
->index());
871 bool KItemListController::wheelEvent(QGraphicsSceneWheelEvent
* event
, const QTransform
& transform
)
878 bool KItemListController::resizeEvent(QGraphicsSceneResizeEvent
* event
, const QTransform
& transform
)
885 bool KItemListController::processEvent(QEvent
* event
, const QTransform
& transform
)
891 switch (event
->type()) {
892 case QEvent::KeyPress
:
893 return keyPressEvent(static_cast<QKeyEvent
*>(event
));
894 case QEvent::InputMethod
:
895 return inputMethodEvent(static_cast<QInputMethodEvent
*>(event
));
896 case QEvent::GraphicsSceneMousePress
:
897 return mousePressEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
898 case QEvent::GraphicsSceneMouseMove
:
899 return mouseMoveEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
900 case QEvent::GraphicsSceneMouseRelease
:
901 return mouseReleaseEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
902 case QEvent::GraphicsSceneMouseDoubleClick
:
903 return mouseDoubleClickEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
904 case QEvent::GraphicsSceneWheel
:
905 return wheelEvent(static_cast<QGraphicsSceneWheelEvent
*>(event
), QTransform());
906 case QEvent::GraphicsSceneDragEnter
:
907 return dragEnterEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
908 case QEvent::GraphicsSceneDragLeave
:
909 return dragLeaveEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
910 case QEvent::GraphicsSceneDragMove
:
911 return dragMoveEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
912 case QEvent::GraphicsSceneDrop
:
913 return dropEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
914 case QEvent::GraphicsSceneHoverEnter
:
915 return hoverEnterEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
916 case QEvent::GraphicsSceneHoverMove
:
917 return hoverMoveEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
918 case QEvent::GraphicsSceneHoverLeave
:
919 return hoverLeaveEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
920 case QEvent::GraphicsSceneResize
:
921 return resizeEvent(static_cast<QGraphicsSceneResizeEvent
*>(event
), transform
);
929 void KItemListController::slotViewScrollOffsetChanged(qreal current
, qreal previous
)
935 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
936 if (rubberBand
->isActive()) {
937 const qreal diff
= current
- previous
;
938 // TODO: Ideally just QCursor::pos() should be used as
939 // new end-position but it seems there is no easy way
940 // to have something like QWidget::mapFromGlobal() for QGraphicsWidget
941 // (... or I just missed an easy way to do the mapping)
942 QPointF endPos
= rubberBand
->endPosition();
943 if (m_view
->scrollOrientation() == Qt::Vertical
) {
949 rubberBand
->setEndPosition(endPos
);
953 void KItemListController::slotRubberBandChanged()
955 if (!m_view
|| !m_model
|| m_model
->count() <= 0) {
959 const KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
960 const QPointF startPos
= rubberBand
->startPosition();
961 const QPointF endPos
= rubberBand
->endPosition();
962 QRectF rubberBandRect
= QRectF(startPos
, endPos
).normalized();
964 const bool scrollVertical
= (m_view
->scrollOrientation() == Qt::Vertical
);
965 if (scrollVertical
) {
966 rubberBandRect
.translate(0, -m_view
->scrollOffset());
968 rubberBandRect
.translate(-m_view
->scrollOffset(), 0);
971 if (!m_oldSelection
.isEmpty()) {
972 // Clear the old selection that was available before the rubberband has
973 // been activated in case if no Shift- or Control-key are pressed
974 const bool shiftOrControlPressed
= QApplication::keyboardModifiers() & Qt::ShiftModifier
||
975 QApplication::keyboardModifiers() & Qt::ControlModifier
;
976 if (!shiftOrControlPressed
) {
977 m_oldSelection
.clear();
981 QSet
<int> selectedItems
;
983 // Select all visible items that intersect with the rubberband
984 foreach (const KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
985 const int index
= widget
->index();
987 const QRectF widgetRect
= m_view
->itemRect(index
);
988 if (widgetRect
.intersects(rubberBandRect
)) {
989 const QRectF iconRect
= widget
->iconRect().translated(widgetRect
.topLeft());
990 const QRectF textRect
= widget
->textRect().translated(widgetRect
.topLeft());
991 if (iconRect
.intersects(rubberBandRect
) || textRect
.intersects(rubberBandRect
)) {
992 selectedItems
.insert(index
);
997 // Select all invisible items that intersect with the rubberband. Instead of
998 // iterating all items only the area which might be touched by the rubberband
1000 const bool increaseIndex
= scrollVertical
?
1001 startPos
.y() > endPos
.y(): startPos
.x() > endPos
.x();
1003 int index
= increaseIndex
? m_view
->lastVisibleIndex() + 1 : m_view
->firstVisibleIndex() - 1;
1004 bool selectionFinished
= false;
1006 const QRectF widgetRect
= m_view
->itemRect(index
);
1007 if (widgetRect
.intersects(rubberBandRect
)) {
1008 selectedItems
.insert(index
);
1011 if (increaseIndex
) {
1013 selectionFinished
= (index
>= m_model
->count()) ||
1014 ( scrollVertical
&& widgetRect
.top() > rubberBandRect
.bottom()) ||
1015 (!scrollVertical
&& widgetRect
.left() > rubberBandRect
.right());
1018 selectionFinished
= (index
< 0) ||
1019 ( scrollVertical
&& widgetRect
.bottom() < rubberBandRect
.top()) ||
1020 (!scrollVertical
&& widgetRect
.right() < rubberBandRect
.left());
1022 } while (!selectionFinished
);
1024 if (QApplication::keyboardModifiers() & Qt::ControlModifier
) {
1025 // If Control is pressed, the selection state of all items in the rubberband is toggled.
1026 // Therefore, the new selection contains:
1027 // 1. All previously selected items which are not inside the rubberband, and
1028 // 2. all items inside the rubberband which have not been selected previously.
1029 m_selectionManager
->setSelectedItems((m_oldSelection
- selectedItems
) + (selectedItems
- m_oldSelection
));
1032 m_selectionManager
->setSelectedItems(selectedItems
+ m_oldSelection
);
1036 void KItemListController::startDragging()
1038 if (!m_view
|| !m_model
) {
1042 const QSet
<int> selectedItems
= m_selectionManager
->selectedItems();
1043 if (selectedItems
.isEmpty()) {
1047 QMimeData
* data
= m_model
->createMimeData(selectedItems
);
1052 // The created drag object will be owned and deleted
1053 // by QApplication::activeWindow().
1054 QDrag
* drag
= new QDrag(QApplication::activeWindow());
1055 drag
->setMimeData(data
);
1057 const QPixmap pixmap
= m_view
->createDragPixmap(selectedItems
);
1058 drag
->setPixmap(pixmap
);
1060 drag
->exec(Qt::MoveAction
| Qt::CopyAction
| Qt::LinkAction
, Qt::CopyAction
);
1063 KItemListWidget
* KItemListController::hoveredWidget() const
1067 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
1068 if (widget
->isHovered()) {
1076 KItemListWidget
* KItemListController::widgetForPos(const QPointF
& pos
) const
1080 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
1081 const QPointF mappedPos
= widget
->mapFromItem(m_view
, pos
);
1083 const bool hovered
= widget
->contains(mappedPos
) &&
1084 !widget
->expansionToggleRect().contains(mappedPos
);
1093 void KItemListController::updateKeyboardAnchor()
1095 const bool validAnchor
= m_keyboardAnchorIndex
>= 0 &&
1096 m_keyboardAnchorIndex
< m_model
->count() &&
1097 keyboardAnchorPos(m_keyboardAnchorIndex
) == m_keyboardAnchorPos
;
1099 const int index
= m_selectionManager
->currentItem();
1100 m_keyboardAnchorIndex
= index
;
1101 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
1105 int KItemListController::nextRowIndex(int index
) const
1107 if (m_keyboardAnchorIndex
< 0) {
1111 const int maxIndex
= m_model
->count() - 1;
1112 if (index
== maxIndex
) {
1116 // Calculate the index of the last column inside the row of the current index
1117 int lastColumnIndex
= index
;
1118 while (keyboardAnchorPos(lastColumnIndex
+ 1) > keyboardAnchorPos(lastColumnIndex
)) {
1120 if (lastColumnIndex
>= maxIndex
) {
1125 // Based on the last column index go to the next row and calculate the nearest index
1126 // that is below the current index
1127 int nextRowIndex
= lastColumnIndex
+ 1;
1128 int searchIndex
= nextRowIndex
;
1129 qreal minDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(nextRowIndex
));
1130 while (searchIndex
< maxIndex
&& keyboardAnchorPos(searchIndex
+ 1) > keyboardAnchorPos(searchIndex
)) {
1132 const qreal searchDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(searchIndex
));
1133 if (searchDiff
< minDiff
) {
1134 minDiff
= searchDiff
;
1135 nextRowIndex
= searchIndex
;
1139 return nextRowIndex
;
1142 int KItemListController::previousRowIndex(int index
) const
1144 if (m_keyboardAnchorIndex
< 0 || index
== 0) {
1148 // Calculate the index of the first column inside the row of the current index
1149 int firstColumnIndex
= index
;
1150 while (keyboardAnchorPos(firstColumnIndex
- 1) < keyboardAnchorPos(firstColumnIndex
)) {
1152 if (firstColumnIndex
<= 0) {
1157 // Based on the first column index go to the previous row and calculate the nearest index
1158 // that is above the current index
1159 int previousRowIndex
= firstColumnIndex
- 1;
1160 int searchIndex
= previousRowIndex
;
1161 qreal minDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(previousRowIndex
));
1162 while (searchIndex
> 0 && keyboardAnchorPos(searchIndex
- 1) < keyboardAnchorPos(searchIndex
)) {
1164 const qreal searchDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(searchIndex
));
1165 if (searchDiff
< minDiff
) {
1166 minDiff
= searchDiff
;
1167 previousRowIndex
= searchIndex
;
1171 return previousRowIndex
;
1174 qreal
KItemListController::keyboardAnchorPos(int index
) const
1176 const QRectF itemRect
= m_view
->itemRect(index
);
1177 if (!itemRect
.isEmpty()) {
1178 return (m_view
->scrollOrientation() == Qt::Vertical
) ? itemRect
.x() : itemRect
.y();
1184 void KItemListController::updateExtendedSelectionRegion()
1187 const bool extend
= (m_selectionBehavior
!= MultiSelection
);
1188 KItemListStyleOption option
= m_view
->styleOption();
1189 if (option
.extendedSelectionRegion
!= extend
) {
1190 option
.extendedSelectionRegion
= extend
;
1191 m_view
->setStyleOption(option
);
1196 #include "kitemlistcontroller.moc"