@@ -1,50 +1,62 | |||||
1 | #include "bar.h" |
|
1 | #include "bar.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( |
|
7 | Bar::Bar(ChartItem *parent) | |
8 |
: |
|
8 | : ChartItem(parent) | |
9 | { |
|
9 | { | |
10 | } |
|
10 | } | |
11 |
|
11 | |||
|
12 | void Bar::setSize(const QSize& size) | |||
|
13 | { | |||
|
14 | //mSize = size; | |||
|
15 | mWidth = size.width(); | |||
|
16 | mHeight = size.height(); | |||
|
17 | } | |||
|
18 | ||||
|
19 | void Bar::setPlotDomain(const PlotDomain& data) | |||
|
20 | { | |||
|
21 | mPlotDomain = data; | |||
|
22 | } | |||
|
23 | ||||
12 | void Bar::resize( int w, int h ) |
|
24 | void Bar::resize( int w, int h ) | |
13 | { |
|
25 | { | |
14 | mWidth = w; |
|
26 | mWidth = w; | |
15 | mHeight = h; |
|
27 | mHeight = h; | |
16 | } |
|
28 | } | |
17 |
|
29 | |||
18 | void Bar::setColor( QColor col ) |
|
30 | void Bar::setColor( QColor col ) | |
19 | { |
|
31 | { | |
20 | mColor = col; |
|
32 | mColor = col; | |
21 | } |
|
33 | } | |
22 | void Bar::setPos(qreal x, qreal y) |
|
34 | void Bar::setPos(qreal x, qreal y) | |
23 | { |
|
35 | { | |
24 | mXpos = x; |
|
36 | mXpos = x; | |
25 | mYpos = y; |
|
37 | mYpos = y; | |
26 | } |
|
38 | } | |
27 |
|
39 | |||
28 | void Bar::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
40 | void Bar::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | |
29 | { |
|
41 | { | |
30 | // Set color for bar. TODO: gradients, textures etc |
|
42 | // Set color for bar. TODO: gradients, textures etc | |
31 | QPen pen = painter->pen(); |
|
43 | QPen pen = painter->pen(); | |
32 | pen.setColor( mColor ); |
|
44 | pen.setColor( mColor ); | |
33 | pen.setWidth( mWidth ); |
|
45 | pen.setWidth( mWidth ); | |
34 | painter->setPen(pen); |
|
46 | painter->setPen(pen); | |
35 |
|
47 | |||
36 | // Draw bar |
|
48 | // Draw bar | |
37 | // TODO: Pen width affects bar height for now. This should be rect |
|
49 | // TODO: Pen width affects bar height for now. This should be rect | |
38 | painter->drawLine(scenePos().x() + mXpos, scenePos().y() + mYpos + parentItem()->boundingRect().height() - mHeight - mWidth, |
|
50 | painter->drawLine(scenePos().x() + mXpos, scenePos().y() + mYpos + parentItem()->boundingRect().height() - mHeight - mWidth, | |
39 | scenePos().x() + mXpos, scenePos().y() + mYpos + parentItem()->boundingRect().height() - mWidth); |
|
51 | scenePos().x() + mXpos, scenePos().y() + mYpos + parentItem()->boundingRect().height() - mWidth); | |
40 | } |
|
52 | } | |
41 |
|
53 | |||
42 | QRectF Bar::boundingRect() const |
|
54 | QRectF Bar::boundingRect() const | |
43 | { |
|
55 | { | |
44 | // TODO: check validity of this (I suppose there is easier way, and currently this bit incorrect :) |
|
56 | // TODO: check validity of this (I suppose there is easier way, and currently this bit incorrect :) | |
45 | // QRectF r(scenePos().x()+mXpos, scenePos().y()+mYpos, scenePos().x() + mWidth, scenePos().y() + mHeight ); |
|
57 | // QRectF r(scenePos().x()+mXpos, scenePos().y()+mYpos, scenePos().x() + mWidth, scenePos().y() + mHeight ); | |
46 | QRectF r(mXpos, mYpos, mXpos + mWidth, mYpos + mHeight); |
|
58 | QRectF r(mXpos, mYpos, mXpos + mWidth, mYpos + mHeight); | |
47 | return r; |
|
59 | return r; | |
48 | } |
|
60 | } | |
49 |
|
61 | |||
50 | QTCOMMERCIALCHART_END_NAMESPACE |
|
62 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,37 +1,43 | |||||
1 | #ifndef BAR_H |
|
1 | #ifndef BAR_H | |
2 | #define BAR_H |
|
2 | #define BAR_H | |
3 |
|
3 | |||
4 | #include <QGraphicsItem> |
|
4 | #include "chartitem_p.h" | |
5 | #include "qchartglobal.h" |
|
5 | #include "qchartglobal.h" | |
6 |
|
6 | |||
7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
8 |
|
8 | |||
9 | // Single bar item of chart |
|
9 | // Single bar item of chart | |
10 |
class Bar : public |
|
10 | class Bar : public ChartItem | |
11 | { |
|
11 | { | |
12 | public: |
|
12 | public: | |
13 |
Bar( |
|
13 | Bar(ChartItem *parent=0); | |
|
14 | ||||
|
15 | // From ChartItem | |||
|
16 | virtual void setSize(const QSize& size); | |||
|
17 | virtual void setPlotDomain(const PlotDomain& data); | |||
14 |
|
18 | |||
15 | // Layout Stuff |
|
19 | // Layout Stuff | |
16 | void resize( int w, int h ); // Size of bar. in screen coordinates. |
|
20 | void resize( int w, int h ); // Size of bar. in screen coordinates. | |
17 | void setColor( QColor col ); // Color of bar |
|
21 | void setColor( QColor col ); // Color of bar | |
18 | void setPos(qreal x, qreal y); |
|
22 | void setPos(qreal x, qreal y); | |
19 |
|
23 | |||
20 | public: |
|
24 | public: | |
21 | // From QGraphicsItem |
|
25 | // From QGraphicsItem | |
22 |
|
26 | |||
23 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0); |
|
27 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0); | |
24 | QRectF boundingRect() const; |
|
28 | QRectF boundingRect() const; | |
25 |
|
29 | |||
26 | private: |
|
30 | private: | |
27 |
|
31 | |||
28 | int mHeight; |
|
32 | int mHeight; | |
29 | int mWidth; |
|
33 | int mWidth; | |
30 | qreal mXpos; |
|
34 | qreal mXpos; | |
31 | qreal mYpos; |
|
35 | qreal mYpos; | |
32 | QColor mColor; |
|
36 | QColor mColor; | |
|
37 | ||||
|
38 | PlotDomain mPlotDomain; | |||
33 | }; |
|
39 | }; | |
34 |
|
40 | |||
35 | QTCOMMERCIALCHART_END_NAMESPACE |
|
41 | QTCOMMERCIALCHART_END_NAMESPACE | |
36 |
|
42 | |||
37 | #endif // BAR_H |
|
43 | #endif // BAR_H |
@@ -1,90 +1,90 | |||||
1 | #include <QDebug> |
|
1 | #include <QDebug> | |
2 | #include "barchartseries.h" |
|
2 | #include "barchartseries.h" | |
|
3 | #include "bargroup.h" | |||
3 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
4 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
4 |
|
5 | |||
5 | BarChartSeries::BarChartSeries(QObject *parent) |
|
6 | BarChartSeries::BarChartSeries(QObject *parent) | |
6 | : QChartSeries(parent) |
|
7 | : QChartSeries(parent) | |
7 | { |
|
8 | { | |
8 | } |
|
9 | } | |
9 |
|
10 | |||
10 | bool BarChartSeries::setData(QAbstractItemModel* model) |
|
11 | bool BarChartSeries::setData(QAbstractItemModel* model) | |
11 | { |
|
12 | { | |
12 | mModel = model; |
|
13 | mModel = model; | |
13 | } |
|
14 | } | |
14 |
|
15 | |||
15 |
|
||||
16 | int BarChartSeries::min() |
|
16 | int BarChartSeries::min() | |
17 | { |
|
17 | { | |
18 | Q_ASSERT(mModel->rowCount() > 0); |
|
18 | Q_ASSERT(mModel->rowCount() > 0); | |
19 | Q_ASSERT(mModel->columnCount() > 0); |
|
19 | Q_ASSERT(mModel->columnCount() > 0); | |
20 |
|
20 | |||
21 | // TODO: make min and max members and update them when data changes. |
|
21 | // TODO: make min and max members and update them when data changes. | |
22 | // This is slower since they are checked every time, even if data is same since previous call. |
|
22 | // This is slower since they are checked every time, even if data is same since previous call. | |
23 | QModelIndex modelIndex = mModel->index(0,0); |
|
23 | QModelIndex modelIndex = mModel->index(0,0); | |
24 | int min = mModel->data(modelIndex).toInt(); |
|
24 | int min = mModel->data(modelIndex).toInt(); | |
25 |
|
25 | |||
26 | for (int i=0; i <mModel->rowCount(); i++) { |
|
26 | for (int i=0; i <mModel->rowCount(); i++) { | |
27 | for(int j=0; j<mModel->columnCount(); j++) { |
|
27 | for(int j=0; j<mModel->columnCount(); j++) { | |
28 | modelIndex = mModel->index(i,j); |
|
28 | modelIndex = mModel->index(i,j); | |
29 | int temp = mModel->data(modelIndex).toInt(); |
|
29 | int temp = mModel->data(modelIndex).toInt(); | |
30 | if (temp < min) { |
|
30 | if (temp < min) { | |
31 | min = temp; |
|
31 | min = temp; | |
32 | } |
|
32 | } | |
33 | } |
|
33 | } | |
34 | } |
|
34 | } | |
35 | return min; |
|
35 | return min; | |
36 | } |
|
36 | } | |
37 |
|
37 | |||
38 | int BarChartSeries::max() |
|
38 | int BarChartSeries::max() | |
39 | { |
|
39 | { | |
40 | Q_ASSERT(mModel->rowCount() > 0); |
|
40 | Q_ASSERT(mModel->rowCount() > 0); | |
41 | Q_ASSERT(mModel->columnCount() > 0); |
|
41 | Q_ASSERT(mModel->columnCount() > 0); | |
42 |
|
42 | |||
43 | // TODO: make min and max members and update them when data changes. |
|
43 | // TODO: make min and max members and update them when data changes. | |
44 | // This is slower since they are checked every time, even if data is same since previous call. |
|
44 | // This is slower since they are checked every time, even if data is same since previous call. | |
45 | QModelIndex modelIndex = mModel->index(0,0); |
|
45 | QModelIndex modelIndex = mModel->index(0,0); | |
46 | int max = mModel->data(modelIndex).toInt(); |
|
46 | int max = mModel->data(modelIndex).toInt(); | |
47 |
|
47 | |||
48 | for (int i=0; i <mModel->rowCount(); i++) { |
|
48 | for (int i=0; i <mModel->rowCount(); i++) { | |
49 | for(int j=0; j<mModel->columnCount(); j++) { |
|
49 | for(int j=0; j<mModel->columnCount(); j++) { | |
50 | modelIndex = mModel->index(i,j); |
|
50 | modelIndex = mModel->index(i,j); | |
51 | int temp = mModel->data(modelIndex).toInt(); |
|
51 | int temp = mModel->data(modelIndex).toInt(); | |
52 | if (temp > max) { |
|
52 | if (temp > max) { | |
53 | max = temp; |
|
53 | max = temp; | |
54 | } |
|
54 | } | |
55 | } |
|
55 | } | |
56 | } |
|
56 | } | |
57 | return max; |
|
57 | return max; | |
58 | } |
|
58 | } | |
59 |
|
59 | |||
60 |
|
60 | |||
61 | int BarChartSeries::countSeries() |
|
61 | int BarChartSeries::countSeries() | |
62 | { |
|
62 | { | |
63 | return mModel->rowCount(); |
|
63 | return mModel->rowCount(); | |
64 | } |
|
64 | } | |
65 |
|
65 | |||
66 | int BarChartSeries::countItemsInSeries() |
|
66 | int BarChartSeries::countItemsInSeries() | |
67 | { |
|
67 | { | |
68 | return mModel->columnCount(); |
|
68 | return mModel->columnCount(); | |
69 | } |
|
69 | } | |
70 |
|
70 | |||
71 | int BarChartSeries::countTotalItems() |
|
71 | int BarChartSeries::countTotalItems() | |
72 | { |
|
72 | { | |
73 | return mModel->rowCount() * mModel->columnCount(); |
|
73 | return mModel->rowCount() * mModel->columnCount(); | |
74 | } |
|
74 | } | |
75 |
|
75 | |||
76 | int BarChartSeries::valueAt(int series, int item) |
|
76 | int BarChartSeries::valueAt(int series, int item) | |
77 | { |
|
77 | { | |
78 | QModelIndex index = mModel->index(series,item); |
|
78 | QModelIndex index = mModel->index(series,item); | |
79 | return mModel->data(index).toInt(); |
|
79 | return mModel->data(index).toInt(); | |
80 | } |
|
80 | } | |
81 |
|
81 | |||
82 |
|
||||
83 | void BarChartSeries::chartSizeChanged(QRectF rect) |
|
82 | void BarChartSeries::chartSizeChanged(QRectF rect) | |
84 | { |
|
83 | { | |
85 | qDebug() << "barchart size changed:" << rect; |
|
84 | qDebug() << "barchart size changed:" << rect; | |
|
85 | // mBarGroup->resize(rect.toRect().width(), rect.toRect().height()); | |||
86 | } |
|
86 | } | |
87 |
|
87 | |||
88 | #include "moc_barchartseries.cpp" |
|
88 | #include "moc_barchartseries.cpp" | |
89 |
|
89 | |||
90 | QTCOMMERCIALCHART_END_NAMESPACE |
|
90 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,45 +1,48 | |||||
1 | #ifndef BARCHARTSERIES_H |
|
1 | #ifndef BARCHARTSERIES_H | |
2 | #define BARCHARTSERIES_H |
|
2 | #define BARCHARTSERIES_H | |
3 |
|
3 | |||
4 | #include <QList> |
|
4 | #include <QList> | |
5 | #include <QRectF> |
|
5 | #include <QRectF> | |
6 | #include <QAbstractItemModel> |
|
6 | #include <QAbstractItemModel> | |
7 | #include "qchartseries.h" |
|
7 | #include "qchartseries.h" | |
8 | #include "qchartglobal.h" |
|
8 | #include "qchartglobal.h" | |
9 |
|
9 | |||
|
10 | // TODO: Can this class be combined with series? | |||
|
11 | class BarGroup; | |||
|
12 | ||||
10 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
13 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
11 |
|
14 | |||
12 | // Container for series |
|
15 | // Container for series | |
13 | class QTCOMMERCIALCHART_EXPORT BarChartSeries : public QChartSeries |
|
16 | class QTCOMMERCIALCHART_EXPORT BarChartSeries : public QChartSeries | |
14 | { |
|
17 | { | |
15 | Q_OBJECT |
|
18 | Q_OBJECT | |
16 | public: |
|
19 | public: | |
17 | BarChartSeries(QObject* parent=0); |
|
20 | BarChartSeries(QObject* parent=0); | |
18 |
|
21 | |||
19 | // from QChartSeries |
|
22 | // from QChartSeries | |
20 | virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeBar; } |
|
23 | virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeBar; } | |
21 |
|
24 | |||
22 | // TODO: This as dataModel instead of n different setters. (data model itself can accept lists and whatnot) |
|
25 | // TODO: This as dataModel instead of n different setters. (data model itself can accept lists and whatnot) | |
23 | virtual bool setData(QAbstractItemModel* model); |
|
26 | virtual bool setData(QAbstractItemModel* model); | |
24 |
|
27 | |||
25 | // Methods to find out minimum and maximum values of data |
|
28 | // Methods to find out minimum and maximum values of data | |
26 | int min(); |
|
29 | int min(); | |
27 | int max(); |
|
30 | int max(); | |
28 | int countSeries(); |
|
31 | int countSeries(); | |
29 | int countItemsInSeries(); // Count items in one series. |
|
32 | int countItemsInSeries(); // Count items in one series. | |
30 | int countTotalItems(); |
|
33 | int countTotalItems(); | |
31 | int valueAt(int series, int item); |
|
34 | int valueAt(int series, int item); | |
32 |
|
35 | |||
33 | public Q_SLOTS: |
|
36 | public Q_SLOTS: | |
34 |
|
37 | |||
35 | void chartSizeChanged(QRectF rect); |
|
38 | void chartSizeChanged(QRectF rect); | |
36 |
|
39 | |||
37 | private: |
|
40 | private: | |
38 |
|
41 | |||
39 | //QList<int> mData; |
|
|||
40 | QAbstractItemModel* mModel; |
|
42 | QAbstractItemModel* mModel; | |
|
43 | BarGroup* mBarGroup; | |||
41 | }; |
|
44 | }; | |
42 |
|
45 | |||
43 | QTCOMMERCIALCHART_END_NAMESPACE |
|
46 | QTCOMMERCIALCHART_END_NAMESPACE | |
44 |
|
47 | |||
45 | #endif // BARCHARTSERIES_H |
|
48 | #endif // BARCHARTSERIES_H |
@@ -1,117 +1,117 | |||||
1 | #include "bargroup.h" |
|
1 | #include "bargroup.h" | |
2 | #include "bar.h" |
|
2 | #include "bar.h" | |
3 | #include <QDebug> |
|
3 | #include <QDebug> | |
4 |
|
4 | |||
5 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
5 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
6 |
|
6 | |||
7 | // TODO: singleton? |
|
7 | // TODO: singleton? | |
8 | //BarGroup* BarGroup::mBarGroupInstance = NULL; |
|
8 | //BarGroup* BarGroup::mBarGroupInstance = NULL; | |
9 |
|
9 | |||
10 | //BarGroup::BarGroup(QGraphicsItem *parent) : |
|
10 | //BarGroup::BarGroup(QGraphicsItem *parent) : | |
11 | // QGraphicsItem(parent) |
|
11 | // QGraphicsItem(parent) | |
12 | // ,mSeries(series) |
|
12 | // ,mSeries(series) | |
13 |
BarGroup::BarGroup(BarChartSeries& series, |
|
13 | BarGroup::BarGroup(BarChartSeries& series, ChartItem *parent) : | |
14 |
|
|
14 | ChartItem(parent) | |
15 | ,mSeries(series) |
|
15 | ,mSeries(series) | |
16 | ,mLayoutSet(false) |
|
16 | ,mLayoutSet(false) | |
17 | ,mLayoutDirty(true) |
|
17 | ,mLayoutDirty(true) | |
18 | { |
|
18 | { | |
19 | dataChanged(); |
|
19 | dataChanged(); | |
20 | } |
|
20 | } | |
21 |
|
21 | |||
22 | void BarGroup::resize( int w, int h ) |
|
22 | void BarGroup::resize( int w, int h ) | |
23 | { |
|
23 | { | |
24 | qDebug() << "QBarChart::resize"; |
|
24 | qDebug() << "QBarChart::resize"; | |
25 | mWidth = w; |
|
25 | mWidth = w; | |
26 | mHeight = h; |
|
26 | mHeight = h; | |
27 | layoutChanged(); |
|
27 | layoutChanged(); | |
28 | mLayoutSet = true; |
|
28 | mLayoutSet = true; | |
29 | } |
|
29 | } | |
30 |
|
30 | |||
31 | void BarGroup::setBarWidth( int w ) |
|
31 | void BarGroup::setBarWidth( int w ) | |
32 | { |
|
32 | { | |
33 | mBarDefaultWidth = w; |
|
33 | mBarDefaultWidth = w; | |
34 | } |
|
34 | } | |
35 |
|
35 | |||
36 | void BarGroup::setColor( QColor color ) |
|
36 | void BarGroup::setColor( QColor color ) | |
37 | { |
|
37 | { | |
38 | mColor = color; |
|
38 | mColor = color; | |
39 | } |
|
39 | } | |
40 |
|
40 | |||
41 | void BarGroup::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
41 | void BarGroup::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | |
42 | { |
|
42 | { | |
43 | if (!mLayoutSet) { |
|
43 | if (!mLayoutSet) { | |
44 | qDebug() << "QBarChart::paint called without layout set. Aborting."; |
|
44 | qDebug() << "QBarChart::paint called without layout set. Aborting."; | |
45 | return; |
|
45 | return; | |
46 | } |
|
46 | } | |
47 | if (mLayoutDirty) { |
|
47 | if (mLayoutDirty) { | |
48 | // Layout or data has changed. Need to redraw. |
|
48 | // Layout or data has changed. Need to redraw. | |
49 | foreach(QGraphicsItem* i, childItems()) { |
|
49 | foreach(QGraphicsItem* i, childItems()) { | |
50 | i->paint(painter,option,widget); |
|
50 | i->paint(painter,option,widget); | |
51 | } |
|
51 | } | |
52 | } |
|
52 | } | |
53 | } |
|
53 | } | |
54 |
|
54 | |||
55 | QRectF BarGroup::boundingRect() const |
|
55 | QRectF BarGroup::boundingRect() const | |
56 | { |
|
56 | { | |
57 | // TODO: correct this (currently ignores position) |
|
57 | // TODO: correct this (currently ignores position) | |
58 | return QRectF(0,0,mWidth,mHeight); |
|
58 | return QRectF(0,0,mWidth,mHeight); | |
59 | } |
|
59 | } | |
60 |
|
60 | |||
61 |
|
61 | |||
62 | void BarGroup::dataChanged() |
|
62 | void BarGroup::dataChanged() | |
63 | { |
|
63 | { | |
64 | qDebug() << "QBarChart::dataChanged mSeries"; |
|
64 | qDebug() << "QBarChart::dataChanged mSeries"; | |
65 |
|
65 | |||
66 | // Find out maximum and minimum of all series |
|
66 | // Find out maximum and minimum of all series | |
67 | mMax = mSeries.max(); |
|
67 | mMax = mSeries.max(); | |
68 | mMin = mSeries.min(); |
|
68 | mMin = mSeries.min(); | |
69 |
|
69 | |||
70 | // Delete old bars |
|
70 | // Delete old bars | |
71 | // Is this correct way to delete childItems? |
|
71 | // Is this correct way to delete childItems? | |
72 | foreach (QGraphicsItem* item, childItems()) { |
|
72 | foreach (QGraphicsItem* item, childItems()) { | |
73 | delete item; |
|
73 | delete item; | |
74 | } |
|
74 | } | |
75 |
|
75 | |||
76 | // Create new graphic items for bars |
|
76 | // Create new graphic items for bars | |
77 | int totalItems = mSeries.countTotalItems(); |
|
77 | int totalItems = mSeries.countTotalItems(); | |
78 | for (int i=0; i<totalItems; i++) { |
|
78 | for (int i=0; i<totalItems; i++) { | |
79 | Bar *bar = new Bar(this); |
|
79 | Bar *bar = new Bar(this); | |
80 | childItems().append(bar); |
|
80 | childItems().append(bar); | |
81 | } |
|
81 | } | |
82 |
|
82 | |||
83 | mLayoutDirty = true; |
|
83 | mLayoutDirty = true; | |
84 | } |
|
84 | } | |
85 |
|
85 | |||
86 | void BarGroup::layoutChanged() |
|
86 | void BarGroup::layoutChanged() | |
87 | { |
|
87 | { | |
88 | // Scale bars to new layout |
|
88 | // Scale bars to new layout | |
89 | // Layout for bars: |
|
89 | // Layout for bars: | |
90 | int count = mSeries.countSeries(); |
|
90 | int count = mSeries.countSeries(); | |
91 | if (count <= 0) { |
|
91 | if (count <= 0) { | |
92 | // Nothing to do. |
|
92 | // Nothing to do. | |
93 | return; |
|
93 | return; | |
94 | } |
|
94 | } | |
95 |
|
95 | |||
96 | // Align center |
|
96 | // Align center | |
97 | int posStep = (mWidth / (count)); |
|
97 | int posStep = (mWidth / (count)); | |
98 | int startPos = (mWidth / count+1); |
|
98 | int startPos = (mWidth / count+1); | |
99 | qDebug() << "startpos" << startPos; |
|
99 | qDebug() << "startpos" << startPos; | |
100 |
|
100 | |||
101 | // Scaling. TODO: better one. |
|
101 | // Scaling. TODO: better one. | |
102 | int itemIndex(0); |
|
102 | int itemIndex(0); | |
103 | for (int series = 0; series < count; series++) { |
|
103 | for (int series = 0; series < count; series++) { | |
104 | for (int item=0; item < count; item++) { |
|
104 | for (int item=0; item < count; item++) { | |
105 | int barHeight = mSeries.valueAt(series, item) * mHeight / mMax; |
|
105 | int barHeight = mSeries.valueAt(series, item) * mHeight / mMax; | |
106 | Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex)); |
|
106 | Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex)); | |
107 |
|
107 | |||
108 | bar->resize(mBarDefaultWidth, barHeight); // TODO: width settable per bar |
|
108 | bar->resize(mBarDefaultWidth, barHeight); // TODO: width settable per bar | |
109 | bar->setColor(mColor); |
|
109 | bar->setColor(mColor); | |
110 | bar->setPos(itemIndex*posStep+startPos, 0); |
|
110 | bar->setPos(itemIndex*posStep+startPos, 0); | |
111 | itemIndex++; |
|
111 | itemIndex++; | |
112 | } |
|
112 | } | |
113 | } |
|
113 | } | |
114 | mLayoutDirty = true; |
|
114 | mLayoutDirty = true; | |
115 | } |
|
115 | } | |
116 |
|
116 | |||
117 | QTCOMMERCIALCHART_END_NAMESPACE |
|
117 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,69 +1,67 | |||||
1 | #ifndef QBARCHART_H |
|
1 | #ifndef QBARCHART_H | |
2 | #define QBARCHART_H |
|
2 | #define QBARCHART_H | |
3 |
|
3 | |||
4 | #include <QGraphicsObject> |
|
4 | #include "chartitem_p.h" | |
5 |
|
||||
6 | #include "bar.h" |
|
5 | #include "bar.h" | |
7 | //#include "qbarchartview.h" |
|
|||
8 | #include "barchartseries.h" |
|
6 | #include "barchartseries.h" | |
9 |
|
7 | |||
10 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
11 |
|
9 | |||
12 | // TODO: Better name for this? The function of this class is to know where each bar in series is laid out. |
|
10 | // TODO: Better name for this? The function of this class is to know where each bar in series is laid out. | |
13 | //class BarGroup : public QGraphicsItemGroup |
|
11 | //class BarGroup : public QGraphicsItemGroup | |
14 |
|
12 | |||
15 |
class BarGroup : public |
|
13 | class BarGroup : public ChartItem | |
16 | { |
|
14 | { | |
17 | /* // TODO: implement as singleton? |
|
15 | /* // TODO: implement as singleton? | |
18 | private: |
|
16 | private: | |
19 | static BarGroup* mBarGroupInstance; |
|
17 | static BarGroup* mBarGroupInstance; | |
20 |
|
18 | |||
21 | public: |
|
19 | public: | |
22 | static BarGroup* instance() |
|
20 | static BarGroup* instance() | |
23 | { |
|
21 | { | |
24 | if (mBarGroupInstance == NULL) { |
|
22 | if (mBarGroupInstance == NULL) { | |
25 | mBarGroupInstance = new BarGroup(); |
|
23 | mBarGroupInstance = new BarGroup(); | |
26 | } |
|
24 | } | |
27 | return mBarGroupInstance; |
|
25 | return mBarGroupInstance; | |
28 | } |
|
26 | } | |
29 | private: |
|
27 | private: | |
30 | */ |
|
28 | */ | |
31 | public: |
|
29 | public: | |
32 |
explicit BarGroup(BarChartSeries& series, |
|
30 | explicit BarGroup(BarChartSeries& series, ChartItem *parent = 0); | |
33 |
|
31 | |||
34 | // Layout "api" |
|
32 | // Layout "api" | |
35 | void resize( int w, int h ); // Size for whole series. Single bars are drawn inside this area |
|
33 | void resize( int w, int h ); // Size for whole series. Single bars are drawn inside this area | |
36 | void setPos(qreal x, qreal y); |
|
34 | void setPos(qreal x, qreal y); | |
37 | void setBarWidth( int w ); // Default width for each bar |
|
35 | void setBarWidth( int w ); // Default width for each bar | |
38 | void setColor( QColor color ); // Default color for each bar |
|
36 | void setColor( QColor color ); // Default color for each bar | |
39 |
|
37 | |||
40 | // From QGraphicsItem |
|
38 | // From QGraphicsItem | |
41 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); |
|
39 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); | |
42 | QRectF boundingRect() const; |
|
40 | QRectF boundingRect() const; | |
43 |
|
41 | |||
44 | private: |
|
42 | private: | |
45 |
|
43 | |||
46 | void dataChanged(); // data of series has changed -> need to recalculate bar sizes |
|
44 | void dataChanged(); // data of series has changed -> need to recalculate bar sizes | |
47 | void layoutChanged(); // layout has changed -> need to recalculate bar sizes |
|
45 | void layoutChanged(); // layout has changed -> need to recalculate bar sizes | |
48 |
|
46 | |||
49 | private: |
|
47 | private: | |
50 |
|
48 | |||
51 | // Data |
|
49 | // Data | |
52 | BarChartSeries& mSeries; |
|
50 | BarChartSeries& mSeries; | |
53 | int mMin; // Min and max values of data. (updated when data is changed, used when drawing) |
|
51 | int mMin; // Min and max values of data. (updated when data is changed, used when drawing) | |
54 | int mMax; |
|
52 | int mMax; | |
55 |
|
53 | |||
56 | int mHeight; // Layout spesific |
|
54 | int mHeight; // Layout spesific | |
57 | int mWidth; |
|
55 | int mWidth; | |
58 | int mBarDefaultWidth; |
|
56 | int mBarDefaultWidth; | |
59 |
|
57 | |||
60 | QColor mColor; |
|
58 | QColor mColor; | |
61 |
|
59 | |||
62 | bool mLayoutSet; // True, if component has been laid out. |
|
60 | bool mLayoutSet; // True, if component has been laid out. | |
63 | bool mLayoutDirty; |
|
61 | bool mLayoutDirty; | |
64 |
|
62 | |||
65 | }; |
|
63 | }; | |
66 |
|
64 | |||
67 | QTCOMMERCIALCHART_END_NAMESPACE |
|
65 | QTCOMMERCIALCHART_END_NAMESPACE | |
68 |
|
66 | |||
69 | #endif // QBARCHART_H |
|
67 | #endif // QBARCHART_H |
General Comments 0
You need to be logged in to leave comments.
Login now