Skip to content
Snippets Groups Projects
Commit 9611235e authored by Romain Vimont's avatar Romain Vimont Committed by Jean-Baptiste Kempf
Browse files

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: default avatarJean-Baptiste Kempf <jb@videolan.org>
parent 22ec1d1b
No related branches found
No related tags found
No related merge requests found
......@@ -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 \
......
/*****************************************************************************
* 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
/*****************************************************************************
* 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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment