[vlc-commits] [Git][videolan/vlc][master] 2 commits: qt/custom_menus: Update the RecentMenu 'insert' implementation

Hugo Beauzée-Luyssen (@chouquette) gitlab at videolan.org
Thu Mar 17 09:39:44 UTC 2022



Hugo Beauzée-Luyssen pushed to branch master at VideoLAN / VLC


Commits:
12cfeb09 by Benjamin Arnaud at 2022-03-17T09:17:17+00:00
qt/custom_menus: Update the RecentMenu 'insert' implementation

Our previous implementation couldn't handle a random insert in the middle of the model.

- - - - -
2eb7186a by Benjamin Arnaud at 2022-03-17T09:17:17+00:00
qt/custom_menus: Simplify the model events implementation

- - - - -


2 changed files:

- modules/gui/qt/menus/custom_menus.cpp
- modules/gui/qt/menus/custom_menus.hpp


Changes:

=====================================
modules/gui/qt/menus/custom_menus.cpp
=====================================
@@ -327,39 +327,56 @@ RecentMenu::RecentMenu(MLRecentsModel* model, MediaLib* ml,  QWidget* parent)
     , m_model(model)
     , m_ml(ml)
 {
-    connect(m_model, &MLRecentsModel::rowsAboutToBeRemoved, this, &RecentMenu::onRowsAboutToBeRemoved);
+    connect(m_model, &MLRecentsModel::rowsRemoved, this, &RecentMenu::onRowsRemoved);
     connect(m_model, &MLRecentsModel::rowsInserted, this, &RecentMenu::onRowInserted);
     connect(m_model, &MLRecentsModel::dataChanged, this, &RecentMenu::onDataChanged);
-    connect(m_model, &MLRecentsModel::modelAboutToBeReset, this, &RecentMenu::onModelAboutToBeReset);
     connect(m_model, &MLRecentsModel::modelReset, this, &RecentMenu::onModelReset);
     m_separator = addSeparator();
     addAction( qtr("&Clear"), m_model, &MLRecentsModel::clearHistory );
     onModelReset();
 }
 
-void RecentMenu::onRowsAboutToBeRemoved(const QModelIndex&, int first, int last)
+void RecentMenu::onRowsRemoved(const QModelIndex&, int first, int last)
 {
-    for (int i = last; i >= first; i--)
+    for (int i = first; i <= last; i++)
     {
-        QAction* action = actions()[i];
-        delete action;
+        delete m_actions.at(i);
     }
-    if (actions().count() == 0)
+
+    QList<QAction *>::iterator begin = m_actions.begin();
+
+    m_actions.erase(begin + first, begin + last);
+
+    if (m_actions.isEmpty())
         setEnabled(false);
 }
 
 void RecentMenu::onRowInserted(const QModelIndex&, int first, int last)
 {
+    QAction * before;
+
+    if (first < m_actions.count())
+        before = m_actions.at(first);
+    else
+        // NOTE: In that case we insert *before* the 'Clear' separator.
+        before = m_separator;
+
     for (int i = first; i <= last; i++)
     {
         QModelIndex index = m_model->index(i);
         QString url = m_model->data(index, MLRecentsModel::RECENT_MEDIA_URL).toString();
 
         QAction *choiceAction = new QAction(url, this);
-        insertAction(m_separator , choiceAction);
-        connect(choiceAction, &QAction::triggered, [this, i](){
-            QModelIndex dataIndex = m_model->index(i);
-            MLItemId id = m_model->data(dataIndex, MLRecentsModel::RECENT_MEDIA_ID).value<MLItemId>();
+
+        // NOTE: We are adding sequentially *before* the next action in the list.
+        insertAction(before, choiceAction);
+
+        m_actions.insert(i, choiceAction);
+
+        connect(choiceAction, &QAction::triggered, [this, choiceAction](){
+            QModelIndex index = m_model->index(m_actions.indexOf(choiceAction));
+
+            MLItemId id = m_model->data(index, MLRecentsModel::RECENT_MEDIA_ID).value<MLItemId>();
             m_ml->addAndPlay(id);
         });
         setEnabled(true);
@@ -370,28 +387,22 @@ void RecentMenu::onDataChanged(const QModelIndex& topLeft, const QModelIndex& bo
 {
     for (int i = topLeft.row(); i <= bottomRight.row(); i++)
     {
-        QAction *choiceAction = actions()[i];
-
         QModelIndex index = m_model->index(i);
         QString title = m_model->data(index, MLRecentsModel::RECENT_MEDIA_URL).toString();
 
-        choiceAction->setText(title);
+        m_actions.at(i)->setText(title);
     }
 }
 
-void RecentMenu::onModelAboutToBeReset()
+void RecentMenu::onModelReset()
 {
-    for (QAction* action  :actions())
+    for (QAction * action : m_actions)
     {
-        if (action == m_separator)
-            break;
         delete action;
     }
-    setEnabled(false);
-}
 
-void RecentMenu::onModelReset()
-{
+    m_actions.clear();
+
     int nb_rows = m_model->rowCount();
     if (nb_rows == 0)
         setEnabled(false);


=====================================
modules/gui/qt/menus/custom_menus.hpp
=====================================
@@ -132,16 +132,17 @@ public:
     RecentMenu(MLRecentsModel* model, MediaLib* ml, QWidget *parent = nullptr);
 
 private slots:
-    void onRowsAboutToBeRemoved(const QModelIndex &parent, int first, int last);
+    void onRowsRemoved(const QModelIndex &parent, int first, int last);
     void onRowInserted(const QModelIndex &parent, int first, int last);
     void onDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles = QVector<int>());
-    void onModelAboutToBeReset();
     void onModelReset();
 
 private:
     MLRecentsModel* m_model = nullptr;
     QAction* m_separator = nullptr;
     MediaLib* m_ml = nullptr;
+
+    QList<QAction *> m_actions;
 };
 
 



View it on GitLab: https://code.videolan.org/videolan/vlc/-/compare/9dcae0aa3ece449ac01d81ea5f414da0b69299c3...2eb7186a550c6f509b5bb9ef822cf46335928a4f

-- 
View it on GitLab: https://code.videolan.org/videolan/vlc/-/compare/9dcae0aa3ece449ac01d81ea5f414da0b69299c3...2eb7186a550c6f509b5bb9ef822cf46335928a4f
You're receiving this email because of your account on code.videolan.org.


VideoLAN code repository instance


More information about the vlc-commits mailing list