fixed some build related problems
[qsqlmon.git] / libqf / libqfgui / gui / widgets / qfprototypedobjecttreemodel / qfprototypedobjecttreemodel.cpp
blobf83c8d345e219ce1b991a3490b4a80ee5ba9bd90
2 //
3 // Author: Frantisek Vacek <fanda.vacek@volny.cz>, (C) 2010
4 //
5 // Copyright: See COPYING file that comes with this distribution
6 //
8 #include "qfprototypedobjecttreemodel.h"
10 #include <QApplication>
12 #include <qflogcust.h>
14 //=================================================
15 // QFPrototypedObjectTreeModel
16 //=================================================
17 QFPrototypedObjectTreeModel::QFPrototypedObjectTreeModel(QObject *parent)
18 : QStandardItemModel(parent)
22 QFPrototypedObjectTreeModel::~QFPrototypedObjectTreeModel()
26 void QFPrototypedObjectTreeModel::setRoot(const QFPrototypedObject &o)
28 f_rootObject = o;
29 reload();
30 reset();
33 void QFPrototypedObjectTreeModel::reload_helper(const QStringList &path, QStandardItem *p_it)
35 static QStringList meta_keys;
36 if(meta_keys.isEmpty()) meta_keys
37 << QFPrototypedObject::HELP_KEY
38 << QFPrototypedObject::OPTIONS_KEY
39 << QFPrototypedObject::UNIT_KEY
40 << QFPrototypedObject::TYPE_KEY
41 << QFPrototypedObject::DELEGATE_KEY
42 << QFPrototypedObject::CAPTION_KEY;
43 foreach(QString key, f_rootObject.keysOnPath(path)) {
44 Item *it = new Item();
45 QStringList path2 = path;
46 path2 << key;
47 it->path = path2;
48 foreach(QString k, meta_keys) {
49 it->meta[k] = f_rootObject.metaValueOnPath(path2, k);
51 QString caption = it->meta.value(QFPrototypedObject::CAPTION_KEY, key).toString();
52 if(caption.isEmpty()) caption = key;
53 it->setText(caption);
54 p_it->appendRow(it);
55 reload_helper(path2, it);
59 void QFPrototypedObjectTreeModel::reload()
61 clear();
62 reload_helper(QStringList(), invisibleRootItem());
65 QFPrototypedObjectTreeModel::Item* QFPrototypedObjectTreeModel::itemFromIndex(const QModelIndex &ix) const
67 Item *it = dynamic_cast<Item*>(QStandardItemModel::itemFromIndex(ix.sibling(ix.row(), 0)));
68 return it;
71 QVariant QFPrototypedObjectTreeModel::itemValue(const QModelIndex &ix, int *p_status) const
73 QVariant ret;
74 int status = QFPrototypedObject::ValueNotFound;
75 Item *it = itemFromIndex(ix);
76 if(it) {
77 ret = f_rootObject.valueOnPath(it->path, QVariant(), &status);
79 if(p_status) *p_status = status;
80 return ret;
83 int QFPrototypedObjectTreeModel::columnCount(const QModelIndex &) const
85 return 2;
88 QModelIndex QFPrototypedObjectTreeModel::index(int row, int column, const QModelIndex & parent) const
90 /// tahle funkce vyrabi generovane sloupce modelu
91 QModelIndex ret = QStandardItemModel::index(row, 0, parent);
92 if(column > 0) ret = createIndex(row, column, ret.internalPointer());
93 return ret;
96 QVariant QFPrototypedObjectTreeModel::headerData(int section, Qt::Orientation orientation, int role) const
98 QVariant ret;
99 if(orientation == Qt::Horizontal) {
100 if(role == Qt::DisplayRole) {
101 if(section == 0) ret = trUtf8("vlastnost");
102 else if(section == 1) ret = trUtf8("hodnota");
105 else ret = QStandardItemModel::headerData(section, orientation, role);
106 return ret;
109 Qt::ItemFlags QFPrototypedObjectTreeModel::flags(const QModelIndex &ix) const
111 Qt::ItemFlags ret = QStandardItemModel::flags(ix);
112 ret &= ~Qt::ItemIsEditable;
113 int col = ix.column();
114 if(col == 1) {
115 Item *it = itemFromIndex(ix);
116 if(it) {
117 if(it->valueType().count()) ret |= Qt::ItemIsEditable;// | Qt::ItemIsSelectable);
120 else {
122 return ret;
125 QVariant QFPrototypedObjectTreeModel::data(const QModelIndex &ix, int role) const
127 QVariant ret = QStandardItemModel::data(ix, role);
128 int col = ix.column();
129 if(col == 1) {
130 Item *it = itemFromIndex(ix);
131 if(it) {
132 if(role == Qt::DisplayRole || role == Qt::EditRole) {
133 //int status;
134 ret = itemValue(ix);
136 if(!ret.isValid()) {
137 QByteArray type_name = it->meta.value("type").toString().toAscii();
138 //qfInfo() << type_name << "->" << QVariant::nameToType(type_name.constData()) << QVariant::typeToName(QVariant::String) << QVariant::typeToName(QVariant::Int);
139 ret = QVariant(QVariant::nameToType(type_name.constData()));
140 //qfInfo() << type_name << "->" << ret.typeName();
144 else if(role == Qt::ForegroundRole) {
145 int status;
146 ret = itemValue(ix, &status);
147 if(status & QFPrototypedObject::ValuePrototype) {
148 ret = QColor(Qt::gray);
151 #if 0
152 else if(role == Qt::FontRole) {
153 int status;
154 ret = itemValue(ix, &status);
155 if(status & QFPrototypedObject::ValuePrototype) {
157 else {
158 QFont f = QApplication::font("QStyledItemDelegate");
159 f.setBold(true);
160 ret = f;
163 #endif
166 else if(col == 0) {
168 if(role == Qt::BackgroundRole) {
169 Item *it = itemFromIndex(ix);
170 if(it && it->path.count() == 1) {
171 if(it->valueType().isEmpty()) ret = QColor("khaki");
174 return ret;
177 bool QFPrototypedObjectTreeModel::setData(const QModelIndex &ix, const QVariant &val, int role)
179 qfLogFuncFrame();
180 bool ret = false;
181 int col = ix.column();
182 if(col == 1) {
183 if(role == Qt::EditRole) {
184 Item *it = itemFromIndex(ix);
185 if(it) {
186 qfTrash() << "\t setting data on path:" << it->path.join("/") << "to:" << val.toString();
187 f_rootObject.setValueOnPath(it->path, val);
188 //qfInfo() << f_rootObject.dump();
189 ret = true;
190 emit propertyChanged(f_designPath, it->path, val);
194 return ret;