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
= itemCount
== 1 &&
207 (key
== Qt::Key_Home
|| key
== Qt::Key_End
||
208 key
== Qt::Key_Up
|| key
== Qt::Key_Down
||
209 key
== Qt::Key_Left
|| key
== Qt::Key_Right
);
210 if (selectSingleItem
) {
211 const int current
= m_selectionManager
->currentItem();
212 m_selectionManager
->setSelected(current
);
222 index
= itemCount
- 1;
228 m_keyboardAnchorIndex
= index
;
229 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
234 if (index
< itemCount
- 1) {
236 m_keyboardAnchorIndex
= index
;
237 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
242 updateKeyboardAnchor();
243 index
= previousRowIndex(index
);
247 updateKeyboardAnchor();
248 index
= nextRowIndex(index
);
252 if (m_view
->scrollOrientation() == Qt::Horizontal
) {
253 // The new current index should correspond to the first item in the current column.
254 int newIndex
= qMax(index
- 1, 0);
255 while (newIndex
!= index
&& m_view
->itemRect(newIndex
).topLeft().y() < m_view
->itemRect(index
).topLeft().y()) {
257 newIndex
= qMax(index
- 1, 0);
259 m_keyboardAnchorIndex
= index
;
260 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
262 const qreal currentItemBottom
= m_view
->itemRect(index
).bottomLeft().y();
263 const qreal height
= m_view
->geometry().height();
265 // The new current item should be the first item in the current
266 // column whose itemRect's top coordinate is larger than targetY.
267 const qreal targetY
= currentItemBottom
- height
;
269 updateKeyboardAnchor();
270 int newIndex
= previousRowIndex(index
);
273 updateKeyboardAnchor();
274 newIndex
= previousRowIndex(index
);
275 } while (m_view
->itemRect(newIndex
).topLeft().y() > targetY
&& newIndex
!= index
);
279 case Qt::Key_PageDown
:
280 if (m_view
->scrollOrientation() == Qt::Horizontal
) {
281 // The new current index should correspond to the last item in the current column.
282 int newIndex
= qMin(index
+ 1, m_model
->count() - 1);
283 while (newIndex
!= index
&& m_view
->itemRect(newIndex
).topLeft().y() > m_view
->itemRect(index
).topLeft().y()) {
285 newIndex
= qMin(index
+ 1, m_model
->count() - 1);
287 m_keyboardAnchorIndex
= index
;
288 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
290 const qreal currentItemTop
= m_view
->itemRect(index
).topLeft().y();
291 const qreal height
= m_view
->geometry().height();
293 // The new current item should be the last item in the current
294 // column whose itemRect's bottom coordinate is smaller than targetY.
295 const qreal targetY
= currentItemTop
+ height
;
297 updateKeyboardAnchor();
298 int newIndex
= nextRowIndex(index
);
301 updateKeyboardAnchor();
302 newIndex
= nextRowIndex(index
);
303 } while (m_view
->itemRect(newIndex
).bottomLeft().y() < targetY
&& newIndex
!= index
);
308 case Qt::Key_Return
: {
309 const QSet
<int> selectedItems
= m_selectionManager
->selectedItems();
310 if (selectedItems
.count() >= 2) {
311 emit
itemsActivated(selectedItems
);
312 } else if (selectedItems
.count() == 1) {
313 emit
itemActivated(selectedItems
.toList().first());
315 emit
itemActivated(index
);
321 if (controlPressed
) {
322 m_selectionManager
->endAnchoredSelection();
323 m_selectionManager
->setSelected(index
, 1, KItemListSelectionManager::Toggle
);
324 m_selectionManager
->beginAnchoredSelection(index
);
326 const int current
= m_selectionManager
->currentItem();
327 m_selectionManager
->setSelected(current
);
332 // Emit the signal itemContextMenuRequested() in case if at least one
333 // item is selected. Otherwise the signal viewContextMenuRequested() will be emitted.
334 const QSet
<int> selectedItems
= m_selectionManager
->selectedItems();
336 if (selectedItems
.count() >= 2) {
337 const int currentItemIndex
= m_selectionManager
->currentItem();
338 index
= selectedItems
.contains(currentItemIndex
)
339 ? currentItemIndex
: selectedItems
.toList().first();
340 } else if (selectedItems
.count() == 1) {
341 index
= selectedItems
.toList().first();
345 const QRectF contextRect
= m_view
->itemContextRect(index
);
346 const QPointF
pos(m_view
->scene()->views().first()->mapToGlobal(contextRect
.bottomRight().toPoint()));
347 emit
itemContextMenuRequested(index
, pos
);
349 emit
viewContextMenuRequested(QCursor::pos());
355 m_keyboardManager
->addKeys(event
->text());
359 if (m_selectionManager
->currentItem() != index
) {
360 if (controlPressed
) {
361 m_selectionManager
->endAnchoredSelection();
364 m_selectionManager
->setCurrentItem(index
);
366 if (!shiftOrControlPressed
|| m_selectionBehavior
== SingleSelection
) {
367 m_selectionManager
->clearSelection();
368 m_selectionManager
->setSelected(index
, 1);
372 m_selectionManager
->beginAnchoredSelection(index
);
375 m_view
->scrollToItem(index
);
380 void KItemListController::slotChangeCurrentItem(const QString
& text
, bool searchFromNextItem
)
382 if (!m_model
|| m_model
->count() == 0) {
385 const int currentIndex
= m_selectionManager
->currentItem();
387 if (searchFromNextItem
) {
388 index
= m_model
->indexForKeyboardSearch(text
, (currentIndex
+ 1) % m_model
->count());
390 index
= m_model
->indexForKeyboardSearch(text
, currentIndex
);
393 m_selectionManager
->setCurrentItem(index
);
394 m_selectionManager
->clearSelection();
395 m_selectionManager
->setSelected(index
, 1);
396 m_selectionManager
->beginAnchoredSelection(index
);
397 m_view
->scrollToItem(index
);
401 void KItemListController::slotAutoActivationTimeout()
403 if (!m_model
|| !m_view
) {
407 const int index
= m_autoActivationTimer
->property("index").toInt();
408 if (index
< 0 || index
>= m_model
->count()) {
412 if (m_model
->supportsDropping(index
)) {
413 if (m_view
->supportsItemExpanding() && m_model
->isExpandable(index
)) {
414 const bool expanded
= m_model
->isExpanded(index
);
415 m_model
->setExpanded(index
, !expanded
);
417 emit
itemActivated(index
);
422 bool KItemListController::inputMethodEvent(QInputMethodEvent
* event
)
428 bool KItemListController::mousePressEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
434 m_pressedMousePos
= transform
.map(event
->pos());
435 m_pressedIndex
= m_view
->itemAt(m_pressedMousePos
);
436 if (m_pressedIndex
>= 0) {
437 emit
itemPressed(m_pressedIndex
, event
->button());
440 if (m_view
->isAboveExpansionToggle(m_pressedIndex
, m_pressedMousePos
)) {
441 m_selectionManager
->endAnchoredSelection();
442 m_selectionManager
->setCurrentItem(m_pressedIndex
);
443 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
447 m_selectionTogglePressed
= m_view
->isAboveSelectionToggle(m_pressedIndex
, m_pressedMousePos
);
448 if (m_selectionTogglePressed
) {
449 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
450 // The previous anchored selection has been finished already in
451 // KItemListSelectionManager::setSelected(). We can safely change
452 // the current item and start a new anchored selection now.
453 m_selectionManager
->setCurrentItem(m_pressedIndex
);
454 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
458 const bool shiftPressed
= event
->modifiers() & Qt::ShiftModifier
;
459 const bool controlPressed
= event
->modifiers() & Qt::ControlModifier
;
461 // The previous selection is cleared if either
462 // 1. The selection mode is SingleSelection, or
463 // 2. the selection mode is MultiSelection, and *none* of the following conditions are met:
464 // a) Shift or Control are pressed.
465 // b) The clicked item is selected already. In that case, the user might want to:
466 // - start dragging multiple items, or
467 // - open the context menu and perform an action for all selected items.
468 const bool shiftOrControlPressed
= shiftPressed
|| controlPressed
;
469 const bool pressedItemAlreadySelected
= m_pressedIndex
>= 0 && m_selectionManager
->isSelected(m_pressedIndex
);
470 const bool clearSelection
= m_selectionBehavior
== SingleSelection
||
471 (!shiftOrControlPressed
&& !pressedItemAlreadySelected
);
472 if (clearSelection
) {
473 m_selectionManager
->clearSelection();
474 } else if (pressedItemAlreadySelected
&& !shiftOrControlPressed
&& (event
->buttons() & Qt::LeftButton
)) {
475 // The user might want to start dragging multiple items, but if he clicks the item
476 // in order to trigger it instead, the other selected items must be deselected.
477 // However, we do not know yet what the user is going to do.
478 // -> remember that the user pressed an item which had been selected already and
479 // clear the selection in mouseReleaseEvent(), unless the items are dragged.
480 m_clearSelectionIfItemsAreNotDragged
= true;
484 // Finish the anchored selection before the current index is changed
485 m_selectionManager
->endAnchoredSelection();
488 if (m_pressedIndex
>= 0) {
489 m_selectionManager
->setCurrentItem(m_pressedIndex
);
491 switch (m_selectionBehavior
) {
495 case SingleSelection
:
496 m_selectionManager
->setSelected(m_pressedIndex
);
500 if (controlPressed
) {
501 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
502 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
503 } else if (!shiftPressed
|| !m_selectionManager
->isAnchoredSelectionActive()) {
504 // Select the pressed item and start a new anchored selection
505 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Select
);
506 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
515 if (event
->buttons() & Qt::RightButton
) {
516 emit
itemContextMenuRequested(m_pressedIndex
, event
->screenPos());
522 if (event
->buttons() & Qt::RightButton
) {
523 const QRectF headerBounds
= m_view
->headerBoundaries();
524 if (headerBounds
.contains(event
->pos())) {
525 emit
headerContextMenuRequested(event
->screenPos());
527 emit
viewContextMenuRequested(event
->screenPos());
532 if (m_selectionBehavior
== MultiSelection
) {
533 QPointF startPos
= m_pressedMousePos
;
534 if (m_view
->scrollOrientation() == Qt::Vertical
) {
535 startPos
.ry() += m_view
->scrollOffset();
536 if (m_view
->itemSize().width() < 0) {
537 // Use a special rubberband for views that have only one column and
538 // expand the rubberband to use the whole width of the view.
542 startPos
.rx() += m_view
->scrollOffset();
545 m_oldSelection
= m_selectionManager
->selectedItems();
546 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
547 rubberBand
->setStartPosition(startPos
);
548 rubberBand
->setEndPosition(startPos
);
549 rubberBand
->setActive(true);
550 connect(rubberBand
, SIGNAL(endPositionChanged(QPointF
,QPointF
)), this, SLOT(slotRubberBandChanged()));
551 m_view
->setAutoScroll(true);
557 bool KItemListController::mouseMoveEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
563 if (m_pressedIndex
>= 0) {
564 // Check whether a dragging should be started
565 if (event
->buttons() & Qt::LeftButton
) {
566 const QPointF pos
= transform
.map(event
->pos());
567 if ((pos
- m_pressedMousePos
).manhattanLength() >= QApplication::startDragDistance()) {
568 if (!m_selectionManager
->isSelected(m_pressedIndex
)) {
569 // Always assure that the dragged item gets selected. Usually this is already
570 // done on the mouse-press event, but when using the selection-toggle on a
571 // selected item the dragged item is not selected yet.
572 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
574 // A selected item has been clicked to drag all selected items
575 // -> the selection should not be cleared when the mouse button is released.
576 m_clearSelectionIfItemsAreNotDragged
= false;
583 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
584 if (rubberBand
->isActive()) {
585 QPointF endPos
= transform
.map(event
->pos());
587 // Update the current item.
588 const int newCurrent
= m_view
->itemAt(endPos
);
589 if (newCurrent
>= 0) {
590 // It's expected that the new current index is also the new anchor (bug 163451).
591 m_selectionManager
->endAnchoredSelection();
592 m_selectionManager
->setCurrentItem(newCurrent
);
593 m_selectionManager
->beginAnchoredSelection(newCurrent
);
596 if (m_view
->scrollOrientation() == Qt::Vertical
) {
597 endPos
.ry() += m_view
->scrollOffset();
598 if (m_view
->itemSize().width() < 0) {
599 // Use a special rubberband for views that have only one column and
600 // expand the rubberband to use the whole width of the view.
601 endPos
.setX(m_view
->size().width());
604 endPos
.rx() += m_view
->scrollOffset();
606 rubberBand
->setEndPosition(endPos
);
613 bool KItemListController::mouseReleaseEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
619 if (m_pressedIndex
>= 0) {
620 emit
itemReleased(m_pressedIndex
, event
->button());
623 const bool isAboveSelectionToggle
= m_view
->isAboveSelectionToggle(m_pressedIndex
, m_pressedMousePos
);
624 if (isAboveSelectionToggle
) {
625 m_selectionTogglePressed
= false;
629 if (!isAboveSelectionToggle
&& m_selectionTogglePressed
) {
630 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
631 m_selectionTogglePressed
= false;
635 const bool shiftOrControlPressed
= event
->modifiers() & Qt::ShiftModifier
||
636 event
->modifiers() & Qt::ControlModifier
;
638 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
639 if (rubberBand
->isActive()) {
640 disconnect(rubberBand
, SIGNAL(endPositionChanged(QPointF
,QPointF
)), this, SLOT(slotRubberBandChanged()));
641 rubberBand
->setActive(false);
642 m_oldSelection
.clear();
643 m_view
->setAutoScroll(false);
646 const QPointF pos
= transform
.map(event
->pos());
647 const int index
= m_view
->itemAt(pos
);
649 if (index
>= 0 && index
== m_pressedIndex
) {
650 // The release event is done above the same item as the press event
652 if (m_clearSelectionIfItemsAreNotDragged
) {
653 // A selected item has been clicked, but no drag operation has been started
654 // -> clear the rest of the selection.
655 m_selectionManager
->clearSelection();
656 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Select
);
657 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
660 if (event
->button() & Qt::LeftButton
) {
661 bool emitItemActivated
= true;
662 if (m_view
->isAboveExpansionToggle(index
, pos
)) {
663 const bool expanded
= m_model
->isExpanded(index
);
664 m_model
->setExpanded(index
, !expanded
);
666 emit
itemExpansionToggleClicked(index
);
667 emitItemActivated
= false;
668 } else if (shiftOrControlPressed
) {
669 // The mouse click should only update the selection, not trigger the item
670 emitItemActivated
= false;
671 } else if (!m_singleClickActivation
) {
672 emitItemActivated
= false;
674 if (emitItemActivated
) {
675 emit
itemActivated(index
);
677 } else if (event
->button() & Qt::MidButton
) {
678 emit
itemMiddleClicked(index
);
682 m_pressedMousePos
= QPointF();
684 m_clearSelectionIfItemsAreNotDragged
= false;
688 bool KItemListController::mouseDoubleClickEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
690 const QPointF pos
= transform
.map(event
->pos());
691 const int index
= m_view
->itemAt(pos
);
693 bool emitItemActivated
= !m_singleClickActivation
&&
694 (event
->button() & Qt::LeftButton
) &&
695 index
>= 0 && index
< m_model
->count();
696 if (emitItemActivated
) {
697 emit
itemActivated(index
);
702 bool KItemListController::dragEnterEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
709 bool KItemListController::dragLeaveEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
716 bool KItemListController::dragMoveEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
719 if (!m_model
|| !m_view
) {
723 KItemListWidget
* oldHoveredWidget
= hoveredWidget();
725 const QPointF pos
= transform
.map(event
->pos());
726 KItemListWidget
* newHoveredWidget
= widgetForPos(pos
);
728 if (oldHoveredWidget
!= newHoveredWidget
) {
729 m_autoActivationTimer
->stop();
731 if (oldHoveredWidget
) {
732 oldHoveredWidget
->setHovered(false);
733 emit
itemUnhovered(oldHoveredWidget
->index());
736 if (newHoveredWidget
) {
737 const int index
= newHoveredWidget
->index();
738 if (m_model
->supportsDropping(index
)) {
739 newHoveredWidget
->setHovered(true);
741 emit
itemHovered(index
);
743 if (m_autoActivationTimer
->interval() >= 0) {
744 m_autoActivationTimer
->setProperty("index", index
);
745 m_autoActivationTimer
->start();
753 bool KItemListController::dropEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
760 m_autoActivationTimer
->stop();
762 const QPointF pos
= transform
.map(event
->pos());
763 const int index
= m_view
->itemAt(pos
);
764 emit
itemDropEvent(index
, event
);
769 bool KItemListController::hoverEnterEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
776 bool KItemListController::hoverMoveEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
779 if (!m_model
|| !m_view
) {
783 KItemListWidget
* oldHoveredWidget
= hoveredWidget();
784 const QPointF pos
= transform
.map(event
->pos());
785 KItemListWidget
* newHoveredWidget
= widgetForPos(pos
);
787 if (oldHoveredWidget
!= newHoveredWidget
) {
788 if (oldHoveredWidget
) {
789 oldHoveredWidget
->setHovered(false);
790 emit
itemUnhovered(oldHoveredWidget
->index());
793 if (newHoveredWidget
) {
794 newHoveredWidget
->setHovered(true);
795 emit
itemHovered(newHoveredWidget
->index());
802 bool KItemListController::hoverLeaveEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
807 if (!m_model
|| !m_view
) {
811 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
812 if (widget
->isHovered()) {
813 widget
->setHovered(false);
814 emit
itemUnhovered(widget
->index());
820 bool KItemListController::wheelEvent(QGraphicsSceneWheelEvent
* event
, const QTransform
& transform
)
827 bool KItemListController::resizeEvent(QGraphicsSceneResizeEvent
* event
, const QTransform
& transform
)
834 bool KItemListController::processEvent(QEvent
* event
, const QTransform
& transform
)
840 switch (event
->type()) {
841 case QEvent::KeyPress
:
842 return keyPressEvent(static_cast<QKeyEvent
*>(event
));
843 case QEvent::InputMethod
:
844 return inputMethodEvent(static_cast<QInputMethodEvent
*>(event
));
845 case QEvent::GraphicsSceneMousePress
:
846 return mousePressEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
847 case QEvent::GraphicsSceneMouseMove
:
848 return mouseMoveEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
849 case QEvent::GraphicsSceneMouseRelease
:
850 return mouseReleaseEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
851 case QEvent::GraphicsSceneMouseDoubleClick
:
852 return mouseDoubleClickEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
853 case QEvent::GraphicsSceneWheel
:
854 return wheelEvent(static_cast<QGraphicsSceneWheelEvent
*>(event
), QTransform());
855 case QEvent::GraphicsSceneDragEnter
:
856 return dragEnterEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
857 case QEvent::GraphicsSceneDragLeave
:
858 return dragLeaveEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
859 case QEvent::GraphicsSceneDragMove
:
860 return dragMoveEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
861 case QEvent::GraphicsSceneDrop
:
862 return dropEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
863 case QEvent::GraphicsSceneHoverEnter
:
864 return hoverEnterEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
865 case QEvent::GraphicsSceneHoverMove
:
866 return hoverMoveEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
867 case QEvent::GraphicsSceneHoverLeave
:
868 return hoverLeaveEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
869 case QEvent::GraphicsSceneResize
:
870 return resizeEvent(static_cast<QGraphicsSceneResizeEvent
*>(event
), transform
);
878 void KItemListController::slotViewScrollOffsetChanged(qreal current
, qreal previous
)
884 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
885 if (rubberBand
->isActive()) {
886 const qreal diff
= current
- previous
;
887 // TODO: Ideally just QCursor::pos() should be used as
888 // new end-position but it seems there is no easy way
889 // to have something like QWidget::mapFromGlobal() for QGraphicsWidget
890 // (... or I just missed an easy way to do the mapping)
891 QPointF endPos
= rubberBand
->endPosition();
892 if (m_view
->scrollOrientation() == Qt::Vertical
) {
898 rubberBand
->setEndPosition(endPos
);
902 void KItemListController::slotRubberBandChanged()
904 if (!m_view
|| !m_model
|| m_model
->count() <= 0) {
908 const KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
909 const QPointF startPos
= rubberBand
->startPosition();
910 const QPointF endPos
= rubberBand
->endPosition();
911 QRectF rubberBandRect
= QRectF(startPos
, endPos
).normalized();
913 const bool scrollVertical
= (m_view
->scrollOrientation() == Qt::Vertical
);
914 if (scrollVertical
) {
915 rubberBandRect
.translate(0, -m_view
->scrollOffset());
917 rubberBandRect
.translate(-m_view
->scrollOffset(), 0);
920 if (!m_oldSelection
.isEmpty()) {
921 // Clear the old selection that was available before the rubberband has
922 // been activated in case if no Shift- or Control-key are pressed
923 const bool shiftOrControlPressed
= QApplication::keyboardModifiers() & Qt::ShiftModifier
||
924 QApplication::keyboardModifiers() & Qt::ControlModifier
;
925 if (!shiftOrControlPressed
) {
926 m_oldSelection
.clear();
930 QSet
<int> selectedItems
;
932 // Select all visible items that intersect with the rubberband
933 foreach (const KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
934 const int index
= widget
->index();
936 const QRectF widgetRect
= m_view
->itemRect(index
);
937 if (widgetRect
.intersects(rubberBandRect
)) {
938 const QRectF iconRect
= widget
->iconRect().translated(widgetRect
.topLeft());
939 const QRectF textRect
= widget
->textRect().translated(widgetRect
.topLeft());
940 if (iconRect
.intersects(rubberBandRect
) || textRect
.intersects(rubberBandRect
)) {
941 selectedItems
.insert(index
);
946 // Select all invisible items that intersect with the rubberband. Instead of
947 // iterating all items only the area which might be touched by the rubberband
949 const bool increaseIndex
= scrollVertical
?
950 startPos
.y() > endPos
.y(): startPos
.x() > endPos
.x();
952 int index
= increaseIndex
? m_view
->lastVisibleIndex() + 1 : m_view
->firstVisibleIndex() - 1;
953 bool selectionFinished
= false;
955 const QRectF widgetRect
= m_view
->itemRect(index
);
956 if (widgetRect
.intersects(rubberBandRect
)) {
957 selectedItems
.insert(index
);
962 selectionFinished
= (index
>= m_model
->count()) ||
963 ( scrollVertical
&& widgetRect
.top() > rubberBandRect
.bottom()) ||
964 (!scrollVertical
&& widgetRect
.left() > rubberBandRect
.right());
967 selectionFinished
= (index
< 0) ||
968 ( scrollVertical
&& widgetRect
.bottom() < rubberBandRect
.top()) ||
969 (!scrollVertical
&& widgetRect
.right() < rubberBandRect
.left());
971 } while (!selectionFinished
);
973 if (QApplication::keyboardModifiers() & Qt::ControlModifier
) {
974 // If Control is pressed, the selection state of all items in the rubberband is toggled.
975 // Therefore, the new selection contains:
976 // 1. All previously selected items which are not inside the rubberband, and
977 // 2. all items inside the rubberband which have not been selected previously.
978 m_selectionManager
->setSelectedItems((m_oldSelection
- selectedItems
) + (selectedItems
- m_oldSelection
));
981 m_selectionManager
->setSelectedItems(selectedItems
+ m_oldSelection
);
985 void KItemListController::startDragging()
987 if (!m_view
|| !m_model
) {
991 const QSet
<int> selectedItems
= m_selectionManager
->selectedItems();
992 if (selectedItems
.isEmpty()) {
996 QMimeData
* data
= m_model
->createMimeData(selectedItems
);
1001 // The created drag object will be owned and deleted
1002 // by QApplication::activeWindow().
1003 QDrag
* drag
= new QDrag(QApplication::activeWindow());
1004 drag
->setMimeData(data
);
1006 const QPixmap pixmap
= m_view
->createDragPixmap(selectedItems
);
1007 drag
->setPixmap(pixmap
);
1009 drag
->exec(Qt::MoveAction
| Qt::CopyAction
| Qt::LinkAction
, Qt::CopyAction
);
1012 KItemListWidget
* KItemListController::hoveredWidget() const
1016 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
1017 if (widget
->isHovered()) {
1025 KItemListWidget
* KItemListController::widgetForPos(const QPointF
& pos
) const
1029 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
1030 const QPointF mappedPos
= widget
->mapFromItem(m_view
, pos
);
1032 const bool hovered
= widget
->contains(mappedPos
) &&
1033 !widget
->expansionToggleRect().contains(mappedPos
);
1042 void KItemListController::updateKeyboardAnchor()
1044 const bool validAnchor
= m_keyboardAnchorIndex
>= 0 &&
1045 m_keyboardAnchorIndex
< m_model
->count() &&
1046 keyboardAnchorPos(m_keyboardAnchorIndex
) == m_keyboardAnchorPos
;
1048 const int index
= m_selectionManager
->currentItem();
1049 m_keyboardAnchorIndex
= index
;
1050 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
1054 int KItemListController::nextRowIndex(int index
) const
1056 if (m_keyboardAnchorIndex
< 0) {
1060 const int maxIndex
= m_model
->count() - 1;
1061 if (index
== maxIndex
) {
1065 // Calculate the index of the last column inside the row of the current index
1066 int lastColumnIndex
= index
;
1067 while (keyboardAnchorPos(lastColumnIndex
+ 1) > keyboardAnchorPos(lastColumnIndex
)) {
1069 if (lastColumnIndex
>= maxIndex
) {
1074 // Based on the last column index go to the next row and calculate the nearest index
1075 // that is below the current index
1076 int nextRowIndex
= lastColumnIndex
+ 1;
1077 int searchIndex
= nextRowIndex
;
1078 qreal minDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(nextRowIndex
));
1079 while (searchIndex
< maxIndex
&& keyboardAnchorPos(searchIndex
+ 1) > keyboardAnchorPos(searchIndex
)) {
1081 const qreal searchDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(searchIndex
));
1082 if (searchDiff
< minDiff
) {
1083 minDiff
= searchDiff
;
1084 nextRowIndex
= searchIndex
;
1088 return nextRowIndex
;
1091 int KItemListController::previousRowIndex(int index
) const
1093 if (m_keyboardAnchorIndex
< 0 || index
== 0) {
1097 // Calculate the index of the first column inside the row of the current index
1098 int firstColumnIndex
= index
;
1099 while (keyboardAnchorPos(firstColumnIndex
- 1) < keyboardAnchorPos(firstColumnIndex
)) {
1101 if (firstColumnIndex
<= 0) {
1106 // Based on the first column index go to the previous row and calculate the nearest index
1107 // that is above the current index
1108 int previousRowIndex
= firstColumnIndex
- 1;
1109 int searchIndex
= previousRowIndex
;
1110 qreal minDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(previousRowIndex
));
1111 while (searchIndex
> 0 && keyboardAnchorPos(searchIndex
- 1) < keyboardAnchorPos(searchIndex
)) {
1113 const qreal searchDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(searchIndex
));
1114 if (searchDiff
< minDiff
) {
1115 minDiff
= searchDiff
;
1116 previousRowIndex
= searchIndex
;
1120 return previousRowIndex
;
1123 qreal
KItemListController::keyboardAnchorPos(int index
) const
1125 const QRectF itemRect
= m_view
->itemRect(index
);
1126 if (!itemRect
.isEmpty()) {
1127 return (m_view
->scrollOrientation() == Qt::Vertical
) ? itemRect
.x() : itemRect
.y();
1133 #include "kitemlistcontroller.moc"