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
),
50 m_autoActivationBehavior(ActivationAndExpansion
),
53 m_selectionManager(new KItemListSelectionManager(this)),
54 m_keyboardManager(new KItemListKeyboardSearchManager(this)),
57 m_autoActivationTimer(0),
59 m_keyboardAnchorIndex(-1),
60 m_keyboardAnchorPos(0)
62 connect(m_keyboardManager
, SIGNAL(changeCurrentItem(QString
,bool)),
63 this, SLOT(slotChangeCurrentItem(QString
,bool)));
64 connect(m_selectionManager
, SIGNAL(currentChanged(int,int)),
65 m_keyboardManager
, SLOT(slotCurrentChanged(int,int)));
67 m_autoActivationTimer
= new QTimer(this);
68 m_autoActivationTimer
->setSingleShot(true);
69 m_autoActivationTimer
->setInterval(-1);
70 connect(m_autoActivationTimer
, SIGNAL(timeout()), this, SLOT(slotAutoActivationTimeout()));
76 KItemListController::~KItemListController()
85 void KItemListController::setModel(KItemModelBase
* model
)
87 if (m_model
== model
) {
91 KItemModelBase
* oldModel
= m_model
;
93 oldModel
->deleteLater();
98 m_model
->setParent(this);
102 m_view
->setModel(m_model
);
105 m_selectionManager
->setModel(m_model
);
107 emit
modelChanged(m_model
, oldModel
);
110 KItemModelBase
* KItemListController::model() const
115 KItemListSelectionManager
* KItemListController::selectionManager() const
117 return m_selectionManager
;
120 void KItemListController::setView(KItemListView
* view
)
122 if (m_view
== view
) {
126 KItemListView
* oldView
= m_view
;
128 disconnect(oldView
, SIGNAL(scrollOffsetChanged(qreal
,qreal
)), this, SLOT(slotViewScrollOffsetChanged(qreal
,qreal
)));
129 oldView
->deleteLater();
135 m_view
->setParent(this);
136 m_view
->setController(this);
137 m_view
->setModel(m_model
);
138 connect(m_view
, SIGNAL(scrollOffsetChanged(qreal
,qreal
)), this, SLOT(slotViewScrollOffsetChanged(qreal
,qreal
)));
139 updateExtendedSelectionRegion();
142 emit
viewChanged(m_view
, oldView
);
145 KItemListView
* KItemListController::view() const
150 void KItemListController::setSelectionBehavior(SelectionBehavior behavior
)
152 m_selectionBehavior
= behavior
;
153 updateExtendedSelectionRegion();
156 KItemListController::SelectionBehavior
KItemListController::selectionBehavior() const
158 return m_selectionBehavior
;
161 void KItemListController::setAutoActivationBehavior(AutoActivationBehavior behavior
)
163 m_autoActivationBehavior
= behavior
;
166 KItemListController::AutoActivationBehavior
KItemListController::autoActivationBehavior() const
168 return m_autoActivationBehavior
;
171 void KItemListController::setAutoActivationDelay(int delay
)
173 m_autoActivationTimer
->setInterval(delay
);
176 int KItemListController::autoActivationDelay() const
178 return m_autoActivationTimer
->interval();
181 void KItemListController::setSingleClickActivation(bool singleClick
)
183 m_singleClickActivation
= singleClick
;
186 bool KItemListController::singleClickActivation() const
188 return m_singleClickActivation
;
191 bool KItemListController::showEvent(QShowEvent
* event
)
197 bool KItemListController::hideEvent(QHideEvent
* event
)
203 bool KItemListController::keyPressEvent(QKeyEvent
* event
)
205 int index
= m_selectionManager
->currentItem();
206 int key
= event
->key();
208 // Handle the expanding/collapsing of items
209 if (m_view
->supportsItemExpanding() && m_model
->isExpandable(index
)) {
210 if (key
== Qt::Key_Right
) {
211 if (m_model
->setExpanded(index
, true)) {
214 } else if (key
== Qt::Key_Left
) {
215 if (m_model
->setExpanded(index
, false)) {
221 const bool shiftPressed
= event
->modifiers() & Qt::ShiftModifier
;
222 const bool controlPressed
= event
->modifiers() & Qt::ControlModifier
;
223 const bool shiftOrControlPressed
= shiftPressed
|| controlPressed
;
225 const int itemCount
= m_model
->count();
227 // For horizontal scroll orientation, transform
228 // the arrow keys to simplify the event handling.
229 if (m_view
->scrollOrientation() == Qt::Horizontal
) {
231 case Qt::Key_Up
: key
= Qt::Key_Left
; break;
232 case Qt::Key_Down
: key
= Qt::Key_Right
; break;
233 case Qt::Key_Left
: key
= Qt::Key_Up
; break;
234 case Qt::Key_Right
: key
= Qt::Key_Down
; break;
239 const bool selectSingleItem
= m_selectionBehavior
!= NoSelection
&&
241 (key
== Qt::Key_Home
|| key
== Qt::Key_End
||
242 key
== Qt::Key_Up
|| key
== Qt::Key_Down
||
243 key
== Qt::Key_Left
|| key
== Qt::Key_Right
);
244 if (selectSingleItem
) {
245 const int current
= m_selectionManager
->currentItem();
246 m_selectionManager
->setSelected(current
);
253 m_keyboardAnchorIndex
= index
;
254 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
258 index
= itemCount
- 1;
259 m_keyboardAnchorIndex
= index
;
260 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
265 const int expandedParentsCount
= m_model
->expandedParentsCount(index
);
266 if (expandedParentsCount
== 0) {
269 // Go to the parent of the current item.
272 } while (index
> 0 && m_model
->expandedParentsCount(index
) == expandedParentsCount
);
274 m_keyboardAnchorIndex
= index
;
275 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
280 if (index
< itemCount
- 1) {
282 m_keyboardAnchorIndex
= index
;
283 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
288 updateKeyboardAnchor();
289 index
= previousRowIndex(index
);
293 updateKeyboardAnchor();
294 index
= nextRowIndex(index
);
298 if (m_view
->scrollOrientation() == Qt::Horizontal
) {
299 // The new current index should correspond to the first item in the current column.
300 int newIndex
= qMax(index
- 1, 0);
301 while (newIndex
!= index
&& m_view
->itemRect(newIndex
).topLeft().y() < m_view
->itemRect(index
).topLeft().y()) {
303 newIndex
= qMax(index
- 1, 0);
305 m_keyboardAnchorIndex
= index
;
306 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
308 const qreal currentItemBottom
= m_view
->itemRect(index
).bottomLeft().y();
309 const qreal height
= m_view
->geometry().height();
311 // The new current item should be the first item in the current
312 // column whose itemRect's top coordinate is larger than targetY.
313 const qreal targetY
= currentItemBottom
- height
;
315 updateKeyboardAnchor();
316 int newIndex
= previousRowIndex(index
);
319 updateKeyboardAnchor();
320 newIndex
= previousRowIndex(index
);
321 } while (m_view
->itemRect(newIndex
).topLeft().y() > targetY
&& newIndex
!= index
);
325 case Qt::Key_PageDown
:
326 if (m_view
->scrollOrientation() == Qt::Horizontal
) {
327 // The new current index should correspond to the last item in the current column.
328 int newIndex
= qMin(index
+ 1, m_model
->count() - 1);
329 while (newIndex
!= index
&& m_view
->itemRect(newIndex
).topLeft().y() > m_view
->itemRect(index
).topLeft().y()) {
331 newIndex
= qMin(index
+ 1, m_model
->count() - 1);
333 m_keyboardAnchorIndex
= index
;
334 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
336 const qreal currentItemTop
= m_view
->itemRect(index
).topLeft().y();
337 const qreal height
= m_view
->geometry().height();
339 // The new current item should be the last item in the current
340 // column whose itemRect's bottom coordinate is smaller than targetY.
341 const qreal targetY
= currentItemTop
+ height
;
343 updateKeyboardAnchor();
344 int newIndex
= nextRowIndex(index
);
347 updateKeyboardAnchor();
348 newIndex
= nextRowIndex(index
);
349 } while (m_view
->itemRect(newIndex
).bottomLeft().y() < targetY
&& newIndex
!= index
);
354 case Qt::Key_Return
: {
355 const QSet
<int> selectedItems
= m_selectionManager
->selectedItems();
356 if (selectedItems
.count() >= 2) {
357 emit
itemsActivated(selectedItems
);
358 } else if (selectedItems
.count() == 1) {
359 emit
itemActivated(selectedItems
.toList().first());
361 emit
itemActivated(index
);
367 if (m_selectionBehavior
== MultiSelection
) {
368 if (controlPressed
) {
369 m_selectionManager
->endAnchoredSelection();
370 m_selectionManager
->setSelected(index
, 1, KItemListSelectionManager::Toggle
);
371 m_selectionManager
->beginAnchoredSelection(index
);
373 const int current
= m_selectionManager
->currentItem();
374 m_selectionManager
->setSelected(current
);
380 // Emit the signal itemContextMenuRequested() in case if at least one
381 // item is selected. Otherwise the signal viewContextMenuRequested() will be emitted.
382 const QSet
<int> selectedItems
= m_selectionManager
->selectedItems();
384 if (selectedItems
.count() >= 2) {
385 const int currentItemIndex
= m_selectionManager
->currentItem();
386 index
= selectedItems
.contains(currentItemIndex
)
387 ? currentItemIndex
: selectedItems
.toList().first();
388 } else if (selectedItems
.count() == 1) {
389 index
= selectedItems
.toList().first();
393 const QRectF contextRect
= m_view
->itemContextRect(index
);
394 const QPointF
pos(m_view
->scene()->views().first()->mapToGlobal(contextRect
.bottomRight().toPoint()));
395 emit
itemContextMenuRequested(index
, pos
);
397 emit
viewContextMenuRequested(QCursor::pos());
403 if (m_selectionBehavior
!= SingleSelection
) {
404 m_selectionManager
->clearSelection();
406 m_keyboardManager
->cancelSearch();
410 m_keyboardManager
->addKeys(event
->text());
414 if (m_selectionManager
->currentItem() != index
) {
415 switch (m_selectionBehavior
) {
417 m_selectionManager
->setCurrentItem(index
);
420 case SingleSelection
:
421 m_selectionManager
->setCurrentItem(index
);
422 m_selectionManager
->clearSelection();
423 m_selectionManager
->setSelected(index
, 1);
427 if (controlPressed
) {
428 m_selectionManager
->endAnchoredSelection();
431 m_selectionManager
->setCurrentItem(index
);
433 if (!shiftOrControlPressed
) {
434 m_selectionManager
->clearSelection();
435 m_selectionManager
->setSelected(index
, 1);
439 m_selectionManager
->beginAnchoredSelection(index
);
444 m_view
->scrollToItem(index
);
449 void KItemListController::slotChangeCurrentItem(const QString
& text
, bool searchFromNextItem
)
451 if (!m_model
|| m_model
->count() == 0) {
454 const int currentIndex
= m_selectionManager
->currentItem();
456 if (searchFromNextItem
) {
457 index
= m_model
->indexForKeyboardSearch(text
, (currentIndex
+ 1) % m_model
->count());
459 index
= m_model
->indexForKeyboardSearch(text
, currentIndex
);
462 m_selectionManager
->setCurrentItem(index
);
463 m_selectionManager
->clearSelection();
464 m_selectionManager
->setSelected(index
, 1);
465 m_selectionManager
->beginAnchoredSelection(index
);
466 m_view
->scrollToItem(index
);
470 void KItemListController::slotAutoActivationTimeout()
472 if (!m_model
|| !m_view
) {
476 const int index
= m_autoActivationTimer
->property("index").toInt();
477 if (index
< 0 || index
>= m_model
->count()) {
481 if (m_model
->supportsDropping(index
)) {
482 if (m_view
->supportsItemExpanding() && m_model
->isExpandable(index
)) {
483 const bool expanded
= m_model
->isExpanded(index
);
484 m_model
->setExpanded(index
, !expanded
);
485 } else if (m_autoActivationBehavior
!= ExpansionOnly
) {
486 emit
itemActivated(index
);
491 bool KItemListController::inputMethodEvent(QInputMethodEvent
* event
)
497 bool KItemListController::mousePressEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
503 m_pressedMousePos
= transform
.map(event
->pos());
504 m_pressedIndex
= m_view
->itemAt(m_pressedMousePos
);
505 emit
mouseButtonPressed(m_pressedIndex
, event
->buttons());
507 if (m_view
->isAboveExpansionToggle(m_pressedIndex
, m_pressedMousePos
)) {
508 m_selectionManager
->endAnchoredSelection();
509 m_selectionManager
->setCurrentItem(m_pressedIndex
);
510 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
514 m_selectionTogglePressed
= m_view
->isAboveSelectionToggle(m_pressedIndex
, m_pressedMousePos
);
515 if (m_selectionTogglePressed
) {
516 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
517 // The previous anchored selection has been finished already in
518 // KItemListSelectionManager::setSelected(). We can safely change
519 // the current item and start a new anchored selection now.
520 m_selectionManager
->setCurrentItem(m_pressedIndex
);
521 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
525 const bool shiftPressed
= event
->modifiers() & Qt::ShiftModifier
;
526 const bool controlPressed
= event
->modifiers() & Qt::ControlModifier
;
528 // The previous selection is cleared if either
529 // 1. The selection mode is SingleSelection, or
530 // 2. the selection mode is MultiSelection, and *none* of the following conditions are met:
531 // a) Shift or Control are pressed.
532 // b) The clicked item is selected already. In that case, the user might want to:
533 // - start dragging multiple items, or
534 // - open the context menu and perform an action for all selected items.
535 const bool shiftOrControlPressed
= shiftPressed
|| controlPressed
;
536 const bool pressedItemAlreadySelected
= m_pressedIndex
>= 0 && m_selectionManager
->isSelected(m_pressedIndex
);
537 const bool clearSelection
= m_selectionBehavior
== SingleSelection
||
538 (!shiftOrControlPressed
&& !pressedItemAlreadySelected
);
539 if (clearSelection
) {
540 m_selectionManager
->clearSelection();
541 } else if (pressedItemAlreadySelected
&& !shiftOrControlPressed
&& (event
->buttons() & Qt::LeftButton
)) {
542 // The user might want to start dragging multiple items, but if he clicks the item
543 // in order to trigger it instead, the other selected items must be deselected.
544 // However, we do not know yet what the user is going to do.
545 // -> remember that the user pressed an item which had been selected already and
546 // clear the selection in mouseReleaseEvent(), unless the items are dragged.
547 m_clearSelectionIfItemsAreNotDragged
= true;
551 // Finish the anchored selection before the current index is changed
552 m_selectionManager
->endAnchoredSelection();
555 if (m_pressedIndex
>= 0) {
556 m_selectionManager
->setCurrentItem(m_pressedIndex
);
558 switch (m_selectionBehavior
) {
562 case SingleSelection
:
563 m_selectionManager
->setSelected(m_pressedIndex
);
567 if (controlPressed
&& !shiftPressed
) {
568 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
569 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
570 } else if (!shiftPressed
|| !m_selectionManager
->isAnchoredSelectionActive()) {
571 // Select the pressed item and start a new anchored selection
572 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Select
);
573 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
582 if (event
->buttons() & Qt::RightButton
) {
583 emit
itemContextMenuRequested(m_pressedIndex
, event
->screenPos());
589 if (event
->buttons() & Qt::RightButton
) {
590 const QRectF headerBounds
= m_view
->headerBoundaries();
591 if (headerBounds
.contains(event
->pos())) {
592 emit
headerContextMenuRequested(event
->screenPos());
594 emit
viewContextMenuRequested(event
->screenPos());
599 if (m_selectionBehavior
== MultiSelection
) {
600 QPointF startPos
= m_pressedMousePos
;
601 if (m_view
->scrollOrientation() == Qt::Vertical
) {
602 startPos
.ry() += m_view
->scrollOffset();
603 if (m_view
->itemSize().width() < 0) {
604 // Use a special rubberband for views that have only one column and
605 // expand the rubberband to use the whole width of the view.
609 startPos
.rx() += m_view
->scrollOffset();
612 m_oldSelection
= m_selectionManager
->selectedItems();
613 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
614 rubberBand
->setStartPosition(startPos
);
615 rubberBand
->setEndPosition(startPos
);
616 rubberBand
->setActive(true);
617 connect(rubberBand
, SIGNAL(endPositionChanged(QPointF
,QPointF
)), this, SLOT(slotRubberBandChanged()));
618 m_view
->setAutoScroll(true);
624 bool KItemListController::mouseMoveEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
630 if (m_pressedIndex
>= 0) {
631 // Check whether a dragging should be started
632 if (event
->buttons() & Qt::LeftButton
) {
633 const QPointF pos
= transform
.map(event
->pos());
634 if ((pos
- m_pressedMousePos
).manhattanLength() >= QApplication::startDragDistance()) {
635 if (!m_selectionManager
->isSelected(m_pressedIndex
)) {
636 // Always assure that the dragged item gets selected. Usually this is already
637 // done on the mouse-press event, but when using the selection-toggle on a
638 // selected item the dragged item is not selected yet.
639 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
641 // A selected item has been clicked to drag all selected items
642 // -> the selection should not be cleared when the mouse button is released.
643 m_clearSelectionIfItemsAreNotDragged
= false;
650 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
651 if (rubberBand
->isActive()) {
652 QPointF endPos
= transform
.map(event
->pos());
654 // Update the current item.
655 const int newCurrent
= m_view
->itemAt(endPos
);
656 if (newCurrent
>= 0) {
657 // It's expected that the new current index is also the new anchor (bug 163451).
658 m_selectionManager
->endAnchoredSelection();
659 m_selectionManager
->setCurrentItem(newCurrent
);
660 m_selectionManager
->beginAnchoredSelection(newCurrent
);
663 if (m_view
->scrollOrientation() == Qt::Vertical
) {
664 endPos
.ry() += m_view
->scrollOffset();
665 if (m_view
->itemSize().width() < 0) {
666 // Use a special rubberband for views that have only one column and
667 // expand the rubberband to use the whole width of the view.
668 endPos
.setX(m_view
->size().width());
671 endPos
.rx() += m_view
->scrollOffset();
673 rubberBand
->setEndPosition(endPos
);
680 bool KItemListController::mouseReleaseEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
686 emit
mouseButtonReleased(m_pressedIndex
, event
->buttons());
688 const bool isAboveSelectionToggle
= m_view
->isAboveSelectionToggle(m_pressedIndex
, m_pressedMousePos
);
689 if (isAboveSelectionToggle
) {
690 m_selectionTogglePressed
= false;
694 if (!isAboveSelectionToggle
&& m_selectionTogglePressed
) {
695 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
696 m_selectionTogglePressed
= false;
700 const bool shiftOrControlPressed
= event
->modifiers() & Qt::ShiftModifier
||
701 event
->modifiers() & Qt::ControlModifier
;
703 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
704 if (rubberBand
->isActive()) {
705 disconnect(rubberBand
, SIGNAL(endPositionChanged(QPointF
,QPointF
)), this, SLOT(slotRubberBandChanged()));
706 rubberBand
->setActive(false);
707 m_oldSelection
.clear();
708 m_view
->setAutoScroll(false);
711 const QPointF pos
= transform
.map(event
->pos());
712 const int index
= m_view
->itemAt(pos
);
714 if (index
>= 0 && index
== m_pressedIndex
) {
715 // The release event is done above the same item as the press event
717 if (m_clearSelectionIfItemsAreNotDragged
) {
718 // A selected item has been clicked, but no drag operation has been started
719 // -> clear the rest of the selection.
720 m_selectionManager
->clearSelection();
721 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Select
);
722 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
725 if (event
->button() & Qt::LeftButton
) {
726 bool emitItemActivated
= true;
727 if (m_view
->isAboveExpansionToggle(index
, pos
)) {
728 const bool expanded
= m_model
->isExpanded(index
);
729 m_model
->setExpanded(index
, !expanded
);
731 emit
itemExpansionToggleClicked(index
);
732 emitItemActivated
= false;
733 } else if (shiftOrControlPressed
) {
734 // The mouse click should only update the selection, not trigger the item
735 emitItemActivated
= false;
736 } else if (!m_singleClickActivation
) {
737 emitItemActivated
= false;
739 if (emitItemActivated
) {
740 emit
itemActivated(index
);
742 } else if (event
->button() & Qt::MidButton
) {
743 emit
itemMiddleClicked(index
);
747 m_pressedMousePos
= QPointF();
749 m_clearSelectionIfItemsAreNotDragged
= false;
753 bool KItemListController::mouseDoubleClickEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
755 const QPointF pos
= transform
.map(event
->pos());
756 const int index
= m_view
->itemAt(pos
);
758 bool emitItemActivated
= !m_singleClickActivation
&&
759 (event
->button() & Qt::LeftButton
) &&
760 index
>= 0 && index
< m_model
->count();
761 if (emitItemActivated
) {
762 emit
itemActivated(index
);
767 bool KItemListController::dragEnterEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
774 bool KItemListController::dragLeaveEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
779 m_view
->setAutoScroll(false);
780 m_view
->hideDropIndicator();
782 KItemListWidget
* widget
= hoveredWidget();
784 widget
->setHovered(false);
785 emit
itemUnhovered(widget
->index());
790 bool KItemListController::dragMoveEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
792 if (!m_model
|| !m_view
) {
796 event
->acceptProposedAction();
798 KItemListWidget
* oldHoveredWidget
= hoveredWidget();
800 const QPointF pos
= transform
.map(event
->pos());
801 KItemListWidget
* newHoveredWidget
= widgetForPos(pos
);
803 if (oldHoveredWidget
!= newHoveredWidget
) {
804 m_autoActivationTimer
->stop();
806 if (oldHoveredWidget
) {
807 oldHoveredWidget
->setHovered(false);
808 emit
itemUnhovered(oldHoveredWidget
->index());
811 if (newHoveredWidget
) {
812 bool droppingBetweenItems
= false;
813 if (m_model
->sortRole().isEmpty()) {
814 // The model supports inserting items between other items.
815 droppingBetweenItems
= (m_view
->showDropIndicator(pos
) >= 0);
818 const int index
= newHoveredWidget
->index();
819 if (!droppingBetweenItems
&& m_model
->supportsDropping(index
)) {
820 // Something has been dragged on an item.
821 m_view
->hideDropIndicator();
822 newHoveredWidget
->setHovered(true);
823 emit
itemHovered(index
);
825 if (m_autoActivationTimer
->interval() >= 0) {
826 m_autoActivationTimer
->setProperty("index", index
);
827 m_autoActivationTimer
->start();
836 bool KItemListController::dropEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
842 m_autoActivationTimer
->stop();
843 m_view
->setAutoScroll(false);
845 const QPointF pos
= transform
.map(event
->pos());
847 int dropAboveIndex
= -1;
848 if (m_model
->sortRole().isEmpty()) {
849 // The model supports inserting of items between other items.
850 dropAboveIndex
= m_view
->showDropIndicator(pos
);
853 if (dropAboveIndex
>= 0) {
854 // Something has been dropped between two items.
855 m_view
->hideDropIndicator();
856 emit
aboveItemDropEvent(dropAboveIndex
, event
);
858 // Something has been dropped on an item or on an empty part of the view.
859 emit
itemDropEvent(m_view
->itemAt(pos
), event
);
865 bool KItemListController::hoverEnterEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
872 bool KItemListController::hoverMoveEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
875 if (!m_model
|| !m_view
) {
879 KItemListWidget
* oldHoveredWidget
= hoveredWidget();
880 const QPointF pos
= transform
.map(event
->pos());
881 KItemListWidget
* newHoveredWidget
= widgetForPos(pos
);
883 if (oldHoveredWidget
!= newHoveredWidget
) {
884 if (oldHoveredWidget
) {
885 oldHoveredWidget
->setHovered(false);
886 emit
itemUnhovered(oldHoveredWidget
->index());
889 if (newHoveredWidget
) {
890 newHoveredWidget
->setHovered(true);
891 emit
itemHovered(newHoveredWidget
->index());
898 bool KItemListController::hoverLeaveEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
903 if (!m_model
|| !m_view
) {
907 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
908 if (widget
->isHovered()) {
909 widget
->setHovered(false);
910 emit
itemUnhovered(widget
->index());
916 bool KItemListController::wheelEvent(QGraphicsSceneWheelEvent
* event
, const QTransform
& transform
)
923 bool KItemListController::resizeEvent(QGraphicsSceneResizeEvent
* event
, const QTransform
& transform
)
930 bool KItemListController::processEvent(QEvent
* event
, const QTransform
& transform
)
936 switch (event
->type()) {
937 case QEvent::KeyPress
:
938 return keyPressEvent(static_cast<QKeyEvent
*>(event
));
939 case QEvent::InputMethod
:
940 return inputMethodEvent(static_cast<QInputMethodEvent
*>(event
));
941 case QEvent::GraphicsSceneMousePress
:
942 return mousePressEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
943 case QEvent::GraphicsSceneMouseMove
:
944 return mouseMoveEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
945 case QEvent::GraphicsSceneMouseRelease
:
946 return mouseReleaseEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
947 case QEvent::GraphicsSceneMouseDoubleClick
:
948 return mouseDoubleClickEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
949 case QEvent::GraphicsSceneWheel
:
950 return wheelEvent(static_cast<QGraphicsSceneWheelEvent
*>(event
), QTransform());
951 case QEvent::GraphicsSceneDragEnter
:
952 return dragEnterEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
953 case QEvent::GraphicsSceneDragLeave
:
954 return dragLeaveEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
955 case QEvent::GraphicsSceneDragMove
:
956 return dragMoveEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
957 case QEvent::GraphicsSceneDrop
:
958 return dropEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
959 case QEvent::GraphicsSceneHoverEnter
:
960 return hoverEnterEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
961 case QEvent::GraphicsSceneHoverMove
:
962 return hoverMoveEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
963 case QEvent::GraphicsSceneHoverLeave
:
964 return hoverLeaveEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
965 case QEvent::GraphicsSceneResize
:
966 return resizeEvent(static_cast<QGraphicsSceneResizeEvent
*>(event
), transform
);
974 void KItemListController::slotViewScrollOffsetChanged(qreal current
, qreal previous
)
980 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
981 if (rubberBand
->isActive()) {
982 const qreal diff
= current
- previous
;
983 // TODO: Ideally just QCursor::pos() should be used as
984 // new end-position but it seems there is no easy way
985 // to have something like QWidget::mapFromGlobal() for QGraphicsWidget
986 // (... or I just missed an easy way to do the mapping)
987 QPointF endPos
= rubberBand
->endPosition();
988 if (m_view
->scrollOrientation() == Qt::Vertical
) {
994 rubberBand
->setEndPosition(endPos
);
998 void KItemListController::slotRubberBandChanged()
1000 if (!m_view
|| !m_model
|| m_model
->count() <= 0) {
1004 const KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
1005 const QPointF startPos
= rubberBand
->startPosition();
1006 const QPointF endPos
= rubberBand
->endPosition();
1007 QRectF rubberBandRect
= QRectF(startPos
, endPos
).normalized();
1009 const bool scrollVertical
= (m_view
->scrollOrientation() == Qt::Vertical
);
1010 if (scrollVertical
) {
1011 rubberBandRect
.translate(0, -m_view
->scrollOffset());
1013 rubberBandRect
.translate(-m_view
->scrollOffset(), 0);
1016 if (!m_oldSelection
.isEmpty()) {
1017 // Clear the old selection that was available before the rubberband has
1018 // been activated in case if no Shift- or Control-key are pressed
1019 const bool shiftOrControlPressed
= QApplication::keyboardModifiers() & Qt::ShiftModifier
||
1020 QApplication::keyboardModifiers() & Qt::ControlModifier
;
1021 if (!shiftOrControlPressed
) {
1022 m_oldSelection
.clear();
1026 QSet
<int> selectedItems
;
1028 // Select all visible items that intersect with the rubberband
1029 foreach (const KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
1030 const int index
= widget
->index();
1032 const QRectF widgetRect
= m_view
->itemRect(index
);
1033 if (widgetRect
.intersects(rubberBandRect
)) {
1034 const QRectF iconRect
= widget
->iconRect().translated(widgetRect
.topLeft());
1035 const QRectF textRect
= widget
->textRect().translated(widgetRect
.topLeft());
1036 if (iconRect
.intersects(rubberBandRect
) || textRect
.intersects(rubberBandRect
)) {
1037 selectedItems
.insert(index
);
1042 // Select all invisible items that intersect with the rubberband. Instead of
1043 // iterating all items only the area which might be touched by the rubberband
1045 const bool increaseIndex
= scrollVertical
?
1046 startPos
.y() > endPos
.y(): startPos
.x() > endPos
.x();
1048 int index
= increaseIndex
? m_view
->lastVisibleIndex() + 1 : m_view
->firstVisibleIndex() - 1;
1049 bool selectionFinished
= false;
1051 const QRectF widgetRect
= m_view
->itemRect(index
);
1052 if (widgetRect
.intersects(rubberBandRect
)) {
1053 selectedItems
.insert(index
);
1056 if (increaseIndex
) {
1058 selectionFinished
= (index
>= m_model
->count()) ||
1059 ( scrollVertical
&& widgetRect
.top() > rubberBandRect
.bottom()) ||
1060 (!scrollVertical
&& widgetRect
.left() > rubberBandRect
.right());
1063 selectionFinished
= (index
< 0) ||
1064 ( scrollVertical
&& widgetRect
.bottom() < rubberBandRect
.top()) ||
1065 (!scrollVertical
&& widgetRect
.right() < rubberBandRect
.left());
1067 } while (!selectionFinished
);
1069 if (QApplication::keyboardModifiers() & Qt::ControlModifier
) {
1070 // If Control is pressed, the selection state of all items in the rubberband is toggled.
1071 // Therefore, the new selection contains:
1072 // 1. All previously selected items which are not inside the rubberband, and
1073 // 2. all items inside the rubberband which have not been selected previously.
1074 m_selectionManager
->setSelectedItems((m_oldSelection
- selectedItems
) + (selectedItems
- m_oldSelection
));
1077 m_selectionManager
->setSelectedItems(selectedItems
+ m_oldSelection
);
1081 void KItemListController::startDragging()
1083 if (!m_view
|| !m_model
) {
1087 const QSet
<int> selectedItems
= m_selectionManager
->selectedItems();
1088 if (selectedItems
.isEmpty()) {
1092 QMimeData
* data
= m_model
->createMimeData(selectedItems
);
1097 // The created drag object will be owned and deleted
1098 // by QApplication::activeWindow().
1099 QDrag
* drag
= new QDrag(QApplication::activeWindow());
1100 drag
->setMimeData(data
);
1102 const QPixmap pixmap
= m_view
->createDragPixmap(selectedItems
);
1103 drag
->setPixmap(pixmap
);
1105 const QPoint
hotSpot(pixmap
.width() / 2, 0);
1106 drag
->setHotSpot(hotSpot
);
1108 drag
->exec(Qt::MoveAction
| Qt::CopyAction
| Qt::LinkAction
, Qt::CopyAction
);
1111 KItemListWidget
* KItemListController::hoveredWidget() const
1115 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
1116 if (widget
->isHovered()) {
1124 KItemListWidget
* KItemListController::widgetForPos(const QPointF
& pos
) const
1128 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
1129 const QPointF mappedPos
= widget
->mapFromItem(m_view
, pos
);
1131 const bool hovered
= widget
->contains(mappedPos
) &&
1132 !widget
->expansionToggleRect().contains(mappedPos
);
1141 void KItemListController::updateKeyboardAnchor()
1143 const bool validAnchor
= m_keyboardAnchorIndex
>= 0 &&
1144 m_keyboardAnchorIndex
< m_model
->count() &&
1145 keyboardAnchorPos(m_keyboardAnchorIndex
) == m_keyboardAnchorPos
;
1147 const int index
= m_selectionManager
->currentItem();
1148 m_keyboardAnchorIndex
= index
;
1149 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
1153 int KItemListController::nextRowIndex(int index
) const
1155 if (m_keyboardAnchorIndex
< 0) {
1159 const int maxIndex
= m_model
->count() - 1;
1160 if (index
== maxIndex
) {
1164 // Calculate the index of the last column inside the row of the current index
1165 int lastColumnIndex
= index
;
1166 while (keyboardAnchorPos(lastColumnIndex
+ 1) > keyboardAnchorPos(lastColumnIndex
)) {
1168 if (lastColumnIndex
>= maxIndex
) {
1173 // Based on the last column index go to the next row and calculate the nearest index
1174 // that is below the current index
1175 int nextRowIndex
= lastColumnIndex
+ 1;
1176 int searchIndex
= nextRowIndex
;
1177 qreal minDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(nextRowIndex
));
1178 while (searchIndex
< maxIndex
&& keyboardAnchorPos(searchIndex
+ 1) > keyboardAnchorPos(searchIndex
)) {
1180 const qreal searchDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(searchIndex
));
1181 if (searchDiff
< minDiff
) {
1182 minDiff
= searchDiff
;
1183 nextRowIndex
= searchIndex
;
1187 return nextRowIndex
;
1190 int KItemListController::previousRowIndex(int index
) const
1192 if (m_keyboardAnchorIndex
< 0 || index
== 0) {
1196 // Calculate the index of the first column inside the row of the current index
1197 int firstColumnIndex
= index
;
1198 while (keyboardAnchorPos(firstColumnIndex
- 1) < keyboardAnchorPos(firstColumnIndex
)) {
1200 if (firstColumnIndex
<= 0) {
1205 // Based on the first column index go to the previous row and calculate the nearest index
1206 // that is above the current index
1207 int previousRowIndex
= firstColumnIndex
- 1;
1208 int searchIndex
= previousRowIndex
;
1209 qreal minDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(previousRowIndex
));
1210 while (searchIndex
> 0 && keyboardAnchorPos(searchIndex
- 1) < keyboardAnchorPos(searchIndex
)) {
1212 const qreal searchDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(searchIndex
));
1213 if (searchDiff
< minDiff
) {
1214 minDiff
= searchDiff
;
1215 previousRowIndex
= searchIndex
;
1219 return previousRowIndex
;
1222 qreal
KItemListController::keyboardAnchorPos(int index
) const
1224 const QRectF itemRect
= m_view
->itemRect(index
);
1225 if (!itemRect
.isEmpty()) {
1226 return (m_view
->scrollOrientation() == Qt::Vertical
) ? itemRect
.x() : itemRect
.y();
1232 void KItemListController::updateExtendedSelectionRegion()
1235 const bool extend
= (m_selectionBehavior
!= MultiSelection
);
1236 KItemListStyleOption option
= m_view
->styleOption();
1237 if (option
.extendedSelectionRegion
!= extend
) {
1238 option
.extendedSelectionRegion
= extend
;
1239 m_view
->setStyleOption(option
);
1244 #include "kitemlistcontroller.moc"