PIVariantEdit ready but widgets fullEditMode

This commit is contained in:
2022-12-16 16:26:16 +03:00
parent 4bb4e87cc2
commit ed9b4a950c
10 changed files with 413 additions and 51 deletions

View File

@@ -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 <QEvent>
#include <QFormLayout>
#include <QGroupBox>
#include <QInputDialog>
#include <QMessageBox>
#include <QToolButton>
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<int, int> & map) {
if (!lay) return;
QVector<QMap<int, QWidget *>> wg;
QMap<QWidget *, Qt::Alignment> 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<int, QWidget *> it(row);
for (int i = 0; i < wg.size(); ++i) {
int mi = map.value(i, i);
if (mi < 0 || mi >= wg.size()) continue;
QMapIterator<int, QWidget *> 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();
}

View File

@@ -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<int, int> & map) { simplify(map); }
void clear();
void changed();
@@ -94,7 +96,7 @@ private:
QToolButton * button_add;
private:
void simplify();
void simplify(const PIMap<int, int> & map = PIMap<int, int>());
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<PIVariantEdit *> array_edits;
PIMap<PIString, PIVariantEdit *> value_edits;
PIMap<PIString, PIValueTreeEdit *> tree_edits;
PIMap<PIString, QLabel *> comm_labels;
Ui::PIValueTreeEditArray * ui_array;
GridWidgets * grid = nullptr;
mutable PIValueTree source;

View File

@@ -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;
}

View File

@@ -21,10 +21,10 @@
#define pivaluetree_edit_parameters_H
#include "pivaluetree.h"
#include "qformlayout.h"
#include "ui_pivaluetree_edit_parameters.h"
#include <QDialog>
#include <QFormLayout>
#include <QLabel>
class PIVariantEdit;

View File

@@ -47,6 +47,16 @@
<item row="3" column="1">
<widget class="EComboBox" name="comboType"/>
</item>
<item row="4" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>Comment:</string>
</property>
</widget>
</item>
<item row="4" column="1">
<widget class="CLineEdit" name="lineComment"/>
</item>
</layout>
</item>
<item>
@@ -148,8 +158,22 @@
<string>Array</string>
</property>
</action>
<action name="actionReorder">
<property name="icon">
<iconset resource="../graphic/qad_graphic.qrc">
<normaloff>:/icons/legend.png</normaloff>:/icons/legend.png</iconset>
</property>
<property name="text">
<string>Reorder ...</string>
</property>
</action>
</widget>
<customwidgets>
<customwidget>
<class>CLineEdit</class>
<extends>QLineEdit</extends>
<header>clineedit.h</header>
</customwidget>
<customwidget>
<class>EComboBox</class>
<extends>QComboBox</extends>

View File

@@ -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;
}

View File

@@ -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 <http://www.gnu.org/licenses/>.
*/
#ifndef pivaluetree_edit_reorder_H
#define pivaluetree_edit_reorder_H
#include "pivaluetree.h"
#include "ui_pivaluetree_edit_reorder.h"
#include <QDialog>
class PIValueTreeEditReorder
: public QDialog
, public Ui::PIValueTreeEditReorder {
Q_OBJECT
public:
PIValueTreeEditReorder(QWidget * parent = nullptr);
~PIValueTreeEditReorder();
bool showFor(PIValueTree & vt);
PIMap<int, int> map;
private:
private slots:
};
#endif

View File

@@ -0,0 +1,152 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>PIValueTreeEditReorder</class>
<widget class="QDialog" name="PIValueTreeEditReorder">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>457</width>
<height>347</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>Change order:</string>
</property>
</widget>
</item>
<item>
<widget class="QListWidget" name="listWidget">
<property name="editTriggers">
<set>QAbstractItemView::NoEditTriggers</set>
</property>
<property name="dragDropMode">
<enum>QAbstractItemView::InternalMove</enum>
</property>
<property name="defaultDropAction">
<enum>Qt::MoveAction</enum>
</property>
<property name="selectionMode">
<enum>QAbstractItemView::ExtendedSelection</enum>
</property>
<property name="verticalScrollMode">
<enum>QAbstractItemView::ScrollPerPixel</enum>
</property>
</widget>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
</layout>
<action name="actionRemove">
<property name="icon">
<iconset resource="../blockview/qad_blockview.qrc">
<normaloff>:/icons/edit-delete.png</normaloff>:/icons/edit-delete.png</iconset>
</property>
<property name="text">
<string>Remove</string>
</property>
</action>
<action name="actionChange">
<property name="icon">
<iconset resource="../application/qad_application.qrc">
<normaloff>:/icons/configure.png</normaloff>:/icons/configure.png</iconset>
</property>
<property name="text">
<string>Change ...</string>
</property>
</action>
<action name="actionRename">
<property name="icon">
<iconset resource="../graphic/qad_graphic.qrc">
<normaloff>:/icons/border-line.png</normaloff>:/icons/border-line.png</iconset>
</property>
<property name="text">
<string>Rename ...</string>
</property>
</action>
<action name="actionValue">
<property name="icon">
<iconset resource="qad_piqt_widgets.qrc">
<normaloff>:/icons/code-variable.png</normaloff>:/icons/code-variable.png</iconset>
</property>
<property name="text">
<string>Value</string>
</property>
</action>
<action name="actionGroup">
<property name="icon">
<iconset resource="qad_piqt_widgets.qrc">
<normaloff>:/icons/code-struct.png</normaloff>:/icons/code-struct.png</iconset>
</property>
<property name="text">
<string>Group</string>
</property>
</action>
<action name="actionArray">
<property name="icon">
<iconset resource="qad_piqt_widgets.qrc">
<normaloff>:/icons/code-union.png</normaloff>:/icons/code-union.png</iconset>
</property>
<property name="text">
<string>Array</string>
</property>
</action>
<action name="actionReorder">
<property name="icon">
<iconset resource="../graphic/qad_graphic.qrc">
<normaloff>:/icons/legend.png</normaloff>:/icons/legend.png</iconset>
</property>
<property name="text">
<string>Reorder ...</string>
</property>
</action>
</widget>
<resources>
<include location="../application/qad_application.qrc"/>
<include location="../blockview/qad_blockview.qrc"/>
<include location="../graphic/qad_graphic.qrc"/>
<include location="qad_piqt_widgets.qrc"/>
</resources>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>PIValueTreeEditReorder</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>260</x>
<y>333</y>
</hint>
<hint type="destinationlabel">
<x>196</x>
<y>221</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>PIValueTreeEditReorder</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>330</x>
<y>333</y>
</hint>
<hint type="destinationlabel">
<x>291</x>
<y>222</y>
</hint>
</hints>
</connection>
</connections>
</ui>

View File

@@ -6,5 +6,6 @@
<file>../../icons/code-variable.png</file>
<file>../../icons/code-struct.png</file>
<file>../../icons/code-union.png</file>
<file>../../icons/legend.png</file>
</qresource>
</RCC>

View File

@@ -1,34 +1,100 @@
#include <QApplication>
#include "piqt_connection_edit.h"
#include <QFileDialog>
#include <qpiconfig.h>
#include <qad_types.h>
#include <evalspinbox.h>
#include <piintrospection_server.h>
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 <QApplication>
#include <QFileDialog>
#include <QScrollArea>
#include <QTimer>
#include <evalspinbox.h>
#include <piintrospection_server.h>
#include <qad_types.h>
#include <qpiconfig.h>
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;*/
}