@@ -1,62 +1,74 | |||||
1 | #include "bar_p.h" |
|
1 | #include "bar_p.h" | |
2 | #include <QDebug> |
|
2 | #include <QDebug> | |
3 | #include <QPainter> |
|
3 | #include <QPainter> | |
4 |
|
4 | |||
5 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
5 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
6 |
|
6 | |||
7 | Bar::Bar(QGraphicsItem *parent) |
|
7 | Bar::Bar(QGraphicsItem *parent) | |
8 | : ChartItem(parent) |
|
8 | : ChartItem(parent) | |
9 | { |
|
9 | { | |
10 | } |
|
10 | } | |
11 |
|
11 | |||
12 | void Bar::setSize(const QSizeF& size) |
|
12 | void Bar::setSize(const QSizeF& size) | |
13 | { |
|
13 | { | |
14 | mWidth = size.width(); |
|
14 | mWidth = size.width(); | |
15 | mHeight = size.height(); |
|
15 | mHeight = size.height(); | |
16 | } |
|
16 | } | |
17 |
|
17 | |||
18 |
|
18 | |||
19 | void Bar::resize( qreal w, qreal h ) |
|
19 | void Bar::resize( qreal w, qreal h ) | |
20 | { |
|
20 | { | |
21 | // qDebug() << "bar::resize" << w << h; |
|
21 | // qDebug() << "bar::resize" << w << h; | |
22 | mWidth = w; |
|
22 | mWidth = w; | |
23 | mHeight = h; |
|
23 | mHeight = h; | |
24 | } |
|
24 | } | |
25 |
|
25 | |||
26 | void Bar::setColor( QColor col ) |
|
26 | void Bar::setColor( QColor col ) | |
27 | { |
|
27 | { | |
28 | mColor = col; |
|
28 | mColor = col; | |
29 | } |
|
29 | } | |
|
30 | ||||
30 | void Bar::setPos(qreal x, qreal y) |
|
31 | void Bar::setPos(qreal x, qreal y) | |
31 | { |
|
32 | { | |
32 | // qDebug() << "Bar::setpos" << x << y; |
|
33 | // qDebug() << "Bar::setpos" << x << y; | |
33 | mXpos = x; |
|
34 | mXpos = x; | |
34 | mYpos = y; |
|
35 | mYpos = y; | |
35 | } |
|
36 | } | |
36 |
|
37 | |||
|
38 | void Bar::setPen(QPen pen) | |||
|
39 | { | |||
|
40 | mPen = pen; | |||
|
41 | } | |||
|
42 | ||||
|
43 | void Bar::setBrush(QBrush brush) | |||
|
44 | { | |||
|
45 | mBrush = brush; | |||
|
46 | } | |||
|
47 | ||||
37 | void Bar::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
48 | void Bar::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | |
38 | { |
|
49 | { | |
39 | if (0 == mHeight) { |
|
50 | if (0 == mHeight) { | |
40 | return; |
|
51 | return; | |
41 | } |
|
52 | } | |
42 | // TODO: accept brush instead of color |
|
53 | // TODO: accept brush instead of color | |
43 | QBrush brush(mColor); |
|
54 | painter->setBrush(mBrush); | |
44 | painter->setBrush(brush); |
|
55 | // QBrush brush(mColor); | |
|
56 | // painter->setBrush(brush); | |||
45 |
|
57 | |||
46 | // This compensates for rounding errors. drawRect takes ints and cumulative error of pos + size may be over 1. |
|
58 | // This compensates for rounding errors. drawRect takes ints and cumulative error of pos + size may be over 1. | |
47 | int x0 = mXpos; |
|
59 | int x0 = mXpos; | |
48 | int x1 = (mXpos + mWidth); |
|
60 | int x1 = (mXpos + mWidth); | |
49 | int w = x1-x0; |
|
61 | int w = x1-x0; | |
50 | int y0 = mYpos; |
|
62 | int y0 = mYpos; | |
51 | int y1 = (mYpos + mHeight); |
|
63 | int y1 = (mYpos + mHeight); | |
52 | int h = y1-y0; |
|
64 | int h = y1-y0; | |
53 | painter->drawRect(x0, y0 ,w ,h); |
|
65 | painter->drawRect(x0, y0 ,w ,h); | |
54 | } |
|
66 | } | |
55 |
|
67 | |||
56 | QRectF Bar::boundingRect() const |
|
68 | QRectF Bar::boundingRect() const | |
57 | { |
|
69 | { | |
58 | QRectF r(mXpos, mYpos, mXpos + mWidth, mYpos + mHeight); |
|
70 | QRectF r(mXpos, mYpos, mXpos + mWidth, mYpos + mHeight); | |
59 | return r; |
|
71 | return r; | |
60 | } |
|
72 | } | |
61 |
|
73 | |||
62 | QTCOMMERCIALCHART_END_NAMESPACE |
|
74 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,42 +1,49 | |||||
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 "chartitem_p.h" | |
5 | #include "qchartglobal.h" |
|
5 | #include "qchartglobal.h" | |
6 | #include <QGraphicsItem> |
|
6 | #include <QGraphicsItem> | |
|
7 | #include <QPen> | |||
|
8 | #include <QBrush> | |||
7 |
|
9 | |||
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
10 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
9 |
|
11 | |||
10 | // Single bar item of chart |
|
12 | // Single bar item of chart | |
11 | class Bar : public ChartItem |
|
13 | class Bar : public ChartItem | |
12 | { |
|
14 | { | |
13 | public: |
|
15 | public: | |
14 | Bar(QGraphicsItem *parent=0); |
|
16 | Bar(QGraphicsItem *parent=0); | |
15 |
|
17 | |||
16 | public: // from ChartItem |
|
18 | public: // from ChartItem | |
17 | void setSize(const QSizeF &size); |
|
19 | void setSize(const QSizeF &size); | |
18 |
|
20 | |||
19 | // Layout Stuff |
|
21 | // Layout Stuff | |
20 |
void resize( |
|
22 | void resize(qreal w, qreal h); // Size of bar. | |
21 | void setColor( QColor col ); // Color of bar |
|
|||
22 | void setPos(qreal x, qreal y); |
|
23 | void setPos(qreal x, qreal y); | |
|
24 | void setPen(QPen pen); | |||
|
25 | void setBrush(QBrush brush); | |||
|
26 | void setColor( QColor col); // deprecated | |||
23 |
|
27 | |||
24 | public: |
|
28 | public: | |
25 | // From QGraphicsItem |
|
29 | // From QGraphicsItem | |
26 |
|
30 | |||
27 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0); |
|
31 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0); | |
28 | QRectF boundingRect() const; |
|
32 | QRectF boundingRect() const; | |
29 |
|
33 | |||
30 | private: |
|
34 | private: | |
31 |
|
35 | |||
32 | qreal mHeight; |
|
36 | qreal mHeight; | |
33 | qreal mWidth; |
|
37 | qreal mWidth; | |
34 | qreal mXpos; |
|
38 | qreal mXpos; | |
35 | qreal mYpos; |
|
39 | qreal mYpos; | |
36 | QColor mColor; |
|
40 | QColor mColor; | |
37 |
|
41 | |||
|
42 | QBrush mBrush; | |||
|
43 | QPen mPen; | |||
|
44 | ||||
38 | }; |
|
45 | }; | |
39 |
|
46 | |||
40 | QTCOMMERCIALCHART_END_NAMESPACE |
|
47 | QTCOMMERCIALCHART_END_NAMESPACE | |
41 |
|
48 | |||
42 | #endif // BAR_H |
|
49 | #endif // BAR_H |
@@ -1,70 +1,73 | |||||
1 | #include "bargroup.h" |
|
1 | #include "bargroup.h" | |
2 | #include "bar_p.h" |
|
2 | #include "bar_p.h" | |
3 | #include "barlabel_p.h" |
|
3 | #include "barlabel_p.h" | |
4 | #include <QDebug> |
|
4 | #include <QDebug> | |
5 |
|
5 | |||
6 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
6 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
7 |
|
7 | |||
8 | BarGroup::BarGroup(BarChartModel& model, QGraphicsItem *parent) : |
|
8 | BarGroup::BarGroup(BarChartModel& model, QGraphicsItem *parent) : | |
9 | BarGroupBase(model,parent) |
|
9 | BarGroupBase(model,parent) | |
10 | { |
|
10 | { | |
11 | mBarDefaultWidth = 5; |
|
11 | mBarDefaultWidth = 5; | |
12 | } |
|
12 | } | |
13 |
|
13 | |||
14 | void BarGroup::layoutChanged() |
|
14 | void BarGroup::layoutChanged() | |
15 | { |
|
15 | { | |
16 | // qDebug() << "BarGroup::layoutChanged"; |
|
16 | // qDebug() << "BarGroup::layoutChanged"; | |
17 | // Scale bars to new layout |
|
17 | // Scale bars to new layout | |
18 | // Layout for bars: |
|
18 | // Layout for bars: | |
19 | if (mModel.countSets() <= 0) { |
|
19 | if (mModel.countSets() <= 0) { | |
20 | qDebug() << "No sets in model!"; |
|
20 | qDebug() << "No sets in model!"; | |
21 | return; |
|
21 | return; | |
22 | } |
|
22 | } | |
23 |
|
23 | |||
24 | if (childItems().count() == 0) { |
|
24 | if (childItems().count() == 0) { | |
25 | qDebug() << "WARNING: BarGroup::layoutChanged called before graphics items are created!"; |
|
25 | qDebug() << "WARNING: BarGroup::layoutChanged called before graphics items are created!"; | |
26 | return; |
|
26 | return; | |
27 | } |
|
27 | } | |
28 |
|
28 | |||
29 | // TODO: better way to auto-layout? |
|
29 | // TODO: better way to auto-layout? | |
30 | // Use reals for accurancy (we might get some compiler warnings... :) |
|
30 | // Use reals for accurancy (we might get some compiler warnings... :) | |
31 | int itemCount = mModel.countCategories(); |
|
31 | int itemCount = mModel.countCategories(); | |
32 |
int se |
|
32 | int setCount = mModel.countSets(); | |
33 |
|
33 | |||
34 | qreal tW = mWidth; |
|
34 | qreal tW = mWidth; | |
35 | qreal tH = mHeight; |
|
35 | qreal tH = mHeight; | |
36 | qreal tM = mModel.max(); |
|
36 | qreal tM = mModel.max(); | |
37 | qreal scale = (tH/tM); |
|
37 | qreal scale = (tH/tM); | |
38 | qreal tC = itemCount+1; |
|
38 | qreal tC = itemCount+1; | |
39 |
qreal xStepPerSe |
|
39 | qreal xStepPerSet = (tW/tC); | |
40 |
|
40 | |||
41 | // Scaling. |
|
41 | // Scaling. | |
42 | int itemIndex(0); |
|
42 | int itemIndex(0); | |
43 |
int labelIndex = itemCount * se |
|
43 | int labelIndex = itemCount * setCount; | |
44 |
|
44 | |||
45 | for (int item=0; item < itemCount; item++) { |
|
45 | for (int item=0; item < itemCount; item++) { | |
46 |
qreal xPos = xStepPerSe |
|
46 | qreal xPos = xStepPerSet * item + ((tW + mBarDefaultWidth*setCount)/(itemCount*2)); | |
47 | qreal yPos = mHeight; |
|
47 | qreal yPos = mHeight; | |
48 |
for (int se |
|
48 | for (int set = 0; set < setCount; set++) { | |
49 |
qreal barHeight = mModel.valueAt(se |
|
49 | qreal barHeight = mModel.valueAt(set, item) * scale; | |
50 | Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex)); |
|
50 | Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex)); | |
51 |
|
51 | |||
52 | // TODO: width settable per bar? |
|
52 | // TODO: width settable per bar? | |
53 | bar->resize(mBarDefaultWidth, barHeight); |
|
53 | bar->resize(mBarDefaultWidth, barHeight); | |
54 |
bar->set |
|
54 | bar->setBrush(mBrushes.at(set)); | |
55 | bar->setPos(xPos, yPos-barHeight); // item*posStep+startPos + series * mBarDefaultWidth, mHeight); |
|
55 | // bar->setPen(mModel.barSet(set).pen()); | |
|
56 | // bar->setColor(mColors.at(set)); | |||
|
57 | // bar->setPen(); | |||
|
58 | bar->setPos(xPos, yPos-barHeight); // item*posStep+startPos + set * mBarDefaultWidth, mHeight); | |||
56 | itemIndex++; |
|
59 | itemIndex++; | |
57 | xPos += mBarDefaultWidth; |
|
60 | xPos += mBarDefaultWidth; | |
58 | } |
|
61 | } | |
59 |
|
62 | |||
60 | // TODO: Layout for labels, remove magic number |
|
63 | // TODO: Layout for labels, remove magic number | |
61 |
xPos = xStepPerSe |
|
64 | xPos = xStepPerSet * item + ((tW + mBarDefaultWidth*setCount)/(itemCount*2)); | |
62 | BarLabel* label = reinterpret_cast<BarLabel*> (childItems().at(labelIndex)); |
|
65 | BarLabel* label = reinterpret_cast<BarLabel*> (childItems().at(labelIndex)); | |
63 | label->setPos(xPos, mHeight + 20); |
|
66 | label->setPos(xPos, mHeight + 20); | |
64 | labelIndex++; |
|
67 | labelIndex++; | |
65 | } |
|
68 | } | |
66 |
|
69 | |||
67 | mLayoutDirty = true; |
|
70 | mLayoutDirty = true; | |
68 | } |
|
71 | } | |
69 |
|
72 | |||
70 | QTCOMMERCIALCHART_END_NAMESPACE |
|
73 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,125 +1,137 | |||||
1 | #include "bargroupbase.h" |
|
1 | #include "bargroupbase.h" | |
2 | #include "bar_p.h" |
|
2 | #include "bar_p.h" | |
3 | #include "barlabel_p.h" |
|
3 | #include "barlabel_p.h" | |
4 | #include "separator_p.h" |
|
4 | #include "separator_p.h" | |
5 | #include <QDebug> |
|
5 | #include <QDebug> | |
6 |
|
6 | |||
7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
8 |
|
8 | |||
9 | BarGroupBase::BarGroupBase(BarChartModel& model, QGraphicsItem *parent) |
|
9 | BarGroupBase::BarGroupBase(BarChartModel& model, QGraphicsItem *parent) | |
10 | : ChartItem(parent) |
|
10 | : ChartItem(parent) | |
11 | ,mBarDefaultWidth(20) // TODO: remove hard coding, when we have layout code ready |
|
11 | ,mBarDefaultWidth(20) // TODO: remove hard coding, when we have layout code ready | |
12 | ,mLayoutSet(false) |
|
12 | ,mLayoutSet(false) | |
13 | ,mLayoutDirty(true) |
|
13 | ,mLayoutDirty(true) | |
14 | ,mSeparatorsVisible(true) |
|
14 | ,mSeparatorsVisible(true) | |
15 | ,mModel(model) |
|
15 | ,mModel(model) | |
16 | { |
|
16 | { | |
17 | dataChanged(); |
|
17 | dataChanged(); | |
18 | } |
|
18 | } | |
19 |
|
19 | |||
20 | void BarGroupBase::setSeparatorsVisible(bool visible) |
|
20 | void BarGroupBase::setSeparatorsVisible(bool visible) | |
21 | { |
|
21 | { | |
22 | mSeparatorsVisible = visible; |
|
22 | mSeparatorsVisible = visible; | |
23 | } |
|
23 | } | |
24 |
|
24 | |||
25 | void BarGroupBase::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
25 | void BarGroupBase::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | |
26 | { |
|
26 | { | |
27 | // qDebug() << "BarGroupBase::paint" << childItems().count(); |
|
27 | // qDebug() << "BarGroupBase::paint" << childItems().count(); | |
28 | if (!mLayoutSet) { |
|
28 | if (!mLayoutSet) { | |
29 | qDebug() << "BarGroupBase::paint called without layout set. Aborting."; |
|
29 | qDebug() << "BarGroupBase::paint called without layout set. Aborting."; | |
30 | return; |
|
30 | return; | |
31 | } |
|
31 | } | |
32 | // if (mLayoutDirty) { |
|
32 | // if (mLayoutDirty) { | |
33 | // Layout or data has changed. Need to redraw. |
|
33 | // Layout or data has changed. Need to redraw. | |
34 | foreach(QGraphicsItem* i, childItems()) { |
|
34 | foreach(QGraphicsItem* i, childItems()) { | |
35 | i->paint(painter,option,widget); |
|
35 | i->paint(painter,option,widget); | |
36 | } |
|
36 | } | |
37 | // } |
|
37 | // } | |
38 | } |
|
38 | } | |
39 |
|
39 | |||
40 | QRectF BarGroupBase::boundingRect() const |
|
40 | QRectF BarGroupBase::boundingRect() const | |
41 | { |
|
41 | { | |
42 | return QRectF(0,0,mWidth,mHeight); |
|
42 | return QRectF(0,0,mWidth,mHeight); | |
43 | } |
|
43 | } | |
44 |
|
44 | |||
45 | void BarGroupBase::setBarWidth( int w ) |
|
45 | void BarGroupBase::setBarWidth( int w ) | |
46 | { |
|
46 | { | |
47 | mBarDefaultWidth = w; |
|
47 | mBarDefaultWidth = w; | |
48 | } |
|
48 | } | |
49 |
|
49 | /* | ||
50 | int BarGroupBase::addColor( QColor color ) |
|
50 | int BarGroupBase::addColor( QColor color ) | |
51 |
|
|
51 | { | |
52 |
|
|
52 | // qDebug() << "BarGroupBase::addColor"; | |
53 | int colorIndex = mColors.count(); |
|
53 | int colorIndex = mColors.count(); | |
54 | mColors.append(color); |
|
54 | mColors.append(color); | |
55 | return colorIndex; |
|
55 | return colorIndex; | |
56 |
|
|
56 | } | |
57 |
|
57 | */ | ||
|
58 | /* | |||
58 | void BarGroupBase::resetColors() |
|
59 | void BarGroupBase::resetColors() | |
59 |
|
|
60 | { | |
60 |
|
|
61 | // qDebug() << "BarGroupBase::resetColors"; | |
61 | mColors.clear(); |
|
62 | mColors.clear(); | |
62 |
|
|
63 | } | |
|
64 | */ | |||
|
65 | void BarGroupBase::resetBrushes() | |||
|
66 | { | |||
|
67 | mBrushes.clear(); | |||
|
68 | } | |||
|
69 | ||||
|
70 | void BarGroupBase::addBrush(QBrush brush) | |||
|
71 | { | |||
|
72 | mBrushes.append(brush); | |||
|
73 | } | |||
|
74 | ||||
63 |
|
75 | |||
64 | void BarGroupBase::dataChanged() |
|
76 | void BarGroupBase::dataChanged() | |
65 | { |
|
77 | { | |
66 | // TODO: performance optimizations. Do we really need to delete and create items every time data is changed or can we reuse them? |
|
78 | // TODO: performance optimizations. Do we really need to delete and create items every time data is changed or can we reuse them? | |
67 |
|
79 | |||
68 | // Delete old bars |
|
80 | // Delete old bars | |
69 | foreach (QGraphicsItem* item, childItems()) { |
|
81 | foreach (QGraphicsItem* item, childItems()) { | |
70 | delete item; |
|
82 | delete item; | |
71 | } |
|
83 | } | |
72 |
|
84 | |||
73 | // Create new graphic items for bars |
|
85 | // Create new graphic items for bars | |
74 | int totalItems = mModel.countTotalItems(); |
|
86 | int totalItems = mModel.countTotalItems(); | |
75 | for (int i=0; i<totalItems; i++) { |
|
87 | for (int i=0; i<totalItems; i++) { | |
76 | Bar *bar = new Bar(this); |
|
88 | Bar *bar = new Bar(this); | |
77 | childItems().append(bar); |
|
89 | childItems().append(bar); | |
78 | } |
|
90 | } | |
79 |
|
91 | |||
80 | int count = mModel.countCategories(); |
|
92 | int count = mModel.countCategories(); | |
81 | for (int i=0; i<count; i++) { |
|
93 | for (int i=0; i<count; i++) { | |
82 | BarLabel* label = new BarLabel(this); |
|
94 | BarLabel* label = new BarLabel(this); | |
83 | label->set(mModel.label(i)); |
|
95 | label->set(mModel.label(i)); | |
84 | childItems().append(label); |
|
96 | childItems().append(label); | |
85 | } |
|
97 | } | |
86 |
|
98 | |||
87 | count = mModel.countCategories() - 1; // There is one less separator than columns |
|
99 | count = mModel.countCategories() - 1; // There is one less separator than columns | |
88 | for (int i=0; i<count; i++) { |
|
100 | for (int i=0; i<count; i++) { | |
89 | Separator* sep = new Separator(this); |
|
101 | Separator* sep = new Separator(this); | |
90 | sep->setColor(QColor(255,0,0,255)); // TODO: color for separations from theme |
|
102 | sep->setColor(QColor(255,0,0,255)); // TODO: color for separations from theme | |
91 | childItems().append(sep); |
|
103 | childItems().append(sep); | |
92 | } |
|
104 | } | |
93 |
|
105 | |||
94 | // TODO: if (autolayout) { layoutChanged() } or something |
|
106 | // TODO: if (autolayout) { layoutChanged() } or something | |
95 | mLayoutDirty = true; |
|
107 | mLayoutDirty = true; | |
96 | } |
|
108 | } | |
97 |
|
109 | |||
98 | //handlers |
|
110 | //handlers | |
99 |
|
111 | |||
100 | void BarGroupBase::handleModelChanged(int index) |
|
112 | void BarGroupBase::handleModelChanged(int index) | |
101 | { |
|
113 | { | |
102 | // qDebug() << "BarGroupBase::handleModelChanged" << index; |
|
114 | // qDebug() << "BarGroupBase::handleModelChanged" << index; | |
103 | dataChanged(); |
|
115 | dataChanged(); | |
104 | } |
|
116 | } | |
105 |
|
117 | |||
106 | void BarGroupBase::handleDomainChanged(const Domain& domain) |
|
118 | void BarGroupBase::handleDomainChanged(const Domain& domain) | |
107 | { |
|
119 | { | |
108 | // qDebug() << "BarGroupBase::handleDomainChanged"; |
|
120 | // qDebug() << "BarGroupBase::handleDomainChanged"; | |
109 | // TODO: Figure out the use case for this. |
|
121 | // TODO: Figure out the use case for this. | |
110 | // Affects the size of visible item, so layout is changed. |
|
122 | // Affects the size of visible item, so layout is changed. | |
111 | // layoutChanged(); |
|
123 | // layoutChanged(); | |
112 | } |
|
124 | } | |
113 |
|
125 | |||
114 | void BarGroupBase::handleGeometryChanged(const QRectF& rect) |
|
126 | void BarGroupBase::handleGeometryChanged(const QRectF& rect) | |
115 | { |
|
127 | { | |
116 | mWidth = rect.width(); |
|
128 | mWidth = rect.width(); | |
117 | mHeight = rect.height(); |
|
129 | mHeight = rect.height(); | |
118 | layoutChanged(); |
|
130 | layoutChanged(); | |
119 | mLayoutSet = true; |
|
131 | mLayoutSet = true; | |
120 | setPos(rect.topLeft()); |
|
132 | setPos(rect.topLeft()); | |
121 | } |
|
133 | } | |
122 |
|
134 | |||
123 | #include "moc_bargroupbase.cpp" |
|
135 | #include "moc_bargroupbase.cpp" | |
124 |
|
136 | |||
125 | QTCOMMERCIALCHART_END_NAMESPACE |
|
137 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,56 +1,70 | |||||
1 | #ifndef BARGROUPBASE_H |
|
1 | #ifndef BARGROUPBASE_H | |
2 | #define BARGROUPBASE_H |
|
2 | #define BARGROUPBASE_H | |
3 |
|
3 | |||
4 | #include "chartitem_p.h" |
|
4 | #include "chartitem_p.h" | |
5 | #include "barchartmodel_p.h" |
|
5 | #include "barchartmodel_p.h" | |
|
6 | #include <QPen> | |||
|
7 | #include <QBrush> | |||
6 | #include <QGraphicsItem> |
|
8 | #include <QGraphicsItem> | |
7 |
|
9 | |||
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
10 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
9 |
|
11 | |||
10 | // Base Class for bar groups. Common implemantation of different groups. Not to be instantiated. |
|
12 | // Base Class for bar groups. Common implemantation of different groups. Not to be instantiated. | |
11 | class BarGroupBase : public QObject, public ChartItem |
|
13 | class BarGroupBase : public QObject, public ChartItem | |
12 | { |
|
14 | { | |
13 | Q_OBJECT |
|
15 | Q_OBJECT | |
14 | public: |
|
16 | public: | |
15 | BarGroupBase(BarChartModel& model, QGraphicsItem *parent = 0); |
|
17 | BarGroupBase(BarChartModel& model, QGraphicsItem *parent = 0); | |
16 | void setSeparatorsVisible(bool visible = true); |
|
18 | void setSeparatorsVisible(bool visible = true); | |
17 |
|
19 | |||
18 | public: |
|
20 | public: | |
19 |
|
21 | |||
20 | // From QGraphicsItem |
|
22 | // From QGraphicsItem | |
21 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); |
|
23 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); | |
22 | QRectF boundingRect() const; |
|
24 | QRectF boundingRect() const; | |
23 |
|
25 | |||
24 | // TODO: these may change with layout awarness. |
|
26 | // TODO: these may change with layout awarness. | |
25 | void setBarWidth( int w ); |
|
27 | void setBarWidth( int w ); | |
26 | int addColor( QColor color ); |
|
28 | // int addColor( QColor color ); | |
27 | void resetColors(); |
|
29 | // void resetColors(); | |
|
30 | ||||
|
31 | void resetBrushes(); | |||
|
32 | void addBrush(QBrush brush); | |||
|
33 | ||||
|
34 | void setPen(QPen pen); | |||
|
35 | QPen pen(); | |||
|
36 | ||||
|
37 | void setBrush(QBrush brush); | |||
|
38 | QBrush brush(); | |||
|
39 | ||||
28 |
|
40 | |||
29 | // TODO: Consider the domain for layoutChanged. May be use case, may not be. If it is, then the derived classes need to implement it |
|
41 | // TODO: Consider the domain for layoutChanged. May be use case, may not be. If it is, then the derived classes need to implement it | |
30 | virtual void dataChanged(); // data of series has changed -> need to recalculate bar sizes |
|
42 | virtual void dataChanged(); // data of series has changed -> need to recalculate bar sizes | |
31 | virtual void layoutChanged() = 0; // layout has changed -> need to recalculate bar sizes |
|
43 | virtual void layoutChanged() = 0; // layout has changed -> need to recalculate bar sizes | |
32 |
|
44 | |||
33 | protected slots: |
|
45 | protected slots: | |
34 | void handleModelChanged(int index); |
|
46 | void handleModelChanged(int index); | |
35 | void handleDomainChanged(const Domain& domain); |
|
47 | void handleDomainChanged(const Domain& domain); | |
36 | void handleGeometryChanged(const QRectF& size); |
|
48 | void handleGeometryChanged(const QRectF& size); | |
37 |
|
49 | |||
38 | protected: |
|
50 | protected: | |
39 |
|
51 | |||
40 | // TODO: consider these. |
|
52 | // TODO: consider these. | |
41 | int mHeight; // Layout spesific |
|
53 | int mHeight; // Layout spesific | |
42 | int mWidth; |
|
54 | int mWidth; | |
43 | int mBarDefaultWidth; |
|
55 | int mBarDefaultWidth; | |
44 |
|
56 | |||
45 | bool mLayoutSet; // True, if component has been laid out. |
|
57 | bool mLayoutSet; // True, if component has been laid out. | |
46 | bool mLayoutDirty; |
|
58 | bool mLayoutDirty; | |
47 |
|
59 | |||
48 | QList<QColor> mColors; // List of colors for series for now |
|
60 | QList<QColor> mColors; // List of colors for series for now | |
49 | bool mSeparatorsVisible; |
|
61 | bool mSeparatorsVisible; | |
50 | BarChartModel& mModel; |
|
62 | BarChartModel& mModel; | |
51 |
|
63 | |||
|
64 | QPen mPen; | |||
|
65 | QList<QBrush> mBrushes; | |||
52 | }; |
|
66 | }; | |
53 |
|
67 | |||
54 | QTCOMMERCIALCHART_END_NAMESPACE |
|
68 | QTCOMMERCIALCHART_END_NAMESPACE | |
55 |
|
69 | |||
56 | #endif // BARGROUPBASE_H |
|
70 | #endif // BARGROUPBASE_H |
@@ -1,79 +1,81 | |||||
1 | #include "percentbargroup.h" |
|
1 | #include "percentbargroup.h" | |
2 | #include "bar_p.h" |
|
2 | #include "bar_p.h" | |
3 | #include "barlabel_p.h" |
|
3 | #include "barlabel_p.h" | |
4 | #include "separator_p.h" |
|
4 | #include "separator_p.h" | |
5 | #include <QDebug> |
|
5 | #include <QDebug> | |
6 |
|
6 | |||
7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
8 |
|
8 | |||
9 |
|
9 | |||
10 | PercentBarGroup::PercentBarGroup(BarChartModel& model, QGraphicsItem *parent) : |
|
10 | PercentBarGroup::PercentBarGroup(BarChartModel& model, QGraphicsItem *parent) : | |
11 | BarGroupBase(model, parent) |
|
11 | BarGroupBase(model, parent) | |
12 | { |
|
12 | { | |
13 | } |
|
13 | } | |
14 |
|
14 | |||
15 | void PercentBarGroup::layoutChanged() |
|
15 | void PercentBarGroup::layoutChanged() | |
16 | { |
|
16 | { | |
17 | // qDebug() << "PercentBarGroup::layoutChanged"; |
|
17 | // qDebug() << "PercentBarGroup::layoutChanged"; | |
18 | // Scale bars to new layout |
|
18 | // Scale bars to new layout | |
19 | // Layout for bars: |
|
19 | // Layout for bars: | |
20 | if (mModel.countSets() <= 0) { |
|
20 | if (mModel.countSets() <= 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: PercentBarGroup::layoutChanged called before graphics items are created!"; |
|
27 | qDebug() << "WARNING: PercentBarGroup::layoutChanged called before graphics items are created!"; | |
28 | return; |
|
28 | return; | |
29 | } |
|
29 | } | |
30 |
|
30 | |||
31 | // TODO: better way to auto-layout |
|
31 | // TODO: better way to auto-layout | |
32 | // Use reals for accurancy (we might get some compiler warnings... :) |
|
32 | // Use reals for accurancy (we might get some compiler warnings... :) | |
33 | int count = mModel.countCategories(); |
|
33 | int count = mModel.countCategories(); | |
34 | int itemIndex(0); |
|
34 | int itemIndex(0); | |
35 | qreal tW = mWidth; |
|
35 | qreal tW = mWidth; | |
36 | qreal tC = count+1; |
|
36 | qreal tC = count+1; | |
37 | qreal xStep = (tW/tC); |
|
37 | qreal xStep = (tW/tC); | |
38 | qreal xPos = ((tW/tC) - mBarDefaultWidth / 2); |
|
38 | qreal xPos = ((tW/tC) - mBarDefaultWidth / 2); | |
39 | int labelIndex = mModel.countCategories() * mModel.countSets(); |
|
39 | int labelIndex = mModel.countCategories() * mModel.countSets(); | |
40 |
|
40 | |||
41 |
for (int c |
|
41 | for (int category = 0; category < mModel.countCategories(); category++) { | |
42 |
qreal colSum = mModel.categorySum(c |
|
42 | qreal colSum = mModel.categorySum(category); | |
43 | qreal h = mHeight; |
|
43 | qreal h = mHeight; | |
44 | qreal scale = (h / colSum); |
|
44 | qreal scale = (h / colSum); | |
45 | qreal yPos = h; |
|
45 | qreal yPos = h; | |
46 |
for (int |
|
46 | for (int set=0; set < mModel.countSets(); set++) { | |
47 |
qreal barHeight = mModel.valueAt( |
|
47 | qreal barHeight = mModel.valueAt(set, category) * scale; | |
48 | Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex)); |
|
48 | Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex)); | |
49 |
|
49 | |||
50 | // TODO: width settable per bar? |
|
50 | // TODO: width settable per bar? | |
51 | bar->resize(mBarDefaultWidth, barHeight); |
|
51 | bar->resize(mBarDefaultWidth, barHeight); | |
52 |
bar->set |
|
52 | bar->setBrush(mBrushes.at(set)); | |
|
53 | // bar->setBrush(mBrush); | |||
|
54 | // bar->setColor(mColors.at(set)); | |||
53 | bar->setPos(xPos, yPos-barHeight); |
|
55 | bar->setPos(xPos, yPos-barHeight); | |
54 | itemIndex++; |
|
56 | itemIndex++; | |
55 | yPos -= barHeight; |
|
57 | yPos -= barHeight; | |
56 | } |
|
58 | } | |
57 |
|
59 | |||
58 | // TODO: Layout for labels, remove magic number |
|
60 | // TODO: Layout for labels, remove magic number | |
59 | BarLabel* label = reinterpret_cast<BarLabel*> (childItems().at(labelIndex)); |
|
61 | BarLabel* label = reinterpret_cast<BarLabel*> (childItems().at(labelIndex)); | |
60 | label->setPos(xPos, mHeight + 20); |
|
62 | label->setPos(xPos, mHeight + 20); | |
61 | labelIndex++; |
|
63 | labelIndex++; | |
62 | xPos += xStep; |
|
64 | xPos += xStep; | |
63 | } |
|
65 | } | |
64 |
|
66 | |||
65 | // Position separators |
|
67 | // Position separators | |
66 | int separatorIndex = labelIndex; // Separators are after labels in childItems(). TODO: better way to store these? |
|
68 | int separatorIndex = labelIndex; // Separators are after labels in childItems(). TODO: better way to store these? | |
67 | xPos = xStep + xStep/2; // Initial position is between first and second group. ie one and half steps from left. |
|
69 | xPos = xStep + xStep/2; // Initial position is between first and second group. ie one and half steps from left. | |
68 | for (int s=0; s < mModel.countCategories() - 1; s++) { |
|
70 | for (int s=0; s < mModel.countCategories() - 1; s++) { | |
69 | Separator* sep = reinterpret_cast<Separator*> (childItems().at(separatorIndex)); |
|
71 | Separator* sep = reinterpret_cast<Separator*> (childItems().at(separatorIndex)); | |
70 | sep->setPos(xPos,0); |
|
72 | sep->setPos(xPos,0); | |
71 | sep->setSize(QSizeF(1,mHeight)); |
|
73 | sep->setSize(QSizeF(1,mHeight)); | |
72 | xPos += xStep; |
|
74 | xPos += xStep; | |
73 | separatorIndex++; |
|
75 | separatorIndex++; | |
74 | } |
|
76 | } | |
75 |
|
77 | |||
76 | mLayoutDirty = true; |
|
78 | mLayoutDirty = true; | |
77 | } |
|
79 | } | |
78 |
|
80 | |||
79 | QTCOMMERCIALCHART_END_NAMESPACE |
|
81 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,36 +1,41 | |||||
1 | #include "qbarset.h" |
|
1 | #include "qbarset.h" | |
2 |
|
2 | |||
3 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
3 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
4 |
|
4 | |||
5 | QBarSet::QBarSet() |
|
5 | QBarSet::QBarSet() | |
6 | { |
|
6 | { | |
7 | } |
|
7 | } | |
8 |
|
8 | |||
9 | void QBarSet::setName(QString name) |
|
9 | void QBarSet::setName(QString name) | |
10 | { |
|
10 | { | |
11 | mName = name; |
|
11 | mName = name; | |
12 | } |
|
12 | } | |
13 | QString QBarSet::name() |
|
13 | QString QBarSet::name() | |
14 | { |
|
14 | { | |
15 | return mName; |
|
15 | return mName; | |
16 | } |
|
16 | } | |
17 |
|
17 | |||
18 | QBarSet& QBarSet::operator << (const qreal &value) |
|
18 | QBarSet& QBarSet::operator << (const qreal &value) | |
19 | { |
|
19 | { | |
20 | mValues.append(value); |
|
20 | mValues.append(value); | |
21 | return *this; |
|
21 | return *this; | |
22 | } |
|
22 | } | |
23 |
|
23 | |||
24 | int QBarSet::count() |
|
24 | int QBarSet::count() | |
25 | { |
|
25 | { | |
26 | return mValues.count(); |
|
26 | return mValues.count(); | |
27 | } |
|
27 | } | |
28 |
|
28 | |||
29 | qreal QBarSet::valueAt(int index) |
|
29 | qreal QBarSet::valueAt(int index) | |
30 | { |
|
30 | { | |
31 | return mValues.at(index); |
|
31 | return mValues.at(index); | |
32 | } |
|
32 | } | |
33 |
|
33 | |||
|
34 | void QBarSet::setValue(int index, qreal value) | |||
|
35 | { | |||
|
36 | mValues.replace(index,value); | |||
|
37 | } | |||
|
38 | ||||
34 | //TODO?: |
|
39 | //TODO?: | |
35 | //#include "moc_qbarset.cpp" |
|
40 | //#include "moc_qbarset.cpp" | |
36 | QTCOMMERCIALCHART_END_NAMESPACE |
|
41 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,32 +1,32 | |||||
1 | #ifndef QBARSET_H |
|
1 | #ifndef QBARSET_H | |
2 | #define QBARSET_H |
|
2 | #define QBARSET_H | |
3 |
|
3 | |||
4 | #include "qchartglobal.h" |
|
4 | #include "qchartglobal.h" | |
|
5 | #include <QPen> | |||
|
6 | #include <QBrush> | |||
5 |
|
7 | |||
6 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
7 |
|
9 | |||
8 | class QTCOMMERCIALCHART_EXPORT QBarSet // TODO? : public QObject |
|
10 | class QTCOMMERCIALCHART_EXPORT QBarSet // TODO? : public QObject | |
9 | { |
|
11 | { | |
10 | //Q_OBJECT; |
|
12 | //Q_OBJECT; | |
11 | public: |
|
13 | public: | |
12 | QBarSet(); |
|
14 | QBarSet(); | |
13 |
|
15 | |||
14 | void setName(QString name); |
|
16 | void setName(QString name); | |
15 | QString name(); |
|
17 | QString name(); | |
16 | QBarSet& operator << (const qreal &value); |
|
18 | QBarSet& operator << (const qreal &value); // appends new value to set | |
17 |
|
19 | |||
18 | //TODO: What is the way to set a single value to n:th item? Is there need for such functionality? |
|
20 | int count(); // count of values in set | |
19 |
|
21 | qreal valueAt(int index); // for modifying individual values | ||
20 | int count(); |
|
22 | void setValue(int index, qreal value); // | |
21 | qreal valueAt(int index); |
|
|||
22 |
|
23 | |||
23 | private: |
|
24 | private: | |
24 |
|
25 | |||
25 | QString mName; |
|
26 | QString mName; | |
26 | QList<qreal> mValues; |
|
27 | QList<qreal> mValues; | |
27 |
|
||||
28 | }; |
|
28 | }; | |
29 |
|
29 | |||
30 | QTCOMMERCIALCHART_END_NAMESPACE |
|
30 | QTCOMMERCIALCHART_END_NAMESPACE | |
31 |
|
31 | |||
32 | #endif // QBARSET_H |
|
32 | #endif // QBARSET_H |
@@ -1,84 +1,86 | |||||
1 | #include "stackedbargroup.h" |
|
1 | #include "stackedbargroup.h" | |
2 | #include "bar_p.h" |
|
2 | #include "bar_p.h" | |
3 | #include "barlabel_p.h" |
|
3 | #include "barlabel_p.h" | |
4 | #include "separator_p.h" |
|
4 | #include "separator_p.h" | |
5 | #include <QDebug> |
|
5 | #include <QDebug> | |
6 |
|
6 | |||
7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
8 |
|
8 | |||
9 | StackedBarGroup::StackedBarGroup(BarChartModel& model, QGraphicsItem *parent) : |
|
9 | StackedBarGroup::StackedBarGroup(BarChartModel& model, QGraphicsItem *parent) : | |
10 | BarGroupBase(model,parent) |
|
10 | BarGroupBase(model,parent) | |
11 | { |
|
11 | { | |
12 | } |
|
12 | } | |
13 |
|
13 | |||
14 | void StackedBarGroup::layoutChanged() |
|
14 | void StackedBarGroup::layoutChanged() | |
15 | { |
|
15 | { | |
16 | // qDebug() << "StackedBarGroup::layoutChanged"; |
|
16 | // qDebug() << "StackedBarGroup::layoutChanged"; | |
17 | // Scale bars to new layout |
|
17 | // Scale bars to new layout | |
18 | // Layout for bars: |
|
18 | // Layout for bars: | |
19 | if (mModel.countSets() <= 0) { |
|
19 | if (mModel.countSets() <= 0) { | |
20 | qDebug() << "No sets in model!"; |
|
20 | qDebug() << "No sets in model!"; | |
21 | // Nothing to do. |
|
21 | // Nothing to do. | |
22 | return; |
|
22 | return; | |
23 | } |
|
23 | } | |
24 |
|
24 | |||
25 | if (mModel.countCategories() == 0) { |
|
25 | if (mModel.countCategories() == 0) { | |
26 | qDebug() << "No categories in model!"; |
|
26 | qDebug() << "No categories in model!"; | |
27 | // Nothing to do |
|
27 | // Nothing to do | |
28 | return; |
|
28 | return; | |
29 | } |
|
29 | } | |
30 |
|
30 | |||
31 | if (childItems().count() == 0) { |
|
31 | if (childItems().count() == 0) { | |
32 | qDebug() << "WARNING: StackedBarGroup::layoutChanged called before graphics items are created!"; |
|
32 | qDebug() << "WARNING: StackedBarGroup::layoutChanged called before graphics items are created!"; | |
33 | return; |
|
33 | return; | |
34 | } |
|
34 | } | |
35 |
|
35 | |||
36 | // TODO: better way to auto-layout |
|
36 | // TODO: better way to auto-layout | |
37 | // Use reals for accurancy (we might get some compiler warnings... :) |
|
37 | // Use reals for accurancy (we might get some compiler warnings... :) | |
38 |
// TODO: use temp variable for c |
|
38 | // TODO: use temp variable for category count... | |
39 | qreal maxSum = mModel.maxCategorySum(); |
|
39 | qreal maxSum = mModel.maxCategorySum(); | |
40 | qreal h = mHeight; |
|
40 | qreal h = mHeight; | |
41 | qreal scale = (h / maxSum); |
|
41 | qreal scale = (h / maxSum); | |
42 |
|
42 | |||
43 | int itemIndex(0); |
|
43 | int itemIndex(0); | |
44 | qreal tW = mWidth; |
|
44 | qreal tW = mWidth; | |
45 | qreal tC = mModel.countCategories() + 1; |
|
45 | qreal tC = mModel.countCategories() + 1; | |
46 | qreal xStep = (tW/tC); |
|
46 | qreal xStep = (tW/tC); | |
47 | qreal xPos = ((tW/tC) - mBarDefaultWidth / 2); |
|
47 | qreal xPos = ((tW/tC) - mBarDefaultWidth / 2); | |
48 | int labelIndex = mModel.countSets() * mModel.countCategories(); |
|
48 | int labelIndex = mModel.countSets() * mModel.countCategories(); | |
49 |
|
49 | |||
50 |
for (int c |
|
50 | for (int category = 0; category < mModel.countCategories(); category++) { | |
51 | qreal yPos = h; |
|
51 | qreal yPos = h; | |
52 |
for (int |
|
52 | for (int set=0; set < mModel.countSets(); set++) { | |
53 |
qreal barHeight = mModel.valueAt( |
|
53 | qreal barHeight = mModel.valueAt(set, category) * scale; | |
54 | Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex)); |
|
54 | Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex)); | |
55 |
|
55 | |||
56 | bar->resize(mBarDefaultWidth, barHeight); |
|
56 | bar->resize(mBarDefaultWidth, barHeight); | |
57 |
bar->set |
|
57 | bar->setBrush(mBrushes.at(set)); | |
|
58 | // bar->setBrush(mBrush); | |||
|
59 | // bar->setColor(mColors.at(set)); | |||
58 | bar->setPos(xPos, yPos-barHeight); |
|
60 | bar->setPos(xPos, yPos-barHeight); | |
59 | itemIndex++; |
|
61 | itemIndex++; | |
60 | yPos -= barHeight; |
|
62 | yPos -= barHeight; | |
61 | } |
|
63 | } | |
62 |
|
64 | |||
63 | // TODO: Layout for labels, remove magic number |
|
65 | // TODO: Layout for labels, remove magic number | |
64 | BarLabel* label = reinterpret_cast<BarLabel*> (childItems().at(labelIndex)); |
|
66 | BarLabel* label = reinterpret_cast<BarLabel*> (childItems().at(labelIndex)); | |
65 | label->setPos(xPos, mHeight + 20); |
|
67 | label->setPos(xPos, mHeight + 20); | |
66 | labelIndex++; |
|
68 | labelIndex++; | |
67 | xPos += xStep; |
|
69 | xPos += xStep; | |
68 | } |
|
70 | } | |
69 |
|
71 | |||
70 | // Position separators |
|
72 | // Position separators | |
71 | int separatorIndex = labelIndex; // Separators are after labels in childItems(). TODO: better way to store these? |
|
73 | int separatorIndex = labelIndex; // Separators are after labels in childItems(). TODO: better way to store these? | |
72 | xPos = xStep + xStep/2; // Initial position is between first and second group. ie one and half steps from left. |
|
74 | xPos = xStep + xStep/2; // Initial position is between first and second group. ie one and half steps from left. | |
73 | for (int s=0; s < mModel.countCategories() - 1; s++) { |
|
75 | for (int s=0; s < mModel.countCategories() - 1; s++) { | |
74 | Separator* sep = reinterpret_cast<Separator*> (childItems().at(separatorIndex)); |
|
76 | Separator* sep = reinterpret_cast<Separator*> (childItems().at(separatorIndex)); | |
75 | sep->setPos(xPos,0); |
|
77 | sep->setPos(xPos,0); | |
76 | sep->setSize(QSizeF(1,mHeight)); |
|
78 | sep->setSize(QSizeF(1,mHeight)); | |
77 | xPos += xStep; |
|
79 | xPos += xStep; | |
78 | separatorIndex++; |
|
80 | separatorIndex++; | |
79 | } |
|
81 | } | |
80 |
|
82 | |||
81 | mLayoutDirty = true; |
|
83 | mLayoutDirty = true; | |
82 | } |
|
84 | } | |
83 |
|
85 | |||
84 | QTCOMMERCIALCHART_END_NAMESPACE |
|
86 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,239 +1,245 | |||||
1 | #include "charttheme_p.h" |
|
1 | #include "charttheme_p.h" | |
2 | #include "qchart.h" |
|
2 | #include "qchart.h" | |
3 | #include "qchartaxis.h" |
|
3 | #include "qchartaxis.h" | |
4 |
|
4 | |||
5 |
|
5 | |||
6 | //series |
|
6 | //series | |
7 | #include "barchartseries.h" |
|
7 | #include "barchartseries.h" | |
8 | #include "stackedbarchartseries.h" |
|
8 | #include "stackedbarchartseries.h" | |
9 | #include "percentbarchartseries.h" |
|
9 | #include "percentbarchartseries.h" | |
10 | #include "qlinechartseries.h" |
|
10 | #include "qlinechartseries.h" | |
11 | #include "qscatterseries.h" |
|
11 | #include "qscatterseries.h" | |
12 | #include "qpieseries.h" |
|
12 | #include "qpieseries.h" | |
13 |
|
13 | |||
14 | //items |
|
14 | //items | |
15 | #include "axisitem_p.h" |
|
15 | #include "axisitem_p.h" | |
16 | #include "bargroup.h" |
|
16 | #include "bargroup.h" | |
17 | #include "stackedbargroup.h" |
|
17 | #include "stackedbargroup.h" | |
18 | #include "linechartitem_p.h" |
|
18 | #include "linechartitem_p.h" | |
19 | #include "percentbargroup.h" |
|
19 | #include "percentbargroup.h" | |
20 | #include "scatterpresenter.h" |
|
20 | #include "scatterpresenter.h" | |
21 | #include "piepresenter.h" |
|
21 | #include "piepresenter.h" | |
22 |
|
22 | |||
23 | //themes |
|
23 | //themes | |
24 | #include "chartthemevanilla_p.h" |
|
24 | #include "chartthemevanilla_p.h" | |
25 | #include "chartthemeicy_p.h" |
|
25 | #include "chartthemeicy_p.h" | |
26 | #include "chartthemegrayscale_p.h" |
|
26 | #include "chartthemegrayscale_p.h" | |
27 | #include "chartthemescientific_p.h" |
|
27 | #include "chartthemescientific_p.h" | |
28 |
|
28 | |||
29 |
|
29 | |||
30 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
30 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
31 |
|
31 | |||
32 | /* TODO |
|
32 | /* TODO | |
33 | case QChart::ChartThemeUnnamed1: |
|
33 | case QChart::ChartThemeUnnamed1: | |
34 | m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xff3fa9f5)), 2)); |
|
34 | m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xff3fa9f5)), 2)); | |
35 | m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xff7AC943)), 2)); |
|
35 | m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xff7AC943)), 2)); | |
36 | m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF931E)), 2)); |
|
36 | m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF931E)), 2)); | |
37 | m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF1D25)), 2)); |
|
37 | m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF1D25)), 2)); | |
38 | m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF7BAC)), 2)); |
|
38 | m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF7BAC)), 2)); | |
39 |
|
39 | |||
40 | m_gradientStartColor = QColor(QRgb(0xfff3dc9e)); |
|
40 | m_gradientStartColor = QColor(QRgb(0xfff3dc9e)); | |
41 | m_gradientEndColor = QColor(QRgb(0xffafafaf)); |
|
41 | m_gradientEndColor = QColor(QRgb(0xffafafaf)); | |
42 | */ |
|
42 | */ | |
43 |
|
43 | |||
44 | ChartTheme::ChartTheme(QChart::ChartTheme id) |
|
44 | ChartTheme::ChartTheme(QChart::ChartTheme id) | |
45 | { |
|
45 | { | |
46 | m_id = id; |
|
46 | m_id = id; | |
47 | m_seriesColor.append(QRgb(0xff000000)); |
|
47 | m_seriesColor.append(QRgb(0xff000000)); | |
48 | m_seriesColor.append(QRgb(0xff707070)); |
|
48 | m_seriesColor.append(QRgb(0xff707070)); | |
49 | m_gradientStartColor = QColor(QRgb(0xffffffff)); |
|
49 | m_gradientStartColor = QColor(QRgb(0xffffffff)); | |
50 | m_gradientEndColor = QColor(QRgb(0xffafafaf)); |
|
50 | m_gradientEndColor = QColor(QRgb(0xffafafaf)); | |
51 | } |
|
51 | } | |
52 |
|
52 | |||
53 |
|
53 | |||
54 | ChartTheme* ChartTheme::createTheme(QChart::ChartTheme theme) |
|
54 | ChartTheme* ChartTheme::createTheme(QChart::ChartTheme theme) | |
55 | { |
|
55 | { | |
56 | switch(theme) { |
|
56 | switch(theme) { | |
57 | case QChart::ChartThemeDefault: |
|
57 | case QChart::ChartThemeDefault: | |
58 | return new ChartTheme(); |
|
58 | return new ChartTheme(); | |
59 | case QChart::ChartThemeVanilla: |
|
59 | case QChart::ChartThemeVanilla: | |
60 | return new ChartThemeVanilla(); |
|
60 | return new ChartThemeVanilla(); | |
61 | case QChart::ChartThemeIcy: |
|
61 | case QChart::ChartThemeIcy: | |
62 | return new ChartThemeIcy(); |
|
62 | return new ChartThemeIcy(); | |
63 | case QChart::ChartThemeGrayscale: |
|
63 | case QChart::ChartThemeGrayscale: | |
64 | return new ChartThemeGrayscale(); |
|
64 | return new ChartThemeGrayscale(); | |
65 | case QChart::ChartThemeScientific: |
|
65 | case QChart::ChartThemeScientific: | |
66 | return new ChartThemeScientific(); |
|
66 | return new ChartThemeScientific(); | |
67 | } |
|
67 | } | |
68 | } |
|
68 | } | |
69 |
|
69 | |||
70 | void ChartTheme::decorate(QChart* chart) |
|
70 | void ChartTheme::decorate(QChart* chart) | |
71 | { |
|
71 | { | |
72 | QLinearGradient backgroundGradient; |
|
72 | QLinearGradient backgroundGradient; | |
73 | backgroundGradient.setColorAt(0.0, m_gradientStartColor); |
|
73 | backgroundGradient.setColorAt(0.0, m_gradientStartColor); | |
74 | backgroundGradient.setColorAt(1.0, m_gradientEndColor); |
|
74 | backgroundGradient.setColorAt(1.0, m_gradientEndColor); | |
75 | backgroundGradient.setCoordinateMode(QGradient::ObjectBoundingMode); |
|
75 | backgroundGradient.setCoordinateMode(QGradient::ObjectBoundingMode); | |
76 | chart->setChartBackgroundBrush(backgroundGradient); |
|
76 | chart->setChartBackgroundBrush(backgroundGradient); | |
77 | } |
|
77 | } | |
78 | //TODO helper to by removed later |
|
78 | //TODO helper to by removed later | |
79 | void ChartTheme::decorate(ChartItem* item, QChartSeries* series,int count) |
|
79 | void ChartTheme::decorate(ChartItem* item, QChartSeries* series,int count) | |
80 | { |
|
80 | { | |
81 | switch(series->type()) |
|
81 | switch(series->type()) | |
82 | { |
|
82 | { | |
83 | case QChartSeries::SeriesTypeLine: { |
|
83 | case QChartSeries::SeriesTypeLine: { | |
84 | QLineChartSeries* s = static_cast<QLineChartSeries*>(series); |
|
84 | QLineChartSeries* s = static_cast<QLineChartSeries*>(series); | |
85 | LineChartItem* i = static_cast<LineChartItem*>(item); |
|
85 | LineChartItem* i = static_cast<LineChartItem*>(item); | |
86 | decorate(i,s,count); |
|
86 | decorate(i,s,count); | |
87 | break; |
|
87 | break; | |
88 | } |
|
88 | } | |
89 | case QChartSeries::SeriesTypeBar: { |
|
89 | case QChartSeries::SeriesTypeBar: { | |
90 | BarChartSeries* b = static_cast<BarChartSeries*>(series); |
|
90 | BarChartSeries* b = static_cast<BarChartSeries*>(series); | |
91 | BarGroup* i = static_cast<BarGroup*>(item); |
|
91 | BarGroup* i = static_cast<BarGroup*>(item); | |
92 | decorate(i,b,count); |
|
92 | decorate(i,b,count); | |
93 | break; |
|
93 | break; | |
94 | } |
|
94 | } | |
95 | case QChartSeries::SeriesTypeStackedBar: { |
|
95 | case QChartSeries::SeriesTypeStackedBar: { | |
96 | StackedBarChartSeries* s = static_cast<StackedBarChartSeries*>(series); |
|
96 | StackedBarChartSeries* s = static_cast<StackedBarChartSeries*>(series); | |
97 | StackedBarGroup* i = static_cast<StackedBarGroup*>(item); |
|
97 | StackedBarGroup* i = static_cast<StackedBarGroup*>(item); | |
98 | decorate(i,s,count); |
|
98 | decorate(i,s,count); | |
99 | break; |
|
99 | break; | |
100 | } |
|
100 | } | |
101 | case QChartSeries::SeriesTypePercentBar: { |
|
101 | case QChartSeries::SeriesTypePercentBar: { | |
102 | PercentBarChartSeries* s = static_cast<PercentBarChartSeries*>(series); |
|
102 | PercentBarChartSeries* s = static_cast<PercentBarChartSeries*>(series); | |
103 | PercentBarGroup* i = static_cast<PercentBarGroup*>(item); |
|
103 | PercentBarGroup* i = static_cast<PercentBarGroup*>(item); | |
104 | decorate(i,s,count); |
|
104 | decorate(i,s,count); | |
105 | break; |
|
105 | break; | |
106 | } |
|
106 | } | |
107 | case QChartSeries::SeriesTypePie: { |
|
107 | case QChartSeries::SeriesTypePie: { | |
108 | QPieSeries* s = static_cast<QPieSeries*>(series); |
|
108 | QPieSeries* s = static_cast<QPieSeries*>(series); | |
109 | PiePresenter* i = static_cast<PiePresenter*>(item); |
|
109 | PiePresenter* i = static_cast<PiePresenter*>(item); | |
110 | decorate(i,s,count); |
|
110 | decorate(i,s,count); | |
111 | break; |
|
111 | break; | |
112 | } |
|
112 | } | |
113 | default: |
|
113 | default: | |
114 | qDebug()<<"Wrong item to be decorated by theme"; |
|
114 | qDebug()<<"Wrong item to be decorated by theme"; | |
115 | break; |
|
115 | break; | |
116 | } |
|
116 | } | |
117 |
|
117 | |||
118 | } |
|
118 | } | |
119 |
|
119 | |||
120 | void ChartTheme::decorate(LineChartItem* item, QLineChartSeries* series,int count) |
|
120 | void ChartTheme::decorate(LineChartItem* item, QLineChartSeries* series,int count) | |
121 | { |
|
121 | { | |
122 | QPen pen; |
|
122 | QPen pen; | |
123 | if(pen != series->pen()){ |
|
123 | if(pen != series->pen()){ | |
124 | item->setPen(series->pen()); |
|
124 | item->setPen(series->pen()); | |
125 | return; |
|
125 | return; | |
126 | } |
|
126 | } | |
127 | pen.setColor(m_seriesColor.at(count%m_seriesColor.size())); |
|
127 | pen.setColor(m_seriesColor.at(count%m_seriesColor.size())); | |
128 | pen.setWidthF(2); |
|
128 | pen.setWidthF(2); | |
129 | item->setPen(pen); |
|
129 | item->setPen(pen); | |
130 | } |
|
130 | } | |
131 |
|
131 | |||
132 | void ChartTheme::decorate(BarGroup* item, BarChartSeries* series,int count) |
|
132 | void ChartTheme::decorate(BarGroup* item, BarChartSeries* series,int count) | |
133 | { |
|
133 | { | |
134 | item->resetColors(); |
|
134 | // TODO: better way to descide series color and remove hard coded colors. | |
|
135 | item->resetBrushes(); | |||
135 | for (int i=0; i<m_seriesColor.count(); i++) { |
|
136 | for (int i=0; i<m_seriesColor.count(); i++) { | |
136 |
|
|
137 | QBrush brush(m_seriesColor.at(i)); | |
|
138 | item->addBrush(brush); | |||
137 | } |
|
139 | } | |
138 |
item->add |
|
140 | item->addBrush(QBrush(QColor(255,0,0,128))); | |
139 |
item->add |
|
141 | item->addBrush(QBrush(QColor(255,255,0,128))); | |
140 |
item->add |
|
142 | item->addBrush(QBrush(QColor(0,255,0,128))); | |
141 |
item->add |
|
143 | item->addBrush(QBrush(QColor(0,0,255,128))); | |
142 |
item->add |
|
144 | item->addBrush(QBrush(QColor(255,128,0,128))); | |
143 | } |
|
145 | } | |
144 |
|
146 | |||
145 | void ChartTheme::decorate(StackedBarGroup* item, StackedBarChartSeries* series,int count) |
|
147 | void ChartTheme::decorate(StackedBarGroup* item, StackedBarChartSeries* series,int count) | |
146 | { |
|
148 | { | |
147 | item->resetColors(); |
|
149 | // TODO: better way to descide series color and remove hard coded colors. | |
148 | for (int i=0; i< m_seriesColor.count(); i++) { |
|
150 | item->resetBrushes(); | |
149 | item->addColor(m_seriesColor.at(i)); |
|
151 | for (int i=0; i<m_seriesColor.count(); i++) { | |
|
152 | QBrush brush(m_seriesColor.at(i)); | |||
|
153 | item->addBrush(brush); | |||
150 | } |
|
154 | } | |
151 |
item->add |
|
155 | item->addBrush(QBrush(QColor(255,0,0,128))); | |
152 |
item->add |
|
156 | item->addBrush(QBrush(QColor(255,255,0,128))); | |
153 |
item->add |
|
157 | item->addBrush(QBrush(QColor(0,255,0,128))); | |
154 |
item->add |
|
158 | item->addBrush(QBrush(QColor(0,0,255,128))); | |
155 |
item->add |
|
159 | item->addBrush(QBrush(QColor(255,128,0,128))); | |
156 | } |
|
160 | } | |
157 |
|
161 | |||
158 | void ChartTheme::decorate(PercentBarGroup* item, PercentBarChartSeries* series,int count) |
|
162 | void ChartTheme::decorate(PercentBarGroup* item, PercentBarChartSeries* series,int count) | |
159 | { |
|
163 | { | |
160 | item->resetColors(); |
|
164 | // TODO: better way to descide series color and remove hard coded colors. | |
161 | for (int i=0; i< m_seriesColor.count(); i++) { |
|
165 | item->resetBrushes(); | |
162 | item->addColor(m_seriesColor.at(i)); |
|
166 | for (int i=0; i<m_seriesColor.count(); i++) { | |
|
167 | QBrush brush(m_seriesColor.at(i)); | |||
|
168 | item->addBrush(brush); | |||
163 | } |
|
169 | } | |
164 |
item->add |
|
170 | item->addBrush(QBrush(QColor(255,0,0,128))); | |
165 |
item->add |
|
171 | item->addBrush(QBrush(QColor(255,255,0,128))); | |
166 |
item->add |
|
172 | item->addBrush(QBrush(QColor(0,255,0,128))); | |
167 |
item->add |
|
173 | item->addBrush(QBrush(QColor(0,0,255,128))); | |
168 |
item->add |
|
174 | item->addBrush(QBrush(QColor(255,128,0,128))); | |
169 | } |
|
175 | } | |
170 |
|
176 | |||
171 | void ChartTheme::decorate(ScatterPresenter* presenter, QScatterSeries* series, int count) |
|
177 | void ChartTheme::decorate(ScatterPresenter* presenter, QScatterSeries* series, int count) | |
172 | { |
|
178 | { | |
173 | Q_ASSERT(presenter); |
|
179 | Q_ASSERT(presenter); | |
174 | Q_ASSERT(series); |
|
180 | Q_ASSERT(series); | |
175 |
|
181 | |||
176 | presenter->m_markerPen.setColor(m_seriesColor.at(count % m_seriesColor.size())); |
|
182 | presenter->m_markerPen.setColor(m_seriesColor.at(count % m_seriesColor.size())); | |
177 |
|
183 | |||
178 | // QPen pen; |
|
184 | // QPen pen; | |
179 | // if(pen != series->pen()){ |
|
185 | // if(pen != series->pen()){ | |
180 | // item->setPen(series->pen()); |
|
186 | // item->setPen(series->pen()); | |
181 | // return; |
|
187 | // return; | |
182 | // } |
|
188 | // } | |
183 | // pen.setColor(m_seriesColor.at(count%m_seriesColor.size())); |
|
189 | // pen.setColor(m_seriesColor.at(count%m_seriesColor.size())); | |
184 | // pen.setWidthF(2); |
|
190 | // pen.setWidthF(2); | |
185 | // item->setPen(pen); |
|
191 | // item->setPen(pen); | |
186 | } |
|
192 | } | |
187 |
|
193 | |||
188 | void ChartTheme::decorate(PiePresenter* item, QPieSeries* series, int /*count*/) |
|
194 | void ChartTheme::decorate(PiePresenter* item, QPieSeries* series, int /*count*/) | |
189 | { |
|
195 | { | |
190 | // create a list of slice colors based on current theme |
|
196 | // create a list of slice colors based on current theme | |
191 | int i = 0; |
|
197 | int i = 0; | |
192 | QList<QColor> colors; |
|
198 | QList<QColor> colors; | |
193 | while (colors.count() < series->count()) { |
|
199 | while (colors.count() < series->count()) { | |
194 |
|
200 | |||
195 | // get base color |
|
201 | // get base color | |
196 | QColor c = m_seriesColor[i++]; |
|
202 | QColor c = m_seriesColor[i++]; | |
197 | i = i % m_seriesColor.count(); |
|
203 | i = i % m_seriesColor.count(); | |
198 |
|
204 | |||
199 | // -1 means achromatic color -> cannot manipulate lightness |
|
205 | // -1 means achromatic color -> cannot manipulate lightness | |
200 | // TODO: find a better way to randomize lightness |
|
206 | // TODO: find a better way to randomize lightness | |
201 | if (c.toHsv().hue() == -1) |
|
207 | if (c.toHsv().hue() == -1) | |
202 | qWarning() << "ChartTheme::decorate() warning: achromatic theme color"; |
|
208 | qWarning() << "ChartTheme::decorate() warning: achromatic theme color"; | |
203 |
|
209 | |||
204 | // randomize lightness |
|
210 | // randomize lightness | |
205 | qreal f = 50 + (qrand() % 100); // 50 is 50% darker, 100 is the same, 150 is 50% lighter |
|
211 | qreal f = 50 + (qrand() % 100); // 50 is 50% darker, 100 is the same, 150 is 50% lighter | |
206 | c = c.lighter(f); |
|
212 | c = c.lighter(f); | |
207 |
|
213 | |||
208 | // find duplicates |
|
214 | // find duplicates | |
209 | bool isUnique = true; |
|
215 | bool isUnique = true; | |
210 | foreach (QColor color, colors) { |
|
216 | foreach (QColor color, colors) { | |
211 | if (c == color) |
|
217 | if (c == color) | |
212 | isUnique = false; |
|
218 | isUnique = false; | |
213 | } |
|
219 | } | |
214 |
|
220 | |||
215 | // add to array if unique |
|
221 | // add to array if unique | |
216 | //if (isUnique) |
|
222 | //if (isUnique) | |
217 | colors << c; |
|
223 | colors << c; | |
218 | } |
|
224 | } | |
219 |
|
225 | |||
220 | // finally update colors |
|
226 | // finally update colors | |
221 | foreach (QPieSliceId id, series->ids()) { |
|
227 | foreach (QPieSliceId id, series->ids()) { | |
222 | QPieSlice s = series->slice(id); |
|
228 | QPieSlice s = series->slice(id); | |
223 | s.setPen(QPen(Qt::black)); // TODO: get from theme |
|
229 | s.setPen(QPen(Qt::black)); // TODO: get from theme | |
224 | s.setBrush(colors.takeFirst()); |
|
230 | s.setBrush(colors.takeFirst()); | |
225 | series->update(s); |
|
231 | series->update(s); | |
226 | } |
|
232 | } | |
227 | } |
|
233 | } | |
228 |
|
234 | |||
229 |
|
235 | |||
230 | void ChartTheme::decorate(QChartAxis& axis,AxisItem* item) |
|
236 | void ChartTheme::decorate(QChartAxis& axis,AxisItem* item) | |
231 | { |
|
237 | { | |
232 | //TODO: dummy defults for now |
|
238 | //TODO: dummy defults for now | |
233 |
|
239 | |||
234 | axis.setLabelsBrush(Qt::black); |
|
240 | axis.setLabelsBrush(Qt::black); | |
235 | axis.setLabelsPen(Qt::NoPen); |
|
241 | axis.setLabelsPen(Qt::NoPen); | |
236 | item->handleAxisChanged(axis); |
|
242 | item->handleAxisChanged(axis); | |
237 | } |
|
243 | } | |
238 |
|
244 | |||
239 | QTCOMMERCIALCHART_END_NAMESPACE |
|
245 | QTCOMMERCIALCHART_END_NAMESPACE |
General Comments 0
You need to be logged in to leave comments.
Login now