rework editor creation for MarkerTraceDelegate
Some checks failed
Build / PC_Application_Ubuntu (push) Has been cancelled
Build / PC_Application_RPi5 (push) Has been cancelled
Build / PC_Application_Windows (push) Has been cancelled
Build / PC_Application_OSX (push) Has been cancelled
Build / PC_Application_OSX_13 (push) Has been cancelled
Build / Embedded_Firmware (push) Has been cancelled
HIL_Tests / Get_Repository (push) Has been cancelled
Unit_Tests / Tests (push) Has been cancelled
HIL_Tests / PC_Application_RPi5 (push) Has been cancelled
HIL_Tests / Embedded_Firmware (push) Has been cancelled
HIL_Tests / HIL (push) Has been cancelled

This commit is contained in:
Jan Käberich 2025-08-22 11:47:48 +02:00
parent f0c7f289cb
commit d882bca547
4 changed files with 39 additions and 33 deletions

View file

@ -1480,6 +1480,35 @@ void Marker::setNumber(int value)
}
}
QWidget *Marker::getTraceEditor(QAbstractItemDelegate *delegate)
{
auto c = new QComboBox;
for(auto t : model->getModel().getTraces()) {
c->addItem(t->name());
if(parentTrace == t) {
// select this item
c->setCurrentIndex(c->count() - 1);
}
}
connect(c, qOverload<int>(&QComboBox::currentIndexChanged), [=](int) {
emit delegate->commitData(c);
});
return c;
}
void Marker::updateTraceFromEditor(QWidget *w)
{
QComboBox *c = (QComboBox*) w;
for(auto t : model->getModel().getTraces()) {
if(c->currentText() == t->name()) {
if(parentTrace != t) {
assignTrace(t);
}
}
}
update();
}
QWidget *Marker::getTypeEditor(QAbstractItemDelegate *delegate)
{
auto c = new QComboBox;

View file

@ -111,8 +111,10 @@ public:
Last,
};
Type getType() const;
QWidget *getTraceEditor(QAbstractItemDelegate *delegate = nullptr);
void updateTraceFromEditor(QWidget *w);
QWidget *getTypeEditor(QAbstractItemDelegate *delegate = nullptr);
void updateTypeFromEditor(QWidget *c);
void updateTypeFromEditor(QWidget *w);
SIUnitEdit* getSettingsEditor();
QWidget *getRestrictEditor();
void adjustSettings(double value);

View file

@ -436,41 +436,17 @@ QSize MarkerTraceDelegate::sizeHint(const QStyleOptionViewItem &, const QModelIn
QWidget *MarkerTraceDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &index) const
{
auto model = (MarkerModel*) index.model();
auto c = new QComboBox(parent);
c->setMaximumHeight(rowHeight);
connect(c, qOverload<int>(&QComboBox::currentIndexChanged), [c](int) {
c->clearFocus();
});
auto traces = model->getModel().getTraces();
for(auto t : traces) {
MarkerWidgetTraceInfo info;
info.trace = t;
c->addItem(t->name(), QVariant::fromValue(info));
}
return c;
auto marker = static_cast<const MarkerModel*>(index.model())->markerFromIndex(index);
auto editor = marker->getTraceEditor(const_cast<MarkerTraceDelegate*>(this));
editor->setMaximumHeight(rowHeight);
editor->setParent(parent);
return editor;
}
void MarkerTraceDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
void MarkerTraceDelegate::setModelData(QWidget *editor, QAbstractItemModel *, const QModelIndex &index) const
{
auto marker = static_cast<const MarkerModel*>(index.model())->markerFromIndex(index);
auto c = (QComboBox*) editor;
MarkerWidgetTraceInfo markerInfo;
markerInfo.trace = marker->trace();
for(int i=0;i<c->count();i++) {
auto info = qvariant_cast<MarkerWidgetTraceInfo>(c->itemData(i));
if(info == markerInfo) {
c->setCurrentIndex(i);
return;
}
}
}
void MarkerTraceDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
auto markerModel = (MarkerModel*) model;
auto c = (QComboBox*) editor;
markerModel->setData(index, c->itemData(c->currentIndex()));
marker->updateTraceFromEditor(editor);
}
QSize MarkerSettingsDelegate::sizeHint(const QStyleOptionViewItem &, const QModelIndex &) const

View file

@ -14,7 +14,6 @@ class MarkerTraceDelegate : public QStyledItemDelegate
Q_OBJECT
QSize sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const override;
QWidget *createEditor(QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index) const override;
void setEditorData(QWidget * editor, const QModelIndex & index) const override;
void setModelData(QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const override;
};