Theme Editor: Enabled editing tag parameters from a treeview
[kugel-rb.git] / utils / themeeditor / parsetreemodel.cpp
bloba0e3abb03b10d4063e982e2fc4425618f2b79ac6
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 ****************************************************************************/
23 #include "parsetreemodel.h"
24 #include "symbols.h"
26 #include <cstdlib>
28 #include <QObject>
30 ParseTreeModel::ParseTreeModel(char* document, QObject* parent):
31 QAbstractItemModel(parent)
33 this->tree = skin_parse(document);
34 this->root = new ParseTreeNode(tree);
38 ParseTreeModel::~ParseTreeModel()
40 if(root)
41 delete root;
42 if(tree)
43 skin_free_tree(tree);
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))
55 return QModelIndex();
57 ParseTreeNode* foundParent;
59 if(parent.isValid())
60 foundParent = static_cast<ParseTreeNode*>(parent.internalPointer());
61 else
62 foundParent = root;
64 if(row < foundParent->numChildren() && row >= 0)
65 return createIndex(row, column, foundParent->child(row));
66 else
67 return QModelIndex();
70 QModelIndex ParseTreeModel::parent(const QModelIndex &child) const
72 if(!child.isValid())
73 return QModelIndex();
75 ParseTreeNode* foundParent = static_cast<ParseTreeNode*>
76 (child.internalPointer())->getParent();
78 if(foundParent == root)
79 return QModelIndex();
81 return createIndex(foundParent->getRow(), 0, foundParent);
84 int ParseTreeModel::rowCount(const QModelIndex &parent) const
86 if(!parent.isValid())
87 return root->numChildren();
89 if(parent.column() != typeColumn)
90 return 0;
92 return static_cast<ParseTreeNode*>(parent.internalPointer())->numChildren();
95 int ParseTreeModel::columnCount(const QModelIndex &parent) const
97 return numColumns;
100 QVariant ParseTreeModel::data(const QModelIndex &index, int role) const
102 if(!index.isValid())
103 return QVariant();
105 if(role != Qt::DisplayRole)
106 return QVariant();
108 return static_cast<ParseTreeNode*>(index.internalPointer())->
109 data(index.column());
112 QVariant ParseTreeModel::headerData(int col, Qt::Orientation orientation,
113 int role) const
115 if(orientation != Qt::Horizontal)
116 return QVariant();
118 if(col >= numColumns)
119 return QVariant();
121 if(role != Qt::DisplayRole)
122 return QVariant();
124 switch(col)
126 case typeColumn:
127 return QObject::tr("Type");
129 case lineColumn:
130 return QObject::tr("Line");
132 case valueColumn:
133 return QObject::tr("Value");
136 return QVariant();
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());
145 if(element->isParam() && index.column() == valueColumn)
146 retval |= Qt::ItemIsEditable;
148 return retval;
151 bool ParseTreeModel::setData(const QModelIndex &index, const QVariant &value,
152 int role)
154 if(role != Qt::EditRole)
155 return false;
157 if(index.column() != valueColumn)
158 return false;
160 ParseTreeNode* element = static_cast<ParseTreeNode*>
161 (index.internalPointer());
163 if(!element->isParam())
164 return false;
166 struct skin_tag_parameter* param = element->getParam();
168 /* Now that we've established that we do, in fact, have a parameter, we'll
169 * set it to its new value if an acceptable one has been entered
171 if(value.toString().trimmed() == QString(QChar(DEFAULTSYM)))
173 if(islower(param->type_code))
174 param->type = skin_tag_parameter::DEFAULT;
175 else
176 return false;
178 else if(tolower(param->type_code) == 's' || tolower(param->type_code) == 'f')
180 if(param->type == skin_tag_parameter::STRING)
181 free(param->data.text);
183 param->type = skin_tag_parameter::STRING;
184 param->data.text = strdup(value.toString().trimmed().toAscii());
186 else if(tolower(param->type_code) == 'i')
188 if(!value.canConvert(QVariant::Int))
189 return false;
191 param->type = skin_tag_parameter::NUMERIC;
192 param->data.numeric = value.toInt();
194 else
196 return false;
199 emit dataChanged(index, index);
200 return true;