@@ -1,96 +1,99 | |||||
1 | #include "bar_p.h" |
|
1 | #include "bar_p.h" | |
2 | #include <QDebug> |
|
2 | #include <QDebug> | |
3 | #include <QPainter> |
|
3 | #include <QPainter> | |
4 | #include <QGraphicsSceneEvent> |
|
4 | #include <QGraphicsSceneEvent> | |
5 |
|
5 | |||
6 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
6 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
7 |
|
7 | |||
8 | Bar::Bar(QString category, QGraphicsItem *parent) |
|
8 | Bar::Bar(QString category, QGraphicsItem *parent) | |
9 | : QGraphicsObject(parent), |
|
9 | : QGraphicsObject(parent), | |
10 | mXpos(0), |
|
10 | mXpos(0), | |
11 | mYpos(0), |
|
11 | mYpos(0), | |
12 | mWidth(0), |
|
12 | mWidth(0), | |
13 | mHeight(0), |
|
13 | mHeight(0), | |
|
14 | mBrush(QBrush()), | |||
|
15 | mPen(QPen()), | |||
14 | mCategory(category) |
|
16 | mCategory(category) | |
15 | { |
|
17 | { | |
16 | setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton); |
|
18 | setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton); | |
17 | setAcceptHoverEvents(true); |
|
19 | setAcceptHoverEvents(true); | |
18 | } |
|
20 | } | |
19 |
|
21 | |||
20 | void Bar::setSize(const QSizeF& size) |
|
22 | void Bar::setSize(const QSizeF& size) | |
21 | { |
|
23 | { | |
22 | mWidth = size.width(); |
|
24 | mWidth = size.width(); | |
23 | mHeight = size.height(); |
|
25 | mHeight = size.height(); | |
24 | } |
|
26 | } | |
25 |
|
27 | |||
26 |
|
28 | |||
27 | void Bar::resize( qreal w, qreal h ) |
|
29 | void Bar::resize( qreal w, qreal h ) | |
28 | { |
|
30 | { | |
29 | mWidth = w; |
|
31 | mWidth = w; | |
30 | mHeight = h; |
|
32 | mHeight = h; | |
31 | } |
|
33 | } | |
32 |
|
34 | |||
33 | void Bar::setPos(qreal x, qreal y) |
|
35 | void Bar::setPos(qreal x, qreal y) | |
34 | { |
|
36 | { | |
35 | mXpos = x; |
|
37 | mXpos = x; | |
36 | mYpos = y; |
|
38 | mYpos = y; | |
37 | } |
|
39 | } | |
38 |
|
40 | |||
39 | void Bar::setPen(QPen pen) |
|
41 | void Bar::setPen(QPen pen) | |
40 | { |
|
42 | { | |
41 | mPen = pen; |
|
43 | mPen = pen; | |
42 | } |
|
44 | } | |
43 |
|
45 | |||
44 | void Bar::setBrush(QBrush brush) |
|
46 | void Bar::setBrush(QBrush brush) | |
45 | { |
|
47 | { | |
46 | mBrush = brush; |
|
48 | mBrush = brush; | |
47 | } |
|
49 | } | |
48 |
|
50 | |||
49 | void Bar::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
51 | void Bar::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | |
50 | { |
|
52 | { | |
51 | Q_UNUSED(option) |
|
53 | Q_UNUSED(option) | |
52 | Q_UNUSED(widget) |
|
54 | Q_UNUSED(widget) | |
53 |
|
55 | |||
54 | if (0 == mHeight) { |
|
56 | if (0 == mHeight) { | |
55 | return; |
|
57 | return; | |
56 | } |
|
58 | } | |
|
59 | painter->setPen(mPen); | |||
57 | painter->setBrush(mBrush); |
|
60 | painter->setBrush(mBrush); | |
58 |
|
61 | |||
59 | // This compensates for rounding errors. drawRect takes ints and cumulative error of pos + size may be over 1. |
|
62 | // This compensates for rounding errors. drawRect takes ints and cumulative error of pos + size may be over 1. | |
60 | int x0 = mXpos; |
|
63 | int x0 = mXpos; | |
61 | int x1 = (mXpos + mWidth); |
|
64 | int x1 = (mXpos + mWidth); | |
62 | int w = x1-x0; |
|
65 | int w = x1-x0; | |
63 | int y0 = mYpos; |
|
66 | int y0 = mYpos; | |
64 | int y1 = (mYpos + mHeight); |
|
67 | int y1 = (mYpos + mHeight); | |
65 | int h = y1-y0; |
|
68 | int h = y1-y0; | |
66 | painter->drawRect(x0, y0 ,w ,h); |
|
69 | painter->drawRect(x0, y0 ,w ,h); | |
67 | } |
|
70 | } | |
68 |
|
71 | |||
69 | QRectF Bar::boundingRect() const |
|
72 | QRectF Bar::boundingRect() const | |
70 | { |
|
73 | { | |
71 | QRectF r(mXpos, mYpos, mWidth, mHeight); |
|
74 | QRectF r(mXpos, mYpos, mWidth, mHeight); | |
72 | return r; |
|
75 | return r; | |
73 | } |
|
76 | } | |
74 |
|
77 | |||
75 | void Bar::mousePressEvent(QGraphicsSceneMouseEvent* event) |
|
78 | void Bar::mousePressEvent(QGraphicsSceneMouseEvent* event) | |
76 | { |
|
79 | { | |
77 | if (event->button() == Qt::LeftButton) { |
|
80 | if (event->button() == Qt::LeftButton) { | |
78 | emit clicked(mCategory); |
|
81 | emit clicked(mCategory); | |
79 | } else if (event->button() == Qt::RightButton) { |
|
82 | } else if (event->button() == Qt::RightButton) { | |
80 | emit rightClicked(mCategory); |
|
83 | emit rightClicked(mCategory); | |
81 | } |
|
84 | } | |
82 | } |
|
85 | } | |
83 |
|
86 | |||
84 | void Bar::hoverEnterEvent(QGraphicsSceneHoverEvent* event) |
|
87 | void Bar::hoverEnterEvent(QGraphicsSceneHoverEvent* event) | |
85 | { |
|
88 | { | |
86 | emit hoverEntered(event->lastScreenPos()); |
|
89 | emit hoverEntered(event->lastScreenPos()); | |
87 | } |
|
90 | } | |
88 |
|
91 | |||
89 | void Bar::hoverLeaveEvent(QGraphicsSceneHoverEvent* /*event*/) |
|
92 | void Bar::hoverLeaveEvent(QGraphicsSceneHoverEvent* /*event*/) | |
90 | { |
|
93 | { | |
91 | emit hoverLeaved(); |
|
94 | emit hoverLeaved(); | |
92 | } |
|
95 | } | |
93 |
|
96 | |||
94 | #include "moc_bar_p.cpp" |
|
97 | #include "moc_bar_p.cpp" | |
95 |
|
98 | |||
96 | QTCOMMERCIALCHART_END_NAMESPACE |
|
99 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,103 +1,104 | |||||
1 | #include "percentbarpresenter_p.h" |
|
1 | #include "percentbarpresenter_p.h" | |
2 | #include "bar_p.h" |
|
2 | #include "bar_p.h" | |
3 | #include "barvalue_p.h" |
|
3 | #include "barvalue_p.h" | |
4 | #include "separator_p.h" |
|
4 | #include "separator_p.h" | |
5 | #include "qbarset.h" |
|
5 | #include "qbarset.h" | |
6 | #include <QDebug> |
|
6 | #include <QDebug> | |
7 |
|
7 | |||
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
9 |
|
9 | |||
10 |
|
10 | |||
11 | PercentBarPresenter::PercentBarPresenter(QBarSeries *series, QChart *parent) : |
|
11 | PercentBarPresenter::PercentBarPresenter(QBarSeries *series, QChart *parent) : | |
12 | BarPresenterBase(series, parent) |
|
12 | BarPresenterBase(series, parent) | |
13 | { |
|
13 | { | |
14 | } |
|
14 | } | |
15 |
|
15 | |||
16 | void PercentBarPresenter::layoutChanged() |
|
16 | void PercentBarPresenter::layoutChanged() | |
17 | { |
|
17 | { | |
18 | // Scale bars to new layout |
|
18 | // Scale bars to new layout | |
19 | // Layout for bars: |
|
19 | // Layout for bars: | |
20 | if (mSeries->barsetCount() <= 0) { |
|
20 | if (mSeries->barsetCount() <= 0) { | |
21 | qDebug() << "No sets in model!"; |
|
21 | qDebug() << "No sets in model!"; | |
22 | // Nothing to do. |
|
22 | // Nothing to do. | |
23 | return; |
|
23 | return; | |
24 | } |
|
24 | } | |
25 |
|
25 | |||
26 | if (childItems().count() == 0) { |
|
26 | if (childItems().count() == 0) { | |
27 | qDebug() << "WARNING: PercentBarPresenter::layoutChanged called before graphics items are created!"; |
|
27 | qDebug() << "WARNING: PercentBarPresenter::layoutChanged called before graphics items are created!"; | |
28 | return; |
|
28 | return; | |
29 | } |
|
29 | } | |
30 |
|
30 | |||
31 | // Use temporary qreals for accurancy (we might get some compiler warnings... :) |
|
31 | // Use temporary qreals for accurancy (we might get some compiler warnings... :) | |
32 | qreal tW = mWidth; |
|
32 | qreal tW = mWidth; | |
33 | qreal tC = mSeries->categoryCount() + 1; |
|
33 | qreal tC = mSeries->categoryCount() + 1; | |
34 | qreal cC = mSeries->categoryCount() * 2 + 1; |
|
34 | qreal cC = mSeries->categoryCount() * 2 + 1; | |
35 | mBarWidth = tW / cC; |
|
35 | mBarWidth = tW / cC; | |
36 | qreal xStep = (tW/tC); |
|
36 | qreal xStep = (tW/tC); | |
37 | qreal xPos = ((tW/tC) - mBarWidth / 2); |
|
37 | qreal xPos = ((tW/tC) - mBarWidth / 2); | |
38 | qreal h = mHeight; |
|
38 | qreal h = mHeight; | |
39 |
|
39 | |||
40 | int itemIndex(0); |
|
40 | int itemIndex(0); | |
41 | for (int category = 0; category < mSeries->categoryCount(); category++) { |
|
41 | for (int category = 0; category < mSeries->categoryCount(); category++) { | |
42 | qreal colSum = mSeries->categorySum(category); |
|
42 | qreal colSum = mSeries->categorySum(category); | |
43 | qreal scale = (h / colSum); |
|
43 | qreal scale = (h / colSum); | |
44 | qreal yPos = h; |
|
44 | qreal yPos = h; | |
45 | for (int set=0; set < mSeries->barsetCount(); set++) { |
|
45 | for (int set=0; set < mSeries->barsetCount(); set++) { | |
46 | qreal barHeight = mSeries->valueAt(set, category) * scale; |
|
46 | qreal barHeight = mSeries->valueAt(set, category) * scale; | |
47 | Bar* bar = mBars.at(itemIndex); |
|
47 | Bar* bar = mBars.at(itemIndex); | |
48 |
|
48 | |||
49 | // TODO: width settable per bar? |
|
49 | // TODO: width settable per bar? | |
50 | bar->resize(mBarWidth, barHeight); |
|
50 | bar->resize(mBarWidth, barHeight); | |
|
51 | bar->setPen(mSeries->barsetAt(set)->pen()); | |||
51 | bar->setBrush(mSeries->barsetAt(set)->brush()); |
|
52 | bar->setBrush(mSeries->barsetAt(set)->brush()); | |
52 | bar->setPos(xPos, yPos-barHeight); |
|
53 | bar->setPos(xPos, yPos-barHeight); | |
53 | itemIndex++; |
|
54 | itemIndex++; | |
54 | yPos -= barHeight; |
|
55 | yPos -= barHeight; | |
55 | } |
|
56 | } | |
56 | xPos += xStep; |
|
57 | xPos += xStep; | |
57 | } |
|
58 | } | |
58 | /* |
|
59 | /* | |
59 | // Position separators |
|
60 | // Position separators | |
60 | xPos = xStep + xStep/2; |
|
61 | xPos = xStep + xStep/2; | |
61 | for (int s=0; s < mSeries->categoryCount() - 1; s++) { |
|
62 | for (int s=0; s < mSeries->categoryCount() - 1; s++) { | |
62 | Separator* sep = mSeparators.at(s); |
|
63 | Separator* sep = mSeparators.at(s); | |
63 | sep->setPos(xPos,0); |
|
64 | sep->setPos(xPos,0); | |
64 | sep->setSize(QSizeF(1,mHeight)); |
|
65 | sep->setSize(QSizeF(1,mHeight)); | |
65 | xPos += xStep; |
|
66 | xPos += xStep; | |
66 | } |
|
67 | } | |
67 | */ |
|
68 | */ | |
68 | // Position floating values |
|
69 | // Position floating values | |
69 | itemIndex = 0; |
|
70 | itemIndex = 0; | |
70 | xPos = (tW/tC); |
|
71 | xPos = (tW/tC); | |
71 | for (int category=0; category < mSeries->categoryCount(); category++) { |
|
72 | for (int category=0; category < mSeries->categoryCount(); category++) { | |
72 | qreal yPos = h; |
|
73 | qreal yPos = h; | |
73 | qreal colSum = mSeries->categorySum(category); |
|
74 | qreal colSum = mSeries->categorySum(category); | |
74 | qreal scale = (h / colSum); |
|
75 | qreal scale = (h / colSum); | |
75 | for (int set=0; set < mSeries->barsetCount(); set++) { |
|
76 | for (int set=0; set < mSeries->barsetCount(); set++) { | |
76 | qreal barHeight = mSeries->valueAt(set,category) * scale; |
|
77 | qreal barHeight = mSeries->valueAt(set,category) * scale; | |
77 | BarValue* value = mFloatingValues.at(itemIndex); |
|
78 | BarValue* value = mFloatingValues.at(itemIndex); | |
78 |
|
79 | |||
79 | QBarSet* barSet = mSeries->barsetAt(set); |
|
80 | QBarSet* barSet = mSeries->barsetAt(set); | |
80 | value->resize(100,50); // TODO: proper layout for this. |
|
81 | value->resize(100,50); // TODO: proper layout for this. | |
81 | value->setPos(xPos, yPos-barHeight/2); |
|
82 | value->setPos(xPos, yPos-barHeight/2); | |
82 | value->setPen(barSet->floatingValuePen()); |
|
83 | value->setPen(barSet->floatingValuePen()); | |
83 |
|
84 | |||
84 | if (mSeries->valueAt(set,category) != 0) { |
|
85 | if (mSeries->valueAt(set,category) != 0) { | |
85 | int p = mSeries->percentageAt(set,category) * 100; |
|
86 | int p = mSeries->percentageAt(set,category) * 100; | |
86 | QString vString(QString::number(p)); |
|
87 | QString vString(QString::number(p)); | |
87 | vString.truncate(3); |
|
88 | vString.truncate(3); | |
88 | vString.append("%"); |
|
89 | vString.append("%"); | |
89 | value->setValueString(vString); |
|
90 | value->setValueString(vString); | |
90 | } else { |
|
91 | } else { | |
91 | value->setValueString(QString("")); |
|
92 | value->setValueString(QString("")); | |
92 | } |
|
93 | } | |
93 |
|
94 | |||
94 | itemIndex++; |
|
95 | itemIndex++; | |
95 | yPos -= barHeight; |
|
96 | yPos -= barHeight; | |
96 | } |
|
97 | } | |
97 | xPos += xStep; |
|
98 | xPos += xStep; | |
98 | } |
|
99 | } | |
99 | } |
|
100 | } | |
100 |
|
101 | |||
101 | #include "moc_percentbarpresenter_p.cpp" |
|
102 | #include "moc_percentbarpresenter_p.cpp" | |
102 |
|
103 | |||
103 | QTCOMMERCIALCHART_END_NAMESPACE |
|
104 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,399 +1,352 | |||||
1 | #include "charttheme_p.h" |
|
1 | #include "charttheme_p.h" | |
2 | #include "qchart.h" |
|
2 | #include "qchart.h" | |
3 | #include "qchartview.h" |
|
3 | #include "qchartview.h" | |
4 | #include "qlegend.h" |
|
4 | #include "qlegend.h" | |
5 | #include "qchartaxis.h" |
|
5 | #include "qchartaxis.h" | |
6 | #include <QTime> |
|
6 | #include <QTime> | |
7 |
|
7 | |||
8 | //series |
|
8 | //series | |
9 | #include "qbarset.h" |
|
9 | #include "qbarset.h" | |
10 | #include "qbarseries.h" |
|
10 | #include "qbarseries.h" | |
11 | #include "qstackedbarseries.h" |
|
11 | #include "qstackedbarseries.h" | |
12 | #include "qpercentbarseries.h" |
|
12 | #include "qpercentbarseries.h" | |
13 | #include "qlineseries.h" |
|
13 | #include "qlineseries.h" | |
14 | #include "qareaseries.h" |
|
14 | #include "qareaseries.h" | |
15 | #include "qscatterseries.h" |
|
15 | #include "qscatterseries.h" | |
16 | #include "qpieseries.h" |
|
16 | #include "qpieseries.h" | |
17 | #include "qpieslice.h" |
|
17 | #include "qpieslice.h" | |
18 | #include "qsplineseries.h" |
|
18 | #include "qsplineseries.h" | |
19 |
|
19 | |||
20 | //items |
|
20 | //items | |
21 | #include "axisitem_p.h" |
|
21 | #include "axisitem_p.h" | |
22 | #include "barpresenter_p.h" |
|
22 | #include "barpresenter_p.h" | |
23 | #include "stackedbarpresenter_p.h" |
|
23 | #include "stackedbarpresenter_p.h" | |
24 | #include "percentbarpresenter_p.h" |
|
24 | #include "percentbarpresenter_p.h" | |
25 | #include "linechartitem_p.h" |
|
25 | #include "linechartitem_p.h" | |
26 | #include "areachartitem_p.h" |
|
26 | #include "areachartitem_p.h" | |
27 | #include "scatterchartitem_p.h" |
|
27 | #include "scatterchartitem_p.h" | |
28 | #include "piechartitem_p.h" |
|
28 | #include "piechartitem_p.h" | |
29 | #include "splinechartitem_p.h" |
|
29 | #include "splinechartitem_p.h" | |
30 |
|
30 | |||
31 | //themes |
|
31 | //themes | |
32 | #include "chartthemedefault_p.h" |
|
32 | #include "chartthemedefault_p.h" | |
33 | #include "chartthemelight_p.h" |
|
33 | #include "chartthemelight_p.h" | |
34 | #include "chartthemebluecerulean_p.h" |
|
34 | #include "chartthemebluecerulean_p.h" | |
35 | #include "chartthemedark_p.h" |
|
35 | #include "chartthemedark_p.h" | |
36 | #include "chartthemebrownsand_p.h" |
|
36 | #include "chartthemebrownsand_p.h" | |
37 | #include "chartthemebluencs_p.h" |
|
37 | #include "chartthemebluencs_p.h" | |
38 | #include "chartthemevanilla_p.h" |
|
38 | #include "chartthemevanilla_p.h" | |
39 | #include "chartthemeicy_p.h" |
|
39 | #include "chartthemeicy_p.h" | |
40 | #include "chartthemegrayscale_p.h" |
|
40 | #include "chartthemegrayscale_p.h" | |
41 | #include "chartthemescientific_p.h" |
|
41 | #include "chartthemescientific_p.h" | |
42 |
|
42 | |||
43 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
43 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
44 |
|
44 | |||
45 | ChartTheme::ChartTheme(QChart::ChartTheme id) : |
|
45 | ChartTheme::ChartTheme(QChart::ChartTheme id) : | |
46 | m_masterFont(QFont()), |
|
46 | m_masterFont(QFont()), | |
47 | m_titleBrush(QColor(QRgb(0x000000))), |
|
47 | m_titleBrush(QColor(QRgb(0x000000))), | |
48 | m_axisLinePen(QPen(QRgb(0x000000))), |
|
48 | m_axisLinePen(QPen(QRgb(0x000000))), | |
49 | m_axisLabelBrush(QColor(QRgb(0x000000))), |
|
49 | m_axisLabelBrush(QColor(QRgb(0x000000))), | |
50 | m_backgroundShadesPen(Qt::NoPen), |
|
50 | m_backgroundShadesPen(Qt::NoPen), | |
51 | m_backgroundShadesBrush(Qt::NoBrush), |
|
51 | m_backgroundShadesBrush(Qt::NoBrush), | |
52 | m_backgroundShades(BackgroundShadesNone), |
|
52 | m_backgroundShades(BackgroundShadesNone), | |
53 | m_gridLinePen(QPen(QRgb(0x000000))) |
|
53 | m_gridLinePen(QPen(QRgb(0x000000))) | |
54 | { |
|
54 | { | |
55 | m_id = id; |
|
55 | m_id = id; | |
56 | qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); |
|
56 | qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); | |
57 | } |
|
57 | } | |
58 |
|
58 | |||
59 |
|
59 | |||
60 | ChartTheme* ChartTheme::createTheme(QChart::ChartTheme theme) |
|
60 | ChartTheme* ChartTheme::createTheme(QChart::ChartTheme theme) | |
61 | { |
|
61 | { | |
62 | switch(theme) { |
|
62 | switch(theme) { | |
63 | case QChart::ChartThemeLight: |
|
63 | case QChart::ChartThemeLight: | |
64 | return new ChartThemeLight(); |
|
64 | return new ChartThemeLight(); | |
65 | case QChart::ChartThemeBlueCerulean: |
|
65 | case QChart::ChartThemeBlueCerulean: | |
66 | return new ChartThemeBlueCerulean(); |
|
66 | return new ChartThemeBlueCerulean(); | |
67 | case QChart::ChartThemeDark: |
|
67 | case QChart::ChartThemeDark: | |
68 | return new ChartThemeDark(); |
|
68 | return new ChartThemeDark(); | |
69 | case QChart::ChartThemeBrownSand: |
|
69 | case QChart::ChartThemeBrownSand: | |
70 | return new ChartThemeBrownSand(); |
|
70 | return new ChartThemeBrownSand(); | |
71 | case QChart::ChartThemeBlueNcs: |
|
71 | case QChart::ChartThemeBlueNcs: | |
72 | return new ChartThemeBlueNcs(); |
|
72 | return new ChartThemeBlueNcs(); | |
73 | case QChart::ChartThemeVanilla: |
|
73 | case QChart::ChartThemeVanilla: | |
74 | return new ChartThemeVanilla(); |
|
74 | return new ChartThemeVanilla(); | |
75 | case QChart::ChartThemeIcy: |
|
75 | case QChart::ChartThemeIcy: | |
76 | return new ChartThemeIcy(); |
|
76 | return new ChartThemeIcy(); | |
77 | case QChart::ChartThemeGrayscale: |
|
77 | case QChart::ChartThemeGrayscale: | |
78 | return new ChartThemeGrayscale(); |
|
78 | return new ChartThemeGrayscale(); | |
79 | case QChart::ChartThemeScientific: |
|
79 | case QChart::ChartThemeScientific: | |
80 | return new ChartThemeScientific(); |
|
80 | return new ChartThemeScientific(); | |
81 | default: |
|
81 | default: | |
82 | return new ChartThemeDefault(); |
|
82 | return new ChartThemeDefault(); | |
83 | } |
|
83 | } | |
84 | } |
|
84 | } | |
85 |
|
85 | |||
86 | void ChartTheme::decorate(QChart* chart,bool force) |
|
86 | void ChartTheme::decorate(QChart* chart,bool force) | |
87 | { |
|
87 | { | |
88 | QPen pen; |
|
88 | QPen pen; | |
89 | QBrush brush; |
|
89 | QBrush brush; | |
90 |
|
90 | |||
91 | if(brush == chart->backgroundBrush() || force) |
|
91 | if(brush == chart->backgroundBrush() || force) | |
92 | { |
|
92 | { | |
93 | if (m_backgroundShades == BackgroundShadesNone) { |
|
93 | if (m_backgroundShades == BackgroundShadesNone) { | |
94 | chart->setBackgroundBrush(m_chartBackgroundGradient); |
|
94 | chart->setBackgroundBrush(m_chartBackgroundGradient); | |
95 | } |
|
95 | } | |
96 | else { |
|
96 | else { | |
97 | chart->setBackgroundBrush(Qt::NoBrush); |
|
97 | chart->setBackgroundBrush(Qt::NoBrush); | |
98 | } |
|
98 | } | |
99 | } |
|
99 | } | |
100 | chart->setTitleFont(m_masterFont); |
|
100 | chart->setTitleFont(m_masterFont); | |
101 | chart->setTitleBrush(m_titleBrush); |
|
101 | chart->setTitleBrush(m_titleBrush); | |
102 | } |
|
102 | } | |
103 |
|
103 | |||
104 | void ChartTheme::decorate(QLegend* legend,bool force) |
|
104 | void ChartTheme::decorate(QLegend* legend,bool force) | |
105 | { |
|
105 | { | |
106 | QPen pen; |
|
106 | QPen pen; | |
107 | QBrush brush; |
|
107 | QBrush brush; | |
108 |
|
108 | |||
109 | if (pen == legend->pen() || force){ |
|
109 | if (pen == legend->pen() || force){ | |
110 | //TODO:: legend->setPen(); |
|
110 | //TODO:: legend->setPen(); | |
111 | } |
|
111 | } | |
112 |
|
112 | |||
113 |
|
113 | |||
114 | if (brush == legend->brush() || force) { |
|
114 | if (brush == legend->brush() || force) { | |
115 | legend->setBrush(m_chartBackgroundGradient); |
|
115 | legend->setBrush(m_chartBackgroundGradient); | |
116 | } |
|
116 | } | |
117 | } |
|
117 | } | |
118 |
|
118 | |||
119 | void ChartTheme::decorate(QAreaSeries* series, int index,bool force) |
|
119 | void ChartTheme::decorate(QAreaSeries* series, int index,bool force) | |
120 | { |
|
120 | { | |
121 | QPen pen; |
|
121 | QPen pen; | |
122 | QBrush brush; |
|
122 | QBrush brush; | |
123 |
|
123 | |||
124 | if (pen == series->pen() || force){ |
|
124 | if (pen == series->pen() || force){ | |
125 |
pen.setColor(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), |
|
125 | pen.setColor(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.0)); | |
126 | pen.setWidthF(2); |
|
126 | pen.setWidthF(2); | |
127 | series->setPen(pen); |
|
127 | series->setPen(pen); | |
128 | } |
|
128 | } | |
129 |
|
129 | |||
130 | if (brush == series->brush() || force) { |
|
130 | if (brush == series->brush() || force) { | |
131 | QBrush brush(m_seriesColors.at(index % m_seriesColors.size())); |
|
131 | QBrush brush(m_seriesColors.at(index % m_seriesColors.size())); | |
132 | series->setBrush(brush); |
|
132 | series->setBrush(brush); | |
133 | } |
|
133 | } | |
134 | } |
|
134 | } | |
135 |
|
135 | |||
136 |
|
136 | |||
137 | void ChartTheme::decorate(QLineSeries* series,int index,bool force) |
|
137 | void ChartTheme::decorate(QLineSeries* series,int index,bool force) | |
138 | { |
|
138 | { | |
139 | QPen pen; |
|
139 | QPen pen; | |
140 | if(pen == series->pen() || force ){ |
|
140 | if(pen == series->pen() || force ){ | |
141 | pen.setColor(m_seriesColors.at(index%m_seriesColors.size())); |
|
141 | pen.setColor(m_seriesColors.at(index%m_seriesColors.size())); | |
142 | pen.setWidthF(2); |
|
142 | pen.setWidthF(2); | |
143 | series->setPen(pen); |
|
143 | series->setPen(pen); | |
144 | } |
|
144 | } | |
145 | } |
|
145 | } | |
146 |
|
146 | |||
147 | void ChartTheme::decorate(QBarSeries* series,int index,bool force) |
|
147 | void ChartTheme::decorate(QBarSeries* series, int index, bool force) | |
148 | { |
|
148 | { | |
149 | QBrush brush; |
|
149 | QBrush brush; | |
|
150 | QPen pen; | |||
150 | QList<QBarSet*> sets = series->barSets(); |
|
151 | QList<QBarSet*> sets = series->barSets(); | |
151 |
|
152 | |||
152 |
for (int i |
|
153 | for (int i(0); i < sets.count(); i++) { | |
153 | qreal pos = 0.5; |
|
154 | qreal pos = 0.5; | |
154 | if (sets.count() > 1) |
|
155 | if (sets.count() > 1) | |
155 | pos = (qreal) i / (qreal) (sets.count() - 1); |
|
156 | pos = (qreal) i / (qreal) (sets.count() - 1); | |
156 |
|
157 | |||
157 | if(brush == sets.at(i)->brush() || force ){ |
|
158 | if (brush == sets.at(i)->brush() || force ) { | |
158 | QColor c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos); |
|
159 | QColor c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos); | |
159 | sets.at(i)->setBrush(QBrush(c)); |
|
160 | sets.at(i)->setBrush(QBrush(c)); | |
160 | } |
|
|||
161 | // Pick label color as far as possible from bar color (within gradient). |
|
|||
162 | // 0.3 is magic number that was picked as value that gave enough contrast with icy theme gradient :) |
|
|||
163 | // TODO: better picking of label color? |
|
|||
164 | QColor c; |
|
|||
165 |
|
||||
166 | if (pos < 0.3) { |
|
|||
167 | c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1); |
|
|||
168 | } else { |
|
|||
169 | c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0); |
|
|||
170 | } |
|
|||
171 | sets.at(i)->setFloatingValuePen(QPen(c)); |
|
|||
172 |
|
||||
173 | } |
|
|||
174 | } |
|
|||
175 |
|
||||
176 | void ChartTheme::decorate(QStackedBarSeries* series,int index,bool force) |
|
|||
177 | { |
|
|||
178 | QBrush brush; |
|
|||
179 | QList<QBarSet*> sets = series->barSets(); |
|
|||
180 |
|
||||
181 | for (int i=0; i<sets.count(); i++) { |
|
|||
182 | qreal pos = 0.5; |
|
|||
183 | if (sets.count() > 1) |
|
|||
184 | pos = (qreal) i / (qreal) (sets.count() - 1); |
|
|||
185 | if(brush == sets.at(i)->brush() || force){ |
|
|||
186 | QColor c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos); |
|
|||
187 | sets.at(i)->setBrush(QBrush(c)); |
|
|||
188 | } |
|
|||
189 | QColor c; |
|
|||
190 | if (pos < 0.3) { |
|
|||
191 | c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1); |
|
|||
192 | } else { |
|
|||
193 | c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0); |
|
|||
194 | } |
|
161 | } | |
195 | sets.at(i)->setFloatingValuePen(QPen(c)); |
|
|||
196 | } |
|
|||
197 | } |
|
|||
198 |
|
162 | |||
199 | void ChartTheme::decorate(QPercentBarSeries* series,int index,bool force) |
|
163 | // Pick label color from the opposite end of the gradient. | |
200 | { |
|
164 | // 0.3 as a boundary seems to work well. | |
201 | QBrush brush; |
|
165 | if (pos < 0.3) | |
202 | QList<QBarSet*> sets = series->barSets(); |
|
166 | sets.at(i)->setFloatingValuePen(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1)); | |
|
167 | else | |||
|
168 | sets.at(i)->setFloatingValuePen(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0)); | |||
203 |
|
169 | |||
204 | for (int i=0; i<sets.count(); i++) { |
|
170 | if (pen == sets.at(i)->pen() || force) { | |
205 | qreal pos = 0.5; |
|
171 | QColor c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.0); | |
206 |
|
|
172 | sets.at(i)->setPen(c); | |
207 | pos = (qreal) i / (qreal) (sets.count() - 1); |
|
|||
208 |
|
||||
209 | if(brush == sets.at(i)->brush() || force){ |
|
|||
210 | QColor c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos); |
|
|||
211 | sets.at(i)->setBrush(QBrush(c)); |
|
|||
212 | } |
|
|||
213 | QColor c; |
|
|||
214 | if (pos < 0.3) { |
|
|||
215 | c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1); |
|
|||
216 | } else { |
|
|||
217 | c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0); |
|
|||
218 | } |
|
173 | } | |
219 | sets.at(i)->setFloatingValuePen(QPen(c)); |
|
|||
220 | } |
|
174 | } | |
221 | } |
|
175 | } | |
222 |
|
176 | |||
223 | void ChartTheme::decorate(QScatterSeries* series, int index,bool force) |
|
177 | void ChartTheme::decorate(QScatterSeries* series, int index,bool force) | |
224 | { |
|
178 | { | |
225 |
|
||||
226 | QPen pen; |
|
179 | QPen pen; | |
227 | QBrush brush; |
|
180 | QBrush brush; | |
228 |
|
181 | |||
229 | if (pen == series->pen() || force) { |
|
182 | if (pen == series->pen() || force) { | |
230 |
pen.setColor(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), |
|
183 | pen.setColor(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.0)); | |
231 | pen.setWidthF(2); |
|
184 | pen.setWidthF(2); | |
232 | series->setPen(pen); |
|
185 | series->setPen(pen); | |
233 | } |
|
186 | } | |
234 |
|
187 | |||
235 | if (brush == series->brush() || force) { |
|
188 | if (brush == series->brush() || force) { | |
236 | QBrush brush(m_seriesColors.at(index % m_seriesColors.size())); |
|
189 | QBrush brush(m_seriesColors.at(index % m_seriesColors.size())); | |
237 | series->setBrush(brush); |
|
190 | series->setBrush(brush); | |
238 | } |
|
191 | } | |
239 | } |
|
192 | } | |
240 |
|
193 | |||
241 | void ChartTheme::decorate(QPieSeries* series, int index, bool force) |
|
194 | void ChartTheme::decorate(QPieSeries* series, int index, bool force) | |
242 | { |
|
195 | { | |
243 | // Get color for a slice from a gradient linearly, beginning from the start of the gradient |
|
|||
244 |
|
||||
245 | QPen pen; |
|
196 | QPen pen; | |
246 | QBrush brush; |
|
197 | QBrush brush; | |
247 |
|
198 | |||
248 | for (int i(0); i < series->slices().count(); i++) { |
|
199 | for (int i(0); i < series->slices().count(); i++) { | |
249 | qreal pos = (qreal) i / (qreal) series->count(); |
|
200 | if (pen == series->slices().at(i)->slicePen() || force) { | |
250 | if( pen == series->slices().at(i)->slicePen() || force){ |
|
201 | QColor penColor = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.0); | |
251 | QColor penColor = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.1); |
|
202 | series->slices().at(i)->setSlicePen(penColor); | |
252 | series->slices().at(i)->setSlicePen(penColor); |
|
|||
253 | } |
|
203 | } | |
254 | if( brush == series->slices().at(i)->sliceBrush() || force){ |
|
204 | ||
255 | QColor brushColor = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos); |
|
205 | // Get color for a slice from a gradient linearly, beginning from the start of the gradient | |
256 | series->slices().at(i)->setSliceBrush(brushColor); |
|
206 | qreal pos = (qreal) i / (qreal) series->count(); | |
|
207 | if (brush == series->slices().at(i)->sliceBrush() || force) { | |||
|
208 | QColor brushColor = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos); | |||
|
209 | series->slices().at(i)->setSliceBrush(brushColor); | |||
257 | } |
|
210 | } | |
258 | } |
|
211 | } | |
259 | } |
|
212 | } | |
260 |
|
213 | |||
261 | void ChartTheme::decorate(QSplineSeries* series, int index, bool force) |
|
214 | void ChartTheme::decorate(QSplineSeries* series, int index, bool force) | |
262 | { |
|
215 | { | |
263 | QPen pen; |
|
216 | QPen pen; | |
264 | if(pen == series->pen() || force){ |
|
217 | if(pen == series->pen() || force){ | |
265 | pen.setColor(m_seriesColors.at(index%m_seriesColors.size())); |
|
218 | pen.setColor(m_seriesColors.at(index%m_seriesColors.size())); | |
266 | pen.setWidthF(2); |
|
219 | pen.setWidthF(2); | |
267 | series->setPen(pen); |
|
220 | series->setPen(pen); | |
268 | } |
|
221 | } | |
269 | } |
|
222 | } | |
270 |
|
223 | |||
271 | void ChartTheme::decorate(QChartAxis* axis,bool axisX, bool force) |
|
224 | void ChartTheme::decorate(QChartAxis* axis,bool axisX, bool force) | |
272 | { |
|
225 | { | |
273 | QPen pen; |
|
226 | QPen pen; | |
274 | QBrush brush; |
|
227 | QBrush brush; | |
275 | QFont font; |
|
228 | QFont font; | |
276 |
|
229 | |||
277 | if (axis->isAxisVisible()) { |
|
230 | if (axis->isAxisVisible()) { | |
278 |
|
231 | |||
279 | if(brush == axis->labelsBrush() || force){ |
|
232 | if(brush == axis->labelsBrush() || force){ | |
280 | axis->setLabelsBrush(m_axisLabelBrush); |
|
233 | axis->setLabelsBrush(m_axisLabelBrush); | |
281 | } |
|
234 | } | |
282 | if(pen == axis->labelsPen() || force){ |
|
235 | if(pen == axis->labelsPen() || force){ | |
283 | axis->setLabelsPen(Qt::NoPen); // NoPen for performance reasons |
|
236 | axis->setLabelsPen(Qt::NoPen); // NoPen for performance reasons | |
284 | } |
|
237 | } | |
285 |
|
238 | |||
286 |
|
239 | |||
287 | if (axis->shadesVisible() || force) { |
|
240 | if (axis->shadesVisible() || force) { | |
288 |
|
241 | |||
289 | if(brush == axis->shadesBrush() || force){ |
|
242 | if(brush == axis->shadesBrush() || force){ | |
290 | axis->setShadesBrush(m_backgroundShadesBrush); |
|
243 | axis->setShadesBrush(m_backgroundShadesBrush); | |
291 | } |
|
244 | } | |
292 |
|
245 | |||
293 | if(pen == axis->shadesPen() || force){ |
|
246 | if(pen == axis->shadesPen() || force){ | |
294 | axis->setShadesPen(m_backgroundShadesPen); |
|
247 | axis->setShadesPen(m_backgroundShadesPen); | |
295 | } |
|
248 | } | |
296 |
|
249 | |||
297 | if(force && (m_backgroundShades == BackgroundShadesBoth |
|
250 | if(force && (m_backgroundShades == BackgroundShadesBoth | |
298 | || (m_backgroundShades == BackgroundShadesVertical && axisX) |
|
251 | || (m_backgroundShades == BackgroundShadesVertical && axisX) | |
299 | || (m_backgroundShades == BackgroundShadesHorizontal && !axisX))){ |
|
252 | || (m_backgroundShades == BackgroundShadesHorizontal && !axisX))){ | |
300 | axis->setShadesVisible(true); |
|
253 | axis->setShadesVisible(true); | |
301 |
|
254 | |||
302 | } |
|
255 | } | |
303 | } |
|
256 | } | |
304 |
|
257 | |||
305 | if(pen == axis->axisPen() || force){ |
|
258 | if(pen == axis->axisPen() || force){ | |
306 | axis->setAxisPen(m_axisLinePen); |
|
259 | axis->setAxisPen(m_axisLinePen); | |
307 | } |
|
260 | } | |
308 |
|
261 | |||
309 | if(pen == axis->gridLinePen() || force){ |
|
262 | if(pen == axis->gridLinePen() || force){ | |
310 | axis->setGridLinePen(m_gridLinePen); |
|
263 | axis->setGridLinePen(m_gridLinePen); | |
311 | } |
|
264 | } | |
312 |
|
265 | |||
313 | if(font == axis->labelsFont() || force){ |
|
266 | if(font == axis->labelsFont() || force){ | |
314 | axis->setLabelsFont(m_masterFont); |
|
267 | axis->setLabelsFont(m_masterFont); | |
315 | } |
|
268 | } | |
316 | } |
|
269 | } | |
317 | } |
|
270 | } | |
318 |
|
271 | |||
319 | void ChartTheme::generateSeriesGradients() |
|
272 | void ChartTheme::generateSeriesGradients() | |
320 | { |
|
273 | { | |
321 | // Generate gradients in HSV color space |
|
274 | // Generate gradients in HSV color space | |
322 | foreach (QColor color, m_seriesColors) { |
|
275 | foreach (QColor color, m_seriesColors) { | |
323 | QLinearGradient g; |
|
276 | QLinearGradient g; | |
324 | qreal h = color.hsvHueF(); |
|
277 | qreal h = color.hsvHueF(); | |
325 | qreal s = color.hsvSaturationF(); |
|
278 | qreal s = color.hsvSaturationF(); | |
326 |
|
279 | |||
327 | // TODO: tune the algorithm to give nice results with most base colors defined in |
|
280 | // TODO: tune the algorithm to give nice results with most base colors defined in | |
328 | // most themes. The rest of the gradients we can define manually in theme specific |
|
281 | // most themes. The rest of the gradients we can define manually in theme specific | |
329 | // implementation. |
|
282 | // implementation. | |
330 | QColor start = color; |
|
283 | QColor start = color; | |
331 | start.setHsvF(h, 0.05, 0.95); |
|
284 | start.setHsvF(h, 0.05, 0.95); | |
332 | g.setColorAt(0.0, start); |
|
285 | g.setColorAt(0.0, start); | |
333 |
|
286 | |||
334 | g.setColorAt(0.5, color); |
|
287 | g.setColorAt(0.5, color); | |
335 |
|
288 | |||
336 | QColor end = color; |
|
289 | QColor end = color; | |
337 | end.setHsvF(h, s, 0.25); |
|
290 | end.setHsvF(h, s, 0.25); | |
338 | g.setColorAt(1.0, end); |
|
291 | g.setColorAt(1.0, end); | |
339 |
|
292 | |||
340 | m_seriesGradients << g; |
|
293 | m_seriesGradients << g; | |
341 | } |
|
294 | } | |
342 | } |
|
295 | } | |
343 |
|
296 | |||
344 |
|
297 | |||
345 | QColor ChartTheme::colorAt(const QColor &start, const QColor &end, qreal pos) |
|
298 | QColor ChartTheme::colorAt(const QColor &start, const QColor &end, qreal pos) | |
346 | { |
|
299 | { | |
347 | Q_ASSERT(pos >=0.0 && pos <= 1.0); |
|
300 | Q_ASSERT(pos >=0.0 && pos <= 1.0); | |
348 | qreal r = start.redF() + ((end.redF() - start.redF()) * pos); |
|
301 | qreal r = start.redF() + ((end.redF() - start.redF()) * pos); | |
349 | qreal g = start.greenF() + ((end.greenF() - start.greenF()) * pos); |
|
302 | qreal g = start.greenF() + ((end.greenF() - start.greenF()) * pos); | |
350 | qreal b = start.blueF() + ((end.blueF() - start.blueF()) * pos); |
|
303 | qreal b = start.blueF() + ((end.blueF() - start.blueF()) * pos); | |
351 | QColor c; |
|
304 | QColor c; | |
352 | c.setRgbF(r, g, b); |
|
305 | c.setRgbF(r, g, b); | |
353 | return c; |
|
306 | return c; | |
354 | } |
|
307 | } | |
355 |
|
308 | |||
356 | QColor ChartTheme::colorAt(const QGradient &gradient, qreal pos) |
|
309 | QColor ChartTheme::colorAt(const QGradient &gradient, qreal pos) | |
357 | { |
|
310 | { | |
358 | Q_ASSERT(pos >=0 && pos <= 1.0); |
|
311 | Q_ASSERT(pos >=0 && pos <= 1.0); | |
359 |
|
312 | |||
360 | // another possibility: |
|
313 | // another possibility: | |
361 | // http://stackoverflow.com/questions/3306786/get-intermediate-color-from-a-gradient |
|
314 | // http://stackoverflow.com/questions/3306786/get-intermediate-color-from-a-gradient | |
362 |
|
315 | |||
363 | QGradientStops stops = gradient.stops(); |
|
316 | QGradientStops stops = gradient.stops(); | |
364 | int count = stops.count(); |
|
317 | int count = stops.count(); | |
365 |
|
318 | |||
366 | // find previous stop relative to position |
|
319 | // find previous stop relative to position | |
367 | QGradientStop prev = stops.first(); |
|
320 | QGradientStop prev = stops.first(); | |
368 | for (int i=0; i<count; i++) { |
|
321 | for (int i=0; i<count; i++) { | |
369 | QGradientStop stop = stops.at(i); |
|
322 | QGradientStop stop = stops.at(i); | |
370 | if (pos > stop.first) |
|
323 | if (pos > stop.first) | |
371 | prev = stop; |
|
324 | prev = stop; | |
372 |
|
325 | |||
373 | // given position is actually a stop position? |
|
326 | // given position is actually a stop position? | |
374 | if (pos == stop.first) { |
|
327 | if (pos == stop.first) { | |
375 | //qDebug() << "stop color" << pos; |
|
328 | //qDebug() << "stop color" << pos; | |
376 | return stop.second; |
|
329 | return stop.second; | |
377 | } |
|
330 | } | |
378 | } |
|
331 | } | |
379 |
|
332 | |||
380 | // find next stop relative to position |
|
333 | // find next stop relative to position | |
381 | QGradientStop next = stops.last(); |
|
334 | QGradientStop next = stops.last(); | |
382 | for (int i=count-1; i>=0; i--) { |
|
335 | for (int i=count-1; i>=0; i--) { | |
383 | QGradientStop stop = stops.at(i); |
|
336 | QGradientStop stop = stops.at(i); | |
384 | if (pos < stop.first) |
|
337 | if (pos < stop.first) | |
385 | next = stop; |
|
338 | next = stop; | |
386 | } |
|
339 | } | |
387 |
|
340 | |||
388 | //qDebug() << "prev" << prev.first << "pos" << pos << "next" << next.first; |
|
341 | //qDebug() << "prev" << prev.first << "pos" << pos << "next" << next.first; | |
389 |
|
342 | |||
390 | qreal range = next.first - prev.first; |
|
343 | qreal range = next.first - prev.first; | |
391 | qreal posDelta = pos - prev.first; |
|
344 | qreal posDelta = pos - prev.first; | |
392 | qreal relativePos = posDelta / range; |
|
345 | qreal relativePos = posDelta / range; | |
393 |
|
346 | |||
394 | //qDebug() << "range" << range << "posDelta" << posDelta << "relativePos" << relativePos; |
|
347 | //qDebug() << "range" << range << "posDelta" << posDelta << "relativePos" << relativePos; | |
395 |
|
348 | |||
396 | return colorAt(prev.second, next.second, relativePos); |
|
349 | return colorAt(prev.second, next.second, relativePos); | |
397 | } |
|
350 | } | |
398 |
|
351 | |||
399 | QTCOMMERCIALCHART_END_NAMESPACE |
|
352 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,81 +1,78 | |||||
1 | #ifndef CHARTTHEME_H |
|
1 | #ifndef CHARTTHEME_H | |
2 | #define CHARTTHEME_H |
|
2 | #define CHARTTHEME_H | |
3 |
|
3 | |||
4 | #include "qchartglobal.h" |
|
4 | #include "qchartglobal.h" | |
5 | #include "qchart.h" |
|
5 | #include "qchart.h" | |
6 | #include <QColor> |
|
6 | #include <QColor> | |
7 | #include <QGradientStops> |
|
7 | #include <QGradientStops> | |
8 |
|
8 | |||
9 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
9 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
10 |
|
10 | |||
11 | class ChartItem; |
|
11 | class ChartItem; | |
12 | class QSeries; |
|
12 | class QSeries; | |
13 | class LineChartItem; |
|
13 | class LineChartItem; | |
14 | class QLineSeries; |
|
14 | class QLineSeries; | |
15 | class BarPresenter; |
|
15 | class BarPresenter; | |
16 | class QBarSeries; |
|
16 | class QBarSeries; | |
17 | class StackedBarPresenter; |
|
17 | class StackedBarPresenter; | |
18 | class QStackedBarSeries; |
|
18 | class QStackedBarSeries; | |
19 | class QPercentBarSeries; |
|
19 | class QPercentBarSeries; | |
20 | class PercentBarPresenter; |
|
20 | class PercentBarPresenter; | |
21 | class QScatterSeries; |
|
21 | class QScatterSeries; | |
22 | class ScatterChartItem; |
|
22 | class ScatterChartItem; | |
23 | class PieChartItem; |
|
23 | class PieChartItem; | |
24 | class QPieSeries; |
|
24 | class QPieSeries; | |
25 | class SplineChartItem; |
|
25 | class SplineChartItem; | |
26 | class QSplineSeries; |
|
26 | class QSplineSeries; | |
27 | class AreaChartItem; |
|
27 | class AreaChartItem; | |
28 | class QAreaSeries; |
|
28 | class QAreaSeries; | |
29 |
|
29 | |||
30 | class ChartTheme |
|
30 | class ChartTheme | |
31 | { |
|
31 | { | |
32 | public: |
|
32 | public: | |
33 | enum BackgroundShadesMode { |
|
33 | enum BackgroundShadesMode { | |
34 | BackgroundShadesNone = 0, |
|
34 | BackgroundShadesNone = 0, | |
35 | BackgroundShadesVertical, |
|
35 | BackgroundShadesVertical, | |
36 | BackgroundShadesHorizontal, |
|
36 | BackgroundShadesHorizontal, | |
37 | BackgroundShadesBoth |
|
37 | BackgroundShadesBoth | |
38 | }; |
|
38 | }; | |
39 |
|
39 | |||
40 | protected: |
|
40 | protected: | |
41 | explicit ChartTheme(QChart::ChartTheme id = QChart::ChartThemeDefault); |
|
41 | explicit ChartTheme(QChart::ChartTheme id = QChart::ChartThemeDefault); | |
42 | public: |
|
42 | public: | |
43 | static ChartTheme* createTheme(QChart::ChartTheme theme); |
|
43 | static ChartTheme* createTheme(QChart::ChartTheme theme); | |
44 | QChart::ChartTheme id() const {return m_id;} |
|
44 | QChart::ChartTheme id() const {return m_id;} | |
45 | void decorate(QChart* chart,bool force = true); |
|
45 | void decorate(QChart* chart,bool force = true); | |
46 | void decorate(QLegend* legend,bool force = true); |
|
46 | void decorate(QLegend* legend,bool force = true); | |
47 | //void decorate(ChartItem* item, QSeries* series,int index); |
|
|||
48 | void decorate(QBarSeries* series, int index,bool force = true); |
|
47 | void decorate(QBarSeries* series, int index,bool force = true); | |
49 | void decorate(QStackedBarSeries* series, int index,bool force = true); |
|
|||
50 | void decorate(QPercentBarSeries* series, int index,bool force = true); |
|
|||
51 | void decorate(QLineSeries* series, int index,bool force = true); |
|
48 | void decorate(QLineSeries* series, int index,bool force = true); | |
52 | void decorate(QAreaSeries* series, int index,bool force = true); |
|
49 | void decorate(QAreaSeries* series, int index,bool force = true); | |
53 | void decorate(QScatterSeries* series, int index,bool force = true); |
|
50 | void decorate(QScatterSeries* series, int index,bool force = true); | |
54 | void decorate(QPieSeries* series, int index,bool force = true); |
|
51 | void decorate(QPieSeries* series, int index,bool force = true); | |
55 | void decorate(QSplineSeries* series, int index,bool force = true); |
|
52 | void decorate(QSplineSeries* series, int index,bool force = true); | |
56 | void decorate(QChartAxis* axis, bool axisX,bool force = true); |
|
53 | void decorate(QChartAxis* axis, bool axisX,bool force = true); | |
57 |
|
54 | |||
58 | public: // utils |
|
55 | public: // utils | |
59 | void generateSeriesGradients(); |
|
56 | void generateSeriesGradients(); | |
60 | static QColor colorAt(const QColor &start, const QColor &end, qreal pos); |
|
57 | static QColor colorAt(const QColor &start, const QColor &end, qreal pos); | |
61 | static QColor colorAt(const QGradient &gradient, qreal pos); |
|
58 | static QColor colorAt(const QGradient &gradient, qreal pos); | |
62 |
|
59 | |||
63 | protected: |
|
60 | protected: | |
64 | QChart::ChartTheme m_id; |
|
61 | QChart::ChartTheme m_id; | |
65 | QList<QColor> m_seriesColors; |
|
62 | QList<QColor> m_seriesColors; | |
66 | QList<QGradient> m_seriesGradients; |
|
63 | QList<QGradient> m_seriesGradients; | |
67 | QLinearGradient m_chartBackgroundGradient; |
|
64 | QLinearGradient m_chartBackgroundGradient; | |
68 |
|
65 | |||
69 | QFont m_masterFont; |
|
66 | QFont m_masterFont; | |
70 | QBrush m_titleBrush; |
|
67 | QBrush m_titleBrush; | |
71 | QPen m_axisLinePen; |
|
68 | QPen m_axisLinePen; | |
72 | QBrush m_axisLabelBrush; |
|
69 | QBrush m_axisLabelBrush; | |
73 | QPen m_backgroundShadesPen; |
|
70 | QPen m_backgroundShadesPen; | |
74 | QBrush m_backgroundShadesBrush; |
|
71 | QBrush m_backgroundShadesBrush; | |
75 | BackgroundShadesMode m_backgroundShades; |
|
72 | BackgroundShadesMode m_backgroundShades; | |
76 | QPen m_gridLinePen; |
|
73 | QPen m_gridLinePen; | |
77 | }; |
|
74 | }; | |
78 |
|
75 | |||
79 | QTCOMMERCIALCHART_END_NAMESPACE |
|
76 | QTCOMMERCIALCHART_END_NAMESPACE | |
80 |
|
77 | |||
81 | #endif // CHARTTHEME_H |
|
78 | #endif // CHARTTHEME_H |
@@ -1,179 +1,173 | |||||
1 | #include "scatterchartitem_p.h" |
|
1 | #include "scatterchartitem_p.h" | |
2 | #include "qscatterseries.h" |
|
2 | #include "qscatterseries.h" | |
3 | #include "chartpresenter_p.h" |
|
3 | #include "chartpresenter_p.h" | |
4 | #include <QPainter> |
|
4 | #include <QPainter> | |
5 | #include <QGraphicsScene> |
|
5 | #include <QGraphicsScene> | |
6 |
|
6 | |||
7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
8 |
|
8 | |||
9 |
|
||||
10 |
|
||||
11 |
|
||||
12 |
|
||||
13 |
|
||||
14 |
|
||||
15 | ScatterChartItem::ScatterChartItem(QScatterSeries *series, QGraphicsItem *parent) : |
|
9 | ScatterChartItem::ScatterChartItem(QScatterSeries *series, QGraphicsItem *parent) : | |
16 | XYChartItem(series,parent), |
|
10 | XYChartItem(series,parent), | |
17 | m_series(series), |
|
11 | m_series(series), | |
18 | m_items(this), |
|
12 | m_items(this), | |
19 | m_shape(QScatterSeries::MarkerShapeRectangle), |
|
13 | m_shape(QScatterSeries::MarkerShapeRectangle), | |
20 |
m_size(1 |
|
14 | m_size(15) | |
21 |
|
15 | |||
22 | { |
|
16 | { | |
23 | Q_ASSERT(parent); |
|
17 | Q_ASSERT(parent); | |
24 | Q_ASSERT(series); |
|
18 | Q_ASSERT(series); | |
25 |
|
19 | |||
26 | QObject::connect(m_series,SIGNAL(updated()), this, SLOT(handleUpdated())); |
|
20 | QObject::connect(m_series,SIGNAL(updated()), this, SLOT(handleUpdated())); | |
27 |
|
21 | |||
28 | setZValue(ChartPresenter::ScatterSeriesZValue); |
|
22 | setZValue(ChartPresenter::ScatterSeriesZValue); | |
29 | setFlags(QGraphicsItem::ItemHasNoContents); |
|
23 | setFlags(QGraphicsItem::ItemHasNoContents); | |
30 | setFlags(QGraphicsItem::ItemClipsChildrenToShape); |
|
24 | setFlags(QGraphicsItem::ItemClipsChildrenToShape); | |
31 |
|
25 | |||
32 | handleUpdated(); |
|
26 | handleUpdated(); | |
33 |
|
27 | |||
34 | m_items.setHandlesChildEvents(false); |
|
28 | m_items.setHandlesChildEvents(false); | |
35 |
|
29 | |||
36 | // TODO: how to draw a drop shadow? |
|
30 | // TODO: how to draw a drop shadow? | |
37 | // QGraphicsDropShadowEffect *dropShadow = new QGraphicsDropShadowEffect(); |
|
31 | // QGraphicsDropShadowEffect *dropShadow = new QGraphicsDropShadowEffect(); | |
38 | // dropShadow->setOffset(2.0); |
|
32 | // dropShadow->setOffset(2.0); | |
39 | // dropShadow->setBlurRadius(2.0); |
|
33 | // dropShadow->setBlurRadius(2.0); | |
40 | // setGraphicsEffect(dropShadow); |
|
34 | // setGraphicsEffect(dropShadow); | |
41 | } |
|
35 | } | |
42 |
|
36 | |||
43 |
|
37 | |||
44 | QRectF ScatterChartItem::boundingRect() const |
|
38 | QRectF ScatterChartItem::boundingRect() const | |
45 | { |
|
39 | { | |
46 | return m_rect; |
|
40 | return m_rect; | |
47 | } |
|
41 | } | |
48 |
|
42 | |||
49 | void ScatterChartItem::createPoints(int count) |
|
43 | void ScatterChartItem::createPoints(int count) | |
50 | { |
|
44 | { | |
51 | for (int i = 0; i < count; ++i) { |
|
45 | for (int i = 0; i < count; ++i) { | |
52 |
|
46 | |||
53 | QGraphicsItem *item; |
|
47 | QGraphicsItem *item; | |
54 |
|
48 | |||
55 | switch (m_shape) { |
|
49 | switch (m_shape) { | |
56 | case QScatterSeries::MarkerShapeCircle:{ |
|
50 | case QScatterSeries::MarkerShapeCircle:{ | |
57 | QGraphicsEllipseItem* i = new QGraphicsEllipseItem(0,0,m_size,m_size); |
|
51 | QGraphicsEllipseItem* i = new QGraphicsEllipseItem(0,0,m_size,m_size); | |
58 | const QRectF& rect = i->boundingRect(); |
|
52 | const QRectF& rect = i->boundingRect(); | |
59 | i->setPos(-rect.width()/2,-rect.height()/2); |
|
53 | i->setPos(-rect.width()/2,-rect.height()/2); | |
60 | item = new Marker(i,this); |
|
54 | item = new Marker(i,this); | |
61 | break; |
|
55 | break; | |
62 | } |
|
56 | } | |
63 | case QScatterSeries::MarkerShapeRectangle:{ |
|
57 | case QScatterSeries::MarkerShapeRectangle:{ | |
64 | QGraphicsRectItem* i = new QGraphicsRectItem(0,0,m_size,m_size); |
|
58 | QGraphicsRectItem* i = new QGraphicsRectItem(0,0,m_size,m_size); | |
65 | i->setPos(-m_size/2,-m_size/2); |
|
59 | i->setPos(-m_size/2,-m_size/2); | |
66 | item = new Marker(i,this); |
|
60 | item = new Marker(i,this); | |
67 | break; |
|
61 | break; | |
68 | } |
|
62 | } | |
69 | default: |
|
63 | default: | |
70 | qWarning()<<"Unsupported marker type"; |
|
64 | qWarning()<<"Unsupported marker type"; | |
71 | break; |
|
65 | break; | |
72 |
|
66 | |||
73 | } |
|
67 | } | |
74 | m_items.addToGroup(item); |
|
68 | m_items.addToGroup(item); | |
75 | } |
|
69 | } | |
76 | } |
|
70 | } | |
77 |
|
71 | |||
78 | void ScatterChartItem::deletePoints(int count) |
|
72 | void ScatterChartItem::deletePoints(int count) | |
79 | { |
|
73 | { | |
80 | QList<QGraphicsItem *> items = m_items.childItems(); |
|
74 | QList<QGraphicsItem *> items = m_items.childItems(); | |
81 |
|
75 | |||
82 | for (int i = 0; i < count; ++i) { |
|
76 | for (int i = 0; i < count; ++i) { | |
83 | delete(items.takeLast()); |
|
77 | delete(items.takeLast()); | |
84 | } |
|
78 | } | |
85 | } |
|
79 | } | |
86 |
|
80 | |||
87 | void ScatterChartItem::markerSelected(Marker* marker) |
|
81 | void ScatterChartItem::markerSelected(Marker* marker) | |
88 | { |
|
82 | { | |
89 | emit XYChartItem::clicked(QPointF(m_series->x(marker->index()), m_series->y(marker->index()))); |
|
83 | emit XYChartItem::clicked(QPointF(m_series->x(marker->index()), m_series->y(marker->index()))); | |
90 | } |
|
84 | } | |
91 |
|
85 | |||
92 | void ScatterChartItem::setLayout(QVector<QPointF>& points) |
|
86 | void ScatterChartItem::setLayout(QVector<QPointF>& points) | |
93 | { |
|
87 | { | |
94 | if(points.size()==0) |
|
88 | if(points.size()==0) | |
95 | { |
|
89 | { | |
96 | XYChartItem::setLayout(points); |
|
90 | XYChartItem::setLayout(points); | |
97 | return; |
|
91 | return; | |
98 | } |
|
92 | } | |
99 |
|
93 | |||
100 | int diff = XYChartItem::points().size() - points.size(); |
|
94 | int diff = XYChartItem::points().size() - points.size(); | |
101 |
|
95 | |||
102 | if(diff>0) { |
|
96 | if(diff>0) { | |
103 | deletePoints(diff); |
|
97 | deletePoints(diff); | |
104 | } |
|
98 | } | |
105 | else if(diff<0) { |
|
99 | else if(diff<0) { | |
106 | createPoints(-diff); |
|
100 | createPoints(-diff); | |
107 | } |
|
101 | } | |
108 |
|
102 | |||
109 | if(diff!=0) handleUpdated(); |
|
103 | if(diff!=0) handleUpdated(); | |
110 |
|
104 | |||
111 | QList<QGraphicsItem*> items = m_items.childItems(); |
|
105 | QList<QGraphicsItem*> items = m_items.childItems(); | |
112 |
|
106 | |||
113 | for(int i=0; i< points.size();i++) { |
|
107 | for(int i=0; i< points.size();i++) { | |
114 | Marker* item = static_cast<Marker*>(items.at(i)); |
|
108 | Marker* item = static_cast<Marker*>(items.at(i)); | |
115 | const QPointF& point = points.at(i); |
|
109 | const QPointF& point = points.at(i); | |
116 | const QRectF& rect = item->boundingRect(); |
|
110 | const QRectF& rect = item->boundingRect(); | |
117 | item->setIndex(i); |
|
111 | item->setIndex(i); | |
118 | item->setPos(point.x()-rect.width()/2,point.y()-rect.height()/2); |
|
112 | item->setPos(point.x()-rect.width()/2,point.y()-rect.height()/2); | |
119 | if(!clipRect().contains(point)) { |
|
113 | if(!clipRect().contains(point)) { | |
120 | item->setVisible(false); |
|
114 | item->setVisible(false); | |
121 | } |
|
115 | } | |
122 | else { |
|
116 | else { | |
123 | item->setVisible(true); |
|
117 | item->setVisible(true); | |
124 | } |
|
118 | } | |
125 | } |
|
119 | } | |
126 |
|
120 | |||
127 | prepareGeometryChange(); |
|
121 | prepareGeometryChange(); | |
128 | m_rect = clipRect(); |
|
122 | m_rect = clipRect(); | |
129 | XYChartItem::setLayout(points); |
|
123 | XYChartItem::setLayout(points); | |
130 | } |
|
124 | } | |
131 |
|
125 | |||
132 |
|
126 | |||
133 | void ScatterChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
127 | void ScatterChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | |
134 | { |
|
128 | { | |
135 | Q_UNUSED(painter) |
|
129 | Q_UNUSED(painter) | |
136 | Q_UNUSED(option) |
|
130 | Q_UNUSED(option) | |
137 | Q_UNUSED(widget) |
|
131 | Q_UNUSED(widget) | |
138 | } |
|
132 | } | |
139 |
|
133 | |||
140 | void ScatterChartItem::setPen(const QPen& pen) |
|
134 | void ScatterChartItem::setPen(const QPen& pen) | |
141 | { |
|
135 | { | |
142 | foreach(QGraphicsItem* item , m_items.childItems()) { |
|
136 | foreach(QGraphicsItem* item , m_items.childItems()) { | |
143 | static_cast<Marker*>(item)->setPen(pen); |
|
137 | static_cast<Marker*>(item)->setPen(pen); | |
144 | } |
|
138 | } | |
145 | } |
|
139 | } | |
146 |
|
140 | |||
147 | void ScatterChartItem::setBrush(const QBrush& brush) |
|
141 | void ScatterChartItem::setBrush(const QBrush& brush) | |
148 | { |
|
142 | { | |
149 | foreach(QGraphicsItem* item , m_items.childItems()) { |
|
143 | foreach(QGraphicsItem* item , m_items.childItems()) { | |
150 | static_cast<Marker*>(item)->setBrush(brush); |
|
144 | static_cast<Marker*>(item)->setBrush(brush); | |
151 | } |
|
145 | } | |
152 | } |
|
146 | } | |
153 |
|
147 | |||
154 | void ScatterChartItem::handleUpdated() |
|
148 | void ScatterChartItem::handleUpdated() | |
155 | { |
|
149 | { | |
156 |
|
150 | |||
157 | int count = m_items.childItems().count(); |
|
151 | int count = m_items.childItems().count(); | |
158 |
|
152 | |||
159 | if(count==0) return; |
|
153 | if(count==0) return; | |
160 |
|
154 | |||
161 | bool recreate = m_size != m_series->size() || m_shape != m_series->shape(); |
|
155 | bool recreate = m_size != m_series->size() || m_shape != m_series->shape(); | |
162 |
|
156 | |||
163 | //TODO: only rewrite on size change |
|
157 | //TODO: only rewrite on size change | |
164 |
|
158 | |||
165 | m_size = m_series->size(); |
|
159 | m_size = m_series->size(); | |
166 | m_shape = m_series->shape(); |
|
160 | m_shape = m_series->shape(); | |
167 |
|
161 | |||
168 | if(recreate){ |
|
162 | if(recreate){ | |
169 | deletePoints(count); |
|
163 | deletePoints(count); | |
170 | createPoints(count); |
|
164 | createPoints(count); | |
171 | } |
|
165 | } | |
172 |
|
166 | |||
173 | setPen(m_series->pen()); |
|
167 | setPen(m_series->pen()); | |
174 | setBrush(m_series->brush()); |
|
168 | setBrush(m_series->brush()); | |
175 | } |
|
169 | } | |
176 |
|
170 | |||
177 | #include "moc_scatterchartitem_p.cpp" |
|
171 | #include "moc_scatterchartitem_p.cpp" | |
178 |
|
172 | |||
179 | QTCOMMERCIALCHART_END_NAMESPACE |
|
173 | QTCOMMERCIALCHART_END_NAMESPACE |
General Comments 0
You need to be logged in to leave comments.
Login now