]> cloud.milkyroute.net Git - dolphin.git/commitdiff
Improve selection action toggle code
authorFelix Ernst <felixernst@kde.org>
Thu, 15 Sep 2022 13:36:05 +0000 (13:36 +0000)
committerFelix Ernst <felixernst@kde.org>
Thu, 15 Sep 2022 13:36:05 +0000 (13:36 +0000)
@broulik noticed an issue in the code. This commit fixes it.

-------------------

Before this commit there was a `QObject::disconnect` call that did
nothing (because it had `nullptr` as the first parameter) and there was a `QObject::connect` call that created
the same connections multiple times because of this.

This had no effect on end users. However such code can lead to
issues in the future e.g. if we ever had a situation in which the selection mode could be toggled for an inactive view container.

This commit solves this by replacing the `QObject::disconnect` call
with one that works. The `QObject::connect` call is moved so
there won't be multiple connections of the same type.

src/dolphinmainwindow.cpp

index d383d04d6d4bc8b48d7cd329fb623b981ea922bc..bcae015d0679dee2a24c7259c3d6d4d1031edddc 100644 (file)
@@ -1433,6 +1433,10 @@ void DolphinMainWindow::activeViewChanged(DolphinViewContainer* viewContainer)
         // except the requestItemInfo so that on hover the information panel can still be updated
         connect(oldViewContainer->view(), &DolphinView::requestItemInfo,
                 this, &DolphinMainWindow::requestItemInfo);
+
+        // Disconnect other slots.
+        disconnect(nullptr, &DolphinViewContainer::selectionModeChanged,
+                   actionCollection()->action(QStringLiteral("toggle_selection_mode")), &QAction::setChecked);
     }
 
     connectViewSignals(viewContainer);
@@ -2286,13 +2290,6 @@ void DolphinMainWindow::updateViewActions()
 {
     m_actionHandler->updateViewActions();
 
-    QAction *toggleSelectionModeAction = actionCollection()->action(QStringLiteral("toggle_selection_mode"));
-    disconnect(nullptr, &DolphinViewContainer::selectionModeChanged,
-               toggleSelectionModeAction, &QAction::setChecked);
-    toggleSelectionModeAction->setChecked(m_activeViewContainer->isSelectionModeEnabled());
-    connect(m_activeViewContainer, &DolphinViewContainer::selectionModeChanged,
-            toggleSelectionModeAction, &QAction::setChecked);
-
     QAction* toggleFilterBarAction = actionCollection()->action(QStringLiteral("toggle_filter"));
     toggleFilterBarAction->setChecked(m_activeViewContainer->isFilterBarVisible());
 
@@ -2350,6 +2347,12 @@ void DolphinMainWindow::connectViewSignals(DolphinViewContainer* container)
     const QAction* toggleSearchAction = actionCollection()->action(QStringLiteral("toggle_search"));
     connect(toggleSearchAction, &QAction::triggered, container, &DolphinViewContainer::setSearchModeEnabled);
 
+    // Make the toggled state of the selection mode actions visually follow the selection mode state of the view.
+    auto toggleSelectionModeAction = actionCollection()->action(QStringLiteral("toggle_selection_mode"));
+    toggleSelectionModeAction->setChecked(m_activeViewContainer->isSelectionModeEnabled());
+    connect(m_activeViewContainer, &DolphinViewContainer::selectionModeChanged,
+            toggleSelectionModeAction, &QAction::setChecked);
+
     const DolphinView* view = container->view();
     connect(view, &DolphinView::selectionChanged,
             this, &DolphinMainWindow::slotSelectionChanged);