From 3b7c05b385dc56fbc0b9ffdd332f8d30e7624d0c Mon Sep 17 00:00:00 2001 From: Felix Ernst Date: Sun, 12 Sep 2021 15:33:39 +0200 Subject: Add Selection Mode The selection mode action is a checkable toggle action named "Select Files and Folders" which has "Space" as the default shortcut. In selection mode a bottom bar with contextual actions is shown. These should mostly mirror the actions which are available through the right-click context menu aka DolphinContextMenu. Resizing of the window might make a overflow button appear in the bottom selection mode bar. This commit makes press and hold in the view activate selection mode. This behaviour is not triggered if the press and hold is used to either start a rubberband selection or a drag operation within a short time. The length of the short timeframe is defined by a QStyleHint. This is currently not implemented in touch because I can't test it. Mix the selection mode bars' background colors using a nice combination of colors from the current color scheme BUG: 427202 --- src/kitemviews/kitemlistcontroller.cpp | 42 ++++++++++++++++++++++++++++++---- src/kitemviews/kitemlistcontroller.h | 13 +++++++++++ 2 files changed, 50 insertions(+), 5 deletions(-) (limited to 'src/kitemviews') diff --git a/src/kitemviews/kitemlistcontroller.cpp b/src/kitemviews/kitemlistcontroller.cpp index 3d83bc914..03ee5cfe6 100644 --- a/src/kitemviews/kitemlistcontroller.cpp +++ b/src/kitemviews/kitemlistcontroller.cpp @@ -27,12 +27,14 @@ #include #include #include +#include #include #include 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), @@ -51,6 +53,7 @@ KItemListController::KItemListController(KItemModelBase* model, KItemListView* v m_pressedIndex(std::nullopt), m_pressedMousePos(), m_autoActivationTimer(nullptr), + m_longPressDetectionTimer(nullptr), m_swipeGesture(Qt::CustomGesture), m_twoFingerTapGesture(Qt::CustomGesture), m_oldSelection(), @@ -69,6 +72,15 @@ KItemListController::KItemListController(KItemModelBase* model, KItemListView* v m_autoActivationTimer->setInterval(-1); connect(m_autoActivationTimer, &QTimer::timeout, this, &KItemListController::slotAutoActivationTimeout); + m_longPressDetectionTimer = new QTimer(this); + m_longPressDetectionTimer->setSingleShot(true); + m_longPressDetectionTimer->setInterval(QGuiApplication::styleHints()->mousePressAndHoldInterval()); + connect(m_longPressDetectionTimer, &QTimer::timeout, this, [this]() { + if (!m_selectionMode) { + Q_EMIT selectionModeRequested(); + } + }); + setModel(model); setView(view); @@ -220,6 +232,16 @@ bool KItemListController::singleClickActivationEnforced() const return m_singleClickActivationEnforced; } +void KItemListController::setSelectionMode(bool enabled) +{ + m_selectionMode = enabled; +} + +bool KItemListController::selectionMode() const +{ + return m_selectionMode; +} + bool KItemListController::keyPressEvent(QKeyEvent* event) { int index = m_selectionManager->currentItem(); @@ -576,10 +598,14 @@ bool KItemListController::mouseMoveEvent(QGraphicsSceneMouseEvent* event, const return false; } + const QPointF pos = transform.map(event->pos()); + if ((pos - m_pressedMousePos).manhattanLength() >= QApplication::startDragDistance()) { + m_longPressDetectionTimer->stop(); + } + 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 @@ -639,6 +665,8 @@ bool KItemListController::mouseReleaseEvent(QGraphicsSceneMouseEvent* event, con m_view->m_tapAndHoldIndicator->setActive(false); } + m_longPressDetectionTimer->stop(); + KItemListRubberBand* rubberBand = m_view->rubberBand(); if (event->source() == Qt::MouseEventSynthesizedByQt && !rubberBand->isActive() && m_isTouchEvent) { return false; @@ -1247,7 +1275,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(); } } @@ -1296,7 +1324,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 @@ -1518,10 +1546,14 @@ 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; const bool leftClick = buttons & Qt::LeftButton; const bool rightClick = buttons & Qt::RightButton; + if (leftClick) { + m_longPressDetectionTimer->start(); + } + // The previous selection is cleared if either // 1. The selection mode is SingleSelection, or // 2. the selection mode is MultiSelection, and *none* of the following conditions are met: @@ -1565,7 +1597,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. diff --git a/src/kitemviews/kitemlistcontroller.h b/src/kitemviews/kitemlistcontroller.h index d2e56eb64..a3d952de5 100644 --- a/src/kitemviews/kitemlistcontroller.h +++ b/src/kitemviews/kitemlistcontroller.h @@ -126,6 +126,9 @@ public: void setSingleClickActivationEnforced(bool singleClick); bool singleClickActivationEnforced() const; + void setSelectionMode(bool enabled); + bool selectionMode() const; + bool processEvent(QEvent* event, const QTransform& transform); Q_SIGNALS: @@ -209,6 +212,14 @@ Q_SIGNALS: */ void escapePressed(); + /** + * Is emitted if left click is pressed down for a long time without moving the cursor too much. + * Moving the cursor would either trigger an item drag if the click was initiated on top of an item + * or a selection rectangle if the click was not initiated on top of an item. + * So long press is only emitted if there wasn't a lot of cursor movement. + */ + void selectionModeRequested(); + void modelChanged(KItemModelBase* current, KItemModelBase* previous); void viewChanged(KItemListView* current, KItemListView* previous); @@ -325,6 +336,7 @@ private: private: bool m_singleClickActivationEnforced; + bool m_selectionMode; bool m_selectionTogglePressed; bool m_clearSelectionIfItemsAreNotDragged; bool m_isSwipeGesture; @@ -344,6 +356,7 @@ private: QPointF m_pressedMousePos; QTimer* m_autoActivationTimer; + QTimer* m_longPressDetectionTimer; Qt::GestureType m_swipeGesture; Qt::GestureType m_twoFingerTapGesture; -- cgit v1.3