diff --git a/src/blackgui/components/simulatorcomponent.cpp b/src/blackgui/components/simulatorcomponent.cpp index c394d0546..16055ec00 100644 --- a/src/blackgui/components/simulatorcomponent.cpp +++ b/src/blackgui/components/simulatorcomponent.cpp @@ -9,6 +9,7 @@ #include "simulatorcomponent.h" #include "ui_simulatorcomponent.h" +#include "blackmisc/iconlist.h" namespace BlackGui { @@ -18,11 +19,33 @@ namespace BlackGui QTabWidget(parent), ui(new Ui::CSimulatorComponent) { ui->setupUi(this); + this->ui->tvp_LiveData->setIconMode(true); + this->addOrUpdateByName("info", "no data yet", CIcons::StandardIconWarning16); } CSimulatorComponent::~CSimulatorComponent() { delete ui; } + + void CSimulatorComponent::addOrUpdateByName(const QString &name, const QString &value, const CIcon &icon) + { + this->ui->tvp_LiveData->addOrUpdateByName(name, value, icon); + } + + void CSimulatorComponent::addOrUpdateByName(const QString &name, const QString &value, CIcons::IconIndexes iconIndex) + { + this->addOrUpdateByName(name, value, CIconList::iconForIndex(iconIndex)); + } + + int CSimulatorComponent::rowCount() const + { + return this->ui->tvp_LiveData->rowCount(); + } + + void CSimulatorComponent::clear() + { + this->ui->tvp_LiveData->clear(); + } } } diff --git a/src/blackgui/components/simulatorcomponent.h b/src/blackgui/components/simulatorcomponent.h index 76acbbf69..8bbfba2f4 100644 --- a/src/blackgui/components/simulatorcomponent.h +++ b/src/blackgui/components/simulatorcomponent.h @@ -13,6 +13,7 @@ #define BLACKGUI_SIMULATORCOMPONENT_H #include "runtimebasedcomponent.h" +#include "blackmisc/icon.h" #include namespace Ui { class CSimulatorComponent; } @@ -34,6 +35,18 @@ namespace BlackGui //! Destructor ~CSimulatorComponent(); + //! Simple add or update name / value pair + void addOrUpdateByName(const QString &name, const QString &value, const BlackMisc::CIcon &icon); + + //! Simple add or update name / value pair + void addOrUpdateByName(const QString &name, const QString &value, BlackMisc::CIcons::IconIndexes iconIndex); + + //! Number of entries + int rowCount() const; + + //! Clear + void clear(); + private: Ui::CSimulatorComponent *ui; }; diff --git a/src/blackgui/components/simulatorcomponent.ui b/src/blackgui/components/simulatorcomponent.ui index 9d7c2c2b3..827fcb948 100644 --- a/src/blackgui/components/simulatorcomponent.ui +++ b/src/blackgui/components/simulatorcomponent.ui @@ -37,7 +37,11 @@ 0 - + + + false + + diff --git a/src/blackgui/models/namevariantpairlistmodel.cpp b/src/blackgui/models/namevariantpairlistmodel.cpp index 6a0ac79b6..abfc48f1f 100644 --- a/src/blackgui/models/namevariantpairlistmodel.cpp +++ b/src/blackgui/models/namevariantpairlistmodel.cpp @@ -16,21 +16,60 @@ namespace BlackGui { namespace Models { - /* - * Constructor - */ - CNameVariantPairModel::CNameVariantPairModel(QObject *parent) : CListModelBase("ViewNameVariantPairList", parent) + CNameVariantPairModel::CNameVariantPairModel(bool withIcon, QObject *parent) : CListModelBase("ViewNameVariantPairList", parent) { + this->setIconMode(withIcon); + + // force strings for translation in resource files + (void)QT_TRANSLATE_NOOP("ViewNameVariantPairList", "name"); + (void)QT_TRANSLATE_NOOP("ViewNameVariantPairList", "value"); + } + + void CNameVariantPairModel::setIconMode(bool withIcon) + { + this->m_columns.clear(); + if (withIcon) + { + this->m_columns.addColumn(CColumn(CNameVariantPair::IndexPixmap, true)); + } this->m_columns.addColumn(CColumn("name", CNameVariantPair::IndexName)); this->m_columns.addColumn(CColumn("value", CNameVariantPair::IndexVariant)); // default sort order this->setSortColumnByPropertyIndex(CNameVariantPair::IndexName); this->m_sortOrder = Qt::AscendingOrder; + } - // force strings for translation in resource files - (void)QT_TRANSLATE_NOOP("ViewNameVariantPairList", "name"); - (void)QT_TRANSLATE_NOOP("ViewNameVariantPairList", "value"); + void CNameVariantPairModel::addOrUpdateByName(const QString &name, const QString &value, const CIcon &icon) + { + int index = this->getNameRowIndex(name); + QVariant qv(value); + CNameVariantPair pair(name, qv, icon); + + if (index < 0) + { + // not in the list yet, append + this->push_back(pair); + } + else + { + // already in list, update + this->update(index, pair); + } + } + + int CNameVariantPairModel::getNameRowIndex(const QString &name) + { + int rowIndex = this->m_container.getNameRowIndex(name); + return rowIndex; + } + + void CNameVariantPairModel::removeByName(const QString &name) + { + int rowIndex = this->getNameRowIndex(name); + if (rowIndex < 0) return; + QModelIndex i = this->index(rowIndex, 0); + this->remove(this->at(i)); } } } diff --git a/src/blackgui/models/namevariantpairlistmodel.h b/src/blackgui/models/namevariantpairlistmodel.h index 77d400d10..29e1ec1a0 100644 --- a/src/blackgui/models/namevariantpairlistmodel.h +++ b/src/blackgui/models/namevariantpairlistmodel.h @@ -31,10 +31,19 @@ namespace BlackGui public: //! Constructor - explicit CNameVariantPairModel(QObject *parent = nullptr); + explicit CNameVariantPairModel(bool withIcon, QObject *parent = nullptr); - //! Constructor - explicit CNameVariantPairModel(const BlackMisc::CNameVariantPairList &nameValues, QObject *parent = nullptr); + //! Icon on / off + void setIconMode(bool withIcon); + + //! Remove by given name + void removeByName(const QString &name); + + //! Add our update a value + void addOrUpdateByName(const QString &name, const QString &value, const BlackMisc::CIcon &icon); + + //! Current row index of given name + int getNameRowIndex(const QString &name); //! Destructor virtual ~CNameVariantPairModel() {} diff --git a/src/blackgui/views/namevariantpairview.cpp b/src/blackgui/views/namevariantpairview.cpp index 52cfb14f5..61fcbacf8 100644 --- a/src/blackgui/views/namevariantpairview.cpp +++ b/src/blackgui/views/namevariantpairview.cpp @@ -19,7 +19,7 @@ namespace BlackGui { CNameVariantPairView::CNameVariantPairView(QWidget *parent) : CViewBase(parent) { - this->m_model = new CNameVariantPairModel(this); + this->m_model = new CNameVariantPairModel(true, this); this->setModel(this->m_model); // via QTableView this->m_model->setSortColumnByPropertyIndex(BlackMisc::CNameVariantPair::IndexName); if (this->m_model->hasValidSortColumn()) @@ -28,5 +28,26 @@ namespace BlackGui this->m_model->getSortOrder()); this->horizontalHeader()->setStretchLastSection(true); } + + void CNameVariantPairView::setIconMode(bool withIcon) + { + Q_ASSERT(this->m_model); + this->m_model->setIconMode(withIcon); + } + + void CNameVariantPairView::addOrUpdateByName(const QString &name, const QString &value, const CIcon &icon) + { + Q_ASSERT(this->m_model); + this->m_model->addOrUpdateByName(name, value, icon); + this->resizeColumnsToContents(); + this->resizeRowsToContents(); + } + + void CNameVariantPairView::removeByName(const QString &name) + { + this->m_model->removeByName(name); + this->resizeColumnsToContents(); + this->resizeRowsToContents(); + } } } diff --git a/src/blackgui/views/namevariantpairview.h b/src/blackgui/views/namevariantpairview.h index 982276256..916a0a337 100644 --- a/src/blackgui/views/namevariantpairview.h +++ b/src/blackgui/views/namevariantpairview.h @@ -26,6 +26,15 @@ namespace BlackGui public: //! Constructor explicit CNameVariantPairView(QWidget *parent = nullptr); + + //! Icon mode + void setIconMode(bool withIcon); + + //! Update or add value, simple string version + void addOrUpdateByName(const QString &name, const QString &value, const BlackMisc::CIcon &icon = BlackMisc::CIcon()); + + //! Remove by name + void removeByName(const QString &name); }; } } diff --git a/src/blackmisc/namevariantpairlist.cpp b/src/blackmisc/namevariantpairlist.cpp index 2d54cd551..e824a9e5f 100644 --- a/src/blackmisc/namevariantpairlist.cpp +++ b/src/blackmisc/namevariantpairlist.cpp @@ -24,6 +24,29 @@ namespace BlackMisc CSequence(other) { } + /* + * Name contained? + */ + bool CNameVariantPairList::containsName(const QString &name) + { + return this->contains(&CNameVariantPair::getName, name); + } + + /* + * Name index + */ + int CNameVariantPairList::getNameRowIndex(const QString &name) + { + for (int i = 0; i < this->size(); i++) + { + if ((*this)[i].getName() == name) + { + return i; + } + } + return -1; + } + /* * Register metadata */ diff --git a/src/blackmisc/namevariantpairlist.h b/src/blackmisc/namevariantpairlist.h index dfce8d6e5..109b728df 100644 --- a/src/blackmisc/namevariantpairlist.h +++ b/src/blackmisc/namevariantpairlist.h @@ -30,6 +30,12 @@ namespace BlackMisc //! Construct from a base class object. CNameVariantPairList(const CSequence &other); + //! Contains name + bool containsName(const QString &name); + + //! Get name index + int getNameRowIndex(const QString &name); + //! CValueObject::toQVariant() virtual QVariant toQVariant() const override { return QVariant::fromValue(*this); }