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
->endAnchoredSelection();
384 m_selectionManager
->setCurrentItem(m_pressedIndex
);
385 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
389 m_selectionTogglePressed
= m_view
->isAboveSelectionToggle(m_pressedIndex
, m_pressedMousePos
);
390 if (m_selectionTogglePressed
) {
391 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
392 // The previous anchored selection has been finished already in
393 // KItemListSelectionManager::setSelected(). We can safely change
394 // the current item and start a new anchored selection now.
395 m_selectionManager
->setCurrentItem(m_pressedIndex
);
396 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
400 const bool shiftPressed
= event
->modifiers() & Qt::ShiftModifier
;
401 const bool controlPressed
= event
->modifiers() & Qt::ControlModifier
;
403 // The previous selection is cleared if either
404 // 1. The selection mode is SingleSelection, or
405 // 2. the selection mode is MultiSelection, and *none* of the following conditions are met:
406 // a) Shift or Control are pressed.
407 // b) The clicked item is selected already. In that case, the user might want to:
408 // - start dragging multiple items, or
409 // - open the context menu and perform an action for all selected items.
410 const bool shiftOrControlPressed
= shiftPressed
|| controlPressed
;
411 const bool pressedItemAlreadySelected
= m_pressedIndex
>= 0 && m_selectionManager
->isSelected(m_pressedIndex
);
412 const bool clearSelection
= m_selectionBehavior
== SingleSelection
||
413 (!shiftOrControlPressed
&& !pressedItemAlreadySelected
);
414 if (clearSelection
) {
415 m_selectionManager
->clearSelection();
419 // Finish the anchored selection before the current index is changed
420 m_selectionManager
->endAnchoredSelection();
423 if (m_pressedIndex
>= 0) {
424 m_selectionManager
->setCurrentItem(m_pressedIndex
);
426 switch (m_selectionBehavior
) {
430 case SingleSelection
:
431 m_selectionManager
->setSelected(m_pressedIndex
);
435 if (controlPressed
) {
436 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
437 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
438 } else if (!shiftPressed
|| !m_selectionManager
->isAnchoredSelectionActive()) {
439 // Select the pressed item and start a new anchored selection
440 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Select
);
441 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
450 if (event
->buttons() & Qt::RightButton
) {
451 emit
itemContextMenuRequested(m_pressedIndex
, event
->screenPos());
457 if (event
->buttons() & Qt::RightButton
) {
458 const QRectF headerBounds
= m_view
->headerBoundaries();
459 if (headerBounds
.contains(event
->pos())) {
460 emit
headerContextMenuRequested(event
->screenPos());
462 emit
viewContextMenuRequested(event
->screenPos());
467 if (m_selectionBehavior
== MultiSelection
) {
468 QPointF startPos
= m_pressedMousePos
;
469 if (m_view
->scrollOrientation() == Qt::Vertical
) {
470 startPos
.ry() += m_view
->scrollOffset();
471 if (m_view
->itemSize().width() < 0) {
472 // Use a special rubberband for views that have only one column and
473 // expand the rubberband to use the whole width of the view.
477 startPos
.rx() += m_view
->scrollOffset();
480 m_oldSelection
= m_selectionManager
->selectedItems();
481 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
482 rubberBand
->setStartPosition(startPos
);
483 rubberBand
->setEndPosition(startPos
);
484 rubberBand
->setActive(true);
485 connect(rubberBand
, SIGNAL(endPositionChanged(QPointF
,QPointF
)), this, SLOT(slotRubberBandChanged()));
486 m_view
->setAutoScroll(true);
492 bool KItemListController::mouseMoveEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
498 if (m_pressedIndex
>= 0) {
499 // Check whether a dragging should be started
500 if (event
->buttons() & Qt::LeftButton
) {
501 const QPointF pos
= transform
.map(event
->pos());
502 if ((pos
- m_pressedMousePos
).manhattanLength() >= QApplication::startDragDistance()) {
503 if (!m_selectionManager
->isSelected(m_pressedIndex
)) {
504 // Always assure that the dragged item gets selected. Usually this is already
505 // done on the mouse-press event, but when using the selection-toggle on a
506 // selected item the dragged item is not selected yet.
507 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
513 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
514 if (rubberBand
->isActive()) {
515 QPointF endPos
= transform
.map(event
->pos());
517 // Update the current item.
518 const int newCurrent
= m_view
->itemAt(endPos
);
519 if (newCurrent
>= 0) {
520 // It's expected that the new current index is also the new anchor (bug 163451).
521 m_selectionManager
->endAnchoredSelection();
522 m_selectionManager
->setCurrentItem(newCurrent
);
523 m_selectionManager
->beginAnchoredSelection(newCurrent
);
526 if (m_view
->scrollOrientation() == Qt::Vertical
) {
527 endPos
.ry() += m_view
->scrollOffset();
528 if (m_view
->itemSize().width() < 0) {
529 // Use a special rubberband for views that have only one column and
530 // expand the rubberband to use the whole width of the view.
531 endPos
.setX(m_view
->size().width());
534 endPos
.rx() += m_view
->scrollOffset();
536 rubberBand
->setEndPosition(endPos
);
543 bool KItemListController::mouseReleaseEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
549 if (m_pressedIndex
>= 0) {
550 emit
itemReleased(m_pressedIndex
, event
->button());
553 const bool isAboveSelectionToggle
= m_view
->isAboveSelectionToggle(m_pressedIndex
, m_pressedMousePos
);
554 if (isAboveSelectionToggle
) {
555 m_selectionTogglePressed
= false;
559 if (!isAboveSelectionToggle
&& m_selectionTogglePressed
) {
560 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
561 m_selectionTogglePressed
= false;
565 const bool shiftOrControlPressed
= event
->modifiers() & Qt::ShiftModifier
||
566 event
->modifiers() & Qt::ControlModifier
;
568 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
569 if (rubberBand
->isActive()) {
570 disconnect(rubberBand
, SIGNAL(endPositionChanged(QPointF
,QPointF
)), this, SLOT(slotRubberBandChanged()));
571 rubberBand
->setActive(false);
572 m_oldSelection
.clear();
573 m_view
->setAutoScroll(false);
576 const QPointF pos
= transform
.map(event
->pos());
577 const int index
= m_view
->itemAt(pos
);
579 if (index
>= 0 && index
== m_pressedIndex
) {
580 // The release event is done above the same item as the press event
582 if (event
->button() & Qt::LeftButton
) {
583 bool emitItemActivated
= true;
584 if (m_view
->isAboveExpansionToggle(index
, pos
)) {
585 const bool expanded
= m_model
->isExpanded(index
);
586 m_model
->setExpanded(index
, !expanded
);
588 emit
itemExpansionToggleClicked(index
);
589 emitItemActivated
= false;
590 } else if (shiftOrControlPressed
) {
591 // The mouse click should only update the selection, not trigger the item
592 emitItemActivated
= false;
593 } else if (!m_singleClickActivation
) {
594 emitItemActivated
= false;
596 if (emitItemActivated
) {
597 emit
itemActivated(index
);
599 } else if (event
->button() & Qt::MidButton
) {
600 emit
itemMiddleClicked(index
);
604 m_pressedMousePos
= QPointF();
609 bool KItemListController::mouseDoubleClickEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
611 const QPointF pos
= transform
.map(event
->pos());
612 const int index
= m_view
->itemAt(pos
);
614 bool emitItemActivated
= !m_singleClickActivation
&&
615 (event
->button() & Qt::LeftButton
) &&
616 index
>= 0 && index
< m_model
->count();
617 if (emitItemActivated
) {
618 emit
itemActivated(index
);
623 bool KItemListController::dragEnterEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
630 bool KItemListController::dragLeaveEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
637 bool KItemListController::dragMoveEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
640 if (!m_model
|| !m_view
) {
644 KItemListWidget
* oldHoveredWidget
= hoveredWidget();
646 const QPointF pos
= transform
.map(event
->pos());
647 KItemListWidget
* newHoveredWidget
= widgetForPos(pos
);
649 if (oldHoveredWidget
!= newHoveredWidget
) {
650 m_autoActivationTimer
->stop();
652 if (oldHoveredWidget
) {
653 oldHoveredWidget
->setHovered(false);
654 emit
itemUnhovered(oldHoveredWidget
->index());
657 if (newHoveredWidget
) {
658 const int index
= newHoveredWidget
->index();
659 if (m_model
->supportsDropping(index
)) {
660 newHoveredWidget
->setHovered(true);
662 emit
itemHovered(index
);
664 if (m_autoActivationTimer
->interval() >= 0) {
665 m_autoActivationTimer
->setProperty("index", index
);
666 m_autoActivationTimer
->start();
674 bool KItemListController::dropEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
681 m_autoActivationTimer
->stop();
683 const QPointF pos
= transform
.map(event
->pos());
684 const int index
= m_view
->itemAt(pos
);
685 emit
itemDropEvent(index
, event
);
690 bool KItemListController::hoverEnterEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
697 bool KItemListController::hoverMoveEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
700 if (!m_model
|| !m_view
) {
704 KItemListWidget
* oldHoveredWidget
= hoveredWidget();
705 const QPointF pos
= transform
.map(event
->pos());
706 KItemListWidget
* newHoveredWidget
= widgetForPos(pos
);
708 if (oldHoveredWidget
!= newHoveredWidget
) {
709 if (oldHoveredWidget
) {
710 oldHoveredWidget
->setHovered(false);
711 emit
itemUnhovered(oldHoveredWidget
->index());
714 if (newHoveredWidget
) {
715 newHoveredWidget
->setHovered(true);
716 emit
itemHovered(newHoveredWidget
->index());
723 bool KItemListController::hoverLeaveEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
728 if (!m_model
|| !m_view
) {
732 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
733 if (widget
->isHovered()) {
734 widget
->setHovered(false);
735 emit
itemUnhovered(widget
->index());
741 bool KItemListController::wheelEvent(QGraphicsSceneWheelEvent
* event
, const QTransform
& transform
)
748 bool KItemListController::resizeEvent(QGraphicsSceneResizeEvent
* event
, const QTransform
& transform
)
755 bool KItemListController::processEvent(QEvent
* event
, const QTransform
& transform
)
761 switch (event
->type()) {
762 case QEvent::KeyPress
:
763 return keyPressEvent(static_cast<QKeyEvent
*>(event
));
764 case QEvent::InputMethod
:
765 return inputMethodEvent(static_cast<QInputMethodEvent
*>(event
));
766 case QEvent::GraphicsSceneMousePress
:
767 return mousePressEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
768 case QEvent::GraphicsSceneMouseMove
:
769 return mouseMoveEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
770 case QEvent::GraphicsSceneMouseRelease
:
771 return mouseReleaseEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
772 case QEvent::GraphicsSceneMouseDoubleClick
:
773 return mouseDoubleClickEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
774 case QEvent::GraphicsSceneWheel
:
775 return wheelEvent(static_cast<QGraphicsSceneWheelEvent
*>(event
), QTransform());
776 case QEvent::GraphicsSceneDragEnter
:
777 return dragEnterEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
778 case QEvent::GraphicsSceneDragLeave
:
779 return dragLeaveEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
780 case QEvent::GraphicsSceneDragMove
:
781 return dragMoveEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
782 case QEvent::GraphicsSceneDrop
:
783 return dropEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
784 case QEvent::GraphicsSceneHoverEnter
:
785 return hoverEnterEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
786 case QEvent::GraphicsSceneHoverMove
:
787 return hoverMoveEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
788 case QEvent::GraphicsSceneHoverLeave
:
789 return hoverLeaveEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
790 case QEvent::GraphicsSceneResize
:
791 return resizeEvent(static_cast<QGraphicsSceneResizeEvent
*>(event
), transform
);
799 void KItemListController::slotViewScrollOffsetChanged(qreal current
, qreal previous
)
805 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
806 if (rubberBand
->isActive()) {
807 const qreal diff
= current
- previous
;
808 // TODO: Ideally just QCursor::pos() should be used as
809 // new end-position but it seems there is no easy way
810 // to have something like QWidget::mapFromGlobal() for QGraphicsWidget
811 // (... or I just missed an easy way to do the mapping)
812 QPointF endPos
= rubberBand
->endPosition();
813 if (m_view
->scrollOrientation() == Qt::Vertical
) {
819 rubberBand
->setEndPosition(endPos
);
823 void KItemListController::slotRubberBandChanged()
825 if (!m_view
|| !m_model
|| m_model
->count() <= 0) {
829 const KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
830 const QPointF startPos
= rubberBand
->startPosition();
831 const QPointF endPos
= rubberBand
->endPosition();
832 QRectF rubberBandRect
= QRectF(startPos
, endPos
).normalized();
834 const bool scrollVertical
= (m_view
->scrollOrientation() == Qt::Vertical
);
835 if (scrollVertical
) {
836 rubberBandRect
.translate(0, -m_view
->scrollOffset());
838 rubberBandRect
.translate(-m_view
->scrollOffset(), 0);
841 if (!m_oldSelection
.isEmpty()) {
842 // Clear the old selection that was available before the rubberband has
843 // been activated in case if no Shift- or Control-key are pressed
844 const bool shiftOrControlPressed
= QApplication::keyboardModifiers() & Qt::ShiftModifier
||
845 QApplication::keyboardModifiers() & Qt::ControlModifier
;
846 if (!shiftOrControlPressed
) {
847 m_oldSelection
.clear();
851 QSet
<int> selectedItems
;
853 // Select all visible items that intersect with the rubberband
854 foreach (const KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
855 const int index
= widget
->index();
857 const QRectF widgetRect
= m_view
->itemRect(index
);
858 if (widgetRect
.intersects(rubberBandRect
)) {
859 const QRectF iconRect
= widget
->iconRect().translated(widgetRect
.topLeft());
860 const QRectF textRect
= widget
->textRect().translated(widgetRect
.topLeft());
861 if (iconRect
.intersects(rubberBandRect
) || textRect
.intersects(rubberBandRect
)) {
862 selectedItems
.insert(index
);
867 // Select all invisible items that intersect with the rubberband. Instead of
868 // iterating all items only the area which might be touched by the rubberband
870 const bool increaseIndex
= scrollVertical
?
871 startPos
.y() > endPos
.y(): startPos
.x() > endPos
.x();
873 int index
= increaseIndex
? m_view
->lastVisibleIndex() + 1 : m_view
->firstVisibleIndex() - 1;
874 bool selectionFinished
= false;
876 const QRectF widgetRect
= m_view
->itemRect(index
);
877 if (widgetRect
.intersects(rubberBandRect
)) {
878 selectedItems
.insert(index
);
883 selectionFinished
= (index
>= m_model
->count()) ||
884 ( scrollVertical
&& widgetRect
.top() > rubberBandRect
.bottom()) ||
885 (!scrollVertical
&& widgetRect
.left() > rubberBandRect
.right());
888 selectionFinished
= (index
< 0) ||
889 ( scrollVertical
&& widgetRect
.bottom() < rubberBandRect
.top()) ||
890 (!scrollVertical
&& widgetRect
.right() < rubberBandRect
.left());
892 } while (!selectionFinished
);
894 if (QApplication::keyboardModifiers() & Qt::ControlModifier
) {
895 // If Control is pressed, the selection state of all items in the rubberband is toggled.
896 // Therefore, the new selection contains:
897 // 1. All previously selected items which are not inside the rubberband, and
898 // 2. all items inside the rubberband which have not been selected previously.
899 m_selectionManager
->setSelectedItems((m_oldSelection
- selectedItems
) + (selectedItems
- m_oldSelection
));
902 m_selectionManager
->setSelectedItems(selectedItems
+ m_oldSelection
);
906 void KItemListController::startDragging()
908 if (!m_view
|| !m_model
) {
912 const QSet
<int> selectedItems
= m_selectionManager
->selectedItems();
913 if (selectedItems
.isEmpty()) {
917 QMimeData
* data
= m_model
->createMimeData(selectedItems
);
922 // The created drag object will be owned and deleted
923 // by QApplication::activeWindow().
924 QDrag
* drag
= new QDrag(QApplication::activeWindow());
925 drag
->setMimeData(data
);
927 const QPixmap pixmap
= m_view
->createDragPixmap(selectedItems
);
928 drag
->setPixmap(pixmap
);
930 drag
->exec(Qt::MoveAction
| Qt::CopyAction
| Qt::LinkAction
, Qt::CopyAction
);
933 KItemListWidget
* KItemListController::hoveredWidget() const
937 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
938 if (widget
->isHovered()) {
946 KItemListWidget
* KItemListController::widgetForPos(const QPointF
& pos
) const
950 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
951 const QPointF mappedPos
= widget
->mapFromItem(m_view
, pos
);
953 const bool hovered
= widget
->contains(mappedPos
) &&
954 !widget
->expansionToggleRect().contains(mappedPos
);
963 void KItemListController::updateKeyboardAnchor()
965 const bool validAnchor
= m_keyboardAnchorIndex
>= 0 &&
966 m_keyboardAnchorIndex
< m_model
->count() &&
967 keyboardAnchorPos(m_keyboardAnchorIndex
) == m_keyboardAnchorPos
;
969 const int index
= m_selectionManager
->currentItem();
970 m_keyboardAnchorIndex
= index
;
971 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
975 int KItemListController::nextRowIndex() const
977 const int currentIndex
= m_selectionManager
->currentItem();
978 if (m_keyboardAnchorIndex
< 0) {
982 const int maxIndex
= m_model
->count() - 1;
983 if (currentIndex
== maxIndex
) {
987 // Calculate the index of the last column inside the row of the current index
988 int lastColumnIndex
= currentIndex
;
989 while (keyboardAnchorPos(lastColumnIndex
+ 1) > keyboardAnchorPos(lastColumnIndex
)) {
991 if (lastColumnIndex
>= maxIndex
) {
996 // Based on the last column index go to the next row and calculate the nearest index
997 // that is below the current index
998 int nextRowIndex
= lastColumnIndex
+ 1;
999 int searchIndex
= nextRowIndex
;
1000 qreal minDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(nextRowIndex
));
1001 while (searchIndex
< maxIndex
&& keyboardAnchorPos(searchIndex
+ 1) > keyboardAnchorPos(searchIndex
)) {
1003 const qreal searchDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(searchIndex
));
1004 if (searchDiff
< minDiff
) {
1005 minDiff
= searchDiff
;
1006 nextRowIndex
= searchIndex
;
1010 return nextRowIndex
;
1013 int KItemListController::previousRowIndex() const
1015 const int currentIndex
= m_selectionManager
->currentItem();
1016 if (m_keyboardAnchorIndex
< 0 || currentIndex
== 0) {
1017 return currentIndex
;
1020 // Calculate the index of the first column inside the row of the current index
1021 int firstColumnIndex
= currentIndex
;
1022 while (keyboardAnchorPos(firstColumnIndex
- 1) < keyboardAnchorPos(firstColumnIndex
)) {
1024 if (firstColumnIndex
<= 0) {
1025 return currentIndex
;
1029 // Based on the first column index go to the previous row and calculate the nearest index
1030 // that is above the current index
1031 int previousRowIndex
= firstColumnIndex
- 1;
1032 int searchIndex
= previousRowIndex
;
1033 qreal minDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(previousRowIndex
));
1034 while (searchIndex
> 0 && keyboardAnchorPos(searchIndex
- 1) < keyboardAnchorPos(searchIndex
)) {
1036 const qreal searchDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(searchIndex
));
1037 if (searchDiff
< minDiff
) {
1038 minDiff
= searchDiff
;
1039 previousRowIndex
= searchIndex
;
1043 return previousRowIndex
;
1046 qreal
KItemListController::keyboardAnchorPos(int index
) const
1048 const QRectF itemRect
= m_view
->itemRect(index
);
1049 if (!itemRect
.isEmpty()) {
1050 return (m_view
->scrollOrientation() == Qt::Vertical
) ? itemRect
.x() : itemRect
.y();
1056 #include "kitemlistcontroller.moc"