From 5327e4f7b09193b4eb09e5b5b61aa163bc2375c7 Mon Sep 17 00:00:00 2001 From: peri4 Date: Fri, 16 Dec 2022 23:06:53 +0300 Subject: [PATCH] PIValueTreeEdit label switch support, label style --- libs/piqt_widgets/pivaluetree_edit.cpp | 112 ++++++++++++++++-- libs/piqt_widgets/pivaluetree_edit.h | 6 +- .../pivaluetree_edit_parameters.cpp | 35 ++++++ .../pivaluetree_edit_parameters.h | 1 + libs/piqt_widgets/pivariant_edit.h | 2 + utils/piconnedit/piconnedit_main.cpp | 2 +- 6 files changed, 145 insertions(+), 13 deletions(-) diff --git a/libs/piqt_widgets/pivaluetree_edit.cpp b/libs/piqt_widgets/pivaluetree_edit.cpp index d1fae40..56f2783 100644 --- a/libs/piqt_widgets/pivaluetree_edit.cpp +++ b/libs/piqt_widgets/pivaluetree_edit.cpp @@ -84,6 +84,7 @@ void PIValueTreeEdit::removeAll() { value_edits.clear(); tree_edits.clear(); comm_labels.clear(); + label_labels.clear(); if (widget_array) { ui_array->layoutArray->removeWidget(grid); grid->hide(); @@ -141,8 +142,7 @@ void PIValueTreeEdit::build() { if (i.attribute(Attribute::hidden, false).toBool()) continue; if (i.attribute(Attribute::isLabel, false).toBool()) { if (i.name().isEmpty()) continue; - auto * l = new QLabel(PI2QString(i.name())); - l->setAlignment(Qt::AlignCenter); + auto * l = newLabel(i); grid->add(i, l); continue; } @@ -203,6 +203,11 @@ void PIValueTreeEdit::actionTriggered(QToolButton * button, const PIString & vn, comm_labels[nn] = comm_labels[vn]; comm_labels.remove(vn); } + if (label_labels.contains(vn)) { + label_labels[nn] = label_labels[vn]; + label_labels[nn]->setText(PI2QString(nn)); + label_labels.remove(vn); + } } if (a == widget_params->actionRemove) { source.remove(vn); @@ -210,6 +215,7 @@ void PIValueTreeEdit::actionTriggered(QToolButton * button, const PIString & vn, value_edits.remove(vn); tree_edits.remove(vn); comm_labels.remove(vn); + label_labels.remove(vn); } if (a == widget_params->actionChange) { auto & vt(source[vn]); @@ -221,12 +227,33 @@ void PIValueTreeEdit::actionTriggered(QToolButton * button, const PIString & vn, ve->setValue(vt); // ve->applyArrayAttributes(); } else { - auto * ve = value_edits.value(vn, nullptr); - if (!ve) return; - vt.setValue(ve->value()); + bool was_label = vt.attribute(Attribute::isLabel, false).toBool(); + auto * ve = value_edits.value(vn, nullptr); + if (ve) vt.setValue(ve->value()); if (!widget_params->showFor(vt)) return; - ve->setAttributes(vt.attributes()); - ve->setValue(vt.value()); + bool now_label = vt.attribute(Attribute::isLabel, false).toBool(); + if (was_label ^ now_label) { + if (now_label) { + auto * l = newLabel(vt); + grid->replace(grid->getRow(button), l); + value_edits.remove(vt.name()); + comm_labels.remove(vt.name()); + } else { + auto * ve = new PIVariantEdit(); + ve->setAttributes(vt.attributes()); + ve->setValue(vt.value()); + grid->replace(grid->getRow(button), PI2QString(vt.name()), ve, PI2QString(vt.comment())); + value_edits[vt.name()] = ve; + } + ve = nullptr; + } + if (ve) { + ve->setAttributes(vt.attributes()); + ve->setValue(vt.value()); + } + if (now_label) { + label_labels[vt.name()]->setStyleSheet(PI2QString(vt.attribute(Attribute::style).toString())); + } } auto * cl = comm_labels.value(vn, nullptr); if (cl) cl->setText(PI2QString(vt.comment())); @@ -295,6 +322,16 @@ void PIValueTreeEdit::applyArrayAttributes() { } +QLabel * PIValueTreeEdit::newLabel(const PIValueTree & vt) { + auto * l = new QLabel(); + l->setAlignment(Qt::AlignCenter); + l->setText(PI2QString(vt.name())); + l->setStyleSheet(PI2QString(vt.attribute(Attribute::style).toString())); + label_labels[vt.name()] = l; + return l; +} + + void PIValueTreeEdit::newRequest(NewType type) { PIString nn = Q2PIString(QInputDialog::getText(nullptr, tr("New item"), tr("Input new name:"))); if (nn.isEmpty()) return; @@ -397,6 +434,29 @@ void PIValueTreeEdit::GridWidgets::add(const PIValueTree & vt, QWidget * w, bool } +void PIValueTreeEdit::GridWidgets::replace(int row, QWidget * w) { + int col = removeRowEdits(row); + lay->addWidget(w, row, col, 1, -1); + widgets << w; +} + + +void PIValueTreeEdit::GridWidgets::replace(int row, QString label, QWidget * w, const QString & comment) { + int col = removeRowEdits(row); + if (!label.isEmpty()) label += ':'; + auto * l = new QLabel(label); + auto * c = new QLabel(comment); + l->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); + c->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); + w->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); + lay->addWidget(l, row, col++, Qt::AlignVCenter | Qt::AlignRight); + lay->addWidget(w, row, col++); + lay->addWidget(c, row, col++, Qt::AlignVCenter | Qt::AlignLeft); + widgets << l << w << c; + labels << l; +} + + int PIValueTreeEdit::GridWidgets::beginRow(const PIValueTree & vt, bool is_group) { if (!create_edit_buttons) return 0; auto * b = new QToolButton(); @@ -456,6 +516,24 @@ void PIValueTreeEdit::GridWidgets::changed() { } +int PIValueTreeEdit::GridWidgets::removeRowEdits(int row) { + int col = create_edit_buttons ? 1 : 0; + for (int c = col; c < lay->columnCount(); ++c) { + auto * li = lay->itemAtPosition(row, c); + if (li) { + QWidget * w = li->widget(); + if (w) { + widgets.removeOne(w); + QLabel * lbl = qobject_cast(w); + if (lbl) labels.removeOne(lbl); + delete w; + } + } + } + return col; +} + + void PIValueTreeEdit::GridWidgets::removeRow(int index) { if (!lay) return; if ((index < 0) || (index >= row_count) || (index >= lay->rowCount())) return; @@ -478,17 +556,28 @@ void PIValueTreeEdit::GridWidgets::removeRow(int index) { void PIValueTreeEdit::GridWidgets::simplify(const PIMap & map) { + struct Info { + Qt::Alignment align; + int row_span = 1; + int col_span = 1; + }; if (!lay) return; QVector> wg; - QMap wa; + QMap wa; for (int r = 0; r < lay->rowCount(); ++r) { QMap row; for (int c = 0; c < lay->columnCount(); ++c) { auto * li = lay->itemAtPosition(r, c); if (!li) continue; if (li->widget()) { - row[c] = li->widget(); - wa[li->widget()] = li->alignment(); + row[c] = li->widget(); + Info info; + info.align = li->alignment(); + int pos[4]; + lay->getItemPosition(lay->indexOf(li), &(pos[0]), &(pos[1]), &(pos[2]), &(pos[3])); + info.row_span = pos[2]; + info.col_span = pos[3]; + wa[li->widget()] = info; } } if (!row.isEmpty()) wg << row; @@ -503,7 +592,8 @@ void PIValueTreeEdit::GridWidgets::simplify(const PIMap & map) { QMapIterator it(wg[mi]); while (it.hasNext()) { it.next(); - lay->addWidget(it.value(), rindex, it.key(), wa.value(it.value())); + Info info = wa.value(it.value()); + lay->addWidget(it.value(), rindex, it.key(), info.row_span, info.col_span, info.align); } ++rindex; } diff --git a/libs/piqt_widgets/pivaluetree_edit.h b/libs/piqt_widgets/pivaluetree_edit.h index d25b697..9c603cf 100644 --- a/libs/piqt_widgets/pivaluetree_edit.h +++ b/libs/piqt_widgets/pivaluetree_edit.h @@ -76,6 +76,7 @@ private: PIValueTreeEdit * addTreeEdit(const PIValueTree & vt); void addValueEdit(const PIValueTree & vt); void applyArrayAttributes(); + QLabel * newLabel(const PIValueTree & vt); class GridWidgets: public QWidget { public: @@ -86,6 +87,8 @@ private: void removeRow(int index); void add(const PIValueTree & vt, QString label, QWidget * w, const QString & comment, bool is_group = false); void add(const PIValueTree & vt, QWidget * w, bool is_group = false); + void replace(int row, QWidget * w); + void replace(int row, QString label, QWidget * w, const QString & comment); int beginRow(const PIValueTree & vt, bool is_group); void rename(QString prev_name, QString new_name); void reorder(const PIMap & map) { simplify(map); } @@ -96,6 +99,7 @@ private: QToolButton * button_add; private: + int removeRowEdits(int row); void simplify(const PIMap & map = PIMap()); int row_count = 0; @@ -114,7 +118,7 @@ private: PIVector array_edits; PIMap value_edits; PIMap tree_edits; - PIMap comm_labels; + PIMap comm_labels, label_labels; Ui::PIValueTreeEditArray * ui_array; GridWidgets * grid = nullptr; mutable PIValueTree source; diff --git a/libs/piqt_widgets/pivaluetree_edit_parameters.cpp b/libs/piqt_widgets/pivaluetree_edit_parameters.cpp index dc06304..09dab33 100644 --- a/libs/piqt_widgets/pivaluetree_edit_parameters.cpp +++ b/libs/piqt_widgets/pivaluetree_edit_parameters.cpp @@ -48,6 +48,7 @@ bool PIValueTreeEditParameters::showFor(PIValueTree & vt) { } comboType->blockSignals(true); + comboType->setEnabled(true); if (vt.isArray()) { uint array_type = PIVariant::typeIDFromName(vt.attribute(Attribute::arrayType).toString()); comboType->setCurrentIndex(comboType->findData(array_type)); @@ -58,13 +59,19 @@ bool PIValueTreeEditParameters::showFor(PIValueTree & vt) { createAttributes(ve_array, layoutArray, vt.attributes(), true); groupArray->show(); } else { + if (vt.attribute(Attribute::isLabel, false).toBool()) { + comboType->setEnabled(false); + checkAttribute(vt, Attribute::style, ""); + } comboType->setCurrentIndex(comboType->findData(vt.value().typeID())); groupArray->hide(); } comboType->blockSignals(false); checkHidden->setChecked(vt.attribute(Attribute::hidden, false).toBool()); checkReadOnly->setChecked(vt.attribute(Attribute::readOnly, false).toBool()); + checkLabel->blockSignals(true); checkLabel->setChecked(vt.attribute(Attribute::isLabel, false).toBool()); + checkLabel->blockSignals(false); lineComment->setText(PI2QString(vt.comment())); createAttributes(ve_attr, layoutAttributes, vt.attributes()); @@ -142,3 +149,31 @@ void PIValueTreeEditParameters::checkAttribute(PIValueTree & vt, PIString an, PI void PIValueTreeEditParameters::on_comboType_currentIndexChanged(int) { createAttributes(ve_attr, layoutAttributes); } + + +void PIValueTreeEditParameters::on_checkLabel_toggled(bool on) { + if (!on) { + comboType->setEnabled(true); + for (int r = 0; r < layoutAttributes->rowCount(); ++r) { + auto * w = qobject_cast(layoutAttributes->itemAt(r, QFormLayout::FieldRole)->widget()); + if (!w) continue; + PIString an = Q2PIString(w->property(property_name).toString()); + if (an == Attribute::style) { + ve_attr.removeOne(w); + layoutAttributes->removeRow(r); + break; + } + } + } else { + comboType->setEnabled(false); + comboType->setCurrentIndex(comboType->findData(PIVariant::typeID())); + ve_attr.clear(); + while (layoutAttributes->rowCount() > 0) + layoutAttributes->removeRow(0); + createAttributes(ve_attr, + layoutAttributes, + { + {Attribute::style, ""} + }); + } +} diff --git a/libs/piqt_widgets/pivaluetree_edit_parameters.h b/libs/piqt_widgets/pivaluetree_edit_parameters.h index bc7a0ca..e96c370 100644 --- a/libs/piqt_widgets/pivaluetree_edit_parameters.h +++ b/libs/piqt_widgets/pivaluetree_edit_parameters.h @@ -51,6 +51,7 @@ private: private slots: void on_comboType_currentIndexChanged(int); + void on_checkLabel_toggled(bool on); }; diff --git a/libs/piqt_widgets/pivariant_edit.h b/libs/piqt_widgets/pivariant_edit.h index f1e7dde..b4611fb 100644 --- a/libs/piqt_widgets/pivariant_edit.h +++ b/libs/piqt_widgets/pivariant_edit.h @@ -78,6 +78,8 @@ private: class QAD_PIQT_UTILS_EXPORT PIVariantEdit: public QWidget { + Q_OBJECT + public: PIVariantEdit(QWidget * parent = nullptr); ~PIVariantEdit(); diff --git a/utils/piconnedit/piconnedit_main.cpp b/utils/piconnedit/piconnedit_main.cpp index 7736d5c..44c1aab 100644 --- a/utils/piconnedit/piconnedit_main.cpp +++ b/utils/piconnedit/piconnedit_main.cpp @@ -58,7 +58,7 @@ int main(int argc, char * argv[]) { e.setFullEditMode(true); e.setGeometry(500, 400, 100, 50); // e.setValue(PIValueTreeConversions::fromJSON(PIJSON::fromJSON(PIValueTreeConversions::toJSON(vt).toJSON()))); - e.setValue(vt); + e.setValue(root); area.setWidget(&e); area.show(); // piCout << PIValueTreeConversions::toText(e.value()); //.toJSON(PIJSON::Tree);