diff --git a/examples/examples.pro b/examples/examples.pro index 201365f..10a3d68 100644 --- a/examples/examples.pro +++ b/examples/examples.pro @@ -15,7 +15,8 @@ SUBDIRS += \ stackedbarchart \ stackedbarchartdrilldown \ tablemodelchart \ - zoomlinechart + zoomlinechart \ + modeldata diff --git a/examples/modeldata/customtablemodel.cpp b/examples/modeldata/customtablemodel.cpp new file mode 100644 index 0000000..90480ee --- /dev/null +++ b/examples/modeldata/customtablemodel.cpp @@ -0,0 +1,125 @@ +/**************************************************************************** +** +** 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 +#include +#include +#include + +CustomTableModel::CustomTableModel(QObject *parent) : + QAbstractTableModel(parent) +{ + qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); + + m_columnCount = 4; + m_rowCount = 15; + + // m_data + for (int i = 0; i < m_rowCount; i++) + { + QVector* dataVec = new QVector(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) + { + if (section%2 == 0) + return "x"; + else + return "y"; + } + else + return QString("%1").arg(section + 1); +} + +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); +} + +void CustomTableModel::addMapping(QString color, int left, int top, int right, int bottom) +{ + addMapping(color, QRect(QPoint(left, top), QPoint(right, bottom))); +} diff --git a/examples/modeldata/customtablemodel.h b/examples/modeldata/customtablemodel.h new file mode 100644 index 0000000..b144f41 --- /dev/null +++ b/examples/modeldata/customtablemodel.h @@ -0,0 +1,55 @@ +/**************************************************************************** +** +** 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 +#include +#include +//#include +#include +#include + +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 addMapping(QString color, int left, int top, int right, int bottom); + void clearMapping() { m_mapping.clear(); } + +private: + QList * > m_data; + QHash m_mapping; + int m_columnCount; + int m_rowCount; +}; + +#endif // XYPOINTSMODEL_H diff --git a/examples/modeldata/main.cpp b/examples/modeldata/main.cpp new file mode 100644 index 0000000..9b452ce --- /dev/null +++ b/examples/modeldata/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 +#include "tablewidget.h" + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + TableWidget w; + w.show(); + + return a.exec(); +} diff --git a/examples/modeldata/modeldata.pro b/examples/modeldata/modeldata.pro new file mode 100644 index 0000000..711e531 --- /dev/null +++ b/examples/modeldata/modeldata.pro @@ -0,0 +1,22 @@ +#------------------------------------------------- +# +# Project created by QtCreator 2012-03-08T14:30:24 +# +#------------------------------------------------- + +!include( ../examples.pri ) { + error( "Couldn't find the examples.pri file!" ) +} + +QT += core gui + +TARGET = modeldata +TEMPLATE = app + + +SOURCES += main.cpp\ + tablewidget.cpp \ + customtablemodel.cpp + +HEADERS += tablewidget.h \ + customtablemodel.h diff --git a/examples/modeldata/tablewidget.cpp b/examples/modeldata/tablewidget.cpp new file mode 100644 index 0000000..ee54ebc --- /dev/null +++ b/examples/modeldata/tablewidget.cpp @@ -0,0 +1,87 @@ +/**************************************************************************** +** +** 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 +#include +#include +#include +#include + +QTCOMMERCIALCHART_USE_NAMESPACE + +TableWidget::TableWidget(QWidget *parent) + : QWidget(parent) +{ + // create simple model for storing data + // user's table data model + CustomTableModel *model = new CustomTableModel; + + // create table view and add model to it + QTableView *tableView = new QTableView; + tableView->setModel(model); + tableView->setMinimumWidth(200); +// tableView->setMaximumWidth(200); + // m_tableView->resizeColumnsToContents(); + tableView->setColumnWidth(0, 50); + tableView->setColumnWidth(1, 50); + tableView->setColumnWidth(2, 50); + tableView->setColumnWidth(3, 50); + + QChart *m_chart = new QChart; + m_chart->setAnimationOptions(QChart::AllAnimations); + QChartView *m_chartView = new QChartView(m_chart); + m_chartView->setRenderHint(QPainter::Antialiasing); + m_chartView->setMinimumSize(640, 480); + + // for storing color hex from the series + QString seriesColorHex = "#000000"; + + // series 1 + QLineSeries *m_series = new QLineSeries; + m_series->setModel(model); + m_series->setModelMapping(0, 1, Qt::Vertical); + m_chart->addSeries(m_series); + + // get the color of the series and use it for showing the mapped area + seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper(); + model->addMapping(seriesColorHex, QRect(0, 0, 2, model->rowCount())); + + // series 2 + m_series = new QLineSeries; + m_series->setModel(model); + m_series->setModelMapping(2,3, Qt::Vertical); + m_chart->addSeries(m_series); + + // get the color of the series and use it for showing the mapped area + seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper(); + model->addMapping(seriesColorHex, QRect(2, 0, 2, model->rowCount())); + +// m_chart->axisX()->setRange(0, 800); +// m_chart->axisY()->setRange(0, 120); + + // create main layout + QGridLayout* mainLayout = new QGridLayout; + mainLayout->addWidget(tableView, 1, 0); + mainLayout->addWidget(m_chartView, 1, 1); + mainLayout->setColumnStretch(1, 1); + setLayout(mainLayout); +} diff --git a/examples/modeldata/tablewidget.h b/examples/modeldata/tablewidget.h new file mode 100644 index 0000000..731620a --- /dev/null +++ b/examples/modeldata/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 + +class TableWidget : public QWidget +{ + Q_OBJECT + +public: + TableWidget(QWidget *parent = 0); +}; + +#endif // TABLEWIDGET_H