diff --git a/Software/PC_Application/LibreVNA-GUI/Traces/Marker/marker.cpp b/Software/PC_Application/LibreVNA-GUI/Traces/Marker/marker.cpp index 5ef4752..53b180f 100644 --- a/Software/PC_Application/LibreVNA-GUI/Traces/Marker/marker.cpp +++ b/Software/PC_Application/LibreVNA-GUI/Traces/Marker/marker.cpp @@ -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(&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; diff --git a/Software/PC_Application/LibreVNA-GUI/Traces/Marker/marker.h b/Software/PC_Application/LibreVNA-GUI/Traces/Marker/marker.h index 12d6643..c2d586c 100644 --- a/Software/PC_Application/LibreVNA-GUI/Traces/Marker/marker.h +++ b/Software/PC_Application/LibreVNA-GUI/Traces/Marker/marker.h @@ -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); diff --git a/Software/PC_Application/LibreVNA-GUI/Traces/Marker/markermodel.cpp b/Software/PC_Application/LibreVNA-GUI/Traces/Marker/markermodel.cpp index e93f121..efc43d7 100644 --- a/Software/PC_Application/LibreVNA-GUI/Traces/Marker/markermodel.cpp +++ b/Software/PC_Application/LibreVNA-GUI/Traces/Marker/markermodel.cpp @@ -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(&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(index.model())->markerFromIndex(index); + auto editor = marker->getTraceEditor(const_cast(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(index.model())->markerFromIndex(index); - auto c = (QComboBox*) editor; - MarkerWidgetTraceInfo markerInfo; - markerInfo.trace = marker->trace(); - for(int i=0;icount();i++) { - auto info = qvariant_cast(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 diff --git a/Software/PC_Application/LibreVNA-GUI/Traces/Marker/markermodel.h b/Software/PC_Application/LibreVNA-GUI/Traces/Marker/markermodel.h index 3f2d683..aa7ec00 100644 --- a/Software/PC_Application/LibreVNA-GUI/Traces/Marker/markermodel.h +++ b/Software/PC_Application/LibreVNA-GUI/Traces/Marker/markermodel.h @@ -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; };