1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
|
/***************************************************************************
* Copyright (C) 2002-2004 by Alexander Dymo *
* cloudtemple@mskat.net *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Library General Public License as *
* published by the Free Software Foundation; either version 2 of the *
* License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "propertyeditor.h"
#ifndef PURE_QT
#include <klocale.h>
#include <kdebug.h>
#include <kiconloader.h>
#else
#include "compat_tools.h"
#endif
#include <qtable.h>
#include <qlayout.h>
#include <qpainter.h>
#include <qptrlist.h>
#include <qvaluelist.h>
#include <qpushbutton.h>
#include "property.h"
#include "multiproperty.h"
#include "propertymachinefactory.h"
namespace PropertyLib{
class PropertyItem: public KListViewItem{
public:
PropertyItem(PropertyEditor *parent, MultiProperty *property)
:KListViewItem(parent, property->description()), m_editor(parent), m_property(property),
m_changed(false)
{
}
PropertyItem(PropertyEditor *editor, KListViewItem *parent, MultiProperty *property)
:KListViewItem(parent, property->description()), m_editor(editor),
m_property(property), m_changed(false)
{
}
/* int type() const
{
return m_property->type();
}
QString name() const
{
return m_property->name();
}
*/
MultiProperty *property() const
{
return m_property;
}
virtual void paintCell(QPainter *p, const QColorGroup &cg, int column, int width, int align)
{
if ((column == 0) && m_changed)
{
QFont font;
font.setBold(true);
p->setFont(font);
p->setBrush(cg.highlight());
p->setPen(cg.highlightedText());
}
if (column == 1)
{
QRect r(0, 0, m_editor->header()->sectionSize(1), height());
//FIXME: this is ugly, but how else can we deal with ValueFromList properties?
QVariant valueToDraw;
if (m_property->type() == Property::ValueFromList)
valueToDraw = m_property->findValueDescription();
else
valueToDraw = m_property->value();
QColorGroup icg(cg);
#ifndef PURE_QT
icg.setColor(QColorGroup::Background, backgroundColor());
#else
icg.setColor(QColorGroup::Background, white);
#endif
m_editor->machine(m_property)->propertyEditor->drawViewer(p, icg, r, valueToDraw);
return;
}
KListViewItem::paintCell(p, cg, column, width, align);
}
virtual void setup()
{
KListViewItem::setup();
setHeight(static_cast<int>(height()*1.5));
}
void setChanged(bool changed)
{
m_changed = changed;
}
private:
PropertyEditor *m_editor;
MultiProperty *m_property;
bool m_changed;
};
class PropertyGroupItem: public KListViewItem{
public:
PropertyGroupItem(KListView *parent, const QString &name)
:KListViewItem(parent, name)
{
init();
}
PropertyGroupItem(KListViewItem *parent, const QString &name)
:KListViewItem(parent, name)
{
init();
}
virtual void paintCell(QPainter *p, const QColorGroup &cg, int column, int width, int align)
{
if (column == 0)
{
QFont font;
font.setBold(true);
p->setFont(font);
p->setBrush(cg.highlight());
p->setPen(cg.highlightedText());
}
KListViewItem::paintCell(p, cg, column, width, align);
}
virtual void setup()
{
KListViewItem::setup();
setHeight(static_cast<int>(height()*1.4));
}
private:
void init()
{
setOpen(true);
}
};
class SeparatorItem: public KListViewItem{
public:
SeparatorItem(KListView *parent)
:KListViewItem(parent)
{
setSelectable(false);
}
};
PropertyEditor::PropertyEditor(QWidget *parent, const char *name)
:KListView(parent, name)
{
setSorting(-1);
addColumn(i18n("Name"));
addColumn(i18n("Value"));
setAllColumnsShowFocus(true);
setColumnWidthMode(0, QListView::Maximum);
setResizeMode(QListView::LastColumn);
header()->setClickEnabled(false);
connect(header(), SIGNAL(sizeChange(int, int, int)),
this, SLOT(updateEditorSize()));
connect(this, SIGNAL(currentChanged(QListViewItem*)),
this, SLOT(slotClicked(QListViewItem*)));
m_currentEditItem = 0;
m_doubleClickForEdit = true;
m_lastClickedItem = 0;
m_currentEditWidget = 0;
m_list = 0;
m_currentEditArea = new QWidget(viewport());
m_currentEditArea->hide();
m_undoButton = new QPushButton(m_currentEditArea);
#ifndef PURE_QT
m_undoButton->setPixmap(SmallIcon("undo"));
#else
m_undoButton->setPixmap( QPixmap("undo.xpm") );
#endif
m_undoButton->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::MinimumExpanding);
m_undoButton->resize(m_undoButton->height(), m_undoButton->height());
m_undoButton->hide();
connect(m_undoButton, SIGNAL(clicked()), this, SLOT(undo()));
m_currentEditLayout = new QGridLayout(m_currentEditArea, 1, 2, 0, 0);
// m_currentEditLayout->addWidget(m_undoButton, 0, 1);
}
PropertyEditor::~PropertyEditor()
{
clearMachineCache();
}
void PropertyEditor::populateProperties(PropertyList *list)
{
if (list == 0)
return;
m_list = list;
connect(m_list, SIGNAL(propertyValueChanged(Property*)), this, SLOT(propertyValueChanged(Property*)));
const QValueList<QPair<QString, QValueList<QString> > >& groups = m_list->propertiesOfGroup();
for (QValueList<QPair<QString, QValueList<QString> > >::const_iterator it = groups.begin();
it != groups.end(); ++it)
{
// qWarning("PropertyEditor::populateProperties: adding group %s", (*it).first.ascii());
PropertyGroupItem *group = 0;
if ( (!(*it).first.isEmpty()) && ((*it).second.count() > 0) )
group = new PropertyGroupItem(this, (*it).first);
const QValueList<QString> &properties = (*it).second;
for (QValueList<QString>::const_iterator it2 = properties.begin(); it2 != properties.end(); ++it2)
{
// qWarning("PropertyEditor::populateProperties: adding property %s", (*it2).ascii());
if (group)
addProperty(group, *it2);
else
addProperty(*it2);
}
}
if (firstChild())
{
setCurrentItem(firstChild());
setSelected(firstChild(), true);
slotClicked(firstChild());
}
}
void PropertyEditor::addProperty(PropertyGroupItem *group, const QString &name)
{
if ((*m_list)[name] == 0)
return;
// qWarning("%s = name : object null ", name.ascii());
PropertyItem *pitem = new PropertyItem(this, group, (*m_list)[name]);
addChildProperties(pitem);
}
void PropertyEditor::addProperty(const QString &name)
{
if ((*m_list)[name] == 0)
return;
// qWarning("%s = name : object null ", name.ascii());
PropertyItem *pitem = new PropertyItem(this, (*m_list)[name]);
addChildProperties(pitem);
}
void PropertyEditor::addChildProperties(PropertyItem *parent)
{
MultiProperty *prop = parent->property();
//force machine creation to get detailed properties appended to current multiproperty
if ( !m_registeredForType.contains(prop->name())
&& (PropertyMachineFactory::getInstance()->hasDetailedEditors(prop->type())) )
{
//FIXME: find better solution
machine(prop);
}
// qWarning("seeking children: count: %d", prop->details.count());
parent->setOpen(true);
for (QValueList<ChildProperty>::iterator it = prop->details.begin(); it != prop->details.end(); ++it)
{
// qWarning("found child %s", (*it).name().ascii());
new PropertyItem(this, parent, new MultiProperty(&m_detailedList, &(*it)));
}
}
void PropertyEditor::clearProperties()
{
m_detailedList.clear();
if (!m_list)
return;
hideEditor();
disconnect(m_list, SIGNAL(propertyValueChanged(Property*)), this, SLOT(propertyValueChanged(Property*)));
clear();
delete m_list;
m_list = 0;
}
void PropertyEditor::propertyValueChanged(Property *property)
{
// qWarning("PropertyEditor::propertyValueChanged");
if (m_currentEditWidget->propertyName() == property->name())
m_currentEditWidget->setValue(property->value(), false);
else
{
// repaint all items
QListViewItemIterator it(this);
while (it.current())
{
repaintItem(it.current());
++it;
}
}
}
void PropertyEditor::propertyChanged(MultiProperty *property, const QVariant &value)
{
if (!property)
return;
qWarning("editor: assign %s to %s", property->name().latin1(), value.toString().latin1());
property->setValue(value, false);
//highlight changed properties
if (m_currentEditItem && (m_currentEditItem->property() == property))
{
m_currentEditItem->setChanged(true);
repaintItem(m_currentEditItem);
}
emit changed();
/* if (m_list->contains(name))
{
(*m_list)[name]->setValue(value, false);
// else if (m_detailedList->contains(*/
}
void PropertyEditor::hideEditor()
{
m_lastClickedItem = 0;
m_currentEditItem = 0;
if (m_currentEditWidget)
{
m_currentEditLayout->remove(m_currentEditWidget);
m_currentEditWidget->hide();
}
m_currentEditLayout->remove(m_undoButton);
m_undoButton->hide();
m_currentEditArea->hide();
m_currentEditWidget = 0;
}
void PropertyEditor::showEditor(PropertyItem *item)
{
m_currentEditItem = item;
placeEditor(item);
m_currentEditWidget->show();
m_undoButton->show();
m_currentEditArea->show();
}
void PropertyEditor::placeEditor(PropertyItem *item)
{
QRect r = itemRect(item);
if (!r.size().isValid())
{
ensureItemVisible(item);
r = itemRect(item);
}
r.setX(header()->sectionPos(1));
r.setWidth(header()->sectionSize(1));
// check if the column is fully visible
if (visibleWidth() < r.right())
r.setRight(visibleWidth());
r = QRect(viewportToContents(r.topLeft()), r.size());
if (item->pixmap(1))
{
r.setX(r.x() + item->pixmap(1)->width());
}
if (PropertyWidget* editor = prepareEditor(item))
{
m_currentEditLayout->addWidget(editor, 0, 0);
m_currentEditLayout->addWidget(m_undoButton, 0, 1);
m_currentEditArea->resize(r.size());
// m_currentEditLayout->invalidate();
moveChild(m_currentEditArea, r.x(), r.y());
m_currentEditWidget = editor;
}
}
PropertyWidget* PropertyEditor::prepareEditor(PropertyItem *item)
{
PropertyWidget *editorWidget = 0;
/* if (item->depth() >= 2)
{
editorWidget = machine(item->name())->propertyEditor;
editorWidget->setValue(m_accessor->value(item->name()), false);
}
else
{*/
editorWidget = machine(item->property())->propertyEditor;
editorWidget->setProperty(item->property());
if (item->property()->type() == Property::ValueFromList)
editorWidget->setValueList(item->property()->valueList());
editorWidget->setValue(item->property()->value(), false);
//}
return editorWidget;
}
void PropertyEditor::updateEditorSize()
{
if (m_currentEditItem)
placeEditor(m_currentEditItem);
}
void PropertyEditor::slotClicked(QListViewItem *item)
{
if (item == 0)
{
hideEditor();
return;
}
if (item != m_lastClickedItem)
{
hideEditor();
PropertyItem *it = dynamic_cast<PropertyItem*>(item);
if (it)
{
showEditor(it);
}
}
m_lastClickedItem = item;
}
Machine *PropertyEditor::machine(MultiProperty *property)
{
// int type = property->type();
QString name = property->name();
QMap<QString, QVariant> values = property->valueList();
if (m_registeredForType[name] == 0)
{
m_registeredForType[name] = PropertyMachineFactory::getInstance()->machineForProperty(property);
connect(m_registeredForType[name]->propertyEditor, SIGNAL(propertyChanged(MultiProperty*, const QVariant&)),
this, SLOT(propertyChanged(MultiProperty*, const QVariant&)));
m_registeredForType[name]->propertyEditor->reparent(m_currentEditArea, 0, m_currentEditArea->childrenRect().topLeft());
m_registeredForType[name]->propertyEditor->hide();
}
return m_registeredForType[name];
}
void PropertyEditor::clearMachineCache()
{
for (QMap<QString, Machine* >::iterator it = m_registeredForType.begin(); it != m_registeredForType.end(); ++it)
{
delete it.data();
}
m_registeredForType.clear();
}
void PropertyEditor::undo()
{
if ((m_currentEditItem == 0) || (m_currentEditWidget == 0)
|| (!m_currentEditWidget->isVisible()))
return;
m_currentEditWidget->undo();
m_currentEditItem->setChanged(false);
repaintItem(m_currentEditItem);
}
}
#ifndef PURE_QT
#include "propertyeditor.moc"
#endif
|