1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-20 20:23:03 +02:00
vcmi/mapeditor/inspector.h

150 lines
4.1 KiB
C
Raw Normal View History

2022-09-04 02:12:33 +02:00
#ifndef INSPECTOR_H
#define INSPECTOR_H
#include <QTableWidget>
#include <QTableWidgetItem>
#include <QStyledItemDelegate>
#include "../lib/int3.h"
#include "../lib/GameConstants.h"
#include "../lib/mapObjects/MapObjects.h"
#include "../lib/ResourceSet.h"
2022-09-04 02:12:33 +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 &);
2022-09-04 02:12:33 +02:00
#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 Initializer
{
public:
//===============DECLARE MAP OBJECTS======================================
DECLARE_OBJ_TYPE(CArmedInstance);
2022-09-07 00:21:00 +02:00
DECLARE_OBJ_TYPE(CGShipyard);
DECLARE_OBJ_TYPE(CGTownInstance);
DECLARE_OBJ_TYPE(CGArtifact);
DECLARE_OBJ_TYPE(CGMine);
DECLARE_OBJ_TYPE(CGResource);
2022-09-07 00:21:00 +02:00
DECLARE_OBJ_TYPE(CGDwelling);
DECLARE_OBJ_TYPE(CGGarrison);
DECLARE_OBJ_TYPE(CGHeroInstance);
2022-09-07 03:33:48 +02:00
DECLARE_OBJ_TYPE(CGCreature);
Initializer(CMap *, CGObjectInstance *);
private:
CMap * map;
};
2022-09-04 02:12:33 +02:00
class Inspector
{
protected:
//===============DECLARE PROPERTIES SETUP=================================
DECLARE_OBJ_PROPERTY_METHODS(CArmedInstance);
DECLARE_OBJ_PROPERTY_METHODS(CGTownInstance);
2022-09-07 00:21:00 +02:00
DECLARE_OBJ_PROPERTY_METHODS(CGShipyard);
DECLARE_OBJ_PROPERTY_METHODS(CGArtifact);
DECLARE_OBJ_PROPERTY_METHODS(CGMine);
DECLARE_OBJ_PROPERTY_METHODS(CGResource);
2022-09-07 00:21:00 +02:00
DECLARE_OBJ_PROPERTY_METHODS(CGDwelling);
DECLARE_OBJ_PROPERTY_METHODS(CGGarrison);
DECLARE_OBJ_PROPERTY_METHODS(CGHeroInstance);
2022-09-07 03:33:48 +02:00
DECLARE_OBJ_PROPERTY_METHODS(CGCreature);
//===============DECLARE PROPERTY VALUE TYPE==============================
QTableWidgetItem * addProperty(unsigned int value);
2022-09-04 02:12:33 +02:00
QTableWidgetItem * addProperty(int value);
QTableWidgetItem * addProperty(const std::string & value);
QTableWidgetItem * addProperty(const QString & value);
QTableWidgetItem * addProperty(const int3 & value);
QTableWidgetItem * addProperty(const PlayerColor & value);
QTableWidgetItem * addProperty(const Res::ERes & value);
2022-09-04 02:12:33 +02:00
QTableWidgetItem * addProperty(bool value);
QTableWidgetItem * addProperty(CGObjectInstance * value);
2022-09-07 03:33:48 +02:00
QTableWidgetItem * addProperty(CGCreature::Character value);
//===============END OF DECLARATION=======================================
public:
Inspector(CMap *, CGObjectInstance *, QTableWidget *);
2022-09-04 02:12:33 +02:00
void setProperty(const QString & key, const QVariant & value);
void updateProperties();
protected:
2022-09-04 02:12:33 +02:00
template<class T>
void addProperty(const QString & key, const T & value, QAbstractItemDelegate * delegate, bool restricted)
2022-09-04 02:12:33 +02:00
{
auto * itemValue = addProperty(value);
if(restricted)
itemValue->setFlags(Qt::NoItemFlags);
QTableWidgetItem * itemKey = nullptr;
if(keyItems.contains(key))
{
itemKey = keyItems[key];
table->setItem(table->row(itemKey), 1, itemValue);
if(delegate)
table->setItemDelegateForRow(table->row(itemKey), delegate);
}
else
{
itemKey = new QTableWidgetItem(key);
itemKey->setFlags(Qt::NoItemFlags);
keyItems[key] = itemKey;
2022-09-04 02:12:33 +02:00
table->setRowCount(row + 1);
table->setItem(row, 0, itemKey);
table->setItem(row, 1, itemValue);
if(delegate)
table->setItemDelegateForRow(row, delegate);
++row;
}
2022-09-04 02:12:33 +02:00
}
template<class T>
void addProperty(const QString & key, const T & value, bool restricted = true)
{
addProperty<T>(key, value, nullptr, restricted);
}
2022-09-04 02:12:33 +02:00
protected:
int row = 0;
QTableWidget * table;
CGObjectInstance * obj;
QMap<QString, QTableWidgetItem*> keyItems;
CMap * map;
};
2022-09-04 02:12:33 +02:00
class InspectorDelegate : public QStyledItemDelegate
2022-09-04 02:12:33 +02:00
{
Q_OBJECT
public:
2022-09-08 01:06:46 +02:00
static InspectorDelegate * boolDelegate();
InspectorDelegate(const QStringList &);
2022-09-04 02:12:33 +02:00
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;
QStringList options;
2022-09-04 02:12:33 +02:00
private slots:
void commitAndCloseEditor(int);
2022-09-04 02:12:33 +02:00
};
#endif // INSPECTOR_H