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 <QGraphicsSceneEvent>
36 #include <KGlobalSettings>
39 KItemListController::KItemListController(QObject
* parent
) :
42 m_selectionBehavior(NoSelection
),
45 m_selectionManager(new KItemListSelectionManager(this)),
46 m_keyboardManager(new KItemListKeyboardSearchManager(this)),
51 connect(m_keyboardManager
, SIGNAL(changeCurrentItem(QString
,bool)),
52 this, SLOT(slotChangeCurrentItem(QString
,bool)));
55 KItemListController::~KItemListController()
59 void KItemListController::setModel(KItemModelBase
* model
)
61 if (m_model
== model
) {
65 KItemModelBase
* oldModel
= m_model
;
69 m_view
->setModel(m_model
);
72 m_selectionManager
->setModel(m_model
);
74 emit
modelChanged(m_model
, oldModel
);
77 KItemModelBase
* KItemListController::model() const
82 KItemListSelectionManager
* KItemListController::selectionManager() const
84 return m_selectionManager
;
87 void KItemListController::setView(KItemListView
* view
)
93 KItemListView
* oldView
= m_view
;
95 disconnect(oldView
, SIGNAL(offsetChanged(qreal
,qreal
)), this, SLOT(slotViewOffsetChanged(qreal
,qreal
)));
101 m_view
->setController(this);
102 m_view
->setModel(m_model
);
103 connect(m_view
, SIGNAL(offsetChanged(qreal
,qreal
)), this, SLOT(slotViewOffsetChanged(qreal
,qreal
)));
106 emit
viewChanged(m_view
, oldView
);
109 KItemListView
* KItemListController::view() const
114 void KItemListController::setSelectionBehavior(SelectionBehavior behavior
)
116 m_selectionBehavior
= behavior
;
119 KItemListController::SelectionBehavior
KItemListController::selectionBehavior() const
121 return m_selectionBehavior
;
124 bool KItemListController::showEvent(QShowEvent
* event
)
130 bool KItemListController::hideEvent(QHideEvent
* event
)
136 bool KItemListController::keyPressEvent(QKeyEvent
* event
)
138 const bool shiftPressed
= event
->modifiers() & Qt::ShiftModifier
;
139 const bool controlPressed
= event
->modifiers() & Qt::ControlModifier
;
140 const bool shiftOrControlPressed
= shiftPressed
|| controlPressed
;
142 int index
= m_selectionManager
->currentItem();
143 const int itemCount
= m_model
->count();
144 const int itemsPerRow
= m_view
->itemsPerOffset();
146 // For horizontal scroll orientation, transform
147 // the arrow keys to simplify the event handling.
148 int key
= event
->key();
149 if (m_view
->scrollOrientation() == Qt::Horizontal
) {
151 case Qt::Key_Up
: key
= Qt::Key_Left
; break;
152 case Qt::Key_Down
: key
= Qt::Key_Right
; break;
153 case Qt::Key_Left
: key
= Qt::Key_Up
; break;
154 case Qt::Key_Right
: key
= Qt::Key_Down
; break;
165 index
= itemCount
- 1;
175 if (index
< itemCount
- 1) {
181 if (index
>= itemsPerRow
) {
182 index
-= itemsPerRow
;
187 if (index
+ itemsPerRow
< itemCount
) {
188 // We are not in the last row yet.
189 index
+= itemsPerRow
;
192 // We are either in the last row already, or we are in the second-last row,
193 // and there is no item below the current item.
194 // In the latter case, we jump to the very last item.
195 const int currentColumn
= index
% itemsPerRow
;
196 const int lastItemColumn
= (itemCount
- 1) % itemsPerRow
;
197 const bool inLastRow
= currentColumn
< lastItemColumn
;
199 index
= itemCount
- 1;
206 emit
itemActivated(index
);
210 if (controlPressed
) {
211 m_selectionManager
->endAnchoredSelection();
212 m_selectionManager
->setSelected(index
, 1, KItemListSelectionManager::Toggle
);
213 m_selectionManager
->beginAnchoredSelection(index
);
218 m_keyboardManager
->addKeys(event
->text());
222 if (m_selectionManager
->currentItem() != index
) {
223 if (controlPressed
) {
224 m_selectionManager
->endAnchoredSelection();
227 m_selectionManager
->setCurrentItem(index
);
229 if (!shiftOrControlPressed
|| m_selectionBehavior
== SingleSelection
) {
230 m_selectionManager
->clearSelection();
231 m_selectionManager
->setSelected(index
, 1);
235 m_selectionManager
->beginAnchoredSelection(index
);
241 void KItemListController::slotChangeCurrentItem(const QString
& text
, bool searchFromNextItem
)
246 const int currentIndex
= m_selectionManager
->currentItem();
248 if (searchFromNextItem
) {
249 index
= m_model
->indexForKeyboardSearch(text
, (currentIndex
+ 1) % m_model
->count());
252 index
= m_model
->indexForKeyboardSearch(text
, currentIndex
);
255 m_selectionManager
->setCurrentItem(index
);
256 m_selectionManager
->clearSelection();
257 m_selectionManager
->setSelected(index
, 1);
258 m_selectionManager
->beginAnchoredSelection(index
);
262 bool KItemListController::inputMethodEvent(QInputMethodEvent
* event
)
268 bool KItemListController::mousePressEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
275 m_pressedMousePos
= transform
.map(event
->pos());
276 m_pressedIndex
= m_view
->itemAt(m_pressedMousePos
);
278 if (m_view
->isAboveExpansionToggle(m_pressedIndex
, m_pressedMousePos
)) {
279 m_selectionManager
->setCurrentItem(m_pressedIndex
);
283 const bool shiftPressed
= event
->modifiers() & Qt::ShiftModifier
;
284 const bool controlPressed
= event
->modifiers() & Qt::ControlModifier
;
286 if (m_selectionBehavior
== SingleSelection
) {
287 m_selectionManager
->clearSelection();
291 // Finish the anchored selection before the current index is changed
292 m_selectionManager
->endAnchoredSelection();
295 if (m_pressedIndex
>= 0) {
296 m_selectionManager
->setCurrentItem(m_pressedIndex
);
298 switch (m_selectionBehavior
) {
302 case SingleSelection
:
303 m_selectionManager
->setSelected(m_pressedIndex
);
307 if (controlPressed
) {
308 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Toggle
);
309 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
310 } else if (!shiftPressed
|| !m_selectionManager
->isAnchoredSelectionActive()) {
311 // Select the pressed item and start a new anchored selection
312 m_selectionManager
->setSelected(m_pressedIndex
, 1, KItemListSelectionManager::Select
);
313 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
);
324 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
325 QPointF startPos
= m_pressedMousePos
;
326 if (m_view
->scrollOrientation() == Qt::Vertical
) {
327 startPos
.ry() += m_view
->offset();
328 if (m_view
->itemSize().width() < 0) {
329 // Use a special rubberband for views that have only one column and
330 // expand the rubberband to use the whole width of the view.
334 startPos
.rx() += m_view
->offset();
337 m_oldSelection
= m_selectionManager
->selectedItems();
338 rubberBand
->setStartPosition(startPos
);
339 rubberBand
->setEndPosition(startPos
);
340 rubberBand
->setActive(true);
341 connect(rubberBand
, SIGNAL(endPositionChanged(QPointF
,QPointF
)), this, SLOT(slotRubberBandChanged()));
342 m_view
->setAutoScroll(true);
348 bool KItemListController::mouseMoveEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
354 if (m_pressedIndex
>= 0) {
355 // Check whether a dragging should be started
356 if (!m_dragging
&& (event
->buttons() & Qt::LeftButton
)) {
357 const QPointF pos
= transform
.map(event
->pos());
358 const qreal minDragDiff
= 4;
359 const bool hasMinDragDiff
= qAbs(pos
.x() - m_pressedMousePos
.x()) >= minDragDiff
||
360 qAbs(pos
.y() - m_pressedMousePos
.y()) >= minDragDiff
;
361 if (hasMinDragDiff
&& startDragging()) {
366 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
367 if (rubberBand
->isActive()) {
368 QPointF endPos
= transform
.map(event
->pos());
369 if (m_view
->scrollOrientation() == Qt::Vertical
) {
370 endPos
.ry() += m_view
->offset();
371 if (m_view
->itemSize().width() < 0) {
372 // Use a special rubberband for views that have only one column and
373 // expand the rubberband to use the whole width of the view.
374 endPos
.setX(m_view
->size().width());
377 endPos
.rx() += m_view
->offset();
379 rubberBand
->setEndPosition(endPos
);
386 bool KItemListController::mouseReleaseEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
392 const bool shiftOrControlPressed
= event
->modifiers() & Qt::ShiftModifier
||
393 event
->modifiers() & Qt::ControlModifier
;
395 bool clearSelection
= !shiftOrControlPressed
&& !m_dragging
&& !(event
->button() == Qt::RightButton
);
397 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
398 if (rubberBand
->isActive()) {
399 disconnect(rubberBand
, SIGNAL(endPositionChanged(QPointF
,QPointF
)), this, SLOT(slotRubberBandChanged()));
400 rubberBand
->setActive(false);
401 m_oldSelection
.clear();
402 m_view
->setAutoScroll(false);
404 if (rubberBand
->startPosition() != rubberBand
->endPosition()) {
405 clearSelection
= false;
409 const QPointF pos
= transform
.map(event
->pos());
410 const int index
= m_view
->itemAt(pos
);
412 if (index
>= 0 && index
== m_pressedIndex
) {
413 // The release event is done above the same item as the press event
415 if (clearSelection
) {
416 // Clear the previous selection but reselect the current index
417 m_selectionManager
->setSelectedItems(QSet
<int>() << index
);
420 if (event
->button() & Qt::LeftButton
) {
421 bool emitItemActivated
= true;
422 if (m_view
->isAboveExpansionToggle(index
, pos
)) {
423 emit
itemExpansionToggleClicked(index
);
424 emitItemActivated
= false;
425 } else if (shiftOrControlPressed
) {
426 // The mouse click should only update the selection, not trigger the item
427 emitItemActivated
= false;
428 } else if (!KGlobalSettings::singleClick()) {
429 emitItemActivated
= false;
431 if (emitItemActivated
) {
432 emit
itemActivated(index
);
434 } else if (event
->button() & Qt::MidButton
) {
435 emit
itemMiddleClicked(index
);
436 } else if (event
->button() & Qt::RightButton
) {
437 emit
contextMenuRequested(index
, QPointF(event
->pos()));
439 } else if (clearSelection
) {
440 m_selectionManager
->clearSelection();
444 m_pressedMousePos
= QPointF();
449 bool KItemListController::mouseDoubleClickEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
)
451 const QPointF pos
= transform
.map(event
->pos());
452 const int index
= m_view
->itemAt(pos
);
454 bool emitItemActivated
= !KGlobalSettings::singleClick() &&
455 (event
->button() & Qt::LeftButton
) &&
456 index
>= 0 && index
< m_model
->count();
457 if (emitItemActivated
) {
458 emit
itemActivated(index
);
463 bool KItemListController::dragEnterEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
470 bool KItemListController::dragLeaveEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
477 bool KItemListController::dragMoveEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
484 bool KItemListController::dropEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
)
493 bool KItemListController::hoverEnterEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
500 bool KItemListController::hoverMoveEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
502 // The implementation assumes that only one item can get hovered no matter
503 // whether they overlap or not.
506 if (!m_model
|| !m_view
) {
510 // Search the previously hovered item that might get unhovered
511 KItemListWidget
* unhoveredWidget
= 0;
512 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
513 if (widget
->isHovered()) {
514 unhoveredWidget
= widget
;
519 // Search the currently hovered item
520 KItemListWidget
* hoveredWidget
= 0;
521 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
522 const QPointF mappedPos
= widget
->mapFromItem(m_view
, event
->pos());
524 const bool hovered
= widget
->contains(mappedPos
) &&
525 !widget
->expansionToggleRect().contains(mappedPos
) &&
526 !widget
->selectionToggleRect().contains(mappedPos
);
528 hoveredWidget
= widget
;
533 if (unhoveredWidget
!= hoveredWidget
) {
534 if (unhoveredWidget
) {
535 unhoveredWidget
->setHovered(false);
536 emit
itemUnhovered(unhoveredWidget
->index());
540 hoveredWidget
->setHovered(true);
541 emit
itemHovered(hoveredWidget
->index());
548 bool KItemListController::hoverLeaveEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
)
553 if (!m_model
|| !m_view
) {
557 foreach (KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
558 if (widget
->isHovered()) {
559 widget
->setHovered(false);
560 emit
itemUnhovered(widget
->index());
566 bool KItemListController::wheelEvent(QGraphicsSceneWheelEvent
* event
, const QTransform
& transform
)
573 bool KItemListController::resizeEvent(QGraphicsSceneResizeEvent
* event
, const QTransform
& transform
)
580 bool KItemListController::processEvent(QEvent
* event
, const QTransform
& transform
)
586 switch (event
->type()) {
587 case QEvent::KeyPress
:
588 return keyPressEvent(static_cast<QKeyEvent
*>(event
));
589 case QEvent::InputMethod
:
590 return inputMethodEvent(static_cast<QInputMethodEvent
*>(event
));
591 case QEvent::GraphicsSceneMousePress
:
592 return mousePressEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
593 case QEvent::GraphicsSceneMouseMove
:
594 return mouseMoveEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
595 case QEvent::GraphicsSceneMouseRelease
:
596 return mouseReleaseEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
597 case QEvent::GraphicsSceneMouseDoubleClick
:
598 return mouseDoubleClickEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
599 case QEvent::GraphicsSceneWheel
:
600 return wheelEvent(static_cast<QGraphicsSceneWheelEvent
*>(event
), QTransform());
601 case QEvent::GraphicsSceneDragEnter
:
602 return dragEnterEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
603 case QEvent::GraphicsSceneDragLeave
:
604 return dragLeaveEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
605 case QEvent::GraphicsSceneDragMove
:
606 return dragMoveEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
607 case QEvent::GraphicsSceneDrop
:
608 return dropEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
609 case QEvent::GraphicsSceneHoverEnter
:
610 return hoverEnterEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
611 case QEvent::GraphicsSceneHoverMove
:
612 return hoverMoveEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
613 case QEvent::GraphicsSceneHoverLeave
:
614 return hoverLeaveEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
615 case QEvent::GraphicsSceneResize
:
616 return resizeEvent(static_cast<QGraphicsSceneResizeEvent
*>(event
), transform
);
624 void KItemListController::slotViewOffsetChanged(qreal current
, qreal previous
)
630 KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
631 if (rubberBand
->isActive()) {
632 const qreal diff
= current
- previous
;
633 // TODO: Ideally just QCursor::pos() should be used as
634 // new end-position but it seems there is no easy way
635 // to have something like QWidget::mapFromGlobal() for QGraphicsWidget
636 // (... or I just missed an easy way to do the mapping)
637 QPointF endPos
= rubberBand
->endPosition();
638 if (m_view
->scrollOrientation() == Qt::Vertical
) {
644 rubberBand
->setEndPosition(endPos
);
648 void KItemListController::slotRubberBandChanged()
650 if (!m_view
|| !m_model
|| m_model
->count() <= 0) {
654 const KItemListRubberBand
* rubberBand
= m_view
->rubberBand();
655 const QPointF startPos
= rubberBand
->startPosition();
656 const QPointF endPos
= rubberBand
->endPosition();
657 QRectF rubberBandRect
= QRectF(startPos
, endPos
).normalized();
659 const bool scrollVertical
= (m_view
->scrollOrientation() == Qt::Vertical
);
660 if (scrollVertical
) {
661 rubberBandRect
.translate(0, -m_view
->offset());
663 rubberBandRect
.translate(-m_view
->offset(), 0);
666 if (!m_oldSelection
.isEmpty()) {
667 // Clear the old selection that was available before the rubberband has
668 // been activated in case if no Shift- or Control-key are pressed
669 const bool shiftOrControlPressed
= QApplication::keyboardModifiers() & Qt::ShiftModifier
||
670 QApplication::keyboardModifiers() & Qt::ControlModifier
;
671 if (!shiftOrControlPressed
) {
672 m_oldSelection
.clear();
676 QSet
<int> selectedItems
;
678 // Select all visible items that intersect with the rubberband
679 foreach (const KItemListWidget
* widget
, m_view
->visibleItemListWidgets()) {
680 const int index
= widget
->index();
682 const QRectF widgetRect
= m_view
->itemBoundingRect(index
);
683 if (widgetRect
.intersects(rubberBandRect
)) {
684 const QRectF iconRect
= widget
->iconBoundingRect().translated(widgetRect
.topLeft());
685 const QRectF textRect
= widget
->textBoundingRect().translated(widgetRect
.topLeft());
686 if (iconRect
.intersects(rubberBandRect
) || textRect
.intersects(rubberBandRect
)) {
687 selectedItems
.insert(index
);
692 // Select all invisible items that intersect with the rubberband. Instead of
693 // iterating all items only the area which might be touched by the rubberband
695 const bool increaseIndex
= scrollVertical
?
696 startPos
.y() > endPos
.y(): startPos
.x() > endPos
.x();
698 int index
= increaseIndex
? m_view
->lastVisibleIndex() + 1 : m_view
->firstVisibleIndex() - 1;
699 bool selectionFinished
= false;
701 const QRectF widgetRect
= m_view
->itemBoundingRect(index
);
702 if (widgetRect
.intersects(rubberBandRect
)) {
703 selectedItems
.insert(index
);
708 selectionFinished
= (index
>= m_model
->count()) ||
709 ( scrollVertical
&& widgetRect
.top() > rubberBandRect
.bottom()) ||
710 (!scrollVertical
&& widgetRect
.left() > rubberBandRect
.right());
713 selectionFinished
= (index
< 0) ||
714 ( scrollVertical
&& widgetRect
.bottom() < rubberBandRect
.top()) ||
715 (!scrollVertical
&& widgetRect
.right() < rubberBandRect
.left());
717 } while (!selectionFinished
);
719 if (QApplication::keyboardModifiers() & Qt::ControlModifier
) {
720 // If Control is pressed, the selection state of all items in the rubberband is toggled.
721 // Therefore, the new selection contains:
722 // 1. All previously selected items which are not inside the rubberband, and
723 // 2. all items inside the rubberband which have not been selected previously.
724 m_selectionManager
->setSelectedItems((m_oldSelection
- selectedItems
) + (selectedItems
- m_oldSelection
));
727 m_selectionManager
->setSelectedItems(selectedItems
+ m_oldSelection
);
731 bool KItemListController::startDragging()
733 if (!m_view
|| !m_model
) {
737 const QSet
<int> selectedItems
= m_selectionManager
->selectedItems();
738 QMimeData
* data
= m_model
->createMimeData(selectedItems
);
743 // The created drag object will be owned and deleted
744 // by QApplication::activeWindow().
745 QDrag
* drag
= new QDrag(QApplication::activeWindow());
746 drag
->setMimeData(data
);
748 const QPixmap pixmap
= m_view
->createDragPixmap(selectedItems
);
749 drag
->setPixmap(pixmap
);
751 drag
->exec(Qt::MoveAction
| Qt::CopyAction
| Qt::LinkAction
, Qt::IgnoreAction
);
755 #include "kitemlistcontroller.moc"