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