BitSet Widget Done and Dusted

This commit is contained in:
Justin Hammond 2019-05-27 19:34:47 +08:00
parent 879d86206c
commit 65d3860ef9
12 changed files with 406 additions and 40 deletions

View file

@ -1,2 +1,2 @@
# qt-openzwave
QT5 Wrapper for OpenZWave
A QT5 Wrapper for OpenZWave. Exposes

View file

@ -8,7 +8,7 @@ QTOpenZwave::QTOpenZwave
QObject (parent),
m_manager(nullptr)
{
//qRegisterMetaType<uint32_t>("uint32_t");
qRegisterMetaType<uint32_t>("uint32_t");
qRegisterMetaType<QTOZW_ValueIDList>("QTOZW_ValueIDList");
qRegisterMetaTypeStreamOperators<QTOZW_ValueIDList>("QTOZW_ValueIDList");
qRegisterMetaType<QTOZW_ValueIDBitSet>("QTOZW_ValueIDBitSet");

View file

@ -597,20 +597,28 @@ bool QTOZWManager_Internal::convertValueID(uint64_t vidKey) {
this->m_manager->GetBitSetSize(vid, &bssize);
this->m_manager->GetBitMask(vid, &bsmask);
QTOZW_ValueIDBitSet vidbs;
vidbs.mask.resize(INT32_MAX);
for (int i = 0; i < 32; ++i) {
vidbs.mask[i] = bsmask & (1 << i);
vidbs.mask.resize(bssize * 8);
for (int i = 1; i < bssize * 8; ++i) {
vidbs.mask[i] = (bsmask & (1 << i));
}
vidbs.values.resize(bssize * 8);
qDebug() << vidbs.values.size();
for (uint8_t i = 0; i < bssize * 8; ++i) {
qDebug() << "doing " << i;
bool value;
this->m_manager->GetValueAsBitSet(vid, i, &value);
vidbs.values[i] = value;
vidbs.label[i] = QString::fromStdString(this->m_manager->GetValueLabel(vid, i));
vidbs.help[i] = QString::fromStdString(this->m_manager->GetValueHelp(vid, i));
for (uint8_t i = 1; i <= bssize * 8; ++i) {
/* OZW is 1 base - QT is 0 base. */
if (vidbs.mask.at(i-1)) {
bool value;
if (this->m_manager->GetValueAsBitSet(vid, i, &value)) {
vidbs.values[i-1] = value;
vidbs.label[i-1] = QString::fromStdString(this->m_manager->GetValueLabel(vid, i));
vidbs.help[i-1] = QString::fromStdString(this->m_manager->GetValueHelp(vid, i));
}
}
}
#if 0
qDebug() << (uint32_t)bsmask;
this->m_manager->GetBitMask(vid, &bsmask);
qDebug().noquote() << BitSettoQString(vidbs.mask);
qDebug().noquote() << BitSettoQString(vidbs.values);
#endif
this->m_valueModel->setValueData(vidKey, QTOZW_ValueIds::ValueIdColumns::Value, QVariant::fromValue(vidbs));
this->m_valueModel->setValueData(vidKey, QTOZW_ValueIds::ValueIdColumns::Type, QTOZW_ValueIds::ValueIdTypes::BitSet);
return true;
@ -1250,7 +1258,21 @@ void QTOZWManager_Internal::pvt_valueModelDataChanged(const QModelIndex &topLeft
}
case OpenZWave::ValueID::ValueType_BitSet:
{
qWarning() << "ValueType_BitSet TODO";
QTOZW_ValueIDBitSet bs = topLeft.data().value<QTOZW_ValueIDBitSet>();
for (int i = 0; i <= bs.values.size()-1; i++) {
if (bs.mask.at(i)) {
bool curval;
this->m_manager->GetValueAsBitSet(vid, (uint8_t)i+1, &curval);
if (curval != bs.values.at(i)) {
/* we send this as a uint32_t so its a atomic change... and return.
* as the chances of other Bits changing are probably high, and a
* each call to SetValue generates traffis, so instead by sending a
* uint32_t here, we only send one packet*/
this->m_manager->SetValue(vid, (int32_t)BitSettoInteger(bs.values));
return;
}
}
}
return;
}
@ -1299,7 +1321,7 @@ bool QTOZWManager::initilizeSource(bool enableServer) {
this->m_sourceNode->setHeartbeatInterval(1000);
this->m_sourceNode->enableRemoting<QTOZWManagerSourceAPI>(this->d_ptr_internal);
QVector<int> roles;
roles << Qt::DisplayRole << Qt::BackgroundRole << Qt::EditRole;
roles << Qt::DisplayRole << Qt::BackgroundRole << Qt::EditRole << Qt::ToolTipRole;
this->m_sourceNode->enableRemoting(this->d_ptr_internal->getNodeModel(), "QTOZW_nodeModel", roles);
this->m_sourceNode->enableRemoting(this->d_ptr_internal->getValueModel(), "QTOZW_valueModel", roles);
this->m_sourceNode->enableRemoting(this->d_ptr_internal->getAssociationModel(), "QTOZW_associationModel", roles);

View file

@ -58,6 +58,14 @@ QVariant QTOZW_ValueIds::data(const QModelIndex &index, int role) const {
}
return value[static_cast<ValueIdColumns>(index.column())];
}
if (role == Qt::ToolTipRole) {
QMap<ValueIdColumns, QVariant> value = this->m_valueData[index.row()];
if (value.size() == 0) {
qWarning() << "data: Cant find any Node on Row " << index.row();
return QVariant();
}
return value[static_cast<ValueIdColumns>(ValueIdColumns::Help)];
}
return QVariant();
}
@ -268,3 +276,27 @@ void QTOZW_ValueIds_internal::resetModel() {
this->m_valueData.clear();
this->endRemoveRows();
}
QString BitSettoQString(QBitArray ba) {
QString result;
for (int i = ba.size()-1; i >= 0 ; --i) {
if (ba.testBit(i))
result[ba.size() - i] = '1';
else
result[ba.size() - i] = '0';
}
return result;
#if 0
result.prepend("0b");
return result;
#endif
}
uint32_t BitSettoInteger(QBitArray ba) {
uint32_t value = 0;
for (int i = 0; i <= ba.size()-1; ++i) {
value += (uint32_t)((ba.at(i) ? 1 : 0) << i);
}
return value;
}

View file

@ -109,6 +109,8 @@ public Q_SLOTS:
void resetModel();
};
QString BitSettoQString(QBitArray ba);
uint32_t BitSettoInteger(QBitArray ba);
#endif // QTOZWVALUEIDMODEL_H

View file

@ -1,5 +1,7 @@
#include "bitsetwidget.h"
#include "ui_bitsetwidget.h"
#include <QCheckBox>
#include <QDebug>
BitSetWidget::BitSetWidget(QWidget *parent) :
QFrame(parent),
@ -12,3 +14,33 @@ BitSetWidget::~BitSetWidget()
{
delete ui;
}
void BitSetWidget::setValue(QTOZW_ValueIDBitSet value) {
this->m_value = value;
for (uint8_t i = 0; i <= this->m_value.values.size() -1; ++i) {
if (this->m_value.mask.at(i)) {
QCheckBox *cb = new QCheckBox(this);
cb->setText(this->m_value.label[i]);
cb->setChecked(this->m_value.values.at(i));
cb->setToolTip(this->m_value.help[i]);
cb->setProperty("BitSetIndex", i);
QObject::connect(cb, &QCheckBox::stateChanged, this, &BitSetWidget::cbChanged);
this->ui->gridLayout_2->addWidget(cb);
}
}
}
void BitSetWidget::cbChanged() {
QCheckBox *cb = qobject_cast<QCheckBox *>(sender());
if (!cb)
throw std::logic_error("Widget is not of Type QCheckBox");
int index = cb->property("BitSetIndex").toInt();
if (this->m_value.values.at(index) != cb->isChecked())
this->m_value.values[index] = cb->isChecked();
emit stateChanged();
}
QTOZW_ValueIDBitSet BitSetWidget::getValue() {
return this->m_value;
}

View file

@ -2,6 +2,7 @@
#define BITSETWIDGET_H
#include <QFrame>
#include <qtozwvalueidmodel.h>
namespace Ui {
class BitSetWidget;
@ -14,9 +15,18 @@ class BitSetWidget : public QFrame
public:
explicit BitSetWidget(QWidget *parent = nullptr);
~BitSetWidget();
void setValue(QTOZW_ValueIDBitSet);
QTOZW_ValueIDBitSet getValue();
Q_SIGNALS:
void stateChanged();
private Q_SLOTS:
void cbChanged();
private:
Ui::BitSetWidget *ui;
QTOZW_ValueIDBitSet m_value;
};
#endif // BITSETWIDGET_H

View file

@ -13,6 +13,7 @@
<property name="windowTitle">
<string>Frame</string>
</property>
<layout class="QGridLayout" name="gridLayout_2"/>
</widget>
<resources/>
<connections/>

View file

@ -4,6 +4,8 @@
#include "qtozw_itemdelegate.h"
#include <qtozwproxymodels.h>
#define CONNECTSIGNALS(x) QObject::connect(this->m_qtozwmanager, &QTOZWManager::x, this, &MainWindow::x)
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
@ -12,6 +14,32 @@ MainWindow::MainWindow(QWidget *parent) :
this->m_openzwave = new QTOpenZwave(this);
this->m_qtozwmanager = this->m_openzwave->GetManager();
QObject::connect(this->m_qtozwmanager, &QTOZWManager::ready, this, &MainWindow::QTOZW_Ready);
CONNECTSIGNALS(valueAdded);
CONNECTSIGNALS(valueRemoved);
CONNECTSIGNALS(valueChanged);
CONNECTSIGNALS(valueRefreshed);
CONNECTSIGNALS(nodeNew);
CONNECTSIGNALS(nodeAdded);
CONNECTSIGNALS(nodeRemoved);
CONNECTSIGNALS(nodeReset);
CONNECTSIGNALS(nodeNaming);
CONNECTSIGNALS(nodeEvent);
CONNECTSIGNALS(nodeProtocolInfo);
CONNECTSIGNALS(nodeEssentialNodeQueriesComplete);
CONNECTSIGNALS(nodeQueriesComplete);
CONNECTSIGNALS(driverReady);
CONNECTSIGNALS(driverFailed);
CONNECTSIGNALS(driverReset);
CONNECTSIGNALS(driverRemoved);
CONNECTSIGNALS(driverAllNodesQueriedSomeDead);
CONNECTSIGNALS(driverAwakeNodesQueried);
CONNECTSIGNALS(driverAllNodesQueried);
CONNECTSIGNALS(controllerCommand);
CONNECTSIGNALS(manufacturerSpecificDBReady);
CONNECTSIGNALS(starting);
CONNECTSIGNALS(started);
CONNECTSIGNALS(stopped);
}
MainWindow::~MainWindow()
@ -43,6 +71,7 @@ void MainWindow::startLocal(QString serialPort, bool startServer) {
}
void MainWindow::QTOZW_Ready() {
this->ui->statusbar->showMessage("OpenZWave Ready");
qDebug() << "OZW Ready " << this->m_serialPort;
if (this->m_qtozwmanager->isRunning() == false) {
this->m_qtozwmanager->open(this->m_serialPort);
@ -52,7 +81,9 @@ void MainWindow::QTOZW_Ready() {
this->ui->nodeView->setModel(proxyNodeModel);
this->ui->nodeView->setSortingEnabled(true);
this->ui->nodeView->horizontalHeader()->setSectionsMovable(true);
this->ui->nodeView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
this->ui->nodeView->verticalHeader()->hide();
this->ui->nodeView->verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
this->ui->nodeView->setSelectionBehavior(QAbstractItemView::SelectRows);
this->ui->nodeView->setSelectionMode(QAbstractItemView::SingleSelection);
QItemSelectionModel *selectNodeModel = this->ui->nodeView->selectionModel();
@ -65,15 +96,18 @@ void MainWindow::QTOZW_Ready() {
QTOZW_ItemDelegate *delegate = new QTOZW_ItemDelegate(this);
this->ui->userView->setItemDelegateForColumn(QTOZW_ValueIds::ValueIdColumns::Value, delegate);
this->ui->userView->setModel(proxyUserValueModel);
this->ui->userView->setSortingEnabled(true);
this->ui->userView->horizontalHeader()->setSectionsMovable(true);
this->ui->userView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
this->ui->userView->verticalHeader()->hide();
this->ui->userView->verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
this->ui->userView->setSelectionBehavior(QAbstractItemView::SelectRows);
this->ui->userView->setSelectionMode(QAbstractItemView::SingleSelection);
this->ui->userView->setEditTriggers(QAbstractItemView::AllEditTriggers);
this->ui->userView->setFrameShape(QFrame::NoFrame);
QTOZW_proxyValueModel *proxyConfigValueModel = new QTOZW_proxyValueModel(this);
proxyConfigValueModel->setSourceModel(this->m_qtozwmanager->getValueModel());
@ -82,11 +116,13 @@ void MainWindow::QTOZW_Ready() {
this->ui->configView->setModel(proxyConfigValueModel);
this->ui->configView->setSortingEnabled(true);
this->ui->configView->horizontalHeader()->setSectionsMovable(true);
this->ui->configView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
this->ui->configView->verticalHeader()->hide();
this->ui->configView->verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
this->ui->configView->setSelectionBehavior(QAbstractItemView::SelectRows);
this->ui->configView->setSelectionMode(QAbstractItemView::SingleSelection);
this->ui->configView->setEditTriggers(QAbstractItemView::AllEditTriggers);
this->ui->configView->setFrameShape(QFrame::NoFrame);
this->ui->configView->setItemDelegateForColumn(QTOZW_ValueIds::ValueIdColumns::Value, delegate);
@ -98,10 +134,13 @@ void MainWindow::QTOZW_Ready() {
this->ui->systemView->setModel(proxySystemValueModel);
this->ui->systemView->setSortingEnabled(true);
this->ui->systemView->horizontalHeader()->setSectionsMovable(true);
this->ui->systemView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
this->ui->systemView->verticalHeader()->hide();
this->ui->systemView->verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
this->ui->systemView->setSelectionBehavior(QAbstractItemView::SelectRows);
this->ui->systemView->setSelectionMode(QAbstractItemView::SingleSelection);
this->ui->systemView->setEditTriggers(QAbstractItemView::AllEditTriggers);
this->ui->systemView->setFrameShape(QFrame::NoFrame);
this->ui->systemView->setItemDelegateForColumn(QTOZW_ValueIds::ValueIdColumns::Value, delegate);
@ -113,9 +152,117 @@ void MainWindow::QTOZW_Ready() {
this->ui->AssociationView->setModel(proxyAssociationModel);
this->ui->AssociationView->setSortingEnabled(true);
this->ui->AssociationView->horizontalHeader()->setSectionsMovable(true);
this->ui->AssociationView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
this->ui->AssociationView->verticalHeader()->hide();
this->ui->AssociationView->verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
this->ui->AssociationView->setSelectionBehavior(QAbstractItemView::SelectRows);
this->ui->AssociationView->setSelectionMode(QAbstractItemView::SingleSelection);
}
void MainWindow::valueAdded(uint64_t vidKey) {
Q_UNUSED(vidKey)
this->ui->statusbar->showMessage("ValueAdded Notification", 2000);
}
void MainWindow::valueRemoved(uint64_t vidKey) {
Q_UNUSED(vidKey)
this->ui->statusbar->showMessage("ValueRemoved Notification", 2000);
}
void MainWindow::valueChanged(uint64_t vidKey) {
Q_UNUSED(vidKey)
this->ui->statusbar->showMessage("ValueChanged Notification", 2000);
}
void MainWindow::valueRefreshed(uint64_t vidKey) {
Q_UNUSED(vidKey)
this->ui->statusbar->showMessage("ValueRefreshed Notification", 2000);
}
void MainWindow::nodeNew(uint8_t node) {
Q_UNUSED(node);
this->ui->statusbar->showMessage("NodeNew Notification", 2000);
}
void MainWindow::nodeAdded(uint8_t node) {
Q_UNUSED(node);
this->ui->statusbar->showMessage("NodeAdded Notification", 2000);
}
void MainWindow::nodeRemoved(uint8_t node) {
Q_UNUSED(node);
this->ui->statusbar->showMessage("NodeRemoved Notification", 2000);
}
void MainWindow::nodeReset(uint8_t node) {
Q_UNUSED(node);
this->ui->statusbar->showMessage("NodeReset Notification", 2000);
}
void MainWindow::nodeNaming(uint8_t node) {
Q_UNUSED(node);
this->ui->statusbar->showMessage("NodeNaming Notification", 2000);
}
void MainWindow::nodeEvent(uint8_t node, uint8_t event) {
Q_UNUSED(node)
Q_UNUSED(event)
this->ui->statusbar->showMessage("NodeEvent Notification", 2000);
}
void MainWindow::nodeProtocolInfo(uint8_t node) {
Q_UNUSED(node)
this->ui->statusbar->showMessage("NodeProtocolInfo Notification", 2000);
}
void MainWindow::nodeEssentialNodeQueriesComplete(uint8_t node) {
Q_UNUSED(node)
this->ui->statusbar->showMessage("NodeEssentialNodeQueriesComplete Notification", 2000);
}
void MainWindow::nodeQueriesComplete(uint8_t node) {
static QMap<uint8_t, bool> refreshdone;
this->ui->statusbar->showMessage("nodeQueriesComplete Notification", 2000);
if (!refreshdone[node]) {
refreshdone[node] = true;
if (node != 1)
this->m_qtozwmanager->requestAllConfigParam(node);
}
}
void MainWindow::driverReady(uint32_t homeID) {
Q_UNUSED(homeID)
this->ui->statusbar->showMessage("DriverReady Notification", 2000);
}
void MainWindow::driverFailed(uint32_t homeID) {
Q_UNUSED(homeID)
this->ui->statusbar->showMessage("DriverFailed Notification", 2000);
}
void MainWindow::driverReset(uint32_t homeID) {
Q_UNUSED(homeID)
this->ui->statusbar->showMessage("DriverReset Notification", 2000);
}
void MainWindow::driverRemoved(uint32_t homeID) {
Q_UNUSED(homeID)
this->ui->statusbar->showMessage("DriverRemoved Notification", 2000);
}
void MainWindow::driverAllNodesQueriedSomeDead() {
this->ui->statusbar->showMessage("DriverAllNodesQueriedSomeDead Notification", 2000);
}
void MainWindow::driverAllNodesQueried() {
this->ui->statusbar->showMessage("DriverAllNodesQueried Notification", 2000);
}
void MainWindow::driverAwakeNodesQueried() {
this->ui->statusbar->showMessage("DriverAwakeNodesQueried Notification", 2000);
}
void MainWindow::controllerCommand(uint8_t command) {
Q_UNUSED(command)
this->ui->statusbar->showMessage("ControllerCommand Notification", 2000);
}
// void ozwNotification(OpenZWave::Notification::NotificationCode event);
// void ozwUserAlert(OpenZWave::Notification::UserAlertNotification event);
void MainWindow::manufacturerSpecificDBReady() {
this->ui->statusbar->showMessage("ManufacturerSpecificDBReady Notification", 2000);
}
void MainWindow::starting() {
this->ui->statusbar->showMessage("Starting", 2000);
}
void MainWindow::started(uint32_t homeID) {
Q_UNUSED(homeID)
this->ui->statusbar->showMessage("Started", 2000);
}
void MainWindow::stopped(uint32_t homeID) {
Q_UNUSED(homeID)
this->ui->statusbar->showMessage("Stopped", 2000);
}

View file

@ -21,6 +21,37 @@ public Q_SLOTS:
void startRemote(QString);
void startLocal(QString, bool);
void QTOZW_Ready();
void valueAdded(uint64_t vidKey);
void valueRemoved(uint64_t vidKey);
void valueChanged(uint64_t vidKey);
void valueRefreshed(uint64_t vidKey);
void nodeNew(uint8_t node);
void nodeAdded(uint8_t node);
void nodeRemoved(uint8_t node);
void nodeReset(uint8_t node);
void nodeNaming(uint8_t node);
void nodeEvent(uint8_t node, uint8_t event);
void nodeProtocolInfo(uint8_t node);
void nodeEssentialNodeQueriesComplete(uint8_t node);
void nodeQueriesComplete(uint8_t node);
void driverReady(uint32_t homeID);
void driverFailed(uint32_t homeID);
void driverReset(uint32_t homeID);
void driverRemoved(uint32_t homeID);
void driverAllNodesQueriedSomeDead();
void driverAllNodesQueried();
void driverAwakeNodesQueried();
void controllerCommand(uint8_t command);
// void ozwNotification(OpenZWave::Notification::NotificationCode event);
// void ozwUserAlert(OpenZWave::Notification::UserAlertNotification event);
void manufacturerSpecificDBReady();
void starting();
void started(uint32_t homeID);
void stopped(uint32_t homeID);
// void error(QTOZWErrorCodes errorcode);
private:
Ui::MainWindow *ui;

View file

@ -19,19 +19,49 @@
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QVBoxLayout" name="verticalLayout">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QTableView" name="nodeView"/>
</item>
<item>
<widget class="QTabWidget" name="tabWidget">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>2</verstretch>
</sizepolicy>
</property>
<property name="currentIndex">
<number>0</number>
<number>3</number>
</property>
<widget class="QWidget" name="tab">
<attribute name="title">
<string>User</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout_2">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QTableView" name="userView"/>
</item>
@ -42,6 +72,18 @@
<string>System</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout_3">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QTableView" name="systemView"/>
</item>
@ -52,6 +94,18 @@
<string>Config</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout_4">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QTableView" name="configView"/>
</item>
@ -62,6 +116,18 @@
<string>Associations</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout_5">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QTableView" name="AssociationView"/>
</item>
@ -80,8 +146,21 @@
<height>22</height>
</rect>
</property>
<widget class="QMenu" name="menu_File">
<property name="title">
<string>&amp;File</string>
</property>
<addaction name="separator"/>
<addaction name="actionE_xit"/>
</widget>
<addaction name="menu_File"/>
</widget>
<widget class="QStatusBar" name="statusbar"/>
<action name="actionE_xit">
<property name="text">
<string>E&amp;xit</string>
</property>
</action>
</widget>
<resources>
<include location="simpleclient.qrc"/>

View file

@ -3,23 +3,12 @@
#include <QApplication>
#include "qtozw_itemdelegate.h"
#include "qtozwvalueidmodel.h"
#include "bitsetwidget.h"
QTOZW_ItemDelegate::QTOZW_ItemDelegate(QObject *parent) : QStyledItemDelegate(parent)
{
}
QString BitSettoQString(QBitArray ba) {
QString result;
for (int i = 0; i < ba.size(); ++i) {
if (ba.testBit(i))
result[i] = '1';
else
result[i] = '0';
}
result.prepend("0b");
return result;
}
void QTOZW_ItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QModelIndex typeIndex = index.sibling(index.row(), QTOZW_ValueIds::ValueIdColumns::Type);
@ -54,11 +43,13 @@ void QTOZW_ItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &op
break;
}
case QTOZW_ValueIds::ValueIdTypes::BitSet: {
QStyleOptionViewItem itemOption(option);
initStyleOption(&itemOption, index);
itemOption.text = BitSettoQString(index.data().value<QTOZW_ValueIDBitSet>().values);
qDebug() << itemOption.text;
QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &itemOption, painter);
BitSetWidget bs;
bs.setValue(index.data().value<QTOZW_ValueIDBitSet>());
bs.setGeometry(option.rect);
painter->save();
painter->translate(option.rect.topLeft());
bs.render(painter, QPoint(), QRegion(), QWidget::DrawChildren);
painter->restore();
}
#if 0
case QTOZW_ValueIds::ValueIdTypes::Int:
@ -109,7 +100,7 @@ QSize QTOZW_ItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QMo
comboBoxOption.rect = option.rect;
comboBoxOption.state = option.state;
comboBoxOption.currentText = val.selectedItem;
return QSize(QFontMetrics(option.font).width(val.selectedItem), comboBoxOption.rect.height());
return QSize(QFontMetrics(option.font).width(val.selectedItem), QStyledItemDelegate::sizeHint(option, index).height());
}
case QTOZW_ValueIds::ValueIdTypes::Bool: {
QStyleOptionButton cbOption;
@ -118,6 +109,11 @@ QSize QTOZW_ItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QMo
cbOption.state |= QStyle::State_Enabled;
return cbOption.rect.size();
}
case QTOZW_ValueIds::ValueIdTypes::BitSet: {
BitSetWidget bs;
bs.setValue(index.data().value<QTOZW_ValueIDBitSet>());
return bs.sizeHint();
}
default:
return QStyledItemDelegate::sizeHint(option, index);
}
@ -145,7 +141,11 @@ QWidget *QTOZW_ItemDelegate::createEditor(QWidget *parent, const QStyleOptionVie
editor->setAutoFillBackground(true);
return editor;
}
case QTOZW_ValueIds::ValueIdTypes::BitSet: {
BitSetWidget *editor = new BitSetWidget(parent);
editor->setAutoFillBackground(true);
return editor;
}
default:
return QStyledItemDelegate::createEditor(parent, option, index);
}
@ -192,7 +192,11 @@ void QTOZW_ItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index
sb->setValue(index.data().toInt());
break;
}
case QTOZW_ValueIds::ValueIdTypes::BitSet: {
BitSetWidget *bs = qobject_cast<BitSetWidget *>(editor);
bs->setValue(index.data().value<QTOZW_ValueIDBitSet>());
break;
}
default:
QStyledItemDelegate::setEditorData(editor, index);
}
@ -241,7 +245,13 @@ void QTOZW_ItemDelegate::setModelData(QWidget* editor, QAbstractItemModel* model
}
break;
}
case QTOZW_ValueIds::ValueIdTypes::BitSet: {
BitSetWidget *bs = qobject_cast<BitSetWidget *>(editor);
if (!bs)
throw std::logic_error("Editor is not a BitSetWidget");
model->setData(index, QVariant::fromValue<QTOZW_ValueIDBitSet>(bs->getValue()), Qt::EditRole);
break;
}
default:
break;
}