1// Copyright (C) 2023 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4#include <QtGraphs/QAbstractAxis>
5#include <private/qabstractaxis_p.h>
6
7QT_BEGIN_NAMESPACE
8
9/*!
10 \class QAbstractAxis
11 \inmodule QtGraphs
12 \ingroup graphs_2D
13 \brief The QAbstractAxis class is a base class used for specialized axis classes.
14
15 Each series can be bound to only one horizontal and vertical axis.
16
17 The properties and visibility of various axis elements, such as axis line, title, labels,
18 and grid lines, can be individually controlled.
19*/
20
21/*!
22 \qmltype AbstractAxis
23 \nativetype QAbstractAxis
24 \inqmlmodule QtGraphs
25 \ingroup graphs_qml_2D
26
27 \brief A base type used for specialized axis types.
28
29 Each series can be bound to only one horizontal and vertical axis.
30
31 The properties and visibility of various axis elements, such as axis line, title, labels,
32 and grid lines, can be individually controlled.
33*/
34
35/*!
36 \enum QAbstractAxis::AxisType
37
38 This enum type specifies the type of the axis object.
39
40 \value Value
41 \value BarCategory
42 \value DateTime
43*/
44
45/*!
46 \fn void QAbstractAxis::type() const
47 Returns the type of the axis.
48*/
49
50/*!
51 \property QAbstractAxis::visible
52 \brief The visibility of the axis.
53 By default, the value is \c true.
54*/
55/*!
56 \qmlproperty bool AbstractAxis::visible
57 The visibility of the axis. By default, the value is \c true.
58*/
59
60/*!
61 \property QAbstractAxis::lineVisible
62 \brief The visibility of the axis line.
63 By default, the value is \c true.
64*/
65/*!
66 \qmlproperty bool AbstractAxis::lineVisible
67 The visibility of the axis line. By default, the value is \c true.
68*/
69
70/*!
71 \property QAbstractAxis::labelsVisible
72 \brief Whether axis labels are visible.
73 By default, the value is \c true.
74*/
75/*!
76 \qmlproperty bool AbstractAxis::labelsVisible
77 The visibility of axis labels. By default, the value is \c true.
78*/
79
80/*!
81 \property QAbstractAxis::labelsAngle
82 \brief The angle of the axis labels in degrees.
83*/
84/*!
85 \qmlproperty real AbstractAxis::labelsAngle
86 The angle of the axis labels in degrees.
87*/
88
89/*!
90 \property QAbstractAxis::labelDelegate
91 \brief A custom QML Component used as a label for the axis.
92 This component should either be a \a Text component or contain "property string text",
93 so that this property will be assigned the value of the label.
94*/
95/*!
96 \qmlproperty Component AbstractAxis::labelDelegate
97 A custom QML Component used as a label for the axis.
98 This component should either be a \a Text component or contain "property string text",
99 so that this property will be assigned the value of the label.
100*/
101
102/*!
103 \property QAbstractAxis::gridVisible
104 \brief The visibility of the grid lines.
105 By default, the value is \c true.
106*/
107/*!
108 \qmlproperty bool AbstractAxis::gridVisible
109 The visibility of the grid lines. By default, the value is \c true.
110*/
111
112/*!
113 \property QAbstractAxis::subGridVisible
114 \brief The visibility of the subgrid lines.
115
116 Applies only to axes that support subgrid lines.
117 By default, the value is \c true.
118*/
119/*!
120 \qmlproperty bool AbstractAxis::subGridVisible
121 The visibility of the subgrid lines. Applies only to axes that support
122 subgrid lines. By default, the value is \c true.
123*/
124
125/*!
126 \property QAbstractAxis::titleText
127 \brief The title of the axis.
128
129 Empty by default. Axis titles support HTML formatting.
130*/
131/*!
132 \qmlproperty string AbstractAxis::titleText
133 The title of the axis. Empty by default. Axis titles support HTML formatting.
134*/
135
136/*!
137 \property QAbstractAxis::titleColor
138 \brief The color used to draw the title text.
139*/
140/*!
141 \qmlproperty color AbstractAxis::titleColor
142 The color used to draw the title text.
143*/
144
145/*!
146 \property QAbstractAxis::titleVisible
147 \brief The visibility of the axis title.
148
149 By default, the value is \c true.
150*/
151/*!
152 \qmlproperty bool AbstractAxis::titleVisible
153 The visibility of the axis title. By default, the value is \c true.
154*/
155
156/*!
157 \property QAbstractAxis::titleFont
158 \brief The font of the title of the axis.
159*/
160/*!
161 \qmlproperty font AbstractAxis::titleFont
162 The font of the title of the axis.
163*/
164
165
166/*!
167 \property QAbstractAxis::alignment
168 \since 6.9
169 \brief The alignment of the axis.
170
171 Can be Qt::AlignLeft, Qt::AlignRight, Qt::AlignBottom, or Qt::AlignTop.
172*/
173/*!
174 \qmlproperty alignment AbstractAxis::alignment
175 \since 6.9
176 The alignment of the axis. Can be \l{Qt::AlignLeft}{Qt.AlignLeft},
177 \l{Qt::AlignRight}{Qt.AlignRight}, \l{Qt::AlignBottom}{Qt.AlignBottom}, or
178 \l{Qt::AlignTop}{Qt.AlignTop}.
179*/
180
181/*!
182 \property QAbstractAxis::textElideMode
183 \since 6.10
184 \brief Property specifies ellipsis placement for overflowing text.
185
186 Can be Qt::ElideLeft, Qt::ElideRight, Qt::ElideMiddle, or Qt::ElideNone.
187 By default, the value is \c Qt::ElideNone.
188*/
189/*!
190 \qmlproperty enumeration AbstractAxis::textElideMode
191 \since 6.10
192 This enum specifies ellipsis placement for overflowing text.
193 Can be \l{Qt::ElideLeft}{Qt.ElideLeft}, \l{Qt::ElideRight}{Qt.ElideRight},
194 \l{Qt::ElideMiddle}{Qt.ElideMiddle}, or \l{Qt::ElideNone}{Qt.ElideNone}.
195 By default, the value is \c Qt.ElideNone.
196*/
197
198/*!
199 \fn void QAbstractAxis::update()
200 This signal is emitted when the axis needs to be updated.
201*/
202
203/*!
204 \qmlsignal AbstractAxis::visibleChanged(bool visible)
205 This signal is emitted when the visibility of the axis changes to \a visible.
206*/
207
208/*!
209 \qmlsignal AbstractAxis::lineVisibleChanged(bool visible)
210 This signal is emitted when the visibility of the axis line changes to \a visible.
211*/
212
213/*!
214 \qmlsignal AbstractAxis::labelsVisibleChanged(bool visible)
215 This signal is emitted when the visibility of the labels of the axis changes to \a visible.
216*/
217
218/*!
219 \qmlsignal AbstractAxis::labelsAngleChanged(string angle)
220 This signal is emitted when the angle of the axis labels changes to \a angle.
221*/
222
223/*!
224 \qmlsignal AbstractAxis::gridVisibleChanged(bool visible)
225 This signal is emitted when the visibility of the grid lines of the axis changes to \a visible.
226*/
227
228/*!
229 \qmlsignal AbstractAxis::minorGridVisibleChanged(bool visible)
230 This signal is emitted when the visibility of the minor grid lines of the axis
231 changes to \a visible.
232*/
233
234/*!
235 \qmlsignal AbstractAxis::titleTextChanged(string text)
236 This signal is emitted when the text of the axis title changes to \a text.
237*/
238
239/*!
240 \qmlsignal AbstractAxis::titleColorChanged(color color)
241 This signal is emitted when the color used to draw the axis title changes to \a color.
242*/
243
244/*!
245 \qmlsignal AbstractAxis::titleVisibleChanged(bool visible)
246 This signal is emitted when the visibility of the title text of the axis changes to \a visible.
247*/
248
249/*!
250 \qmlsignal AbstractAxis::titleFontChanged(Font font)
251 This signal is emitted when the font of the axis title changes to \a font.
252*/
253
254/*!
255 \qmlsignal AbstractAxis::rangeChanged(string min, string max)
256 This signal is emitted when the axis range changes. \a min and \a max are
257 the min and max of the new range.
258*/
259
260/*!
261 \qmlsignal AbstractAxis::textElideModeChanged(enumeration elideMode)
262 This signal is emitted when the \l textElideMode changes. \a elideMode is the
263 new value of the \l Qt::TextElideMode type.
264*/
265
266/*!
267 \internal
268 Constructs a new axis object that is a child of \a parent. The ownership is taken by
269 graph when the axis is added.
270*/
271
272Q_LOGGING_CATEGORY(lcAxis2D, "qt.graphs2d.axis.properties")
273
274QAbstractAxis::QAbstractAxis(QAbstractAxisPrivate &dd, QObject *parent)
275 : QObject(dd, parent)
276{}
277
278/*!
279 Destructs the axis object. When the axis is added to a graph, the graph object takes ownership.
280*/
281
282QAbstractAxis::~QAbstractAxis()
283{
284}
285
286/*!
287 Determines whether the axis line and tick marks are \a visible.
288 */
289void QAbstractAxis::setLineVisible(bool visible)
290{
291 Q_D(QAbstractAxis);
292 if (d->m_lineVisible != visible) {
293 d->m_lineVisible = visible;
294 emit update();
295 emit lineVisibleChanged(visible);
296 } else {
297 qCDebug(lcAxis2D) << "QAbstractAxis::setLineVisible. Visibility is already set to:"
298 << visible;
299 }
300}
301
302bool QAbstractAxis::isLineVisible() const
303{
304 Q_D(const QAbstractAxis);
305 return d->m_lineVisible;
306}
307
308void QAbstractAxis::setGridVisible(bool visible)
309{
310 Q_D(QAbstractAxis);
311 if (d->m_gridVisible != visible) {
312 d->m_gridVisible = visible;
313 emit update();
314 emit gridVisibleChanged(visible);
315 } else {
316 qCDebug(lcAxis2D) << "QAbstractAxis::setGridVisible. Visibility is already set to:"
317 << visible;
318 }
319}
320
321bool QAbstractAxis::isGridVisible() const
322{
323 Q_D(const QAbstractAxis);
324 return d->m_gridVisible;
325}
326
327void QAbstractAxis::setSubGridVisible(bool visible)
328{
329 Q_D(QAbstractAxis);
330 if (d->m_subGridVisible != visible) {
331 d->m_subGridVisible = visible;
332 emit update();
333 emit subGridVisibleChanged(visible);
334 } else {
335 qCDebug(lcAxis2D) << "QAbstractAxis::setSubGridVisible. Visibility is already set to:"
336 << visible;
337 }
338}
339
340bool QAbstractAxis::isSubGridVisible() const
341{
342 Q_D(const QAbstractAxis);
343 return d->m_subGridVisible;
344}
345
346void QAbstractAxis::setLabelsVisible(bool visible)
347{
348 Q_D(QAbstractAxis);
349 if (d->m_labelsVisible != visible) {
350 d->m_labelsVisible = visible;
351 emit update();
352 emit labelsVisibleChanged(visible);
353 } else {
354 qCDebug(lcAxis2D) << "QAbstractAxis::setLabelsVisible. Visibility is already set to:"
355 << visible;
356 }
357}
358
359bool QAbstractAxis::labelsVisible() const
360{
361 Q_D(const QAbstractAxis);
362 return d->m_labelsVisible;
363}
364
365void QAbstractAxis::setLabelsAngle(qreal angle)
366{
367 Q_D(QAbstractAxis);
368 if (d->m_labelsAngle != angle) {
369 d->m_labelsAngle = angle;
370 emit update();
371 emit labelsAngleChanged(angle);
372 } else {
373 qCDebug(lcAxis2D, "QAbstractAxis::setLabelsAngle. Angle is already set to: %f",
374 angle);
375 }
376}
377
378qreal QAbstractAxis::labelsAngle() const
379{
380 Q_D(const QAbstractAxis);
381 return d->m_labelsAngle;
382}
383
384QQmlComponent *QAbstractAxis::labelDelegate() const
385{
386 Q_D(const QAbstractAxis);
387 return d->m_labelDelegate;
388}
389
390void QAbstractAxis::setLabelDelegate(QQmlComponent *newLabelDelegate)
391{
392 Q_D(QAbstractAxis);
393 if (d->m_labelDelegate == newLabelDelegate) {
394 qCDebug(lcAxis2D) << "QAbstractAxis::setLabelDelegate. LabelDelegate is already set to:"
395 << newLabelDelegate;
396 return;
397 }
398
399 d->m_labelDelegate = newLabelDelegate;
400 emit labelDelegateChanged();
401 emit update();
402}
403
404void QAbstractAxis::setTitleVisible(bool visible)
405{
406 Q_D(QAbstractAxis);
407 if (d->m_titleVisible != visible) {
408 d->m_titleVisible = visible;
409 emit update();
410 emit titleVisibleChanged(visible);
411 } else {
412 qCDebug(lcAxis2D) << "QAbstractAxis::setTitleVisible. visibility is already set to:"
413 << visible;
414 }
415}
416
417bool QAbstractAxis::isTitleVisible() const
418{
419 Q_D(const QAbstractAxis);
420 return d->m_titleVisible;
421}
422
423/*!
424 Sets the color used to draw titles to \a color.
425 */
426void QAbstractAxis::setTitleColor(QColor color)
427{
428 Q_D(QAbstractAxis);
429 if (!color.isValid())
430 qCWarning(lcAxis2D) << "QAbstractAxis::setTitleColor. Tried to use invalid color.";
431
432 if (d->m_titleColor != color) {
433 d->m_titleColor = color;
434 emit update();
435 emit titleColorChanged(color);
436 } else {
437 qCDebug(lcAxis2D) << "QAbstractAxis::setTitleColor. Color is already set to:"
438 << color;
439 }
440}
441
442/*!
443 Returns the color used to draw titles.
444*/
445QColor QAbstractAxis::titleColor() const
446{
447 Q_D(const QAbstractAxis);
448 return d->m_titleColor;
449}
450
451/*!
452 Sets the font used to draw titles to \a font.
453*/
454void QAbstractAxis::setTitleFont(const QFont &font)
455{
456 Q_D(QAbstractAxis);
457 if (d->m_titleFont != font) {
458 d->m_titleFont = font;
459 emit update();
460 emit titleFontChanged(font);
461 } else {
462 qCDebug(lcAxis2D) << "QAbstractAxis::setTitleFont. Font is already set to:"
463 << font;
464 }
465}
466
467/*!
468 Returns the font used to draw titles.
469*/
470QFont QAbstractAxis::titleFont() const
471{
472 Q_D(const QAbstractAxis);
473 return d->m_titleFont;
474}
475
476void QAbstractAxis::setTitleText(const QString &title)
477{
478 Q_D(QAbstractAxis);
479 if (d->m_title != title) {
480 d->m_title = title;
481 emit update();
482 emit titleTextChanged(title);
483 } else {
484 qCDebug(lcAxis2D, "QAbstractAxis::setTitleText. Title is already set to: %s",
485 qPrintable(title));
486 }
487}
488
489QString QAbstractAxis::titleText() const
490{
491 Q_D(const QAbstractAxis);
492 return d->m_title;
493}
494
495bool QAbstractAxis::isVisible() const
496{
497 Q_D(const QAbstractAxis);
498 return d->m_visible;
499}
500
501/*!
502 Sets the visibility of the axis, labels, and grid lines to \a visible.
503*/
504void QAbstractAxis::setVisible(bool visible)
505{
506 Q_D(QAbstractAxis);
507 if (d->m_visible != visible) {
508 d->m_visible = visible;
509 emit update();
510 emit visibleChanged(visible);
511 } else {
512 qCDebug(lcAxis2D) << "QAbstractAxis::setVisible. axis, labels and grid visibility is already set to:"
513 << visible;
514 }
515}
516
517
518/*!
519 Makes the axis, labels, and grid lines visible.
520*/
521void QAbstractAxis::show()
522{
523 setVisible(true);
524}
525
526/*!
527 Makes the axis, labels, and grid lines invisible.
528*/
529void QAbstractAxis::hide()
530{
531 setVisible(false);
532}
533
534/*!
535 Sets the minimum value shown on the axis.
536 Depending on the actual axis type, the \a min parameter is converted to the appropriate type
537 of value. If the conversion is impossible, the function call does nothing.
538*/
539void QAbstractAxis::setMin(const QVariant &min)
540{
541 Q_D(QAbstractAxis);
542 d->setMin(min);
543}
544
545/*!
546 Sets the maximum value shown on the axis.
547 Depending on the actual axis type, the \a max parameter is converted to the appropriate type
548 of value. If the conversion is impossible, the function call does nothing.
549*/
550void QAbstractAxis::setMax(const QVariant &max)
551{
552 Q_D(QAbstractAxis);
553 d->setMax(max);
554}
555
556/*!
557 Sets the range shown on the axis.
558 Depending on the actual axis type, the \a min and \a max parameters are converted to
559 appropriate types of values. If the conversion is impossible, the function call does nothing.
560*/
561void QAbstractAxis::setRange(const QVariant &min, const QVariant &max)
562{
563 Q_D(QAbstractAxis);
564 d->setRange(min, max);
565}
566
567Qt::Alignment QAbstractAxis::alignment() const
568{
569 const Q_D(QAbstractAxis);
570 return d->m_alignment;
571}
572
573void QAbstractAxis::setAlignment(Qt::Alignment alignment)
574{
575 Q_D(QAbstractAxis);
576 if (d->m_alignment == alignment) {
577 qCDebug(lcAxis2D) << "QAbstractAxis::setAlignment. Alignment is already set to:"
578 << alignment;
579 return;
580 }
581 switch (alignment) {
582 case Qt::AlignTop:
583 case Qt::AlignBottom:
584 case Qt::AlignLeft:
585 case Qt::AlignRight:
586 d->m_alignment = alignment;
587 if (d->m_graph)
588 d->m_graph->updateComponentSizes();
589 emit update();
590 emit alignmentChanged(alignment);
591 break;
592 default:
593 qCWarning(lcAxis2D, "invalid alignment.");
594 break;
595 }
596}
597
598Qt::TextElideMode QAbstractAxis::textElideMode() const
599{
600 Q_D(const QAbstractAxis);
601 return d->m_textElide;
602}
603
604void QAbstractAxis::setTextElideMode(Qt::TextElideMode elide)
605{
606 Q_D(QAbstractAxis);
607 if (d->m_textElide == elide) {
608 qCDebug(lcAxis2D) << "QAbstractAxis::setTextElide. Text elide is already set to:"
609 << elide;
610 return;
611 }
612
613 d->m_textElide = elide;
614 Q_EMIT textElideModeChanged(elideMode: d->m_textElide);
615}
616
617/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
618
619QAbstractAxisPrivate::QAbstractAxisPrivate() {}
620
621QAbstractAxisPrivate::~QAbstractAxisPrivate()
622{
623 Q_Q(QAbstractAxis);
624 if (m_graph)
625 m_graph->removeAxis(axis: q);
626}
627
628void QAbstractAxisPrivate::handleRangeChanged(qreal min, qreal max)
629{
630 setRange(min,max);
631}
632
633QT_END_NAMESPACE
634
635
636

source code of qtgraphs/src/graphs2d/axis/qabstractaxis.cpp