21
21
#include "optionstreemodel.h"
23
#include <QStringList>
22
25
#include "optionstree.h"
24
OptionsTreeModel::OptionsTreeModel(OptionsTree* tree, QObject* parent) : QAbstractItemModel(parent), tree_(tree), flat_(false)
28
// Enable this if you have Trolltech Labs' ModelTest and are not going
29
// to distribute the source or binary. You need to include modeltest.pri
31
//#define HAVE_MODELTEST
34
#include <modeltest.h>
37
OptionsTreeModel::OptionsTreeModel(OptionsTree* tree, QObject* parent)
38
: QAbstractItemModel(parent),
43
connect(tree_, SIGNAL(optionChanged(const QString&)), SLOT(optionChanged(const QString&)));
44
connect(tree_, SIGNAL(optionAboutToBeInserted(const QString&)), SLOT(optionAboutToBeInserted(const QString&)));
45
connect(tree_, SIGNAL(optionInserted(const QString&)), SLOT(optionInserted(const QString&)));
46
connect(tree_, SIGNAL(optionAboutToBeRemoved(const QString&)), SLOT(optionAboutToBeRemoved(const QString&)));
47
connect(tree_, SIGNAL(optionRemoved(const QString&)), SLOT(optionRemoved(const QString&)));
50
new ModelTest(this, this);
28
54
void OptionsTreeModel::setFlat(bool b)
64
* Get the parent option of @a option
65
* @param option the option name to be splitted
66
* @return the part of option until the last dot (or empty)
68
QString OptionsTreeModel::getParentName(const QString &option) const
71
int dot_index = option.lastIndexOf('.');
72
if (dot_index != -1) {
73
parentname = option.left(dot_index);
81
* Get index of given @a option
82
* @param option the option to retrieve the index for
83
* @param sec Section the new index should point to
84
* @return a QModelIndex to @a option
86
QModelIndex OptionsTreeModel::index(const QString &option, Section sec) const
93
QStringList options = tree_->getChildOptionNames("",false,false);
95
int row = options.indexOf(option);
96
return createIndex(row, sec, nameToIndex(options.at(row)));
98
QString parentname(getParentName(option));
100
QStringList children = tree_->getChildOptionNames(parentname,true,true);
102
int row = children.indexOf(option);
104
return createIndex(row, sec, nameToIndex(option));
36
109
Qt::ItemFlags OptionsTreeModel::flags(const QModelIndex& index) const
111
if (!index.isValid()) {
38
115
Qt::ItemFlags f = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
39
if (index.column() == Value)
116
if ((index.column() == Value) && !internalNode(indexToOptionName(index)))
40
117
f |= Qt::ItemIsEditable;
44
121
int OptionsTreeModel::rowCount(const QModelIndex& parent) const
47
return (parent.isValid() ? 0 : tree_->getChildOptionNames("",!flat_).count());
52
option = tree_->getChildOptionNames("",false,true).at(parent.internalId());
53
return tree_->getChildOptionNames(option,true,true).count();
123
if ((Section)parent.column() == Name || !parent.isValid()) {
125
return (parent.isValid() ? 0 : tree_->getChildOptionNames("",false,false).count());
128
if (parent.isValid())
129
option = indexToOptionName(parent);
130
return tree_->getChildOptionNames(option,true,true).count();
57
136
int OptionsTreeModel::columnCount(const QModelIndex&) const
64
143
if (!index.isValid())
65
144
return QVariant();
67
QString option = tree_->getChildOptionNames("",false,true).at(index.internalId());
146
QString option = indexToOptionName(index);
68
147
Section section = (Section) index.column();
69
if (role == Qt::DisplayRole) {
148
if ((role == Qt::DisplayRole) || (role == Qt::EditRole)) {
70
149
if (section == Name) {
75
153
int dot_index = option.lastIndexOf('.');
76
return option.right(option.length() - dot_index - 1);
154
return option.mid(dot_index + 1);
79
else if (section == Type)
80
return tree_->getOption(option).typeName();
81
else if (section == Value)
82
return tree_->getOption(option).toString();
83
else if (section == Comment)
84
return tree_->getComment(option);
157
else if (section == Comment) {
158
return tree_->getComment(option);
159
} else if (!tree_->isInternalNode(option)) {
161
return tree_->getOption(option).typeName();
162
else if (section == Value)
163
return tree_->getOption(option);//.toString();
165
} else if (role == Qt::ToolTipRole) {
166
if (!tree_->isInternalNode(option)) {
167
return tree_->getComment(option);
86
170
return QVariant();
97
181
case Type: return QString(tr("Type"));
98
182
case Value: return QString(tr("Value"));
99
183
case Comment: return QString(tr("Comment"));
105
189
QModelIndex OptionsTreeModel::index(int row, int column, const QModelIndex & parent) const
107
// FIXME: Horribly inefficient
108
QStringList all_options = tree_->getChildOptionNames("",false,true);
191
if (column < 0 || column >= SectionBound || row < 0) {
192
return QModelIndex();
111
QStringList options = tree_->getChildOptionNames("",false,false);
112
id = all_options.indexOf(options.at(row));
197
options = tree_->getChildOptionNames("",false,false);
115
199
QString parent_option;
116
if (parent.isValid())
117
parent_option = all_options.at(parent.internalId());
118
QStringList children = tree_->getChildOptionNames(parent_option,true,true);
119
if (row >= children.count())
120
return QModelIndex();
121
id = all_options.indexOf(children.at(row));
200
if (parent.isValid()) {
201
parent_option = indexToOptionName(parent);
203
options = tree_->getChildOptionNames(parent_option,true,true);
205
if (row >= options.size()) {
206
return QModelIndex();
209
id = nameToIndex(options.at(row));
123
210
return createIndex(row,column,id);
126
QModelIndex OptionsTreeModel::parent(const QModelIndex& index) const
128
if (!index.isValid() || flat_)
129
return QModelIndex();
131
QStringList all_options = tree_->getChildOptionNames("",false,true);
132
QString option = all_options.at(index.internalId());
134
// Determine the parent option
135
int dot_index = option.lastIndexOf('.');
137
return QModelIndex();
138
QString parent_option = option.left(dot_index);
140
// Determine the parent's parent
141
QString parent_parent_option;
142
dot_index = parent_option.lastIndexOf('.');
144
parent_parent_option = parent_option.left(dot_index);
145
int row = tree_->getChildOptionNames(parent_parent_option,true,true).indexOf(parent_option);
147
return createIndex(row,0,all_options.indexOf(parent_option));
213
QModelIndex OptionsTreeModel::parent(const QModelIndex& modelindex) const
215
if (!modelindex.isValid() || flat_)
216
return QModelIndex();
218
QString option = indexToOptionName(modelindex);
220
QString parent_option = getParentName(option);
222
return index(parent_option);
225
bool OptionsTreeModel::setData ( const QModelIndex & index, const QVariant & value, int role)
227
QString option = indexToOptionName(index);
228
if ((role != Qt::EditRole) || ((Section) index.column() != Value) || internalNode(option)) {
231
QVariant current = tree_->getOption(option);
232
QVariant newval = value;
233
if (!newval.canConvert(current.type())) {
234
qWarning("Sorry don't know how to do that!");
237
newval.convert(current.type());
238
tree_->setOption(option, newval);
245
void OptionsTreeModel::optionAboutToBeInserted(const QString& option)
247
QString parentname(getParentName(option));
249
// FIXME? handle cases when parent doesn't exist either.
251
QModelIndex parent(index(parentname));
253
QStringList children = tree_->getChildOptionNames(parentname,true,true);
256
int row = children.indexOf(option);
258
emit beginInsertRows(parent, row, row);
262
void OptionsTreeModel::optionInserted(const QString& option)
268
void OptionsTreeModel::optionAboutToBeRemoved(const QString& option)
270
QString parentname(getParentName(option));
272
QModelIndex parent(index(parentname));
274
QStringList children = tree_->getChildOptionNames(parentname,true,true);
276
int row = children.indexOf(option);
279
realRemove.push(true);
280
emit beginRemoveRows(parent, row, row);
282
realRemove.push(false);
285
void OptionsTreeModel::optionRemoved(const QString& option)
288
if (realRemove.pop()) endRemoveRows ();
292
void OptionsTreeModel::optionChanged(const QString& option)
294
// only need to notify about options the view can possibly know anything about.
295
if (nameMap.contains(option)) {
296
QModelIndex modelindex(index(option, Value));
297
emit dataChanged(modelindex, modelindex);
300
bool OptionsTreeModel::internalNode(QString option) const
302
return tree_->isInternalNode(option);
305
int OptionsTreeModel::nameToIndex(QString name) const
307
if (!nameMap.contains(name)) {
309
//qDebug() << "adding " << name << " as " << idx;
311
indexMap[idx] = name;
314
return nameMap[name];
318
QString OptionsTreeModel::indexToOptionName(QModelIndex idx) const
320
return indexMap[idx.internalId()];