Debian Codegraph @

src/GUI/property_list.cxx

raw symbols SCIP
1 /*
2 * SPDX-FileName: property_list.cxx
3 * SPDX-FileComment: Implementation of the <property-list> widget.
4 * SPDX-FileCopyrightText: Copyright (C) 2001 Steve BAKER
5 * SPDX-FileCopyrightText: Copyright (C) 2001 Jim WILSON
6 * SPDX-FileCopyrightText: Copyright (C) 2006 Melchior FRANZ
7 * SPDX-License-Identifier: GPL-2.0-or-later
8 */
9
10 #include <config.h>
11
12 #include <simgear/compiler.h>
13
14 #include <sstream>
15 #include <iomanip>
16 #include <iostream>
17 #include <string>
18
19 using std::cout;
20 using std::endl;
21 using std::string;
22
23 #include <Main/fg_os.hxx> // fgGetKeyModifiers()
24 #include <Main/fg_props.hxx>
25
26 #include "property_list.hxx"
27
28
29 static string getValueTypeString(const SGPropertyNode *node)
30 {
31 using namespace simgear;
32 string result;
33
34 props::Type type = node->getType();
35 if (type == props::UNSPECIFIED)
36 result = "unspecified";
37 else if (type == props::NONE)
38 result = "none";
39 else if (type == props::BOOL)
40 result = "bool";
41 else if (type == props::INT)
42 result = "int";
43 else if (type == props::LONG)
44 result = "long";
45 else if (type == props::FLOAT)
46 result = "float";
47 else if (type == props::DOUBLE)
48 result = "double";
49 else if (type == props::STRING)
50 result = "string";
51 else if (type == props::VEC3D)
52 result = "vec3d";
53 else if (type == props::VEC4D)
54 result = "vec4d";
55
56 return result;
57 }
58
59
60 static void dumpProperties(const SGPropertyNode *node)
61 {
62 using namespace simgear;
63 cout << node->getPath() << '/' << endl;
64 for (int i = 0; i < node->nChildren(); i++) {
65 const SGPropertyNode *c = node->getChild(i);
66 if (c->nChildren()) {
67 cout << std::setw(11) << "<dir>" << " " << c->getNameString() << endl;
68 continue;
69 }
70
71
72 int index = c->getIndex();
73 cout << std::setw(11) << getValueTypeString(c) << " " << c->getNameString();
74 if (index > 0)
75 cout << '[' << index << ']';
76 cout << " = ";
77
78 switch (c->getType()) {
79 case props::DOUBLE:
80 case props::FLOAT:
81 case props::VEC3D:
82 case props::VEC4D:
83 {
84 std::streamsize precision = cout.precision(15);
85 c->printOn(cout);
86 cout.precision(precision);
87 }
88 break;
89 case props::LONG:
90 case props::INT:
91 case props::BOOL:
92 c->printOn(cout);
93 break;
94 case props::STRING:
95 cout << '"' << c->getStringValue() << '"';
96 break;
97 case props::NONE:
98 break;
99 default:
100 cout << '\'' << c->getStringValue() << '\'';
101 }
102 if (c->isAlias()) {
103 cout << " => " << c->getAliasTarget()->getPath();
104 }
105 cout << endl;
106 }
107 cout << endl;
108 }
109
110
111 static void sanitize(std::string& s)
112 {
113 std::string r = s;
114 s = "";
115 for (unsigned i = 0; i < r.size(); i++) {
116 if (r[i] == '\a')
117 s += "\\a";
118 else if (r[i] == '\b')
119 s += "\\b";
120 else if (r[i] == '\t')
121 s += "\\t";
122 else if (r[i] == '\n')
123 s += "\\n";
124 else if (r[i] == '\v')
125 s += "\\v";
126 else if (r[i] == '\f')
127 s += "\\f";
128 else if (r[i] == '\r')
129 s += "\\r";
130 else if (r[i] == '\'')
131 s += "\'";
132 else if (r[i] == '\\')
133 s += "\\";
134 else if (isprint(r[i]))
135 s += r[i];
136 else {
137 const char *hex = "0123456789abcdef";
138 int c = r[i] & 255;
139 s += std::string("\\x") + hex[c / 16] + hex[c % 16];
140 }
141 }
142 }
143
144
145 PropertyList::PropertyList(int minx, int miny, int maxx, int maxy, SGPropertyNode *start, bool readonly) :
146 puaList(minx, miny, maxx, maxy, short(0), 20),
147 GUI_ID(FGCLASS_PROPERTYLIST),
148 _curr(start),
149 _return(0),
150 _entries(0),
151 _num_entries(0),
152 _verbose(false),
153 _readonly(readonly)
154 {
155 _list_box->setUserData(this);
156 if (!_readonly) _list_box->setCallback(handle_select);
157 _list_box->setValue(0);
158 update();
159 }
160
161
162 PropertyList::~PropertyList()
163 {
164 delete_arrays();
165 }
166
167
168 void PropertyList::delete_arrays()
169 {
170 if (!_entries)
171 return;
172
173 for (int i = 0; i < _num_entries; i++)
174 delete[] _entries[i];
175
176 delete[] _entries;
177 delete[] _children;
178 _entries = 0;
179 _children = 0;
180 }
181
182
183 void PropertyList::handle_select(puObject *list_box)
184 {
185 PropertyList *prop_list = (PropertyList *)list_box->getUserData();
186 int selected = list_box->getIntegerValue();
187 const auto mod_ctrl = fgGetKeyModifiers() & KEYMOD_CTRL;
188 const auto mod_shift = fgGetKeyModifiers() & KEYMOD_SHIFT;
189 const auto mod_alt = fgGetKeyModifiers() & KEYMOD_ALT;
190
191 if (selected >= 0 && selected < prop_list->_num_entries) {
192 const char *src = prop_list->_entries[selected];
193
194 if (prop_list->_dot_files && (selected < 2)) {
195 if (src[0] == '.' && (src[1] == '\0' || src[1] == ' ')) {
196 if (mod_ctrl && mod_shift)
197 prop_list->_curr->fireValueChanged();
198 else if (mod_ctrl)
199 prop_list->toggleVerbosity();
200 else if (mod_shift)
201 dumpProperties(prop_list->_curr);
202
203 prop_list->update();
204 return;
205
206 } else if (!strcmp(src, "..")) {
207 SGPropertyNode *parent = prop_list->getCurrent()->getParent();
208 if (parent) {
209 if (mod_ctrl)
210 for (; parent->getParent(); parent = parent->getParent())
211 ;
212 prop_list->setCurrent(parent);
213 }
214 return;
215 }
216 }
217
218 // we know we're dealing with a regular entry, so convert
219 // it to an index into children[]
220 if (prop_list->_dot_files)
221 selected -= 2;
222
223 SGPropertyNode_ptr child = prop_list->_children[selected].node;
224
225 // check if it's a directory
226 if (child->nChildren()) {
227 prop_list->setTopItem(0);
228 prop_list->setCurrent(child);
229 return;
230 }
231
232 // it is a regular property
233 if (child->getType() == simgear::props::BOOL && mod_ctrl && !mod_shift && !mod_alt) {
234 child->setBoolValue(!child->getBoolValue());
235 prop_list->update(true);
236 } else if (mod_alt && mod_ctrl) {
237 child->setAttribute(SGPropertyNode::TRACE_READ,
238 !child->getAttribute(SGPropertyNode::TRACE_READ));
239 } else if (mod_alt) {
240 child->setAttribute(SGPropertyNode::TRACE_WRITE,
241 !child->getAttribute(SGPropertyNode::TRACE_WRITE));
242 } else
243 prop_list->publish(child);
244
245 } else {
246 // the user clicked on blank screen
247 prop_list->update(true);
248 }
249 }
250
251
252 void PropertyList::update(bool restore_pos)
253 {
254 int pi;
255
256 delete_arrays();
257 _num_entries = (int)_curr->nChildren();
258
259 // instantiate string objects and add [.] and [..] for subdirs
260 if (_readonly || !_curr->getParent()) {
261 _entries = new char*[_num_entries + 1];
262 pi = 0;
263 _dot_files = false;
264
265 } else {
266 _num_entries += 2; // for . and ..
267 _entries = new char*[_num_entries + 1];
268
269 _entries[0] = new char[16];
270 strcpy(_entries[0], _verbose ? ". [verbose]" : ".");
271
272 _entries[1] = new char[3];
273 strcpy(_entries[1], "..");
274
275 pi = 2;
276 _dot_files = true;
277 }
278
279 int i;
280 _num_children = _curr->nChildren();
281 _children = new NodeData[_num_children];
282 for (i = 0; i < _num_children; i++)
283 _children[i].node = _curr->getChild(i);
284
285 qsort(_children, _num_children, sizeof(_children[0]), nodeNameCompare);
286
287 // Make lists of the children's names, values, etc.
288 for (i = 0; i < _num_children; i++, pi++) {
289 _children[i].text = &_entries[pi];
290 _entries[pi] = 0; // make it deletable
291 updateTextForEntry(_children[i]);
292 _children[i].setListener(this);
293 }
294
295 _entries[_num_entries] = 0;
296
297 int top = getTopItem();
298 newList(_entries);
299 if (restore_pos)
300 setTopItem(top);
301 }
302
303
304 void PropertyList::updateTextForEntry(NodeData& data)
305 {
306 using namespace simgear;
307 SGPropertyNode *node = data.node;
308 auto name = node->getDisplayName(true);
309 auto type = getValueTypeString(node);
310 auto value = node->getStringValue();
311
312 std::ostringstream line;
313 line << name;
314
315 int children = node->nChildren();
316 if (children) {
317 line << '/';
318 if (_verbose) {
319 const SGPropertyNode* id = node->getChild("id");
320 const SGPropertyNode* name = node->getChild("name");
321 const SGPropertyNode* desc = node->getChild("desc");
322 if (id || name || desc) {
323 line << " (";
324 if (id) {
325 line << "id: " << id->getStringValue();
326 if (name != NULL)
327 line << ", ";
328 } else if (name) {
329 line << "name: " << name->getStringValue();
330 } else if (desc) {
331 line << "desc: " << desc->getStringValue();
332 }
333 line << ")";
334 }
335 }
336 }
337
338 if (node->hasValue()) {
339 if (node->getType() == props::STRING
340 || node->getType() == props::UNSPECIFIED)
341 sanitize(value);
342
343 line << " = '" << value << "' (" << type;
344
345 if (_verbose) {
346 std::string ext;
347 if (!node->getAttribute(SGPropertyNode::READ))
348 ext += 'r';
349 if (!node->getAttribute(SGPropertyNode::WRITE))
350 ext += 'w';
351 if (node->getAttribute(SGPropertyNode::TRACE_READ))
352 ext += 'R';
353 if (node->getAttribute(SGPropertyNode::TRACE_WRITE))
354 ext += 'W';
355 if (node->getAttribute(SGPropertyNode::ARCHIVE))
356 ext += 'A';
357 if (node->getAttribute(SGPropertyNode::USERARCHIVE))
358 ext += 'U';
359 if (node->getAttribute(SGPropertyNode::PRESERVE))
360 ext += 'P';
361 if (node->isTied())
362 ext += 'T';
363
364 if (!ext.empty())
365 line << ", " << ext;
366
367 int num = node->nListeners();
368 if (data.listener)
369 num--;
370 if (num)
371 line << ", L" << num;
372 }
373 line << ')';
374 }
375 else
376 if ((_verbose)&&(node->getAttribute(SGPropertyNode::PRESERVE)))
377 {
378 // only preserve/protection flag matters for nodes without values
379 line << " (P)";
380 }
381
382 if (_verbose && node->isAlias()) {
383 line << " => " << node->getAliasTarget()->getPath();
384 }
385
386 auto out = line.str();
387 if (out.size() >= PUSTRING_MAX)
388 out.resize(PUSTRING_MAX - 1);
389
390 delete[] *data.text;
391 *data.text = new char[out.size() + 1];
392 strcpy(*data.text, out.c_str());
393 }
394
395
396 void PropertyList::valueChanged(SGPropertyNode *nd)
397 {
398 for (int i = 0; i < _num_children; i++)
399 if (_children[i].node == nd) {
400 updateTextForEntry(_children[i]);
401 return;
402 }
403 }
404
405
406 int PropertyList::nodeNameCompare(const void *p1, const void *p2)
407 {
408 const SGPropertyNode *n1 = (*(const NodeData *)p1).node;
409 const SGPropertyNode *n2 = (*(const NodeData *)p2).node;
410
411 int diff = strcmp(n1->getNameString().c_str(), n2->getNameString().c_str());
412 return diff ? diff : n1->getIndex() - n2->getIndex();
413 }
414
415
416 void PropertyList::setValue(const char *s)
417 {
418 try {
419 SGPropertyNode *p = fgGetNode(s, false);
420 if (p)
421 setCurrent(p);
422 else
423 throw std::runtime_error("node doesn't exist");
424 } catch (const std::exception& m) {
425 SG_LOG(SG_GENERAL, SG_DEBUG, "property-list node '" << s << "': " << m.what());
426 }
427 }
428
429
430 void PropertyList::setCurrent(SGPropertyNode *p)
431 {
432 bool same = (_curr == p);
433 _return = _curr = p;
434 update(same);
435 if (!same)
436 publish(p);
437 }
438