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