From ed9b4a950cf897c55fc626c9dc338b2a260293a9 Mon Sep 17 00:00:00 2001 From: peri4 Date: Fri, 16 Dec 2022 16:26:16 +0300 Subject: [PATCH] PIVariantEdit ready but widgets fullEditMode --- libs/piqt_widgets/pivaluetree_edit.cpp | 66 ++++++-- libs/piqt_widgets/pivaluetree_edit.h | 6 +- .../pivaluetree_edit_parameters.cpp | 2 + .../pivaluetree_edit_parameters.h | 2 +- .../pivaluetree_edit_parameters.ui | 24 +++ .../piqt_widgets/pivaluetree_edit_reorder.cpp | 30 ++++ libs/piqt_widgets/pivaluetree_edit_reorder.h | 47 ++++++ libs/piqt_widgets/pivaluetree_edit_reorder.ui | 152 ++++++++++++++++++ libs/piqt_widgets/qad_piqt_widgets.qrc | 1 + utils/piconnedit/piconnedit_main.cpp | 134 +++++++++++---- 10 files changed, 413 insertions(+), 51 deletions(-) create mode 100644 libs/piqt_widgets/pivaluetree_edit_reorder.cpp create mode 100644 libs/piqt_widgets/pivaluetree_edit_reorder.h create mode 100644 libs/piqt_widgets/pivaluetree_edit_reorder.ui diff --git a/libs/piqt_widgets/pivaluetree_edit.cpp b/libs/piqt_widgets/pivaluetree_edit.cpp index 9ffe2e1..d1fae40 100644 --- a/libs/piqt_widgets/pivaluetree_edit.cpp +++ b/libs/piqt_widgets/pivaluetree_edit.cpp @@ -2,15 +2,16 @@ #include "piqt.h" #include "pivaluetree_edit_parameters.h" +#include "pivaluetree_edit_reorder.h" #include "pivariant_edit.h" -#include "qinputdialog.h" -#include "qmessagebox.h" -#include "qtoolbutton.h" #include "ui_pivaluetree_edit_array.h" #include #include #include +#include +#include +#include using Attribute = PIValueTree::Attribute; @@ -18,10 +19,11 @@ const char property_name[] = "__name__"; PIValueTreeEdit::PIValueTreeEdit(QWidget * parent): QWidget(parent) { - widget_params = new PIValueTreeEditParameters(); - ui_array = new Ui::PIValueTreeEditArray(); - grid = new GridWidgets(this); - auto * lay = new QBoxLayout(QBoxLayout::TopToBottom); + widget_params = new PIValueTreeEditParameters(); + widget_reorder = new PIValueTreeEditReorder(); + ui_array = new Ui::PIValueTreeEditArray(); + grid = new GridWidgets(this); + auto * lay = new QBoxLayout(QBoxLayout::TopToBottom); lay->setContentsMargins(0, 0, 0, 0); setLayout(lay); } @@ -81,6 +83,7 @@ void PIValueTreeEdit::removeAll() { array_edits.clear(); value_edits.clear(); tree_edits.clear(); + comm_labels.clear(); if (widget_array) { ui_array->layoutArray->removeWidget(grid); grid->hide(); @@ -89,7 +92,7 @@ void PIValueTreeEdit::removeAll() { widget_array = nullptr; } QLayoutItem * child = nullptr; - while ((child = layout()->takeAt(0)) != nullptr) { + while ((child = layout()->takeAt(0))) { delete child; } grid->clear(); @@ -196,12 +199,17 @@ void PIValueTreeEdit::actionTriggered(QToolButton * button, const PIString & vn, tree_edits[nn] = tree_edits[vn]; tree_edits.remove(vn); } + if (comm_labels.contains(vn)) { + comm_labels[nn] = comm_labels[vn]; + comm_labels.remove(vn); + } } if (a == widget_params->actionRemove) { source.remove(vn); grid->removeRow(grid->getRow(button)); value_edits.remove(vn); tree_edits.remove(vn); + comm_labels.remove(vn); } if (a == widget_params->actionChange) { auto & vt(source[vn]); @@ -220,6 +228,19 @@ void PIValueTreeEdit::actionTriggered(QToolButton * button, const PIString & vn, ve->setAttributes(vt.attributes()); ve->setValue(vt.value()); } + auto * cl = comm_labels.value(vn, nullptr); + if (cl) cl->setText(PI2QString(vt.comment())); + } + if (a == widget_params->actionReorder) { + if (!widget_reorder->showFor(source)) return; + grid->reorder(widget_reorder->map); + auto cl = source.children(); + source.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]); + } } } @@ -304,10 +325,20 @@ void PIValueTreeEdit::newRequest(NewType type) { // PIValueTreeEdit::GridWidgets PIValueTreeEdit::GridWidgets::GridWidgets(PIValueTreeEdit * p) { - parent = p; - icon_conf = QIcon(":/icons/configure.png"); - menu_group.addActions({p->widget_params->actionRename, p->widget_params->actionRemove}); - menu_conf.addActions({p->widget_params->actionRename, p->widget_params->actionChange, p->widget_params->actionRemove}); + parent = p; + icon_conf = QIcon(":/icons/configure.png"); + auto newSeparator = []() { + auto * a = new QAction(); + a->setSeparator(true); + return a; + }; + menu_group.addActions( + {p->widget_params->actionRename, p->widget_params->actionReorder, newSeparator(), p->widget_params->actionRemove}); + menu_conf.addActions({p->widget_params->actionRename, + p->widget_params->actionChange, + p->widget_params->actionReorder, + newSeparator(), + p->widget_params->actionRemove}); menu_new.addActions({p->widget_params->actionValue, p->widget_params->actionGroup, p->widget_params->actionArray}); button_add = new QToolButton(); button_add->setIcon(QIcon(":/icons/list-add.png")); @@ -351,6 +382,7 @@ void PIValueTreeEdit::GridWidgets::add(const PIValueTree & vt, QString label, QW lay->addWidget(c, row_count, col++, Qt::AlignVCenter | Qt::AlignLeft); widgets << l << w << c; labels << l; + parent->comm_labels[vt.name()] = c; ++row_count; changed(); } @@ -420,6 +452,7 @@ void PIValueTreeEdit::GridWidgets::changed() { if (!create_edit_buttons || !lay) return; lay->addWidget(button_add, row_count, 0); button_add->show(); + adjustSize(); } @@ -444,7 +477,7 @@ void PIValueTreeEdit::GridWidgets::removeRow(int index) { } -void PIValueTreeEdit::GridWidgets::simplify() { +void PIValueTreeEdit::GridWidgets::simplify(const PIMap & map) { if (!lay) return; QVector> wg; QMap wa; @@ -464,8 +497,10 @@ void PIValueTreeEdit::GridWidgets::simplify() { lay = new QGridLayout(); lay->setContentsMargins(0, 0, 0, 0); int rindex = 0; - for (const auto & row: wg) { - QMapIterator it(row); + for (int i = 0; i < wg.size(); ++i) { + int mi = map.value(i, i); + if (mi < 0 || mi >= wg.size()) continue; + QMapIterator it(wg[mi]); while (it.hasNext()) { it.next(); lay->addWidget(it.value(), rindex, it.key(), wa.value(it.value())); @@ -473,4 +508,5 @@ void PIValueTreeEdit::GridWidgets::simplify() { ++rindex; } setLayout(lay); + adjustSize(); } diff --git a/libs/piqt_widgets/pivaluetree_edit.h b/libs/piqt_widgets/pivaluetree_edit.h index 5b81bdd..d25b697 100644 --- a/libs/piqt_widgets/pivaluetree_edit.h +++ b/libs/piqt_widgets/pivaluetree_edit.h @@ -32,6 +32,7 @@ class QGridLayout; class QToolButton; class PIVariantEdit; class PIValueTreeEditParameters; +class PIValueTreeEditReorder; namespace Ui { class PIValueTreeEditArray; } @@ -87,6 +88,7 @@ private: void add(const PIValueTree & vt, QWidget * w, bool is_group = false); int beginRow(const PIValueTree & vt, bool is_group); void rename(QString prev_name, QString new_name); + void reorder(const PIMap & map) { simplify(map); } void clear(); void changed(); @@ -94,7 +96,7 @@ private: QToolButton * button_add; private: - void simplify(); + void simplify(const PIMap & map = PIMap()); int row_count = 0; QGridLayout * lay = nullptr; @@ -107,10 +109,12 @@ private: QWidget * widget_array = nullptr; PIValueTreeEditParameters * widget_params; + PIValueTreeEditReorder * widget_reorder; PIStringList root_path; PIVector array_edits; PIMap value_edits; PIMap tree_edits; + PIMap comm_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 ab354d9..dc06304 100644 --- a/libs/piqt_widgets/pivaluetree_edit_parameters.cpp +++ b/libs/piqt_widgets/pivaluetree_edit_parameters.cpp @@ -65,6 +65,7 @@ bool PIValueTreeEditParameters::showFor(PIValueTree & vt) { checkHidden->setChecked(vt.attribute(Attribute::hidden, false).toBool()); checkReadOnly->setChecked(vt.attribute(Attribute::readOnly, false).toBool()); checkLabel->setChecked(vt.attribute(Attribute::isLabel, false).toBool()); + lineComment->setText(PI2QString(vt.comment())); createAttributes(ve_attr, layoutAttributes, vt.attributes()); if (exec() != QDialog::Accepted) return false; @@ -76,6 +77,7 @@ bool PIValueTreeEditParameters::showFor(PIValueTree & vt) { vt.setValue(var); } applyAttributes(vt); + vt.setComment(Q2PIString(lineComment->text())); return true; } diff --git a/libs/piqt_widgets/pivaluetree_edit_parameters.h b/libs/piqt_widgets/pivaluetree_edit_parameters.h index b119b9b..bc7a0ca 100644 --- a/libs/piqt_widgets/pivaluetree_edit_parameters.h +++ b/libs/piqt_widgets/pivaluetree_edit_parameters.h @@ -21,10 +21,10 @@ #define pivaluetree_edit_parameters_H #include "pivaluetree.h" -#include "qformlayout.h" #include "ui_pivaluetree_edit_parameters.h" #include +#include #include class PIVariantEdit; diff --git a/libs/piqt_widgets/pivaluetree_edit_parameters.ui b/libs/piqt_widgets/pivaluetree_edit_parameters.ui index 3d3d10f..232eb0c 100644 --- a/libs/piqt_widgets/pivaluetree_edit_parameters.ui +++ b/libs/piqt_widgets/pivaluetree_edit_parameters.ui @@ -47,6 +47,16 @@ + + + + Comment: + + + + + + @@ -148,8 +158,22 @@ Array + + + + :/icons/legend.png:/icons/legend.png + + + Reorder ... + + + + CLineEdit + QLineEdit +
clineedit.h
+
EComboBox QComboBox diff --git a/libs/piqt_widgets/pivaluetree_edit_reorder.cpp b/libs/piqt_widgets/pivaluetree_edit_reorder.cpp new file mode 100644 index 0000000..80c20a8 --- /dev/null +++ b/libs/piqt_widgets/pivaluetree_edit_reorder.cpp @@ -0,0 +1,30 @@ +#include "pivaluetree_edit_reorder.h" + +#include "piqt.h" + + +PIValueTreeEditReorder::PIValueTreeEditReorder(QWidget * parent): QDialog(parent) { + setupUi(this); +} + + +PIValueTreeEditReorder::~PIValueTreeEditReorder() {} + + +bool PIValueTreeEditReorder::showFor(PIValueTree & vt) { + setWindowTitle(tr("Reorder of \"%1\"").arg(PI2QString(vt.name()))); + + PIStringList old_list; + for (const auto & c: vt.children()) + old_list << c.name(); + listWidget->clear(); + listWidget->addItems(PI2QStringList(old_list)); + + if (exec() != QDialog::Accepted) return false; + + map.clear(); + for (int i = 0; i < listWidget->count(); ++i) + map[i] = old_list.indexOf(Q2PIString(listWidget->item(i)->text())); + + return true; +} diff --git a/libs/piqt_widgets/pivaluetree_edit_reorder.h b/libs/piqt_widgets/pivaluetree_edit_reorder.h new file mode 100644 index 0000000..75dd817 --- /dev/null +++ b/libs/piqt_widgets/pivaluetree_edit_reorder.h @@ -0,0 +1,47 @@ +/* + PIQt Utils - Qt utilites for PIP + + Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . +*/ + +#ifndef pivaluetree_edit_reorder_H +#define pivaluetree_edit_reorder_H + +#include "pivaluetree.h" +#include "ui_pivaluetree_edit_reorder.h" + +#include + + +class PIValueTreeEditReorder + : public QDialog + , public Ui::PIValueTreeEditReorder { + Q_OBJECT + +public: + PIValueTreeEditReorder(QWidget * parent = nullptr); + ~PIValueTreeEditReorder(); + + bool showFor(PIValueTree & vt); + + PIMap map; + +private: +private slots: +}; + + +#endif diff --git a/libs/piqt_widgets/pivaluetree_edit_reorder.ui b/libs/piqt_widgets/pivaluetree_edit_reorder.ui new file mode 100644 index 0000000..2366088 --- /dev/null +++ b/libs/piqt_widgets/pivaluetree_edit_reorder.ui @@ -0,0 +1,152 @@ + + + PIValueTreeEditReorder + + + + 0 + 0 + 457 + 347 + + + + + + + Change order: + + + + + + + QAbstractItemView::NoEditTriggers + + + QAbstractItemView::InternalMove + + + Qt::MoveAction + + + QAbstractItemView::ExtendedSelection + + + QAbstractItemView::ScrollPerPixel + + + + + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + + + :/icons/edit-delete.png:/icons/edit-delete.png + + + Remove + + + + + + :/icons/configure.png:/icons/configure.png + + + Change ... + + + + + + :/icons/border-line.png:/icons/border-line.png + + + Rename ... + + + + + + :/icons/code-variable.png:/icons/code-variable.png + + + Value + + + + + + :/icons/code-struct.png:/icons/code-struct.png + + + Group + + + + + + :/icons/code-union.png:/icons/code-union.png + + + Array + + + + + + :/icons/legend.png:/icons/legend.png + + + Reorder ... + + + + + + + + + + + + buttonBox + accepted() + PIValueTreeEditReorder + accept() + + + 260 + 333 + + + 196 + 221 + + + + + buttonBox + rejected() + PIValueTreeEditReorder + reject() + + + 330 + 333 + + + 291 + 222 + + + + + diff --git a/libs/piqt_widgets/qad_piqt_widgets.qrc b/libs/piqt_widgets/qad_piqt_widgets.qrc index 53f119a..ebda946 100644 --- a/libs/piqt_widgets/qad_piqt_widgets.qrc +++ b/libs/piqt_widgets/qad_piqt_widgets.qrc @@ -6,5 +6,6 @@ ../../icons/code-variable.png ../../icons/code-struct.png ../../icons/code-union.png + ../../icons/legend.png diff --git a/utils/piconnedit/piconnedit_main.cpp b/utils/piconnedit/piconnedit_main.cpp index 5bb5957..7736d5c 100644 --- a/utils/piconnedit/piconnedit_main.cpp +++ b/utils/piconnedit/piconnedit_main.cpp @@ -1,34 +1,100 @@ -#include -#include "piqt_connection_edit.h" -#include -#include -#include -#include -#include - - -int main(int argc, char * argv[]) { - PIINTROSPECTION_START(piconnedit) - QApplication a(argc, argv); - enableHighDPI(); - ConnectionEdit w; - if (a.arguments().size() > 1) { - QPIConfig cfg(a.arguments()[1]); - QByteArray model = cfg.getValue("connectionmodel").toByteArray(); - if (!model.isEmpty()) w.setModel(model); - } else - w.recreateConnection(); - if (w.exec() == QDialog::Accepted) { - QString c = QFileDialog::getSaveFileName(&w, "Save config to file", a.applicationDirPath(), "*.conf"); - if (!c.isEmpty()) { - QFile f(c); - if (f.open(QIODevice::WriteOnly)) { - QTextStream ts(&f); - ts << w.configuration(); - ts << "connectionmodel = " << QByteArray2QString(w.model()) << "\n"; - f.close(); - } - } - } - return 0; -} +#include "pifile.h" +#include "pijson.h" +#include "piqt_connection_edit.h" +#include "pivaluetree_conversions.h" +#include "pivaluetree_edit.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +int main(int argc, char * argv[]) { + QApplication a(argc, argv); + + PIValueTree root, group, array; + root.addChild({"bool", false}); + root.addChild({ + "integer", + 256, + {{PIValueTree::Attribute::minimum, -100}, {PIValueTree::Attribute::maximum, 200}, {PIValueTree::Attribute::singleStep, 5}} + }); + root.addChild({"Label here", PIVariant(), {{PIValueTree::Attribute::isLabel, true}}}); + root.addChild({"string", "str"}); + group.addChild({"substr", "str2"}); + group.addChild({"real", 12.3}); + group.setName("group"); + array.setAttribute(PIValueTree::Attribute::arrayType, "string"); + array.setAttribute(PIValueTree::Attribute::arrayResize, true); + array.setAttribute(PIValueTree::Attribute::arrayMinCount, 0); + array.setAttribute(PIValueTree::Attribute::arrayMaxCount, 10); + array.setName("array"); + array.addChild({"0", "1E+2"}); + array.addChild({"1", -5}); + array.addChild({"3", 0.1}); + root.addChild(group); + root.addChild(array); + PIString json = PIValueTreeConversions::toJSON(root).toJSON(PIJSON::Tree); + // piCout << PIValueTreeConversions::fromJSON(PIJSON::fromJSON(json)); + + PIFile f("C:/work/config.conf", PIIODevice::ReadOnly); + auto vt = PIValueTreeConversions::fromText(&f); + PIVariantTypes::Enum en; + en << "first" + << "second"; + en << PIVariantTypes::Enumerator(10, "TEN"); + en.selectValue(1); + vt.addChild({"Enum", en}); + // piCout << PIValueTreeConversions::toText(vt); + + QScrollArea area; + area.setWidgetResizable(true); + PIValueTreeEdit e; + e.setGroupingEnabled(false); + e.setFullEditMode(true); + e.setGeometry(500, 400, 100, 50); + // e.setValue(PIValueTreeConversions::fromJSON(PIJSON::fromJSON(PIValueTreeConversions::toJSON(vt).toJSON()))); + e.setValue(vt); + area.setWidget(&e); + area.show(); + // piCout << PIValueTreeConversions::toText(e.value()); //.toJSON(PIJSON::Tree); + + QTimer timer; + QObject::connect(&timer, &QTimer::timeout, [&]() { e.rollback(); }); + // timer.start(5); + // QTimer::singleShot(1000, [&e](){e.setValue(50);}); + // QTimer::singleShot(2000, [&e, vt]() { + // e.setValue(PIValueTreeConversions::fromText(PIValueTreeConversions::toText(vt) + "\n[]\nAHAHA = 123\n")); + //}); + // QTimer::singleShot(3000, [&e]() { e.setFullEditMode(false); }); + return a.exec(); + + /* + PIINTROSPECTION_START(piconnedit) + QApplication a(argc, argv); + enableHighDPI(); + ConnectionEdit w; + if (a.arguments().size() > 1) { + QPIConfig cfg(a.arguments()[1]); + QByteArray model = cfg.getValue("connectionmodel").toByteArray(); + if (!model.isEmpty()) w.setModel(model); + } else + w.recreateConnection(); + if (w.exec() == QDialog::Accepted) { + QString c = QFileDialog::getSaveFileName(&w, "Save config to file", a.applicationDirPath(), "*.conf"); + if (!c.isEmpty()) { + QFile f(c); + if (f.open(QIODevice::WriteOnly)) { + QTextStream ts(&f); + ts << w.configuration(); + ts << "connectionmodel = " << QByteArray2QString(w.model()) << "\n"; + f.close(); + } + } + } + return 0;*/ +}