-
Notifications
You must be signed in to change notification settings - Fork 70
/
ObjectPropertyModel.cpp
91 lines (75 loc) · 2.14 KB
/
ObjectPropertyModel.cpp
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
#include "ObjectPropertyModel.h"
#include "lib/ObjectProxy.h"
#include <QtCore/QMetaProperty>
#include <QtDebug>
ObjectPropertyModel::ObjectPropertyModel(QObject* parent)
: QStandardItemModel(parent)
{
setColumnCount(2);
setObject(ObjectProxy::Pointer());
connect(this,SIGNAL(itemChanged(QStandardItem*)),
this,SLOT(updatePropertyValue(QStandardItem*)));
}
QVariant ObjectPropertyModel::toEditValue(const QVariant& value)
{
// TODO - Find existing code which can handle display and editing
// of common value types.
switch (value.userType())
{
case QVariant::Size:
case QVariant::SizeF:
{
QSize size = value.toSize();
return QString("%1 x %2").arg(size.width()).arg(size.height());
}
case QVariant::Rect:
case QVariant::RectF:
{
QRect rect = value.toRect();
return QString("(%1,%2) (%3,%4)").arg(rect.left()).arg(rect.top()).arg(rect.width()).arg(rect.height());
}
}
return value;
}
void ObjectPropertyModel::updatePropertyValue(QStandardItem* item)
{
QStandardItem* propertyItem = this->item(item->row(),0);
QString propertyName = propertyItem->text();
if (m_object)
{
QVariant newValue = item->data(Qt::EditRole);
qDebug() << "setting property" << propertyName << "to" << newValue;
m_object->writeProperty(propertyName,newValue);
}
}
void ObjectPropertyModel::setObject(ObjectProxy::Pointer object)
{
clear();
setHorizontalHeaderLabels(
QStringList() << tr("Property") << tr("Value")
);
m_object = object;
if (object)
{
object->refresh();
int rowCount = 0;
QListIterator<ObjectProxy::Property> iter(object->properties());
while (iter.hasNext())
{
ObjectProxy::Property property = iter.next();
QVariant value = property.value;
QVariant editValue = toEditValue(value);
QStandardItem* nameItem = new QStandardItem(property.name);
nameItem->setEditable(false);
QStandardItem* valueItem = new QStandardItem;
valueItem->setData(editValue,Qt::EditRole);
valueItem->setEditable(property.isWritable);
insertRow(rowCount,QList<QStandardItem*>() << nameItem << valueItem);
++rowCount;
}
}
}
ObjectProxy::Pointer ObjectPropertyModel::object() const
{
return m_object;
}