PIValueTreeEdit File and Dir support

This commit is contained in:
2022-12-19 14:29:42 +03:00
parent dfba7f1510
commit dc2419dcad
8 changed files with 363 additions and 62 deletions

View File

@@ -37,14 +37,14 @@ PIValueTreeEdit::~PIValueTreeEdit() {
void PIValueTreeEdit::setValue(const PIValueTree & v) {
source = v;
current = source = v;
build();
}
PIValueTree PIValueTreeEdit::value() const {
applyValues();
return source;
return current;
}
@@ -61,19 +61,36 @@ void PIValueTreeEdit::setFullEditMode(bool yes) {
void PIValueTreeEdit::rollback() {
current = source;
build();
}
void PIValueTreeEdit::clear() {
source = PIValueTree();
current = PIValueTree();
removeAll();
}
void PIValueTreeEdit::retranslate() {
for (const auto & i: value_edits)
i.second->retranslate();
for (const auto & i: tree_edits)
i.second->retranslate();
for (const auto & i: comm_labels) {
i.second->setText(PIVariantEditorBase::vtTr(current.child(i.first).comment()));
}
for (const auto & i: label_labels) {
i.second->setText(PIVariantEditorBase::vtTr(i.first));
}
grid->retranslate();
}
void PIValueTreeEdit::changeEvent(QEvent * e) {
if (e->type() == QEvent::LanguageChange) {
if (widget_array) ui_array->retranslateUi(widget_array);
retranslate();
}
QWidget::changeEvent(e);
}
@@ -104,18 +121,18 @@ void PIValueTreeEdit::build() {
grid->create_edit_buttons = false;
removeAll();
// piCout << source.attributes().value(Attribute::arrayType) << array_type;
if (source.isArray()) {
if (current.isArray()) {
widget_array = new QWidget();
ui_array->setupUi(widget_array);
applyArrayAttributes();
ui_array->layoutArray->addWidget(grid);
grid->button_add->hide();
grid->show();
uint array_type = PIVariant::typeIDFromName(source.attribute(Attribute::arrayType).toString());
uint array_type = PIVariant::typeIDFromName(current.attribute(Attribute::arrayType).toString());
int index = 0;
for (const auto & i: source.children()) {
for (const auto & i: current.children()) {
auto * ve = new PIVariantEdit();
ve->setAttributes(source.attributes());
ve->setAttributes(current.attributes());
ve->setValue(i.value(), array_type);
grid->add(PIValueTree(), PIString::fromNumber(++index), ve, i.comment());
array_edits << ve;
@@ -127,7 +144,7 @@ void PIValueTreeEdit::build() {
array_edits.resize(grid->rowCount());
for (int i = grid->rowCount(); i < value; ++i) {
auto * ve = new PIVariantEdit();
ve->setAttributes(source.attributes());
ve->setAttributes(current.attributes());
ve->setValue(PIVariant::fromType(array_type), array_type);
grid->add(PIValueTree(), PIString::fromNumber(i + 1), ve, "");
array_edits << ve;
@@ -137,9 +154,9 @@ void PIValueTreeEdit::build() {
} else {
grid->create_edit_buttons = is_full_edit;
layout()->addWidget(grid);
if (!source.hasChildren()) grid->clear();
for (const auto & i: source.children()) {
if (i.attribute(Attribute::hidden, false).toBool()) continue;
if (!current.hasChildren()) grid->clear();
for (const auto & i: current.children()) {
if (i.attribute(Attribute::hidden, false).toBool() && !is_full_edit) continue;
if (i.attribute(Attribute::isLabel, false).toBool()) {
if (i.name().isEmpty()) continue;
auto * l = newLabel(i);
@@ -157,21 +174,21 @@ void PIValueTreeEdit::build() {
void PIValueTreeEdit::applyValues() const {
if (source.isArray()) {
if (array_edits.isNotEmpty()) source.mergeAttributes(array_edits[0]->attributes());
source.clearChildren();
if (current.isArray()) {
if (array_edits.isNotEmpty()) current.mergeAttributes(array_edits[0]->attributes());
current.clearChildren();
for (int i = 0; i < array_edits.size_s(); ++i)
source.addChild({PIString::fromNumber(i), array_edits[i]->value()});
current.addChild({PIString::fromNumber(i), array_edits[i]->value()});
} else {
auto vit = value_edits.makeIterator();
while (vit.next()) {
auto & c(source.child(vit.key()));
auto & c(current.child(vit.key()));
c.mergeAttributes(vit.value()->attributes());
c.setValue(vit.value()->value());
}
auto tit = tree_edits.makeIterator();
while (tit.next()) {
auto & c(source.child(tit.key()));
auto & c(current.child(tit.key()));
if (!c.isNull()) c = tit.value()->value();
}
}
@@ -182,13 +199,13 @@ void PIValueTreeEdit::actionTriggered(QToolButton * button, const PIString & vn,
if (a == widget_params->actionRename) {
PIString nn = Q2PIString(QInputDialog::getText(nullptr, tr("Rename"), tr("Input new name:"), QLineEdit::Normal, PI2QString(vn)));
if (nn.isEmpty() || (nn == vn)) return;
for (const auto & c: source.children()) {
for (const auto & c: current.children()) {
if (c.name() == nn) {
QMessageBox::critical(nullptr, tr("Rename"), tr("This name already exists!"));
return;
}
}
source[vn].setName(nn);
current[vn].setName(nn);
button->setProperty(property_name, PI2QString(nn));
grid->rename(vn, nn);
if (value_edits.contains(vn)) {
@@ -210,7 +227,7 @@ void PIValueTreeEdit::actionTriggered(QToolButton * button, const PIString & vn,
}
}
if (a == widget_params->actionRemove) {
source.remove(vn);
current.remove(vn);
grid->removeRow(grid->getRow(button));
value_edits.remove(vn);
tree_edits.remove(vn);
@@ -218,7 +235,7 @@ void PIValueTreeEdit::actionTriggered(QToolButton * button, const PIString & vn,
label_labels.remove(vn);
}
if (a == widget_params->actionChange) {
auto & vt(source[vn]);
auto & vt(current[vn]);
if (vt.isArray()) {
auto * ve = tree_edits.value(vn, nullptr);
if (!ve) return;
@@ -229,7 +246,10 @@ void PIValueTreeEdit::actionTriggered(QToolButton * button, const PIString & vn,
} else {
bool was_label = vt.attribute(Attribute::isLabel, false).toBool();
auto * ve = value_edits.value(vn, nullptr);
if (ve) vt.setValue(ve->value());
if (ve) {
vt.setValue(ve->value());
vt.mergeAttributes(ve->attributes());
}
if (!widget_params->showFor(vt)) return;
bool now_label = vt.attribute(Attribute::isLabel, false).toBool();
if (was_label ^ now_label) {
@@ -257,14 +277,14 @@ void PIValueTreeEdit::actionTriggered(QToolButton * button, const PIString & vn,
if (cl) cl->setText(PIVariantEditorBase::vtTr(vt.comment()));
}
if (a == widget_params->actionReorder) {
if (!widget_reorder->showFor(source)) return;
if (!widget_reorder->showFor(current)) return;
grid->reorder(widget_reorder->map);
auto cl = source.children();
source.clearChildren();
auto cl = current.children();
current.clearChildren();
for (int i = 0; i < cl.size_s(); ++i) {
int mi = widget_reorder->map.value(i, i);
if (mi < 0 || mi >= cl.size_s()) continue;
source.addChild(cl[mi]);
current.addChild(cl[mi]);
}
}
}
@@ -307,15 +327,15 @@ void PIValueTreeEdit::addValueEdit(const PIValueTree & vt) {
void PIValueTreeEdit::applyArrayAttributes() {
ui_array->spinCount->setRange(source.attribute(Attribute::arrayMinCount, 0).toInt(),
source.attribute(Attribute::arrayMaxCount, 65536).toInt());
ui_array->spinCount->setValue(source.children().size_s());
ui_array->widgetEdit->setVisible(source.attribute(Attribute::arrayResize, false).toBool());
uint array_type = PIVariant::typeIDFromName(source.attribute(Attribute::arrayType).toString());
ui_array->spinCount->setRange(current.attribute(Attribute::arrayMinCount, 0).toInt(),
current.attribute(Attribute::arrayMaxCount, 65536).toInt());
ui_array->spinCount->setValue(current.children().size_s());
ui_array->widgetEdit->setVisible(current.attribute(Attribute::arrayResize, false).toBool());
uint array_type = PIVariant::typeIDFromName(current.attribute(Attribute::arrayType).toString());
for (int i = 0; i < array_edits.size_s(); ++i) {
auto * w = array_edits[i];
w->setAttributes(source.attributes());
w->setValue(i < source.children().size_s() ? source.children()[i].value() : PIVariant(), array_type);
w->setAttributes(current.attributes());
w->setValue(i < current.children().size_s() ? current.children()[i].value() : PIVariant(), array_type);
}
}
@@ -340,7 +360,7 @@ void PIValueTreeEdit::applyVariantEdit(PIVariantEdit * ve, const PIValueTree & v
void PIValueTreeEdit::newRequest(NewType type) {
PIString nn = Q2PIString(QInputDialog::getText(nullptr, tr("New item"), tr("Input new name:")));
if (nn.isEmpty()) return;
for (const auto & c: source.children()) {
for (const auto & c: current.children()) {
if (c.name() == nn) {
QMessageBox::critical(nullptr, tr("New item"), tr("This name already exists!"));
return;
@@ -355,7 +375,7 @@ void PIValueTreeEdit::newRequest(NewType type) {
vt.setAttribute(Attribute::arrayType, PIVariant::typeName<PIString>());
if (!widget_params->showFor(vt)) return;
}
source.addChild(vt);
current.addChild(vt);
switch (type) {
case NewType::Value: addValueEdit(vt); break;
case NewType::Group: addTreeEdit(vt); break;
@@ -501,16 +521,15 @@ void PIValueTreeEdit::GridWidgets::rename(const PIString & prev_name, const PISt
break;
}
}
} else {
for (auto * l: labels)
if (l->property(property_name).toString() == PI2QString(prev_name)) {
l->setProperty(property_name, PI2QString(new_name));
QString nn = PIVariantEditorBase::vtTr(new_name);
if (!nn.isEmpty()) nn += ':';
l->setText(nn);
break;
}
}
for (auto * l: labels)
if (l->property(property_name).toString() == PI2QString(prev_name)) {
l->setProperty(property_name, PI2QString(new_name));
QString nn = PIVariantEditorBase::vtTr(new_name);
if (!nn.isEmpty()) nn += ':';
l->setText(nn);
break;
}
}
@@ -534,6 +553,22 @@ void PIValueTreeEdit::GridWidgets::changed() {
}
void PIValueTreeEdit::GridWidgets::retranslate() {
if (parent->is_grouping) {
for (auto * w: widgets) {
auto * gb = qobject_cast<QGroupBox *>(w);
if (!gb) continue;
gb->setTitle(PIVariantEditorBase::vtTr(Q2PIString(gb->property(property_name).toString())));
}
}
for (auto * l: labels) {
QString nn = PIVariantEditorBase::vtTr(Q2PIString(l->property(property_name).toString()));
if (!nn.isEmpty()) nn += ':';
l->setText(nn);
}
}
int PIValueTreeEdit::GridWidgets::removeRowEdits(int row) {
int col = create_edit_buttons ? 1 : 0;
for (int c = col; c < lay->columnCount(); ++c) {
@@ -576,8 +611,8 @@ void PIValueTreeEdit::GridWidgets::removeRow(int index) {
void PIValueTreeEdit::GridWidgets::simplify(const PIMap<int, int> & map) {
struct Info {
Qt::Alignment align;
int row_span = 1;
int col_span = 1;
int row_span = 0;
int col_span = 0;
};
if (!lay) return;
QVector<QMap<int, QWidget *>> wg;
@@ -596,6 +631,7 @@ void PIValueTreeEdit::GridWidgets::simplify(const PIMap<int, int> & map) {
info.row_span = pos[2];
info.col_span = pos[3];
wa[li->widget()] = info;
c += (pos[3] - 1);
}
}
if (!row.isEmpty()) wg << row;