PIValueTreeEdit label switch support, label style

This commit is contained in:
2022-12-16 23:06:53 +03:00
parent 55e5e819f8
commit 5327e4f7b0
6 changed files with 145 additions and 13 deletions

View File

@@ -84,6 +84,7 @@ void PIValueTreeEdit::removeAll() {
value_edits.clear(); value_edits.clear();
tree_edits.clear(); tree_edits.clear();
comm_labels.clear(); comm_labels.clear();
label_labels.clear();
if (widget_array) { if (widget_array) {
ui_array->layoutArray->removeWidget(grid); ui_array->layoutArray->removeWidget(grid);
grid->hide(); grid->hide();
@@ -141,8 +142,7 @@ void PIValueTreeEdit::build() {
if (i.attribute(Attribute::hidden, false).toBool()) continue; if (i.attribute(Attribute::hidden, false).toBool()) continue;
if (i.attribute(Attribute::isLabel, false).toBool()) { if (i.attribute(Attribute::isLabel, false).toBool()) {
if (i.name().isEmpty()) continue; if (i.name().isEmpty()) continue;
auto * l = new QLabel(PI2QString(i.name())); auto * l = newLabel(i);
l->setAlignment(Qt::AlignCenter);
grid->add(i, l); grid->add(i, l);
continue; continue;
} }
@@ -203,6 +203,11 @@ void PIValueTreeEdit::actionTriggered(QToolButton * button, const PIString & vn,
comm_labels[nn] = comm_labels[vn]; comm_labels[nn] = comm_labels[vn];
comm_labels.remove(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) { if (a == widget_params->actionRemove) {
source.remove(vn); source.remove(vn);
@@ -210,6 +215,7 @@ void PIValueTreeEdit::actionTriggered(QToolButton * button, const PIString & vn,
value_edits.remove(vn); value_edits.remove(vn);
tree_edits.remove(vn); tree_edits.remove(vn);
comm_labels.remove(vn); comm_labels.remove(vn);
label_labels.remove(vn);
} }
if (a == widget_params->actionChange) { if (a == widget_params->actionChange) {
auto & vt(source[vn]); auto & vt(source[vn]);
@@ -221,12 +227,33 @@ void PIValueTreeEdit::actionTriggered(QToolButton * button, const PIString & vn,
ve->setValue(vt); ve->setValue(vt);
// ve->applyArrayAttributes(); // ve->applyArrayAttributes();
} else { } else {
bool was_label = vt.attribute(Attribute::isLabel, false).toBool();
auto * ve = value_edits.value(vn, nullptr); auto * ve = value_edits.value(vn, nullptr);
if (!ve) return; if (ve) vt.setValue(ve->value());
vt.setValue(ve->value());
if (!widget_params->showFor(vt)) return; if (!widget_params->showFor(vt)) return;
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->setAttributes(vt.attributes());
ve->setValue(vt.value()); 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); auto * cl = comm_labels.value(vn, nullptr);
if (cl) cl->setText(PI2QString(vt.comment())); 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) { void PIValueTreeEdit::newRequest(NewType type) {
PIString nn = Q2PIString(QInputDialog::getText(nullptr, tr("New item"), tr("Input new name:"))); PIString nn = Q2PIString(QInputDialog::getText(nullptr, tr("New item"), tr("Input new name:")));
if (nn.isEmpty()) return; 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) { int PIValueTreeEdit::GridWidgets::beginRow(const PIValueTree & vt, bool is_group) {
if (!create_edit_buttons) return 0; if (!create_edit_buttons) return 0;
auto * b = new QToolButton(); 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<QLabel *>(w);
if (lbl) labels.removeOne(lbl);
delete w;
}
}
}
return col;
}
void PIValueTreeEdit::GridWidgets::removeRow(int index) { void PIValueTreeEdit::GridWidgets::removeRow(int index) {
if (!lay) return; if (!lay) return;
if ((index < 0) || (index >= row_count) || (index >= lay->rowCount())) return; if ((index < 0) || (index >= row_count) || (index >= lay->rowCount())) return;
@@ -478,9 +556,14 @@ void PIValueTreeEdit::GridWidgets::removeRow(int index) {
void PIValueTreeEdit::GridWidgets::simplify(const PIMap<int, int> & map) { void PIValueTreeEdit::GridWidgets::simplify(const PIMap<int, int> & map) {
struct Info {
Qt::Alignment align;
int row_span = 1;
int col_span = 1;
};
if (!lay) return; if (!lay) return;
QVector<QMap<int, QWidget *>> wg; QVector<QMap<int, QWidget *>> wg;
QMap<QWidget *, Qt::Alignment> wa; QMap<QWidget *, Info> wa;
for (int r = 0; r < lay->rowCount(); ++r) { for (int r = 0; r < lay->rowCount(); ++r) {
QMap<int, QWidget *> row; QMap<int, QWidget *> row;
for (int c = 0; c < lay->columnCount(); ++c) { for (int c = 0; c < lay->columnCount(); ++c) {
@@ -488,7 +571,13 @@ void PIValueTreeEdit::GridWidgets::simplify(const PIMap<int, int> & map) {
if (!li) continue; if (!li) continue;
if (li->widget()) { if (li->widget()) {
row[c] = li->widget(); row[c] = li->widget();
wa[li->widget()] = li->alignment(); 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; if (!row.isEmpty()) wg << row;
@@ -503,7 +592,8 @@ void PIValueTreeEdit::GridWidgets::simplify(const PIMap<int, int> & map) {
QMapIterator<int, QWidget *> it(wg[mi]); QMapIterator<int, QWidget *> it(wg[mi]);
while (it.hasNext()) { while (it.hasNext()) {
it.next(); 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; ++rindex;
} }

View File

@@ -76,6 +76,7 @@ private:
PIValueTreeEdit * addTreeEdit(const PIValueTree & vt); PIValueTreeEdit * addTreeEdit(const PIValueTree & vt);
void addValueEdit(const PIValueTree & vt); void addValueEdit(const PIValueTree & vt);
void applyArrayAttributes(); void applyArrayAttributes();
QLabel * newLabel(const PIValueTree & vt);
class GridWidgets: public QWidget { class GridWidgets: public QWidget {
public: public:
@@ -86,6 +87,8 @@ private:
void removeRow(int index); 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, QString label, QWidget * w, const QString & comment, bool is_group = false);
void add(const PIValueTree & vt, QWidget * w, 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); int beginRow(const PIValueTree & vt, bool is_group);
void rename(QString prev_name, QString new_name); void rename(QString prev_name, QString new_name);
void reorder(const PIMap<int, int> & map) { simplify(map); } void reorder(const PIMap<int, int> & map) { simplify(map); }
@@ -96,6 +99,7 @@ private:
QToolButton * button_add; QToolButton * button_add;
private: private:
int removeRowEdits(int row);
void simplify(const PIMap<int, int> & map = PIMap<int, int>()); void simplify(const PIMap<int, int> & map = PIMap<int, int>());
int row_count = 0; int row_count = 0;
@@ -114,7 +118,7 @@ private:
PIVector<PIVariantEdit *> array_edits; PIVector<PIVariantEdit *> array_edits;
PIMap<PIString, PIVariantEdit *> value_edits; PIMap<PIString, PIVariantEdit *> value_edits;
PIMap<PIString, PIValueTreeEdit *> tree_edits; PIMap<PIString, PIValueTreeEdit *> tree_edits;
PIMap<PIString, QLabel *> comm_labels; PIMap<PIString, QLabel *> comm_labels, label_labels;
Ui::PIValueTreeEditArray * ui_array; Ui::PIValueTreeEditArray * ui_array;
GridWidgets * grid = nullptr; GridWidgets * grid = nullptr;
mutable PIValueTree source; mutable PIValueTree source;

View File

@@ -48,6 +48,7 @@ bool PIValueTreeEditParameters::showFor(PIValueTree & vt) {
} }
comboType->blockSignals(true); comboType->blockSignals(true);
comboType->setEnabled(true);
if (vt.isArray()) { if (vt.isArray()) {
uint array_type = PIVariant::typeIDFromName(vt.attribute(Attribute::arrayType).toString()); uint array_type = PIVariant::typeIDFromName(vt.attribute(Attribute::arrayType).toString());
comboType->setCurrentIndex(comboType->findData(array_type)); comboType->setCurrentIndex(comboType->findData(array_type));
@@ -58,13 +59,19 @@ bool PIValueTreeEditParameters::showFor(PIValueTree & vt) {
createAttributes(ve_array, layoutArray, vt.attributes(), true); createAttributes(ve_array, layoutArray, vt.attributes(), true);
groupArray->show(); groupArray->show();
} else { } else {
if (vt.attribute(Attribute::isLabel, false).toBool()) {
comboType->setEnabled(false);
checkAttribute(vt, Attribute::style, "");
}
comboType->setCurrentIndex(comboType->findData(vt.value().typeID())); comboType->setCurrentIndex(comboType->findData(vt.value().typeID()));
groupArray->hide(); groupArray->hide();
} }
comboType->blockSignals(false); comboType->blockSignals(false);
checkHidden->setChecked(vt.attribute(Attribute::hidden, false).toBool()); checkHidden->setChecked(vt.attribute(Attribute::hidden, false).toBool());
checkReadOnly->setChecked(vt.attribute(Attribute::readOnly, false).toBool()); checkReadOnly->setChecked(vt.attribute(Attribute::readOnly, false).toBool());
checkLabel->blockSignals(true);
checkLabel->setChecked(vt.attribute(Attribute::isLabel, false).toBool()); checkLabel->setChecked(vt.attribute(Attribute::isLabel, false).toBool());
checkLabel->blockSignals(false);
lineComment->setText(PI2QString(vt.comment())); lineComment->setText(PI2QString(vt.comment()));
createAttributes(ve_attr, layoutAttributes, vt.attributes()); createAttributes(ve_attr, layoutAttributes, vt.attributes());
@@ -142,3 +149,31 @@ void PIValueTreeEditParameters::checkAttribute(PIValueTree & vt, PIString an, PI
void PIValueTreeEditParameters::on_comboType_currentIndexChanged(int) { void PIValueTreeEditParameters::on_comboType_currentIndexChanged(int) {
createAttributes(ve_attr, layoutAttributes); 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<PIVariantEdit *>(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<PIString>()));
ve_attr.clear();
while (layoutAttributes->rowCount() > 0)
layoutAttributes->removeRow(0);
createAttributes(ve_attr,
layoutAttributes,
{
{Attribute::style, ""}
});
}
}

View File

@@ -51,6 +51,7 @@ private:
private slots: private slots:
void on_comboType_currentIndexChanged(int); void on_comboType_currentIndexChanged(int);
void on_checkLabel_toggled(bool on);
}; };

View File

@@ -78,6 +78,8 @@ private:
class QAD_PIQT_UTILS_EXPORT PIVariantEdit: public QWidget { class QAD_PIQT_UTILS_EXPORT PIVariantEdit: public QWidget {
Q_OBJECT
public: public:
PIVariantEdit(QWidget * parent = nullptr); PIVariantEdit(QWidget * parent = nullptr);
~PIVariantEdit(); ~PIVariantEdit();

View File

@@ -58,7 +58,7 @@ int main(int argc, char * argv[]) {
e.setFullEditMode(true); e.setFullEditMode(true);
e.setGeometry(500, 400, 100, 50); e.setGeometry(500, 400, 100, 50);
// e.setValue(PIValueTreeConversions::fromJSON(PIJSON::fromJSON(PIValueTreeConversions::toJSON(vt).toJSON()))); // e.setValue(PIValueTreeConversions::fromJSON(PIJSON::fromJSON(PIValueTreeConversions::toJSON(vt).toJSON())));
e.setValue(vt); e.setValue(root);
area.setWidget(&e); area.setWidget(&e);
area.show(); area.show();
// piCout << PIValueTreeConversions::toText(e.value()); //.toJSON(PIJSON::Tree); // piCout << PIValueTreeConversions::toText(e.value()); //.toJSON(PIJSON::Tree);