refs #303 Simulator table view

* Simulator component
* Name / variant pair object / view / model
This commit is contained in:
Klaus Basan
2014-08-02 22:15:15 +02:00
parent 42f89ebeca
commit 9afc9024ca
9 changed files with 159 additions and 12 deletions

View File

@@ -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();
}
}
}

View File

@@ -13,6 +13,7 @@
#define BLACKGUI_SIMULATORCOMPONENT_H
#include "runtimebasedcomponent.h"
#include "blackmisc/icon.h"
#include <QTabWidget>
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;
};

View File

@@ -37,7 +37,11 @@
<number>0</number>
</property>
<item>
<widget class="BlackGui::Views::CNameVariantPairView" name="tvp_LiveData"/>
<widget class="BlackGui::Views::CNameVariantPairView" name="tvp_LiveData">
<attribute name="verticalHeaderVisible">
<bool>false</bool>
</attribute>
</widget>
</item>
</layout>
</widget>

View File

@@ -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));
}
}
}

View File

@@ -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() {}

View File

@@ -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();
}
}
}

View File

@@ -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);
};
}
}

View File

@@ -24,6 +24,29 @@ namespace BlackMisc
CSequence<CNameVariantPair>(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
*/

View File

@@ -30,6 +30,12 @@ namespace BlackMisc
//! Construct from a base class object.
CNameVariantPairList(const CSequence<CNameVariantPair> &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); }