PIValueTreeEdit File and Dir support
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user