##// END OF EJS Templates
Marek Rosa -
r275:e59660aaa64a merge
parent child
Show More
@@ -26,15 +26,15 int main(int argc, char *argv[])
26
26
27 // Create some test data to chart
27 // Create some test data to chart
28 *set0 << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10 << 11 << 12;
28 *set0 << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10 << 11 << 12;
29 *set1 << 5 << 0 << 0 << 4 << 0 << 7 << 8 << 9 << 9 << 0 << 4 << 2;
29 // *set1 << 5 << 0 << 0 << 4 << 0 << 7 << 8 << 9 << 9 << 0 << 4 << 2;
30 *set2 << 3 << 5 << 8 << 13 << 8 << 5 << 3 << 2 << 1 << 1 << 3 << 5;
30 // *set2 << 3 << 5 << 8 << 13 << 8 << 5 << 3 << 2 << 1 << 1 << 3 << 5;
31 *set3 << 5 << 6 << 7 << 3 << 4 << 5 << 8 << 9 << 10 << 5 << 2 << 7;
31 // *set3 << 5 << 6 << 7 << 3 << 4 << 5 << 8 << 9 << 10 << 5 << 2 << 7;
32 *set4 << 9 << 7 << 5 << 3 << 1 << 2 << 4 << 6 << 8 << 10 << 1 << 6;
32 *set4 << 9 << 7 << 5 << 3 << 1 << 2 << 4 << 6 << 8 << 10 << 1 << 6;
33
33
34 series0->addBarSet(set0);
34 series0->addBarSet(set0);
35 series0->addBarSet(set1);
35 // series0->addBarSet(set1);
36 series0->addBarSet(set2);
36 // series0->addBarSet(set2);
37 series0->addBarSet(set3);
37 // series0->addBarSet(set3);
38 series0->addBarSet(set4);
38 series0->addBarSet(set4);
39
39
40 ChartWidget* chartWidget = new ChartWidget(&window);
40 ChartWidget* chartWidget = new ChartWidget(&window);
@@ -16,11 +16,13 m_shadesEnabled(true),
16 m_grid(parent),
16 m_grid(parent),
17 m_shades(parent),
17 m_shades(parent),
18 m_labels(parent),
18 m_labels(parent),
19 m_origin(0,0)
19 m_axis(parent)
20 {
20 {
21 //initial initialization
21 //initial initialization
22 m_axis.setZValue(ChartPresenter::AxisZValue);
22 m_shades.setZValue(ChartPresenter::ShadesZValue);
23 m_shades.setZValue(ChartPresenter::ShadesZValue);
23 m_grid.setZValue(ChartPresenter::GridZValue);
24 m_grid.setZValue(ChartPresenter::GridZValue);
25 setFlags(QGraphicsItem::ItemHasNoContents);
24 }
26 }
25
27
26 AxisItem::~AxisItem()
28 AxisItem::~AxisItem()
@@ -34,10 +36,12 QRectF AxisItem::boundingRect() const
34
36
35 void AxisItem::createItems(int count)
37 void AxisItem::createItems(int count)
36 {
38 {
39 m_axis.addToGroup(new QGraphicsLineItem(this));
37 for (int i = 0; i < count; ++i) {
40 for (int i = 0; i < count; ++i) {
38 m_grid.addToGroup(new QGraphicsLineItem(this));
41 m_grid.addToGroup(new QGraphicsLineItem(this));
39 m_labels.addToGroup(new QGraphicsSimpleTextItem(this));
42 m_labels.addToGroup(new QGraphicsSimpleTextItem(this));
40 if(i%2) m_shades.addToGroup(new QGraphicsRectItem(this));
43 if(i%2) m_shades.addToGroup(new QGraphicsRectItem(this));
44 m_axis.addToGroup(new QGraphicsLineItem(this));
41 }
45 }
42 }
46 }
43
47
@@ -55,6 +59,10 void AxisItem::clear()
55 delete item;
59 delete item;
56 }
60 }
57
61
62 foreach(QGraphicsItem* item , m_axis.childItems()) {
63 delete item;
64 }
65
58 m_thicksList.clear();
66 m_thicksList.clear();
59
67
60 }
68 }
@@ -66,10 +74,12 void AxisItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
66
74
67 void AxisItem::updateItem(int count)
75 void AxisItem::updateItem(int count)
68 {
76 {
77 if(count ==0) return;
69
78
70 QList<QGraphicsItem *> lines = m_grid.childItems();
79 QList<QGraphicsItem *> lines = m_grid.childItems();
71 QList<QGraphicsItem *> labels = m_labels.childItems();
80 QList<QGraphicsItem *> labels = m_labels.childItems();
72 QList<QGraphicsItem *> shades = m_shades.childItems();
81 QList<QGraphicsItem *> shades = m_shades.childItems();
82 QList<QGraphicsItem *> axis = m_axis.childItems();
73
83
74 switch (m_type)
84 switch (m_type)
75 {
85 {
@@ -77,7 +87,8 void AxisItem::updateItem(int count)
77 {
87 {
78 const qreal deltaX = m_rect.width() / (count-1);
88 const qreal deltaX = m_rect.width() / (count-1);
79
89
80 m_axis.setLine(m_rect.left(), m_rect.bottom(), m_rect.right(), m_rect.bottom());
90 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
91 lineItem->setLine(m_rect.left(), m_rect.bottom(), m_rect.right(), m_rect.bottom());
81
92
82 for (int i = 0; i < count; ++i) {
93 for (int i = 0; i < count; ++i) {
83 int x = i * deltaX + m_rect.left();
94 int x = i * deltaX + m_rect.left();
@@ -88,11 +99,12 void AxisItem::updateItem(int count)
88 QPointF center = labelItem->boundingRect().center();
99 QPointF center = labelItem->boundingRect().center();
89 labelItem->setTransformOriginPoint(center.x(), center.y());
100 labelItem->setTransformOriginPoint(center.x(), center.y());
90 labelItem->setPos(x - center.x(), m_rect.bottom() + label_padding);
101 labelItem->setPos(x - center.x(), m_rect.bottom() + label_padding);
91
92 if(i%2){
102 if(i%2){
93 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2));
103 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2));
94 rectItem->setRect(x,m_rect.top(),deltaX,m_rect.height());
104 rectItem->setRect(x,m_rect.top(),deltaX,m_rect.height());
95 }
105 }
106 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
107 lineItem->setLine(x,m_rect.bottom(),x,m_rect.bottom()+5);
96 }
108 }
97 }
109 }
98 break;
110 break;
@@ -101,7 +113,8 void AxisItem::updateItem(int count)
101 {
113 {
102 const qreal deltaY = m_rect.height()/ (count-1);
114 const qreal deltaY = m_rect.height()/ (count-1);
103
115
104 m_axis.setLine(m_rect.left() , m_rect.top(), m_rect.left(), m_rect.bottom());
116 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
117 lineItem->setLine(m_rect.left() , m_rect.top(), m_rect.left(), m_rect.bottom());
105
118
106 for (int i = 0; i < count; ++i) {
119 for (int i = 0; i < count; ++i) {
107 int y = i * -deltaY + m_rect.bottom();
120 int y = i * -deltaY + m_rect.bottom();
@@ -116,6 +129,8 void AxisItem::updateItem(int count)
116 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2));
129 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2));
117 rectItem->setRect(m_rect.left(),y,m_rect.width(),deltaY);
130 rectItem->setRect(m_rect.left(),y,m_rect.width(),deltaY);
118 }
131 }
132 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
133 lineItem->setLine(m_rect.left()-5,y,m_rect.left(),y);
119 }
134 }
120 }
135 }
121 break;
136 break;
@@ -170,14 +185,14 void AxisItem::handleLabelsChanged(QChartAxis* axis,const QStringList& labels)
170 {
185 {
171 m_thicksList=labels;
186 m_thicksList=labels;
172 QList<QGraphicsItem*> items = m_labels.childItems();
187 QList<QGraphicsItem*> items = m_labels.childItems();
173 if(items.size()!=m_thicksList.size()){
188 //if(items.size()!=m_thicksList.size()){
174 clear();
189 clear();
175 m_thicksList=labels;
190 m_thicksList=labels;
176 createItems(m_thicksList.size());
191 createItems(m_thicksList.size());
177 updateItem(m_thicksList.size());
192 updateItem(m_thicksList.size());
178 items = m_labels.childItems();
193 items = m_labels.childItems();
179 handleAxisUpdate(axis);
194 handleAxisUpdate(axis);
180 }
195 // }
181
196
182 Q_ASSERT(items.size()==m_thicksList.size());
197 Q_ASSERT(items.size()==m_thicksList.size());
183
198
@@ -283,7 +298,9 void AxisItem::setShadesPen(const QPen& pen)
283
298
284 void AxisItem::setAxisPen(const QPen& pen)
299 void AxisItem::setAxisPen(const QPen& pen)
285 {
300 {
286 m_axis.setPen(pen);
301 foreach(QGraphicsItem* item , m_axis.childItems()) {
302 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
303 }
287 }
304 }
288
305
289 void AxisItem::setGridPen(const QPen& pen)
306 void AxisItem::setGridPen(const QPen& pen)
@@ -66,9 +66,8 private:
66 QGraphicsItemGroup m_grid;
66 QGraphicsItemGroup m_grid;
67 QGraphicsItemGroup m_shades;
67 QGraphicsItemGroup m_shades;
68 QGraphicsItemGroup m_labels;
68 QGraphicsItemGroup m_labels;
69 QGraphicsLineItem m_axis;
69 QGraphicsItemGroup m_axis;
70 QStringList m_thicksList;
70 QStringList m_thicksList;
71 QPointF m_origin;
72
71
73 };
72 };
74
73
@@ -63,7 +63,7 void Bar::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidg
63
63
64 QRectF Bar::boundingRect() const
64 QRectF Bar::boundingRect() const
65 {
65 {
66 QRectF r(mXpos, mYpos, mXpos + mWidth, mYpos + mHeight);
66 QRectF r(mXpos, mYpos, mWidth, mHeight);
67 return r;
67 return r;
68 }
68 }
69
69
@@ -51,10 +51,9 QBarSet* BarChartModel::nextSet(bool getFirst)
51 return set;
51 return set;
52 }
52 }
53
53
54
54 QBarSet* BarChartModel::setAt(int index)
55 QBarSet& BarChartModel::setAt(int index)
56 {
55 {
57 return *mDataModel.at(index);
56 return mDataModel.at(index);
58 }
57 }
59
58
60 int BarChartModel::countSets()
59 int BarChartModel::countSets()
@@ -23,8 +23,7 public:
23 void addBarSet(QBarSet *set);
23 void addBarSet(QBarSet *set);
24 void removeBarSet(QBarSet *set);
24 void removeBarSet(QBarSet *set);
25 QBarSet* nextSet(bool getFirst);
25 QBarSet* nextSet(bool getFirst);
26
26 QBarSet *setAt(int index);
27 QBarSet& setAt(int index); // Internal
28
27
29 int countSets(); // Number of sets in model
28 int countSets(); // Number of sets in model
30 int countCategories(); // Number of categories
29 int countCategories(); // Number of categories
@@ -29,7 +29,7 void BarLabel::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
29
29
30 QRectF BarLabel::boundingRect() const
30 QRectF BarLabel::boundingRect() const
31 {
31 {
32 QRectF r(mXpos, mYpos, mXpos + mSize.width(), mYpos + mSize.height());
32 QRectF r(mXpos, mYpos, mSize.width(), mSize.height());
33 return r;
33 return r;
34 }
34 }
35
35
@@ -52,7 +52,7 void BarPresenter::layoutChanged()
52
52
53 // TODO: width settable per bar?
53 // TODO: width settable per bar?
54 bar->resize(mBarDefaultWidth, barHeight);
54 bar->resize(mBarDefaultWidth, barHeight);
55 bar->setBrush(mModel.setAt(set).brush());
55 bar->setBrush(mModel.setAt(set)->brush());
56 bar->setPos(xPos, yPos-barHeight); // item*posStep+startPos + set * mBarDefaultWidth, mHeight);
56 bar->setPos(xPos, yPos-barHeight); // item*posStep+startPos + set * mBarDefaultWidth, mHeight);
57 itemIndex++;
57 itemIndex++;
58 xPos += mBarDefaultWidth;
58 xPos += mBarDefaultWidth;
@@ -63,13 +63,13 void BarPresenterBase::dataChanged()
63 mFloatingValues.clear();
63 mFloatingValues.clear();
64
64
65 // Create new graphic items for bars
65 // Create new graphic items for bars
66 for (int s=0; s<mModel.countSets(); s++) {
66 for (int c=0; c<mModel.countCategories(); c++) {
67 QBarSet *set = mModel.nextSet(0==s);
67 for (int s=0; s<mModel.countSets(); s++) {
68 for (int c=0; c<mModel.countCategories(); c++) {
68 QBarSet *set = mModel.setAt(s);
69 Bar *bar = new Bar(this);
69 Bar *bar = new Bar(this);
70 childItems().append(bar);
70 childItems().append(bar);
71 mBars.append(bar);
71 mBars.append(bar);
72 connect(bar,SIGNAL(clicked()),set,SLOT(toggleFloatingValuesVisible()));
72 connect(bar,SIGNAL(clicked()),set,SLOT(barClicked()));
73 }
73 }
74 }
74 }
75
75
@@ -92,31 +92,20 void BarPresenterBase::dataChanged()
92 }
92 }
93
93
94 // Create floating values
94 // Create floating values
95 for (int s=0; s<mModel.countSets(); s++) {
95 for (int category=0; category<mModel.countCategories(); category++) {
96 QBarSet *set = mModel.nextSet(0==s);
96 for (int s=0; s<mModel.countSets(); s++) {
97 for (int category=0; category<mModel.countCategories(); category++) {
97 QBarSet *set = mModel.setAt(s);
98 BarValue *value = new BarValue(*set, this);
98 BarValue *value = new BarValue(*set, this);
99 childItems().append(value);
99 childItems().append(value);
100 mFloatingValues.append(value);
100 mFloatingValues.append(value);
101 connect(set,SIGNAL(clicked()),value,SLOT(toggleVisible()));
101 }
102 }
102 connect(set,SIGNAL(setFloatingValuesVisible(QBarSet*)),this,SLOT(setFloatingValues(QBarSet*)));
103 }
103 }
104
104
105 // TODO: if (autolayout) { layoutChanged() } or something
105 // TODO: if (autolayout) { layoutChanged() } or something
106 mLayoutDirty = true;
106 mLayoutDirty = true;
107 }
107 }
108
108
109 void BarPresenterBase::setFloatingValues(QBarSet *set)
110 {
111 qDebug() << "BarPresenterBase::setFloatingValues";
112 // TODO: better way to map set to BarValues?
113 for (int i=0; i<mFloatingValues.count(); i++) {
114 if (mFloatingValues.at(i)->belongsToSet(set)) {
115 mFloatingValues.at(i)->setVisible(set->isFloatingValuesVisible());
116 }
117 }
118 }
119
120 //handlers
109 //handlers
121
110
122 void BarPresenterBase::handleModelChanged(int index)
111 void BarPresenterBase::handleModelChanged(int index)
@@ -41,9 +41,6 public:
41 virtual void dataChanged(); // data of series has changed -> need to recalculate bar sizes
41 virtual void dataChanged(); // data of series has changed -> need to recalculate bar sizes
42 virtual void layoutChanged() = 0; // layout has changed -> need to recalculate bar sizes
42 virtual void layoutChanged() = 0; // layout has changed -> need to recalculate bar sizes
43
43
44 public Q_SLOTS:
45 void setFloatingValues(QBarSet *set);
46
47 protected slots:
44 protected slots:
48 void handleModelChanged(int index);
45 void handleModelChanged(int index);
49 void handleDomainChanged(const Domain& domain);
46 void handleDomainChanged(const Domain& domain);
@@ -5,10 +5,10
5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
6
6
7 BarValue::BarValue(QBarSet &set, QGraphicsItem *parent)
7 BarValue::BarValue(QBarSet &set, QGraphicsItem *parent)
8 : QGraphicsItem(parent)
8 : QGraphicsObject(parent)
9 ,mBarSet(set)
9 ,mBarSet(set)
10 {
10 {
11 // setVisible(false);
11 setVisible(false);
12 }
12 }
13
13
14 void BarValue::setValueString(QString str)
14 void BarValue::setValueString(QString str)
@@ -56,9 +56,14 void BarValue::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
56
56
57 QRectF BarValue::boundingRect() const
57 QRectF BarValue::boundingRect() const
58 {
58 {
59 QRectF r(mXpos, mYpos, mXpos + mWidth, mYpos + mHeight);
59 QRectF r(mXpos, mYpos, mWidth, mHeight);
60 return r;
60 return r;
61 }
61 }
62
62
63 void BarValue::toggleVisible()
64 {
65 setVisible(!isVisible());
66 }
63
67
68 #include "moc_barvalue_p.cpp"
64 QTCOMMERCIALCHART_END_NAMESPACE
69 QTCOMMERCIALCHART_END_NAMESPACE
@@ -2,7 +2,7
2 #define BARVALUE_P_H
2 #define BARVALUE_P_H
3
3
4 #include "qchartglobal.h"
4 #include "qchartglobal.h"
5 #include <QGraphicsItem>
5 #include <QGraphicsObject>
6 #include <QPen>
6 #include <QPen>
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
@@ -12,8 +12,9 class QBarSet;
12 // Visual class for floating bar values
12 // Visual class for floating bar values
13 // TODO: fonts, colors etc.
13 // TODO: fonts, colors etc.
14 // By default these are not visible.
14 // By default these are not visible.
15 class BarValue : public QGraphicsItem // TODO: public QGraphicsObject for signals?
15 class BarValue : public QGraphicsObject
16 {
16 {
17 Q_OBJECT
17 public:
18 public:
18 BarValue(QBarSet &set, QGraphicsItem *parent = 0);
19 BarValue(QBarSet &set, QGraphicsItem *parent = 0);
19
20
@@ -32,6 +33,9 public:
32 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
33 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
33 QRectF boundingRect() const;
34 QRectF boundingRect() const;
34
35
36 public Q_SLOTS:
37 void toggleVisible();
38
35 private:
39 private:
36
40
37 QBarSet& mBarSet;
41 QBarSet& mBarSet;
@@ -50,7 +50,7 void PercentBarPresenter::layoutChanged()
50
50
51 // TODO: width settable per bar?
51 // TODO: width settable per bar?
52 bar->resize(mBarDefaultWidth, barHeight);
52 bar->resize(mBarDefaultWidth, barHeight);
53 bar->setBrush(mModel.setAt(set).brush());
53 bar->setBrush(mModel.setAt(set)->brush());
54 bar->setPos(xPos, yPos-barHeight);
54 bar->setPos(xPos, yPos-barHeight);
55 itemIndex++;
55 itemIndex++;
56 yPos -= barHeight;
56 yPos -= barHeight;
@@ -65,24 +65,11 bool QBarSet::isFloatingValuesVisible()
65
65
66 void QBarSet::barClicked()
66 void QBarSet::barClicked()
67 {
67 {
68 qDebug() << "QBarset::barClicked";
68 qDebug() << "QBarset::barClicked" << this;
69 // Some bar of this set has been clicked
69 // Some bar of this set has been clicked
70 // TODO: What happens then?
70 // TODO: What happens then?
71 emit clicked(); // Notify that set has been clicked
71 emit clicked(); // Notify that set has been clicked
72 }
72 }
73
73
74 void QBarSet::toggleFloatingValuesVisible()
75 {
76 qDebug() << "QBarset::toggleFloatingValuesVisible";
77 // TODO: toggle vs explicit set?
78 if (mFloatingValuesVisible) {
79 mFloatingValuesVisible=false;
80 } else {
81 mFloatingValuesVisible=true;
82 }
83 emit setFloatingValuesVisible(this);
84 }
85
86
87 #include "moc_qbarset.cpp"
74 #include "moc_qbarset.cpp"
88 QTCOMMERCIALCHART_END_NAMESPACE
75 QTCOMMERCIALCHART_END_NAMESPACE
@@ -31,7 +31,6 public:
31
31
32 Q_SIGNALS:
32 Q_SIGNALS:
33 void clicked();
33 void clicked();
34 void setFloatingValuesVisible(QBarSet* set);
35 /*
34 /*
36 void hoverEnter();
35 void hoverEnter();
37 void hoverLeave();
36 void hoverLeave();
@@ -39,7 +38,6 Q_SIGNALS:
39
38
40 public Q_SLOTS:
39 public Q_SLOTS:
41 void barClicked();
40 void barClicked();
42 void toggleFloatingValuesVisible();
43
41
44 private:
42 private:
45
43
@@ -55,7 +55,7 void StackedBarPresenter::layoutChanged()
55 Bar* bar = mBars.at(itemIndex);
55 Bar* bar = mBars.at(itemIndex);
56
56
57 bar->resize(mBarDefaultWidth, barHeight);
57 bar->resize(mBarDefaultWidth, barHeight);
58 bar->setBrush(mModel.setAt(set).brush());
58 bar->setBrush(mModel.setAt(set)->brush());
59 bar->setPos(xPos, yPos-barHeight);
59 bar->setPos(xPos, yPos-barHeight);
60 itemIndex++;
60 itemIndex++;
61 yPos -= barHeight;
61 yPos -= barHeight;
@@ -104,6 +104,7 void QChart::createChartBackgroundItem()
104 {
104 {
105 if(!m_backgroundItem) {
105 if(!m_backgroundItem) {
106 m_backgroundItem = new QGraphicsRectItem(this);
106 m_backgroundItem = new QGraphicsRectItem(this);
107 m_backgroundItem->setPen(Qt::NoPen);
107 m_backgroundItem->setZValue(ChartPresenter::BackgroundZValue);
108 m_backgroundItem->setZValue(ChartPresenter::BackgroundZValue);
108 }
109 }
109 }
110 }
General Comments 0
You need to be logged in to leave comments. Login now