]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistcontroller.cpp
Fix context-menu selection issue
[dolphin.git] / src / kitemviews / kitemlistcontroller.cpp
1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
3 * *
4 * Based on the Itemviews NG project from Trolltech Labs: *
5 * http://qt.gitorious.org/qt-labs/itemviews-ng *
6 * *
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. *
11 * *
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. *
16 * *
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 ***************************************************************************/
22
23 #include "kitemlistcontroller.h"
24
25 #include "kitemlistview.h"
26 #include "kitemlistrubberband_p.h"
27 #include "kitemlistselectionmanager.h"
28 #include "kitemlistkeyboardsearchmanager_p.h"
29
30 #include <QApplication>
31 #include <QDrag>
32 #include <QEvent>
33 #include <QGraphicsSceneEvent>
34 #include <QMimeData>
35
36 #include <KGlobalSettings>
37 #include <KDebug>
38
39 KItemListController::KItemListController(QObject* parent) :
40 QObject(parent),
41 m_dragging(false),
42 m_selectionBehavior(NoSelection),
43 m_model(0),
44 m_view(0),
45 m_selectionManager(new KItemListSelectionManager(this)),
46 m_keyboardManager(new KItemListKeyboardSearchManager(this)),
47 m_pressedIndex(-1),
48 m_pressedMousePos(),
49 m_oldSelection()
50 {
51 connect(m_keyboardManager, SIGNAL(changeCurrentItem(QString,bool)),
52 this, SLOT(slotChangeCurrentItem(QString,bool)));
53 }
54
55 KItemListController::~KItemListController()
56 {
57 }
58
59 void KItemListController::setModel(KItemModelBase* model)
60 {
61 if (m_model == model) {
62 return;
63 }
64
65 KItemModelBase* oldModel = m_model;
66 m_model = model;
67
68 if (m_view) {
69 m_view->setModel(m_model);
70 }
71
72 m_selectionManager->setModel(m_model);
73
74 emit modelChanged(m_model, oldModel);
75 }
76
77 KItemModelBase* KItemListController::model() const
78 {
79 return m_model;
80 }
81
82 KItemListSelectionManager* KItemListController::selectionManager() const
83 {
84 return m_selectionManager;
85 }
86
87 void KItemListController::setView(KItemListView* view)
88 {
89 if (m_view == view) {
90 return;
91 }
92
93 KItemListView* oldView = m_view;
94 if (oldView) {
95 disconnect(oldView, SIGNAL(offsetChanged(qreal,qreal)), this, SLOT(slotViewOffsetChanged(qreal,qreal)));
96 }
97
98 m_view = view;
99
100 if (m_view) {
101 m_view->setController(this);
102 m_view->setModel(m_model);
103 connect(m_view, SIGNAL(offsetChanged(qreal,qreal)), this, SLOT(slotViewOffsetChanged(qreal,qreal)));
104 }
105
106 emit viewChanged(m_view, oldView);
107 }
108
109 KItemListView* KItemListController::view() const
110 {
111 return m_view;
112 }
113
114 void KItemListController::setSelectionBehavior(SelectionBehavior behavior)
115 {
116 m_selectionBehavior = behavior;
117 }
118
119 KItemListController::SelectionBehavior KItemListController::selectionBehavior() const
120 {
121 return m_selectionBehavior;
122 }
123
124 bool KItemListController::showEvent(QShowEvent* event)
125 {
126 Q_UNUSED(event);
127 return false;
128 }
129
130 bool KItemListController::hideEvent(QHideEvent* event)
131 {
132 Q_UNUSED(event);
133 return false;
134 }
135
136 bool KItemListController::keyPressEvent(QKeyEvent* event)
137 {
138 const bool shiftPressed = event->modifiers() & Qt::ShiftModifier;
139 const bool controlPressed = event->modifiers() & Qt::ControlModifier;
140 const bool shiftOrControlPressed = shiftPressed || controlPressed;
141
142 int index = m_selectionManager->currentItem();
143 const int itemCount = m_model->count();
144 const int itemsPerRow = m_view->itemsPerOffset();
145
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) {
150 switch (key) {
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;
155 default: break;
156 }
157 }
158
159 switch (key) {
160 case Qt::Key_Home:
161 index = 0;
162 break;
163
164 case Qt::Key_End:
165 index = itemCount - 1;
166 break;
167
168 case Qt::Key_Left:
169 if (index > 0) {
170 index--;
171 }
172 break;
173
174 case Qt::Key_Right:
175 if (index < itemCount - 1) {
176 index++;
177 }
178 break;
179
180 case Qt::Key_Up:
181 if (index >= itemsPerRow) {
182 index -= itemsPerRow;
183 }
184 break;
185
186 case Qt::Key_Down:
187 if (index + itemsPerRow < itemCount) {
188 // We are not in the last row yet.
189 index += itemsPerRow;
190 }
191 else {
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;
198 if (!inLastRow) {
199 index = itemCount - 1;
200 }
201 }
202 break;
203
204 case Qt::Key_Enter:
205 case Qt::Key_Return:
206 emit itemActivated(index);
207 break;
208
209 case Qt::Key_Space:
210 if (controlPressed) {
211 m_selectionManager->endAnchoredSelection();
212 m_selectionManager->setSelected(index, 1, KItemListSelectionManager::Toggle);
213 m_selectionManager->beginAnchoredSelection(index);
214 break;
215 }
216
217 default:
218 m_keyboardManager->addKeys(event->text());
219 return false;
220 }
221
222 if (m_selectionManager->currentItem() != index) {
223 if (controlPressed) {
224 m_selectionManager->endAnchoredSelection();
225 }
226
227 m_selectionManager->setCurrentItem(index);
228
229 if (!shiftOrControlPressed || m_selectionBehavior == SingleSelection) {
230 m_selectionManager->clearSelection();
231 m_selectionManager->setSelected(index, 1);
232 }
233
234 if (!shiftPressed) {
235 m_selectionManager->beginAnchoredSelection(index);
236 }
237 }
238 return true;
239 }
240
241 void KItemListController::slotChangeCurrentItem(const QString& text, bool searchFromNextItem)
242 {
243 if (!m_model) {
244 return;
245 }
246 const int currentIndex = m_selectionManager->currentItem();
247 int index;
248 if (searchFromNextItem) {
249 index = m_model->indexForKeyboardSearch(text, (currentIndex + 1) % m_model->count());
250 }
251 else {
252 index = m_model->indexForKeyboardSearch(text, currentIndex);
253 }
254 if (index >= 0) {
255 m_selectionManager->setCurrentItem(index);
256 m_selectionManager->clearSelection();
257 m_selectionManager->setSelected(index, 1);
258 m_selectionManager->beginAnchoredSelection(index);
259 }
260 }
261
262 bool KItemListController::inputMethodEvent(QInputMethodEvent* event)
263 {
264 Q_UNUSED(event);
265 return false;
266 }
267
268 bool KItemListController::mousePressEvent(QGraphicsSceneMouseEvent* event, const QTransform& transform)
269 {
270 if (!m_view) {
271 return false;
272 }
273
274 m_dragging = false;
275 m_pressedMousePos = transform.map(event->pos());
276 m_pressedIndex = m_view->itemAt(m_pressedMousePos);
277
278 if (m_view->isAboveExpansionToggle(m_pressedIndex, m_pressedMousePos)) {
279 m_selectionManager->setCurrentItem(m_pressedIndex);
280 return true;
281 }
282
283 const bool shiftPressed = event->modifiers() & Qt::ShiftModifier;
284 const bool controlPressed = event->modifiers() & Qt::ControlModifier;
285
286 if (m_selectionBehavior == SingleSelection) {
287 m_selectionManager->clearSelection();
288 }
289
290 if (!shiftPressed) {
291 // Finish the anchored selection before the current index is changed
292 m_selectionManager->endAnchoredSelection();
293 }
294
295 if (m_pressedIndex >= 0) {
296 m_selectionManager->setCurrentItem(m_pressedIndex);
297
298 switch (m_selectionBehavior) {
299 case NoSelection:
300 break;
301
302 case SingleSelection:
303 m_selectionManager->setSelected(m_pressedIndex);
304 break;
305
306 case MultiSelection:
307 if (controlPressed) {
308 m_selectionManager->setSelected(m_pressedIndex, 1, KItemListSelectionManager::Toggle);
309 m_selectionManager->beginAnchoredSelection(m_pressedIndex);
310 } else if (event->buttons() & Qt::RightButton) {
311 // Only clear the selection if a context menu is requested above a non-selected item
312 if (!m_selectionManager->selectedItems().contains(m_pressedIndex)) {
313 m_selectionManager->setSelectedItems(QSet<int>() << m_pressedIndex);
314 }
315 } else if (!shiftPressed || !m_selectionManager->isAnchoredSelectionActive()) {
316 // Select the pressed item and start a new anchored selection
317 m_selectionManager->setSelected(m_pressedIndex, 1, KItemListSelectionManager::Select);
318 m_selectionManager->beginAnchoredSelection(m_pressedIndex);
319 }
320 break;
321
322 default:
323 Q_ASSERT(false);
324 break;
325 }
326
327 return true;
328 } else {
329 if (event->buttons() & Qt::RightButton) {
330 m_selectionManager->clearSelection();
331 }
332
333 KItemListRubberBand* rubberBand = m_view->rubberBand();
334 QPointF startPos = m_pressedMousePos;
335 if (m_view->scrollOrientation() == Qt::Vertical) {
336 startPos.ry() += m_view->offset();
337 if (m_view->itemSize().width() < 0) {
338 // Use a special rubberband for views that have only one column and
339 // expand the rubberband to use the whole width of the view.
340 startPos.setX(0);
341 }
342 } else {
343 startPos.rx() += m_view->offset();
344 }
345
346 m_oldSelection = m_selectionManager->selectedItems();
347 rubberBand->setStartPosition(startPos);
348 rubberBand->setEndPosition(startPos);
349 rubberBand->setActive(true);
350 connect(rubberBand, SIGNAL(endPositionChanged(QPointF,QPointF)), this, SLOT(slotRubberBandChanged()));
351 m_view->setAutoScroll(true);
352 }
353
354 return false;
355 }
356
357 bool KItemListController::mouseMoveEvent(QGraphicsSceneMouseEvent* event, const QTransform& transform)
358 {
359 if (!m_view) {
360 return false;
361 }
362
363 if (m_pressedIndex >= 0) {
364 // Check whether a dragging should be started
365 if (!m_dragging && (event->buttons() & Qt::LeftButton)) {
366 const QPointF pos = transform.map(event->pos());
367 const qreal minDragDiff = 4;
368 const bool hasMinDragDiff = qAbs(pos.x() - m_pressedMousePos.x()) >= minDragDiff ||
369 qAbs(pos.y() - m_pressedMousePos.y()) >= minDragDiff;
370 if (hasMinDragDiff && startDragging()) {
371 m_dragging = true;
372 }
373 }
374 } else {
375 KItemListRubberBand* rubberBand = m_view->rubberBand();
376 if (rubberBand->isActive()) {
377 QPointF endPos = transform.map(event->pos());
378 if (m_view->scrollOrientation() == Qt::Vertical) {
379 endPos.ry() += m_view->offset();
380 if (m_view->itemSize().width() < 0) {
381 // Use a special rubberband for views that have only one column and
382 // expand the rubberband to use the whole width of the view.
383 endPos.setX(m_view->size().width());
384 }
385 } else {
386 endPos.rx() += m_view->offset();
387 }
388 rubberBand->setEndPosition(endPos);
389 }
390 }
391
392 return false;
393 }
394
395 bool KItemListController::mouseReleaseEvent(QGraphicsSceneMouseEvent* event, const QTransform& transform)
396 {
397 if (!m_view) {
398 return false;
399 }
400
401 const bool shiftOrControlPressed = event->modifiers() & Qt::ShiftModifier ||
402 event->modifiers() & Qt::ControlModifier;
403
404 bool clearSelection = !shiftOrControlPressed && !m_dragging && event->button() != Qt::RightButton;
405
406 KItemListRubberBand* rubberBand = m_view->rubberBand();
407 if (rubberBand->isActive()) {
408 disconnect(rubberBand, SIGNAL(endPositionChanged(QPointF,QPointF)), this, SLOT(slotRubberBandChanged()));
409 rubberBand->setActive(false);
410 m_oldSelection.clear();
411 m_view->setAutoScroll(false);
412
413 if (rubberBand->startPosition() != rubberBand->endPosition()) {
414 clearSelection = false;
415 }
416 }
417
418 const QPointF pos = transform.map(event->pos());
419 const int index = m_view->itemAt(pos);
420
421 if (index >= 0 && index == m_pressedIndex) {
422 // The release event is done above the same item as the press event
423
424 if (clearSelection) {
425 // Clear the previous selection but reselect the current index
426 m_selectionManager->setSelectedItems(QSet<int>() << index);
427 }
428
429 if (event->button() & Qt::LeftButton) {
430 bool emitItemActivated = true;
431 if (m_view->isAboveExpansionToggle(index, pos)) {
432 emit itemExpansionToggleClicked(index);
433 emitItemActivated = false;
434 } else if (shiftOrControlPressed) {
435 // The mouse click should only update the selection, not trigger the item
436 emitItemActivated = false;
437 } else if (!KGlobalSettings::singleClick()) {
438 emitItemActivated = false;
439 }
440 if (emitItemActivated) {
441 emit itemActivated(index);
442 }
443 } else if (event->button() & Qt::MidButton) {
444 emit itemMiddleClicked(index);
445 } else if (event->button() & Qt::RightButton) {
446 emit contextMenuRequested(index, QPointF(event->pos()));
447 }
448 } else if (clearSelection) {
449 m_selectionManager->clearSelection();
450 }
451
452 m_dragging = false;
453 m_pressedMousePos = QPointF();
454 m_pressedIndex = -1;
455 return false;
456 }
457
458 bool KItemListController::mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event, const QTransform& transform)
459 {
460 const QPointF pos = transform.map(event->pos());
461 const int index = m_view->itemAt(pos);
462
463 bool emitItemActivated = !KGlobalSettings::singleClick() &&
464 (event->button() & Qt::LeftButton) &&
465 index >= 0 && index < m_model->count();
466 if (emitItemActivated) {
467 emit itemActivated(index);
468 }
469 return false;
470 }
471
472 bool KItemListController::dragEnterEvent(QGraphicsSceneDragDropEvent* event, const QTransform& transform)
473 {
474 Q_UNUSED(event);
475 Q_UNUSED(transform);
476 return false;
477 }
478
479 bool KItemListController::dragLeaveEvent(QGraphicsSceneDragDropEvent* event, const QTransform& transform)
480 {
481 Q_UNUSED(event);
482 Q_UNUSED(transform);
483 return false;
484 }
485
486 bool KItemListController::dragMoveEvent(QGraphicsSceneDragDropEvent* event, const QTransform& transform)
487 {
488 Q_UNUSED(event);
489 Q_UNUSED(transform);
490 return false;
491 }
492
493 bool KItemListController::dropEvent(QGraphicsSceneDragDropEvent* event, const QTransform& transform)
494 {
495 Q_UNUSED(event);
496 Q_UNUSED(transform);
497
498 m_dragging = false;
499 return false;
500 }
501
502 bool KItemListController::hoverEnterEvent(QGraphicsSceneHoverEvent* event, const QTransform& transform)
503 {
504 Q_UNUSED(event);
505 Q_UNUSED(transform);
506 return false;
507 }
508
509 bool KItemListController::hoverMoveEvent(QGraphicsSceneHoverEvent* event, const QTransform& transform)
510 {
511 // The implementation assumes that only one item can get hovered no matter
512 // whether they overlap or not.
513
514 Q_UNUSED(transform);
515 if (!m_model || !m_view) {
516 return false;
517 }
518
519 // Search the previously hovered item that might get unhovered
520 KItemListWidget* unhoveredWidget = 0;
521 foreach (KItemListWidget* widget, m_view->visibleItemListWidgets()) {
522 if (widget->isHovered()) {
523 unhoveredWidget = widget;
524 break;
525 }
526 }
527
528 // Search the currently hovered item
529 KItemListWidget* hoveredWidget = 0;
530 foreach (KItemListWidget* widget, m_view->visibleItemListWidgets()) {
531 const QPointF mappedPos = widget->mapFromItem(m_view, event->pos());
532
533 const bool hovered = widget->contains(mappedPos) &&
534 !widget->expansionToggleRect().contains(mappedPos) &&
535 !widget->selectionToggleRect().contains(mappedPos);
536 if (hovered) {
537 hoveredWidget = widget;
538 break;
539 }
540 }
541
542 if (unhoveredWidget != hoveredWidget) {
543 if (unhoveredWidget) {
544 unhoveredWidget->setHovered(false);
545 emit itemUnhovered(unhoveredWidget->index());
546 }
547
548 if (hoveredWidget) {
549 hoveredWidget->setHovered(true);
550 emit itemHovered(hoveredWidget->index());
551 }
552 }
553
554 return false;
555 }
556
557 bool KItemListController::hoverLeaveEvent(QGraphicsSceneHoverEvent* event, const QTransform& transform)
558 {
559 Q_UNUSED(event);
560 Q_UNUSED(transform);
561
562 if (!m_model || !m_view) {
563 return false;
564 }
565
566 foreach (KItemListWidget* widget, m_view->visibleItemListWidgets()) {
567 if (widget->isHovered()) {
568 widget->setHovered(false);
569 emit itemUnhovered(widget->index());
570 }
571 }
572 return false;
573 }
574
575 bool KItemListController::wheelEvent(QGraphicsSceneWheelEvent* event, const QTransform& transform)
576 {
577 Q_UNUSED(event);
578 Q_UNUSED(transform);
579 return false;
580 }
581
582 bool KItemListController::resizeEvent(QGraphicsSceneResizeEvent* event, const QTransform& transform)
583 {
584 Q_UNUSED(event);
585 Q_UNUSED(transform);
586 return false;
587 }
588
589 bool KItemListController::processEvent(QEvent* event, const QTransform& transform)
590 {
591 if (!event) {
592 return false;
593 }
594
595 switch (event->type()) {
596 case QEvent::KeyPress:
597 return keyPressEvent(static_cast<QKeyEvent*>(event));
598 case QEvent::InputMethod:
599 return inputMethodEvent(static_cast<QInputMethodEvent*>(event));
600 case QEvent::GraphicsSceneMousePress:
601 return mousePressEvent(static_cast<QGraphicsSceneMouseEvent*>(event), QTransform());
602 case QEvent::GraphicsSceneMouseMove:
603 return mouseMoveEvent(static_cast<QGraphicsSceneMouseEvent*>(event), QTransform());
604 case QEvent::GraphicsSceneMouseRelease:
605 return mouseReleaseEvent(static_cast<QGraphicsSceneMouseEvent*>(event), QTransform());
606 case QEvent::GraphicsSceneMouseDoubleClick:
607 return mouseDoubleClickEvent(static_cast<QGraphicsSceneMouseEvent*>(event), QTransform());
608 case QEvent::GraphicsSceneWheel:
609 return wheelEvent(static_cast<QGraphicsSceneWheelEvent*>(event), QTransform());
610 case QEvent::GraphicsSceneDragEnter:
611 return dragEnterEvent(static_cast<QGraphicsSceneDragDropEvent*>(event), QTransform());
612 case QEvent::GraphicsSceneDragLeave:
613 return dragLeaveEvent(static_cast<QGraphicsSceneDragDropEvent*>(event), QTransform());
614 case QEvent::GraphicsSceneDragMove:
615 return dragMoveEvent(static_cast<QGraphicsSceneDragDropEvent*>(event), QTransform());
616 case QEvent::GraphicsSceneDrop:
617 return dropEvent(static_cast<QGraphicsSceneDragDropEvent*>(event), QTransform());
618 case QEvent::GraphicsSceneHoverEnter:
619 return hoverEnterEvent(static_cast<QGraphicsSceneHoverEvent*>(event), QTransform());
620 case QEvent::GraphicsSceneHoverMove:
621 return hoverMoveEvent(static_cast<QGraphicsSceneHoverEvent*>(event), QTransform());
622 case QEvent::GraphicsSceneHoverLeave:
623 return hoverLeaveEvent(static_cast<QGraphicsSceneHoverEvent*>(event), QTransform());
624 case QEvent::GraphicsSceneResize:
625 return resizeEvent(static_cast<QGraphicsSceneResizeEvent*>(event), transform);
626 default:
627 break;
628 }
629
630 return false;
631 }
632
633 void KItemListController::slotViewOffsetChanged(qreal current, qreal previous)
634 {
635 if (!m_view) {
636 return;
637 }
638
639 KItemListRubberBand* rubberBand = m_view->rubberBand();
640 if (rubberBand->isActive()) {
641 const qreal diff = current - previous;
642 // TODO: Ideally just QCursor::pos() should be used as
643 // new end-position but it seems there is no easy way
644 // to have something like QWidget::mapFromGlobal() for QGraphicsWidget
645 // (... or I just missed an easy way to do the mapping)
646 QPointF endPos = rubberBand->endPosition();
647 if (m_view->scrollOrientation() == Qt::Vertical) {
648 endPos.ry() += diff;
649 } else {
650 endPos.rx() += diff;
651 }
652
653 rubberBand->setEndPosition(endPos);
654 }
655 }
656
657 void KItemListController::slotRubberBandChanged()
658 {
659 if (!m_view || !m_model || m_model->count() <= 0) {
660 return;
661 }
662
663 const KItemListRubberBand* rubberBand = m_view->rubberBand();
664 const QPointF startPos = rubberBand->startPosition();
665 const QPointF endPos = rubberBand->endPosition();
666 QRectF rubberBandRect = QRectF(startPos, endPos).normalized();
667
668 const bool scrollVertical = (m_view->scrollOrientation() == Qt::Vertical);
669 if (scrollVertical) {
670 rubberBandRect.translate(0, -m_view->offset());
671 } else {
672 rubberBandRect.translate(-m_view->offset(), 0);
673 }
674
675 if (!m_oldSelection.isEmpty()) {
676 // Clear the old selection that was available before the rubberband has
677 // been activated in case if no Shift- or Control-key are pressed
678 const bool shiftOrControlPressed = QApplication::keyboardModifiers() & Qt::ShiftModifier ||
679 QApplication::keyboardModifiers() & Qt::ControlModifier;
680 if (!shiftOrControlPressed) {
681 m_oldSelection.clear();
682 }
683 }
684
685 QSet<int> selectedItems;
686
687 // Select all visible items that intersect with the rubberband
688 foreach (const KItemListWidget* widget, m_view->visibleItemListWidgets()) {
689 const int index = widget->index();
690
691 const QRectF widgetRect = m_view->itemBoundingRect(index);
692 if (widgetRect.intersects(rubberBandRect)) {
693 const QRectF iconRect = widget->iconBoundingRect().translated(widgetRect.topLeft());
694 const QRectF textRect = widget->textBoundingRect().translated(widgetRect.topLeft());
695 if (iconRect.intersects(rubberBandRect) || textRect.intersects(rubberBandRect)) {
696 selectedItems.insert(index);
697 }
698 }
699 }
700
701 // Select all invisible items that intersect with the rubberband. Instead of
702 // iterating all items only the area which might be touched by the rubberband
703 // will be checked.
704 const bool increaseIndex = scrollVertical ?
705 startPos.y() > endPos.y(): startPos.x() > endPos.x();
706
707 int index = increaseIndex ? m_view->lastVisibleIndex() + 1 : m_view->firstVisibleIndex() - 1;
708 bool selectionFinished = false;
709 do {
710 const QRectF widgetRect = m_view->itemBoundingRect(index);
711 if (widgetRect.intersects(rubberBandRect)) {
712 selectedItems.insert(index);
713 }
714
715 if (increaseIndex) {
716 ++index;
717 selectionFinished = (index >= m_model->count()) ||
718 ( scrollVertical && widgetRect.top() > rubberBandRect.bottom()) ||
719 (!scrollVertical && widgetRect.left() > rubberBandRect.right());
720 } else {
721 --index;
722 selectionFinished = (index < 0) ||
723 ( scrollVertical && widgetRect.bottom() < rubberBandRect.top()) ||
724 (!scrollVertical && widgetRect.right() < rubberBandRect.left());
725 }
726 } while (!selectionFinished);
727
728 if (QApplication::keyboardModifiers() & Qt::ControlModifier) {
729 // If Control is pressed, the selection state of all items in the rubberband is toggled.
730 // Therefore, the new selection contains:
731 // 1. All previously selected items which are not inside the rubberband, and
732 // 2. all items inside the rubberband which have not been selected previously.
733 m_selectionManager->setSelectedItems((m_oldSelection - selectedItems) + (selectedItems - m_oldSelection));
734 }
735 else {
736 m_selectionManager->setSelectedItems(selectedItems + m_oldSelection);
737 }
738 }
739
740 bool KItemListController::startDragging()
741 {
742 if (!m_view || !m_model) {
743 return false;
744 }
745
746 const QSet<int> selectedItems = m_selectionManager->selectedItems();
747 QMimeData* data = m_model->createMimeData(selectedItems);
748 if (!data) {
749 return false;
750 }
751
752 // The created drag object will be owned and deleted
753 // by QApplication::activeWindow().
754 QDrag* drag = new QDrag(QApplication::activeWindow());
755 drag->setMimeData(data);
756
757 const QPixmap pixmap = m_view->createDragPixmap(selectedItems);
758 drag->setPixmap(pixmap);
759
760 drag->exec(Qt::MoveAction | Qt::CopyAction | Qt::LinkAction, Qt::IgnoreAction);
761 return true;
762 }
763
764 #include "kitemlistcontroller.moc"