Files
qad/libs/piqt_widgets/pivariant_edit.cpp

112 lines
2.5 KiB
C++

#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;
}
bool PIVariantEditorBase::editorExists(uint type_id) {
return factories().value(type_id, nullptr);
}
PIVariantMap PIVariantEditorBase::editorDefaultAttributes(uint type_id) {
return default_attributes().value(type_id);
}
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;
}
PIMap<uint, PIVariantMap> & PIVariantEditorBase::default_attributes() {
static PIMap<uint, PIVariantMap> 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::attributes() const {
if (!editor) return PIVariantMap();
return editor->attributes();
}
void PIVariantEdit::setFullEditMode(bool on) {
if (!editor) return;
editor->setFullEditMode(on);
}