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 12
21 const char *properties[PROPERTY_COUNT] = {
22  "hp",
23  "con",
24  "regen",
25  "ac",
26  "wc",
27  "weight",
28  "experience",
29  "level",
30  "suggested_level",
31  "damage",
32  "speed",
33  "dps"
34 };
35 
37  getManager()->archetypes()->each([this, resourcesManager] (archetype *arch) {
38  if (QUERY_FLAG(&arch->clone, FLAG_MONSTER) && (!arch->head)) {
39  myMonsters.push_back(resourcesManager->wrap(&arch->clone, nullptr));
40  }
41  });
42 }
43 
45 }
46 
47 int ArchetypesModel::rowCount(const QModelIndex &parent) const {
48  (void)parent;
49  return myMonsters.size();
50 }
51 
52 int ArchetypesModel::columnCount(const QModelIndex &parent) const {
53  (void)parent;
54  return PROPERTY_COUNT;
55 }
56 
57 QVariant ArchetypesModel::data(const QModelIndex &index, int role) const {
58  if (role != Qt::DisplayRole && role != Qt::EditRole) {
59  return QVariant();
60  }
61  auto monster = myMonsters[index.row()];
62  return monster->property(properties[index.column()]);
63 }
64 
65 QVariant ArchetypesModel::headerData(int section, Qt::Orientation orientation, int role) const {
66  if (orientation == Qt::Vertical) {
67  if (role == Qt::DisplayRole) {
68  return myMonsters[section]->displayName();
69  } else if (role == Qt::DecorationRole) {
70  return CREPixmap::getIcon(myMonsters[section]->face());
71  }
72  return QVariant();
73  }
74 
75  if (role != Qt::DisplayRole) {
76  return QAbstractTableModel::headerData(section, orientation, role);
77  }
78  return properties[section];
79 }
80 
81 Qt::ItemFlags ArchetypesModel::flags(const QModelIndex &index) const {
82  if (!index.isValid()) {
83  return Qt::NoItemFlags;
84  }
85 
86  if (index.column() < 0 || index.column() >= PROPERTY_COUNT) {
88  }
89 
90  Qt::ItemFlags flags = QAbstractItemModel::flags(index);
91 
92  int pi = ObjectWrapper::staticMetaObject.indexOfProperty(properties[index.column()]);
93  if (pi != -1 && ObjectWrapper::staticMetaObject.property(pi).isWritable()) {
94  flags |= Qt::ItemIsEditable;
95  }
96  return flags;
97 }
98 
99 bool ArchetypesModel::setData(const QModelIndex &index, const QVariant &value, int role) {
100  if (role != Qt::EditRole)
101  return false;
102 
103  auto monster = myMonsters[index.row()];
104  monster->setProperty(properties[index.column()], value);
105 
106 // emit dataChanged(index, index, {Qt::DisplayRole, Qt::EditRole});
107 // emit archetypeModified(monster->arch()->arch());
108  return true;
109 }
ArchetypesModel::ArchetypesModel
ArchetypesModel(ResourcesManager *resourcesManager)
Definition: ArchetypesModel.cpp:36
ArchetypesModel::headerData
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
Definition: ArchetypesModel.cpp:65
ArchetypesModel::myMonsters
std::vector< ObjectWrapper * > myMonsters
Definition: ArchetypesModel.h:42
QUERY_FLAG
#define QUERY_FLAG(xyz, p)
Definition: define.h:226
archininventory.arch
arch
DIALOGCHECK MINARGS 1 MAXARGS 1
Definition: archininventory.py:16
ArchetypesModel::~ArchetypesModel
virtual ~ArchetypesModel()
Definition: ArchetypesModel.cpp:44
flags
static const flag_definition flags[]
Definition: gridarta-types-convert.cpp:101
monster
the faster the spell may be cast there are several other common only the caster may be affected by the spell The most common spell range is that of touch This denotes that the caster much touch the recipient of the spell in order to release the spell monster
Definition: spell-info.txt:45
AssetsManager.h
getManager
AssetsManager * getManager()
Definition: assets.cpp:305
ArchetypesModel.h
ArchetypesModel::setData
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole)
Definition: ArchetypesModel.cpp:99
ResourcesManager
Definition: ResourcesManager.h:80
AssetsManager::archetypes
Archetypes * archetypes()
Definition: AssetsManager.h:44
archetype
Definition: object.h:474
nlohmann::detail::void
j template void())
Definition: json.hpp:4099
FLAG_MONSTER
#define FLAG_MONSTER
Definition: define.h:245
ArchetypesModel::columnCount
int columnCount(const QModelIndex &parent=QModelIndex()) const
Definition: ArchetypesModel.cpp:52
ResourcesManager.h
ArchetypesModel::rowCount
int rowCount(const QModelIndex &parent=QModelIndex()) const
Definition: ArchetypesModel.cpp:47
CREPixmap.h
AssetsCollection::each
void each(std::function< void(T *)> op)
Definition: AssetsCollection.h:158
autojail.value
value
Definition: autojail.py:6
assets.h
npc_dialog.index
int index
Definition: npc_dialog.py:102
properties
const char * properties[PROPERTY_COUNT]
Definition: ArchetypesModel.cpp:21
CREPixmap::getIcon
static QIcon getIcon(uint16_t faceNumber)
Definition: CREPixmap.cpp:65
PROPERTY_COUNT
#define PROPERTY_COUNT
Definition: ArchetypesModel.cpp:20
ObjectWrapper.h
ArchetypesModel::data
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
Definition: ArchetypesModel.cpp:57
ArchetypesModel::flags
Qt::ItemFlags flags(const QModelIndex &index) const
Definition: ArchetypesModel.cpp:81