Theme Editor: Removed the NEWLINE parse tree element
[kugel-rb.git] / utils / themeeditor / parsetreenode.cpp
blob70124c338dedc4c71457734b8344c4ef405bc3d0
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2010 Robert Bieber
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #include "symbols.h"
23 #include "tag_table.h"
25 #include "parsetreenode.h"
26 #include "parsetreemodel.h"
28 /* Root element constructor */
29 ParseTreeNode::ParseTreeNode(struct skin_element* data)
30 : parent(0), element(0), param(0), children()
32 while(data)
34 children.append(new ParseTreeNode(data, this));
35 data = data->next;
39 /* Normal element constructor */
40 ParseTreeNode::ParseTreeNode(struct skin_element* data, ParseTreeNode* parent)
41 : parent(parent), element(data), param(0), children()
43 switch(element->type)
46 case TAG:
47 for(int i = 0; i < element->params_count; i++)
49 if(element->params[i].type == skin_tag_parameter::CODE)
50 children.append(new ParseTreeNode(element->params[i].data.code,
51 this));
52 else
53 children.append(new ParseTreeNode(&element->params[i], this));
55 break;
57 /* CONDITIONAL and SUBLINES fall through to the same code */
58 case CONDITIONAL:
59 case SUBLINES:
60 for(int i = 0; i < element->children_count; i++)
62 children.append(new ParseTreeNode(data->children[i], this));
64 break;
66 case VIEWPORT:
67 case LINE:
68 for(int i = 0; i < data->children_count; i++)
70 for(struct skin_element* current = data->children[i]; current;
71 current = current->next)
73 children.append(new ParseTreeNode(current, this));
76 break;
78 default:
79 break;
83 /* Parameter constructor */
84 ParseTreeNode::ParseTreeNode(skin_tag_parameter *data, ParseTreeNode *parent)
85 : parent(parent), element(0), param(data), children()
90 QString ParseTreeNode::genCode() const
92 QString buffer = "";
94 if(element)
96 switch(element->type)
99 case VIEWPORT:
100 case LINE:
101 for(int i = 0; i < children.count(); i++)
104 Adding a % in case of tag, because the tag rendering code
105 doesn't insert its own
107 if(children[i]->element->type == TAG)
108 buffer.append(TAGSYM);
109 buffer.append(children[i]->genCode());
110 if(element->type == LINE || i == 0)
111 buffer.append('\n');
113 break;
115 case SUBLINES:
116 for(int i = 0; i < children.count(); i++)
118 buffer.append(children[i]->genCode());
119 if(i != children.count() - 1)
120 buffer.append(MULTILINESYM);
122 buffer.append('\n');
123 break;
125 case CONDITIONAL:
126 /* Inserts a %?, the tag renderer doesn't deal with the TAGSYM */
127 buffer.append(TAGSYM);
128 buffer.append(CONDITIONSYM);
129 buffer.append(children[0]->genCode());
131 /* Inserting the sublines */
132 buffer.append(ENUMLISTOPENSYM);
133 for(int i = 1; i < children.count(); i++)
135 buffer.append(children[i]->genCode());
136 if(i != children.count() - 1)
137 buffer.append(ENUMLISTSEPERATESYM);
139 buffer.append(ENUMLISTCLOSESYM);
140 break;
142 case TAG:
143 /* When generating code, we DO NOT insert the leading TAGSYM, leave
144 * the calling functions to handle that
146 buffer.append(element->tag->name);
148 if(element->params_count > 0)
150 /* Rendering parameters if there are any */
151 buffer.append(ARGLISTOPENSYM);
152 for(int i = 0; i < children.count(); i++)
154 buffer.append(children[i]->genCode());
155 if(i != children.count() - 1)
156 buffer.append(ARGLISTSEPERATESYM);
158 buffer.append(ARGLISTCLOSESYM);
160 break;
162 case TEXT:
163 for(char* cursor = element->text; *cursor; cursor++)
165 if(find_escape_character(*cursor))
166 buffer.append(TAGSYM);
167 buffer.append(*cursor);
169 break;
171 case COMMENT:
172 buffer.append(COMMENTSYM);
173 buffer.append(element->text);
174 buffer.append('\n');
175 break;
178 else if(param)
180 switch(param->type)
182 case skin_tag_parameter::STRING:
183 for(char* cursor = param->data.text; *cursor; cursor++)
185 if(find_escape_character(*cursor))
186 buffer.append(TAGSYM);
187 buffer.append(*cursor);
189 break;
191 case skin_tag_parameter::NUMERIC:
192 buffer.append(QString::number(param->data.numeric, 10));
193 break;
195 case skin_tag_parameter::DEFAULT:
196 buffer.append(DEFAULTSYM);
197 break;
199 case skin_tag_parameter::CODE:
200 buffer.append(QObject::tr("This doesn't belong here"));
201 break;
205 else
207 for(int i = 0; i < children.count(); i++)
208 buffer.append(children[i]->genCode());
211 return buffer;
214 ParseTreeNode* ParseTreeNode::child(int row)
216 if(row < 0 || row >= children.count())
217 return 0;
219 return children[row];
222 int ParseTreeNode::numChildren() const
224 return children.count();
228 QVariant ParseTreeNode::data(int column) const
230 switch(column)
232 case ParseTreeModel::typeColumn:
233 if(element)
235 switch(element->type)
237 case VIEWPORT:
238 return QObject::tr("Viewport");
240 case LINE:
241 return QObject::tr("Logical Line");
243 case SUBLINES:
244 return QObject::tr("Alternator");
246 case COMMENT:
247 return QObject::tr("Comment");
249 case CONDITIONAL:
250 return QObject::tr("Conditional Tag");
252 case TAG:
253 return QObject::tr("Tag");
255 case TEXT:
256 return QObject::tr("Plaintext");
259 else if(param)
261 switch(param->type)
263 case skin_tag_parameter::STRING:
264 return QObject::tr("String");
266 case skin_tag_parameter::NUMERIC:
267 return QObject::tr("Number");
269 case skin_tag_parameter::DEFAULT:
270 return QObject::tr("Default Argument");
272 case skin_tag_parameter::CODE:
273 return QObject::tr("This doesn't belong here");
276 else
278 return QObject::tr("Root");
281 break;
283 case ParseTreeModel::valueColumn:
284 if(element)
286 switch(element->type)
288 case VIEWPORT:
289 case LINE:
290 case SUBLINES:
291 case CONDITIONAL:
292 return QString();
294 case TEXT:
295 case COMMENT:
296 return QString(element->text);
298 case TAG:
299 return QString(element->tag->name);
302 else if(param)
304 switch(param->type)
306 case skin_tag_parameter::DEFAULT:
307 return QObject::tr("-");
309 case skin_tag_parameter::STRING:
310 return QString(param->data.text);
312 case skin_tag_parameter::NUMERIC:
313 return QString::number(param->data.numeric, 10);
315 case skin_tag_parameter::CODE:
316 return QObject::tr("Seriously, something's wrong here");
319 else
321 return QString();
323 break;
325 case ParseTreeModel::lineColumn:
326 if(element)
327 return QString::number(element->line, 10);
328 else
329 return QString();
330 break;
333 return QVariant();
337 int ParseTreeNode::getRow() const
339 if(!parent)
340 return -1;
342 return parent->children.indexOf(const_cast<ParseTreeNode*>(this));
345 ParseTreeNode* ParseTreeNode::getParent() const
347 return parent;
350 ParseTreeNode::~ParseTreeNode()
352 for(int i = 0; i < children.count(); i++)
353 delete children[i];