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