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>
43 #include <QAccessible>
45 KItemListController::KItemListController(KItemModelBase
* model
, KItemListView
* view
, QObject
* parent
) :
47 m_singleClickActivationEnforced(false),
48 m_selectionTogglePressed(false),
49 m_clearSelectionIfItemsAreNotDragged(false),
50 m_selectionBehavior(NoSelection
),
51 m_autoActivationBehavior(ActivationAndExpansion
),
52 m_mouseDoubleClickAction(ActivateItemOnly
),
55 m_selectionManager(new KItemListSelectionManager(this)),
56 m_keyboardManager(new KItemListKeyboardSearchManager(this)),
59 m_autoActivationTimer(0),
61 m_keyboardAnchorIndex(-1),
62 m_keyboardAnchorPos(0)
64 connect(m_keyboardManager
, SIGNAL(changeCurrentItem(QString
,bool)),
65 this, SLOT(slotChangeCurrentItem(QString
,bool)));
66 connect(m_selectionManager
, SIGNAL(currentChanged(int,int)),
67 m_keyboardManager
, SLOT(slotCurrentChanged(int,int)));
69 m_autoActivationTimer
= new QTimer(this);
70 m_autoActivationTimer
->setSingleShot(true);
71 m_autoActivationTimer
->setInterval(-1);
72 connect(m_autoActivationTimer
, SIGNAL(timeout()), this, SLOT(slotAutoActivationTimeout()));
78 KItemListController::~KItemListController()
87 void KItemListController::setModel(KItemModelBase
* model
)
89 if (m_model
== model
) {
93 KItemModelBase
* oldModel
= m_model
;
95 oldModel
->deleteLater();
100 m_model
->setParent(this);
104 m_view
->setModel(m_model
);
107 m_selectionManager
->setModel(m_model
);
109 emit
modelChanged(m_model
, oldModel
);
112 KItemModelBase
* KItemListController::model() const
117 KItemListSelectionManager
* KItemListController::selectionManager() const
119 return m_selectionManager
;
122 void KItemListController::setView(KItemListView
* view
)
124 if (m_view
== view
) {
128 KItemListView
* oldView
= m_view
;
130 disconnect(oldView
, SIGNAL(scrollOffsetChanged(qreal
,qreal
)), this, SLOT(slotViewScrollOffsetChanged(qreal
,qreal
)));
131 oldView
->deleteLater();
137 m_view
->setParent(this);
138 m_view
->setController(this);
139 m_view
->setModel(m_model
);
140 connect(m_view
, SIGNAL(scrollOffsetChanged(qreal
,qreal
)), this, SLOT(slotViewScrollOffsetChanged(qreal
,qreal
)));
141 updateExtendedSelectionRegion();
144 emit
viewChanged(m_view
, oldView
);
147 KItemListView
* KItemListController::view() const
152 void KItemListController::setSelectionBehavior(SelectionBehavior behavior
)
154 m_selectionBehavior
= behavior
;
155 updateExtendedSelectionRegion();
158 KItemListController::SelectionBehavior
KItemListController::selectionBehavior() const
160 return m_selectionBehavior
;
163 void KItemListController::setAutoActivationBehavior(AutoActivationBehavior behavior
)
165 m_autoActivationBehavior
= behavior
;
168 KItemListController::AutoActivationBehavior
KItemListController::autoActivationBehavior() const
170 return m_autoActivationBehavior
;
173 void KItemListController::setMouseDoubleClickAction(MouseDoubleClickAction action
)
175 m_mouseDoubleClickAction
= action
;
178 KItemListController::MouseDoubleClickAction
KItemListController::mouseDoubleClickAction() const
180 return m_mouseDoubleClickAction
;
183 void KItemListController::setAutoActivationDelay(int delay
)
185 m_autoActivationTimer
->setInterval(delay
);
188 int KItemListController::autoActivationDelay() const
190 return m_autoActivationTimer
->interval();
193 void KItemListController::setSingleClickActivationEnforced(bool singleClick
)
195 m_singleClickActivationEnforced
= singleClick
;
198 bool KItemListController::singleClickActivationEnforced() const
200 return m_singleClickActivationEnforced
;
203 bool KItemListController::showEvent(QShowEvent
* event
)
209 bool KItemListController::hideEvent(QHideEvent
* event
)
215 bool KItemListController::keyPressEvent(QKeyEvent
* event
)
217 int index
= m_selectionManager
->currentItem();
218 int key
= event
->key();
220 // Handle the expanding/collapsing of items
221 if (m_view
->supportsItemExpanding() && m_model
->isExpandable(index
)) {
222 if (key
== Qt::Key_Right
) {
223 if (m_model
->setExpanded(index
, true)) {
226 } else if (key
== Qt::Key_Left
) {
227 if (m_model
->setExpanded(index
, false)) {
233 const bool shiftPressed
= event
->modifiers() & Qt::ShiftModifier
;
234 const bool controlPressed
= event
->modifiers() & Qt::ControlModifier
;
235 const bool shiftOrControlPressed
= shiftPressed
|| controlPressed
;
237 const int itemCount
= m_model
->count();
239 // For horizontal scroll orientation, transform
240 // the arrow keys to simplify the event handling.
241 if (m_view
->scrollOrientation() == Qt::Horizontal
) {
243 case Qt::Key_Up
: key
= Qt::Key_Left
; break;
244 case Qt::Key_Down
: key
= Qt::Key_Right
; break;
245 case Qt::Key_Left
: key
= Qt::Key_Up
; break;
246 case Qt::Key_Right
: key
= Qt::Key_Down
; break;
251 const bool selectSingleItem
= m_selectionBehavior
!= NoSelection
&&
253 (key
== Qt::Key_Home
|| key
== Qt::Key_End
||
254 key
== Qt::Key_Up
|| key
== Qt::Key_Down
||
255 key
== Qt::Key_Left
|| key
== Qt::Key_Right
);
256 if (selectSingleItem
) {
257 const int current
= m_selectionManager
->currentItem();
258 m_selectionManager
->setSelected(current
);
265 m_keyboardAnchorIndex
= index
;
266 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
270 index
= itemCount
- 1;
271 m_keyboardAnchorIndex
= index
;
272 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
277 const int expandedParentsCount
= m_model
->expandedParentsCount(index
);
278 if (expandedParentsCount
== 0) {
281 // Go to the parent of the current item.
284 } while (index
> 0 && m_model
->expandedParentsCount(index
) == expandedParentsCount
);
286 m_keyboardAnchorIndex
= index
;
287 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
292 if (index
< itemCount
- 1) {
294 m_keyboardAnchorIndex
= index
;
295 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
300 updateKeyboardAnchor();
301 index
= previousRowIndex(index
);
305 updateKeyboardAnchor();
306 index
= nextRowIndex(index
);
310 if (m_view
->scrollOrientation() == Qt::Horizontal
) {
311 // The new current index should correspond to the first item in the current column.
312 int newIndex
= qMax(index
- 1, 0);
313 while (newIndex
!= index
&& m_view
->itemRect(newIndex
).topLeft().y() < m_view
->itemRect(index
).topLeft().y()) {
315 newIndex
= qMax(index
- 1, 0);
317 m_keyboardAnchorIndex
= index
;
318 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
320 const qreal currentItemBottom
= m_view
->itemRect(index
).bottomLeft().y();
321 const qreal height
= m_view
->geometry().height();
323 // The new current item should be the first item in the current
324 // column whose itemRect's top coordinate is larger than targetY.
325 const qreal targetY
= currentItemBottom
- height
;
327 updateKeyboardAnchor();
328 int newIndex
= previousRowIndex(index
);
331 updateKeyboardAnchor();
332 newIndex
= previousRowIndex(index
);
333 } while (m_view
->itemRect(newIndex
).topLeft().y() > targetY
&& newIndex
!= index
);
337 case Qt::Key_PageDown
:
338 if (m_view
->scrollOrientation() == Qt::Horizontal
) {
339 // The new current index should correspond to the last item in the current column.
340 int newIndex
= qMin(index
+ 1, m_model
->count() - 1);
341 while (newIndex
!= index
&& m_view
->itemRect(newIndex
).topLeft().y() > m_view
->itemRect(index
).topLeft().y()) {
343 newIndex
= qMin(index
+ 1, m_model
->count() - 1);
345 m_keyboardAnchorIndex
= index
;
346 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
348 const qreal currentItemTop
= m_view
->itemRect(index
).topLeft().y();
349 const qreal height
= m_view
->geometry().height();
351 // The new current item should be the last item in the current
352 // column whose itemRect's bottom coordinate is smaller than targetY.
353 const qreal targetY
= currentItemTop
+ height
;
355 updateKeyboardAnchor();
356 int newIndex
= nextRowIndex(index
);
359 updateKeyboardAnchor();
360 newIndex
= nextRowIndex(index
);
361 } while (m_view
->itemRect(newIndex
).bottomLeft().y() < targetY
&& newIndex
!= index
);
366 case Qt::Key_Return
: {
367 const KItemSet selectedItems
= m_selectionManager
->selectedItems();
368 if (selectedItems
.count() >= 2) {
369 emit
itemsActivated(selectedItems
);
370 } else if (selectedItems
.count() == 1) {
371 emit
itemActivated(selectedItems
.first());
373 emit
itemActivated(index
);
379 // Emit the signal itemContextMenuRequested() in case if at least one
380 // item is selected. Otherwise the signal viewContextMenuRequested() will be emitted.
381 const KItemSet selectedItems
= m_selectionManager
->selectedItems();
383 if (selectedItems
.count() >= 2) {
384 const int currentItemIndex
= m_selectionManager
->currentItem();
385 index
= selectedItems
.contains(currentItemIndex
)
386 ? currentItemIndex
: selectedItems
.first();
387 } else if (selectedItems
.count() == 1) {
388 index
= selectedItems
.first();
392 const QRectF contextRect
= m_view
->itemContextRect(index
);
393 const QPointF
pos(m_view
->scene()->views().first()->mapToGlobal(contextRect
.bottomRight().toPoint()));
394 emit
itemContextMenuRequested(index
, pos
);
396 emit
viewContextMenuRequested(QCursor::pos());
402 if (m_selectionBehavior
!= SingleSelection
) {
403 m_selectionManager
->clearSelection();
405 m_keyboardManager
->cancelSearch();
406 emit
escapePressed();
410 if (m_selectionBehavior
== MultiSelection
) {
411 if (controlPressed
) {
412 // Toggle the selection state of the current item.
413 m_selectionManager
->endAnchoredSelection();
414 m_selectionManager
->setSelected(index
, 1, KItemListSelectionManager::Toggle
);
415 m_selectionManager
->beginAnchoredSelection(index
);
418 // Select the current item if it is not selected yet.
419 const int current
= m_selectionManager
->currentItem();
420 if (!m_selectionManager
->isSelected(current
)) {
421 m_selectionManager
->setSelected(current
);
426 // Fall through to the default case and add the Space to the current search string.
429 m_keyboardManager
->addKeys(event
->text());
430 // Make sure unconsumed events get propagated up the chain. #302329
435 if (m_selectionManager
->currentItem() != index
) {
436 switch (m_selectionBehavior
) {
438 m_selectionManager
->setCurrentItem(index
);
441 case SingleSelection
:
442 m_selectionManager
->setCurrentItem(index
);
443 m_selectionManager
->clearSelection();
444 m_selectionManager
->setSelected(index
, 1);
448 if (controlPressed
) {
449 m_selectionManager
->endAnchoredSelection();
452 m_selectionManager
->setCurrentItem(index
);
454 if (!shiftOrControlPressed
) {
455 m_selectionManager
->clearSelection();
456 m_selectionManager
->setSelected(index
, 1);
460 m_selectionManager
->beginAnchoredSelection(index
);
465 m_view
->scrollToItem(index
);
470 void KItemListController::slotChangeCurrentItem(const QString
& text
, bool searchFromNextItem
)
472 if (!m_model
|| m_model
->count() == 0) {
475 const int currentIndex
= m_selectionManager
->currentItem();
477 if (searchFromNextItem
) {
478 index
= m_model
->indexForKeyboardSearch(text
, (currentIndex
+ 1) % m_model
->count());
480 index
= m_model
->indexForKeyboardSearch(text
, currentIndex
);
483 m_selectionManager
->setCurrentItem(index
);
485 if (m_selectionBehavior
!= NoSelection
) {
486 m_selectionManager
->clearSelection();
487 m_selectionManager
->setSelected(index
, 1);
488 m_selectionManager
->beginAnchoredSelection(index
);
491 m_view
->scrollToItem(index
);
495 void KItemListController::slotAutoActivationTimeout()
497 if (!m_model
|| !m_view
) {
501 const int index
= m_autoActivationTimer
->property("index").toInt();
502 if (index
< 0 || index
>= m_model
->count()) {
506 /* m_view->isUnderMouse() fixes a bug in the Folder-View-Panel and in the
509 * Bug: When you drag a file onto a Folder-View-Item or a Places-Item and
510 * then move away before the auto-activation timeout triggers, than the
511 * item still becomes activated/expanded.
513 * See Bug 293200 and 305783
515 if (m_model
->supportsDropping(index
) && m_view
->isUnderMouse()) {
516 if (m_view
->supportsItemExpanding() && m_model
->isExpandable(index
)) {
517 const bool expanded
= m_model
->isExpanded(index
);
518 m_model
->setExpanded(index
, !expanded
);
519 } else if (m_autoActivationBehavior
!= ExpansionOnly
) {
520 emit
itemActivated(index
);
525 bool KItemListController::inputMethodEvent(QInputMethodEvent
* event
)
531 bool KItemListController::mousePressEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
537 m_pressedMousePos
= transform
.map(event
->pos());
538 m_pressedIndex
= m_view
->itemAt(m_pressedMousePos
);
539 emit
mouseButtonPressed(m_pressedIndex
, event
->buttons());
541 // TODO: Qt5: Replace Qt::XButton1 by Qt::BackButton and Qt::XButton2 by Qt::ForwardButton
542 if (event
->buttons() & (Qt::XButton1
| Qt::XButton2
)) {
543 // Do not select items when clicking the back/forward buttons, see
544 // https://bugs.kde.org/show_bug.cgi?id=327412.
548 if (m_view
->isAboveExpansionToggle(m_pressedIndex
, m_pressedMousePos
)) {
549 m_selectionManager
->endAnchoredSelection();
550 m_selectionManager
->setCurrentItem(m_pressedIndex
);
551 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
555 m_selectionTogglePressed
= m_view
->isAboveSelectionToggle(m_pressedIndex
, m_pressedMousePos
);
556 if (m_selectionTogglePressed
) {
557 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
558 // The previous anchored selection has been finished already in
559 // KItemListSelectionManager::setSelected(). We can safely change
560 // the current item and start a new anchored selection now.
561 m_selectionManager
->setCurrentItem(m_pressedIndex
);
562 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
566 const bool shiftPressed
= event
->modifiers() & Qt::ShiftModifier
;
567 const bool controlPressed
= event
->modifiers() & Qt::ControlModifier
;
569 // The previous selection is cleared if either
570 // 1. The selection mode is SingleSelection, or
571 // 2. the selection mode is MultiSelection, and *none* of the following conditions are met:
572 // a) Shift or Control are pressed.
573 // b) The clicked item is selected already. In that case, the user might want to:
574 // - start dragging multiple items, or
575 // - open the context menu and perform an action for all selected items.
576 const bool shiftOrControlPressed
= shiftPressed
|| controlPressed
;
577 const bool pressedItemAlreadySelected
= m_pressedIndex
>= 0 && m_selectionManager
->isSelected(m_pressedIndex
);
578 const bool clearSelection
= m_selectionBehavior
== SingleSelection
||
579 (!shiftOrControlPressed
&& !pressedItemAlreadySelected
);
580 if (clearSelection
) {
581 m_selectionManager
->clearSelection();
582 } else if (pressedItemAlreadySelected
&& !shiftOrControlPressed
&& (event
->buttons() & Qt::LeftButton
)) {
583 // The user might want to start dragging multiple items, but if he clicks the item
584 // in order to trigger it instead, the other selected items must be deselected.
585 // However, we do not know yet what the user is going to do.
586 // -> remember that the user pressed an item which had been selected already and
587 // clear the selection in mouseReleaseEvent(), unless the items are dragged.
588 m_clearSelectionIfItemsAreNotDragged
= true;
592 // Finish the anchored selection before the current index is changed
593 m_selectionManager
->endAnchoredSelection();
596 if (m_pressedIndex
>= 0) {
597 m_selectionManager
->setCurrentItem(m_pressedIndex
);
599 switch (m_selectionBehavior
) {
603 case SingleSelection
:
604 m_selectionManager
->setSelected(m_pressedIndex
);
608 if (controlPressed
&& !shiftPressed
) {
609 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
610 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
611 } else if (!shiftPressed
|| !m_selectionManager
->isAnchoredSelectionActive()) {
612 // Select the pressed item and start a new anchored selection
613 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Select
);
614 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
623 if (event
->buttons() & Qt::RightButton
) {
624 emit
itemContextMenuRequested(m_pressedIndex
, event
->screenPos());
630 if (event
->buttons() & Qt::RightButton
) {
631 const QRectF headerBounds
= m_view
->headerBoundaries();
632 if (headerBounds
.contains(event
->pos())) {
633 emit
headerContextMenuRequested(event
->screenPos());
635 emit
viewContextMenuRequested(event
->screenPos());
640 if (m_selectionBehavior
== MultiSelection
) {
641 QPointF startPos
= m_pressedMousePos
;
642 if (m_view
->scrollOrientation() == Qt::Vertical
) {
643 startPos
.ry() += m_view
->scrollOffset();
644 if (m_view
->itemSize().width() < 0) {
645 // Use a special rubberband for views that have only one column and
646 // expand the rubberband to use the whole width of the view.
650 startPos
.rx() += m_view
->scrollOffset();
653 m_oldSelection
= m_selectionManager
->selectedItems();
654 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
655 rubberBand
->setStartPosition(startPos
);
656 rubberBand
->setEndPosition(startPos
);
657 rubberBand
->setActive(true);
658 connect(rubberBand
, SIGNAL(endPositionChanged(QPointF
,QPointF
)), this, SLOT(slotRubberBandChanged()));
659 m_view
->setAutoScroll(true);
665 bool KItemListController::mouseMoveEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
671 if (m_pressedIndex
>= 0) {
672 // Check whether a dragging should be started
673 if (event
->buttons() & Qt::LeftButton
) {
674 const QPointF pos
= transform
.map(event
->pos());
675 if ((pos
- m_pressedMousePos
).manhattanLength() >= QApplication::startDragDistance()) {
676 if (!m_selectionManager
->isSelected(m_pressedIndex
)) {
677 // Always assure that the dragged item gets selected. Usually this is already
678 // done on the mouse-press event, but when using the selection-toggle on a
679 // selected item the dragged item is not selected yet.
680 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
682 // A selected item has been clicked to drag all selected items
683 // -> the selection should not be cleared when the mouse button is released.
684 m_clearSelectionIfItemsAreNotDragged
= false;
691 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
692 if (rubberBand
->isActive()) {
693 QPointF endPos
= transform
.map(event
->pos());
695 // Update the current item.
696 const int newCurrent
= m_view
->itemAt(endPos
);
697 if (newCurrent
>= 0) {
698 // It's expected that the new current index is also the new anchor (bug 163451).
699 m_selectionManager
->endAnchoredSelection();
700 m_selectionManager
->setCurrentItem(newCurrent
);
701 m_selectionManager
->beginAnchoredSelection(newCurrent
);
704 if (m_view
->scrollOrientation() == Qt::Vertical
) {
705 endPos
.ry() += m_view
->scrollOffset();
706 if (m_view
->itemSize().width() < 0) {
707 // Use a special rubberband for views that have only one column and
708 // expand the rubberband to use the whole width of the view.
709 endPos
.setX(m_view
->size().width());
712 endPos
.rx() += m_view
->scrollOffset();
714 rubberBand
->setEndPosition(endPos
);
721 bool KItemListController::mouseReleaseEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
727 emit
mouseButtonReleased(m_pressedIndex
, event
->buttons());
729 const bool isAboveSelectionToggle
= m_view
->isAboveSelectionToggle(m_pressedIndex
, m_pressedMousePos
);
730 if (isAboveSelectionToggle
) {
731 m_selectionTogglePressed
= false;
735 if (!isAboveSelectionToggle
&& m_selectionTogglePressed
) {
736 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
737 m_selectionTogglePressed
= false;
741 const bool shiftOrControlPressed
= event
->modifiers() & Qt::ShiftModifier
||
742 event
->modifiers() & Qt::ControlModifier
;
744 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
745 if (rubberBand
->isActive()) {
746 disconnect(rubberBand
, SIGNAL(endPositionChanged(QPointF
,QPointF
)), this, SLOT(slotRubberBandChanged()));
747 rubberBand
->setActive(false);
748 m_oldSelection
.clear();
749 m_view
->setAutoScroll(false);
752 const QPointF pos
= transform
.map(event
->pos());
753 const int index
= m_view
->itemAt(pos
);
755 if (index
>= 0 && index
== m_pressedIndex
) {
756 // The release event is done above the same item as the press event
758 if (m_clearSelectionIfItemsAreNotDragged
) {
759 // A selected item has been clicked, but no drag operation has been started
760 // -> clear the rest of the selection.
761 m_selectionManager
->clearSelection();
762 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Select
);
763 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
766 if (event
->button() & Qt::LeftButton
) {
767 bool emitItemActivated
= true;
768 if (m_view
->isAboveExpansionToggle(index
, pos
)) {
769 const bool expanded
= m_model
->isExpanded(index
);
770 m_model
->setExpanded(index
, !expanded
);
772 emit
itemExpansionToggleClicked(index
);
773 emitItemActivated
= false;
774 } else if (shiftOrControlPressed
) {
775 // The mouse click should only update the selection, not trigger the item
776 emitItemActivated
= false;
777 } else if (!(KGlobalSettings::singleClick() || m_singleClickActivationEnforced
)) {
778 emitItemActivated
= false;
780 if (emitItemActivated
) {
781 emit
itemActivated(index
);
783 } else if (event
->button() & Qt::MidButton
) {
784 emit
itemMiddleClicked(index
);
788 m_pressedMousePos
= QPointF();
790 m_clearSelectionIfItemsAreNotDragged
= false;
794 bool KItemListController::mouseDoubleClickEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
796 const QPointF pos
= transform
.map(event
->pos());
797 const int index
= m_view
->itemAt(pos
);
799 // Expand item if desired - See Bug 295573
800 if (m_mouseDoubleClickAction
!= ActivateItemOnly
) {
801 if (m_view
&& m_model
&& m_view
->supportsItemExpanding() && m_model
->isExpandable(index
)) {
802 const bool expanded
= m_model
->isExpanded(index
);
803 m_model
->setExpanded(index
, !expanded
);
807 bool emitItemActivated
= !(KGlobalSettings::singleClick() || m_singleClickActivationEnforced
) &&
808 (event
->button() & Qt::LeftButton
) &&
809 index
>= 0 && index
< m_model
->count();
810 if (emitItemActivated
) {
811 emit
itemActivated(index
);
816 bool KItemListController::dragEnterEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
823 bool KItemListController::dragLeaveEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
828 m_view
->setAutoScroll(false);
829 m_view
->hideDropIndicator();
831 KItemListWidget
* widget
= hoveredWidget();
833 widget
->setHovered(false);
834 emit
itemUnhovered(widget
->index());
839 bool KItemListController::dragMoveEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
841 if (!m_model
|| !m_view
) {
845 event
->acceptProposedAction();
847 KItemListWidget
* oldHoveredWidget
= hoveredWidget();
849 const QPointF pos
= transform
.map(event
->pos());
850 KItemListWidget
* newHoveredWidget
= widgetForPos(pos
);
852 if (oldHoveredWidget
!= newHoveredWidget
) {
853 m_autoActivationTimer
->stop();
855 if (oldHoveredWidget
) {
856 oldHoveredWidget
->setHovered(false);
857 emit
itemUnhovered(oldHoveredWidget
->index());
861 if (newHoveredWidget
) {
862 bool droppingBetweenItems
= false;
863 if (m_model
->sortRole().isEmpty()) {
864 // The model supports inserting items between other items.
865 droppingBetweenItems
= (m_view
->showDropIndicator(pos
) >= 0);
868 const int index
= newHoveredWidget
->index();
869 if (!droppingBetweenItems
) {
870 if (m_model
->supportsDropping(index
)) {
871 // Something has been dragged on an item.
872 m_view
->hideDropIndicator();
873 if (!newHoveredWidget
->isHovered()) {
874 newHoveredWidget
->setHovered(true);
875 emit
itemHovered(index
);
878 if (!m_autoActivationTimer
->isActive() && m_autoActivationTimer
->interval() >= 0) {
879 m_autoActivationTimer
->setProperty("index", index
);
880 m_autoActivationTimer
->start();
884 m_autoActivationTimer
->stop();
885 if (newHoveredWidget
&& newHoveredWidget
->isHovered()) {
886 newHoveredWidget
->setHovered(false);
887 emit
itemUnhovered(index
);
891 m_view
->hideDropIndicator();
897 bool KItemListController::dropEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
903 m_autoActivationTimer
->stop();
904 m_view
->setAutoScroll(false);
906 const QPointF pos
= transform
.map(event
->pos());
908 int dropAboveIndex
= -1;
909 if (m_model
->sortRole().isEmpty()) {
910 // The model supports inserting of items between other items.
911 dropAboveIndex
= m_view
->showDropIndicator(pos
);
914 if (dropAboveIndex
>= 0) {
915 // Something has been dropped between two items.
916 m_view
->hideDropIndicator();
917 emit
aboveItemDropEvent(dropAboveIndex
, event
);
919 // Something has been dropped on an item or on an empty part of the view.
920 emit
itemDropEvent(m_view
->itemAt(pos
), event
);
923 QAccessible::updateAccessibility(view(), 0, QAccessible::DragDropEnd
);
928 bool KItemListController::hoverEnterEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
935 bool KItemListController::hoverMoveEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
938 if (!m_model
|| !m_view
) {
942 KItemListWidget
* oldHoveredWidget
= hoveredWidget();
943 const QPointF pos
= transform
.map(event
->pos());
944 KItemListWidget
* newHoveredWidget
= widgetForPos(pos
);
946 if (oldHoveredWidget
!= newHoveredWidget
) {
947 if (oldHoveredWidget
) {
948 oldHoveredWidget
->setHovered(false);
949 emit
itemUnhovered(oldHoveredWidget
->index());
952 if (newHoveredWidget
) {
953 newHoveredWidget
->setHovered(true);
954 const QPointF mappedPos
= newHoveredWidget
->mapFromItem(m_view
, pos
);
955 newHoveredWidget
->setHoverPosition(mappedPos
);
956 emit
itemHovered(newHoveredWidget
->index());
958 } else if (oldHoveredWidget
) {
959 const QPointF mappedPos
= oldHoveredWidget
->mapFromItem(m_view
, pos
);
960 oldHoveredWidget
->setHoverPosition(mappedPos
);
966 bool KItemListController::hoverLeaveEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
971 if (!m_model
|| !m_view
) {
975 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
976 if (widget
->isHovered()) {
977 widget
->setHovered(false);
978 emit
itemUnhovered(widget
->index());
984 bool KItemListController::wheelEvent(QGraphicsSceneWheelEvent
* event
, const QTransform
& transform
)
991 bool KItemListController::resizeEvent(QGraphicsSceneResizeEvent
* event
, const QTransform
& transform
)
998 bool KItemListController::processEvent(QEvent
* event
, const QTransform
& transform
)
1004 switch (event
->type()) {
1005 case QEvent::KeyPress
:
1006 return keyPressEvent(static_cast<QKeyEvent
*>(event
));
1007 case QEvent::InputMethod
:
1008 return inputMethodEvent(static_cast<QInputMethodEvent
*>(event
));
1009 case QEvent::GraphicsSceneMousePress
:
1010 return mousePressEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
1011 case QEvent::GraphicsSceneMouseMove
:
1012 return mouseMoveEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
1013 case QEvent::GraphicsSceneMouseRelease
:
1014 return mouseReleaseEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
1015 case QEvent::GraphicsSceneMouseDoubleClick
:
1016 return mouseDoubleClickEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
1017 case QEvent::GraphicsSceneWheel
:
1018 return wheelEvent(static_cast<QGraphicsSceneWheelEvent
*>(event
), QTransform());
1019 case QEvent::GraphicsSceneDragEnter
:
1020 return dragEnterEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
1021 case QEvent::GraphicsSceneDragLeave
:
1022 return dragLeaveEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
1023 case QEvent::GraphicsSceneDragMove
:
1024 return dragMoveEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
1025 case QEvent::GraphicsSceneDrop
:
1026 return dropEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
1027 case QEvent::GraphicsSceneHoverEnter
:
1028 return hoverEnterEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
1029 case QEvent::GraphicsSceneHoverMove
:
1030 return hoverMoveEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
1031 case QEvent::GraphicsSceneHoverLeave
:
1032 return hoverLeaveEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
1033 case QEvent::GraphicsSceneResize
:
1034 return resizeEvent(static_cast<QGraphicsSceneResizeEvent
*>(event
), transform
);
1042 void KItemListController::slotViewScrollOffsetChanged(qreal current
, qreal previous
)
1048 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
1049 if (rubberBand
->isActive()) {
1050 const qreal diff
= current
- previous
;
1051 // TODO: Ideally just QCursor::pos() should be used as
1052 // new end-position but it seems there is no easy way
1053 // to have something like QWidget::mapFromGlobal() for QGraphicsWidget
1054 // (... or I just missed an easy way to do the mapping)
1055 QPointF endPos
= rubberBand
->endPosition();
1056 if (m_view
->scrollOrientation() == Qt::Vertical
) {
1057 endPos
.ry() += diff
;
1059 endPos
.rx() += diff
;
1062 rubberBand
->setEndPosition(endPos
);
1066 void KItemListController::slotRubberBandChanged()
1068 if (!m_view
|| !m_model
|| m_model
->count() <= 0) {
1072 const KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
1073 const QPointF startPos
= rubberBand
->startPosition();
1074 const QPointF endPos
= rubberBand
->endPosition();
1075 QRectF rubberBandRect
= QRectF(startPos
, endPos
).normalized();
1077 const bool scrollVertical
= (m_view
->scrollOrientation() == Qt::Vertical
);
1078 if (scrollVertical
) {
1079 rubberBandRect
.translate(0, -m_view
->scrollOffset());
1081 rubberBandRect
.translate(-m_view
->scrollOffset(), 0);
1084 if (!m_oldSelection
.isEmpty()) {
1085 // Clear the old selection that was available before the rubberband has
1086 // been activated in case if no Shift- or Control-key are pressed
1087 const bool shiftOrControlPressed
= QApplication::keyboardModifiers() & Qt::ShiftModifier
||
1088 QApplication::keyboardModifiers() & Qt::ControlModifier
;
1089 if (!shiftOrControlPressed
) {
1090 m_oldSelection
.clear();
1094 KItemSet selectedItems
;
1096 // Select all visible items that intersect with the rubberband
1097 foreach (const KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
1098 const int index
= widget
->index();
1100 const QRectF widgetRect
= m_view
->itemRect(index
);
1101 if (widgetRect
.intersects(rubberBandRect
)) {
1102 const QRectF iconRect
= widget
->iconRect().translated(widgetRect
.topLeft());
1103 const QRectF textRect
= widget
->textRect().translated(widgetRect
.topLeft());
1104 if (iconRect
.intersects(rubberBandRect
) || textRect
.intersects(rubberBandRect
)) {
1105 selectedItems
.insert(index
);
1110 // Select all invisible items that intersect with the rubberband. Instead of
1111 // iterating all items only the area which might be touched by the rubberband
1113 const bool increaseIndex
= scrollVertical
?
1114 startPos
.y() > endPos
.y(): startPos
.x() > endPos
.x();
1116 int index
= increaseIndex
? m_view
->lastVisibleIndex() + 1 : m_view
->firstVisibleIndex() - 1;
1117 bool selectionFinished
= false;
1119 const QRectF widgetRect
= m_view
->itemRect(index
);
1120 if (widgetRect
.intersects(rubberBandRect
)) {
1121 selectedItems
.insert(index
);
1124 if (increaseIndex
) {
1126 selectionFinished
= (index
>= m_model
->count()) ||
1127 ( scrollVertical
&& widgetRect
.top() > rubberBandRect
.bottom()) ||
1128 (!scrollVertical
&& widgetRect
.left() > rubberBandRect
.right());
1131 selectionFinished
= (index
< 0) ||
1132 ( scrollVertical
&& widgetRect
.bottom() < rubberBandRect
.top()) ||
1133 (!scrollVertical
&& widgetRect
.right() < rubberBandRect
.left());
1135 } while (!selectionFinished
);
1137 if (QApplication::keyboardModifiers() & Qt::ControlModifier
) {
1138 // If Control is pressed, the selection state of all items in the rubberband is toggled.
1139 // Therefore, the new selection contains:
1140 // 1. All previously selected items which are not inside the rubberband, and
1141 // 2. all items inside the rubberband which have not been selected previously.
1142 m_selectionManager
->setSelectedItems(m_oldSelection
^ selectedItems
);
1145 m_selectionManager
->setSelectedItems(selectedItems
+ m_oldSelection
);
1149 void KItemListController::startDragging()
1151 if (!m_view
|| !m_model
) {
1155 const KItemSet selectedItems
= m_selectionManager
->selectedItems();
1156 if (selectedItems
.isEmpty()) {
1160 QMimeData
* data
= m_model
->createMimeData(selectedItems
);
1165 // The created drag object will be owned and deleted
1166 // by QApplication::activeWindow().
1167 QDrag
* drag
= new QDrag(QApplication::activeWindow());
1168 drag
->setMimeData(data
);
1170 const QPixmap pixmap
= m_view
->createDragPixmap(selectedItems
);
1171 drag
->setPixmap(pixmap
);
1173 const QPoint
hotSpot(pixmap
.width() / 2, 0);
1174 drag
->setHotSpot(hotSpot
);
1176 drag
->exec(Qt::MoveAction
| Qt::CopyAction
| Qt::LinkAction
, Qt::CopyAction
);
1177 QAccessible::updateAccessibility(view(), 0, QAccessible::DragDropStart
);
1180 KItemListWidget
* KItemListController::hoveredWidget() const
1184 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
1185 if (widget
->isHovered()) {
1193 KItemListWidget
* KItemListController::widgetForPos(const QPointF
& pos
) const
1197 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
1198 const QPointF mappedPos
= widget
->mapFromItem(m_view
, pos
);
1200 const bool hovered
= widget
->contains(mappedPos
) &&
1201 !widget
->expansionToggleRect().contains(mappedPos
);
1210 void KItemListController::updateKeyboardAnchor()
1212 const bool validAnchor
= m_keyboardAnchorIndex
>= 0 &&
1213 m_keyboardAnchorIndex
< m_model
->count() &&
1214 keyboardAnchorPos(m_keyboardAnchorIndex
) == m_keyboardAnchorPos
;
1216 const int index
= m_selectionManager
->currentItem();
1217 m_keyboardAnchorIndex
= index
;
1218 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
1222 int KItemListController::nextRowIndex(int index
) const
1224 if (m_keyboardAnchorIndex
< 0) {
1228 const int maxIndex
= m_model
->count() - 1;
1229 if (index
== maxIndex
) {
1233 // Calculate the index of the last column inside the row of the current index
1234 int lastColumnIndex
= index
;
1235 while (keyboardAnchorPos(lastColumnIndex
+ 1) > keyboardAnchorPos(lastColumnIndex
)) {
1237 if (lastColumnIndex
>= maxIndex
) {
1242 // Based on the last column index go to the next row and calculate the nearest index
1243 // that is below the current index
1244 int nextRowIndex
= lastColumnIndex
+ 1;
1245 int searchIndex
= nextRowIndex
;
1246 qreal minDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(nextRowIndex
));
1247 while (searchIndex
< maxIndex
&& keyboardAnchorPos(searchIndex
+ 1) > keyboardAnchorPos(searchIndex
)) {
1249 const qreal searchDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(searchIndex
));
1250 if (searchDiff
< minDiff
) {
1251 minDiff
= searchDiff
;
1252 nextRowIndex
= searchIndex
;
1256 return nextRowIndex
;
1259 int KItemListController::previousRowIndex(int index
) const
1261 if (m_keyboardAnchorIndex
< 0 || index
== 0) {
1265 // Calculate the index of the first column inside the row of the current index
1266 int firstColumnIndex
= index
;
1267 while (keyboardAnchorPos(firstColumnIndex
- 1) < keyboardAnchorPos(firstColumnIndex
)) {
1269 if (firstColumnIndex
<= 0) {
1274 // Based on the first column index go to the previous row and calculate the nearest index
1275 // that is above the current index
1276 int previousRowIndex
= firstColumnIndex
- 1;
1277 int searchIndex
= previousRowIndex
;
1278 qreal minDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(previousRowIndex
));
1279 while (searchIndex
> 0 && keyboardAnchorPos(searchIndex
- 1) < keyboardAnchorPos(searchIndex
)) {
1281 const qreal searchDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(searchIndex
));
1282 if (searchDiff
< minDiff
) {
1283 minDiff
= searchDiff
;
1284 previousRowIndex
= searchIndex
;
1288 return previousRowIndex
;
1291 qreal
KItemListController::keyboardAnchorPos(int index
) const
1293 const QRectF itemRect
= m_view
->itemRect(index
);
1294 if (!itemRect
.isEmpty()) {
1295 return (m_view
->scrollOrientation() == Qt::Vertical
) ? itemRect
.x() : itemRect
.y();
1301 void KItemListController::updateExtendedSelectionRegion()
1304 const bool extend
= (m_selectionBehavior
!= MultiSelection
);
1305 KItemListStyleOption option
= m_view
->styleOption();
1306 if (option
.extendedSelectionRegion
!= extend
) {
1307 option
.extendedSelectionRegion
= extend
;
1308 m_view
->setStyleOption(option
);
1313 #include "kitemlistcontroller.moc"