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