summaryrefslogtreecommitdiffstats
path: root/tools/qmldebugger/propertyview.cpp
blob: d5bb3dff1ffe58d2474da6d164e6175a5e68bd26 (plain)
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
#include "propertyview.h"
#include <QtCore/qdebug.h>
#include <QtGui/qboxlayout.h>
#include <QtGui/qtreewidget.h>

QT_BEGIN_NAMESPACE

PropertyView::PropertyView(QWidget *parent)
: QWidget(parent), m_tree(0)
{
    QVBoxLayout *layout = new QVBoxLayout;
    layout->setContentsMargins(0, 0, 0, 0);
    layout->setSpacing(0);
    setLayout(layout);

    m_tree = new QTreeWidget(this);
    m_tree->setExpandsOnDoubleClick(false);
    m_tree->setHeaderLabels(QStringList() << tr("Property") << tr("Value"));
    QObject::connect(m_tree, SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)),
                     this, SLOT(itemDoubleClicked(QTreeWidgetItem *)));

    m_tree->setColumnCount(2);

    layout->addWidget(m_tree);
}

class PropertyViewItem : public QObject, public QTreeWidgetItem
{
    Q_OBJECT
public:
    PropertyViewItem(QTreeWidget *widget);
    PropertyViewItem(QTreeWidgetItem *parent);

    QmlDebugPropertyReference property;
};

PropertyViewItem::PropertyViewItem(QTreeWidget *widget)
: QTreeWidgetItem(widget)
{
}

PropertyViewItem::PropertyViewItem(QTreeWidgetItem *parent)
: QTreeWidgetItem(parent)
{
}


void PropertyView::setObject(const QmlDebugObjectReference &object)
{
    m_object = object;
    m_tree->clear();

    QList<QmlDebugPropertyReference> properties = object.properties();
    for (int i=0; i<properties.count(); i++) {
        const QmlDebugPropertyReference &p = properties[i];

        PropertyViewItem *item = new PropertyViewItem(m_tree);
        item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
        item->setCheckState(0, Qt::Unchecked);
        item->property = p;

        item->setText(0, p.name());

        if (!p.binding().isEmpty()) {
            PropertyViewItem *binding = new PropertyViewItem(item);
            binding->setText(1, p.binding());
            binding->setForeground(1, Qt::darkGreen);
        }

        item->setExpanded(true);
    }

    m_tree->resizeColumnToContents(0);
}

const QmlDebugObjectReference &PropertyView::object() const
{
    return m_object;
}

void PropertyView::clear()
{
    setObject(QmlDebugObjectReference());
}

void PropertyView::updateProperty(const QString &name, const QVariant &value)
{
    for (int i=0; i<m_tree->topLevelItemCount(); i++) {
        PropertyViewItem *item = static_cast<PropertyViewItem *>(m_tree->topLevelItem(i));
        if (item->property.name() == name)
            item->setText(1, value.toString());
    }
}

void PropertyView::setPropertyIsWatched(const QString &name, bool watched)
{
    for (int i=0; i<m_tree->topLevelItemCount(); i++) {
        PropertyViewItem *item = static_cast<PropertyViewItem *>(m_tree->topLevelItem(i));
        if (item->property.name() == name) {
            if (watched)
                item->setCheckState(0, Qt::Checked);
            else
                item->setCheckState(0, Qt::Unchecked);
        }
    }
}

void PropertyView::itemDoubleClicked(QTreeWidgetItem *i)
{
    PropertyViewItem *item = static_cast<PropertyViewItem *>(i);
    if (!item->property.name().isEmpty())
        emit propertyDoubleClicked(item->property);
}

QT_END_NAMESPACE

#include "propertyview.moc"