X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/blobdiff_plain/362817d1834f2ada3ea4552a25fa39bbbb540f8c..bfaa79b55a8490b1a4b292674d6758c4c8b52504:/src/kitemviews/kitemlistcontroller.cpp diff --git a/src/kitemviews/kitemlistcontroller.cpp b/src/kitemviews/kitemlistcontroller.cpp index 60bfef518..220748be7 100644 --- a/src/kitemviews/kitemlistcontroller.cpp +++ b/src/kitemviews/kitemlistcontroller.cpp @@ -30,14 +30,18 @@ #include #include #include +#include #include +#include #include +#include #include #include KItemListController::KItemListController(QObject* parent) : QObject(parent), + m_singleClickActivation(KGlobalSettings::singleClick()), m_selectionTogglePressed(false), m_selectionBehavior(NoSelection), m_model(0), @@ -46,10 +50,18 @@ KItemListController::KItemListController(QObject* parent) : m_keyboardManager(new KItemListKeyboardSearchManager(this)), m_pressedIndex(-1), m_pressedMousePos(), - m_oldSelection() + m_autoActivationTimer(0), + m_oldSelection(), + m_keyboardAnchorIndex(-1), + m_keyboardAnchorPos(0) { connect(m_keyboardManager, SIGNAL(changeCurrentItem(QString,bool)), this, SLOT(slotChangeCurrentItem(QString,bool))); + + m_autoActivationTimer = new QTimer(this); + m_autoActivationTimer->setSingleShot(true); + m_autoActivationTimer->setInterval(-1); + connect(m_autoActivationTimer, SIGNAL(timeout()), this, SLOT(slotAutoActivationTimeout())); } KItemListController::~KItemListController() @@ -121,6 +133,26 @@ KItemListController::SelectionBehavior KItemListController::selectionBehavior() return m_selectionBehavior; } +void KItemListController::setAutoActivationDelay(int delay) +{ + m_autoActivationTimer->setInterval(delay); +} + +int KItemListController::autoActivationDelay() const +{ + return m_autoActivationTimer->interval(); +} + +void KItemListController::setSingleClickActivation(bool singleClick) +{ + m_singleClickActivation = singleClick; +} + +bool KItemListController::singleClickActivation() const +{ + return m_singleClickActivation; +} + bool KItemListController::showEvent(QShowEvent* event) { Q_UNUSED(event); @@ -156,7 +188,6 @@ bool KItemListController::keyPressEvent(QKeyEvent* event) const bool shiftOrControlPressed = shiftPressed || controlPressed; const int itemCount = m_model->count(); - const int itemsPerRow = m_view->itemsPerOffset(); // For horizontal scroll orientation, transform // the arrow keys to simplify the event handling. @@ -169,7 +200,16 @@ 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); + if (selectSingleItem) { + const int current = m_selectionManager->currentItem(); + m_selectionManager->setSelected(current); + return true; + } switch (key) { case Qt::Key_Home: @@ -182,52 +222,76 @@ bool KItemListController::keyPressEvent(QKeyEvent* event) case Qt::Key_Left: if (index > 0) { - index--; + --index; + m_keyboardAnchorIndex = index; + m_keyboardAnchorPos = keyboardAnchorPos(index); } break; case Qt::Key_Right: if (index < itemCount - 1) { - index++; + ++index; + m_keyboardAnchorIndex = index; + m_keyboardAnchorPos = keyboardAnchorPos(index); } break; case Qt::Key_Up: - if (index >= itemsPerRow) { - index -= itemsPerRow; - } + updateKeyboardAnchor(); + index = previousRowIndex(); break; case Qt::Key_Down: - if (index + itemsPerRow < itemCount) { - // We are not in the last row yet. - index += itemsPerRow; - } - else { - // We are either in the last row already, or we are in the second-last row, - // and there is no item below the current item. - // In the latter case, we jump to the very last item. - const int currentColumn = index % itemsPerRow; - const int lastItemColumn = (itemCount - 1) % itemsPerRow; - const bool inLastRow = currentColumn < lastItemColumn; - if (!inLastRow) { - index = itemCount - 1; - } - } + updateKeyboardAnchor(); + index = nextRowIndex(); break; case Qt::Key_Enter: - case Qt::Key_Return: - emit itemActivated(index); + case Qt::Key_Return: { + const QSet selectedItems = m_selectionManager->selectedItems(); + if (selectedItems.count() >= 2) { + emit itemsActivated(selectedItems); + } else if (selectedItems.count() == 1) { + emit itemActivated(selectedItems.toList().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); - break; + } 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(); + int index = -1; + if (selectedItems.count() >= 2) { + const int currentItemIndex = m_selectionManager->currentItem(); + index = selectedItems.contains(currentItemIndex) + ? currentItemIndex : selectedItems.toList().first(); + } else if (selectedItems.count() == 1) { + index = selectedItems.toList().first(); + } + + if (index >= 0) { + const QRectF contextRect = m_view->itemContextRect(index); + const QPointF pos(m_view->scene()->views().first()->mapToGlobal(contextRect.bottomRight().toPoint())); + emit itemContextMenuRequested(index, pos); + } else { + emit viewContextMenuRequested(QCursor::pos()); } + break; + } default: m_keyboardManager->addKeys(event->text()); @@ -264,8 +328,7 @@ void KItemListController::slotChangeCurrentItem(const QString& text, bool search int index; if (searchFromNextItem) { index = m_model->indexForKeyboardSearch(text, (currentIndex + 1) % m_model->count()); - } - else { + } else { index = m_model->indexForKeyboardSearch(text, currentIndex); } if (index >= 0) { @@ -273,6 +336,28 @@ void KItemListController::slotChangeCurrentItem(const QString& text, bool search m_selectionManager->clearSelection(); m_selectionManager->setSelected(index, 1); m_selectionManager->beginAnchoredSelection(index); + m_view->scrollToItem(index); + } +} + +void KItemListController::slotAutoActivationTimeout() +{ + if (!m_model || !m_view) { + return; + } + + const int index = m_autoActivationTimer->property("index").toInt(); + if (index < 0 || index >= m_model->count()) { + return; + } + + if (m_model->supportsDropping(index)) { + if (m_view->supportsItemExpanding() && m_model->isExpandable(index)) { + const bool expanded = m_model->isExpanded(index); + m_model->setExpanded(index, !expanded); + } else { + emit itemActivated(index); + } } } @@ -290,6 +375,9 @@ 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()); + } if (m_view->isAboveExpansionToggle(m_pressedIndex, m_pressedMousePos)) { m_selectionManager->setCurrentItem(m_pressedIndex); @@ -299,13 +387,29 @@ bool KItemListController::mousePressEvent(QGraphicsSceneMouseEvent* event, const m_selectionTogglePressed = m_view->isAboveSelectionToggle(m_pressedIndex, m_pressedMousePos); if (m_selectionTogglePressed) { m_selectionManager->setSelected(m_pressedIndex, 1, KItemListSelectionManager::Toggle); + // The previous anchored selection has been finished already in + // KItemListSelectionManager::setSelected(). We can safely change + // the current item and start a new anchored selection now. + m_selectionManager->setCurrentItem(m_pressedIndex); + m_selectionManager->beginAnchoredSelection(m_pressedIndex); return true; } const bool shiftPressed = event->modifiers() & Qt::ShiftModifier; const bool controlPressed = event->modifiers() & Qt::ControlModifier; - if (m_selectionBehavior == SingleSelection) { + // 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: + // a) Shift or Control are pressed. + // b) The clicked item is selected already. In that case, the user might want to: + // - start dragging multiple items, or + // - open the context menu and perform an action for all selected items. + const bool shiftOrControlPressed = shiftPressed || controlPressed; + const bool pressedItemAlreadySelected = m_pressedIndex >= 0 && m_selectionManager->isSelected(m_pressedIndex); + const bool clearSelection = m_selectionBehavior == SingleSelection || + (!shiftOrControlPressed && !pressedItemAlreadySelected); + if (clearSelection) { m_selectionManager->clearSelection(); } @@ -329,11 +433,6 @@ bool KItemListController::mousePressEvent(QGraphicsSceneMouseEvent* event, const if (controlPressed) { m_selectionManager->setSelected(m_pressedIndex, 1, KItemListSelectionManager::Toggle); m_selectionManager->beginAnchoredSelection(m_pressedIndex); - } else if (event->buttons() & Qt::RightButton) { - // Only clear the selection if a context menu is requested above a non-selected item - if (!m_selectionManager->selectedItems().contains(m_pressedIndex)) { - m_selectionManager->setSelectedItems(QSet() << m_pressedIndex); - } } else if (!shiftPressed || !m_selectionManager->isAnchoredSelectionActive()) { // Select the pressed item and start a new anchored selection m_selectionManager->setSelected(m_pressedIndex, 1, KItemListSelectionManager::Select); @@ -354,8 +453,6 @@ bool KItemListController::mousePressEvent(QGraphicsSceneMouseEvent* event, const } if (event->buttons() & Qt::RightButton) { - m_selectionManager->clearSelection(); - const QRectF headerBounds = m_view->headerBoundaries(); if (headerBounds.contains(event->pos())) { emit headerContextMenuRequested(event->screenPos()); @@ -414,6 +511,16 @@ bool KItemListController::mouseMoveEvent(QGraphicsSceneMouseEvent* event, const KItemListRubberBand* rubberBand = m_view->rubberBand(); if (rubberBand->isActive()) { QPointF endPos = transform.map(event->pos()); + + // Update the current item. + const int newCurrent = m_view->itemAt(endPos); + if (newCurrent >= 0) { + // It's expected that the new current index is also the new anchor (bug 163451). + m_selectionManager->endAnchoredSelection(); + m_selectionManager->setCurrentItem(newCurrent); + m_selectionManager->beginAnchoredSelection(newCurrent); + } + if (m_view->scrollOrientation() == Qt::Vertical) { endPos.ry() += m_view->scrollOffset(); if (m_view->itemSize().width() < 0) { @@ -437,6 +544,10 @@ bool KItemListController::mouseReleaseEvent(QGraphicsSceneMouseEvent* event, con return false; } + if (m_pressedIndex >= 0) { + emit itemReleased(m_pressedIndex, event->button()); + } + const bool isAboveSelectionToggle = m_view->isAboveSelectionToggle(m_pressedIndex, m_pressedMousePos); if (isAboveSelectionToggle) { m_selectionTogglePressed = false; @@ -452,18 +563,12 @@ bool KItemListController::mouseReleaseEvent(QGraphicsSceneMouseEvent* event, con const bool shiftOrControlPressed = event->modifiers() & Qt::ShiftModifier || event->modifiers() & Qt::ControlModifier; - bool clearSelection = !shiftOrControlPressed && event->button() != Qt::RightButton; - KItemListRubberBand* rubberBand = m_view->rubberBand(); if (rubberBand->isActive()) { disconnect(rubberBand, SIGNAL(endPositionChanged(QPointF,QPointF)), this, SLOT(slotRubberBandChanged())); rubberBand->setActive(false); m_oldSelection.clear(); m_view->setAutoScroll(false); - - if (rubberBand->startPosition() != rubberBand->endPosition()) { - clearSelection = false; - } } const QPointF pos = transform.map(event->pos()); @@ -472,11 +577,6 @@ 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 (clearSelection) { - // Clear the previous selection but reselect the current index - m_selectionManager->setSelectedItems(QSet() << index); - } - if (event->button() & Qt::LeftButton) { bool emitItemActivated = true; if (m_view->isAboveExpansionToggle(index, pos)) { @@ -488,7 +588,7 @@ 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 (!KGlobalSettings::singleClick()) { + } else if (!m_singleClickActivation) { emitItemActivated = false; } if (emitItemActivated) { @@ -497,8 +597,6 @@ bool KItemListController::mouseReleaseEvent(QGraphicsSceneMouseEvent* event, con } else if (event->button() & Qt::MidButton) { emit itemMiddleClicked(index); } - } else if (clearSelection) { - m_selectionManager->clearSelection(); } m_pressedMousePos = QPointF(); @@ -511,7 +609,7 @@ bool KItemListController::mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event, const QPointF pos = transform.map(event->pos()); const int index = m_view->itemAt(pos); - bool emitItemActivated = !KGlobalSettings::singleClick() && + bool emitItemActivated = !m_singleClickActivation && (event->button() & Qt::LeftButton) && index >= 0 && index < m_model->count(); if (emitItemActivated) { @@ -542,8 +640,13 @@ bool KItemListController::dragMoveEvent(QGraphicsSceneDragDropEvent* event, cons } KItemListWidget* oldHoveredWidget = hoveredWidget(); - KItemListWidget* newHoveredWidget = widgetForPos(event->pos()); + + const QPointF pos = transform.map(event->pos()); + KItemListWidget* newHoveredWidget = widgetForPos(pos); + if (oldHoveredWidget != newHoveredWidget) { + m_autoActivationTimer->stop(); + if (oldHoveredWidget) { oldHoveredWidget->setHovered(false); emit itemUnhovered(oldHoveredWidget->index()); @@ -555,6 +658,11 @@ bool KItemListController::dragMoveEvent(QGraphicsSceneDragDropEvent* event, cons newHoveredWidget->setHovered(true); } emit itemHovered(index); + + if (m_autoActivationTimer->interval() >= 0) { + m_autoActivationTimer->setProperty("index", index); + m_autoActivationTimer->start(); + } } } @@ -568,6 +676,8 @@ bool KItemListController::dropEvent(QGraphicsSceneDragDropEvent* event, const QT return false; } + m_autoActivationTimer->stop(); + const QPointF pos = transform.map(event->pos()); const int index = m_view->itemAt(pos); emit itemDropEvent(index, event); @@ -590,7 +700,9 @@ bool KItemListController::hoverMoveEvent(QGraphicsSceneHoverEvent* event, const } KItemListWidget* oldHoveredWidget = hoveredWidget(); - KItemListWidget* newHoveredWidget = widgetForPos(event->pos()); + const QPointF pos = transform.map(event->pos()); + KItemListWidget* newHoveredWidget = widgetForPos(pos); + if (oldHoveredWidget != newHoveredWidget) { if (oldHoveredWidget) { oldHoveredWidget->setHovered(false); @@ -813,7 +925,7 @@ void KItemListController::startDragging() const QPixmap pixmap = m_view->createDragPixmap(selectedItems); drag->setPixmap(pixmap); - drag->exec(Qt::MoveAction | Qt::CopyAction | Qt::LinkAction, Qt::IgnoreAction); + drag->exec(Qt::CopyAction); } KItemListWidget* KItemListController::hoveredWidget() const @@ -846,4 +958,97 @@ KItemListWidget* KItemListController::widgetForPos(const QPointF& pos) const return 0; } +void KItemListController::updateKeyboardAnchor() +{ + const bool validAnchor = m_keyboardAnchorIndex >= 0 && + m_keyboardAnchorIndex < m_model->count() && + keyboardAnchorPos(m_keyboardAnchorIndex) == m_keyboardAnchorPos; + if (!validAnchor) { + const int index = m_selectionManager->currentItem(); + m_keyboardAnchorIndex = index; + m_keyboardAnchorPos = keyboardAnchorPos(index); + } +} + +int KItemListController::nextRowIndex() const +{ + const int currentIndex = m_selectionManager->currentItem(); + if (m_keyboardAnchorIndex < 0) { + return currentIndex; + } + + const int maxIndex = m_model->count() - 1; + if (currentIndex == maxIndex) { + return currentIndex; + } + + // Calculate the index of the last column inside the row of the current index + int lastColumnIndex = currentIndex; + while (keyboardAnchorPos(lastColumnIndex + 1) > keyboardAnchorPos(lastColumnIndex)) { + ++lastColumnIndex; + if (lastColumnIndex >= maxIndex) { + return currentIndex; + } + } + + // Based on the last column index go to the next row and calculate the nearest index + // that is below the current index + int nextRowIndex = lastColumnIndex + 1; + int searchIndex = nextRowIndex; + qreal minDiff = qAbs(m_keyboardAnchorPos - keyboardAnchorPos(nextRowIndex)); + while (searchIndex < maxIndex && keyboardAnchorPos(searchIndex + 1) > keyboardAnchorPos(searchIndex)) { + ++searchIndex; + const qreal searchDiff = qAbs(m_keyboardAnchorPos - keyboardAnchorPos(searchIndex)); + if (searchDiff < minDiff) { + minDiff = searchDiff; + nextRowIndex = searchIndex; + } + } + + return nextRowIndex; +} + +int KItemListController::previousRowIndex() const +{ + const int currentIndex = m_selectionManager->currentItem(); + if (m_keyboardAnchorIndex < 0 || currentIndex == 0) { + return currentIndex; + } + + // Calculate the index of the first column inside the row of the current index + int firstColumnIndex = currentIndex; + while (keyboardAnchorPos(firstColumnIndex - 1) < keyboardAnchorPos(firstColumnIndex)) { + --firstColumnIndex; + if (firstColumnIndex <= 0) { + return currentIndex; + } + } + + // Based on the first column index go to the previous row and calculate the nearest index + // that is above the current index + int previousRowIndex = firstColumnIndex - 1; + int searchIndex = previousRowIndex; + qreal minDiff = qAbs(m_keyboardAnchorPos - keyboardAnchorPos(previousRowIndex)); + while (searchIndex > 0 && keyboardAnchorPos(searchIndex - 1) < keyboardAnchorPos(searchIndex)) { + --searchIndex; + const qreal searchDiff = qAbs(m_keyboardAnchorPos - keyboardAnchorPos(searchIndex)); + if (searchDiff < minDiff) { + minDiff = searchDiff; + previousRowIndex = searchIndex; + } + } + + return previousRowIndex; +} + +qreal KItemListController::keyboardAnchorPos(int index) const +{ + const QRectF itemRect = m_view->itemRect(index); + if (!itemRect.isEmpty()) { + return (m_view->scrollOrientation() == Qt::Vertical) ? itemRect.x() : itemRect.y(); + } + + return 0; +} + #include "kitemlistcontroller.moc"