Crossfire Server, Trunk
ArchetypesModel.cpp
Go to the documentation of this file.
1 /*
2  * Crossfire -- cooperative multi-player graphical RPG and adventure game
3  *
4  * Copyright (c) 2022 the Crossfire Development Team
5  *
6  * Crossfire is free software and comes with ABSOLUTELY NO WARRANTY. You are
7  * welcome to redistribute it under certain conditions. For details, please
8  * see COPYING and LICENSE.
9  *
10  * The authors can be reached via e-mail at <crossfire@metalforge.org>.
11  */
12 
13 #include "ArchetypesModel.h"
14 #include "assets.h"
15 #include "AssetsManager.h"
16 #include "CREPixmap.h"
18 #include "ResourcesManager.h"
19 
20 #define PROPERTY_COUNT 20
21 const char *properties[PROPERTY_COUNT] = {
22  "arch_name",
23  "hp",
24  "maxhp",
25  "con",
26  "regen",
27  "ac",
28  "wc",
29  "weight",
30  "experience",
31  "level",
32  "suggested_level",
33  "damage",
34  "speed",
35  "dps",
36  "sp",
37  "maxsp",
38  "pow",
39  "wis",
40  "str",
41  "intl",
42 };
43 
45  getManager()->archetypes()->each([this, resourcesManager] (archetype *arch) {
46  if (QUERY_FLAG(&arch->clone, FLAG_MONSTER) && (!arch->head)) {
47  myMonsters.push_back(resourcesManager->wrap(&arch->clone, nullptr));
48  }
49  });
50 }
51 
53 }
54 
55 int ArchetypesModel::rowCount(const QModelIndex &parent) const {
56  (void)parent;
57  return myMonsters.size();
58 }
59 
60 int ArchetypesModel::columnCount(const QModelIndex &parent) const {
61  (void)parent;
62  return PROPERTY_COUNT;
63 }
64 
65 QVariant ArchetypesModel::data(const QModelIndex &index, int role) const {
66  if (role != Qt::DisplayRole && role != Qt::EditRole) {
67  return QVariant();
68  }
69  auto monster = myMonsters[index.row()];
70  return monster->property(properties[index.column()]);
71 }
72 
73 QVariant ArchetypesModel::headerData(int section, Qt::Orientation orientation, int role) const {
74  if (orientation == Qt::Vertical) {
75  if (role == Qt::DisplayRole) {
76  return myMonsters[section]->displayName();
77  } else if (role == Qt::DecorationRole) {
78  return CREPixmap::getIcon(myMonsters[section]->face());
79  }
80  return QVariant();
81  }
82 
83  if (role != Qt::DisplayRole) {
84  return QAbstractTableModel::headerData(section, orientation, role);
85  }
86  return properties[section];
87 }
88 
89 Qt::ItemFlags ArchetypesModel::flags(const QModelIndex &index) const {
90  if (!index.isValid()) {
91  return Qt::NoItemFlags;
92  }
93 
94  if (index.column() < 0 || index.column() >= PROPERTY_COUNT) {
96  }
97 
98  Qt::ItemFlags flags = QAbstractItemModel::flags(index);
99 
100  int pi = ObjectWrapper::staticMetaObject.indexOfProperty(properties[index.column()]);
101  if (pi != -1 && ObjectWrapper::staticMetaObject.property(pi).isWritable()) {
102  flags |= Qt::ItemIsEditable;
103  }
104  return flags;
105 }
106 
107 bool ArchetypesModel::setData(const QModelIndex &index, const QVariant &value, int role) {
108  if (role != Qt::EditRole)
109  return false;
110 
111  auto monster = myMonsters[index.row()];
112  monster->setProperty(properties[index.column()], value);
113 
114 // emit dataChanged(index, index, {Qt::DisplayRole, Qt::EditRole});
115 // emit archetypeModified(monster->arch()->arch());
116  return true;
117 }
QUERY_FLAG
#define QUERY_FLAG(xyz, p)
Definition: define.h:226
ResourcesManager
Definition: ResourcesManager.h:80
archininventory.arch
arch
DIALOGCHECK MINARGS 1 MAXARGS 1
Definition: archininventory.py:16
AssetsManager.h
ArchetypesModel::columnCount
int columnCount(const QModelIndex &parent=QModelIndex()) const
Definition: ArchetypesModel.cpp:60
CREPixmap::getIcon
static QIcon getIcon(uint16_t faceNumber)
Definition: CREPixmap.cpp:65
ObjectWrapper.h
flags
static const flag_definition flags[]
Definition: gridarta-types-convert.cpp:101
ArchetypesModel::headerData
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
Definition: ArchetypesModel.cpp:73
getManager
AssetsManager * getManager()
Definition: assets.cpp:305
PROPERTY_COUNT
#define PROPERTY_COUNT
Definition: ArchetypesModel.cpp:20
ArchetypesModel.h
ArchetypesModel::data
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
Definition: ArchetypesModel.cpp:65
archetype
Definition: object.h:483
AssetsCollection::each
void each(std::function< void(T *)> op)
Definition: AssetsCollection.h:158
nlohmann::detail::void
j template void())
Definition: json.hpp:4099
FLAG_MONSTER
#define FLAG_MONSTER
Definition: define.h:245
AssetsManager::archetypes
Archetypes * archetypes()
Definition: AssetsManager.h:44
ResourcesManager.h
ArchetypesModel::setData
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole)
Definition: ArchetypesModel.cpp:107
ArchetypesModel::rowCount
int rowCount(const QModelIndex &parent=QModelIndex()) const
Definition: ArchetypesModel.cpp:55
ArchetypesModel::ArchetypesModel
ArchetypesModel(ResourcesManager *resourcesManager)
Definition: ArchetypesModel.cpp:44
autojail.value
value
Definition: autojail.py:6
ArchetypesModel::~ArchetypesModel
virtual ~ArchetypesModel()
Definition: ArchetypesModel.cpp:52
assets.h
npc_dialog.index
int index
Definition: npc_dialog.py:102
ArchetypesModel::flags
Qt::ItemFlags flags(const QModelIndex &index) const
Definition: ArchetypesModel.cpp:89
ArchetypesModel::myMonsters
std::vector< ObjectWrapper * > myMonsters
Definition: ArchetypesModel.h:42
CREPixmap.h
properties
const char * properties[PROPERTY_COUNT]
Definition: ArchetypesModel.cpp:21