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