1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
4 * Based on the Itemviews NG project from Trolltech Labs: *
5 * http://qt.gitorious.org/qt-labs/itemviews-ng *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program; if not, write to the *
19 * Free Software Foundation, Inc., *
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
21 ***************************************************************************/
23 #include "kitemlistcontroller.h"
25 #include "kitemlistview.h"
26 #include "kitemlistrubberband_p.h"
27 #include "kitemlistselectionmanager.h"
28 #include "kitemlistkeyboardsearchmanager_p.h"
30 #include <QApplication>
33 #include <QGraphicsScene>
34 #include <QGraphicsSceneEvent>
35 #include <QGraphicsView>
39 #include <KGlobalSettings>
42 KItemListController::KItemListController(QObject
* parent
) :
44 m_singleClickActivation(KGlobalSettings::singleClick()),
45 m_selectionTogglePressed(false),
46 m_clearSelectionIfItemsAreNotDragged(false),
47 m_selectionBehavior(NoSelection
),
50 m_selectionManager(new KItemListSelectionManager(this)),
51 m_keyboardManager(new KItemListKeyboardSearchManager(this)),
54 m_autoActivationTimer(0),
56 m_keyboardAnchorIndex(-1),
57 m_keyboardAnchorPos(0)
59 connect(m_keyboardManager
, SIGNAL(changeCurrentItem(QString
,bool)),
60 this, SLOT(slotChangeCurrentItem(QString
,bool)));
62 m_autoActivationTimer
= new QTimer(this);
63 m_autoActivationTimer
->setSingleShot(true);
64 m_autoActivationTimer
->setInterval(-1);
65 connect(m_autoActivationTimer
, SIGNAL(timeout()), this, SLOT(slotAutoActivationTimeout()));
68 KItemListController::~KItemListController()
72 void KItemListController::setModel(KItemModelBase
* model
)
74 if (m_model
== model
) {
78 KItemModelBase
* oldModel
= m_model
;
82 m_view
->setModel(m_model
);
85 m_selectionManager
->setModel(m_model
);
87 emit
modelChanged(m_model
, oldModel
);
90 KItemModelBase
* KItemListController::model() const
95 KItemListSelectionManager
* KItemListController::selectionManager() const
97 return m_selectionManager
;
100 void KItemListController::setView(KItemListView
* view
)
102 if (m_view
== view
) {
106 KItemListView
* oldView
= m_view
;
108 disconnect(oldView
, SIGNAL(scrollOffsetChanged(qreal
,qreal
)), this, SLOT(slotViewScrollOffsetChanged(qreal
,qreal
)));
114 m_view
->setController(this);
115 m_view
->setModel(m_model
);
116 connect(m_view
, SIGNAL(scrollOffsetChanged(qreal
,qreal
)), this, SLOT(slotViewScrollOffsetChanged(qreal
,qreal
)));
119 emit
viewChanged(m_view
, oldView
);
122 KItemListView
* KItemListController::view() const
127 void KItemListController::setSelectionBehavior(SelectionBehavior behavior
)
129 m_selectionBehavior
= behavior
;
132 KItemListController::SelectionBehavior
KItemListController::selectionBehavior() const
134 return m_selectionBehavior
;
137 void KItemListController::setAutoActivationDelay(int delay
)
139 m_autoActivationTimer
->setInterval(delay
);
142 int KItemListController::autoActivationDelay() const
144 return m_autoActivationTimer
->interval();
147 void KItemListController::setSingleClickActivation(bool singleClick
)
149 m_singleClickActivation
= singleClick
;
152 bool KItemListController::singleClickActivation() const
154 return m_singleClickActivation
;
157 bool KItemListController::showEvent(QShowEvent
* event
)
163 bool KItemListController::hideEvent(QHideEvent
* event
)
169 bool KItemListController::keyPressEvent(QKeyEvent
* event
)
171 int index
= m_selectionManager
->currentItem();
172 int key
= event
->key();
174 // Handle the expanding/collapsing of items
175 if (m_view
->supportsItemExpanding() && m_model
->isExpandable(index
)) {
176 if (key
== Qt::Key_Right
) {
177 if (m_model
->setExpanded(index
, true)) {
180 } else if (key
== Qt::Key_Left
) {
181 if (m_model
->setExpanded(index
, false)) {
187 const bool shiftPressed
= event
->modifiers() & Qt::ShiftModifier
;
188 const bool controlPressed
= event
->modifiers() & Qt::ControlModifier
;
189 const bool shiftOrControlPressed
= shiftPressed
|| controlPressed
;
191 const int itemCount
= m_model
->count();
193 // For horizontal scroll orientation, transform
194 // the arrow keys to simplify the event handling.
195 if (m_view
->scrollOrientation() == Qt::Horizontal
) {
197 case Qt::Key_Up
: key
= Qt::Key_Left
; break;
198 case Qt::Key_Down
: key
= Qt::Key_Right
; break;
199 case Qt::Key_Left
: key
= Qt::Key_Up
; break;
200 case Qt::Key_Right
: key
= Qt::Key_Down
; break;
205 const bool selectSingleItem
= itemCount
== 1 &&
206 (key
== Qt::Key_Home
|| key
== Qt::Key_End
||
207 key
== Qt::Key_Up
|| key
== Qt::Key_Down
||
208 key
== Qt::Key_Left
|| key
== Qt::Key_Right
);
209 if (selectSingleItem
) {
210 const int current
= m_selectionManager
->currentItem();
211 m_selectionManager
->setSelected(current
);
221 index
= itemCount
- 1;
227 m_keyboardAnchorIndex
= index
;
228 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
233 if (index
< itemCount
- 1) {
235 m_keyboardAnchorIndex
= index
;
236 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
241 updateKeyboardAnchor();
242 index
= previousRowIndex();
246 updateKeyboardAnchor();
247 index
= nextRowIndex();
251 case Qt::Key_Return
: {
252 const QSet
<int> selectedItems
= m_selectionManager
->selectedItems();
253 if (selectedItems
.count() >= 2) {
254 emit
itemsActivated(selectedItems
);
255 } else if (selectedItems
.count() == 1) {
256 emit
itemActivated(selectedItems
.toList().first());
258 emit
itemActivated(index
);
264 if (controlPressed
) {
265 m_selectionManager
->endAnchoredSelection();
266 m_selectionManager
->setSelected(index
, 1, KItemListSelectionManager::Toggle
);
267 m_selectionManager
->beginAnchoredSelection(index
);
269 const int current
= m_selectionManager
->currentItem();
270 m_selectionManager
->setSelected(current
);
275 // Emit the signal itemContextMenuRequested() in case if at least one
276 // item is selected. Otherwise the signal viewContextMenuRequested() will be emitted.
277 const QSet
<int> selectedItems
= m_selectionManager
->selectedItems();
279 if (selectedItems
.count() >= 2) {
280 const int currentItemIndex
= m_selectionManager
->currentItem();
281 index
= selectedItems
.contains(currentItemIndex
)
282 ? currentItemIndex
: selectedItems
.toList().first();
283 } else if (selectedItems
.count() == 1) {
284 index
= selectedItems
.toList().first();
288 const QRectF contextRect
= m_view
->itemContextRect(index
);
289 const QPointF
pos(m_view
->scene()->views().first()->mapToGlobal(contextRect
.bottomRight().toPoint()));
290 emit
itemContextMenuRequested(index
, pos
);
292 emit
viewContextMenuRequested(QCursor::pos());
298 m_keyboardManager
->addKeys(event
->text());
302 if (m_selectionManager
->currentItem() != index
) {
303 if (controlPressed
) {
304 m_selectionManager
->endAnchoredSelection();
307 m_selectionManager
->setCurrentItem(index
);
309 if (!shiftOrControlPressed
|| m_selectionBehavior
== SingleSelection
) {
310 m_selectionManager
->clearSelection();
311 m_selectionManager
->setSelected(index
, 1);
315 m_selectionManager
->beginAnchoredSelection(index
);
318 m_view
->scrollToItem(index
);
323 void KItemListController::slotChangeCurrentItem(const QString
& text
, bool searchFromNextItem
)
325 if (!m_model
|| m_model
->count() == 0) {
328 const int currentIndex
= m_selectionManager
->currentItem();
330 if (searchFromNextItem
) {
331 index
= m_model
->indexForKeyboardSearch(text
, (currentIndex
+ 1) % m_model
->count());
333 index
= m_model
->indexForKeyboardSearch(text
, currentIndex
);
336 m_selectionManager
->setCurrentItem(index
);
337 m_selectionManager
->clearSelection();
338 m_selectionManager
->setSelected(index
, 1);
339 m_selectionManager
->beginAnchoredSelection(index
);
340 m_view
->scrollToItem(index
);
344 void KItemListController::slotAutoActivationTimeout()
346 if (!m_model
|| !m_view
) {
350 const int index
= m_autoActivationTimer
->property("index").toInt();
351 if (index
< 0 || index
>= m_model
->count()) {
355 if (m_model
->supportsDropping(index
)) {
356 if (m_view
->supportsItemExpanding() && m_model
->isExpandable(index
)) {
357 const bool expanded
= m_model
->isExpanded(index
);
358 m_model
->setExpanded(index
, !expanded
);
360 emit
itemActivated(index
);
365 bool KItemListController::inputMethodEvent(QInputMethodEvent
* event
)
371 bool KItemListController::mousePressEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
377 m_pressedMousePos
= transform
.map(event
->pos());
378 m_pressedIndex
= m_view
->itemAt(m_pressedMousePos
);
379 if (m_pressedIndex
>= 0) {
380 emit
itemPressed(m_pressedIndex
, event
->button());
383 if (m_view
->isAboveExpansionToggle(m_pressedIndex
, m_pressedMousePos
)) {
384 m_selectionManager
->endAnchoredSelection();
385 m_selectionManager
->setCurrentItem(m_pressedIndex
);
386 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
390 m_selectionTogglePressed
= m_view
->isAboveSelectionToggle(m_pressedIndex
, m_pressedMousePos
);
391 if (m_selectionTogglePressed
) {
392 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
393 // The previous anchored selection has been finished already in
394 // KItemListSelectionManager::setSelected(). We can safely change
395 // the current item and start a new anchored selection now.
396 m_selectionManager
->setCurrentItem(m_pressedIndex
);
397 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
401 const bool shiftPressed
= event
->modifiers() & Qt::ShiftModifier
;
402 const bool controlPressed
= event
->modifiers() & Qt::ControlModifier
;
404 // The previous selection is cleared if either
405 // 1. The selection mode is SingleSelection, or
406 // 2. the selection mode is MultiSelection, and *none* of the following conditions are met:
407 // a) Shift or Control are pressed.
408 // b) The clicked item is selected already. In that case, the user might want to:
409 // - start dragging multiple items, or
410 // - open the context menu and perform an action for all selected items.
411 const bool shiftOrControlPressed
= shiftPressed
|| controlPressed
;
412 const bool pressedItemAlreadySelected
= m_pressedIndex
>= 0 && m_selectionManager
->isSelected(m_pressedIndex
);
413 const bool clearSelection
= m_selectionBehavior
== SingleSelection
||
414 (!shiftOrControlPressed
&& !pressedItemAlreadySelected
);
415 if (clearSelection
) {
416 m_selectionManager
->clearSelection();
417 } else if (pressedItemAlreadySelected
&& (event
->buttons() & Qt::LeftButton
)) {
418 // The user might want to start dragging multiple items, but if he clicks the item
419 // in order to trigger it instead, the other selected items must be deselected.
420 // However, we do not know yet what the user is going to do.
421 // -> remember that the user pressed an item which had been selected already and
422 // clear the selection in mouseReleaseEvent(), unless the items are dragged.
423 m_clearSelectionIfItemsAreNotDragged
= true;
427 // Finish the anchored selection before the current index is changed
428 m_selectionManager
->endAnchoredSelection();
431 if (m_pressedIndex
>= 0) {
432 m_selectionManager
->setCurrentItem(m_pressedIndex
);
434 switch (m_selectionBehavior
) {
438 case SingleSelection
:
439 m_selectionManager
->setSelected(m_pressedIndex
);
443 if (controlPressed
) {
444 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
445 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
446 } else if (!shiftPressed
|| !m_selectionManager
->isAnchoredSelectionActive()) {
447 // Select the pressed item and start a new anchored selection
448 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Select
);
449 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
458 if (event
->buttons() & Qt::RightButton
) {
459 emit
itemContextMenuRequested(m_pressedIndex
, event
->screenPos());
465 if (event
->buttons() & Qt::RightButton
) {
466 const QRectF headerBounds
= m_view
->headerBoundaries();
467 if (headerBounds
.contains(event
->pos())) {
468 emit
headerContextMenuRequested(event
->screenPos());
470 emit
viewContextMenuRequested(event
->screenPos());
475 if (m_selectionBehavior
== MultiSelection
) {
476 QPointF startPos
= m_pressedMousePos
;
477 if (m_view
->scrollOrientation() == Qt::Vertical
) {
478 startPos
.ry() += m_view
->scrollOffset();
479 if (m_view
->itemSize().width() < 0) {
480 // Use a special rubberband for views that have only one column and
481 // expand the rubberband to use the whole width of the view.
485 startPos
.rx() += m_view
->scrollOffset();
488 m_oldSelection
= m_selectionManager
->selectedItems();
489 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
490 rubberBand
->setStartPosition(startPos
);
491 rubberBand
->setEndPosition(startPos
);
492 rubberBand
->setActive(true);
493 connect(rubberBand
, SIGNAL(endPositionChanged(QPointF
,QPointF
)), this, SLOT(slotRubberBandChanged()));
494 m_view
->setAutoScroll(true);
500 bool KItemListController::mouseMoveEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
506 if (m_pressedIndex
>= 0) {
507 // Check whether a dragging should be started
508 if (event
->buttons() & Qt::LeftButton
) {
509 const QPointF pos
= transform
.map(event
->pos());
510 if ((pos
- m_pressedMousePos
).manhattanLength() >= QApplication::startDragDistance()) {
511 if (!m_selectionManager
->isSelected(m_pressedIndex
)) {
512 // Always assure that the dragged item gets selected. Usually this is already
513 // done on the mouse-press event, but when using the selection-toggle on a
514 // selected item the dragged item is not selected yet.
515 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
517 // A selected item has been clicked to drag all selected items
518 // -> the selection should not be cleared when the mouse button is released.
519 m_clearSelectionIfItemsAreNotDragged
= false;
526 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
527 if (rubberBand
->isActive()) {
528 QPointF endPos
= transform
.map(event
->pos());
530 // Update the current item.
531 const int newCurrent
= m_view
->itemAt(endPos
);
532 if (newCurrent
>= 0) {
533 // It's expected that the new current index is also the new anchor (bug 163451).
534 m_selectionManager
->endAnchoredSelection();
535 m_selectionManager
->setCurrentItem(newCurrent
);
536 m_selectionManager
->beginAnchoredSelection(newCurrent
);
539 if (m_view
->scrollOrientation() == Qt::Vertical
) {
540 endPos
.ry() += m_view
->scrollOffset();
541 if (m_view
->itemSize().width() < 0) {
542 // Use a special rubberband for views that have only one column and
543 // expand the rubberband to use the whole width of the view.
544 endPos
.setX(m_view
->size().width());
547 endPos
.rx() += m_view
->scrollOffset();
549 rubberBand
->setEndPosition(endPos
);
556 bool KItemListController::mouseReleaseEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
562 if (m_pressedIndex
>= 0) {
563 emit
itemReleased(m_pressedIndex
, event
->button());
566 const bool isAboveSelectionToggle
= m_view
->isAboveSelectionToggle(m_pressedIndex
, m_pressedMousePos
);
567 if (isAboveSelectionToggle
) {
568 m_selectionTogglePressed
= false;
572 if (!isAboveSelectionToggle
&& m_selectionTogglePressed
) {
573 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
574 m_selectionTogglePressed
= false;
578 const bool shiftOrControlPressed
= event
->modifiers() & Qt::ShiftModifier
||
579 event
->modifiers() & Qt::ControlModifier
;
581 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
582 if (rubberBand
->isActive()) {
583 disconnect(rubberBand
, SIGNAL(endPositionChanged(QPointF
,QPointF
)), this, SLOT(slotRubberBandChanged()));
584 rubberBand
->setActive(false);
585 m_oldSelection
.clear();
586 m_view
->setAutoScroll(false);
589 const QPointF pos
= transform
.map(event
->pos());
590 const int index
= m_view
->itemAt(pos
);
592 if (index
>= 0 && index
== m_pressedIndex
) {
593 // The release event is done above the same item as the press event
595 if (m_clearSelectionIfItemsAreNotDragged
) {
596 // A selected item has been clicked, but no drag operation has been started
597 // -> clear the rest of the selection.
598 m_selectionManager
->clearSelection();
599 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Select
);
600 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
603 if (event
->button() & Qt::LeftButton
) {
604 bool emitItemActivated
= true;
605 if (m_view
->isAboveExpansionToggle(index
, pos
)) {
606 const bool expanded
= m_model
->isExpanded(index
);
607 m_model
->setExpanded(index
, !expanded
);
609 emit
itemExpansionToggleClicked(index
);
610 emitItemActivated
= false;
611 } else if (shiftOrControlPressed
) {
612 // The mouse click should only update the selection, not trigger the item
613 emitItemActivated
= false;
614 } else if (!m_singleClickActivation
) {
615 emitItemActivated
= false;
617 if (emitItemActivated
) {
618 emit
itemActivated(index
);
620 } else if (event
->button() & Qt::MidButton
) {
621 emit
itemMiddleClicked(index
);
625 m_pressedMousePos
= QPointF();
627 m_clearSelectionIfItemsAreNotDragged
= false;
631 bool KItemListController::mouseDoubleClickEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
633 const QPointF pos
= transform
.map(event
->pos());
634 const int index
= m_view
->itemAt(pos
);
636 bool emitItemActivated
= !m_singleClickActivation
&&
637 (event
->button() & Qt::LeftButton
) &&
638 index
>= 0 && index
< m_model
->count();
639 if (emitItemActivated
) {
640 emit
itemActivated(index
);
645 bool KItemListController::dragEnterEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
652 bool KItemListController::dragLeaveEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
659 bool KItemListController::dragMoveEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
662 if (!m_model
|| !m_view
) {
666 KItemListWidget
* oldHoveredWidget
= hoveredWidget();
668 const QPointF pos
= transform
.map(event
->pos());
669 KItemListWidget
* newHoveredWidget
= widgetForPos(pos
);
671 if (oldHoveredWidget
!= newHoveredWidget
) {
672 m_autoActivationTimer
->stop();
674 if (oldHoveredWidget
) {
675 oldHoveredWidget
->setHovered(false);
676 emit
itemUnhovered(oldHoveredWidget
->index());
679 if (newHoveredWidget
) {
680 const int index
= newHoveredWidget
->index();
681 if (m_model
->supportsDropping(index
)) {
682 newHoveredWidget
->setHovered(true);
684 emit
itemHovered(index
);
686 if (m_autoActivationTimer
->interval() >= 0) {
687 m_autoActivationTimer
->setProperty("index", index
);
688 m_autoActivationTimer
->start();
696 bool KItemListController::dropEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
703 m_autoActivationTimer
->stop();
705 const QPointF pos
= transform
.map(event
->pos());
706 const int index
= m_view
->itemAt(pos
);
707 emit
itemDropEvent(index
, event
);
712 bool KItemListController::hoverEnterEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
719 bool KItemListController::hoverMoveEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
722 if (!m_model
|| !m_view
) {
726 KItemListWidget
* oldHoveredWidget
= hoveredWidget();
727 const QPointF pos
= transform
.map(event
->pos());
728 KItemListWidget
* newHoveredWidget
= widgetForPos(pos
);
730 if (oldHoveredWidget
!= newHoveredWidget
) {
731 if (oldHoveredWidget
) {
732 oldHoveredWidget
->setHovered(false);
733 emit
itemUnhovered(oldHoveredWidget
->index());
736 if (newHoveredWidget
) {
737 newHoveredWidget
->setHovered(true);
738 emit
itemHovered(newHoveredWidget
->index());
745 bool KItemListController::hoverLeaveEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
750 if (!m_model
|| !m_view
) {
754 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
755 if (widget
->isHovered()) {
756 widget
->setHovered(false);
757 emit
itemUnhovered(widget
->index());
763 bool KItemListController::wheelEvent(QGraphicsSceneWheelEvent
* event
, const QTransform
& transform
)
770 bool KItemListController::resizeEvent(QGraphicsSceneResizeEvent
* event
, const QTransform
& transform
)
777 bool KItemListController::processEvent(QEvent
* event
, const QTransform
& transform
)
783 switch (event
->type()) {
784 case QEvent::KeyPress
:
785 return keyPressEvent(static_cast<QKeyEvent
*>(event
));
786 case QEvent::InputMethod
:
787 return inputMethodEvent(static_cast<QInputMethodEvent
*>(event
));
788 case QEvent::GraphicsSceneMousePress
:
789 return mousePressEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
790 case QEvent::GraphicsSceneMouseMove
:
791 return mouseMoveEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
792 case QEvent::GraphicsSceneMouseRelease
:
793 return mouseReleaseEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
794 case QEvent::GraphicsSceneMouseDoubleClick
:
795 return mouseDoubleClickEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
796 case QEvent::GraphicsSceneWheel
:
797 return wheelEvent(static_cast<QGraphicsSceneWheelEvent
*>(event
), QTransform());
798 case QEvent::GraphicsSceneDragEnter
:
799 return dragEnterEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
800 case QEvent::GraphicsSceneDragLeave
:
801 return dragLeaveEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
802 case QEvent::GraphicsSceneDragMove
:
803 return dragMoveEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
804 case QEvent::GraphicsSceneDrop
:
805 return dropEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
806 case QEvent::GraphicsSceneHoverEnter
:
807 return hoverEnterEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
808 case QEvent::GraphicsSceneHoverMove
:
809 return hoverMoveEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
810 case QEvent::GraphicsSceneHoverLeave
:
811 return hoverLeaveEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
812 case QEvent::GraphicsSceneResize
:
813 return resizeEvent(static_cast<QGraphicsSceneResizeEvent
*>(event
), transform
);
821 void KItemListController::slotViewScrollOffsetChanged(qreal current
, qreal previous
)
827 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
828 if (rubberBand
->isActive()) {
829 const qreal diff
= current
- previous
;
830 // TODO: Ideally just QCursor::pos() should be used as
831 // new end-position but it seems there is no easy way
832 // to have something like QWidget::mapFromGlobal() for QGraphicsWidget
833 // (... or I just missed an easy way to do the mapping)
834 QPointF endPos
= rubberBand
->endPosition();
835 if (m_view
->scrollOrientation() == Qt::Vertical
) {
841 rubberBand
->setEndPosition(endPos
);
845 void KItemListController::slotRubberBandChanged()
847 if (!m_view
|| !m_model
|| m_model
->count() <= 0) {
851 const KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
852 const QPointF startPos
= rubberBand
->startPosition();
853 const QPointF endPos
= rubberBand
->endPosition();
854 QRectF rubberBandRect
= QRectF(startPos
, endPos
).normalized();
856 const bool scrollVertical
= (m_view
->scrollOrientation() == Qt::Vertical
);
857 if (scrollVertical
) {
858 rubberBandRect
.translate(0, -m_view
->scrollOffset());
860 rubberBandRect
.translate(-m_view
->scrollOffset(), 0);
863 if (!m_oldSelection
.isEmpty()) {
864 // Clear the old selection that was available before the rubberband has
865 // been activated in case if no Shift- or Control-key are pressed
866 const bool shiftOrControlPressed
= QApplication::keyboardModifiers() & Qt::ShiftModifier
||
867 QApplication::keyboardModifiers() & Qt::ControlModifier
;
868 if (!shiftOrControlPressed
) {
869 m_oldSelection
.clear();
873 QSet
<int> selectedItems
;
875 // Select all visible items that intersect with the rubberband
876 foreach (const KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
877 const int index
= widget
->index();
879 const QRectF widgetRect
= m_view
->itemRect(index
);
880 if (widgetRect
.intersects(rubberBandRect
)) {
881 const QRectF iconRect
= widget
->iconRect().translated(widgetRect
.topLeft());
882 const QRectF textRect
= widget
->textRect().translated(widgetRect
.topLeft());
883 if (iconRect
.intersects(rubberBandRect
) || textRect
.intersects(rubberBandRect
)) {
884 selectedItems
.insert(index
);
889 // Select all invisible items that intersect with the rubberband. Instead of
890 // iterating all items only the area which might be touched by the rubberband
892 const bool increaseIndex
= scrollVertical
?
893 startPos
.y() > endPos
.y(): startPos
.x() > endPos
.x();
895 int index
= increaseIndex
? m_view
->lastVisibleIndex() + 1 : m_view
->firstVisibleIndex() - 1;
896 bool selectionFinished
= false;
898 const QRectF widgetRect
= m_view
->itemRect(index
);
899 if (widgetRect
.intersects(rubberBandRect
)) {
900 selectedItems
.insert(index
);
905 selectionFinished
= (index
>= m_model
->count()) ||
906 ( scrollVertical
&& widgetRect
.top() > rubberBandRect
.bottom()) ||
907 (!scrollVertical
&& widgetRect
.left() > rubberBandRect
.right());
910 selectionFinished
= (index
< 0) ||
911 ( scrollVertical
&& widgetRect
.bottom() < rubberBandRect
.top()) ||
912 (!scrollVertical
&& widgetRect
.right() < rubberBandRect
.left());
914 } while (!selectionFinished
);
916 if (QApplication::keyboardModifiers() & Qt::ControlModifier
) {
917 // If Control is pressed, the selection state of all items in the rubberband is toggled.
918 // Therefore, the new selection contains:
919 // 1. All previously selected items which are not inside the rubberband, and
920 // 2. all items inside the rubberband which have not been selected previously.
921 m_selectionManager
->setSelectedItems((m_oldSelection
- selectedItems
) + (selectedItems
- m_oldSelection
));
924 m_selectionManager
->setSelectedItems(selectedItems
+ m_oldSelection
);
928 void KItemListController::startDragging()
930 if (!m_view
|| !m_model
) {
934 const QSet
<int> selectedItems
= m_selectionManager
->selectedItems();
935 if (selectedItems
.isEmpty()) {
939 QMimeData
* data
= m_model
->createMimeData(selectedItems
);
944 // The created drag object will be owned and deleted
945 // by QApplication::activeWindow().
946 QDrag
* drag
= new QDrag(QApplication::activeWindow());
947 drag
->setMimeData(data
);
949 const QPixmap pixmap
= m_view
->createDragPixmap(selectedItems
);
950 drag
->setPixmap(pixmap
);
952 drag
->exec(Qt::MoveAction
| Qt::CopyAction
| Qt::LinkAction
, Qt::CopyAction
);
955 KItemListWidget
* KItemListController::hoveredWidget() const
959 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
960 if (widget
->isHovered()) {
968 KItemListWidget
* KItemListController::widgetForPos(const QPointF
& pos
) const
972 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
973 const QPointF mappedPos
= widget
->mapFromItem(m_view
, pos
);
975 const bool hovered
= widget
->contains(mappedPos
) &&
976 !widget
->expansionToggleRect().contains(mappedPos
);
985 void KItemListController::updateKeyboardAnchor()
987 const bool validAnchor
= m_keyboardAnchorIndex
>= 0 &&
988 m_keyboardAnchorIndex
< m_model
->count() &&
989 keyboardAnchorPos(m_keyboardAnchorIndex
) == m_keyboardAnchorPos
;
991 const int index
= m_selectionManager
->currentItem();
992 m_keyboardAnchorIndex
= index
;
993 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
997 int KItemListController::nextRowIndex() const
999 const int currentIndex
= m_selectionManager
->currentItem();
1000 if (m_keyboardAnchorIndex
< 0) {
1001 return currentIndex
;
1004 const int maxIndex
= m_model
->count() - 1;
1005 if (currentIndex
== maxIndex
) {
1006 return currentIndex
;
1009 // Calculate the index of the last column inside the row of the current index
1010 int lastColumnIndex
= currentIndex
;
1011 while (keyboardAnchorPos(lastColumnIndex
+ 1) > keyboardAnchorPos(lastColumnIndex
)) {
1013 if (lastColumnIndex
>= maxIndex
) {
1014 return currentIndex
;
1018 // Based on the last column index go to the next row and calculate the nearest index
1019 // that is below the current index
1020 int nextRowIndex
= lastColumnIndex
+ 1;
1021 int searchIndex
= nextRowIndex
;
1022 qreal minDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(nextRowIndex
));
1023 while (searchIndex
< maxIndex
&& keyboardAnchorPos(searchIndex
+ 1) > keyboardAnchorPos(searchIndex
)) {
1025 const qreal searchDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(searchIndex
));
1026 if (searchDiff
< minDiff
) {
1027 minDiff
= searchDiff
;
1028 nextRowIndex
= searchIndex
;
1032 return nextRowIndex
;
1035 int KItemListController::previousRowIndex() const
1037 const int currentIndex
= m_selectionManager
->currentItem();
1038 if (m_keyboardAnchorIndex
< 0 || currentIndex
== 0) {
1039 return currentIndex
;
1042 // Calculate the index of the first column inside the row of the current index
1043 int firstColumnIndex
= currentIndex
;
1044 while (keyboardAnchorPos(firstColumnIndex
- 1) < keyboardAnchorPos(firstColumnIndex
)) {
1046 if (firstColumnIndex
<= 0) {
1047 return currentIndex
;
1051 // Based on the first column index go to the previous row and calculate the nearest index
1052 // that is above the current index
1053 int previousRowIndex
= firstColumnIndex
- 1;
1054 int searchIndex
= previousRowIndex
;
1055 qreal minDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(previousRowIndex
));
1056 while (searchIndex
> 0 && keyboardAnchorPos(searchIndex
- 1) < keyboardAnchorPos(searchIndex
)) {
1058 const qreal searchDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(searchIndex
));
1059 if (searchDiff
< minDiff
) {
1060 minDiff
= searchDiff
;
1061 previousRowIndex
= searchIndex
;
1065 return previousRowIndex
;
1068 qreal
KItemListController::keyboardAnchorPos(int index
) const
1070 const QRectF itemRect
= m_view
->itemRect(index
);
1071 if (!itemRect
.isEmpty()) {
1072 return (m_view
->scrollOrientation() == Qt::Vertical
) ? itemRect
.x() : itemRect
.y();
1078 #include "kitemlistcontroller.moc"