[vlc-devel] [PATCH 1/9] qt: add a selectedCount property SelectableListModel to track the number of selected items

Pierre Lamot pierre at videolabs.io
Mon Nov 18 18:15:34 CET 2019


---
 .../gui/qt/components/playlist/playlist_model.cpp |  2 ++
 .../gui/qt/components/selectable_list_model.cpp   | 15 +++++++++++++++
 .../gui/qt/components/selectable_list_model.hpp   |  7 +++++++
 3 files changed, 24 insertions(+)

diff --git a/modules/gui/qt/components/playlist/playlist_model.cpp b/modules/gui/qt/components/playlist/playlist_model.cpp
index 49b8aaad22..1b571ae2ae 100644
--- a/modules/gui/qt/components/playlist/playlist_model.cpp
+++ b/modules/gui/qt/components/playlist/playlist_model.cpp
@@ -180,6 +180,7 @@ void PlaylistListModelPrivate::onItemsReset(const QVector<PlaylistItem>& newCont
     q->endResetModel();
 
     emit q->countChanged(m_items.size());
+    emit q->selectedCountChanged();
 }
 
 void PlaylistListModelPrivate::onItemsAdded(const QVector<PlaylistItem>& added, size_t index)
@@ -224,6 +225,7 @@ void PlaylistListModelPrivate::onItemsRemoved(size_t index, size_t count)
     q->endRemoveRows();
 
     emit q->countChanged(m_items.size());
+    emit q->selectedCountChanged();
 }
 
 
diff --git a/modules/gui/qt/components/selectable_list_model.cpp b/modules/gui/qt/components/selectable_list_model.cpp
index d9564a9ee6..cf6711761d 100644
--- a/modules/gui/qt/components/selectable_list_model.cpp
+++ b/modules/gui/qt/components/selectable_list_model.cpp
@@ -30,6 +30,7 @@ void SelectableListModel::setSelected(int row, bool selected)
 
     QModelIndex modelIndex = index(row);
     emit dataChanged(modelIndex, modelIndex, { getSelectedRole() });
+    emit selectedCountChanged();
 }
 
 bool SelectableListModel::isSelected(int row) const
@@ -43,6 +44,7 @@ void SelectableListModel::toggleSelected(int row)
 
     QModelIndex modelIndex = index(row);
     emit dataChanged(modelIndex, modelIndex, { getSelectedRole() });
+    emit selectedCountChanged();
 }
 
 void SelectableListModel::setSelection(const QList<int> &sortedIndexes)
@@ -71,6 +73,7 @@ void SelectableListModel::setSelection(const QList<int> &sortedIndexes)
     QModelIndex first = index(0);
     QModelIndex last = index(itemsCount - 1);
     emit dataChanged(first, last, { getSelectedRole() });
+    emit selectedCountChanged();
 }
 
 QList<int> SelectableListModel::getSelection() const
@@ -94,6 +97,7 @@ void SelectableListModel::setRangeSelected(int start, int count, bool selected)
     QModelIndex first = index(start);
     QModelIndex last = index(start + count - 1);
     emit dataChanged(first, last, { getSelectedRole() });
+    emit selectedCountChanged();
 }
 
 void SelectableListModel::selectAll()
@@ -116,4 +120,15 @@ void SelectableListModel::deselectAll()
     setRangeSelected(0, count, false);
 }
 
+int SelectableListModel::getSelectedCount() const
+{
+    int itemsCount = rowCount();
+    int count = 0;
+    for (int i = 0 ; i < itemsCount; i++) {
+        if (isRowSelected(i))
+            count++;
+    }
+    return count;
+}
+
 } // namespace vlc
diff --git a/modules/gui/qt/components/selectable_list_model.hpp b/modules/gui/qt/components/selectable_list_model.hpp
index 9795ff6dcf..11375a09e4 100644
--- a/modules/gui/qt/components/selectable_list_model.hpp
+++ b/modules/gui/qt/components/selectable_list_model.hpp
@@ -26,6 +26,8 @@ namespace vlc {
 class SelectableListModel : public QAbstractListModel
 {
     Q_OBJECT
+
+    Q_PROPERTY(int selectedCount READ getSelectedCount NOTIFY selectedCountChanged)
 public:
     SelectableListModel(QObject *parent = nullptr) :
         QAbstractListModel(parent) {}
@@ -39,6 +41,11 @@ public:
     Q_INVOKABLE void selectAll();
     Q_INVOKABLE void deselectAll();
 
+    virtual int getSelectedCount() const;
+
+signals:
+    void selectedCountChanged();
+
 protected:
     virtual bool isRowSelected(int row) const = 0;
     virtual void setRowSelected(int row, bool selected) = 0;
-- 
2.17.1



More information about the vlc-devel mailing list