1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
4 * Based on the Itemviews NG project from Trolltech Labs: *
5 * http://qt.gitorious.org/qt-labs/itemviews-ng *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program; if not, write to the *
19 * Free Software Foundation, Inc., *
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
21 ***************************************************************************/
23 #include "kitemlistcontroller.h"
25 #include "kitemlistview.h"
26 #include "kitemlistrubberband_p.h"
27 #include "kitemlistselectionmanager.h"
28 #include "kitemlistkeyboardsearchmanager_p.h"
30 #include <QApplication>
33 #include <QGraphicsScene>
34 #include <QGraphicsSceneEvent>
35 #include <QGraphicsView>
39 #include <KGlobalSettings>
42 KItemListController::KItemListController(QObject
* parent
) :
44 m_selectionTogglePressed(false),
45 m_selectionBehavior(NoSelection
),
48 m_selectionManager(new KItemListSelectionManager(this)),
49 m_keyboardManager(new KItemListKeyboardSearchManager(this)),
52 m_autoActivationTimer(0),
54 m_keyboardAnchorIndex(-1),
55 m_keyboardAnchorPos(0)
57 connect(m_keyboardManager
, SIGNAL(changeCurrentItem(QString
,bool)),
58 this, SLOT(slotChangeCurrentItem(QString
,bool)));
60 m_autoActivationTimer
= new QTimer(this);
61 m_autoActivationTimer
->setSingleShot(true);
62 m_autoActivationTimer
->setInterval(-1);
63 connect(m_autoActivationTimer
, SIGNAL(timeout()), this, SLOT(slotAutoActivationTimeout()));
66 KItemListController::~KItemListController()
70 void KItemListController::setModel(KItemModelBase
* model
)
72 if (m_model
== model
) {
76 KItemModelBase
* oldModel
= m_model
;
80 m_view
->setModel(m_model
);
83 m_selectionManager
->setModel(m_model
);
85 emit
modelChanged(m_model
, oldModel
);
88 KItemModelBase
* KItemListController::model() const
93 KItemListSelectionManager
* KItemListController::selectionManager() const
95 return m_selectionManager
;
98 void KItemListController::setView(KItemListView
* view
)
100 if (m_view
== view
) {
104 KItemListView
* oldView
= m_view
;
106 disconnect(oldView
, SIGNAL(scrollOffsetChanged(qreal
,qreal
)), this, SLOT(slotViewScrollOffsetChanged(qreal
,qreal
)));
112 m_view
->setController(this);
113 m_view
->setModel(m_model
);
114 connect(m_view
, SIGNAL(scrollOffsetChanged(qreal
,qreal
)), this, SLOT(slotViewScrollOffsetChanged(qreal
,qreal
)));
117 emit
viewChanged(m_view
, oldView
);
120 KItemListView
* KItemListController::view() const
125 void KItemListController::setSelectionBehavior(SelectionBehavior behavior
)
127 m_selectionBehavior
= behavior
;
130 KItemListController::SelectionBehavior
KItemListController::selectionBehavior() const
132 return m_selectionBehavior
;
135 void KItemListController::setAutoActivationDelay(int delay
)
137 m_autoActivationTimer
->setInterval(delay
);
140 int KItemListController::autoActivationDelay() const
142 return m_autoActivationTimer
->interval();
145 bool KItemListController::showEvent(QShowEvent
* event
)
151 bool KItemListController::hideEvent(QHideEvent
* event
)
157 bool KItemListController::keyPressEvent(QKeyEvent
* event
)
159 int index
= m_selectionManager
->currentItem();
160 int key
= event
->key();
162 // Handle the expanding/collapsing of items
163 if (m_view
->supportsItemExpanding() && m_model
->isExpandable(index
)) {
164 if (key
== Qt::Key_Right
) {
165 if (m_model
->setExpanded(index
, true)) {
168 } else if (key
== Qt::Key_Left
) {
169 if (m_model
->setExpanded(index
, false)) {
175 const bool shiftPressed
= event
->modifiers() & Qt::ShiftModifier
;
176 const bool controlPressed
= event
->modifiers() & Qt::ControlModifier
;
177 const bool shiftOrControlPressed
= shiftPressed
|| controlPressed
;
179 const int itemCount
= m_model
->count();
181 // For horizontal scroll orientation, transform
182 // the arrow keys to simplify the event handling.
183 if (m_view
->scrollOrientation() == Qt::Horizontal
) {
185 case Qt::Key_Up
: key
= Qt::Key_Left
; break;
186 case Qt::Key_Down
: key
= Qt::Key_Right
; break;
187 case Qt::Key_Left
: key
= Qt::Key_Up
; break;
188 case Qt::Key_Right
: key
= Qt::Key_Down
; break;
193 const bool selectSingleItem
= itemCount
== 1 &&
194 (key
== Qt::Key_Home
|| key
== Qt::Key_End
||
195 key
== Qt::Key_Up
|| key
== Qt::Key_Down
||
196 key
== Qt::Key_Left
|| key
== Qt::Key_Right
);
197 if (selectSingleItem
) {
198 const int current
= m_selectionManager
->currentItem();
199 m_selectionManager
->setSelected(current
);
209 index
= itemCount
- 1;
215 m_keyboardAnchorIndex
= index
;
216 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
221 if (index
< itemCount
- 1) {
223 m_keyboardAnchorIndex
= index
;
224 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
229 updateKeyboardAnchor();
230 index
= previousRowIndex();
234 updateKeyboardAnchor();
235 index
= nextRowIndex();
239 case Qt::Key_Return
: {
240 const QSet
<int> selectedItems
= m_selectionManager
->selectedItems();
241 if (selectedItems
.count() >= 2) {
242 emit
itemsActivated(selectedItems
);
243 } else if (selectedItems
.count() == 1) {
244 emit
itemActivated(selectedItems
.toList().first());
246 emit
itemActivated(index
);
252 if (controlPressed
) {
253 m_selectionManager
->endAnchoredSelection();
254 m_selectionManager
->setSelected(index
, 1, KItemListSelectionManager::Toggle
);
255 m_selectionManager
->beginAnchoredSelection(index
);
257 const int current
= m_selectionManager
->currentItem();
258 m_selectionManager
->setSelected(current
);
263 // Emit the signal itemContextMenuRequested() in case if at least one
264 // item is selected. Otherwise the signal viewContextMenuRequested() will be emitted.
265 const QSet
<int> selectedItems
= m_selectionManager
->selectedItems();
267 if (selectedItems
.count() >= 2) {
268 const int currentItemIndex
= m_selectionManager
->currentItem();
269 index
= selectedItems
.contains(currentItemIndex
)
270 ? currentItemIndex
: selectedItems
.toList().first();
271 } else if (selectedItems
.count() == 1) {
272 index
= selectedItems
.toList().first();
276 const QRectF contextRect
= m_view
->itemContextRect(index
);
277 const QPointF
pos(m_view
->scene()->views().first()->mapToGlobal(contextRect
.bottomRight().toPoint()));
278 emit
itemContextMenuRequested(index
, pos
);
280 emit
viewContextMenuRequested(QCursor::pos());
286 m_keyboardManager
->addKeys(event
->text());
290 if (m_selectionManager
->currentItem() != index
) {
291 if (controlPressed
) {
292 m_selectionManager
->endAnchoredSelection();
295 m_selectionManager
->setCurrentItem(index
);
297 if (!shiftOrControlPressed
|| m_selectionBehavior
== SingleSelection
) {
298 m_selectionManager
->clearSelection();
299 m_selectionManager
->setSelected(index
, 1);
303 m_selectionManager
->beginAnchoredSelection(index
);
306 m_view
->scrollToItem(index
);
311 void KItemListController::slotChangeCurrentItem(const QString
& text
, bool searchFromNextItem
)
313 if (!m_model
|| m_model
->count() == 0) {
316 const int currentIndex
= m_selectionManager
->currentItem();
318 if (searchFromNextItem
) {
319 index
= m_model
->indexForKeyboardSearch(text
, (currentIndex
+ 1) % m_model
->count());
321 index
= m_model
->indexForKeyboardSearch(text
, currentIndex
);
324 m_selectionManager
->setCurrentItem(index
);
325 m_selectionManager
->clearSelection();
326 m_selectionManager
->setSelected(index
, 1);
327 m_selectionManager
->beginAnchoredSelection(index
);
328 m_view
->scrollToItem(index
);
332 void KItemListController::slotAutoActivationTimeout()
334 if (!m_model
|| !m_view
) {
338 const int index
= m_autoActivationTimer
->property("index").toInt();
339 if (index
< 0 || index
>= m_model
->count()) {
343 if (m_model
->supportsDropping(index
)) {
344 if (m_view
->supportsItemExpanding() && m_model
->isExpandable(index
)) {
345 const bool expanded
= m_model
->isExpanded(index
);
346 m_model
->setExpanded(index
, !expanded
);
348 emit
itemActivated(index
);
353 bool KItemListController::inputMethodEvent(QInputMethodEvent
* event
)
359 bool KItemListController::mousePressEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
365 m_pressedMousePos
= transform
.map(event
->pos());
366 m_pressedIndex
= m_view
->itemAt(m_pressedMousePos
);
367 if (m_pressedIndex
>= 0) {
368 emit
itemPressed(m_pressedIndex
, event
->button());
371 if (m_view
->isAboveExpansionToggle(m_pressedIndex
, m_pressedMousePos
)) {
372 m_selectionManager
->setCurrentItem(m_pressedIndex
);
376 m_selectionTogglePressed
= m_view
->isAboveSelectionToggle(m_pressedIndex
, m_pressedMousePos
);
377 if (m_selectionTogglePressed
) {
378 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
379 // The previous anchored selection has been finished already in
380 // KItemListSelectionManager::setSelected(). We can safely change
381 // the current item and start a new anchored selection now.
382 m_selectionManager
->setCurrentItem(m_pressedIndex
);
383 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
387 const bool shiftPressed
= event
->modifiers() & Qt::ShiftModifier
;
388 const bool controlPressed
= event
->modifiers() & Qt::ControlModifier
;
390 // The previous selection is cleared if either
391 // 1. The selection mode is SingleSelection, or
392 // 2. the selection mode is MultiSelection, and *none* of the following conditions are met:
393 // a) Shift or Control are pressed.
394 // b) The clicked item is selected already. In that case, the user might want to:
395 // - start dragging multiple items, or
396 // - open the context menu and perform an action for all selected items.
397 const bool shiftOrControlPressed
= shiftPressed
|| controlPressed
;
398 const bool pressedItemAlreadySelected
= m_pressedIndex
>= 0 && m_selectionManager
->isSelected(m_pressedIndex
);
399 const bool clearSelection
= m_selectionBehavior
== SingleSelection
||
400 (!shiftOrControlPressed
&& !pressedItemAlreadySelected
);
401 if (clearSelection
) {
402 m_selectionManager
->clearSelection();
406 // Finish the anchored selection before the current index is changed
407 m_selectionManager
->endAnchoredSelection();
410 if (m_pressedIndex
>= 0) {
411 m_selectionManager
->setCurrentItem(m_pressedIndex
);
413 switch (m_selectionBehavior
) {
417 case SingleSelection
:
418 m_selectionManager
->setSelected(m_pressedIndex
);
422 if (controlPressed
) {
423 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
424 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
425 } else if (!shiftPressed
|| !m_selectionManager
->isAnchoredSelectionActive()) {
426 // Select the pressed item and start a new anchored selection
427 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Select
);
428 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
437 if (event
->buttons() & Qt::RightButton
) {
438 emit
itemContextMenuRequested(m_pressedIndex
, event
->screenPos());
444 if (event
->buttons() & Qt::RightButton
) {
445 const QRectF headerBounds
= m_view
->headerBoundaries();
446 if (headerBounds
.contains(event
->pos())) {
447 emit
headerContextMenuRequested(event
->screenPos());
449 emit
viewContextMenuRequested(event
->screenPos());
454 if (m_selectionBehavior
== MultiSelection
) {
455 QPointF startPos
= m_pressedMousePos
;
456 if (m_view
->scrollOrientation() == Qt::Vertical
) {
457 startPos
.ry() += m_view
->scrollOffset();
458 if (m_view
->itemSize().width() < 0) {
459 // Use a special rubberband for views that have only one column and
460 // expand the rubberband to use the whole width of the view.
464 startPos
.rx() += m_view
->scrollOffset();
467 m_oldSelection
= m_selectionManager
->selectedItems();
468 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
469 rubberBand
->setStartPosition(startPos
);
470 rubberBand
->setEndPosition(startPos
);
471 rubberBand
->setActive(true);
472 connect(rubberBand
, SIGNAL(endPositionChanged(QPointF
,QPointF
)), this, SLOT(slotRubberBandChanged()));
473 m_view
->setAutoScroll(true);
479 bool KItemListController::mouseMoveEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
485 if (m_pressedIndex
>= 0) {
486 // Check whether a dragging should be started
487 if (event
->buttons() & Qt::LeftButton
) {
488 const QPointF pos
= transform
.map(event
->pos());
489 if ((pos
- m_pressedMousePos
).manhattanLength() >= QApplication::startDragDistance()) {
490 if (!m_selectionManager
->isSelected(m_pressedIndex
)) {
491 // Always assure that the dragged item gets selected. Usually this is already
492 // done on the mouse-press event, but when using the selection-toggle on a
493 // selected item the dragged item is not selected yet.
494 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
500 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
501 if (rubberBand
->isActive()) {
502 QPointF endPos
= transform
.map(event
->pos());
504 // Update the current item.
505 const int newCurrent
= m_view
->itemAt(endPos
);
506 if (newCurrent
>= 0) {
507 // It's expected that the new current index is also the new anchor (bug 163451).
508 m_selectionManager
->endAnchoredSelection();
509 m_selectionManager
->setCurrentItem(newCurrent
);
510 m_selectionManager
->beginAnchoredSelection(newCurrent
);
513 if (m_view
->scrollOrientation() == Qt::Vertical
) {
514 endPos
.ry() += m_view
->scrollOffset();
515 if (m_view
->itemSize().width() < 0) {
516 // Use a special rubberband for views that have only one column and
517 // expand the rubberband to use the whole width of the view.
518 endPos
.setX(m_view
->size().width());
521 endPos
.rx() += m_view
->scrollOffset();
523 rubberBand
->setEndPosition(endPos
);
530 bool KItemListController::mouseReleaseEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
536 if (m_pressedIndex
>= 0) {
537 emit
itemReleased(m_pressedIndex
, event
->button());
540 const bool isAboveSelectionToggle
= m_view
->isAboveSelectionToggle(m_pressedIndex
, m_pressedMousePos
);
541 if (isAboveSelectionToggle
) {
542 m_selectionTogglePressed
= false;
546 if (!isAboveSelectionToggle
&& m_selectionTogglePressed
) {
547 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
548 m_selectionTogglePressed
= false;
552 const bool shiftOrControlPressed
= event
->modifiers() & Qt::ShiftModifier
||
553 event
->modifiers() & Qt::ControlModifier
;
555 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
556 if (rubberBand
->isActive()) {
557 disconnect(rubberBand
, SIGNAL(endPositionChanged(QPointF
,QPointF
)), this, SLOT(slotRubberBandChanged()));
558 rubberBand
->setActive(false);
559 m_oldSelection
.clear();
560 m_view
->setAutoScroll(false);
563 const QPointF pos
= transform
.map(event
->pos());
564 const int index
= m_view
->itemAt(pos
);
566 if (index
>= 0 && index
== m_pressedIndex
) {
567 // The release event is done above the same item as the press event
569 if (event
->button() & Qt::LeftButton
) {
570 bool emitItemActivated
= true;
571 if (m_view
->isAboveExpansionToggle(index
, pos
)) {
572 const bool expanded
= m_model
->isExpanded(index
);
573 m_model
->setExpanded(index
, !expanded
);
575 emit
itemExpansionToggleClicked(index
);
576 emitItemActivated
= false;
577 } else if (shiftOrControlPressed
) {
578 // The mouse click should only update the selection, not trigger the item
579 emitItemActivated
= false;
580 } else if (!KGlobalSettings::singleClick()) {
581 emitItemActivated
= false;
583 if (emitItemActivated
) {
584 emit
itemActivated(index
);
586 } else if (event
->button() & Qt::MidButton
) {
587 emit
itemMiddleClicked(index
);
591 m_pressedMousePos
= QPointF();
596 bool KItemListController::mouseDoubleClickEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
598 const QPointF pos
= transform
.map(event
->pos());
599 const int index
= m_view
->itemAt(pos
);
601 bool emitItemActivated
= !KGlobalSettings::singleClick() &&
602 (event
->button() & Qt::LeftButton
) &&
603 index
>= 0 && index
< m_model
->count();
604 if (emitItemActivated
) {
605 emit
itemActivated(index
);
610 bool KItemListController::dragEnterEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
617 bool KItemListController::dragLeaveEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
624 bool KItemListController::dragMoveEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
627 if (!m_model
|| !m_view
) {
631 KItemListWidget
* oldHoveredWidget
= hoveredWidget();
633 const QPointF pos
= transform
.map(event
->pos());
634 KItemListWidget
* newHoveredWidget
= widgetForPos(pos
);
636 if (oldHoveredWidget
!= newHoveredWidget
) {
637 m_autoActivationTimer
->stop();
639 if (oldHoveredWidget
) {
640 oldHoveredWidget
->setHovered(false);
641 emit
itemUnhovered(oldHoveredWidget
->index());
644 if (newHoveredWidget
) {
645 const int index
= newHoveredWidget
->index();
646 if (m_model
->supportsDropping(index
)) {
647 newHoveredWidget
->setHovered(true);
649 emit
itemHovered(index
);
651 if (m_autoActivationTimer
->interval() >= 0) {
652 m_autoActivationTimer
->setProperty("index", index
);
653 m_autoActivationTimer
->start();
661 bool KItemListController::dropEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
668 m_autoActivationTimer
->stop();
670 const QPointF pos
= transform
.map(event
->pos());
671 const int index
= m_view
->itemAt(pos
);
672 emit
itemDropEvent(index
, event
);
677 bool KItemListController::hoverEnterEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
684 bool KItemListController::hoverMoveEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
687 if (!m_model
|| !m_view
) {
691 KItemListWidget
* oldHoveredWidget
= hoveredWidget();
692 const QPointF pos
= transform
.map(event
->pos());
693 KItemListWidget
* newHoveredWidget
= widgetForPos(pos
);
695 if (oldHoveredWidget
!= newHoveredWidget
) {
696 if (oldHoveredWidget
) {
697 oldHoveredWidget
->setHovered(false);
698 emit
itemUnhovered(oldHoveredWidget
->index());
701 if (newHoveredWidget
) {
702 newHoveredWidget
->setHovered(true);
703 emit
itemHovered(newHoveredWidget
->index());
710 bool KItemListController::hoverLeaveEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
715 if (!m_model
|| !m_view
) {
719 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
720 if (widget
->isHovered()) {
721 widget
->setHovered(false);
722 emit
itemUnhovered(widget
->index());
728 bool KItemListController::wheelEvent(QGraphicsSceneWheelEvent
* event
, const QTransform
& transform
)
735 bool KItemListController::resizeEvent(QGraphicsSceneResizeEvent
* event
, const QTransform
& transform
)
742 bool KItemListController::processEvent(QEvent
* event
, const QTransform
& transform
)
748 switch (event
->type()) {
749 case QEvent::KeyPress
:
750 return keyPressEvent(static_cast<QKeyEvent
*>(event
));
751 case QEvent::InputMethod
:
752 return inputMethodEvent(static_cast<QInputMethodEvent
*>(event
));
753 case QEvent::GraphicsSceneMousePress
:
754 return mousePressEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
755 case QEvent::GraphicsSceneMouseMove
:
756 return mouseMoveEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
757 case QEvent::GraphicsSceneMouseRelease
:
758 return mouseReleaseEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
759 case QEvent::GraphicsSceneMouseDoubleClick
:
760 return mouseDoubleClickEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
761 case QEvent::GraphicsSceneWheel
:
762 return wheelEvent(static_cast<QGraphicsSceneWheelEvent
*>(event
), QTransform());
763 case QEvent::GraphicsSceneDragEnter
:
764 return dragEnterEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
765 case QEvent::GraphicsSceneDragLeave
:
766 return dragLeaveEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
767 case QEvent::GraphicsSceneDragMove
:
768 return dragMoveEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
769 case QEvent::GraphicsSceneDrop
:
770 return dropEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
771 case QEvent::GraphicsSceneHoverEnter
:
772 return hoverEnterEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
773 case QEvent::GraphicsSceneHoverMove
:
774 return hoverMoveEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
775 case QEvent::GraphicsSceneHoverLeave
:
776 return hoverLeaveEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
777 case QEvent::GraphicsSceneResize
:
778 return resizeEvent(static_cast<QGraphicsSceneResizeEvent
*>(event
), transform
);
786 void KItemListController::slotViewScrollOffsetChanged(qreal current
, qreal previous
)
792 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
793 if (rubberBand
->isActive()) {
794 const qreal diff
= current
- previous
;
795 // TODO: Ideally just QCursor::pos() should be used as
796 // new end-position but it seems there is no easy way
797 // to have something like QWidget::mapFromGlobal() for QGraphicsWidget
798 // (... or I just missed an easy way to do the mapping)
799 QPointF endPos
= rubberBand
->endPosition();
800 if (m_view
->scrollOrientation() == Qt::Vertical
) {
806 rubberBand
->setEndPosition(endPos
);
810 void KItemListController::slotRubberBandChanged()
812 if (!m_view
|| !m_model
|| m_model
->count() <= 0) {
816 const KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
817 const QPointF startPos
= rubberBand
->startPosition();
818 const QPointF endPos
= rubberBand
->endPosition();
819 QRectF rubberBandRect
= QRectF(startPos
, endPos
).normalized();
821 const bool scrollVertical
= (m_view
->scrollOrientation() == Qt::Vertical
);
822 if (scrollVertical
) {
823 rubberBandRect
.translate(0, -m_view
->scrollOffset());
825 rubberBandRect
.translate(-m_view
->scrollOffset(), 0);
828 if (!m_oldSelection
.isEmpty()) {
829 // Clear the old selection that was available before the rubberband has
830 // been activated in case if no Shift- or Control-key are pressed
831 const bool shiftOrControlPressed
= QApplication::keyboardModifiers() & Qt::ShiftModifier
||
832 QApplication::keyboardModifiers() & Qt::ControlModifier
;
833 if (!shiftOrControlPressed
) {
834 m_oldSelection
.clear();
838 QSet
<int> selectedItems
;
840 // Select all visible items that intersect with the rubberband
841 foreach (const KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
842 const int index
= widget
->index();
844 const QRectF widgetRect
= m_view
->itemRect(index
);
845 if (widgetRect
.intersects(rubberBandRect
)) {
846 const QRectF iconRect
= widget
->iconRect().translated(widgetRect
.topLeft());
847 const QRectF textRect
= widget
->textRect().translated(widgetRect
.topLeft());
848 if (iconRect
.intersects(rubberBandRect
) || textRect
.intersects(rubberBandRect
)) {
849 selectedItems
.insert(index
);
854 // Select all invisible items that intersect with the rubberband. Instead of
855 // iterating all items only the area which might be touched by the rubberband
857 const bool increaseIndex
= scrollVertical
?
858 startPos
.y() > endPos
.y(): startPos
.x() > endPos
.x();
860 int index
= increaseIndex
? m_view
->lastVisibleIndex() + 1 : m_view
->firstVisibleIndex() - 1;
861 bool selectionFinished
= false;
863 const QRectF widgetRect
= m_view
->itemRect(index
);
864 if (widgetRect
.intersects(rubberBandRect
)) {
865 selectedItems
.insert(index
);
870 selectionFinished
= (index
>= m_model
->count()) ||
871 ( scrollVertical
&& widgetRect
.top() > rubberBandRect
.bottom()) ||
872 (!scrollVertical
&& widgetRect
.left() > rubberBandRect
.right());
875 selectionFinished
= (index
< 0) ||
876 ( scrollVertical
&& widgetRect
.bottom() < rubberBandRect
.top()) ||
877 (!scrollVertical
&& widgetRect
.right() < rubberBandRect
.left());
879 } while (!selectionFinished
);
881 if (QApplication::keyboardModifiers() & Qt::ControlModifier
) {
882 // If Control is pressed, the selection state of all items in the rubberband is toggled.
883 // Therefore, the new selection contains:
884 // 1. All previously selected items which are not inside the rubberband, and
885 // 2. all items inside the rubberband which have not been selected previously.
886 m_selectionManager
->setSelectedItems((m_oldSelection
- selectedItems
) + (selectedItems
- m_oldSelection
));
889 m_selectionManager
->setSelectedItems(selectedItems
+ m_oldSelection
);
893 void KItemListController::startDragging()
895 if (!m_view
|| !m_model
) {
899 const QSet
<int> selectedItems
= m_selectionManager
->selectedItems();
900 if (selectedItems
.isEmpty()) {
904 QMimeData
* data
= m_model
->createMimeData(selectedItems
);
909 // The created drag object will be owned and deleted
910 // by QApplication::activeWindow().
911 QDrag
* drag
= new QDrag(QApplication::activeWindow());
912 drag
->setMimeData(data
);
914 const QPixmap pixmap
= m_view
->createDragPixmap(selectedItems
);
915 drag
->setPixmap(pixmap
);
917 drag
->exec(Qt::CopyAction
);
920 KItemListWidget
* KItemListController::hoveredWidget() const
924 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
925 if (widget
->isHovered()) {
933 KItemListWidget
* KItemListController::widgetForPos(const QPointF
& pos
) const
937 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
938 const QPointF mappedPos
= widget
->mapFromItem(m_view
, pos
);
940 const bool hovered
= widget
->contains(mappedPos
) &&
941 !widget
->expansionToggleRect().contains(mappedPos
);
950 void KItemListController::updateKeyboardAnchor()
952 const bool validAnchor
= m_keyboardAnchorIndex
>= 0 &&
953 m_keyboardAnchorIndex
< m_model
->count() &&
954 keyboardAnchorPos(m_keyboardAnchorIndex
) == m_keyboardAnchorPos
;
956 const int index
= m_selectionManager
->currentItem();
957 m_keyboardAnchorIndex
= index
;
958 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
962 int KItemListController::nextRowIndex() const
964 const int currentIndex
= m_selectionManager
->currentItem();
965 if (m_keyboardAnchorIndex
< 0) {
969 const int maxIndex
= m_model
->count() - 1;
970 if (currentIndex
== maxIndex
) {
974 // Calculate the index of the last column inside the row of the current index
975 int lastColumnIndex
= currentIndex
;
976 while (keyboardAnchorPos(lastColumnIndex
+ 1) > keyboardAnchorPos(lastColumnIndex
)) {
978 if (lastColumnIndex
>= maxIndex
) {
983 // Based on the last column index go to the next row and calculate the nearest index
984 // that is below the current index
985 int nextRowIndex
= lastColumnIndex
+ 1;
986 int searchIndex
= nextRowIndex
;
987 qreal minDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(nextRowIndex
));
988 while (searchIndex
< maxIndex
&& keyboardAnchorPos(searchIndex
+ 1) > keyboardAnchorPos(searchIndex
)) {
990 const qreal searchDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(searchIndex
));
991 if (searchDiff
< minDiff
) {
992 minDiff
= searchDiff
;
993 nextRowIndex
= searchIndex
;
1000 int KItemListController::previousRowIndex() const
1002 const int currentIndex
= m_selectionManager
->currentItem();
1003 if (m_keyboardAnchorIndex
< 0 || currentIndex
== 0) {
1004 return currentIndex
;
1007 // Calculate the index of the first column inside the row of the current index
1008 int firstColumnIndex
= currentIndex
;
1009 while (keyboardAnchorPos(firstColumnIndex
- 1) < keyboardAnchorPos(firstColumnIndex
)) {
1011 if (firstColumnIndex
<= 0) {
1012 return currentIndex
;
1016 // Based on the first column index go to the previous row and calculate the nearest index
1017 // that is above the current index
1018 int previousRowIndex
= firstColumnIndex
- 1;
1019 int searchIndex
= previousRowIndex
;
1020 qreal minDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(previousRowIndex
));
1021 while (searchIndex
> 0 && keyboardAnchorPos(searchIndex
- 1) < keyboardAnchorPos(searchIndex
)) {
1023 const qreal searchDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(searchIndex
));
1024 if (searchDiff
< minDiff
) {
1025 minDiff
= searchDiff
;
1026 previousRowIndex
= searchIndex
;
1030 return previousRowIndex
;
1033 qreal
KItemListController::keyboardAnchorPos(int index
) const
1035 const QRectF itemRect
= m_view
->itemRect(index
);
1036 if (!itemRect
.isEmpty()) {
1037 return (m_view
->scrollOrientation() == Qt::Vertical
) ? itemRect
.x() : itemRect
.y();
1043 #include "kitemlistcontroller.moc"