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
);
34 this->root
= new ParseTreeNode(tree
);
38 ParseTreeModel::~ParseTreeModel()
46 QString
ParseTreeModel::genCode()
48 return root
->genCode();
51 QModelIndex
ParseTreeModel::index(int row
, int column
,
52 const QModelIndex
& parent
) const
54 if(!hasIndex(row
, column
, parent
))
57 ParseTreeNode
* foundParent
;
60 foundParent
= static_cast<ParseTreeNode
*>(parent
.internalPointer());
64 if(row
< foundParent
->numChildren() && row
>= 0)
65 return createIndex(row
, column
, foundParent
->child(row
));
70 QModelIndex
ParseTreeModel::parent(const QModelIndex
&child
) const
75 ParseTreeNode
* foundParent
= static_cast<ParseTreeNode
*>
76 (child
.internalPointer())->getParent();
78 if(foundParent
== root
)
81 return createIndex(foundParent
->getRow(), 0, foundParent
);
84 int ParseTreeModel::rowCount(const QModelIndex
&parent
) const
87 return root
->numChildren();
89 if(parent
.column() != typeColumn
)
92 return static_cast<ParseTreeNode
*>(parent
.internalPointer())->numChildren();
95 int ParseTreeModel::columnCount(const QModelIndex
&parent
) const
100 QVariant
ParseTreeModel::data(const QModelIndex
&index
, int role
) const
105 if(role
!= Qt::DisplayRole
)
108 return static_cast<ParseTreeNode
*>(index
.internalPointer())->
109 data(index
.column());
112 QVariant
ParseTreeModel::headerData(int col
, Qt::Orientation orientation
,
115 if(orientation
!= Qt::Horizontal
)
118 if(col
>= numColumns
)
121 if(role
!= Qt::DisplayRole
)
127 return QObject::tr("Type");
130 return QObject::tr("Line");
133 return QObject::tr("Value");
139 Qt::ItemFlags
ParseTreeModel::flags(const QModelIndex
&index
) const
141 Qt::ItemFlags retval
= Qt::ItemIsEnabled
| Qt::ItemIsSelectable
;
143 ParseTreeNode
* element
= static_cast<ParseTreeNode
*>
144 (index
.internalPointer());
146 if((element
->isParam()
147 || element
->getElement()->type
== TEXT
148 || element
->getElement()->type
== COMMENT
)
149 && index
.column() == valueColumn
)
151 retval
|= Qt::ItemIsEditable
;
157 bool ParseTreeModel::setData(const QModelIndex
&index
, const QVariant
&value
,
160 if(role
!= Qt::EditRole
)
163 if(index
.column() != valueColumn
)
166 ParseTreeNode
* node
= static_cast<ParseTreeNode
*>
167 (index
.internalPointer());
171 struct skin_tag_parameter
* param
= node
->getParam();
173 /* Now that we've established that we do, in fact, have a parameter,
174 * set it to its new value if an acceptable one has been entered
176 if(value
.toString().trimmed() == QString(QChar(DEFAULTSYM
)))
178 if(islower(param
->type_code
))
179 param
->type
= skin_tag_parameter::DEFAULT
;
183 else if(tolower(param
->type_code
) == 's'
184 || tolower(param
->type_code
) == 'f')
186 if(param
->type
== skin_tag_parameter::STRING
)
187 free(param
->data
.text
);
189 param
->type
= skin_tag_parameter::STRING
;
190 param
->data
.text
= strdup(value
.toString().trimmed().toAscii());
192 else if(tolower(param
->type_code
) == 'i')
194 if(!value
.canConvert(QVariant::Int
))
197 param
->type
= skin_tag_parameter::NUMERIC
;
198 param
->data
.numeric
= value
.toInt();
207 struct skin_element
* element
= node
->getElement();
209 if(element
->type
!= COMMENT
&& element
->type
!= TEXT
)
213 element
->text
= strdup(value
.toString().trimmed().toAscii());
216 emit
dataChanged(index
, index
);