m_autoActivationTimer = new QTimer(this);
m_autoActivationTimer->setSingleShot(true);
- m_autoActivationTimer->setInterval(-1);
+ m_autoActivationTimer->setInterval(750);
connect(m_autoActivationTimer, &QTimer::timeout, this, &KItemListController::slotAutoActivationTimeout);
setModel(model);
return -1;
}
-void KItemListController::setAutoActivationDelay(int delay)
+void KItemListController::setAutoActivationEnabled(bool enabled)
{
- m_autoActivationTimer->setInterval(delay);
+ m_autoActivationEnabled = enabled;
}
-int KItemListController::autoActivationDelay() const
+bool KItemListController::isAutoActivationEnabled() const
{
- return m_autoActivationTimer->interval();
+ return m_autoActivationEnabled;
}
void KItemListController::setSingleClickActivationEnforced(bool singleClick)
const bool horizontalScrolling = m_view->scrollOrientation() == Qt::Horizontal;
+ if (m_view->layoutDirection() == Qt::RightToLeft) {
+ // swap left and right arrow keys
+ switch (key) {
+ case Qt::Key_Left:
+ key = Qt::Key_Right;
+ break;
+ case Qt::Key_Right:
+ key = Qt::Key_Left;
+ break;
+ default:
+ break;
+ }
+ }
+
// Handle the expanding/collapsing of items
// expand / collapse all selected directories
if (m_view->supportsItemExpanding() && m_model->isExpandable(index) && (key == Qt::Key_Right || key == Qt::Key_Left)) {
}
const bool controlPressed = event->modifiers() & Qt::ControlModifier;
- const bool shiftOrControlPressed = shiftPressed || controlPressed;
+ if (m_selectionMode && !controlPressed && !shiftPressed && (key == Qt::Key_Enter || key == Qt::Key_Return)) {
+ key = Qt::Key_Space; // In selection mode one moves around with arrow keys and toggles selection with Enter.
+ }
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;
}
}
- if (m_view->layoutDirection() == Qt::RightToLeft) {
- if (horizontalScrolling) {
- // swap up and down arrow keys
- switch (key) {
- case Qt::Key_Up:
- key = Qt::Key_Down;
- break;
- case Qt::Key_Down:
- key = Qt::Key_Up;
- break;
- default:
- break;
- }
- } else if (!m_view->supportsItemExpanding()) {
- // swap left and right arrow keys
- switch (key) {
- case Qt::Key_Left:
- key = Qt::Key_Right;
- break;
- case Qt::Key_Right:
- key = Qt::Key_Left;
- break;
- default:
- break;
- }
- }
- }
-
const bool selectSingleItem = m_selectionBehavior != NoSelection && itemCount == 1 && navigationPressed;
if (selectSingleItem) {
case Qt::Key_Space:
if (m_selectionBehavior == MultiSelection) {
- if (controlPressed) {
+ if (controlPressed || m_selectionMode) {
// Toggle the selection state of the current item.
m_selectionManager->endAnchoredSelection();
m_selectionManager->setSelected(index, 1, KItemListSelectionManager::Toggle);
break;
case MultiSelection:
- if (controlPressed) {
+ if (controlPressed || (m_selectionMode && !shiftPressed)) {
m_selectionManager->endAnchoredSelection();
}
m_selectionManager->setCurrentItem(index);
- if (!shiftOrControlPressed) {
+ if (!shiftPressed && !controlPressed && !m_selectionMode) {
m_selectionManager->clearSelection();
m_selectionManager->setSelected(index, 1);
}
return;
}
int index;
- if (searchFromNextItem) {
- const int currentIndex = m_selectionManager->currentItem();
- index = m_model->indexForKeyboardSearch(text, (currentIndex + 1) % m_model->count());
+ // In selection mode, always use the current (underlined) item, or the next item, for search start position.
+ if (m_selectionBehavior == NoSelection || m_selectionMode || m_selectionManager->hasSelection()) {
+ index = m_model->indexForKeyboardSearch(text, searchFromNextItem ? m_selectionManager->currentItem() + 1 : m_selectionManager->currentItem());
} else {
index = m_model->indexForKeyboardSearch(text, 0);
}
if (index >= 0) {
+ if (m_selectionMode) {
+ m_selectionManager->endAnchoredSelection();
+ }
+
m_selectionManager->setCurrentItem(index);
if (m_selectionBehavior != NoSelection) {
- m_selectionManager->replaceSelection(index);
+ if (!m_selectionMode) { // Don't clear the selection in selection mode.
+ m_selectionManager->replaceSelection(index);
+ }
m_selectionManager->beginAnchoredSelection(index);
}
return false;
}
+ for (KItemListWidget *widget : m_view->visibleItemListWidgets()) {
+ widget->setPressed(false);
+ }
+
if (m_view->m_tapAndHoldIndicator->isActive()) {
m_view->m_tapAndHoldIndicator->setActive(false);
}
const QPointF pos = transform.map(event->pos());
const std::optional<int> index = m_view->itemAt(pos);
+ if (event->button() & (Qt::ForwardButton | Qt::BackButton)) {
+ // "Forward" and "Back" are reserved for quickly navigating through the
+ // history. Double-clicking those buttons should be interpreted as two
+ // separate button presses. We arrive here for the second click, which
+ // we now react to just as we would for a singular click
+ Q_EMIT mouseButtonPressed(index.value_or(-1), event->button());
+ return false;
+ }
+
if (!index.has_value()) {
Q_EMIT doubleClickViewBackground(event->button());
return false;
}
}
- if (event->button() & Qt::RightButton) {
+ if (event->button() & ~Qt::LeftButton) {
return false;
}
Q_EMIT itemHovered(index);
}
- if (!m_autoActivationTimer->isActive() && m_autoActivationTimer->interval() >= 0 && m_model->canEnterOnHover(index)) {
+ if (m_autoActivationEnabled && !m_autoActivationTimer->isActive() && m_model->canEnterOnHover(index)) {
m_autoActivationTimer->setProperty("index", index);
m_autoActivationTimer->start();
newHoveredWidget->startActivateSoonAnimation(m_autoActivationTimer->remainingTime());
// we also unhover any old expansion toggle hovers, in case the mouse movement from expansion toggle to icon+text is too fast (i.e. newHoveredWidget is never null between the transition)
unhoverOldExpansionWidget();
- const bool isOverIconAndText = newHoveredWidget->iconRect().contains(mappedPos) || newHoveredWidget->textRect().contains(mappedPos);
+ const bool isOverIconAndText = newHoveredWidget->selectionRectCore().contains(mappedPos);
const bool hasMultipleSelection = m_selectionManager->selectedItems().count() > 1;
if (hasMultipleSelection && !isOverIconAndText) {
const auto widgets = m_view->visibleItemListWidgets();
for (KItemListWidget *widget : widgets) {
+ widget->setPressed(false);
if (widget->isHovered()) {
widget->setHovered(false);
Q_EMIT itemUnhovered(widget->index());
const QRectF widgetRect = m_view->itemRect(index);
if (widgetRect.intersects(rubberBandRect)) {
// Select the full row intersecting with the rubberband rectangle
- const QRectF selectionRect = widget->selectionRect().translated(widgetRect.topLeft());
- const QRectF iconRect = widget->iconRect().translated(widgetRect.topLeft());
- if (selectionRect.intersects(rubberBandRect) || iconRect.intersects(rubberBandRect)) {
+ const QRectF selectionRect = widget->selectionRectFull().translated(widgetRect.topLeft());
+ if (selectionRect.intersects(rubberBandRect)) {
selectedItems.insert(index);
}
}
const auto widgets = m_view->visibleItemListWidgets();
for (KItemListWidget *widget : widgets) {
const QPointF mappedPos = widget->mapFromItem(m_view, pos);
- if (widget->contains(mappedPos) || widget->selectionRect().contains(mappedPos)) {
+ if (widget->contains(mappedPos)) {
return widget;
}
}
const auto widgets = m_view->visibleItemListWidgets();
for (KItemListWidget *widget : widgets) {
const QPointF mappedPos = widget->mapFromItem(m_view, pos);
- if (widget->contains(mappedPos)) {
+ if (widget->selectionRectCore().contains(mappedPos)) {
return widget;
}
}
// simplify the overall logic and possibilities both for users and devs.
const bool leftClick = buttons & Qt::LeftButton;
const bool rightClick = buttons & Qt::RightButton;
+ const bool singleClickActivation = m_view->style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick);
// The previous selection is cleared if either
// 1. The selection mode is SingleSelection, or
if (selectedItemsCount > 1 && m_pressedIndex.has_value()) {
const auto row = m_view->m_visibleItems.value(m_pressedIndex.value());
const auto mappedPos = row->mapFromItem(m_view, pos);
- if (pressedItemAlreadySelected || row->iconRect().contains(mappedPos) || row->textRect().contains(mappedPos)) {
+ if (row->selectionRectCore().contains(mappedPos)) {
// we are indeed inside the text/icon rect, keep m_pressedIndex what it is
// and short-circuit for single-click activation (it will then propagate to onRelease and activate the item)
// or we just keep going for double-click activation
- if (m_view->style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick) || m_singleClickActivationEnforced) {
+ if (singleClickActivation || m_singleClickActivationEnforced) {
if (!pressedItemAlreadySelected) {
// An unselected item was clicked directly while deselecting multiple other items so we mark it "current".
m_selectionManager->setCurrentItem(m_pressedIndex.value());
m_selectionManager->setSelected(m_pressedIndex.value(), 1, KItemListSelectionManager::Toggle);
}
}
+ if (leftClick) {
+ row->setPressed(true);
+ }
return true; // event handled, don't create rubber band
}
} else {
if (m_pressedIndex.has_value()) {
// The hover highlight area of an item is being pressed.
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));
+
+ const bool hitTargetIsRowEmptyRegion = !row->selectionRectCore().contains(row->mapFromItem(m_view, pos));
// again, when this method returns false, a rubberBand selection is created as the event is not consumed;
// createRubberBand here tells us whether to return true or false.
bool createRubberBand = (hitTargetIsRowEmptyRegion && m_selectionManager->selectedItems().isEmpty());
+ if (leftClick) {
+ row->setPressed(true);
+ }
+
if (rightClick && hitTargetIsRowEmptyRegion) {
// We have a right click outside the icon and text rect but within the hover highlight area.
// We don't want items to get selected through this, so we return now.
break;
case SingleSelection:
- if (!leftClick || shiftOrControlPressed
- || (!m_view->style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick) && !m_singleClickActivationEnforced)) {
+ if (!leftClick || shiftOrControlPressed || (!singleClickActivation && !m_singleClickActivationEnforced)) {
m_selectionManager->setSelected(m_pressedIndex.value());
}
break;
// 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) {
+ if (!row->selectionRectCore().contains(mappedPos)) {
createRubberBand = true;
} else {
m_selectionManager->setSelected(m_pressedIndex.value(), 1, KItemListSelectionManager::Toggle);
}
} else if (!shiftPressed || !m_selectionManager->isAnchoredSelectionActive()) {
// Select the pressed item and start a new anchored selection
- if (!leftClick || shiftOrControlPressed
- || (!m_view->style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick) && !m_singleClickActivationEnforced)) {
+ if (!leftClick || shiftOrControlPressed || (!singleClickActivation && !m_singleClickActivationEnforced)) {
m_selectionManager->setSelected(m_pressedIndex.value(), 1, KItemListSelectionManager::Select);
}
m_selectionManager->beginAnchoredSelection(m_pressedIndex.value());