Files
DbLogAnalyzer/models/checklistmodel.cpp

183 lines
3.7 KiB
C++
Raw Normal View History

2019-04-01 10:43:36 +02:00
#include "checklistmodel.h"
ChecklistModel::ChecklistModel(QObject *parent) :
QAbstractListModel(parent)
{
}
ChecklistModel::ChecklistModel(const QStringList &items, QObject *parent) :
QAbstractListModel(parent)
{
for (const auto &item : items)
2019-04-07 18:20:01 +02:00
m_items.append({ item, item, true });
2019-04-01 10:43:36 +02:00
}
2019-04-07 18:20:01 +02:00
ChecklistModel::ChecklistModel(const QList<ChecklistItem> &items, QObject *parent) :
2019-04-01 10:43:36 +02:00
QAbstractListModel(parent),
m_items(items)
{
}
2019-04-07 18:20:01 +02:00
const QList<ChecklistModel::ChecklistItem> &ChecklistModel::items() const
2019-04-01 10:43:36 +02:00
{
2019-04-07 18:20:01 +02:00
return m_items;
}
void ChecklistModel::setItems(const QList<ChecklistItem> &items)
{
emit beginResetModel();
2019-04-01 10:43:36 +02:00
2019-04-07 18:20:01 +02:00
m_items = items;
2019-04-01 10:43:36 +02:00
2019-04-07 18:20:01 +02:00
emit endResetModel();
2019-04-01 10:43:36 +02:00
}
void ChecklistModel::setItems(const QStringList &items)
{
emit beginResetModel();
m_items.clear();
for (const auto &item : items)
2019-04-07 18:20:01 +02:00
m_items.append({ item, item, true });
2019-04-01 10:43:36 +02:00
emit endResetModel();
}
2019-04-07 18:20:01 +02:00
QStringList ChecklistModel::itemTexts() const
2019-04-01 10:43:36 +02:00
{
2019-04-07 18:20:01 +02:00
QStringList items;
2019-04-01 10:43:36 +02:00
2019-04-07 18:20:01 +02:00
for (const auto &item : m_items)
items.append(item.displayText);
2019-04-01 10:43:36 +02:00
2019-04-07 18:20:01 +02:00
return items;
2019-04-01 10:43:36 +02:00
}
2019-04-07 18:20:01 +02:00
QVariantList ChecklistModel::itemDatas() const
2019-04-01 10:43:36 +02:00
{
2019-04-07 18:20:01 +02:00
QVariantList items;
2019-04-01 10:43:36 +02:00
2019-04-07 18:20:01 +02:00
for (const auto &item : m_items)
items.append(item.data);
2019-04-01 10:43:36 +02:00
return items;
}
2019-04-07 18:20:01 +02:00
QList<ChecklistModel::ChecklistItem> ChecklistModel::enabledItems() const
{
QList<ChecklistModel::ChecklistItem> items;
for (const auto &item : m_items)
if (item.checked)
items.append(item);
return items;
}
QList<ChecklistModel::ChecklistItem> ChecklistModel::disabledItems() const
{
QList<ChecklistModel::ChecklistItem> items;
for (const auto &item : m_items)
if (!item.checked)
items.append(item);
return items;
}
QStringList ChecklistModel::enabledTexts() const
2019-04-01 10:43:36 +02:00
{
QStringList items;
2019-04-07 18:20:01 +02:00
for (const auto &item : m_items)
if (item.checked)
items.append(item.displayText);
2019-04-01 10:43:36 +02:00
return items;
}
2019-04-07 18:20:01 +02:00
QStringList ChecklistModel::disabledTexts() const
2019-04-01 10:43:36 +02:00
{
2019-04-07 18:20:01 +02:00
QStringList items;
for (const auto &item : m_items)
if (!item.checked)
items.append(item.displayText);
return items;
2019-04-01 10:43:36 +02:00
}
2019-04-07 18:20:01 +02:00
QVariantList ChecklistModel::enabledItemDatas() const
2019-04-01 10:43:36 +02:00
{
2019-04-07 18:20:01 +02:00
QVariantList items;
for (const auto &item : m_items)
if (item.checked)
items.append(item.data);
return items;
2019-04-01 10:43:36 +02:00
}
2019-04-07 18:20:01 +02:00
QVariantList ChecklistModel::disabledItemDatas() const
2019-04-01 10:43:36 +02:00
{
2019-04-07 18:20:01 +02:00
QVariantList items;
for (const auto &item : m_items)
if (!item.checked)
items.append(item.data);
return items;
2019-04-01 10:43:36 +02:00
}
2019-04-07 18:20:01 +02:00
int ChecklistModel::rowCount(const QModelIndex &parent) const
2019-04-01 10:43:36 +02:00
{
2019-04-07 18:20:01 +02:00
if (parent.isValid())
return 0;
return m_items.count();
2019-04-01 10:43:36 +02:00
}
QVariant ChecklistModel::data(const QModelIndex &index, int role) const
{
if (index.row() < 0 || index.row() >= m_items.size())
2019-04-07 18:20:01 +02:00
return {};
2019-04-01 10:43:36 +02:00
const auto &item = m_items.at(index.row());
2019-04-07 18:20:01 +02:00
switch (role)
{
case Qt::DisplayRole: return item.displayText;
case Qt::EditRole: return item.data;
case Qt::CheckStateRole: return item.checked ? Qt::Checked : Qt::Unchecked;
}
2019-04-01 10:43:36 +02:00
return QVariant();
}
bool ChecklistModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
2019-04-07 18:20:01 +02:00
if (index.row() < 0 || index.row() >= m_items.size())
return false;
auto &item = m_items[index.row()];
switch (role)
2019-04-01 10:43:36 +02:00
{
2019-04-07 18:20:01 +02:00
case Qt::CheckStateRole:
item.checked = value.toBool();
2019-04-01 10:43:36 +02:00
emit dataChanged(index, index, { Qt::CheckStateRole });
return true;
}
return false;
}
Qt::ItemFlags ChecklistModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return QAbstractListModel::flags(index);
2019-04-07 18:20:01 +02:00
2019-04-01 10:43:36 +02:00
return QAbstractListModel::flags(index) | Qt::ItemIsUserCheckable;
}