OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
C4ConsoleQtDefinitionListViewer.cpp
Go to the documentation of this file.
1 /*
2 * OpenClonk, http://www.openclonk.org
3 *
4 * Copyright (c) 2001-2009, RedWolf Design GmbH, http://www.clonk.de/
5 * Copyright (c) 2013, The OpenClonk Team and contributors
6 *
7 * Distributed under the terms of the ISC license; see accompanying file
8 * "COPYING" for details.
9 *
10 * "Clonk" is a registered trademark of Matthes Bender, used with permission.
11 * See accompanying file "TRADEMARK" for details.
12 *
13 * To redistribute this file separately, substitute the full license texts
14 * for the above references.
15 */
16 
17 /* Proplist table view */
18 
19 #include "C4Include.h"
20 #include "script/C4Value.h"
21 #include "object/C4Def.h"
22 #include "object/C4DefList.h"
24 #include "editor/C4Console.h"
25 #include "editor/C4EditCursor.h"
26 
27 
28 /* Defintion tree */
29 
30 void C4ConsoleQtDefinitionListModel::DefListNode::SortByName()
31 {
32  // sort self
33  std::sort(items.begin(), items.end(),
34  [](const std::unique_ptr<DefListNode> & a, const std::unique_ptr<DefListNode> & b) -> bool
35  { return a->name.Compare(b->name) < 0; });
36  // sort children recursively
37  int32_t idx = 0;
38  for (auto & child : items)
39  {
40  child->SortByName();
41  child->idx = idx++; // re-assign indices to reflect new sorting
42  }
43 }
44 
45 /* Defintion view model */
46 
47 C4ConsoleQtDefinitionListModel::C4ConsoleQtDefinitionListModel()
48 {
49  // initial model data
50  ReInit();
51 }
52 
53 C4ConsoleQtDefinitionListModel::~C4ConsoleQtDefinitionListModel() = default;
54 
55 void C4ConsoleQtDefinitionListModel::EnsureInit()
56 {
57  // Init if not already done
58  if (!root.get() || root->items.empty())
59  if (::Definitions.GetDefCount())
60  ReInit();
61 }
62 
63 void C4ConsoleQtDefinitionListModel::ReInit()
64 {
65  // Re-fill definition model with all loaded definitions matching condition
66  // (TODO: Add conditional lists)
67  root = std::make_unique<C4ConsoleQtDefinitionListModel::DefListNode>();
68  int32_t index = 0; C4Def *def;
69  while ((def = ::Definitions.GetDef(index++)))
70  {
71  // Ignore hidden defs
72  if (def->HideInCreator) continue;
73  // Build path leading to this definition
74  DefListNode *node_parent = root.get();
75  StdCopyStrBuf fn(def->Filename), fn2;
77  for (;;)
78  {
79  bool is_parent_folder = fn.SplitAtChar(DirectorySeparator, &fn2);
80  if (!is_parent_folder || WildcardMatch(C4CFN_DefFiles, fn.getData())) // ignore non-.ocd-folders (except for final definition)
81  {
82  // Find if path is already there
83  RemoveExtension(&fn);
84  DefListNode *node_child = nullptr;
85  for (auto &test_node_child : node_parent->items)
86  if (test_node_child->filename == fn)
87  {
88  node_child = &*test_node_child;
89  break;
90  }
91  // If not, create it
92  if (!node_child)
93  {
94  node_parent->items.emplace_back((node_child = new DefListNode()));
95  node_child->idx = node_parent->items.size() - 1;
96  node_child->parent = node_parent;
97  node_child->name.Copy(fn);
98  node_child->filename.Copy(fn);
99  }
100  // And fill in node if this is not a parent folder
101  if (!is_parent_folder)
102  {
103  node_child->def = def;
104  const char *def_name = def->GetName();
105  if (def_name && *def_name) node_child->name.Copy(def_name);
106  break;
107  }
108  else
109  {
110  // Parent folder: Next path segment
111  node_parent = node_child;
112  }
113  }
114  fn = fn2;
115  }
116  }
117  // Descend into singleton root classes. I.e. if all elements are children of Objects/Items, move the root in there.
118  DefListNode *new_root = root.get();
119  while (new_root->items.size() == 1 && !new_root->items[0]->def)
120  {
121  std::unique_ptr<DefListNode> tmp(new_root->items[0].release());
122  root.reset(tmp.release());
123  new_root = root.get();
124  }
125  root->parent = nullptr;
126  // Copy group path names into definitions for later lookup by script
127  QStringList group_names;
128  DefListNode *node = root.get();
129  while (node)
130  {
131  if (node->def)
132  {
133  node->def->ConsoleGroupPath.Copy(group_names.join('/').toUtf8());
134  }
135  // Walk over tree. Remember groups in group_names string list.
136  if (!node->items.empty())
137  {
138  if (node != root.get()) group_names.append(node->name.getData());
139  node = node->items[0].get();
140  }
141  else
142  {
143  int32_t idx = node->idx + 1;
144  while ((node = node->parent))
145  {
146  if (node->items.size() > idx)
147  {
148  node = node->items[idx].get();
149  break;
150  }
151  if (group_names.size()) group_names.pop_back();
152  idx = node->idx + 1;
153  }
154  }
155  }
156  // Sort everything by display name (recursively)
157  root->SortByName();
158  // Model reset to invalidate all indexes
159  beginResetModel();
160  endResetModel();
161 }
162 
163 void C4ConsoleQtDefinitionListModel::OnItemRemoved(C4Def *p)
164 {
165  for (auto idx : this->persistentIndexList())
166  if (idx.internalPointer() == p)
167  this->changePersistentIndex(idx, QModelIndex());
168  ReInit();
169 }
170 
171 class C4Def *C4ConsoleQtDefinitionListModel::GetDefByModelIndex(const QModelIndex &idx)
172 {
173  DefListNode *node = static_cast<DefListNode *>(idx.internalPointer());
174  if (node) return node->def; else return nullptr;
175 }
176 
177 const char *C4ConsoleQtDefinitionListModel::GetNameByModelIndex(const QModelIndex &idx)
178 {
179  DefListNode *node = static_cast<DefListNode *>(idx.internalPointer());
180  if (node) return node->name.getData(); else return nullptr;
181 }
182 
183 int C4ConsoleQtDefinitionListModel::rowCount(const QModelIndex & parent) const
184 {
185  int result = 0;
186  DefListNode *parent_node = parent.isValid() ? static_cast<DefListNode *>(parent.internalPointer()) : nullptr;
187  if (!parent_node) parent_node = root.get();
188  if (parent_node) result = parent_node->items.size();
189  return result;
190 }
191 
192 int C4ConsoleQtDefinitionListModel::columnCount(const QModelIndex & parent) const
193 {
194  return 1; // Name only
195 }
196 
197 QVariant C4ConsoleQtDefinitionListModel::data(const QModelIndex & index, int role) const
198 {
199  // Object list lookup is done in index(). Here we just use the pointer.
200  DefListNode *node = static_cast<DefListNode *>(index.internalPointer());
201  if (!node) return QVariant();
202  if (role == Qt::DisplayRole)
203  {
204  return QString(node->name.getData());
205  }
206  // Nothing to show
207  return QVariant();
208 }
209 
210 QModelIndex C4ConsoleQtDefinitionListModel::index(int row, int column, const QModelIndex &parent) const
211 {
212  // Current index out of range?
213  if (row < 0 || column != 0) return QModelIndex();
214  DefListNode *parent_node = parent.isValid() ? static_cast<DefListNode *>(parent.internalPointer()) : nullptr;
215  if (!parent_node) parent_node = root.get();
216  if (parent_node->items.size() <= row) return QModelIndex();
217  // Index into tree
218  DefListNode *node = parent_node->items[row].get();
219  return createIndex(row, column, node);
220 }
221 
222 QModelIndex C4ConsoleQtDefinitionListModel::parent(const QModelIndex &index) const
223 {
224  // Find parent through tree
225  DefListNode *node = static_cast<DefListNode *>(index.internalPointer());
226  if (!node) return QModelIndex();
227  DefListNode *parent_node = node->parent;
228  if (!parent_node || parent_node == root.get()) return QModelIndex();
229  return createIndex(parent_node->idx, index.column(), parent_node);
230 }
231 
232 QModelIndex C4ConsoleQtDefinitionListModel::GetModelIndexByItem(C4Def *def) const
233 {
234  // Just search tree
235  DefListNode *node = root.get();
236  while (node)
237  {
238  if (node->def == def) break;
239  if (!node->items.empty())
240  node = node->items[0].get();
241  else
242  {
243  int32_t idx = node->idx + 1;
244  while ((node = node->parent))
245  {
246  if (node->items.size() > idx)
247  {
248  node = node->items[idx].get();
249  break;
250  }
251  idx = node->idx + 1;
252  }
253  }
254  }
255  // Def found in tree?
256  if (node)
257  {
258  return createIndex(node->idx, 0, node);
259  }
260  else
261  {
262  return QModelIndex();
263  }
264 }
C4Def * GetDef(int32_t Index)
#define b
int32_t GetDefCount()
#define a
const char * GetName() const override
Definition: C4PropList.cpp:267
#define AltDirectorySeparator
bool HideInCreator
Definition: C4Def.h:149
Definition: C4Def.h:98
void RemoveExtension(char *szFilename)
Definition: StdFile.cpp:306
int ReplaceChar(char cOld, char cNew)
Definition: StdBuf.cpp:336
bool WildcardMatch(const char *szWildcard, const char *szString)
Definition: StdFile.cpp:374
char Filename[_MAX_FNAME+1]
Definition: C4Def.h:177
#define C4CFN_DefFiles
Definition: C4Components.h:166
C4DefList Definitions
Definition: C4Globals.cpp:49
#define DirectorySeparator
void Copy()
Definition: StdBuf.h:467