Rework itemdelegate, part I.

This commit is contained in:
2025-12-29 15:44:06 +01:00
parent 527f66759f
commit 0bb39a74fe
10 changed files with 64 additions and 147 deletions

View File

@@ -30,7 +30,6 @@ SOURCES += \
bcdelegate.cpp \
bcdevicepanel.cpp \
bclegacy.cpp \
bctoolbutton.cpp \
bctransmitter.cpp \
bcvalue.cpp \
bcvaluemodel.cpp \
@@ -49,7 +48,6 @@ HEADERS += \
bcdelegate.h \
bcdevicepanel.h \
bcmainwindow.h \
bctoolbutton.h \
bctransmitter.h \
bcvalue.h \
bcvaluemodel.h \

View File

@@ -49,8 +49,8 @@
BCDelegate::BCDelegate(QTableView* view)
: QStyledItemDelegate(view), _view{view}
BCDelegate::BCDelegate(const BCValueList& valueList, QTableView* view)
: QStyledItemDelegate{view}, _valueList{valueList}, _view{view}
{
}
@@ -59,10 +59,10 @@ BCDelegate::BCDelegate(QTableView* view)
QString BCDelegate::displayText(const QVariant& dataValue, const QLocale& locale) const
{
// Wir prüfen, ob im Variant unser Struct steckt
if (dataValue.canConvert<BCValue*>())
if (dataValue.canConvert<const BCValue*>())
{
BCValue& bc = *dataValue.value<BCValue*>();
qDebug() << " --- YES: " << bc.label;
const BCValue& bc = *dataValue.value<const BCValue*>();
//qDebug() << " --- YES: " << bc.label;
// Hier bauen wir den String zusammen, den man sieht,
// wenn KEIN Editor offen ist.
// Format: "Label: Wert Einheit"
@@ -70,7 +70,7 @@ QString BCDelegate::displayText(const QVariant& dataValue, const QLocale& locale
}
else
{
//qDebug() << " --- Nö!";
qDebug() << " --- Nö!";
}
// Fallback für normale Strings/Zahlen
@@ -162,6 +162,7 @@ void BCDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const
void BCDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex& index) const
{
// __fix!
editor->setGeometry(option.rect);
}
@@ -181,11 +182,12 @@ QSize BCDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex
void BCDelegate::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-Overly: Oranger Rahmen, wenn Zeile passt
// 2. Unser Custom-Overlay: Oranger Rahmen, wenn Zeile passt
if (index.row() == _highlightedRow)
{
painter->save();

View File

@@ -38,6 +38,7 @@
class QPropertyAnimation;
class QVariantAnimation;
class QTableView;
class BCValueList;
class BCDelegate : public QStyledItemDelegate
{
@@ -46,7 +47,7 @@ class BCDelegate : public QStyledItemDelegate
public:
explicit BCDelegate(QTableView* view );
explicit BCDelegate(const BCValueList& valueList, QTableView* view );
QString displayText(const QVariant& dataValue, const QLocale& locale) const override;
@@ -89,9 +90,12 @@ private:
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};
QTableView* _view{};
QPropertyAnimation* _animation{};
private:

View File

@@ -39,8 +39,10 @@ BCDevicePanel::BCDevicePanel(QWidget *parent)
setupUi(this);
_valueView->setModel( &_valueModel );
//_valueView->resizeColumnsToContents();
_valueView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
_valueView->setItemDelegate( new BCDelegate( _valueView) );
//_valueView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
BCDelegate* delegate = new BCDelegate( _valueModel.getValueList(), _valueView);
_valueView->setItemDelegate( delegate );
}

View File

@@ -30,6 +30,8 @@
***************************************************************************/
//#include <QToolButton>
#include "qassert.h"
#include <bcmainwindow.h>
#include <bcdelegate.h>
@@ -38,7 +40,7 @@
/**
* @brief Das Mainwindow erzeugen
* @param parent
* @param parent Das Elternwidget
*/
BCMainWindow::BCMainWindow(QWidget *parent)
@@ -54,30 +56,24 @@ BCMainWindow::BCMainWindow(QWidget *parent)
}
/**
* @brief Destruktor. Räumt den Workthread auf.
*/
BCMainWindow::~BCMainWindow()
{
_worker.quit(); // Event Loop stoppen
_worker.wait(); // Warten bis Thread wirklich fertig ist
}
/**
* @brief Initialisiert alle Komponenten des MainWindows.
*/
void BCMainWindow::initMainWindow()
{
// Die Daten und auch die Datenmodelle für die Views werden
// vom DataManager verwaltet und an die Views weitergereicht.
/*
auto setDeviceModel = [&]( BCDevice::ID deviceID, BCDevicePanel* panel )
{
auto model = _dataManager.getModel( deviceID );
if( model)
{
QAbstractItemView* valueView = panel->getValueView();
valueView->setModel( *model );
//valueView->resizeColumnsToContents();
//_valueView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
}
};
*/
auto configureAction = [&]( QToolButton* button, QAction* action, BCDevice::ID deviceID, const QString& panelTitle="" )
{
@@ -145,8 +141,8 @@ 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( _connectButton, &QPushButton::clicked, &_transmitter, &BCTransmitter::onToggleConnectionState );
connect( _syncButton, &QPushButton::clicked, this, &BCMainWindow::onSyncFromDevice );
connect( _connectButton, &QToolButton::clicked, &_transmitter, &BCTransmitter::onToggleConnectionState );
connect( _syncButton, &QToolButton::clicked, this, &BCMainWindow::onSyncFromDevice );
connect( &_transmitter, &BCTransmitter::valueUpdated, this, &BCMainWindow::onValueUpdated );

View File

@@ -22,7 +22,7 @@
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item alignment="Qt::AlignmentFlag::AlignHCenter">
<widget class="BCToolButton" name="_motorButton">
<widget class="QToolButton" name="_motorButton">
<property name="minimumSize">
<size>
<width>64</width>
@@ -41,7 +41,7 @@
</widget>
</item>
<item alignment="Qt::AlignmentFlag::AlignHCenter">
<widget class="BCToolButton" name="_batteryButton">
<widget class="QToolButton" name="_batteryButton">
<property name="minimumSize">
<size>
<width>64</width>
@@ -60,7 +60,7 @@
</widget>
</item>
<item alignment="Qt::AlignmentFlag::AlignHCenter">
<widget class="BCToolButton" name="_consoleButton">
<widget class="QToolButton" name="_consoleButton">
<property name="minimumSize">
<size>
<width>64</width>
@@ -79,7 +79,7 @@
</widget>
</item>
<item alignment="Qt::AlignmentFlag::AlignHCenter">
<widget class="BCToolButton" name="_pimpButton">
<widget class="QToolButton" name="_pimpButton">
<property name="minimumSize">
<size>
<width>64</width>
@@ -111,14 +111,14 @@
</spacer>
</item>
<item alignment="Qt::AlignmentFlag::AlignHCenter">
<widget class="QPushButton" name="_syncButton">
<widget class="QToolButton" name="_syncButton">
<property name="text">
<string>Sync</string>
</property>
</widget>
</item>
<item alignment="Qt::AlignmentFlag::AlignHCenter">
<widget class="QPushButton" name="_connectButton">
<item>
<widget class="QToolButton" name="_connectButton">
<property name="text">
<string>Connect</string>
</property>
@@ -242,11 +242,6 @@
<header location="global">bcdevicepanel.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>BCToolButton</class>
<extends>QToolButton</extends>
<header location="global">bctoolbutton.h</header>
</customwidget>
</customwidgets>
<resources>
<include location="bionxcontrol.qrc"/>

View File

@@ -1,44 +0,0 @@
/***************************************************************************
BionxControl
Copyright © 2025 christoph holzheuer
christoph.holzheuer@gmail.com
Using:
mhs_can_drv.c
© 2011 - 2023 by MHS-Elektronik GmbH & Co. KG, Germany
Klaus Demlehner, klaus@mhs-elektronik.de
@see www.mhs-elektronik.de
Based on Bionx data type descriptions from:
BigXionFlasher USB V 0.2.4 rev. 97
© 2011-2013 by Thomas Koenig <info@bigxionflasher.org>
@see www.bigxionflasher.org
Bionx Bike Info
© 2018 Thorsten Schmidt (tschmidt@ts-soft.de)
@see www.ts-soft.de
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
@see https://github.com/bikemike/bionx-bikeinfo
***************************************************************************/
#include <QDebug>
#include <bctoolbutton.h>
BCToolButton::BCToolButton(QWidget* parent)
: QToolButton(parent)
{
qDebug() << " --- dis cant work: " << objectName();
//setDefaultAction(
}

View File

@@ -1,46 +0,0 @@
/***************************************************************************
BionxControl
Copyright © 2025 christoph holzheuer
christoph.holzheuer@gmail.com
Using:
mhs_can_drv.c
© 2011 - 2023 by MHS-Elektronik GmbH & Co. KG, Germany
Klaus Demlehner, klaus@mhs-elektronik.de
@see www.mhs-elektronik.de
Based on Bionx data type descriptions from:
BigXionFlasher USB V 0.2.4 rev. 97
© 2011-2013 by Thomas Koenig <info@bigxionflasher.org>
@see www.bigxionflasher.org
Bionx Bike Info
© 2018 Thorsten Schmidt (tschmidt@ts-soft.de)
@see www.ts-soft.de
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
@see https://github.com/bikemike/bionx-bikeinfo
***************************************************************************/
#ifndef BCTOOLBUTTON_H
#define BCTOOLBUTTON_H
#include <QToolButton>
class BCToolButton : public QToolButton
{
public:
BCToolButton( QWidget* parent=nullptr );
};
#endif // BCTOOLBUTTON_H

View File

@@ -43,7 +43,7 @@
/*
Werte haben verschiedene Längen (1,2 und 4 Byte) und werder auf unterschiedliche Art und Weise
ausgelesen und geschrieben (Siehe BCValueTypeWord). Sin können also Wert-Typen zugeordnet werden. Ein Werttyp
ausgelesen und geschrieben (Siehe BCValueTypeWord). Sie können also Wert-Typen zugeordnet werden. Ein Werttyp
lässet über eine ID identifizieren, die mit der phyikalische Einheit des Wertes überschneiden kann,
aber nicht muss: : Km/h, kWh, BCValueTypeWord ... bilden eigene Typen, SoC, Assistence Level sind auch eigene Typen,
Teilen sich jedoch die Einheit '%'.
@@ -56,6 +56,10 @@
*/
/**
* @brief BCAbstractTransmitter ist das abstrakte Basisinterface für die eigentliche
* Datenübertragung auf Treiberebene.
*/
class BCAbstractTransmitter
{
@@ -101,9 +105,9 @@ public:
//mutable std::optional<uint32_t> rawValue;
};
// ?? ist das nötig?
Q_DECLARE_METATYPE(BCValue*)
Q_DECLARE_METATYPE(const BCValue*)
//Q_DECLARE_METATYPE(const BCValue&)
//using BCValueList = QList<BCValue>;

View File

@@ -140,10 +140,6 @@ int BCValueModel::columnCount(const QModelIndex& parent) const
/**
* @brief Gibt die Header-Einträge zurück
* @param section
* @param orientation
* @param role
* @return
*/
QVariant BCValueModel::headerData(int section, Qt::Orientation orientation, int role) const
@@ -162,8 +158,17 @@ 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.
*/
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)) {
@@ -173,13 +178,13 @@ QVariant BCValueModel::data(const QModelIndex& index, int role) const
return QVariant();
*/
int row = index.row();
int col = index.column();
if (!index.isValid() || row >= _valueList.size())
return QVariant();
const BCValue* entry = &_valueList.at( row );
return QVariant::fromValue( entry );
const BCValue& entry = _valueList.at( row );
/*
if (role == Qt::DisplayRole || role == Qt::EditRole)
{
if( col == 0 )
@@ -187,8 +192,9 @@ QVariant BCValueModel::data(const QModelIndex& index, int role) const
else if( col == 1 )
return entry.visibleValue;
}
*/
return QVariant();
}