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