summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarek Rosa <marek.rosa@digia.com>2012-06-03 13:30:02 +0300
committerMarek Rosa <marek.rosa@digia.com>2012-06-03 13:30:02 +0300
commit5e4ada19cdb6ef592e2caf79489e37a06ae021d5 (patch)
treeb24967a94e00e4e5f68d5b2f1dce06c692071e5b
parentafd78609180446d3eb091f78b84e695ed9298b01 (diff)
Added barmodelmapper example
-rw-r--r--examples/barmodelmapper/barmodelmapper.pro18
-rw-r--r--examples/barmodelmapper/customtablemodel.cpp117
-rw-r--r--examples/barmodelmapper/customtablemodel.h51
-rw-r--r--examples/barmodelmapper/main.cpp31
-rw-r--r--examples/barmodelmapper/tablewidget.cpp106
-rw-r--r--examples/barmodelmapper/tablewidget.h34
-rw-r--r--examples/examples.pro3
-rw-r--r--examples/modeldata/tablewidget.cpp2
8 files changed, 360 insertions, 2 deletions
diff --git a/examples/barmodelmapper/barmodelmapper.pro b/examples/barmodelmapper/barmodelmapper.pro
new file mode 100644
index 00000000..93c3e7e6
--- /dev/null
+++ b/examples/barmodelmapper/barmodelmapper.pro
@@ -0,0 +1,18 @@
+!include( ../examples.pri ) {
+ error( "Couldn't find the examples.pri file!" )
+}
+
+QT += core gui
+
+TARGET = barmodelmapper
+TEMPLATE = app
+
+
+SOURCES += main.cpp\
+ tablewidget.cpp \
+ customtablemodel.cpp
+
+HEADERS += tablewidget.h \
+ customtablemodel.h
+
+!system_build:mac: QMAKE_POST_LINK += "$$MAC_POST_LINK_PREFIX $$MAC_EXAMPLES_BIN_DIR"
diff --git a/examples/barmodelmapper/customtablemodel.cpp b/examples/barmodelmapper/customtablemodel.cpp
new file mode 100644
index 00000000..c1374890
--- /dev/null
+++ b/examples/barmodelmapper/customtablemodel.cpp
@@ -0,0 +1,117 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Digia Plc
+** All rights reserved.
+** For any questions to Digia, please use contact form at http://qt.digia.com
+**
+** This file is part of the Qt Commercial Charts Add-on.
+**
+** $QT_BEGIN_LICENSE$
+** Licensees holding valid Qt Commercial licenses may use this file in
+** accordance with the Qt Commercial License Agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Digia.
+**
+** If you have questions regarding the use of this file, please use
+** contact form at http://qt.digia.com
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "customtablemodel.h"
+#include <QVector>
+#include <QTime>
+#include <QRect>
+#include <QColor>
+
+CustomTableModel::CustomTableModel(QObject *parent) :
+ QAbstractTableModel(parent)
+{
+ qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
+
+ m_columnCount = 6;
+ m_rowCount = 10;
+
+ // m_data
+ for (int i = 0; i < m_rowCount; i++)
+ {
+ QVector<qreal>* dataVec = new QVector<qreal>(m_columnCount);
+ for (int k = 0; k < dataVec->size(); k++)
+ {
+ if (k%2 == 0)
+ dataVec->replace(k, i * 50 + qrand()%20);
+ else
+ dataVec->replace(k, qrand()%100);
+ }
+ m_data.append(dataVec);
+ }
+}
+
+int CustomTableModel::rowCount(const QModelIndex & parent) const
+{
+ Q_UNUSED(parent)
+ return m_data.count();
+}
+
+int CustomTableModel::columnCount(const QModelIndex & parent) const
+{
+ Q_UNUSED(parent)
+ return m_columnCount;
+}
+
+QVariant CustomTableModel::headerData (int section, Qt::Orientation orientation, int role ) const
+{
+ if (role != Qt::DisplayRole)
+ return QVariant();
+
+ if (orientation == Qt::Horizontal)
+ {
+ return QString("201%1").arg(section);
+ }
+ else
+ return QString("%1").arg(section);
+}
+
+QVariant CustomTableModel::data(const QModelIndex & index, int role) const
+{
+ if (role == Qt::DisplayRole)
+ {
+ return m_data[index.row()]->at(index.column());
+ }
+ else if (role == Qt::EditRole)
+ {
+ return m_data[index.row()]->at(index.column());
+ }
+ else if (role == Qt::BackgroundRole)
+ {
+ QRect rect;
+ foreach(rect, m_mapping)
+ if(rect.contains(index.column(), index.row()))
+ return QColor(m_mapping.key(rect));
+
+ // cell not mapped return white color
+ return QColor(Qt::white);
+ }
+ return QVariant();
+}
+
+bool CustomTableModel::setData ( const QModelIndex & index, const QVariant & value, int role)
+{
+ if (index.isValid() && role == Qt::EditRole)
+ {
+ m_data[index.row()]->replace(index.column(), value.toDouble());
+ emit dataChanged(index, index);
+ return true;
+ }
+ return false;
+}
+
+Qt::ItemFlags CustomTableModel::flags ( const QModelIndex & index ) const
+{
+ return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
+}
+
+void CustomTableModel::addMapping(QString color, QRect area)
+{
+ m_mapping.insertMulti(color, area);
+}
diff --git a/examples/barmodelmapper/customtablemodel.h b/examples/barmodelmapper/customtablemodel.h
new file mode 100644
index 00000000..64d5f193
--- /dev/null
+++ b/examples/barmodelmapper/customtablemodel.h
@@ -0,0 +1,51 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Digia Plc
+** All rights reserved.
+** For any questions to Digia, please use contact form at http://qt.digia.com
+**
+** This file is part of the Qt Commercial Charts Add-on.
+**
+** $QT_BEGIN_LICENSE$
+** Licensees holding valid Qt Commercial licenses may use this file in
+** accordance with the Qt Commercial License Agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Digia.
+**
+** If you have questions regarding the use of this file, please use
+** contact form at http://qt.digia.com
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef XYPOINTSMODEL_H
+#define XYPOINTSMODEL_H
+
+#include <QAbstractTableModel>
+#include <QHash>
+#include <QRect>
+
+class CustomTableModel : public QAbstractTableModel
+{
+ Q_OBJECT
+public:
+ explicit CustomTableModel(QObject *parent = 0);
+
+ int rowCount ( const QModelIndex & parent = QModelIndex() ) const;
+ int columnCount ( const QModelIndex & parent = QModelIndex() ) const;
+ QVariant headerData (int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const;
+ QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
+ bool setData ( const QModelIndex & index, const QVariant & value, int role = Qt::EditRole );
+ Qt::ItemFlags flags ( const QModelIndex & index ) const;
+
+ void addMapping(QString color, QRect area);
+ void clearMapping() { m_mapping.clear(); }
+
+private:
+ QList<QVector<qreal> * > m_data;
+ QHash<QString, QRect> m_mapping;
+ int m_columnCount;
+ int m_rowCount;
+};
+
+#endif // XYPOINTSMODEL_H
diff --git a/examples/barmodelmapper/main.cpp b/examples/barmodelmapper/main.cpp
new file mode 100644
index 00000000..9b452ce3
--- /dev/null
+++ b/examples/barmodelmapper/main.cpp
@@ -0,0 +1,31 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Digia Plc
+** All rights reserved.
+** For any questions to Digia, please use contact form at http://qt.digia.com
+**
+** This file is part of the Qt Commercial Charts Add-on.
+**
+** $QT_BEGIN_LICENSE$
+** Licensees holding valid Qt Commercial licenses may use this file in
+** accordance with the Qt Commercial License Agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Digia.
+**
+** If you have questions regarding the use of this file, please use
+** contact form at http://qt.digia.com
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include <QtGui/QApplication>
+#include "tablewidget.h"
+
+int main(int argc, char *argv[])
+{
+ QApplication a(argc, argv);
+ TableWidget w;
+ w.show();
+
+ return a.exec();
+}
diff --git a/examples/barmodelmapper/tablewidget.cpp b/examples/barmodelmapper/tablewidget.cpp
new file mode 100644
index 00000000..0f44c107
--- /dev/null
+++ b/examples/barmodelmapper/tablewidget.cpp
@@ -0,0 +1,106 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Digia Plc
+** All rights reserved.
+** For any questions to Digia, please use contact form at http://qt.digia.com
+**
+** This file is part of the Qt Commercial Charts Add-on.
+**
+** $QT_BEGIN_LICENSE$
+** Licensees holding valid Qt Commercial licenses may use this file in
+** accordance with the Qt Commercial License Agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Digia.
+**
+** If you have questions regarding the use of this file, please use
+** contact form at http://qt.digia.com
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "tablewidget.h"
+#include "customtablemodel.h"
+#include <QGridLayout>
+#include <QTableView>
+#include <QChart>
+#include <QChartView>
+#include <QLineSeries>
+#include <QVXYModelMapper>
+#include <QGroupedBarSeries>
+#include <QBarSet>
+#include <QVBarModelMapper>
+#include <QHeaderView>
+
+QTCOMMERCIALCHART_USE_NAMESPACE
+
+TableWidget::TableWidget(QWidget *parent)
+ : QWidget(parent)
+{
+ // create simple model for storing data
+ // user's table data model
+ //! [1]
+ CustomTableModel *model = new CustomTableModel;
+ //! [1]
+
+ //! [2]
+ // create table view and add model to it
+ QTableView *tableView = new QTableView;
+ tableView->setModel(model);
+ tableView->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
+ tableView->verticalHeader()->setResizeMode(QHeaderView::Stretch);
+ //! [2]
+
+ //! [3]
+ QChart *chart = new QChart;
+ chart->setAnimationOptions(QChart::AllAnimations);
+ //! [3]
+
+ // series 1
+ //! [4]
+ QGroupedBarSeries *series = new QGroupedBarSeries;
+
+ int first = 3;
+ int count = 5;
+ QVBarModelMapper *mapper = new QVBarModelMapper(this);
+ mapper->setFirstBarSetColumn(1);
+ mapper->setLastBarSetColumn(4);
+ mapper->setFirst(3);
+ mapper->setCount(count);
+ mapper->setSeries(series);
+ mapper->setModel(model);
+ chart->addSeries(series);
+ //! [4]
+
+ QStringList categories;
+ categories << "June" << "July" << "August" << "September" << "October" << "November";
+
+ chart->axisX()->categories()->insert(categories);
+
+ //! [5]
+ // for storing color hex from the series
+ QString seriesColorHex = "#000000";
+
+ // get the color of the series and use it for showing the mapped area
+ QList<QBarSet*> barsets = series->barSets();
+ for (int i = 0; i < barsets.count(); i++) {
+ seriesColorHex = "#" + QString::number(barsets.at(i)->brush().color().rgb(), 16).right(6).toUpper();
+ model->addMapping(seriesColorHex, QRect(1 + i, first, 1, barsets.at(i)->count()));
+ }
+ //! [5]
+
+ //! [8]
+ QChartView *chartView = new QChartView(chart);
+ chartView->setRenderHint(QPainter::Antialiasing);
+ chartView->setMinimumSize(640, 480);
+ //! [8]
+
+ //! [9]
+ // create main layout
+ QGridLayout* mainLayout = new QGridLayout;
+ mainLayout->addWidget(tableView, 1, 0);
+ mainLayout->addWidget(chartView, 1, 1);
+ mainLayout->setColumnStretch(1, 1);
+ mainLayout->setColumnStretch(0, 0);
+ setLayout(mainLayout);
+ //! [9]
+}
diff --git a/examples/barmodelmapper/tablewidget.h b/examples/barmodelmapper/tablewidget.h
new file mode 100644
index 00000000..731620ad
--- /dev/null
+++ b/examples/barmodelmapper/tablewidget.h
@@ -0,0 +1,34 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Digia Plc
+** All rights reserved.
+** For any questions to Digia, please use contact form at http://qt.digia.com
+**
+** This file is part of the Qt Commercial Charts Add-on.
+**
+** $QT_BEGIN_LICENSE$
+** Licensees holding valid Qt Commercial licenses may use this file in
+** accordance with the Qt Commercial License Agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Digia.
+**
+** If you have questions regarding the use of this file, please use
+** contact form at http://qt.digia.com
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef TABLEWIDGET_H
+#define TABLEWIDGET_H
+
+#include <QtGui/QWidget>
+
+class TableWidget : public QWidget
+{
+ Q_OBJECT
+
+public:
+ TableWidget(QWidget *parent = 0);
+};
+
+#endif // TABLEWIDGET_H
diff --git a/examples/examples.pro b/examples/examples.pro
index d7fa2bca..4a2b6998 100644
--- a/examples/examples.pro
+++ b/examples/examples.pro
@@ -21,4 +21,5 @@ SUBDIRS += \
zoomlinechart \
modeldata \
groupedbarchart \
- legend
+ legend \
+ barmodelmapper
diff --git a/examples/modeldata/tablewidget.cpp b/examples/modeldata/tablewidget.cpp
index fcd70ff5..1c5b811d 100644
--- a/examples/modeldata/tablewidget.cpp
+++ b/examples/modeldata/tablewidget.cpp
@@ -94,7 +94,7 @@ TableWidget::TableWidget(QWidget *parent)
//! [8]
QChartView *chartView = new QChartView(chart);
chartView->setRenderHint(QPainter::Antialiasing);
-// chartView->setMinimumSize(640, 480);
+ chartView->setMinimumSize(640, 480);
//! [8]
//! [9]