vlc | branch: master | Romain Vimont <[email protected]> | Tue Jul 9 18:30:26 2019 +0200| [9611235e4dbf9e10f557f01028898bff991ec9c6] | committer: Jean-Baptiste Kempf
qt: introduce SelectableListModel Add a QAbstractListModel subclass which maintain a selection state. This will avoid to manage selection in an additional layer (DelegateModel) in Qml. Signed-off-by: Jean-Baptiste Kempf <[email protected]> > http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=9611235e4dbf9e10f557f01028898bff991ec9c6 --- modules/gui/qt/Makefile.am | 3 + .../gui/qt/components/selectable_list_model.cpp | 119 +++++++++++++++++++++ .../gui/qt/components/selectable_list_model.hpp | 52 +++++++++ 3 files changed, 174 insertions(+) diff --git a/modules/gui/qt/Makefile.am b/modules/gui/qt/Makefile.am index d4e5193af0..0055489d13 100644 --- a/modules/gui/qt/Makefile.am +++ b/modules/gui/qt/Makefile.am @@ -93,6 +93,8 @@ libqt_plugin_la_SOURCES = \ gui/qt/components/player_controller.cpp gui/qt/components/player_controller.hpp gui/qt/components/player_controller_p.hpp \ gui/qt/components/preferences_widgets.cpp \ gui/qt/components/preferences_widgets.hpp \ + gui/qt/components/selectable_list_model.hpp \ + gui/qt/components/selectable_list_model.cpp \ gui/qt/components/complete_preferences.cpp \ gui/qt/components/complete_preferences.hpp \ gui/qt/components/simple_preferences.cpp \ @@ -260,6 +262,7 @@ nodist_libqt_plugin_la_SOURCES = \ gui/qt/components/controller_widget.moc.cpp \ gui/qt/components/custom_menus.moc.cpp \ gui/qt/components/recent_media_model.moc.cpp \ + gui/qt/components/selectable_list_model.moc.cpp \ gui/qt/components/settings.moc.cpp \ gui/qt/components/voutwindow/videosurface.moc.cpp \ gui/qt/components/voutwindow/qvoutwindow.moc.cpp \ diff --git a/modules/gui/qt/components/selectable_list_model.cpp b/modules/gui/qt/components/selectable_list_model.cpp new file mode 100644 index 0000000000..d9564a9ee6 --- /dev/null +++ b/modules/gui/qt/components/selectable_list_model.cpp @@ -0,0 +1,119 @@ +/***************************************************************************** + * Copyright (C) 2019 VLC authors and VideoLAN + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * ( at your option ) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. + *****************************************************************************/ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#include "selectable_list_model.hpp" + +namespace vlc { + +void SelectableListModel::setSelected(int row, bool selected) +{ + setRowSelected(row, selected); + + QModelIndex modelIndex = index(row); + emit dataChanged(modelIndex, modelIndex, { getSelectedRole() }); +} + +bool SelectableListModel::isSelected(int row) const +{ + return isRowSelected(row); +} + +void SelectableListModel::toggleSelected(int row) +{ + setRowSelected(row, !isRowSelected(row)); + + QModelIndex modelIndex = index(row); + emit dataChanged(modelIndex, modelIndex, { getSelectedRole() }); +} + +void SelectableListModel::setSelection(const QList<int> &sortedIndexes) +{ + int itemsCount = rowCount(); + + if (!itemsCount) + return; + + int indexesCount = sortedIndexes.size(); + int p = 0; /* points to the current index in sortedIndexes */ + for (int i = 0; i < itemsCount; ++i) + { + /* the indexes are sorted, so only check the next one */ + if (p < indexesCount && i == sortedIndexes[p]) + { + setRowSelected(i, true); + ++p; + } + else + { + setRowSelected(i, false); + } + } + + QModelIndex first = index(0); + QModelIndex last = index(itemsCount - 1); + emit dataChanged(first, last, { getSelectedRole() }); +} + +QList<int> SelectableListModel::getSelection() const +{ + int itemsCount = rowCount(); + + QList<int> indexes; + for (int i = 0; i < itemsCount; ++i) + { + if (isRowSelected(i)) + indexes.push_back(i); + } + return indexes; +} + +void SelectableListModel::setRangeSelected(int start, int count, bool selected) +{ + for (int i = start; i < start + count; ++i) + setRowSelected(i, selected); + + QModelIndex first = index(start); + QModelIndex last = index(start + count - 1); + emit dataChanged(first, last, { getSelectedRole() }); +} + +void SelectableListModel::selectAll() +{ + int count = rowCount(); + + if (!count) + return; + + setRangeSelected(0, count, true); +} + +void SelectableListModel::deselectAll() +{ + int count = rowCount(); + + if (!count) + return; + + setRangeSelected(0, count, false); +} + +} // namespace vlc diff --git a/modules/gui/qt/components/selectable_list_model.hpp b/modules/gui/qt/components/selectable_list_model.hpp new file mode 100644 index 0000000000..9795ff6dcf --- /dev/null +++ b/modules/gui/qt/components/selectable_list_model.hpp @@ -0,0 +1,52 @@ +/***************************************************************************** + * Copyright (C) 2019 VLC authors and VideoLAN + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * ( at your option ) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. + *****************************************************************************/ + +#ifndef VLC_QT_SELECTABLE_LIST_MODEL_HPP_ +#define VLC_QT_SELECTABLE_LIST_MODEL_HPP_ + +#include <QAbstractListModel> + +namespace vlc { + +class SelectableListModel : public QAbstractListModel +{ + Q_OBJECT +public: + SelectableListModel(QObject *parent = nullptr) : + QAbstractListModel(parent) {} + + Q_INVOKABLE bool isSelected(int index) const; + Q_INVOKABLE void setSelected(int index, bool selected); + Q_INVOKABLE void toggleSelected(int index); + Q_INVOKABLE void setSelection(const QList<int> &sortedIndexes); + Q_INVOKABLE QList<int> getSelection() const; + Q_INVOKABLE void setRangeSelected(int first, int count, bool selected); + Q_INVOKABLE void selectAll(); + Q_INVOKABLE void deselectAll(); + +protected: + virtual bool isRowSelected(int row) const = 0; + virtual void setRowSelected(int row, bool selected) = 0; + + /* return the role to notify when selection changes */ + virtual int getSelectedRole() const = 0; +}; + +} // namespace vlc + +#endif _______________________________________________ vlc-commits mailing list [email protected] https://mailman.videolan.org/listinfo/vlc-commits
