2016-07-04 23:26:48 +02:00
|
|
|
#ifndef MODELS_CHECKLISTMODEL_H
|
|
|
|
#define MODELS_CHECKLISTMODEL_H
|
2015-04-22 18:57:44 +02:00
|
|
|
|
2016-08-29 15:41:11 +02:00
|
|
|
#include "../global.h"
|
2015-04-22 18:57:44 +02:00
|
|
|
|
|
|
|
#include <QAbstractListModel>
|
|
|
|
#include <QList>
|
|
|
|
|
2016-04-09 02:33:09 +02:00
|
|
|
QT_FORWARD_DECLARE_CLASS(QSettings)
|
2015-04-22 18:57:44 +02:00
|
|
|
|
|
|
|
namespace Models {
|
|
|
|
|
|
|
|
class ChecklistModel;
|
|
|
|
|
2017-05-01 03:16:25 +02:00
|
|
|
class QT_UTILITIES_EXPORT ChecklistItem {
|
2015-04-22 18:57:44 +02:00
|
|
|
friend class ChecklistModel;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ChecklistItem(const QVariant &id = QVariant(), const QString &label = QString(), Qt::CheckState checked = Qt::Unchecked);
|
|
|
|
|
|
|
|
const QVariant &id() const;
|
|
|
|
const QString &label() const;
|
|
|
|
Qt::CheckState checkState() const;
|
|
|
|
bool isChecked() const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
QVariant m_id;
|
|
|
|
QString m_label;
|
|
|
|
Qt::CheckState m_checkState;
|
|
|
|
};
|
|
|
|
|
2017-05-01 03:16:25 +02:00
|
|
|
inline ChecklistItem::ChecklistItem(const QVariant &id, const QString &label, Qt::CheckState checkState)
|
|
|
|
: m_id(id)
|
|
|
|
, m_label(label)
|
|
|
|
, m_checkState(checkState)
|
|
|
|
{
|
|
|
|
}
|
2015-04-22 18:57:44 +02:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Returns the ID of the item.
|
|
|
|
*/
|
|
|
|
inline const QVariant &ChecklistItem::id() const
|
|
|
|
{
|
|
|
|
return m_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Returns the label.
|
|
|
|
*/
|
|
|
|
inline const QString &ChecklistItem::label() const
|
|
|
|
{
|
|
|
|
return m_label;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Returns the check state.
|
|
|
|
*/
|
|
|
|
inline Qt::CheckState ChecklistItem::checkState() const
|
|
|
|
{
|
|
|
|
return m_checkState;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Returns whether the item is checked.
|
|
|
|
*/
|
|
|
|
|
|
|
|
inline bool ChecklistItem::isChecked() const
|
|
|
|
{
|
|
|
|
return m_checkState == Qt::Checked;
|
|
|
|
}
|
|
|
|
|
2017-05-01 03:16:25 +02:00
|
|
|
class QT_UTILITIES_EXPORT ChecklistModel : public QAbstractListModel {
|
2015-04-22 18:57:44 +02:00
|
|
|
Q_OBJECT
|
|
|
|
public:
|
|
|
|
explicit ChecklistModel(QObject *parent = nullptr);
|
|
|
|
|
|
|
|
int rowCount(const QModelIndex &parent = QModelIndex()) const;
|
|
|
|
Qt::ItemFlags flags(const QModelIndex &index) const;
|
|
|
|
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
|
|
|
|
QMap<int, QVariant> itemData(const QModelIndex &index) const;
|
|
|
|
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::DisplayRole);
|
|
|
|
bool setItemData(const QModelIndex &index, const QMap<int, QVariant> &roles);
|
|
|
|
virtual QString labelForId(const QVariant &id) const;
|
|
|
|
Qt::DropActions supportedDropActions() const;
|
|
|
|
bool insertRows(int row, int count, const QModelIndex &parent);
|
|
|
|
bool removeRows(int row, int count, const QModelIndex &parent);
|
|
|
|
const QList<ChecklistItem> &items() const;
|
|
|
|
void setItems(const QList<ChecklistItem> &items);
|
|
|
|
void restore(QSettings &settings, const QString &name);
|
|
|
|
void save(QSettings &settings, const QString &name) const;
|
|
|
|
static inline constexpr int idRole();
|
|
|
|
|
|
|
|
private:
|
|
|
|
QList<ChecklistItem> m_items;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Returns the items.
|
|
|
|
*/
|
|
|
|
inline const QList<ChecklistItem> &ChecklistModel::items() const
|
|
|
|
{
|
|
|
|
return m_items;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Returns the role used to get or set the item ID.
|
|
|
|
*/
|
|
|
|
inline constexpr int ChecklistModel::idRole()
|
|
|
|
{
|
|
|
|
return Qt::UserRole + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-04 23:26:48 +02:00
|
|
|
#endif // MODELS_CHECKLISTMODEL_H
|