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>();
55 connect(m_itemBuilder.get(), &ItemBuilder::finished,
this, &AxisLabels::scheduleLayout);
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) {
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();
151void AxisLabels::geometryChange(
const QRectF &newGeometry,
const QRectF &oldGeometry)
155 if (newGeometry != oldGeometry) {
160void AxisLabels::scheduleLayout()
162 if (!m_layoutScheduled) {
163 auto scheduleLayoutLambda = [
this]() {
165 m_layoutScheduled =
false;
168 m_layoutScheduled =
true;
172bool AxisLabels::isHorizontal()
174 return m_direction == Direction::HorizontalLeftRight || m_direction == Direction::HorizontalRightLeft;
177void AxisLabels::updateLabels()
179 m_itemBuilder->clear();
181 if (!m_itemBuilder->component() || !m_source) {
185 m_itemBuilder->setCount(m_source->itemCount());
186 m_itemBuilder->build(
this);
189void AxisLabels::layout()
191 if (!m_itemBuilder->isFinished()) {
197 auto totalWidth = 0.0;
198 auto maxHeight = 0.0;
199 auto totalHeight = 0.0;
201 auto labels = m_itemBuilder->items();
202 for (
auto label : labels) {
203 maxWidth = std::max(maxWidth,
label->width());
204 maxHeight = std::max(maxHeight,
label->height());
205 totalWidth +=
label->width();
206 totalHeight +=
label->height();
209 auto impWidth = isHorizontal() ? totalWidth : maxWidth;
210 auto impHeight = isHorizontal() ? maxHeight : totalHeight;
212 if (qFuzzyCompare(impWidth,
width()) && qFuzzyCompare(impHeight,
height())) {
219 auto spacing = (isHorizontal() ?
width() :
height()) / (labels.
size() - 1);
221 auto layoutWidth = isHorizontal() ? 0.0 :
width();
222 auto layoutHeight = isHorizontal() ?
height() : 0.0;
224 for (
auto label : labels) {
228 switch (m_direction) {
229 case Direction::HorizontalLeftRight:
232 case Direction::HorizontalRightLeft:
233 x =
width() - i * spacing;
235 case Direction::VerticalTopBottom:
238 case Direction::VerticalBottomTop:
244 x += (layoutWidth -
label->width()) / 2;
246 x += layoutWidth -
label->width();
250 y += (layoutHeight -
label->height()) / 2;
252 y += layoutHeight -
label->height();
255 if (m_constrainToBounds) {
256 x = std::max(
x, 0.0);
258 y = std::max(
y, 0.0);
268void AxisLabels::onBeginCreate(
int index,
QQuickItem *item)
283 auto attached =
static_cast<AxisLabelsAttached *
>(qmlAttachedPropertiesObject<AxisLabels>(item,
true));
284 attached->setIndex(index);
285 attached->setLabel(m_source->item(index).
toString());
288#include "moc_AxisLabels.cpp"
Abstract base class for data sources.
QString label(StandardShortcut id)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool disconnect(const QMetaObject::Connection &connection)
virtual void geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry)
void setImplicitHeight(qreal)
void setImplicitWidth(qreal)
QSizeF size() const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
QString toString() const const