Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
itemview_propertysheet.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3
5
6#include <QtDesigner/abstractformeditor.h>
7
8#include <QtWidgets/qabstractitemview.h>
9#include <QtWidgets/qheaderview.h>
10#include <QtCore/qdebug.h>
11
13
14using namespace Qt::StringLiterals;
15
16namespace qdesigner_internal {
17
18struct Property {
19 Property() = default;
20 Property(QDesignerPropertySheetExtension *sheet, int id)
21 : m_sheet(sheet), m_id(id) {}
22
24 int m_id{-1};
25};
26
28 ItemViewPropertySheetPrivate(QDesignerFormEditorInterface *core,
29 QHeaderView *horizontalHeader,
30 QHeaderView *verticalHeader);
31
33 inline QString fakePropertyName(const QString &prefix, const QString &realName);
34
35 // Maps index of fake property to index of real property in respective sheet
37
38 // Maps name of fake property to name of real property
40
43};
44
45// Name of the fake group
46static constexpr auto headerGroup = "Header"_L1;
47
48// Name of the real properties
49static constexpr auto visibleProperty = "visible"_L1;
50static constexpr auto cascadingSectionResizesProperty = "cascadingSectionResizes"_L1;
51static constexpr auto defaultSectionSizeProperty = "defaultSectionSize"_L1;
52static constexpr auto highlightSectionsProperty = "highlightSections"_L1;
53static constexpr auto minimumSectionSizeProperty = "minimumSectionSize"_L1;
54static constexpr auto showSortIndicatorProperty = "showSortIndicator"_L1;
55static constexpr auto stretchLastSectionProperty = "stretchLastSection"_L1;
56
57/***************** ItemViewPropertySheetPrivate *********************/
58
60 QHeaderView *horizontalHeader,
61 QHeaderView *verticalHeader)
62{
63 if (horizontalHeader)
64 m_propertySheet.insert(horizontalHeader,
65 qt_extension<QDesignerPropertySheetExtension*>
66 (core->extensionManager(), horizontalHeader));
67 if (verticalHeader)
68 m_propertySheet.insert(verticalHeader,
69 qt_extension<QDesignerPropertySheetExtension*>
70 (core->extensionManager(), verticalHeader));
71}
72
74{
75 if (m_realPropertyNames.isEmpty())
76 m_realPropertyNames = {
77 visibleProperty, cascadingSectionResizesProperty,
78 defaultSectionSizeProperty, highlightSectionsProperty,
79 minimumSectionSizeProperty, showSortIndicatorProperty,
80 stretchLastSectionProperty
81 };
82 return m_realPropertyNames;
83}
84
85QString ItemViewPropertySheetPrivate::fakePropertyName(const QString &prefix,
86 const QString &realName)
87{
88 // prefix = "header", realPropertyName = "isVisible" returns "headerIsVisible"
89 QString fakeName = prefix + realName.at(0).toUpper() + realName.mid(1);
90 m_propertyNameMap.insert(fakeName, realName);
91 return fakeName;
92}
93
94/***************** ItemViewPropertySheet *********************/
95
96/*!
97 \class qdesigner_internal::ItemViewPropertySheet
98
99 \brief
100 Adds header fake properties to QTreeView and QTableView objects
101
102 QHeaderView objects are currently not shown in the object inspector.
103 This class adds some fake properties to the property sheet
104 of QTreeView and QTableView objects that nevertheless allow the manipulation
105 of the headers attached to the item view object.
106
107 Currently the defaultAlignment property is not shown because the property sheet
108 would only show integers, instead of the Qt::Alignment enumeration.
109
110 The fake properties here need special handling in QDesignerResource, uiloader and uic.
111 */
112
113ItemViewPropertySheet::ItemViewPropertySheet(QTreeView *treeViewObject, QObject *parent)
114 : QDesignerPropertySheet(treeViewObject, parent),
115 d(new ItemViewPropertySheetPrivate(core(), treeViewObject->header(), nullptr))
116{
117 initHeaderProperties(treeViewObject->header(), u"header"_s);
118}
119
120ItemViewPropertySheet::ItemViewPropertySheet(QTableView *tableViewObject, QObject *parent)
121 : QDesignerPropertySheet(tableViewObject, parent),
122 d(new ItemViewPropertySheetPrivate(core(),
123 tableViewObject->horizontalHeader(),
124 tableViewObject->verticalHeader()))
125{
126 initHeaderProperties(tableViewObject->horizontalHeader(), u"horizontalHeader"_s);
127 initHeaderProperties(tableViewObject->verticalHeader(), u"verticalHeader"_s);
128}
129
134
135void ItemViewPropertySheet::initHeaderProperties(QHeaderView *hv, const QString &prefix)
136{
137 QDesignerPropertySheetExtension *headerSheet = d->m_propertySheet.value(hv);
138 Q_ASSERT(headerSheet);
139 const QString headerGroupS = headerGroup;
140 const QStringList &realPropertyNames = d->realPropertyNames();
141 for (const QString &realPropertyName : realPropertyNames) {
142 const int headerIndex = headerSheet->indexOf(realPropertyName);
143 Q_ASSERT(headerIndex != -1);
144 const QVariant defaultValue = realPropertyName == visibleProperty ?
145 QVariant(true) : headerSheet->property(headerIndex);
146 const QString fakePropertyName = d->fakePropertyName(prefix, realPropertyName);
147 const int fakeIndex = createFakeProperty(fakePropertyName, defaultValue);
148 d->m_propertyIdMap.insert(fakeIndex, Property(headerSheet, headerIndex));
149 setAttribute(fakeIndex, true);
150 setPropertyGroup(fakeIndex, headerGroupS);
151 }
152}
153
154/*!
155 Returns the mapping of fake property names to real property names
156 */
158{
159 return d->m_propertyNameMap;
160}
161
163{
164 const auto it = d->m_propertyIdMap.constFind(index);
165 if (it != d->m_propertyIdMap.constEnd())
166 return it.value().m_sheet->property(it.value().m_id);
167 return QDesignerPropertySheet::property(index);
168}
169
170void ItemViewPropertySheet::setProperty(int index, const QVariant &value)
171{
172 const auto it = d->m_propertyIdMap.find(index);
173 if (it != d->m_propertyIdMap.end()) {
174 it.value().m_sheet->setProperty(it.value().m_id, value);
175 } else {
176 QDesignerPropertySheet::setProperty(index, value);
177 }
178}
179
180void ItemViewPropertySheet::setChanged(int index, bool changed)
181{
182 const auto it = d->m_propertyIdMap.find(index);
183 if (it != d->m_propertyIdMap.end()) {
184 it.value().m_sheet->setChanged(it.value().m_id, changed);
185 } else {
186 QDesignerPropertySheet::setChanged(index, changed);
187 }
188}
189
190bool ItemViewPropertySheet::isChanged(int index) const
191{
192 const auto it = d->m_propertyIdMap.constFind(index);
193 if (it != d->m_propertyIdMap.constEnd())
194 return it.value().m_sheet->isChanged(it.value().m_id);
195 return QDesignerPropertySheet::isChanged(index);
196}
197
198bool ItemViewPropertySheet::hasReset(int index) const
199{
200 const auto it = d->m_propertyIdMap.constFind(index);
201 if (it != d->m_propertyIdMap.constEnd())
202 return it.value().m_sheet->hasReset(it.value().m_id);
203 return QDesignerPropertySheet::hasReset(index);
204}
205
207{
208 const auto it = d->m_propertyIdMap.find(index);
209 if (it != d->m_propertyIdMap.end()) {
210 QDesignerPropertySheetExtension *headerSheet = it.value().m_sheet;
211 const int headerIndex = it.value().m_id;
212 const bool resetRC = headerSheet->reset(headerIndex);
213 // Resetting for "visible" might fail and the stored default
214 // of the Widget database is "false" due to the widget not being
215 // visible at the time it was determined. Reset to "true" manually.
216 if (!resetRC && headerSheet->propertyName(headerIndex) == visibleProperty) {
217 headerSheet->setProperty(headerIndex, QVariant(true));
218 headerSheet->setChanged(headerIndex, false);
219 return true;
220 }
221 return resetRC;
222 }
223 return QDesignerPropertySheet::reset(index);
224}
225
226} // namespace qdesigner_internal
227
228QT_END_NAMESPACE
Adds header fake properties to QTreeView and QTableView objects.
void setProperty(int index, const QVariant &value) override
QVariant property(int index) const override
void setChanged(int index, bool changed) override
QHash< QString, QString > propertyNameMap() const
Returns the mapping of fake property names to real property names.
Combined button and popup list for selecting options.
Auxiliary methods to store/retrieve settings.
static constexpr auto highlightSectionsProperty
static constexpr auto cascadingSectionResizesProperty
static constexpr auto minimumSectionSizeProperty
static constexpr auto defaultSectionSizeProperty
static constexpr auto headerGroup
static constexpr auto stretchLastSectionProperty
static constexpr auto showSortIndicatorProperty
static constexpr auto visibleProperty
QHash< QHeaderView *, QDesignerPropertySheetExtension * > m_propertySheet
ItemViewPropertySheetPrivate(QDesignerFormEditorInterface *core, QHeaderView *horizontalHeader, QHeaderView *verticalHeader)
QString fakePropertyName(const QString &prefix, const QString &realName)
QDesignerPropertySheetExtension * m_sheet
Property(QDesignerPropertySheetExtension *sheet, int id)