]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistcontroller.cpp
Remove unused #include
[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::showEvent(QShowEvent* event)
214 {
215 Q_UNUSED(event);
216 return false;
217 }
218
219 bool KItemListController::hideEvent(QHideEvent* event)
220 {
221 Q_UNUSED(event);
222 return false;
223 }
224
225 bool KItemListController::keyPressEvent(QKeyEvent* event)
226 {
227 int index = m_selectionManager->currentItem();
228 int key = event->key();
229
230 // Handle the expanding/collapsing of items
231 if (m_view->supportsItemExpanding() && m_model->isExpandable(index)) {
232 if (key == Qt::Key_Right) {
233 if (m_model->setExpanded(index, true)) {
234 return true;
235 }
236 } else if (key == Qt::Key_Left) {
237 if (m_model->setExpanded(index, false)) {
238 return true;
239 }
240 }
241 }
242
243 const bool shiftPressed = event->modifiers() & Qt::ShiftModifier;
244 const bool controlPressed = event->modifiers() & Qt::ControlModifier;
245 const bool shiftOrControlPressed = shiftPressed || controlPressed;
246
247 const int itemCount = m_model->count();
248
249 // For horizontal scroll orientation, transform
250 // the arrow keys to simplify the event handling.
251 if (m_view->scrollOrientation() == Qt::Horizontal) {
252 switch (key) {
253 case Qt::Key_Up: key = Qt::Key_Left; break;
254 case Qt::Key_Down: key = Qt::Key_Right; break;
255 case Qt::Key_Left: key = Qt::Key_Up; break;
256 case Qt::Key_Right: key = Qt::Key_Down; break;
257 default: break;
258 }
259 }
260
261 const bool selectSingleItem = m_selectionBehavior != NoSelection &&
262 itemCount == 1 &&
263 (key == Qt::Key_Home || key == Qt::Key_End ||
264 key == Qt::Key_Up || key == Qt::Key_Down ||
265 key == Qt::Key_Left || key == Qt::Key_Right);
266 if (selectSingleItem) {
267 const int current = m_selectionManager->currentItem();
268 m_selectionManager->setSelected(current);
269 return true;
270 }
271
272 switch (key) {
273 case Qt::Key_Home:
274 index = 0;
275 m_keyboardAnchorIndex = index;
276 m_keyboardAnchorPos = keyboardAnchorPos(index);
277 break;
278
279 case Qt::Key_End:
280 index = itemCount - 1;
281 m_keyboardAnchorIndex = index;
282 m_keyboardAnchorPos = keyboardAnchorPos(index);
283 break;
284
285 case Qt::Key_Left:
286 if (index > 0) {
287 const int expandedParentsCount = m_model->expandedParentsCount(index);
288 if (expandedParentsCount == 0) {
289 --index;
290 } else {
291 // Go to the parent of the current item.
292 do {
293 --index;
294 } while (index > 0 && m_model->expandedParentsCount(index) == expandedParentsCount);
295 }
296 m_keyboardAnchorIndex = index;
297 m_keyboardAnchorPos = keyboardAnchorPos(index);
298 }
299 break;
300
301 case Qt::Key_Right:
302 if (index < itemCount - 1) {
303 ++index;
304 m_keyboardAnchorIndex = index;
305 m_keyboardAnchorPos = keyboardAnchorPos(index);
306 }
307 break;
308
309 case Qt::Key_Up:
310 updateKeyboardAnchor();
311 index = previousRowIndex(index);
312 break;
313
314 case Qt::Key_Down:
315 updateKeyboardAnchor();
316 index = nextRowIndex(index);
317 break;
318
319 case Qt::Key_PageUp:
320 if (m_view->scrollOrientation() == Qt::Horizontal) {
321 // The new current index should correspond to the first item in the current column.
322 int newIndex = qMax(index - 1, 0);
323 while (newIndex != index && m_view->itemRect(newIndex).topLeft().y() < m_view->itemRect(index).topLeft().y()) {
324 index = newIndex;
325 newIndex = qMax(index - 1, 0);
326 }
327 m_keyboardAnchorIndex = index;
328 m_keyboardAnchorPos = keyboardAnchorPos(index);
329 } else {
330 const qreal currentItemBottom = m_view->itemRect(index).bottomLeft().y();
331 const qreal height = m_view->geometry().height();
332
333 // The new current item should be the first item in the current
334 // column whose itemRect's top coordinate is larger than targetY.
335 const qreal targetY = currentItemBottom - height;
336
337 updateKeyboardAnchor();
338 int newIndex = previousRowIndex(index);
339 do {
340 index = newIndex;
341 updateKeyboardAnchor();
342 newIndex = previousRowIndex(index);
343 } while (m_view->itemRect(newIndex).topLeft().y() > targetY && newIndex != index);
344 }
345 break;
346
347 case Qt::Key_PageDown:
348 if (m_view->scrollOrientation() == Qt::Horizontal) {
349 // The new current index should correspond to the last item in the current column.
350 int newIndex = qMin(index + 1, m_model->count() - 1);
351 while (newIndex != index && m_view->itemRect(newIndex).topLeft().y() > m_view->itemRect(index).topLeft().y()) {
352 index = newIndex;
353 newIndex = qMin(index + 1, m_model->count() - 1);
354 }
355 m_keyboardAnchorIndex = index;
356 m_keyboardAnchorPos = keyboardAnchorPos(index);
357 } else {
358 const qreal currentItemTop = m_view->itemRect(index).topLeft().y();
359 const qreal height = m_view->geometry().height();
360
361 // The new current item should be the last item in the current
362 // column whose itemRect's bottom coordinate is smaller than targetY.
363 const qreal targetY = currentItemTop + height;
364
365 updateKeyboardAnchor();
366 int newIndex = nextRowIndex(index);
367 do {
368 index = newIndex;
369 updateKeyboardAnchor();
370 newIndex = nextRowIndex(index);
371 } while (m_view->itemRect(newIndex).bottomLeft().y() < targetY && newIndex != index);
372 }
373 break;
374
375 case Qt::Key_Enter:
376 case Qt::Key_Return: {
377 const KItemSet selectedItems = m_selectionManager->selectedItems();
378 if (selectedItems.count() >= 2) {
379 emit itemsActivated(selectedItems);
380 } else if (selectedItems.count() == 1) {
381 emit itemActivated(selectedItems.first());
382 } else {
383 emit itemActivated(index);
384 }
385 break;
386 }
387
388 case Qt::Key_Menu: {
389 // Emit the signal itemContextMenuRequested() in case if at least one
390 // item is selected. Otherwise the signal viewContextMenuRequested() will be emitted.
391 const KItemSet selectedItems = m_selectionManager->selectedItems();
392 int index = -1;
393 if (selectedItems.count() >= 2) {
394 const int currentItemIndex = m_selectionManager->currentItem();
395 index = selectedItems.contains(currentItemIndex)
396 ? currentItemIndex : selectedItems.first();
397 } else if (selectedItems.count() == 1) {
398 index = selectedItems.first();
399 }
400
401 if (index >= 0) {
402 const QRectF contextRect = m_view->itemContextRect(index);
403 const QPointF pos(m_view->scene()->views().first()->mapToGlobal(contextRect.bottomRight().toPoint()));
404 emit itemContextMenuRequested(index, pos);
405 } else {
406 emit viewContextMenuRequested(QCursor::pos());
407 }
408 break;
409 }
410
411 case Qt::Key_Escape:
412 if (m_selectionBehavior != SingleSelection) {
413 m_selectionManager->clearSelection();
414 }
415 m_keyboardManager->cancelSearch();
416 emit escapePressed();
417 break;
418
419 case Qt::Key_Space:
420 if (m_selectionBehavior == MultiSelection) {
421 if (controlPressed) {
422 // Toggle the selection state of the current item.
423 m_selectionManager->endAnchoredSelection();
424 m_selectionManager->setSelected(index, 1, KItemListSelectionManager::Toggle);
425 m_selectionManager->beginAnchoredSelection(index);
426 break;
427 } else {
428 // Select the current item if it is not selected yet.
429 const int current = m_selectionManager->currentItem();
430 if (!m_selectionManager->isSelected(current)) {
431 m_selectionManager->setSelected(current);
432 break;
433 }
434 }
435 }
436 // fall through
437 // to the default case and add the Space to the current search string.
438 default:
439 m_keyboardManager->addKeys(event->text());
440 // Make sure unconsumed events get propagated up the chain. #302329
441 event->ignore();
442 return false;
443 }
444
445 if (m_selectionManager->currentItem() != index) {
446 switch (m_selectionBehavior) {
447 case NoSelection:
448 m_selectionManager->setCurrentItem(index);
449 break;
450
451 case SingleSelection:
452 m_selectionManager->setCurrentItem(index);
453 m_selectionManager->clearSelection();
454 m_selectionManager->setSelected(index, 1);
455 break;
456
457 case MultiSelection:
458 if (controlPressed) {
459 m_selectionManager->endAnchoredSelection();
460 }
461
462 m_selectionManager->setCurrentItem(index);
463
464 if (!shiftOrControlPressed) {
465 m_selectionManager->clearSelection();
466 m_selectionManager->setSelected(index, 1);
467 }
468
469 if (!shiftPressed) {
470 m_selectionManager->beginAnchoredSelection(index);
471 }
472 break;
473 }
474
475 m_view->scrollToItem(index);
476 }
477 return true;
478 }
479
480 void KItemListController::slotChangeCurrentItem(const QString& text, bool searchFromNextItem)
481 {
482 if (!m_model || m_model->count() == 0) {
483 return;
484 }
485 const int currentIndex = m_selectionManager->currentItem();
486 int index;
487 if (searchFromNextItem) {
488 index = m_model->indexForKeyboardSearch(text, (currentIndex + 1) % m_model->count());
489 } else {
490 index = m_model->indexForKeyboardSearch(text, currentIndex);
491 }
492 if (index >= 0) {
493 m_selectionManager->setCurrentItem(index);
494
495 if (m_selectionBehavior != NoSelection) {
496 m_selectionManager->clearSelection();
497 m_selectionManager->setSelected(index, 1);
498 m_selectionManager->beginAnchoredSelection(index);
499 }
500
501 m_view->scrollToItem(index);
502 }
503 }
504
505 void KItemListController::slotAutoActivationTimeout()
506 {
507 if (!m_model || !m_view) {
508 return;
509 }
510
511 const int index = m_autoActivationTimer->property("index").toInt();
512 if (index < 0 || index >= m_model->count()) {
513 return;
514 }
515
516 /* m_view->isUnderMouse() fixes a bug in the Folder-View-Panel and in the
517 * Places-Panel.
518 *
519 * Bug: When you drag a file onto a Folder-View-Item or a Places-Item and
520 * then move away before the auto-activation timeout triggers, than the
521 * item still becomes activated/expanded.
522 *
523 * See Bug 293200 and 305783
524 */
525 if (m_model->supportsDropping(index) && m_view->isUnderMouse()) {
526 if (m_view->supportsItemExpanding() && m_model->isExpandable(index)) {
527 const bool expanded = m_model->isExpanded(index);
528 m_model->setExpanded(index, !expanded);
529 } else if (m_autoActivationBehavior != ExpansionOnly) {
530 emit itemActivated(index);
531 }
532 }
533 }
534
535 bool KItemListController::inputMethodEvent(QInputMethodEvent* event)
536 {
537 Q_UNUSED(event);
538 return false;
539 }
540
541 bool KItemListController::mousePressEvent(QGraphicsSceneMouseEvent* event, const QTransform& transform)
542 {
543 if (!m_view) {
544 return false;
545 }
546
547 m_pressedMousePos = transform.map(event->pos());
548 m_pressedIndex = m_view->itemAt(m_pressedMousePos);
549 emit mouseButtonPressed(m_pressedIndex, event->buttons());
550
551 if (event->buttons() & (Qt::BackButton | Qt::ForwardButton)) {
552 // Do not select items when clicking the back/forward buttons, see
553 // https://bugs.kde.org/show_bug.cgi?id=327412.
554 return true;
555 }
556
557 if (m_view->isAboveExpansionToggle(m_pressedIndex, m_pressedMousePos)) {
558 m_selectionManager->endAnchoredSelection();
559 m_selectionManager->setCurrentItem(m_pressedIndex);
560 m_selectionManager->beginAnchoredSelection(m_pressedIndex);
561 return true;
562 }
563
564 m_selectionTogglePressed = m_view->isAboveSelectionToggle(m_pressedIndex, m_pressedMousePos);
565 if (m_selectionTogglePressed) {
566 m_selectionManager->setSelected(m_pressedIndex, 1, KItemListSelectionManager::Toggle);
567 // The previous anchored selection has been finished already in
568 // KItemListSelectionManager::setSelected(). We can safely change
569 // the current item and start a new anchored selection now.
570 m_selectionManager->setCurrentItem(m_pressedIndex);
571 m_selectionManager->beginAnchoredSelection(m_pressedIndex);
572 return true;
573 }
574
575 const bool shiftPressed = event->modifiers() & Qt::ShiftModifier;
576 const bool controlPressed = event->modifiers() & Qt::ControlModifier;
577
578 // The previous selection is cleared if either
579 // 1. The selection mode is SingleSelection, or
580 // 2. the selection mode is MultiSelection, and *none* of the following conditions are met:
581 // a) Shift or Control are pressed.
582 // b) The clicked item is selected already. In that case, the user might want to:
583 // - start dragging multiple items, or
584 // - open the context menu and perform an action for all selected items.
585 const bool shiftOrControlPressed = shiftPressed || controlPressed;
586 const bool pressedItemAlreadySelected = m_pressedIndex >= 0 && m_selectionManager->isSelected(m_pressedIndex);
587 const bool clearSelection = m_selectionBehavior == SingleSelection ||
588 (!shiftOrControlPressed && !pressedItemAlreadySelected);
589 if (clearSelection) {
590 m_selectionManager->clearSelection();
591 } else if (pressedItemAlreadySelected && !shiftOrControlPressed && (event->buttons() & Qt::LeftButton)) {
592 // The user might want to start dragging multiple items, but if he clicks the item
593 // in order to trigger it instead, the other selected items must be deselected.
594 // However, we do not know yet what the user is going to do.
595 // -> remember that the user pressed an item which had been selected already and
596 // clear the selection in mouseReleaseEvent(), unless the items are dragged.
597 m_clearSelectionIfItemsAreNotDragged = true;
598
599 if (m_selectionManager->selectedItems().count() == 1 && m_view->isAboveText(m_pressedIndex, m_pressedMousePos)) {
600 emit selectedItemTextPressed(m_pressedIndex);
601 }
602 }
603
604 if (!shiftPressed) {
605 // Finish the anchored selection before the current index is changed
606 m_selectionManager->endAnchoredSelection();
607 }
608
609 if (m_pressedIndex >= 0) {
610 m_selectionManager->setCurrentItem(m_pressedIndex);
611
612 switch (m_selectionBehavior) {
613 case NoSelection:
614 break;
615
616 case SingleSelection:
617 m_selectionManager->setSelected(m_pressedIndex);
618 break;
619
620 case MultiSelection:
621 if (controlPressed && !shiftPressed) {
622 m_selectionManager->setSelected(m_pressedIndex, 1, KItemListSelectionManager::Toggle);
623 m_selectionManager->beginAnchoredSelection(m_pressedIndex);
624 } else if (!shiftPressed || !m_selectionManager->isAnchoredSelectionActive()) {
625 // Select the pressed item and start a new anchored selection
626 m_selectionManager->setSelected(m_pressedIndex, 1, KItemListSelectionManager::Select);
627 m_selectionManager->beginAnchoredSelection(m_pressedIndex);
628 }
629 break;
630
631 default:
632 Q_ASSERT(false);
633 break;
634 }
635
636 if (event->buttons() & Qt::RightButton) {
637 emit itemContextMenuRequested(m_pressedIndex, event->screenPos());
638 }
639
640 return true;
641 }
642
643 if (event->buttons() & Qt::RightButton) {
644 const QRectF headerBounds = m_view->headerBoundaries();
645 if (headerBounds.contains(event->pos())) {
646 emit headerContextMenuRequested(event->screenPos());
647 } else {
648 emit viewContextMenuRequested(event->screenPos());
649 }
650 return true;
651 }
652
653 if (m_selectionBehavior == MultiSelection) {
654 QPointF startPos = m_pressedMousePos;
655 if (m_view->scrollOrientation() == Qt::Vertical) {
656 startPos.ry() += m_view->scrollOffset();
657 if (m_view->itemSize().width() < 0) {
658 // Use a special rubberband for views that have only one column and
659 // expand the rubberband to use the whole width of the view.
660 startPos.setX(0);
661 }
662 } else {
663 startPos.rx() += m_view->scrollOffset();
664 }
665
666 m_oldSelection = m_selectionManager->selectedItems();
667 KItemListRubberBand* rubberBand = m_view->rubberBand();
668 rubberBand->setStartPosition(startPos);
669 rubberBand->setEndPosition(startPos);
670 rubberBand->setActive(true);
671 connect(rubberBand, &KItemListRubberBand::endPositionChanged, this, &KItemListController::slotRubberBandChanged);
672 m_view->setAutoScroll(true);
673 }
674
675 return false;
676 }
677
678 bool KItemListController::mouseMoveEvent(QGraphicsSceneMouseEvent* event, const QTransform& transform)
679 {
680 if (!m_view) {
681 return false;
682 }
683
684 if (m_pressedIndex >= 0) {
685 // Check whether a dragging should be started
686 if (event->buttons() & Qt::LeftButton) {
687 const QPointF pos = transform.map(event->pos());
688 if ((pos - m_pressedMousePos).manhattanLength() >= QApplication::startDragDistance()) {
689 if (!m_selectionManager->isSelected(m_pressedIndex)) {
690 // Always assure that the dragged item gets selected. Usually this is already
691 // done on the mouse-press event, but when using the selection-toggle on a
692 // selected item the dragged item is not selected yet.
693 m_selectionManager->setSelected(m_pressedIndex, 1, KItemListSelectionManager::Toggle);
694 } else {
695 // A selected item has been clicked to drag all selected items
696 // -> the selection should not be cleared when the mouse button is released.
697 m_clearSelectionIfItemsAreNotDragged = false;
698 }
699
700 startDragging();
701 }
702 }
703 } else {
704 KItemListRubberBand* rubberBand = m_view->rubberBand();
705 if (rubberBand->isActive()) {
706 QPointF endPos = transform.map(event->pos());
707
708 // Update the current item.
709 const int newCurrent = m_view->itemAt(endPos);
710 if (newCurrent >= 0) {
711 // It's expected that the new current index is also the new anchor (bug 163451).
712 m_selectionManager->endAnchoredSelection();
713 m_selectionManager->setCurrentItem(newCurrent);
714 m_selectionManager->beginAnchoredSelection(newCurrent);
715 }
716
717 if (m_view->scrollOrientation() == Qt::Vertical) {
718 endPos.ry() += m_view->scrollOffset();
719 if (m_view->itemSize().width() < 0) {
720 // Use a special rubberband for views that have only one column and
721 // expand the rubberband to use the whole width of the view.
722 endPos.setX(m_view->size().width());
723 }
724 } else {
725 endPos.rx() += m_view->scrollOffset();
726 }
727 rubberBand->setEndPosition(endPos);
728 }
729 }
730
731 return false;
732 }
733
734 bool KItemListController::mouseReleaseEvent(QGraphicsSceneMouseEvent* event, const QTransform& transform)
735 {
736 if (!m_view) {
737 return false;
738 }
739
740 emit mouseButtonReleased(m_pressedIndex, event->buttons());
741
742 const bool isAboveSelectionToggle = m_view->isAboveSelectionToggle(m_pressedIndex, m_pressedMousePos);
743 if (isAboveSelectionToggle) {
744 m_selectionTogglePressed = false;
745 return true;
746 }
747
748 if (!isAboveSelectionToggle && m_selectionTogglePressed) {
749 m_selectionManager->setSelected(m_pressedIndex, 1, KItemListSelectionManager::Toggle);
750 m_selectionTogglePressed = false;
751 return true;
752 }
753
754 const bool shiftOrControlPressed = event->modifiers() & Qt::ShiftModifier ||
755 event->modifiers() & Qt::ControlModifier;
756
757 KItemListRubberBand* rubberBand = m_view->rubberBand();
758 if (rubberBand->isActive()) {
759 disconnect(rubberBand, &KItemListRubberBand::endPositionChanged, this, &KItemListController::slotRubberBandChanged);
760 rubberBand->setActive(false);
761 m_oldSelection.clear();
762 m_view->setAutoScroll(false);
763 }
764
765 const QPointF pos = transform.map(event->pos());
766 const int index = m_view->itemAt(pos);
767
768 if (index >= 0 && index == m_pressedIndex) {
769 // The release event is done above the same item as the press event
770
771 if (m_clearSelectionIfItemsAreNotDragged) {
772 // A selected item has been clicked, but no drag operation has been started
773 // -> clear the rest of the selection.
774 m_selectionManager->clearSelection();
775 m_selectionManager->setSelected(m_pressedIndex, 1, KItemListSelectionManager::Select);
776 m_selectionManager->beginAnchoredSelection(m_pressedIndex);
777 }
778
779 if (event->button() & Qt::LeftButton) {
780 bool emitItemActivated = true;
781 if (m_view->isAboveExpansionToggle(index, pos)) {
782 const bool expanded = m_model->isExpanded(index);
783 m_model->setExpanded(index, !expanded);
784
785 emit itemExpansionToggleClicked(index);
786 emitItemActivated = false;
787 } else if (shiftOrControlPressed) {
788 // The mouse click should only update the selection, not trigger the item
789 emitItemActivated = false;
790 } else if (!(m_view->style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick) || m_singleClickActivationEnforced)) {
791 emitItemActivated = false;
792 }
793 if (emitItemActivated) {
794 emit itemActivated(index);
795 }
796 } else if (event->button() & Qt::MidButton) {
797 emit itemMiddleClicked(index);
798 }
799 }
800
801 m_pressedMousePos = QPointF();
802 m_pressedIndex = -1;
803 m_clearSelectionIfItemsAreNotDragged = false;
804 return false;
805 }
806
807 bool KItemListController::mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event, const QTransform& transform)
808 {
809 const QPointF pos = transform.map(event->pos());
810 const int index = m_view->itemAt(pos);
811
812 // Expand item if desired - See Bug 295573
813 if (m_mouseDoubleClickAction != ActivateItemOnly) {
814 if (m_view && m_model && m_view->supportsItemExpanding() && m_model->isExpandable(index)) {
815 const bool expanded = m_model->isExpanded(index);
816 m_model->setExpanded(index, !expanded);
817 }
818 }
819
820 if (event->button() & Qt::RightButton) {
821 m_selectionManager->clearSelection();
822 if (index >= 0) {
823 m_selectionManager->setSelected(index);
824 emit itemContextMenuRequested(index, event->screenPos());
825 } else {
826 const QRectF headerBounds = m_view->headerBoundaries();
827 if (headerBounds.contains(event->pos())) {
828 emit headerContextMenuRequested(event->screenPos());
829 } else {
830 emit viewContextMenuRequested(event->screenPos());
831 }
832 }
833 return true;
834 }
835
836 bool emitItemActivated = !(m_view->style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick) || m_singleClickActivationEnforced) &&
837 (event->button() & Qt::LeftButton) &&
838 index >= 0 && index < m_model->count();
839 if (emitItemActivated) {
840 emit itemActivated(index);
841 }
842 return false;
843 }
844
845 bool KItemListController::dragEnterEvent(QGraphicsSceneDragDropEvent* event, const QTransform& transform)
846 {
847 Q_UNUSED(event);
848 Q_UNUSED(transform);
849
850 DragAndDropHelper::clearUrlListMatchesUrlCache();
851
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