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: klfpobjeditwidget.cpp 978 2016-12-31 05:16:11Z phfaist $ */
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
KLFPObjModel(KLFAbstractPropertizedObject * pobj,QObject * parent)41 KLFPObjModel::KLFPObjModel(KLFAbstractPropertizedObject *pobj, QObject *parent)
42 : QAbstractItemModel(parent)
43 {
44 KLF_DEBUG_TIME_BLOCK(KLF_FUNC_NAME) ;
45
46 KLF_INIT_PRIVATE(KLFPObjModel) ;
47
48 setPObj(pobj);
49 }
50
~KLFPObjModel()51 KLFPObjModel::~KLFPObjModel()
52 {
53 KLF_DEBUG_TIME_BLOCK(KLF_FUNC_NAME) ;
54
55 KLF_DELETE_PRIVATE ;
56 }
57
setPObj(KLFAbstractPropertizedObject * pobj)58 void KLFPObjModel::setPObj(KLFAbstractPropertizedObject *pobj)
59 {
60 KLF_DEBUG_BLOCK(KLF_FUNC_NAME);
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
pObj()74 KLFAbstractPropertizedObject * KLFPObjModel::pObj()
75 {
76 return d->pObj;
77 }
78
data(const QModelIndex & index,int role) const79 QVariant KLFPObjModel::data(const QModelIndex& index, int role) const
80 {
81 KLF_DEBUG_TIME_BLOCK(KLF_FUNC_NAME) ;
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
flags(const QModelIndex & index) const121 Qt::ItemFlags KLFPObjModel::flags(const QModelIndex& index) const
122 {
123 KLF_DEBUG_TIME_BLOCK(KLF_FUNC_NAME) ;
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
hasChildren(const QModelIndex & parent) const151 bool KLFPObjModel::hasChildren(const QModelIndex &parent) const
152 {
153 if (!parent.isValid())
154 return true;
155 return false;
156 }
157
headerData(int section,Qt::Orientation orientation,int role) const158 QVariant 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
hasIndex(int row,int column,const QModelIndex & parent) const179 bool 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
index(int row,int column,const QModelIndex & parent) const190 QModelIndex 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
parent(const QModelIndex &) const198 QModelIndex KLFPObjModel::parent(const QModelIndex &/*index*/) const
199 {
200 return QModelIndex();
201 }
202
rowCount(const QModelIndex & parent) const203 int KLFPObjModel::rowCount(const QModelIndex &parent) const
204 {
205 if (parent.isValid())
206 return 0;
207 return d->propertyNames.size();
208 }
209
columnCount(const QModelIndex & parent) const210 int KLFPObjModel::columnCount(const QModelIndex &parent) const
211 {
212 if (parent.isValid())
213 return 0;
214 return 2;
215 }
216
canFetchMore(const QModelIndex &) const217 bool KLFPObjModel::canFetchMore(const QModelIndex& /*parent*/) const
218 {
219 return false;
220 }
221
fetchMore(const QModelIndex &)222 void KLFPObjModel::fetchMore(const QModelIndex& /*parent*/)
223 {
224 return;
225 }
226
updateData()227 void KLFPObjModel::updateData()
228 {
229 setPObj(d->pObj);
230 }
231
232
233 // -------------------------------------------------
234
235
236 struct KLFPObjEditWidgetPrivate {
KLF_PRIVATE_HEADKLFPObjEditWidgetPrivate237 KLF_PRIVATE_HEAD(KLFPObjEditWidget) {
238 model = NULL;
239 delegate = NULL;
240 }
241
242 KLFPObjModel * model;
243 QItemDelegate * delegate;
244 };
245
246
KLFPObjEditWidget(KLFAbstractPropertizedObject * pobj,QWidget * parent)247 KLFPObjEditWidget::KLFPObjEditWidget(KLFAbstractPropertizedObject *pobj, QWidget *parent)
248 : QTreeView(parent)
249 {
250 KLF_DEBUG_TIME_BLOCK(KLF_FUNC_NAME) ;
251
252 KLF_INIT_PRIVATE(KLFPObjEditWidget) ;
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
KLFPObjEditWidget(QWidget * parent)261 KLFPObjEditWidget::KLFPObjEditWidget(QWidget *parent)
262 : QTreeView(parent)
263 {
264 KLF_DEBUG_TIME_BLOCK(KLF_FUNC_NAME) ;
265
266 KLF_INIT_PRIVATE(KLFPObjEditWidget) ;
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
~KLFPObjEditWidget()276 KLFPObjEditWidget::~KLFPObjEditWidget()
277 {
278 KLF_DEBUG_TIME_BLOCK(KLF_FUNC_NAME) ;
279
280 KLF_DELETE_PRIVATE ;
281 }
282
setPObj(KLFAbstractPropertizedObject * pobj)283 void KLFPObjEditWidget::setPObj(KLFAbstractPropertizedObject *pobj)
284 {
285 d->model->setPObj(pobj);
286 }
287
288
289
290
291
292
293