#include "variantfactory.h" #include "variantmanager.h" #include "PropertyDataLinkEdit.h" #include "../vpControls/VControlObject.h" VariantFactory::~VariantFactory() { QList editors = theEditorToProperty.keys(); QListIterator it(editors); while (it.hasNext()) delete it.next(); } /// /// 关联属性管理器 /// /// void VariantFactory::connectPropertyManager(QtVariantPropertyManager *manager) { // 将数值和属性变化的信号进行绑定 connect(manager, SIGNAL(valueChanged(QtProperty *, const QVariant &)), this, SLOT(slotPropertyChanged(QtProperty *, const QVariant &))); connect(manager, SIGNAL(attributeChanged(QtProperty *, const QString &, const QVariant &)), this, SLOT(slotPropertyAttributeChanged(QtProperty *, const QString &, const QVariant &))); QtVariantEditorFactory::connectPropertyManager(manager); } /// /// 通过实现 createEditor 虚函数来为每种不同的类型增加对话框 /// /// /// /// /// #include "objectcontroller.h" //#include "VButton.h" QWidget *VariantFactory::createEditor(QtVariantPropertyManager *manager, QtProperty *property, QWidget *parent) { // 为DataLink类型的属性增加自定义对话框 if (manager->propertyType(property) == VariantManager::tagDataLinkTypeId()) { // 当前属性控件里的数值 // QString strValue = manager->value(property).toString(); // 设置对话框的初始化数据 //editor->setValue(manager->value(property).toString()); // 2021-9-14增加,此处保存关联控件的类型名称,用于根据不同类型的控件显示不同的对话框界面 QObject* pObject = ((ObjectController*)manager->parent())->object(); VControlObject* pControl = VControlObject::controlPtr(pObject); //// 对象类名称(之前是需要通过类名称区分控件类型,现在不需要了) //QString strClassname = pObject->metaObject()->className(); // 变动的属性项的名字,在扩展属性多Link的时候需要用来区分是哪个数据链接有了变动 QString strPropertyName = property->propertyName(); // TODO: 此处需要绑定本属性对应的索引号,这样关联的时候才知道这个Edit中的内容该关联到控件的哪个DataLink中 //editor->setControlClass(strClassname); //editor->setControlPtr(pControl); // 针对tagDataLinkTypeId 新建对应的自定义属性Edit PropertyDataLinkEdit* editor = new PropertyDataLinkEdit( parent, pControl, strPropertyName ); // 保存Edit和Property的对应关系 theCreatedEditors[property].append(editor); theEditorToProperty[editor] = property; // 将数值改变消息和对话框销毁消息绑定 connect(editor, SIGNAL(dataLinkChanged(const QString &)), this, SLOT(slotSetValue(const QString &))); connect(editor, SIGNAL(destroyed(QObject *)), this, SLOT(slotEditorDestroyed(QObject *))); qDebug() << "VariantFactory::createEditor - New DesignerPropertyDataLinkEdit with ClassName: " << pObject->metaObject()->className();; return editor; } return QtVariantEditorFactory::createEditor(manager, property, parent); } void VariantFactory::disconnectPropertyManager(QtVariantPropertyManager *manager) { disconnect(manager, SIGNAL(valueChanged(QtProperty *, const QVariant &)), this, SLOT(slotPropertyChanged(QtProperty *, const QVariant &))); disconnect(manager, SIGNAL(attributeChanged(QtProperty *, const QString &, const QVariant &)), this, SLOT(slotPropertyAttributeChanged(QtProperty *, const QString &, const QVariant &))); QtVariantEditorFactory::disconnectPropertyManager(manager); } void VariantFactory::slotPropertyChanged(QtProperty *property, const QVariant &value) { if (!theCreatedEditors.contains(property)) return; QtVariantPropertyManager *manager = propertyManager(property); QList editors = theCreatedEditors[property]; QListIterator itEditor(editors); while (itEditor.hasNext()) { if (manager->propertyType(property) == VariantManager::tagDataLinkTypeId()) { // 在属性值变更时,接收到对应的数据类型变更通知,则调用Edit中对应的方法为控件赋值 PropertyDataLinkEdit*pDataLinkEdit = dynamic_cast(itEditor.next()); // pTagColorListEdit->setValueColorList(value.toString().split('|')); pDataLinkEdit->setValue(value.toString()); } } } void VariantFactory::slotPropertyAttributeChanged(QtProperty *property, const QString &attribute, const QVariant &value) { Q_UNUSED(attribute); Q_UNUSED(value); if (!theCreatedEditors.contains(property)) return; QtVariantPropertyManager *manager = propertyManager(property); if (manager->propertyType(property) == VariantManager::tagDataLinkTypeId()) { } } void VariantFactory::slotSetValue(const QString &value) { QObject *object = sender(); QMap::ConstIterator itEditor = theEditorToProperty.constBegin(); while (itEditor != theEditorToProperty.constEnd()) { if (itEditor.key() == object) { QtProperty *property = itEditor.value(); QtVariantPropertyManager *manager = propertyManager(property); if (!manager) return; // 通过属性设计器给属性赋值 manager->setValue(property, value); qDebug() << "VariantFactory::slotSetValue - " << value; return; } itEditor++; } } void VariantFactory::slotEditorDestroyed(QObject *object) { QMap::ConstIterator itEditor = theEditorToProperty.constBegin(); while (itEditor != theEditorToProperty.constEnd()) { if (itEditor.key() == object) { QWidget *editor = itEditor.key(); QtProperty *property = itEditor.value(); theEditorToProperty.remove(editor); theCreatedEditors[property].removeAll(editor); if (theCreatedEditors[property].isEmpty()) theCreatedEditors.remove(property); return; } itEditor++; } }