#include <QGraphicsSceneEvent>
#include <QGraphicsView>
#include <QMimeData>
+#include <QStyleHints>
#include <QTimer>
#include <QTouchEvent>
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),
m_pressedIndex(std::nullopt),
m_pressedMousePos(),
m_autoActivationTimer(nullptr),
+ m_longPressDetectionTimer(nullptr),
m_swipeGesture(Qt::CustomGesture),
m_twoFingerTapGesture(Qt::CustomGesture),
m_oldSelection(),
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 selectionModeChangeRequested(true);
+ }
+ });
+
setModel(model);
setView(view);
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();
}
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();
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
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;
// 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();
}
}
}
} 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
}
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;
+ 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:
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.
} 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;