]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistcontroller.cpp
Merge branch 'release/20.08' into master
[dolphin.git] / src / kitemviews / kitemlistcontroller.cpp
1 /*
2 * SPDX-FileCopyrightText: 2011 Peter Penz <peter.penz19@gmail.com>
3 * SPDX-FileCopyrightText: 2012 Frank Reininghaus <frank78ac@googlemail.com>
4 *
5 * Based on the Itemviews NG project from Trolltech Labs
6 *
7 * SPDX-License-Identifier: GPL-2.0-or-later
8 */
9
10 #include "kitemlistcontroller.h"
11
12 #include "kitemlistselectionmanager.h"
13 #include "kitemlistview.h"
14 #include "private/kitemlistkeyboardsearchmanager.h"
15 #include "private/kitemlistrubberband.h"
16 #include "views/draganddrophelper.h"
17
18 #include <QAccessible>
19 #include <QApplication>
20 #include <QDrag>
21 #include <QGraphicsScene>
22 #include <QGraphicsSceneEvent>
23 #include <QGraphicsView>
24 #include <QMimeData>
25 #include <QTimer>
26
27 KItemListController::KItemListController(KItemModelBase* model, KItemListView* view, QObject* parent) :
28 QObject(parent),
29 m_singleClickActivationEnforced(false),
30 m_selectionTogglePressed(false),
31 m_clearSelectionIfItemsAreNotDragged(false),
32 m_selectionBehavior(NoSelection),
33 m_autoActivationBehavior(ActivationAndExpansion),
34 m_mouseDoubleClickAction(ActivateItemOnly),
35 m_model(nullptr),
36 m_view(nullptr),
37 m_selectionManager(new KItemListSelectionManager(this)),
38 m_keyboardManager(new KItemListKeyboardSearchManager(this)),
39 m_pressedIndex(-1),
40 m_pressedMousePos(),
41 m_autoActivationTimer(nullptr),
42 m_oldSelection(),
43 m_keyboardAnchorIndex(-1),
44 m_keyboardAnchorPos(0)
45 {
46 connect(m_keyboardManager, &KItemListKeyboardSearchManager::changeCurrentItem,
47 this, &KItemListController::slotChangeCurrentItem);
48 connect(m_selectionManager, &KItemListSelectionManager::currentChanged,
49 m_keyboardManager, &KItemListKeyboardSearchManager::slotCurrentChanged);
50 connect(m_selectionManager, &KItemListSelectionManager::selectionChanged,
51 m_keyboardManager, &KItemListKeyboardSearchManager::slotSelectionChanged);
52
53 m_autoActivationTimer = new QTimer(this);
54 m_autoActivationTimer->setSingleShot(true);
55 m_autoActivationTimer->setInterval(-1);
56 connect(m_autoActivationTimer, &QTimer::timeout, this, &KItemListController::slotAutoActivationTimeout);
57
58 setModel(model);
59 setView(view);
60 }
61
62 KItemListController::~KItemListController()
63 {
64 setView(nullptr);
65 Q_ASSERT(!m_view);
66
67 setModel(nullptr);
68 Q_ASSERT(!m_model);
69 }
70
71 void KItemListController::setModel(KItemModelBase* model)
72 {
73 if (m_model == model) {
74 return;
75 }
76
77 KItemModelBase* oldModel = m_model;
78 if (oldModel) {
79 oldModel->deleteLater();
80 }
81
82 m_model = model;
83 if (m_model) {
84 m_model->setParent(this);
85 }
86
87 if (m_view) {
88 m_view->setModel(m_model);
89 }
90
91 m_selectionManager->setModel(m_model);
92
93 emit modelChanged(m_model, oldModel);
94 }
95
96 KItemModelBase* KItemListController::model() const
97 {
98 return m_model;
99 }
100
101 KItemListSelectionManager* KItemListController::selectionManager() const
102 {
103 return m_selectionManager;
104 }
105
106 void KItemListController::setView(KItemListView* view)
107 {
108 if (m_view == view) {
109 return;
110 }
111
112 KItemListView* oldView = m_view;
113 if (oldView) {
114 disconnect(oldView, &KItemListView::scrollOffsetChanged, this, &KItemListController::slotViewScrollOffsetChanged);
115 oldView->deleteLater();
116 }
117
118 m_view = view;
119
120 if (m_view) {
121 m_view->setParent(this);
122 m_view->setController(this);
123 m_view->setModel(m_model);
124 connect(m_view, &KItemListView::scrollOffsetChanged, this, &KItemListController::slotViewScrollOffsetChanged);
125 updateExtendedSelectionRegion();
126 }
127
128 emit viewChanged(m_view, oldView);
129 }
130
131 KItemListView* KItemListController::view() const
132 {
133 return m_view;
134 }
135
136 void KItemListController::setSelectionBehavior(SelectionBehavior behavior)
137 {
138 m_selectionBehavior = behavior;
139 updateExtendedSelectionRegion();
140 }
141
142 KItemListController::SelectionBehavior KItemListController::selectionBehavior() const
143 {
144 return m_selectionBehavior;
145 }
146
147 void KItemListController::setAutoActivationBehavior(AutoActivationBehavior behavior)
148 {
149 m_autoActivationBehavior = behavior;
150 }
151
152 KItemListController::AutoActivationBehavior KItemListController::autoActivationBehavior() const
153 {
154 return m_autoActivationBehavior;
155 }
156
157 void KItemListController::setMouseDoubleClickAction(MouseDoubleClickAction action)
158 {
159 m_mouseDoubleClickAction = action;
160 }
161
162 KItemListController::MouseDoubleClickAction KItemListController::mouseDoubleClickAction() const
163 {
164 return m_mouseDoubleClickAction;
165 }
166
167 int KItemListController::indexCloseToMousePressedPosition() const
168 {
169 QHashIterator<KItemListWidget*, KItemListGroupHeader*> it(m_view->m_visibleGroups);
170 while (it.hasNext()) {
171 it.next();
172 KItemListGroupHeader *groupHeader = it.value();
173 const QPointF mappedToGroup = groupHeader->mapFromItem(nullptr, m_pressedMousePos);
174 if (groupHeader->contains(mappedToGroup)) {
175 return it.key()->index();
176 }
177 }
178 return -1;
179 }
180
181 void KItemListController::setAutoActivationDelay(int delay)
182 {
183 m_autoActivationTimer->setInterval(delay);
184 }
185
186 int KItemListController::autoActivationDelay() const
187 {
188 return m_autoActivationTimer->interval();
189 }
190
191 void KItemListController::setSingleClickActivationEnforced(bool singleClick)
192 {
193 m_singleClickActivationEnforced = singleClick;
194 }
195
196 bool KItemListController::singleClickActivationEnforced() const
197 {
198 return m_singleClickActivationEnforced;
199 }
200
201 bool KItemListController::keyPressEvent(QKeyEvent* event)
202 {
203 int index = m_selectionManager->currentItem();
204 int key = event->key();
205
206 // Handle the expanding/collapsing of items
207 if (m_view->supportsItemExpanding() && m_model->isExpandable(index)) {
208 if (key == Qt::Key_Right) {
209 if (m_model->setExpanded(index, true)) {
210 return true;
211 }
212 } else if (key == Qt::Key_Left) {
213 if (m_model->setExpanded(index, false)) {
214 return true;
215 }
216 }
217 }
218
219 const bool shiftPressed = event->modifiers() & Qt::ShiftModifier;
220 const bool controlPressed = event->modifiers() & Qt::ControlModifier;
221 const bool shiftOrControlPressed = shiftPressed || controlPressed;
222 const bool navigationPressed = key == Qt::Key_Home || key == Qt::Key_End ||
223 key == Qt::Key_PageUp || key == Qt::Key_PageDown ||
224 key == Qt::Key_Up || key == Qt::Key_Down ||
225 key == Qt::Key_Left || key == Qt::Key_Right;
226
227 const int itemCount = m_model->count();
228
229 // For horizontal scroll orientation, transform
230 // the arrow keys to simplify the event handling.
231 if (m_view->scrollOrientation() == Qt::Horizontal) {
232 switch (key) {
233 case Qt::Key_Up: key = Qt::Key_Left; break;
234 case Qt::Key_Down: key = Qt::Key_Right; break;
235 case Qt::Key_Left: key = Qt::Key_Up; break;
236 case Qt::Key_Right: key = Qt::Key_Down; break;
237 default: break;
238 }
239 }
240
241 const bool selectSingleItem = m_selectionBehavior != NoSelection && itemCount == 1 && navigationPressed;
242
243 if (selectSingleItem) {
244 const int current = m_selectionManager->currentItem();
245 m_selectionManager->setSelected(current);
246 return true;
247 }
248
249 switch (key) {
250 case Qt::Key_Home:
251 index = 0;
252 m_keyboardAnchorIndex = index;
253 m_keyboardAnchorPos = keyboardAnchorPos(index);
254 break;
255
256 case Qt::Key_End:
257 index = itemCount - 1;
258 m_keyboardAnchorIndex = index;
259 m_keyboardAnchorPos = keyboardAnchorPos(index);
260 break;
261
262 case Qt::Key_Left:
263 if (index > 0) {
264 const int expandedParentsCount = m_model->expandedParentsCount(index);
265 if (expandedParentsCount == 0) {
266 --index;
267 } else {
268 // Go to the parent of the current item.
269 do {
270 --index;
271 } while (index > 0 && m_model->expandedParentsCount(index) == expandedParentsCount);
272 }
273 m_keyboardAnchorIndex = index;
274 m_keyboardAnchorPos = keyboardAnchorPos(index);
275 }
276 break;
277
278 case Qt::Key_Right:
279 if (index < itemCount - 1) {
280 ++index;
281 m_keyboardAnchorIndex = index;
282 m_keyboardAnchorPos = keyboardAnchorPos(index);
283 }
284 break;
285
286 case Qt::Key_Up:
287 updateKeyboardAnchor();
288 index = previousRowIndex(index);
289 break;
290
291 case Qt::Key_Down:
292 updateKeyboardAnchor();
293 index = nextRowIndex(index);
294 break;
295
296 case Qt::Key_PageUp:
297 if (m_view->scrollOrientation() == Qt::Horizontal) {
298 // The new current index should correspond to the first item in the current column.
299 int newIndex = qMax(index - 1, 0);
300 while (newIndex != index && m_view->itemRect(newIndex).topLeft().y() < m_view->itemRect(index).topLeft().y()) {
301 index = newIndex;
302 newIndex = qMax(index - 1, 0);
303 }
304 m_keyboardAnchorIndex = index;
305 m_keyboardAnchorPos = keyboardAnchorPos(index);
306 } else {
307 const qreal currentItemBottom = m_view->itemRect(index).bottomLeft().y();
308 const qreal height = m_view->geometry().height();
309
310 // The new current item should be the first item in the current
311 // column whose itemRect's top coordinate is larger than targetY.
312 const qreal targetY = currentItemBottom - height;
313
314 updateKeyboardAnchor();
315 int newIndex = previousRowIndex(index);
316 do {
317 index = newIndex;
318 updateKeyboardAnchor();
319 newIndex = previousRowIndex(index);
320 } while (m_view->itemRect(newIndex).topLeft().y() > targetY && newIndex != index);
321 }
322 break;
323
324 case Qt::Key_PageDown:
325 if (m_view->scrollOrientation() == Qt::Horizontal) {
326 // The new current index should correspond to the last item in the current column.
327 int newIndex = qMin(index + 1, m_model->count() - 1);
328 while (newIndex != index && m_view->itemRect(newIndex).topLeft().y() > m_view->itemRect(index).topLeft().y()) {
329 index = newIndex;
330 newIndex = qMin(index + 1, m_model->count() - 1);
331 }
332 m_keyboardAnchorIndex = index;
333 m_keyboardAnchorPos = keyboardAnchorPos(index);
334 } else {
335 const qreal currentItemTop = m_view->itemRect(index).topLeft().y();
336 const qreal height = m_view->geometry().height();
337
338 // The new current item should be the last item in the current
339 // column whose itemRect's bottom coordinate is smaller than targetY.
340 const qreal targetY = currentItemTop + height;
341
342 updateKeyboardAnchor();
343 int newIndex = nextRowIndex(index);
344 do {
345 index = newIndex;
346 updateKeyboardAnchor();
347 newIndex = nextRowIndex(index);
348 } while (m_view->itemRect(newIndex).bottomLeft().y() < targetY && newIndex != index);
349 }
350 break;
351
352 case Qt::Key_Enter:
353 case Qt::Key_Return: {
354 const KItemSet selectedItems = m_selectionManager->selectedItems();
355 if (selectedItems.count() >= 2) {
356 emit itemsActivated(selectedItems);
357 } else if (selectedItems.count() == 1) {
358 emit itemActivated(selectedItems.first());
359 } else {
360 emit itemActivated(index);
361 }
362 break;
363 }
364
365 case Qt::Key_Menu: {
366 // Emit the signal itemContextMenuRequested() in case if at least one
367 // item is selected. Otherwise the signal viewContextMenuRequested() will be emitted.
368 const KItemSet selectedItems = m_selectionManager->selectedItems();
369 int index = -1;
370 if (selectedItems.count() >= 2) {
371 const int currentItemIndex = m_selectionManager->currentItem();
372 index = selectedItems.contains(currentItemIndex)
373 ? currentItemIndex : selectedItems.first();
374 } else if (selectedItems.count() == 1) {
375 index = selectedItems.first();
376 }
377
378 if (index >= 0) {
379 const QRectF contextRect = m_view->itemContextRect(index);
380 const QPointF pos(m_view->scene()->views().first()->mapToGlobal(contextRect.bottomRight().toPoint()));
381 emit itemContextMenuRequested(index, pos);
382 } else {
383 emit viewContextMenuRequested(QCursor::pos());
384 }
385 break;
386 }
387
388 case Qt::Key_Escape:
389 if (m_selectionBehavior != SingleSelection) {
390 m_selectionManager->clearSelection();
391 }
392 m_keyboardManager->cancelSearch();
393 emit escapePressed();
394 break;
395
396 case Qt::Key_Space:
397 if (m_selectionBehavior == MultiSelection) {
398 if (controlPressed) {
399 // Toggle the selection state of the current item.
400 m_selectionManager->endAnchoredSelection();
401 m_selectionManager->setSelected(index, 1, KItemListSelectionManager::Toggle);
402 m_selectionManager->beginAnchoredSelection(index);
403 break;
404 } else {
405 // Select the current item if it is not selected yet.
406 const int current = m_selectionManager->currentItem();
407 if (!m_selectionManager->isSelected(current)) {
408 m_selectionManager->setSelected(current);
409 break;
410 }
411 }
412 }
413 Q_FALLTHROUGH(); // fall through to the default case and add the Space to the current search string.
414 default:
415 m_keyboardManager->addKeys(event->text());
416 // Make sure unconsumed events get propagated up the chain. #302329
417 event->ignore();
418 return false;
419 }
420
421 if (m_selectionManager->currentItem() != index) {
422 switch (m_selectionBehavior) {
423 case NoSelection:
424 m_selectionManager->setCurrentItem(index);
425 break;
426
427 case SingleSelection:
428 m_selectionManager->setCurrentItem(index);
429 m_selectionManager->clearSelection();
430 m_selectionManager->setSelected(index, 1);
431 break;
432
433 case MultiSelection:
434 if (controlPressed) {
435 m_selectionManager->endAnchoredSelection();
436 }
437
438 m_selectionManager->setCurrentItem(index);
439
440 if (!shiftOrControlPressed) {
441 m_selectionManager->clearSelection();
442 m_selectionManager->setSelected(index, 1);
443 }
444
445 if (!shiftPressed) {
446 m_selectionManager->beginAnchoredSelection(index);
447 }
448 break;
449 }
450 }
451
452 if (navigationPressed) {
453 m_view->scrollToItem(index);
454 }
455 return true;
456 }
457
458 void KItemListController::slotChangeCurrentItem(const QString& text, bool searchFromNextItem)
459 {
460 if (!m_model || m_model->count() == 0) {
461 return;
462 }
463 int index;
464 if (searchFromNextItem) {
465 const int currentIndex = m_selectionManager->currentItem();
466 index = m_model->indexForKeyboardSearch(text, (currentIndex + 1) % m_model->count());
467 } else {
468 index = m_model->indexForKeyboardSearch(text, 0);
469 }
470 if (index >= 0) {
471 m_selectionManager->setCurrentItem(index);
472
473 if (m_selectionBehavior != NoSelection) {
474 m_selectionManager->replaceSelection(index);
475 m_selectionManager->beginAnchoredSelection(index);
476 }
477
478 m_view->scrollToItem(index);
479 }
480 }
481
482 void KItemListController::slotAutoActivationTimeout()
483 {
484 if (!m_model || !m_view) {
485 return;
486 }
487
488 const int index = m_autoActivationTimer->property("index").toInt();
489 if (index < 0 || index >= m_model->count()) {
490 return;
491 }
492
493 /* m_view->isUnderMouse() fixes a bug in the Folder-View-Panel and in the
494 * Places-Panel.
495 *
496 * Bug: When you drag a file onto a Folder-View-Item or a Places-Item and
497 * then move away before the auto-activation timeout triggers, than the
498 * item still becomes activated/expanded.
499 *
500 * See Bug 293200 and 305783
501 */
502 if (m_model->supportsDropping(index) && m_view->isUnderMouse()) {
503 if (m_view->supportsItemExpanding() && m_model->isExpandable(index)) {
504 const bool expanded = m_model->isExpanded(index);
505 m_model->setExpanded(index, !expanded);
506 } else if (m_autoActivationBehavior != ExpansionOnly) {
507 emit itemActivated(index);
508 }
509 }
510 }
511
512 bool KItemListController::inputMethodEvent(QInputMethodEvent* event)
513 {
514 Q_UNUSED(event)
515 return false;
516 }
517
518 bool KItemListController::mousePressEvent(QGraphicsSceneMouseEvent* event, const QTransform& transform)
519 {
520 if (!m_view) {
521 return false;
522 }
523
524 m_pressedMousePos = transform.map(event->pos());
525 m_pressedIndex = m_view->itemAt(m_pressedMousePos);
526 emit mouseButtonPressed(m_pressedIndex, event->buttons());
527
528 if (event->buttons() & (Qt::BackButton | Qt::ForwardButton)) {
529 // Do not select items when clicking the back/forward buttons, see
530 // https://bugs.kde.org/show_bug.cgi?id=327412.
531 return true;
532 }
533
534 if (m_view->isAboveExpansionToggle(m_pressedIndex, m_pressedMousePos)) {
535 m_selectionManager->endAnchoredSelection();
536 m_selectionManager->setCurrentItem(m_pressedIndex);
537 m_selectionManager->beginAnchoredSelection(m_pressedIndex);
538 return true;
539 }
540
541 m_selectionTogglePressed = m_view->isAboveSelectionToggle(m_pressedIndex, m_pressedMousePos);
542 if (m_selectionTogglePressed) {
543 m_selectionManager->setSelected(m_pressedIndex, 1, KItemListSelectionManager::Toggle);
544 // The previous anchored selection has been finished already in
545 // KItemListSelectionManager::setSelected(). We can safely change
546 // the current item and start a new anchored selection now.
547 m_selectionManager->setCurrentItem(m_pressedIndex);
548 m_selectionManager->beginAnchoredSelection(m_pressedIndex);
549 return true;
550 }
551
552 const bool shiftPressed = event->modifiers() & Qt::ShiftModifier;
553 const bool controlPressed = event->modifiers() & Qt::ControlModifier;
554
555 // The previous selection is cleared if either
556 // 1. The selection mode is SingleSelection, or
557 // 2. the selection mode is MultiSelection, and *none* of the following conditions are met:
558 // a) Shift or Control are pressed.
559 // b) The clicked item is selected already. In that case, the user might want to:
560 // - start dragging multiple items, or
561 // - open the context menu and perform an action for all selected items.
562 const bool shiftOrControlPressed = shiftPressed || controlPressed;
563 const bool pressedItemAlreadySelected = m_pressedIndex >= 0 && m_selectionManager->isSelected(m_pressedIndex);
564 const bool clearSelection = m_selectionBehavior == SingleSelection ||
565 (!shiftOrControlPressed && !pressedItemAlreadySelected);
566 if (clearSelection) {
567 m_selectionManager->clearSelection();
568 } else if (pressedItemAlreadySelected && !shiftOrControlPressed && (event->buttons() & Qt::LeftButton)) {
569 // The user might want to start dragging multiple items, but if he clicks the item
570 // in order to trigger it instead, the other selected items must be deselected.
571 // However, we do not know yet what the user is going to do.
572 // -> remember that the user pressed an item which had been selected already and
573 // clear the selection in mouseReleaseEvent(), unless the items are dragged.
574 m_clearSelectionIfItemsAreNotDragged = true;
575
576 if (m_selectionManager->selectedItems().count() == 1 && m_view->isAboveText(m_pressedIndex, m_pressedMousePos)) {
577 emit selectedItemTextPressed(m_pressedIndex);
578 }
579 }
580
581 if (!shiftPressed) {
582 // Finish the anchored selection before the current index is changed
583 m_selectionManager->endAnchoredSelection();
584 }
585
586 if (event->buttons() & Qt::RightButton) {
587 // Stop rubber band from persisting after right-clicks
588 KItemListRubberBand* rubberBand = m_view->rubberBand();
589 if (rubberBand->isActive()) {
590 disconnect(rubberBand, &KItemListRubberBand::endPositionChanged, this, &KItemListController::slotRubberBandChanged);
591 rubberBand->setActive(false);
592 m_view->setAutoScroll(false);
593 }
594 }
595
596 if (m_pressedIndex >= 0) {
597 m_selectionManager->setCurrentItem(m_pressedIndex);
598
599 switch (m_selectionBehavior) {
600 case NoSelection:
601 break;
602
603 case SingleSelection:
604 m_selectionManager->setSelected(m_pressedIndex);
605 break;
606
607 case MultiSelection:
608 if (controlPressed && !shiftPressed) {
609 m_selectionManager->setSelected(m_pressedIndex, 1, KItemListSelectionManager::Toggle);
610 m_selectionManager->beginAnchoredSelection(m_pressedIndex);
611 } else if (!shiftPressed || !m_selectionManager->isAnchoredSelectionActive()) {
612 // Select the pressed item and start a new anchored selection
613 m_selectionManager->setSelected(m_pressedIndex, 1, KItemListSelectionManager::Select);
614 m_selectionManager->beginAnchoredSelection(m_pressedIndex);
615 }
616 break;
617
618 default:
619 Q_ASSERT(false);
620 break;
621 }
622
623 if (event->buttons() & Qt::RightButton) {
624 emit itemContextMenuRequested(m_pressedIndex, event->screenPos());
625 }
626
627 return true;
628 }
629
630 if (event->buttons() & Qt::RightButton) {
631 const QRectF headerBounds = m_view->headerBoundaries();
632 if (headerBounds.contains(event->pos())) {
633 emit headerContextMenuRequested(event->screenPos());
634 } else {
635 emit viewContextMenuRequested(event->screenPos());
636 }
637 return true;
638 }
639
640 if (m_selectionBehavior == MultiSelection) {
641 QPointF startPos = m_pressedMousePos;
642 if (m_view->scrollOrientation() == Qt::Vertical) {
643 startPos.ry() += m_view->scrollOffset();
644 if (m_view->itemSize().width() < 0) {
645 // Use a special rubberband for views that have only one column and
646 // expand the rubberband to use the whole width of the view.
647 startPos.setX(0);
648 }
649 } else {
650 startPos.rx() += m_view->scrollOffset();
651 }
652
653 m_oldSelection = m_selectionManager->selectedItems();
654 KItemListRubberBand* rubberBand = m_view->rubberBand();
655 rubberBand->setStartPosition(startPos);
656 rubberBand->setEndPosition(startPos);
657 rubberBand->setActive(true);
658 connect(rubberBand, &KItemListRubberBand::endPositionChanged, this, &KItemListController::slotRubberBandChanged);
659 m_view->setAutoScroll(true);
660 }
661
662 return false;
663 }
664
665 bool KItemListController::mouseMoveEvent(QGraphicsSceneMouseEvent* event, const QTransform& transform)
666 {
667 if (!m_view) {
668 return false;
669 }
670
671 if (m_pressedIndex >= 0) {
672 // Check whether a dragging should be started
673 if (event->buttons() & Qt::LeftButton) {
674 const QPointF pos = transform.map(event->pos());
675 if ((pos - m_pressedMousePos).manhattanLength() >= QApplication::startDragDistance()) {
676 if (!m_selectionManager->isSelected(m_pressedIndex)) {
677 // Always assure that the dragged item gets selected. Usually this is already
678 // done on the mouse-press event, but when using the selection-toggle on a
679 // selected item the dragged item is not selected yet.
680 m_selectionManager->setSelected(m_pressedIndex, 1, KItemListSelectionManager::Toggle);
681 } else {
682 // A selected item has been clicked to drag all selected items
683 // -> the selection should not be cleared when the mouse button is released.
684 m_clearSelectionIfItemsAreNotDragged = false;
685 }
686
687 startDragging();
688 }
689 }
690 } else {
691 KItemListRubberBand* rubberBand = m_view->rubberBand();
692 if (rubberBand->isActive()) {
693 QPointF endPos = transform.map(event->pos());
694
695 // Update the current item.
696 const int newCurrent = m_view->itemAt(endPos);
697 if (newCurrent >= 0) {
698 // It's expected that the new current index is also the new anchor (bug 163451).
699 m_selectionManager->endAnchoredSelection();
700 m_selectionManager->setCurrentItem(newCurrent);
701 m_selectionManager->beginAnchoredSelection(newCurrent);
702 }
703
704 if (m_view->scrollOrientation() == Qt::Vertical) {
705 endPos.ry() += m_view->scrollOffset();
706 if (m_view->itemSize().width() < 0) {
707 // Use a special rubberband for views that have only one column and
708 // expand the rubberband to use the whole width of the view.
709 endPos.setX(m_view->size().width());
710 }
711 } else {
712 endPos.rx() += m_view->scrollOffset();
713 }
714 rubberBand->setEndPosition(endPos);
715 }
716 }
717
718 return false;
719 }
720
721 bool KItemListController::mouseReleaseEvent(QGraphicsSceneMouseEvent* event, const QTransform& transform)
722 {
723 if (!m_view) {
724 return false;
725 }
726
727 emit mouseButtonReleased(m_pressedIndex, event->buttons());
728
729 const bool isAboveSelectionToggle = m_view->isAboveSelectionToggle(m_pressedIndex, m_pressedMousePos);
730 if (isAboveSelectionToggle) {
731 m_selectionTogglePressed = false;
732 return true;
733 }
734
735 if (!isAboveSelectionToggle && m_selectionTogglePressed) {
736 m_selectionManager->setSelected(m_pressedIndex, 1, KItemListSelectionManager::Toggle);
737 m_selectionTogglePressed = false;
738 return true;
739 }
740
741 const bool shiftOrControlPressed = event->modifiers() & Qt::ShiftModifier ||
742 event->modifiers() & Qt::ControlModifier;
743
744 KItemListRubberBand* rubberBand = m_view->rubberBand();
745 if (rubberBand->isActive()) {
746 disconnect(rubberBand, &KItemListRubberBand::endPositionChanged, this, &KItemListController::slotRubberBandChanged);
747 rubberBand->setActive(false);
748 m_oldSelection.clear();
749 m_view->setAutoScroll(false);
750 }
751
752 const QPointF pos = transform.map(event->pos());
753 const int index = m_view->itemAt(pos);
754
755 if (index >= 0 && index == m_pressedIndex) {
756 // The release event is done above the same item as the press event
757
758 if (m_clearSelectionIfItemsAreNotDragged) {
759 // A selected item has been clicked, but no drag operation has been started
760 // -> clear the rest of the selection.
761 m_selectionManager->clearSelection();
762 m_selectionManager->setSelected(m_pressedIndex, 1, KItemListSelectionManager::Select);
763 m_selectionManager->beginAnchoredSelection(m_pressedIndex);
764 }
765
766 if (event->button() & Qt::LeftButton) {
767 bool emitItemActivated = true;
768 if (m_view->isAboveExpansionToggle(index, pos)) {
769 const bool expanded = m_model->isExpanded(index);
770 m_model->setExpanded(index, !expanded);
771
772 emit itemExpansionToggleClicked(index);
773 emitItemActivated = false;
774 } else if (shiftOrControlPressed) {
775 // The mouse click should only update the selection, not trigger the item
776 emitItemActivated = false;
777 } else if (!(m_view->style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick) || m_singleClickActivationEnforced)) {
778 emitItemActivated = false;
779 }
780 if (emitItemActivated) {
781 emit itemActivated(index);
782 }
783 } else if (event->button() & Qt::MiddleButton) {
784 emit itemMiddleClicked(index);
785 }
786 }
787
788 m_pressedMousePos = QPointF();
789 m_pressedIndex = -1;
790 m_clearSelectionIfItemsAreNotDragged = false;
791 return false;
792 }
793
794 bool KItemListController::mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event, const QTransform& transform)
795 {
796 const QPointF pos = transform.map(event->pos());
797 const int index = m_view->itemAt(pos);
798
799 // Expand item if desired - See Bug 295573
800 if (m_mouseDoubleClickAction != ActivateItemOnly) {
801 if (m_view && m_model && m_view->supportsItemExpanding() && m_model->isExpandable(index)) {
802 const bool expanded = m_model->isExpanded(index);
803 m_model->setExpanded(index, !expanded);
804 }
805 }
806
807 if (event->button() & Qt::RightButton) {
808 m_selectionManager->clearSelection();
809 if (index >= 0) {
810 m_selectionManager->setSelected(index);
811 emit itemContextMenuRequested(index, event->screenPos());
812 } else {
813 const QRectF headerBounds = m_view->headerBoundaries();
814 if (headerBounds.contains(event->pos())) {
815 emit headerContextMenuRequested(event->screenPos());
816 } else {
817 emit viewContextMenuRequested(event->screenPos());
818 }
819 }
820 return true;
821 }
822
823 bool emitItemActivated = !(m_view->style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick) || m_singleClickActivationEnforced) &&
824 (event->button() & Qt::LeftButton) &&
825 index >= 0 && index < m_model->count();
826 if (emitItemActivated) {
827 emit itemActivated(index);
828 }
829 return false;
830 }
831
832 bool KItemListController::dragEnterEvent(QGraphicsSceneDragDropEvent* event, const QTransform& transform)
833 {
834 Q_UNUSED(event)
835 Q_UNUSED(transform)
836
837 DragAndDropHelper::clearUrlListMatchesUrlCache();
838
839 return false;
840 }
841
842 bool KItemListController::dragLeaveEvent(QGraphicsSceneDragDropEvent* event, const QTransform& transform)
843 {
844 Q_UNUSED(event)
845 Q_UNUSED(transform)
846
847 m_autoActivationTimer->stop();
848 m_view->setAutoScroll(false);
849 m_view->hideDropIndicator();
850
851 KItemListWidget* widget = hoveredWidget();
852 if (widget) {
853 widget->setHovered(false);
854 emit itemUnhovered(widget->index());
855 }
856 return false;
857 }
858
859 bool KItemListController::dragMoveEvent(QGraphicsSceneDragDropEvent* event, const QTransform& transform)
860 {
861 if (!m_model || !m_view) {
862 return false;
863 }
864
865
866 QUrl hoveredDir = m_model->directory();
867 KItemListWidget* oldHoveredWidget = hoveredWidget();
868
869 const QPointF pos = transform.map(event->pos());
870 KItemListWidget* newHoveredWidget = widgetForPos(pos);
871
872 if (oldHoveredWidget != newHoveredWidget) {
873 m_autoActivationTimer->stop();
874
875 if (oldHoveredWidget) {
876 oldHoveredWidget->setHovered(false);
877 emit itemUnhovered(oldHoveredWidget->index());
878 }
879 }
880
881 if (newHoveredWidget) {
882 bool droppingBetweenItems = false;
883 if (m_model->sortRole().isEmpty()) {
884 // The model supports inserting items between other items.
885 droppingBetweenItems = (m_view->showDropIndicator(pos) >= 0);
886 }
887
888 const int index = newHoveredWidget->index();
889
890 if (m_model->isDir(index)) {
891 hoveredDir = m_model->url(index);
892 }
893
894 if (!droppingBetweenItems) {
895 if (m_model->supportsDropping(index)) {
896 // Something has been dragged on an item.
897 m_view->hideDropIndicator();
898 if (!newHoveredWidget->isHovered()) {
899 newHoveredWidget->setHovered(true);
900 emit itemHovered(index);
901 }
902
903 if (!m_autoActivationTimer->isActive() && m_autoActivationTimer->interval() >= 0) {
904 m_autoActivationTimer->setProperty("index", index);
905 m_autoActivationTimer->start();
906 }
907 }
908 } else {
909 m_autoActivationTimer->stop();
910 if (newHoveredWidget && newHoveredWidget->isHovered()) {
911 newHoveredWidget->setHovered(false);
912 emit itemUnhovered(index);
913 }
914 }
915 } else {
916 m_view->hideDropIndicator();
917 }
918
919 if (DragAndDropHelper::urlListMatchesUrl(event->mimeData()->urls(), hoveredDir)) {
920 event->setDropAction(Qt::IgnoreAction);
921 event->ignore();
922 } else {
923 event->setDropAction(event->proposedAction());
924 event->accept();
925 }
926 return false;
927 }
928
929 bool KItemListController::dropEvent(QGraphicsSceneDragDropEvent* event, const QTransform& transform)
930 {
931 if (!m_view) {
932 return false;
933 }
934
935 m_autoActivationTimer->stop();
936 m_view->setAutoScroll(false);
937
938 const QPointF pos = transform.map(event->pos());
939
940 int dropAboveIndex = -1;
941 if (m_model->sortRole().isEmpty()) {
942 // The model supports inserting of items between other items.
943 dropAboveIndex = m_view->showDropIndicator(pos);
944 }
945
946 if (dropAboveIndex >= 0) {
947 // Something has been dropped between two items.
948 m_view->hideDropIndicator();
949 emit aboveItemDropEvent(dropAboveIndex, event);
950 } else if (!event->mimeData()->hasFormat(m_model->blacklistItemDropEventMimeType())) {
951 // Something has been dropped on an item or on an empty part of the view.
952 emit itemDropEvent(m_view->itemAt(pos), event);
953 }
954
955 QAccessibleEvent accessibilityEvent(view(), QAccessible::DragDropEnd);
956 QAccessible::updateAccessibility(&accessibilityEvent);
957
958 return true;
959 }
960
961 bool KItemListController::hoverEnterEvent(QGraphicsSceneHoverEvent* event, const QTransform& transform)
962 {
963 Q_UNUSED(event)
964 Q_UNUSED(transform)
965 return false;
966 }
967
968 bool KItemListController::hoverMoveEvent(QGraphicsSceneHoverEvent* event, const QTransform& transform)
969 {
970 Q_UNUSED(transform)
971 if (!m_model || !m_view) {
972 return false;
973 }
974
975 KItemListWidget* oldHoveredWidget = hoveredWidget();
976 const QPointF pos = transform.map(event->pos());
977 KItemListWidget* newHoveredWidget = widgetForPos(pos);
978
979 if (oldHoveredWidget != newHoveredWidget) {
980 if (oldHoveredWidget) {
981 oldHoveredWidget->setHovered(false);
982 emit itemUnhovered(oldHoveredWidget->index());
983 }
984
985 if (newHoveredWidget) {
986 newHoveredWidget->setHovered(true);
987 const QPointF mappedPos = newHoveredWidget->mapFromItem(m_view, pos);
988 newHoveredWidget->setHoverPosition(mappedPos);
989 emit itemHovered(newHoveredWidget->index());
990 }
991 } else if (oldHoveredWidget) {
992 const QPointF mappedPos = oldHoveredWidget->mapFromItem(m_view, pos);
993 oldHoveredWidget->setHoverPosition(mappedPos);
994 }
995
996 return false;
997 }
998
999 bool KItemListController::hoverLeaveEvent(QGraphicsSceneHoverEvent* event, const QTransform& transform)
1000 {
1001 Q_UNUSED(event)
1002 Q_UNUSED(transform)
1003
1004 if (!m_model || !m_view) {
1005 return false;
1006 }
1007
1008 foreach (KItemListWidget* widget, m_view->visibleItemListWidgets()) {
1009 if (widget->isHovered()) {
1010 widget->setHovered(false);
1011 emit itemUnhovered(widget->index());
1012 }
1013 }
1014 return false;
1015 }
1016
1017 bool KItemListController::wheelEvent(QGraphicsSceneWheelEvent* event, const QTransform& transform)
1018 {
1019 Q_UNUSED(event)
1020 Q_UNUSED(transform)
1021 return false;
1022 }
1023
1024 bool KItemListController::resizeEvent(QGraphicsSceneResizeEvent* event, const QTransform& transform)
1025 {
1026 Q_UNUSED(event)
1027 Q_UNUSED(transform)
1028 return false;
1029 }
1030
1031 bool KItemListController::processEvent(QEvent* event, const QTransform& transform)
1032 {
1033 if (!event) {
1034 return false;
1035 }
1036
1037 switch (event->type()) {
1038 case QEvent::KeyPress:
1039 return keyPressEvent(static_cast<QKeyEvent*>(event));
1040 case QEvent::InputMethod:
1041 return inputMethodEvent(static_cast<QInputMethodEvent*>(event));
1042 case QEvent::GraphicsSceneMousePress:
1043 return mousePressEvent(static_cast<QGraphicsSceneMouseEvent*>(event), QTransform());
1044 case QEvent::GraphicsSceneMouseMove:
1045 return mouseMoveEvent(static_cast<QGraphicsSceneMouseEvent*>(event), QTransform());
1046 case QEvent::GraphicsSceneMouseRelease:
1047 return mouseReleaseEvent(static_cast<QGraphicsSceneMouseEvent*>(event), QTransform());
1048 case QEvent::GraphicsSceneMouseDoubleClick:
1049 return mouseDoubleClickEvent(static_cast<QGraphicsSceneMouseEvent*>(event), QTransform());
1050 case QEvent::GraphicsSceneWheel:
1051 return wheelEvent(static_cast<QGraphicsSceneWheelEvent*>(event), QTransform());
1052 case QEvent::GraphicsSceneDragEnter:
1053 return dragEnterEvent(static_cast<QGraphicsSceneDragDropEvent*>(event), QTransform());
1054 case QEvent::GraphicsSceneDragLeave:
1055 return dragLeaveEvent(static_cast<QGraphicsSceneDragDropEvent*>(event), QTransform());
1056 case QEvent::GraphicsSceneDragMove:
1057 return dragMoveEvent(static_cast<QGraphicsSceneDragDropEvent*>(event), QTransform());
1058 case QEvent::GraphicsSceneDrop:
1059 return dropEvent(static_cast<QGraphicsSceneDragDropEvent*>(event), QTransform());
1060 case QEvent::GraphicsSceneHoverEnter:
1061 return hoverEnterEvent(static_cast<QGraphicsSceneHoverEvent*>(event), QTransform());
1062 case QEvent::GraphicsSceneHoverMove:
1063 return hoverMoveEvent(static_cast<QGraphicsSceneHoverEvent*>(event), QTransform());
1064 case QEvent::GraphicsSceneHoverLeave:
1065 return hoverLeaveEvent(static_cast<QGraphicsSceneHoverEvent*>(event), QTransform());
1066 case QEvent::GraphicsSceneResize:
1067 return resizeEvent(static_cast<QGraphicsSceneResizeEvent*>(event), transform);
1068 default:
1069 break;
1070 }
1071
1072 return false;
1073 }
1074
1075 void KItemListController::slotViewScrollOffsetChanged(qreal current, qreal previous)
1076 {
1077 if (!m_view) {
1078 return;
1079 }
1080
1081 KItemListRubberBand* rubberBand = m_view->rubberBand();
1082 if (rubberBand->isActive()) {
1083 const qreal diff = current - previous;
1084 // TODO: Ideally just QCursor::pos() should be used as
1085 // new end-position but it seems there is no easy way
1086 // to have something like QWidget::mapFromGlobal() for QGraphicsWidget
1087 // (... or I just missed an easy way to do the mapping)
1088 QPointF endPos = rubberBand->endPosition();
1089 if (m_view->scrollOrientation() == Qt::Vertical) {
1090 endPos.ry() += diff;
1091 } else {
1092 endPos.rx() += diff;
1093 }
1094
1095 rubberBand->setEndPosition(endPos);
1096 }
1097 }
1098
1099 void KItemListController::slotRubberBandChanged()
1100 {
1101 if (!m_view || !m_model || m_model->count() <= 0) {
1102 return;
1103 }
1104
1105 const KItemListRubberBand* rubberBand = m_view->rubberBand();
1106 const QPointF startPos = rubberBand->startPosition();
1107 const QPointF endPos = rubberBand->endPosition();
1108 QRectF rubberBandRect = QRectF(startPos, endPos).normalized();
1109
1110 const bool scrollVertical = (m_view->scrollOrientation() == Qt::Vertical);
1111 if (scrollVertical) {
1112 rubberBandRect.translate(0, -m_view->scrollOffset());
1113 } else {
1114 rubberBandRect.translate(-m_view->scrollOffset(), 0);
1115 }
1116
1117 if (!m_oldSelection.isEmpty()) {
1118 // Clear the old selection that was available before the rubberband has
1119 // been activated in case if no Shift- or Control-key are pressed
1120 const bool shiftOrControlPressed = QApplication::keyboardModifiers() & Qt::ShiftModifier ||
1121 QApplication::keyboardModifiers() & Qt::ControlModifier;
1122 if (!shiftOrControlPressed) {
1123 m_oldSelection.clear();
1124 }
1125 }
1126
1127 KItemSet selectedItems;
1128
1129 // Select all visible items that intersect with the rubberband
1130 foreach (const KItemListWidget* widget, m_view->visibleItemListWidgets()) {
1131 const int index = widget->index();
1132
1133 const QRectF widgetRect = m_view->itemRect(index);
1134 if (widgetRect.intersects(rubberBandRect)) {
1135 const QRectF iconRect = widget->iconRect().translated(widgetRect.topLeft());
1136 const QRectF textRect = widget->textRect().translated(widgetRect.topLeft());
1137 if (iconRect.intersects(rubberBandRect) || textRect.intersects(rubberBandRect)) {
1138 selectedItems.insert(index);
1139 }
1140 }
1141 }
1142
1143 // Select all invisible items that intersect with the rubberband. Instead of
1144 // iterating all items only the area which might be touched by the rubberband
1145 // will be checked.
1146 const bool increaseIndex = scrollVertical ?
1147 startPos.y() > endPos.y(): startPos.x() > endPos.x();
1148
1149 int index = increaseIndex ? m_view->lastVisibleIndex() + 1 : m_view->firstVisibleIndex() - 1;
1150 bool selectionFinished = false;
1151 do {
1152 const QRectF widgetRect = m_view->itemRect(index);
1153 if (widgetRect.intersects(rubberBandRect)) {
1154 selectedItems.insert(index);
1155 }
1156
1157 if (increaseIndex) {
1158 ++index;
1159 selectionFinished = (index >= m_model->count()) ||
1160 ( scrollVertical && widgetRect.top() > rubberBandRect.bottom()) ||
1161 (!scrollVertical && widgetRect.left() > rubberBandRect.right());
1162 } else {
1163 --index;
1164 selectionFinished = (index < 0) ||
1165 ( scrollVertical && widgetRect.bottom() < rubberBandRect.top()) ||
1166 (!scrollVertical && widgetRect.right() < rubberBandRect.left());
1167 }
1168 } while (!selectionFinished);
1169
1170 if (QApplication::keyboardModifiers() & Qt::ControlModifier) {
1171 // If Control is pressed, the selection state of all items in the rubberband is toggled.
1172 // Therefore, the new selection contains:
1173 // 1. All previously selected items which are not inside the rubberband, and
1174 // 2. all items inside the rubberband which have not been selected previously.
1175 m_selectionManager->setSelectedItems(m_oldSelection ^ selectedItems);
1176 }
1177 else {
1178 m_selectionManager->setSelectedItems(selectedItems + m_oldSelection);
1179 }
1180 }
1181
1182 void KItemListController::startDragging()
1183 {
1184 if (!m_view || !m_model) {
1185 return;
1186 }
1187
1188 const KItemSet selectedItems = m_selectionManager->selectedItems();
1189 if (selectedItems.isEmpty()) {
1190 return;
1191 }
1192
1193 QMimeData* data = m_model->createMimeData(selectedItems);
1194 if (!data) {
1195 return;
1196 }
1197
1198 // The created drag object will be owned and deleted
1199 // by QApplication::activeWindow().
1200 QDrag* drag = new QDrag(QApplication::activeWindow());
1201 drag->setMimeData(data);
1202
1203 const QPixmap pixmap = m_view->createDragPixmap(selectedItems);
1204 drag->setPixmap(pixmap);
1205
1206 const QPoint hotSpot((pixmap.width() / pixmap.devicePixelRatio()) / 2, 0);
1207 drag->setHotSpot(hotSpot);
1208
1209 drag->exec(Qt::MoveAction | Qt::CopyAction | Qt::LinkAction, Qt::CopyAction);
1210
1211 QAccessibleEvent accessibilityEvent(view(), QAccessible::DragDropStart);
1212 QAccessible::updateAccessibility(&accessibilityEvent);
1213 }
1214
1215 KItemListWidget* KItemListController::hoveredWidget() const
1216 {
1217 Q_ASSERT(m_view);
1218
1219 foreach (KItemListWidget* widget, m_view->visibleItemListWidgets()) {
1220 if (widget->isHovered()) {
1221 return widget;
1222 }
1223 }
1224
1225 return nullptr;
1226 }
1227
1228 KItemListWidget* KItemListController::widgetForPos(const QPointF& pos) const
1229 {
1230 Q_ASSERT(m_view);
1231
1232 foreach (KItemListWidget* widget, m_view->visibleItemListWidgets()) {
1233 const QPointF mappedPos = widget->mapFromItem(m_view, pos);
1234
1235 const bool hovered = widget->contains(mappedPos) &&
1236 !widget->expansionToggleRect().contains(mappedPos);
1237 if (hovered) {
1238 return widget;
1239 }
1240 }
1241
1242 return nullptr;
1243 }
1244
1245 void KItemListController::updateKeyboardAnchor()
1246 {
1247 const bool validAnchor = m_keyboardAnchorIndex >= 0 &&
1248 m_keyboardAnchorIndex < m_model->count() &&
1249 keyboardAnchorPos(m_keyboardAnchorIndex) == m_keyboardAnchorPos;
1250 if (!validAnchor) {
1251 const int index = m_selectionManager->currentItem();
1252 m_keyboardAnchorIndex = index;
1253 m_keyboardAnchorPos = keyboardAnchorPos(index);
1254 }
1255 }
1256
1257 int KItemListController::nextRowIndex(int index) const
1258 {
1259 if (m_keyboardAnchorIndex < 0) {
1260 return index;
1261 }
1262
1263 const int maxIndex = m_model->count() - 1;
1264 if (index == maxIndex) {
1265 return index;
1266 }
1267
1268 // Calculate the index of the last column inside the row of the current index
1269 int lastColumnIndex = index;
1270 while (keyboardAnchorPos(lastColumnIndex + 1) > keyboardAnchorPos(lastColumnIndex)) {
1271 ++lastColumnIndex;
1272 if (lastColumnIndex >= maxIndex) {
1273 return index;
1274 }
1275 }
1276
1277 // Based on the last column index go to the next row and calculate the nearest index
1278 // that is below the current index
1279 int nextRowIndex = lastColumnIndex + 1;
1280 int searchIndex = nextRowIndex;
1281 qreal minDiff = qAbs(m_keyboardAnchorPos - keyboardAnchorPos(nextRowIndex));
1282 while (searchIndex < maxIndex && keyboardAnchorPos(searchIndex + 1) > keyboardAnchorPos(searchIndex)) {
1283 ++searchIndex;
1284 const qreal searchDiff = qAbs(m_keyboardAnchorPos - keyboardAnchorPos(searchIndex));
1285 if (searchDiff < minDiff) {
1286 minDiff = searchDiff;
1287 nextRowIndex = searchIndex;
1288 }
1289 }
1290
1291 return nextRowIndex;
1292 }
1293
1294 int KItemListController::previousRowIndex(int index) const
1295 {
1296 if (m_keyboardAnchorIndex < 0 || index == 0) {
1297 return index;
1298 }
1299
1300 // Calculate the index of the first column inside the row of the current index
1301 int firstColumnIndex = index;
1302 while (keyboardAnchorPos(firstColumnIndex - 1) < keyboardAnchorPos(firstColumnIndex)) {
1303 --firstColumnIndex;
1304 if (firstColumnIndex <= 0) {
1305 return index;
1306 }
1307 }
1308
1309 // Based on the first column index go to the previous row and calculate the nearest index
1310 // that is above the current index
1311 int previousRowIndex = firstColumnIndex - 1;
1312 int searchIndex = previousRowIndex;
1313 qreal minDiff = qAbs(m_keyboardAnchorPos - keyboardAnchorPos(previousRowIndex));
1314 while (searchIndex > 0 && keyboardAnchorPos(searchIndex - 1) < keyboardAnchorPos(searchIndex)) {
1315 --searchIndex;
1316 const qreal searchDiff = qAbs(m_keyboardAnchorPos - keyboardAnchorPos(searchIndex));
1317 if (searchDiff < minDiff) {
1318 minDiff = searchDiff;
1319 previousRowIndex = searchIndex;
1320 }
1321 }
1322
1323 return previousRowIndex;
1324 }
1325
1326 qreal KItemListController::keyboardAnchorPos(int index) const
1327 {
1328 const QRectF itemRect = m_view->itemRect(index);
1329 if (!itemRect.isEmpty()) {
1330 return (m_view->scrollOrientation() == Qt::Vertical) ? itemRect.x() : itemRect.y();
1331 }
1332
1333 return 0;
1334 }
1335
1336 void KItemListController::updateExtendedSelectionRegion()
1337 {
1338 if (m_view) {
1339 const bool extend = (m_selectionBehavior != MultiSelection);
1340 KItemListStyleOption option = m_view->styleOption();
1341 if (option.extendedSelectionRegion != extend) {
1342 option.extendedSelectionRegion = extend;
1343 m_view->setStyleOption(option);
1344 }
1345 }
1346 }
1347