#include "omobidisplaytextmodel.h" OmobiDisplayTextModel::OmobiDisplayTextModel(QObject* parent) : QAbstractListModel(parent) { this->maximumTextSets = 0; this->maximumTextLength = 0; connect(this, &OmobiDisplayTextModel::rowsInserted, this, &OmobiDisplayTextModel::rowCountChanged); connect(this, &OmobiDisplayTextModel::rowsRemoved, this, &OmobiDisplayTextModel::rowCountChanged); } int OmobiDisplayTextModel::rowCount(const QModelIndex &) const { return this->texts.length(); } QVariant OmobiDisplayTextModel::data(const QModelIndex &index, int role) const { 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; } } return QVariant(); } QHash OmobiDisplayTextModel::roleNames() const { static const QHash roles { { TextRole, "text" }, { ActiveRole, "active" }, { RuntimeRole, "runtime" }, { ColorRole, "color" }, { AlignmentRole, "alignment" }, { ScrollRole, "scroll" }, { ScrollSpeedRole, "scrollSpeed" }, { ScrollCountRole, "scrollCount" }, { IndexRole, "index" } }; return roles; } void OmobiDisplayTextModel::append( QString text, bool active, unsigned int runtime, QString color, QString alignment, bool scroll, unsigned int scrollSpeed, unsigned int scrollCount ) { // check length maximums if(this->texts.length() >= this->maximumTextSets) return; if(text.length() > this->maximumTextLength) text = text.left(this->maximumTextLength); QMap roles = { { TextRole, text }, { ActiveRole, active }, { RuntimeRole, runtime }, { ColorRole, color }, { AlignmentRole, alignment }, { ScrollRole, scroll }, { ScrollSpeedRole, scrollSpeed }, { ScrollCountRole, scrollCount } }; this->append(roles); } void OmobiDisplayTextModel::append(const QMap &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; 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; emit dataChanged(index, index, QVector({role})); return true; } 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); } bool OmobiDisplayTextModel::setTexts(QList> texts) { this->beginResetModel(); this->resetInternalData(); this->texts.clear(); this->endResetModel(); for(QMap text : texts) this->append(text); return true; } QList> OmobiDisplayTextModel::getTexts() { return this->texts; } QMap OmobiDisplayTextModel::getText(const QModelIndex &index) { if (index.row() < this->rowCount()) return this->texts[index.row()]; return QMap(); } int OmobiDisplayTextModel::getMaximumTextSets() { return this->maximumTextSets; }