Crossfire Server, Trunk
ArchetypePanel.cpp
Go to the documentation of this file.
1 #include <Qt>
2 #include "CREMapInformation.h"
4 
5 extern "C" {
6 #include "global.h"
7 }
8 
9 #include "ArchetypePanel.h"
10 #include "CREUtils.h"
11 #include "ResourcesManager.h"
12 
13 #include "assets.h"
14 #include "AssetsManager.h"
15 #include "Archetypes.h"
16 #include "ArchetypeLoader.h"
17 #include "ResourcesManager.h"
18 
20 {
21  Q_ASSERT(store);
22  myStore = store;
23  myResources = resources;
24  myArchetype = nullptr;
25 
26  QGridLayout* layout = new QGridLayout(this);
27 
28  myDisplay = new QTextEdit(this);
29  layout->addWidget(myDisplay, 1, 1);
30 
31  myUsing = new QTreeWidget(this);
32  myUsing->setHeaderLabel(tr("Used by"));
33  myUsing->setIconSize(QSize(32, 32));
34  myUsing->setSortingEnabled(true);
35  myUsing->sortByColumn(0, Qt::AscendingOrder);
36  layout->addWidget(myUsing, 2, 1);
37 }
38 
40 {
42 
45  char* final = stringbuffer_finish(dump);
46  myInitialArch = final;
47  free(final);
48 
49  // Hacks: replace initial "arch" by "Object", and remove the "more 0" line which messes things
50  myInitialArch.replace(0, 4, "Object");
51  auto more = myInitialArch.find("more 0\n");
52  if (more != std::string::npos) {
53  myInitialArch.erase(more, 7);
54  }
55  myDisplay->setText(myInitialArch.data());
56 
57  myUsing->clear();
58 
59  QTreeWidgetItem* rootArch = nullptr, *rootTreasure = nullptr, *rootMap = nullptr, *rootCrafting = nullptr;
60 
62  [this, &rootArch, &rootTreasure, &rootMap, &rootCrafting]
63  (ArchetypeUse use, const archt* arch, const treasurelist* list, const CREMapInformation* map, const recipe* rec) -> bool
64  {
65  if (use == OTHER_ARCH || use == DEATH_ANIM)
66  {
67  if (rootArch == NULL)
68  {
69  rootArch = CREUtils::archetypeNode(NULL);
70  myUsing->addTopLevelItem(rootArch);
71  rootArch->setExpanded(true);
72  }
73  auto node = CREUtils::archetypeNode(arch, rootArch);
74  if (use == DEATH_ANIM) {
75  node->setText(0, node->text(0) + " (as death animation)");
76  }
77  }
78  if (use == TREASURE_USE)
79  {
80  if (rootTreasure == NULL)
81  {
82  rootTreasure = CREUtils::treasureNode(NULL);
83  myUsing->addTopLevelItem(rootTreasure);
84  rootTreasure->setExpanded(true);
85  }
86  CREUtils::treasureNode(list, rootTreasure);
87  }
88  if (use == MAP_USE || use == RANDOM_MAP_FINAL_EXIT)
89  {
90  if (rootMap == nullptr)
91  {
92  rootMap = new QTreeWidgetItem(myUsing, QStringList("Maps"));
93  rootMap->setExpanded(true);
94  }
95 
96  auto node = CREUtils::mapNode(map, rootMap);
97  if (use == RANDOM_MAP_FINAL_EXIT)
98  {
99  node->setText(0, node->text(0) + " (final exit of random map)");
100  }
101  }
102  if (use == ALCHEMY_PRODUCT)
103  {
104  if (rootCrafting == nullptr)
105  {
106  rootCrafting = new QTreeWidgetItem(myUsing, QStringList("Alchemy product"));
107  myUsing->addTopLevelItem(rootCrafting);
108  rootCrafting->setExpanded(true);
109  }
110  CREUtils::formulaeNode(rec, rootCrafting);
111  }
112 
113  return true;
114  });
115 
116  if (rootArch)
117  CREUtils::addCountSuffix(rootArch);
118  if (rootTreasure)
119  CREUtils::addCountSuffix(rootTreasure);
120  if (rootMap)
121  CREUtils::addCountSuffix(rootMap);
122  if (rootCrafting)
123  CREUtils::addCountSuffix(rootCrafting);
124  myUsing->scrollToTop();
125 }
126 
128  auto text = myDisplay->toPlainText().toStdString();
129  if (text == myInitialArch) {
130  return;
131  }
132 
133  BufferReader *br = bufferreader_init_from_memory(nullptr, text.data(), text.length());
134 
135  auto origin = myResources->originOf(myArchetype);
136  ArchetypeLoader loader(getManager()->archetypes(), nullptr);
137  loader.load(br, origin);
139 
141 }
ArchetypePanel::myUsing
QTreeWidget * myUsing
Definition: ArchetypePanel.h:29
CRETPanel
Definition: CREPanel.h:18
global.h
stringbuffer_new
StringBuffer * stringbuffer_new(void)
Definition: stringbuffer.c:57
CREUtils::addCountSuffix
static void addCountSuffix(QTreeWidgetItem *item, int column=0)
Definition: CREUtils.cpp:169
layout
Definition: main.c:85
ResourcesManager::archetypeUse
static void archetypeUse(const archt *item, CREMapInformationManager *store, AssetUseCallback callback)
Definition: ResourcesManager.cpp:105
ResourcesManager
Definition: ResourcesManager.h:67
archininventory.arch
arch
DIALOGCHECK MINARGS 1 MAXARGS 1
Definition: archininventory.py:16
AssetsManager.h
CREMapInformation.h
CREUtils::archetypeNode
static QTreeWidgetItem * archetypeNode(QTreeWidgetItem *parent)
Definition: CREUtils.cpp:14
CREUtils.h
guildoracle.list
list
Definition: guildoracle.py:87
ArchetypePanel::myStore
CREMapInformationManager * myStore
Definition: ArchetypePanel.h:25
ArchetypeUse
ArchetypeUse
Definition: ResourcesManager.h:56
DEATH_ANIM
@ DEATH_ANIM
Definition: ResourcesManager.h:58
CREUtils::mapNode
static QTreeWidgetItem * mapNode(QTreeWidgetItem *parent)
Definition: CREUtils.cpp:159
ArchetypePanel::commitData
virtual void commitData() override
Definition: ArchetypePanel.cpp:127
getManager
AssetsManager * getManager()
Definition: assets.cpp:333
archt
Definition: object.h:470
CREMapInformation
Definition: CREMapInformation.h:16
recipestruct
Definition: recipe.h:10
disinfect.map
map
Definition: disinfect.py:4
ArchetypeLoader
Definition: ArchetypeLoader.h:22
bufferreader_init_from_memory
BufferReader * bufferreader_init_from_memory(BufferReader *br, const char *data, size_t length)
Definition: bufferreader.c:93
object_dump
void object_dump(const object *op, StringBuffer *sb)
Definition: object.c:649
ArchetypeLoader::load
virtual void load(BufferReader *reader, const std::string &filename) override
Definition: ArchetypeLoader.cpp:27
ArchetypePanel::ArchetypePanel
ArchetypePanel(CREMapInformationManager *store, ResourcesManager *resources, QWidget *parent)
Definition: ArchetypePanel.cpp:19
dump
Definition: quest.c:859
ArchetypePanel.h
stringbuffer_finish
char * stringbuffer_finish(StringBuffer *sb)
Definition: stringbuffer.c:76
ArchetypePanel::myResources
ResourcesManager * myResources
Definition: ArchetypePanel.h:26
ArchetypePanel::myInitialArch
std::string myInitialArch
Definition: ArchetypePanel.h:28
MAP_USE
@ MAP_USE
Definition: ResourcesManager.h:60
ArchetypePanel::myDisplay
QTextEdit * myDisplay
Definition: ArchetypePanel.h:27
guild_entry.text
text
Definition: guild_entry.py:44
CREMapInformationManager.h
ResourcesManager.h
ResourcesManager::originOf
std::string originOf(const archt *arch) const
Definition: ResourcesManager.h:95
ArchetypePanel::myArchetype
archt * myArchetype
Definition: ArchetypePanel.h:30
StringBuffer
Definition: stringbuffer.c:25
CREUtils::formulaeNode
static QTreeWidgetItem * formulaeNode(const recipe *recipe, QTreeWidgetItem *parent)
Definition: CREUtils.cpp:106
ResourcesManager::archetypeModified
void archetypeModified(archetype *arch)
Definition: ResourcesManager.cpp:181
archt::clone
object clone
Definition: object.h:474
TREASURE_USE
@ TREASURE_USE
Definition: ResourcesManager.h:59
bufferreader_destroy
void bufferreader_destroy(BufferReader *br)
Definition: bufferreader.c:40
assets.h
Archetypes.h
ALCHEMY_PRODUCT
@ ALCHEMY_PRODUCT
Definition: ResourcesManager.h:62
ArchetypeLoader.h
CREUtils::treasureNode
static QTreeWidgetItem * treasureNode(QTreeWidgetItem *parent)
Definition: CREUtils.cpp:40
RANDOM_MAP_FINAL_EXIT
@ RANDOM_MAP_FINAL_EXIT
Definition: ResourcesManager.h:61
ArchetypePanel::setItem
virtual void setItem(archt *archetype) override
Definition: ArchetypePanel.cpp:39
CREMapInformationManager
Definition: CREMapInformationManager.h:17
BufferReader
Definition: bufferreader.c:21
OTHER_ARCH
@ OTHER_ARCH
Definition: ResourcesManager.h:57
treasureliststruct
Definition: treasure.h:82
archetype
struct archt archetype