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 "kitemlistview.h"
27 #include "kitemlistrubberband_p.h"
28 #include "kitemlistselectionmanager.h"
29 #include "kitemlistkeyboardsearchmanager_p.h"
31 #include <QApplication>
34 #include <QGraphicsScene>
35 #include <QGraphicsSceneEvent>
36 #include <QGraphicsView>
40 #include <KGlobalSettings>
43 KItemListController::KItemListController(QObject
* parent
) :
45 m_singleClickActivation(KGlobalSettings::singleClick()),
46 m_selectionTogglePressed(false),
47 m_clearSelectionIfItemsAreNotDragged(false),
48 m_selectionBehavior(NoSelection
),
51 m_selectionManager(new KItemListSelectionManager(this)),
52 m_keyboardManager(new KItemListKeyboardSearchManager(this)),
55 m_autoActivationTimer(0),
57 m_keyboardAnchorIndex(-1),
58 m_keyboardAnchorPos(0)
60 connect(m_keyboardManager
, SIGNAL(changeCurrentItem(QString
,bool)),
61 this, SLOT(slotChangeCurrentItem(QString
,bool)));
63 m_autoActivationTimer
= new QTimer(this);
64 m_autoActivationTimer
->setSingleShot(true);
65 m_autoActivationTimer
->setInterval(-1);
66 connect(m_autoActivationTimer
, SIGNAL(timeout()), this, SLOT(slotAutoActivationTimeout()));
69 KItemListController::~KItemListController()
73 void KItemListController::setModel(KItemModelBase
* model
)
75 if (m_model
== model
) {
79 KItemModelBase
* oldModel
= m_model
;
83 m_view
->setModel(m_model
);
86 m_selectionManager
->setModel(m_model
);
88 emit
modelChanged(m_model
, oldModel
);
91 KItemModelBase
* KItemListController::model() const
96 KItemListSelectionManager
* KItemListController::selectionManager() const
98 return m_selectionManager
;
101 void KItemListController::setView(KItemListView
* view
)
103 if (m_view
== view
) {
107 KItemListView
* oldView
= m_view
;
109 disconnect(oldView
, SIGNAL(scrollOffsetChanged(qreal
,qreal
)), this, SLOT(slotViewScrollOffsetChanged(qreal
,qreal
)));
115 m_view
->setController(this);
116 m_view
->setModel(m_model
);
117 connect(m_view
, SIGNAL(scrollOffsetChanged(qreal
,qreal
)), this, SLOT(slotViewScrollOffsetChanged(qreal
,qreal
)));
120 emit
viewChanged(m_view
, oldView
);
123 KItemListView
* KItemListController::view() const
128 void KItemListController::setSelectionBehavior(SelectionBehavior behavior
)
130 m_selectionBehavior
= behavior
;
133 KItemListController::SelectionBehavior
KItemListController::selectionBehavior() const
135 return m_selectionBehavior
;
138 void KItemListController::setAutoActivationDelay(int delay
)
140 m_autoActivationTimer
->setInterval(delay
);
143 int KItemListController::autoActivationDelay() const
145 return m_autoActivationTimer
->interval();
148 void KItemListController::setSingleClickActivation(bool singleClick
)
150 m_singleClickActivation
= singleClick
;
153 bool KItemListController::singleClickActivation() const
155 return m_singleClickActivation
;
158 bool KItemListController::showEvent(QShowEvent
* event
)
164 bool KItemListController::hideEvent(QHideEvent
* event
)
170 bool KItemListController::keyPressEvent(QKeyEvent
* event
)
172 int index
= m_selectionManager
->currentItem();
173 int key
= event
->key();
175 // Handle the expanding/collapsing of items
176 if (m_view
->supportsItemExpanding() && m_model
->isExpandable(index
)) {
177 if (key
== Qt::Key_Right
) {
178 if (m_model
->setExpanded(index
, true)) {
181 } else if (key
== Qt::Key_Left
) {
182 if (m_model
->setExpanded(index
, false)) {
188 const bool shiftPressed
= event
->modifiers() & Qt::ShiftModifier
;
189 const bool controlPressed
= event
->modifiers() & Qt::ControlModifier
;
190 const bool shiftOrControlPressed
= shiftPressed
|| controlPressed
;
192 const int itemCount
= m_model
->count();
194 // For horizontal scroll orientation, transform
195 // the arrow keys to simplify the event handling.
196 if (m_view
->scrollOrientation() == Qt::Horizontal
) {
198 case Qt::Key_Up
: key
= Qt::Key_Left
; break;
199 case Qt::Key_Down
: key
= Qt::Key_Right
; break;
200 case Qt::Key_Left
: key
= Qt::Key_Up
; break;
201 case Qt::Key_Right
: key
= Qt::Key_Down
; break;
206 const bool selectSingleItem
= m_selectionBehavior
!= NoSelection
&&
208 (key
== Qt::Key_Home
|| key
== Qt::Key_End
||
209 key
== Qt::Key_Up
|| key
== Qt::Key_Down
||
210 key
== Qt::Key_Left
|| key
== Qt::Key_Right
);
211 if (selectSingleItem
) {
212 const int current
= m_selectionManager
->currentItem();
213 m_selectionManager
->setSelected(current
);
220 m_keyboardAnchorIndex
= index
;
221 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
225 index
= itemCount
- 1;
226 m_keyboardAnchorIndex
= index
;
227 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
233 m_keyboardAnchorIndex
= index
;
234 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
239 if (index
< itemCount
- 1) {
241 m_keyboardAnchorIndex
= index
;
242 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
247 updateKeyboardAnchor();
248 index
= previousRowIndex(index
);
252 updateKeyboardAnchor();
253 index
= nextRowIndex(index
);
257 if (m_view
->scrollOrientation() == Qt::Horizontal
) {
258 // The new current index should correspond to the first item in the current column.
259 int newIndex
= qMax(index
- 1, 0);
260 while (newIndex
!= index
&& m_view
->itemRect(newIndex
).topLeft().y() < m_view
->itemRect(index
).topLeft().y()) {
262 newIndex
= qMax(index
- 1, 0);
264 m_keyboardAnchorIndex
= index
;
265 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
267 const qreal currentItemBottom
= m_view
->itemRect(index
).bottomLeft().y();
268 const qreal height
= m_view
->geometry().height();
270 // The new current item should be the first item in the current
271 // column whose itemRect's top coordinate is larger than targetY.
272 const qreal targetY
= currentItemBottom
- height
;
274 updateKeyboardAnchor();
275 int newIndex
= previousRowIndex(index
);
278 updateKeyboardAnchor();
279 newIndex
= previousRowIndex(index
);
280 } while (m_view
->itemRect(newIndex
).topLeft().y() > targetY
&& newIndex
!= index
);
284 case Qt::Key_PageDown
:
285 if (m_view
->scrollOrientation() == Qt::Horizontal
) {
286 // The new current index should correspond to the last item in the current column.
287 int newIndex
= qMin(index
+ 1, m_model
->count() - 1);
288 while (newIndex
!= index
&& m_view
->itemRect(newIndex
).topLeft().y() > m_view
->itemRect(index
).topLeft().y()) {
290 newIndex
= qMin(index
+ 1, m_model
->count() - 1);
292 m_keyboardAnchorIndex
= index
;
293 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
295 const qreal currentItemTop
= m_view
->itemRect(index
).topLeft().y();
296 const qreal height
= m_view
->geometry().height();
298 // The new current item should be the last item in the current
299 // column whose itemRect's bottom coordinate is smaller than targetY.
300 const qreal targetY
= currentItemTop
+ height
;
302 updateKeyboardAnchor();
303 int newIndex
= nextRowIndex(index
);
306 updateKeyboardAnchor();
307 newIndex
= nextRowIndex(index
);
308 } while (m_view
->itemRect(newIndex
).bottomLeft().y() < targetY
&& newIndex
!= index
);
313 case Qt::Key_Return
: {
314 const QSet
<int> selectedItems
= m_selectionManager
->selectedItems();
315 if (selectedItems
.count() >= 2) {
316 emit
itemsActivated(selectedItems
);
317 } else if (selectedItems
.count() == 1) {
318 emit
itemActivated(selectedItems
.toList().first());
320 emit
itemActivated(index
);
326 if (m_selectionBehavior
== MultiSelection
) {
327 if (controlPressed
) {
328 m_selectionManager
->endAnchoredSelection();
329 m_selectionManager
->setSelected(index
, 1, KItemListSelectionManager::Toggle
);
330 m_selectionManager
->beginAnchoredSelection(index
);
332 const int current
= m_selectionManager
->currentItem();
333 m_selectionManager
->setSelected(current
);
339 // Emit the signal itemContextMenuRequested() in case if at least one
340 // item is selected. Otherwise the signal viewContextMenuRequested() will be emitted.
341 const QSet
<int> selectedItems
= m_selectionManager
->selectedItems();
343 if (selectedItems
.count() >= 2) {
344 const int currentItemIndex
= m_selectionManager
->currentItem();
345 index
= selectedItems
.contains(currentItemIndex
)
346 ? currentItemIndex
: selectedItems
.toList().first();
347 } else if (selectedItems
.count() == 1) {
348 index
= selectedItems
.toList().first();
352 const QRectF contextRect
= m_view
->itemContextRect(index
);
353 const QPointF
pos(m_view
->scene()->views().first()->mapToGlobal(contextRect
.bottomRight().toPoint()));
354 emit
itemContextMenuRequested(index
, pos
);
356 emit
viewContextMenuRequested(QCursor::pos());
362 m_keyboardManager
->addKeys(event
->text());
366 if (m_selectionManager
->currentItem() != index
) {
367 switch (m_selectionBehavior
) {
369 m_selectionManager
->setCurrentItem(index
);
372 case SingleSelection
:
373 m_selectionManager
->setCurrentItem(index
);
374 m_selectionManager
->clearSelection();
375 m_selectionManager
->setSelected(index
, 1);
379 if (controlPressed
) {
380 m_selectionManager
->endAnchoredSelection();
383 m_selectionManager
->setCurrentItem(index
);
385 if (!shiftOrControlPressed
) {
386 m_selectionManager
->clearSelection();
387 m_selectionManager
->setSelected(index
, 1);
391 m_selectionManager
->beginAnchoredSelection(index
);
396 m_view
->scrollToItem(index
);
401 void KItemListController::slotChangeCurrentItem(const QString
& text
, bool searchFromNextItem
)
403 if (!m_model
|| m_model
->count() == 0) {
406 const int currentIndex
= m_selectionManager
->currentItem();
408 if (searchFromNextItem
) {
409 index
= m_model
->indexForKeyboardSearch(text
, (currentIndex
+ 1) % m_model
->count());
411 index
= m_model
->indexForKeyboardSearch(text
, currentIndex
);
414 m_selectionManager
->setCurrentItem(index
);
415 m_selectionManager
->clearSelection();
416 m_selectionManager
->setSelected(index
, 1);
417 m_selectionManager
->beginAnchoredSelection(index
);
418 m_view
->scrollToItem(index
);
422 void KItemListController::slotAutoActivationTimeout()
424 if (!m_model
|| !m_view
) {
428 const int index
= m_autoActivationTimer
->property("index").toInt();
429 if (index
< 0 || index
>= m_model
->count()) {
433 if (m_model
->supportsDropping(index
)) {
434 if (m_view
->supportsItemExpanding() && m_model
->isExpandable(index
)) {
435 const bool expanded
= m_model
->isExpanded(index
);
436 m_model
->setExpanded(index
, !expanded
);
438 emit
itemActivated(index
);
443 bool KItemListController::inputMethodEvent(QInputMethodEvent
* event
)
449 bool KItemListController::mousePressEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
455 m_pressedMousePos
= transform
.map(event
->pos());
456 m_pressedIndex
= m_view
->itemAt(m_pressedMousePos
);
457 if (m_pressedIndex
>= 0) {
458 emit
itemPressed(m_pressedIndex
, event
->button());
461 if (m_view
->isAboveExpansionToggle(m_pressedIndex
, m_pressedMousePos
)) {
462 m_selectionManager
->endAnchoredSelection();
463 m_selectionManager
->setCurrentItem(m_pressedIndex
);
464 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
468 m_selectionTogglePressed
= m_view
->isAboveSelectionToggle(m_pressedIndex
, m_pressedMousePos
);
469 if (m_selectionTogglePressed
) {
470 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
471 // The previous anchored selection has been finished already in
472 // KItemListSelectionManager::setSelected(). We can safely change
473 // the current item and start a new anchored selection now.
474 m_selectionManager
->setCurrentItem(m_pressedIndex
);
475 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
479 const bool shiftPressed
= event
->modifiers() & Qt::ShiftModifier
;
480 const bool controlPressed
= event
->modifiers() & Qt::ControlModifier
;
482 // The previous selection is cleared if either
483 // 1. The selection mode is SingleSelection, or
484 // 2. the selection mode is MultiSelection, and *none* of the following conditions are met:
485 // a) Shift or Control are pressed.
486 // b) The clicked item is selected already. In that case, the user might want to:
487 // - start dragging multiple items, or
488 // - open the context menu and perform an action for all selected items.
489 const bool shiftOrControlPressed
= shiftPressed
|| controlPressed
;
490 const bool pressedItemAlreadySelected
= m_pressedIndex
>= 0 && m_selectionManager
->isSelected(m_pressedIndex
);
491 const bool clearSelection
= m_selectionBehavior
== SingleSelection
||
492 (!shiftOrControlPressed
&& !pressedItemAlreadySelected
);
493 if (clearSelection
) {
494 m_selectionManager
->clearSelection();
495 } else if (pressedItemAlreadySelected
&& !shiftOrControlPressed
&& (event
->buttons() & Qt::LeftButton
)) {
496 // The user might want to start dragging multiple items, but if he clicks the item
497 // in order to trigger it instead, the other selected items must be deselected.
498 // However, we do not know yet what the user is going to do.
499 // -> remember that the user pressed an item which had been selected already and
500 // clear the selection in mouseReleaseEvent(), unless the items are dragged.
501 m_clearSelectionIfItemsAreNotDragged
= true;
505 // Finish the anchored selection before the current index is changed
506 m_selectionManager
->endAnchoredSelection();
509 if (m_pressedIndex
>= 0) {
510 m_selectionManager
->setCurrentItem(m_pressedIndex
);
512 switch (m_selectionBehavior
) {
516 case SingleSelection
:
517 m_selectionManager
->setSelected(m_pressedIndex
);
521 if (controlPressed
) {
522 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
523 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
524 } else if (!shiftPressed
|| !m_selectionManager
->isAnchoredSelectionActive()) {
525 // Select the pressed item and start a new anchored selection
526 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Select
);
527 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
536 if (event
->buttons() & Qt::RightButton
) {
537 emit
itemContextMenuRequested(m_pressedIndex
, event
->screenPos());
543 if (event
->buttons() & Qt::RightButton
) {
544 const QRectF headerBounds
= m_view
->headerBoundaries();
545 if (headerBounds
.contains(event
->pos())) {
546 emit
headerContextMenuRequested(event
->screenPos());
548 emit
viewContextMenuRequested(event
->screenPos());
553 if (m_selectionBehavior
== MultiSelection
) {
554 QPointF startPos
= m_pressedMousePos
;
555 if (m_view
->scrollOrientation() == Qt::Vertical
) {
556 startPos
.ry() += m_view
->scrollOffset();
557 if (m_view
->itemSize().width() < 0) {
558 // Use a special rubberband for views that have only one column and
559 // expand the rubberband to use the whole width of the view.
563 startPos
.rx() += m_view
->scrollOffset();
566 m_oldSelection
= m_selectionManager
->selectedItems();
567 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
568 rubberBand
->setStartPosition(startPos
);
569 rubberBand
->setEndPosition(startPos
);
570 rubberBand
->setActive(true);
571 connect(rubberBand
, SIGNAL(endPositionChanged(QPointF
,QPointF
)), this, SLOT(slotRubberBandChanged()));
572 m_view
->setAutoScroll(true);
578 bool KItemListController::mouseMoveEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
584 if (m_pressedIndex
>= 0) {
585 // Check whether a dragging should be started
586 if (event
->buttons() & Qt::LeftButton
) {
587 const QPointF pos
= transform
.map(event
->pos());
588 if ((pos
- m_pressedMousePos
).manhattanLength() >= QApplication::startDragDistance()) {
589 if (!m_selectionManager
->isSelected(m_pressedIndex
)) {
590 // Always assure that the dragged item gets selected. Usually this is already
591 // done on the mouse-press event, but when using the selection-toggle on a
592 // selected item the dragged item is not selected yet.
593 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
595 // A selected item has been clicked to drag all selected items
596 // -> the selection should not be cleared when the mouse button is released.
597 m_clearSelectionIfItemsAreNotDragged
= false;
604 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
605 if (rubberBand
->isActive()) {
606 QPointF endPos
= transform
.map(event
->pos());
608 // Update the current item.
609 const int newCurrent
= m_view
->itemAt(endPos
);
610 if (newCurrent
>= 0) {
611 // It's expected that the new current index is also the new anchor (bug 163451).
612 m_selectionManager
->endAnchoredSelection();
613 m_selectionManager
->setCurrentItem(newCurrent
);
614 m_selectionManager
->beginAnchoredSelection(newCurrent
);
617 if (m_view
->scrollOrientation() == Qt::Vertical
) {
618 endPos
.ry() += m_view
->scrollOffset();
619 if (m_view
->itemSize().width() < 0) {
620 // Use a special rubberband for views that have only one column and
621 // expand the rubberband to use the whole width of the view.
622 endPos
.setX(m_view
->size().width());
625 endPos
.rx() += m_view
->scrollOffset();
627 rubberBand
->setEndPosition(endPos
);
634 bool KItemListController::mouseReleaseEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
640 if (m_pressedIndex
>= 0) {
641 emit
itemReleased(m_pressedIndex
, event
->button());
644 const bool isAboveSelectionToggle
= m_view
->isAboveSelectionToggle(m_pressedIndex
, m_pressedMousePos
);
645 if (isAboveSelectionToggle
) {
646 m_selectionTogglePressed
= false;
650 if (!isAboveSelectionToggle
&& m_selectionTogglePressed
) {
651 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
652 m_selectionTogglePressed
= false;
656 const bool shiftOrControlPressed
= event
->modifiers() & Qt::ShiftModifier
||
657 event
->modifiers() & Qt::ControlModifier
;
659 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
660 if (rubberBand
->isActive()) {
661 disconnect(rubberBand
, SIGNAL(endPositionChanged(QPointF
,QPointF
)), this, SLOT(slotRubberBandChanged()));
662 rubberBand
->setActive(false);
663 m_oldSelection
.clear();
664 m_view
->setAutoScroll(false);
667 const QPointF pos
= transform
.map(event
->pos());
668 const int index
= m_view
->itemAt(pos
);
670 if (index
>= 0 && index
== m_pressedIndex
) {
671 // The release event is done above the same item as the press event
673 if (m_clearSelectionIfItemsAreNotDragged
) {
674 // A selected item has been clicked, but no drag operation has been started
675 // -> clear the rest of the selection.
676 m_selectionManager
->clearSelection();
677 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Select
);
678 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
681 if (event
->button() & Qt::LeftButton
) {
682 bool emitItemActivated
= true;
683 if (m_view
->isAboveExpansionToggle(index
, pos
)) {
684 const bool expanded
= m_model
->isExpanded(index
);
685 m_model
->setExpanded(index
, !expanded
);
687 emit
itemExpansionToggleClicked(index
);
688 emitItemActivated
= false;
689 } else if (shiftOrControlPressed
) {
690 // The mouse click should only update the selection, not trigger the item
691 emitItemActivated
= false;
692 } else if (!m_singleClickActivation
) {
693 emitItemActivated
= false;
695 if (emitItemActivated
) {
696 emit
itemActivated(index
);
698 } else if (event
->button() & Qt::MidButton
) {
699 emit
itemMiddleClicked(index
);
703 m_pressedMousePos
= QPointF();
705 m_clearSelectionIfItemsAreNotDragged
= false;
709 bool KItemListController::mouseDoubleClickEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
711 const QPointF pos
= transform
.map(event
->pos());
712 const int index
= m_view
->itemAt(pos
);
714 bool emitItemActivated
= !m_singleClickActivation
&&
715 (event
->button() & Qt::LeftButton
) &&
716 index
>= 0 && index
< m_model
->count();
717 if (emitItemActivated
) {
718 emit
itemActivated(index
);
723 bool KItemListController::dragEnterEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
730 bool KItemListController::dragLeaveEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
735 KItemListWidget
* widget
= hoveredWidget();
737 widget
->setHovered(false);
738 emit
itemUnhovered(widget
->index());
743 bool KItemListController::dragMoveEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
746 if (!m_model
|| !m_view
) {
750 KItemListWidget
* oldHoveredWidget
= hoveredWidget();
752 const QPointF pos
= transform
.map(event
->pos());
753 KItemListWidget
* newHoveredWidget
= widgetForPos(pos
);
755 if (oldHoveredWidget
!= newHoveredWidget
) {
756 m_autoActivationTimer
->stop();
758 if (oldHoveredWidget
) {
759 oldHoveredWidget
->setHovered(false);
760 emit
itemUnhovered(oldHoveredWidget
->index());
763 if (newHoveredWidget
) {
764 const int index
= newHoveredWidget
->index();
765 if (m_model
->supportsDropping(index
)) {
766 newHoveredWidget
->setHovered(true);
768 emit
itemHovered(index
);
770 if (m_autoActivationTimer
->interval() >= 0) {
771 m_autoActivationTimer
->setProperty("index", index
);
772 m_autoActivationTimer
->start();
780 bool KItemListController::dropEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
787 m_autoActivationTimer
->stop();
789 const QPointF pos
= transform
.map(event
->pos());
790 const int index
= m_view
->itemAt(pos
);
791 emit
itemDropEvent(index
, event
);
796 bool KItemListController::hoverEnterEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
803 bool KItemListController::hoverMoveEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
806 if (!m_model
|| !m_view
) {
810 KItemListWidget
* oldHoveredWidget
= hoveredWidget();
811 const QPointF pos
= transform
.map(event
->pos());
812 KItemListWidget
* newHoveredWidget
= widgetForPos(pos
);
814 if (oldHoveredWidget
!= newHoveredWidget
) {
815 if (oldHoveredWidget
) {
816 oldHoveredWidget
->setHovered(false);
817 emit
itemUnhovered(oldHoveredWidget
->index());
820 if (newHoveredWidget
) {
821 newHoveredWidget
->setHovered(true);
822 emit
itemHovered(newHoveredWidget
->index());
829 bool KItemListController::hoverLeaveEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
834 if (!m_model
|| !m_view
) {
838 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
839 if (widget
->isHovered()) {
840 widget
->setHovered(false);
841 emit
itemUnhovered(widget
->index());
847 bool KItemListController::wheelEvent(QGraphicsSceneWheelEvent
* event
, const QTransform
& transform
)
854 bool KItemListController::resizeEvent(QGraphicsSceneResizeEvent
* event
, const QTransform
& transform
)
861 bool KItemListController::processEvent(QEvent
* event
, const QTransform
& transform
)
867 switch (event
->type()) {
868 case QEvent::KeyPress
:
869 return keyPressEvent(static_cast<QKeyEvent
*>(event
));
870 case QEvent::InputMethod
:
871 return inputMethodEvent(static_cast<QInputMethodEvent
*>(event
));
872 case QEvent::GraphicsSceneMousePress
:
873 return mousePressEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
874 case QEvent::GraphicsSceneMouseMove
:
875 return mouseMoveEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
876 case QEvent::GraphicsSceneMouseRelease
:
877 return mouseReleaseEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
878 case QEvent::GraphicsSceneMouseDoubleClick
:
879 return mouseDoubleClickEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
880 case QEvent::GraphicsSceneWheel
:
881 return wheelEvent(static_cast<QGraphicsSceneWheelEvent
*>(event
), QTransform());
882 case QEvent::GraphicsSceneDragEnter
:
883 return dragEnterEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
884 case QEvent::GraphicsSceneDragLeave
:
885 return dragLeaveEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
886 case QEvent::GraphicsSceneDragMove
:
887 return dragMoveEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
888 case QEvent::GraphicsSceneDrop
:
889 return dropEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
890 case QEvent::GraphicsSceneHoverEnter
:
891 return hoverEnterEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
892 case QEvent::GraphicsSceneHoverMove
:
893 return hoverMoveEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
894 case QEvent::GraphicsSceneHoverLeave
:
895 return hoverLeaveEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
896 case QEvent::GraphicsSceneResize
:
897 return resizeEvent(static_cast<QGraphicsSceneResizeEvent
*>(event
), transform
);
905 void KItemListController::slotViewScrollOffsetChanged(qreal current
, qreal previous
)
911 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
912 if (rubberBand
->isActive()) {
913 const qreal diff
= current
- previous
;
914 // TODO: Ideally just QCursor::pos() should be used as
915 // new end-position but it seems there is no easy way
916 // to have something like QWidget::mapFromGlobal() for QGraphicsWidget
917 // (... or I just missed an easy way to do the mapping)
918 QPointF endPos
= rubberBand
->endPosition();
919 if (m_view
->scrollOrientation() == Qt::Vertical
) {
925 rubberBand
->setEndPosition(endPos
);
929 void KItemListController::slotRubberBandChanged()
931 if (!m_view
|| !m_model
|| m_model
->count() <= 0) {
935 const KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
936 const QPointF startPos
= rubberBand
->startPosition();
937 const QPointF endPos
= rubberBand
->endPosition();
938 QRectF rubberBandRect
= QRectF(startPos
, endPos
).normalized();
940 const bool scrollVertical
= (m_view
->scrollOrientation() == Qt::Vertical
);
941 if (scrollVertical
) {
942 rubberBandRect
.translate(0, -m_view
->scrollOffset());
944 rubberBandRect
.translate(-m_view
->scrollOffset(), 0);
947 if (!m_oldSelection
.isEmpty()) {
948 // Clear the old selection that was available before the rubberband has
949 // been activated in case if no Shift- or Control-key are pressed
950 const bool shiftOrControlPressed
= QApplication::keyboardModifiers() & Qt::ShiftModifier
||
951 QApplication::keyboardModifiers() & Qt::ControlModifier
;
952 if (!shiftOrControlPressed
) {
953 m_oldSelection
.clear();
957 QSet
<int> selectedItems
;
959 // Select all visible items that intersect with the rubberband
960 foreach (const KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
961 const int index
= widget
->index();
963 const QRectF widgetRect
= m_view
->itemRect(index
);
964 if (widgetRect
.intersects(rubberBandRect
)) {
965 const QRectF iconRect
= widget
->iconRect().translated(widgetRect
.topLeft());
966 const QRectF textRect
= widget
->textRect().translated(widgetRect
.topLeft());
967 if (iconRect
.intersects(rubberBandRect
) || textRect
.intersects(rubberBandRect
)) {
968 selectedItems
.insert(index
);
973 // Select all invisible items that intersect with the rubberband. Instead of
974 // iterating all items only the area which might be touched by the rubberband
976 const bool increaseIndex
= scrollVertical
?
977 startPos
.y() > endPos
.y(): startPos
.x() > endPos
.x();
979 int index
= increaseIndex
? m_view
->lastVisibleIndex() + 1 : m_view
->firstVisibleIndex() - 1;
980 bool selectionFinished
= false;
982 const QRectF widgetRect
= m_view
->itemRect(index
);
983 if (widgetRect
.intersects(rubberBandRect
)) {
984 selectedItems
.insert(index
);
989 selectionFinished
= (index
>= m_model
->count()) ||
990 ( scrollVertical
&& widgetRect
.top() > rubberBandRect
.bottom()) ||
991 (!scrollVertical
&& widgetRect
.left() > rubberBandRect
.right());
994 selectionFinished
= (index
< 0) ||
995 ( scrollVertical
&& widgetRect
.bottom() < rubberBandRect
.top()) ||
996 (!scrollVertical
&& widgetRect
.right() < rubberBandRect
.left());
998 } while (!selectionFinished
);
1000 if (QApplication::keyboardModifiers() & Qt::ControlModifier
) {
1001 // If Control is pressed, the selection state of all items in the rubberband is toggled.
1002 // Therefore, the new selection contains:
1003 // 1. All previously selected items which are not inside the rubberband, and
1004 // 2. all items inside the rubberband which have not been selected previously.
1005 m_selectionManager
->setSelectedItems((m_oldSelection
- selectedItems
) + (selectedItems
- m_oldSelection
));
1008 m_selectionManager
->setSelectedItems(selectedItems
+ m_oldSelection
);
1012 void KItemListController::startDragging()
1014 if (!m_view
|| !m_model
) {
1018 const QSet
<int> selectedItems
= m_selectionManager
->selectedItems();
1019 if (selectedItems
.isEmpty()) {
1023 QMimeData
* data
= m_model
->createMimeData(selectedItems
);
1028 // The created drag object will be owned and deleted
1029 // by QApplication::activeWindow().
1030 QDrag
* drag
= new QDrag(QApplication::activeWindow());
1031 drag
->setMimeData(data
);
1033 const QPixmap pixmap
= m_view
->createDragPixmap(selectedItems
);
1034 drag
->setPixmap(pixmap
);
1036 drag
->exec(Qt::MoveAction
| Qt::CopyAction
| Qt::LinkAction
, Qt::CopyAction
);
1039 KItemListWidget
* KItemListController::hoveredWidget() const
1043 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
1044 if (widget
->isHovered()) {
1052 KItemListWidget
* KItemListController::widgetForPos(const QPointF
& pos
) const
1056 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
1057 const QPointF mappedPos
= widget
->mapFromItem(m_view
, pos
);
1059 const bool hovered
= widget
->contains(mappedPos
) &&
1060 !widget
->expansionToggleRect().contains(mappedPos
);
1069 void KItemListController::updateKeyboardAnchor()
1071 const bool validAnchor
= m_keyboardAnchorIndex
>= 0 &&
1072 m_keyboardAnchorIndex
< m_model
->count() &&
1073 keyboardAnchorPos(m_keyboardAnchorIndex
) == m_keyboardAnchorPos
;
1075 const int index
= m_selectionManager
->currentItem();
1076 m_keyboardAnchorIndex
= index
;
1077 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
1081 int KItemListController::nextRowIndex(int index
) const
1083 if (m_keyboardAnchorIndex
< 0) {
1087 const int maxIndex
= m_model
->count() - 1;
1088 if (index
== maxIndex
) {
1092 // Calculate the index of the last column inside the row of the current index
1093 int lastColumnIndex
= index
;
1094 while (keyboardAnchorPos(lastColumnIndex
+ 1) > keyboardAnchorPos(lastColumnIndex
)) {
1096 if (lastColumnIndex
>= maxIndex
) {
1101 // Based on the last column index go to the next row and calculate the nearest index
1102 // that is below the current index
1103 int nextRowIndex
= lastColumnIndex
+ 1;
1104 int searchIndex
= nextRowIndex
;
1105 qreal minDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(nextRowIndex
));
1106 while (searchIndex
< maxIndex
&& keyboardAnchorPos(searchIndex
+ 1) > keyboardAnchorPos(searchIndex
)) {
1108 const qreal searchDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(searchIndex
));
1109 if (searchDiff
< minDiff
) {
1110 minDiff
= searchDiff
;
1111 nextRowIndex
= searchIndex
;
1115 return nextRowIndex
;
1118 int KItemListController::previousRowIndex(int index
) const
1120 if (m_keyboardAnchorIndex
< 0 || index
== 0) {
1124 // Calculate the index of the first column inside the row of the current index
1125 int firstColumnIndex
= index
;
1126 while (keyboardAnchorPos(firstColumnIndex
- 1) < keyboardAnchorPos(firstColumnIndex
)) {
1128 if (firstColumnIndex
<= 0) {
1133 // Based on the first column index go to the previous row and calculate the nearest index
1134 // that is above the current index
1135 int previousRowIndex
= firstColumnIndex
- 1;
1136 int searchIndex
= previousRowIndex
;
1137 qreal minDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(previousRowIndex
));
1138 while (searchIndex
> 0 && keyboardAnchorPos(searchIndex
- 1) < keyboardAnchorPos(searchIndex
)) {
1140 const qreal searchDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(searchIndex
));
1141 if (searchDiff
< minDiff
) {
1142 minDiff
= searchDiff
;
1143 previousRowIndex
= searchIndex
;
1147 return previousRowIndex
;
1150 qreal
KItemListController::keyboardAnchorPos(int index
) const
1152 const QRectF itemRect
= m_view
->itemRect(index
);
1153 if (!itemRect
.isEmpty()) {
1154 return (m_view
->scrollOrientation() == Qt::Vertical
) ? itemRect
.x() : itemRect
.y();
1160 #include "kitemlistcontroller.moc"