1
/****************************************************************************
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
5
** This file is part of the designer application of the Qt Toolkit.
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.
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.
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.
21
** Contact info@trolltech.com if any conditions of this licensing are
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.
27
****************************************************************************/
29
#include "qpropertyeditor_model_p.h"
30
#include <QtCore/qdebug.h>
32
using namespace qdesigner_internal;
34
QPropertyEditorModel::QPropertyEditorModel(QObject *parent)
35
: QAbstractItemModel(parent), m_initialInput(0)
39
QPropertyEditorModel::~QPropertyEditorModel()
43
void QPropertyEditorModel::setInitialInput(IProperty *initialInput)
45
Q_ASSERT(initialInput);
47
m_initialInput = initialInput;
51
QModelIndex QPropertyEditorModel::index(int row, int column, const QModelIndex &parent) const
53
if (!parent.isValid())
54
return createIndex(row, column, m_initialInput);
56
return createIndex(row, column, childAt(privateData(parent), row));
59
QModelIndex QPropertyEditorModel::parent(const QModelIndex &index) const
61
if (!index.isValid() || privateData(index) == m_initialInput)
64
Q_ASSERT(privateData(index));
66
return indexOf(parentOf(privateData(index)));
69
int QPropertyEditorModel::rowCount(const QModelIndex &parent) const
71
if (!parent.isValid())
74
if (IProperty *p = privateData(parent)) {
75
return (p->kind() == IProperty::Property_Group)
76
? static_cast<IPropertyGroup*>(p)->propertyCount()
80
return (m_initialInput->kind() == IProperty::Property_Group)
81
? static_cast<IPropertyGroup*>(m_initialInput)->propertyCount()
85
int QPropertyEditorModel::columnCount(const QModelIndex &parent) const
92
bool QPropertyEditorModel::setData(const QModelIndex &index, const QVariant &value, int role)
94
if (IProperty *property = privateData(index)) {
95
if (role == Qt::EditRole) {
96
property->setValue(value);
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);
114
QVariant QPropertyEditorModel::data(const QModelIndex &index, int role) const
118
if (!privateData(index))
121
IProperty *o = privateData(index);
122
switch (index.column()) { // ### cleanup
126
case Qt::DisplayRole:
127
return o->propertyName().isEmpty()
128
? QLatin1String("<noname>")
139
case Qt::DisplayRole:
140
return o->toString();
141
case Qt::DecorationRole:
142
return o->decoration();
156
QString QPropertyEditorModel::columnText(int col) const
159
case 0: return QLatin1String("Property");
160
case 1: return QLatin1String("Value");
161
default: return QString();
167
void QPropertyEditorModel::refreshHelper(IProperty *property)
169
QModelIndex index0 = indexOf(property, 0);
170
QModelIndex index1 = indexOf(property, 1);
171
emit dataChanged(index0, index1);
174
void QPropertyEditorModel::refresh(IProperty *property)
176
refreshHelper(property);
178
// Refresh everyone up to the root
180
IProperty *prop = property;
183
prop = prop->parent();
186
// Refresh all children
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);
197
void QPropertyEditorModel::refresh()
199
refresh(m_initialInput);
202
bool QPropertyEditorModel::isEditable(const QModelIndex &index) const
204
return index.column() == 1 && privateData(index)->hasEditor();
207
QModelIndex QPropertyEditorModel::buddy(const QModelIndex &index) const
209
if (index.column() == 0)
210
return createIndex(index.row(), 1, index.internalPointer());
214
QVariant QPropertyEditorModel::headerData(int section, Qt::Orientation orientation, int role) const
216
if (orientation == Qt::Horizontal) {
217
if (role != Qt::DisplayRole)
219
return columnText(section);
221
return QAbstractItemModel::headerData(section, orientation, role);
224
Qt::ItemFlags QPropertyEditorModel::flags(const QModelIndex &index) const
226
Qt::ItemFlags foo = QAbstractItemModel::flags(index);
227
if (isEditable(index))
228
foo |= Qt::ItemIsEditable;