variantfactory.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. // 2022-5-22 注释,此处获取的VObjectControl经常不正确,换了一种方式来获取了
  47. //QObject* pObject = ((ObjectController*)manager->parent())->object();
  48. //VControlObject* pControl = VControlObject::controlPtr(pObject);
  49. //// 对象类名称(之前是需要通过类名称区分控件类型,现在不需要了)
  50. //QString strClassname = pObject->metaObject()->className();
  51. // 变动的属性项的名字,在扩展属性多Link的时候需要用来区分是哪个数据链接有了变动
  52. QString strPropertyName = property->propertyName();
  53. // TODO: 此处需要绑定本属性对应的索引号,这样关联的时候才知道这个Edit中的内容该关联到控件的哪个DataLink中
  54. // 针对tagDataLinkTypeId 新建对应的自定义属性Edit
  55. PropertyDataLinkEdit* editor = new PropertyDataLinkEdit(
  56. parent,
  57. strPropertyName
  58. );
  59. // 保存Edit和Property的对应关系
  60. theCreatedEditors[property].append(editor);
  61. theEditorToProperty[editor] = property;
  62. // 将数值改变消息和对话框销毁消息绑定
  63. connect(editor, SIGNAL(dataLinkChanged(const QString &)),
  64. this, SLOT(slotSetValue(const QString &)));
  65. connect(editor, SIGNAL(destroyed(QObject *)),
  66. this, SLOT(slotEditorDestroyed(QObject *)));
  67. // qDebug() << "VariantFactory::createEditor - New DesignerPropertyDataLinkEdit with ClassName: " << pObject->metaObject()->className();;
  68. return editor;
  69. }
  70. return QtVariantEditorFactory::createEditor(manager, property, parent);
  71. }
  72. void VariantFactory::disconnectPropertyManager(QtVariantPropertyManager *manager)
  73. {
  74. disconnect(manager, SIGNAL(valueChanged(QtProperty *, const QVariant &)),
  75. this, SLOT(slotPropertyChanged(QtProperty *, const QVariant &)));
  76. disconnect(manager, SIGNAL(attributeChanged(QtProperty *, const QString &, const QVariant &)),
  77. this, SLOT(slotPropertyAttributeChanged(QtProperty *, const QString &, const QVariant &)));
  78. QtVariantEditorFactory::disconnectPropertyManager(manager);
  79. }
  80. void VariantFactory::slotPropertyChanged(QtProperty *property, const QVariant &value)
  81. {
  82. if (!theCreatedEditors.contains(property))
  83. return;
  84. QtVariantPropertyManager *manager = propertyManager(property);
  85. QList<QWidget *> editors = theCreatedEditors[property];
  86. QListIterator<QWidget *> itEditor(editors);
  87. while (itEditor.hasNext())
  88. {
  89. if (manager->propertyType(property) == VariantManager::tagDataLinkTypeId())
  90. {
  91. // 在属性值变更时,接收到对应的数据类型变更通知,则调用Edit中对应的方法为控件赋值
  92. PropertyDataLinkEdit*pDataLinkEdit = dynamic_cast<PropertyDataLinkEdit*>(itEditor.next());
  93. // pTagColorListEdit->setValueColorList(value.toString().split('|'));
  94. pDataLinkEdit->setValue(value.toString());
  95. }
  96. }
  97. }
  98. void VariantFactory::slotPropertyAttributeChanged(QtProperty *property,
  99. const QString &attribute, const QVariant &value)
  100. {
  101. Q_UNUSED(attribute);
  102. Q_UNUSED(value);
  103. if (!theCreatedEditors.contains(property))
  104. return;
  105. QtVariantPropertyManager *manager = propertyManager(property);
  106. if (manager->propertyType(property) == VariantManager::tagDataLinkTypeId())
  107. {
  108. }
  109. }
  110. void VariantFactory::slotSetValue(const QString &value)
  111. {
  112. QObject *object = sender();
  113. QMap<QWidget *, QtProperty *>::ConstIterator itEditor = theEditorToProperty.constBegin();
  114. while (itEditor != theEditorToProperty.constEnd())
  115. {
  116. if (itEditor.key() == object)
  117. {
  118. QtProperty *property = itEditor.value();
  119. QtVariantPropertyManager *manager = propertyManager(property);
  120. if (!manager)
  121. return;
  122. // 通过属性设计器给属性赋值
  123. manager->setValue(property, value);
  124. qDebug() << "VariantFactory::slotSetValue - " << value;
  125. return;
  126. }
  127. itEditor++;
  128. }
  129. }
  130. void VariantFactory::slotEditorDestroyed(QObject *object)
  131. {
  132. QMap<QWidget *, QtProperty *>::ConstIterator itEditor = theEditorToProperty.constBegin();
  133. while (itEditor != theEditorToProperty.constEnd()) {
  134. if (itEditor.key() == object) {
  135. QWidget *editor = itEditor.key();
  136. QtProperty *property = itEditor.value();
  137. theEditorToProperty.remove(editor);
  138. theCreatedEditors[property].removeAll(editor);
  139. if (theCreatedEditors[property].isEmpty())
  140. theCreatedEditors.remove(property);
  141. return;
  142. }
  143. itEditor++;
  144. }
  145. }