~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to tools/designer/src/components/propertyeditor/qpropertyeditor_model.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
 
4
**
 
5
** This file is part of the designer application of the Qt Toolkit.
 
6
**
 
7
** This file may be distributed under the terms of the Q Public License
 
8
** as defined by Trolltech AS of Norway and appearing in the file
 
9
** LICENSE.QPL included in the packaging of this file.
 
10
**
 
11
** This file may be distributed and/or modified under the terms of the
 
12
** GNU General Public License version 2 as published by the Free Software
 
13
** Foundation and appearing in the file LICENSE.GPL included in the
 
14
** packaging of this file.
 
15
**
 
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
 
17
**   information about Qt Commercial License Agreements.
 
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
 
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
 
20
**
 
21
** Contact info@trolltech.com if any conditions of this licensing are
 
22
** not clear to you.
 
23
**
 
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 
26
**
 
27
****************************************************************************/
 
28
 
 
29
#include "qpropertyeditor_model_p.h"
 
30
#include <QtCore/qdebug.h>
 
31
 
 
32
using namespace qdesigner_internal;
 
33
 
 
34
QPropertyEditorModel::QPropertyEditorModel(QObject *parent)
 
35
    : QAbstractItemModel(parent), m_initialInput(0)
 
36
{
 
37
}
 
38
 
 
39
QPropertyEditorModel::~QPropertyEditorModel()
 
40
{
 
41
}
 
42
 
 
43
void QPropertyEditorModel::setInitialInput(IProperty *initialInput)
 
44
{
 
45
    Q_ASSERT(initialInput);
 
46
 
 
47
    m_initialInput = initialInput;
 
48
    reset();
 
49
}
 
50
 
 
51
QModelIndex QPropertyEditorModel::index(int row, int column, const QModelIndex &parent) const
 
52
{
 
53
    if (!parent.isValid())
 
54
        return createIndex(row, column, m_initialInput);
 
55
 
 
56
    return createIndex(row, column, childAt(privateData(parent), row));
 
57
}
 
58
 
 
59
QModelIndex QPropertyEditorModel::parent(const QModelIndex &index) const
 
60
{
 
61
    if (!index.isValid() || privateData(index) == m_initialInput)
 
62
        return QModelIndex();
 
63
 
 
64
    Q_ASSERT(privateData(index));
 
65
 
 
66
    return indexOf(parentOf(privateData(index)));
 
67
}
 
68
 
 
69
int QPropertyEditorModel::rowCount(const QModelIndex &parent) const
 
70
{
 
71
    if (!parent.isValid())
 
72
        return 1;
 
73
 
 
74
    if (IProperty *p = privateData(parent)) {
 
75
        return (p->kind() == IProperty::Property_Group)
 
76
            ? static_cast<IPropertyGroup*>(p)->propertyCount()
 
77
            : 0;
 
78
    }
 
79
 
 
80
    return (m_initialInput->kind() == IProperty::Property_Group)
 
81
        ? static_cast<IPropertyGroup*>(m_initialInput)->propertyCount()
 
82
        : 0;
 
83
}
 
84
 
 
85
int QPropertyEditorModel::columnCount(const QModelIndex &parent) const
 
86
{
 
87
    Q_UNUSED(parent);
 
88
 
 
89
    return 2;
 
90
}
 
91
 
 
92
bool QPropertyEditorModel::setData(const QModelIndex &index, const QVariant &value, int role)
 
93
{
 
94
    if (IProperty *property = privateData(index)) {
 
95
        if (role == Qt::EditRole) {
 
96
            property->setValue(value);
 
97
            refresh(property);
 
98
 
 
99
            IProperty *nonfake = property;
 
100
            while (nonfake != 0 && nonfake->isFake())
 
101
                nonfake = nonfake->parent();
 
102
            if (nonfake != 0 && nonfake->dirty()) {
 
103
                nonfake->setDirty(false);
 
104
                emit propertyChanged(nonfake);
 
105
            }
 
106
        }
 
107
 
 
108
        return true;
 
109
    }
 
110
 
 
111
    return false;
 
112
}
 
113
 
 
114
QVariant QPropertyEditorModel::data(const QModelIndex &index, int role) const
 
115
{
 
116
    Q_UNUSED(role);
 
117
 
 
118
    if (!privateData(index))
 
119
        return QVariant();
 
120
 
 
121
    IProperty *o = privateData(index);
 
122
    switch (index.column()) {  // ### cleanup
 
123
        case 0:
 
124
            switch (role) {
 
125
                case Qt::EditRole:
 
126
                case Qt::DisplayRole:
 
127
                    return o->propertyName().isEmpty()
 
128
                        ? QLatin1String("<noname>")
 
129
                        : o->propertyName();
 
130
                default:
 
131
                    break;
 
132
            }
 
133
            break;
 
134
 
 
135
        case 1: {
 
136
            switch (role) {
 
137
                case Qt::EditRole:
 
138
                    return o->value();
 
139
                case Qt::DisplayRole:
 
140
                    return o->toString();
 
141
                case Qt::DecorationRole:
 
142
                    return o->decoration();
 
143
                default:
 
144
                    break;
 
145
            }
 
146
            break;
 
147
        }
 
148
 
 
149
        default:
 
150
            break;
 
151
    }
 
152
 
 
153
    return QVariant();
 
154
}
 
155
 
 
156
QString QPropertyEditorModel::columnText(int col) const
 
157
{
 
158
    switch (col) {
 
159
        case 0: return QLatin1String("Property");
 
160
        case 1: return QLatin1String("Value");
 
161
        default: return QString();
 
162
    }
 
163
}
 
164
 
 
165
 
 
166
 
 
167
void QPropertyEditorModel::refreshHelper(IProperty *property)
 
168
{
 
169
    QModelIndex index0 = indexOf(property, 0);
 
170
    QModelIndex index1 = indexOf(property, 1);
 
171
    emit dataChanged(index0, index1);
 
172
}
 
173
 
 
174
void QPropertyEditorModel::refresh(IProperty *property)
 
175
{
 
176
    refreshHelper(property);
 
177
 
 
178
    // Refresh everyone up to the root
 
179
 
 
180
    IProperty *prop = property;
 
181
    while (prop != 0) {
 
182
        refreshHelper(prop);
 
183
        prop = prop->parent();
 
184
    }
 
185
 
 
186
    // Refresh all children
 
187
 
 
188
    if (property->kind() == IProperty::Property_Group) {
 
189
        IPropertyGroup *prop_group = static_cast<IPropertyGroup*>(property);
 
190
        for (int i = 0; i < prop_group->propertyCount(); ++i) {
 
191
            IProperty *child_prop = prop_group->propertyAt(i);
 
192
            refreshHelper(child_prop);
 
193
        }
 
194
    }
 
195
}
 
196
 
 
197
void QPropertyEditorModel::refresh()
 
198
{
 
199
    refresh(m_initialInput);
 
200
}
 
201
 
 
202
bool QPropertyEditorModel::isEditable(const QModelIndex &index) const
 
203
{
 
204
    return index.column() == 1 && privateData(index)->hasEditor();
 
205
}
 
206
 
 
207
QModelIndex QPropertyEditorModel::buddy(const QModelIndex &index) const
 
208
{
 
209
    if (index.column() == 0)
 
210
        return createIndex(index.row(), 1, index.internalPointer());
 
211
    return index;
 
212
}
 
213
 
 
214
QVariant QPropertyEditorModel::headerData(int section, Qt::Orientation orientation, int role) const
 
215
{
 
216
    if (orientation == Qt::Horizontal) {
 
217
        if (role != Qt::DisplayRole)
 
218
            return QVariant();
 
219
        return columnText(section);
 
220
    }
 
221
    return QAbstractItemModel::headerData(section, orientation, role);
 
222
}
 
223
 
 
224
Qt::ItemFlags QPropertyEditorModel::flags(const QModelIndex &index) const
 
225
{
 
226
    Qt::ItemFlags foo = QAbstractItemModel::flags(index);
 
227
    if (isEditable(index))
 
228
        foo |= Qt::ItemIsEditable;
 
229
    return foo;
 
230
}