summaryrefslogtreecommitdiffstats
path: root/tools/designer/src/components/formeditor/itemview_propertysheet.cpp
blob: 13740e2d971b726f0874072e14ac635635db83ad (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
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
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the Qt Designer of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the either Technology Preview License Agreement or the
** Beta Release License Agreement.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain
** additional rights. These rights are described in the Nokia Qt LGPL
** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
** package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://www.qtsoftware.com/contact.
** $QT_END_LICENSE$
**
****************************************************************************/

#include "itemview_propertysheet.h"

#include <QtDesigner/QDesignerFormEditorInterface>

#include <QtGui/QAbstractItemView>
#include <QtGui/QHeaderView>

QT_BEGIN_NAMESPACE

namespace qdesigner_internal {

struct Property {
    Property() : m_sheet(0),m_id(-1) {}
    Property(QDesignerPropertySheetExtension *sheet, int id)
        : m_sheet(sheet), m_id(id) {}
    bool operator==(const Property &p) { return m_sheet == p.m_sheet && m_id == p.m_id; }
    uint qHash() {
        return ((int)(m_sheet-(QDesignerPropertySheetExtension*)(0))) & m_id;
    }

    QDesignerPropertySheetExtension *m_sheet;
    int m_id;
};

class ItemViewPropertySheetPrivate {

public:
    ItemViewPropertySheetPrivate(QHeaderView *horizontalHeader,
                                 QHeaderView *verticalHeader,
                                 QObject *parent);

    inline void createMapping(int fakeId, QHeaderView *header, const QString &headerName);
    inline QStringList realPropertyNames();
    inline QString fakePropertyName(const QString &prefix, const QString &realName);

    QDesignerFormEditorInterface *m_core;

    // Maps index of fake property
    // to index of real property in respective sheet
    QHash<int, Property> m_propertyIdMap;

    // Maps name of fake property
    // to name of real property
    QHash<QString, QString> m_propertyNameMap;

private:
    static QDesignerFormEditorInterface *formEditorForObject(QObject *o);

    QHeaderView *m_hHeader;
    QHeaderView *m_vHeader;
    QHash<QHeaderView *, QDesignerPropertySheetExtension *> m_propertySheet;
    QStringList m_realPropertyNames;
};

// Name of the fake group
static const char *headerGroup = "Header";

// Name of the real properties
static const char *visibleProperty = "visible";
static const char *cascadingSectionResizesProperty = "cascadingSectionResizes";
static const char *defaultSectionSizeProperty = "defaultSectionSize";
static const char *highlightSectionsProperty = "highlightSections";
static const char *minimumSectionSizeProperty = "minimumSectionSize";
static const char *showSortIndicatorProperty = "showSortIndicator";
static const char *stretchLastSectionProperty = "stretchLastSection";
} // namespace qdesigner_internal

using namespace qdesigner_internal;


/***************** ItemViewPropertySheetPrivate *********************/

ItemViewPropertySheetPrivate::ItemViewPropertySheetPrivate(QHeaderView *horizontalHeader,
                                                           QHeaderView *verticalHeader,
                                                           QObject *parent)
        : m_core(formEditorForObject(parent)),
          m_hHeader(horizontalHeader),
          m_vHeader(verticalHeader)
{
    if (horizontalHeader)
        m_propertySheet.insert(horizontalHeader,
                               qt_extension<QDesignerPropertySheetExtension*>
                               (m_core->extensionManager(), horizontalHeader));
    if (verticalHeader)
        m_propertySheet.insert(verticalHeader,
                               qt_extension<QDesignerPropertySheetExtension*>
                               (m_core->extensionManager(), verticalHeader));
}

// Find the form editor in the hierarchy.
// We know that the parent of the sheet is the extension manager
// whose parent is the core.
QDesignerFormEditorInterface *ItemViewPropertySheetPrivate::formEditorForObject(QObject *o)
{
    do {
        if (QDesignerFormEditorInterface* core = qobject_cast<QDesignerFormEditorInterface*>(o))
            return core;
        o = o->parent();
    } while(o);
    Q_ASSERT(o);
    return 0;
}

void ItemViewPropertySheetPrivate::createMapping(int fakeId, QHeaderView *header,
                                                 const QString &headerName)
{
    const int realPropertyId = m_propertySheet.value(header)->indexOf(headerName);
    QDesignerPropertySheetExtension *propertySheet = m_propertySheet.value(header);
    m_propertyIdMap.insert(fakeId, Property(propertySheet, realPropertyId));
}

QStringList ItemViewPropertySheetPrivate::realPropertyNames()
{
    if (m_realPropertyNames.isEmpty())
        m_realPropertyNames
            << QLatin1String(visibleProperty)
            << QLatin1String(cascadingSectionResizesProperty)
            << QLatin1String(defaultSectionSizeProperty)
            << QLatin1String(highlightSectionsProperty)
            << QLatin1String(minimumSectionSizeProperty)
            << QLatin1String(showSortIndicatorProperty)
            << QLatin1String(stretchLastSectionProperty);
    return m_realPropertyNames;
}

QString ItemViewPropertySheetPrivate::fakePropertyName(const QString &prefix,
                                                       const QString &realName)
{
    // prefix = "header", realPropertyName = "isVisible" returns "headerIsVisible"
    QString fakeName = prefix + realName.at(0).toUpper() + realName.mid(1);
    m_propertyNameMap.insert(fakeName, realName);
    return fakeName;
}

/***************** ItemViewPropertySheet *********************/

/*!
  \class qdesigner_internal::ItemViewPropertySheet

  \brief
    Adds header fake properties to QTreeView and QTableView objects

    QHeaderView objects are currently not shown in the object inspector.
    This class adds some fake properties to the property sheet
    of QTreeView and QTableView objects that nevertheless allow the manipulation
    of the headers attached to the item view object.

    Currently the defaultAlignment property is not shown because the property sheet
    would only show integers, instead of the Qt::Alignment enumeration.

    The fake properties here need special handling in QDesignerResource, uiloader and uic.
  */

ItemViewPropertySheet::ItemViewPropertySheet(QTreeView *treeViewObject, QObject *parent)
        : QDesignerPropertySheet(treeViewObject, parent),
        d(new ItemViewPropertySheetPrivate(treeViewObject->header(), 0, parent))
{
    QHeaderView *hHeader = treeViewObject->header();

    foreach (const QString &realPropertyName, d->realPropertyNames()) {
        const QString fakePropertyName
                = d->fakePropertyName(QLatin1String("header"), realPropertyName);
        d->createMapping(createFakeProperty(fakePropertyName, 0), hHeader, realPropertyName);
    }

    foreach (int id, d->m_propertyIdMap.keys()) {
        setAttribute(id, true);
        setPropertyGroup(id, QLatin1String(headerGroup));
    }
}


ItemViewPropertySheet::ItemViewPropertySheet(QTableView *tableViewObject, QObject *parent)
        : QDesignerPropertySheet(tableViewObject, parent),
        d(new ItemViewPropertySheetPrivate(tableViewObject->horizontalHeader(),
                                           tableViewObject->verticalHeader(), parent))
{
    QHeaderView *hHeader = tableViewObject->horizontalHeader();
    QHeaderView *vHeader = tableViewObject->verticalHeader();

    foreach (const QString &realPropertyName, d->realPropertyNames()) {
        const QString fakePropertyName
                = d->fakePropertyName(QLatin1String("horizontalHeader"), realPropertyName);
        d->createMapping(createFakeProperty(fakePropertyName, 0), hHeader, realPropertyName);
    }
    foreach (const QString &realPropertyName, d->realPropertyNames()) {
        const QString fakePropertyName
                = d->fakePropertyName(QLatin1String("verticalHeader"), realPropertyName);
        d->createMapping(createFakeProperty(fakePropertyName, 0), vHeader, realPropertyName);
    }

    foreach (int id, d->m_propertyIdMap.keys()) {
        setAttribute(id, true);
        setPropertyGroup(id, QLatin1String(headerGroup));
    }
}

ItemViewPropertySheet::~ItemViewPropertySheet()
{
    delete d;
}

/*!
  Returns the mapping of fake property names to real property names
  */
QHash<QString,QString> ItemViewPropertySheet::propertyNameMap() const
{
    return d->m_propertyNameMap;
}

QVariant ItemViewPropertySheet::property(int index) const
{
    if (d->m_propertyIdMap.contains(index)) {
        Property realProperty = d->m_propertyIdMap.value(index);
        return realProperty.m_sheet->property(realProperty.m_id);
    } else {
        return QDesignerPropertySheet::property(index);
    }
}

void ItemViewPropertySheet::setProperty(int index, const QVariant &value)
{
    if (d->m_propertyIdMap.contains(index)) {
        Property realProperty = d->m_propertyIdMap.value(index);
        realProperty.m_sheet->setProperty(realProperty.m_id, value);
    } else {
        QDesignerPropertySheet::setProperty(index, value);
    }
}

void ItemViewPropertySheet::setChanged(int index, bool changed)
{
    if (d->m_propertyIdMap.contains(index)) {
        Property realProperty = d->m_propertyIdMap.value(index);
        realProperty.m_sheet->setChanged(realProperty.m_id, changed);
    }
    QDesignerPropertySheet::setChanged(index, changed);
}

bool ItemViewPropertySheet::reset(int index)
{
    if (d->m_propertyIdMap.contains(index)) {
        Property realProperty = d->m_propertyIdMap.value(index);
        return realProperty.m_sheet->reset(realProperty.m_id);
    } else {
        return QDesignerPropertySheet::reset(index);
    }
}

QT_END_NAMESPACE