blob: 9bfef63ae0a4c2b5077fed8d6582476ceea86dc7 (
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
|
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include "tickmarklabelcontainer.h"
#include <QQmlInfo>
QT_BEGIN_NAMESPACE
TickmarkLabelContainer::TickmarkLabelContainer(QQuickItem *parent)
: TickmarkContainer(parent)
{
}
QFont TickmarkLabelContainer::font() const
{
return mFont;
}
void TickmarkLabelContainer::setFont(const QFont &font)
{
if (mFont == font)
return;
mFont = font;
makeDirtyAndPolish(DelegateItemDirtyFlag::FontDirty | DelegateItemDirtyFlag::GeometryDirty);
emit fontChanged();
}
void TickmarkLabelContainer::resetFont()
{
setFont(QFont());
}
void TickmarkLabelContainer::recreateDelegateItems()
{
destroyDelegateItems();
int largestImplicitWidth = 0;
QQmlContext *ourQmlContext = qmlContext(this);
for (int i = 0; i < mCount; ++i) {
auto *label = qobject_cast<QQuickItem *>(mDelegate->beginCreate(ourQmlContext));
if (!label) {
qmlWarning(this) << "Failed to create delegate:\n" << mDelegate->errors();
destroyDelegateItems();
return;
}
const QVariantMap initialProperties = {
{ "parent", QVariant::fromValue(this) },
{ "value", valueForDelegateItemAt(i) }
};
mDelegate->setInitialProperties(label, initialProperties);
if (label->property("index").isValid())
label->setProperty("index", i);
mDelegateItems.append(label);
mDelegate->completeCreate();
// The position depends on the size of the label, which we can't know before creating it.
label->setPosition(positionForDelegateItemAt(i));
if (label->implicitWidth() > largestImplicitWidth)
largestImplicitWidth = label->implicitWidth();
}
if (isVertical()) {
setImplicitWidth(largestImplicitWidth);
for (auto *label : std::as_const(mDelegateItems))
label->setWidth(largestImplicitWidth);
} else if (!mDelegateItems.isEmpty()) { // Horizontal
const auto *firstLabel = mDelegateItems.first();
// All labels should have the same implicitHeight, so just use the first one.
setImplicitHeight(firstLabel ? firstLabel->implicitHeight() : 0);
}
}
void TickmarkLabelContainer::updatePolish()
{
QQuickItem::updatePolish();
if (mDirtyFlags.testFlag(DelegateItemDirtyFlag::AllDirty)) {
recreateDelegateItems();
return;
}
Q_ASSERT(mDelegateItems.size() == mCount);
const bool fontDirty = mDirtyFlags.testFlag(DelegateItemDirtyFlag::FontDirty);
const bool valueDirty = mDirtyFlags.testFlag(DelegateItemDirtyFlag::ValueDirty);
const bool geometryDirty = mDirtyFlags.testFlag(DelegateItemDirtyFlag::GeometryDirty);
int largestImplicitWidth = 0;
for (int i = 0; i < mCount; ++i) {
auto *label = mDelegateItems.at(i);
if (fontDirty) {
const bool setSuccessfully = label->setProperty("font", mFont);
if (!setSuccessfully)
qmlWarning(this) << "Failed to set font property of label at index" << i;
}
if (valueDirty) {
const bool setSuccessfully = label->setProperty("value", valueForDelegateItemAt(i));
if (!setSuccessfully)
qmlWarning(this) << "Failed to set value property of label at index" << i;
}
if (geometryDirty) {
label->setPosition(positionForDelegateItemAt(i));
if (label->implicitWidth() > largestImplicitWidth)
largestImplicitWidth = label->implicitWidth();
}
}
if (geometryDirty)
updateSizes(largestImplicitWidth);
}
QT_END_NAMESPACE
|