1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
3 * Copyright (C) 2012 by Frank Reininghaus <frank78ac@googlemail.com> *
5 * Based on the Itemviews NG project from Trolltech Labs: *
6 * http://qt.gitorious.org/qt-labs/itemviews-ng *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, write to the *
20 * Free Software Foundation, Inc., *
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
22 ***************************************************************************/
24 #include "kitemlistcontroller.h"
26 #include <KGlobalSettings>
29 #include "kitemlistview.h"
30 #include "kitemlistselectionmanager.h"
32 #include "private/kitemlistrubberband.h"
33 #include "private/kitemlistkeyboardsearchmanager.h"
35 #include <QApplication>
38 #include <QGraphicsScene>
39 #include <QGraphicsSceneEvent>
40 #include <QGraphicsView>
44 KItemListController::KItemListController(KItemModelBase
* model
, KItemListView
* view
, QObject
* parent
) :
46 m_singleClickActivation(KGlobalSettings::singleClick()),
47 m_selectionTogglePressed(false),
48 m_clearSelectionIfItemsAreNotDragged(false),
49 m_selectionBehavior(NoSelection
),
52 m_selectionManager(new KItemListSelectionManager(this)),
53 m_keyboardManager(new KItemListKeyboardSearchManager(this)),
56 m_autoActivationTimer(0),
58 m_keyboardAnchorIndex(-1),
59 m_keyboardAnchorPos(0)
61 connect(m_keyboardManager
, SIGNAL(changeCurrentItem(QString
,bool)),
62 this, SLOT(slotChangeCurrentItem(QString
,bool)));
64 m_autoActivationTimer
= new QTimer(this);
65 m_autoActivationTimer
->setSingleShot(true);
66 m_autoActivationTimer
->setInterval(-1);
67 connect(m_autoActivationTimer
, SIGNAL(timeout()), this, SLOT(slotAutoActivationTimeout()));
73 KItemListController::~KItemListController()
84 void KItemListController::setModel(KItemModelBase
* model
)
86 if (m_model
== model
) {
90 KItemModelBase
* oldModel
= m_model
;
93 m_model
->setParent(this);
97 m_view
->setModel(m_model
);
100 m_selectionManager
->setModel(m_model
);
102 emit
modelChanged(m_model
, oldModel
);
105 KItemModelBase
* KItemListController::model() const
110 KItemListSelectionManager
* KItemListController::selectionManager() const
112 return m_selectionManager
;
115 void KItemListController::setView(KItemListView
* view
)
117 if (m_view
== view
) {
121 KItemListView
* oldView
= m_view
;
123 disconnect(oldView
, SIGNAL(scrollOffsetChanged(qreal
,qreal
)), this, SLOT(slotViewScrollOffsetChanged(qreal
,qreal
)));
129 m_view
->setController(this);
130 m_view
->setModel(m_model
);
131 connect(m_view
, SIGNAL(scrollOffsetChanged(qreal
,qreal
)), this, SLOT(slotViewScrollOffsetChanged(qreal
,qreal
)));
132 updateExtendedSelectionRegion();
135 emit
viewChanged(m_view
, oldView
);
138 KItemListView
* KItemListController::view() const
143 void KItemListController::setSelectionBehavior(SelectionBehavior behavior
)
145 m_selectionBehavior
= behavior
;
146 updateExtendedSelectionRegion();
149 KItemListController::SelectionBehavior
KItemListController::selectionBehavior() const
151 return m_selectionBehavior
;
154 void KItemListController::setAutoActivationDelay(int delay
)
156 m_autoActivationTimer
->setInterval(delay
);
159 int KItemListController::autoActivationDelay() const
161 return m_autoActivationTimer
->interval();
164 void KItemListController::setSingleClickActivation(bool singleClick
)
166 m_singleClickActivation
= singleClick
;
169 bool KItemListController::singleClickActivation() const
171 return m_singleClickActivation
;
174 bool KItemListController::showEvent(QShowEvent
* event
)
180 bool KItemListController::hideEvent(QHideEvent
* event
)
186 bool KItemListController::keyPressEvent(QKeyEvent
* event
)
188 int index
= m_selectionManager
->currentItem();
189 int key
= event
->key();
191 // Handle the expanding/collapsing of items
192 if (m_view
->supportsItemExpanding() && m_model
->isExpandable(index
)) {
193 if (key
== Qt::Key_Right
) {
194 if (m_model
->setExpanded(index
, true)) {
197 } else if (key
== Qt::Key_Left
) {
198 if (m_model
->setExpanded(index
, false)) {
204 const bool shiftPressed
= event
->modifiers() & Qt::ShiftModifier
;
205 const bool controlPressed
= event
->modifiers() & Qt::ControlModifier
;
206 const bool shiftOrControlPressed
= shiftPressed
|| controlPressed
;
208 const int itemCount
= m_model
->count();
210 // For horizontal scroll orientation, transform
211 // the arrow keys to simplify the event handling.
212 if (m_view
->scrollOrientation() == Qt::Horizontal
) {
214 case Qt::Key_Up
: key
= Qt::Key_Left
; break;
215 case Qt::Key_Down
: key
= Qt::Key_Right
; break;
216 case Qt::Key_Left
: key
= Qt::Key_Up
; break;
217 case Qt::Key_Right
: key
= Qt::Key_Down
; break;
222 const bool selectSingleItem
= m_selectionBehavior
!= NoSelection
&&
224 (key
== Qt::Key_Home
|| key
== Qt::Key_End
||
225 key
== Qt::Key_Up
|| key
== Qt::Key_Down
||
226 key
== Qt::Key_Left
|| key
== Qt::Key_Right
);
227 if (selectSingleItem
) {
228 const int current
= m_selectionManager
->currentItem();
229 m_selectionManager
->setSelected(current
);
236 m_keyboardAnchorIndex
= index
;
237 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
241 index
= itemCount
- 1;
242 m_keyboardAnchorIndex
= index
;
243 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
249 m_keyboardAnchorIndex
= index
;
250 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
255 if (index
< itemCount
- 1) {
257 m_keyboardAnchorIndex
= index
;
258 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
263 updateKeyboardAnchor();
264 index
= previousRowIndex(index
);
268 updateKeyboardAnchor();
269 index
= nextRowIndex(index
);
273 if (m_view
->scrollOrientation() == Qt::Horizontal
) {
274 // The new current index should correspond to the first item in the current column.
275 int newIndex
= qMax(index
- 1, 0);
276 while (newIndex
!= index
&& m_view
->itemRect(newIndex
).topLeft().y() < m_view
->itemRect(index
).topLeft().y()) {
278 newIndex
= qMax(index
- 1, 0);
280 m_keyboardAnchorIndex
= index
;
281 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
283 const qreal currentItemBottom
= m_view
->itemRect(index
).bottomLeft().y();
284 const qreal height
= m_view
->geometry().height();
286 // The new current item should be the first item in the current
287 // column whose itemRect's top coordinate is larger than targetY.
288 const qreal targetY
= currentItemBottom
- height
;
290 updateKeyboardAnchor();
291 int newIndex
= previousRowIndex(index
);
294 updateKeyboardAnchor();
295 newIndex
= previousRowIndex(index
);
296 } while (m_view
->itemRect(newIndex
).topLeft().y() > targetY
&& newIndex
!= index
);
300 case Qt::Key_PageDown
:
301 if (m_view
->scrollOrientation() == Qt::Horizontal
) {
302 // The new current index should correspond to the last item in the current column.
303 int newIndex
= qMin(index
+ 1, m_model
->count() - 1);
304 while (newIndex
!= index
&& m_view
->itemRect(newIndex
).topLeft().y() > m_view
->itemRect(index
).topLeft().y()) {
306 newIndex
= qMin(index
+ 1, m_model
->count() - 1);
308 m_keyboardAnchorIndex
= index
;
309 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
311 const qreal currentItemTop
= m_view
->itemRect(index
).topLeft().y();
312 const qreal height
= m_view
->geometry().height();
314 // The new current item should be the last item in the current
315 // column whose itemRect's bottom coordinate is smaller than targetY.
316 const qreal targetY
= currentItemTop
+ height
;
318 updateKeyboardAnchor();
319 int newIndex
= nextRowIndex(index
);
322 updateKeyboardAnchor();
323 newIndex
= nextRowIndex(index
);
324 } while (m_view
->itemRect(newIndex
).bottomLeft().y() < targetY
&& newIndex
!= index
);
329 case Qt::Key_Return
: {
330 const QSet
<int> selectedItems
= m_selectionManager
->selectedItems();
331 if (selectedItems
.count() >= 2) {
332 emit
itemsActivated(selectedItems
);
333 } else if (selectedItems
.count() == 1) {
334 emit
itemActivated(selectedItems
.toList().first());
336 emit
itemActivated(index
);
342 if (m_selectionBehavior
== MultiSelection
) {
343 if (controlPressed
) {
344 m_selectionManager
->endAnchoredSelection();
345 m_selectionManager
->setSelected(index
, 1, KItemListSelectionManager::Toggle
);
346 m_selectionManager
->beginAnchoredSelection(index
);
348 const int current
= m_selectionManager
->currentItem();
349 m_selectionManager
->setSelected(current
);
355 // Emit the signal itemContextMenuRequested() in case if at least one
356 // item is selected. Otherwise the signal viewContextMenuRequested() will be emitted.
357 const QSet
<int> selectedItems
= m_selectionManager
->selectedItems();
359 if (selectedItems
.count() >= 2) {
360 const int currentItemIndex
= m_selectionManager
->currentItem();
361 index
= selectedItems
.contains(currentItemIndex
)
362 ? currentItemIndex
: selectedItems
.toList().first();
363 } else if (selectedItems
.count() == 1) {
364 index
= selectedItems
.toList().first();
368 const QRectF contextRect
= m_view
->itemContextRect(index
);
369 const QPointF
pos(m_view
->scene()->views().first()->mapToGlobal(contextRect
.bottomRight().toPoint()));
370 emit
itemContextMenuRequested(index
, pos
);
372 emit
viewContextMenuRequested(QCursor::pos());
378 m_keyboardManager
->addKeys(event
->text());
382 if (m_selectionManager
->currentItem() != index
) {
383 switch (m_selectionBehavior
) {
385 m_selectionManager
->setCurrentItem(index
);
388 case SingleSelection
:
389 m_selectionManager
->setCurrentItem(index
);
390 m_selectionManager
->clearSelection();
391 m_selectionManager
->setSelected(index
, 1);
395 if (controlPressed
) {
396 m_selectionManager
->endAnchoredSelection();
399 m_selectionManager
->setCurrentItem(index
);
401 if (!shiftOrControlPressed
) {
402 m_selectionManager
->clearSelection();
403 m_selectionManager
->setSelected(index
, 1);
407 m_selectionManager
->beginAnchoredSelection(index
);
412 m_view
->scrollToItem(index
);
417 void KItemListController::slotChangeCurrentItem(const QString
& text
, bool searchFromNextItem
)
419 if (!m_model
|| m_model
->count() == 0) {
422 const int currentIndex
= m_selectionManager
->currentItem();
424 if (searchFromNextItem
) {
425 index
= m_model
->indexForKeyboardSearch(text
, (currentIndex
+ 1) % m_model
->count());
427 index
= m_model
->indexForKeyboardSearch(text
, currentIndex
);
430 m_selectionManager
->setCurrentItem(index
);
431 m_selectionManager
->clearSelection();
432 m_selectionManager
->setSelected(index
, 1);
433 m_selectionManager
->beginAnchoredSelection(index
);
434 m_view
->scrollToItem(index
);
438 void KItemListController::slotAutoActivationTimeout()
440 if (!m_model
|| !m_view
) {
444 const int index
= m_autoActivationTimer
->property("index").toInt();
445 if (index
< 0 || index
>= m_model
->count()) {
449 if (m_model
->supportsDropping(index
)) {
450 if (m_view
->supportsItemExpanding() && m_model
->isExpandable(index
)) {
451 const bool expanded
= m_model
->isExpanded(index
);
452 m_model
->setExpanded(index
, !expanded
);
454 emit
itemActivated(index
);
459 bool KItemListController::inputMethodEvent(QInputMethodEvent
* event
)
465 bool KItemListController::mousePressEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
471 m_pressedMousePos
= transform
.map(event
->pos());
472 m_pressedIndex
= m_view
->itemAt(m_pressedMousePos
);
473 emit
mouseButtonPressed(m_pressedIndex
, event
->buttons());
475 if (m_view
->isAboveExpansionToggle(m_pressedIndex
, m_pressedMousePos
)) {
476 m_selectionManager
->endAnchoredSelection();
477 m_selectionManager
->setCurrentItem(m_pressedIndex
);
478 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
482 m_selectionTogglePressed
= m_view
->isAboveSelectionToggle(m_pressedIndex
, m_pressedMousePos
);
483 if (m_selectionTogglePressed
) {
484 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
485 // The previous anchored selection has been finished already in
486 // KItemListSelectionManager::setSelected(). We can safely change
487 // the current item and start a new anchored selection now.
488 m_selectionManager
->setCurrentItem(m_pressedIndex
);
489 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
493 const bool shiftPressed
= event
->modifiers() & Qt::ShiftModifier
;
494 const bool controlPressed
= event
->modifiers() & Qt::ControlModifier
;
496 // The previous selection is cleared if either
497 // 1. The selection mode is SingleSelection, or
498 // 2. the selection mode is MultiSelection, and *none* of the following conditions are met:
499 // a) Shift or Control are pressed.
500 // b) The clicked item is selected already. In that case, the user might want to:
501 // - start dragging multiple items, or
502 // - open the context menu and perform an action for all selected items.
503 const bool shiftOrControlPressed
= shiftPressed
|| controlPressed
;
504 const bool pressedItemAlreadySelected
= m_pressedIndex
>= 0 && m_selectionManager
->isSelected(m_pressedIndex
);
505 const bool clearSelection
= m_selectionBehavior
== SingleSelection
||
506 (!shiftOrControlPressed
&& !pressedItemAlreadySelected
);
507 if (clearSelection
) {
508 m_selectionManager
->clearSelection();
509 } else if (pressedItemAlreadySelected
&& !shiftOrControlPressed
&& (event
->buttons() & Qt::LeftButton
)) {
510 // The user might want to start dragging multiple items, but if he clicks the item
511 // in order to trigger it instead, the other selected items must be deselected.
512 // However, we do not know yet what the user is going to do.
513 // -> remember that the user pressed an item which had been selected already and
514 // clear the selection in mouseReleaseEvent(), unless the items are dragged.
515 m_clearSelectionIfItemsAreNotDragged
= true;
519 // Finish the anchored selection before the current index is changed
520 m_selectionManager
->endAnchoredSelection();
523 if (m_pressedIndex
>= 0) {
524 m_selectionManager
->setCurrentItem(m_pressedIndex
);
526 switch (m_selectionBehavior
) {
530 case SingleSelection
:
531 m_selectionManager
->setSelected(m_pressedIndex
);
535 if (controlPressed
) {
536 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
537 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
538 } else if (!shiftPressed
|| !m_selectionManager
->isAnchoredSelectionActive()) {
539 // Select the pressed item and start a new anchored selection
540 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Select
);
541 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
550 if (event
->buttons() & Qt::RightButton
) {
551 emit
itemContextMenuRequested(m_pressedIndex
, event
->screenPos());
557 if (event
->buttons() & Qt::RightButton
) {
558 const QRectF headerBounds
= m_view
->headerBoundaries();
559 if (headerBounds
.contains(event
->pos())) {
560 emit
headerContextMenuRequested(event
->screenPos());
562 emit
viewContextMenuRequested(event
->screenPos());
567 if (m_selectionBehavior
== MultiSelection
) {
568 QPointF startPos
= m_pressedMousePos
;
569 if (m_view
->scrollOrientation() == Qt::Vertical
) {
570 startPos
.ry() += m_view
->scrollOffset();
571 if (m_view
->itemSize().width() < 0) {
572 // Use a special rubberband for views that have only one column and
573 // expand the rubberband to use the whole width of the view.
577 startPos
.rx() += m_view
->scrollOffset();
580 m_oldSelection
= m_selectionManager
->selectedItems();
581 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
582 rubberBand
->setStartPosition(startPos
);
583 rubberBand
->setEndPosition(startPos
);
584 rubberBand
->setActive(true);
585 connect(rubberBand
, SIGNAL(endPositionChanged(QPointF
,QPointF
)), this, SLOT(slotRubberBandChanged()));
586 m_view
->setAutoScroll(true);
592 bool KItemListController::mouseMoveEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
598 if (m_pressedIndex
>= 0) {
599 // Check whether a dragging should be started
600 if (event
->buttons() & Qt::LeftButton
) {
601 const QPointF pos
= transform
.map(event
->pos());
602 if ((pos
- m_pressedMousePos
).manhattanLength() >= QApplication::startDragDistance()) {
603 if (!m_selectionManager
->isSelected(m_pressedIndex
)) {
604 // Always assure that the dragged item gets selected. Usually this is already
605 // done on the mouse-press event, but when using the selection-toggle on a
606 // selected item the dragged item is not selected yet.
607 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
609 // A selected item has been clicked to drag all selected items
610 // -> the selection should not be cleared when the mouse button is released.
611 m_clearSelectionIfItemsAreNotDragged
= false;
618 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
619 if (rubberBand
->isActive()) {
620 QPointF endPos
= transform
.map(event
->pos());
622 // Update the current item.
623 const int newCurrent
= m_view
->itemAt(endPos
);
624 if (newCurrent
>= 0) {
625 // It's expected that the new current index is also the new anchor (bug 163451).
626 m_selectionManager
->endAnchoredSelection();
627 m_selectionManager
->setCurrentItem(newCurrent
);
628 m_selectionManager
->beginAnchoredSelection(newCurrent
);
631 if (m_view
->scrollOrientation() == Qt::Vertical
) {
632 endPos
.ry() += m_view
->scrollOffset();
633 if (m_view
->itemSize().width() < 0) {
634 // Use a special rubberband for views that have only one column and
635 // expand the rubberband to use the whole width of the view.
636 endPos
.setX(m_view
->size().width());
639 endPos
.rx() += m_view
->scrollOffset();
641 rubberBand
->setEndPosition(endPos
);
648 bool KItemListController::mouseReleaseEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
654 emit
mouseButtonReleased(m_pressedIndex
, event
->buttons());
656 const bool isAboveSelectionToggle
= m_view
->isAboveSelectionToggle(m_pressedIndex
, m_pressedMousePos
);
657 if (isAboveSelectionToggle
) {
658 m_selectionTogglePressed
= false;
662 if (!isAboveSelectionToggle
&& m_selectionTogglePressed
) {
663 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
664 m_selectionTogglePressed
= false;
668 const bool shiftOrControlPressed
= event
->modifiers() & Qt::ShiftModifier
||
669 event
->modifiers() & Qt::ControlModifier
;
671 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
672 if (rubberBand
->isActive()) {
673 disconnect(rubberBand
, SIGNAL(endPositionChanged(QPointF
,QPointF
)), this, SLOT(slotRubberBandChanged()));
674 rubberBand
->setActive(false);
675 m_oldSelection
.clear();
676 m_view
->setAutoScroll(false);
679 const QPointF pos
= transform
.map(event
->pos());
680 const int index
= m_view
->itemAt(pos
);
682 if (index
>= 0 && index
== m_pressedIndex
) {
683 // The release event is done above the same item as the press event
685 if (m_clearSelectionIfItemsAreNotDragged
) {
686 // A selected item has been clicked, but no drag operation has been started
687 // -> clear the rest of the selection.
688 m_selectionManager
->clearSelection();
689 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Select
);
690 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
693 if (event
->button() & Qt::LeftButton
) {
694 bool emitItemActivated
= true;
695 if (m_view
->isAboveExpansionToggle(index
, pos
)) {
696 const bool expanded
= m_model
->isExpanded(index
);
697 m_model
->setExpanded(index
, !expanded
);
699 emit
itemExpansionToggleClicked(index
);
700 emitItemActivated
= false;
701 } else if (shiftOrControlPressed
) {
702 // The mouse click should only update the selection, not trigger the item
703 emitItemActivated
= false;
704 } else if (!m_singleClickActivation
) {
705 emitItemActivated
= false;
707 if (emitItemActivated
) {
708 emit
itemActivated(index
);
710 } else if (event
->button() & Qt::MidButton
) {
711 emit
itemMiddleClicked(index
);
715 m_pressedMousePos
= QPointF();
717 m_clearSelectionIfItemsAreNotDragged
= false;
721 bool KItemListController::mouseDoubleClickEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
723 const QPointF pos
= transform
.map(event
->pos());
724 const int index
= m_view
->itemAt(pos
);
726 bool emitItemActivated
= !m_singleClickActivation
&&
727 (event
->button() & Qt::LeftButton
) &&
728 index
>= 0 && index
< m_model
->count();
729 if (emitItemActivated
) {
730 emit
itemActivated(index
);
735 bool KItemListController::dragEnterEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
742 bool KItemListController::dragLeaveEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
747 m_view
->setAutoScroll(false);
749 KItemListWidget
* widget
= hoveredWidget();
751 widget
->setHovered(false);
752 emit
itemUnhovered(widget
->index());
757 bool KItemListController::dragMoveEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
760 if (!m_model
|| !m_view
) {
764 event
->acceptProposedAction();
766 KItemListWidget
* oldHoveredWidget
= hoveredWidget();
768 const QPointF pos
= transform
.map(event
->pos());
769 KItemListWidget
* newHoveredWidget
= widgetForPos(pos
);
771 if (oldHoveredWidget
!= newHoveredWidget
) {
772 m_autoActivationTimer
->stop();
774 if (oldHoveredWidget
) {
775 oldHoveredWidget
->setHovered(false);
776 emit
itemUnhovered(oldHoveredWidget
->index());
779 if (newHoveredWidget
) {
780 const int index
= newHoveredWidget
->index();
781 if (m_model
->supportsDropping(index
)) {
782 newHoveredWidget
->setHovered(true);
784 emit
itemHovered(index
);
786 if (m_autoActivationTimer
->interval() >= 0) {
787 m_autoActivationTimer
->setProperty("index", index
);
788 m_autoActivationTimer
->start();
796 bool KItemListController::dropEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
803 m_autoActivationTimer
->stop();
804 m_view
->setAutoScroll(false);
806 const QPointF pos
= transform
.map(event
->pos());
807 const int index
= m_view
->itemAt(pos
);
808 emit
itemDropEvent(index
, event
);
813 bool KItemListController::hoverEnterEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
820 bool KItemListController::hoverMoveEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
823 if (!m_model
|| !m_view
) {
827 KItemListWidget
* oldHoveredWidget
= hoveredWidget();
828 const QPointF pos
= transform
.map(event
->pos());
829 KItemListWidget
* newHoveredWidget
= widgetForPos(pos
);
831 if (oldHoveredWidget
!= newHoveredWidget
) {
832 if (oldHoveredWidget
) {
833 oldHoveredWidget
->setHovered(false);
834 emit
itemUnhovered(oldHoveredWidget
->index());
837 if (newHoveredWidget
) {
838 newHoveredWidget
->setHovered(true);
839 emit
itemHovered(newHoveredWidget
->index());
846 bool KItemListController::hoverLeaveEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
851 if (!m_model
|| !m_view
) {
855 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
856 if (widget
->isHovered()) {
857 widget
->setHovered(false);
858 emit
itemUnhovered(widget
->index());
864 bool KItemListController::wheelEvent(QGraphicsSceneWheelEvent
* event
, const QTransform
& transform
)
871 bool KItemListController::resizeEvent(QGraphicsSceneResizeEvent
* event
, const QTransform
& transform
)
878 bool KItemListController::processEvent(QEvent
* event
, const QTransform
& transform
)
884 switch (event
->type()) {
885 case QEvent::KeyPress
:
886 return keyPressEvent(static_cast<QKeyEvent
*>(event
));
887 case QEvent::InputMethod
:
888 return inputMethodEvent(static_cast<QInputMethodEvent
*>(event
));
889 case QEvent::GraphicsSceneMousePress
:
890 return mousePressEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
891 case QEvent::GraphicsSceneMouseMove
:
892 return mouseMoveEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
893 case QEvent::GraphicsSceneMouseRelease
:
894 return mouseReleaseEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
895 case QEvent::GraphicsSceneMouseDoubleClick
:
896 return mouseDoubleClickEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
897 case QEvent::GraphicsSceneWheel
:
898 return wheelEvent(static_cast<QGraphicsSceneWheelEvent
*>(event
), QTransform());
899 case QEvent::GraphicsSceneDragEnter
:
900 return dragEnterEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
901 case QEvent::GraphicsSceneDragLeave
:
902 return dragLeaveEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
903 case QEvent::GraphicsSceneDragMove
:
904 return dragMoveEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
905 case QEvent::GraphicsSceneDrop
:
906 return dropEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
907 case QEvent::GraphicsSceneHoverEnter
:
908 return hoverEnterEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
909 case QEvent::GraphicsSceneHoverMove
:
910 return hoverMoveEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
911 case QEvent::GraphicsSceneHoverLeave
:
912 return hoverLeaveEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
913 case QEvent::GraphicsSceneResize
:
914 return resizeEvent(static_cast<QGraphicsSceneResizeEvent
*>(event
), transform
);
922 void KItemListController::slotViewScrollOffsetChanged(qreal current
, qreal previous
)
928 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
929 if (rubberBand
->isActive()) {
930 const qreal diff
= current
- previous
;
931 // TODO: Ideally just QCursor::pos() should be used as
932 // new end-position but it seems there is no easy way
933 // to have something like QWidget::mapFromGlobal() for QGraphicsWidget
934 // (... or I just missed an easy way to do the mapping)
935 QPointF endPos
= rubberBand
->endPosition();
936 if (m_view
->scrollOrientation() == Qt::Vertical
) {
942 rubberBand
->setEndPosition(endPos
);
946 void KItemListController::slotRubberBandChanged()
948 if (!m_view
|| !m_model
|| m_model
->count() <= 0) {
952 const KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
953 const QPointF startPos
= rubberBand
->startPosition();
954 const QPointF endPos
= rubberBand
->endPosition();
955 QRectF rubberBandRect
= QRectF(startPos
, endPos
).normalized();
957 const bool scrollVertical
= (m_view
->scrollOrientation() == Qt::Vertical
);
958 if (scrollVertical
) {
959 rubberBandRect
.translate(0, -m_view
->scrollOffset());
961 rubberBandRect
.translate(-m_view
->scrollOffset(), 0);
964 if (!m_oldSelection
.isEmpty()) {
965 // Clear the old selection that was available before the rubberband has
966 // been activated in case if no Shift- or Control-key are pressed
967 const bool shiftOrControlPressed
= QApplication::keyboardModifiers() & Qt::ShiftModifier
||
968 QApplication::keyboardModifiers() & Qt::ControlModifier
;
969 if (!shiftOrControlPressed
) {
970 m_oldSelection
.clear();
974 QSet
<int> selectedItems
;
976 // Select all visible items that intersect with the rubberband
977 foreach (const KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
978 const int index
= widget
->index();
980 const QRectF widgetRect
= m_view
->itemRect(index
);
981 if (widgetRect
.intersects(rubberBandRect
)) {
982 const QRectF iconRect
= widget
->iconRect().translated(widgetRect
.topLeft());
983 const QRectF textRect
= widget
->textRect().translated(widgetRect
.topLeft());
984 if (iconRect
.intersects(rubberBandRect
) || textRect
.intersects(rubberBandRect
)) {
985 selectedItems
.insert(index
);
990 // Select all invisible items that intersect with the rubberband. Instead of
991 // iterating all items only the area which might be touched by the rubberband
993 const bool increaseIndex
= scrollVertical
?
994 startPos
.y() > endPos
.y(): startPos
.x() > endPos
.x();
996 int index
= increaseIndex
? m_view
->lastVisibleIndex() + 1 : m_view
->firstVisibleIndex() - 1;
997 bool selectionFinished
= false;
999 const QRectF widgetRect
= m_view
->itemRect(index
);
1000 if (widgetRect
.intersects(rubberBandRect
)) {
1001 selectedItems
.insert(index
);
1004 if (increaseIndex
) {
1006 selectionFinished
= (index
>= m_model
->count()) ||
1007 ( scrollVertical
&& widgetRect
.top() > rubberBandRect
.bottom()) ||
1008 (!scrollVertical
&& widgetRect
.left() > rubberBandRect
.right());
1011 selectionFinished
= (index
< 0) ||
1012 ( scrollVertical
&& widgetRect
.bottom() < rubberBandRect
.top()) ||
1013 (!scrollVertical
&& widgetRect
.right() < rubberBandRect
.left());
1015 } while (!selectionFinished
);
1017 if (QApplication::keyboardModifiers() & Qt::ControlModifier
) {
1018 // If Control is pressed, the selection state of all items in the rubberband is toggled.
1019 // Therefore, the new selection contains:
1020 // 1. All previously selected items which are not inside the rubberband, and
1021 // 2. all items inside the rubberband which have not been selected previously.
1022 m_selectionManager
->setSelectedItems((m_oldSelection
- selectedItems
) + (selectedItems
- m_oldSelection
));
1025 m_selectionManager
->setSelectedItems(selectedItems
+ m_oldSelection
);
1029 void KItemListController::startDragging()
1031 if (!m_view
|| !m_model
) {
1035 const QSet
<int> selectedItems
= m_selectionManager
->selectedItems();
1036 if (selectedItems
.isEmpty()) {
1040 QMimeData
* data
= m_model
->createMimeData(selectedItems
);
1045 // The created drag object will be owned and deleted
1046 // by QApplication::activeWindow().
1047 QDrag
* drag
= new QDrag(QApplication::activeWindow());
1048 drag
->setMimeData(data
);
1050 const QPixmap pixmap
= m_view
->createDragPixmap(selectedItems
);
1051 drag
->setPixmap(pixmap
);
1053 drag
->exec(Qt::MoveAction
| Qt::CopyAction
| Qt::LinkAction
, Qt::CopyAction
);
1056 KItemListWidget
* KItemListController::hoveredWidget() const
1060 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
1061 if (widget
->isHovered()) {
1069 KItemListWidget
* KItemListController::widgetForPos(const QPointF
& pos
) const
1073 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
1074 const QPointF mappedPos
= widget
->mapFromItem(m_view
, pos
);
1076 const bool hovered
= widget
->contains(mappedPos
) &&
1077 !widget
->expansionToggleRect().contains(mappedPos
);
1086 void KItemListController::updateKeyboardAnchor()
1088 const bool validAnchor
= m_keyboardAnchorIndex
>= 0 &&
1089 m_keyboardAnchorIndex
< m_model
->count() &&
1090 keyboardAnchorPos(m_keyboardAnchorIndex
) == m_keyboardAnchorPos
;
1092 const int index
= m_selectionManager
->currentItem();
1093 m_keyboardAnchorIndex
= index
;
1094 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
1098 int KItemListController::nextRowIndex(int index
) const
1100 if (m_keyboardAnchorIndex
< 0) {
1104 const int maxIndex
= m_model
->count() - 1;
1105 if (index
== maxIndex
) {
1109 // Calculate the index of the last column inside the row of the current index
1110 int lastColumnIndex
= index
;
1111 while (keyboardAnchorPos(lastColumnIndex
+ 1) > keyboardAnchorPos(lastColumnIndex
)) {
1113 if (lastColumnIndex
>= maxIndex
) {
1118 // Based on the last column index go to the next row and calculate the nearest index
1119 // that is below the current index
1120 int nextRowIndex
= lastColumnIndex
+ 1;
1121 int searchIndex
= nextRowIndex
;
1122 qreal minDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(nextRowIndex
));
1123 while (searchIndex
< maxIndex
&& keyboardAnchorPos(searchIndex
+ 1) > keyboardAnchorPos(searchIndex
)) {
1125 const qreal searchDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(searchIndex
));
1126 if (searchDiff
< minDiff
) {
1127 minDiff
= searchDiff
;
1128 nextRowIndex
= searchIndex
;
1132 return nextRowIndex
;
1135 int KItemListController::previousRowIndex(int index
) const
1137 if (m_keyboardAnchorIndex
< 0 || index
== 0) {
1141 // Calculate the index of the first column inside the row of the current index
1142 int firstColumnIndex
= index
;
1143 while (keyboardAnchorPos(firstColumnIndex
- 1) < keyboardAnchorPos(firstColumnIndex
)) {
1145 if (firstColumnIndex
<= 0) {
1150 // Based on the first column index go to the previous row and calculate the nearest index
1151 // that is above the current index
1152 int previousRowIndex
= firstColumnIndex
- 1;
1153 int searchIndex
= previousRowIndex
;
1154 qreal minDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(previousRowIndex
));
1155 while (searchIndex
> 0 && keyboardAnchorPos(searchIndex
- 1) < keyboardAnchorPos(searchIndex
)) {
1157 const qreal searchDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(searchIndex
));
1158 if (searchDiff
< minDiff
) {
1159 minDiff
= searchDiff
;
1160 previousRowIndex
= searchIndex
;
1164 return previousRowIndex
;
1167 qreal
KItemListController::keyboardAnchorPos(int index
) const
1169 const QRectF itemRect
= m_view
->itemRect(index
);
1170 if (!itemRect
.isEmpty()) {
1171 return (m_view
->scrollOrientation() == Qt::Vertical
) ? itemRect
.x() : itemRect
.y();
1177 void KItemListController::updateExtendedSelectionRegion()
1180 const bool extend
= (m_selectionBehavior
!= MultiSelection
);
1181 KItemListStyleOption option
= m_view
->styleOption();
1182 if (option
.extendedSelectionRegion
!= extend
) {
1183 option
.extendedSelectionRegion
= extend
;
1184 m_view
->setStyleOption(option
);
1189 #include "kitemlistcontroller.moc"