Crossfire Server, Trunk
ArchetypesModel.cpp
Go to the documentation of this file.
1 #include "ArchetypesModel.h"
2 #include "assets.h"
3 #include "AssetsManager.h"
4 #include "CREPixmap.h"
6 #include "ResourcesManager.h"
7 
8 #define PROPERTY_COUNT 11
9 const char *properties[PROPERTY_COUNT] = {
10  "hp",
11  "con",
12  "regen",
13  "ac",
14  "wc",
15  "weight",
16  "experience",
17  "level",
18  "damage",
19  "speed",
20  "dps"
21 };
22 
24  getManager()->archetypes()->each([this, resourcesManager] (archetype *arch) {
25  if (QUERY_FLAG(&arch->clone, FLAG_MONSTER) && (!arch->head)) {
26  myMonsters.push_back(resourcesManager->wrap(&arch->clone, nullptr));
27  }
28  });
29 }
30 
32 }
33 
34 int ArchetypesModel::rowCount(const QModelIndex &parent) const {
35  (void)parent;
36  return myMonsters.size();
37 }
38 
39 int ArchetypesModel::columnCount(const QModelIndex &parent) const {
40  (void)parent;
41  return PROPERTY_COUNT;
42 }
43 
44 QVariant ArchetypesModel::data(const QModelIndex &index, int role) const {
45  if (role != Qt::DisplayRole && role != Qt::EditRole) {
46  return QVariant();
47  }
48  auto monster = myMonsters[index.row()];
49  return monster->property(properties[index.column()]);
50 }
51 
52 QVariant ArchetypesModel::headerData(int section, Qt::Orientation orientation, int role) const {
53  if (orientation == Qt::Vertical) {
54  if (role == Qt::DisplayRole) {
55  return myMonsters[section]->displayName();
56  } else if (role == Qt::DecorationRole) {
57  return CREPixmap::getIcon(myMonsters[section]->face());
58  }
59  return QVariant();
60  }
61 
62  if (role != Qt::DisplayRole) {
63  return QAbstractTableModel::headerData(section, orientation, role);
64  }
65  return properties[section];
66 }
67 
68 Qt::ItemFlags ArchetypesModel::flags(const QModelIndex &index) const {
69  if (!index.isValid()) {
70  return Qt::NoItemFlags;
71  }
72 
73  if (index.column() < 0 || index.column() >= PROPERTY_COUNT) {
75  }
76 
77  Qt::ItemFlags flags = QAbstractItemModel::flags(index);
78 
79  int pi = ObjectWrapper::staticMetaObject.indexOfProperty(properties[index.column()]);
80  if (pi != -1 && ObjectWrapper::staticMetaObject.property(pi).isWritable()) {
81  flags |= Qt::ItemIsEditable;
82  }
83  return flags;
84 }
85 
86 bool ArchetypesModel::setData(const QModelIndex &index, const QVariant &value, int role) {
87  if (role != Qt::EditRole)
88  return false;
89 
90  auto monster = myMonsters[index.row()];
91  monster->setProperty(properties[index.column()], value);
92 
93 // emit dataChanged(index, index, {Qt::DisplayRole, Qt::EditRole});
94 // emit archetypeModified(monster->arch()->arch());
95  return true;
96 }
QUERY_FLAG
#define QUERY_FLAG(xyz, p)
Definition: define.h:226
ResourcesManager
Definition: ResourcesManager.h:67
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:39
CREPixmap::getIcon
static QIcon getIcon(uint16_t faceNumber)
Definition: CREPixmap.cpp:55
ObjectWrapper.h
ArchetypesModel::headerData
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
Definition: ArchetypesModel.cpp:52
flags
static const flag_definition flags[]
Definition: gridarta-types-convert.c:101
getManager
AssetsManager * getManager()
Definition: assets.cpp:333
archt
Definition: object.h:470
PROPERTY_COUNT
#define PROPERTY_COUNT
Definition: ArchetypesModel.cpp:8
ArchetypesModel.h
ArchetypesModel::data
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
Definition: ArchetypesModel.cpp:44
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:86
ArchetypesModel::rowCount
int rowCount(const QModelIndex &parent=QModelIndex()) const
Definition: ArchetypesModel.cpp:34
ArchetypesModel::ArchetypesModel
ArchetypesModel(ResourcesManager *resourcesManager)
Definition: ArchetypesModel.cpp:23
autojail.value
value
Definition: autojail.py:6
ArchetypesModel::~ArchetypesModel
virtual ~ArchetypesModel()
Definition: ArchetypesModel.cpp:31
assets.h
npc_dialog.index
int index
Definition: npc_dialog.py:102
ArchetypesModel::flags
Qt::ItemFlags flags(const QModelIndex &index) const
Definition: ArchetypesModel.cpp:68
ArchetypesModel::myMonsters
std::vector< ObjectWrapper * > myMonsters
Definition: ArchetypesModel.h:32
CREPixmap.h
properties
const char * properties[PROPERTY_COUNT]
Definition: ArchetypesModel.cpp:9