@@ -0,0 +1,110 | |||
|
1 | /**************************************************************************** | |
|
2 | ** | |
|
3 | ** Copyright (C) 2012 Digia Plc | |
|
4 | ** All rights reserved. | |
|
5 | ** For any questions to Digia, please use contact form at http://qt.digia.com | |
|
6 | ** | |
|
7 | ** This file is part of the Qt Commercial Charts Add-on. | |
|
8 | ** | |
|
9 | ** $QT_BEGIN_LICENSE$ | |
|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in | |
|
11 | ** accordance with the Qt Commercial License Agreement provided with the | |
|
12 | ** Software or, alternatively, in accordance with the terms contained in | |
|
13 | ** a written agreement between you and Digia. | |
|
14 | ** | |
|
15 | ** If you have questions regarding the use of this file, please use | |
|
16 | ** contact form at http://qt.digia.com | |
|
17 | ** $QT_END_LICENSE$ | |
|
18 | ** | |
|
19 | ****************************************************************************/ | |
|
20 | ||
|
21 | #include "customtablemodel.h" | |
|
22 | #include <QVector> | |
|
23 | #include <QTime> | |
|
24 | #include <QRect> | |
|
25 | #include <QColor> | |
|
26 | ||
|
27 | CustomTableModel::CustomTableModel(QObject *parent) : | |
|
28 | QAbstractTableModel(parent) | |
|
29 | { | |
|
30 | qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime())); | |
|
31 | ||
|
32 | m_columnCount = 4; | |
|
33 | m_rowCount = 15; | |
|
34 | ||
|
35 | // m_data | |
|
36 | for (int i = 0; i < m_rowCount; i++) { | |
|
37 | QVector<qreal>* dataVec = new QVector<qreal>(m_columnCount); | |
|
38 | for (int k = 0; k < dataVec->size(); k++) { | |
|
39 | if (k % 2 == 0) | |
|
40 | dataVec->replace(k, i * 50 + qrand() % 20); | |
|
41 | else | |
|
42 | dataVec->replace(k, qrand() % 100); | |
|
43 | } | |
|
44 | m_data.append(dataVec); | |
|
45 | } | |
|
46 | } | |
|
47 | ||
|
48 | int CustomTableModel::rowCount(const QModelIndex &parent) const | |
|
49 | { | |
|
50 | Q_UNUSED(parent) | |
|
51 | return m_data.count(); | |
|
52 | } | |
|
53 | ||
|
54 | int CustomTableModel::columnCount(const QModelIndex &parent) const | |
|
55 | { | |
|
56 | Q_UNUSED(parent) | |
|
57 | return m_columnCount; | |
|
58 | } | |
|
59 | ||
|
60 | QVariant CustomTableModel::headerData(int section, Qt::Orientation orientation, int role) const | |
|
61 | { | |
|
62 | if (role != Qt::DisplayRole) | |
|
63 | return QVariant(); | |
|
64 | ||
|
65 | if (orientation == Qt::Horizontal) { | |
|
66 | if (section % 2 == 0) | |
|
67 | return "x"; | |
|
68 | else | |
|
69 | return "y"; | |
|
70 | } else { | |
|
71 | return QString("%1").arg(section + 1); | |
|
72 | } | |
|
73 | } | |
|
74 | ||
|
75 | QVariant CustomTableModel::data(const QModelIndex &index, int role) const | |
|
76 | { | |
|
77 | if (role == Qt::DisplayRole) { | |
|
78 | return m_data[index.row()]->at(index.column()); | |
|
79 | } else if (role == Qt::EditRole) { | |
|
80 | return m_data[index.row()]->at(index.column()); | |
|
81 | } else if (role == Qt::BackgroundRole) { | |
|
82 | foreach (QRect rect, m_mapping) { | |
|
83 | if (rect.contains(index.column(), index.row())) | |
|
84 | return QColor(m_mapping.key(rect)); | |
|
85 | } | |
|
86 | // cell not mapped return white color | |
|
87 | return QColor(Qt::white); | |
|
88 | } | |
|
89 | return QVariant(); | |
|
90 | } | |
|
91 | ||
|
92 | bool CustomTableModel::setData(const QModelIndex &index, const QVariant &value, int role) | |
|
93 | { | |
|
94 | if (index.isValid() && role == Qt::EditRole) { | |
|
95 | m_data[index.row()]->replace(index.column(), value.toDouble()); | |
|
96 | emit dataChanged(index, index); | |
|
97 | return true; | |
|
98 | } | |
|
99 | return false; | |
|
100 | } | |
|
101 | ||
|
102 | Qt::ItemFlags CustomTableModel::flags(const QModelIndex &index) const | |
|
103 | { | |
|
104 | return QAbstractItemModel::flags(index) | Qt::ItemIsEditable; | |
|
105 | } | |
|
106 | ||
|
107 | void CustomTableModel::addMapping(QString color, QRect area) | |
|
108 | { | |
|
109 | m_mapping.insertMulti(color, area); | |
|
110 | } |
@@ -0,0 +1,51 | |||
|
1 | /**************************************************************************** | |
|
2 | ** | |
|
3 | ** Copyright (C) 2012 Digia Plc | |
|
4 | ** All rights reserved. | |
|
5 | ** For any questions to Digia, please use contact form at http://qt.digia.com | |
|
6 | ** | |
|
7 | ** This file is part of the Qt Commercial Charts Add-on. | |
|
8 | ** | |
|
9 | ** $QT_BEGIN_LICENSE$ | |
|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in | |
|
11 | ** accordance with the Qt Commercial License Agreement provided with the | |
|
12 | ** Software or, alternatively, in accordance with the terms contained in | |
|
13 | ** a written agreement between you and Digia. | |
|
14 | ** | |
|
15 | ** If you have questions regarding the use of this file, please use | |
|
16 | ** contact form at http://qt.digia.com | |
|
17 | ** $QT_END_LICENSE$ | |
|
18 | ** | |
|
19 | ****************************************************************************/ | |
|
20 | ||
|
21 | #ifndef CUSTOMTABLEMODEL_H | |
|
22 | #define CUSTOMTABLEMODEL_H | |
|
23 | ||
|
24 | #include <QAbstractTableModel> | |
|
25 | #include <QHash> | |
|
26 | #include <QRect> | |
|
27 | ||
|
28 | class CustomTableModel : public QAbstractTableModel | |
|
29 | { | |
|
30 | Q_OBJECT | |
|
31 | public: | |
|
32 | explicit CustomTableModel(QObject *parent = 0); | |
|
33 | ||
|
34 | int rowCount(const QModelIndex &parent = QModelIndex()) const; | |
|
35 | int columnCount(const QModelIndex &parent = QModelIndex()) const; | |
|
36 | QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; | |
|
37 | QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; | |
|
38 | bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole); | |
|
39 | Qt::ItemFlags flags(const QModelIndex &index) const; | |
|
40 | ||
|
41 | void addMapping(QString color, QRect area); | |
|
42 | void clearMapping() { m_mapping.clear(); } | |
|
43 | ||
|
44 | private: | |
|
45 | QList<QVector<qreal> * > m_data; | |
|
46 | QHash<QString, QRect> m_mapping; | |
|
47 | int m_columnCount; | |
|
48 | int m_rowCount; | |
|
49 | }; | |
|
50 | ||
|
51 | #endif // CUSTOMTABLEMODEL_H |
@@ -0,0 +1,16 | |||
|
1 | !include( ../examples.pri ) { | |
|
2 | error( "Couldn't find the examples.pri file!" ) | |
|
3 | } | |
|
4 | ||
|
5 | QT += core gui | |
|
6 | ||
|
7 | TARGET = logaxis | |
|
8 | TEMPLATE = app | |
|
9 | ||
|
10 | ||
|
11 | SOURCES += main.cpp\ | |
|
12 | tablewidget.cpp \ | |
|
13 | customtablemodel.cpp | |
|
14 | ||
|
15 | HEADERS += tablewidget.h \ | |
|
16 | customtablemodel.h |
@@ -0,0 +1,30 | |||
|
1 | /**************************************************************************** | |
|
2 | ** | |
|
3 | ** Copyright (C) 2012 Digia Plc | |
|
4 | ** All rights reserved. | |
|
5 | ** For any questions to Digia, please use contact form at http://qt.digia.com | |
|
6 | ** | |
|
7 | ** This file is part of the Qt Commercial Charts Add-on. | |
|
8 | ** | |
|
9 | ** $QT_BEGIN_LICENSE$ | |
|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in | |
|
11 | ** accordance with the Qt Commercial License Agreement provided with the | |
|
12 | ** Software or, alternatively, in accordance with the terms contained in | |
|
13 | ** a written agreement between you and Digia. | |
|
14 | ** | |
|
15 | ** If you have questions regarding the use of this file, please use | |
|
16 | ** contact form at http://qt.digia.com | |
|
17 | ** $QT_END_LICENSE$ | |
|
18 | ** | |
|
19 | ****************************************************************************/ | |
|
20 | ||
|
21 | #include <QApplication> | |
|
22 | #include "tablewidget.h" | |
|
23 | ||
|
24 | int main(int argc, char *argv[]) | |
|
25 | { | |
|
26 | QApplication a(argc, argv); | |
|
27 | TableWidget w; | |
|
28 | w.show(); | |
|
29 | return a.exec(); | |
|
30 | } |
@@ -0,0 +1,153 | |||
|
1 | /**************************************************************************** | |
|
2 | ** | |
|
3 | ** Copyright (C) 2012 Digia Plc | |
|
4 | ** All rights reserved. | |
|
5 | ** For any questions to Digia, please use contact form at http://qt.digia.com | |
|
6 | ** | |
|
7 | ** This file is part of the Qt Commercial Charts Add-on. | |
|
8 | ** | |
|
9 | ** $QT_BEGIN_LICENSE$ | |
|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in | |
|
11 | ** accordance with the Qt Commercial License Agreement provided with the | |
|
12 | ** Software or, alternatively, in accordance with the terms contained in | |
|
13 | ** a written agreement between you and Digia. | |
|
14 | ** | |
|
15 | ** If you have questions regarding the use of this file, please use | |
|
16 | ** contact form at http://qt.digia.com | |
|
17 | ** $QT_END_LICENSE$ | |
|
18 | ** | |
|
19 | ****************************************************************************/ | |
|
20 | ||
|
21 | #include "tablewidget.h" | |
|
22 | #include "customtablemodel.h" | |
|
23 | #include <QGridLayout> | |
|
24 | #include <QTableView> | |
|
25 | #include <QChart> | |
|
26 | #include <QChartView> | |
|
27 | #include <QLineSeries> | |
|
28 | #include <QVXYModelMapper> | |
|
29 | #include <QHeaderView> | |
|
30 | #include <QDoubleSpinBox> | |
|
31 | #include <QLogValueAxis> | |
|
32 | #include <QLineEdit> | |
|
33 | #include <QDateTimeAxis> | |
|
34 | ||
|
35 | QTCOMMERCIALCHART_USE_NAMESPACE | |
|
36 | ||
|
37 | TableWidget::TableWidget(QWidget *parent) | |
|
38 | : QWidget(parent), | |
|
39 | m_logBaseSpinBox(0), | |
|
40 | m_axis(0) | |
|
41 | { | |
|
42 | // create simple model for storing data | |
|
43 | // user's table data model | |
|
44 | //! [1] | |
|
45 | CustomTableModel *model = new CustomTableModel; | |
|
46 | //! [1] | |
|
47 | ||
|
48 | //! [2] | |
|
49 | // create table view and add model to it | |
|
50 | QTableView *tableView = new QTableView; | |
|
51 | tableView->setModel(model); | |
|
52 | #if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)) | |
|
53 | tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); | |
|
54 | tableView->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch); | |
|
55 | #else | |
|
56 | tableView->horizontalHeader()->setResizeMode(QHeaderView::Stretch); | |
|
57 | tableView->verticalHeader()->setResizeMode(QHeaderView::Stretch); | |
|
58 | #endif | |
|
59 | //! [2] | |
|
60 | ||
|
61 | //! [3] | |
|
62 | QChart *chart = new QChart; | |
|
63 | chart->setAnimationOptions(QChart::AllAnimations); | |
|
64 | //! [3] | |
|
65 | ||
|
66 | // series 1 | |
|
67 | //! [4] | |
|
68 | QLineSeries *series = new QLineSeries; | |
|
69 | series->setName("Line 1"); | |
|
70 | QVXYModelMapper *mapper = new QVXYModelMapper(this); | |
|
71 | mapper->setXColumn(0); | |
|
72 | mapper->setYColumn(1); | |
|
73 | mapper->setSeries(series); | |
|
74 | mapper->setModel(model); | |
|
75 | chart->addSeries(series); | |
|
76 | //! [4] | |
|
77 | ||
|
78 | //! [5] | |
|
79 | // for storing color hex from the series | |
|
80 | QString seriesColorHex = "#000000"; | |
|
81 | ||
|
82 | // get the color of the series and use it for showing the mapped area | |
|
83 | seriesColorHex = "#" + QString::number(series->pen().color().rgb(), 16).right(6).toUpper(); | |
|
84 | model->addMapping(seriesColorHex, QRect(0, 0, 2, model->rowCount())); | |
|
85 | //! [5] | |
|
86 | ||
|
87 | ||
|
88 | // series 2 | |
|
89 | //! [6] | |
|
90 | QLineSeries *series2 = new QLineSeries; | |
|
91 | series2->setName("Line 2"); | |
|
92 | ||
|
93 | mapper = new QVXYModelMapper(this); | |
|
94 | mapper->setXColumn(2); | |
|
95 | mapper->setYColumn(3); | |
|
96 | mapper->setSeries(series2); | |
|
97 | mapper->setModel(model); | |
|
98 | chart->addSeries(series2); | |
|
99 | //! [6] | |
|
100 | ||
|
101 | //! [7] | |
|
102 | // get the color of the series and use it for showing the mapped area | |
|
103 | seriesColorHex = "#" + QString::number(series2->pen().color().rgb(), 16).right(6).toUpper(); | |
|
104 | model->addMapping(seriesColorHex, QRect(2, 0, 2, model->rowCount())); | |
|
105 | //! [7] | |
|
106 | ||
|
107 | //! [8] | |
|
108 | chart->createDefaultAxes(); | |
|
109 | ||
|
110 | m_axis = new QLogValueAxis; | |
|
111 | chart->addAxis(m_axis, Qt::AlignRight); | |
|
112 | chart->setAxisY(m_axis, series); | |
|
113 | chart->setAxisY(m_axis, series2); | |
|
114 | // series->attachAxis(m_axis); | |
|
115 | // series2->attachAxis(m_axis); | |
|
116 | ||
|
117 | ||
|
118 | QChartView *chartView = new QChartView(chart); | |
|
119 | chartView->setRenderHint(QPainter::Antialiasing); | |
|
120 | chartView->setMinimumSize(640, 480); | |
|
121 | //! [8] | |
|
122 | ||
|
123 | m_logBaseSpinBox = new QDoubleSpinBox; | |
|
124 | m_logBaseSpinBox->setRange(0.01, 100.0); | |
|
125 | ||
|
126 | connect(m_logBaseSpinBox, SIGNAL(valueChanged(double)), this, SLOT(logBaseChanged(double))); | |
|
127 | ||
|
128 | QLineEdit *format = new QLineEdit; | |
|
129 | connect(format, SIGNAL(textEdited(QString)), this, SLOT(formatChanged(QString))); | |
|
130 | ||
|
131 | //! [9] | |
|
132 | // create main layout | |
|
133 | QGridLayout *mainLayout = new QGridLayout; | |
|
134 | mainLayout->addWidget(tableView, 1, 0); | |
|
135 | mainLayout->addWidget(chartView, 1, 1); | |
|
136 | mainLayout->addWidget(m_logBaseSpinBox, 1, 2); | |
|
137 | mainLayout->addWidget(format, 1, 3); | |
|
138 | mainLayout->setColumnStretch(1, 1); | |
|
139 | mainLayout->setColumnStretch(0, 0); | |
|
140 | setLayout(mainLayout); | |
|
141 | //! [9] | |
|
142 | } | |
|
143 | ||
|
144 | void TableWidget::logBaseChanged(double base) | |
|
145 | { | |
|
146 | m_axis->setBase(base); | |
|
147 | } | |
|
148 | ||
|
149 | void TableWidget::formatChanged(const QString& format) | |
|
150 | { | |
|
151 | m_axis->setLabelFormat(format); | |
|
152 | // m_axis->setFormat(format); | |
|
153 | } |
@@ -0,0 +1,55 | |||
|
1 | /**************************************************************************** | |
|
2 | ** | |
|
3 | ** Copyright (C) 2012 Digia Plc | |
|
4 | ** All rights reserved. | |
|
5 | ** For any questions to Digia, please use contact form at http://qt.digia.com | |
|
6 | ** | |
|
7 | ** This file is part of the Qt Commercial Charts Add-on. | |
|
8 | ** | |
|
9 | ** $QT_BEGIN_LICENSE$ | |
|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in | |
|
11 | ** accordance with the Qt Commercial License Agreement provided with the | |
|
12 | ** Software or, alternatively, in accordance with the terms contained in | |
|
13 | ** a written agreement between you and Digia. | |
|
14 | ** | |
|
15 | ** If you have questions regarding the use of this file, please use | |
|
16 | ** contact form at http://qt.digia.com | |
|
17 | ** $QT_END_LICENSE$ | |
|
18 | ** | |
|
19 | ****************************************************************************/ | |
|
20 | ||
|
21 | #ifndef TABLEWIDGET_H | |
|
22 | #define TABLEWIDGET_H | |
|
23 | ||
|
24 | #include <QWidget> | |
|
25 | #include <QChartGlobal> | |
|
26 | ||
|
27 | class QDoubleSpinBox; | |
|
28 | ||
|
29 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
|
30 | class QChart; | |
|
31 | class QLogValueAxis; | |
|
32 | class QDateTimeAxis; | |
|
33 | QTCOMMERCIALCHART_END_NAMESPACE | |
|
34 | ||
|
35 | QTCOMMERCIALCHART_USE_NAMESPACE | |
|
36 | ||
|
37 | class TableWidget : public QWidget | |
|
38 | { | |
|
39 | Q_OBJECT | |
|
40 | ||
|
41 | public: | |
|
42 | TableWidget(QWidget *parent = 0); | |
|
43 | ||
|
44 | public slots: | |
|
45 | void logBaseChanged(double base); | |
|
46 | void formatChanged(const QString& format); | |
|
47 | ||
|
48 | private: | |
|
49 | QDoubleSpinBox *m_logBaseSpinBox; | |
|
50 | QLogValueAxis *m_axis; | |
|
51 | // QDateTimeAxis *m_axis; | |
|
52 | ||
|
53 | }; | |
|
54 | ||
|
55 | #endif // TABLEWIDGET_H |
General Comments 0
You need to be logged in to leave comments.
Login now