PIVariantEdit::Enum full edit mode implement

This commit is contained in:
2022-12-17 23:52:14 +03:00
parent 5327e4f7b0
commit e0b4dc06db
9 changed files with 398 additions and 12 deletions

View File

@@ -240,16 +240,14 @@ void PIValueTreeEdit::actionTriggered(QToolButton * button, const PIString & vn,
comm_labels.remove(vt.name());
} else {
auto * ve = new PIVariantEdit();
ve->setAttributes(vt.attributes());
ve->setValue(vt.value());
applyVariantEdit(ve, vt);
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());
applyVariantEdit(ve, vt);
}
if (now_label) {
label_labels[vt.name()]->setStyleSheet(PI2QString(vt.attribute(Attribute::style).toString()));
@@ -301,8 +299,7 @@ PIValueTreeEdit * PIValueTreeEdit::addTreeEdit(const PIValueTree & vt) {
void PIValueTreeEdit::addValueEdit(const PIValueTree & vt) {
auto * ve = new PIVariantEdit();
ve->setAttributes(vt.attributes());
ve->setValue(vt.value());
applyVariantEdit(ve, vt);
grid->add(vt, PI2QString(vt.name()), ve, PI2QString(vt.comment()));
value_edits[vt.name()] = ve;
}
@@ -332,6 +329,13 @@ QLabel * PIValueTreeEdit::newLabel(const PIValueTree & vt) {
}
void PIValueTreeEdit::applyVariantEdit(PIVariantEdit * ve, const PIValueTree & vt) {
ve->setFullEditMode(is_full_edit);
ve->setAttributes(vt.attributes());
ve->setValue(vt.value());
}
void PIValueTreeEdit::newRequest(NewType type) {
PIString nn = Q2PIString(QInputDialog::getText(nullptr, tr("New item"), tr("Input new name:")));
if (nn.isEmpty()) return;

View File

@@ -77,6 +77,7 @@ private:
void addValueEdit(const PIValueTree & vt);
void applyArrayAttributes();
QLabel * newLabel(const PIValueTree & vt);
void applyVariantEdit(PIVariantEdit * ve, const PIValueTree & vt);
class GridWidgets: public QWidget {
public:

View File

@@ -0,0 +1,74 @@
#include "pivariant_edit_enum.h"
#include "piqt.h"
PIValueTreeEditEnum::PIValueTreeEditEnum(QWidget * parent): QDialog(parent) {
setupUi(this);
}
PIValueTreeEditEnum::~PIValueTreeEditEnum() {}
bool PIValueTreeEditEnum::showFor(const PIVariantTypes::Enum & v) {
ret = PIVariantTypes::Enum();
treeWidget->clear();
for (auto e: v.enum_list)
addItem(PI2QString(e.name), e.value);
if (exec() != QDialog::Accepted) return false;
for (int i = 0; i < treeWidget->topLevelItemCount(); ++i) {
auto * ti = treeWidget->topLevelItem(i);
ret << PIVariantTypes::Enumerator(ti->text(1).toInt(), Q2PIString(ti->text(0)));
}
return true;
}
QTreeWidgetItem * PIValueTreeEditEnum::addItem(QString n, int v) {
auto * ti = new QTreeWidgetItem({n, QString::number(v)});
treeWidget->addTopLevelItem(ti);
auto f = ti->flags();
f.setFlag(Qt::ItemIsEditable);
ti->setFlags(f);
return ti;
}
void PIValueTreeEditEnum::on_buttonAdd_clicked() {
bool is_edit = false;
for (int i = 0; i < treeWidget->topLevelItemCount(); ++i) {
auto * ti = treeWidget->topLevelItem(i);
for (int c = 0; c < treeWidget->columnCount(); ++c) {
if (treeWidget->isPersistentEditorOpen(ti, c)) {
is_edit = true;
break;
}
}
if (is_edit) break;
}
if (is_edit) {
buttonAdd->setFocus();
treeWidget->setFocus();
}
int max = -1;
for (int i = 0; i < treeWidget->topLevelItemCount(); ++i)
max = piMaxi(max, treeWidget->topLevelItem(i)->text(1).toInt());
auto * ti = addItem("name", max + 1);
treeWidget->editItem(ti, 0);
}
void PIValueTreeEditEnum::on_buttonRemove_clicked() {
qDeleteAll(treeWidget->selectedItems());
}
void PIValueTreeEditEnum::on_buttonClear_clicked() {
treeWidget->clear();
}

View File

@@ -0,0 +1,52 @@
/*
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 pivariant_edit_enum_H
#define pivariant_edit_enum_H
#include "ui_pivariant_edit_enum.h"
#include <QDialog>
#include <pivariant.h>
class PIValueTreeEditEnum
: public QDialog
, public Ui::PIValueTreeEditEnum {
Q_OBJECT
public:
PIValueTreeEditEnum(QWidget * parent = nullptr);
~PIValueTreeEditEnum();
bool showFor(const PIVariantTypes::Enum & v);
PIVariantTypes::Enum ret;
private:
QTreeWidgetItem * addItem(QString n, int v);
private slots:
void on_buttonAdd_clicked();
void on_buttonRemove_clicked();
void on_buttonClear_clicked();
};
#endif

View File

@@ -0,0 +1,223 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>PIValueTreeEditEnum</class>
<widget class="QDialog" name="PIValueTreeEditEnum">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>457</width>
<height>347</height>
</rect>
</property>
<property name="windowTitle">
<string>Edit Enum</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QWidget" name="widget" native="true">
<layout class="QHBoxLayout" name="horizontalLayout">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QToolButton" name="buttonAdd">
<property name="icon">
<iconset resource="qad_piqt_widgets.qrc">
<normaloff>:/icons/list-add.png</normaloff>:/icons/list-add.png</iconset>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="buttonRemove">
<property name="icon">
<iconset resource="../blockview/qad_blockview.qrc">
<normaloff>:/icons/edit-delete.png</normaloff>:/icons/edit-delete.png</iconset>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Preferred</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>5</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QToolButton" name="buttonClear">
<property name="icon">
<iconset resource="../application/qad_application.qrc">
<normaloff>:/icons/edit-clear.png</normaloff>:/icons/edit-clear.png</iconset>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</spacer>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QTreeWidget" name="treeWidget">
<property name="selectionMode">
<enum>QAbstractItemView::ExtendedSelection</enum>
</property>
<property name="verticalScrollMode">
<enum>QAbstractItemView::ScrollPerPixel</enum>
</property>
<property name="rootIsDecorated">
<bool>false</bool>
</property>
<property name="itemsExpandable">
<bool>false</bool>
</property>
<property name="expandsOnDoubleClick">
<bool>false</bool>
</property>
<column>
<property name="text">
<string>Name</string>
</property>
</column>
<column>
<property name="text">
<string>Value</string>
</property>
</column>
</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>PIValueTreeEditEnum</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>PIValueTreeEditEnum</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

@@ -1,10 +1,12 @@
#include "pivariant_edit_widgets.h"
#include "pivaluetree.h"
#include "pivariant_edit_enum.h"
#include "pivarianttypes.h"
#include <QEvent>
#include <QMessageBox>
#include <QToolButton>
REGISTER_PIVARIANTEDITOR(bool, PIVariantEditors::Bool);
REGISTER_PIVARIANTEDITOR(short, PIVariantEditors::Int);
@@ -153,6 +155,31 @@ void PIVariantEditors::DateTime::applyAttributes(const PIVariantMap & a) {
// PIVariantEditors::Enum
PIVariantEditors::Enum::Enum() {
widget = new QComboBox();
layout()->addWidget(widget);
edit_widget = new QWidget();
edit_widget->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
edit_widget->setLayout(new QBoxLayout(QBoxLayout::LeftToRight));
QMargins m = edit_widget->layout()->contentsMargins();
m.setTop(0);
m.setBottom(0);
m.setRight(0);
edit_widget->layout()->setContentsMargins(m);
auto * b = new QToolButton();
b->setIcon(QIcon(":/icons/document-edit.png"));
connect(b, &QToolButton::clicked, this, [this]() {
PIValueTreeEditEnum dlg;
if (!dlg.showFor(src)) return;
setValue(PIVariant(dlg.ret));
});
edit_widget->layout()->addWidget(b);
layout()->setSpacing(0);
layout()->addWidget(edit_widget);
setFullEditMode(false);
}
void PIVariantEditors::Enum::setValue(const PIVariant & v) {
src = v.toEnum();
int sv = src.selectedValue();
@@ -173,3 +200,8 @@ PIVariant PIVariantEditors::Enum::value() const {
void PIVariantEditors::Enum::applyAttributes(const PIVariantMap & a) {
widget->setEnabled(!a.value(Attribute::readOnly, !widget->isEnabled()).toBool());
}
void PIVariantEditors::Enum::setFullEditMode(bool on) {
edit_widget->setVisible(on);
}

View File

@@ -190,18 +190,17 @@ class QAD_PIQT_UTILS_EXPORT Enum: public PIVariantEditorBase {
Q_OBJECT
public:
Enum() {
widget = new QComboBox();
layout()->addWidget(widget);
}
Enum();
void setValue(const PIVariant & v) override;
PIVariant value() const override;
PIVariantMap attributes() const override { return {}; }
private:
void applyAttributes(const PIVariantMap & a) override;
void setFullEditMode(bool on) override;
mutable PIVariantTypes::Enum src;
QComboBox * widget;
QWidget * edit_widget;
};

View File

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

View File

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