LedDisplay/LedDisplayController/leddisplaytextmodel.cpp

152 lines
4 KiB
C++
Raw Normal View History

2020-10-18 15:08:12 +02:00
#include "leddisplaytextmodel.h"
2020-10-11 21:01:21 +02:00
2020-10-18 15:08:12 +02:00
LedDisplayTextModel::LedDisplayTextModel(QObject* parent) : QAbstractListModel(parent)
2020-10-11 21:01:21 +02:00
{
this->maximumTextSets = 0;
this->maximumTextLength = 0;
2020-10-18 15:08:12 +02:00
connect(this, &LedDisplayTextModel::rowsInserted, this, &LedDisplayTextModel::rowCountChanged);
connect(this, &LedDisplayTextModel::rowsRemoved, this, &LedDisplayTextModel::rowCountChanged);
2020-10-11 21:01:21 +02:00
}
2020-10-18 15:08:12 +02:00
int LedDisplayTextModel::rowCount(const QModelIndex &) const
2020-10-11 21:01:21 +02:00
{
return this->texts.length();
}
2020-10-18 15:08:12 +02:00
QVariant LedDisplayTextModel::data(const QModelIndex &index, int role) const
2020-10-11 21:01:21 +02:00
{
return this->data(index.row(), role);
}
2020-10-18 15:08:12 +02:00
QVariant LedDisplayTextModel::data(int row, int role) const {
if (row < rowCount()) {
if(this->texts[row].contains(role)) {
if(role == IndexRole)
return row;
QVariant value = this->texts[row][role];
2020-10-18 15:08:12 +02:00
value.convert(this->roleDataTypes[LedDisplayTextModelRole(role)]);
return value;
}
}
2020-10-11 21:01:21 +02:00
return QVariant();
}
2020-10-18 15:08:12 +02:00
QHash<int, QByteArray> LedDisplayTextModel::roleNames() const
2020-10-11 21:01:21 +02:00
{
static const QHash<int, QByteArray> roles {
{ TextRole, "text" },
{ ActiveRole, "active" },
{ RuntimeRole, "runtime" },
{ ColorRole, "color" },
{ AlignmentRole, "alignment" },
{ ScrollRole, "scroll" },
2020-10-12 14:13:52 +02:00
{ ScrollSpeedRole, "scrollSpeed" },
{ ScrollCountRole, "scrollCount" },
{ IndexRole, "index" }
2020-10-11 21:01:21 +02:00
};
return roles;
}
2020-10-18 15:08:12 +02:00
void LedDisplayTextModel::append(
2020-10-11 21:01:21 +02:00
QString text,
bool active,
unsigned int runtime,
QString color,
QString alignment,
bool scroll,
2020-10-12 14:13:52 +02:00
unsigned int scrollSpeed,
unsigned int scrollCount
2020-10-11 21:01:21 +02:00
) {
2020-10-13 17:00:25 +02:00
// check length maximums
if(this->texts.length() >= this->maximumTextSets)
2020-10-13 17:00:25 +02:00
return;
if(text.length() > this->maximumTextLength)
text = text.left(this->maximumTextLength);
2020-10-11 21:01:21 +02:00
QMap<int, QVariant> roles = {
{ TextRole, text },
{ ActiveRole, active },
{ RuntimeRole, runtime },
{ ColorRole, color },
{ AlignmentRole, alignment },
{ ScrollRole, scroll },
2020-10-12 14:13:52 +02:00
{ ScrollSpeedRole, scrollSpeed },
{ ScrollCountRole, scrollCount }
2020-10-11 21:01:21 +02:00
};
this->append(roles);
}
2020-10-18 15:08:12 +02:00
void LedDisplayTextModel::append(const QMap<int, QVariant> &roles) {
2020-10-11 21:01:21 +02:00
int row = this->texts.length();
this->beginInsertRows(QModelIndex(), row, row);
this->texts.insert(row, roles);
this->endInsertRows();
}
2020-10-18 15:08:12 +02:00
bool LedDisplayTextModel::setData(const QModelIndex &index, const QVariant &value, int role) {
2020-10-11 21:01:21 +02:00
if (index.row() >= rowCount() || !this->texts[index.row()].contains(role))
return false;
if(this->texts[index.row()][role] == value)
return true;
if(role == TextRole && value.toString().length() > this->maximumTextLength)
this->texts[index.row()][role] = value.toString().left(this->maximumTextLength);
else
this->texts[index.row()][role] = value;
qDebug() << "setting index: " << index << " role: " << role << " value " << value;
2020-10-11 21:01:21 +02:00
emit dataChanged(index, index, QVector<int>({role}));
2020-10-11 21:01:21 +02:00
return true;
2020-10-11 21:01:21 +02:00
}
2020-10-18 15:08:12 +02:00
void LedDisplayTextModel::remove(int row)
2020-10-11 21:01:21 +02:00
{
if (row < 0 || row >= this->rowCount())
return;
beginRemoveRows(QModelIndex(), row, row);
this->texts.removeAt(row);
endRemoveRows();
}
2020-10-18 15:08:12 +02:00
void LedDisplayTextModel::clear() {
2020-10-11 21:01:21 +02:00
for(int i = 0; i < this->texts.length(); i++)
this->remove(i);
}
2020-10-18 15:08:12 +02:00
bool LedDisplayTextModel::setTexts(QList<QMap<int, QVariant>> texts) {
this->beginResetModel();
this->resetInternalData();
this->texts.clear();
this->endResetModel();
for(QMap<int, QVariant> text : texts)
this->append(text);
return true;
}
2020-10-18 15:08:12 +02:00
QList<QMap<int, QVariant>> LedDisplayTextModel::getTexts() {
return this->texts;
2020-10-11 21:01:21 +02:00
}
2020-10-18 15:08:12 +02:00
QMap<int, QVariant> LedDisplayTextModel::getText(const QModelIndex &index) {
if (index.row() < this->rowCount())
return this->texts[index.row()];
return QMap<int, QVariant>();
}
2020-10-18 15:08:12 +02:00
int LedDisplayTextModel::getMaximumTextSets() {
return this->maximumTextSets;
}