variantmanager.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. 
  2. #include "variantmanager.h"
  3. // 获取DataLink的TypeID
  4. int VariantManager::tagDataLinkTypeId()
  5. {
  6. return qMetaTypeId<DataLink>();
  7. }
  8. // 获取TableExInfo的TypeID
  9. int VariantManager::tagTableExInfoTypeId()
  10. {
  11. return qMetaTypeId<CONTROL_PROPERTY_EX>();
  12. }
  13. VariantManager::VariantManager(QObject *parent)
  14. : QtVariantPropertyManager(parent),
  15. propertyEditor_(Q_NULLPTR)
  16. {
  17. }
  18. VariantManager::~VariantManager()
  19. {
  20. }
  21. bool VariantManager::isPropertyTypeSupported(int propertyType) const
  22. {
  23. if (
  24. propertyType == tagDataLinkTypeId() ||
  25. propertyType == tagTableExInfoTypeId()
  26. // propertyType == functionTypeId() ||
  27. // ||
  28. //propertyType == TagTextListTypeId() ||
  29. //propertyType == filePathTypeId() ||
  30. //propertyType == scriptTypeId()
  31. )
  32. return true;
  33. return QtVariantPropertyManager::isPropertyTypeSupported(propertyType);
  34. }
  35. int VariantManager::valueType(int propertyType) const
  36. {
  37. if (propertyType == tagDataLinkTypeId())
  38. return QVariant::String;
  39. return QtVariantPropertyManager::valueType(propertyType);
  40. }
  41. QVariant VariantManager::value(const QtProperty *property) const
  42. {
  43. if (theValues.contains(property))
  44. return theValues[property].value;
  45. return QtVariantPropertyManager::value(property);
  46. }
  47. QStringList VariantManager::attributes(int propertyType) const
  48. {
  49. //if (propertyType == functionTypeId())
  50. //{
  51. // QStringList attr;
  52. // attr << QLatin1String("supportevents");
  53. // return attr;
  54. //} else
  55. if (propertyType == tagDataLinkTypeId())
  56. {
  57. return QtVariantPropertyManager::attributes(propertyType);
  58. }
  59. else if (propertyType == tagTableExInfoTypeId())
  60. {
  61. return QtVariantPropertyManager::attributes(propertyType);
  62. //QStringList attr;
  63. // attr << QLatin1String("TableExInfoTest");
  64. // return attr;
  65. }
  66. // else if (propertyType == TagTextListTypeId()) {
  67. // return QtVariantPropertyManager::attributes(propertyType);
  68. //} else if (propertyType == filePathTypeId()) {
  69. // QStringList attr;
  70. // attr << QLatin1String("filter");
  71. // return attr;
  72. //} else if (propertyType == scriptTypeId()) {
  73. // QStringList attr;
  74. // attr << QLatin1String("supportevents");
  75. // return attr;
  76. //}
  77. return QtVariantPropertyManager::attributes(propertyType);
  78. }
  79. int VariantManager::attributeType(int propertyType, const QString &attribute) const
  80. {
  81. if (propertyType == tagDataLinkTypeId())
  82. {
  83. return QtVariantPropertyManager::attributeType(propertyType, attribute);
  84. }
  85. else if (propertyType == tagTableExInfoTypeId())
  86. {
  87. return QtVariantPropertyManager::attributeType(propertyType, attribute);
  88. }
  89. //if (propertyType == functionTypeId()) {
  90. // if (attribute == QLatin1String("supportevents"))
  91. // return QVariant::String;
  92. // return 0;
  93. //} else if (propertyType == tagColorListTypeId()) {
  94. // return QtVariantPropertyManager::attributeType(propertyType, attribute);
  95. //} else if (propertyType == TagTextListTypeId()) {
  96. // return QtVariantPropertyManager::attributeType(propertyType, attribute);
  97. //} else if (propertyType == filePathTypeId()) {
  98. // if (attribute == QLatin1String("filter"))
  99. // return QVariant::String;
  100. // return 0;
  101. //} else if (propertyType == scriptTypeId()) {
  102. // if (attribute == QLatin1String("supportevents"))
  103. // return QVariant::String;
  104. // return 0;
  105. //}
  106. return QtVariantPropertyManager::attributeType(propertyType, attribute);
  107. }
  108. QVariant VariantManager::attributeValue(const QtProperty *property,
  109. const QString &attribute) const
  110. {
  111. if (theValues.contains(property)) {
  112. if (attribute == QLatin1String("supportevents"))
  113. return theValues[property].attribute;
  114. else if (attribute == QLatin1String("filter"))
  115. return theValues[property].attribute;
  116. return QVariant();
  117. }
  118. return QtVariantPropertyManager::attributeValue(property, attribute);
  119. }
  120. QString VariantManager::valueText(const QtProperty *property) const
  121. {
  122. if (theValues.contains(property))
  123. return theValues[property].value;
  124. return QtVariantPropertyManager::valueText(property);
  125. }
  126. /// <summary>
  127. /// 为控件赋值
  128. /// </summary>
  129. /// <param name="property"></param>
  130. /// <param name="val"></param>
  131. void VariantManager::setValue(QtProperty *property, const QVariant &val)
  132. {
  133. // qDebug() << "VariantManager::setValue - " << val.toString();
  134. // 为自定义类型控件属性赋值
  135. if (theValues.contains(property)) {
  136. if (val.type() != QVariant::String && !val.canConvert(QVariant::String))
  137. return;
  138. QString str = val.value<QString>();
  139. Data d = theValues[property];
  140. if (d.value == str)
  141. return;
  142. d.value = str;
  143. theValues[property] = d;
  144. emit propertyChanged(property);
  145. emit valueChanged(property, str);
  146. return;
  147. }
  148. // 为通用类型控件属性赋值
  149. QtVariantPropertyManager::setValue(property, val);
  150. }
  151. void VariantManager::setAttribute(QtProperty *property,
  152. const QString &attribute,
  153. const QVariant &val)
  154. {
  155. if (theValues.contains(property)) {
  156. if (attribute == QLatin1String("supportevents") ||
  157. attribute == QLatin1String("filter")) {
  158. if (val.type() != QVariant::String && !val.canConvert(QVariant::String))
  159. return;
  160. QString str = val.value<QString>();
  161. Data d = theValues[property];
  162. if (d.attribute == str)
  163. return;
  164. d.attribute = str;
  165. theValues[property] = d;
  166. emit attributeChanged(property, attribute, str);
  167. }
  168. return;
  169. }
  170. QtVariantPropertyManager::setAttribute(property, attribute, val);
  171. }
  172. void VariantManager::initializeProperty(QtProperty *property)
  173. {
  174. //if (propertyType(property) == functionTypeId() ||
  175. // propertyType(property) == tagColorListTypeId() ||
  176. // propertyType(property) == TagTextListTypeId() ||
  177. // propertyType(property) == filePathTypeId() ||
  178. // propertyType(property) == scriptTypeId()) {
  179. // theValues[property] = Data();
  180. //}
  181. // 初始化自定义类型
  182. if (propertyType(property) == tagDataLinkTypeId() ||
  183. propertyType(property) == tagTableExInfoTypeId())
  184. {
  185. theValues[property] = Data();
  186. }
  187. // 初始化属性列表
  188. QtVariantPropertyManager::initializeProperty(property);
  189. }
  190. void VariantManager::uninitializeProperty(QtProperty *property)
  191. {
  192. theValues.remove(property);
  193. QtVariantPropertyManager::uninitializeProperty(property);
  194. }
  195. void VariantManager::setPropertyEditor(QtTreePropertyBrowser *editor)
  196. {
  197. if(propertyEditor_ != editor) {
  198. propertyEditor_ = editor;
  199. }
  200. }
  201. QtTreePropertyBrowser *VariantManager::getPropertyEditor()
  202. {
  203. return propertyEditor_;
  204. }