X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/blobdiff_plain/5289e852c16e2d9dbd6c4855a232c1daab496fe5..7479fef79962cede2c5d5ad7383c11a83c4f978d:/src/kitemviews/kitemlistcontroller.cpp diff --git a/src/kitemviews/kitemlistcontroller.cpp b/src/kitemviews/kitemlistcontroller.cpp index 9dda3384a..2b5266600 100644 --- a/src/kitemviews/kitemlistcontroller.cpp +++ b/src/kitemviews/kitemlistcontroller.cpp @@ -1,71 +1,71 @@ -/*************************************************************************** - * Copyright (C) 2011 by Peter Penz * - * * - * Based on the Itemviews NG project from Trolltech Labs: * - * http://qt.gitorious.org/qt-labs/itemviews-ng * - * * - * This program is free software; you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation; either version 2 of the License, or * - * (at your option) any later version. * - * * - * This program is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * - * GNU General Public License for more details. * - * * - * You should have received a copy of the GNU General Public License * - * along with this program; if not, write to the * - * Free Software Foundation, Inc., * - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * - ***************************************************************************/ +/* + * SPDX-FileCopyrightText: 2011 Peter Penz + * SPDX-FileCopyrightText: 2012 Frank Reininghaus + * + * Based on the Itemviews NG project from Trolltech Labs + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ #include "kitemlistcontroller.h" -#include "kitemlistview.h" -#include "kitemlistrubberband_p.h" #include "kitemlistselectionmanager.h" -#include "kitemlistkeyboardsearchmanager_p.h" +#include "kitemlistview.h" +#include "private/kitemlistkeyboardsearchmanager.h" +#include "private/kitemlistrubberband.h" +#include "views/draganddrophelper.h" +#include #include #include -#include #include #include #include #include #include -#include -#include - -KItemListController::KItemListController(QObject* parent) : +KItemListController::KItemListController(KItemModelBase* model, KItemListView* view, QObject* parent) : QObject(parent), - m_singleClickActivation(KGlobalSettings::singleClick()), + m_singleClickActivationEnforced(false), m_selectionTogglePressed(false), + m_clearSelectionIfItemsAreNotDragged(false), m_selectionBehavior(NoSelection), - m_model(0), - m_view(0), + m_autoActivationBehavior(ActivationAndExpansion), + m_mouseDoubleClickAction(ActivateItemOnly), + m_model(nullptr), + m_view(nullptr), m_selectionManager(new KItemListSelectionManager(this)), m_keyboardManager(new KItemListKeyboardSearchManager(this)), m_pressedIndex(-1), m_pressedMousePos(), - m_autoActivationTimer(0), + m_autoActivationTimer(nullptr), m_oldSelection(), m_keyboardAnchorIndex(-1), m_keyboardAnchorPos(0) { - connect(m_keyboardManager, SIGNAL(changeCurrentItem(QString,bool)), - this, SLOT(slotChangeCurrentItem(QString,bool))); + connect(m_keyboardManager, &KItemListKeyboardSearchManager::changeCurrentItem, + this, &KItemListController::slotChangeCurrentItem); + connect(m_selectionManager, &KItemListSelectionManager::currentChanged, + m_keyboardManager, &KItemListKeyboardSearchManager::slotCurrentChanged); + connect(m_selectionManager, &KItemListSelectionManager::selectionChanged, + m_keyboardManager, &KItemListKeyboardSearchManager::slotSelectionChanged); m_autoActivationTimer = new QTimer(this); m_autoActivationTimer->setSingleShot(true); m_autoActivationTimer->setInterval(-1); - connect(m_autoActivationTimer, SIGNAL(timeout()), this, SLOT(slotAutoActivationTimeout())); + connect(m_autoActivationTimer, &QTimer::timeout, this, &KItemListController::slotAutoActivationTimeout); + + setModel(model); + setView(view); } KItemListController::~KItemListController() { + setView(nullptr); + Q_ASSERT(!m_view); + + setModel(nullptr); + Q_ASSERT(!m_model); } void KItemListController::setModel(KItemModelBase* model) @@ -75,7 +75,14 @@ void KItemListController::setModel(KItemModelBase* model) } KItemModelBase* oldModel = m_model; + if (oldModel) { + oldModel->deleteLater(); + } + m_model = model; + if (m_model) { + m_model->setParent(this); + } if (m_view) { m_view->setModel(m_model); @@ -104,15 +111,18 @@ void KItemListController::setView(KItemListView* view) KItemListView* oldView = m_view; if (oldView) { - disconnect(oldView, SIGNAL(scrollOffsetChanged(qreal,qreal)), this, SLOT(slotViewScrollOffsetChanged(qreal,qreal))); + disconnect(oldView, &KItemListView::scrollOffsetChanged, this, &KItemListController::slotViewScrollOffsetChanged); + oldView->deleteLater(); } m_view = view; if (m_view) { + m_view->setParent(this); m_view->setController(this); m_view->setModel(m_model); - connect(m_view, SIGNAL(scrollOffsetChanged(qreal,qreal)), this, SLOT(slotViewScrollOffsetChanged(qreal,qreal))); + connect(m_view, &KItemListView::scrollOffsetChanged, this, &KItemListController::slotViewScrollOffsetChanged); + updateExtendedSelectionRegion(); } emit viewChanged(m_view, oldView); @@ -126,6 +136,7 @@ KItemListView* KItemListController::view() const void KItemListController::setSelectionBehavior(SelectionBehavior behavior) { m_selectionBehavior = behavior; + updateExtendedSelectionRegion(); } KItemListController::SelectionBehavior KItemListController::selectionBehavior() const @@ -133,36 +144,58 @@ KItemListController::SelectionBehavior KItemListController::selectionBehavior() return m_selectionBehavior; } -void KItemListController::setAutoActivationDelay(int delay) +void KItemListController::setAutoActivationBehavior(AutoActivationBehavior behavior) { - m_autoActivationTimer->setInterval(delay); + m_autoActivationBehavior = behavior; } -int KItemListController::autoActivationDelay() const +KItemListController::AutoActivationBehavior KItemListController::autoActivationBehavior() const { - return m_autoActivationTimer->interval(); + return m_autoActivationBehavior; } -void KItemListController::setSingleClickActivation(bool singleClick) +void KItemListController::setMouseDoubleClickAction(MouseDoubleClickAction action) { - m_singleClickActivation = singleClick; + m_mouseDoubleClickAction = action; } -bool KItemListController::singleClickActivation() const +KItemListController::MouseDoubleClickAction KItemListController::mouseDoubleClickAction() const { - return m_singleClickActivation; + return m_mouseDoubleClickAction; } -bool KItemListController::showEvent(QShowEvent* event) +int KItemListController::indexCloseToMousePressedPosition() const { - Q_UNUSED(event); - return false; + QHashIterator it(m_view->m_visibleGroups); + while (it.hasNext()) { + it.next(); + KItemListGroupHeader *groupHeader = it.value(); + const QPointF mappedToGroup = groupHeader->mapFromItem(nullptr, m_pressedMousePos); + if (groupHeader->contains(mappedToGroup)) { + return it.key()->index(); + } + } + return -1; } -bool KItemListController::hideEvent(QHideEvent* event) +void KItemListController::setAutoActivationDelay(int delay) { - Q_UNUSED(event); - return false; + m_autoActivationTimer->setInterval(delay); +} + +int KItemListController::autoActivationDelay() const +{ + return m_autoActivationTimer->interval(); +} + +void KItemListController::setSingleClickActivationEnforced(bool singleClick) +{ + m_singleClickActivationEnforced = singleClick; +} + +bool KItemListController::singleClickActivationEnforced() const +{ + return m_singleClickActivationEnforced; } bool KItemListController::keyPressEvent(QKeyEvent* event) @@ -186,6 +219,10 @@ bool KItemListController::keyPressEvent(QKeyEvent* event) const bool shiftPressed = event->modifiers() & Qt::ShiftModifier; const bool controlPressed = event->modifiers() & Qt::ControlModifier; const bool shiftOrControlPressed = shiftPressed || controlPressed; + const bool navigationPressed = key == Qt::Key_Home || key == Qt::Key_End || + key == Qt::Key_PageUp || key == Qt::Key_PageDown || + key == Qt::Key_Up || key == Qt::Key_Down || + key == Qt::Key_Left || key == Qt::Key_Right; const int itemCount = m_model->count(); @@ -200,11 +237,9 @@ bool KItemListController::keyPressEvent(QKeyEvent* event) default: break; } } - - const bool selectSingleItem = itemCount == 1 && - (key == Qt::Key_Home || key == Qt::Key_End || - key == Qt::Key_Up || key == Qt::Key_Down || - key == Qt::Key_Left || key == Qt::Key_Right); + + const bool selectSingleItem = m_selectionBehavior != NoSelection && itemCount == 1 && navigationPressed; + if (selectSingleItem) { const int current = m_selectionManager->currentItem(); m_selectionManager->setSelected(current); @@ -214,15 +249,27 @@ bool KItemListController::keyPressEvent(QKeyEvent* event) switch (key) { case Qt::Key_Home: index = 0; + m_keyboardAnchorIndex = index; + m_keyboardAnchorPos = keyboardAnchorPos(index); break; case Qt::Key_End: index = itemCount - 1; + m_keyboardAnchorIndex = index; + m_keyboardAnchorPos = keyboardAnchorPos(index); break; case Qt::Key_Left: if (index > 0) { - --index; + const int expandedParentsCount = m_model->expandedParentsCount(index); + if (expandedParentsCount == 0) { + --index; + } else { + // Go to the parent of the current item. + do { + --index; + } while (index > 0 && m_model->expandedParentsCount(index) == expandedParentsCount); + } m_keyboardAnchorIndex = index; m_keyboardAnchorPos = keyboardAnchorPos(index); } @@ -238,49 +285,94 @@ bool KItemListController::keyPressEvent(QKeyEvent* event) case Qt::Key_Up: updateKeyboardAnchor(); - index = previousRowIndex(); + index = previousRowIndex(index); break; case Qt::Key_Down: updateKeyboardAnchor(); - index = nextRowIndex(); + index = nextRowIndex(index); + break; + + case Qt::Key_PageUp: + if (m_view->scrollOrientation() == Qt::Horizontal) { + // The new current index should correspond to the first item in the current column. + int newIndex = qMax(index - 1, 0); + while (newIndex != index && m_view->itemRect(newIndex).topLeft().y() < m_view->itemRect(index).topLeft().y()) { + index = newIndex; + newIndex = qMax(index - 1, 0); + } + m_keyboardAnchorIndex = index; + m_keyboardAnchorPos = keyboardAnchorPos(index); + } else { + const qreal currentItemBottom = m_view->itemRect(index).bottomLeft().y(); + const qreal height = m_view->geometry().height(); + + // The new current item should be the first item in the current + // column whose itemRect's top coordinate is larger than targetY. + const qreal targetY = currentItemBottom - height; + + updateKeyboardAnchor(); + int newIndex = previousRowIndex(index); + do { + index = newIndex; + updateKeyboardAnchor(); + newIndex = previousRowIndex(index); + } while (m_view->itemRect(newIndex).topLeft().y() > targetY && newIndex != index); + } + break; + + case Qt::Key_PageDown: + if (m_view->scrollOrientation() == Qt::Horizontal) { + // The new current index should correspond to the last item in the current column. + int newIndex = qMin(index + 1, m_model->count() - 1); + while (newIndex != index && m_view->itemRect(newIndex).topLeft().y() > m_view->itemRect(index).topLeft().y()) { + index = newIndex; + newIndex = qMin(index + 1, m_model->count() - 1); + } + m_keyboardAnchorIndex = index; + m_keyboardAnchorPos = keyboardAnchorPos(index); + } else { + const qreal currentItemTop = m_view->itemRect(index).topLeft().y(); + const qreal height = m_view->geometry().height(); + + // The new current item should be the last item in the current + // column whose itemRect's bottom coordinate is smaller than targetY. + const qreal targetY = currentItemTop + height; + + updateKeyboardAnchor(); + int newIndex = nextRowIndex(index); + do { + index = newIndex; + updateKeyboardAnchor(); + newIndex = nextRowIndex(index); + } while (m_view->itemRect(newIndex).bottomLeft().y() < targetY && newIndex != index); + } break; case Qt::Key_Enter: case Qt::Key_Return: { - const QSet selectedItems = m_selectionManager->selectedItems(); + const KItemSet selectedItems = m_selectionManager->selectedItems(); if (selectedItems.count() >= 2) { emit itemsActivated(selectedItems); } else if (selectedItems.count() == 1) { - emit itemActivated(selectedItems.toList().first()); + emit itemActivated(selectedItems.first()); } else { emit itemActivated(index); } break; } - case Qt::Key_Space: - if (controlPressed) { - m_selectionManager->endAnchoredSelection(); - m_selectionManager->setSelected(index, 1, KItemListSelectionManager::Toggle); - m_selectionManager->beginAnchoredSelection(index); - } else { - const int current = m_selectionManager->currentItem(); - m_selectionManager->setSelected(current); - } - break; - case Qt::Key_Menu: { // Emit the signal itemContextMenuRequested() in case if at least one // item is selected. Otherwise the signal viewContextMenuRequested() will be emitted. - const QSet selectedItems = m_selectionManager->selectedItems(); + const KItemSet selectedItems = m_selectionManager->selectedItems(); int index = -1; if (selectedItems.count() >= 2) { const int currentItemIndex = m_selectionManager->currentItem(); index = selectedItems.contains(currentItemIndex) - ? currentItemIndex : selectedItems.toList().first(); + ? currentItemIndex : selectedItems.first(); } else if (selectedItems.count() == 1) { - index = selectedItems.toList().first(); + index = selectedItems.first(); } if (index >= 0) { @@ -293,27 +385,71 @@ bool KItemListController::keyPressEvent(QKeyEvent* event) break; } + case Qt::Key_Escape: + if (m_selectionBehavior != SingleSelection) { + m_selectionManager->clearSelection(); + } + m_keyboardManager->cancelSearch(); + emit escapePressed(); + break; + + case Qt::Key_Space: + if (m_selectionBehavior == MultiSelection) { + if (controlPressed) { + // Toggle the selection state of the current item. + m_selectionManager->endAnchoredSelection(); + m_selectionManager->setSelected(index, 1, KItemListSelectionManager::Toggle); + m_selectionManager->beginAnchoredSelection(index); + break; + } else { + // Select the current item if it is not selected yet. + const int current = m_selectionManager->currentItem(); + if (!m_selectionManager->isSelected(current)) { + m_selectionManager->setSelected(current); + break; + } + } + } + Q_FALLTHROUGH(); // fall through to the default case and add the Space to the current search string. default: m_keyboardManager->addKeys(event->text()); + // Make sure unconsumed events get propagated up the chain. #302329 + event->ignore(); return false; } if (m_selectionManager->currentItem() != index) { - if (controlPressed) { - m_selectionManager->endAnchoredSelection(); - } - - m_selectionManager->setCurrentItem(index); + switch (m_selectionBehavior) { + case NoSelection: + m_selectionManager->setCurrentItem(index); + break; - if (!shiftOrControlPressed || m_selectionBehavior == SingleSelection) { + case SingleSelection: + m_selectionManager->setCurrentItem(index); m_selectionManager->clearSelection(); m_selectionManager->setSelected(index, 1); - } + break; - if (!shiftPressed) { - m_selectionManager->beginAnchoredSelection(index); + case MultiSelection: + if (controlPressed) { + m_selectionManager->endAnchoredSelection(); + } + + m_selectionManager->setCurrentItem(index); + + if (!shiftOrControlPressed) { + m_selectionManager->clearSelection(); + m_selectionManager->setSelected(index, 1); + } + + if (!shiftPressed) { + m_selectionManager->beginAnchoredSelection(index); + } + break; } + } + if (navigationPressed) { m_view->scrollToItem(index); } return true; @@ -324,18 +460,21 @@ void KItemListController::slotChangeCurrentItem(const QString& text, bool search if (!m_model || m_model->count() == 0) { return; } - const int currentIndex = m_selectionManager->currentItem(); int index; if (searchFromNextItem) { + const int currentIndex = m_selectionManager->currentItem(); index = m_model->indexForKeyboardSearch(text, (currentIndex + 1) % m_model->count()); } else { - index = m_model->indexForKeyboardSearch(text, currentIndex); + index = m_model->indexForKeyboardSearch(text, 0); } if (index >= 0) { m_selectionManager->setCurrentItem(index); - m_selectionManager->clearSelection(); - m_selectionManager->setSelected(index, 1); - m_selectionManager->beginAnchoredSelection(index); + + if (m_selectionBehavior != NoSelection) { + m_selectionManager->replaceSelection(index); + m_selectionManager->beginAnchoredSelection(index); + } + m_view->scrollToItem(index); } } @@ -351,11 +490,20 @@ void KItemListController::slotAutoActivationTimeout() return; } - if (m_model->supportsDropping(index)) { + /* m_view->isUnderMouse() fixes a bug in the Folder-View-Panel and in the + * Places-Panel. + * + * Bug: When you drag a file onto a Folder-View-Item or a Places-Item and + * then move away before the auto-activation timeout triggers, than the + * item still becomes activated/expanded. + * + * See Bug 293200 and 305783 + */ + if (m_model->supportsDropping(index) && m_view->isUnderMouse()) { if (m_view->supportsItemExpanding() && m_model->isExpandable(index)) { const bool expanded = m_model->isExpanded(index); m_model->setExpanded(index, !expanded); - } else { + } else if (m_autoActivationBehavior != ExpansionOnly) { emit itemActivated(index); } } @@ -363,7 +511,7 @@ void KItemListController::slotAutoActivationTimeout() bool KItemListController::inputMethodEvent(QInputMethodEvent* event) { - Q_UNUSED(event); + Q_UNUSED(event) return false; } @@ -375,12 +523,18 @@ bool KItemListController::mousePressEvent(QGraphicsSceneMouseEvent* event, const m_pressedMousePos = transform.map(event->pos()); m_pressedIndex = m_view->itemAt(m_pressedMousePos); - if (m_pressedIndex >= 0) { - emit itemPressed(m_pressedIndex, event->button()); + emit mouseButtonPressed(m_pressedIndex, event->buttons()); + + if (event->buttons() & (Qt::BackButton | Qt::ForwardButton)) { + // Do not select items when clicking the back/forward buttons, see + // https://bugs.kde.org/show_bug.cgi?id=327412. + return true; } if (m_view->isAboveExpansionToggle(m_pressedIndex, m_pressedMousePos)) { + m_selectionManager->endAnchoredSelection(); m_selectionManager->setCurrentItem(m_pressedIndex); + m_selectionManager->beginAnchoredSelection(m_pressedIndex); return true; } @@ -411,6 +565,17 @@ bool KItemListController::mousePressEvent(QGraphicsSceneMouseEvent* event, const (!shiftOrControlPressed && !pressedItemAlreadySelected); if (clearSelection) { m_selectionManager->clearSelection(); + } else if (pressedItemAlreadySelected && !shiftOrControlPressed && (event->buttons() & Qt::LeftButton)) { + // The user might want to start dragging multiple items, but if he clicks the item + // in order to trigger it instead, the other selected items must be deselected. + // However, we do not know yet what the user is going to do. + // -> remember that the user pressed an item which had been selected already and + // clear the selection in mouseReleaseEvent(), unless the items are dragged. + m_clearSelectionIfItemsAreNotDragged = true; + + if (m_selectionManager->selectedItems().count() == 1 && m_view->isAboveText(m_pressedIndex, m_pressedMousePos)) { + emit selectedItemTextPressed(m_pressedIndex); + } } if (!shiftPressed) { @@ -418,6 +583,16 @@ bool KItemListController::mousePressEvent(QGraphicsSceneMouseEvent* event, const m_selectionManager->endAnchoredSelection(); } + if (event->buttons() & Qt::RightButton) { + // Stop rubber band from persisting after right-clicks + KItemListRubberBand* rubberBand = m_view->rubberBand(); + if (rubberBand->isActive()) { + disconnect(rubberBand, &KItemListRubberBand::endPositionChanged, this, &KItemListController::slotRubberBandChanged); + rubberBand->setActive(false); + m_view->setAutoScroll(false); + } + } + if (m_pressedIndex >= 0) { m_selectionManager->setCurrentItem(m_pressedIndex); @@ -430,7 +605,7 @@ bool KItemListController::mousePressEvent(QGraphicsSceneMouseEvent* event, const break; case MultiSelection: - if (controlPressed) { + if (controlPressed && !shiftPressed) { m_selectionManager->setSelected(m_pressedIndex, 1, KItemListSelectionManager::Toggle); m_selectionManager->beginAnchoredSelection(m_pressedIndex); } else if (!shiftPressed || !m_selectionManager->isAnchoredSelectionActive()) { @@ -480,7 +655,7 @@ bool KItemListController::mousePressEvent(QGraphicsSceneMouseEvent* event, const rubberBand->setStartPosition(startPos); rubberBand->setEndPosition(startPos); rubberBand->setActive(true); - connect(rubberBand, SIGNAL(endPositionChanged(QPointF,QPointF)), this, SLOT(slotRubberBandChanged())); + connect(rubberBand, &KItemListRubberBand::endPositionChanged, this, &KItemListController::slotRubberBandChanged); m_view->setAutoScroll(true); } @@ -503,7 +678,12 @@ bool KItemListController::mouseMoveEvent(QGraphicsSceneMouseEvent* event, const // done on the mouse-press event, but when using the selection-toggle on a // selected item the dragged item is not selected yet. m_selectionManager->setSelected(m_pressedIndex, 1, KItemListSelectionManager::Toggle); + } else { + // A selected item has been clicked to drag all selected items + // -> the selection should not be cleared when the mouse button is released. + m_clearSelectionIfItemsAreNotDragged = false; } + startDragging(); } } @@ -544,9 +724,7 @@ bool KItemListController::mouseReleaseEvent(QGraphicsSceneMouseEvent* event, con return false; } - if (m_pressedIndex >= 0) { - emit itemReleased(m_pressedIndex, event->button()); - } + emit mouseButtonReleased(m_pressedIndex, event->buttons()); const bool isAboveSelectionToggle = m_view->isAboveSelectionToggle(m_pressedIndex, m_pressedMousePos); if (isAboveSelectionToggle) { @@ -565,7 +743,7 @@ bool KItemListController::mouseReleaseEvent(QGraphicsSceneMouseEvent* event, con KItemListRubberBand* rubberBand = m_view->rubberBand(); if (rubberBand->isActive()) { - disconnect(rubberBand, SIGNAL(endPositionChanged(QPointF,QPointF)), this, SLOT(slotRubberBandChanged())); + disconnect(rubberBand, &KItemListRubberBand::endPositionChanged, this, &KItemListController::slotRubberBandChanged); rubberBand->setActive(false); m_oldSelection.clear(); m_view->setAutoScroll(false); @@ -577,6 +755,14 @@ bool KItemListController::mouseReleaseEvent(QGraphicsSceneMouseEvent* event, con if (index >= 0 && index == m_pressedIndex) { // The release event is done above the same item as the press event + if (m_clearSelectionIfItemsAreNotDragged) { + // A selected item has been clicked, but no drag operation has been started + // -> clear the rest of the selection. + m_selectionManager->clearSelection(); + m_selectionManager->setSelected(m_pressedIndex, 1, KItemListSelectionManager::Select); + m_selectionManager->beginAnchoredSelection(m_pressedIndex); + } + if (event->button() & Qt::LeftButton) { bool emitItemActivated = true; if (m_view->isAboveExpansionToggle(index, pos)) { @@ -588,19 +774,20 @@ bool KItemListController::mouseReleaseEvent(QGraphicsSceneMouseEvent* event, con } else if (shiftOrControlPressed) { // The mouse click should only update the selection, not trigger the item emitItemActivated = false; - } else if (!m_singleClickActivation) { + } else if (!(m_view->style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick) || m_singleClickActivationEnforced)) { emitItemActivated = false; } if (emitItemActivated) { emit itemActivated(index); } - } else if (event->button() & Qt::MidButton) { + } else if (event->button() & Qt::MiddleButton) { emit itemMiddleClicked(index); } } m_pressedMousePos = QPointF(); m_pressedIndex = -1; + m_clearSelectionIfItemsAreNotDragged = false; return false; } @@ -609,7 +796,31 @@ bool KItemListController::mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event, const QPointF pos = transform.map(event->pos()); const int index = m_view->itemAt(pos); - bool emitItemActivated = !m_singleClickActivation && + // Expand item if desired - See Bug 295573 + if (m_mouseDoubleClickAction != ActivateItemOnly) { + if (m_view && m_model && m_view->supportsItemExpanding() && m_model->isExpandable(index)) { + const bool expanded = m_model->isExpanded(index); + m_model->setExpanded(index, !expanded); + } + } + + if (event->button() & Qt::RightButton) { + m_selectionManager->clearSelection(); + if (index >= 0) { + m_selectionManager->setSelected(index); + emit itemContextMenuRequested(index, event->screenPos()); + } else { + const QRectF headerBounds = m_view->headerBoundaries(); + if (headerBounds.contains(event->pos())) { + emit headerContextMenuRequested(event->screenPos()); + } else { + emit viewContextMenuRequested(event->screenPos()); + } + } + return true; + } + + bool emitItemActivated = !(m_view->style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick) || m_singleClickActivationEnforced) && (event->button() & Qt::LeftButton) && index >= 0 && index < m_model->count(); if (emitItemActivated) { @@ -620,25 +831,39 @@ bool KItemListController::mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event, bool KItemListController::dragEnterEvent(QGraphicsSceneDragDropEvent* event, const QTransform& transform) { - Q_UNUSED(event); - Q_UNUSED(transform); + Q_UNUSED(event) + Q_UNUSED(transform) + + DragAndDropHelper::clearUrlListMatchesUrlCache(); + return false; } bool KItemListController::dragLeaveEvent(QGraphicsSceneDragDropEvent* event, const QTransform& transform) { - Q_UNUSED(event); - Q_UNUSED(transform); + Q_UNUSED(event) + Q_UNUSED(transform) + + m_autoActivationTimer->stop(); + m_view->setAutoScroll(false); + m_view->hideDropIndicator(); + + KItemListWidget* widget = hoveredWidget(); + if (widget) { + widget->setHovered(false); + emit itemUnhovered(widget->index()); + } return false; } bool KItemListController::dragMoveEvent(QGraphicsSceneDragDropEvent* event, const QTransform& transform) { - Q_UNUSED(transform); if (!m_model || !m_view) { return false; } + + QUrl hoveredDir = m_model->directory(); KItemListWidget* oldHoveredWidget = hoveredWidget(); const QPointF pos = transform.map(event->pos()); @@ -651,50 +876,98 @@ bool KItemListController::dragMoveEvent(QGraphicsSceneDragDropEvent* event, cons oldHoveredWidget->setHovered(false); emit itemUnhovered(oldHoveredWidget->index()); } + } - if (newHoveredWidget) { - const int index = newHoveredWidget->index(); + if (newHoveredWidget) { + bool droppingBetweenItems = false; + if (m_model->sortRole().isEmpty()) { + // The model supports inserting items between other items. + droppingBetweenItems = (m_view->showDropIndicator(pos) >= 0); + } + + const int index = newHoveredWidget->index(); + + if (m_model->isDir(index)) { + hoveredDir = m_model->url(index); + } + + if (!droppingBetweenItems) { if (m_model->supportsDropping(index)) { - newHoveredWidget->setHovered(true); - } - emit itemHovered(index); + // Something has been dragged on an item. + m_view->hideDropIndicator(); + if (!newHoveredWidget->isHovered()) { + newHoveredWidget->setHovered(true); + emit itemHovered(index); + } - if (m_autoActivationTimer->interval() >= 0) { - m_autoActivationTimer->setProperty("index", index); - m_autoActivationTimer->start(); + if (!m_autoActivationTimer->isActive() && m_autoActivationTimer->interval() >= 0) { + m_autoActivationTimer->setProperty("index", index); + m_autoActivationTimer->start(); + } + } + } else { + m_autoActivationTimer->stop(); + if (newHoveredWidget && newHoveredWidget->isHovered()) { + newHoveredWidget->setHovered(false); + emit itemUnhovered(index); } } + } else { + m_view->hideDropIndicator(); } + if (DragAndDropHelper::urlListMatchesUrl(event->mimeData()->urls(), hoveredDir)) { + event->setDropAction(Qt::IgnoreAction); + event->ignore(); + } else { + event->setDropAction(event->proposedAction()); + event->accept(); + } return false; } bool KItemListController::dropEvent(QGraphicsSceneDragDropEvent* event, const QTransform& transform) { - Q_UNUSED(transform) if (!m_view) { return false; } m_autoActivationTimer->stop(); + m_view->setAutoScroll(false); const QPointF pos = transform.map(event->pos()); - const int index = m_view->itemAt(pos); - emit itemDropEvent(index, event); + + int dropAboveIndex = -1; + if (m_model->sortRole().isEmpty()) { + // The model supports inserting of items between other items. + dropAboveIndex = m_view->showDropIndicator(pos); + } + + if (dropAboveIndex >= 0) { + // Something has been dropped between two items. + m_view->hideDropIndicator(); + emit aboveItemDropEvent(dropAboveIndex, event); + } else if (!event->mimeData()->hasFormat(m_model->blacklistItemDropEventMimeType())) { + // Something has been dropped on an item or on an empty part of the view. + emit itemDropEvent(m_view->itemAt(pos), event); + } + + QAccessibleEvent accessibilityEvent(view(), QAccessible::DragDropEnd); + QAccessible::updateAccessibility(&accessibilityEvent); return true; } bool KItemListController::hoverEnterEvent(QGraphicsSceneHoverEvent* event, const QTransform& transform) { - Q_UNUSED(event); - Q_UNUSED(transform); + Q_UNUSED(event) + Q_UNUSED(transform) return false; } bool KItemListController::hoverMoveEvent(QGraphicsSceneHoverEvent* event, const QTransform& transform) { - Q_UNUSED(transform); + Q_UNUSED(transform) if (!m_model || !m_view) { return false; } @@ -711,8 +984,13 @@ bool KItemListController::hoverMoveEvent(QGraphicsSceneHoverEvent* event, const if (newHoveredWidget) { newHoveredWidget->setHovered(true); + const QPointF mappedPos = newHoveredWidget->mapFromItem(m_view, pos); + newHoveredWidget->setHoverPosition(mappedPos); emit itemHovered(newHoveredWidget->index()); } + } else if (oldHoveredWidget) { + const QPointF mappedPos = oldHoveredWidget->mapFromItem(m_view, pos); + oldHoveredWidget->setHoverPosition(mappedPos); } return false; @@ -720,8 +998,8 @@ bool KItemListController::hoverMoveEvent(QGraphicsSceneHoverEvent* event, const bool KItemListController::hoverLeaveEvent(QGraphicsSceneHoverEvent* event, const QTransform& transform) { - Q_UNUSED(event); - Q_UNUSED(transform); + Q_UNUSED(event) + Q_UNUSED(transform) if (!m_model || !m_view) { return false; @@ -738,15 +1016,15 @@ bool KItemListController::hoverLeaveEvent(QGraphicsSceneHoverEvent* event, const bool KItemListController::wheelEvent(QGraphicsSceneWheelEvent* event, const QTransform& transform) { - Q_UNUSED(event); - Q_UNUSED(transform); + Q_UNUSED(event) + Q_UNUSED(transform) return false; } bool KItemListController::resizeEvent(QGraphicsSceneResizeEvent* event, const QTransform& transform) { - Q_UNUSED(event); - Q_UNUSED(transform); + Q_UNUSED(event) + Q_UNUSED(transform) return false; } @@ -846,7 +1124,7 @@ void KItemListController::slotRubberBandChanged() } } - QSet selectedItems; + KItemSet selectedItems; // Select all visible items that intersect with the rubberband foreach (const KItemListWidget* widget, m_view->visibleItemListWidgets()) { @@ -894,7 +1172,7 @@ void KItemListController::slotRubberBandChanged() // Therefore, the new selection contains: // 1. All previously selected items which are not inside the rubberband, and // 2. all items inside the rubberband which have not been selected previously. - m_selectionManager->setSelectedItems((m_oldSelection - selectedItems) + (selectedItems - m_oldSelection)); + m_selectionManager->setSelectedItems(m_oldSelection ^ selectedItems); } else { m_selectionManager->setSelectedItems(selectedItems + m_oldSelection); @@ -907,7 +1185,7 @@ void KItemListController::startDragging() return; } - const QSet selectedItems = m_selectionManager->selectedItems(); + const KItemSet selectedItems = m_selectionManager->selectedItems(); if (selectedItems.isEmpty()) { return; } @@ -925,7 +1203,13 @@ void KItemListController::startDragging() const QPixmap pixmap = m_view->createDragPixmap(selectedItems); drag->setPixmap(pixmap); - drag->exec(Qt::MoveAction | Qt::CopyAction | Qt::LinkAction, Qt::CopyAction); + const QPoint hotSpot((pixmap.width() / pixmap.devicePixelRatio()) / 2, 0); + drag->setHotSpot(hotSpot); + + drag->exec(Qt::MoveAction | Qt::CopyAction | Qt::LinkAction, Qt::MoveAction); + + QAccessibleEvent accessibilityEvent(view(), QAccessible::DragDropStart); + QAccessible::updateAccessibility(&accessibilityEvent); } KItemListWidget* KItemListController::hoveredWidget() const @@ -938,7 +1222,7 @@ KItemListWidget* KItemListController::hoveredWidget() const } } - return 0; + return nullptr; } KItemListWidget* KItemListController::widgetForPos(const QPointF& pos) const @@ -955,7 +1239,7 @@ KItemListWidget* KItemListController::widgetForPos(const QPointF& pos) const } } - return 0; + return nullptr; } void KItemListController::updateKeyboardAnchor() @@ -970,24 +1254,23 @@ void KItemListController::updateKeyboardAnchor() } } -int KItemListController::nextRowIndex() const +int KItemListController::nextRowIndex(int index) const { - const int currentIndex = m_selectionManager->currentItem(); if (m_keyboardAnchorIndex < 0) { - return currentIndex; + return index; } const int maxIndex = m_model->count() - 1; - if (currentIndex == maxIndex) { - return currentIndex; + if (index == maxIndex) { + return index; } // Calculate the index of the last column inside the row of the current index - int lastColumnIndex = currentIndex; + int lastColumnIndex = index; while (keyboardAnchorPos(lastColumnIndex + 1) > keyboardAnchorPos(lastColumnIndex)) { ++lastColumnIndex; if (lastColumnIndex >= maxIndex) { - return currentIndex; + return index; } } @@ -1008,19 +1291,18 @@ int KItemListController::nextRowIndex() const return nextRowIndex; } -int KItemListController::previousRowIndex() const +int KItemListController::previousRowIndex(int index) const { - const int currentIndex = m_selectionManager->currentItem(); - if (m_keyboardAnchorIndex < 0 || currentIndex == 0) { - return currentIndex; + if (m_keyboardAnchorIndex < 0 || index == 0) { + return index; } // Calculate the index of the first column inside the row of the current index - int firstColumnIndex = currentIndex; + int firstColumnIndex = index; while (keyboardAnchorPos(firstColumnIndex - 1) < keyboardAnchorPos(firstColumnIndex)) { --firstColumnIndex; if (firstColumnIndex <= 0) { - return currentIndex; + return index; } } @@ -1051,4 +1333,15 @@ qreal KItemListController::keyboardAnchorPos(int index) const return 0; } -#include "kitemlistcontroller.moc" +void KItemListController::updateExtendedSelectionRegion() +{ + if (m_view) { + const bool extend = (m_selectionBehavior != MultiSelection); + KItemListStyleOption option = m_view->styleOption(); + if (option.extendedSelectionRegion != extend) { + option.extendedSelectionRegion = extend; + m_view->setStyleOption(option); + } + } +} +