##// END OF EJS Templates
Added axis to QML api
Tero Ahola -
r1096:1c6e297eef84
parent child
Show More
@@ -0,0 +1,69
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
21 #include "declarativeaxis.h"
22 #include "declarativechart.h"
23 #include <QAxis>
24
25 QTCOMMERCIALCHART_BEGIN_NAMESPACE
26
27 DeclarativeAxis::DeclarativeAxis(QObject *parent) :
28 QAxis(parent),
29 m_role(DeclarativeAxis::RoleX)
30 {
31 }
32
33 void DeclarativeAxis::classBegin()
34 {
35 }
36
37 void DeclarativeAxis::componentComplete()
38 {
39 DeclarativeChart *declarativeChart = qobject_cast<DeclarativeChart *>(parent());
40 if (declarativeChart) {
41 QChart *chart = qobject_cast<QChart *>(declarativeChart->m_chart);
42 Q_ASSERT(chart);
43 QAxis *axis = 0;
44 if (m_role == RoleX)
45 axis = chart->axisX();
46 else
47 axis = chart->axisY();
48 Q_ASSERT(axis);
49 // TODO?
50 //foreach(DeclarativeCategory catecory, categories());
51 //axis->categories()->insert(0, "Jun");
52 // axis->categories()->insert(1, "Jul");
53 // axis->categories()->insert(2, "Aug");
54 // axis->categories()->insert(3, "Sep");
55 // axis->categories()->insert(4, "Oct");
56 // axis->categories()->insert(5, "Nov");
57 //axis->categories()->insert(6, "Dec");
58 }
59 }
60
61 void DeclarativeAxis::setRole(AxisRole role)
62 {
63 // TODO: apply the role to possible parent chart
64 m_role = role;
65 }
66
67 #include "moc_declarativeaxis.cpp"
68
69 QTCOMMERCIALCHART_END_NAMESPACE
@@ -0,0 +1,58
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
21 #ifndef DECLARATIVE_AXIS_H
22 #define DECLARATIVE_AXIS_H
23
24 #include "qchartglobal.h"
25 #include <QAxis>
26 #include <QDeclarativeParserStatus>
27
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29
30 class DeclarativeAxis: public QAxis, public QDeclarativeParserStatus
31 {
32 Q_INTERFACES(QDeclarativeParserStatus)
33 Q_OBJECT
34 Q_PROPERTY(AxisRole role READ role WRITE setRole /*NOTIFY roleChanged*/)
35 Q_ENUMS(AxisRole)
36
37 public:
38 enum AxisRole {
39 RoleX = 0,
40 RoleY
41 };
42
43 public:
44 explicit DeclarativeAxis(QObject *parent = 0);
45
46 public: // from QDeclarativeParserStatus
47 virtual void classBegin();
48 virtual void componentComplete();
49
50 public:
51 void setRole(AxisRole role);
52 AxisRole role() { return m_role; }
53 AxisRole m_role;
54 };
55
56 QTCOMMERCIALCHART_END_NAMESPACE
57
58 #endif // DECLARATIVE_AXIS_H
@@ -1,115 +1,114
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "declarativechart.h"
21 #include "declarativechart.h"
22 #include <QPainter>
22 #include <QPainter>
23
23
24 QTCOMMERCIALCHART_BEGIN_NAMESPACE
24 QTCOMMERCIALCHART_BEGIN_NAMESPACE
25
25
26 DeclarativeChart::DeclarativeChart(QDeclarativeItem *parent)
26 DeclarativeChart::DeclarativeChart(QDeclarativeItem *parent)
27 : QDeclarativeItem(parent),
27 : QDeclarativeItem(parent),
28 m_chart(new QChart(this)),
28 m_chart(new QChart(this)),
29 m_legend(LegendDisabled)
29 m_legend(LegendDisabled)
30 {
30 {
31 m_chart->setAnimationOptions(QChart::SeriesAnimations);
32 setFlag(QGraphicsItem::ItemHasNoContents, false);
31 setFlag(QGraphicsItem::ItemHasNoContents, false);
33 }
32 }
34
33
35 DeclarativeChart::~DeclarativeChart()
34 DeclarativeChart::~DeclarativeChart()
36 {
35 {
37 delete m_chart;
36 delete m_chart;
38 }
37 }
39
38
40 void DeclarativeChart::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry)
39 void DeclarativeChart::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry)
41 {
40 {
42 Q_UNUSED(oldGeometry)
41 Q_UNUSED(oldGeometry)
43
42
44 if (newGeometry.isValid()) {
43 if (newGeometry.isValid()) {
45 if (newGeometry.width() > 0 && newGeometry.height() > 0) {
44 if (newGeometry.width() > 0 && newGeometry.height() > 0) {
46 m_chart->resize(newGeometry.width(), newGeometry.height());
45 m_chart->resize(newGeometry.width(), newGeometry.height());
47 }
46 }
48 }
47 }
49 }
48 }
50
49
51 void DeclarativeChart::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
50 void DeclarativeChart::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
52 {
51 {
53 Q_UNUSED(option)
52 Q_UNUSED(option)
54 Q_UNUSED(widget)
53 Q_UNUSED(widget)
55
54
56 // TODO: optimized?
55 // TODO: optimized?
57 painter->setRenderHint(QPainter::Antialiasing, true);
56 painter->setRenderHint(QPainter::Antialiasing, true);
58 }
57 }
59
58
60 void DeclarativeChart::setAnimationOptions(QChart::AnimationOption animations)
59 void DeclarativeChart::setAnimationOptions(QChart::AnimationOption animations)
61 {
60 {
62 m_chart->setAnimationOptions(animations);
61 m_chart->setAnimationOptions(animations);
63 }
62 }
64
63
65 QChart::AnimationOption DeclarativeChart::animationOptions()
64 QChart::AnimationOption DeclarativeChart::animationOptions()
66 {
65 {
67 if (m_chart->animationOptions().testFlag(QChart::AllAnimations))
66 if (m_chart->animationOptions().testFlag(QChart::AllAnimations))
68 return QChart::AllAnimations;
67 return QChart::AllAnimations;
69 else if (m_chart->animationOptions().testFlag(QChart::GridAxisAnimations))
68 else if (m_chart->animationOptions().testFlag(QChart::GridAxisAnimations))
70 return QChart::GridAxisAnimations;
69 return QChart::GridAxisAnimations;
71 else if (m_chart->animationOptions().testFlag(QChart::SeriesAnimations))
70 else if (m_chart->animationOptions().testFlag(QChart::SeriesAnimations))
72 return QChart::SeriesAnimations;
71 return QChart::SeriesAnimations;
73 else
72 else
74 return QChart::NoAnimation;
73 return QChart::NoAnimation;
75 }
74 }
76
75
77 void DeclarativeChart::setLegend(ChartLegend legend)
76 void DeclarativeChart::setLegend(ChartLegend legend)
78 {
77 {
79 if (legend != m_legend) {
78 if (legend != m_legend) {
80 m_legend = legend;
79 m_legend = legend;
81 switch (m_legend) {
80 switch (m_legend) {
82 case LegendDisabled:
81 case LegendDisabled:
83 m_chart->legend()->setVisible(false);
82 m_chart->legend()->setVisible(false);
84 break;
83 break;
85 case LegendTop:
84 case LegendTop:
86 m_chart->legend()->setVisible(true);
85 m_chart->legend()->setVisible(true);
87 m_chart->legend()->setAlignment(QLegend::AlignmentTop);
86 m_chart->legend()->setAlignment(QLegend::AlignmentTop);
88 break;
87 break;
89 case LegendBottom:
88 case LegendBottom:
90 m_chart->legend()->setVisible(true);
89 m_chart->legend()->setVisible(true);
91 m_chart->legend()->setAlignment(QLegend::AlignmentBottom);
90 m_chart->legend()->setAlignment(QLegend::AlignmentBottom);
92 break;
91 break;
93 case LegendLeft:
92 case LegendLeft:
94 m_chart->legend()->setVisible(true);
93 m_chart->legend()->setVisible(true);
95 m_chart->legend()->setAlignment(QLegend::AlignmentLeft);
94 m_chart->legend()->setAlignment(QLegend::AlignmentLeft);
96 break;
95 break;
97 case LegendRight:
96 case LegendRight:
98 m_chart->legend()->setVisible(true);
97 m_chart->legend()->setVisible(true);
99 m_chart->legend()->setAlignment(QLegend::AlignmentRight);
98 m_chart->legend()->setAlignment(QLegend::AlignmentRight);
100 break;
99 break;
101 default:
100 default:
102 m_chart->legend()->setVisible(false);
101 m_chart->legend()->setVisible(false);
103 break;
102 break;
104 }
103 }
105 }
104 }
106 }
105 }
107
106
108 DeclarativeChart::ChartLegend DeclarativeChart::legend()
107 DeclarativeChart::ChartLegend DeclarativeChart::legend()
109 {
108 {
110 return m_legend;
109 return m_legend;
111 }
110 }
112
111
113 #include "moc_declarativechart.cpp"
112 #include "moc_declarativechart.cpp"
114
113
115 QTCOMMERCIALCHART_END_NAMESPACE
114 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,60 +1,60
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 //#include "DeclarativeXySeries.h"
21 //#include "DeclarativeXySeries.h"
22 #include "declarativexyseries.h"
22 #include "declarativexyseries.h"
23 #include "qxyseries.h"
23 #include "qxyseries.h"
24 #include "declarativechart.h"
24 #include "declarativechart.h"
25
25
26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27
27
28 DeclarativeXySeries::DeclarativeXySeries()
28 DeclarativeXySeries::DeclarativeXySeries()
29 {
29 {
30 }
30 }
31
31
32 DeclarativeXySeries::~DeclarativeXySeries()
32 DeclarativeXySeries::~DeclarativeXySeries()
33 {
33 {
34 }
34 }
35
35
36 void DeclarativeXySeries::classBegin()
36 void DeclarativeXySeries::classBegin()
37 {
37 {
38 }
38 }
39
39
40 void DeclarativeXySeries::componentComplete()
40 void DeclarativeXySeries::componentComplete()
41 {
41 {
42 QAbstractSeries *thisObj = reinterpret_cast<QAbstractSeries *>(series());
42 QAbstractSeries *thisObj = reinterpret_cast<QAbstractSeries *>(series());
43 DeclarativeChart *declarativeChart = qobject_cast<DeclarativeChart *>(thisObj->parent());
43 DeclarativeChart *declarativeChart = qobject_cast<DeclarativeChart *>(thisObj->parent());
44
44
45 if (declarativeChart) {
45 if (declarativeChart) {
46 QChart *chart = qobject_cast<QChart *>(declarativeChart->m_chart);
46 QChart *chart = qobject_cast<QChart *>(declarativeChart->m_chart);
47 Q_ASSERT(chart);
47 Q_ASSERT(chart);
48 chart->addSeries(thisObj);
48 chart->addSeries(thisObj);
49 }
49 }
50 }
50 }
51
51
52 void DeclarativeXySeries::appendPoints(QDeclarativeListProperty<DeclarativeXyPoint> *list,
52 void DeclarativeXySeries::appendPoints(QDeclarativeListProperty<DeclarativeXyPoint> *list,
53 DeclarativeXyPoint *element)
53 DeclarativeXyPoint *element)
54 {
54 {
55 QXYSeries *series = qobject_cast<QXYSeries *>(list->object);
55 QXYSeries *series = qobject_cast<QXYSeries *>(list->object);
56 if (series)
56 if (series)
57 series->append(element->x(), element->y());
57 series->append(element->x(), element->y());
58 }
58 }
59
59
60 QTCOMMERCIALCHART_END_NAMESPACE
60 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,61 +1,63
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include <QtDeclarative/qdeclarativeextensionplugin.h>
21 #include <QtDeclarative/qdeclarativeextensionplugin.h>
22 #include <QtDeclarative/qdeclarative.h>
22 #include <QtDeclarative/qdeclarative.h>
23 #include "qchart.h"
23 #include "qchart.h"
24 #include "declarativechart.h"
24 #include "declarativechart.h"
25 #include "declarativeaxis.h"
25 #include "declarativexypoint.h"
26 #include "declarativexypoint.h"
26 #include "declarativelineseries.h"
27 #include "declarativelineseries.h"
27 #include "declarativesplineseries.h"
28 #include "declarativesplineseries.h"
28 #include "declarativeareaseries.h"
29 #include "declarativeareaseries.h"
29 #include "declarativescatterseries.h"
30 #include "declarativescatterseries.h"
30 #include "declarativebarseries.h"
31 #include "declarativebarseries.h"
31 #include "declarativepieseries.h"
32 #include "declarativepieseries.h"
32
33
33 QTCOMMERCIALCHART_BEGIN_NAMESPACE
34 QTCOMMERCIALCHART_BEGIN_NAMESPACE
34
35
35 class ChartQmlPlugin : public QDeclarativeExtensionPlugin
36 class ChartQmlPlugin : public QDeclarativeExtensionPlugin
36 {
37 {
37 Q_OBJECT
38 Q_OBJECT
38 public:
39 public:
39 virtual void registerTypes(const char *uri)
40 virtual void registerTypes(const char *uri)
40 {
41 {
41 Q_ASSERT(QLatin1String(uri) == QLatin1String("QtCommercial.Chart"));
42 Q_ASSERT(QLatin1String(uri) == QLatin1String("QtCommercial.Chart"));
42
43
43 qmlRegisterType<DeclarativeChart>(uri, 1, 0, "Chart");
44 qmlRegisterType<DeclarativeChart>(uri, 1, 0, "Chart");
45 qmlRegisterType<DeclarativeAxis>(uri, 1, 0, "Axis");
44 qmlRegisterType<DeclarativeXyPoint>(uri, 1, 0, "XyPoint");
46 qmlRegisterType<DeclarativeXyPoint>(uri, 1, 0, "XyPoint");
45 qmlRegisterType<DeclarativeScatterSeries>(uri, 1, 0, "ScatterSeries");
47 qmlRegisterType<DeclarativeScatterSeries>(uri, 1, 0, "ScatterSeries");
46 qmlRegisterType<DeclarativeLineSeries>(uri, 1, 0, "LineSeries");
48 qmlRegisterType<DeclarativeLineSeries>(uri, 1, 0, "LineSeries");
47 qmlRegisterType<DeclarativeSplineSeries>(uri, 1, 0, "SplineSeries");
49 qmlRegisterType<DeclarativeSplineSeries>(uri, 1, 0, "SplineSeries");
48 qmlRegisterType<DeclarativeAreaSeries>(uri, 1, 0, "AreaSeries");
50 qmlRegisterType<DeclarativeAreaSeries>(uri, 1, 0, "AreaSeries");
49 qmlRegisterType<DeclarativeBarSeries>(uri, 1, 0, "BarSeries");
51 qmlRegisterType<DeclarativeBarSeries>(uri, 1, 0, "BarSeries");
50 qmlRegisterType<DeclarativePieSeries>(uri, 1, 0, "PieSeries");
52 qmlRegisterType<DeclarativePieSeries>(uri, 1, 0, "PieSeries");
51 qmlRegisterType<QPieSlice>(uri, 1, 0, "PieSlice");
53 qmlRegisterType<QPieSlice>(uri, 1, 0, "PieSlice");
52 }
54 }
53 };
55 };
54
56
55 #include "plugin.moc"
57 #include "plugin.moc"
56
58
57 QTCOMMERCIALCHART_END_NAMESPACE
59 QTCOMMERCIALCHART_END_NAMESPACE
58
60
59 QTCOMMERCIALCHART_USE_NAMESPACE
61 QTCOMMERCIALCHART_USE_NAMESPACE
60
62
61 Q_EXPORT_PLUGIN2(qtcommercialchartqml, QT_PREPEND_NAMESPACE(ChartQmlPlugin))
63 Q_EXPORT_PLUGIN2(qtcommercialchartqml, QT_PREPEND_NAMESPACE(ChartQmlPlugin))
@@ -1,52 +1,54
1 TEMPLATE = lib
1 TEMPLATE = lib
2 TARGET = qtcommercialchartqml
2 TARGET = qtcommercialchartqml
3 CONFIG += qt plugin
3 CONFIG += qt plugin
4 QT += declarative
4 QT += declarative
5
5
6 !include( ../config.pri ) {
6 !include( ../config.pri ) {
7 error( "Couldn't find the config.pri file!" )
7 error( "Couldn't find the config.pri file!" )
8 }
8 }
9
9
10 DESTDIR = $$CHART_BUILD_PLUGIN_DIR
10 DESTDIR = $$CHART_BUILD_PLUGIN_DIR
11 contains(QT_MAJOR_VERSION, 5) {
11 contains(QT_MAJOR_VERSION, 5) {
12 # TODO: QtQuick2 not supported by the implementation currently
12 # TODO: QtQuick2 not supported by the implementation currently
13 DEFINES += QTQUICK2
13 DEFINES += QTQUICK2
14 }
14 }
15
15
16 OBJECTS_DIR = $$CHART_BUILD_DIR/plugin
16 OBJECTS_DIR = $$CHART_BUILD_DIR/plugin
17 MOC_DIR = $$CHART_BUILD_DIR/plugin
17 MOC_DIR = $$CHART_BUILD_DIR/plugin
18 UI_DIR = $$CHART_BUILD_DIR/plugin
18 UI_DIR = $$CHART_BUILD_DIR/plugin
19 RCC_DIR = $$CHART_BUILD_DIR/plugin
19 RCC_DIR = $$CHART_BUILD_DIR/plugin
20
20
21 SOURCES += \
21 SOURCES += \
22 plugin.cpp \
22 plugin.cpp \
23 declarativechart.cpp \
23 declarativechart.cpp \
24 declarativexyseries.cpp \
24 declarativeaxis.cpp \
25 declarativexypoint.cpp \
25 declarativexypoint.cpp \
26 declarativexyseries.cpp \
26 declarativelineseries.cpp \
27 declarativelineseries.cpp \
27 declarativesplineseries.cpp \
28 declarativesplineseries.cpp \
28 declarativeareaseries.cpp \
29 declarativeareaseries.cpp \
29 declarativescatterseries.cpp \
30 declarativescatterseries.cpp \
30 declarativepieseries.cpp \
31 declarativepieseries.cpp \
31 declarativebarseries.cpp
32 declarativebarseries.cpp
32 HEADERS += \
33 HEADERS += \
33 declarativechart.h \
34 declarativechart.h \
34 declarativexyseries.h \
35 declarativeaxis.h \
35 declarativexypoint.h \
36 declarativexypoint.h \
37 declarativexyseries.h \
36 declarativelineseries.h \
38 declarativelineseries.h \
37 declarativesplineseries.h \
39 declarativesplineseries.h \
38 declarativeareaseries.h \
40 declarativeareaseries.h \
39 declarativescatterseries.h \
41 declarativescatterseries.h \
40 declarativepieseries.h \
42 declarativepieseries.h \
41 declarativebarseries.h
43 declarativebarseries.h
42
44
43 TARGETPATH = QtCommercial/Chart
45 TARGETPATH = QtCommercial/Chart
44 target.path = $$[QT_INSTALL_IMPORTS]/$$TARGETPATH
46 target.path = $$[QT_INSTALL_IMPORTS]/$$TARGETPATH
45 qmldir.files += $$PWD/qmldir
47 qmldir.files += $$PWD/qmldir
46 qmldir.path += $$[QT_INSTALL_IMPORTS]/$$TARGETPATH
48 qmldir.path += $$[QT_INSTALL_IMPORTS]/$$TARGETPATH
47 INSTALLS += target qmldir
49 INSTALLS += target qmldir
48
50
49 FILE = $$PWD/qmldir
51 FILE = $$PWD/qmldir
50 win32:{FILE = $$replace(FILE, "/","\\")}
52 win32:{FILE = $$replace(FILE, "/","\\")}
51 QMAKE_POST_LINK += $$QMAKE_COPY $$FILE $$CHART_BUILD_PLUGIN_DIR
53 QMAKE_POST_LINK += $$QMAKE_COPY $$FILE $$CHART_BUILD_PLUGIN_DIR
52 !system_build:mac: QMAKE_POST_LINK += " & $$MAC_POST_LINK_PREFIX $$MAC_PLUGINS_BIN_DIR"
54 !system_build:mac: QMAKE_POST_LINK += " & $$MAC_POST_LINK_PREFIX $$MAC_PLUGINS_BIN_DIR"
@@ -1,466 +1,466
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "chartaxis_p.h"
21 #include "chartaxis_p.h"
22 #include "qaxis.h"
22 #include "qaxis.h"
23 #include "qaxis_p.h"
23 #include "qaxis_p.h"
24 #include "qaxiscategories_p.h"
24 #include "qaxiscategories_p.h"
25 #include "chartpresenter_p.h"
25 #include "chartpresenter_p.h"
26 #include "chartanimator_p.h"
26 #include "chartanimator_p.h"
27 #include <QPainter>
27 #include <QPainter>
28 #include <QDebug>
28 #include <QDebug>
29 #include <cmath>
29 #include <cmath>
30
30
31 static int label_padding = 5;
31 static int label_padding = 5;
32
32
33 QTCOMMERCIALCHART_BEGIN_NAMESPACE
33 QTCOMMERCIALCHART_BEGIN_NAMESPACE
34
34
35 ChartAxis::ChartAxis(QAxis *axis,ChartPresenter *presenter,AxisType type) : Chart(presenter),
35 ChartAxis::ChartAxis(QAxis *axis,ChartPresenter *presenter,AxisType type) : Chart(presenter),
36 m_chartAxis(axis),
36 m_chartAxis(axis),
37 m_type(type),
37 m_type(type),
38 m_labelsAngle(0),
38 m_labelsAngle(0),
39 m_grid(new QGraphicsItemGroup(presenter->rootItem())),
39 m_grid(new QGraphicsItemGroup(presenter->rootItem())),
40 m_shades(new QGraphicsItemGroup(presenter->rootItem())),
40 m_shades(new QGraphicsItemGroup(presenter->rootItem())),
41 m_labels(new QGraphicsItemGroup(presenter->rootItem())),
41 m_labels(new QGraphicsItemGroup(presenter->rootItem())),
42 m_axis(new QGraphicsItemGroup(presenter->rootItem())),
42 m_axis(new QGraphicsItemGroup(presenter->rootItem())),
43 m_min(0),
43 m_min(0),
44 m_max(0),
44 m_max(0),
45 m_ticksCount(0)
45 m_ticksCount(0)
46 {
46 {
47 //initial initialization
47 //initial initialization
48 m_axis->setZValue(ChartPresenter::AxisZValue);
48 m_axis->setZValue(ChartPresenter::AxisZValue);
49 m_axis->setHandlesChildEvents(false);
49 m_axis->setHandlesChildEvents(false);
50
50
51 m_shades->setZValue(ChartPresenter::ShadesZValue);
51 m_shades->setZValue(ChartPresenter::ShadesZValue);
52 m_grid->setZValue(ChartPresenter::GridZValue);
52 m_grid->setZValue(ChartPresenter::GridZValue);
53
53
54 QObject::connect(m_chartAxis->d_ptr.data(),SIGNAL(updated()),this,SLOT(handleAxisUpdated()));
54 QObject::connect(m_chartAxis->d_ptr.data(),SIGNAL(updated()),this,SLOT(handleAxisUpdated()));
55 QObject::connect(m_chartAxis->categories()->d_ptr.data(),SIGNAL(updated()),this,SLOT(handleAxisCategoriesUpdated()));
55 QObject::connect(m_chartAxis->categories()->d_ptr.data(),SIGNAL(updated()),this,SLOT(handleAxisCategoriesUpdated()));
56
56
57 handleAxisUpdated();
57 handleAxisUpdated();
58 }
58 }
59
59
60 ChartAxis::~ChartAxis()
60 ChartAxis::~ChartAxis()
61 {
61 {
62 }
62 }
63
63
64 void ChartAxis::createItems(int count)
64 void ChartAxis::createItems(int count)
65 {
65 {
66
66
67 if (m_axis->children().size() == 0)
67 if (m_axis->children().size() == 0)
68 m_axis->addToGroup(new AxisItem(this));
68 m_axis->addToGroup(new AxisItem(this));
69 for (int i = 0; i < count; ++i) {
69 for (int i = 0; i < count; ++i) {
70 m_grid->addToGroup(new QGraphicsLineItem());
70 m_grid->addToGroup(new QGraphicsLineItem());
71 m_labels->addToGroup(new QGraphicsSimpleTextItem());
71 m_labels->addToGroup(new QGraphicsSimpleTextItem());
72 m_axis->addToGroup(new QGraphicsLineItem());
72 m_axis->addToGroup(new QGraphicsLineItem());
73 if ((m_grid->childItems().size())%2 && m_grid->childItems().size()>2) m_shades->addToGroup(new QGraphicsRectItem());
73 if ((m_grid->childItems().size())%2 && m_grid->childItems().size()>2) m_shades->addToGroup(new QGraphicsRectItem());
74 }
74 }
75 }
75 }
76
76
77 void ChartAxis::deleteItems(int count)
77 void ChartAxis::deleteItems(int count)
78 {
78 {
79 QList<QGraphicsItem *> lines = m_grid->childItems();
79 QList<QGraphicsItem *> lines = m_grid->childItems();
80 QList<QGraphicsItem *> labels = m_labels->childItems();
80 QList<QGraphicsItem *> labels = m_labels->childItems();
81 QList<QGraphicsItem *> shades = m_shades->childItems();
81 QList<QGraphicsItem *> shades = m_shades->childItems();
82 QList<QGraphicsItem *> axis = m_axis->childItems();
82 QList<QGraphicsItem *> axis = m_axis->childItems();
83
83
84 for (int i = 0; i < count; ++i) {
84 for (int i = 0; i < count; ++i) {
85 if (lines.size()%2 && lines.size() > 1) delete(shades.takeLast());
85 if (lines.size()%2 && lines.size() > 1) delete(shades.takeLast());
86 delete(lines.takeLast());
86 delete(lines.takeLast());
87 delete(labels.takeLast());
87 delete(labels.takeLast());
88 delete(axis.takeLast());
88 delete(axis.takeLast());
89 }
89 }
90 }
90 }
91
91
92 void ChartAxis::updateLayout(QVector<qreal> &layout)
92 void ChartAxis::updateLayout(QVector<qreal> &layout)
93 {
93 {
94 if (animator()) {
94 if (animator()) {
95 animator()->updateLayout(this,layout);
95 animator()->updateLayout(this,layout);
96 } else {
96 } else {
97 setLayout(layout);
97 setLayout(layout);
98 }
98 }
99 }
99 }
100
100
101 bool ChartAxis::createLabels(QStringList &labels,qreal min, qreal max,int ticks) const
101 bool ChartAxis::createLabels(QStringList &labels,qreal min, qreal max,int ticks) const
102 {
102 {
103 Q_ASSERT(max>=min);
103 Q_ASSERT(max>=min);
104 Q_ASSERT(ticks>1);
104 Q_ASSERT(ticks>1);
105
105
106 QAxisCategories* categories = m_chartAxis->categories();
106 QAxisCategories* categories = m_chartAxis->categories();
107
107
108 bool category = categories->count()>0;
108 bool category = categories->count()>0;
109
109
110 if (!category) {
110 if (!category) {
111 int n = qMax(int(-floor(log10((max-min)/(ticks-1)))),0);
111 int n = qMax(int(-floor(log10((max-min)/(ticks-1)))),0);
112 n++;
112 n++;
113 for (int i=0; i< ticks; i++) {
113 for (int i=0; i< ticks; i++) {
114 qreal value = min + (i * (max - min)/ (ticks-1));
114 qreal value = min + (i * (max - min)/ (ticks-1));
115 labels << QString::number(value,'f',n);
115 labels << QString::number(value,'f',n);
116 }
116 }
117 } else {
117 } else {
118 QList<qreal> values = categories->values();
118 QList<qreal> values = categories->values();
119 for (int i=0; i< ticks; i++) {
119 for (int i=0; i< ticks; i++) {
120 qreal value = (min + (i * (max - min)/ (ticks-1)));
120 qreal value = (min + (i * (max - min)/ (ticks-1)));
121 int j=0;
121 int j=0;
122 for (; j<values.count(); j++) {
122 for (; j<values.count(); j++) {
123 if (values.at(j) > value) break;
123 if (values.at(j) > value) break;
124 }
124 }
125 if (j!=0) value=values.at(j-1);
125 if (j!=0) value=values.at(j-1);
126
126
127 QString label = categories->label(value);
127 QString label = categories->label(value);
128 labels << label;
128 labels << label;
129 }
129 }
130 }
130 }
131
131
132 return category;
132 return category;
133 }
133 }
134
134
135 void ChartAxis::setAxisOpacity(qreal opacity)
135 void ChartAxis::setAxisOpacity(qreal opacity)
136 {
136 {
137 m_axis->setOpacity(opacity);
137 m_axis->setOpacity(opacity);
138 }
138 }
139
139
140 qreal ChartAxis::axisOpacity() const
140 qreal ChartAxis::axisOpacity() const
141 {
141 {
142 return m_axis->opacity();
142 return m_axis->opacity();
143 }
143 }
144
144
145 void ChartAxis::setGridOpacity(qreal opacity)
145 void ChartAxis::setGridOpacity(qreal opacity)
146 {
146 {
147 m_grid->setOpacity(opacity);
147 m_grid->setOpacity(opacity);
148 }
148 }
149
149
150 qreal ChartAxis::gridOpacity() const
150 qreal ChartAxis::gridOpacity() const
151 {
151 {
152 return m_grid->opacity();
152 return m_grid->opacity();
153 }
153 }
154
154
155 void ChartAxis::setLabelsOpacity(qreal opacity)
155 void ChartAxis::setLabelsOpacity(qreal opacity)
156 {
156 {
157 m_labels->setOpacity(opacity);
157 m_labels->setOpacity(opacity);
158 }
158 }
159
159
160 qreal ChartAxis::labelsOpacity() const
160 qreal ChartAxis::labelsOpacity() const
161 {
161 {
162 return m_labels->opacity();
162 return m_labels->opacity();
163 }
163 }
164
164
165 void ChartAxis::setShadesOpacity(qreal opacity)
165 void ChartAxis::setShadesOpacity(qreal opacity)
166 {
166 {
167 m_shades->setOpacity(opacity);
167 m_shades->setOpacity(opacity);
168 }
168 }
169
169
170 qreal ChartAxis::shadesOpacity() const
170 qreal ChartAxis::shadesOpacity() const
171 {
171 {
172 return m_shades->opacity();
172 return m_shades->opacity();
173 }
173 }
174
174
175 void ChartAxis::setLabelsAngle(int angle)
175 void ChartAxis::setLabelsAngle(int angle)
176 {
176 {
177 foreach(QGraphicsItem* item , m_labels->childItems()) {
177 foreach(QGraphicsItem* item , m_labels->childItems()) {
178 item->setRotation(angle);
178 item->setRotation(angle);
179 }
179 }
180
180
181 m_labelsAngle=angle;
181 m_labelsAngle=angle;
182 }
182 }
183
183
184 void ChartAxis::setLabelsPen(const QPen &pen)
184 void ChartAxis::setLabelsPen(const QPen &pen)
185 {
185 {
186 foreach(QGraphicsItem* item , m_labels->childItems()) {
186 foreach(QGraphicsItem* item , m_labels->childItems()) {
187 static_cast<QGraphicsSimpleTextItem*>(item)->setPen(pen);
187 static_cast<QGraphicsSimpleTextItem*>(item)->setPen(pen);
188 }
188 }
189 }
189 }
190
190
191 void ChartAxis::setLabelsBrush(const QBrush &brush)
191 void ChartAxis::setLabelsBrush(const QBrush &brush)
192 {
192 {
193 foreach(QGraphicsItem* item , m_labels->childItems()) {
193 foreach(QGraphicsItem* item , m_labels->childItems()) {
194 static_cast<QGraphicsSimpleTextItem*>(item)->setBrush(brush);
194 static_cast<QGraphicsSimpleTextItem*>(item)->setBrush(brush);
195 }
195 }
196 }
196 }
197
197
198 void ChartAxis::setLabelsFont(const QFont &font)
198 void ChartAxis::setLabelsFont(const QFont &font)
199 {
199 {
200 foreach(QGraphicsItem* item , m_labels->childItems()) {
200 foreach(QGraphicsItem* item , m_labels->childItems()) {
201 static_cast<QGraphicsSimpleTextItem*>(item)->setFont(font);
201 static_cast<QGraphicsSimpleTextItem*>(item)->setFont(font);
202 }
202 }
203 }
203 }
204
204
205 void ChartAxis::setShadesBrush(const QBrush &brush)
205 void ChartAxis::setShadesBrush(const QBrush &brush)
206 {
206 {
207 foreach(QGraphicsItem* item , m_shades->childItems()) {
207 foreach(QGraphicsItem* item , m_shades->childItems()) {
208 static_cast<QGraphicsRectItem*>(item)->setBrush(brush);
208 static_cast<QGraphicsRectItem*>(item)->setBrush(brush);
209 }
209 }
210 }
210 }
211
211
212 void ChartAxis::setShadesPen(const QPen &pen)
212 void ChartAxis::setShadesPen(const QPen &pen)
213 {
213 {
214 foreach(QGraphicsItem* item , m_shades->childItems()) {
214 foreach(QGraphicsItem* item , m_shades->childItems()) {
215 static_cast<QGraphicsRectItem*>(item)->setPen(pen);
215 static_cast<QGraphicsRectItem*>(item)->setPen(pen);
216 }
216 }
217 }
217 }
218
218
219 void ChartAxis::setAxisPen(const QPen &pen)
219 void ChartAxis::setAxisPen(const QPen &pen)
220 {
220 {
221 foreach(QGraphicsItem* item , m_axis->childItems()) {
221 foreach(QGraphicsItem* item , m_axis->childItems()) {
222 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
222 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
223 }
223 }
224 }
224 }
225
225
226 void ChartAxis::setGridPen(const QPen &pen)
226 void ChartAxis::setGridPen(const QPen &pen)
227 {
227 {
228 foreach(QGraphicsItem* item , m_grid->childItems()) {
228 foreach(QGraphicsItem* item , m_grid->childItems()) {
229 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
229 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
230 }
230 }
231 }
231 }
232
232
233 QVector<qreal> ChartAxis::calculateLayout() const
233 QVector<qreal> ChartAxis::calculateLayout() const
234 {
234 {
235 Q_ASSERT(m_ticksCount>=2);
235 Q_ASSERT(m_ticksCount>=2);
236
236
237 QVector<qreal> points;
237 QVector<qreal> points;
238 points.resize(m_ticksCount);
238 points.resize(m_ticksCount);
239
239
240 switch (m_type)
240 switch (m_type)
241 {
241 {
242 case X_AXIS:
242 case X_AXIS:
243 {
243 {
244 const qreal deltaX = m_rect.width()/(m_ticksCount-1);
244 const qreal deltaX = m_rect.width()/(m_ticksCount-1);
245 for (int i = 0; i < m_ticksCount; ++i) {
245 for (int i = 0; i < m_ticksCount; ++i) {
246 int x = i * deltaX + m_rect.left();
246 int x = i * deltaX + m_rect.left();
247 points[i] = x;
247 points[i] = x;
248 }
248 }
249 }
249 }
250 break;
250 break;
251 case Y_AXIS:
251 case Y_AXIS:
252 {
252 {
253 const qreal deltaY = m_rect.height()/(m_ticksCount-1);
253 const qreal deltaY = m_rect.height()/(m_ticksCount-1);
254 for (int i = 0; i < m_ticksCount; ++i) {
254 for (int i = 0; i < m_ticksCount; ++i) {
255 int y = i * -deltaY + m_rect.bottom();
255 int y = i * -deltaY + m_rect.bottom();
256 points[i] = y;
256 points[i] = y;
257 }
257 }
258 }
258 }
259 break;
259 break;
260 }
260 }
261 return points;
261 return points;
262 }
262 }
263
263
264 void ChartAxis::setLayout(QVector<qreal> &layout)
264 void ChartAxis::setLayout(QVector<qreal> &layout)
265 {
265 {
266 int diff = m_layoutVector.size() - layout.size();
266 int diff = m_layoutVector.size() - layout.size();
267
267
268 if (diff>0) {
268 if (diff>0) {
269 deleteItems(diff);
269 deleteItems(diff);
270 } else if (diff<0) {
270 } else if (diff<0) {
271 createItems(-diff);
271 createItems(-diff);
272 }
272 }
273
273
274 if( diff!=0) handleAxisUpdated();
274 if( diff!=0) handleAxisUpdated();
275
275
276 QStringList ticksList;
276 QStringList ticksList;
277
277
278 bool categories = createLabels(ticksList,m_min,m_max,layout.size());
278 bool categories = createLabels(ticksList,m_min,m_max,layout.size());
279
279
280 QList<QGraphicsItem *> lines = m_grid->childItems();
280 QList<QGraphicsItem *> lines = m_grid->childItems();
281 QList<QGraphicsItem *> labels = m_labels->childItems();
281 QList<QGraphicsItem *> labels = m_labels->childItems();
282 QList<QGraphicsItem *> shades = m_shades->childItems();
282 QList<QGraphicsItem *> shades = m_shades->childItems();
283 QList<QGraphicsItem *> axis = m_axis->childItems();
283 QList<QGraphicsItem *> axis = m_axis->childItems();
284
284
285 Q_ASSERT(labels.size() == ticksList.size());
285 Q_ASSERT(labels.size() == ticksList.size());
286 Q_ASSERT(layout.size() == ticksList.size());
286 Q_ASSERT(layout.size() == ticksList.size());
287
287
288 qreal minWidth = 0;
288 qreal minWidth = 0;
289 qreal minHeight = 0;
289 qreal minHeight = 0;
290
290
291 switch (m_type)
291 switch (m_type)
292 {
292 {
293 case X_AXIS:
293 case X_AXIS:
294 {
294 {
295 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
295 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
296 lineItem->setLine(m_rect.left(), m_rect.bottom(), m_rect.right(), m_rect.bottom());
296 lineItem->setLine(m_rect.left(), m_rect.bottom(), m_rect.right(), m_rect.bottom());
297
297
298 for (int i = 0; i < layout.size(); ++i) {
298 for (int i = 0; i < layout.size(); ++i) {
299 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
299 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
300 lineItem->setLine(layout[i], m_rect.top(), layout[i], m_rect.bottom());
300 lineItem->setLine(layout[i], m_rect.top(), layout[i], m_rect.bottom());
301 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
301 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
302 if (!categories || i<1) {
302 if (!categories || i<1) {
303 labelItem->setText(ticksList.at(i));
303 labelItem->setText(ticksList.at(i));
304 const QRectF& rect = labelItem->boundingRect();
304 const QRectF& rect = labelItem->boundingRect();
305 minWidth+=rect.width();
305 minWidth+=rect.width();
306 minHeight=qMax(rect.height(),minHeight);
306 minHeight=qMax(rect.height(),minHeight);
307 QPointF center = rect.center();
307 QPointF center = rect.center();
308 labelItem->setTransformOriginPoint(center.x(), center.y());
308 labelItem->setTransformOriginPoint(center.x(), center.y());
309 labelItem->setPos(layout[i] - center.x(), m_rect.bottom() + label_padding);
309 labelItem->setPos(layout[i] - center.x(), m_rect.bottom() + label_padding);
310 } else {
310 } else {
311 labelItem->setText(ticksList.at(i));
311 labelItem->setText(ticksList.at(i));
312 const QRectF& rect = labelItem->boundingRect();
312 const QRectF& rect = labelItem->boundingRect();
313 minWidth+=rect.width();
313 minWidth+=rect.width();
314 minHeight=qMax(rect.height()+label_padding,minHeight);
314 minHeight=qMax(rect.height()+label_padding,minHeight);
315 QPointF center = rect.center();
315 QPointF center = rect.center();
316 labelItem->setTransformOriginPoint(center.x(), center.y());
316 labelItem->setTransformOriginPoint(center.x(), center.y());
317 labelItem->setPos(layout[i] - (layout[i] - layout[i-1])/2 - center.x(), m_rect.bottom() + label_padding);
317 labelItem->setPos(layout[i] - (layout[i] - layout[i-1])/2 - center.x(), m_rect.bottom() + label_padding);
318 }
318 }
319
319
320 if ((i+1)%2 && i>1) {
320 if ((i+1)%2 && i>1) {
321 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1));
321 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1));
322 rectItem->setRect(layout[i-1],m_rect.top(),layout[i]-layout[i-1],m_rect.height());
322 rectItem->setRect(layout[i-1],m_rect.top(),layout[i]-layout[i-1],m_rect.height());
323 }
323 }
324 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
324 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
325 lineItem->setLine(layout[i],m_rect.bottom(),layout[i],m_rect.bottom()+5);
325 lineItem->setLine(layout[i],m_rect.bottom(),layout[i],m_rect.bottom()+5);
326 }
326 }
327
327
328 }
328 }
329 break;
329 break;
330
330
331 case Y_AXIS:
331 case Y_AXIS:
332 {
332 {
333 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
333 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
334 lineItem->setLine(m_rect.left() , m_rect.top(), m_rect.left(), m_rect.bottom());
334 lineItem->setLine(m_rect.left() , m_rect.top(), m_rect.left(), m_rect.bottom());
335
335
336 for (int i = 0; i < layout.size(); ++i) {
336 for (int i = 0; i < layout.size(); ++i) {
337 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
337 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
338 lineItem->setLine(m_rect.left() , layout[i], m_rect.right(), layout[i]);
338 lineItem->setLine(m_rect.left() , layout[i], m_rect.right(), layout[i]);
339 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
339 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
340
340
341 if (!categories || i<1) {
341 if (!categories || i<1) {
342 labelItem->setText(ticksList.at(i));
342 labelItem->setText(ticksList.at(i));
343 const QRectF& rect = labelItem->boundingRect();
343 const QRectF& rect = labelItem->boundingRect();
344 minWidth=qMax(rect.width()+label_padding,minWidth);
344 minWidth=qMax(rect.width()+label_padding,minWidth);
345 minHeight+=rect.height();
345 minHeight+=rect.height();
346 QPointF center = rect.center();
346 QPointF center = rect.center();
347 labelItem->setTransformOriginPoint(center.x(), center.y());
347 labelItem->setTransformOriginPoint(center.x(), center.y());
348 labelItem->setPos(m_rect.left() - rect.width() - label_padding , layout[i]-center.y());
348 labelItem->setPos(m_rect.left() - rect.width() - label_padding , layout[i]-center.y());
349 } else {
349 } else {
350 labelItem->setText(ticksList.at(i));
350 labelItem->setText(ticksList.at(i));
351 const QRectF& rect = labelItem->boundingRect();
351 const QRectF& rect = labelItem->boundingRect();
352 minWidth=qMax(rect.width(),minWidth);
352 minWidth=qMax(rect.width(),minWidth);
353 minHeight+=rect.height();
353 minHeight+=rect.height();
354 QPointF center = rect.center();
354 QPointF center = rect.center();
355 labelItem->setTransformOriginPoint(center.x(), center.y());
355 labelItem->setTransformOriginPoint(center.x(), center.y());
356 labelItem->setPos(m_rect.left() - rect.width() - label_padding , layout[i] - (layout[i] - layout[i-1])/2 -center.y());
356 labelItem->setPos(m_rect.left() - rect.width() - label_padding , layout[i] - (layout[i] - layout[i-1])/2 -center.y());
357 }
357 }
358
358
359 if ((i+1)%2 && i>1) {
359 if ((i+1)%2 && i>1) {
360 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1));
360 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1));
361 rectItem->setRect(m_rect.left(),layout[i],m_rect.width(),layout[i-1]-layout[i]);
361 rectItem->setRect(m_rect.left(),layout[i],m_rect.width(),layout[i-1]-layout[i]);
362 }
362 }
363 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
363 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
364 lineItem->setLine(m_rect.left()-5,layout[i],m_rect.left(),layout[i]);
364 lineItem->setLine(m_rect.left()-5,layout[i],m_rect.left(),layout[i]);
365 }
365 }
366 }
366 }
367 break;
367 break;
368 default:
368 default:
369 qWarning()<<"Unknown axis type";
369 qWarning()<<"Unknown axis type";
370 break;
370 break;
371 }
371 }
372
372
373 m_layoutVector=layout;
373 m_layoutVector=layout;
374
374
375 presenter()->setMinimumMarginWidth(this,minWidth);
375 presenter()->setMinimumMarginWidth(this,minWidth);
376 presenter()->setMinimumMarginHeight(this,minHeight);
376 presenter()->setMinimumMarginHeight(this,minHeight);
377
377
378 }
378 }
379
379
380 bool ChartAxis::isEmpty()
380 bool ChartAxis::isEmpty()
381 {
381 {
382 return m_rect.isEmpty() || qFuzzyIsNull(m_min - m_max) || m_ticksCount==0;
382 return m_rect.isEmpty() || qFuzzyIsNull(m_min - m_max) || m_ticksCount==0;
383 }
383 }
384
384
385 //handlers
385 //handlers
386
386
387 void ChartAxis::handleAxisCategoriesUpdated()
387 void ChartAxis::handleAxisCategoriesUpdated()
388 {
388 {
389 if (isEmpty()) return;
389 if (isEmpty()) return;
390 updateLayout(m_layoutVector);
390 updateLayout(m_layoutVector);
391 }
391 }
392
392
393 void ChartAxis::handleAxisUpdated()
393 void ChartAxis::handleAxisUpdated()
394 {
394 {
395
395
396 if (isEmpty()) return;
396 if (isEmpty()) return;
397
397
398 if (m_chartAxis->isAxisVisible()) {
398 if (m_chartAxis->isAxisVisible()) {
399 setAxisOpacity(100);
399 setAxisOpacity(100);
400 } else {
400 } else {
401 setAxisOpacity(0);
401 setAxisOpacity(0);
402 }
402 }
403
403
404 if (m_chartAxis->isGridLineVisible()) {
404 if (m_chartAxis->isGridLineVisible()) {
405 setGridOpacity(100);
405 setGridOpacity(100);
406 } else {
406 } else {
407 setGridOpacity(0);
407 setGridOpacity(0);
408 }
408 }
409
409
410 if (m_chartAxis->labelsVisible()) {
410 if (m_chartAxis->labelsVisible()) {
411 setLabelsOpacity(100);
411 setLabelsOpacity(100);
412 } else {
412 } else {
413 setLabelsOpacity(0);
413 setLabelsOpacity(0);
414 }
414 }
415
415
416 if (m_chartAxis->shadesVisible()) {
416 if (m_chartAxis->shadesVisible()) {
417 setShadesOpacity(m_chartAxis->shadesOpacity());
417 setShadesOpacity(m_chartAxis->shadesOpacity());
418 } else {
418 } else {
419 setShadesOpacity(0);
419 setShadesOpacity(0);
420 }
420 }
421
421
422 setLabelsAngle(m_chartAxis->labelsAngle());
422 setLabelsAngle(m_chartAxis->labelsAngle());
423 setAxisPen(m_chartAxis->axisPen());
423 setAxisPen(m_chartAxis->axisPen());
424 setLabelsPen(m_chartAxis->labelsPen());
424 setLabelsPen(m_chartAxis->labelsPen());
425 setLabelsBrush(m_chartAxis->labelsBrush());
425 setLabelsBrush(m_chartAxis->labelsBrush());
426 setLabelsFont(m_chartAxis->labelsFont());
426 setLabelsFont(m_chartAxis->labelsFont());
427 setGridPen(m_chartAxis->gridLinePen());
427 setGridPen(m_chartAxis->gridLinePen());
428 setShadesPen(m_chartAxis->shadesPen());
428 setShadesPen(m_chartAxis->shadesPen());
429 setShadesBrush(m_chartAxis->shadesBrush());
429 setShadesBrush(m_chartAxis->shadesBrush());
430
430
431 }
431 }
432
432
433 void ChartAxis::handleRangeChanged(qreal min, qreal max,int tickCount)
433 void ChartAxis::handleRangeChanged(qreal min, qreal max,int tickCount)
434 {
434 {
435 if (qFuzzyIsNull(min - max) || tickCount < 2)
435 if (qFuzzyIsNull(min - max) || tickCount < 2)
436 return;
436 return;
437
437
438 m_min = min;
438 m_min = min;
439 m_max = max;
439 m_max = max;
440 m_ticksCount= tickCount;
440 m_ticksCount= tickCount;
441
441
442 if (isEmpty()) return;
442 if (isEmpty()) return;
443 QVector<qreal> layout = calculateLayout();
443 QVector<qreal> layout = calculateLayout();
444 updateLayout(layout);
444 updateLayout(layout);
445
445
446 }
446 }
447
447
448 void ChartAxis::handleGeometryChanged(const QRectF &rect)
448 void ChartAxis::handleGeometryChanged(const QRectF &rect)
449 {
449 {
450 if(m_rect != rect)
450 if(m_rect != rect)
451 {
451 {
452 m_rect = rect;
452 m_rect = rect;
453 if (isEmpty()) return;
453 if (isEmpty()) return;
454 QVector<qreal> layout = calculateLayout();
454 QVector<qreal> layout = calculateLayout();
455 updateLayout(layout);
455 updateLayout(layout);
456 }
456 }
457 }
457 }
458
458
459 void ChartAxis::axisSelected()
459 void ChartAxis::axisSelected()
460 {
460 {
461 qDebug()<<"TODO: axis clicked";
461 qDebug()<<"TODO: axis clicked";
462 }
462 }
463
463
464 #include "moc_chartaxis_p.cpp"
464 #include "moc_chartaxis_p.cpp"
465
465
466 QTCOMMERCIALCHART_END_NAMESPACE
466 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,108 +1,110
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #ifndef QAXIS_H
21 #ifndef QAXIS_H
22 #define QAXIS_H
22 #define QAXIS_H
23
23
24 #include <qchartglobal.h>
24 #include <qchartglobal.h>
25 #include <qaxiscategories.h>
25 #include <qaxiscategories.h>
26 #include <QPen>
26 #include <QPen>
27 #include <QFont>
27 #include <QFont>
28
28
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30
30
31 class QAxisPrivate;
31 class QAxisPrivate;
32
32
33 class QTCOMMERCIALCHART_EXPORT QAxis : public QObject
33 class QTCOMMERCIALCHART_EXPORT QAxis : public QObject
34 {
34 {
35 Q_OBJECT
35 Q_OBJECT
36 Q_PROPERTY(bool labelsVisible READ labelsVisible WRITE setLabelsVisible)
37
36 public:
38 public:
37
39
38 QAxis(QObject *parent =0);
40 QAxis(QObject *parent =0);
39 ~QAxis();
41 ~QAxis();
40
42
41 //axis handling
43 //axis handling
42 bool isAxisVisible() const;
44 bool isAxisVisible() const;
43 void setAxisVisible(bool visible = true);
45 void setAxisVisible(bool visible = true);
44 void setAxisPen(const QPen &pen);
46 void setAxisPen(const QPen &pen);
45 QPen axisPen() const;
47 QPen axisPen() const;
46
48
47 //grid handling
49 //grid handling
48 bool isGridLineVisible() const;
50 bool isGridLineVisible() const;
49 void setGridLineVisible(bool visible = true);
51 void setGridLineVisible(bool visible = true);
50 void setGridLinePen(const QPen &pen);
52 void setGridLinePen(const QPen &pen);
51 QPen gridLinePen() const;
53 QPen gridLinePen() const;
52
54
53 //labels handling
55 //labels handling
54 bool labelsVisible() const;
56 bool labelsVisible() const;
55 void setLabelsVisible(bool visible = true);
57 void setLabelsVisible(bool visible = true);
56 void setLabelsPen(const QPen &pen);
58 void setLabelsPen(const QPen &pen);
57 QPen labelsPen() const;
59 QPen labelsPen() const;
58 void setLabelsBrush(const QBrush &brush);
60 void setLabelsBrush(const QBrush &brush);
59 QBrush labelsBrush() const;
61 QBrush labelsBrush() const;
60 void setLabelsFont(const QFont &font);
62 void setLabelsFont(const QFont &font);
61 QFont labelsFont() const;
63 QFont labelsFont() const;
62 void setLabelsAngle(int angle);
64 void setLabelsAngle(int angle);
63 int labelsAngle() const;
65 int labelsAngle() const;
64
66
65 //shades handling
67 //shades handling
66 bool shadesVisible() const;
68 bool shadesVisible() const;
67 void setShadesVisible(bool visible = true);
69 void setShadesVisible(bool visible = true);
68 void setShadesPen(const QPen &pen);
70 void setShadesPen(const QPen &pen);
69 QPen shadesPen() const;
71 QPen shadesPen() const;
70 void setShadesBrush(const QBrush &brush);
72 void setShadesBrush(const QBrush &brush);
71 QBrush shadesBrush() const;
73 QBrush shadesBrush() const;
72 void setShadesOpacity(qreal opacity);
74 void setShadesOpacity(qreal opacity);
73 qreal shadesOpacity() const;
75 qreal shadesOpacity() const;
74
76
75 //range handling
77 //range handling
76 void setMin(qreal min);
78 void setMin(qreal min);
77 qreal min() const;
79 qreal min() const;
78 void setMax(qreal max);
80 void setMax(qreal max);
79 qreal max() const;
81 qreal max() const;
80 void setRange(qreal min, qreal max);
82 void setRange(qreal min, qreal max);
81
83
82 //ticks handling
84 //ticks handling
83 void setTicksCount(int count);
85 void setTicksCount(int count);
84 int ticksCount() const;
86 int ticksCount() const;
85
87
86 void setNiceNumbersEnabled(bool enable = true);
88 void setNiceNumbersEnabled(bool enable = true);
87 bool niceNumbersEnabled() const;
89 bool niceNumbersEnabled() const;
88
90
89 QAxisCategories* categories();
91 QAxisCategories* categories();
90
92
91 void show();
93 void show();
92 void hide();
94 void hide();
93
95
94 Q_SIGNALS:
96 Q_SIGNALS:
95 void minChanged(qreal min);
97 void minChanged(qreal min);
96 void maxChanged(qreal max);
98 void maxChanged(qreal max);
97 void rangeChanged(qreal min, qreal max);
99 void rangeChanged(qreal min, qreal max);
98 void ticksCountChanged(int count);
100 void ticksCountChanged(int count);
99
101
100 private:
102 private:
101 QScopedPointer<QAxisPrivate> d_ptr;
103 QScopedPointer<QAxisPrivate> d_ptr;
102 Q_DISABLE_COPY(QAxis);
104 Q_DISABLE_COPY(QAxis);
103 friend class ChartDataSet;
105 friend class ChartDataSet;
104 friend class ChartAxis;
106 friend class ChartAxis;
105 };
107 };
106
108
107 QTCOMMERCIALCHART_END_NAMESPACE
109 QTCOMMERCIALCHART_END_NAMESPACE
108 #endif /* QCHARTAXIS_H_ */
110 #endif /* QCHARTAXIS_H_ */
General Comments 0
You need to be logged in to leave comments. Login now