##// END OF EJS Templates
Minor changes to spline and model examples
Marek Rosa -
r894:29eac6e74027
parent child
Show More
@@ -1,125 +1,120
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "customtablemodel.h"
21 #include "customtablemodel.h"
22 #include <QVector>
22 #include <QVector>
23 #include <QTime>
23 #include <QTime>
24 #include <QRect>
24 #include <QRect>
25 #include <QColor>
25 #include <QColor>
26
26
27 CustomTableModel::CustomTableModel(QObject *parent) :
27 CustomTableModel::CustomTableModel(QObject *parent) :
28 QAbstractTableModel(parent)
28 QAbstractTableModel(parent)
29 {
29 {
30 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
30 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
31
31
32 m_columnCount = 4;
32 m_columnCount = 4;
33 m_rowCount = 15;
33 m_rowCount = 15;
34
34
35 // m_data
35 // m_data
36 for (int i = 0; i < m_rowCount; i++)
36 for (int i = 0; i < m_rowCount; i++)
37 {
37 {
38 QVector<qreal>* dataVec = new QVector<qreal>(m_columnCount);
38 QVector<qreal>* dataVec = new QVector<qreal>(m_columnCount);
39 for (int k = 0; k < dataVec->size(); k++)
39 for (int k = 0; k < dataVec->size(); k++)
40 {
40 {
41 if (k%2 == 0)
41 if (k%2 == 0)
42 dataVec->replace(k, i * 50 + qrand()%20);
42 dataVec->replace(k, i * 50 + qrand()%20);
43 else
43 else
44 dataVec->replace(k, qrand()%100);
44 dataVec->replace(k, qrand()%100);
45 }
45 }
46 m_data.append(dataVec);
46 m_data.append(dataVec);
47 }
47 }
48 }
48 }
49
49
50 int CustomTableModel::rowCount(const QModelIndex & parent) const
50 int CustomTableModel::rowCount(const QModelIndex & parent) const
51 {
51 {
52 Q_UNUSED(parent)
52 Q_UNUSED(parent)
53 return m_data.count();
53 return m_data.count();
54 }
54 }
55
55
56 int CustomTableModel::columnCount(const QModelIndex & parent) const
56 int CustomTableModel::columnCount(const QModelIndex & parent) const
57 {
57 {
58 Q_UNUSED(parent)
58 Q_UNUSED(parent)
59 return m_columnCount;
59 return m_columnCount;
60 }
60 }
61
61
62 QVariant CustomTableModel::headerData (int section, Qt::Orientation orientation, int role ) const
62 QVariant CustomTableModel::headerData (int section, Qt::Orientation orientation, int role ) const
63 {
63 {
64 if (role != Qt::DisplayRole)
64 if (role != Qt::DisplayRole)
65 return QVariant();
65 return QVariant();
66
66
67 if (orientation == Qt::Horizontal)
67 if (orientation == Qt::Horizontal)
68 {
68 {
69 if (section%2 == 0)
69 if (section%2 == 0)
70 return "x";
70 return "x";
71 else
71 else
72 return "y";
72 return "y";
73 }
73 }
74 else
74 else
75 return QString("%1").arg(section + 1);
75 return QString("%1").arg(section + 1);
76 }
76 }
77
77
78 QVariant CustomTableModel::data(const QModelIndex & index, int role) const
78 QVariant CustomTableModel::data(const QModelIndex & index, int role) const
79 {
79 {
80 if (role == Qt::DisplayRole)
80 if (role == Qt::DisplayRole)
81 {
81 {
82 return m_data[index.row()]->at(index.column());
82 return m_data[index.row()]->at(index.column());
83 }
83 }
84 else if (role == Qt::EditRole)
84 else if (role == Qt::EditRole)
85 {
85 {
86 return m_data[index.row()]->at(index.column());
86 return m_data[index.row()]->at(index.column());
87 }
87 }
88 else if (role == Qt::BackgroundRole)
88 else if (role == Qt::BackgroundRole)
89 {
89 {
90 QRect rect;
90 QRect rect;
91 foreach(rect, m_mapping)
91 foreach(rect, m_mapping)
92 if(rect.contains(index.column(), index.row()))
92 if(rect.contains(index.column(), index.row()))
93 return QColor(m_mapping.key(rect));
93 return QColor(m_mapping.key(rect));
94
94
95 // cell not mapped return white color
95 // cell not mapped return white color
96 return QColor(Qt::white);
96 return QColor(Qt::white);
97 }
97 }
98 return QVariant();
98 return QVariant();
99 }
99 }
100
100
101 bool CustomTableModel::setData ( const QModelIndex & index, const QVariant & value, int role)
101 bool CustomTableModel::setData ( const QModelIndex & index, const QVariant & value, int role)
102 {
102 {
103 if (index.isValid() && role == Qt::EditRole)
103 if (index.isValid() && role == Qt::EditRole)
104 {
104 {
105 m_data[index.row()]->replace(index.column(), value.toDouble());
105 m_data[index.row()]->replace(index.column(), value.toDouble());
106 emit dataChanged(index, index);
106 emit dataChanged(index, index);
107 return true;
107 return true;
108 }
108 }
109 return false;
109 return false;
110 }
110 }
111
111
112 Qt::ItemFlags CustomTableModel::flags ( const QModelIndex & index ) const
112 Qt::ItemFlags CustomTableModel::flags ( const QModelIndex & index ) const
113 {
113 {
114 return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
114 return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
115 }
115 }
116
116
117 void CustomTableModel::addMapping(QString color, QRect area)
117 void CustomTableModel::addMapping(QString color, QRect area)
118 {
118 {
119 m_mapping.insertMulti(color, area);
119 m_mapping.insertMulti(color, area);
120 }
120 }
121
122 void CustomTableModel::addMapping(QString color, int left, int top, int right, int bottom)
123 {
124 addMapping(color, QRect(QPoint(left, top), QPoint(right, bottom)));
125 }
@@ -1,55 +1,51
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #ifndef XYPOINTSMODEL_H
21 #ifndef XYPOINTSMODEL_H
22 #define XYPOINTSMODEL_H
22 #define XYPOINTSMODEL_H
23
23
24 #include <QAbstractTableModel>
24 #include <QAbstractTableModel>
25 #include <QPointF>
26 #include <QStringList>
27 //#include <QColor>
28 #include <QHash>
25 #include <QHash>
29 #include <QRect>
26 #include <QRect>
30
27
31 class CustomTableModel : public QAbstractTableModel
28 class CustomTableModel : public QAbstractTableModel
32 {
29 {
33 Q_OBJECT
30 Q_OBJECT
34 public:
31 public:
35 explicit CustomTableModel(QObject *parent = 0);
32 explicit CustomTableModel(QObject *parent = 0);
36
33
37 int rowCount ( const QModelIndex & parent = QModelIndex() ) const;
34 int rowCount ( const QModelIndex & parent = QModelIndex() ) const;
38 int columnCount ( const QModelIndex & parent = QModelIndex() ) const;
35 int columnCount ( const QModelIndex & parent = QModelIndex() ) const;
39 QVariant headerData (int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const;
36 QVariant headerData (int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const;
40 QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
37 QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
41 bool setData ( const QModelIndex & index, const QVariant & value, int role = Qt::EditRole );
38 bool setData ( const QModelIndex & index, const QVariant & value, int role = Qt::EditRole );
42 Qt::ItemFlags flags ( const QModelIndex & index ) const;
39 Qt::ItemFlags flags ( const QModelIndex & index ) const;
43
40
44 void addMapping(QString color, QRect area);
41 void addMapping(QString color, QRect area);
45 void addMapping(QString color, int left, int top, int right, int bottom);
46 void clearMapping() { m_mapping.clear(); }
42 void clearMapping() { m_mapping.clear(); }
47
43
48 private:
44 private:
49 QList<QVector<qreal> * > m_data;
45 QList<QVector<qreal> * > m_data;
50 QHash<QString, QRect> m_mapping;
46 QHash<QString, QRect> m_mapping;
51 int m_columnCount;
47 int m_columnCount;
52 int m_rowCount;
48 int m_rowCount;
53 };
49 };
54
50
55 #endif // XYPOINTSMODEL_H
51 #endif // XYPOINTSMODEL_H
@@ -1,87 +1,84
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "tablewidget.h"
21 #include "tablewidget.h"
22 #include "customtablemodel.h"
22 #include "customtablemodel.h"
23 #include <QGridLayout>
23 #include <QGridLayout>
24 #include <QTableView>
24 #include <QTableView>
25 #include <QChart>
25 #include <QChart>
26 #include <QChartView>
26 #include <QChartView>
27 #include <QLineSeries>
27 #include <QLineSeries>
28
28
29 QTCOMMERCIALCHART_USE_NAMESPACE
29 QTCOMMERCIALCHART_USE_NAMESPACE
30
30
31 TableWidget::TableWidget(QWidget *parent)
31 TableWidget::TableWidget(QWidget *parent)
32 : QWidget(parent)
32 : QWidget(parent)
33 {
33 {
34 // create simple model for storing data
34 // create simple model for storing data
35 // user's table data model
35 // user's table data model
36 CustomTableModel *model = new CustomTableModel;
36 CustomTableModel *model = new CustomTableModel;
37
37
38 // create table view and add model to it
38 // create table view and add model to it
39 QTableView *tableView = new QTableView;
39 QTableView *tableView = new QTableView;
40 tableView->setModel(model);
40 tableView->setModel(model);
41 tableView->setMinimumWidth(200);
41 // tableView->
42 // tableView->setMaximumWidth(200);
42 // tableView->setMinimumWidth(200);
43 // m_tableView->resizeColumnsToContents();
43 tableView->setColumnWidth(0, 56);
44 tableView->setColumnWidth(0, 50);
44 tableView->setColumnWidth(1, 56);
45 tableView->setColumnWidth(1, 50);
45 tableView->setColumnWidth(2, 56);
46 tableView->setColumnWidth(2, 50);
46 tableView->setColumnWidth(3, 56);
47 tableView->setColumnWidth(3, 50);
48
47
49 QChart *m_chart = new QChart;
48 QChart *m_chart = new QChart;
50 m_chart->setAnimationOptions(QChart::AllAnimations);
49 m_chart->setAnimationOptions(QChart::AllAnimations);
51 QChartView *m_chartView = new QChartView(m_chart);
50 QChartView *m_chartView = new QChartView(m_chart);
52 m_chartView->setRenderHint(QPainter::Antialiasing);
51 m_chartView->setRenderHint(QPainter::Antialiasing);
53 m_chartView->setMinimumSize(640, 480);
52 m_chartView->setMinimumSize(640, 480);
54
53
55 // for storing color hex from the series
54 // for storing color hex from the series
56 QString seriesColorHex = "#000000";
55 QString seriesColorHex = "#000000";
57
56
58 // series 1
57 // series 1
59 QLineSeries *m_series = new QLineSeries;
58 QLineSeries *m_series = new QLineSeries;
60 m_series->setModel(model);
59 m_series->setModel(model);
61 m_series->setModelMapping(0, 1, Qt::Vertical);
60 m_series->setModelMapping(0, 1, Qt::Vertical);
62 m_chart->addSeries(m_series);
61 m_chart->addSeries(m_series);
63
62
64 // get the color of the series and use it for showing the mapped area
63 // get the color of the series and use it for showing the mapped area
65 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
64 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
66 model->addMapping(seriesColorHex, QRect(0, 0, 2, model->rowCount()));
65 model->addMapping(seriesColorHex, QRect(0, 0, 2, model->rowCount()));
67
66
68 // series 2
67 // series 2
69 m_series = new QLineSeries;
68 m_series = new QLineSeries;
70 m_series->setModel(model);
69 m_series->setModel(model);
71 m_series->setModelMapping(2,3, Qt::Vertical);
70 m_series->setModelMapping(2,3, Qt::Vertical);
72 m_chart->addSeries(m_series);
71 m_chart->addSeries(m_series);
73
72
74 // get the color of the series and use it for showing the mapped area
73 // get the color of the series and use it for showing the mapped area
75 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
74 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
76 model->addMapping(seriesColorHex, QRect(2, 0, 2, model->rowCount()));
75 model->addMapping(seriesColorHex, QRect(2, 0, 2, model->rowCount()));
77
76
78 // m_chart->axisX()->setRange(0, 800);
79 // m_chart->axisY()->setRange(0, 120);
80
81 // create main layout
77 // create main layout
82 QGridLayout* mainLayout = new QGridLayout;
78 QGridLayout* mainLayout = new QGridLayout;
83 mainLayout->addWidget(tableView, 1, 0);
79 mainLayout->addWidget(tableView, 1, 0);
84 mainLayout->addWidget(m_chartView, 1, 1);
80 mainLayout->addWidget(m_chartView, 1, 1);
85 mainLayout->setColumnStretch(1, 1);
81 mainLayout->setColumnStretch(1, 1);
82 mainLayout->setColumnStretch(0, 0);
86 setLayout(mainLayout);
83 setLayout(mainLayout);
87 }
84 }
@@ -1,67 +1,68
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include <QApplication>
21 #include <QApplication>
22 #include <QMainWindow>
22 #include <QMainWindow>
23 #include <QChartView>
23 #include <QChartView>
24 #include <QSplineSeries>
24 #include <QSplineSeries>
25
25
26 QTCOMMERCIALCHART_USE_NAMESPACE
26 QTCOMMERCIALCHART_USE_NAMESPACE
27
27
28 int main(int argc, char *argv[])
28 int main(int argc, char *argv[])
29 {
29 {
30 QApplication a(argc, argv);
30 QApplication a(argc, argv);
31
31
32 //![1]
32 //![1]
33 QSplineSeries* series = new QSplineSeries();
33 QSplineSeries* series = new QSplineSeries();
34 QPen red(Qt::red);
34 QPen red(Qt::red);
35 red.setWidth(3);
35 red.setWidth(3);
36 series->setPen(red);
36 series->setPen(red);
37 //![1]
37 //![1]
38
38
39 //![2]
39 //![2]
40 series->append(0, 6);
40 series->append(0, 6);
41 series->append(2, 4);
41 series->append(2, 4);
42 series->append(3, 8);
42 series->append(3, 8);
43 series->append(7, 4);
43 series->append(7, 4);
44 series->append(10, 5);
44 series->append(10, 5);
45 *series << QPointF(11, 1) << QPointF(13, 3) << QPointF(17, 6) << QPointF(18, 3) << QPointF(20, 2);
45 *series << QPointF(11, 1) << QPointF(13, 3) << QPointF(17, 6) << QPointF(18, 3) << QPointF(20, 2);
46 //![2]
46 //![2]
47
47
48 //![3]
48 //![3]
49 QChart* chart = new QChart();
49 QChart* chart = new QChart();
50 chart->addSeries(series);
50 chart->addSeries(series);
51 chart->setTitle("Simple spline chart example");
51 chart->setTitle("Simple spline chart example");
52 chart->axisY()->setRange(0, 10);
52 //![3]
53 //![3]
53
54
54 //![4]
55 //![4]
55 QChartView* chartView = new QChartView(chart);
56 QChartView* chartView = new QChartView(chart);
56 chartView->setRenderHint(QPainter::Antialiasing);
57 chartView->setRenderHint(QPainter::Antialiasing);
57 //![4]
58 //![4]
58
59
59 //![5]
60 //![5]
60 QMainWindow window;
61 QMainWindow window;
61 window.setCentralWidget(chartView);
62 window.setCentralWidget(chartView);
62 window.resize(400, 300);
63 window.resize(400, 300);
63 window.show();
64 window.show();
64 //![5]
65 //![5]
65
66
66 return a.exec();
67 return a.exec();
67 }
68 }
General Comments 0
You need to be logged in to leave comments. Login now