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
),
50 m_autoActivationBehavior(ActivationAndExpansion
),
51 m_mouseDoubleClickAction(ActivateItemOnly
),
54 m_selectionManager(new KItemListSelectionManager(this)),
55 m_keyboardManager(new KItemListKeyboardSearchManager(this)),
58 m_autoActivationTimer(0),
60 m_keyboardAnchorIndex(-1),
61 m_keyboardAnchorPos(0)
63 connect(m_keyboardManager
, SIGNAL(changeCurrentItem(QString
,bool)),
64 this, SLOT(slotChangeCurrentItem(QString
,bool)));
65 connect(m_selectionManager
, SIGNAL(currentChanged(int,int)),
66 m_keyboardManager
, SLOT(slotCurrentChanged(int,int)));
68 m_autoActivationTimer
= new QTimer(this);
69 m_autoActivationTimer
->setSingleShot(true);
70 m_autoActivationTimer
->setInterval(-1);
71 connect(m_autoActivationTimer
, SIGNAL(timeout()), this, SLOT(slotAutoActivationTimeout()));
77 KItemListController::~KItemListController()
86 void KItemListController::setModel(KItemModelBase
* model
)
88 if (m_model
== model
) {
92 KItemModelBase
* oldModel
= m_model
;
94 oldModel
->deleteLater();
99 m_model
->setParent(this);
103 m_view
->setModel(m_model
);
106 m_selectionManager
->setModel(m_model
);
108 emit
modelChanged(m_model
, oldModel
);
111 KItemModelBase
* KItemListController::model() const
116 KItemListSelectionManager
* KItemListController::selectionManager() const
118 return m_selectionManager
;
121 void KItemListController::setView(KItemListView
* view
)
123 if (m_view
== view
) {
127 KItemListView
* oldView
= m_view
;
129 disconnect(oldView
, SIGNAL(scrollOffsetChanged(qreal
,qreal
)), this, SLOT(slotViewScrollOffsetChanged(qreal
,qreal
)));
130 oldView
->deleteLater();
136 m_view
->setParent(this);
137 m_view
->setController(this);
138 m_view
->setModel(m_model
);
139 connect(m_view
, SIGNAL(scrollOffsetChanged(qreal
,qreal
)), this, SLOT(slotViewScrollOffsetChanged(qreal
,qreal
)));
140 updateExtendedSelectionRegion();
143 emit
viewChanged(m_view
, oldView
);
146 KItemListView
* KItemListController::view() const
151 void KItemListController::setSelectionBehavior(SelectionBehavior behavior
)
153 m_selectionBehavior
= behavior
;
154 updateExtendedSelectionRegion();
157 KItemListController::SelectionBehavior
KItemListController::selectionBehavior() const
159 return m_selectionBehavior
;
162 void KItemListController::setAutoActivationBehavior(AutoActivationBehavior behavior
)
164 m_autoActivationBehavior
= behavior
;
167 KItemListController::AutoActivationBehavior
KItemListController::autoActivationBehavior() const
169 return m_autoActivationBehavior
;
172 void KItemListController::setMouseDoubleClickAction(MouseDoubleClickAction action
)
174 m_mouseDoubleClickAction
= action
;
177 KItemListController::MouseDoubleClickAction
KItemListController::mouseDoubleClickAction() const
179 return m_mouseDoubleClickAction
;
182 void KItemListController::setAutoActivationDelay(int delay
)
184 m_autoActivationTimer
->setInterval(delay
);
187 int KItemListController::autoActivationDelay() const
189 return m_autoActivationTimer
->interval();
192 void KItemListController::setSingleClickActivation(bool singleClick
)
194 m_singleClickActivation
= singleClick
;
197 bool KItemListController::singleClickActivation() const
199 return m_singleClickActivation
;
202 bool KItemListController::showEvent(QShowEvent
* event
)
208 bool KItemListController::hideEvent(QHideEvent
* event
)
214 bool KItemListController::keyPressEvent(QKeyEvent
* event
)
216 int index
= m_selectionManager
->currentItem();
217 int key
= event
->key();
219 // Handle the expanding/collapsing of items
220 if (m_view
->supportsItemExpanding() && m_model
->isExpandable(index
)) {
221 if (key
== Qt::Key_Right
) {
222 if (m_model
->setExpanded(index
, true)) {
225 } else if (key
== Qt::Key_Left
) {
226 if (m_model
->setExpanded(index
, false)) {
232 const bool shiftPressed
= event
->modifiers() & Qt::ShiftModifier
;
233 const bool controlPressed
= event
->modifiers() & Qt::ControlModifier
;
234 const bool shiftOrControlPressed
= shiftPressed
|| controlPressed
;
236 const int itemCount
= m_model
->count();
238 // For horizontal scroll orientation, transform
239 // the arrow keys to simplify the event handling.
240 if (m_view
->scrollOrientation() == Qt::Horizontal
) {
242 case Qt::Key_Up
: key
= Qt::Key_Left
; break;
243 case Qt::Key_Down
: key
= Qt::Key_Right
; break;
244 case Qt::Key_Left
: key
= Qt::Key_Up
; break;
245 case Qt::Key_Right
: key
= Qt::Key_Down
; break;
250 const bool selectSingleItem
= m_selectionBehavior
!= NoSelection
&&
252 (key
== Qt::Key_Home
|| key
== Qt::Key_End
||
253 key
== Qt::Key_Up
|| key
== Qt::Key_Down
||
254 key
== Qt::Key_Left
|| key
== Qt::Key_Right
);
255 if (selectSingleItem
) {
256 const int current
= m_selectionManager
->currentItem();
257 m_selectionManager
->setSelected(current
);
264 m_keyboardAnchorIndex
= index
;
265 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
269 index
= itemCount
- 1;
270 m_keyboardAnchorIndex
= index
;
271 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
276 const int expandedParentsCount
= m_model
->expandedParentsCount(index
);
277 if (expandedParentsCount
== 0) {
280 // Go to the parent of the current item.
283 } while (index
> 0 && m_model
->expandedParentsCount(index
) == expandedParentsCount
);
285 m_keyboardAnchorIndex
= index
;
286 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
291 if (index
< itemCount
- 1) {
293 m_keyboardAnchorIndex
= index
;
294 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
299 updateKeyboardAnchor();
300 index
= previousRowIndex(index
);
304 updateKeyboardAnchor();
305 index
= nextRowIndex(index
);
309 if (m_view
->scrollOrientation() == Qt::Horizontal
) {
310 // The new current index should correspond to the first item in the current column.
311 int newIndex
= qMax(index
- 1, 0);
312 while (newIndex
!= index
&& m_view
->itemRect(newIndex
).topLeft().y() < m_view
->itemRect(index
).topLeft().y()) {
314 newIndex
= qMax(index
- 1, 0);
316 m_keyboardAnchorIndex
= index
;
317 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
319 const qreal currentItemBottom
= m_view
->itemRect(index
).bottomLeft().y();
320 const qreal height
= m_view
->geometry().height();
322 // The new current item should be the first item in the current
323 // column whose itemRect's top coordinate is larger than targetY.
324 const qreal targetY
= currentItemBottom
- height
;
326 updateKeyboardAnchor();
327 int newIndex
= previousRowIndex(index
);
330 updateKeyboardAnchor();
331 newIndex
= previousRowIndex(index
);
332 } while (m_view
->itemRect(newIndex
).topLeft().y() > targetY
&& newIndex
!= index
);
336 case Qt::Key_PageDown
:
337 if (m_view
->scrollOrientation() == Qt::Horizontal
) {
338 // The new current index should correspond to the last item in the current column.
339 int newIndex
= qMin(index
+ 1, m_model
->count() - 1);
340 while (newIndex
!= index
&& m_view
->itemRect(newIndex
).topLeft().y() > m_view
->itemRect(index
).topLeft().y()) {
342 newIndex
= qMin(index
+ 1, m_model
->count() - 1);
344 m_keyboardAnchorIndex
= index
;
345 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
347 const qreal currentItemTop
= m_view
->itemRect(index
).topLeft().y();
348 const qreal height
= m_view
->geometry().height();
350 // The new current item should be the last item in the current
351 // column whose itemRect's bottom coordinate is smaller than targetY.
352 const qreal targetY
= currentItemTop
+ height
;
354 updateKeyboardAnchor();
355 int newIndex
= nextRowIndex(index
);
358 updateKeyboardAnchor();
359 newIndex
= nextRowIndex(index
);
360 } while (m_view
->itemRect(newIndex
).bottomLeft().y() < targetY
&& newIndex
!= index
);
365 case Qt::Key_Return
: {
366 const QSet
<int> selectedItems
= m_selectionManager
->selectedItems();
367 if (selectedItems
.count() >= 2) {
368 emit
itemsActivated(selectedItems
);
369 } else if (selectedItems
.count() == 1) {
370 emit
itemActivated(selectedItems
.toList().first());
372 emit
itemActivated(index
);
378 if (m_selectionBehavior
== MultiSelection
) {
379 if (controlPressed
) {
380 m_selectionManager
->endAnchoredSelection();
381 m_selectionManager
->setSelected(index
, 1, KItemListSelectionManager::Toggle
);
382 m_selectionManager
->beginAnchoredSelection(index
);
384 const int current
= m_selectionManager
->currentItem();
385 m_selectionManager
->setSelected(current
);
391 // Emit the signal itemContextMenuRequested() in case if at least one
392 // item is selected. Otherwise the signal viewContextMenuRequested() will be emitted.
393 const QSet
<int> selectedItems
= m_selectionManager
->selectedItems();
395 if (selectedItems
.count() >= 2) {
396 const int currentItemIndex
= m_selectionManager
->currentItem();
397 index
= selectedItems
.contains(currentItemIndex
)
398 ? currentItemIndex
: selectedItems
.toList().first();
399 } else if (selectedItems
.count() == 1) {
400 index
= selectedItems
.toList().first();
404 const QRectF contextRect
= m_view
->itemContextRect(index
);
405 const QPointF
pos(m_view
->scene()->views().first()->mapToGlobal(contextRect
.bottomRight().toPoint()));
406 emit
itemContextMenuRequested(index
, pos
);
408 emit
viewContextMenuRequested(QCursor::pos());
414 if (m_selectionBehavior
!= SingleSelection
) {
415 m_selectionManager
->clearSelection();
417 m_keyboardManager
->cancelSearch();
421 m_keyboardManager
->addKeys(event
->text());
425 if (m_selectionManager
->currentItem() != index
) {
426 switch (m_selectionBehavior
) {
428 m_selectionManager
->setCurrentItem(index
);
431 case SingleSelection
:
432 m_selectionManager
->setCurrentItem(index
);
433 m_selectionManager
->clearSelection();
434 m_selectionManager
->setSelected(index
, 1);
438 if (controlPressed
) {
439 m_selectionManager
->endAnchoredSelection();
442 m_selectionManager
->setCurrentItem(index
);
444 if (!shiftOrControlPressed
) {
445 m_selectionManager
->clearSelection();
446 m_selectionManager
->setSelected(index
, 1);
450 m_selectionManager
->beginAnchoredSelection(index
);
455 m_view
->scrollToItem(index
);
460 void KItemListController::slotChangeCurrentItem(const QString
& text
, bool searchFromNextItem
)
462 if (!m_model
|| m_model
->count() == 0) {
465 const int currentIndex
= m_selectionManager
->currentItem();
467 if (searchFromNextItem
) {
468 index
= m_model
->indexForKeyboardSearch(text
, (currentIndex
+ 1) % m_model
->count());
470 index
= m_model
->indexForKeyboardSearch(text
, currentIndex
);
473 m_selectionManager
->setCurrentItem(index
);
474 m_selectionManager
->clearSelection();
475 m_selectionManager
->setSelected(index
, 1);
476 m_selectionManager
->beginAnchoredSelection(index
);
477 m_view
->scrollToItem(index
);
481 void KItemListController::slotAutoActivationTimeout()
483 if (!m_model
|| !m_view
) {
487 const int index
= m_autoActivationTimer
->property("index").toInt();
488 if (index
< 0 || index
>= m_model
->count()) {
492 /* m_view->isUnderMouse() fixes a bug in the Folder-View-Panel and in the
495 * Bug: When you drag a file onto a Folder-View-Item or a Places-Item and
496 * then move away before the auto-activation timeout triggers, than the
497 * item still becomes activated/expanded.
499 * See Bug 293200 and 305783
501 if (m_model
->supportsDropping(index
) && m_view
->isUnderMouse()) {
502 if (m_view
->supportsItemExpanding() && m_model
->isExpandable(index
)) {
503 const bool expanded
= m_model
->isExpanded(index
);
504 m_model
->setExpanded(index
, !expanded
);
505 } else if (m_autoActivationBehavior
!= ExpansionOnly
) {
506 emit
itemActivated(index
);
511 bool KItemListController::inputMethodEvent(QInputMethodEvent
* event
)
517 bool KItemListController::mousePressEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
523 m_pressedMousePos
= transform
.map(event
->pos());
524 m_pressedIndex
= m_view
->itemAt(m_pressedMousePos
);
525 emit
mouseButtonPressed(m_pressedIndex
, event
->buttons());
527 if (m_view
->isAboveExpansionToggle(m_pressedIndex
, m_pressedMousePos
)) {
528 m_selectionManager
->endAnchoredSelection();
529 m_selectionManager
->setCurrentItem(m_pressedIndex
);
530 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
534 m_selectionTogglePressed
= m_view
->isAboveSelectionToggle(m_pressedIndex
, m_pressedMousePos
);
535 if (m_selectionTogglePressed
) {
536 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
537 // The previous anchored selection has been finished already in
538 // KItemListSelectionManager::setSelected(). We can safely change
539 // the current item and start a new anchored selection now.
540 m_selectionManager
->setCurrentItem(m_pressedIndex
);
541 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
545 const bool shiftPressed
= event
->modifiers() & Qt::ShiftModifier
;
546 const bool controlPressed
= event
->modifiers() & Qt::ControlModifier
;
548 // The previous selection is cleared if either
549 // 1. The selection mode is SingleSelection, or
550 // 2. the selection mode is MultiSelection, and *none* of the following conditions are met:
551 // a) Shift or Control are pressed.
552 // b) The clicked item is selected already. In that case, the user might want to:
553 // - start dragging multiple items, or
554 // - open the context menu and perform an action for all selected items.
555 const bool shiftOrControlPressed
= shiftPressed
|| controlPressed
;
556 const bool pressedItemAlreadySelected
= m_pressedIndex
>= 0 && m_selectionManager
->isSelected(m_pressedIndex
);
557 const bool clearSelection
= m_selectionBehavior
== SingleSelection
||
558 (!shiftOrControlPressed
&& !pressedItemAlreadySelected
);
559 if (clearSelection
) {
560 m_selectionManager
->clearSelection();
561 } else if (pressedItemAlreadySelected
&& !shiftOrControlPressed
&& (event
->buttons() & Qt::LeftButton
)) {
562 // The user might want to start dragging multiple items, but if he clicks the item
563 // in order to trigger it instead, the other selected items must be deselected.
564 // However, we do not know yet what the user is going to do.
565 // -> remember that the user pressed an item which had been selected already and
566 // clear the selection in mouseReleaseEvent(), unless the items are dragged.
567 m_clearSelectionIfItemsAreNotDragged
= true;
571 // Finish the anchored selection before the current index is changed
572 m_selectionManager
->endAnchoredSelection();
575 if (m_pressedIndex
>= 0) {
576 m_selectionManager
->setCurrentItem(m_pressedIndex
);
578 switch (m_selectionBehavior
) {
582 case SingleSelection
:
583 m_selectionManager
->setSelected(m_pressedIndex
);
587 if (controlPressed
&& !shiftPressed
) {
588 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
589 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
590 } else if (!shiftPressed
|| !m_selectionManager
->isAnchoredSelectionActive()) {
591 // Select the pressed item and start a new anchored selection
592 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Select
);
593 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
602 if (event
->buttons() & Qt::RightButton
) {
603 emit
itemContextMenuRequested(m_pressedIndex
, event
->screenPos());
609 if (event
->buttons() & Qt::RightButton
) {
610 const QRectF headerBounds
= m_view
->headerBoundaries();
611 if (headerBounds
.contains(event
->pos())) {
612 emit
headerContextMenuRequested(event
->screenPos());
614 emit
viewContextMenuRequested(event
->screenPos());
619 if (m_selectionBehavior
== MultiSelection
) {
620 QPointF startPos
= m_pressedMousePos
;
621 if (m_view
->scrollOrientation() == Qt::Vertical
) {
622 startPos
.ry() += m_view
->scrollOffset();
623 if (m_view
->itemSize().width() < 0) {
624 // Use a special rubberband for views that have only one column and
625 // expand the rubberband to use the whole width of the view.
629 startPos
.rx() += m_view
->scrollOffset();
632 m_oldSelection
= m_selectionManager
->selectedItems();
633 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
634 rubberBand
->setStartPosition(startPos
);
635 rubberBand
->setEndPosition(startPos
);
636 rubberBand
->setActive(true);
637 connect(rubberBand
, SIGNAL(endPositionChanged(QPointF
,QPointF
)), this, SLOT(slotRubberBandChanged()));
638 m_view
->setAutoScroll(true);
644 bool KItemListController::mouseMoveEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
650 if (m_pressedIndex
>= 0) {
651 // Check whether a dragging should be started
652 if (event
->buttons() & Qt::LeftButton
) {
653 const QPointF pos
= transform
.map(event
->pos());
654 if ((pos
- m_pressedMousePos
).manhattanLength() >= QApplication::startDragDistance()) {
655 if (!m_selectionManager
->isSelected(m_pressedIndex
)) {
656 // Always assure that the dragged item gets selected. Usually this is already
657 // done on the mouse-press event, but when using the selection-toggle on a
658 // selected item the dragged item is not selected yet.
659 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
661 // A selected item has been clicked to drag all selected items
662 // -> the selection should not be cleared when the mouse button is released.
663 m_clearSelectionIfItemsAreNotDragged
= false;
670 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
671 if (rubberBand
->isActive()) {
672 QPointF endPos
= transform
.map(event
->pos());
674 // Update the current item.
675 const int newCurrent
= m_view
->itemAt(endPos
);
676 if (newCurrent
>= 0) {
677 // It's expected that the new current index is also the new anchor (bug 163451).
678 m_selectionManager
->endAnchoredSelection();
679 m_selectionManager
->setCurrentItem(newCurrent
);
680 m_selectionManager
->beginAnchoredSelection(newCurrent
);
683 if (m_view
->scrollOrientation() == Qt::Vertical
) {
684 endPos
.ry() += m_view
->scrollOffset();
685 if (m_view
->itemSize().width() < 0) {
686 // Use a special rubberband for views that have only one column and
687 // expand the rubberband to use the whole width of the view.
688 endPos
.setX(m_view
->size().width());
691 endPos
.rx() += m_view
->scrollOffset();
693 rubberBand
->setEndPosition(endPos
);
700 bool KItemListController::mouseReleaseEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
706 emit
mouseButtonReleased(m_pressedIndex
, event
->buttons());
708 const bool isAboveSelectionToggle
= m_view
->isAboveSelectionToggle(m_pressedIndex
, m_pressedMousePos
);
709 if (isAboveSelectionToggle
) {
710 m_selectionTogglePressed
= false;
714 if (!isAboveSelectionToggle
&& m_selectionTogglePressed
) {
715 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
716 m_selectionTogglePressed
= false;
720 const bool shiftOrControlPressed
= event
->modifiers() & Qt::ShiftModifier
||
721 event
->modifiers() & Qt::ControlModifier
;
723 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
724 if (rubberBand
->isActive()) {
725 disconnect(rubberBand
, SIGNAL(endPositionChanged(QPointF
,QPointF
)), this, SLOT(slotRubberBandChanged()));
726 rubberBand
->setActive(false);
727 m_oldSelection
.clear();
728 m_view
->setAutoScroll(false);
731 const QPointF pos
= transform
.map(event
->pos());
732 const int index
= m_view
->itemAt(pos
);
734 if (index
>= 0 && index
== m_pressedIndex
) {
735 // The release event is done above the same item as the press event
737 if (m_clearSelectionIfItemsAreNotDragged
) {
738 // A selected item has been clicked, but no drag operation has been started
739 // -> clear the rest of the selection.
740 m_selectionManager
->clearSelection();
741 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Select
);
742 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
745 if (event
->button() & Qt::LeftButton
) {
746 bool emitItemActivated
= true;
747 if (m_view
->isAboveExpansionToggle(index
, pos
)) {
748 const bool expanded
= m_model
->isExpanded(index
);
749 m_model
->setExpanded(index
, !expanded
);
751 emit
itemExpansionToggleClicked(index
);
752 emitItemActivated
= false;
753 } else if (shiftOrControlPressed
) {
754 // The mouse click should only update the selection, not trigger the item
755 emitItemActivated
= false;
756 } else if (!m_singleClickActivation
) {
757 emitItemActivated
= false;
759 if (emitItemActivated
) {
760 emit
itemActivated(index
);
762 } else if (event
->button() & Qt::MidButton
) {
763 emit
itemMiddleClicked(index
);
767 m_pressedMousePos
= QPointF();
769 m_clearSelectionIfItemsAreNotDragged
= false;
773 bool KItemListController::mouseDoubleClickEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
775 const QPointF pos
= transform
.map(event
->pos());
776 const int index
= m_view
->itemAt(pos
);
778 // Expand item if desired - See Bug 295573
779 if (m_mouseDoubleClickAction
!= ActivateItemOnly
) {
780 if (m_view
&& m_model
&& m_view
->supportsItemExpanding() && m_model
->isExpandable(index
)) {
781 const bool expanded
= m_model
->isExpanded(index
);
782 m_model
->setExpanded(index
, !expanded
);
786 bool emitItemActivated
= !m_singleClickActivation
&&
787 (event
->button() & Qt::LeftButton
) &&
788 index
>= 0 && index
< m_model
->count();
789 if (emitItemActivated
) {
790 emit
itemActivated(index
);
795 bool KItemListController::dragEnterEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
802 bool KItemListController::dragLeaveEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
807 m_view
->setAutoScroll(false);
808 m_view
->hideDropIndicator();
810 KItemListWidget
* widget
= hoveredWidget();
812 widget
->setHovered(false);
813 emit
itemUnhovered(widget
->index());
818 bool KItemListController::dragMoveEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
820 if (!m_model
|| !m_view
) {
824 event
->acceptProposedAction();
826 KItemListWidget
* oldHoveredWidget
= hoveredWidget();
828 const QPointF pos
= transform
.map(event
->pos());
829 KItemListWidget
* newHoveredWidget
= widgetForPos(pos
);
831 if (oldHoveredWidget
!= newHoveredWidget
) {
832 m_autoActivationTimer
->stop();
834 if (oldHoveredWidget
) {
835 oldHoveredWidget
->setHovered(false);
836 emit
itemUnhovered(oldHoveredWidget
->index());
839 if (newHoveredWidget
) {
840 bool droppingBetweenItems
= false;
841 if (m_model
->sortRole().isEmpty()) {
842 // The model supports inserting items between other items.
843 droppingBetweenItems
= (m_view
->showDropIndicator(pos
) >= 0);
846 const int index
= newHoveredWidget
->index();
847 if (!droppingBetweenItems
&& m_model
->supportsDropping(index
)) {
848 // Something has been dragged on an item.
849 m_view
->hideDropIndicator();
850 newHoveredWidget
->setHovered(true);
851 emit
itemHovered(index
);
853 if (m_autoActivationTimer
->interval() >= 0) {
854 m_autoActivationTimer
->setProperty("index", index
);
855 m_autoActivationTimer
->start();
864 bool KItemListController::dropEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
870 m_autoActivationTimer
->stop();
871 m_view
->setAutoScroll(false);
873 const QPointF pos
= transform
.map(event
->pos());
875 int dropAboveIndex
= -1;
876 if (m_model
->sortRole().isEmpty()) {
877 // The model supports inserting of items between other items.
878 dropAboveIndex
= m_view
->showDropIndicator(pos
);
881 if (dropAboveIndex
>= 0) {
882 // Something has been dropped between two items.
883 m_view
->hideDropIndicator();
884 emit
aboveItemDropEvent(dropAboveIndex
, event
);
886 // Something has been dropped on an item or on an empty part of the view.
887 emit
itemDropEvent(m_view
->itemAt(pos
), event
);
893 bool KItemListController::hoverEnterEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
900 bool KItemListController::hoverMoveEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
903 if (!m_model
|| !m_view
) {
907 KItemListWidget
* oldHoveredWidget
= hoveredWidget();
908 const QPointF pos
= transform
.map(event
->pos());
909 KItemListWidget
* newHoveredWidget
= widgetForPos(pos
);
911 if (oldHoveredWidget
!= newHoveredWidget
) {
912 if (oldHoveredWidget
) {
913 oldHoveredWidget
->setHovered(false);
914 emit
itemUnhovered(oldHoveredWidget
->index());
917 if (newHoveredWidget
) {
918 newHoveredWidget
->setHovered(true);
919 emit
itemHovered(newHoveredWidget
->index());
926 bool KItemListController::hoverLeaveEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
931 if (!m_model
|| !m_view
) {
935 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
936 if (widget
->isHovered()) {
937 widget
->setHovered(false);
938 emit
itemUnhovered(widget
->index());
944 bool KItemListController::wheelEvent(QGraphicsSceneWheelEvent
* event
, const QTransform
& transform
)
951 bool KItemListController::resizeEvent(QGraphicsSceneResizeEvent
* event
, const QTransform
& transform
)
958 bool KItemListController::processEvent(QEvent
* event
, const QTransform
& transform
)
964 switch (event
->type()) {
965 case QEvent::KeyPress
:
966 return keyPressEvent(static_cast<QKeyEvent
*>(event
));
967 case QEvent::InputMethod
:
968 return inputMethodEvent(static_cast<QInputMethodEvent
*>(event
));
969 case QEvent::GraphicsSceneMousePress
:
970 return mousePressEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
971 case QEvent::GraphicsSceneMouseMove
:
972 return mouseMoveEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
973 case QEvent::GraphicsSceneMouseRelease
:
974 return mouseReleaseEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
975 case QEvent::GraphicsSceneMouseDoubleClick
:
976 return mouseDoubleClickEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
977 case QEvent::GraphicsSceneWheel
:
978 return wheelEvent(static_cast<QGraphicsSceneWheelEvent
*>(event
), QTransform());
979 case QEvent::GraphicsSceneDragEnter
:
980 return dragEnterEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
981 case QEvent::GraphicsSceneDragLeave
:
982 return dragLeaveEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
983 case QEvent::GraphicsSceneDragMove
:
984 return dragMoveEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
985 case QEvent::GraphicsSceneDrop
:
986 return dropEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
987 case QEvent::GraphicsSceneHoverEnter
:
988 return hoverEnterEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
989 case QEvent::GraphicsSceneHoverMove
:
990 return hoverMoveEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
991 case QEvent::GraphicsSceneHoverLeave
:
992 return hoverLeaveEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
993 case QEvent::GraphicsSceneResize
:
994 return resizeEvent(static_cast<QGraphicsSceneResizeEvent
*>(event
), transform
);
1002 void KItemListController::slotViewScrollOffsetChanged(qreal current
, qreal previous
)
1008 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
1009 if (rubberBand
->isActive()) {
1010 const qreal diff
= current
- previous
;
1011 // TODO: Ideally just QCursor::pos() should be used as
1012 // new end-position but it seems there is no easy way
1013 // to have something like QWidget::mapFromGlobal() for QGraphicsWidget
1014 // (... or I just missed an easy way to do the mapping)
1015 QPointF endPos
= rubberBand
->endPosition();
1016 if (m_view
->scrollOrientation() == Qt::Vertical
) {
1017 endPos
.ry() += diff
;
1019 endPos
.rx() += diff
;
1022 rubberBand
->setEndPosition(endPos
);
1026 void KItemListController::slotRubberBandChanged()
1028 if (!m_view
|| !m_model
|| m_model
->count() <= 0) {
1032 const KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
1033 const QPointF startPos
= rubberBand
->startPosition();
1034 const QPointF endPos
= rubberBand
->endPosition();
1035 QRectF rubberBandRect
= QRectF(startPos
, endPos
).normalized();
1037 const bool scrollVertical
= (m_view
->scrollOrientation() == Qt::Vertical
);
1038 if (scrollVertical
) {
1039 rubberBandRect
.translate(0, -m_view
->scrollOffset());
1041 rubberBandRect
.translate(-m_view
->scrollOffset(), 0);
1044 if (!m_oldSelection
.isEmpty()) {
1045 // Clear the old selection that was available before the rubberband has
1046 // been activated in case if no Shift- or Control-key are pressed
1047 const bool shiftOrControlPressed
= QApplication::keyboardModifiers() & Qt::ShiftModifier
||
1048 QApplication::keyboardModifiers() & Qt::ControlModifier
;
1049 if (!shiftOrControlPressed
) {
1050 m_oldSelection
.clear();
1054 QSet
<int> selectedItems
;
1056 // Select all visible items that intersect with the rubberband
1057 foreach (const KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
1058 const int index
= widget
->index();
1060 const QRectF widgetRect
= m_view
->itemRect(index
);
1061 if (widgetRect
.intersects(rubberBandRect
)) {
1062 const QRectF iconRect
= widget
->iconRect().translated(widgetRect
.topLeft());
1063 const QRectF textRect
= widget
->textRect().translated(widgetRect
.topLeft());
1064 if (iconRect
.intersects(rubberBandRect
) || textRect
.intersects(rubberBandRect
)) {
1065 selectedItems
.insert(index
);
1070 // Select all invisible items that intersect with the rubberband. Instead of
1071 // iterating all items only the area which might be touched by the rubberband
1073 const bool increaseIndex
= scrollVertical
?
1074 startPos
.y() > endPos
.y(): startPos
.x() > endPos
.x();
1076 int index
= increaseIndex
? m_view
->lastVisibleIndex() + 1 : m_view
->firstVisibleIndex() - 1;
1077 bool selectionFinished
= false;
1079 const QRectF widgetRect
= m_view
->itemRect(index
);
1080 if (widgetRect
.intersects(rubberBandRect
)) {
1081 selectedItems
.insert(index
);
1084 if (increaseIndex
) {
1086 selectionFinished
= (index
>= m_model
->count()) ||
1087 ( scrollVertical
&& widgetRect
.top() > rubberBandRect
.bottom()) ||
1088 (!scrollVertical
&& widgetRect
.left() > rubberBandRect
.right());
1091 selectionFinished
= (index
< 0) ||
1092 ( scrollVertical
&& widgetRect
.bottom() < rubberBandRect
.top()) ||
1093 (!scrollVertical
&& widgetRect
.right() < rubberBandRect
.left());
1095 } while (!selectionFinished
);
1097 if (QApplication::keyboardModifiers() & Qt::ControlModifier
) {
1098 // If Control is pressed, the selection state of all items in the rubberband is toggled.
1099 // Therefore, the new selection contains:
1100 // 1. All previously selected items which are not inside the rubberband, and
1101 // 2. all items inside the rubberband which have not been selected previously.
1102 m_selectionManager
->setSelectedItems((m_oldSelection
- selectedItems
) + (selectedItems
- m_oldSelection
));
1105 m_selectionManager
->setSelectedItems(selectedItems
+ m_oldSelection
);
1109 void KItemListController::startDragging()
1111 if (!m_view
|| !m_model
) {
1115 const QSet
<int> selectedItems
= m_selectionManager
->selectedItems();
1116 if (selectedItems
.isEmpty()) {
1120 QMimeData
* data
= m_model
->createMimeData(selectedItems
);
1125 // The created drag object will be owned and deleted
1126 // by QApplication::activeWindow().
1127 QDrag
* drag
= new QDrag(QApplication::activeWindow());
1128 drag
->setMimeData(data
);
1130 const QPixmap pixmap
= m_view
->createDragPixmap(selectedItems
);
1131 drag
->setPixmap(pixmap
);
1133 const QPoint
hotSpot(pixmap
.width() / 2, 0);
1134 drag
->setHotSpot(hotSpot
);
1136 drag
->exec(Qt::MoveAction
| Qt::CopyAction
| Qt::LinkAction
, Qt::CopyAction
);
1139 KItemListWidget
* KItemListController::hoveredWidget() const
1143 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
1144 if (widget
->isHovered()) {
1152 KItemListWidget
* KItemListController::widgetForPos(const QPointF
& pos
) const
1156 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
1157 const QPointF mappedPos
= widget
->mapFromItem(m_view
, pos
);
1159 const bool hovered
= widget
->contains(mappedPos
) &&
1160 !widget
->expansionToggleRect().contains(mappedPos
);
1169 void KItemListController::updateKeyboardAnchor()
1171 const bool validAnchor
= m_keyboardAnchorIndex
>= 0 &&
1172 m_keyboardAnchorIndex
< m_model
->count() &&
1173 keyboardAnchorPos(m_keyboardAnchorIndex
) == m_keyboardAnchorPos
;
1175 const int index
= m_selectionManager
->currentItem();
1176 m_keyboardAnchorIndex
= index
;
1177 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
1181 int KItemListController::nextRowIndex(int index
) const
1183 if (m_keyboardAnchorIndex
< 0) {
1187 const int maxIndex
= m_model
->count() - 1;
1188 if (index
== maxIndex
) {
1192 // Calculate the index of the last column inside the row of the current index
1193 int lastColumnIndex
= index
;
1194 while (keyboardAnchorPos(lastColumnIndex
+ 1) > keyboardAnchorPos(lastColumnIndex
)) {
1196 if (lastColumnIndex
>= maxIndex
) {
1201 // Based on the last column index go to the next row and calculate the nearest index
1202 // that is below the current index
1203 int nextRowIndex
= lastColumnIndex
+ 1;
1204 int searchIndex
= nextRowIndex
;
1205 qreal minDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(nextRowIndex
));
1206 while (searchIndex
< maxIndex
&& keyboardAnchorPos(searchIndex
+ 1) > keyboardAnchorPos(searchIndex
)) {
1208 const qreal searchDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(searchIndex
));
1209 if (searchDiff
< minDiff
) {
1210 minDiff
= searchDiff
;
1211 nextRowIndex
= searchIndex
;
1215 return nextRowIndex
;
1218 int KItemListController::previousRowIndex(int index
) const
1220 if (m_keyboardAnchorIndex
< 0 || index
== 0) {
1224 // Calculate the index of the first column inside the row of the current index
1225 int firstColumnIndex
= index
;
1226 while (keyboardAnchorPos(firstColumnIndex
- 1) < keyboardAnchorPos(firstColumnIndex
)) {
1228 if (firstColumnIndex
<= 0) {
1233 // Based on the first column index go to the previous row and calculate the nearest index
1234 // that is above the current index
1235 int previousRowIndex
= firstColumnIndex
- 1;
1236 int searchIndex
= previousRowIndex
;
1237 qreal minDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(previousRowIndex
));
1238 while (searchIndex
> 0 && keyboardAnchorPos(searchIndex
- 1) < keyboardAnchorPos(searchIndex
)) {
1240 const qreal searchDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(searchIndex
));
1241 if (searchDiff
< minDiff
) {
1242 minDiff
= searchDiff
;
1243 previousRowIndex
= searchIndex
;
1247 return previousRowIndex
;
1250 qreal
KItemListController::keyboardAnchorPos(int index
) const
1252 const QRectF itemRect
= m_view
->itemRect(index
);
1253 if (!itemRect
.isEmpty()) {
1254 return (m_view
->scrollOrientation() == Qt::Vertical
) ? itemRect
.x() : itemRect
.y();
1260 void KItemListController::updateExtendedSelectionRegion()
1263 const bool extend
= (m_selectionBehavior
!= MultiSelection
);
1264 KItemListStyleOption option
= m_view
->styleOption();
1265 if (option
.extendedSelectionRegion
!= extend
) {
1266 option
.extendedSelectionRegion
= extend
;
1267 m_view
->setStyleOption(option
);
1272 #include "kitemlistcontroller.moc"