variantfactory.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #include "variantfactory.h"
  2. #include "variantmanager.h"
  3. #include "PropertyDataLinkEdit.h"
  4. #include "../vpControls/VControlObject.h"
  5. VariantFactory::~VariantFactory()
  6. {
  7. QList<QWidget *> editors = theEditorToProperty.keys();
  8. QListIterator<QWidget *> it(editors);
  9. while (it.hasNext())
  10. delete it.next();
  11. }
  12. /// <summary>
  13. /// 关联属性管理器
  14. /// </summary>
  15. /// <param name="manager"></param>
  16. void VariantFactory::connectPropertyManager(QtVariantPropertyManager *manager)
  17. {
  18. // 将数值和属性变化的信号进行绑定
  19. connect(manager, SIGNAL(valueChanged(QtProperty *, const QVariant &)),
  20. this, SLOT(slotPropertyChanged(QtProperty *, const QVariant &)));
  21. connect(manager, SIGNAL(attributeChanged(QtProperty *, const QString &, const QVariant &)),
  22. this, SLOT(slotPropertyAttributeChanged(QtProperty *, const QString &, const QVariant &)));
  23. QtVariantEditorFactory::connectPropertyManager(manager);
  24. }
  25. /// <summary>
  26. /// 通过实现 createEditor 虚函数来为每种不同的类型增加对话框
  27. /// </summary>
  28. /// <param name="manager"></param>
  29. /// <param name="property"></param>
  30. /// <param name="parent"></param>
  31. /// <returns></returns>
  32. #include "objectcontroller.h"
  33. //#include "VButton.h"
  34. QWidget *VariantFactory::createEditor(QtVariantPropertyManager *manager,
  35. QtProperty *property,
  36. QWidget *parent)
  37. {
  38. // 为DataLink类型的属性增加自定义对话框
  39. if (manager->propertyType(property) == VariantManager::tagDataLinkTypeId())
  40. {
  41. // 当前属性控件里的数值
  42. // QString strValue = manager->value(property).toString();
  43. // 设置对话框的初始化数据
  44. //editor->setValue(manager->value(property).toString());
  45. // 2021-9-14增加,此处保存关联控件的类型名称,用于根据不同类型的控件显示不同的对话框界面
  46. QObject* pObject = ((ObjectController*)manager->parent())->object();
  47. VControlObject* pControl = VControlObject::controlPtr(pObject);
  48. //// 对象类名称(之前是需要通过类名称区分控件类型,现在不需要了)
  49. //QString strClassname = pObject->metaObject()->className();
  50. // 变动的属性项的名字,在扩展属性多Link的时候需要用来区分是哪个数据链接有了变动
  51. QString strPropertyName = property->propertyName();
  52. // TODO: 此处需要绑定本属性对应的索引号,这样关联的时候才知道这个Edit中的内容该关联到控件的哪个DataLink中
  53. //editor->setControlClass(strClassname);
  54. //editor->setControlPtr(pControl);
  55. // 针对tagDataLinkTypeId 新建对应的自定义属性Edit
  56. PropertyDataLinkEdit* editor = new PropertyDataLinkEdit(
  57. parent,
  58. pControl,
  59. strPropertyName
  60. );
  61. // 保存Edit和Property的对应关系
  62. theCreatedEditors[property].append(editor);
  63. theEditorToProperty[editor] = property;
  64. // 将数值改变消息和对话框销毁消息绑定
  65. connect(editor, SIGNAL(dataLinkChanged(const QString &)),
  66. this, SLOT(slotSetValue(const QString &)));
  67. connect(editor, SIGNAL(destroyed(QObject *)),
  68. this, SLOT(slotEditorDestroyed(QObject *)));
  69. qDebug() << "VariantFactory::createEditor - New DesignerPropertyDataLinkEdit with ClassName: " << pObject->metaObject()->className();;
  70. return editor;
  71. }
  72. return QtVariantEditorFactory::createEditor(manager, property, parent);
  73. }
  74. void VariantFactory::disconnectPropertyManager(QtVariantPropertyManager *manager)
  75. {
  76. disconnect(manager, SIGNAL(valueChanged(QtProperty *, const QVariant &)),
  77. this, SLOT(slotPropertyChanged(QtProperty *, const QVariant &)));
  78. disconnect(manager, SIGNAL(attributeChanged(QtProperty *, const QString &, const QVariant &)),
  79. this, SLOT(slotPropertyAttributeChanged(QtProperty *, const QString &, const QVariant &)));
  80. QtVariantEditorFactory::disconnectPropertyManager(manager);
  81. }
  82. void VariantFactory::slotPropertyChanged(QtProperty *property, const QVariant &value)
  83. {
  84. if (!theCreatedEditors.contains(property))
  85. return;
  86. QtVariantPropertyManager *manager = propertyManager(property);
  87. QList<QWidget *> editors = theCreatedEditors[property];
  88. QListIterator<QWidget *> itEditor(editors);
  89. while (itEditor.hasNext())
  90. {
  91. if (manager->propertyType(property) == VariantManager::tagDataLinkTypeId())
  92. {
  93. // 在属性值变更时,接收到对应的数据类型变更通知,则调用Edit中对应的方法为控件赋值
  94. PropertyDataLinkEdit*pDataLinkEdit = dynamic_cast<PropertyDataLinkEdit*>(itEditor.next());
  95. // pTagColorListEdit->setValueColorList(value.toString().split('|'));
  96. pDataLinkEdit->setValue(value.toString());
  97. }
  98. }
  99. }
  100. void VariantFactory::slotPropertyAttributeChanged(QtProperty *property,
  101. const QString &attribute, const QVariant &value)
  102. {
  103. Q_UNUSED(attribute);
  104. Q_UNUSED(value);
  105. if (!theCreatedEditors.contains(property))
  106. return;
  107. QtVariantPropertyManager *manager = propertyManager(property);
  108. if (manager->propertyType(property) == VariantManager::tagDataLinkTypeId())
  109. {
  110. }
  111. }
  112. void VariantFactory::slotSetValue(const QString &value)
  113. {
  114. QObject *object = sender();
  115. QMap<QWidget *, QtProperty *>::ConstIterator itEditor = theEditorToProperty.constBegin();
  116. while (itEditor != theEditorToProperty.constEnd())
  117. {
  118. if (itEditor.key() == object)
  119. {
  120. QtProperty *property = itEditor.value();
  121. QtVariantPropertyManager *manager = propertyManager(property);
  122. if (!manager)
  123. return;
  124. // 通过属性设计器给属性赋值
  125. manager->setValue(property, value);
  126. qDebug() << "VariantFactory::slotSetValue - " << value;
  127. return;
  128. }
  129. itEditor++;
  130. }
  131. }
  132. void VariantFactory::slotEditorDestroyed(QObject *object)
  133. {
  134. QMap<QWidget *, QtProperty *>::ConstIterator itEditor = theEditorToProperty.constBegin();
  135. while (itEditor != theEditorToProperty.constEnd()) {
  136. if (itEditor.key() == object) {
  137. QWidget *editor = itEditor.key();
  138. QtProperty *property = itEditor.value();
  139. theEditorToProperty.remove(editor);
  140. theCreatedEditors[property].removeAll(editor);
  141. if (theCreatedEditors[property].isEmpty())
  142. theCreatedEditors.remove(property);
  143. return;
  144. }
  145. itEditor++;
  146. }
  147. }