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"
29 #include <QApplication>
32 #include <QGraphicsSceneEvent>
37 KItemListController::KItemListController(QObject
* parent
) :
40 m_selectionBehavior(NoSelection
),
43 m_selectionManager(new KItemListSelectionManager(this)),
50 KItemListController::~KItemListController()
54 void KItemListController::setModel(KItemModelBase
* model
)
56 if (m_model
== model
) {
60 KItemModelBase
* oldModel
= m_model
;
64 m_view
->setModel(m_model
);
67 m_selectionManager
->setModel(m_model
);
69 emit
modelChanged(m_model
, oldModel
);
72 KItemModelBase
* KItemListController::model() const
77 KItemListSelectionManager
* KItemListController::selectionManager() const
79 return m_selectionManager
;
82 void KItemListController::setView(KItemListView
* view
)
88 KItemListView
* oldView
= m_view
;
90 disconnect(oldView
, SIGNAL(offsetChanged(qreal
,qreal
)), this, SLOT(slotViewOffsetChanged(qreal
,qreal
)));
96 m_view
->setController(this);
97 m_view
->setModel(m_model
);
98 connect(m_view
, SIGNAL(offsetChanged(qreal
,qreal
)), this, SLOT(slotViewOffsetChanged(qreal
,qreal
)));
101 emit
viewChanged(m_view
, oldView
);
104 KItemListView
* KItemListController::view() const
109 void KItemListController::setSelectionBehavior(SelectionBehavior behavior
)
111 m_selectionBehavior
= behavior
;
114 KItemListController::SelectionBehavior
KItemListController::selectionBehavior() const
116 return m_selectionBehavior
;
119 bool KItemListController::showEvent(QShowEvent
* event
)
125 bool KItemListController::hideEvent(QHideEvent
* event
)
131 bool KItemListController::keyPressEvent(QKeyEvent
* event
)
133 const bool shiftPressed
= event
->modifiers() & Qt::ShiftModifier
;
134 const bool controlPressed
= event
->modifiers() & Qt::ControlModifier
;
135 const bool shiftOrControlPressed
= shiftPressed
|| controlPressed
;
137 int index
= m_selectionManager
->currentItem();
138 const int itemCount
= m_model
->count();
139 const int itemsPerRow
= m_view
->itemsPerOffset();
141 // For horizontal scroll orientation, transform
142 // the arrow keys to simplify the event handling.
143 int key
= event
->key();
144 if (m_view
->scrollOrientation() == Qt::Horizontal
) {
146 case Qt::Key_Up
: key
= Qt::Key_Left
; break;
147 case Qt::Key_Down
: key
= Qt::Key_Right
; break;
148 case Qt::Key_Left
: key
= Qt::Key_Up
; break;
149 case Qt::Key_Right
: key
= Qt::Key_Down
; break;
160 index
= itemCount
- 1;
170 if (index
< itemCount
- 1) {
176 if (index
>= itemsPerRow
) {
177 index
-= itemsPerRow
;
182 if (index
+ itemsPerRow
< itemCount
) {
183 // We are not in the last row yet.
184 index
+= itemsPerRow
;
187 // We are either in the last row already, or we are in the second-last row,
188 // and there is no item below the current item.
189 // In the latter case, we jump to the very last item.
190 const int currentColumn
= index
% itemsPerRow
;
191 const int lastItemColumn
= (itemCount
- 1) % itemsPerRow
;
192 const bool inLastRow
= currentColumn
< lastItemColumn
;
194 index
= itemCount
- 1;
200 if (controlPressed
) {
201 m_selectionManager
->endAnchoredSelection();
202 m_selectionManager
->setSelected(index
, 1, KItemListSelectionManager::Toggle
);
203 m_selectionManager
->beginAnchoredSelection(index
);
210 if (m_selectionManager
->currentItem() != index
) {
211 if (controlPressed
) {
212 m_selectionManager
->endAnchoredSelection();
215 m_selectionManager
->setCurrentItem(index
);
217 if (!shiftOrControlPressed
|| m_selectionBehavior
== SingleSelection
) {
218 m_selectionManager
->clearSelection();
219 m_selectionManager
->setSelected(index
, 1);
223 m_selectionManager
->beginAnchoredSelection(index
);
229 bool KItemListController::inputMethodEvent(QInputMethodEvent
* event
)
235 bool KItemListController::mousePressEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
242 m_pressedMousePos
= transform
.map(event
->pos());
243 m_pressedIndex
= m_view
->itemAt(m_pressedMousePos
);
245 if (m_view
->isAboveExpansionToggle(m_pressedIndex
, m_pressedMousePos
)) {
249 const bool shiftPressed
= event
->modifiers() & Qt::ShiftModifier
;
250 const bool controlPressed
= event
->modifiers() & Qt::ControlModifier
;
252 if (m_selectionBehavior
== SingleSelection
) {
253 m_selectionManager
->clearSelection();
257 // Finish the anchored selection before the current index is changed
258 m_selectionManager
->endAnchoredSelection();
261 if (m_pressedIndex
>= 0) {
262 m_selectionManager
->setCurrentItem(m_pressedIndex
);
264 switch (m_selectionBehavior
) {
268 case SingleSelection
:
269 m_selectionManager
->setSelected(m_pressedIndex
);
273 if (controlPressed
) {
274 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
275 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
276 } else if (!shiftPressed
|| !m_selectionManager
->isAnchoredSelectionActive()) {
277 // Select the pressed item and start a new anchored selection
278 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Select
);
279 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
290 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
291 QPointF startPos
= m_pressedMousePos
;
292 if (m_view
->scrollOrientation() == Qt::Vertical
) {
293 startPos
.ry() += m_view
->offset();
294 if (m_view
->itemSize().width() < 0) {
295 // Use a special rubberband for views that have only one column and
296 // expand the rubberband to use the whole width of the view.
300 startPos
.rx() += m_view
->offset();
303 m_oldSelection
= m_selectionManager
->selectedItems();
304 rubberBand
->setStartPosition(startPos
);
305 rubberBand
->setEndPosition(startPos
);
306 rubberBand
->setActive(true);
307 connect(rubberBand
, SIGNAL(endPositionChanged(QPointF
,QPointF
)), this, SLOT(slotRubberBandChanged()));
313 bool KItemListController::mouseMoveEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
319 if (m_pressedIndex
>= 0) {
320 // Check whether a dragging should be started
321 if (!m_dragging
&& (event
->buttons() & Qt::LeftButton
)) {
322 const QPointF pos
= transform
.map(event
->pos());
323 const qreal minDragDiff
= 4;
324 const bool hasMinDragDiff
= qAbs(pos
.x() - m_pressedMousePos
.x()) >= minDragDiff
||
325 qAbs(pos
.y() - m_pressedMousePos
.y()) >= minDragDiff
;
326 if (hasMinDragDiff
&& startDragging()) {
331 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
332 if (rubberBand
->isActive()) {
333 QPointF endPos
= transform
.map(event
->pos());
334 if (m_view
->scrollOrientation() == Qt::Vertical
) {
335 endPos
.ry() += m_view
->offset();
336 if (m_view
->itemSize().width() < 0) {
337 // Use a special rubberband for views that have only one column and
338 // expand the rubberband to use the whole width of the view.
339 endPos
.setX(m_view
->size().width());
342 endPos
.rx() += m_view
->offset();
344 rubberBand
->setEndPosition(endPos
);
351 bool KItemListController::mouseReleaseEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
357 const bool shiftOrControlPressed
= event
->modifiers() & Qt::ShiftModifier
||
358 event
->modifiers() & Qt::ControlModifier
;
360 bool clearSelection
= !shiftOrControlPressed
&& !m_dragging
&& !(event
->button() == Qt::RightButton
);
362 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
363 if (rubberBand
->isActive()) {
364 disconnect(rubberBand
, SIGNAL(endPositionChanged(QPointF
,QPointF
)), this, SLOT(slotRubberBandChanged()));
365 rubberBand
->setActive(false);
366 m_oldSelection
.clear();
368 if (rubberBand
->startPosition() != rubberBand
->endPosition()) {
369 clearSelection
= false;
373 const QPointF pos
= transform
.map(event
->pos());
374 const int index
= m_view
->itemAt(pos
);
376 if (index
>= 0 && index
== m_pressedIndex
) {
377 // The release event is done above the same item as the press event
379 if (clearSelection
) {
380 // Clear the previous selection but reselect the current index
381 m_selectionManager
->setSelectedItems(QSet
<int>() << index
);
384 bool emitItemClicked
= true;
385 if (event
->button() & Qt::LeftButton
) {
386 if (m_view
->isAboveExpansionToggle(index
, pos
)) {
387 emit
itemExpansionToggleClicked(index
);
388 emitItemClicked
= false;
389 } else if (shiftOrControlPressed
) {
390 // The mouse click should only update the selection, not trigger the item
391 emitItemClicked
= false;
395 if (emitItemClicked
) {
396 emit
itemClicked(index
, event
->button());
398 } else if (clearSelection
) {
399 m_selectionManager
->clearSelection();
403 m_pressedMousePos
= QPointF();
408 bool KItemListController::mouseDoubleClickEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
415 bool KItemListController::dragEnterEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
422 bool KItemListController::dragLeaveEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
429 bool KItemListController::dragMoveEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
436 bool KItemListController::dropEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
445 bool KItemListController::hoverEnterEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
452 bool KItemListController::hoverMoveEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
454 // The implementation assumes that only one item can get hovered no matter
455 // whether they overlap or not.
458 if (!m_model
|| !m_view
) {
462 // Search the previously hovered item that might get unhovered
463 KItemListWidget
* unhoveredWidget
= 0;
464 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
465 if (widget
->isHovered()) {
466 unhoveredWidget
= widget
;
471 // Search the currently hovered item
472 KItemListWidget
* hoveredWidget
= 0;
473 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
474 const QPointF mappedPos
= widget
->mapFromItem(m_view
, event
->pos());
476 const bool hovered
= widget
->contains(mappedPos
) &&
477 !widget
->expansionToggleRect().contains(mappedPos
) &&
478 !widget
->selectionToggleRect().contains(mappedPos
);
480 hoveredWidget
= widget
;
485 if (unhoveredWidget
!= hoveredWidget
) {
486 if (unhoveredWidget
) {
487 unhoveredWidget
->setHovered(false);
488 emit
itemUnhovered(unhoveredWidget
->index());
492 hoveredWidget
->setHovered(true);
493 emit
itemHovered(hoveredWidget
->index());
500 bool KItemListController::hoverLeaveEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
505 if (!m_model
|| !m_view
) {
509 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
510 if (widget
->isHovered()) {
511 widget
->setHovered(false);
512 emit
itemUnhovered(widget
->index());
518 bool KItemListController::wheelEvent(QGraphicsSceneWheelEvent
* event
, const QTransform
& transform
)
525 bool KItemListController::resizeEvent(QGraphicsSceneResizeEvent
* event
, const QTransform
& transform
)
532 bool KItemListController::processEvent(QEvent
* event
, const QTransform
& transform
)
538 switch (event
->type()) {
539 case QEvent::KeyPress
:
540 return keyPressEvent(static_cast<QKeyEvent
*>(event
));
541 case QEvent::InputMethod
:
542 return inputMethodEvent(static_cast<QInputMethodEvent
*>(event
));
543 case QEvent::GraphicsSceneMousePress
:
544 return mousePressEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
545 case QEvent::GraphicsSceneMouseMove
:
546 return mouseMoveEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
547 case QEvent::GraphicsSceneMouseRelease
:
548 return mouseReleaseEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
549 case QEvent::GraphicsSceneWheel
:
550 return wheelEvent(static_cast<QGraphicsSceneWheelEvent
*>(event
), QTransform());
551 case QEvent::GraphicsSceneDragEnter
:
552 return dragEnterEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
553 case QEvent::GraphicsSceneDragLeave
:
554 return dragLeaveEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
555 case QEvent::GraphicsSceneDragMove
:
556 return dragMoveEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
557 case QEvent::GraphicsSceneDrop
:
558 return dropEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
559 case QEvent::GraphicsSceneHoverEnter
:
560 return hoverEnterEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
561 case QEvent::GraphicsSceneHoverMove
:
562 return hoverMoveEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
563 case QEvent::GraphicsSceneHoverLeave
:
564 return hoverLeaveEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
565 case QEvent::GraphicsSceneResize
:
566 return resizeEvent(static_cast<QGraphicsSceneResizeEvent
*>(event
), transform
);
574 void KItemListController::slotViewOffsetChanged(qreal current
, qreal previous
)
580 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
581 if (rubberBand
->isActive()) {
582 const qreal diff
= current
- previous
;
583 // TODO: Ideally just QCursor::pos() should be used as
584 // new end-position but it seems there is no easy way
585 // to have something like QWidget::mapFromGlobal() for QGraphicsWidget
586 // (... or I just missed an easy way to do the mapping)
587 QPointF endPos
= rubberBand
->endPosition();
588 if (m_view
->scrollOrientation() == Qt::Vertical
) {
594 rubberBand
->setEndPosition(endPos
);
598 void KItemListController::slotRubberBandChanged()
600 if (!m_view
|| !m_model
|| m_model
->count() <= 0) {
604 const KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
605 const QPointF startPos
= rubberBand
->startPosition();
606 const QPointF endPos
= rubberBand
->endPosition();
607 QRectF rubberBandRect
= QRectF(startPos
, endPos
).normalized();
609 const bool scrollVertical
= (m_view
->scrollOrientation() == Qt::Vertical
);
610 if (scrollVertical
) {
611 rubberBandRect
.translate(0, -m_view
->offset());
613 rubberBandRect
.translate(-m_view
->offset(), 0);
616 if (!m_oldSelection
.isEmpty()) {
617 // Clear the old selection that was available before the rubberband has
618 // been activated in case if no Shift- or Control-key are pressed
619 const bool shiftOrControlPressed
= QApplication::keyboardModifiers() & Qt::ShiftModifier
||
620 QApplication::keyboardModifiers() & Qt::ControlModifier
;
621 if (!shiftOrControlPressed
) {
622 m_oldSelection
.clear();
626 QSet
<int> selectedItems
;
628 // Select all visible items that intersect with the rubberband
629 foreach (const KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
630 const int index
= widget
->index();
632 const QRectF widgetRect
= m_view
->itemBoundingRect(index
);
633 if (widgetRect
.intersects(rubberBandRect
)) {
634 const QRectF iconRect
= widget
->iconBoundingRect().translated(widgetRect
.topLeft());
635 const QRectF textRect
= widget
->textBoundingRect().translated(widgetRect
.topLeft());
636 if (iconRect
.intersects(rubberBandRect
) || textRect
.intersects(rubberBandRect
)) {
637 selectedItems
.insert(index
);
642 // Select all invisible items that intersect with the rubberband. Instead of
643 // iterating all items only the area which might be touched by the rubberband
645 const bool increaseIndex
= scrollVertical
?
646 startPos
.y() > endPos
.y(): startPos
.x() > endPos
.x();
648 int index
= increaseIndex
? m_view
->lastVisibleIndex() + 1 : m_view
->firstVisibleIndex() - 1;
649 bool selectionFinished
= false;
651 const QRectF widgetRect
= m_view
->itemBoundingRect(index
);
652 if (widgetRect
.intersects(rubberBandRect
)) {
653 selectedItems
.insert(index
);
658 selectionFinished
= (index
>= m_model
->count()) ||
659 ( scrollVertical
&& widgetRect
.top() > rubberBandRect
.bottom()) ||
660 (!scrollVertical
&& widgetRect
.left() > rubberBandRect
.right());
663 selectionFinished
= (index
< 0) ||
664 ( scrollVertical
&& widgetRect
.bottom() < rubberBandRect
.top()) ||
665 (!scrollVertical
&& widgetRect
.right() < rubberBandRect
.left());
667 } while (!selectionFinished
);
669 m_selectionManager
->setSelectedItems(selectedItems
+ m_oldSelection
);
672 bool KItemListController::startDragging()
674 if (!m_view
|| !m_model
) {
678 const QSet
<int> selectedItems
= m_selectionManager
->selectedItems();
679 QMimeData
* data
= m_model
->createMimeData(selectedItems
);
684 // The created drag object will be owned and deleted
685 // by QApplication::activeWindow().
686 QDrag
* drag
= new QDrag(QApplication::activeWindow());
687 drag
->setMimeData(data
);
689 const QPixmap pixmap
= m_view
->createDragPixmap(selectedItems
);
690 drag
->setPixmap(pixmap
);
692 drag
->exec(Qt::MoveAction
| Qt::CopyAction
| Qt::LinkAction
, Qt::IgnoreAction
);
696 #include "kitemlistcontroller.moc"