]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/kitemviews/kitemlistcontroller.cpp
Remove the automoc noise
[dolphin.git] / src / kitemviews / kitemlistcontroller.cpp
index 2c2101935aec1c3a2616482b4e3b826020bb6b90..854a0087aa4f3173e1b1bdad51f8d84c38b7ce32 100644 (file)
 #include <QGraphicsView>
 #include <QMimeData>
 #include <QTimer>
+#include <QAccessible>
 
 KItemListController::KItemListController(KItemModelBase* model, KItemListView* view, QObject* parent) :
     QObject(parent),
-    m_singleClickActivation(KGlobalSettings::singleClick()),
+    m_singleClickActivationEnforced(false),
     m_selectionTogglePressed(false),
     m_clearSelectionIfItemsAreNotDragged(false),
     m_selectionBehavior(NoSelection),
+    m_autoActivationBehavior(ActivationAndExpansion),
+    m_mouseDoubleClickAction(ActivateItemOnly),
     m_model(0),
     m_view(0),
     m_selectionManager(new KItemListSelectionManager(this)),
@@ -58,15 +61,15 @@ KItemListController::KItemListController(KItemModelBase* model, KItemListView* v
     m_keyboardAnchorIndex(-1),
     m_keyboardAnchorPos(0)
 {
-    connect(m_keyboardManager, SIGNAL(changeCurrentItem(QString,bool)),
-            this, SLOT(slotChangeCurrentItem(QString,bool)));
-    connect(m_selectionManager, SIGNAL(currentChanged(int,int)),
-            m_keyboardManager, SLOT(slotCurrentChanged(int,int)));
+    connect(m_keyboardManager, &KItemListKeyboardSearchManager::changeCurrentItem,
+            this, &KItemListController::slotChangeCurrentItem);
+    connect(m_selectionManager, &KItemListSelectionManager::currentChanged,
+            m_keyboardManager, &KItemListKeyboardSearchManager::slotCurrentChanged);
 
     m_autoActivationTimer = new QTimer(this);
     m_autoActivationTimer->setSingleShot(true);
     m_autoActivationTimer->setInterval(-1);
-    connect(m_autoActivationTimer, SIGNAL(timeout()), this, SLOT(slotAutoActivationTimeout()));
+    connect(m_autoActivationTimer, &QTimer::timeout, this, &KItemListController::slotAutoActivationTimeout);
 
     setModel(model);
     setView(view);
@@ -124,7 +127,7 @@ void KItemListController::setView(KItemListView* view)
 
     KItemListView* oldView = m_view;
     if (oldView) {
-        disconnect(oldView, SIGNAL(scrollOffsetChanged(qreal,qreal)), this, SLOT(slotViewScrollOffsetChanged(qreal,qreal)));
+        disconnect(oldView, &KItemListView::scrollOffsetChanged, this, &KItemListController::slotViewScrollOffsetChanged);
         oldView->deleteLater();
     }
 
@@ -134,7 +137,7 @@ void KItemListController::setView(KItemListView* view)
         m_view->setParent(this);
         m_view->setController(this);
         m_view->setModel(m_model);
-        connect(m_view, SIGNAL(scrollOffsetChanged(qreal,qreal)), this, SLOT(slotViewScrollOffsetChanged(qreal,qreal)));
+        connect(m_view, &KItemListView::scrollOffsetChanged, this, &KItemListController::slotViewScrollOffsetChanged);
         updateExtendedSelectionRegion();
     }
 
@@ -157,6 +160,26 @@ KItemListController::SelectionBehavior KItemListController::selectionBehavior()
     return m_selectionBehavior;
 }
 
+void KItemListController::setAutoActivationBehavior(AutoActivationBehavior behavior)
+{
+    m_autoActivationBehavior = behavior;
+}
+
+KItemListController::AutoActivationBehavior KItemListController::autoActivationBehavior() const
+{
+    return m_autoActivationBehavior;
+}
+
+void KItemListController::setMouseDoubleClickAction(MouseDoubleClickAction action)
+{
+    m_mouseDoubleClickAction = action;
+}
+
+KItemListController::MouseDoubleClickAction KItemListController::mouseDoubleClickAction() const
+{
+    return m_mouseDoubleClickAction;
+}
+
 void KItemListController::setAutoActivationDelay(int delay)
 {
     m_autoActivationTimer->setInterval(delay);
@@ -167,14 +190,14 @@ int KItemListController::autoActivationDelay() const
     return m_autoActivationTimer->interval();
 }
 
-void KItemListController::setSingleClickActivation(bool singleClick)
+void KItemListController::setSingleClickActivationEnforced(bool singleClick)
 {
-    m_singleClickActivation = singleClick;
+    m_singleClickActivationEnforced = singleClick;
 }
 
-bool KItemListController::singleClickActivation() const
+bool KItemListController::singleClickActivationEnforced() const
 {
-    return m_singleClickActivation;
+    return m_singleClickActivationEnforced;
 }
 
 bool KItemListController::showEvent(QShowEvent* event)
@@ -341,41 +364,28 @@ bool KItemListController::keyPressEvent(QKeyEvent* event)
 
     case Qt::Key_Enter:
     case Qt::Key_Return: {
-        const QSet<int> selectedItems = m_selectionManager->selectedItems();
+        const KItemSet selectedItems = m_selectionManager->selectedItems();
         if (selectedItems.count() >= 2) {
             emit itemsActivated(selectedItems);
         } else if (selectedItems.count() == 1) {
-            emit itemActivated(selectedItems.toList().first());
+            emit itemActivated(selectedItems.first());
         } else {
             emit itemActivated(index);
         }
         break;
     }
 
-    case Qt::Key_Space:
-        if (m_selectionBehavior == MultiSelection) {
-            if (controlPressed) {
-                m_selectionManager->endAnchoredSelection();
-                m_selectionManager->setSelected(index, 1, KItemListSelectionManager::Toggle);
-                m_selectionManager->beginAnchoredSelection(index);
-            } 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<int> selectedItems = m_selectionManager->selectedItems();
+        const KItemSet 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();
+                    ? currentItemIndex : selectedItems.first();
         } else if (selectedItems.count() == 1) {
-            index = selectedItems.toList().first();
+            index = selectedItems.first();
         }
 
         if (index >= 0) {
@@ -393,10 +403,32 @@ bool KItemListController::keyPressEvent(QKeyEvent* event)
             m_selectionManager->clearSelection();
         }
         m_keyboardManager->cancelSearch();
+        emit escapePressed();
         break;
 
+    case Qt::Key_Space:
+        if (m_selectionBehavior == MultiSelection) {
+            if (controlPressed) {
+                // Toggle the selection state of the current item.
+                m_selectionManager->endAnchoredSelection();
+                m_selectionManager->setSelected(index, 1, KItemListSelectionManager::Toggle);
+                m_selectionManager->beginAnchoredSelection(index);
+                break;
+            } else {
+                // Select the current item if it is not selected yet.
+                const int current = m_selectionManager->currentItem();
+                if (!m_selectionManager->isSelected(current)) {
+                    m_selectionManager->setSelected(current);
+                    break;
+                }
+            }
+        }
+        // Fall through to the default case and add the Space to the current search string.
+
     default:
         m_keyboardManager->addKeys(event->text());
+        // Make sure unconsumed events get propagated up the chain. #302329
+        event->ignore();
         return false;
     }
 
@@ -449,9 +481,13 @@ void KItemListController::slotChangeCurrentItem(const QString& text, bool search
     }
     if (index >= 0) {
         m_selectionManager->setCurrentItem(index);
-        m_selectionManager->clearSelection();
-        m_selectionManager->setSelected(index, 1);
-        m_selectionManager->beginAnchoredSelection(index);
+
+        if (m_selectionBehavior != NoSelection) {
+            m_selectionManager->clearSelection();
+            m_selectionManager->setSelected(index, 1);
+            m_selectionManager->beginAnchoredSelection(index);
+        }
+
         m_view->scrollToItem(index);
     }
 }
@@ -467,11 +503,20 @@ void KItemListController::slotAutoActivationTimeout()
         return;
     }
 
-    if (m_model->supportsDropping(index)) {
+    /* m_view->isUnderMouse() fixes a bug in the Folder-View-Panel and in the
+     * Places-Panel.
+     *
+     * Bug: When you drag a file onto a Folder-View-Item or a Places-Item and
+     * then move away before the auto-activation timeout triggers, than the
+     * item still becomes activated/expanded.
+     *
+     * See Bug 293200 and 305783
+     */
+    if (m_model->supportsDropping(index) && m_view->isUnderMouse()) {
         if (m_view->supportsItemExpanding() && m_model->isExpandable(index)) {
             const bool expanded = m_model->isExpanded(index);
             m_model->setExpanded(index, !expanded);
-        } else {
+        } else if (m_autoActivationBehavior != ExpansionOnly) {
             emit itemActivated(index);
         }
     }
@@ -493,6 +538,13 @@ bool KItemListController::mousePressEvent(QGraphicsSceneMouseEvent* event, const
     m_pressedIndex = m_view->itemAt(m_pressedMousePos);
     emit mouseButtonPressed(m_pressedIndex, event->buttons());
 
+    // TODO: Qt5: Replace Qt::XButton1 by Qt::BackButton and Qt::XButton2 by Qt::ForwardButton
+    if (event->buttons() & (Qt::XButton1 | Qt::XButton2)) {
+        // Do not select items when clicking the back/forward buttons, see
+        // https://bugs.kde.org/show_bug.cgi?id=327412.
+        return true;
+    }
+
     if (m_view->isAboveExpansionToggle(m_pressedIndex, m_pressedMousePos)) {
         m_selectionManager->endAnchoredSelection();
         m_selectionManager->setCurrentItem(m_pressedIndex);
@@ -603,7 +655,7 @@ bool KItemListController::mousePressEvent(QGraphicsSceneMouseEvent* event, const
         rubberBand->setStartPosition(startPos);
         rubberBand->setEndPosition(startPos);
         rubberBand->setActive(true);
-        connect(rubberBand, SIGNAL(endPositionChanged(QPointF,QPointF)), this, SLOT(slotRubberBandChanged()));
+        connect(rubberBand, &KItemListRubberBand::endPositionChanged, this, &KItemListController::slotRubberBandChanged);
         m_view->setAutoScroll(true);
     }
 
@@ -691,7 +743,7 @@ bool KItemListController::mouseReleaseEvent(QGraphicsSceneMouseEvent* event, con
 
     KItemListRubberBand* rubberBand = m_view->rubberBand();
     if (rubberBand->isActive()) {
-        disconnect(rubberBand, SIGNAL(endPositionChanged(QPointF,QPointF)), this, SLOT(slotRubberBandChanged()));
+        disconnect(rubberBand, &KItemListRubberBand::endPositionChanged, this, &KItemListController::slotRubberBandChanged);
         rubberBand->setActive(false);
         m_oldSelection.clear();
         m_view->setAutoScroll(false);
@@ -722,7 +774,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 (!m_singleClickActivation) {
+            } else if (!(KGlobalSettings::singleClick() || m_singleClickActivationEnforced)) {
                 emitItemActivated = false;
             }
             if (emitItemActivated) {
@@ -744,7 +796,15 @@ bool KItemListController::mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event,
     const QPointF pos = transform.map(event->pos());
     const int index = m_view->itemAt(pos);
 
-    bool emitItemActivated = !m_singleClickActivation &&
+    // Expand item if desired - See Bug 295573
+    if (m_mouseDoubleClickAction != ActivateItemOnly) {
+        if (m_view && m_model && m_view->supportsItemExpanding() && m_model->isExpandable(index)) {
+            const bool expanded = m_model->isExpanded(index);
+            m_model->setExpanded(index, !expanded);
+        }
+    }
+
+    bool emitItemActivated = !(KGlobalSettings::singleClick() || m_singleClickActivationEnforced) &&
                              (event->button() & Qt::LeftButton) &&
                              index >= 0 && index < m_model->count();
     if (emitItemActivated) {
@@ -778,7 +838,6 @@ bool KItemListController::dragLeaveEvent(QGraphicsSceneDragDropEvent* event, con
 
 bool KItemListController::dragMoveEvent(QGraphicsSceneDragDropEvent* event, const QTransform& transform)
 {
-    Q_UNUSED(transform);
     if (!m_model || !m_view) {
         return false;
     }
@@ -797,24 +856,39 @@ bool KItemListController::dragMoveEvent(QGraphicsSceneDragDropEvent* event, cons
             oldHoveredWidget->setHovered(false);
             emit itemUnhovered(oldHoveredWidget->index());
         }
+    }
 
-        if (newHoveredWidget) {
-            const int index = newHoveredWidget->index();
+    if (newHoveredWidget) {
+        bool droppingBetweenItems = false;
+        if (m_model->sortRole().isEmpty()) {
+            // The model supports inserting items between other items.
+            droppingBetweenItems = (m_view->showDropIndicator(pos) >= 0);
+        }
+
+        const int index = newHoveredWidget->index();
+        if (!droppingBetweenItems) {
             if (m_model->supportsDropping(index)) {
-                newHoveredWidget->setHovered(true);
-            } else if (m_model->sortRole().isEmpty()) {
-                // The model supports inserting of items on
-                // the given index. Assure that a drop-indicator
-                // is shown by the view.
-                m_view->showDropIndicator(pos);
-            }
-            emit itemHovered(index);
+                // Something has been dragged on an item.
+                m_view->hideDropIndicator();
+                if (!newHoveredWidget->isHovered()) {
+                    newHoveredWidget->setHovered(true);
+                    emit itemHovered(index);
+                }
 
-            if (m_autoActivationTimer->interval() >= 0) {
-                m_autoActivationTimer->setProperty("index", index);
-                m_autoActivationTimer->start();
+                if (!m_autoActivationTimer->isActive() && m_autoActivationTimer->interval() >= 0) {
+                    m_autoActivationTimer->setProperty("index", index);
+                    m_autoActivationTimer->start();
+                }
+            }
+        } else {
+            m_autoActivationTimer->stop();
+            if (newHoveredWidget && newHoveredWidget->isHovered()) {
+                newHoveredWidget->setHovered(false);
+                emit itemUnhovered(index);
             }
         }
+    } else {
+        m_view->hideDropIndicator();
     }
 
     return false;
@@ -822,7 +896,6 @@ bool KItemListController::dragMoveEvent(QGraphicsSceneDragDropEvent* event, cons
 
 bool KItemListController::dropEvent(QGraphicsSceneDragDropEvent* event, const QTransform& transform)
 {
-    Q_UNUSED(transform)
     if (!m_view) {
         return false;
     }
@@ -831,16 +904,24 @@ bool KItemListController::dropEvent(QGraphicsSceneDragDropEvent* event, const QT
     m_view->setAutoScroll(false);
 
     const QPointF pos = transform.map(event->pos());
+
+    int dropAboveIndex = -1;
     if (m_model->sortRole().isEmpty()) {
-        // The model supports inserting of items on
-        // a given index.
-        const int dropIndex = m_view->showDropIndicator(pos);
+        // The model supports inserting of items between other items.
+        dropAboveIndex = m_view->showDropIndicator(pos);
+    }
+
+    if (dropAboveIndex >= 0) {
+        // Something has been dropped between two items.
         m_view->hideDropIndicator();
-        emit itemDropEvent(dropIndex, event);
+        emit aboveItemDropEvent(dropAboveIndex, event);
     } else {
+        // Something has been dropped on an item or on an empty part of the view.
         emit itemDropEvent(m_view->itemAt(pos), event);
     }
 
+    QAccessible::updateAccessibility(view(), 0, QAccessible::DragDropEnd);
+
     return true;
 }
 
@@ -870,8 +951,13 @@ bool KItemListController::hoverMoveEvent(QGraphicsSceneHoverEvent* event, const
 
         if (newHoveredWidget) {
             newHoveredWidget->setHovered(true);
+            const QPointF mappedPos = newHoveredWidget->mapFromItem(m_view, pos);
+            newHoveredWidget->setHoverPosition(mappedPos);
             emit itemHovered(newHoveredWidget->index());
         }
+    } else if (oldHoveredWidget) {
+        const QPointF mappedPos = oldHoveredWidget->mapFromItem(m_view, pos);
+        oldHoveredWidget->setHoverPosition(mappedPos);
     }
 
     return false;
@@ -1005,7 +1091,7 @@ void KItemListController::slotRubberBandChanged()
         }
     }
 
-    QSet<int> selectedItems;
+    KItemSet selectedItems;
 
     // Select all visible items that intersect with the rubberband
     foreach (const KItemListWidget* widget, m_view->visibleItemListWidgets()) {
@@ -1053,7 +1139,7 @@ void KItemListController::slotRubberBandChanged()
         // Therefore, the new selection contains:
         // 1. All previously selected items which are not inside the rubberband, and
         // 2. all items inside the rubberband which have not been selected previously.
-        m_selectionManager->setSelectedItems((m_oldSelection - selectedItems) + (selectedItems - m_oldSelection));
+        m_selectionManager->setSelectedItems(m_oldSelection ^ selectedItems);
     }
     else {
         m_selectionManager->setSelectedItems(selectedItems + m_oldSelection);
@@ -1066,7 +1152,7 @@ void KItemListController::startDragging()
         return;
     }
 
-    const QSet<int> selectedItems = m_selectionManager->selectedItems();
+    const KItemSet selectedItems = m_selectionManager->selectedItems();
     if (selectedItems.isEmpty()) {
         return;
     }
@@ -1084,12 +1170,11 @@ void KItemListController::startDragging()
     const QPixmap pixmap = m_view->createDragPixmap(selectedItems);
     drag->setPixmap(pixmap);
 
-    // TODO: The vertical hotspot of -24 should be replaced by the
-    // height of the QCursor-pixmap.
-    const QPoint hotSpot(pixmap.width() / 2, -24);
+    const QPoint hotSpot(pixmap.width() / 2, 0);
     drag->setHotSpot(hotSpot);
 
     drag->exec(Qt::MoveAction | Qt::CopyAction | Qt::LinkAction, Qt::CopyAction);
+    QAccessible::updateAccessibility(view(), 0, QAccessible::DragDropStart);
 }
 
 KItemListWidget* KItemListController::hoveredWidget() const
@@ -1225,4 +1310,3 @@ void KItemListController::updateExtendedSelectionRegion()
     }
 }
 
-#include "kitemlistcontroller.moc"