diff --git a/BionxControl.pro b/BionxControl.pro index dc36b74..a883f5a 100644 --- a/BionxControl.pro +++ b/BionxControl.pro @@ -27,8 +27,8 @@ windows SOURCES += \ bc.cpp \ bcdatamanager.cpp \ - bcdelegate.cpp \ bcdevicepanel.cpp \ + bcitemdelegate.cpp \ bclegacy.cpp \ bctransmitter.cpp \ bcvalue.cpp \ @@ -45,8 +45,8 @@ HEADERS += \ bccandriver.h \ bccandrivertinycan.h \ bcdatamanager.h \ - bcdelegate.h \ bcdevicepanel.h \ + bcitemdelegate.h \ bcmainwindow.h \ bctransmitter.h \ bcvalue.h \ diff --git a/bcdevicepanel.cpp b/bcdevicepanel.cpp index e61a836..ecd2c76 100644 --- a/bcdevicepanel.cpp +++ b/bcdevicepanel.cpp @@ -31,7 +31,7 @@ #include -#include +#include BCDevicePanel::BCDevicePanel(QWidget *parent) : QWidget(parent) @@ -41,8 +41,10 @@ BCDevicePanel::BCDevicePanel(QWidget *parent) //_valueView->resizeColumnsToContents(); //_valueView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents); - BCDelegate* delegate = new BCDelegate( _valueModel.getValueList(), _valueView); - _valueView->setItemDelegate( delegate ); + // __fix! ziemlich wildes ge-pointere, hier + _itemDelegate = new BCItemDelegate( _valueModel.getValueList(), _valueView); + _valueView->setItemDelegate( _itemDelegate ); + } @@ -78,10 +80,12 @@ const BCValueList& BCDevicePanel::getValueListX() return _valueModel.getValueList(); } +/* BCValueModel& BCDevicePanel::getValueModel() { return _valueModel; } +*/ // __FIX ist das ok so? void BCDevicePanel::onValueListReady( BCDevice::ID deviceID, BCValueList valueList ) @@ -91,5 +95,11 @@ void BCDevicePanel::onValueListReady( BCDevice::ID deviceID, BCValueList valueLi _valueModel.takeValueList( valueList ); } +void BCDevicePanel::onValueUpdated(int index, BC::State state, const QString& newVisibleValue ) +{ + _valueModel.onValueUpdated( index,state,newVisibleValue); + _itemDelegate->onHighlightRow( index ); + +} diff --git a/bcdevicepanel.h b/bcdevicepanel.h index 2cb158e..7319782 100644 --- a/bcdevicepanel.h +++ b/bcdevicepanel.h @@ -36,6 +36,8 @@ #include #include +class BCItemDelegate; + class BCDevicePanel : public QWidget, private Ui::BCDevicePanel { Q_OBJECT @@ -53,17 +55,18 @@ public: QTableView* getValueView(); const BCValueList& getValueListX(); - BCValueModel &getValueModel(); + //BCValueModel &getValueModel(); public slots: void onValueListReady( BCDevice::ID deviceID, BCValueList valueList ); + void onValueUpdated( int index, BC::State state, const QString& newVisibleValue="" ); protected: - BCDevice::ID _devideID{BCDevice::ID::Invalid}; - BCValueModel _valueModel; - + BCDevice::ID _devideID{BCDevice::ID::Invalid}; + BCValueModel _valueModel; + BCItemDelegate* _itemDelegate{}; }; diff --git a/bcdevicepanel.ui b/bcdevicepanel.ui index 3e1baca..a7a7a14 100644 --- a/bcdevicepanel.ui +++ b/bcdevicepanel.ui @@ -14,12 +14,27 @@ Form - background-color:green; + Fitze! + + 0 + + + 0 + + + 0 + + + 0 + + + 0 + @@ -39,7 +54,7 @@ false - background-color:#0000ff + background-color:#eeeeee QFrame::Shape::NoFrame diff --git a/bcitemdelegate.cpp b/bcitemdelegate.cpp index 0a30bbe..7873f76 100644 --- a/bcitemdelegate.cpp +++ b/bcitemdelegate.cpp @@ -43,20 +43,20 @@ #include #include -#include "bcdelegate.h" +#include "bcitemdelegate.h" #include "bcvalue.h" -#include "qapplication.h" -BCDelegate::BCDelegate(const BCValueList& valueList, QTableView* view) + +BCItemDelegate::BCItemDelegate(const BCValueList& valueList, QTableView* view) : QStyledItemDelegate{view}, _valueList{valueList}, _view{view} { } - -QString BCDelegate::displayText(const QVariant& dataValue, const QLocale& locale) const +/* +QString BCItemDelegate::displayText(const QVariant& dataValue, const QLocale& locale) const { // Wir prüfen, ob im Variant unser Struct steckt if (dataValue.canConvert()) @@ -76,9 +76,10 @@ QString BCDelegate::displayText(const QVariant& dataValue, const QLocale& locale // Fallback für normale Strings/Zahlen return QStyledItemDelegate::displayText(dataValue, locale); } +*/ -QWidget *BCDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex& index) const +QWidget *BCItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex& index) const { QVariant rawData = index.data(Qt::EditRole); //if (!rawData.canConvert()) @@ -128,7 +129,7 @@ QWidget *BCDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &o */ } -void BCDelegate::setEditorData(QWidget *editor, const QModelIndex& index) const +void BCItemDelegate::setEditorData(QWidget *editor, const QModelIndex& index) const { // Daten vom Model in den Editor laden const BCValue& bc = *index.data(Qt::EditRole).value(); @@ -147,7 +148,7 @@ void BCDelegate::setEditorData(QWidget *editor, const QModelIndex& index) const } } -void BCDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex& index) const +void BCItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex& index) const { // Daten vom Editor zurück ins Model speichern (Beim Schließen) QSlider *slider = editor->findChild("slider"); @@ -160,13 +161,13 @@ void BCDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const } } -void BCDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex& index) const +void BCItemDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex& index) const { // __fix! editor->setGeometry(option.rect); } -QSize BCDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex& index) const +QSize BCItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex& index) const { return QStyledItemDelegate::sizeHint(option,index); /* @@ -180,34 +181,13 @@ QSize BCDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex } -void BCDelegate::paint(QPainter *painter, const QStyleOptionViewItem& option, const QModelIndex& index) const +void BCItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem& option, const QModelIndex& index) const { - /* // 1. Standard-Zeichnen (Text, Hintergrund, Selection) durchführen QStyledItemDelegate::paint(painter, option, index); - // 2. Unser Custom-Overlay: Oranger Rahmen, wenn Zeile passt - if (index.row() == _highlightedRow) - { - painter->save(); - - qDebug() << " --- is highlight: " << index.row(); - - // Setup Stift - QPen pen(QColor(255, 165, 0)); // Orange - pen.setWidth(2); - painter->setPen(pen); - - // Rechteck zeichnen (leicht eingezogen, damit es nicht abgeschnitten wird) - QRect rect = option.rect.adjusted(1, 1, -1, -1); - painter->drawRect(rect); - - painter->restore(); - } - */ - - QStyledItemDelegate::paint(painter, option, index); + //QPen pen(QColor(255, 165, 0)); // Orange /* if (index.row() == _highlightedRow && _opacity > 0.0) @@ -231,61 +211,70 @@ void BCDelegate::paint(QPainter *painter, const QStyleOptionViewItem& option, co int row = index.row(); - if (m_rowOpacities.contains(row)) + if (index.column() == 1 ) { - /* - qreal opacity = m_rowOpacities.value(row); - if (opacity > 0.01) + qDebug() << " --- is highlight: " << row << " : " << index.column(); + //if( m_rowOpacities.contains(row)) { - painter->save(); - painter->setOpacity(opacity); - painter->fillRect(option.rect, QColor(255, 140, 0, 120)); - painter->restore(); + qDebug() << " --- PAINT highlight:"; + //if ( m_rowOpacities.contains(row)) + paintHighlightRow(painter,option,index); } - */ - paintHighlightRow(painter,option,index); } } -void BCDelegate::paintHighlightRow(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const +/* +qreal opacity = m_rowOpacities.value(row); +if (opacity > 0.01) +{ + painter->save(); + painter->setOpacity(opacity); + painter->fillRect(option.rect, QColor(255, 140, 0, 120)); + painter->restore(); +} +*/ + + +void BCItemDelegate::paintHighlightRow(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const { painter->save(); painter->setRenderHint(QPainter::Antialiasing); - int row = index.row(); qreal opacity = m_rowOpacities.value(row); painter->setOpacity(opacity); // Margin von 4px - QRect itemRect = option.rect.adjusted(4, 4, -4, -4); + QRect itemRect = option.rect.adjusted(3, 3, -3, -3); // Border von 2px berücksichtigen (nach innen) QRect contentRect = itemRect.adjusted(2, 2, -2, -2); - + // painter->fillRect(contentRect,Qt::green); + /* // Hintergrund (weiß) painter->setBrush(Qt::white); painter->setPen(Qt::NoPen); painter->drawRoundedRect(itemRect, 8, 8); - + */ // Border (2px solid #2196F3) - QPen borderPen( Qt::red, 2); + QPen borderPen( Qt::red, 1); painter->setPen(borderPen); painter->setBrush(Qt::NoBrush); - painter->drawRoundedRect(itemRect, 8, 8); + painter->drawRoundedRect(itemRect, 2, 2); // Padding von 8px für den Content - QRect textRect = contentRect.adjusted(8, 8, -8, -8); + //QRect textRect = contentRect.adjusted(8, 8, -8, -8); + /* // Text zeichnen painter->setPen(Qt::black); // oder option.palette.color(QPalette::Text) QString text = index.data(Qt::DisplayRole).toString(); painter->drawText(textRect, Qt::AlignLeft | Qt::AlignVCenter, text); - +*/ painter->restore(); } -void BCDelegate::onHighlightRow(int row) +void BCItemDelegate::onHighlightRow(int row) { qDebug() << " --- should highlight: " << row; @@ -334,7 +323,7 @@ void BCDelegate::onHighlightRow(int row) } // Optional: alle Highlights sofort clearen -void BCDelegate::clearAllHighlights() +void BCItemDelegate::clearAllHighlights() { for(auto* anim : std::as_const(m_rowAnimations)) { @@ -350,11 +339,11 @@ void BCDelegate::clearAllHighlights() } } -void BCDelegate::updateRow(int row) +void BCItemDelegate::updateRow(int row) { if (_view && _view->model() && row >= 0) { - QModelIndex idx = _view->model()->index(row, 0); + QModelIndex idx = _view->model()->index(row,1); QRect rect = _view->visualRect(idx); if (!rect.isEmpty()) { _view->viewport()->update(rect); @@ -362,35 +351,3 @@ void BCDelegate::updateRow(int row) } } -QString BCDelegate::formatDisplayString(const QModelIndex& index) const -{ - if (!index.isValid()) - return QString(); - - QString displayStr; - - /* - QString label = index.data(BCValueListModel::LabelRole).toString(); - QVariant value = index.data(BCValueListModel::ValueRole); - QString unit = index.data(BCValueListModel::UnitRole).toString(); - - QString valueStr = value.toString(); - - // Formatierung für Zahlen - bool ok; - double numValue = value.toDouble(&ok); - if (ok) { - valueStr = QString::number(numValue, 'f', 2); - } - - // Zusammensetzen des Anzeige-Strings - QString displayStr = label; - if (!displayStr.isEmpty() && !valueStr.isEmpty()) - displayStr += ": "; - displayStr += valueStr; - if (!unit.isEmpty()) - displayStr += " " + unit; - */ - - return displayStr; -} diff --git a/bcitemdelegate.h b/bcitemdelegate.h index 924ab97..b88ad5e 100644 --- a/bcitemdelegate.h +++ b/bcitemdelegate.h @@ -30,8 +30,8 @@ ***************************************************************************/ -#ifndef BCDELEGATE_H -#define BCDELEGATE_H +#ifndef BCITEMDELEGATE_H +#define BCITEMDELEGATE_H #include @@ -40,16 +40,15 @@ class QVariantAnimation; class QTableView; class BCValueList; -class BCDelegate : public QStyledItemDelegate +class BCItemDelegate : public QStyledItemDelegate { Q_OBJECT - Q_PROPERTY(qreal highlightOpacity READ highlightOpacity WRITE setHighlightOpacity) public: - explicit BCDelegate(const BCValueList& valueList, QTableView* view ); + explicit BCItemDelegate(const BCValueList& valueList, QTableView* view ); - QString displayText(const QVariant& dataValue, const QLocale& locale) const override; + // QString displayText(const QVariant& dataValue, const QLocale& locale) const override; // Zuständig für den Edit-Modus (Doppelklick) QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex& index) const override; @@ -60,6 +59,7 @@ public: QSize sizeHint(const QStyleOptionViewItem &option,const QModelIndex& index) const override; void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex& index) const override; + /* qreal highlightOpacity() const { return _opacity; @@ -72,6 +72,7 @@ public: // __fix! unsinn! emit viewUpdateNeeded(); } +*/ void clearAllHighlights(); @@ -82,19 +83,18 @@ public slots: signals: - void viewUpdateNeeded(); + //void viewUpdateNeeded(); private: void updateRow(int row); - QString formatDisplayString(const QModelIndex& index) const; void paintHighlightRow(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const; const BCValueList& _valueList; QTableView* _view{}; - int _highlightedRow{-1}; - qreal _opacity{1.0}; + //int _highlightedRow{-1}; + //qreal _opacity{1.0}; QPropertyAnimation* _animation{}; @@ -106,4 +106,4 @@ private: }; -#endif // BCDELEGATE_H +#endif // BCITEMDELEGATE_H diff --git a/bcmainwindow.cpp b/bcmainwindow.cpp index b6fc2e8..dc13d79 100644 --- a/bcmainwindow.cpp +++ b/bcmainwindow.cpp @@ -34,7 +34,7 @@ #include "qassert.h" #include -#include +#include #include @@ -114,17 +114,11 @@ void BCMainWindow::initMainWindow() configureAction(_batteryButton, _batteryAction, BCDevice::ID::Battery, "Battery Settings"_L1 ); configureAction(_pimpButton, _pimpAction, BCDevice::ID::Pimp, "Pimp my Ride"_L1 ); - - /* - BCDelegate* _delegate = new BCDelegate( _valueView); - //_delegate = new AnimatedDelegate(_valueView ); - _valueView->setItemDelegate( _delegate ); - // Verwendung: - connect(_delegate, &BCDelegate::viewUpdateNeeded, _valueView->viewport(), QOverload<>::of(&QWidget::update)); + connect(_delegate, &BCItemDelegate::viewUpdateNeeded, _valueView->viewport(), QOverload<>::of(&QWidget::update)); //_valueView->setItemDelegate(_delegate); @@ -140,7 +134,7 @@ void BCMainWindow::initMainWindow() // besser: model::emit dataChanged // also: emit dataChanged(index, index, {Qt::DisplayRole, Qt::EditRole, ValueRole}); - //connect( &_dataManager, &BCMainWindow::valueTouched, _delegate, &BCDelegate::onHighlightRow ); + //connect( &_dataManager, &BCMainWindow::valueTouched, _delegate, &BCItemDelegate::onHighlightRow ); connect( _connectButton, &QToolButton::clicked, &_transmitter, &BCTransmitter::onToggleConnectionState ); connect( _syncButton, &QToolButton::clicked, this, &BCMainWindow::onSyncFromDevice ); @@ -222,10 +216,8 @@ void BCMainWindow::onValueUpdated(BCDevice::ID deviceID, int index, BC::State st if( _devicePanels.contains( deviceID ) ) { BCDevicePanel& panel = *_devicePanels[deviceID]; - panel.getValueModel().onValueUpdated( index, state, newValue ); + panel.onValueUpdated( index, state, newValue ); } - -//_devicePanels; } void BCMainWindow::onRunnerMessage(const QString &msg) @@ -258,7 +250,7 @@ void BCMainWindow::onSyncFromDevice() emit sendValueCommand( BC::OpID::ReadValue, &value); emit valueTouched( value.indexRow ); - bc::processEventsFor(500); + bc::processEventsFor(50); diff --git a/bcmainwindow.ui b/bcmainwindow.ui index f17f588..6852c6a 100644 --- a/bcmainwindow.ui +++ b/bcmainwindow.ui @@ -14,144 +14,155 @@ BCMainWindow - + - - - background-color: grey; + + + Bionx Control - - - - - - 64 - 64 - - - - ... - - - - 64 - 64 - - - - - - - - - 64 - 64 - - - - ... - - - - 64 - 64 - - - - - - - - - 64 - 64 - - - - ... - - - - 64 - 64 - - - - - - - - - 64 - 64 - - - - ... - - - - 48 - 48 - - - - - - - - Qt::Orientation::Vertical - - - - 20 - 239 - - - - - - - - Sync - - - - - - - Connect - - - true - - - - - - - - 200 - 0 - - - - background-color: hotpink; - - - 0 - - - 0 - - - - - - + + + + + background-color: grey; + + + + + + + 64 + 64 + + + + ... + + + + 64 + 64 + + + + + + + + + 64 + 64 + + + + ... + + + + 64 + 64 + + + + + + + + + 64 + 64 + + + + ... + + + + 64 + 64 + + + + + + + + + 64 + 64 + + + + ... + + + + 48 + 48 + + + + + + + + Qt::Orientation::Vertical + + + + 20 + 239 + + + + + + + + Sync + + + + + + + Connect + + + true + + + + + + + + + + + 200 + 0 + + + + background-color: hotpink; + + + 0 + + + 0 + + + + + + + + diff --git a/bctransmitter.cpp b/bctransmitter.cpp index 2d9c25d..28508d9 100644 --- a/bctransmitter.cpp +++ b/bctransmitter.cpp @@ -88,9 +88,6 @@ void BCTransmitter::enqueueValueOp(BC::OpID opID, const BCValue* value) QMutexLocker locker(&_mutex); _valueQueue.enqueue( value ); - // Logging (Thread-safe via Signal) - //emit messageLogged(QString("Command %1 queued.").arg(cmd.label)); - // wir wollen nicht den ganzen Value verschicken, erstrecht // wollen wir den Value in verschiedenen Threads gleichzeitig // in die Hand nehmen, also hantieren wir nur mit den Inidizes. @@ -133,7 +130,7 @@ void BCTransmitter::processValueOp( BC::OpID opID ) // Abkürzung const BCValue& val = *currentValue; // Value ist 'under construction' - emit valueUpdated( val.deviceID, val.indexRow, BC::State::Locked ); + //emit valueUpdated( val.deviceID, val.indexRow, BC::State::Locked ); if( opID == BC::OpID::ReadValue ) { QString result = currentValue->readRawValueX( *this ); @@ -151,8 +148,6 @@ void BCTransmitter::processValueOp( BC::OpID opID ) qDebug() << " --- OUCH!"; } - - //emit valueStateChanged(cmd.id, true); //emit valueStateChanged(0, true); } diff --git a/bcvalue.h b/bcvalue.h index 240f9c1..bf5d19c 100644 --- a/bcvalue.h +++ b/bcvalue.h @@ -136,6 +136,7 @@ public: // Copy Assignment Operator BCValueList& operator=(const BCValueList& other) { + qDebug() << "BC copy assignment: " << this; QList::operator=( other ); return *this; } @@ -143,6 +144,7 @@ public: // Move Assignment Operator BCValueList& operator=(BCValueList&& other) noexcept { + qDebug() << "BC move assignment: " << this; QList::operator=( other ); return *this; } diff --git a/bcvaluemodel.cpp b/bcvaluemodel.cpp index a78470b..befd979 100644 --- a/bcvaluemodel.cpp +++ b/bcvaluemodel.cpp @@ -31,6 +31,7 @@ #include +#include /** * @brief Konstruktor, ohne Besonderheiten @@ -94,7 +95,7 @@ void BCValueModel::takeValueList(BCValueList& newValueList) void BCValueModel::onValueUpdated( int row, BC::State state, const QString& newVisisbleValue ) { - qDebug() << " Panel update: " << newVisisbleValue; + qDebug() << " BCValueModel::onValueUpdated update: " << newVisisbleValue; if( row > -1 && row < _valueList.size() ) { const BCValue& value = _valueList[row]; @@ -160,41 +161,53 @@ QVariant BCValueModel::headerData(int section, Qt::Orientation orientation, int /** - * @brief Gibt die Model-Daten zurück. Das Model Gibt hier, unabhängig von der DataRole, immer das - * gesamte BCValue Object zurück. Die Umsetzung von Status- und Typeinfromationen in GUI-Elemente - * findet nicht hier, sondern im BCDelagate statt. + * @brief Gibt die Model-Daten zurück. */ QVariant BCValueModel::data(const QModelIndex& index, int role) const { Q_UNUSED(role) - /* - // Bonus: Rechtsbündig für Zahlen - if (role == Qt::TextAlignmentRole && (index.column() == 0 || index.column() == 2)) { - return Qt::AlignRight | Qt::AlignVCenter; - } + int row = index.row(); + int col = index.column(); - return QVariant(); - */ - int row = index.row(); - if (!index.isValid() || row >= _valueList.size()) + bool wrongRole = ( role != Qt::DisplayRole && role != Qt::EditRole); + if (wrongRole || !index.isValid() || row >= _valueList.size() ) return QVariant(); - const BCValue* entry = &_valueList.at( row ); - return QVariant::fromValue( entry ); + const BCValue& value = _valueList.at( row ); - /* - if (role == Qt::DisplayRole || role == Qt::EditRole) + if( col == 0 ) + return value.label; + + if( col == 1) { - if( col == 0 ) - return entry.label; - else if( col == 1 ) - return entry.visibleValue; + if( role == Qt::DisplayRole ) + return QString("%1 %2").arg( value.visibleValue, value.valueType->unitLabel); + + return value.visibleValue; } - */ + + return QVariant(); + /* +Das Model Gibt hier, unabhängig von der DataRole, immer das + * gesamte BCValue Object zurück. Die Umsetzung von Status- und Typeinfromationen in GUI-Elemente + * findet nicht hier, sondern im BCDelagate statt. + +// falsch! wie geben hier doch ordentlich die einzelwerte zurück + + // Bonus: Rechtsbündig für Zahlen + if (role == Qt::TextAlignmentRole && (index.column() == 0 || index.column() == 2)) { + return Qt::AlignRight | Qt::AlignVCenter; + } + + //return QVariant::fromValue( entry ); + + return QVariant(); + + */ }