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()
84 void KItemListController::setModel(KItemModelBase
* model
)
86 if (m_model
== model
) {
90 KItemModelBase
* oldModel
= m_model
;
92 oldModel
->deleteLater();
97 m_model
->setParent(this);
101 m_view
->setModel(m_model
);
104 m_selectionManager
->setModel(m_model
);
106 emit
modelChanged(m_model
, oldModel
);
109 KItemModelBase
* KItemListController::model() const
114 KItemListSelectionManager
* KItemListController::selectionManager() const
116 return m_selectionManager
;
119 void KItemListController::setView(KItemListView
* view
)
121 if (m_view
== view
) {
125 KItemListView
* oldView
= m_view
;
127 disconnect(oldView
, SIGNAL(scrollOffsetChanged(qreal
,qreal
)), this, SLOT(slotViewScrollOffsetChanged(qreal
,qreal
)));
128 oldView
->deleteLater();
134 m_view
->setParent(this);
135 m_view
->setController(this);
136 m_view
->setModel(m_model
);
137 connect(m_view
, SIGNAL(scrollOffsetChanged(qreal
,qreal
)), this, SLOT(slotViewScrollOffsetChanged(qreal
,qreal
)));
138 updateExtendedSelectionRegion();
141 emit
viewChanged(m_view
, oldView
);
144 KItemListView
* KItemListController::view() const
149 void KItemListController::setSelectionBehavior(SelectionBehavior behavior
)
151 m_selectionBehavior
= behavior
;
152 updateExtendedSelectionRegion();
155 KItemListController::SelectionBehavior
KItemListController::selectionBehavior() const
157 return m_selectionBehavior
;
160 void KItemListController::setAutoActivationDelay(int delay
)
162 m_autoActivationTimer
->setInterval(delay
);
165 int KItemListController::autoActivationDelay() const
167 return m_autoActivationTimer
->interval();
170 void KItemListController::setSingleClickActivation(bool singleClick
)
172 m_singleClickActivation
= singleClick
;
175 bool KItemListController::singleClickActivation() const
177 return m_singleClickActivation
;
180 bool KItemListController::showEvent(QShowEvent
* event
)
186 bool KItemListController::hideEvent(QHideEvent
* event
)
192 bool KItemListController::keyPressEvent(QKeyEvent
* event
)
194 int index
= m_selectionManager
->currentItem();
195 int key
= event
->key();
197 // Handle the expanding/collapsing of items
198 if (m_view
->supportsItemExpanding() && m_model
->isExpandable(index
)) {
199 if (key
== Qt::Key_Right
) {
200 if (m_model
->setExpanded(index
, true)) {
203 } else if (key
== Qt::Key_Left
) {
204 if (m_model
->setExpanded(index
, false)) {
210 const bool shiftPressed
= event
->modifiers() & Qt::ShiftModifier
;
211 const bool controlPressed
= event
->modifiers() & Qt::ControlModifier
;
212 const bool shiftOrControlPressed
= shiftPressed
|| controlPressed
;
214 const int itemCount
= m_model
->count();
216 // For horizontal scroll orientation, transform
217 // the arrow keys to simplify the event handling.
218 if (m_view
->scrollOrientation() == Qt::Horizontal
) {
220 case Qt::Key_Up
: key
= Qt::Key_Left
; break;
221 case Qt::Key_Down
: key
= Qt::Key_Right
; break;
222 case Qt::Key_Left
: key
= Qt::Key_Up
; break;
223 case Qt::Key_Right
: key
= Qt::Key_Down
; break;
228 const bool selectSingleItem
= m_selectionBehavior
!= NoSelection
&&
230 (key
== Qt::Key_Home
|| key
== Qt::Key_End
||
231 key
== Qt::Key_Up
|| key
== Qt::Key_Down
||
232 key
== Qt::Key_Left
|| key
== Qt::Key_Right
);
233 if (selectSingleItem
) {
234 const int current
= m_selectionManager
->currentItem();
235 m_selectionManager
->setSelected(current
);
242 m_keyboardAnchorIndex
= index
;
243 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
247 index
= itemCount
- 1;
248 m_keyboardAnchorIndex
= index
;
249 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
255 m_keyboardAnchorIndex
= index
;
256 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
261 if (index
< itemCount
- 1) {
263 m_keyboardAnchorIndex
= index
;
264 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
269 updateKeyboardAnchor();
270 index
= previousRowIndex(index
);
274 updateKeyboardAnchor();
275 index
= nextRowIndex(index
);
279 if (m_view
->scrollOrientation() == Qt::Horizontal
) {
280 // The new current index should correspond to the first item in the current column.
281 int newIndex
= qMax(index
- 1, 0);
282 while (newIndex
!= index
&& m_view
->itemRect(newIndex
).topLeft().y() < m_view
->itemRect(index
).topLeft().y()) {
284 newIndex
= qMax(index
- 1, 0);
286 m_keyboardAnchorIndex
= index
;
287 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
289 const qreal currentItemBottom
= m_view
->itemRect(index
).bottomLeft().y();
290 const qreal height
= m_view
->geometry().height();
292 // The new current item should be the first item in the current
293 // column whose itemRect's top coordinate is larger than targetY.
294 const qreal targetY
= currentItemBottom
- height
;
296 updateKeyboardAnchor();
297 int newIndex
= previousRowIndex(index
);
300 updateKeyboardAnchor();
301 newIndex
= previousRowIndex(index
);
302 } while (m_view
->itemRect(newIndex
).topLeft().y() > targetY
&& newIndex
!= index
);
306 case Qt::Key_PageDown
:
307 if (m_view
->scrollOrientation() == Qt::Horizontal
) {
308 // The new current index should correspond to the last item in the current column.
309 int newIndex
= qMin(index
+ 1, m_model
->count() - 1);
310 while (newIndex
!= index
&& m_view
->itemRect(newIndex
).topLeft().y() > m_view
->itemRect(index
).topLeft().y()) {
312 newIndex
= qMin(index
+ 1, m_model
->count() - 1);
314 m_keyboardAnchorIndex
= index
;
315 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
317 const qreal currentItemTop
= m_view
->itemRect(index
).topLeft().y();
318 const qreal height
= m_view
->geometry().height();
320 // The new current item should be the last item in the current
321 // column whose itemRect's bottom coordinate is smaller than targetY.
322 const qreal targetY
= currentItemTop
+ height
;
324 updateKeyboardAnchor();
325 int newIndex
= nextRowIndex(index
);
328 updateKeyboardAnchor();
329 newIndex
= nextRowIndex(index
);
330 } while (m_view
->itemRect(newIndex
).bottomLeft().y() < targetY
&& newIndex
!= index
);
335 case Qt::Key_Return
: {
336 const QSet
<int> selectedItems
= m_selectionManager
->selectedItems();
337 if (selectedItems
.count() >= 2) {
338 emit
itemsActivated(selectedItems
);
339 } else if (selectedItems
.count() == 1) {
340 emit
itemActivated(selectedItems
.toList().first());
342 emit
itemActivated(index
);
348 if (m_selectionBehavior
== MultiSelection
) {
349 if (controlPressed
) {
350 m_selectionManager
->endAnchoredSelection();
351 m_selectionManager
->setSelected(index
, 1, KItemListSelectionManager::Toggle
);
352 m_selectionManager
->beginAnchoredSelection(index
);
354 const int current
= m_selectionManager
->currentItem();
355 m_selectionManager
->setSelected(current
);
361 // Emit the signal itemContextMenuRequested() in case if at least one
362 // item is selected. Otherwise the signal viewContextMenuRequested() will be emitted.
363 const QSet
<int> selectedItems
= m_selectionManager
->selectedItems();
365 if (selectedItems
.count() >= 2) {
366 const int currentItemIndex
= m_selectionManager
->currentItem();
367 index
= selectedItems
.contains(currentItemIndex
)
368 ? currentItemIndex
: selectedItems
.toList().first();
369 } else if (selectedItems
.count() == 1) {
370 index
= selectedItems
.toList().first();
374 const QRectF contextRect
= m_view
->itemContextRect(index
);
375 const QPointF
pos(m_view
->scene()->views().first()->mapToGlobal(contextRect
.bottomRight().toPoint()));
376 emit
itemContextMenuRequested(index
, pos
);
378 emit
viewContextMenuRequested(QCursor::pos());
384 if (m_selectionBehavior
!= SingleSelection
) {
385 m_selectionManager
->clearSelection();
387 m_keyboardManager
->cancelSearch();
391 m_keyboardManager
->addKeys(event
->text());
395 if (m_selectionManager
->currentItem() != index
) {
396 switch (m_selectionBehavior
) {
398 m_selectionManager
->setCurrentItem(index
);
401 case SingleSelection
:
402 m_selectionManager
->setCurrentItem(index
);
403 m_selectionManager
->clearSelection();
404 m_selectionManager
->setSelected(index
, 1);
408 if (controlPressed
) {
409 m_selectionManager
->endAnchoredSelection();
412 m_selectionManager
->setCurrentItem(index
);
414 if (!shiftOrControlPressed
) {
415 m_selectionManager
->clearSelection();
416 m_selectionManager
->setSelected(index
, 1);
420 m_selectionManager
->beginAnchoredSelection(index
);
425 m_view
->scrollToItem(index
);
430 void KItemListController::slotChangeCurrentItem(const QString
& text
, bool searchFromNextItem
)
432 if (!m_model
|| m_model
->count() == 0) {
435 const int currentIndex
= m_selectionManager
->currentItem();
437 if (searchFromNextItem
) {
438 index
= m_model
->indexForKeyboardSearch(text
, (currentIndex
+ 1) % m_model
->count());
440 index
= m_model
->indexForKeyboardSearch(text
, currentIndex
);
443 m_selectionManager
->setCurrentItem(index
);
444 m_selectionManager
->clearSelection();
445 m_selectionManager
->setSelected(index
, 1);
446 m_selectionManager
->beginAnchoredSelection(index
);
447 m_view
->scrollToItem(index
);
451 void KItemListController::slotAutoActivationTimeout()
453 if (!m_model
|| !m_view
) {
457 const int index
= m_autoActivationTimer
->property("index").toInt();
458 if (index
< 0 || index
>= m_model
->count()) {
462 if (m_model
->supportsDropping(index
)) {
463 if (m_view
->supportsItemExpanding() && m_model
->isExpandable(index
)) {
464 const bool expanded
= m_model
->isExpanded(index
);
465 m_model
->setExpanded(index
, !expanded
);
467 emit
itemActivated(index
);
472 bool KItemListController::inputMethodEvent(QInputMethodEvent
* event
)
478 bool KItemListController::mousePressEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
484 m_pressedMousePos
= transform
.map(event
->pos());
485 m_pressedIndex
= m_view
->itemAt(m_pressedMousePos
);
486 emit
mouseButtonPressed(m_pressedIndex
, event
->buttons());
488 if (m_view
->isAboveExpansionToggle(m_pressedIndex
, m_pressedMousePos
)) {
489 m_selectionManager
->endAnchoredSelection();
490 m_selectionManager
->setCurrentItem(m_pressedIndex
);
491 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
495 m_selectionTogglePressed
= m_view
->isAboveSelectionToggle(m_pressedIndex
, m_pressedMousePos
);
496 if (m_selectionTogglePressed
) {
497 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
498 // The previous anchored selection has been finished already in
499 // KItemListSelectionManager::setSelected(). We can safely change
500 // the current item and start a new anchored selection now.
501 m_selectionManager
->setCurrentItem(m_pressedIndex
);
502 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
506 const bool shiftPressed
= event
->modifiers() & Qt::ShiftModifier
;
507 const bool controlPressed
= event
->modifiers() & Qt::ControlModifier
;
509 // The previous selection is cleared if either
510 // 1. The selection mode is SingleSelection, or
511 // 2. the selection mode is MultiSelection, and *none* of the following conditions are met:
512 // a) Shift or Control are pressed.
513 // b) The clicked item is selected already. In that case, the user might want to:
514 // - start dragging multiple items, or
515 // - open the context menu and perform an action for all selected items.
516 const bool shiftOrControlPressed
= shiftPressed
|| controlPressed
;
517 const bool pressedItemAlreadySelected
= m_pressedIndex
>= 0 && m_selectionManager
->isSelected(m_pressedIndex
);
518 const bool clearSelection
= m_selectionBehavior
== SingleSelection
||
519 (!shiftOrControlPressed
&& !pressedItemAlreadySelected
);
520 if (clearSelection
) {
521 m_selectionManager
->clearSelection();
522 } else if (pressedItemAlreadySelected
&& !shiftOrControlPressed
&& (event
->buttons() & Qt::LeftButton
)) {
523 // The user might want to start dragging multiple items, but if he clicks the item
524 // in order to trigger it instead, the other selected items must be deselected.
525 // However, we do not know yet what the user is going to do.
526 // -> remember that the user pressed an item which had been selected already and
527 // clear the selection in mouseReleaseEvent(), unless the items are dragged.
528 m_clearSelectionIfItemsAreNotDragged
= true;
532 // Finish the anchored selection before the current index is changed
533 m_selectionManager
->endAnchoredSelection();
536 if (m_pressedIndex
>= 0) {
537 m_selectionManager
->setCurrentItem(m_pressedIndex
);
539 switch (m_selectionBehavior
) {
543 case SingleSelection
:
544 m_selectionManager
->setSelected(m_pressedIndex
);
548 if (controlPressed
) {
549 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
550 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
551 } else if (!shiftPressed
|| !m_selectionManager
->isAnchoredSelectionActive()) {
552 // Select the pressed item and start a new anchored selection
553 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Select
);
554 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
563 if (event
->buttons() & Qt::RightButton
) {
564 emit
itemContextMenuRequested(m_pressedIndex
, event
->screenPos());
570 if (event
->buttons() & Qt::RightButton
) {
571 const QRectF headerBounds
= m_view
->headerBoundaries();
572 if (headerBounds
.contains(event
->pos())) {
573 emit
headerContextMenuRequested(event
->screenPos());
575 emit
viewContextMenuRequested(event
->screenPos());
580 if (m_selectionBehavior
== MultiSelection
) {
581 QPointF startPos
= m_pressedMousePos
;
582 if (m_view
->scrollOrientation() == Qt::Vertical
) {
583 startPos
.ry() += m_view
->scrollOffset();
584 if (m_view
->itemSize().width() < 0) {
585 // Use a special rubberband for views that have only one column and
586 // expand the rubberband to use the whole width of the view.
590 startPos
.rx() += m_view
->scrollOffset();
593 m_oldSelection
= m_selectionManager
->selectedItems();
594 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
595 rubberBand
->setStartPosition(startPos
);
596 rubberBand
->setEndPosition(startPos
);
597 rubberBand
->setActive(true);
598 connect(rubberBand
, SIGNAL(endPositionChanged(QPointF
,QPointF
)), this, SLOT(slotRubberBandChanged()));
599 m_view
->setAutoScroll(true);
605 bool KItemListController::mouseMoveEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
611 if (m_pressedIndex
>= 0) {
612 // Check whether a dragging should be started
613 if (event
->buttons() & Qt::LeftButton
) {
614 const QPointF pos
= transform
.map(event
->pos());
615 if ((pos
- m_pressedMousePos
).manhattanLength() >= QApplication::startDragDistance()) {
616 if (!m_selectionManager
->isSelected(m_pressedIndex
)) {
617 // Always assure that the dragged item gets selected. Usually this is already
618 // done on the mouse-press event, but when using the selection-toggle on a
619 // selected item the dragged item is not selected yet.
620 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
622 // A selected item has been clicked to drag all selected items
623 // -> the selection should not be cleared when the mouse button is released.
624 m_clearSelectionIfItemsAreNotDragged
= false;
631 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
632 if (rubberBand
->isActive()) {
633 QPointF endPos
= transform
.map(event
->pos());
635 // Update the current item.
636 const int newCurrent
= m_view
->itemAt(endPos
);
637 if (newCurrent
>= 0) {
638 // It's expected that the new current index is also the new anchor (bug 163451).
639 m_selectionManager
->endAnchoredSelection();
640 m_selectionManager
->setCurrentItem(newCurrent
);
641 m_selectionManager
->beginAnchoredSelection(newCurrent
);
644 if (m_view
->scrollOrientation() == Qt::Vertical
) {
645 endPos
.ry() += m_view
->scrollOffset();
646 if (m_view
->itemSize().width() < 0) {
647 // Use a special rubberband for views that have only one column and
648 // expand the rubberband to use the whole width of the view.
649 endPos
.setX(m_view
->size().width());
652 endPos
.rx() += m_view
->scrollOffset();
654 rubberBand
->setEndPosition(endPos
);
661 bool KItemListController::mouseReleaseEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
667 emit
mouseButtonReleased(m_pressedIndex
, event
->buttons());
669 const bool isAboveSelectionToggle
= m_view
->isAboveSelectionToggle(m_pressedIndex
, m_pressedMousePos
);
670 if (isAboveSelectionToggle
) {
671 m_selectionTogglePressed
= false;
675 if (!isAboveSelectionToggle
&& m_selectionTogglePressed
) {
676 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
677 m_selectionTogglePressed
= false;
681 const bool shiftOrControlPressed
= event
->modifiers() & Qt::ShiftModifier
||
682 event
->modifiers() & Qt::ControlModifier
;
684 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
685 if (rubberBand
->isActive()) {
686 disconnect(rubberBand
, SIGNAL(endPositionChanged(QPointF
,QPointF
)), this, SLOT(slotRubberBandChanged()));
687 rubberBand
->setActive(false);
688 m_oldSelection
.clear();
689 m_view
->setAutoScroll(false);
692 const QPointF pos
= transform
.map(event
->pos());
693 const int index
= m_view
->itemAt(pos
);
695 if (index
>= 0 && index
== m_pressedIndex
) {
696 // The release event is done above the same item as the press event
698 if (m_clearSelectionIfItemsAreNotDragged
) {
699 // A selected item has been clicked, but no drag operation has been started
700 // -> clear the rest of the selection.
701 m_selectionManager
->clearSelection();
702 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Select
);
703 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
706 if (event
->button() & Qt::LeftButton
) {
707 bool emitItemActivated
= true;
708 if (m_view
->isAboveExpansionToggle(index
, pos
)) {
709 const bool expanded
= m_model
->isExpanded(index
);
710 m_model
->setExpanded(index
, !expanded
);
712 emit
itemExpansionToggleClicked(index
);
713 emitItemActivated
= false;
714 } else if (shiftOrControlPressed
) {
715 // The mouse click should only update the selection, not trigger the item
716 emitItemActivated
= false;
717 } else if (!m_singleClickActivation
) {
718 emitItemActivated
= false;
720 if (emitItemActivated
) {
721 emit
itemActivated(index
);
723 } else if (event
->button() & Qt::MidButton
) {
724 emit
itemMiddleClicked(index
);
728 m_pressedMousePos
= QPointF();
730 m_clearSelectionIfItemsAreNotDragged
= false;
734 bool KItemListController::mouseDoubleClickEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
736 const QPointF pos
= transform
.map(event
->pos());
737 const int index
= m_view
->itemAt(pos
);
739 bool emitItemActivated
= !m_singleClickActivation
&&
740 (event
->button() & Qt::LeftButton
) &&
741 index
>= 0 && index
< m_model
->count();
742 if (emitItemActivated
) {
743 emit
itemActivated(index
);
748 bool KItemListController::dragEnterEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
755 bool KItemListController::dragLeaveEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
760 m_view
->setAutoScroll(false);
761 m_view
->hideDropIndicator();
763 KItemListWidget
* widget
= hoveredWidget();
765 widget
->setHovered(false);
766 emit
itemUnhovered(widget
->index());
771 bool KItemListController::dragMoveEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
774 if (!m_model
|| !m_view
) {
778 event
->acceptProposedAction();
780 KItemListWidget
* oldHoveredWidget
= hoveredWidget();
782 const QPointF pos
= transform
.map(event
->pos());
783 KItemListWidget
* newHoveredWidget
= widgetForPos(pos
);
785 if (oldHoveredWidget
!= newHoveredWidget
) {
786 m_autoActivationTimer
->stop();
788 if (oldHoveredWidget
) {
789 oldHoveredWidget
->setHovered(false);
790 emit
itemUnhovered(oldHoveredWidget
->index());
793 if (newHoveredWidget
) {
794 const int index
= newHoveredWidget
->index();
795 if (m_model
->supportsDropping(index
)) {
796 newHoveredWidget
->setHovered(true);
797 } else if (m_model
->sortRole().isEmpty()) {
798 // The model supports the inserting of items on
799 // the given index as no sort-role has been
800 // specified. Assure that a drag-indicator
801 // is shown by the view.
802 const int dropIndex
= m_view
->showDropIndicator(pos
);
803 Q_UNUSED(dropIndex
); // TODO
805 emit
itemHovered(index
);
807 if (m_autoActivationTimer
->interval() >= 0) {
808 m_autoActivationTimer
->setProperty("index", index
);
809 m_autoActivationTimer
->start();
817 bool KItemListController::dropEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
824 m_autoActivationTimer
->stop();
825 m_view
->setAutoScroll(false);
827 const QPointF pos
= transform
.map(event
->pos());
828 const int index
= m_view
->itemAt(pos
);
829 emit
itemDropEvent(index
, event
);
834 bool KItemListController::hoverEnterEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
841 bool KItemListController::hoverMoveEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
844 if (!m_model
|| !m_view
) {
848 KItemListWidget
* oldHoveredWidget
= hoveredWidget();
849 const QPointF pos
= transform
.map(event
->pos());
850 KItemListWidget
* newHoveredWidget
= widgetForPos(pos
);
852 if (oldHoveredWidget
!= newHoveredWidget
) {
853 if (oldHoveredWidget
) {
854 oldHoveredWidget
->setHovered(false);
855 emit
itemUnhovered(oldHoveredWidget
->index());
858 if (newHoveredWidget
) {
859 newHoveredWidget
->setHovered(true);
860 emit
itemHovered(newHoveredWidget
->index());
867 bool KItemListController::hoverLeaveEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
872 if (!m_model
|| !m_view
) {
876 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
877 if (widget
->isHovered()) {
878 widget
->setHovered(false);
879 emit
itemUnhovered(widget
->index());
885 bool KItemListController::wheelEvent(QGraphicsSceneWheelEvent
* event
, const QTransform
& transform
)
892 bool KItemListController::resizeEvent(QGraphicsSceneResizeEvent
* event
, const QTransform
& transform
)
899 bool KItemListController::processEvent(QEvent
* event
, const QTransform
& transform
)
905 switch (event
->type()) {
906 case QEvent::KeyPress
:
907 return keyPressEvent(static_cast<QKeyEvent
*>(event
));
908 case QEvent::InputMethod
:
909 return inputMethodEvent(static_cast<QInputMethodEvent
*>(event
));
910 case QEvent::GraphicsSceneMousePress
:
911 return mousePressEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
912 case QEvent::GraphicsSceneMouseMove
:
913 return mouseMoveEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
914 case QEvent::GraphicsSceneMouseRelease
:
915 return mouseReleaseEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
916 case QEvent::GraphicsSceneMouseDoubleClick
:
917 return mouseDoubleClickEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
918 case QEvent::GraphicsSceneWheel
:
919 return wheelEvent(static_cast<QGraphicsSceneWheelEvent
*>(event
), QTransform());
920 case QEvent::GraphicsSceneDragEnter
:
921 return dragEnterEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
922 case QEvent::GraphicsSceneDragLeave
:
923 return dragLeaveEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
924 case QEvent::GraphicsSceneDragMove
:
925 return dragMoveEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
926 case QEvent::GraphicsSceneDrop
:
927 return dropEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
928 case QEvent::GraphicsSceneHoverEnter
:
929 return hoverEnterEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
930 case QEvent::GraphicsSceneHoverMove
:
931 return hoverMoveEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
932 case QEvent::GraphicsSceneHoverLeave
:
933 return hoverLeaveEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
934 case QEvent::GraphicsSceneResize
:
935 return resizeEvent(static_cast<QGraphicsSceneResizeEvent
*>(event
), transform
);
943 void KItemListController::slotViewScrollOffsetChanged(qreal current
, qreal previous
)
949 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
950 if (rubberBand
->isActive()) {
951 const qreal diff
= current
- previous
;
952 // TODO: Ideally just QCursor::pos() should be used as
953 // new end-position but it seems there is no easy way
954 // to have something like QWidget::mapFromGlobal() for QGraphicsWidget
955 // (... or I just missed an easy way to do the mapping)
956 QPointF endPos
= rubberBand
->endPosition();
957 if (m_view
->scrollOrientation() == Qt::Vertical
) {
963 rubberBand
->setEndPosition(endPos
);
967 void KItemListController::slotRubberBandChanged()
969 if (!m_view
|| !m_model
|| m_model
->count() <= 0) {
973 const KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
974 const QPointF startPos
= rubberBand
->startPosition();
975 const QPointF endPos
= rubberBand
->endPosition();
976 QRectF rubberBandRect
= QRectF(startPos
, endPos
).normalized();
978 const bool scrollVertical
= (m_view
->scrollOrientation() == Qt::Vertical
);
979 if (scrollVertical
) {
980 rubberBandRect
.translate(0, -m_view
->scrollOffset());
982 rubberBandRect
.translate(-m_view
->scrollOffset(), 0);
985 if (!m_oldSelection
.isEmpty()) {
986 // Clear the old selection that was available before the rubberband has
987 // been activated in case if no Shift- or Control-key are pressed
988 const bool shiftOrControlPressed
= QApplication::keyboardModifiers() & Qt::ShiftModifier
||
989 QApplication::keyboardModifiers() & Qt::ControlModifier
;
990 if (!shiftOrControlPressed
) {
991 m_oldSelection
.clear();
995 QSet
<int> selectedItems
;
997 // Select all visible items that intersect with the rubberband
998 foreach (const KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
999 const int index
= widget
->index();
1001 const QRectF widgetRect
= m_view
->itemRect(index
);
1002 if (widgetRect
.intersects(rubberBandRect
)) {
1003 const QRectF iconRect
= widget
->iconRect().translated(widgetRect
.topLeft());
1004 const QRectF textRect
= widget
->textRect().translated(widgetRect
.topLeft());
1005 if (iconRect
.intersects(rubberBandRect
) || textRect
.intersects(rubberBandRect
)) {
1006 selectedItems
.insert(index
);
1011 // Select all invisible items that intersect with the rubberband. Instead of
1012 // iterating all items only the area which might be touched by the rubberband
1014 const bool increaseIndex
= scrollVertical
?
1015 startPos
.y() > endPos
.y(): startPos
.x() > endPos
.x();
1017 int index
= increaseIndex
? m_view
->lastVisibleIndex() + 1 : m_view
->firstVisibleIndex() - 1;
1018 bool selectionFinished
= false;
1020 const QRectF widgetRect
= m_view
->itemRect(index
);
1021 if (widgetRect
.intersects(rubberBandRect
)) {
1022 selectedItems
.insert(index
);
1025 if (increaseIndex
) {
1027 selectionFinished
= (index
>= m_model
->count()) ||
1028 ( scrollVertical
&& widgetRect
.top() > rubberBandRect
.bottom()) ||
1029 (!scrollVertical
&& widgetRect
.left() > rubberBandRect
.right());
1032 selectionFinished
= (index
< 0) ||
1033 ( scrollVertical
&& widgetRect
.bottom() < rubberBandRect
.top()) ||
1034 (!scrollVertical
&& widgetRect
.right() < rubberBandRect
.left());
1036 } while (!selectionFinished
);
1038 if (QApplication::keyboardModifiers() & Qt::ControlModifier
) {
1039 // If Control is pressed, the selection state of all items in the rubberband is toggled.
1040 // Therefore, the new selection contains:
1041 // 1. All previously selected items which are not inside the rubberband, and
1042 // 2. all items inside the rubberband which have not been selected previously.
1043 m_selectionManager
->setSelectedItems((m_oldSelection
- selectedItems
) + (selectedItems
- m_oldSelection
));
1046 m_selectionManager
->setSelectedItems(selectedItems
+ m_oldSelection
);
1050 void KItemListController::startDragging()
1052 if (!m_view
|| !m_model
) {
1056 const QSet
<int> selectedItems
= m_selectionManager
->selectedItems();
1057 if (selectedItems
.isEmpty()) {
1061 QMimeData
* data
= m_model
->createMimeData(selectedItems
);
1066 // The created drag object will be owned and deleted
1067 // by QApplication::activeWindow().
1068 QDrag
* drag
= new QDrag(QApplication::activeWindow());
1069 drag
->setMimeData(data
);
1071 const QPixmap pixmap
= m_view
->createDragPixmap(selectedItems
);
1072 drag
->setPixmap(pixmap
);
1074 // TODO: The vertical hotspot of -24 should be replaced by the
1075 // height of the QCursor-pixmap.
1076 const QPoint
hotSpot(pixmap
.width() / 2, -24);
1077 drag
->setHotSpot(hotSpot
);
1079 drag
->exec(Qt::MoveAction
| Qt::CopyAction
| Qt::LinkAction
, Qt::CopyAction
);
1082 KItemListWidget
* KItemListController::hoveredWidget() const
1086 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
1087 if (widget
->isHovered()) {
1095 KItemListWidget
* KItemListController::widgetForPos(const QPointF
& pos
) const
1099 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
1100 const QPointF mappedPos
= widget
->mapFromItem(m_view
, pos
);
1102 const bool hovered
= widget
->contains(mappedPos
) &&
1103 !widget
->expansionToggleRect().contains(mappedPos
);
1112 void KItemListController::updateKeyboardAnchor()
1114 const bool validAnchor
= m_keyboardAnchorIndex
>= 0 &&
1115 m_keyboardAnchorIndex
< m_model
->count() &&
1116 keyboardAnchorPos(m_keyboardAnchorIndex
) == m_keyboardAnchorPos
;
1118 const int index
= m_selectionManager
->currentItem();
1119 m_keyboardAnchorIndex
= index
;
1120 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
1124 int KItemListController::nextRowIndex(int index
) const
1126 if (m_keyboardAnchorIndex
< 0) {
1130 const int maxIndex
= m_model
->count() - 1;
1131 if (index
== maxIndex
) {
1135 // Calculate the index of the last column inside the row of the current index
1136 int lastColumnIndex
= index
;
1137 while (keyboardAnchorPos(lastColumnIndex
+ 1) > keyboardAnchorPos(lastColumnIndex
)) {
1139 if (lastColumnIndex
>= maxIndex
) {
1144 // Based on the last column index go to the next row and calculate the nearest index
1145 // that is below the current index
1146 int nextRowIndex
= lastColumnIndex
+ 1;
1147 int searchIndex
= nextRowIndex
;
1148 qreal minDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(nextRowIndex
));
1149 while (searchIndex
< maxIndex
&& keyboardAnchorPos(searchIndex
+ 1) > keyboardAnchorPos(searchIndex
)) {
1151 const qreal searchDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(searchIndex
));
1152 if (searchDiff
< minDiff
) {
1153 minDiff
= searchDiff
;
1154 nextRowIndex
= searchIndex
;
1158 return nextRowIndex
;
1161 int KItemListController::previousRowIndex(int index
) const
1163 if (m_keyboardAnchorIndex
< 0 || index
== 0) {
1167 // Calculate the index of the first column inside the row of the current index
1168 int firstColumnIndex
= index
;
1169 while (keyboardAnchorPos(firstColumnIndex
- 1) < keyboardAnchorPos(firstColumnIndex
)) {
1171 if (firstColumnIndex
<= 0) {
1176 // Based on the first column index go to the previous row and calculate the nearest index
1177 // that is above the current index
1178 int previousRowIndex
= firstColumnIndex
- 1;
1179 int searchIndex
= previousRowIndex
;
1180 qreal minDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(previousRowIndex
));
1181 while (searchIndex
> 0 && keyboardAnchorPos(searchIndex
- 1) < keyboardAnchorPos(searchIndex
)) {
1183 const qreal searchDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(searchIndex
));
1184 if (searchDiff
< minDiff
) {
1185 minDiff
= searchDiff
;
1186 previousRowIndex
= searchIndex
;
1190 return previousRowIndex
;
1193 qreal
KItemListController::keyboardAnchorPos(int index
) const
1195 const QRectF itemRect
= m_view
->itemRect(index
);
1196 if (!itemRect
.isEmpty()) {
1197 return (m_view
->scrollOrientation() == Qt::Vertical
) ? itemRect
.x() : itemRect
.y();
1203 void KItemListController::updateExtendedSelectionRegion()
1206 const bool extend
= (m_selectionBehavior
!= MultiSelection
);
1207 KItemListStyleOption option
= m_view
->styleOption();
1208 if (option
.extendedSelectionRegion
!= extend
) {
1209 option
.extendedSelectionRegion
= extend
;
1210 m_view
->setStyleOption(option
);
1215 #include "kitemlistcontroller.moc"