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