1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
23 #include "parsetreemodel.h"
32 ParseTreeModel::ParseTreeModel(const char* document
, QObject
* parent
):
33 QAbstractItemModel(parent
)
35 this->tree
= skin_parse(document
);
38 this->root
= new ParseTreeNode(tree
);
42 scene
= new QGraphicsScene();
46 ParseTreeModel::~ParseTreeModel()
54 QString
ParseTreeModel::genCode()
57 return root
->genCode();
62 QString
ParseTreeModel::changeTree(const char *document
)
64 struct skin_element
* test
= skin_parse(document
);
68 QString error
= tr("Error on line ") +
69 QString::number(skin_error_line())
70 + tr(": ") + QString(skin_error_message());
74 ParseTreeNode
* temp
= new ParseTreeNode(test
);
75 if(root
&& temp
->genHash() == root
->genHash())
78 return tr("Document Parses Successfully");
83 emit
beginRemoveRows(QModelIndex(), 0, root
->numChildren() - 1);
90 emit
beginInsertRows(QModelIndex(), 0, temp
->numChildren() - 1);
93 return tr("Document Parses Successfully");
97 QModelIndex
ParseTreeModel::index(int row
, int column
,
98 const QModelIndex
& parent
) const
100 if(!hasIndex(row
, column
, parent
))
101 return QModelIndex();
103 ParseTreeNode
* foundParent
;
106 foundParent
= static_cast<ParseTreeNode
*>(parent
.internalPointer());
110 if(row
< foundParent
->numChildren() && row
>= 0)
111 return createIndex(row
, column
, foundParent
->child(row
));
113 return QModelIndex();
116 QModelIndex
ParseTreeModel::parent(const QModelIndex
&child
) const
119 return QModelIndex();
121 ParseTreeNode
* foundParent
= static_cast<ParseTreeNode
*>
122 (child
.internalPointer())->getParent();
124 if(foundParent
== root
)
125 return QModelIndex();
127 return createIndex(foundParent
->getRow(), 0, foundParent
);
130 int ParseTreeModel::rowCount(const QModelIndex
&parent
) const
135 if(!parent
.isValid())
136 return root
->numChildren();
138 if(parent
.column() != typeColumn
)
141 return static_cast<ParseTreeNode
*>(parent
.internalPointer())->numChildren();
144 int ParseTreeModel::columnCount(const QModelIndex
&parent
) const
152 QVariant
ParseTreeModel::data(const QModelIndex
&index
, int role
) const
157 if(role
!= Qt::DisplayRole
)
160 return static_cast<ParseTreeNode
*>(index
.internalPointer())->
161 data(index
.column());
164 QVariant
ParseTreeModel::headerData(int col
, Qt::Orientation orientation
,
167 if(orientation
!= Qt::Horizontal
)
170 if(col
>= numColumns
)
173 if(role
!= Qt::DisplayRole
)
179 return QObject::tr("Type");
182 return QObject::tr("Line");
185 return QObject::tr("Value");
191 Qt::ItemFlags
ParseTreeModel::flags(const QModelIndex
&index
) const
193 Qt::ItemFlags retval
= Qt::ItemIsEnabled
| Qt::ItemIsSelectable
;
195 ParseTreeNode
* element
= static_cast<ParseTreeNode
*>
196 (index
.internalPointer());
198 if((element
->isParam()
199 || element
->getElement()->type
== TEXT
200 || element
->getElement()->type
== COMMENT
)
201 && index
.column() == valueColumn
)
203 retval
|= Qt::ItemIsEditable
;
209 bool ParseTreeModel::setData(const QModelIndex
&index
, const QVariant
&value
,
212 if(role
!= Qt::EditRole
)
215 if(index
.column() != valueColumn
)
218 ParseTreeNode
* node
= static_cast<ParseTreeNode
*>
219 (index
.internalPointer());
223 struct skin_tag_parameter
* param
= node
->getParam();
225 /* Now that we've established that we do, in fact, have a parameter,
226 * set it to its new value if an acceptable one has been entered
228 if(value
.toString().trimmed() == QString(QChar(DEFAULTSYM
)))
230 if(islower(param
->type_code
))
231 param
->type
= skin_tag_parameter::DEFAULT
;
235 else if(tolower(param
->type_code
) == 's'
236 || tolower(param
->type_code
) == 'f')
238 if(param
->type
== skin_tag_parameter::STRING
)
239 free(param
->data
.text
);
241 param
->type
= skin_tag_parameter::STRING
;
242 param
->data
.text
= strdup(value
.toString().trimmed().toAscii());
244 else if(tolower(param
->type_code
) == 'i')
246 if(!value
.canConvert(QVariant::Int
))
249 param
->type
= skin_tag_parameter::NUMERIC
;
250 param
->data
.numeric
= value
.toInt();
259 struct skin_element
* element
= node
->getElement();
261 if(element
->type
!= COMMENT
&& element
->type
!= TEXT
)
265 element
->data
= strdup(value
.toString().trimmed().toAscii());
268 emit
dataChanged(index
, index
);
272 QGraphicsScene
* ParseTreeModel::render(ProjectModel
* project
)
276 /* Setting the background */
277 scene
->setBackgroundBrush(QBrush(QPixmap(":/render/scenebg.png")));
279 /* Adding the screen */
280 RBScreen
* screen
= new RBScreen(project
);
281 scene
->addItem(screen
);