inspector.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * inspector.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. #include "../StdInc.h"
  12. #include <QTableWidget>
  13. #include <QTableWidgetItem>
  14. #include <QStyledItemDelegate>
  15. #include "../lib/int3.h"
  16. #include "../lib/GameConstants.h"
  17. #include "../lib/mapObjects/CGCreature.h"
  18. #include "../lib/mapObjects/MapObjects.h"
  19. #include "../lib/mapObjects/CRewardableObject.h"
  20. #include "../lib/CGeneralTextHandler.h"
  21. #include "../lib/ResourceSet.h"
  22. #include "../lib/MetaString.h"
  23. #define DECLARE_OBJ_TYPE(x) void initialize(x*);
  24. #define DECLARE_OBJ_PROPERTY_METHODS(x) \
  25. void updateProperties(x*); \
  26. void setProperty(x*, const QString &, const QVariant &);
  27. #define INIT_OBJ_TYPE(x) initialize(dynamic_cast<x*>(o))
  28. #define UPDATE_OBJ_PROPERTIES(x) updateProperties(dynamic_cast<x*>(obj))
  29. #define SET_PROPERTIES(x) setProperty(dynamic_cast<x*>(obj), key, value)
  30. class MapController;
  31. class Initializer
  32. {
  33. public:
  34. //===============DECLARE MAP OBJECTS======================================
  35. DECLARE_OBJ_TYPE(CArmedInstance);
  36. DECLARE_OBJ_TYPE(CGShipyard);
  37. DECLARE_OBJ_TYPE(CGTownInstance);
  38. DECLARE_OBJ_TYPE(CGArtifact);
  39. DECLARE_OBJ_TYPE(CGMine);
  40. DECLARE_OBJ_TYPE(CGResource);
  41. DECLARE_OBJ_TYPE(CGDwelling);
  42. DECLARE_OBJ_TYPE(CGGarrison);
  43. DECLARE_OBJ_TYPE(CGHeroInstance);
  44. DECLARE_OBJ_TYPE(CGCreature);
  45. DECLARE_OBJ_TYPE(CGSignBottle);
  46. DECLARE_OBJ_TYPE(CGLighthouse);
  47. //DECLARE_OBJ_TYPE(CRewardableObject);
  48. //DECLARE_OBJ_TYPE(CGEvent);
  49. //DECLARE_OBJ_TYPE(CGPandoraBox);
  50. //DECLARE_OBJ_TYPE(CGSeerHut);
  51. Initializer(CGObjectInstance *, const PlayerColor &);
  52. private:
  53. PlayerColor defaultPlayer;
  54. };
  55. class Inspector
  56. {
  57. protected:
  58. struct PropertyEditorPlaceholder {};
  59. //===============DECLARE PROPERTIES SETUP=================================
  60. DECLARE_OBJ_PROPERTY_METHODS(CArmedInstance);
  61. DECLARE_OBJ_PROPERTY_METHODS(CGTownInstance);
  62. DECLARE_OBJ_PROPERTY_METHODS(CGShipyard);
  63. DECLARE_OBJ_PROPERTY_METHODS(CGArtifact);
  64. DECLARE_OBJ_PROPERTY_METHODS(CGMine);
  65. DECLARE_OBJ_PROPERTY_METHODS(CGResource);
  66. DECLARE_OBJ_PROPERTY_METHODS(CGDwelling);
  67. DECLARE_OBJ_PROPERTY_METHODS(CGGarrison);
  68. DECLARE_OBJ_PROPERTY_METHODS(CGHeroInstance);
  69. DECLARE_OBJ_PROPERTY_METHODS(CGCreature);
  70. DECLARE_OBJ_PROPERTY_METHODS(CGSignBottle);
  71. DECLARE_OBJ_PROPERTY_METHODS(CGLighthouse);
  72. DECLARE_OBJ_PROPERTY_METHODS(CRewardableObject);
  73. DECLARE_OBJ_PROPERTY_METHODS(CGPandoraBox);
  74. DECLARE_OBJ_PROPERTY_METHODS(CGEvent);
  75. DECLARE_OBJ_PROPERTY_METHODS(CGSeerHut);
  76. DECLARE_OBJ_PROPERTY_METHODS(CGQuestGuard);
  77. //===============DECLARE PROPERTY VALUE TYPE==============================
  78. QTableWidgetItem * addProperty(unsigned int value);
  79. QTableWidgetItem * addProperty(int value);
  80. QTableWidgetItem * addProperty(const MetaString & value);
  81. QTableWidgetItem * addProperty(const TextIdentifier & value);
  82. QTableWidgetItem * addProperty(const std::string & value);
  83. QTableWidgetItem * addProperty(const QString & value);
  84. QTableWidgetItem * addProperty(const int3 & value);
  85. QTableWidgetItem * addProperty(const PlayerColor & value);
  86. QTableWidgetItem * addProperty(const GameResID & value);
  87. QTableWidgetItem * addProperty(bool value);
  88. QTableWidgetItem * addProperty(CGObjectInstance * value);
  89. QTableWidgetItem * addProperty(CGCreature::Character value);
  90. QTableWidgetItem * addProperty(PropertyEditorPlaceholder value);
  91. //===============END OF DECLARATION=======================================
  92. public:
  93. Inspector(MapController &, CGObjectInstance *, QTableWidget *);
  94. void setProperty(const QString & key, const QTableWidgetItem * item);
  95. void setProperty(const QString & key, const QVariant & value);
  96. void updateProperties();
  97. protected:
  98. template<class T>
  99. void addProperty(const QString & key, const T & value, QAbstractItemDelegate * delegate, bool restricted)
  100. {
  101. auto * itemValue = addProperty(value);
  102. if(!restricted)
  103. itemValue->setFlags(itemValue->flags() | Qt::ItemIsEnabled | Qt::ItemIsSelectable);
  104. if(!(itemValue->flags() & Qt::ItemIsUserCheckable))
  105. itemValue->setFlags(itemValue->flags() | Qt::ItemIsEditable);
  106. QTableWidgetItem * itemKey = nullptr;
  107. if(keyItems.contains(key))
  108. {
  109. itemKey = keyItems[key];
  110. table->setItem(table->row(itemKey), 1, itemValue);
  111. if(delegate)
  112. table->setItemDelegateForRow(table->row(itemKey), delegate);
  113. }
  114. else
  115. {
  116. itemKey = new QTableWidgetItem(key);
  117. keyItems[key] = itemKey;
  118. table->setRowCount(row + 1);
  119. table->setItem(row, 0, itemKey);
  120. table->setItem(row, 1, itemValue);
  121. if(delegate)
  122. table->setItemDelegateForRow(row, delegate);
  123. ++row;
  124. }
  125. itemKey->setFlags(restricted ? Qt::NoItemFlags : Qt::ItemIsEnabled);
  126. }
  127. template<class T>
  128. void addProperty(const QString & key, const T & value, bool restricted = true)
  129. {
  130. addProperty<T>(key, value, nullptr, restricted);
  131. }
  132. protected:
  133. int row = 0;
  134. QTableWidget * table;
  135. CGObjectInstance * obj;
  136. QMap<QString, QTableWidgetItem*> keyItems;
  137. MapController & controller;
  138. };
  139. class InspectorDelegate : public QStyledItemDelegate
  140. {
  141. Q_OBJECT
  142. public:
  143. using QStyledItemDelegate::QStyledItemDelegate;
  144. QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
  145. void setEditorData(QWidget *editor, const QModelIndex &index) const override;
  146. void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override;
  147. QList<std::pair<QString, QVariant>> options;
  148. };