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