13#include "ItemBuilder.h"
14#include "datasource/ChartDataSource.h"
16AxisLabelsAttached::AxisLabelsAttached(
QObject *parent)
21int AxisLabelsAttached::index()
const
26void AxisLabelsAttached::setIndex(
int newIndex)
28 if (newIndex == m_index) {
36QString AxisLabelsAttached::label()
const
41void AxisLabelsAttached::setLabel(
const QString &newLabel)
43 if (newLabel == m_label) {
54 m_itemBuilder = std::make_unique<ItemBuilder>();
56 connect(m_itemBuilder.get(), &ItemBuilder::beginCreate,
this, &AxisLabels::onBeginCreate);
59AxisLabels::~AxisLabels() =
default;
61AxisLabels::Direction AxisLabels::direction()
const
66void AxisLabels::setDirection(AxisLabels::Direction newDirection)
68 if (newDirection == m_direction) {
72 m_direction = newDirection;
79 return m_itemBuilder->component();
84 if (newDelegate == m_itemBuilder->component()) {
88 m_itemBuilder->setComponent(newDelegate);
100 if (newSource == m_source) {
105 m_source->disconnect(
this);
108 m_source = newSource;
111 connect(m_source, &ChartDataSource::dataChanged,
this, [
this]() {
127 if (newAlignment == m_alignment) {
131 m_alignment = newAlignment;
133 Q_EMIT alignmentChanged();
136bool AxisLabels::constrainToBounds()
const
138 return m_constrainToBounds;
141void AxisLabels::setConstrainToBounds(
bool newConstrainToBounds)
143 if (newConstrainToBounds == m_constrainToBounds) {
147 m_constrainToBounds = newConstrainToBounds;
149 Q_EMIT constrainToBoundsChanged();
152void AxisLabels::updatePolish()
154 if (!m_itemBuilder->isFinished()) {
159 auto totalWidth = 0.0;
160 auto maxHeight = 0.0;
161 auto totalHeight = 0.0;
163 auto labels = m_itemBuilder->items();
164 for (
auto label : labels) {
165 maxWidth = std::max(maxWidth,
label->implicitWidth());
166 maxHeight = std::max(maxHeight,
label->implicitHeight());
167 totalWidth +=
label->implicitWidth();
168 totalHeight +=
label->implicitHeight();
171 auto impWidth = isHorizontal() ? totalWidth : maxWidth;
172 auto impHeight = isHorizontal() ? maxHeight : totalHeight;
174 if (qFuzzyCompare(impWidth,
width()) && qFuzzyCompare(impHeight,
height())) {
178 setImplicitSize(impWidth, impHeight);
180 auto spacing = (isHorizontal() ?
width() :
height()) / (labels.
size() - 1);
182 auto layoutWidth = isHorizontal() ? 0.0 :
width();
183 auto layoutHeight = isHorizontal() ?
height() : 0.0;
185 for (
auto label : labels) {
189 switch (m_direction) {
190 case Direction::HorizontalLeftRight:
193 case Direction::HorizontalRightLeft:
194 x =
width() - i * spacing;
196 case Direction::VerticalTopBottom:
199 case Direction::VerticalBottomTop:
205 x += (layoutWidth -
label->implicitWidth()) / 2;
207 x += layoutWidth -
label->implicitWidth();
211 y += (layoutHeight -
label->implicitHeight()) / 2;
213 y += layoutHeight -
label->implicitHeight();
216 if (m_constrainToBounds) {
217 x = std::max(
x, 0.0);
219 y = std::max(
y, 0.0);
230void AxisLabels::geometryChange(
const QRectF &newGeometry,
const QRectF &oldGeometry)
234 if (newGeometry != oldGeometry) {
239bool AxisLabels::isHorizontal()
241 return m_direction == Direction::HorizontalLeftRight || m_direction == Direction::HorizontalRightLeft;
244void AxisLabels::updateLabels()
246 m_itemBuilder->clear();
248 if (!m_itemBuilder->component() || !m_source) {
252 m_itemBuilder->setCount(m_source->itemCount());
253 m_itemBuilder->build(
this);
256void AxisLabels::onBeginCreate(
int index,
QQuickItem *item)
261 auto attached =
static_cast<AxisLabelsAttached *
>(qmlAttachedPropertiesObject<AxisLabels>(item,
true));
262 attached->setIndex(index);
263 attached->setLabel(m_source->item(index).toString());
266#include "moc_AxisLabels.cpp"
Abstract base class for data sources.
QString label(StandardShortcut id)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
virtual void geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry)
void implicitHeightChanged()
void implicitWidthChanged()
QSizeF size() const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)