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"
30 ParseTreeModel::ParseTreeModel(const char* document
, QObject
* parent
):
31 QAbstractItemModel(parent
)
33 this->tree
= skin_parse(document
);
36 this->root
= new ParseTreeNode(tree
);
42 ParseTreeModel::~ParseTreeModel()
50 QString
ParseTreeModel::genCode()
53 return root
->genCode();
58 QString
ParseTreeModel::changeTree(const char *document
)
60 struct skin_element
* test
= skin_parse(document
);
64 QString error
= tr("Error on line ") +
65 QString::number(skin_error_line())
66 + tr(": ") + QString(skin_error_message());
70 ParseTreeNode
* temp
= new ParseTreeNode(test
);
71 if(root
&& temp
->genHash() == root
->genHash())
74 return tr("Document Parses Successfully");
79 emit
beginRemoveRows(QModelIndex(), 0, root
->numChildren() - 1);
86 emit
beginInsertRows(QModelIndex(), 0, temp
->numChildren() - 1);
89 return tr("Document Parses Successfully");
93 QModelIndex
ParseTreeModel::index(int row
, int column
,
94 const QModelIndex
& parent
) const
96 if(!hasIndex(row
, column
, parent
))
99 ParseTreeNode
* foundParent
;
102 foundParent
= static_cast<ParseTreeNode
*>(parent
.internalPointer());
106 if(row
< foundParent
->numChildren() && row
>= 0)
107 return createIndex(row
, column
, foundParent
->child(row
));
109 return QModelIndex();
112 QModelIndex
ParseTreeModel::parent(const QModelIndex
&child
) const
115 return QModelIndex();
117 ParseTreeNode
* foundParent
= static_cast<ParseTreeNode
*>
118 (child
.internalPointer())->getParent();
120 if(foundParent
== root
)
121 return QModelIndex();
123 return createIndex(foundParent
->getRow(), 0, foundParent
);
126 int ParseTreeModel::rowCount(const QModelIndex
&parent
) const
131 if(!parent
.isValid())
132 return root
->numChildren();
134 if(parent
.column() != typeColumn
)
137 return static_cast<ParseTreeNode
*>(parent
.internalPointer())->numChildren();
140 int ParseTreeModel::columnCount(const QModelIndex
&parent
) const
148 QVariant
ParseTreeModel::data(const QModelIndex
&index
, int role
) const
153 if(role
!= Qt::DisplayRole
)
156 return static_cast<ParseTreeNode
*>(index
.internalPointer())->
157 data(index
.column());
160 QVariant
ParseTreeModel::headerData(int col
, Qt::Orientation orientation
,
163 if(orientation
!= Qt::Horizontal
)
166 if(col
>= numColumns
)
169 if(role
!= Qt::DisplayRole
)
175 return QObject::tr("Type");
178 return QObject::tr("Line");
181 return QObject::tr("Value");
187 Qt::ItemFlags
ParseTreeModel::flags(const QModelIndex
&index
) const
189 Qt::ItemFlags retval
= Qt::ItemIsEnabled
| Qt::ItemIsSelectable
;
191 ParseTreeNode
* element
= static_cast<ParseTreeNode
*>
192 (index
.internalPointer());
194 if((element
->isParam()
195 || element
->getElement()->type
== TEXT
196 || element
->getElement()->type
== COMMENT
)
197 && index
.column() == valueColumn
)
199 retval
|= Qt::ItemIsEditable
;
205 bool ParseTreeModel::setData(const QModelIndex
&index
, const QVariant
&value
,
208 if(role
!= Qt::EditRole
)
211 if(index
.column() != valueColumn
)
214 ParseTreeNode
* node
= static_cast<ParseTreeNode
*>
215 (index
.internalPointer());
219 struct skin_tag_parameter
* param
= node
->getParam();
221 /* Now that we've established that we do, in fact, have a parameter,
222 * set it to its new value if an acceptable one has been entered
224 if(value
.toString().trimmed() == QString(QChar(DEFAULTSYM
)))
226 if(islower(param
->type_code
))
227 param
->type
= skin_tag_parameter::DEFAULT
;
231 else if(tolower(param
->type_code
) == 's'
232 || tolower(param
->type_code
) == 'f')
234 if(param
->type
== skin_tag_parameter::STRING
)
235 free(param
->data
.text
);
237 param
->type
= skin_tag_parameter::STRING
;
238 param
->data
.text
= strdup(value
.toString().trimmed().toAscii());
240 else if(tolower(param
->type_code
) == 'i')
242 if(!value
.canConvert(QVariant::Int
))
245 param
->type
= skin_tag_parameter::NUMERIC
;
246 param
->data
.numeric
= value
.toInt();
255 struct skin_element
* element
= node
->getElement();
257 if(element
->type
!= COMMENT
&& element
->type
!= TEXT
)
261 element
->text
= strdup(value
.toString().trimmed().toAscii());
264 emit
dataChanged(index
, index
);