X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/blobdiff_plain/3ccc0420a694deeb0d236c7b39ce569bd074f3e8..e19aa49a082076d5ae69931893c1d47b98f85e30:/src/kitemviews/kitemlistcontroller.cpp diff --git a/src/kitemviews/kitemlistcontroller.cpp b/src/kitemviews/kitemlistcontroller.cpp index 966dc822b..7297f8aaa 100644 --- a/src/kitemviews/kitemlistcontroller.cpp +++ b/src/kitemviews/kitemlistcontroller.cpp @@ -17,6 +17,7 @@ #include #include +#include #include #include @@ -32,6 +33,7 @@ KItemListController::KItemListController(KItemModelBase* model, KItemListView* view, QObject* parent) : QObject(parent), m_singleClickActivationEnforced(false), + m_selectionMode(false), m_selectionTogglePressed(false), m_clearSelectionIfItemsAreNotDragged(false), m_isSwipeGesture(false), @@ -219,6 +221,16 @@ bool KItemListController::singleClickActivationEnforced() const return m_singleClickActivationEnforced; } +void KItemListController::setSelectionModeEnabled(bool enabled) +{ + m_selectionMode = enabled; +} + +bool KItemListController::selectionMode() const +{ + return m_selectionMode; +} + bool KItemListController::keyPressEvent(QKeyEvent* event) { int index = m_selectionManager->currentItem(); @@ -407,7 +419,9 @@ bool KItemListController::keyPressEvent(QKeyEvent* event) } case Qt::Key_Escape: - if (m_selectionBehavior != SingleSelection) { + if (m_selectionMode) { + Q_EMIT selectionModeChangeRequested(false); + } else if (m_selectionBehavior != SingleSelection) { m_selectionManager->clearSelection(); } m_keyboardManager->cancelSearch(); @@ -575,10 +589,11 @@ bool KItemListController::mouseMoveEvent(QGraphicsSceneMouseEvent* event, const return false; } + const QPointF pos = transform.map(event->pos()); + if (m_pressedIndex.has_value() && !m_view->rubberBand()->isActive()) { // Check whether a dragging should be started if (event->buttons() & Qt::LeftButton) { - const QPointF pos = transform.map(event->pos()); if ((pos - m_pressedMousePos).manhattanLength() >= QApplication::startDragDistance()) { if (!m_selectionManager->isSelected(m_pressedIndex.value())) { // Always assure that the dragged item gets selected. Usually this is already @@ -724,7 +739,7 @@ bool KItemListController::dragMoveEvent(QGraphicsSceneDragDropEvent* event, cons KItemListWidget* oldHoveredWidget = hoveredWidget(); const QPointF pos = transform.map(event->pos()); - KItemListWidget* newHoveredWidget = widgetForPos(pos); + KItemListWidget* newHoveredWidget = widgetForDropPos(pos); if (oldHoveredWidget != newHoveredWidget) { m_autoActivationTimer->stop(); @@ -806,7 +821,12 @@ bool KItemListController::dropEvent(QGraphicsSceneDragDropEvent* event, const QT Q_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. - Q_EMIT itemDropEvent(m_view->itemAt(pos).value_or(-1), event); + const KItemListWidget *receivingWidget = widgetForDropPos(pos); + if (receivingWidget) { + Q_EMIT itemDropEvent(receivingWidget->index(), event); + } else { + Q_EMIT itemDropEvent(-1, event); + } } QAccessibleEvent accessibilityEvent(view(), QAccessible::DragDropEnd); @@ -872,7 +892,8 @@ bool KItemListController::hoverMoveEvent(QGraphicsSceneHoverEvent* event, const newHoveredWidget->setExpansionAreaHovered(true); } else { // make sure we unhover the old one first if old!=new - if (auto oldHoveredWidget = hoveredWidget(); oldHoveredWidget && oldHoveredWidget != newHoveredWidget) { + auto oldHoveredWidget = hoveredWidget(); + if (oldHoveredWidget && oldHoveredWidget != newHoveredWidget) { oldHoveredWidget->setHovered(false); Q_EMIT itemUnhovered(oldHoveredWidget->index()); } @@ -889,9 +910,11 @@ bool KItemListController::hoverMoveEvent(QGraphicsSceneHoverEvent* event, const // (no-op in this branch for masked hover) } else { - newHoveredWidget->setHovered(true); newHoveredWidget->setHoverPosition(mappedPos); - Q_EMIT itemHovered(newHoveredWidget->index()); + if (oldHoveredWidget != newHoveredWidget) { + newHoveredWidget->setHovered(true); + Q_EMIT itemHovered(newHoveredWidget->index()); + } } } } else { @@ -1238,7 +1261,7 @@ void KItemListController::slotRubberBandChanged() // been activated in case if no Shift- or Control-key are pressed const bool shiftOrControlPressed = QApplication::keyboardModifiers() & Qt::ShiftModifier || QApplication::keyboardModifiers() & Qt::ControlModifier; - if (!shiftOrControlPressed) { + if (!shiftOrControlPressed && !m_selectionMode) { m_oldSelection.clear(); } } @@ -1287,7 +1310,7 @@ void KItemListController::slotRubberBandChanged() } } while (!selectionFinished); - if (QApplication::keyboardModifiers() & Qt::ControlModifier) { + if ((QApplication::keyboardModifiers() & Qt::ControlModifier) || m_selectionMode) { // If Control is pressed, the selection state of all items in the rubberband is toggled. // Therefore, the new selection contains: // 1. All previously selected items which are not inside the rubberband, and @@ -1310,10 +1333,11 @@ void KItemListController::startDragging() return; } - QMimeData* data = m_model->createMimeData(selectedItems); + QMimeData *data = m_model->createMimeData(selectedItems); if (!data) { return; } + KUrlMimeData::exportUrlsToPortal(data); // The created drag object will be owned and deleted // by QApplication::activeWindow(). @@ -1361,6 +1385,21 @@ KItemListWidget* KItemListController::widgetForPos(const QPointF& pos) const return nullptr; } +KItemListWidget* KItemListController::widgetForDropPos(const QPointF& pos) const +{ + Q_ASSERT(m_view); + + const auto widgets = m_view->visibleItemListWidgets(); + for (KItemListWidget* widget : widgets) { + const QPointF mappedPos = widget->mapFromItem(m_view, pos); + if (widget->contains(mappedPos)) { + return widget; + } + } + + return nullptr; +} + void KItemListController::updateKeyboardAnchor() { const bool validAnchor = m_keyboardAnchorIndex >= 0 && @@ -1493,7 +1532,9 @@ bool KItemListController::onPress(const QPoint& screenPos, const QPointF& pos, c } const bool shiftPressed = modifiers & Qt::ShiftModifier; - const bool controlPressed = modifiers & Qt::ControlModifier; + const bool controlPressed = (modifiers & Qt::ControlModifier) || m_selectionMode; // Keeping selectionMode similar to pressing control will hopefully + // simplify the overall logic and possibilities both for users and devs. + const bool leftClick = buttons & Qt::LeftButton; const bool rightClick = buttons & Qt::RightButton; // The previous selection is cleared if either @@ -1539,7 +1580,7 @@ bool KItemListController::onPress(const QPoint& screenPos, const QPointF& pos, c return false; } } - } else if (pressedItemAlreadySelected && !shiftOrControlPressed && (buttons & Qt::LeftButton)) { + } else if (pressedItemAlreadySelected && !shiftOrControlPressed && leftClick) { // 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. @@ -1575,6 +1616,7 @@ bool KItemListController::onPress(const QPoint& screenPos, const QPointF& pos, c } if (m_pressedIndex.has_value()) { + // The hover highlight area of an item is being pressed. m_selectionManager->setCurrentItem(m_pressedIndex.value()); const auto row = m_view->m_visibleItems.value(m_pressedIndex.value()); // anything outside of row.contains() will be the empty region of the row rect const bool hitTargetIsRowEmptyRegion = !row->contains(row->mapFromItem(m_view, pos)); @@ -1583,8 +1625,15 @@ bool KItemListController::onPress(const QPoint& screenPos, const QPointF& pos, c bool createRubberBand = (hitTargetIsRowEmptyRegion && m_selectionManager->selectedItems().isEmpty()); if (rightClick && hitTargetIsRowEmptyRegion) { - // we got a right click outside the text rect, default to action on the current url and not the pressed item - Q_EMIT itemContextMenuRequested(m_pressedIndex.value(), screenPos); + // We have a right click outside the icon and text rect but within the hover highlight area + // but it is unclear if this means that a selection rectangle for an item was clicked or the background of the view. + if (m_selectionManager->selectedItems().contains(m_pressedIndex.value())) { + // The selection rectangle for an item was clicked + Q_EMIT itemContextMenuRequested(m_pressedIndex.value(), screenPos); + } else { + row->setHovered(false); // Removes the hover highlight so the context menu doesn't look like it applies to the row. + Q_EMIT viewContextMenuRequested(screenPos); + } return true; } @@ -1597,10 +1646,21 @@ bool KItemListController::onPress(const QPoint& screenPos, const QPointF& pos, c break; case MultiSelection: - if (controlPressed && !shiftPressed) { - m_selectionManager->setSelected(m_pressedIndex.value(), 1, KItemListSelectionManager::Toggle); - m_selectionManager->beginAnchoredSelection(m_pressedIndex.value()); - createRubberBand = false; // multi selection, don't propagate any further + if (controlPressed && !shiftPressed && leftClick) { + // A left mouse button press is happening on an item while control is pressed. This either means a user wants to: + // - toggle the selection of item(s) or + // - they want to begin a drag on the item(s) to copy them. + // We rule out the latter, if the item is not clicked directly and was unselected previously. + const auto row = m_view->m_visibleItems.value(m_pressedIndex.value()); + const auto mappedPos = row->mapFromItem(m_view, pos); + if (!row->iconRect().contains(mappedPos) && !row->textRect().contains(mappedPos) && !pressedItemAlreadySelected) { + createRubberBand = true; + } else { + m_selectionManager->setSelected(m_pressedIndex.value(), 1, KItemListSelectionManager::Toggle); + m_selectionManager->beginAnchoredSelection(m_pressedIndex.value()); + createRubberBand = false; // multi selection, don't propagate any further + // This will be the start of an item drag-to-copy operation if the user now moves the mouse before releasing the mouse button. + } } else if (!shiftPressed || !m_selectionManager->isAnchoredSelectionActive()) { // Select the pressed item and start a new anchored selection m_selectionManager->setSelected(m_pressedIndex.value(), 1, KItemListSelectionManager::Select); @@ -1702,7 +1762,7 @@ bool KItemListController::onRelease(const QPointF& pos, const Qt::KeyboardModifi } else { const bool singleClickActivation = m_view->style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick) || m_singleClickActivationEnforced; if (!singleClickActivation) { - emitItemActivated = touch; + emitItemActivated = touch && !m_selectionMode; } else { // activate on single click only if we didn't come from a rubber band release emitItemActivated = !rubberBandRelease;