##// END OF EJS Templates
Refactored themes; now enabled for line, scatter and pies...
Tero Ahola -
r103:399cbfcd557c
parent child
Show More
@@ -0,0 +1,24
1 #ifndef CHARTITEMCONTROL_H
2 #define CHARTITEMCONTROL_H
3
4 #include "plotdomain_p.h"
5 #include <qchartglobal.h>
6 #include <QSize>
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
10 class ChartTheme;
11 class PlotDomain;
12
13 class ChartItemControl
14 {
15 public:
16 virtual void setPos (const QPointF & pos) = 0;
17 virtual void resize(const QSize &size) = 0;
18 virtual void setTheme(ChartTheme *theme) = 0;
19 virtual void setPlotDomain(const PlotDomain& data) = 0;
20 };
21
22 QTCOMMERCIALCHART_END_NAMESPACE
23
24 #endif // CHARTITEMCONTROL_H
@@ -0,0 +1,102
1 #include "charttheme_p.h"
2 #include "qchart.h"
3
4 QTCOMMERCIALCHART_BEGIN_NAMESPACE
5
6 void ChartThemeData::setTheme(int theme)
7 {
8 if (theme == m_currentTheme) return;
9
10 m_seriesThemes.clear();
11 m_seriesIndex = 0;
12 m_currentTheme = theme;
13
14 switch (theme) {
15 case QChart::ChartThemeDefault:
16 // line: solid, dashed, dotted
17 // line width: 1
18 // line color (and opacity)
19 // line shadow (on/off)
20 // marker shape: "x", "o", "."
21 // TODO: define the default theme based on the OS
22 m_seriesThemes.append(SeriesTheme(m_seriesThemes.count(), QRgb(0xff000000), 2));
23 m_seriesThemes.append(SeriesTheme(m_seriesThemes.count(), QRgb(0xff707070), 2));
24 m_gradientStartColor = QColor(QRgb(0xffffffff));
25 m_gradientEndColor = QColor(QRgb(0xffafafaf));
26 break;
27 case QChart::ChartThemeVanilla:
28 m_seriesThemes.append(SeriesTheme(m_seriesThemes.count(), QColor(217, 197, 116), 10));
29 m_seriesThemes.append(SeriesTheme(m_seriesThemes.count(), QColor(214, 168, 150), 10));
30 m_seriesThemes.append(SeriesTheme(m_seriesThemes.count(), QColor(160, 160, 113), 10));
31 m_seriesThemes.append(SeriesTheme(m_seriesThemes.count(), QColor(210, 210, 52), 10));
32 m_seriesThemes.append(SeriesTheme(m_seriesThemes.count(), QColor(136, 114, 58), 10));
33
34 m_gradientStartColor = QColor(QRgb(0xff9d844d));
35 m_gradientEndColor = QColor(QRgb(0xffafafaf));
36 break;
37 case QChart::ChartThemeIcy:
38 m_seriesThemes.append(SeriesTheme(m_seriesThemes.count(), QColor(0, 3, 165), 2));
39 m_seriesThemes.append(SeriesTheme(m_seriesThemes.count(), QColor(49, 52, 123), 2));
40 m_seriesThemes.append(SeriesTheme(m_seriesThemes.count(), QColor(71, 114, 187), 2));
41 m_seriesThemes.append(SeriesTheme(m_seriesThemes.count(), QColor(48, 97, 87), 2));
42 m_seriesThemes.append(SeriesTheme(m_seriesThemes.count(), QColor(19, 71, 90), 2));
43 m_seriesThemes.append(SeriesTheme(m_seriesThemes.count(), QColor(110, 70, 228), 2));
44
45 m_gradientStartColor = QColor(QRgb(0xffe4ffff));
46 m_gradientEndColor = QColor(QRgb(0xffe4ffff));
47 break;
48 case QChart::ChartThemeGrayscale:
49 m_seriesThemes.append(SeriesTheme(m_seriesThemes.count(), QColor(0, 0, 0), 2));
50 m_seriesThemes.append(SeriesTheme(m_seriesThemes.count(), QColor(50, 50, 50), 2));
51 m_seriesThemes.append(SeriesTheme(m_seriesThemes.count(), QColor(100, 100, 100), 2));
52 m_seriesThemes.append(SeriesTheme(m_seriesThemes.count(), QColor(140, 140, 140), 2));
53 m_seriesThemes.append(SeriesTheme(m_seriesThemes.count(), QColor(180, 180, 180), 2));
54
55 m_gradientStartColor = QColor(QRgb(0xffffffff));
56 m_gradientEndColor = QColor(QRgb(0xffafafaf));
57 break;
58 case QChart::ChartThemeUnnamed1:
59 m_seriesThemes.append(SeriesTheme(m_seriesThemes.count(), QColor(QRgb(0xff3fa9f5)), 2));
60 m_seriesThemes.append(SeriesTheme(m_seriesThemes.count(), QColor(QRgb(0xff7AC943)), 2));
61 m_seriesThemes.append(SeriesTheme(m_seriesThemes.count(), QColor(QRgb(0xffFF931E)), 2));
62 m_seriesThemes.append(SeriesTheme(m_seriesThemes.count(), QColor(QRgb(0xffFF1D25)), 2));
63 m_seriesThemes.append(SeriesTheme(m_seriesThemes.count(), QColor(QRgb(0xffFF7BAC)), 2));
64
65 m_gradientStartColor = QColor(QRgb(0xfff3dc9e));
66 m_gradientEndColor = QColor(QRgb(0xffafafaf));
67 break;
68 default:
69 Q_ASSERT(false);
70 break;
71 }
72
73 //themeChanged();
74 // TODO: update coloring of different elements to match the selected theme
75 }
76
77 ChartTheme::ChartTheme(QObject *parent) :
78 QObject(parent),
79 d(new ChartThemeData())
80 {
81 d->m_currentTheme = QChart::ChartThemeInvalid;
82 d->m_seriesIndex = 0;
83 }
84
85 SeriesTheme ChartTheme::themeForSeries()
86 {
87 if (d->m_seriesThemes.count() == 0) {
88 return SeriesTheme();
89 } else {
90 // Get the next available theme for the series; if no more themes available start over
91 // beginning from the first theme in the list
92 SeriesTheme nextTheme =
93 d->m_seriesThemes[d->m_seriesIndex % d->m_seriesThemes.count()];
94 d->m_seriesIndex++;
95 return nextTheme;
96 }
97 }
98
99
100 #include "moc_charttheme_p.cpp"
101
102 QTCOMMERCIALCHART_END_NAMESPACE
@@ -0,0 +1,80
1 #ifndef CHARTTHEME_H
2 #define CHARTTHEME_H
3
4 #include "qchartglobal.h"
5 #include <QObject>
6 #include <QSharedData>
7 #include <QColor>
8 #include <QLinearGradient>
9 #include <QPen>
10
11 QTCOMMERCIALCHART_BEGIN_NAMESPACE
12
13 /*!
14 * The theme specific settings for the appearance of a series. TODO: These can be overridden by setting
15 * custom settings to a QChartSeries object.
16 */
17 struct SeriesTheme {
18 public:
19 // SeriesTheme() :
20 // themeIndex(-1), lineColor(QColor()) {}
21 // SeriesTheme(int index, QColor line) :
22 // themeIndex(index), lineColor(line) {}
23 SeriesTheme() :
24 themeIndex(-1), linePen(QPen()), markerPen(QPen()) {}
25 SeriesTheme(int index, QColor lineColor, qreal lineWidth/*, QPen marker*/) :
26 themeIndex(index),
27 linePen(QPen(QBrush(lineColor), lineWidth)),
28 markerPen(linePen) {}
29
30 //const QBrush & brush, qreal width, Qt::PenStyle style = Qt::SolidLine, Qt::PenCapStyle cap = Qt::SquareCap, Qt::PenJoinStyle join = Qt::BevelJo
31 int themeIndex;
32 // TODO:
33 //QColor lineColor;
34 QPen linePen;
35 //QBrush lineBrush;
36 QPen markerPen;
37 //QBrush markerBrush;
38 };
39
40 /*!
41 * Explicitly shared data class for the themes.
42 */
43 class ChartThemeData : public QSharedData
44 {
45 public:
46 ChartThemeData() : m_currentTheme(0) {}
47 ~ChartThemeData() {}
48
49 public:
50 void setTheme(int theme);
51
52 public:
53 int m_currentTheme;
54 QColor m_gradientStartColor;
55 QColor m_gradientEndColor;
56 QList<SeriesTheme> m_seriesThemes;
57 int m_seriesIndex;
58 };
59
60 class ChartTheme : public QObject
61 {
62 Q_OBJECT
63 public:
64 explicit ChartTheme(QObject *parent = 0);
65 explicit ChartTheme(const ChartTheme &other) : d(other.d) {}
66 void operator =(const ChartTheme &other) { d = other.d; }
67
68 //signals:
69 // void themeChanged(ChartTheme theme);
70 SeriesTheme themeForSeries();
71
72 public:
73 // All the graphical elements of a QChart share the same theme settings
74 // so let's use explicitly shared data
75 QExplicitlySharedDataPointer<ChartThemeData> d;
76 };
77
78 QTCOMMERCIALCHART_END_NAMESPACE
79
80 #endif // CHARTTHEME_H
@@ -0,0 +1,40
1 #ifndef PIESERIESPRIVATE_H
2 #define PIESERIESPRIVATE_H
3
4 #include "chartitemcontrol.h"
5 #include "qpieseries.h"
6 #include <QRectF>
7 #include <QColor>
8
9 class QGraphicsItem;
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11 class PieSlice;
12
13 class QPieSeriesPrivate : public ChartItemControl
14 {
15 public:
16 // TODO: use a generic data class instead of x and y
17 QPieSeriesPrivate();
18 ~QPieSeriesPrivate();
19
20 public: // from ChartItemControl
21 void setPos(const QPointF & pos);
22 void resize(const QSize &size);
23 void setTheme(ChartTheme *theme);
24 void setPlotDomain(const PlotDomain& data);
25
26 public:
27 bool setData(QList<qreal> data);
28 void resizeSlices(QRectF rect);
29 QGraphicsItem *m_parentItem;
30 QList<qreal> m_data;
31 QList<PieSlice*> m_slices;
32 QRectF m_chartSize;
33 qreal m_sizeFactor;
34 QPieSeries::PiePosition m_position;
35 ChartTheme *m_chartTheme;
36 };
37
38 QTCOMMERCIALCHART_END_NAMESPACE
39
40 #endif // PIESERIESPRIVATE_H
@@ -6,9 +6,10
6
6
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8
8
9 AxisItem::AxisItem(AxisType type,QGraphicsItem* parent): ChartItem(parent),
9 AxisItem::AxisItem(AxisType type,QGraphicsItem* parent) :
10 m_ticks(4),
10 QGraphicsItem(parent),
11 m_type(type)
11 m_ticks(4),
12 m_type(type)
12 {
13 {
13 }
14 }
14
15
@@ -16,12 +17,6 AxisItem::~AxisItem()
16 {
17 {
17 }
18 }
18
19
19 void AxisItem::setSize(const QSize& size)
20 {
21 m_rect = QRectF(QPoint(0,0),size);
22 createItems();
23 }
24
25 void AxisItem::setLength(int length)
20 void AxisItem::setLength(int length)
26 {
21 {
27 QPainterPath path;
22 QPainterPath path;
@@ -47,6 +42,25 void AxisItem::setPlotDomain(const PlotDomain& plotDomain)
47 m_plotDomain = plotDomain;
42 m_plotDomain = plotDomain;
48 createItems();
43 createItems();
49 }
44 }
45
46 void AxisItem::setPos(const QPointF & pos)
47 {
48 QGraphicsItem::setPos(pos);
49 }
50
51 void AxisItem::resize(const QSize &size)
52 {
53 m_rect = QRectF(QPoint(0,0),size);
54 createItems();
55 }
56
57 void AxisItem::setTheme(ChartTheme *theme)
58 {
59 if (theme) {
60 // TODO: add axis related properties to the theme class and use them here
61 }
62 }
63
50 /*
64 /*
51 void AxisItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget)
65 void AxisItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget)
52 {
66 {
@@ -1,13 +1,13
1 #ifndef AXISITEM_H_
1 #ifndef AXISITEM_H_
2 #define AXISITEM_H_
2 #define AXISITEM_H_
3
3
4 #include "chartitem_p.h"
5 #include "plotdomain_p.h"
4 #include "plotdomain_p.h"
5 #include "chartitemcontrol.h"
6 #include <QGraphicsItem>
6 #include <QGraphicsItem>
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 class AxisItem: public ChartItem
10 class AxisItem: public QGraphicsItem, public ChartItemControl
11 {
11 {
12 public:
12 public:
13 enum AxisType{X_AXIS,Y_AXIS};
13 enum AxisType{X_AXIS,Y_AXIS};
@@ -19,10 +19,13 public:
19 QRectF boundingRect() const;
19 QRectF boundingRect() const;
20 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){};
20 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){};
21
21
22 //from ChartItem
22 public: // from ChartItemControl
23 void setSize(const QSize& size);
23 void setPos (const QPointF & pos);
24 void resize(const QSize &size);
25 void setTheme(ChartTheme *theme);
24 void setPlotDomain(const PlotDomain& data);
26 void setPlotDomain(const PlotDomain& data);
25
27
28 public:
26 void setLength(int length);
29 void setLength(int length);
27 void setWidth(int width);
30 void setWidth(int width);
28 AxisType axisType() const {return m_type;};
31 AxisType axisType() const {return m_type;};
@@ -4,12 +4,17
4
4
5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
6
6
7 Bar::Bar(ChartItem *parent)
7 Bar::Bar(QGraphicsItem *parent)
8 : ChartItem(parent)
8 : QGraphicsItem(parent)
9 {
9 {
10 }
10 }
11
11
12 void Bar::setSize(const QSize& size)
12 void Bar::setPos(const QPointF & pos)
13 {
14 QGraphicsItem::setPos(pos);
15 }
16
17 void Bar::resize(const QSize& size)
13 {
18 {
14 mWidth = size.width();
19 mWidth = size.width();
15 mHeight = size.height();
20 mHeight = size.height();
@@ -20,6 +25,11 void Bar::setPlotDomain(const PlotDomain& data)
20 mPlotDomain = data;
25 mPlotDomain = data;
21 }
26 }
22
27
28 void Bar::setTheme(ChartTheme *theme)
29 {
30 // TODO
31 }
32
23 void Bar::resize( int w, int h )
33 void Bar::resize( int w, int h )
24 {
34 {
25 // qDebug() << "bar::resize" << w << h;
35 // qDebug() << "bar::resize" << w << h;
@@ -1,20 +1,23
1 #ifndef BAR_H
1 #ifndef BAR_H
2 #define BAR_H
2 #define BAR_H
3
3
4 #include "chartitem_p.h"
4 #include "chartitemcontrol.h"
5 #include "qchartglobal.h"
5 #include "qchartglobal.h"
6 #include <QGraphicsItem>
6
7
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8
9
9 // Single bar item of chart
10 // Single bar item of chart
10 class Bar : public ChartItem
11 class Bar : public QGraphicsItem, public ChartItemControl
11 {
12 {
12 public:
13 public:
13 Bar(ChartItem *parent=0);
14 Bar(QGraphicsItem *parent=0);
14
15
15 // From ChartItem
16 public: // from ChartItemControl
16 virtual void setSize(const QSize& size);
17 void setPos (const QPointF & pos);
17 virtual void setPlotDomain(const PlotDomain& data);
18 void resize(const QSize &size);
19 void setTheme(ChartTheme *theme);
20 void setPlotDomain(const PlotDomain& data);
18
21
19 // Layout Stuff
22 // Layout Stuff
20 void resize( int w, int h ); // Size of bar. in screen coordinates.
23 void resize( int w, int h ); // Size of bar. in screen coordinates.
@@ -5,7 +5,7
5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
6
6
7 BarGroup::BarGroup(BarChartSeries& series, QGraphicsItem *parent) :
7 BarGroup::BarGroup(BarChartSeries& series, QGraphicsItem *parent) :
8 ChartItem(parent)
8 QGraphicsItem(parent)
9 ,mSeries(series)
9 ,mSeries(series)
10 ,mLayoutSet(false)
10 ,mLayoutSet(false)
11 ,mLayoutDirty(true)
11 ,mLayoutDirty(true)
@@ -14,8 +14,12 BarGroup::BarGroup(BarChartSeries& series, QGraphicsItem *parent) :
14 dataChanged();
14 dataChanged();
15 }
15 }
16
16
17 void BarGroup::setPos(const QPointF & pos)
18 {
19 QGraphicsItem::setPos(pos);
20 }
17
21
18 void BarGroup::setSize(const QSize& size)
22 void BarGroup::resize(const QSize& size)
19 {
23 {
20 qDebug() << "BarGroup::setSize";
24 qDebug() << "BarGroup::setSize";
21 mWidth = size.width();
25 mWidth = size.width();
@@ -31,6 +35,11 void BarGroup::setPlotDomain(const PlotDomain& data)
31 mPlotDomain = data;
35 mPlotDomain = data;
32 }
36 }
33
37
38 void BarGroup::setTheme(ChartTheme *theme)
39 {
40 // TODO
41 }
42
34 void BarGroup::setBarWidth( int w )
43 void BarGroup::setBarWidth( int w )
35 {
44 {
36 mBarDefaultWidth = w;
45 mBarDefaultWidth = w;
@@ -1,20 +1,23
1 #ifndef QBARGROUP_H
1 #ifndef QBARGROUP_H
2 #define QBARGROUP_H
2 #define QBARGROUP_H
3
3
4 #include "chartitem_p.h"
4 #include "chartitemcontrol.h"
5 #include "bar.h"
5 #include "bar.h"
6 #include "barchartseries.h"
6 #include "barchartseries.h"
7 #include <QGraphicsItem>
7
8
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
10
10 class BarGroup : public ChartItem
11 class BarGroup : public QGraphicsItem, public ChartItemControl
11 {
12 {
12 public:
13 public:
13 explicit BarGroup(BarChartSeries& series, QGraphicsItem *parent = 0);
14 explicit BarGroup(BarChartSeries& series, QGraphicsItem *parent = 0);
14
15
15 // From ChartItem
16 public: // from ChartItemControl
16 virtual void setSize(const QSize& size);
17 void setPos (const QPointF & pos);
17 virtual void setPlotDomain(const PlotDomain& data);
18 void resize(const QSize &size);
19 void setTheme(ChartTheme *theme);
20 void setPlotDomain(const PlotDomain& data);
18
21
19 // Layout "api"
22 // Layout "api"
20 void setPos(qreal x, qreal y);
23 void setPos(qreal x, qreal y);
@@ -7,19 +7,25
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8
8
9 StackedBarGroup::StackedBarGroup(StackedBarChartSeries& series, QGraphicsItem *parent) :
9 StackedBarGroup::StackedBarGroup(StackedBarChartSeries& series, QGraphicsItem *parent) :
10 ChartItem(parent)
10 QGraphicsItem(parent)
11 ,mSeries(series)
11 ,mSeries(series)
12 ,mLayoutSet(false)
12 ,mLayoutSet(false)
13 ,mLayoutDirty(true)
13 ,mLayoutDirty(true)
14 ,mBarDefaultWidth(20) // TODO: remove hard coding, when we have layout code ready
14 ,mBarDefaultWidth(20) // TODO: remove hard coding, when we have layout code ready
15 {
15 {
16 dataChanged();
16 dataChanged();
17 }
17 }
18
18
19
19
20 void StackedBarGroup::setSize(const QSize& size)
20 void StackedBarGroup::setPos(const QPointF & pos)
21 {
21 {
22 qDebug() << "StackedBarGroup::setSize";
22 qDebug() << "StackedBarGroup::setPos";
23 // TODO:
24 }
25
26 void StackedBarGroup::resize(const QSize& size)
27 {
28 qDebug() << "StackedBarGroup::resize";
23 mWidth = size.width();
29 mWidth = size.width();
24 mHeight = size.height();
30 mHeight = size.height();
25 layoutChanged();
31 layoutChanged();
@@ -32,6 +38,12 void StackedBarGroup::setPlotDomain(const PlotDomain& data)
32 // TODO:
38 // TODO:
33 }
39 }
34
40
41 void StackedBarGroup::setTheme(ChartTheme *theme)
42 {
43 qDebug() << "StackedBarGroup::setTheme";
44 // TODO:
45 }
46
35 void StackedBarGroup::setBarWidth( int w )
47 void StackedBarGroup::setBarWidth( int w )
36 {
48 {
37 mBarDefaultWidth = w;
49 mBarDefaultWidth = w;
@@ -1,21 +1,25
1 #ifndef PERCENTBARGROUP_H
1 #ifndef PERCENTBARGROUP_H
2 #define PERCENTBARGROUP_H
2 #define PERCENTBARGROUP_H
3
3
4 #include "chartitem_p.h"
4 #include "chartitemcontrol.h"
5 #include "bar.h"
5 #include "bar.h"
6 #include "percentbarchartseries.h"
6 #include "percentbarchartseries.h"
7 #include <QGraphicsItem>
7
8
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
10
10 class PercentBarGroup : public ChartItem
11 class PercentBarGroup : public QGraphicsItem, public ChartItemControl
11 {
12 {
12 public:
13 public:
13 PercentBarGroup(PercentBarChartSeries& series, QGraphicsItem *parent = 0);
14 PercentBarGroup(PercentBarChartSeries& series, QGraphicsItem *parent = 0);
14
15
15 // From ChartItem
16 public: // From ChartItemControl
16 virtual void setSize(const QSize& size);
17 void setPos(const QPointF & pos);
17 virtual void setPlotDomain(const PlotDomain& data);
18 void resize(const QSize &size);
19 void setTheme(ChartTheme *theme);
20 void setPlotDomain(const PlotDomain& data);
18
21
22 public:
19 // Layout "api"
23 // Layout "api"
20 void setPos(qreal x, qreal y);
24 void setPos(qreal x, qreal y);
21 void setBarWidth( int w ); // Default width for each bar
25 void setBarWidth( int w ); // Default width for each bar
@@ -5,19 +5,24
5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
6
6
7 PercentBarGroup::PercentBarGroup(PercentBarChartSeries& series, QGraphicsItem *parent) :
7 PercentBarGroup::PercentBarGroup(PercentBarChartSeries& series, QGraphicsItem *parent) :
8 ChartItem(parent)
8 QGraphicsItem(parent)
9 ,mSeries(series)
9 ,mSeries(series)
10 ,mLayoutSet(false)
10 ,mLayoutSet(false)
11 ,mLayoutDirty(true)
11 ,mLayoutDirty(true)
12 ,mBarDefaultWidth(20) // TODO: remove hard coding, when we have layout code ready
12 ,mBarDefaultWidth(20) // TODO: remove hard coding, when we have layout code ready
13 {
13 {
14 dataChanged();
14 dataChanged();
15 }
15 }
16
16
17 void PercentBarGroup::setPos(const QPointF & pos)
18 {
19 qDebug() << "PercentBarGroup::setPos";
20 // TODO:
21 }
17
22
18 void PercentBarGroup::setSize(const QSize& size)
23 void PercentBarGroup::resize(const QSize& size)
19 {
24 {
20 qDebug() << "PercentBarGroup::setSize";
25 qDebug() << "PercentBarGroup::resize";
21 mWidth = size.width();
26 mWidth = size.width();
22 mHeight = size.height();
27 mHeight = size.height();
23 layoutChanged();
28 layoutChanged();
@@ -30,6 +35,12 void PercentBarGroup::setPlotDomain(const PlotDomain& data)
30 // TODO:
35 // TODO:
31 }
36 }
32
37
38 void PercentBarGroup::setTheme(ChartTheme *theme)
39 {
40 qDebug() << "PercentBarGroup::setTheme";
41 // TODO:
42 }
43
33 void PercentBarGroup::setBarWidth( int w )
44 void PercentBarGroup::setBarWidth( int w )
34 {
45 {
35 mBarDefaultWidth = w;
46 mBarDefaultWidth = w;
@@ -1,22 +1,25
1 #ifndef STACKEDBARGROUP_H
1 #ifndef STACKEDBARGROUP_H
2 #define STACKEDBARGROUP_H
2 #define STACKEDBARGROUP_H
3
3
4 #include "chartitem_p.h"
4 #include "chartitemcontrol.h"
5 #include "bar.h"
5 #include "bar.h"
6 #include "stackedbarchartseries.h"
6 #include "stackedbarchartseries.h"
7 #include <QGraphicsItem>
7
8
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
10
10 class StackedBarGroup : public ChartItem
11 class StackedBarGroup : public QGraphicsItem, public ChartItemControl
11 {
12 {
12 public:
13 public:
13 StackedBarGroup(StackedBarChartSeries& series, QGraphicsItem *parent = 0);
14 StackedBarGroup(StackedBarChartSeries& series, QGraphicsItem *parent = 0);
14
15
15 // From ChartItem
16 public: // From ChartItemControl
16 virtual void setSize(const QSize& size);
17 void setPos(const QPointF & pos);
17 virtual void setPlotDomain(const PlotDomain& data);
18 void resize(const QSize &size);
19 void setTheme(ChartTheme *theme);
20 void setPlotDomain(const PlotDomain& data);
18
21
19 // Layout "api"
22 public: // Layout "api"
20 void setPos(qreal x, qreal y);
23 void setPos(qreal x, qreal y);
21 void setBarWidth( int w ); // Default width for each bar
24 void setBarWidth( int w ); // Default width for each bar
22
25
@@ -46,14 +46,10 QPainterPath PieSlice::shape() const
46
46
47 void PieSlice::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
47 void PieSlice::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
48 {
48 {
49 // Setup painter
50 painter->setBrush(m_color);
51 painter->setRenderHint(QPainter::Antialiasing);
49 painter->setRenderHint(QPainter::Antialiasing);
52 QPen pen;
50 painter->setPen(m_theme.linePen);
53 //pen.setColor(m_color.darker());
51 // TODO:
54 pen.setColor(Qt::gray);
52 painter->setBrush(m_theme.linePen.color());
55 pen.setWidth(1);
56 painter->setPen(pen);
57
53
58 // From Qt docs:
54 // From Qt docs:
59 // The startAngle and spanAngle must be specified in 1/16th of a degree, i.e. a full circle equals 5760 (16 * 360).
55 // The startAngle and spanAngle must be specified in 1/16th of a degree, i.e. a full circle equals 5760 (16 * 360).
@@ -2,6 +2,7
2 #define PIESLICE_H
2 #define PIESLICE_H
3
3
4 #include "qchartglobal.h"
4 #include "qchartglobal.h"
5 #include "charttheme_p.h"
5 #include <QGraphicsItem>
6 #include <QGraphicsItem>
6 #include <QRectF>
7 #include <QRectF>
7 #include <QColor>
8 #include <QColor>
@@ -25,6 +26,7 public:
25 qreal m_startAngle;
26 qreal m_startAngle;
26 qreal m_span;
27 qreal m_span;
27 QRectF m_rect;
28 QRectF m_rect;
29 SeriesTheme m_theme;
28 };
30 };
29
31
30 QTCOMMERCIALCHART_END_NAMESPACE
32 QTCOMMERCIALCHART_END_NAMESPACE
@@ -3,6 +3,7
3 #include "qscatterseries.h"
3 #include "qscatterseries.h"
4 #include "qscatterseries_p.h"
4 #include "qscatterseries_p.h"
5 #include "qpieseries.h"
5 #include "qpieseries.h"
6 #include "qpieseries_p.h"
6 #include "qxychartseries.h"
7 #include "qxychartseries.h"
7 #include "qchartaxis.h"
8 #include "qchartaxis.h"
8 #include "barchartseries.h"
9 #include "barchartseries.h"
@@ -11,6 +12,8
11 #include "stackedbargroup.h"
12 #include "stackedbargroup.h"
12 #include "percentbarchartseries.h"
13 #include "percentbarchartseries.h"
13 #include "percentbargroup.h"
14 #include "percentbargroup.h"
15 #include "charttheme_p.h"
16 #include "chartitemcontrol.h"
14
17
15 #include "xylinechartitem_p.h"
18 #include "xylinechartitem_p.h"
16 #include "plotdomain_p.h"
19 #include "plotdomain_p.h"
@@ -25,16 +28,17 QChart::QChart(QGraphicsObject* parent) : QGraphicsObject(parent),
25 m_titleItem(0),
28 m_titleItem(0),
26 m_axisXItem(new AxisItem(AxisItem::X_AXIS,this)),
29 m_axisXItem(new AxisItem(AxisItem::X_AXIS,this)),
27 m_plotDataIndex(0),
30 m_plotDataIndex(0),
28 m_marginSize(0)
31 m_marginSize(0),
32 m_chartTheme(new ChartTheme())
29 {
33 {
30 // TODO: the default theme?
34 // TODO: the default theme?
31 setTheme(QChart::ChartThemeDefault);
35 setTheme(QChart::ChartThemeDefault);
32
36
33 PlotDomain domain;
37 PlotDomain domain;
34 m_plotDomainList<<domain;
38 m_plotDomainList << domain;
35 m_axisYItem << new AxisItem(AxisItem::Y_AXIS,this);
39 m_axisYItem << new AxisItem(AxisItem::Y_AXIS,this);
36 m_chartItems<<m_axisXItem;
40 m_chartItemControls << m_axisXItem;
37 m_chartItems<<m_axisYItem.at(0);
41 m_chartItemControls << m_axisYItem.at(0);
38 }
42 }
39
43
40 QChart::~QChart(){}
44 QChart::~QChart(){}
@@ -55,11 +59,6 void QChart::addSeries(QChartSeries* series)
55 case QChartSeries::SeriesTypeLine: {
59 case QChartSeries::SeriesTypeLine: {
56
60
57 QXYChartSeries* xyseries = static_cast<QXYChartSeries*>(series);
61 QXYChartSeries* xyseries = static_cast<QXYChartSeries*>(series);
58 // Use color defined by theme in case the series does not define a custom color
59
60 if (!xyseries->pen().color().isValid() && m_themeColors.count()) //TODO: wtf
61 xyseries->setPen(nextColor());
62
63 m_plotDataIndex = 0 ;
62 m_plotDataIndex = 0 ;
64 m_plotDomainList.resize(1);
63 m_plotDomainList.resize(1);
65
64
@@ -76,9 +75,12 void QChart::addSeries(QChartSeries* series)
76 }
75 }
77
76
78 XYLineChartItem* item = new XYLineChartItem(xyseries,this);
77 XYLineChartItem* item = new XYLineChartItem(xyseries,this);
79 m_chartItems<<item;
80
78
81 foreach(ChartItem* i ,m_chartItems)
79 // TODO: combine ChartItemControl and ChartItem apis
80 m_chartItemControls << item;
81 item->setTheme(m_chartTheme);
82
83 foreach(ChartItemControl* i, m_chartItemControls)
82 i->setPlotDomain(m_plotDomainList.at(m_plotDataIndex));
84 i->setPlotDomain(m_plotDomainList.at(m_plotDataIndex));
83
85
84 break;
86 break;
@@ -96,7 +98,7 void QChart::addSeries(QChartSeries* series)
96 barGroup->addColor(QColor(0,0,255,128));
98 barGroup->addColor(QColor(0,0,255,128));
97 barGroup->addColor(QColor(255,128,0,128));
99 barGroup->addColor(QColor(255,128,0,128));
98
100
99 m_chartItems<<barGroup;
101 m_chartItemControls << barGroup;
100 childItems().append(barGroup);
102 childItems().append(barGroup);
101 break;
103 break;
102 }
104 }
@@ -113,7 +115,7 void QChart::addSeries(QChartSeries* series)
113 stackedBarGroup->addColor(QColor(0,0,255,128));
115 stackedBarGroup->addColor(QColor(0,0,255,128));
114 stackedBarGroup->addColor(QColor(255,128,0,128));
116 stackedBarGroup->addColor(QColor(255,128,0,128));
115
117
116 m_chartItems<<stackedBarGroup;
118 m_chartItemControls << stackedBarGroup;
117 childItems().append(stackedBarGroup);
119 childItems().append(stackedBarGroup);
118 break;
120 break;
119 }
121 }
@@ -130,31 +132,26 void QChart::addSeries(QChartSeries* series)
130 percentBarGroup->addColor(QColor(0,0,255,128));
132 percentBarGroup->addColor(QColor(0,0,255,128));
131 percentBarGroup->addColor(QColor(255,128,0,128));
133 percentBarGroup->addColor(QColor(255,128,0,128));
132
134
133 m_chartItems<<percentBarGroup;
135 m_chartItemControls << percentBarGroup;
134 childItems().append(percentBarGroup);
136 childItems().append(percentBarGroup);
135 break;
137 break;
136 }
138 }
137 case QChartSeries::SeriesTypeScatter: {
139 case QChartSeries::SeriesTypeScatter: {
138 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
140 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
141 scatterSeries->d->m_theme = m_chartTheme->themeForSeries();
139 scatterSeries->d->setParentItem(this);
142 scatterSeries->d->setParentItem(this);
140 // Set pre-defined colors in case the series has no colors defined
143 m_chartItemControls << scatterSeries->d;
141 if (!scatterSeries->markerColor().isValid())
144 //TODO:? scatterSeries->d->m_themeIndex = m_chartSeries.count() - 1;
142 scatterSeries->setMarkerColor(nextColor());
143 connect(this, SIGNAL(sizeChanged(QRectF)),
144 scatterSeries, SLOT(chartSizeChanged(QRectF)));
145 // QColor nextColor = m_themeColors.takeFirst();
146 // nextColor.setAlpha(150); // TODO: default opacity?
147 // scatterSeries->setMarkerColor(nextColor);
148 break;
145 break;
149 }
146 }
150 case QChartSeries::SeriesTypePie: {
147 case QChartSeries::SeriesTypePie: {
151 QPieSeries *pieSeries = qobject_cast<QPieSeries *>(series);
148 QPieSeries *pieSeries = qobject_cast<QPieSeries *>(series);
152 for (int i(0); i < pieSeries->sliceCount(); i++) {
149 // for (int i(0); i < pieSeries->sliceCount(); i++) {
153 if (!pieSeries->sliceColor(i).isValid())
150 // if (!pieSeries->sliceColor(i).isValid())
154 pieSeries->setSliceColor(i, nextColor());
151 // pieSeries->setSliceColor(i, nextColor());
155 }
152 // }
156 connect(this, SIGNAL(sizeChanged(QRectF)),
153 pieSeries->d->setTheme(m_chartTheme);
157 pieSeries, SLOT(chartSizeChanged(QRectF)));
154 m_chartItemControls << pieSeries->d;
158
155
159 // Set pre-defined colors in case the series has no colors defined
156 // Set pre-defined colors in case the series has no colors defined
160 // TODO: how to define the color for all the slices of a pie?
157 // TODO: how to define the color for all the slices of a pie?
@@ -217,27 +214,19 void QChart::setSize(const QSize& size)
217 }
214 }
218
215
219 //recalculate background gradient
216 //recalculate background gradient
220 if(m_backgroundItem){
217 if (m_backgroundItem) {
221 m_backgroundItem->setRect(rect);
218 m_backgroundItem->setRect(rect);
222 if(m_bacgroundOrinetation==HorizonatlGradientOrientation)
219 if (m_bacgroundOrinetation == HorizonatlGradientOrientation)
223 m_backgroundGradient.setFinalStop(m_backgroundItem->rect().width(),0);
220 m_backgroundGradient.setFinalStop(m_backgroundItem->rect().width(), 0);
224 else
221 else
225 m_backgroundGradient.setFinalStop(0,m_backgroundItem->rect().height());
222 m_backgroundGradient.setFinalStop(0, m_backgroundItem->rect().height());
226
227 m_backgroundItem->setBrush(m_backgroundGradient);
223 m_backgroundItem->setBrush(m_backgroundGradient);
228 }
224 }
229
225
230 //resize elements
226 foreach (ChartItemControl *ctrl, m_chartItemControls) {
231 foreach (ChartItem* item ,m_chartItems) {
227 ctrl->setPos(rect.topLeft());
232 item->setPos(rect.topLeft());
228 ctrl->resize(rect.size());
233 item->setSize(rect.size());
234
235 }
229 }
236 // TODO: TTD for setting scale
237 //emit scaleChanged(100, 100);
238 // TODO: calculate the origo
239 // TODO: not sure if emitting a signal here is the best from performance point of view
240 emit sizeChanged(QRectF(0, 0, size.width(), size.height()));
241
230
242 update();
231 update();
243 }
232 }
@@ -251,8 +240,8 void QChart::setBackground(const QColor& startColor, const QColor& endColor, Gra
251 }
240 }
252
241
253 m_bacgroundOrinetation = orientation;
242 m_bacgroundOrinetation = orientation;
254 m_backgroundGradient.setColorAt( 0.0, startColor);
243 m_backgroundGradient.setColorAt(0.0, startColor);
255 m_backgroundGradient.setColorAt( 1.0, endColor);
244 m_backgroundGradient.setColorAt(1.0, endColor);
256 m_backgroundGradient.setStart(0,0);
245 m_backgroundGradient.setStart(0,0);
257
246
258 if(orientation == VerticalGradientOrientation){
247 if(orientation == VerticalGradientOrientation){
@@ -285,86 +274,15 void QChart::setMargin(int margin)
285
274
286 void QChart::setTheme(QChart::ChartThemeId theme)
275 void QChart::setTheme(QChart::ChartThemeId theme)
287 {
276 {
288 // if (theme != m_currentTheme) {
277 if (theme != m_chartTheme->d->m_currentTheme) {
289 m_themeColors.clear();
278 m_chartTheme->d->setTheme(theme);
290
279 setBackground(m_chartTheme->d->m_gradientStartColor,
291 // TODO: define color themes
280 m_chartTheme->d->m_gradientEndColor,
292 switch (theme) {
281 m_bacgroundOrinetation);
293 case QChart::ChartThemeDefault:
282 foreach (ChartItemControl *ctrl, m_chartItemControls)
294 // TODO: define the default theme based on the OS
283 ctrl->setTheme(m_chartTheme);
295 m_themeColors.append(QColor(QRgb(0xff000000)));
284 update();
296 m_themeColors.append(QColor(QRgb(0xff707070)));
297 setBackground(QColor(QRgb(0xffffffff)), QColor(QRgb(0xffafafaf)), VerticalGradientOrientation);
298 break;
299 case QChart::ChartThemeVanilla:
300 m_themeColors.append(QColor(217, 197, 116));
301 m_themeColors.append(QColor(214, 168, 150));
302 m_themeColors.append(QColor(160, 160, 113));
303 m_themeColors.append(QColor(210, 210, 52));
304 m_themeColors.append(QColor(136, 114, 58));
305
306 setBackground(QColor(QRgb(0xff9d844d)), QColor(QRgb(0xffafafaf)), VerticalGradientOrientation);
307 break;
308 case QChart::ChartThemeIcy:
309 m_themeColors.append(QColor(0, 3, 165));
310 m_themeColors.append(QColor(49, 52, 123));
311 m_themeColors.append(QColor(71, 114, 187));
312 m_themeColors.append(QColor(48, 97, 87));
313 m_themeColors.append(QColor(19, 71, 90));
314 m_themeColors.append(QColor(110, 70, 228));
315
316 setBackground(QColor(QRgb(0xffe4ffff)), QColor(QRgb(0xffe4ffff)), VerticalGradientOrientation);
317 break;
318 case QChart::ChartThemeGrayscale:
319 m_themeColors.append(QColor(0, 0, 0));
320 m_themeColors.append(QColor(50, 50, 50));
321 m_themeColors.append(QColor(100, 100, 100));
322 m_themeColors.append(QColor(140, 140, 140));
323 m_themeColors.append(QColor(180, 180, 180));
324
325 setBackground(QColor(QRgb(0xffffffff)), QColor(QRgb(0xffafafaf)), VerticalGradientOrientation);
326 break;
327 case QChart::ChartThemeUnnamed1:
328 m_themeColors.append(QColor(QRgb(0xff3fa9f5)));
329 m_themeColors.append(QColor(QRgb(0xff7AC943)));
330 m_themeColors.append(QColor(QRgb(0xffFF931E)));
331 m_themeColors.append(QColor(QRgb(0xffFF1D25)));
332 m_themeColors.append(QColor(QRgb(0xffFF7BAC)));
333
334 setBackground(QColor(QRgb(0xfff3dc9e)), QColor(QRgb(0xffafafaf)), VerticalGradientOrientation);
335 break;
336 default:
337 Q_ASSERT(false);
338 break;
339 }
340
341 if(m_backgroundItem){
342 m_backgroundItem->setBrush(m_backgroundGradient);
343 m_backgroundItem->setPen(Qt::NoPen);
344 }
345
346 foreach(QChartSeries* series, m_chartSeries) {
347 // TODO: other series interested on themes?
348 if (series->type() == QChartSeries::SeriesTypeLine) {
349 QXYChartSeries *lineseries = reinterpret_cast<QXYChartSeries *>(series);
350 lineseries->setPen(nextColor());
351 } else if (series->type() == QChartSeries::SeriesTypeScatter) {
352 QScatterSeries *scatter = qobject_cast<QScatterSeries *>(series);
353 scatter->setMarkerColor(nextColor());
354 } else if (series->type() == QChartSeries::SeriesTypePie) {
355 QPieSeries *pieSeries = qobject_cast<QPieSeries *>(series);
356 for (int i(0); i < pieSeries->sliceCount(); i++)
357 pieSeries->setSliceColor(i, nextColor());
358 }
359 }
285 }
360 update();
361 }
362
363 QColor QChart::nextColor()
364 {
365 QColor nextColor = m_themeColors.first();
366 m_themeColors.move(0, m_themeColors.size() - 1);
367 return nextColor;
368 }
286 }
369
287
370 void QChart::zoomInToRect(const QRect& rectangle)
288 void QChart::zoomInToRect(const QRect& rectangle)
@@ -385,8 +303,8 void QChart::zoomInToRect(const QRect& rectangle)
385 m_plotDomainList<<domain;
303 m_plotDomainList<<domain;
386 m_plotDataIndex++;
304 m_plotDataIndex++;
387
305
388 foreach (ChartItem* item ,m_chartItems)
306 foreach (ChartItemControl* ctrl, m_chartItemControls)
389 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
307 ctrl->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
390 update();
308 update();
391 }
309 }
392
310
@@ -394,7 +312,7 void QChart::zoomIn()
394 {
312 {
395 if (m_plotDataIndex < m_plotDomainList.count() - 1) {
313 if (m_plotDataIndex < m_plotDomainList.count() - 1) {
396 m_plotDataIndex++;
314 m_plotDataIndex++;
397 foreach (ChartItem* item ,m_chartItems)
315 foreach (ChartItemControl* item, m_chartItemControls)
398 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
316 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
399 update();
317 update();
400 } else {
318 } else {
@@ -410,7 +328,7 void QChart::zoomOut()
410 {
328 {
411 if (m_plotDataIndex > 0) {
329 if (m_plotDataIndex > 0) {
412 m_plotDataIndex--;
330 m_plotDataIndex--;
413 foreach (ChartItem* item ,m_chartItems)
331 foreach (ChartItemControl* item, m_chartItemControls)
414 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
332 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
415 update();
333 update();
416 }
334 }
@@ -420,7 +338,7 void QChart::zoomReset()
420 {
338 {
421 if (m_plotDataIndex > 0) {
339 if (m_plotDataIndex > 0) {
422 m_plotDataIndex = 0;
340 m_plotDataIndex = 0;
423 foreach (ChartItem* item ,m_chartItems)
341 foreach (ChartItemControl* item, m_chartItemControls)
424 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
342 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
425 update();
343 update();
426 }
344 }
@@ -12,9 +12,10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
12 class AxisItem;
12 class AxisItem;
13 class QChartSeries;
13 class QChartSeries;
14 class PlotDomain;
14 class PlotDomain;
15 class ChartItem;
16 class BarGroup;
15 class BarGroup;
17 class QChartAxis;
16 class QChartAxis;
17 class ChartTheme;
18 class ChartItemControl;
18
19
19 // TODO: We don't need to have QChart tied to QGraphicsItem:
20 // TODO: We don't need to have QChart tied to QGraphicsItem:
20 //class QTCOMMERCIALCHART_EXPORT QChart
21 //class QTCOMMERCIALCHART_EXPORT QChart
@@ -33,8 +34,9 public:
33 VerticalGradientOrientation
34 VerticalGradientOrientation
34 };
35 };
35 enum ChartThemeId {
36 enum ChartThemeId {
37 ChartThemeInvalid = -1,
36 /*! The default theme follows the GUI style of the Operating System */
38 /*! The default theme follows the GUI style of the Operating System */
37 ChartThemeDefault = 0,
39 ChartThemeDefault,
38 ChartThemeVanilla,
40 ChartThemeVanilla,
39 ChartThemeIcy,
41 ChartThemeIcy,
40 ChartThemeGrayscale,
42 ChartThemeGrayscale,
@@ -75,14 +77,7 public:
75 private:
77 private:
76 void setAxis(AxisItem *item, const QChartAxis& axis);
78 void setAxis(AxisItem *item, const QChartAxis& axis);
77
79
78 signals:
79 //TODO chage to const QSize& size
80 void sizeChanged(QRectF rect);
81 void scaleChanged(qreal xscale, qreal yscale);
82
83 private:
80 private:
84 QColor nextColor();
85
86 Q_DISABLE_COPY(QChart)
81 Q_DISABLE_COPY(QChart)
87 QGraphicsRectItem* m_backgroundItem;
82 QGraphicsRectItem* m_backgroundItem;
88 QLinearGradient m_backgroundGradient;
83 QLinearGradient m_backgroundGradient;
@@ -91,12 +86,12 private:
91 AxisItem* m_axisXItem;
86 AxisItem* m_axisXItem;
92 QList<AxisItem*> m_axisYItem;
87 QList<AxisItem*> m_axisYItem;
93 QRect m_rect;
88 QRect m_rect;
94 QList<QChartSeries*> m_chartSeries;
89 QList<QChartSeries *> m_chartSeries;
90 QList<ChartItemControl *> m_chartItemControls;
95 QVector<PlotDomain> m_plotDomainList;
91 QVector<PlotDomain> m_plotDomainList;
96 QList<ChartItem*> m_chartItems;
97 int m_plotDataIndex;
92 int m_plotDataIndex;
98 int m_marginSize;
93 int m_marginSize;
99 QList<QColor> m_themeColors;
94 ChartTheme *m_chartTheme;
100 };
95 };
101
96
102 QTCOMMERCIALCHART_END_NAMESPACE
97 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,78 +1,80
1 #include "qpieseries_p.h"
1 #include "qpieseries.h"
2 #include "qpieseries.h"
2 #include "pieslice.h"
3 #include <QGraphicsObject>
3 #include <QGraphicsObject>
4 #include "pieslice.h"
4 #include <QDebug>
5 #include <QDebug>
5
6
6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7
8
8 QPieSeries::QPieSeries(QGraphicsObject *parent) :
9 QPieSeriesPrivate::QPieSeriesPrivate() :
9 QChartSeries(parent),
10 m_sizeFactor(1.0),
10 m_sizeFactor(1.0),
11 m_position(PiePositionMaximized)
11 m_position(QPieSeries::PiePositionMaximized)
12 {
12 {
13 }
13 }
14
14
15 QPieSeries::~QPieSeries()
15 QPieSeriesPrivate::~QPieSeriesPrivate()
16 {
16 {
17 while (m_slices.count())
17 while (m_slices.count())
18 delete m_slices.takeLast();
18 delete m_slices.takeLast();
19 }
19 }
20
20
21 bool QPieSeries::setData(QList<qreal> data)
21 bool QPieSeriesPrivate::setData(QList<qreal> data)
22 {
22 {
23 m_data = data;
23 m_data = data;
24
24
25 // Create slices
25 if (m_parentItem) {
26 qreal fullPie = 360;
26 // Create slices
27 qreal total = 0;
27 qreal fullPie = 360;
28 foreach (qreal value, m_data)
28 qreal total = 0;
29 total += value;
29 foreach (qreal value, m_data)
30 total += value;
31
32 m_chartSize = m_parentItem->boundingRect();
33 qreal angle = 0;
34 // TODO: no need to create new slices in case size changed; we should re-use the existing ones
35 foreach (qreal value, m_data) {
36 qreal span = value / total * fullPie;
37 PieSlice *slice = new PieSlice(QColor(), angle, span, m_parentItem->boundingRect());
38 slice->setParentItem(m_parentItem);
39 m_slices.append(slice);
40 angle += span;
41 }
30
42
31 QGraphicsItem *parentItem = qobject_cast<QGraphicsItem *>(parent());
43 setTheme(m_chartTheme);
32 Q_ASSERT(parentItem);
44 resizeSlices(m_chartSize);
33 m_chartSize = parentItem->boundingRect();
34 qreal angle = 0;
35 // TODO: no need to create new slices in case size changed; we should re-use the existing ones
36 foreach (qreal value, m_data) {
37 qreal span = value / total * fullPie;
38 PieSlice *slice = new PieSlice(QColor(), angle, span, parentItem->boundingRect());
39 slice->setParentItem(parentItem);
40 m_slices.append(slice);
41 angle += span;
42 }
45 }
43
46
44 resizeSlices(m_chartSize);
45 return true;
47 return true;
46 }
48 }
47
49
48 void QPieSeries::setSliceColor(int index, QColor color)
50 void QPieSeriesPrivate::setPos(const QPointF & pos)
49 {
51 {
50 if (index >= 0 && index < m_slices.count())
52 // TODO
51 m_slices.at(index)->m_color = color;
52 }
53 }
53
54
54 QColor QPieSeries::sliceColor(int index)
55 void QPieSeriesPrivate::resize(const QSize &size)
55 {
56 {
56 if (index >= 0 && index < m_slices.count())
57 // TODO: allow user setting the size?
57 return m_slices.at(index)->m_color;
58 // TODO: allow user defining the margins?
58 else
59 m_chartSize = QRect(0, 0, size.width(), size.height());
59 return QColor();
60 resizeSlices(m_chartSize);
60 }
61 }
61
62
62 int QPieSeries::sliceCount()
63 void QPieSeriesPrivate::setTheme(ChartTheme *theme)
63 {
64 {
64 return m_slices.count();
65 if (theme) {
66 m_chartTheme = theme;
67 for (int i(0); i < m_slices.count(); i++)
68 m_slices.at(i)->m_theme = theme->themeForSeries();
69 }
65 }
70 }
66
71
67 void QPieSeries::chartSizeChanged(QRectF chartRect)
72 void QPieSeriesPrivate::setPlotDomain(const PlotDomain& plotDomain)
68 {
73 {
69 // TODO: allow user setting the size?
74 // TODO
70 // TODO: allow user defining the margins?
71 m_chartSize = chartRect;
72 resizeSlices(m_chartSize);
73 }
75 }
74
76
75 void QPieSeries::resizeSlices(QRectF rect)
77 void QPieSeriesPrivate::resizeSlices(QRectF rect)
76 {
78 {
77 QRectF tempRect = rect;
79 QRectF tempRect = rect;
78 if (tempRect.width() < tempRect.height()) {
80 if (tempRect.width() < tempRect.height()) {
@@ -86,25 +88,25 void QPieSeries::resizeSlices(QRectF rect)
86 }
88 }
87
89
88 switch (m_position) {
90 switch (m_position) {
89 case PiePositionTopLeft: {
91 case QPieSeries::PiePositionTopLeft: {
90 tempRect.setHeight(tempRect.height() / 2);
92 tempRect.setHeight(tempRect.height() / 2);
91 tempRect.setWidth(tempRect.height());
93 tempRect.setWidth(tempRect.height());
92 tempRect.moveCenter(QPointF(rect.center().x() / 2, rect.center().y() / 2));
94 tempRect.moveCenter(QPointF(rect.center().x() / 2, rect.center().y() / 2));
93 break;
95 break;
94 }
96 }
95 case PiePositionTopRight: {
97 case QPieSeries::PiePositionTopRight: {
96 tempRect.setHeight(tempRect.height() / 2);
98 tempRect.setHeight(tempRect.height() / 2);
97 tempRect.setWidth(tempRect.height());
99 tempRect.setWidth(tempRect.height());
98 tempRect.moveCenter(QPointF((rect.center().x() / 2) * 3, rect.center().y() / 2));
100 tempRect.moveCenter(QPointF((rect.center().x() / 2) * 3, rect.center().y() / 2));
99 break;
101 break;
100 }
102 }
101 case PiePositionBottomLeft: {
103 case QPieSeries::PiePositionBottomLeft: {
102 tempRect.setHeight(tempRect.height() / 2);
104 tempRect.setHeight(tempRect.height() / 2);
103 tempRect.setWidth(tempRect.height());
105 tempRect.setWidth(tempRect.height());
104 tempRect.moveCenter(QPointF(rect.center().x() / 2, (rect.center().y() / 2) * 3));
106 tempRect.moveCenter(QPointF(rect.center().x() / 2, (rect.center().y() / 2) * 3));
105 break;
107 break;
106 }
108 }
107 case PiePositionBottomRight: {
109 case QPieSeries::PiePositionBottomRight: {
108 tempRect.setHeight(tempRect.height() / 2);
110 tempRect.setHeight(tempRect.height() / 2);
109 tempRect.setWidth(tempRect.height());
111 tempRect.setWidth(tempRect.height());
110 tempRect.moveCenter(QPointF((rect.center().x() / 2) * 3, (rect.center().y() / 2) * 3));
112 tempRect.moveCenter(QPointF((rect.center().x() / 2) * 3, (rect.center().y() / 2) * 3));
@@ -118,23 +120,65 void QPieSeries::resizeSlices(QRectF rect)
118 slice->m_rect = tempRect;
120 slice->m_rect = tempRect;
119 }
121 }
120
122
123 QPieSeries::QPieSeries(QGraphicsObject *parent) :
124 QChartSeries(parent),
125 d(new QPieSeriesPrivate())
126 {
127 QGraphicsItem *parentItem = qobject_cast<QGraphicsItem *>(parent);
128 if (parentItem)
129 d->m_parentItem = parentItem;
130 }
131
132 QPieSeries::~QPieSeries()
133 {
134 delete d;
135 }
136
137 bool QPieSeries::setData(QList<qreal> data)
138 {
139 return d->setData(data);
140 }
141
142 void QPieSeries::setSliceColor(int index, QColor color)
143 {
144 if (index >= 0 && index < d->m_slices.count())
145 d->m_slices.at(index)->m_color = color;
146 }
147
148 QColor QPieSeries::sliceColor(int index)
149 {
150 if (index >= 0 && index < d->m_slices.count())
151 return d->m_slices.at(index)->m_color;
152 else
153 return QColor();
154 }
155
156 int QPieSeries::sliceCount()
157 {
158 return d->m_slices.count();
159 }
160
121 void QPieSeries::setSizeFactor(qreal factor)
161 void QPieSeries::setSizeFactor(qreal factor)
122 {
162 {
123 if (factor > 0.0)
163 if (factor > 0.0)
124 m_sizeFactor = factor;
164 d->m_sizeFactor = factor;
125 resizeSlices(m_chartSize);
165 d->resizeSlices(d->m_chartSize);
126
166
127 // Initiate update via the parent graphics item
167 // Initiate update via the parent graphics item
128 // TODO: potential issue: what if this function is called from the parent context?
168 // TODO: potential issue: what if this function is called from the parent context?
129 QGraphicsItem *parentItem = qobject_cast<QGraphicsItem *>(parent());
169 if (d->m_parentItem)
130 Q_ASSERT(parentItem);
170 d->m_parentItem->update();
131 parentItem->update();
171 }
172
173 qreal QPieSeries::sizeFactor()
174 {
175 return d->m_sizeFactor;
132 }
176 }
133
177
134 void QPieSeries::setPosition(PiePosition position)
178 void QPieSeries::setPosition(PiePosition position)
135 {
179 {
136 m_position = position;
180 d->m_position = position;
137 resizeSlices(m_chartSize);
181 d->resizeSlices(d->m_chartSize);
138
182
139 // Initiate update via the parent graphics item
183 // Initiate update via the parent graphics item
140 // TODO: potential issue: what if this function is called from the parent context?
184 // TODO: potential issue: what if this function is called from the parent context?
@@ -8,6 +8,7
8
8
9 class QGraphicsObject;
9 class QGraphicsObject;
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11 class QPieSeriesPrivate;
11 class PieSlice;
12 class PieSlice;
12
13
13 class QTCOMMERCIALCHART_EXPORT QPieSeries : public QChartSeries
14 class QTCOMMERCIALCHART_EXPORT QPieSeries : public QChartSeries
@@ -37,23 +38,14 public:
37 QColor sliceColor(int index);
38 QColor sliceColor(int index);
38 int sliceCount();
39 int sliceCount();
39 void setSizeFactor(qreal sizeFactor);
40 void setSizeFactor(qreal sizeFactor);
40 qreal sizeFactor() { return m_sizeFactor; }
41 qreal sizeFactor();
41 void setPosition(PiePosition position);
42 void setPosition(PiePosition position);
42
43
43 public Q_SLOTS:
44 void chartSizeChanged(QRectF rect);
45
46 private:
44 private:
47 void resizeSlices(QRectF rect);
45 Q_DECLARE_PRIVATE(QPieSeries)
48 //Q_DECLARE_PRIVATE(QPieSeries)
49 Q_DISABLE_COPY(QPieSeries)
46 Q_DISABLE_COPY(QPieSeries)
50 friend class QChart;
47 friend class QChart;
51 // TODO: move the followin to internal impl
48 QPieSeriesPrivate *const d;
52 QList<qreal> m_data;
53 QList<PieSlice*> m_slices;
54 QRectF m_chartSize;
55 qreal m_sizeFactor;
56 PiePosition m_position;
57 };
49 };
58
50
59 QTCOMMERCIALCHART_END_NAMESPACE
51 QTCOMMERCIALCHART_END_NAMESPACE
@@ -39,14 +39,19 QRectF QScatterSeriesPrivate::boundingRect() const
39
39
40 void QScatterSeriesPrivate::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
40 void QScatterSeriesPrivate::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
41 {
41 {
42 QPen pen = painter->pen();
42 // TODO: The opacity should be user definable?
43 QBrush brush = pen.brush();
44 // TODO: The opacity should be user definable...
45 //brush.setColor(QColor(255, 82, 0, 100));
43 //brush.setColor(QColor(255, 82, 0, 100));
46 brush.setColor(m_markerColor);
44 if (m_markerColor.isValid()) {
47 pen.setBrush(brush);
45 QPen pen = painter->pen();
48 pen.setWidth(4);
46 QBrush brush = pen.brush();
49 painter->setPen(pen);
47 brush.setColor(m_markerColor);
48 pen.setBrush(brush);
49 pen.setWidth(4);
50 painter->setPen(pen);
51 }
52 else
53 painter->setPen(m_theme.markerPen);
54 // brush.setColor(m_theme..lineColor);
50
55
51 // TODO: m_scenex and m_sceny are left empty during construction -> we would need a resize
56 // TODO: m_scenex and m_sceny are left empty during construction -> we would need a resize
52 // event right after construction or maybe given a size during initialization
57 // event right after construction or maybe given a size during initialization
@@ -57,6 +62,26 void QScatterSeriesPrivate::paint(QPainter *painter, const QStyleOptionGraphicsI
57 }
62 }
58 }
63 }
59
64
65 void QScatterSeriesPrivate::setPos(const QPointF & pos)
66 {
67 // TODO
68 }
69
70 void QScatterSeriesPrivate::resize(const QSize &size)
71 {
72 resize(QRect(0, 0, size.width(), size.height()));
73 }
74
75 void QScatterSeriesPrivate::setTheme(ChartTheme *theme)
76 {
77 m_theme = theme->themeForSeries();
78 }
79
80 void QScatterSeriesPrivate::setPlotDomain(const PlotDomain& plotDomain)
81 {
82 // TODO
83 }
84
60 QScatterSeries::QScatterSeries(QObject *parent) :
85 QScatterSeries::QScatterSeries(QObject *parent) :
61 QChartSeries(parent),
86 QChartSeries(parent),
62 d(new QScatterSeriesPrivate(qobject_cast<QGraphicsItem *> (parent)))
87 d(new QScatterSeriesPrivate(qobject_cast<QGraphicsItem *> (parent)))
@@ -74,14 +99,6 bool QScatterSeries::setData(QList<qreal> x, QList<qreal> y)
74 return true;
99 return true;
75 }
100 }
76
101
77 void QScatterSeries::chartSizeChanged(QRectF rect)
78 {
79 // Recalculate scatter data point locations on the scene
80 // d->transform().reset();
81 // d->transform().translate();
82 d->resize(rect);
83 }
84
85 void QScatterSeries::setMarkerColor(QColor color)
102 void QScatterSeries::setMarkerColor(QColor color)
86 {
103 {
87 d->m_markerColor = color;
104 d->m_markerColor = color;
@@ -21,7 +21,6 public: // from QChartSeries
21 bool setData(QList<qreal> x, QList<qreal> y);
21 bool setData(QList<qreal> x, QList<qreal> y);
22
22
23 public Q_SLOTS:
23 public Q_SLOTS:
24 void chartSizeChanged(QRectF rect);
25 // TODO: also affects opacity of the marker...? To be documented
24 // TODO: also affects opacity of the marker...? To be documented
26 void setMarkerColor(QColor color);
25 void setMarkerColor(QColor color);
27 QColor markerColor();
26 QColor markerColor();
@@ -2,6 +2,8
2 #define QSCATTERSERIESPRIVATE_H
2 #define QSCATTERSERIESPRIVATE_H
3
3
4 #include "qchartseries.h"
4 #include "qchartseries.h"
5 #include "charttheme_p.h"
6 #include "chartitemcontrol.h"
5 #include <QGraphicsItem>
7 #include <QGraphicsItem>
6
8
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
@@ -9,16 +11,22 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 /*!
11 /*!
10 * The PIMPL class of QScatterSeries.
12 * The PIMPL class of QScatterSeries.
11 */
13 */
12 class QScatterSeriesPrivate : public QGraphicsItem
14 class QScatterSeriesPrivate : public QGraphicsItem, public ChartItemControl
13 {
15 {
14 public:
16 public:
15 QScatterSeriesPrivate(QGraphicsItem *parent);
17 QScatterSeriesPrivate(QGraphicsItem *parent);
16
18
17 public: // from QGraphicsItem
19 public: // from QGraphicsItem
20 void setPos(const QPointF & pos);
18 void resize(QRectF rect);
21 void resize(QRectF rect);
19 QRectF boundingRect() const;
22 QRectF boundingRect() const;
20 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
23 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
21
24
25 public: // from ChartItemControl
26 void resize(const QSize &size);
27 void setTheme(ChartTheme *theme);
28 void setPlotDomain(const PlotDomain& data);
29
22 public:
30 public:
23 // TODO: use the chart data class instead of list of x and y values?
31 // TODO: use the chart data class instead of list of x and y values?
24 QList<qreal> m_x;
32 QList<qreal> m_x;
@@ -28,8 +36,9 public:
28 QList<qreal> m_scenex;
36 QList<qreal> m_scenex;
29 QList<qreal> m_sceney;
37 QList<qreal> m_sceney;
30 QColor m_markerColor;
38 QColor m_markerColor;
39 SeriesTheme m_theme;
31 };
40 };
32
41
33 QTCOMMERCIALCHART_END_NAMESPACE
42 QTCOMMERCIALCHART_END_NAMESPACE
34
43
35 #endif // QSCATTERSERIES_H
44 #endif // QSCATTERSERIESPRIVATE_H
@@ -30,16 +30,18 SOURCES += \
30 pieslice.cpp \
30 pieslice.cpp \
31 qchartview.cpp \
31 qchartview.cpp \
32 qchartseries.cpp \
32 qchartseries.cpp \
33 qchartaxis.cpp
33 qchartaxis.cpp \
34 charttheme.cpp
34
35
35 PRIVATE_HEADERS += \
36 PRIVATE_HEADERS += \
36 xylinechart/xylinechartitem_p.h \
37 xylinechart/xylinechartitem_p.h \
37 plotdomain_p.h \
38 plotdomain_p.h \
38 qscatterseries_p.h \
39 qscatterseries_p.h \
40 qpieseries_p.h \
39 pieslice.h \
41 pieslice.h \
40 axisitem_p.h \
42 axisitem_p.h \
41 chartitem_p.h
43 charttheme_p.h
42
44
43 PUBLIC_HEADERS += \
45 PUBLIC_HEADERS += \
44 qchartseries.h \
46 qchartseries.h \
45 qscatterseries.h \
47 qscatterseries.h \
@@ -57,7 +59,8 PUBLIC_HEADERS += \
57 qchartview.h \
59 qchartview.h \
58 qchartaxis.h
60 qchartaxis.h
59
61
60 HEADERS += $$PUBLIC_HEADERS
62 HEADERS += $$PUBLIC_HEADERS \
63 chartitemcontrol.h
61 HEADERS += $$PRIVATE_HEADERS
64 HEADERS += $$PRIVATE_HEADERS
62
65
63 INCLUDEPATH += xylinechart \
66 INCLUDEPATH += xylinechart \
@@ -97,3 +100,11 unix:QMAKE_DISTCLEAN += -r $$CHART_BUILD_HEADER_DIR $$CHART_BUILD_LIB_DIR
97 win32:QMAKE_DISTCLEAN += /Q $$CHART_BUILD_HEADER_DIR $$CHART_BUILD_LIB_DIR
100 win32:QMAKE_DISTCLEAN += /Q $$CHART_BUILD_HEADER_DIR $$CHART_BUILD_LIB_DIR
98
101
99
102
103
104
105
106
107
108
109
110
@@ -7,19 +7,33
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 XYLineChartItem::XYLineChartItem(QXYChartSeries* series,QGraphicsItem *parent):ChartItem(parent),
10 XYLineChartItem::XYLineChartItem(QXYChartSeries* series,QGraphicsItem *parent) :
11 m_series(series),
11 QGraphicsItem(parent),
12 m_pathItem(new QGraphicsPathItem(this))
12 m_series(series),
13 m_pathItem(new QGraphicsPathItem(this))
13 {
14 {
14 setFlags(QGraphicsItem::ItemClipsChildrenToShape);
15 setFlags(QGraphicsItem::ItemClipsChildrenToShape);
15 }
16 }
16
17
17 void XYLineChartItem::setSize(const QSize& size)
18 void XYLineChartItem::setPos(const QPointF & pos)
18 {
19 {
19 m_rect=QRect(0,0,size.width(),size.height());
20 QGraphicsItem::setPos(pos);
21 }
22
23 void XYLineChartItem::resize(const QSize &size)
24 {
25 m_rect = QRect(0, 0, size.width(), size.height());
20 prepareGeometryChange();
26 prepareGeometryChange();
21 updateGeometry();
27 updateGeometry();
28 }
22
29
30 void XYLineChartItem::setTheme(ChartTheme *theme)
31 {
32 if (theme) {
33 m_theme = theme->themeForSeries();
34 prepareGeometryChange();
35 updateGeometry();
36 }
23 }
37 }
24
38
25 void XYLineChartItem::setPlotDomain(const PlotDomain& data)
39 void XYLineChartItem::setPlotDomain(const PlotDomain& data)
@@ -59,7 +73,7 void XYLineChartItem::updateGeometry()
59 }
73 }
60
74
61 m_pathItem->setPath(path);
75 m_pathItem->setPath(path);
62 m_pathItem->setPen(m_series->pen());
76 m_pathItem->setPen(m_theme.linePen);
63 m_pathItem->setBrush(Qt::NoBrush);
77 m_pathItem->setBrush(Qt::NoBrush);
64 }
78 }
65
79
@@ -2,13 +2,15
2 #define XYLINECHARTITEM_H
2 #define XYLINECHARTITEM_H
3
3
4 #include "qchartglobal.h"
4 #include "qchartglobal.h"
5 #include "chartitem_p.h"
5 #include "chartitemcontrol.h"
6 #include "charttheme_p.h"
7 #include <QGraphicsItem>
6
8
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8
10
9 class QXYChartSeries;
11 class QXYChartSeries;
10
12
11 class XYLineChartItem : public ChartItem
13 class XYLineChartItem : public QGraphicsItem, public ChartItemControl
12 {
14 {
13
15
14 public:
16 public:
@@ -19,8 +21,11 public:
19 QRectF boundingRect() const;
21 QRectF boundingRect() const;
20 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){};
22 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){};
21 // virtual QPainterPath shape() const;
23 // virtual QPainterPath shape() const;
22 //from ChartItem
24
23 void setSize(const QSize& size);
25 public: // from ChartItemControl
26 void setPos(const QPointF & pos);
27 void resize(const QSize &size);
28 void setTheme(ChartTheme *theme);
24 void setPlotDomain(const PlotDomain& data);
29 void setPlotDomain(const PlotDomain& data);
25
30
26 private:
31 private:
@@ -32,6 +37,7 private:
32 QXYChartSeries* m_series;
37 QXYChartSeries* m_series;
33 PlotDomain m_plotDomain;
38 PlotDomain m_plotDomain;
34 QGraphicsPathItem *m_pathItem;
39 QGraphicsPathItem *m_pathItem;
40 SeriesTheme m_theme;
35 };
41 };
36
42
37 QTCOMMERCIALCHART_END_NAMESPACE
43 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,2 +1,5
1 TEMPLATE = subdirs
1 TEMPLATE = subdirs
2 SUBDIRS += chartwidgettest
2 SUBDIRS += chartwidgettest
3
4
5
1 NO CONTENT: file was removed
NO CONTENT: file was removed
General Comments 0
You need to be logged in to leave comments. Login now