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_selectionBehavior(NoSelection
),
49 m_selectionManager(new KItemListSelectionManager(this)),
50 m_keyboardManager(new KItemListKeyboardSearchManager(this)),
53 m_autoActivationTimer(0),
55 m_keyboardAnchorIndex(-1),
56 m_keyboardAnchorPos(0)
58 connect(m_keyboardManager
, SIGNAL(changeCurrentItem(QString
,bool)),
59 this, SLOT(slotChangeCurrentItem(QString
,bool)));
61 m_autoActivationTimer
= new QTimer(this);
62 m_autoActivationTimer
->setSingleShot(true);
63 m_autoActivationTimer
->setInterval(-1);
64 connect(m_autoActivationTimer
, SIGNAL(timeout()), this, SLOT(slotAutoActivationTimeout()));
67 KItemListController::~KItemListController()
71 void KItemListController::setModel(KItemModelBase
* model
)
73 if (m_model
== model
) {
77 KItemModelBase
* oldModel
= m_model
;
81 m_view
->setModel(m_model
);
84 m_selectionManager
->setModel(m_model
);
86 emit
modelChanged(m_model
, oldModel
);
89 KItemModelBase
* KItemListController::model() const
94 KItemListSelectionManager
* KItemListController::selectionManager() const
96 return m_selectionManager
;
99 void KItemListController::setView(KItemListView
* view
)
101 if (m_view
== view
) {
105 KItemListView
* oldView
= m_view
;
107 disconnect(oldView
, SIGNAL(scrollOffsetChanged(qreal
,qreal
)), this, SLOT(slotViewScrollOffsetChanged(qreal
,qreal
)));
113 m_view
->setController(this);
114 m_view
->setModel(m_model
);
115 connect(m_view
, SIGNAL(scrollOffsetChanged(qreal
,qreal
)), this, SLOT(slotViewScrollOffsetChanged(qreal
,qreal
)));
118 emit
viewChanged(m_view
, oldView
);
121 KItemListView
* KItemListController::view() const
126 void KItemListController::setSelectionBehavior(SelectionBehavior behavior
)
128 m_selectionBehavior
= behavior
;
131 KItemListController::SelectionBehavior
KItemListController::selectionBehavior() const
133 return m_selectionBehavior
;
136 void KItemListController::setAutoActivationDelay(int delay
)
138 m_autoActivationTimer
->setInterval(delay
);
141 int KItemListController::autoActivationDelay() const
143 return m_autoActivationTimer
->interval();
146 void KItemListController::setSingleClickActivation(bool singleClick
)
148 m_singleClickActivation
= singleClick
;
151 bool KItemListController::singleClickActivation() const
153 return m_singleClickActivation
;
156 bool KItemListController::showEvent(QShowEvent
* event
)
162 bool KItemListController::hideEvent(QHideEvent
* event
)
168 bool KItemListController::keyPressEvent(QKeyEvent
* event
)
170 int index
= m_selectionManager
->currentItem();
171 int key
= event
->key();
173 // Handle the expanding/collapsing of items
174 if (m_view
->supportsItemExpanding() && m_model
->isExpandable(index
)) {
175 if (key
== Qt::Key_Right
) {
176 if (m_model
->setExpanded(index
, true)) {
179 } else if (key
== Qt::Key_Left
) {
180 if (m_model
->setExpanded(index
, false)) {
186 const bool shiftPressed
= event
->modifiers() & Qt::ShiftModifier
;
187 const bool controlPressed
= event
->modifiers() & Qt::ControlModifier
;
188 const bool shiftOrControlPressed
= shiftPressed
|| controlPressed
;
190 const int itemCount
= m_model
->count();
192 // For horizontal scroll orientation, transform
193 // the arrow keys to simplify the event handling.
194 if (m_view
->scrollOrientation() == Qt::Horizontal
) {
196 case Qt::Key_Up
: key
= Qt::Key_Left
; break;
197 case Qt::Key_Down
: key
= Qt::Key_Right
; break;
198 case Qt::Key_Left
: key
= Qt::Key_Up
; break;
199 case Qt::Key_Right
: key
= Qt::Key_Down
; break;
204 const bool selectSingleItem
= itemCount
== 1 &&
205 (key
== Qt::Key_Home
|| key
== Qt::Key_End
||
206 key
== Qt::Key_Up
|| key
== Qt::Key_Down
||
207 key
== Qt::Key_Left
|| key
== Qt::Key_Right
);
208 if (selectSingleItem
) {
209 const int current
= m_selectionManager
->currentItem();
210 m_selectionManager
->setSelected(current
);
220 index
= itemCount
- 1;
226 m_keyboardAnchorIndex
= index
;
227 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
232 if (index
< itemCount
- 1) {
234 m_keyboardAnchorIndex
= index
;
235 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
240 updateKeyboardAnchor();
241 index
= previousRowIndex();
245 updateKeyboardAnchor();
246 index
= nextRowIndex();
250 case Qt::Key_Return
: {
251 const QSet
<int> selectedItems
= m_selectionManager
->selectedItems();
252 if (selectedItems
.count() >= 2) {
253 emit
itemsActivated(selectedItems
);
254 } else if (selectedItems
.count() == 1) {
255 emit
itemActivated(selectedItems
.toList().first());
257 emit
itemActivated(index
);
263 if (controlPressed
) {
264 m_selectionManager
->endAnchoredSelection();
265 m_selectionManager
->setSelected(index
, 1, KItemListSelectionManager::Toggle
);
266 m_selectionManager
->beginAnchoredSelection(index
);
268 const int current
= m_selectionManager
->currentItem();
269 m_selectionManager
->setSelected(current
);
274 // Emit the signal itemContextMenuRequested() in case if at least one
275 // item is selected. Otherwise the signal viewContextMenuRequested() will be emitted.
276 const QSet
<int> selectedItems
= m_selectionManager
->selectedItems();
278 if (selectedItems
.count() >= 2) {
279 const int currentItemIndex
= m_selectionManager
->currentItem();
280 index
= selectedItems
.contains(currentItemIndex
)
281 ? currentItemIndex
: selectedItems
.toList().first();
282 } else if (selectedItems
.count() == 1) {
283 index
= selectedItems
.toList().first();
287 const QRectF contextRect
= m_view
->itemContextRect(index
);
288 const QPointF
pos(m_view
->scene()->views().first()->mapToGlobal(contextRect
.bottomRight().toPoint()));
289 emit
itemContextMenuRequested(index
, pos
);
291 emit
viewContextMenuRequested(QCursor::pos());
297 m_keyboardManager
->addKeys(event
->text());
301 if (m_selectionManager
->currentItem() != index
) {
302 if (controlPressed
) {
303 m_selectionManager
->endAnchoredSelection();
306 m_selectionManager
->setCurrentItem(index
);
308 if (!shiftOrControlPressed
|| m_selectionBehavior
== SingleSelection
) {
309 m_selectionManager
->clearSelection();
310 m_selectionManager
->setSelected(index
, 1);
314 m_selectionManager
->beginAnchoredSelection(index
);
317 m_view
->scrollToItem(index
);
322 void KItemListController::slotChangeCurrentItem(const QString
& text
, bool searchFromNextItem
)
324 if (!m_model
|| m_model
->count() == 0) {
327 const int currentIndex
= m_selectionManager
->currentItem();
329 if (searchFromNextItem
) {
330 index
= m_model
->indexForKeyboardSearch(text
, (currentIndex
+ 1) % m_model
->count());
332 index
= m_model
->indexForKeyboardSearch(text
, currentIndex
);
335 m_selectionManager
->setCurrentItem(index
);
336 m_selectionManager
->clearSelection();
337 m_selectionManager
->setSelected(index
, 1);
338 m_selectionManager
->beginAnchoredSelection(index
);
339 m_view
->scrollToItem(index
);
343 void KItemListController::slotAutoActivationTimeout()
345 if (!m_model
|| !m_view
) {
349 const int index
= m_autoActivationTimer
->property("index").toInt();
350 if (index
< 0 || index
>= m_model
->count()) {
354 if (m_model
->supportsDropping(index
)) {
355 if (m_view
->supportsItemExpanding() && m_model
->isExpandable(index
)) {
356 const bool expanded
= m_model
->isExpanded(index
);
357 m_model
->setExpanded(index
, !expanded
);
359 emit
itemActivated(index
);
364 bool KItemListController::inputMethodEvent(QInputMethodEvent
* event
)
370 bool KItemListController::mousePressEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
376 m_pressedMousePos
= transform
.map(event
->pos());
377 m_pressedIndex
= m_view
->itemAt(m_pressedMousePos
);
378 if (m_pressedIndex
>= 0) {
379 emit
itemPressed(m_pressedIndex
, event
->button());
382 if (m_view
->isAboveExpansionToggle(m_pressedIndex
, m_pressedMousePos
)) {
383 m_selectionManager
->setCurrentItem(m_pressedIndex
);
387 m_selectionTogglePressed
= m_view
->isAboveSelectionToggle(m_pressedIndex
, m_pressedMousePos
);
388 if (m_selectionTogglePressed
) {
389 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
390 // The previous anchored selection has been finished already in
391 // KItemListSelectionManager::setSelected(). We can safely change
392 // the current item and start a new anchored selection now.
393 m_selectionManager
->setCurrentItem(m_pressedIndex
);
394 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
398 const bool shiftPressed
= event
->modifiers() & Qt::ShiftModifier
;
399 const bool controlPressed
= event
->modifiers() & Qt::ControlModifier
;
401 // The previous selection is cleared if either
402 // 1. The selection mode is SingleSelection, or
403 // 2. the selection mode is MultiSelection, and *none* of the following conditions are met:
404 // a) Shift or Control are pressed.
405 // b) The clicked item is selected already. In that case, the user might want to:
406 // - start dragging multiple items, or
407 // - open the context menu and perform an action for all selected items.
408 const bool shiftOrControlPressed
= shiftPressed
|| controlPressed
;
409 const bool pressedItemAlreadySelected
= m_pressedIndex
>= 0 && m_selectionManager
->isSelected(m_pressedIndex
);
410 const bool clearSelection
= m_selectionBehavior
== SingleSelection
||
411 (!shiftOrControlPressed
&& !pressedItemAlreadySelected
);
412 if (clearSelection
) {
413 m_selectionManager
->clearSelection();
417 // Finish the anchored selection before the current index is changed
418 m_selectionManager
->endAnchoredSelection();
421 if (m_pressedIndex
>= 0) {
422 m_selectionManager
->setCurrentItem(m_pressedIndex
);
424 switch (m_selectionBehavior
) {
428 case SingleSelection
:
429 m_selectionManager
->setSelected(m_pressedIndex
);
433 if (controlPressed
) {
434 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
435 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
436 } else if (!shiftPressed
|| !m_selectionManager
->isAnchoredSelectionActive()) {
437 // Select the pressed item and start a new anchored selection
438 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Select
);
439 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
448 if (event
->buttons() & Qt::RightButton
) {
449 emit
itemContextMenuRequested(m_pressedIndex
, event
->screenPos());
455 if (event
->buttons() & Qt::RightButton
) {
456 const QRectF headerBounds
= m_view
->headerBoundaries();
457 if (headerBounds
.contains(event
->pos())) {
458 emit
headerContextMenuRequested(event
->screenPos());
460 emit
viewContextMenuRequested(event
->screenPos());
465 if (m_selectionBehavior
== MultiSelection
) {
466 QPointF startPos
= m_pressedMousePos
;
467 if (m_view
->scrollOrientation() == Qt::Vertical
) {
468 startPos
.ry() += m_view
->scrollOffset();
469 if (m_view
->itemSize().width() < 0) {
470 // Use a special rubberband for views that have only one column and
471 // expand the rubberband to use the whole width of the view.
475 startPos
.rx() += m_view
->scrollOffset();
478 m_oldSelection
= m_selectionManager
->selectedItems();
479 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
480 rubberBand
->setStartPosition(startPos
);
481 rubberBand
->setEndPosition(startPos
);
482 rubberBand
->setActive(true);
483 connect(rubberBand
, SIGNAL(endPositionChanged(QPointF
,QPointF
)), this, SLOT(slotRubberBandChanged()));
484 m_view
->setAutoScroll(true);
490 bool KItemListController::mouseMoveEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
496 if (m_pressedIndex
>= 0) {
497 // Check whether a dragging should be started
498 if (event
->buttons() & Qt::LeftButton
) {
499 const QPointF pos
= transform
.map(event
->pos());
500 if ((pos
- m_pressedMousePos
).manhattanLength() >= QApplication::startDragDistance()) {
501 if (!m_selectionManager
->isSelected(m_pressedIndex
)) {
502 // Always assure that the dragged item gets selected. Usually this is already
503 // done on the mouse-press event, but when using the selection-toggle on a
504 // selected item the dragged item is not selected yet.
505 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
511 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
512 if (rubberBand
->isActive()) {
513 QPointF endPos
= transform
.map(event
->pos());
515 // Update the current item.
516 const int newCurrent
= m_view
->itemAt(endPos
);
517 if (newCurrent
>= 0) {
518 // It's expected that the new current index is also the new anchor (bug 163451).
519 m_selectionManager
->endAnchoredSelection();
520 m_selectionManager
->setCurrentItem(newCurrent
);
521 m_selectionManager
->beginAnchoredSelection(newCurrent
);
524 if (m_view
->scrollOrientation() == Qt::Vertical
) {
525 endPos
.ry() += m_view
->scrollOffset();
526 if (m_view
->itemSize().width() < 0) {
527 // Use a special rubberband for views that have only one column and
528 // expand the rubberband to use the whole width of the view.
529 endPos
.setX(m_view
->size().width());
532 endPos
.rx() += m_view
->scrollOffset();
534 rubberBand
->setEndPosition(endPos
);
541 bool KItemListController::mouseReleaseEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
547 if (m_pressedIndex
>= 0) {
548 emit
itemReleased(m_pressedIndex
, event
->button());
551 const bool isAboveSelectionToggle
= m_view
->isAboveSelectionToggle(m_pressedIndex
, m_pressedMousePos
);
552 if (isAboveSelectionToggle
) {
553 m_selectionTogglePressed
= false;
557 if (!isAboveSelectionToggle
&& m_selectionTogglePressed
) {
558 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
559 m_selectionTogglePressed
= false;
563 const bool shiftOrControlPressed
= event
->modifiers() & Qt::ShiftModifier
||
564 event
->modifiers() & Qt::ControlModifier
;
566 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
567 if (rubberBand
->isActive()) {
568 disconnect(rubberBand
, SIGNAL(endPositionChanged(QPointF
,QPointF
)), this, SLOT(slotRubberBandChanged()));
569 rubberBand
->setActive(false);
570 m_oldSelection
.clear();
571 m_view
->setAutoScroll(false);
574 const QPointF pos
= transform
.map(event
->pos());
575 const int index
= m_view
->itemAt(pos
);
577 if (index
>= 0 && index
== m_pressedIndex
) {
578 // The release event is done above the same item as the press event
580 if (event
->button() & Qt::LeftButton
) {
581 bool emitItemActivated
= true;
582 if (m_view
->isAboveExpansionToggle(index
, pos
)) {
583 const bool expanded
= m_model
->isExpanded(index
);
584 m_model
->setExpanded(index
, !expanded
);
586 emit
itemExpansionToggleClicked(index
);
587 emitItemActivated
= false;
588 } else if (shiftOrControlPressed
) {
589 // The mouse click should only update the selection, not trigger the item
590 emitItemActivated
= false;
591 } else if (!m_singleClickActivation
) {
592 emitItemActivated
= false;
594 if (emitItemActivated
) {
595 emit
itemActivated(index
);
597 } else if (event
->button() & Qt::MidButton
) {
598 emit
itemMiddleClicked(index
);
602 m_pressedMousePos
= QPointF();
607 bool KItemListController::mouseDoubleClickEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
609 const QPointF pos
= transform
.map(event
->pos());
610 const int index
= m_view
->itemAt(pos
);
612 bool emitItemActivated
= !m_singleClickActivation
&&
613 (event
->button() & Qt::LeftButton
) &&
614 index
>= 0 && index
< m_model
->count();
615 if (emitItemActivated
) {
616 emit
itemActivated(index
);
621 bool KItemListController::dragEnterEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
628 bool KItemListController::dragLeaveEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
635 bool KItemListController::dragMoveEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
638 if (!m_model
|| !m_view
) {
642 KItemListWidget
* oldHoveredWidget
= hoveredWidget();
644 const QPointF pos
= transform
.map(event
->pos());
645 KItemListWidget
* newHoveredWidget
= widgetForPos(pos
);
647 if (oldHoveredWidget
!= newHoveredWidget
) {
648 m_autoActivationTimer
->stop();
650 if (oldHoveredWidget
) {
651 oldHoveredWidget
->setHovered(false);
652 emit
itemUnhovered(oldHoveredWidget
->index());
655 if (newHoveredWidget
) {
656 const int index
= newHoveredWidget
->index();
657 if (m_model
->supportsDropping(index
)) {
658 newHoveredWidget
->setHovered(true);
660 emit
itemHovered(index
);
662 if (m_autoActivationTimer
->interval() >= 0) {
663 m_autoActivationTimer
->setProperty("index", index
);
664 m_autoActivationTimer
->start();
672 bool KItemListController::dropEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
679 m_autoActivationTimer
->stop();
681 const QPointF pos
= transform
.map(event
->pos());
682 const int index
= m_view
->itemAt(pos
);
683 emit
itemDropEvent(index
, event
);
688 bool KItemListController::hoverEnterEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
695 bool KItemListController::hoverMoveEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
698 if (!m_model
|| !m_view
) {
702 KItemListWidget
* oldHoveredWidget
= hoveredWidget();
703 const QPointF pos
= transform
.map(event
->pos());
704 KItemListWidget
* newHoveredWidget
= widgetForPos(pos
);
706 if (oldHoveredWidget
!= newHoveredWidget
) {
707 if (oldHoveredWidget
) {
708 oldHoveredWidget
->setHovered(false);
709 emit
itemUnhovered(oldHoveredWidget
->index());
712 if (newHoveredWidget
) {
713 newHoveredWidget
->setHovered(true);
714 emit
itemHovered(newHoveredWidget
->index());
721 bool KItemListController::hoverLeaveEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
726 if (!m_model
|| !m_view
) {
730 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
731 if (widget
->isHovered()) {
732 widget
->setHovered(false);
733 emit
itemUnhovered(widget
->index());
739 bool KItemListController::wheelEvent(QGraphicsSceneWheelEvent
* event
, const QTransform
& transform
)
746 bool KItemListController::resizeEvent(QGraphicsSceneResizeEvent
* event
, const QTransform
& transform
)
753 bool KItemListController::processEvent(QEvent
* event
, const QTransform
& transform
)
759 switch (event
->type()) {
760 case QEvent::KeyPress
:
761 return keyPressEvent(static_cast<QKeyEvent
*>(event
));
762 case QEvent::InputMethod
:
763 return inputMethodEvent(static_cast<QInputMethodEvent
*>(event
));
764 case QEvent::GraphicsSceneMousePress
:
765 return mousePressEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
766 case QEvent::GraphicsSceneMouseMove
:
767 return mouseMoveEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
768 case QEvent::GraphicsSceneMouseRelease
:
769 return mouseReleaseEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
770 case QEvent::GraphicsSceneMouseDoubleClick
:
771 return mouseDoubleClickEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
772 case QEvent::GraphicsSceneWheel
:
773 return wheelEvent(static_cast<QGraphicsSceneWheelEvent
*>(event
), QTransform());
774 case QEvent::GraphicsSceneDragEnter
:
775 return dragEnterEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
776 case QEvent::GraphicsSceneDragLeave
:
777 return dragLeaveEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
778 case QEvent::GraphicsSceneDragMove
:
779 return dragMoveEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
780 case QEvent::GraphicsSceneDrop
:
781 return dropEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
782 case QEvent::GraphicsSceneHoverEnter
:
783 return hoverEnterEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
784 case QEvent::GraphicsSceneHoverMove
:
785 return hoverMoveEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
786 case QEvent::GraphicsSceneHoverLeave
:
787 return hoverLeaveEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
788 case QEvent::GraphicsSceneResize
:
789 return resizeEvent(static_cast<QGraphicsSceneResizeEvent
*>(event
), transform
);
797 void KItemListController::slotViewScrollOffsetChanged(qreal current
, qreal previous
)
803 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
804 if (rubberBand
->isActive()) {
805 const qreal diff
= current
- previous
;
806 // TODO: Ideally just QCursor::pos() should be used as
807 // new end-position but it seems there is no easy way
808 // to have something like QWidget::mapFromGlobal() for QGraphicsWidget
809 // (... or I just missed an easy way to do the mapping)
810 QPointF endPos
= rubberBand
->endPosition();
811 if (m_view
->scrollOrientation() == Qt::Vertical
) {
817 rubberBand
->setEndPosition(endPos
);
821 void KItemListController::slotRubberBandChanged()
823 if (!m_view
|| !m_model
|| m_model
->count() <= 0) {
827 const KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
828 const QPointF startPos
= rubberBand
->startPosition();
829 const QPointF endPos
= rubberBand
->endPosition();
830 QRectF rubberBandRect
= QRectF(startPos
, endPos
).normalized();
832 const bool scrollVertical
= (m_view
->scrollOrientation() == Qt::Vertical
);
833 if (scrollVertical
) {
834 rubberBandRect
.translate(0, -m_view
->scrollOffset());
836 rubberBandRect
.translate(-m_view
->scrollOffset(), 0);
839 if (!m_oldSelection
.isEmpty()) {
840 // Clear the old selection that was available before the rubberband has
841 // been activated in case if no Shift- or Control-key are pressed
842 const bool shiftOrControlPressed
= QApplication::keyboardModifiers() & Qt::ShiftModifier
||
843 QApplication::keyboardModifiers() & Qt::ControlModifier
;
844 if (!shiftOrControlPressed
) {
845 m_oldSelection
.clear();
849 QSet
<int> selectedItems
;
851 // Select all visible items that intersect with the rubberband
852 foreach (const KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
853 const int index
= widget
->index();
855 const QRectF widgetRect
= m_view
->itemRect(index
);
856 if (widgetRect
.intersects(rubberBandRect
)) {
857 const QRectF iconRect
= widget
->iconRect().translated(widgetRect
.topLeft());
858 const QRectF textRect
= widget
->textRect().translated(widgetRect
.topLeft());
859 if (iconRect
.intersects(rubberBandRect
) || textRect
.intersects(rubberBandRect
)) {
860 selectedItems
.insert(index
);
865 // Select all invisible items that intersect with the rubberband. Instead of
866 // iterating all items only the area which might be touched by the rubberband
868 const bool increaseIndex
= scrollVertical
?
869 startPos
.y() > endPos
.y(): startPos
.x() > endPos
.x();
871 int index
= increaseIndex
? m_view
->lastVisibleIndex() + 1 : m_view
->firstVisibleIndex() - 1;
872 bool selectionFinished
= false;
874 const QRectF widgetRect
= m_view
->itemRect(index
);
875 if (widgetRect
.intersects(rubberBandRect
)) {
876 selectedItems
.insert(index
);
881 selectionFinished
= (index
>= m_model
->count()) ||
882 ( scrollVertical
&& widgetRect
.top() > rubberBandRect
.bottom()) ||
883 (!scrollVertical
&& widgetRect
.left() > rubberBandRect
.right());
886 selectionFinished
= (index
< 0) ||
887 ( scrollVertical
&& widgetRect
.bottom() < rubberBandRect
.top()) ||
888 (!scrollVertical
&& widgetRect
.right() < rubberBandRect
.left());
890 } while (!selectionFinished
);
892 if (QApplication::keyboardModifiers() & Qt::ControlModifier
) {
893 // If Control is pressed, the selection state of all items in the rubberband is toggled.
894 // Therefore, the new selection contains:
895 // 1. All previously selected items which are not inside the rubberband, and
896 // 2. all items inside the rubberband which have not been selected previously.
897 m_selectionManager
->setSelectedItems((m_oldSelection
- selectedItems
) + (selectedItems
- m_oldSelection
));
900 m_selectionManager
->setSelectedItems(selectedItems
+ m_oldSelection
);
904 void KItemListController::startDragging()
906 if (!m_view
|| !m_model
) {
910 const QSet
<int> selectedItems
= m_selectionManager
->selectedItems();
911 if (selectedItems
.isEmpty()) {
915 QMimeData
* data
= m_model
->createMimeData(selectedItems
);
920 // The created drag object will be owned and deleted
921 // by QApplication::activeWindow().
922 QDrag
* drag
= new QDrag(QApplication::activeWindow());
923 drag
->setMimeData(data
);
925 const QPixmap pixmap
= m_view
->createDragPixmap(selectedItems
);
926 drag
->setPixmap(pixmap
);
928 drag
->exec(Qt::MoveAction
| Qt::CopyAction
| Qt::LinkAction
, Qt::CopyAction
);
931 KItemListWidget
* KItemListController::hoveredWidget() const
935 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
936 if (widget
->isHovered()) {
944 KItemListWidget
* KItemListController::widgetForPos(const QPointF
& pos
) const
948 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
949 const QPointF mappedPos
= widget
->mapFromItem(m_view
, pos
);
951 const bool hovered
= widget
->contains(mappedPos
) &&
952 !widget
->expansionToggleRect().contains(mappedPos
);
961 void KItemListController::updateKeyboardAnchor()
963 const bool validAnchor
= m_keyboardAnchorIndex
>= 0 &&
964 m_keyboardAnchorIndex
< m_model
->count() &&
965 keyboardAnchorPos(m_keyboardAnchorIndex
) == m_keyboardAnchorPos
;
967 const int index
= m_selectionManager
->currentItem();
968 m_keyboardAnchorIndex
= index
;
969 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
973 int KItemListController::nextRowIndex() const
975 const int currentIndex
= m_selectionManager
->currentItem();
976 if (m_keyboardAnchorIndex
< 0) {
980 const int maxIndex
= m_model
->count() - 1;
981 if (currentIndex
== maxIndex
) {
985 // Calculate the index of the last column inside the row of the current index
986 int lastColumnIndex
= currentIndex
;
987 while (keyboardAnchorPos(lastColumnIndex
+ 1) > keyboardAnchorPos(lastColumnIndex
)) {
989 if (lastColumnIndex
>= maxIndex
) {
994 // Based on the last column index go to the next row and calculate the nearest index
995 // that is below the current index
996 int nextRowIndex
= lastColumnIndex
+ 1;
997 int searchIndex
= nextRowIndex
;
998 qreal minDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(nextRowIndex
));
999 while (searchIndex
< maxIndex
&& keyboardAnchorPos(searchIndex
+ 1) > keyboardAnchorPos(searchIndex
)) {
1001 const qreal searchDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(searchIndex
));
1002 if (searchDiff
< minDiff
) {
1003 minDiff
= searchDiff
;
1004 nextRowIndex
= searchIndex
;
1008 return nextRowIndex
;
1011 int KItemListController::previousRowIndex() const
1013 const int currentIndex
= m_selectionManager
->currentItem();
1014 if (m_keyboardAnchorIndex
< 0 || currentIndex
== 0) {
1015 return currentIndex
;
1018 // Calculate the index of the first column inside the row of the current index
1019 int firstColumnIndex
= currentIndex
;
1020 while (keyboardAnchorPos(firstColumnIndex
- 1) < keyboardAnchorPos(firstColumnIndex
)) {
1022 if (firstColumnIndex
<= 0) {
1023 return currentIndex
;
1027 // Based on the first column index go to the previous row and calculate the nearest index
1028 // that is above the current index
1029 int previousRowIndex
= firstColumnIndex
- 1;
1030 int searchIndex
= previousRowIndex
;
1031 qreal minDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(previousRowIndex
));
1032 while (searchIndex
> 0 && keyboardAnchorPos(searchIndex
- 1) < keyboardAnchorPos(searchIndex
)) {
1034 const qreal searchDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(searchIndex
));
1035 if (searchDiff
< minDiff
) {
1036 minDiff
= searchDiff
;
1037 previousRowIndex
= searchIndex
;
1041 return previousRowIndex
;
1044 qreal
KItemListController::keyboardAnchorPos(int index
) const
1046 const QRectF itemRect
= m_view
->itemRect(index
);
1047 if (!itemRect
.isEmpty()) {
1048 return (m_view
->scrollOrientation() == Qt::Vertical
) ? itemRect
.x() : itemRect
.y();
1054 #include "kitemlistcontroller.moc"