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