1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-16 10:19:47 +02:00
vcmi/mapeditor/inspector/inspector.h

181 lines
5.6 KiB
C++
Raw Normal View History

2022-10-08 22:32:02 +02:00
/*
* inspector.h, part of VCMI engine
*
* Authors: listed in file AUTHORS in main folder
*
* License: GNU General Public License v2.0 or later
* Full text of license available in license.txt file, in main folder
*
*/
#pragma once
2022-09-18 01:23:17 +02:00
2022-10-16 13:37:01 +02:00
#include "../StdInc.h"
2022-09-18 01:23:17 +02:00
#include <QTableWidget>
#include <QTableWidgetItem>
#include <QStyledItemDelegate>
#include "../lib/int3.h"
#include "../lib/GameConstants.h"
2023-06-17 14:21:42 +02:00
#include "../lib/mapObjects/CGCreature.h"
2022-09-18 01:23:17 +02:00
#include "../lib/mapObjects/MapObjects.h"
2023-09-16 05:34:27 +02:00
#include "../lib/mapObjects/CRewardableObject.h"
2023-09-28 14:03:58 +02:00
#include "../lib/CGeneralTextHandler.h"
2022-09-18 01:23:17 +02:00
#include "../lib/ResourceSet.h"
2023-09-28 13:28:37 +02:00
#include "../lib/MetaString.h"
2022-09-18 01:23:17 +02:00
#define DECLARE_OBJ_TYPE(x) void initialize(x*);
#define DECLARE_OBJ_PROPERTY_METHODS(x) \
void updateProperties(x*); \
void setProperty(x*, const QString &, const QVariant &);
#define INIT_OBJ_TYPE(x) initialize(dynamic_cast<x*>(o))
#define UPDATE_OBJ_PROPERTIES(x) updateProperties(dynamic_cast<x*>(obj))
#define SET_PROPERTIES(x) setProperty(dynamic_cast<x*>(obj), key, value)
class MapController;
2022-09-18 01:23:17 +02:00
class Initializer
{
public:
//===============DECLARE MAP OBJECTS======================================
DECLARE_OBJ_TYPE(CArmedInstance);
DECLARE_OBJ_TYPE(CGShipyard);
DECLARE_OBJ_TYPE(CGTownInstance);
DECLARE_OBJ_TYPE(CGArtifact);
DECLARE_OBJ_TYPE(CGMine);
DECLARE_OBJ_TYPE(CGResource);
DECLARE_OBJ_TYPE(CGDwelling);
DECLARE_OBJ_TYPE(CGGarrison);
2023-10-16 22:24:12 +02:00
DECLARE_OBJ_TYPE(CGHeroPlaceholder);
2022-09-18 01:23:17 +02:00
DECLARE_OBJ_TYPE(CGHeroInstance);
DECLARE_OBJ_TYPE(CGCreature);
DECLARE_OBJ_TYPE(CGSignBottle);
DECLARE_OBJ_TYPE(CGLighthouse);
2023-09-16 05:34:27 +02:00
//DECLARE_OBJ_TYPE(CRewardableObject);
2022-09-18 01:23:17 +02:00
//DECLARE_OBJ_TYPE(CGEvent);
//DECLARE_OBJ_TYPE(CGPandoraBox);
2022-10-16 01:20:24 +02:00
//DECLARE_OBJ_TYPE(CGSeerHut);
2022-09-18 01:23:17 +02:00
Initializer(CGObjectInstance *, const PlayerColor &);
private:
PlayerColor defaultPlayer;
};
class Inspector
{
protected:
struct PropertyEditorPlaceholder {};
//===============DECLARE PROPERTIES SETUP=================================
DECLARE_OBJ_PROPERTY_METHODS(CArmedInstance);
DECLARE_OBJ_PROPERTY_METHODS(CGTownInstance);
DECLARE_OBJ_PROPERTY_METHODS(CGShipyard);
DECLARE_OBJ_PROPERTY_METHODS(CGArtifact);
DECLARE_OBJ_PROPERTY_METHODS(CGMine);
DECLARE_OBJ_PROPERTY_METHODS(CGResource);
DECLARE_OBJ_PROPERTY_METHODS(CGDwelling);
DECLARE_OBJ_PROPERTY_METHODS(CGGarrison);
2023-10-16 22:24:12 +02:00
DECLARE_OBJ_PROPERTY_METHODS(CGHeroPlaceholder);
2022-09-18 01:23:17 +02:00
DECLARE_OBJ_PROPERTY_METHODS(CGHeroInstance);
DECLARE_OBJ_PROPERTY_METHODS(CGCreature);
DECLARE_OBJ_PROPERTY_METHODS(CGSignBottle);
DECLARE_OBJ_PROPERTY_METHODS(CGLighthouse);
2023-09-16 05:34:27 +02:00
DECLARE_OBJ_PROPERTY_METHODS(CRewardableObject);
2022-09-18 01:23:17 +02:00
DECLARE_OBJ_PROPERTY_METHODS(CGPandoraBox);
DECLARE_OBJ_PROPERTY_METHODS(CGEvent);
2022-10-16 01:20:24 +02:00
DECLARE_OBJ_PROPERTY_METHODS(CGSeerHut);
2023-10-11 23:28:10 +02:00
DECLARE_OBJ_PROPERTY_METHODS(CGQuestGuard);
2022-09-18 01:23:17 +02:00
//===============DECLARE PROPERTY VALUE TYPE==============================
QTableWidgetItem * addProperty(unsigned int value);
QTableWidgetItem * addProperty(int value);
2023-09-28 13:28:37 +02:00
QTableWidgetItem * addProperty(const MetaString & value);
2023-09-28 14:03:58 +02:00
QTableWidgetItem * addProperty(const TextIdentifier & value);
2022-09-18 01:23:17 +02:00
QTableWidgetItem * addProperty(const std::string & value);
QTableWidgetItem * addProperty(const QString & value);
QTableWidgetItem * addProperty(const int3 & value);
QTableWidgetItem * addProperty(const PlayerColor & value);
2023-04-05 02:26:29 +02:00
QTableWidgetItem * addProperty(const GameResID & value);
2022-09-18 01:23:17 +02:00
QTableWidgetItem * addProperty(bool value);
QTableWidgetItem * addProperty(CGObjectInstance * value);
QTableWidgetItem * addProperty(CGCreature::Character value);
QTableWidgetItem * addProperty(PropertyEditorPlaceholder value);
//===============END OF DECLARATION=======================================
public:
Inspector(MapController &, CGObjectInstance *, QTableWidget *);
2022-09-18 01:23:17 +02:00
2023-09-10 22:05:57 +02:00
void setProperty(const QString & key, const QTableWidgetItem * item);
2022-09-18 01:23:17 +02:00
void setProperty(const QString & key, const QVariant & value);
void updateProperties();
protected:
template<class T>
void addProperty(const QString & key, const T & value, QAbstractItemDelegate * delegate, bool restricted)
{
auto * itemValue = addProperty(value);
2023-09-10 22:05:57 +02:00
if(!restricted)
itemValue->setFlags(itemValue->flags() | Qt::ItemIsEnabled | Qt::ItemIsSelectable);
if(!(itemValue->flags() & Qt::ItemIsUserCheckable))
itemValue->setFlags(itemValue->flags() | Qt::ItemIsEditable);
2022-09-18 01:23:17 +02:00
QTableWidgetItem * itemKey = nullptr;
if(keyItems.contains(key))
{
itemKey = keyItems[key];
table->setItem(table->row(itemKey), 1, itemValue);
2024-02-29 23:11:28 +02:00
table->setItemDelegateForRow(table->row(itemKey), delegate);
2022-09-18 01:23:17 +02:00
}
else
{
itemKey = new QTableWidgetItem(key);
keyItems[key] = itemKey;
table->setRowCount(row + 1);
table->setItem(row, 0, itemKey);
table->setItem(row, 1, itemValue);
2024-02-29 23:11:28 +02:00
table->setItemDelegateForRow(row, delegate);
2022-09-18 01:23:17 +02:00
++row;
}
2023-09-10 18:10:50 +02:00
itemKey->setFlags(restricted ? Qt::NoItemFlags : Qt::ItemIsEnabled);
2022-09-18 01:23:17 +02:00
}
template<class T>
void addProperty(const QString & key, const T & value, bool restricted = true)
{
addProperty<T>(key, value, nullptr, restricted);
}
2023-09-28 14:03:58 +02:00
2022-09-18 01:23:17 +02:00
protected:
int row = 0;
QTableWidget * table;
CGObjectInstance * obj;
QMap<QString, QTableWidgetItem*> keyItems;
MapController & controller;
2022-09-18 01:23:17 +02:00
};
class InspectorDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
using QStyledItemDelegate::QStyledItemDelegate;
QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
void setEditorData(QWidget *editor, const QModelIndex &index) const override;
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override;
2023-09-10 22:05:57 +02:00
QList<std::pair<QString, QVariant>> options;
2022-09-18 01:23:17 +02:00
};
2024-02-29 23:11:28 +02:00
class OwnerDelegate : public InspectorDelegate
{
Q_OBJECT
public:
OwnerDelegate(MapController & controller, bool addNeutral = true);
2024-02-29 23:11:28 +02:00
};