##// END OF EJS Templates
Added barmodelmapper example
Marek Rosa -
r1367:5e4ada19cdb6
parent child
Show More
@@ -0,0 +1,18
1 !include( ../examples.pri ) {
2 error( "Couldn't find the examples.pri file!" )
3 }
4
5 QT += core gui
6
7 TARGET = barmodelmapper
8 TEMPLATE = app
9
10
11 SOURCES += main.cpp\
12 tablewidget.cpp \
13 customtablemodel.cpp
14
15 HEADERS += tablewidget.h \
16 customtablemodel.h
17
18 !system_build:mac: QMAKE_POST_LINK += "$$MAC_POST_LINK_PREFIX $$MAC_EXAMPLES_BIN_DIR"
@@ -0,0 +1,117
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 = 6;
33 m_rowCount = 10;
34
35 // m_data
36 for (int i = 0; i < m_rowCount; i++)
37 {
38 QVector<qreal>* dataVec = new QVector<qreal>(m_columnCount);
39 for (int k = 0; k < dataVec->size(); k++)
40 {
41 if (k%2 == 0)
42 dataVec->replace(k, i * 50 + qrand()%20);
43 else
44 dataVec->replace(k, qrand()%100);
45 }
46 m_data.append(dataVec);
47 }
48 }
49
50 int CustomTableModel::rowCount(const QModelIndex & parent) const
51 {
52 Q_UNUSED(parent)
53 return m_data.count();
54 }
55
56 int CustomTableModel::columnCount(const QModelIndex & parent) const
57 {
58 Q_UNUSED(parent)
59 return m_columnCount;
60 }
61
62 QVariant CustomTableModel::headerData (int section, Qt::Orientation orientation, int role ) const
63 {
64 if (role != Qt::DisplayRole)
65 return QVariant();
66
67 if (orientation == Qt::Horizontal)
68 {
69 return QString("201%1").arg(section);
70 }
71 else
72 return QString("%1").arg(section);
73 }
74
75 QVariant CustomTableModel::data(const QModelIndex & index, int role) const
76 {
77 if (role == Qt::DisplayRole)
78 {
79 return m_data[index.row()]->at(index.column());
80 }
81 else if (role == Qt::EditRole)
82 {
83 return m_data[index.row()]->at(index.column());
84 }
85 else if (role == Qt::BackgroundRole)
86 {
87 QRect rect;
88 foreach(rect, m_mapping)
89 if(rect.contains(index.column(), index.row()))
90 return QColor(m_mapping.key(rect));
91
92 // cell not mapped return white color
93 return QColor(Qt::white);
94 }
95 return QVariant();
96 }
97
98 bool CustomTableModel::setData ( const QModelIndex & index, const QVariant & value, int role)
99 {
100 if (index.isValid() && role == Qt::EditRole)
101 {
102 m_data[index.row()]->replace(index.column(), value.toDouble());
103 emit dataChanged(index, index);
104 return true;
105 }
106 return false;
107 }
108
109 Qt::ItemFlags CustomTableModel::flags ( const QModelIndex & index ) const
110 {
111 return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
112 }
113
114 void CustomTableModel::addMapping(QString color, QRect area)
115 {
116 m_mapping.insertMulti(color, area);
117 }
@@ -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 XYPOINTSMODEL_H
22 #define XYPOINTSMODEL_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 // XYPOINTSMODEL_H
@@ -0,0 +1,31
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 <QtGui/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
30 return a.exec();
31 }
@@ -0,0 +1,106
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 <QGroupedBarSeries>
30 #include <QBarSet>
31 #include <QVBarModelMapper>
32 #include <QHeaderView>
33
34 QTCOMMERCIALCHART_USE_NAMESPACE
35
36 TableWidget::TableWidget(QWidget *parent)
37 : QWidget(parent)
38 {
39 // create simple model for storing data
40 // user's table data model
41 //! [1]
42 CustomTableModel *model = new CustomTableModel;
43 //! [1]
44
45 //! [2]
46 // create table view and add model to it
47 QTableView *tableView = new QTableView;
48 tableView->setModel(model);
49 tableView->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
50 tableView->verticalHeader()->setResizeMode(QHeaderView::Stretch);
51 //! [2]
52
53 //! [3]
54 QChart *chart = new QChart;
55 chart->setAnimationOptions(QChart::AllAnimations);
56 //! [3]
57
58 // series 1
59 //! [4]
60 QGroupedBarSeries *series = new QGroupedBarSeries;
61
62 int first = 3;
63 int count = 5;
64 QVBarModelMapper *mapper = new QVBarModelMapper(this);
65 mapper->setFirstBarSetColumn(1);
66 mapper->setLastBarSetColumn(4);
67 mapper->setFirst(3);
68 mapper->setCount(count);
69 mapper->setSeries(series);
70 mapper->setModel(model);
71 chart->addSeries(series);
72 //! [4]
73
74 QStringList categories;
75 categories << "June" << "July" << "August" << "September" << "October" << "November";
76
77 chart->axisX()->categories()->insert(categories);
78
79 //! [5]
80 // for storing color hex from the series
81 QString seriesColorHex = "#000000";
82
83 // get the color of the series and use it for showing the mapped area
84 QList<QBarSet*> barsets = series->barSets();
85 for (int i = 0; i < barsets.count(); i++) {
86 seriesColorHex = "#" + QString::number(barsets.at(i)->brush().color().rgb(), 16).right(6).toUpper();
87 model->addMapping(seriesColorHex, QRect(1 + i, first, 1, barsets.at(i)->count()));
88 }
89 //! [5]
90
91 //! [8]
92 QChartView *chartView = new QChartView(chart);
93 chartView->setRenderHint(QPainter::Antialiasing);
94 chartView->setMinimumSize(640, 480);
95 //! [8]
96
97 //! [9]
98 // create main layout
99 QGridLayout* mainLayout = new QGridLayout;
100 mainLayout->addWidget(tableView, 1, 0);
101 mainLayout->addWidget(chartView, 1, 1);
102 mainLayout->setColumnStretch(1, 1);
103 mainLayout->setColumnStretch(0, 0);
104 setLayout(mainLayout);
105 //! [9]
106 }
@@ -0,0 +1,34
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 <QtGui/QWidget>
25
26 class TableWidget : public QWidget
27 {
28 Q_OBJECT
29
30 public:
31 TableWidget(QWidget *parent = 0);
32 };
33
34 #endif // TABLEWIDGET_H
@@ -21,4 +21,5 SUBDIRS += \
21 zoomlinechart \
21 zoomlinechart \
22 modeldata \
22 modeldata \
23 groupedbarchart \
23 groupedbarchart \
24 legend
24 legend \
25 barmodelmapper
@@ -94,7 +94,7 TableWidget::TableWidget(QWidget *parent)
94 //! [8]
94 //! [8]
95 QChartView *chartView = new QChartView(chart);
95 QChartView *chartView = new QChartView(chart);
96 chartView->setRenderHint(QPainter::Antialiasing);
96 chartView->setRenderHint(QPainter::Antialiasing);
97 // chartView->setMinimumSize(640, 480);
97 chartView->setMinimumSize(640, 480);
98 //! [8]
98 //! [8]
99
99
100 //! [9]
100 //! [9]
General Comments 0
You need to be logged in to leave comments. Login now