inspector.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 "baseinspectoritemdelegate.h"
  16. #include "../lib/int3.h"
  17. #include "../lib/GameConstants.h"
  18. #include "../lib/mapObjects/CGCreature.h"
  19. #include "../lib/mapObjects/CGResource.h"
  20. #include "../lib/mapObjects/CQuest.h"
  21. #include "../lib/mapObjects/MapObjects.h"
  22. #include "../lib/mapObjects/FlaggableMapObject.h"
  23. #include "../lib/mapObjects/CRewardableObject.h"
  24. #include "../lib/texts/CGeneralTextHandler.h"
  25. #include "../lib/ResourceSet.h"
  26. #define DECLARE_OBJ_TYPE(x) void initialize(x*);
  27. #define DECLARE_OBJ_PROPERTY_METHODS(x) \
  28. void updateProperties(x*); \
  29. void setProperty(x*, const QString &, const QVariant &);
  30. #define INIT_OBJ_TYPE(x) initialize(dynamic_cast<x*>(o))
  31. #define UPDATE_OBJ_PROPERTIES(x) updateProperties(dynamic_cast<x*>(obj))
  32. #define SET_PROPERTIES(x) setProperty(dynamic_cast<x*>(obj), key, value)
  33. class MapController;
  34. class Initializer
  35. {
  36. public:
  37. //===============DECLARE MAP OBJECTS======================================
  38. DECLARE_OBJ_TYPE(CArmedInstance);
  39. DECLARE_OBJ_TYPE(CGShipyard);
  40. DECLARE_OBJ_TYPE(CGTownInstance);
  41. DECLARE_OBJ_TYPE(CGArtifact);
  42. DECLARE_OBJ_TYPE(CGMine);
  43. DECLARE_OBJ_TYPE(CGDwelling);
  44. DECLARE_OBJ_TYPE(CGGarrison);
  45. DECLARE_OBJ_TYPE(CGHeroPlaceholder);
  46. DECLARE_OBJ_TYPE(CGHeroInstance);
  47. DECLARE_OBJ_TYPE(CGCreature);
  48. DECLARE_OBJ_TYPE(CGSignBottle);
  49. DECLARE_OBJ_TYPE(FlaggableMapObject);
  50. //DECLARE_OBJ_TYPE(CRewardableObject);
  51. //DECLARE_OBJ_TYPE(CGEvent);
  52. //DECLARE_OBJ_TYPE(CGPandoraBox);
  53. //DECLARE_OBJ_TYPE(CGSeerHut);
  54. Initializer(MapController & controller, CGObjectInstance *, const PlayerColor &);
  55. private:
  56. MapController & controller;
  57. PlayerColor defaultPlayer;
  58. };
  59. class Inspector
  60. {
  61. QList<std::pair<QString, QVariant>> characterIdentifiers;
  62. protected:
  63. struct PropertyEditorPlaceholder {};
  64. //===============DECLARE PROPERTIES SETUP=================================
  65. DECLARE_OBJ_PROPERTY_METHODS(CArmedInstance);
  66. DECLARE_OBJ_PROPERTY_METHODS(CGTownInstance);
  67. DECLARE_OBJ_PROPERTY_METHODS(CGShipyard);
  68. DECLARE_OBJ_PROPERTY_METHODS(CGArtifact);
  69. DECLARE_OBJ_PROPERTY_METHODS(CGMine);
  70. DECLARE_OBJ_PROPERTY_METHODS(CGResource);
  71. DECLARE_OBJ_PROPERTY_METHODS(CGDwelling);
  72. DECLARE_OBJ_PROPERTY_METHODS(CGGarrison);
  73. DECLARE_OBJ_PROPERTY_METHODS(CGHeroPlaceholder);
  74. DECLARE_OBJ_PROPERTY_METHODS(CGHeroInstance);
  75. DECLARE_OBJ_PROPERTY_METHODS(CGCreature);
  76. DECLARE_OBJ_PROPERTY_METHODS(CGSignBottle);
  77. DECLARE_OBJ_PROPERTY_METHODS(FlaggableMapObject);
  78. DECLARE_OBJ_PROPERTY_METHODS(CRewardableObject);
  79. DECLARE_OBJ_PROPERTY_METHODS(CGPandoraBox);
  80. DECLARE_OBJ_PROPERTY_METHODS(CGEvent);
  81. DECLARE_OBJ_PROPERTY_METHODS(CGSeerHut);
  82. DECLARE_OBJ_PROPERTY_METHODS(CGQuestGuard);
  83. //===============DECLARE PROPERTY VALUE TYPE==============================
  84. QTableWidgetItem * addProperty(unsigned int value);
  85. QTableWidgetItem * addProperty(int value);
  86. QTableWidgetItem * addProperty(const MetaString & value);
  87. QTableWidgetItem * addProperty(const TextIdentifier & value);
  88. QTableWidgetItem * addProperty(const std::string & value);
  89. QTableWidgetItem * addProperty(const QString & value);
  90. QTableWidgetItem * addProperty(const int3 & value);
  91. QTableWidgetItem * addProperty(const PlayerColor & value);
  92. QTableWidgetItem * addProperty(const GameResID & value);
  93. QTableWidgetItem * addProperty(bool value);
  94. QTableWidgetItem * addProperty(CGObjectInstance * value);
  95. QTableWidgetItem * addProperty(CGCreature::Character value);
  96. QTableWidgetItem * addProperty(const std::optional<CGDwellingRandomizationInfo> & value);
  97. QTableWidgetItem * addProperty(PropertyEditorPlaceholder value);
  98. //===============END OF DECLARATION=======================================
  99. public:
  100. Inspector(MapController &, CGObjectInstance *, QTableWidget *);
  101. void setProperty(const QString & key, const QTableWidgetItem * item);
  102. void setProperty(const QString & key, const QVariant & value);
  103. void updateProperties();
  104. protected:
  105. template<class T>
  106. void addProperty(const QString & key, const T & value, BaseInspectorItemDelegate * delegate, bool restricted)
  107. {
  108. auto * itemValue = addProperty(value);
  109. if(!restricted)
  110. itemValue->setFlags(itemValue->flags() | Qt::ItemIsEnabled | Qt::ItemIsSelectable);
  111. if(!(itemValue->flags() & Qt::ItemIsUserCheckable))
  112. itemValue->setFlags(itemValue->flags() | Qt::ItemIsEditable);
  113. QTableWidgetItem * itemKey = nullptr;
  114. if(keyItems.contains(key))
  115. {
  116. itemKey = keyItems[key];
  117. table->setItem(table->row(itemKey), 1, itemValue);
  118. table->setItemDelegateForRow(table->row(itemKey), delegate);
  119. }
  120. else
  121. {
  122. itemKey = new QTableWidgetItem(key);
  123. keyItems[key] = itemKey;
  124. table->setRowCount(row + 1);
  125. table->setItem(row, 0, itemKey);
  126. table->setItem(row, 1, itemValue);
  127. table->setItemDelegateForRow(row, delegate);
  128. ++row;
  129. }
  130. itemKey->setFlags(restricted ? Qt::NoItemFlags : Qt::ItemIsEnabled);
  131. if(delegate != nullptr)
  132. {
  133. QModelIndex index = table->model()->index(itemValue->row(), itemValue->column());
  134. delegate->updateModelData(table->model(), index);
  135. }
  136. }
  137. template<class T>
  138. void addProperty(const QString & key, const T & value, bool restricted = true)
  139. {
  140. addProperty<T>(key, value, nullptr, restricted);
  141. }
  142. protected:
  143. int row = 0;
  144. QTableWidget * table;
  145. CGObjectInstance * obj;
  146. QMap<QString, QTableWidgetItem*> keyItems;
  147. MapController & controller;
  148. };
  149. class InspectorDelegate : public BaseInspectorItemDelegate
  150. {
  151. Q_OBJECT
  152. public:
  153. using BaseInspectorItemDelegate::BaseInspectorItemDelegate;
  154. QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
  155. void setEditorData(QWidget *editor, const QModelIndex &index) const override;
  156. void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override;
  157. QList<std::pair<QString, QVariant>> options;
  158. };
  159. class OwnerDelegate : public InspectorDelegate
  160. {
  161. Q_OBJECT
  162. public:
  163. OwnerDelegate(MapController & controller, bool addNeutral = true);
  164. };