2020-10-11 21:01:21 +02:00
|
|
|
#include "omobidisplaytextmodel.h"
|
|
|
|
|
|
|
|
OmobiDisplayTextModel::OmobiDisplayTextModel(QObject* parent) : QAbstractListModel(parent)
|
|
|
|
{
|
2020-10-14 23:54:12 +02:00
|
|
|
this->maximumTextSets = 0;
|
|
|
|
this->maximumTextLength = 0;
|
|
|
|
|
|
|
|
connect(this, &OmobiDisplayTextModel::rowsInserted, this, &OmobiDisplayTextModel::rowCountChanged);
|
|
|
|
connect(this, &OmobiDisplayTextModel::rowsRemoved, this, &OmobiDisplayTextModel::rowCountChanged);
|
2020-10-11 21:01:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int OmobiDisplayTextModel::rowCount(const QModelIndex &) const
|
|
|
|
{
|
|
|
|
return this->texts.length();
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant OmobiDisplayTextModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
2020-10-14 23:54:12 +02:00
|
|
|
return this->data(index.row(), role);
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant OmobiDisplayTextModel::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];
|
|
|
|
value.convert(this->roleDataTypes[OmobiDisplayTextModelRole(role)]);
|
|
|
|
return value;
|
|
|
|
}
|
2020-10-13 01:58:14 +02:00
|
|
|
}
|
2020-10-11 21:01:21 +02:00
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
QHash<int, QByteArray> OmobiDisplayTextModel::roleNames() const
|
|
|
|
{
|
|
|
|
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" },
|
2020-10-13 01:58:14 +02:00
|
|
|
{ IndexRole, "index" }
|
2020-10-11 21:01:21 +02:00
|
|
|
};
|
|
|
|
return roles;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OmobiDisplayTextModel::append(
|
|
|
|
QString text,
|
|
|
|
bool active,
|
|
|
|
unsigned int runtime,
|
|
|
|
QString color,
|
|
|
|
QString alignment,
|
|
|
|
bool scroll,
|
2020-10-12 14:13:52 +02:00
|
|
|
unsigned int scrollSpeed,
|
2020-10-13 01:58:14 +02:00
|
|
|
unsigned int scrollCount
|
2020-10-11 21:01:21 +02:00
|
|
|
) {
|
2020-10-13 17:00:25 +02:00
|
|
|
|
2020-10-14 23:54:12 +02:00
|
|
|
// check length maximums
|
|
|
|
if(this->texts.length() >= this->maximumTextSets)
|
2020-10-13 17:00:25 +02:00
|
|
|
return;
|
|
|
|
|
2020-10-14 23:54:12 +02:00
|
|
|
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 },
|
2020-10-13 01:58:14 +02:00
|
|
|
{ ScrollCountRole, scrollCount }
|
2020-10-11 21:01:21 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
this->append(roles);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OmobiDisplayTextModel::append(const QMap<int, QVariant> &roles) {
|
|
|
|
int row = this->texts.length();
|
|
|
|
this->beginInsertRows(QModelIndex(), row, row);
|
|
|
|
this->texts.insert(row, roles);
|
|
|
|
this->endInsertRows();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OmobiDisplayTextModel::setData(const QModelIndex &index, const QVariant &value, int role) {
|
|
|
|
if (index.row() >= rowCount() || !this->texts[index.row()].contains(role))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if(this->texts[index.row()][role] == value)
|
|
|
|
return true;
|
|
|
|
|
2020-10-14 23:54:12 +02:00
|
|
|
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
|
|
|
|
2020-10-14 23:54:12 +02:00
|
|
|
emit dataChanged(index, index, QVector<int>({role}));
|
2020-10-11 21:01:21 +02:00
|
|
|
|
2020-10-14 23:54:12 +02:00
|
|
|
return true;
|
2020-10-11 21:01:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void OmobiDisplayTextModel::remove(int row)
|
|
|
|
{
|
|
|
|
if (row < 0 || row >= this->rowCount())
|
|
|
|
return;
|
|
|
|
|
|
|
|
beginRemoveRows(QModelIndex(), row, row);
|
|
|
|
this->texts.removeAt(row);
|
|
|
|
endRemoveRows();
|
|
|
|
}
|
|
|
|
|
|
|
|
void OmobiDisplayTextModel::clear() {
|
|
|
|
for(int i = 0; i < this->texts.length(); i++)
|
|
|
|
this->remove(i);
|
|
|
|
}
|
|
|
|
|
2020-10-13 01:58:14 +02:00
|
|
|
bool OmobiDisplayTextModel::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;
|
|
|
|
}
|
|
|
|
|
|
|
|
QList<QMap<int, QVariant>> OmobiDisplayTextModel::getTexts() {
|
|
|
|
return this->texts;
|
2020-10-11 21:01:21 +02:00
|
|
|
}
|
2020-10-14 23:54:12 +02:00
|
|
|
|
|
|
|
QMap<int, QVariant> OmobiDisplayTextModel::getText(const QModelIndex &index) {
|
|
|
|
if (index.row() < this->rowCount())
|
|
|
|
return this->texts[index.row()];
|
|
|
|
|
|
|
|
return QMap<int, QVariant>();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int OmobiDisplayTextModel::getMaximumTextSets() {
|
|
|
|
return this->maximumTextSets;
|
|
|
|
}
|