PIValueTreeEdit File and Dir support
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -58,6 +58,7 @@ public:
|
||||
|
||||
void rollback();
|
||||
void clear();
|
||||
void retranslate();
|
||||
|
||||
private:
|
||||
enum class NewType {
|
||||
@@ -95,6 +96,7 @@ private:
|
||||
void reorder(const PIMap<int, int> & map) { simplify(map); }
|
||||
void clear();
|
||||
void changed();
|
||||
void retranslate();
|
||||
|
||||
bool create_edit_buttons = false;
|
||||
QToolButton * button_add;
|
||||
@@ -122,7 +124,7 @@ private:
|
||||
PIMap<PIString, QLabel *> comm_labels, label_labels;
|
||||
Ui::PIValueTreeEditArray * ui_array;
|
||||
GridWidgets * grid = nullptr;
|
||||
mutable PIValueTree source;
|
||||
mutable PIValueTree source, current;
|
||||
bool is_grouping = true, is_full_edit = false;
|
||||
};
|
||||
|
||||
|
||||
@@ -118,3 +118,9 @@ void PIVariantEdit::setFullEditMode(bool on) {
|
||||
if (!editor) return;
|
||||
editor->setFullEditMode(on);
|
||||
}
|
||||
|
||||
|
||||
void PIVariantEdit::retranslate() {
|
||||
if (!editor) return;
|
||||
editor->retranslate();
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@
|
||||
#include "qad_piqt_utils_export.h"
|
||||
|
||||
#include <QBoxLayout>
|
||||
#include <QDebug>
|
||||
#include <QLabel>
|
||||
#include <QWidget>
|
||||
|
||||
@@ -48,6 +49,8 @@ public:
|
||||
virtual PIVariantMap attributes() const { return PIVariantMap(); }
|
||||
static PIVariantMap defaultAttributes() { return PIVariantMap(); }
|
||||
|
||||
virtual void retranslate() {}
|
||||
|
||||
static QString vtTr(const PIString & txt);
|
||||
|
||||
template<typename T>
|
||||
@@ -58,6 +61,7 @@ public:
|
||||
}
|
||||
factories()[type_id] = []() -> PIVariantEditorBase * { return new T(); };
|
||||
default_attributes()[type_id] = T::defaultAttributes();
|
||||
// qDebug() << "register" << T::staticMetaObject.className() << type_id;
|
||||
}
|
||||
|
||||
static PIVariantEditorBase * createEditor(uint type_id);
|
||||
@@ -69,7 +73,6 @@ protected:
|
||||
virtual void setFullEditMode(bool on) {}
|
||||
|
||||
virtual void applyAttributes(const PIVariantMap & a) {}
|
||||
virtual void retranslate() {}
|
||||
|
||||
private:
|
||||
void changeEvent(QEvent * e) override;
|
||||
@@ -93,6 +96,7 @@ public:
|
||||
PIVariantMap attributes() const;
|
||||
|
||||
void setFullEditMode(bool on);
|
||||
void retranslate();
|
||||
|
||||
private:
|
||||
PIVariantEditorBase * editor = nullptr;
|
||||
|
||||
@@ -5,6 +5,8 @@
|
||||
#include "pivarianttypes.h"
|
||||
|
||||
#include <QEvent>
|
||||
#include <QFileDialog>
|
||||
#include <QInputDialog>>
|
||||
#include <QMessageBox>
|
||||
#include <QToolButton>
|
||||
|
||||
@@ -21,6 +23,9 @@ REGISTER_PIVARIANTEDITOR(PIDate, PIVariantEditors::Date);
|
||||
REGISTER_PIVARIANTEDITOR(PIDateTime, PIVariantEditors::DateTime);
|
||||
REGISTER_PIVARIANTEDITOR(PIVariantTypes::Color, PIVariantEditors::Color);
|
||||
REGISTER_PIVARIANTEDITOR(PIVariantTypes::Enum, PIVariantEditors::Enum);
|
||||
REGISTER_PIVARIANTEDITOR(PINetworkAddress, PIVariantEditors::NetworkAddress);
|
||||
REGISTER_PIVARIANTEDITOR(PIVariantTypes::File, PIVariantEditors::File);
|
||||
REGISTER_PIVARIANTEDITOR(PIVariantTypes::Dir, PIVariantEditors::Dir);
|
||||
|
||||
using Attribute = PIValueTree::Attribute;
|
||||
|
||||
@@ -48,14 +53,19 @@ PIVariantMap PIVariantEditors::Int::defaultAttributes() {
|
||||
}
|
||||
|
||||
|
||||
void PIVariantEditors::Int::retranslate() {
|
||||
widget->setPrefix(PIVariantEditorBase::vtTr(prefix));
|
||||
widget->setSuffix(PIVariantEditorBase::vtTr(suffix));
|
||||
}
|
||||
|
||||
|
||||
void PIVariantEditors::Int::applyAttributes(const PIVariantMap & a) {
|
||||
prefix = a.value(Attribute::prefix).toString();
|
||||
suffix = a.value(Attribute::suffix).toString();
|
||||
widget->setPrefix(PIVariantEditorBase::vtTr(prefix));
|
||||
widget->setSuffix(PIVariantEditorBase::vtTr(suffix));
|
||||
widget->setRange(a.value(Attribute::minimum, widget->minimum()).toInt(), a.value(Attribute::maximum, widget->maximum()).toInt());
|
||||
widget->setSingleStep(a.value(Attribute::singleStep, widget->singleStep()).toInt());
|
||||
widget->setReadOnly(a.value(Attribute::readOnly, widget->isReadOnly()).toBool());
|
||||
retranslate();
|
||||
}
|
||||
|
||||
|
||||
@@ -85,15 +95,20 @@ PIVariantMap PIVariantEditors::Double::defaultAttributes() {
|
||||
}
|
||||
|
||||
|
||||
void PIVariantEditors::Double::retranslate() {
|
||||
widget->setPrefix(PIVariantEditorBase::vtTr(prefix));
|
||||
widget->setSuffix(PIVariantEditorBase::vtTr(suffix));
|
||||
}
|
||||
|
||||
|
||||
void PIVariantEditors::Double::applyAttributes(const PIVariantMap & a) {
|
||||
prefix = a.value(Attribute::prefix).toString();
|
||||
suffix = a.value(Attribute::suffix).toString();
|
||||
widget->setPrefix(PIVariantEditorBase::vtTr(prefix));
|
||||
widget->setSuffix(PIVariantEditorBase::vtTr(suffix));
|
||||
widget->setRange(a.value(Attribute::minimum, widget->minimum()).toDouble(), a.value(Attribute::maximum, widget->maximum()).toDouble());
|
||||
widget->setSingleStep(a.value(Attribute::singleStep, widget->singleStep()).toDouble());
|
||||
widget->setDecimals(a.value(Attribute::decimals, widget->decimals()).toInt());
|
||||
widget->setReadOnly(a.value(Attribute::readOnly, widget->isReadOnly()).toBool());
|
||||
retranslate();
|
||||
}
|
||||
|
||||
|
||||
@@ -166,10 +181,7 @@ PIVariantEditors::Enum::Enum() {
|
||||
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);
|
||||
edit_widget->layout()->setContentsMargins(m.left(), 0, 0, 0);
|
||||
auto * b = new QToolButton();
|
||||
b->setIcon(QIcon(":/icons/document-edit.png"));
|
||||
connect(b, &QToolButton::clicked, this, [this]() {
|
||||
@@ -209,3 +221,174 @@ void PIVariantEditors::Enum::applyAttributes(const PIVariantMap & a) {
|
||||
void PIVariantEditors::Enum::setFullEditMode(bool on) {
|
||||
edit_widget->setVisible(on);
|
||||
}
|
||||
|
||||
|
||||
// PIVariantEditors::NetworkAddress
|
||||
|
||||
PIVariantEditors::NetworkAddress::NetworkAddress() {
|
||||
widget = new QLineEdit();
|
||||
widget->setInputMask("000.000.000.000:00000;_");
|
||||
layout()->addWidget(widget);
|
||||
}
|
||||
|
||||
|
||||
void PIVariantEditors::NetworkAddress::setValue(const PIVariant & v) {
|
||||
if (has_port)
|
||||
widget->setText(PI2QString(v.toNetworkAddress().toString()));
|
||||
else
|
||||
widget->setText(PI2QString(v.toNetworkAddress().ipString()));
|
||||
}
|
||||
|
||||
|
||||
PIVariant PIVariantEditors::NetworkAddress::value() const {
|
||||
return PINetworkAddress(Q2PIString(widget->text()));
|
||||
}
|
||||
|
||||
|
||||
PIVariantMap PIVariantEditors::NetworkAddress::attributes() const {
|
||||
return {
|
||||
{"port", has_port}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
PIVariantMap PIVariantEditors::NetworkAddress::defaultAttributes() {
|
||||
return {
|
||||
{"port", true}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
void PIVariantEditors::NetworkAddress::applyAttributes(const PIVariantMap & a) {
|
||||
has_port = a.value("port", true).toBool();
|
||||
if (has_port)
|
||||
widget->setInputMask("000.000.000.000:00000;_");
|
||||
else
|
||||
widget->setInputMask("000.000.000.000;_");
|
||||
widget->setReadOnly(a.value(Attribute::readOnly, widget->isReadOnly()).toBool());
|
||||
}
|
||||
|
||||
|
||||
// PIVariantEditors::FileBase
|
||||
|
||||
PIVariantEditors::FileBase::FileBase() {
|
||||
filter = "All files(*)";
|
||||
widget = new CLineEdit();
|
||||
layout()->addWidget(widget);
|
||||
sel_widget = new QWidget();
|
||||
sel_widget->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
|
||||
sel_widget->setLayout(new QBoxLayout(QBoxLayout::LeftToRight));
|
||||
QMargins m = sel_widget->layout()->contentsMargins();
|
||||
sel_widget->layout()->setContentsMargins(m.left(), 0, 0, 0);
|
||||
auto * b = new QToolButton();
|
||||
b->setIcon(QIcon(":/icons/document-open.png"));
|
||||
b->setToolTip(tr("Choose") + " ...");
|
||||
connect(b, &QToolButton::clicked, this, [this]() {
|
||||
QString ret;
|
||||
if (is_dir)
|
||||
ret = QFileDialog::getExistingDirectory(this, tr("Select directory"), widget->text());
|
||||
else {
|
||||
if (is_save)
|
||||
ret = QFileDialog::getSaveFileName(this, tr("Select file"), widget->text(), PIVariantEditorBase::vtTr(filter));
|
||||
else
|
||||
ret = QFileDialog::getOpenFileName(this, tr("Select file"), widget->text(), PIVariantEditorBase::vtTr(filter));
|
||||
}
|
||||
if (ret.isEmpty()) return;
|
||||
if (!is_abs) ret = QDir::current().relativeFilePath(ret);
|
||||
widget->setText(ret);
|
||||
});
|
||||
sel_widget->layout()->addWidget(b);
|
||||
layout()->addWidget(sel_widget);
|
||||
edit_widget = new QWidget();
|
||||
edit_widget->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
|
||||
edit_widget->setLayout(new QBoxLayout(QBoxLayout::LeftToRight));
|
||||
m = edit_widget->layout()->contentsMargins();
|
||||
edit_widget->layout()->setContentsMargins(m.left(), 0, 0, 0);
|
||||
b = new QToolButton();
|
||||
b->setIcon(QIcon(":/icons/document-edit.png"));
|
||||
b->setPopupMode(QToolButton::InstantPopup);
|
||||
b->setMenu(&edit_menu);
|
||||
edit_widget->layout()->addWidget(b);
|
||||
layout()->setSpacing(0);
|
||||
layout()->addWidget(edit_widget);
|
||||
setFullEditMode(false);
|
||||
}
|
||||
|
||||
|
||||
PIVariantMap PIVariantEditors::FileBase::attributes() const {
|
||||
return {
|
||||
{Attribute::filter, filter },
|
||||
{Attribute::absolutePath, is_abs },
|
||||
{Attribute::onlyExisting, !is_save}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
PIVariantMap PIVariantEditors::FileBase::defaultAttributes() {
|
||||
return {
|
||||
{Attribute::filter, "" },
|
||||
{Attribute::absolutePath, false},
|
||||
{Attribute::onlyExisting, true }
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
void PIVariantEditors::FileBase::applyAttributes(const PIVariantMap & a) {
|
||||
filter = a.value(Attribute::filter).toString();
|
||||
is_abs = a.value(Attribute::absolutePath).toBool();
|
||||
is_save = !a.value(Attribute::onlyExisting, true).toBool();
|
||||
bool ro = a.value(Attribute::readOnly).toBool();
|
||||
widget->setReadOnly(ro);
|
||||
sel_widget->setHidden(ro);
|
||||
if (act_abs) act_abs->setChecked(is_abs);
|
||||
if (act_save) act_save->setChecked(!is_save);
|
||||
}
|
||||
|
||||
|
||||
void PIVariantEditors::FileBase::setFullEditMode(bool on) {
|
||||
edit_widget->setVisible(on);
|
||||
}
|
||||
|
||||
void PIVariantEditors::FileBase::createMenu() {
|
||||
act_abs = edit_menu.addAction(tr("Absolute path"), this, [this](bool on) { is_abs = on; });
|
||||
act_abs->setCheckable(true);
|
||||
act_abs->setChecked(is_abs);
|
||||
if (is_dir) return;
|
||||
act_save = edit_menu.addAction(tr("Existing only"), this, [this](bool on) { is_save = !on; });
|
||||
act_save->setCheckable(true);
|
||||
act_save->setChecked(!is_save);
|
||||
edit_menu.addAction(tr("Set filter ..."), this, [this]() {
|
||||
bool ok = false;
|
||||
QString nf = QInputDialog::getText(nullptr, tr("Select filter"), tr("Input filter:"), QLineEdit::Normal, PI2QString(filter), &ok);
|
||||
if (!ok) return;
|
||||
filter = Q2PIString(nf);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
// PIVariantEditors::File
|
||||
|
||||
void PIVariantEditors::File::setValue(const PIVariant & v) {
|
||||
widget->setText(PI2QString(v.toFile().file));
|
||||
}
|
||||
|
||||
|
||||
PIVariant PIVariantEditors::File::value() const {
|
||||
PIVariantTypes::File v;
|
||||
v.file = Q2PIString(widget->text());
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
// PIVariantEditors::Dir
|
||||
|
||||
void PIVariantEditors::Dir::setValue(const PIVariant & v) {
|
||||
widget->setText(PI2QString(v.toDir().dir));
|
||||
}
|
||||
|
||||
|
||||
PIVariant PIVariantEditors::Dir::value() const {
|
||||
PIVariantTypes::Dir v;
|
||||
v.dir = Q2PIString(widget->text());
|
||||
return v;
|
||||
}
|
||||
|
||||
@@ -26,6 +26,7 @@
|
||||
#include <QComboBox>
|
||||
#include <QDateEdit>
|
||||
#include <QDoubleSpinBox>
|
||||
#include <QMenu>
|
||||
#include <QSpinBox>
|
||||
#include <QTimeEdit>
|
||||
#include <clineedit.h>
|
||||
@@ -64,6 +65,7 @@ public:
|
||||
PIVariant value() const override { return widget->value(); }
|
||||
PIVariantMap attributes() const override;
|
||||
static PIVariantMap defaultAttributes();
|
||||
void retranslate();
|
||||
|
||||
private:
|
||||
void applyAttributes(const PIVariantMap & a) override;
|
||||
@@ -85,6 +87,7 @@ public:
|
||||
PIVariant value() const override { return widget->value(); }
|
||||
PIVariantMap attributes() const override;
|
||||
static PIVariantMap defaultAttributes();
|
||||
void retranslate();
|
||||
|
||||
private:
|
||||
void applyAttributes(const PIVariantMap & a) override;
|
||||
@@ -206,6 +209,71 @@ private:
|
||||
};
|
||||
|
||||
|
||||
class QAD_PIQT_UTILS_EXPORT NetworkAddress: public PIVariantEditorBase {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
NetworkAddress();
|
||||
void setValue(const PIVariant & v) override;
|
||||
PIVariant value() const override;
|
||||
PIVariantMap attributes() const override;
|
||||
static PIVariantMap defaultAttributes();
|
||||
|
||||
private:
|
||||
void applyAttributes(const PIVariantMap & a) override;
|
||||
QLineEdit * widget;
|
||||
bool has_port = true;
|
||||
};
|
||||
|
||||
|
||||
class QAD_PIQT_UTILS_EXPORT FileBase: public PIVariantEditorBase {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
FileBase();
|
||||
PIVariantMap attributes() const override;
|
||||
static PIVariantMap defaultAttributes();
|
||||
|
||||
protected:
|
||||
void applyAttributes(const PIVariantMap & a) override;
|
||||
void setFullEditMode(bool on) override;
|
||||
void createMenu();
|
||||
CLineEdit * widget;
|
||||
QWidget * sel_widget;
|
||||
QWidget * edit_widget;
|
||||
QMenu edit_menu;
|
||||
QAction *act_abs = nullptr, *act_save = nullptr;
|
||||
bool is_dir = false, is_abs = false, is_save = false;
|
||||
PIString filter;
|
||||
};
|
||||
|
||||
|
||||
class QAD_PIQT_UTILS_EXPORT File: public FileBase {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
File(): FileBase() {
|
||||
is_dir = false;
|
||||
createMenu();
|
||||
}
|
||||
void setValue(const PIVariant & v) override;
|
||||
PIVariant value() const override;
|
||||
};
|
||||
|
||||
|
||||
class QAD_PIQT_UTILS_EXPORT Dir: public FileBase {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
Dir(): FileBase() {
|
||||
is_dir = true;
|
||||
createMenu();
|
||||
}
|
||||
void setValue(const PIVariant & v) override;
|
||||
PIVariant value() const override;
|
||||
};
|
||||
|
||||
|
||||
}; // namespace PIVariantEditors
|
||||
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
<file>../../icons/code-struct.png</file>
|
||||
<file>../../icons/code-union.png</file>
|
||||
<file>../../icons/legend.png</file>
|
||||
<file>../../icons/document-open.png</file>
|
||||
<file>../../icons/document-edit.png</file>
|
||||
</qresource>
|
||||
</RCC>
|
||||
|
||||
Reference in New Issue
Block a user