[vlc-devel] [PATCH 05/18] qt: allow to specify whether CheckableListMenu should use a QActionGroup

Pierre Lamot pierre at videolabs.io
Wed Sep 23 14:27:39 CEST 2020


---
 modules/gui/qt/menus/custom_menus.cpp | 15 +++++++++++++-
 modules/gui/qt/menus/custom_menus.hpp | 11 ++++++++--
 modules/gui/qt/menus/menus.cpp        | 30 +++++++++++++--------------
 3 files changed, 38 insertions(+), 18 deletions(-)

diff --git a/modules/gui/qt/menus/custom_menus.cpp b/modules/gui/qt/menus/custom_menus.cpp
index 02b29285f9..8f21defc71 100644
--- a/modules/gui/qt/menus/custom_menus.cpp
+++ b/modules/gui/qt/menus/custom_menus.cpp
@@ -202,11 +202,16 @@ void RendererMenu::RendererSelected(QAction *action)
 
 /*   CheckableListMenu   */
 
-CheckableListMenu::CheckableListMenu(QString title, QAbstractListModel* model , QWidget *parent)
+CheckableListMenu::CheckableListMenu(QString title, QAbstractListModel* model , GroupingMode grouping,  QWidget *parent)
     : QMenu(parent)
     , m_model(model)
+    , m_grouping(grouping)
 {
     this->setTitle(title);
+    if (m_grouping == GROUPED)
+    {
+        m_actionGroup = new QActionGroup(this);
+    }
 
     connect(m_model, &QAbstractListModel::rowsAboutToBeRemoved, this, &CheckableListMenu::onRowsAboutToBeRemoved);
     connect(m_model, &QAbstractListModel::rowsInserted, this, &CheckableListMenu::onRowInserted);
@@ -221,6 +226,8 @@ void CheckableListMenu::onRowsAboutToBeRemoved(const QModelIndex &, int first, i
     for (int i = last; i >= first; i--)
     {
         QAction* action = actions()[i];
+        if (m_actionGroup)
+            m_actionGroup->removeAction(action);
         delete action;
     }
     if (actions().count() == 0)
@@ -237,6 +244,8 @@ void CheckableListMenu::onRowInserted(const QModelIndex &, int first, int last)
 
         QAction *choiceAction = new QAction(title, this);
         addAction(choiceAction);
+        if (m_actionGroup)
+            m_actionGroup->addAction(choiceAction);
         connect(choiceAction, &QAction::triggered, [this, i](bool checked){
             QModelIndex dataIndex = m_model->index(i);
             m_model->setData(dataIndex, QVariant::fromValue<bool>(checked), Qt::CheckStateRole);
@@ -267,7 +276,11 @@ void CheckableListMenu::onDataChanged(const QModelIndex &topLeft, const QModelIn
 void CheckableListMenu::onModelAboutToBeReset()
 {
     for (QAction* action  :actions())
+    {
+        if (m_actionGroup)
+            m_actionGroup->addAction(action);
         delete action;
+    }
     setEnabled(false);
 }
 
diff --git a/modules/gui/qt/menus/custom_menus.hpp b/modules/gui/qt/menus/custom_menus.hpp
index 63185177c0..1dc62a76d1 100644
--- a/modules/gui/qt/menus/custom_menus.hpp
+++ b/modules/gui/qt/menus/custom_menus.hpp
@@ -72,13 +72,19 @@ class CheckableListMenu : public QMenu
 {
     Q_OBJECT
 public:
+    enum GroupingMode {
+        GROUPED,
+        UNGROUPED
+    };
+
     /**
      * @brief CheckableListMenu
      * @param title the title of the menu
      * @param model the model to observe, the model should provide at least Qt::DisplayRole and Qt::CheckStateRole
+     * @param grouping whether the menu should use an ActionGroup or not
      * @param parent QObject parent
      */
-    CheckableListMenu(QString title, QAbstractListModel* model , QWidget *parent = nullptr);
+    CheckableListMenu(QString title, QAbstractListModel* model ,  GroupingMode grouping = UNGROUPED, QWidget *parent = nullptr);
 
 private slots:
     void onRowsAboutToBeRemoved(const QModelIndex &parent, int first, int last);
@@ -89,7 +95,8 @@ private slots:
 
 private:
     QAbstractListModel* m_model;
-    QMenu * m_submenu;
+    GroupingMode m_grouping;
+    QActionGroup* m_actionGroup = nullptr;
 };
 
 
diff --git a/modules/gui/qt/menus/menus.cpp b/modules/gui/qt/menus/menus.cpp
index 6c24ae1a61..fa244bdc5e 100644
--- a/modules/gui/qt/menus/menus.cpp
+++ b/modules/gui/qt/menus/menus.cpp
@@ -380,7 +380,7 @@ QMenu *VLCMenuBar::InterfacesMenu( intf_thread_t *p_intf, QMenu *current )
 {
     assert(current);
     VLCVarChoiceModel* model = new VLCVarChoiceModel(VLC_OBJECT(p_intf), "intf-add", current);
-    CheckableListMenu* submenu = new CheckableListMenu(qtr("Interfaces"), model, current);
+    CheckableListMenu* submenu = new CheckableListMenu(qtr("Interfaces"), model, CheckableListMenu::UNGROUPED, current);
     current->addMenu(submenu);
     return current;
 }
@@ -425,7 +425,7 @@ QMenu *VLCMenuBar::AudioMenu( intf_thread_t *p_intf, QMenu * current )
 {
     if( current->isEmpty() )
     {
-        current->addMenu(new CheckableListMenu(qtr( "Audio &Track" ), THEMIM->getAudioTracks(), current));
+        current->addMenu(new CheckableListMenu(qtr( "Audio &Track" ), THEMIM->getAudioTracks(), CheckableListMenu::GROUPED, current));
 
         QAction *audioDeviceAction = new QAction( qtr( "Audio &Device" ), current );
         QMenu *audioDeviceSubmenu = new QMenu( current );
@@ -435,10 +435,10 @@ QMenu *VLCMenuBar::AudioMenu( intf_thread_t *p_intf, QMenu * current )
             updateAudioDevice( p_intf, audioDeviceSubmenu );
         });
 
-        current->addMenu( new CheckableListMenu(qtr( "&Stereo Mode" ), THEMIM->getAudioStereoMode(), current) );
+        current->addMenu( new CheckableListMenu(qtr( "&Stereo Mode" ), THEMIM->getAudioStereoMode(), CheckableListMenu::GROUPED, current) );
         current->addSeparator();
 
-        current->addMenu( new CheckableListMenu(qtr( "&Visualizations" ), THEMIM->getAudioVisualizations(), current) );
+        current->addMenu( new CheckableListMenu(qtr( "&Visualizations" ), THEMIM->getAudioVisualizations(), CheckableListMenu::GROUPED, current) );
         VolumeEntries( p_intf, current );
     }
 
@@ -452,7 +452,7 @@ QMenu *VLCMenuBar::SubtitleMenu( intf_thread_t *p_intf, QMenu *current, bool b_p
     {
         addDPStaticEntry( current, qtr( "Add &Subtitle File..." ), "",
                 &DialogsProvider::loadSubtitlesFile);
-        current->addMenu(new CheckableListMenu(qtr( "Sub &Track" ), THEMIM->getSubtitleTracks(), current));
+        current->addMenu(new CheckableListMenu(qtr( "Sub &Track" ), THEMIM->getSubtitleTracks(), CheckableListMenu::GROUPED, current));
         current->addSeparator();
     }
     return current;
@@ -466,7 +466,7 @@ QMenu *VLCMenuBar::VideoMenu( intf_thread_t *p_intf, QMenu *current )
 {
     if( current->isEmpty() )
     {
-        current->addMenu(new CheckableListMenu(qtr( "Video &Track" ), THEMIM->getVideoTracks(), current));
+        current->addMenu(new CheckableListMenu(qtr( "Video &Track" ), THEMIM->getVideoTracks(), CheckableListMenu::GROUPED, current));
 
         current->addSeparator();
         /* Surface modifiers */
@@ -479,14 +479,14 @@ QMenu *VLCMenuBar::VideoMenu( intf_thread_t *p_intf, QMenu *current )
 
         current->addSeparator();
         /* Size modifiers */
-        current->addMenu( new CheckableListMenu(qtr( "&Zoom" ), THEMIM->getZoom(), current) );
-        current->addMenu( new CheckableListMenu(qtr( "&Aspect Ratio" ), THEMIM->getAspectRatio(), current) );
-        current->addMenu( new CheckableListMenu(qtr( "&Crop" ), THEMIM->getCrop(), current) );
+        current->addMenu( new CheckableListMenu(qtr( "&Zoom" ), THEMIM->getZoom(), CheckableListMenu::GROUPED, current) );
+        current->addMenu( new CheckableListMenu(qtr( "&Aspect Ratio" ), THEMIM->getAspectRatio(), CheckableListMenu::GROUPED, current) );
+        current->addMenu( new CheckableListMenu(qtr( "&Crop" ), THEMIM->getCrop(), CheckableListMenu::GROUPED, current) );
 
         current->addSeparator();
         /* Rendering modifiers */
-        current->addMenu( new CheckableListMenu(qtr( "&Deinterlace" ), THEMIM->getDeinterlace(), current) );
-        current->addMenu( new CheckableListMenu(qtr( "&Deinterlace mode" ), THEMIM->getDeinterlaceMode(), current) );
+        current->addMenu( new CheckableListMenu(qtr( "&Deinterlace" ), THEMIM->getDeinterlace(), CheckableListMenu::GROUPED, current) );
+        current->addMenu( new CheckableListMenu(qtr( "&Deinterlace mode" ), THEMIM->getDeinterlaceMode(), CheckableListMenu::GROUPED, current) );
 
         current->addSeparator();
         /* Other actions */
@@ -507,11 +507,11 @@ QMenu *VLCMenuBar::NavigMenu( intf_thread_t *p_intf, QMenu *menu )
     QAction *action;
     QMenu *submenu;
 
-    menu->addMenu( new CheckableListMenu( qtr( "T&itle" ), THEMIM->getTitles(), menu) );
-    submenu = new CheckableListMenu( qtr( "&Chapter" ), THEMIM->getChapters(), menu );
+    menu->addMenu( new CheckableListMenu( qtr( "T&itle" ), THEMIM->getTitles(), CheckableListMenu::GROUPED, menu) );
+    submenu = new CheckableListMenu( qtr( "&Chapter" ), THEMIM->getChapters(), CheckableListMenu::GROUPED, menu );
     submenu->setTearOffEnabled( true );
     menu->addMenu( submenu );
-    menu->addMenu( new CheckableListMenu( qtr("&Program") , THEMIM->getPrograms(), menu) );
+    menu->addMenu( new CheckableListMenu( qtr("&Program") , THEMIM->getPrograms(), CheckableListMenu::GROUPED , menu) );
 
     submenu = new QMenu( qtr( I_MENU_BOOKMARK ), menu );
     submenu->setTearOffEnabled( true );
@@ -851,7 +851,7 @@ QMenu* VLCMenuBar::PopupMenu( intf_thread_t *p_intf, bool show )
             }
 
             VLCVarChoiceModel* skinmodel = new VLCVarChoiceModel(p_object, "intf-skins", submenu);
-            CheckableListMenu* skinsubmenu = new CheckableListMenu(qtr("Interface"), skinmodel, submenu);
+            CheckableListMenu* skinsubmenu = new CheckableListMenu(qtr("Interface"), skinmodel, CheckableListMenu::GROUPED, submenu);
             submenu->addMenu(skinsubmenu);
 
             submenu->addSeparator();
-- 
2.25.1



More information about the vlc-devel mailing list