]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistcontroller.cpp
Renamed some signals for consistency with KItemViews classes
[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 (!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);
314 }
315 break;
316
317 default:
318 Q_ASSERT(false);
319 break;
320 }
321
322 return true;
323 } else {
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.
331 startPos.setX(0);
332 }
333 } else {
334 startPos.rx() += m_view->offset();
335 }
336
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);
343 }
344
345 return false;
346 }
347
348 bool KItemListController::mouseMoveEvent(QGraphicsSceneMouseEvent* event, const QTransform& transform)
349 {
350 if (!m_view) {
351 return false;
352 }
353
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()) {
362 m_dragging = true;
363 }
364 }
365 } else {
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());
375 }
376 } else {
377 endPos.rx() += m_view->offset();
378 }
379 rubberBand->setEndPosition(endPos);
380 }
381 }
382
383 return false;
384 }
385
386 bool KItemListController::mouseReleaseEvent(QGraphicsSceneMouseEvent* event, const QTransform& transform)
387 {
388 if (!m_view) {
389 return false;
390 }
391
392 const bool shiftOrControlPressed = event->modifiers() & Qt::ShiftModifier ||
393 event->modifiers() & Qt::ControlModifier;
394
395 bool clearSelection = !shiftOrControlPressed && !m_dragging && !(event->button() == Qt::RightButton);
396
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);
403
404 if (rubberBand->startPosition() != rubberBand->endPosition()) {
405 clearSelection = false;
406 }
407 }
408
409 const QPointF pos = transform.map(event->pos());
410 const int index = m_view->itemAt(pos);
411
412 if (index >= 0 && index == m_pressedIndex) {
413 // The release event is done above the same item as the press event
414
415 if (clearSelection) {
416 // Clear the previous selection but reselect the current index
417 m_selectionManager->setSelectedItems(QSet<int>() << index);
418 }
419
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;
430 }
431 if (emitItemActivated) {
432 emit itemActivated(index);
433 }
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()));
438 }
439 } else if (clearSelection) {
440 m_selectionManager->clearSelection();
441 }
442
443 m_dragging = false;
444 m_pressedMousePos = QPointF();
445 m_pressedIndex = -1;
446 return false;
447 }
448
449 bool KItemListController::mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event, const QTransform& transform)
450 {
451 const QPointF pos = transform.map(event->pos());
452 const int index = m_view->itemAt(pos);
453
454 bool emitItemActivated = !KGlobalSettings::singleClick() &&
455 (event->button() & Qt::LeftButton) &&
456 index >= 0 && index < m_model->count();
457 if (emitItemActivated) {
458 emit itemActivated(index);
459 }
460 return false;
461 }
462
463 bool KItemListController::dragEnterEvent(QGraphicsSceneDragDropEvent* event, const QTransform& transform)
464 {
465 Q_UNUSED(event);
466 Q_UNUSED(transform);
467 return false;
468 }
469
470 bool KItemListController::dragLeaveEvent(QGraphicsSceneDragDropEvent* event, const QTransform& transform)
471 {
472 Q_UNUSED(event);
473 Q_UNUSED(transform);
474 return false;
475 }
476
477 bool KItemListController::dragMoveEvent(QGraphicsSceneDragDropEvent* event, const QTransform& transform)
478 {
479 Q_UNUSED(event);
480 Q_UNUSED(transform);
481 return false;
482 }
483
484 bool KItemListController::dropEvent(QGraphicsSceneDragDropEvent* event, const QTransform& transform)
485 {
486 Q_UNUSED(event);
487 Q_UNUSED(transform);
488
489 m_dragging = false;
490 return false;
491 }
492
493 bool KItemListController::hoverEnterEvent(QGraphicsSceneHoverEvent* event, const QTransform& transform)
494 {
495 Q_UNUSED(event);
496 Q_UNUSED(transform);
497 return false;
498 }
499
500 bool KItemListController::hoverMoveEvent(QGraphicsSceneHoverEvent* event, const QTransform& transform)
501 {
502 // The implementation assumes that only one item can get hovered no matter
503 // whether they overlap or not.
504
505 Q_UNUSED(transform);
506 if (!m_model || !m_view) {
507 return false;
508 }
509
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;
515 break;
516 }
517 }
518
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());
523
524 const bool hovered = widget->contains(mappedPos) &&
525 !widget->expansionToggleRect().contains(mappedPos) &&
526 !widget->selectionToggleRect().contains(mappedPos);
527 if (hovered) {
528 hoveredWidget = widget;
529 break;
530 }
531 }
532
533 if (unhoveredWidget != hoveredWidget) {
534 if (unhoveredWidget) {
535 unhoveredWidget->setHovered(false);
536 emit itemUnhovered(unhoveredWidget->index());
537 }
538
539 if (hoveredWidget) {
540 hoveredWidget->setHovered(true);
541 emit itemHovered(hoveredWidget->index());
542 }
543 }
544
545 return false;
546 }
547
548 bool KItemListController::hoverLeaveEvent(QGraphicsSceneHoverEvent* event, const QTransform& transform)
549 {
550 Q_UNUSED(event);
551 Q_UNUSED(transform);
552
553 if (!m_model || !m_view) {
554 return false;
555 }
556
557 foreach (KItemListWidget* widget, m_view->visibleItemListWidgets()) {
558 if (widget->isHovered()) {
559 widget->setHovered(false);
560 emit itemUnhovered(widget->index());
561 }
562 }
563 return false;
564 }
565
566 bool KItemListController::wheelEvent(QGraphicsSceneWheelEvent* event, const QTransform& transform)
567 {
568 Q_UNUSED(event);
569 Q_UNUSED(transform);
570 return false;
571 }
572
573 bool KItemListController::resizeEvent(QGraphicsSceneResizeEvent* event, const QTransform& transform)
574 {
575 Q_UNUSED(event);
576 Q_UNUSED(transform);
577 return false;
578 }
579
580 bool KItemListController::processEvent(QEvent* event, const QTransform& transform)
581 {
582 if (!event) {
583 return false;
584 }
585
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);
617 default:
618 break;
619 }
620
621 return false;
622 }
623
624 void KItemListController::slotViewOffsetChanged(qreal current, qreal previous)
625 {
626 if (!m_view) {
627 return;
628 }
629
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) {
639 endPos.ry() += diff;
640 } else {
641 endPos.rx() += diff;
642 }
643
644 rubberBand->setEndPosition(endPos);
645 }
646 }
647
648 void KItemListController::slotRubberBandChanged()
649 {
650 if (!m_view || !m_model || m_model->count() <= 0) {
651 return;
652 }
653
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();
658
659 const bool scrollVertical = (m_view->scrollOrientation() == Qt::Vertical);
660 if (scrollVertical) {
661 rubberBandRect.translate(0, -m_view->offset());
662 } else {
663 rubberBandRect.translate(-m_view->offset(), 0);
664 }
665
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();
673 }
674 }
675
676 QSet<int> selectedItems;
677
678 // Select all visible items that intersect with the rubberband
679 foreach (const KItemListWidget* widget, m_view->visibleItemListWidgets()) {
680 const int index = widget->index();
681
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);
688 }
689 }
690 }
691
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
694 // will be checked.
695 const bool increaseIndex = scrollVertical ?
696 startPos.y() > endPos.y(): startPos.x() > endPos.x();
697
698 int index = increaseIndex ? m_view->lastVisibleIndex() + 1 : m_view->firstVisibleIndex() - 1;
699 bool selectionFinished = false;
700 do {
701 const QRectF widgetRect = m_view->itemBoundingRect(index);
702 if (widgetRect.intersects(rubberBandRect)) {
703 selectedItems.insert(index);
704 }
705
706 if (increaseIndex) {
707 ++index;
708 selectionFinished = (index >= m_model->count()) ||
709 ( scrollVertical && widgetRect.top() > rubberBandRect.bottom()) ||
710 (!scrollVertical && widgetRect.left() > rubberBandRect.right());
711 } else {
712 --index;
713 selectionFinished = (index < 0) ||
714 ( scrollVertical && widgetRect.bottom() < rubberBandRect.top()) ||
715 (!scrollVertical && widgetRect.right() < rubberBandRect.left());
716 }
717 } while (!selectionFinished);
718
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));
725 }
726 else {
727 m_selectionManager->setSelectedItems(selectedItems + m_oldSelection);
728 }
729 }
730
731 bool KItemListController::startDragging()
732 {
733 if (!m_view || !m_model) {
734 return false;
735 }
736
737 const QSet<int> selectedItems = m_selectionManager->selectedItems();
738 QMimeData* data = m_model->createMimeData(selectedItems);
739 if (!data) {
740 return false;
741 }
742
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);
747
748 const QPixmap pixmap = m_view->createDragPixmap(selectedItems);
749 drag->setPixmap(pixmap);
750
751 drag->exec(Qt::MoveAction | Qt::CopyAction | Qt::LinkAction, Qt::IgnoreAction);
752 return true;
753 }
754
755 #include "kitemlistcontroller.moc"