PIVariantEdit, PIValueTreeEdit
This commit is contained in:
90
libs/piqt_widgets/pivariant_edit.cpp
Normal file
90
libs/piqt_widgets/pivariant_edit.cpp
Normal file
@@ -0,0 +1,90 @@
|
||||
#include "pivariant_edit.h"
|
||||
#include "piqt.h"
|
||||
#include <QEvent>
|
||||
|
||||
|
||||
PIVariantEditorBase * PIVariantEditorBase::createEditor(uint type_id) {
|
||||
auto f = factories().value(type_id, nullptr);
|
||||
if (!f) return nullptr;
|
||||
auto ret = f();
|
||||
ret->retranslate();
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
void PIVariantEditorBase::createBoxLayout(QBoxLayout::Direction d) {
|
||||
auto * l = new QBoxLayout(d);
|
||||
l->setContentsMargins(0, 0, 0, 0);
|
||||
setLayout(l);
|
||||
}
|
||||
|
||||
|
||||
void PIVariantEditorBase::changeEvent(QEvent * e) {
|
||||
if (e->type() == QEvent::LanguageChange)
|
||||
retranslate();
|
||||
QWidget::changeEvent(e);
|
||||
}
|
||||
|
||||
|
||||
PIMap<uint, PIVariantEditorBase*(*)()> & PIVariantEditorBase::factories() {
|
||||
static PIMap<uint, PIVariantEditorBase*(*)()> ret;
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
PIVariantEdit::PIVariantEdit(QWidget * parent): QWidget(parent) {
|
||||
label = new QLabel();
|
||||
label->setAlignment(Qt::AlignCenter);
|
||||
auto * l = new QBoxLayout(QBoxLayout::LeftToRight);
|
||||
l->setContentsMargins(0, 0, 0, 0);
|
||||
setLayout(l);
|
||||
setValue(PIVariant());
|
||||
}
|
||||
|
||||
|
||||
PIVariantEdit::~PIVariantEdit() {
|
||||
delete label;
|
||||
}
|
||||
|
||||
|
||||
void PIVariantEdit::setValue(const PIVariant & v, uint type_id) {
|
||||
if (type_id == 0) type_id = v.typeID();
|
||||
if (current_type_id != type_id || !editor) {
|
||||
if (editor) delete editor;
|
||||
current_type_id = type_id;
|
||||
editor = PIVariantEditorBase::createEditor(current_type_id);
|
||||
if (editor) {
|
||||
editor->applyAttributes(_attributes);
|
||||
layout()->removeWidget(label);
|
||||
layout()->addWidget(editor);
|
||||
label->hide();
|
||||
} else {
|
||||
label->setText(!v.isValid() ? tr("Invalid type") : tr("No editor for %1").arg(PI2QString(PIVariant::typeNameFromID(type_id))));
|
||||
layout()->addWidget(label);
|
||||
label->show();
|
||||
}
|
||||
}
|
||||
if (!editor) return;
|
||||
editor->setValue(v);
|
||||
}
|
||||
|
||||
|
||||
PIVariant PIVariantEdit::value() const {
|
||||
if (!editor) return PIVariant();
|
||||
return editor->value();
|
||||
}
|
||||
|
||||
|
||||
void PIVariantEdit::setAttributes(const PIVariantMap & a) {
|
||||
_attributes = a;
|
||||
if (!editor) return;
|
||||
editor->applyAttributes(_attributes);
|
||||
}
|
||||
|
||||
|
||||
PIVariantMap PIVariantEdit::defaultAttributes() const {
|
||||
if (!editor) return PIVariantMap();
|
||||
return editor->defaultAttributes();
|
||||
}
|
||||
Reference in New Issue
Block a user