##// END OF EJS Templates
Added support for adding and removing data with model. Updated the example
Marek Rosa -
r545:366c5163e81a
parent child
Show More
@@ -1,126 +1,144
1 #include "customtablemodel.h"
1 #include "customtablemodel.h"
2
2
3 CustomTableModel::CustomTableModel(QObject *parent) :
3 CustomTableModel::CustomTableModel(QObject *parent) :
4 QAbstractTableModel(parent)
4 QAbstractTableModel(parent)
5 {
5 {
6 m_points.append(QPointF(10, 50));
6 m_points.append(QPointF(10, 50));
7 m_labels.append("Apples");
7 m_labels.append("Apples");
8 m_points.append(QPointF(60, 70));
8 m_points.append(QPointF(60, 70));
9 m_labels.append("Oranges");
9 m_labels.append("Oranges");
10 m_points.append(QPointF(110, 50));
10 m_points.append(QPointF(110, 50));
11 m_labels.append("Bananas");
11 m_labels.append("Bananas");
12 m_points.append(QPointF(140, 40));
12 m_points.append(QPointF(140, 40));
13 m_labels.append("Lemons");
13 m_labels.append("Lemons");
14 m_points.append(QPointF(200, 150));
14 m_points.append(QPointF(200, 150));
15 m_labels.append("Plums");
15 m_labels.append("Plums");
16 m_points.append(QPointF(225, 75));
16 m_points.append(QPointF(225, 75));
17 m_labels.append("Pearls");
17 m_labels.append("Pearls");
18 }
18 }
19
19
20 int CustomTableModel::rowCount(const QModelIndex & parent) const
20 int CustomTableModel::rowCount(const QModelIndex & parent) const
21 {
21 {
22 return m_points.count();
22 return m_points.count();
23 }
23 }
24
24
25 int CustomTableModel::columnCount(const QModelIndex & parent) const
25 int CustomTableModel::columnCount(const QModelIndex & parent) const
26 {
26 {
27 return 2;
27 return 3;
28 }
28 }
29
29
30 QVariant CustomTableModel::headerData (int section, Qt::Orientation orientation, int role ) const
30 QVariant CustomTableModel::headerData (int section, Qt::Orientation orientation, int role ) const
31 {
31 {
32 if (role != Qt::DisplayRole)
32 if (role != Qt::DisplayRole)
33 return QVariant();
33 return QVariant();
34
34
35 if (orientation == Qt::Horizontal)
35 if (orientation == Qt::Horizontal)
36 {
36 {
37 switch(section)
37 switch(section)
38 {
38 {
39 case 0:
39 case 0:
40 return "x";
40 return "x";
41 case 1:
41 case 1:
42 return "y";
42 return "y";
43 case 2:
43 case 2:
44 return "Fruit";
44 return "Fruit";
45 default:
45 default:
46 return "What?";
46 return "What?";
47 }
47 }
48 }
48 }
49 else
49 else
50 return QString("%1").arg(section + 1);
50 return QString("%1").arg(section + 1);
51 }
51 }
52
52
53 QVariant CustomTableModel::data(const QModelIndex & index, int role) const
53 QVariant CustomTableModel::data(const QModelIndex & index, int role) const
54 {
54 {
55 if (role == Qt::DisplayRole)
55 if (role == Qt::DisplayRole)
56 {
56 {
57 switch(index.column())
57 switch(index.column())
58 {
58 {
59 case 0:
59 case 0:
60 return m_points[index.row()].x();
60 return m_points[index.row()].x();
61 case 1:
61 case 1:
62 return m_points[index.row()].y();
62 return m_points[index.row()].y();
63 case 2:
63 case 2:
64 return m_labels[index.row()];
64 return m_labels[index.row()];
65 default:
65 default:
66 return QVariant();
66 return QVariant();
67 }
67 }
68 }
68 }
69 else if (role == Qt::EditRole)
69 else if (role == Qt::EditRole)
70 {
70 {
71 switch(index.column())
71 switch(index.column())
72 {
72 {
73 case 0:
73 case 0:
74 return m_points[index.row()].x();
74 return m_points[index.row()].x();
75 case 1:
75 case 1:
76 return m_points[index.row()].y();
76 return m_points[index.row()].y();
77 case 2:
77 case 2:
78 return m_labels[index.row()];
78 return m_labels[index.row()];
79 default:
79 default:
80 return QVariant();
80 return QVariant();
81 }
81 }
82 }
82 }
83 }
83 }
84
84
85 bool CustomTableModel::setData ( const QModelIndex & index, const QVariant & value, int role)
85 bool CustomTableModel::setData ( const QModelIndex & index, const QVariant & value, int role)
86 {
86 {
87 if (index.isValid() && role == Qt::EditRole)
87 if (index.isValid() && role == Qt::EditRole)
88 {
88 {
89 switch(index.column())
89 switch(index.column())
90 {
90 {
91 case 0:
91 case 0:
92 m_points[index.row()].setX(value.toDouble());
92 m_points[index.row()].setX(value.toDouble());
93 break;
93 break;
94 case 1:
94 case 1:
95 m_points[index.row()].setY(value.toDouble());
95 m_points[index.row()].setY(value.toDouble());
96 break;
96 break;
97 case 2:
97 case 2:
98 m_labels.replace(index.row(), value.toString());
98 m_labels.replace(index.row(), value.toString());
99 break;
99 break;
100 default:
100 default:
101 return false;
101 return false;
102 }
102 }
103 emit dataChanged(index, index);
103 emit dataChanged(index, index);
104 return true;
104 return true;
105 }
105 }
106 return false;
106 return false;
107 }
107 }
108
108
109 Qt::ItemFlags CustomTableModel::flags ( const QModelIndex & index ) const
109 Qt::ItemFlags CustomTableModel::flags ( const QModelIndex & index ) const
110 {
110 {
111 // if (!index.isValid())
111 // if (!index.isValid())
112 // return Qt::ItemIsEnabled;
112 // return Qt::ItemIsEnabled;
113 return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
113 return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
114 }
114 }
115
115
116 bool CustomTableModel::insertRows ( int row, int count, const QModelIndex & parent)
116 bool CustomTableModel::insertRows ( int row, int count, const QModelIndex & parent)
117 {
117 {
118 beginInsertRows(QModelIndex(), row /*dataTable.count()*/, row + count - 1);
118 beginInsertRows(QModelIndex(), row /*dataTable.count()*/, row + count - 1);
119 for (int i = 0; i < count; i++)
119 for (int i = row; i < row + count; i++)
120 {
120 {
121 m_points.append(QPointF());
121 m_points.insert(row, QPointF());
122 m_labels.append("");
122 m_labels.insert(row,(""));
123 }
123 }
124 endInsertRows();
124 endInsertRows();
125 return true;
125 return true;
126 }
126 }
127
128 bool CustomTableModel::removeRows ( int row, int count, const QModelIndex & parent)
129 {
130 if (row > this->rowCount() - 1)
131 return false;
132 if (row < 0)
133 row = 0;
134 if (row + count > rowCount())
135 return false;
136 beginRemoveRows(parent, row, row + count - 1);
137 for (int i = row; i < row + count; i++)
138 {
139 m_points.removeAt(row);
140 m_labels.removeAt(row);
141 }
142 endRemoveRows();
143 return true;
144 }
@@ -1,32 +1,33
1 #ifndef XYPOINTSMODEL_H
1 #ifndef XYPOINTSMODEL_H
2 #define XYPOINTSMODEL_H
2 #define XYPOINTSMODEL_H
3
3
4 #include <QAbstractTableModel>
4 #include <QAbstractTableModel>
5 #include <QPointF>
5 #include <QPointF>
6 #include <QStringList>
6 #include <QStringList>
7
7
8 class CustomTableModel : public QAbstractTableModel
8 class CustomTableModel : public QAbstractTableModel
9 {
9 {
10 Q_OBJECT
10 Q_OBJECT
11 public:
11 public:
12 explicit CustomTableModel(QObject *parent = 0);
12 explicit CustomTableModel(QObject *parent = 0);
13
13
14 int rowCount ( const QModelIndex & parent = QModelIndex() ) const;
14 int rowCount ( const QModelIndex & parent = QModelIndex() ) const;
15 int columnCount ( const QModelIndex & parent = QModelIndex() ) const;
15 int columnCount ( const QModelIndex & parent = QModelIndex() ) const;
16 QVariant headerData (int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const;
16 QVariant headerData (int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const;
17 QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
17 QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
18 bool setData ( const QModelIndex & index, const QVariant & value, int role = Qt::EditRole );
18 bool setData ( const QModelIndex & index, const QVariant & value, int role = Qt::EditRole );
19 Qt::ItemFlags flags ( const QModelIndex & index ) const;
19 Qt::ItemFlags flags ( const QModelIndex & index ) const;
20 bool insertRows ( int row, int count, const QModelIndex & parent = QModelIndex() );
20 bool insertRows ( int row, int count, const QModelIndex & parent = QModelIndex() );
21 bool removeRows ( int row, int count, const QModelIndex & parent = QModelIndex() );
21
22
22 //signals:
23 //signals:
23
24
24 //public slots:
25 //public slots:
25 private:
26 private:
26 QList<QPointF> m_points;
27 QList<QPointF> m_points;
27 QStringList m_labels;
28 QStringList m_labels;
28
29
29
30
30 };
31 };
31
32
32 #endif // XYPOINTSMODEL_H
33 #endif // XYPOINTSMODEL_H
@@ -1,54 +1,109
1 #include "tablewidget.h"
1 #include "tablewidget.h"
2 #include <QGridLayout>
2 #include <QGridLayout>
3 #include <QTableView>
3 #include <QTableView>
4 #include <QStyledItemDelegate>
4 #include <QStyledItemDelegate>
5 #include "qlineseries.h"
5 #include "qlineseries.h"
6 #include "qsplineseries.h"
6 #include "qsplineseries.h"
7 #include "qscatterseries.h"
7 #include "customtablemodel.h"
8 #include "customtablemodel.h"
8 #include "qpieseries.h"
9 #include "qpieseries.h"
10 #include <QPushButton>
11 #include <QRadioButton>
9
12
10 TableWidget::TableWidget(QWidget *parent)
13 TableWidget::TableWidget(QWidget *parent)
11 : QWidget(parent)
14 : QWidget(parent)
12 {
15 {
13
14
15 // create simple model for storing data
16 // create simple model for storing data
16 // user's table data model
17 // user's table data model
17 CustomTableModel* model = new CustomTableModel;
18 m_model = new CustomTableModel;
18 QTableView* tableView = new QTableView;
19 tableView = new QTableView;
19 tableView->setModel(model);
20 tableView->setModel(m_model);
20 tableView->setMinimumSize(340, 480);
21 tableView->setMinimumSize(340, 480);
21 // tableView->setItemDelegate(new QStyledItemDelegate);
22 // tableView->setItemDelegate(new QStyledItemDelegate);
22 chartView = new QChartView;
23 chartView = new QChartView;
23 chartView->setMinimumSize(640, 480);
24 chartView->setMinimumSize(640, 480);
24
25
25 // create
26 // create
26 // QLineSeries* series = new QLineSeries;
27 // QLineSeries* series = new QLineSeries;
27 QSplineSeries* series = new QSplineSeries;
28 // QSplineSeries* series = new QSplineSeries;
28 series->setModel(model);
29 // QScatterSeries* series = new QScatterSeries;
29 series->setModelMapping(0,1, Qt::Vertical);
30 // series->setModel(m_model);
30 // series->setModelMappingY(1);
31 // series->setModelMapping(0,1, Qt::Vertical);
31
32 // series->add(QPointF(150, 100));
33 // series->add(QPointF(200, 130));
34 // series->add(QPointF(250, 120));
35 // series->add(QPointF(300, 140));
36 // series->add(QPointF(350, 160));
37
32
38 // QPieSeries* pieSeries = new QPieSeries;
33 // QPieSeries* pieSeries = new QPieSeries;
39 // pieSeries->setModel(model);
34 // pieSeries->setModel(model);
40 // pieSeries
35 // pieSeries
41
36
42 chartView->addSeries(series);
37 // chartView->addSeries(series);
38
39 // add, remove data buttons
40 QPushButton* addRowButton = new QPushButton("Add row");
41 connect(addRowButton, SIGNAL(clicked()), this, SLOT(addRow()));
42
43 QPushButton* removeRowButton = new QPushButton("Remove row");
44 connect(removeRowButton, SIGNAL(clicked()), this, SLOT(removeRow()));
45
46 // buttons layout
47 QVBoxLayout* buttonsLayout = new QVBoxLayout;
48 buttonsLayout->addWidget(addRowButton);
49 buttonsLayout->addWidget(removeRowButton);
50 buttonsLayout->addStretch();
51
52 // chart type radio buttons
53 lineRadioButton = new QRadioButton("Line");
54 splineRadioButton = new QRadioButton("Spline");
55 scatterRadioButton = new QRadioButton("Scatter");
56
57 connect(lineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType()));
58 connect(splineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType()));
59 connect(scatterRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType()));
60 lineRadioButton->setChecked(true);
61
62 // radio buttons layout
63 QHBoxLayout* radioLayout = new QHBoxLayout;
64 radioLayout->addWidget(lineRadioButton);
65 radioLayout->addWidget(splineRadioButton);
66 radioLayout->addWidget(scatterRadioButton);
43
67
44 // create main layout
68 // create main layout
45 QGridLayout* mainLayout = new QGridLayout;
69 QGridLayout* mainLayout = new QGridLayout;
70 mainLayout->addLayout(buttonsLayout, 0, 1);
71 mainLayout->addLayout(radioLayout, 0, 2);
46 mainLayout->addWidget(tableView, 1, 1);
72 mainLayout->addWidget(tableView, 1, 1);
47 mainLayout->addWidget(chartView, 1, 2);
73 mainLayout->addWidget(chartView, 1, 2);
48 setLayout(mainLayout);
74 setLayout(mainLayout);
49 }
75 }
50
76
77 void TableWidget::addRow()
78 {
79 // m_model->insertRow(m_model->rowCount());
80 m_model->insertRow(tableView->currentIndex().row() + 1);
81
82 }
83
84 void TableWidget::removeRow()
85 {
86 // m_model->removeRow(m_model->rowCount() - 1);
87 m_model->removeRow(tableView->currentIndex().row());
88 }
89
90 void TableWidget::updateChartType()
91 {
92 chartView->removeAllSeries();
93
94 if (lineRadioButton->isChecked())
95 series = new QLineSeries;
96 else if (splineRadioButton->isChecked())
97 series = new QSplineSeries;
98 else
99 series = new QScatterSeries;
100
101 series->setModel(m_model);
102 series->setModelMapping(0,1, Qt::Vertical);
103 chartView->addSeries(series);
104 }
105
51 TableWidget::~TableWidget()
106 TableWidget::~TableWidget()
52 {
107 {
53
108
54 }
109 }
@@ -1,23 +1,39
1 #ifndef TABLEWIDGET_H
1 #ifndef TABLEWIDGET_H
2 #define TABLEWIDGET_H
2 #define TABLEWIDGET_H
3
3
4 #include <QtGui/QWidget>
4 #include <QtGui/QWidget>
5 #include "qchartview.h"
5 #include "qchartview.h"
6 #include "qxyseries.h"
6
7
7 QTCOMMERCIALCHART_USE_NAMESPACE
8 QTCOMMERCIALCHART_USE_NAMESPACE
8
9
10 class CustomTableModel;
11 class QTableView;
12 class QRadioButton;
13 //class QSeries;
14
9 class TableWidget : public QWidget
15 class TableWidget : public QWidget
10 {
16 {
11 Q_OBJECT
17 Q_OBJECT
12
18
13 public:
19 public:
14 TableWidget(QWidget *parent = 0);
20 TableWidget(QWidget *parent = 0);
15 ~TableWidget();
21 ~TableWidget();
16
22
17
23
24 public slots:
25 void addRow();
26 void removeRow();
27 void updateChartType();
18
28
19 private:
29 private:
20 QChartView* chartView;
30 QChartView* chartView;
31 QXYSeries* series;
32 CustomTableModel* m_model;
33 QTableView* tableView;
34 QRadioButton* lineRadioButton;
35 QRadioButton* splineRadioButton;
36 QRadioButton* scatterRadioButton;
21 };
37 };
22
38
23 #endif // TABLEWIDGET_H
39 #endif // TABLEWIDGET_H
@@ -1,84 +1,88
1 #include "linechartitem_p.h"
1 #include "linechartitem_p.h"
2 #include "qlineseries.h"
2 #include "qlineseries.h"
3 #include "chartpresenter_p.h"
3 #include "chartpresenter_p.h"
4 #include <QPainter>
4 #include <QPainter>
5 #include <QGraphicsSceneMouseEvent>
5 #include <QGraphicsSceneMouseEvent>
6
6
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 //TODO: optimize : remove points which are not visible
10 //TODO: optimize : remove points which are not visible
11
11
12 LineChartItem::LineChartItem(QLineSeries* series,QGraphicsItem *parent):XYChartItem(series,parent),
12 LineChartItem::LineChartItem(QLineSeries* series,QGraphicsItem *parent):XYChartItem(series,parent),
13 m_series(series),
13 m_series(series),
14 m_pointsVisible(false)
14 m_pointsVisible(false)
15 {
15 {
16 setZValue(ChartPresenter::LineChartZValue);
16 setZValue(ChartPresenter::LineChartZValue);
17 QObject::connect(series,SIGNAL(updated()),this,SLOT(handleUpdated()));
17 QObject::connect(series,SIGNAL(updated()),this,SLOT(handleUpdated()));
18 QObject::connect(this,SIGNAL(clicked(const QPointF&)),series,SIGNAL(clicked(const QPointF&)));
18 QObject::connect(this,SIGNAL(clicked(const QPointF&)),series,SIGNAL(clicked(const QPointF&)));
19 handleUpdated();
19 handleUpdated();
20 }
20 }
21
21
22 QRectF LineChartItem::boundingRect() const
22 QRectF LineChartItem::boundingRect() const
23 {
23 {
24 return m_rect;
24 return m_rect;
25 }
25 }
26
26
27 QPainterPath LineChartItem::shape() const
27 QPainterPath LineChartItem::shape() const
28 {
28 {
29 return m_path;
29 return m_path;
30 }
30 }
31
31
32 void LineChartItem::setGeometry(QVector<QPointF>& points)
32 void LineChartItem::setGeometry(QVector<QPointF>& points)
33 {
33 {
34 if(points.size()==0) return;
34 if(points.size()==0)
35 {
36 XYChartItem::setGeometry(points);
37 return;
38 }
35
39
36 QList<QGraphicsItem*> items = m_items.childItems();
40 QList<QGraphicsItem*> items = m_items.childItems();
37
41
38 QPainterPath linePath(points.at(0));
42 QPainterPath linePath(points.at(0));
39
43
40 for(int i=1; i< points.size();i++) {
44 for(int i=1; i< points.size();i++) {
41 linePath.lineTo(points.at(i));
45 linePath.lineTo(points.at(i));
42 }
46 }
43
47
44 prepareGeometryChange();
48 prepareGeometryChange();
45 m_path = linePath;
49 m_path = linePath;
46 m_rect = linePath.boundingRect();
50 m_rect = linePath.boundingRect();
47
51
48 XYChartItem::setGeometry(points);
52 XYChartItem::setGeometry(points);
49 }
53 }
50
54
51 void LineChartItem::handleUpdated()
55 void LineChartItem::handleUpdated()
52 {
56 {
53 m_pointsVisible = m_series->pointsVisible();
57 m_pointsVisible = m_series->pointsVisible();
54 m_linePen = m_series->pen();
58 m_linePen = m_series->pen();
55 m_pointPen = m_series->pen();
59 m_pointPen = m_series->pen();
56 m_pointPen.setWidthF(2*m_pointPen.width());
60 m_pointPen.setWidthF(2*m_pointPen.width());
57 update();
61 update();
58 }
62 }
59
63
60 //painter
64 //painter
61
65
62 void LineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
66 void LineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
63 {
67 {
64 Q_UNUSED(widget);
68 Q_UNUSED(widget);
65 Q_UNUSED(option);
69 Q_UNUSED(option);
66 painter->save();
70 painter->save();
67 painter->setPen(m_linePen);
71 painter->setPen(m_linePen);
68 painter->setClipRect(clipRect());
72 painter->setClipRect(clipRect());
69 painter->drawPath(m_path);
73 painter->drawPath(m_path);
70 if(m_pointsVisible){
74 if(m_pointsVisible){
71 painter->setPen(m_pointPen);
75 painter->setPen(m_pointPen);
72 painter->drawPoints(points());
76 painter->drawPoints(points());
73 }
77 }
74 painter->restore();
78 painter->restore();
75 }
79 }
76
80
77 void LineChartItem::mousePressEvent( QGraphicsSceneMouseEvent * event )
81 void LineChartItem::mousePressEvent( QGraphicsSceneMouseEvent * event )
78 {
82 {
79 emit clicked(calculateDomainPoint(event->pos()));
83 emit clicked(calculateDomainPoint(event->pos()));
80 }
84 }
81
85
82 #include "moc_linechartitem_p.cpp"
86 #include "moc_linechartitem_p.cpp"
83
87
84 QTCOMMERCIALCHART_END_NAMESPACE
88 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,177 +1,181
1 #include "scatterchartitem_p.h"
1 #include "scatterchartitem_p.h"
2 #include "qscatterseries.h"
2 #include "qscatterseries.h"
3 #include "chartpresenter_p.h"
3 #include "chartpresenter_p.h"
4 #include <QPainter>
4 #include <QPainter>
5 #include <QGraphicsScene>
5 #include <QGraphicsScene>
6
6
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8
8
9
9
10
10
11
11
12
12
13
13
14
14
15 ScatterChartItem::ScatterChartItem(QScatterSeries *series, QGraphicsItem *parent) :
15 ScatterChartItem::ScatterChartItem(QScatterSeries *series, QGraphicsItem *parent) :
16 XYChartItem(series,parent),
16 XYChartItem(series,parent),
17 m_series(series),
17 m_series(series),
18 m_items(this),
18 m_items(this),
19 m_shape(QScatterSeries::MarkerShapeRectangle),
19 m_shape(QScatterSeries::MarkerShapeRectangle),
20 m_size(10)
20 m_size(10)
21
21
22 {
22 {
23 Q_ASSERT(parent);
23 Q_ASSERT(parent);
24 Q_ASSERT(series);
24 Q_ASSERT(series);
25
25
26 QObject::connect(m_series,SIGNAL(updated()), this, SLOT(handleUpdated()));
26 QObject::connect(m_series,SIGNAL(updated()), this, SLOT(handleUpdated()));
27 QObject::connect(this,SIGNAL(clicked(const QPointF&)), m_series, SIGNAL(clicked(const QPointF&)));
27 QObject::connect(this,SIGNAL(clicked(const QPointF&)), m_series, SIGNAL(clicked(const QPointF&)));
28
28
29 setZValue(ChartPresenter::ScatterSeriesZValue);
29 setZValue(ChartPresenter::ScatterSeriesZValue);
30 setFlags(QGraphicsItem::ItemHasNoContents);
30 setFlags(QGraphicsItem::ItemHasNoContents);
31 setFlags(QGraphicsItem::ItemClipsChildrenToShape);
31 setFlags(QGraphicsItem::ItemClipsChildrenToShape);
32
32
33 handleUpdated();
33 handleUpdated();
34
34
35 m_items.setHandlesChildEvents(false);
35 m_items.setHandlesChildEvents(false);
36
36
37 // TODO: how to draw a drop shadow?
37 // TODO: how to draw a drop shadow?
38 // QGraphicsDropShadowEffect *dropShadow = new QGraphicsDropShadowEffect();
38 // QGraphicsDropShadowEffect *dropShadow = new QGraphicsDropShadowEffect();
39 // dropShadow->setOffset(2.0);
39 // dropShadow->setOffset(2.0);
40 // dropShadow->setBlurRadius(2.0);
40 // dropShadow->setBlurRadius(2.0);
41 // setGraphicsEffect(dropShadow);
41 // setGraphicsEffect(dropShadow);
42 }
42 }
43
43
44
44
45 QRectF ScatterChartItem::boundingRect() const
45 QRectF ScatterChartItem::boundingRect() const
46 {
46 {
47 return m_rect;
47 return m_rect;
48 }
48 }
49
49
50 void ScatterChartItem::createPoints(int count)
50 void ScatterChartItem::createPoints(int count)
51 {
51 {
52 for (int i = 0; i < count; ++i) {
52 for (int i = 0; i < count; ++i) {
53
53
54 QGraphicsItem *item;
54 QGraphicsItem *item;
55
55
56 switch (m_shape) {
56 switch (m_shape) {
57 case QScatterSeries::MarkerShapeCircle:{
57 case QScatterSeries::MarkerShapeCircle:{
58 QGraphicsEllipseItem* i = new QGraphicsEllipseItem(0,0,m_size,m_size);
58 QGraphicsEllipseItem* i = new QGraphicsEllipseItem(0,0,m_size,m_size);
59 const QRectF& rect = i->boundingRect();
59 const QRectF& rect = i->boundingRect();
60 i->setPos(-rect.width()/2,-rect.height()/2);
60 i->setPos(-rect.width()/2,-rect.height()/2);
61 item = new Marker(i,this);
61 item = new Marker(i,this);
62 break;
62 break;
63 }
63 }
64 case QScatterSeries::MarkerShapeRectangle:{
64 case QScatterSeries::MarkerShapeRectangle:{
65 QGraphicsRectItem* i = new QGraphicsRectItem(0,0,m_size,m_size);
65 QGraphicsRectItem* i = new QGraphicsRectItem(0,0,m_size,m_size);
66 i->setPos(-m_size/2,-m_size/2);
66 i->setPos(-m_size/2,-m_size/2);
67 item = new Marker(i,this);
67 item = new Marker(i,this);
68 break;
68 break;
69 }
69 }
70 default:
70 default:
71 qWarning()<<"Unsupported marker type";
71 qWarning()<<"Unsupported marker type";
72 break;
72 break;
73
73
74 }
74 }
75 m_items.addToGroup(item);
75 m_items.addToGroup(item);
76 }
76 }
77 }
77 }
78
78
79 void ScatterChartItem::deletePoints(int count)
79 void ScatterChartItem::deletePoints(int count)
80 {
80 {
81 QList<QGraphicsItem *> items = m_items.childItems();
81 QList<QGraphicsItem *> items = m_items.childItems();
82
82
83 for (int i = 0; i < count; ++i) {
83 for (int i = 0; i < count; ++i) {
84 delete(items.takeLast());
84 delete(items.takeLast());
85 }
85 }
86 }
86 }
87
87
88 void ScatterChartItem::markerSelected(Marker* marker)
88 void ScatterChartItem::markerSelected(Marker* marker)
89 {
89 {
90 emit clicked(QPointF(m_series->x(marker->index()), m_series->y(marker->index())));
90 emit clicked(QPointF(m_series->x(marker->index()), m_series->y(marker->index())));
91 }
91 }
92
92
93 void ScatterChartItem::setGeometry(QVector<QPointF>& points)
93 void ScatterChartItem::setGeometry(QVector<QPointF>& points)
94 {
94 {
95 if(points.size()==0) return;
95 if(points.size()==0)
96 {
97 XYChartItem::setGeometry(points);
98 return;
99 }
96
100
97 int diff = XYChartItem::points().size() - points.size();
101 int diff = XYChartItem::points().size() - points.size();
98
102
99 if(diff>0) {
103 if(diff>0) {
100 deletePoints(diff);
104 deletePoints(diff);
101 }
105 }
102 else if(diff<0) {
106 else if(diff<0) {
103 createPoints(-diff);
107 createPoints(-diff);
104 }
108 }
105
109
106 if(diff!=0) handleUpdated();
110 if(diff!=0) handleUpdated();
107
111
108 QList<QGraphicsItem*> items = m_items.childItems();
112 QList<QGraphicsItem*> items = m_items.childItems();
109
113
110 for(int i=0; i< points.size();i++) {
114 for(int i=0; i< points.size();i++) {
111 Marker* item = static_cast<Marker*>(items.at(i));
115 Marker* item = static_cast<Marker*>(items.at(i));
112 const QPointF& point = points.at(i);
116 const QPointF& point = points.at(i);
113 const QRectF& rect = item->boundingRect();
117 const QRectF& rect = item->boundingRect();
114 item->setIndex(i);
118 item->setIndex(i);
115 item->setPos(point.x()-rect.width()/2,point.y()-rect.height()/2);
119 item->setPos(point.x()-rect.width()/2,point.y()-rect.height()/2);
116 if(!clipRect().contains(point)) {
120 if(!clipRect().contains(point)) {
117 item->setVisible(false);
121 item->setVisible(false);
118 }
122 }
119 else {
123 else {
120 item->setVisible(true);
124 item->setVisible(true);
121 }
125 }
122 }
126 }
123
127
124 prepareGeometryChange();
128 prepareGeometryChange();
125 m_rect = clipRect();
129 m_rect = clipRect();
126 XYChartItem::setGeometry(points);
130 XYChartItem::setGeometry(points);
127 }
131 }
128
132
129
133
130 void ScatterChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
134 void ScatterChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
131 {
135 {
132 Q_UNUSED(painter);
136 Q_UNUSED(painter);
133 Q_UNUSED(option);
137 Q_UNUSED(option);
134 Q_UNUSED(widget);
138 Q_UNUSED(widget);
135 }
139 }
136
140
137 void ScatterChartItem::setPen(const QPen& pen)
141 void ScatterChartItem::setPen(const QPen& pen)
138 {
142 {
139 foreach(QGraphicsItem* item , m_items.childItems()) {
143 foreach(QGraphicsItem* item , m_items.childItems()) {
140 static_cast<Marker*>(item)->setPen(pen);
144 static_cast<Marker*>(item)->setPen(pen);
141 }
145 }
142 }
146 }
143
147
144 void ScatterChartItem::setBrush(const QBrush& brush)
148 void ScatterChartItem::setBrush(const QBrush& brush)
145 {
149 {
146 foreach(QGraphicsItem* item , m_items.childItems()) {
150 foreach(QGraphicsItem* item , m_items.childItems()) {
147 static_cast<Marker*>(item)->setBrush(brush);
151 static_cast<Marker*>(item)->setBrush(brush);
148 }
152 }
149 }
153 }
150
154
151 void ScatterChartItem::handleUpdated()
155 void ScatterChartItem::handleUpdated()
152 {
156 {
153
157
154 int count = m_items.childItems().count();
158 int count = m_items.childItems().count();
155
159
156 if(count==0) return;
160 if(count==0) return;
157
161
158 bool recreate = m_size != m_series->size() || m_shape != m_series->shape();
162 bool recreate = m_size != m_series->size() || m_shape != m_series->shape();
159
163
160 //TODO: only rewrite on size change
164 //TODO: only rewrite on size change
161
165
162 m_size = m_series->size();
166 m_size = m_series->size();
163 m_shape = m_series->shape();
167 m_shape = m_series->shape();
164
168
165 if(recreate){
169 if(recreate){
166 deletePoints(count);
170 deletePoints(count);
167 createPoints(count);
171 createPoints(count);
168 }
172 }
169
173
170 setPen(m_series->pen());
174 setPen(m_series->pen());
171 setBrush(m_series->brush());
175 setBrush(m_series->brush());
172
176
173 }
177 }
174
178
175 #include "moc_scatterchartitem_p.cpp"
179 #include "moc_scatterchartitem_p.cpp"
176
180
177 QTCOMMERCIALCHART_END_NAMESPACE
181 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,102 +1,106
1 #include "splinechartitem_p.h"
1 #include "splinechartitem_p.h"
2 #include "chartpresenter_p.h"
2 #include "chartpresenter_p.h"
3 #include <QPainter>
3 #include <QPainter>
4
4
5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
6
6
7 SplineChartItem::SplineChartItem(QSplineSeries* series, QGraphicsItem *parent) :
7 SplineChartItem::SplineChartItem(QSplineSeries* series, QGraphicsItem *parent) :
8 XYChartItem(series, parent),
8 XYChartItem(series, parent),
9 m_series(series)
9 m_series(series)
10 {
10 {
11 setZValue(ChartPresenter::LineChartZValue);
11 setZValue(ChartPresenter::LineChartZValue);
12 QObject::connect(series,SIGNAL(updated()),this,SLOT(handleUpdated()));
12 QObject::connect(series,SIGNAL(updated()),this,SLOT(handleUpdated()));
13 handleUpdated();
13 handleUpdated();
14 }
14 }
15
15
16 QRectF SplineChartItem::boundingRect() const
16 QRectF SplineChartItem::boundingRect() const
17 {
17 {
18 return m_rect;
18 return m_rect;
19 }
19 }
20
20
21 QPainterPath SplineChartItem::shape() const
21 QPainterPath SplineChartItem::shape() const
22 {
22 {
23 return m_path;
23 return m_path;
24 }
24 }
25
25
26 QPointF SplineChartItem::calculateGeometryControlPoint(int index) const
26 QPointF SplineChartItem::calculateGeometryControlPoint(int index) const
27 {
27 {
28 return XYChartItem::calculateGeometryPoint(m_series->controlPoint(index));
28 return XYChartItem::calculateGeometryPoint(m_series->controlPoint(index));
29 }
29 }
30
30
31 void SplineChartItem::setGeometry(QVector<QPointF>& points)
31 void SplineChartItem::setGeometry(QVector<QPointF>& points)
32 {
32 {
33
33
34 if(points.size()==0) return;
34 if(points.size()==0)
35 {
36 XYChartItem::setGeometry(points);
37 return;
38 }
35
39
36 QPainterPath splinePath;
40 QPainterPath splinePath;
37 const QPointF& point = points.at(0);
41 const QPointF& point = points.at(0);
38 splinePath.moveTo(point);
42 splinePath.moveTo(point);
39
43
40 for (int i = 0; i < points.size() - 1; i++)
44 for (int i = 0; i < points.size() - 1; i++)
41 {
45 {
42 const QPointF& point = points.at(i + 1);
46 const QPointF& point = points.at(i + 1);
43 splinePath.cubicTo(calculateGeometryControlPoint(2 * i), calculateGeometryControlPoint(2 * i + 1), point);
47 splinePath.cubicTo(calculateGeometryControlPoint(2 * i), calculateGeometryControlPoint(2 * i + 1), point);
44 }
48 }
45
49
46 prepareGeometryChange();
50 prepareGeometryChange();
47 m_path = splinePath;
51 m_path = splinePath;
48 m_rect = splinePath.boundingRect();
52 m_rect = splinePath.boundingRect();
49 XYChartItem::setGeometry(points);
53 XYChartItem::setGeometry(points);
50 }
54 }
51
55
52 void SplineChartItem::setLinePen(const QPen& pen)
56 void SplineChartItem::setLinePen(const QPen& pen)
53 {
57 {
54 m_pen = pen;
58 m_pen = pen;
55 }
59 }
56
60
57 //handlers
61 //handlers
58
62
59 void SplineChartItem::handleUpdated()
63 void SplineChartItem::handleUpdated()
60 {
64 {
61 //m_items.setVisible(m_series->pointsVisible());
65 //m_items.setVisible(m_series->pointsVisible());
62 setLinePen(m_series->pen());
66 setLinePen(m_series->pen());
63 update();
67 update();
64 }
68 }
65
69
66 //painter
70 //painter
67
71
68 void SplineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
72 void SplineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
69 {
73 {
70 Q_UNUSED(widget);
74 Q_UNUSED(widget);
71 Q_UNUSED(option);
75 Q_UNUSED(option);
72 painter->save();
76 painter->save();
73 painter->setClipRect(clipRect());
77 painter->setClipRect(clipRect());
74 painter->setPen(m_pen);
78 painter->setPen(m_pen);
75 painter->drawPath(m_path);
79 painter->drawPath(m_path);
76
80
77 const QVector<QPointF> points = XYChartItem::points();
81 const QVector<QPointF> points = XYChartItem::points();
78
82
79 for (int i = 0; i < points.size() - 1; i++)
83 for (int i = 0; i < points.size() - 1; i++)
80 {
84 {
81 painter->setPen(Qt::red);
85 painter->setPen(Qt::red);
82 painter->drawEllipse(points[i], 2, 2);
86 painter->drawEllipse(points[i], 2, 2);
83
87
84 painter->setPen(Qt::blue);
88 painter->setPen(Qt::blue);
85 // painter->drawLine(m_series->at(i), m_series->controlPoint(2 * i));
89 // painter->drawLine(m_series->at(i), m_series->controlPoint(2 * i));
86 // painter->drawLine(m_series->at(i + 1), m_series->controlPoint(2 * i + 1));
90 // painter->drawLine(m_series->at(i + 1), m_series->controlPoint(2 * i + 1));
87 // painter->drawEllipse(calculateGeometryControlPoint(2 * i), 4, 4);
91 // painter->drawEllipse(calculateGeometryControlPoint(2 * i), 4, 4);
88 // painter->drawEllipse(calculateGeometryControlPoint(2 * i + 1), 4, 4);
92 // painter->drawEllipse(calculateGeometryControlPoint(2 * i + 1), 4, 4);
89 }
93 }
90 if (points.count() > 0)
94 if (points.count() > 0)
91 {
95 {
92 painter->setPen(Qt::red);
96 painter->setPen(Qt::red);
93 painter->drawEllipse(points[points.count() - 1], 2, 2);
97 painter->drawEllipse(points[points.count() - 1], 2, 2);
94 }
98 }
95 painter->restore();
99 painter->restore();
96 }
100 }
97
101
98
102
99
103
100 #include "moc_splinechartitem_p.cpp"
104 #include "moc_splinechartitem_p.cpp"
101
105
102 QTCOMMERCIALCHART_END_NAMESPACE
106 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,291 +1,303
1 #include "qxyseries.h"
1 #include "qxyseries.h"
2
2
3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
4
4
5 /*!
5 /*!
6 \class QXYSeries
6 \class QXYSeries
7 \brief The QXYSeries class is a base class for line, spline and scatter series.
7 \brief The QXYSeries class is a base class for line, spline and scatter series.
8 */
8 */
9
9
10 /*!
10 /*!
11 \fn QPen QXYSeries::pen() const
11 \fn QPen QXYSeries::pen() const
12 \brief Returns pen used to draw points for series.
12 \brief Returns pen used to draw points for series.
13 \sa setPen()
13 \sa setPen()
14 */
14 */
15
15
16 /*!
16 /*!
17 \fn QBrush QXYSeries::brush() const
17 \fn QBrush QXYSeries::brush() const
18 \brief Returns brush used to draw points for series.
18 \brief Returns brush used to draw points for series.
19 \sa setBrush()
19 \sa setBrush()
20 */
20 */
21
21
22 /*!
22 /*!
23 \fn void QXYSeries::pointReplaced(int index)
23 \fn void QXYSeries::pointReplaced(int index)
24 \brief \internal \a index
24 \brief \internal \a index
25 */
25 */
26
26
27 /*!
27 /*!
28 \fn void QXYSeries::pointAdded(int index)
28 \fn void QXYSeries::pointAdded(int index)
29 \brief \internal \a index
29 \brief \internal \a index
30 */
30 */
31
31
32 /*!
32 /*!
33 \fn void QXYSeries::pointRemoved(int index)
33 \fn void QXYSeries::pointRemoved(int index)
34 \brief \internal \a index
34 \brief \internal \a index
35 */
35 */
36
36
37 /*!
37 /*!
38 \fn void QXYSeries::updated()
38 \fn void QXYSeries::updated()
39 \brief \internal
39 \brief \internal
40 */
40 */
41
41
42 /*!
42 /*!
43 Constructs empty series object which is a child of \a parent.
43 Constructs empty series object which is a child of \a parent.
44 When series object is added to QChartView or QChart instance ownerships is transfered.
44 When series object is added to QChartView or QChart instance ownerships is transfered.
45 */
45 */
46 QXYSeries::QXYSeries(QObject* parent):QSeries(parent)
46 QXYSeries::QXYSeries(QObject* parent):QSeries(parent)
47 {
47 {
48 m_model = NULL;
48 m_model = NULL;
49 m_mapX = -1;
49 m_mapX = -1;
50 m_mapY = -1;
50 m_mapY = -1;
51 m_mapOrientation = Qt::Vertical;
51 m_mapOrientation = Qt::Vertical;
52 // m_mapYOrientation = Qt::Vertical;
52 // m_mapYOrientation = Qt::Vertical;
53 }
53 }
54 /*!
54 /*!
55 Destroys the object. Series added to QChartView or QChart instances are owned by those,
55 Destroys the object. Series added to QChartView or QChart instances are owned by those,
56 and are deleted when mentioned object are destroyed.
56 and are deleted when mentioned object are destroyed.
57 */
57 */
58 QXYSeries::~QXYSeries()
58 QXYSeries::~QXYSeries()
59 {
59 {
60 }
60 }
61
61
62 /*!
62 /*!
63 Adds data point \a x \a y to the series. Points are connected with lines on the chart.
63 Adds data point \a x \a y to the series. Points are connected with lines on the chart.
64 */
64 */
65 void QXYSeries::add(qreal x,qreal y)
65 void QXYSeries::add(qreal x,qreal y)
66 {
66 {
67 Q_ASSERT(m_x.size() == m_y.size());
67 Q_ASSERT(m_x.size() == m_y.size());
68 m_x<<x;
68 m_x<<x;
69 m_y<<y;
69 m_y<<y;
70 emit pointAdded(m_x.size()-1);
70 emit pointAdded(m_x.size()-1);
71 }
71 }
72
72
73 /*!
73 /*!
74 This is an overloaded function.
74 This is an overloaded function.
75 Adds data \a point to the series. Points are connected with lines on the chart.
75 Adds data \a point to the series. Points are connected with lines on the chart.
76 */
76 */
77 void QXYSeries::add(const QPointF& point)
77 void QXYSeries::add(const QPointF& point)
78 {
78 {
79 add(point.x(),point.y());
79 add(point.x(),point.y());
80 }
80 }
81
81
82 /*!
82 /*!
83 This is an overloaded function.
83 This is an overloaded function.
84 Adds list of data \a points to the series. Points are connected with lines on the chart.
84 Adds list of data \a points to the series. Points are connected with lines on the chart.
85 */
85 */
86 void QXYSeries::add(const QList<QPointF> points)
86 void QXYSeries::add(const QList<QPointF> points)
87 {
87 {
88 foreach(const QPointF& point , points) {
88 foreach(const QPointF& point , points) {
89 add(point.x(),point.y());
89 add(point.x(),point.y());
90 }
90 }
91 }
91 }
92
92
93 /*!
93 /*!
94 Modifies \a y value for given \a x a value.
94 Modifies \a y value for given \a x a value.
95 */
95 */
96 void QXYSeries::replace(qreal x,qreal y)
96 void QXYSeries::replace(qreal x,qreal y)
97 {
97 {
98 int index = m_x.indexOf(x);
98 int index = m_x.indexOf(x);
99 m_x[index]=x;
99 m_x[index]=x;
100 m_y[index]=y;
100 m_y[index]=y;
101 emit pointReplaced(index);
101 emit pointReplaced(index);
102 }
102 }
103
103
104 /*!
104 /*!
105 This is an overloaded function.
105 This is an overloaded function.
106 Replaces current y value of for given \a point x value with \a point y value.
106 Replaces current y value of for given \a point x value with \a point y value.
107 */
107 */
108 void QXYSeries::replace(const QPointF& point)
108 void QXYSeries::replace(const QPointF& point)
109 {
109 {
110 replace(point.x(),point.y());
110 replace(point.x(),point.y());
111 }
111 }
112
112
113 /*!
113 /*!
114 Removes current \a x and \a y value.
114 Removes current \a x and \a y value.
115 */
115 */
116 void QXYSeries::remove(qreal x,qreal y)
116 void QXYSeries::remove(qreal x,qreal y)
117 {
117 {
118 int index =-1;
118 int index =-1;
119 do{
119 do{
120 index = m_x.indexOf(x,index+1);
120 index = m_x.indexOf(x,index+1);
121 }while(index !=-1 && m_y.at(index)!=y);
121 }while(index !=-1 && m_y.at(index)!=y);
122
122
123 if(index==-1) return;
123 if(index==-1) return;
124 emit pointRemoved(index);
124
125 m_x.remove(index);
125 m_x.remove(index);
126 m_y.remove(index);
126 m_y.remove(index);
127 emit pointRemoved(index);
127 }
128 }
128
129
129 /*!
130 /*!
130 Removes current \a point x value. Note \a point y value is ignored.
131 Removes current \a point x value. Note \a point y value is ignored.
131 */
132 */
132 void QXYSeries::remove(const QPointF& point)
133 void QXYSeries::remove(const QPointF& point)
133 {
134 {
134 remove(point.x(),point.y());
135 remove(point.x(),point.y());
135 }
136 }
136
137
137 /*!
138 /*!
138 Removes all data points from the series.
139 Removes all data points from the series.
139 */
140 */
140 void QXYSeries::removeAll()
141 void QXYSeries::removeAll()
141 {
142 {
142 m_x.clear();
143 m_x.clear();
143 m_y.clear();
144 m_y.clear();
144 }
145 }
145
146
146 /*!
147 /*!
147 \internal \a pos
148 \internal \a pos
148 */
149 */
149 qreal QXYSeries::x(int pos) const
150 qreal QXYSeries::x(int pos) const
150 {
151 {
151 if (m_model)
152 if (m_model)
152 if (m_mapOrientation == Qt::Vertical)
153 if (m_mapOrientation == Qt::Vertical)
153 // consecutive data is read from model's column
154 // consecutive data is read from model's column
154 return m_model->data(m_model->index(pos, m_mapX), Qt::DisplayRole).toDouble();
155 return m_model->data(m_model->index(pos, m_mapX), Qt::DisplayRole).toDouble();
155 else
156 else
156 // consecutive data is read from model's row
157 // consecutive data is read from model's row
157 return m_model->data(m_model->index(m_mapX, pos), Qt::DisplayRole).toDouble();
158 return m_model->data(m_model->index(m_mapX, pos), Qt::DisplayRole).toDouble();
158 else
159 else
159 // model is not specified, return the data from series' internal data store
160 // model is not specified, return the data from series' internal data store
160 return m_x.at(pos);
161 return m_x.at(pos);
161 }
162 }
162
163
163 /*!
164 /*!
164 \internal \a pos
165 \internal \a pos
165 */
166 */
166 qreal QXYSeries::y(int pos) const
167 qreal QXYSeries::y(int pos) const
167 {
168 {
168 if (m_model)
169 if (m_model)
169 if (m_mapOrientation == Qt::Vertical)
170 if (m_mapOrientation == Qt::Vertical)
170 // consecutive data is read from model's column
171 // consecutive data is read from model's column
171 return m_model->data(m_model->index(pos, m_mapY), Qt::DisplayRole).toDouble();
172 return m_model->data(m_model->index(pos, m_mapY), Qt::DisplayRole).toDouble();
172 else
173 else
173 // consecutive data is read from model's row
174 // consecutive data is read from model's row
174 return m_model->data(m_model->index(m_mapY, pos), Qt::DisplayRole).toDouble();
175 return m_model->data(m_model->index(m_mapY, pos), Qt::DisplayRole).toDouble();
175 else
176 else
176 // model is not specified, return the data from series' internal data store
177 // model is not specified, return the data from series' internal data store
177 return m_y.at(pos);
178 return m_y.at(pos);
178 }
179 }
179
180
180 /*!
181 /*!
181 Returns number of data points within series.
182 Returns number of data points within series.
182 */
183 */
183 int QXYSeries::count() const
184 int QXYSeries::count() const
184 {
185 {
185 Q_ASSERT(m_x.size() == m_y.size());
186 Q_ASSERT(m_x.size() == m_y.size());
186
187
187 if (m_model)
188 if (m_model)
188 if (m_mapOrientation == Qt::Vertical)
189 if (m_mapOrientation == Qt::Vertical)
189 // data is in a column, so return the number of items in single column
190 // data is in a column, so return the number of items in single column
190 return m_model->rowCount();
191 return m_model->rowCount();
191 else
192 else
192 // data is in a row, so return the number of items in single row
193 // data is in a row, so return the number of items in single row
193 m_model->columnCount();
194 m_model->columnCount();
194 else
195 else
195 // model is not specified, return the number of points in the series internal data store
196 // model is not specified, return the number of points in the series internal data store
196 return m_x.size();
197 return m_x.size();
197 }
198 }
198
199
199 /*!
200 /*!
200 Returns the data points of the series.
201 Returns the data points of the series.
201 */
202 */
202 QList<QPointF> QXYSeries::data()
203 QList<QPointF> QXYSeries::data()
203 {
204 {
204 QList<QPointF> data;
205 QList<QPointF> data;
205 for (int i(0); i < m_x.count() && i < m_y.count(); i++)
206 for (int i(0); i < m_x.count() && i < m_y.count(); i++)
206 data.append(QPointF(m_x.at(i), m_y.at(i)));
207 data.append(QPointF(m_x.at(i), m_y.at(i)));
207 return data;
208 return data;
208 }
209 }
209
210
210
211
211 /*!
212 /*!
212 Sets \a pen used for drawing points on the chart. If the pen is not defined, the
213 Sets \a pen used for drawing points on the chart. If the pen is not defined, the
213 pen from chart theme is used.
214 pen from chart theme is used.
214 \sa QChart::setChartTheme()
215 \sa QChart::setChartTheme()
215 */
216 */
216 void QXYSeries::setPen(const QPen& pen)
217 void QXYSeries::setPen(const QPen& pen)
217 {
218 {
218 if(pen!=m_pen){
219 if(pen!=m_pen){
219 m_pen=pen;
220 m_pen=pen;
220 emit updated();
221 emit updated();
221 }
222 }
222 }
223 }
223
224
224 /*!
225 /*!
225 Sets \a brush used for drawing points on the chart. If the brush is not defined, brush
226 Sets \a brush used for drawing points on the chart. If the brush is not defined, brush
226 from chart theme setting is used.
227 from chart theme setting is used.
227 \sa QChart::setChartTheme()
228 \sa QChart::setChartTheme()
228 */
229 */
229
230
230 void QXYSeries::setBrush(const QBrush& brush)
231 void QXYSeries::setBrush(const QBrush& brush)
231 {
232 {
232 if(brush!=m_brush){
233 if(brush!=m_brush){
233 m_brush=brush;
234 m_brush=brush;
234 emit updated();
235 emit updated();
235 }
236 }
236 }
237 }
237
238
238
239
239 /*!
240 /*!
240 Stream operator for adding a data \a point to the series.
241 Stream operator for adding a data \a point to the series.
241 \sa add()
242 \sa add()
242 */
243 */
243
244
244 QXYSeries& QXYSeries::operator<< (const QPointF &point)
245 QXYSeries& QXYSeries::operator<< (const QPointF &point)
245 {
246 {
246 add(point);
247 add(point);
247 return *this;
248 return *this;
248 }
249 }
249
250
250
251
251 /*!
252 /*!
252 Stream operator for adding a list of \a points to the series.
253 Stream operator for adding a list of \a points to the series.
253 \sa add()
254 \sa add()
254 */
255 */
255
256
256 QXYSeries& QXYSeries::operator<< (const QList<QPointF> points)
257 QXYSeries& QXYSeries::operator<< (const QList<QPointF> points)
257 {
258 {
258 add(points);
259 add(points);
259 return *this;
260 return *this;
260 }
261 }
261
262
262
263
263 void QXYSeries::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
264 void QXYSeries::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
264 {
265 {
265 emit pointReplaced(topLeft.row());
266 emit pointReplaced(topLeft.row());
266 }
267 }
267
268
269 void QXYSeries::modelDataAdded(QModelIndex parent, int start, int end)
270 {
271 emit pointAdded(start);
272 }
273
274 void QXYSeries::modelDataRemoved(QModelIndex parent, int start, int end)
275 {
276 emit pointRemoved(start);
277 }
278
268 bool QXYSeries::setModel(QAbstractItemModel* model) {
279 bool QXYSeries::setModel(QAbstractItemModel* model) {
269 m_model = model;
280 m_model = model;
270 // for (int i = 0; i < m_model->rowCount(); i++)
281 // for (int i = 0; i < m_model->rowCount(); i++)
271 // emit pointAdded(i);
282 // emit pointAdded(i);
272 connect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(modelUpdated(QModelIndex, QModelIndex)));
283 connect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(modelUpdated(QModelIndex, QModelIndex)));
273 // connect(m_model,SIGNAL(), this, SLOT(modelUpdated(QModelIndex, QModelIndex)));
284 connect(m_model,SIGNAL(rowsInserted(QModelIndex, int, int)), this, SLOT(modelDataAdded(QModelIndex,int,int)));
285 connect(m_model, SIGNAL(rowsRemoved(QModelIndex, int, int)), this, SLOT(modelDataRemoved(QModelIndex,int,int)));
274 }
286 }
275
287
276 void QXYSeries::setModelMapping(int modelX, int modelY, Qt::Orientation orientation)
288 void QXYSeries::setModelMapping(int modelX, int modelY, Qt::Orientation orientation)
277 {
289 {
278 m_mapX = modelX;
290 m_mapX = modelX;
279 m_mapY = modelY;
291 m_mapY = modelY;
280 m_mapOrientation = orientation;
292 m_mapOrientation = orientation;
281 }
293 }
282
294
283 //void QXYSeries::setModelMappingY(int modelLineIndex, Qt::Orientation orientation)
295 //void QXYSeries::setModelMappingY(int modelLineIndex, Qt::Orientation orientation)
284 //{
296 //{
285 // m_mapY = modelLineIndex;
297 // m_mapY = modelLineIndex;
286 // m_mapYOrientation = orientation;
298 // m_mapYOrientation = orientation;
287 //}
299 //}
288
300
289 #include "moc_qxyseries.cpp"
301 #include "moc_qxyseries.cpp"
290
302
291 QTCOMMERCIALCHART_END_NAMESPACE
303 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,73 +1,75
1 #ifndef QXYSERIES_H_
1 #ifndef QXYSERIES_H_
2 #define QXYSERIES_H_
2 #define QXYSERIES_H_
3
3
4 #include "qchartglobal.h"
4 #include "qchartglobal.h"
5 #include "qseries.h"
5 #include "qseries.h"
6 #include <QDebug>
6 #include <QDebug>
7 #include <QPen>
7 #include <QPen>
8 #include <QBrush>
8 #include <QBrush>
9
9
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11
11
12 class QTCOMMERCIALCHART_EXPORT QXYSeries : public QSeries
12 class QTCOMMERCIALCHART_EXPORT QXYSeries : public QSeries
13 {
13 {
14 Q_OBJECT
14 Q_OBJECT
15 protected:
15 protected:
16 QXYSeries(QObject* parent=0);
16 QXYSeries(QObject* parent=0);
17 virtual ~QXYSeries();
17 virtual ~QXYSeries();
18
18
19 public:
19 public:
20 void add(qreal x, qreal y);
20 void add(qreal x, qreal y);
21 void add(const QPointF& point);
21 void add(const QPointF& point);
22 void add(const QList<QPointF> points);
22 void add(const QList<QPointF> points);
23 void replace(qreal x,qreal y);
23 void replace(qreal x,qreal y);
24 void replace(const QPointF& point);
24 void replace(const QPointF& point);
25 void remove(qreal x, qreal y);
25 void remove(qreal x, qreal y);
26 void remove(const QPointF& point);
26 void remove(const QPointF& point);
27 void removeAll();
27 void removeAll();
28
28
29 int count() const;
29 int count() const;
30 qreal x(int pos) const;
30 qreal x(int pos) const;
31 qreal y(int pos) const;
31 qreal y(int pos) const;
32 QList<QPointF> data();
32 QList<QPointF> data();
33
33
34 QXYSeries& operator << (const QPointF &point);
34 QXYSeries& operator << (const QPointF &point);
35 QXYSeries& operator << (const QList<QPointF> points);
35 QXYSeries& operator << (const QList<QPointF> points);
36
36
37 void setPen(const QPen& pen);
37 void setPen(const QPen& pen);
38 QPen pen() const {return m_pen;}
38 QPen pen() const {return m_pen;}
39 void setBrush(const QBrush& pen);
39 void setBrush(const QBrush& pen);
40 QBrush brush() const {return m_brush;}
40 QBrush brush() const {return m_brush;}
41
41
42 bool setModel(QAbstractItemModel* model);
42 bool setModel(QAbstractItemModel* model);
43 QAbstractItemModel* model() {return m_model;}
43 QAbstractItemModel* model() {return m_model;}
44
44
45 void setModelMapping(int modelX, int modelY, Qt::Orientation orientation = Qt::Vertical);
45 virtual void setModelMapping(int modelX, int modelY, Qt::Orientation orientation = Qt::Vertical);
46 // void setModelMappingY(int modelLineIndex, Qt::Orientation orientation = Qt::Vertical);
46 // void setModelMappingY(int modelLineIndex, Qt::Orientation orientation = Qt::Vertical);
47
47
48 private slots:
48 private slots:
49 void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight);
49 void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight);
50 void modelDataAdded(QModelIndex parent, int start, int end);
51 void modelDataRemoved(QModelIndex parent, int start, int end);
50
52
51 signals:
53 signals:
52 void updated();
54 void updated();
53 void pointReplaced(int index);
55 void pointReplaced(int index);
54 void pointRemoved(int index);
56 void pointRemoved(int index);
55 void pointAdded(int index);
57 void pointAdded(int index);
56
58
57 protected:
59 protected:
58 QVector<qreal> m_x;
60 QVector<qreal> m_x;
59 QVector<qreal> m_y;
61 QVector<qreal> m_y;
60
62
61 QPen m_pen;
63 QPen m_pen;
62 QBrush m_brush;
64 QBrush m_brush;
63
65
64 QAbstractItemModel* m_model;
66 QAbstractItemModel* m_model;
65 int m_mapX;
67 int m_mapX;
66 Qt::Orientation m_mapOrientation;
68 Qt::Orientation m_mapOrientation;
67 int m_mapY;
69 int m_mapY;
68 // Qt::Orientation m_mapYOrientation;
70 // Qt::Orientation m_mapYOrientation;
69 };
71 };
70
72
71 QTCOMMERCIALCHART_END_NAMESPACE
73 QTCOMMERCIALCHART_END_NAMESPACE
72
74
73 #endif
75 #endif
@@ -1,156 +1,156
1 #include "xychartitem_p.h"
1 #include "xychartitem_p.h"
2 #include "qxyseries.h"
2 #include "qxyseries.h"
3 #include "chartpresenter_p.h"
3 #include "chartpresenter_p.h"
4 #include "chartanimator_p.h"
4 #include "chartanimator_p.h"
5 #include <QPainter>
5 #include <QPainter>
6
6
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 //TODO: optimize : remove points which are not visible
10 //TODO: optimize : remove points which are not visible
11
11
12 XYChartItem::XYChartItem(QXYSeries* series, QGraphicsItem *parent):ChartItem(parent),
12 XYChartItem::XYChartItem(QXYSeries* series, QGraphicsItem *parent):ChartItem(parent),
13 m_minX(0),
13 m_minX(0),
14 m_maxX(0),
14 m_maxX(0),
15 m_minY(0),
15 m_minY(0),
16 m_maxY(0),
16 m_maxY(0),
17 m_series(series)
17 m_series(series)
18 {
18 {
19 QObject::connect(series,SIGNAL(pointReplaced(int)),this,SLOT(handlePointReplaced(int)));
19 QObject::connect(series,SIGNAL(pointReplaced(int)),this,SLOT(handlePointReplaced(int)));
20 QObject::connect(series,SIGNAL(pointAdded(int)),this,SLOT(handlePointAdded(int)));
20 QObject::connect(series,SIGNAL(pointAdded(int)),this,SLOT(handlePointAdded(int)));
21 QObject::connect(series,SIGNAL(pointRemoved(int)),this,SLOT(handlePointRemoved(int)));
21 QObject::connect(series,SIGNAL(pointRemoved(int)),this,SLOT(handlePointRemoved(int)));
22
22
23 }
23 }
24
24
25 QPointF XYChartItem::calculateGeometryPoint(const QPointF& point) const
25 QPointF XYChartItem::calculateGeometryPoint(const QPointF& point) const
26 {
26 {
27 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
27 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
28 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
28 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
29 qreal x = (point.x() - m_minX)* deltaX;
29 qreal x = (point.x() - m_minX)* deltaX;
30 qreal y = (point.y() - m_minY)*-deltaY + m_size.height();
30 qreal y = (point.y() - m_minY)*-deltaY + m_size.height();
31 return QPointF(x,y);
31 return QPointF(x,y);
32 }
32 }
33
33
34
34
35 QPointF XYChartItem::calculateGeometryPoint(int index) const
35 QPointF XYChartItem::calculateGeometryPoint(int index) const
36 {
36 {
37 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
37 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
38 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
38 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
39 qreal x = (m_series->x(index) - m_minX)* deltaX;
39 qreal x = (m_series->x(index) - m_minX)* deltaX;
40 qreal y = (m_series->y(index) - m_minY)*-deltaY + m_size.height();
40 qreal y = (m_series->y(index) - m_minY)*-deltaY + m_size.height();
41 return QPointF(x,y);
41 return QPointF(x,y);
42 }
42 }
43
43
44 QVector<QPointF> XYChartItem::calculateGeometryPoints() const
44 QVector<QPointF> XYChartItem::calculateGeometryPoints() const
45 {
45 {
46 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
46 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
47 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
47 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
48
48
49 QVector<QPointF> points;
49 QVector<QPointF> points;
50 points.reserve(m_series->count());
50 points.reserve(m_series->count());
51 for (int i = 0; i < m_series->count(); ++i) {
51 for (int i = 0; i < m_series->count(); ++i) {
52 qreal x = (m_series->x(i) - m_minX)* deltaX;
52 qreal x = (m_series->x(i) - m_minX)* deltaX;
53 qreal y = (m_series->y(i) - m_minY)*-deltaY + m_size.height();
53 qreal y = (m_series->y(i) - m_minY)*-deltaY + m_size.height();
54 points << QPointF(x,y);
54 points << QPointF(x,y);
55 }
55 }
56 return points;
56 return points;
57 }
57 }
58
58
59 QPointF XYChartItem::calculateDomainPoint(const QPointF& point) const
59 QPointF XYChartItem::calculateDomainPoint(const QPointF& point) const
60 {
60 {
61 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
61 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
62 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
62 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
63 qreal x = point.x()/deltaX +m_minX;
63 qreal x = point.x()/deltaX +m_minX;
64 qreal y = (point.y()-m_size.height())/(-deltaY)+ m_minY;
64 qreal y = (point.y()-m_size.height())/(-deltaY)+ m_minY;
65 return QPointF(x,y);
65 return QPointF(x,y);
66 }
66 }
67
67
68 void XYChartItem::updatePoints(QVector<QPointF>& points)
68 void XYChartItem::updatePoints(QVector<QPointF>& points)
69 {
69 {
70 if(m_animator){
70 if(m_animator){
71 m_animator->applyLayout(this,points);
71 m_animator->applyLayout(this,points);
72 }
72 }
73 else setGeometry(points);
73 else setGeometry(points);
74
74
75 }
75 }
76
76
77 void XYChartItem::updatePoint(QVector<QPointF>& points)
77 void XYChartItem::updatePoint(QVector<QPointF>& points)
78 {
78 {
79 setGeometry(points);
79 setGeometry(points);
80 }
80 }
81
81
82 void XYChartItem::setGeometry(QVector<QPointF>& points)
82 void XYChartItem::setGeometry(QVector<QPointF>& points)
83 {
83 {
84 m_points = points;
84 m_points = points;
85 }
85 }
86
86
87 //handlers
87 //handlers
88
88
89 void XYChartItem::handlePointAdded(int index)
89 void XYChartItem::handlePointAdded(int index)
90 {
90 {
91 Q_ASSERT(index<m_series->count());
91 Q_ASSERT(index<m_series->count());
92 Q_ASSERT(index>=0);
92 Q_ASSERT(index>=0);
93
93
94 QPointF point = calculateGeometryPoint(index);
94 QPointF point = calculateGeometryPoint(index);
95 QVector<QPointF> points = m_points;
95 QVector<QPointF> points = m_points;
96 points.insert(index,point);
96 points.insert(index,point);
97 updatePoints(points);
97 updatePoints(points);
98 update();
98 update();
99 }
99 }
100 void XYChartItem::handlePointRemoved(int index)
100 void XYChartItem::handlePointRemoved(int index)
101 {
101 {
102 Q_ASSERT(index<m_series->count());
102 Q_ASSERT(index<m_series->count() + 1);
103 Q_ASSERT(index>=0);
103 Q_ASSERT(index>=0);
104 QPointF point = calculateGeometryPoint(index);
104 // QPointF point = calculateGeometryPoint(index);
105 QVector<QPointF> points = m_points;
105 QVector<QPointF> points = m_points;
106 points.remove(index);
106 points.remove(index);
107 updatePoints(points);
107 updatePoints(points);
108 update();
108 update();
109 }
109 }
110
110
111 void XYChartItem::handlePointReplaced(int index)
111 void XYChartItem::handlePointReplaced(int index)
112 {
112 {
113 Q_ASSERT(index<m_series->count());
113 Q_ASSERT(index<m_series->count());
114 Q_ASSERT(index>=0);
114 Q_ASSERT(index>=0);
115 QPointF point = calculateGeometryPoint(index);
115 QPointF point = calculateGeometryPoint(index);
116 QVector<QPointF> points = m_points;
116 QVector<QPointF> points = m_points;
117 points.replace(index,point);
117 points.replace(index,point);
118 updatePoint(points);
118 updatePoint(points);
119 update();
119 update();
120 }
120 }
121
121
122 void XYChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY)
122 void XYChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY)
123 {
123 {
124 m_minX=minX;
124 m_minX=minX;
125 m_maxX=maxX;
125 m_maxX=maxX;
126 m_minY=minY;
126 m_minY=minY;
127 m_maxY=maxY;
127 m_maxY=maxY;
128
128
129 if(isEmpty()) return;
129 if(isEmpty()) return;
130 QVector<QPointF> points = calculateGeometryPoints();
130 QVector<QPointF> points = calculateGeometryPoints();
131 updatePoints(points);
131 updatePoints(points);
132 update();
132 update();
133 }
133 }
134
134
135 void XYChartItem::handleGeometryChanged(const QRectF& rect)
135 void XYChartItem::handleGeometryChanged(const QRectF& rect)
136 {
136 {
137 Q_ASSERT(rect.isValid());
137 Q_ASSERT(rect.isValid());
138 m_size=rect.size();
138 m_size=rect.size();
139 m_clipRect=rect.translated(-rect.topLeft());
139 m_clipRect=rect.translated(-rect.topLeft());
140 setPos(rect.topLeft());
140 setPos(rect.topLeft());
141
141
142 if(isEmpty()) return;
142 if(isEmpty()) return;
143 QVector<QPointF> points = calculateGeometryPoints();
143 QVector<QPointF> points = calculateGeometryPoints();
144 updatePoints(points);
144 updatePoints(points);
145 update();
145 update();
146 }
146 }
147
147
148
148
149 bool XYChartItem::isEmpty()
149 bool XYChartItem::isEmpty()
150 {
150 {
151 return !m_clipRect.isValid() || m_maxX - m_minX == 0 || m_maxY - m_minY ==0 ;
151 return !m_clipRect.isValid() || m_maxX - m_minX == 0 || m_maxY - m_minY ==0 ;
152 }
152 }
153
153
154 #include "moc_xychartitem_p.cpp"
154 #include "moc_xychartitem_p.cpp"
155
155
156 QTCOMMERCIALCHART_END_NAMESPACE
156 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now