From 65d3860ef9669ed4e39e2d1efe232f79d93ce221 Mon Sep 17 00:00:00 2001 From: Justin Hammond Date: Mon, 27 May 2019 19:34:47 +0800 Subject: [PATCH] BitSet Widget Done and Dusted --- README.md | 2 +- qt-openzwave/qtopenzwave.cpp | 2 +- qt-openzwave/qtozwmanager.cpp | 48 ++++++--- qt-openzwave/qtozwvalueidmodel.cpp | 32 ++++++ qt-openzwave/qtozwvalueidmodel.h | 2 + simpleclient/bitsetwidget.cpp | 32 ++++++ simpleclient/bitsetwidget.h | 10 ++ simpleclient/bitsetwidget.ui | 1 + simpleclient/mainwindow.cpp | 153 +++++++++++++++++++++++++++- simpleclient/mainwindow.h | 31 ++++++ simpleclient/mainwindow.ui | 81 ++++++++++++++- simpleclient/qtozw_itemdelegate.cpp | 52 ++++++---- 12 files changed, 406 insertions(+), 40 deletions(-) diff --git a/README.md b/README.md index c78fbef..9862a90 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,2 @@ # qt-openzwave -QT5 Wrapper for OpenZWave +A QT5 Wrapper for OpenZWave. Exposes diff --git a/qt-openzwave/qtopenzwave.cpp b/qt-openzwave/qtopenzwave.cpp index 1f7e817..0f9794f 100644 --- a/qt-openzwave/qtopenzwave.cpp +++ b/qt-openzwave/qtopenzwave.cpp @@ -8,7 +8,7 @@ QTOpenZwave::QTOpenZwave QObject (parent), m_manager(nullptr) { - //qRegisterMetaType("uint32_t"); + qRegisterMetaType("uint32_t"); qRegisterMetaType("QTOZW_ValueIDList"); qRegisterMetaTypeStreamOperators("QTOZW_ValueIDList"); qRegisterMetaType("QTOZW_ValueIDBitSet"); diff --git a/qt-openzwave/qtozwmanager.cpp b/qt-openzwave/qtozwmanager.cpp index b5ecd46..f669928 100644 --- a/qt-openzwave/qtozwmanager.cpp +++ b/qt-openzwave/qtozwmanager.cpp @@ -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(); + 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(this->d_ptr_internal); QVector 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); diff --git a/qt-openzwave/qtozwvalueidmodel.cpp b/qt-openzwave/qtozwvalueidmodel.cpp index 02832bb..688267c 100644 --- a/qt-openzwave/qtozwvalueidmodel.cpp +++ b/qt-openzwave/qtozwvalueidmodel.cpp @@ -58,6 +58,14 @@ QVariant QTOZW_ValueIds::data(const QModelIndex &index, int role) const { } return value[static_cast(index.column())]; } + if (role == Qt::ToolTipRole) { + QMap 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::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; +} diff --git a/qt-openzwave/qtozwvalueidmodel.h b/qt-openzwave/qtozwvalueidmodel.h index 0a67af5..da6a4eb 100644 --- a/qt-openzwave/qtozwvalueidmodel.h +++ b/qt-openzwave/qtozwvalueidmodel.h @@ -109,6 +109,8 @@ public Q_SLOTS: void resetModel(); }; +QString BitSettoQString(QBitArray ba); +uint32_t BitSettoInteger(QBitArray ba); #endif // QTOZWVALUEIDMODEL_H diff --git a/simpleclient/bitsetwidget.cpp b/simpleclient/bitsetwidget.cpp index 0b80da7..cdf0515 100644 --- a/simpleclient/bitsetwidget.cpp +++ b/simpleclient/bitsetwidget.cpp @@ -1,5 +1,7 @@ #include "bitsetwidget.h" #include "ui_bitsetwidget.h" +#include +#include 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(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; + +} diff --git a/simpleclient/bitsetwidget.h b/simpleclient/bitsetwidget.h index f3a9c62..1dd79c1 100644 --- a/simpleclient/bitsetwidget.h +++ b/simpleclient/bitsetwidget.h @@ -2,6 +2,7 @@ #define BITSETWIDGET_H #include +#include 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 diff --git a/simpleclient/bitsetwidget.ui b/simpleclient/bitsetwidget.ui index 05223f7..58b286d 100644 --- a/simpleclient/bitsetwidget.ui +++ b/simpleclient/bitsetwidget.ui @@ -13,6 +13,7 @@ Frame + diff --git a/simpleclient/mainwindow.cpp b/simpleclient/mainwindow.cpp index 18a1d57..7fac1f7 100644 --- a/simpleclient/mainwindow.cpp +++ b/simpleclient/mainwindow.cpp @@ -4,6 +4,8 @@ #include "qtozw_itemdelegate.h" #include +#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 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); +} + + diff --git a/simpleclient/mainwindow.h b/simpleclient/mainwindow.h index 5734066..1e2740b 100644 --- a/simpleclient/mainwindow.h +++ b/simpleclient/mainwindow.h @@ -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; diff --git a/simpleclient/mainwindow.ui b/simpleclient/mainwindow.ui index dbfa2ad..a34cae5 100644 --- a/simpleclient/mainwindow.ui +++ b/simpleclient/mainwindow.ui @@ -19,19 +19,49 @@ + + 0 + + + 0 + + + 0 + + + 0 + + + + 0 + 2 + + - 0 + 3 User + + 0 + + + 0 + + + 0 + + + 0 + @@ -42,6 +72,18 @@ System + + 0 + + + 0 + + + 0 + + + 0 + @@ -52,6 +94,18 @@ Config + + 0 + + + 0 + + + 0 + + + 0 + @@ -62,6 +116,18 @@ Associations + + 0 + + + 0 + + + 0 + + + 0 + @@ -80,8 +146,21 @@ 22 + + + &File + + + + + + + + E&xit + + diff --git a/simpleclient/qtozw_itemdelegate.cpp b/simpleclient/qtozw_itemdelegate.cpp index 223e0f2..2d335e3 100644 --- a/simpleclient/qtozw_itemdelegate.cpp +++ b/simpleclient/qtozw_itemdelegate.cpp @@ -3,23 +3,12 @@ #include #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().values); - qDebug() << itemOption.text; - QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &itemOption, painter); + BitSetWidget bs; + bs.setValue(index.data().value()); + 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()); + 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(editor); + bs->setValue(index.data().value()); + 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(editor); + if (!bs) + throw std::logic_error("Editor is not a BitSetWidget"); + model->setData(index, QVariant::fromValue(bs->getValue()), Qt::EditRole); + break; + } default: break; }