added a serviceitemmodel and working on implementing move and insert

This commit is contained in:
Chris Cochrun 2022-03-29 09:21:28 -05:00
parent 2857ce6c2b
commit 105c958810
9 changed files with 396 additions and 159 deletions

View file

@ -1,88 +1,96 @@
#include "serviceitem.h"
#include <qvariant.h>
ServiceItem::ServiceItem(QObject *parent)
: QAbstractListModel(parent)
: QObject{parent}
{
}
int ServiceItem::rowCount(const QModelIndex &parent) const
ServiceItem::ServiceItem(const QString &name, const QString &type, QObject *parent)
: QObject(parent),m_name(name),m_type(type)
{
// For list models only the root node (an invalid parent) should return the list's size. For all
// other (valid) parents, rowCount() should return 0 so that it does not become a tree model.
if (parent.isValid())
return 0;
// FIXME: Implement me!
return m_data.count();
}
QVariant ServiceItem::data(const QModelIndex &index, int role) const
ServiceItem::ServiceItem(const QString &name, const QString &type, const QString &background,
const QString &backgroundType, QObject *parent)
: QObject(parent),m_name(name),m_type(type),m_background(background),m_backgroundType(backgroundType)
{
if (!index.isValid())
return QVariant();
const Data &data = m_data.at(index.row());
switch (role) {
case NameRole:
return data.name;
case TypeRole:
return data.type;
case BackgroundRole:
return data.background;
case BackgroundTypeRole:
return data.backgroundType;
case TextRole:
return data.text;
default:
return QVariant();
}
}
QHash<int, QByteArray> ServiceItem::roleNames() const {
static QHash<int, QByteArray> mapping {
{NameRole, "name"},
{TypeRole, "type"},
{BackgroundRole, "background"},
{BackgroundTypeRole, "backgroundType"},
{TextRole, "text"}
};
return mapping;
}
bool ServiceItem::setData(const QModelIndex &index,
const QVariant &value,
int role) {
const Data &oldData = m_data.at(index.row());
const QVariant newData = data(index, role);
if (newData != value) {
// FIXME: Implement me!
switch (role) {
case NameRole:
m_data.at(index.row()).name = newData.toString();
case TypeRole:
return data.type;
case BackgroundRole:
return data.background;
case BackgroundTypeRole:
return data.backgroundType;
case TextRole:
return data.text;
default:
return QVariant();
}
oldData = newData;
emit dataChanged(index, index, QVector<int>() << role);
return true;
}
return false;
}
Qt::ItemFlags ServiceItem::flags(const QModelIndex &index) const
ServiceItem::ServiceItem(const QString &name, const QString &type, const QString &background,
const QString &backgroundType, const QStringList &text, QObject *parent)
: QObject(parent),m_name(name),m_type(type),m_background(background),m_backgroundType(backgroundType),m_text(text)
{
if (!index.isValid())
return Qt::NoItemFlags;
return Qt::ItemIsEditable; // FIXME: Implement me!
}
QString ServiceItem::name() const {
return m_name;
}
QString ServiceItem::type() const {
return m_type;
}
QString ServiceItem::background() const
{
return m_background;
}
QString ServiceItem::backgroundType() const
{
return m_backgroundType;
}
QStringList ServiceItem::text() const
{
return m_text;
}
void ServiceItem::setName(QString name)
{
if (m_name == name)
return;
m_name = name;
emit nameChanged(m_name);
}
void ServiceItem::setType(QString type)
{
if (m_type == type)
return;
m_type = type;
emit typeChanged(m_type);
}
void ServiceItem::setBackground(QString background)
{
if (m_background == background)
return;
m_background = background;
emit backgroundChanged(m_background);
}
void ServiceItem::setBackgroundType(QString backgroundType)
{
if (m_backgroundType == backgroundType)
return;
m_backgroundType = backgroundType;
emit backgroundTypeChanged(m_backgroundType);
}
void ServiceItem::setText(QStringList text)
{
if (m_text == text)
return;
m_text = text;
emit textChanged(m_text);
}