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 }
QUERY_FLAG
#define QUERY_FLAG(xyz, p)
Definition: define.h:226
ResourcesManager
Definition: ResourcesManager.h:79
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:52
CREPixmap::getIcon
static QIcon getIcon(uint16_t faceNumber)
Definition: CREPixmap.cpp:67
ObjectWrapper.h
ArchetypesModel::headerData
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
Definition: ArchetypesModel.cpp:65
flags
static const flag_definition flags[]
Definition: gridarta-types-convert.c:101
getManager
AssetsManager * getManager()
Definition: assets.cpp:335
archt
Definition: object.h:469
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:57
AssetsCollection::each
void each(std::function< void(T *)> op)
Definition: AssetsCollection.h:135
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:99
ArchetypesModel::rowCount
int rowCount(const QModelIndex &parent=QModelIndex()) const
Definition: ArchetypesModel.cpp:47
ArchetypesModel::ArchetypesModel
ArchetypesModel(ResourcesManager *resourcesManager)
Definition: ArchetypesModel.cpp:36
autojail.value
value
Definition: autojail.py:6
ArchetypesModel::~ArchetypesModel
virtual ~ArchetypesModel()
Definition: ArchetypesModel.cpp:44
assets.h
npc_dialog.index
int index
Definition: npc_dialog.py:102
ArchetypesModel::flags
Qt::ItemFlags flags(const QModelIndex &index) const
Definition: ArchetypesModel.cpp:81
ArchetypesModel::myMonsters
std::vector< ObjectWrapper * > myMonsters
Definition: ArchetypesModel.h:44
CREPixmap.h
properties
const char * properties[PROPERTY_COUNT]
Definition: ArchetypesModel.cpp:21