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"
30 #include <QGraphicsSceneEvent>
34 KItemListController::KItemListController(QObject
* parent
) :
36 m_selectionBehavior(NoSelection
),
39 m_selectionManager(new KItemListSelectionManager(this)),
44 KItemListController::~KItemListController()
48 void KItemListController::setModel(KItemModelBase
* model
)
50 if (m_model
== model
) {
54 KItemModelBase
* oldModel
= m_model
;
58 m_view
->setModel(m_model
);
61 m_selectionManager
->setModel(m_model
);
63 emit
modelChanged(m_model
, oldModel
);
66 KItemModelBase
* KItemListController::model() const
71 KItemListSelectionManager
* KItemListController::selectionManager() const
73 return m_selectionManager
;
76 void KItemListController::setView(KItemListView
* view
)
82 KItemListView
* oldView
= m_view
;
84 disconnect(oldView
, SIGNAL(offsetChanged(qreal
,qreal
)), this, SLOT(slotViewOffsetChanged(qreal
,qreal
)));
90 m_view
->setController(this);
91 m_view
->setModel(m_model
);
92 connect(m_view
, SIGNAL(offsetChanged(qreal
,qreal
)), this, SLOT(slotViewOffsetChanged(qreal
,qreal
)));
95 emit
viewChanged(m_view
, oldView
);
98 KItemListView
* KItemListController::view() const
103 void KItemListController::setSelectionBehavior(SelectionBehavior behavior
)
105 m_selectionBehavior
= behavior
;
108 KItemListController::SelectionBehavior
KItemListController::selectionBehavior() const
110 return m_selectionBehavior
;
113 bool KItemListController::showEvent(QShowEvent
* event
)
119 bool KItemListController::hideEvent(QHideEvent
* event
)
125 bool KItemListController::keyPressEvent(QKeyEvent
* event
)
127 const bool shiftPressed
= event
->modifiers() & Qt::ShiftModifier
;
128 const bool controlPressed
= event
->modifiers() & Qt::ControlModifier
;
129 const bool shiftOrControlPressed
= shiftPressed
|| controlPressed
;
131 int index
= m_selectionManager
->currentItem();
132 const int itemCount
= m_model
->count();
133 const int itemsPerRow
= m_view
->itemsPerOffset();
135 // For horizontal scroll orientation, transform
136 // the arrow keys to simplify the event handling.
137 int key
= event
->key();
138 if (m_view
->scrollOrientation() == Qt::Horizontal
) {
140 case Qt::Key_Up
: key
= Qt::Key_Left
; break;
141 case Qt::Key_Down
: key
= Qt::Key_Right
; break;
142 case Qt::Key_Left
: key
= Qt::Key_Up
; break;
143 case Qt::Key_Right
: key
= Qt::Key_Down
; break;
154 index
= itemCount
- 1;
164 if (index
< itemCount
- 1) {
170 if (index
>= itemsPerRow
) {
171 index
-= itemsPerRow
;
176 if (index
+ itemsPerRow
< itemCount
) {
177 // We are not in the last row yet.
178 index
+= itemsPerRow
;
181 // We are either in the last row already, or we are in the second-last row,
182 // and there is no item below the current item.
183 // In the latter case, we jump to the very last item.
184 const int currentColumn
= index
% itemsPerRow
;
185 const int lastItemColumn
= (itemCount
- 1) % itemsPerRow
;
186 const bool inLastRow
= currentColumn
< lastItemColumn
;
188 index
= itemCount
- 1;
194 if (controlPressed
) {
195 m_selectionManager
->endAnchoredSelection();
196 m_selectionManager
->setSelected(index
, 1, KItemListSelectionManager::Toggle
);
197 m_selectionManager
->beginAnchoredSelection(index
);
204 if (m_selectionManager
->currentItem() != index
) {
205 if (controlPressed
) {
206 m_selectionManager
->endAnchoredSelection();
209 m_selectionManager
->setCurrentItem(index
);
211 if (!shiftOrControlPressed
|| m_selectionBehavior
== SingleSelection
) {
212 m_selectionManager
->clearSelection();
213 m_selectionManager
->setSelected(index
, 1);
217 m_selectionManager
->beginAnchoredSelection(index
);
223 bool KItemListController::inputMethodEvent(QInputMethodEvent
* event
)
229 bool KItemListController::mousePressEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
235 const QPointF pos
= transform
.map(event
->pos());
236 m_pressedIndex
= m_view
->itemAt(pos
);
238 if (m_view
->isAboveExpansionToggle(m_pressedIndex
, pos
)) {
242 const bool shiftPressed
= event
->modifiers() & Qt::ShiftModifier
;
243 const bool controlPressed
= event
->modifiers() & Qt::ControlModifier
;
244 const bool shiftOrControlPressed
= shiftPressed
|| controlPressed
;
246 if (!shiftOrControlPressed
|| m_selectionBehavior
== SingleSelection
) {
247 m_selectionManager
->clearSelection();
251 // Finish the anchored selection before the current index is changed
252 m_selectionManager
->endAnchoredSelection();
255 if (m_pressedIndex
>= 0) {
256 m_selectionManager
->setCurrentItem(m_pressedIndex
);
258 switch (m_selectionBehavior
) {
262 case SingleSelection
:
263 m_selectionManager
->setSelected(m_pressedIndex
);
267 if (controlPressed
) {
268 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
269 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
270 } else if (!shiftPressed
|| !m_selectionManager
->isAnchoredSelectionActive()) {
271 // Select the pressed item and start a new anchored selection
272 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Select
);
273 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
284 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
285 QPointF startPos
= pos
;
286 if (m_view
->scrollOrientation() == Qt::Vertical
) {
287 startPos
.ry() += m_view
->offset();
288 if (m_view
->itemSize().width() < 0) {
289 // Use a special rubberband for views that have only one column and
290 // expand the rubberband to use the whole width of the view.
294 startPos
.rx() += m_view
->offset();
296 rubberBand
->setStartPosition(startPos
);
297 rubberBand
->setEndPosition(startPos
);
298 rubberBand
->setActive(true);
299 connect(rubberBand
, SIGNAL(endPositionChanged(QPointF
,QPointF
)), this, SLOT(slotRubberBandChanged()));
305 bool KItemListController::mouseMoveEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
311 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
312 if (rubberBand
->isActive()) {
313 QPointF endPos
= transform
.map(event
->pos());
314 if (m_view
->scrollOrientation() == Qt::Vertical
) {
315 endPos
.ry() += m_view
->offset();
316 if (m_view
->itemSize().width() < 0) {
317 // Use a special rubberband for views that have only one column and
318 // expand the rubberband to use the whole width of the view.
319 endPos
.setX(m_view
->size().width());
322 endPos
.rx() += m_view
->offset();
324 rubberBand
->setEndPosition(endPos
);
330 bool KItemListController::mouseReleaseEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
336 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
337 if (rubberBand
->isActive()) {
338 disconnect(rubberBand
, SIGNAL(endPositionChanged(QPointF
,QPointF
)), this, SLOT(slotRubberBandChanged()));
339 rubberBand
->setActive(false);
344 const QPointF pos
= transform
.map(event
->pos());
345 const int index
= m_view
->itemAt(pos
);
346 const bool shiftOrControlPressed
= event
->modifiers() & Qt::ShiftModifier
|| event
->modifiers() & Qt::ControlModifier
;
348 if (index
>= 0 && index
== m_pressedIndex
) {
349 // The release event is done above the same item as the press event
350 bool emitItemClicked
= true;
351 if (event
->button() & Qt::LeftButton
) {
352 if (m_view
->isAboveExpansionToggle(index
, pos
)) {
353 emit
itemExpansionToggleClicked(index
);
354 emitItemClicked
= false;
356 else if (shiftOrControlPressed
) {
357 // The mouse click should only update the selection, not trigger the item
358 emitItemClicked
= false;
362 if (emitItemClicked
) {
363 emit
itemClicked(index
, event
->button());
365 } else if (!shiftOrControlPressed
) {
366 m_selectionManager
->clearSelection();
373 bool KItemListController::mouseDoubleClickEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
380 bool KItemListController::dragEnterEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
387 bool KItemListController::dragLeaveEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
394 bool KItemListController::dragMoveEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
401 bool KItemListController::dropEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
408 bool KItemListController::hoverEnterEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
415 bool KItemListController::hoverMoveEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
417 // The implementation assumes that only one item can get hovered no matter
418 // whether they overlap or not.
421 if (!m_model
|| !m_view
) {
425 // Search the previously hovered item that might get unhovered
426 KItemListWidget
* unhoveredWidget
= 0;
427 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
428 if (widget
->isHovered()) {
429 unhoveredWidget
= widget
;
434 // Search the currently hovered item
435 KItemListWidget
* hoveredWidget
= 0;
436 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
437 const QPointF mappedPos
= widget
->mapFromItem(m_view
, event
->pos());
439 const bool hovered
= widget
->contains(mappedPos
) &&
440 !widget
->expansionToggleRect().contains(mappedPos
) &&
441 !widget
->selectionToggleRect().contains(mappedPos
);
443 hoveredWidget
= widget
;
448 if (unhoveredWidget
!= hoveredWidget
) {
449 if (unhoveredWidget
) {
450 unhoveredWidget
->setHovered(false);
451 emit
itemUnhovered(unhoveredWidget
->index());
455 hoveredWidget
->setHovered(true);
456 emit
itemHovered(hoveredWidget
->index());
463 bool KItemListController::hoverLeaveEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
468 if (!m_model
|| !m_view
) {
472 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
473 if (widget
->isHovered()) {
474 widget
->setHovered(false);
475 emit
itemUnhovered(widget
->index());
481 bool KItemListController::wheelEvent(QGraphicsSceneWheelEvent
* event
, const QTransform
& transform
)
488 bool KItemListController::resizeEvent(QGraphicsSceneResizeEvent
* event
, const QTransform
& transform
)
495 bool KItemListController::processEvent(QEvent
* event
, const QTransform
& transform
)
501 switch (event
->type()) {
502 case QEvent::KeyPress
:
503 return keyPressEvent(static_cast<QKeyEvent
*>(event
));
504 case QEvent::InputMethod
:
505 return inputMethodEvent(static_cast<QInputMethodEvent
*>(event
));
506 case QEvent::GraphicsSceneMousePress
:
507 return mousePressEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
508 case QEvent::GraphicsSceneMouseMove
:
509 return mouseMoveEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
510 case QEvent::GraphicsSceneMouseRelease
:
511 return mouseReleaseEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
512 case QEvent::GraphicsSceneWheel
:
513 return wheelEvent(static_cast<QGraphicsSceneWheelEvent
*>(event
), QTransform());
514 case QEvent::GraphicsSceneDragEnter
:
515 return dragEnterEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
516 case QEvent::GraphicsSceneDragLeave
:
517 return dragLeaveEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
518 case QEvent::GraphicsSceneDragMove
:
519 return dragMoveEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
520 case QEvent::GraphicsSceneDrop
:
521 return dropEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
522 case QEvent::GraphicsSceneHoverEnter
:
523 return hoverEnterEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
524 case QEvent::GraphicsSceneHoverMove
:
525 return hoverMoveEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
526 case QEvent::GraphicsSceneHoverLeave
:
527 return hoverLeaveEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
528 case QEvent::GraphicsSceneResize
:
529 return resizeEvent(static_cast<QGraphicsSceneResizeEvent
*>(event
), transform
);
537 void KItemListController::slotViewOffsetChanged(qreal current
, qreal previous
)
543 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
544 if (rubberBand
->isActive()) {
545 const qreal diff
= current
- previous
;
546 // TODO: Ideally just QCursor::pos() should be used as
547 // new end-position but it seems there is no easy way
548 // to have something like QWidget::mapFromGlobal() for QGraphicsWidget
549 // (... or I just missed an easy way to do the mapping)
550 QPointF endPos
= rubberBand
->endPosition();
551 if (m_view
->scrollOrientation() == Qt::Vertical
) {
557 rubberBand
->setEndPosition(endPos
);
561 void KItemListController::slotRubberBandChanged()
563 if (!m_view
|| !m_model
|| m_model
->count() <= 0) {
567 const KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
568 const QPointF startPos
= rubberBand
->startPosition();
569 const QPointF endPos
= rubberBand
->endPosition();
570 QRectF rubberBandRect
= QRectF(startPos
, endPos
).normalized();
572 const bool scrollVertical
= (m_view
->scrollOrientation() == Qt::Vertical
);
573 if (scrollVertical
) {
574 rubberBandRect
.translate(0, -m_view
->offset());
576 rubberBandRect
.translate(-m_view
->offset(), 0);
579 QSet
<int> selectedItems
;
581 // Select all visible items that intersect with the rubberband
582 foreach (const KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
583 const int index
= widget
->index();
585 const QRectF widgetRect
= m_view
->itemBoundingRect(index
);
586 if (widgetRect
.intersects(rubberBandRect
)) {
587 const QRectF iconRect
= widget
->iconBoundingRect().translated(widgetRect
.topLeft());
588 const QRectF textRect
= widget
->textBoundingRect().translated(widgetRect
.topLeft());
589 if (iconRect
.intersects(rubberBandRect
) || textRect
.intersects(rubberBandRect
)) {
590 selectedItems
.insert(index
);
595 // Select all invisible items that intersect with the rubberband. Instead of
596 // iterating all items only the area which might be touched by the rubberband
598 const bool increaseIndex
= scrollVertical
?
599 startPos
.y() > endPos
.y(): startPos
.x() > endPos
.x();
601 int index
= increaseIndex
? m_view
->lastVisibleIndex() + 1 : m_view
->firstVisibleIndex() - 1;
602 bool selectionFinished
= false;
604 const QRectF widgetRect
= m_view
->itemBoundingRect(index
);
605 if (widgetRect
.intersects(rubberBandRect
)) {
606 selectedItems
.insert(index
);
611 selectionFinished
= (index
>= m_model
->count()) ||
612 ( scrollVertical
&& widgetRect
.top() > rubberBandRect
.bottom()) ||
613 (!scrollVertical
&& widgetRect
.left() > rubberBandRect
.right());
616 selectionFinished
= (index
< 0) ||
617 ( scrollVertical
&& widgetRect
.bottom() < rubberBandRect
.top()) ||
618 (!scrollVertical
&& widgetRect
.right() < rubberBandRect
.left());
620 } while (!selectionFinished
);
622 m_selectionManager
->setSelectedItems(selectedItems
);
625 #include "kitemlistcontroller.moc"