[KLF Backend][KLF Tools][KLF Home]
KLatexFormula Project
klfpobjeditwidget.cpp
Go to the documentation of this file.
1/***************************************************************************
2 * file klfpobjeditwidget.cpp
3 * This file is part of the KLatexFormula Project.
4 * Copyright (C) 2012 by Philippe Faist
5 * philippe.faist at bluewin.ch
6 * *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program; if not, write to the *
19 * Free Software Foundation, Inc., *
20 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
21 ***************************************************************************/
22/* $Id$ */
23
24
25#include <QAbstractItemModel>
26#include <QItemDelegate>
27
28#include <klfdebug.h>
29
30#include "klfpobj.h"
31#include "klfdatautil.h"
32
33#include "klfpobjeditwidget.h"
34#include "klfpobjeditwidget_p.h"
35
36
37
38// -----------------------------------------------
39
40
41KLFPObjModel::KLFPObjModel(KLFAbstractPropertizedObject *pobj, QObject *parent)
42 : QAbstractItemModel(parent)
43{
45
46 KLF_INIT_PRIVATE(KLFPObjModel) ;
47
48 setPObj(pobj);
49}
50
51KLFPObjModel::~KLFPObjModel()
52{
54
56}
57
58void KLFPObjModel::setPObj(KLFAbstractPropertizedObject *pobj)
59{
61 klfDbg("pobj="<<pobj) ;
62
63 beginResetModel();
64
65 d->pObj = pobj;
66 if (d->pObj != NULL)
67 d->propertyNames = d->pObj->propertyNameList();
68 else
69 d->propertyNames = QStringList();
70
71 endResetModel();
72}
73
74KLFAbstractPropertizedObject * KLFPObjModel::pObj()
75{
76 return d->pObj;
77}
78
79QVariant KLFPObjModel::data(const QModelIndex& index, int role) const
80{
82 klfDbg( "\tindex="<<index<<"; role="<<role ) ;
83
84 if (!index.isValid()) {
85 klfDbg("Invalid index.") ;
86 return QVariant();
87 }
88
89 int n = index.row();
90
91 if (index.column() < 0 || index.column() > 1 ||
92 n < 0 || n >= d->propertyNames.count()) {
93 // out of range
94 klfWarning("Index out of range: "<<index) ;
95 return QVariant();
96 }
97
98 if (index.column() == 0) {
99 // property name
100 if (role == Qt::ToolTipRole || role == Qt::DisplayRole) {
101 // current contents
102 return d->propertyNames[n];
103 }
104
105 } else {
106 // property value
107
108 if (role == Qt::ToolTipRole || role == Qt::DisplayRole) {
109 return QVariant(klfSaveVariantToText(d->pObj->property(d->propertyNames[n])));
110 }
111 if (role == Qt::EditRole) {
112 // current contents
113 return d->pObj->property(d->propertyNames[n]);
114 }
115 }
116
117 // by default
118 return QVariant();
119}
120
121Qt::ItemFlags KLFPObjModel::flags(const QModelIndex& index) const
122{
124 klfDbg( "\tindex="<<index ) ;
125
126 if (!index.isValid()) {
127 klfDbg("Invalid index.") ;
128 return 0;
129 }
130
131 int n = index.row();
132
133 if (index.column() < 0 || index.column() > 1 ||
134 n < 0 || n >= d->propertyNames.count()) {
135 // out of range
136 klfWarning("Index out of range: "<<index) ;
137 return 0;
138 }
139
140 if (index.column() == 0) {
141 // property name
142 return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
143 } else {
144 // property value
145 return Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable;
146 }
147
148 return 0;
149}
150
151bool KLFPObjModel::hasChildren(const QModelIndex &parent) const
152{
153 if (!parent.isValid())
154 return true;
155 return false;
156}
157
158QVariant KLFPObjModel::headerData(int section, Qt::Orientation orientation, int role) const
159{
160 if (orientation == Qt::Horizontal) {
161 if (role == Qt::SizeHintRole) {
162 if (section == 0) { // property name
163 return QSize(100,30);
164 } else { // property value
165 return QSize(100,30);
166 }
167 }
168 if (role == Qt::DisplayRole) {
169 if (section == 0) {
170 return tr("Property");
171 } else {
172 return tr("Value");
173 }
174 }
175 }
176 return QVariant();
177}
178
179bool KLFPObjModel::hasIndex(int row, int column, const QModelIndex &parent) const
180{
181 if (column < 0 || column > 1)
182 return false;
183 if (row < 0 || row >= d->propertyNames.size())
184 return false;
185 if (parent.isValid())
186 return false;
187 return true;
188}
189
190QModelIndex KLFPObjModel::index(int row, int column, const QModelIndex &parent) const
191{
192 if (!hasIndex(row, column, parent))
193 return QModelIndex();
194
195 return createIndex(row, column);
196}
197
198QModelIndex KLFPObjModel::parent(const QModelIndex &/*index*/) const
199{
200 return QModelIndex();
201}
202
203int KLFPObjModel::rowCount(const QModelIndex &parent) const
204{
205 if (parent.isValid())
206 return 0;
207 return d->propertyNames.size();
208}
209
210int KLFPObjModel::columnCount(const QModelIndex &parent) const
211{
212 if (parent.isValid())
213 return 0;
214 return 2;
215}
216
217bool KLFPObjModel::canFetchMore(const QModelIndex& /*parent*/) const
218{
219 return false;
220}
221
222void KLFPObjModel::fetchMore(const QModelIndex& /*parent*/)
223{
224 return;
225}
226
227void KLFPObjModel::updateData()
228{
229 setPObj(d->pObj);
230}
231
232
233// -------------------------------------------------
234
235
236struct KLFPObjEditWidgetPrivate {
238 model = NULL;
239 delegate = NULL;
240 }
241
242 KLFPObjModel * model;
243 QItemDelegate * delegate;
244};
245
246
248 : QTreeView(parent)
249{
251
253
254 d->model = new KLFPObjModel(pobj, this);
255 d->delegate = new QItemDelegate(this);
256
257 setModel(d->model);
258 setItemDelegate(d->delegate);
259}
260
262 : QTreeView(parent)
263{
265
267
268 d->model = new KLFPObjModel(NULL, this);
269 d->delegate = new QItemDelegate(this);
270
271 setModel(d->model);
272 setItemDelegate(d->delegate);
273}
274
275
277{
279
281}
282
284{
285 d->model->setPObj(pobj);
286}
287
288
289
290
291
292
An abstract object characterized by properties.
Definition klfpobj.h:59
virtual QStringList propertyNameList() const =0
Queries what property are (or can be) set.
virtual void setPObj(KLFAbstractPropertizedObject *pobj)
KLFPObjEditWidget(QWidget *parent=NULL)
KLF_EXPORT QByteArray klfSaveVariantToText(const QVariant &value, bool saveListAndMapsAsXML, QByteArray *savedType, QByteArray *savedListOrMapType)
Debugging utilities.
#define KLF_DEBUG_TIME_BLOCK(msg)
Utility to time the execution of a block.
Definition klfdebug.h:151
#define klfWarning(streamableItems)
Definition klfdebug.h:171
#define KLF_DEBUG_BLOCK(msg)
Utility to debug the execution of a block.
Definition klfdebug.h:152
#define KLF_FUNC_NAME
Definition klfdebug.h:194
#define klfDbg(streamableItems)
print debug stream items
Definition klfdebug.h:158
#define KLF_PRIVATE_HEAD(ClassName)
Definition klfdefs.h:81
#define KLF_DELETE_PRIVATE
Definition klfdefs.h:96
#define KLF_INIT_PRIVATE(ClassName)
Definition klfdefs.h:94
int column() const
bool isValid() const
int row() const
typedef ItemFlags

Generated by doxygen 1.9.7