##// END OF EJS Templates
pieseries: bool return value for append/insert/remove...
pieseries: bool return value for append/insert/remove Needed for reporting operation errors when dealing with null pointers and double add/remove.

File last commit:

r1097:431f68143802
r1104:9f3130f2bd01
Show More
customtablemodel.cpp
276 lines | 7.7 KiB | text/x-c | CppLexer
Jani Honkonen
Add/modify license headers
r830 /****************************************************************************
**
** 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$
**
****************************************************************************/
Marek Rosa
Table model data example
r519 #include "customtablemodel.h"
Marek Rosa
Added support for data from model to QBarSeries. Various fixes and small modifications to data from model support to other series
r630 #include <QVector>
Marek Rosa
XYSeries: added support for limited mapping area
r734 #include <QTime>
Marek Rosa
tablemodelchart: added coloring to TableView
r849 #include <QRect>
#include <QColor>
Marek Rosa
Table model data example
r519
CustomTableModel::CustomTableModel(QObject *parent) :
QAbstractTableModel(parent)
{
Marek Rosa
XYSeries: added support for limited mapping area
r734 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
Marek Rosa
Model example for the RnD offsite
r1097 m_columnCount = 3;
Marek Rosa
Improvements to QSpline model support
r1090 m_rowCount = 9;
Marek Rosa
PieSeries model support - further refactoring
r1081
Marek Rosa
Model example for the RnD offsite
r1097 m_labels.append("Apples");
m_labels.append("Oranges");
m_labels.append("Pears");
m_labels.append("Peaches");
m_labels.append("Coconuts");
m_labels.append("Bananas");
m_labels.append("Kiwis");
m_labels.append("Grapes");
m_labels.append("Plums");
Marek Rosa
Added support for data from model to QBarSeries. Various fixes and small modifications to data from model support to other series
r630 // m_data
Marek Rosa
PieSeries model support - further refactoring
r1081 for (int i = 0; i < m_rowCount; i++)
Marek Rosa
Added support for data from model to QBarSeries. Various fixes and small modifications to data from model support to other series
r630 {
Marek Rosa
PieSeries model support - further refactoring
r1081 QVector<qreal>* dataVec = new QVector<qreal>(m_columnCount);
Marek Rosa
Added support for data from model to QBarSeries. Various fixes and small modifications to data from model support to other series
r630 for (int k = 0; k < dataVec->size(); k++)
Marek Rosa
Some more work on mapping with limits
r735 {
Marek Rosa
Added support for data from model to QBarSeries. Various fixes and small modifications to data from model support to other series
r630 if (k%2 == 0)
dataVec->replace(k, i * 50 + qrand()%20);
else
dataVec->replace(k, qrand()%100);
Marek Rosa
Some more work on mapping with limits
r735 }
Marek Rosa
Added support for data from model to QBarSeries. Various fixes and small modifications to data from model support to other series
r630 m_data.append(dataVec);
Marek Rosa
Model example for the RnD offsite
r1097 // m_labels.append(QString("Row: %1").arg((i + 1)));
Marek Rosa
Added support for data from model to QBarSeries. Various fixes and small modifications to data from model support to other series
r630 }
Marek Rosa
Table model data example
r519 }
int CustomTableModel::rowCount(const QModelIndex & parent) const
{
Tero Ahola
A few more warnings removed from examples
r613 Q_UNUSED(parent)
Marek Rosa
Added support for data from model to QBarSeries. Various fixes and small modifications to data from model support to other series
r630 return m_data.count();
Marek Rosa
Table model data example
r519 }
int CustomTableModel::columnCount(const QModelIndex & parent) const
{
Tero Ahola
A few more warnings removed from examples
r613 Q_UNUSED(parent)
Marek Rosa
PieSeries model support - further refactoring
r1081 return m_columnCount;
Marek Rosa
Table model data example
r519 }
QVariant CustomTableModel::headerData (int section, Qt::Orientation orientation, int role ) const
{
if (role != Qt::DisplayRole)
return QVariant();
if (orientation == Qt::Horizontal)
{
switch(section)
{
Marek Rosa
Model example for the RnD offsite
r1097 case 0:
return "Fruit";
case 1:
return "Count";
case 2:
return "Ordered";
Marek Rosa
QXYSeries: model data orientation added. QBarSeries: some model data function placeholders
r527 default:
Marek Rosa
Added support for data from model to QBarSeries. Various fixes and small modifications to data from model support to other series
r630 if (section%2 == 0)
return "x";
else
return "y";
Marek Rosa
Table model data example
r519 }
}
else
Marek Rosa
XYSeries model with limits working.
r833 return QString("%1").arg(section /*+ 1*/);
Marek Rosa
Table model data example
r519 }
QVariant CustomTableModel::data(const QModelIndex & index, int role) const
{
if (role == Qt::DisplayRole)
{
switch(index.column())
{
Marek Rosa
Model example for the RnD offsite
r1097 case 0:
return m_labels[index.row()];
Marek Rosa
Table model data example
r519 default:
Marek Rosa
Added support for data from model to QBarSeries. Various fixes and small modifications to data from model support to other series
r630 return m_data[index.row()]->at(index.column());
Tero Ahola
A few more warnings removed from examples
r613 break;
Marek Rosa
Table model data example
r519 }
}
else if (role == Qt::EditRole)
{
switch(index.column())
{
Marek Rosa
Model example for the RnD offsite
r1097 case 0:
return m_labels[index.row()];
Marek Rosa
Table model data example
r519 default:
Marek Rosa
Added support for data from model to QBarSeries. Various fixes and small modifications to data from model support to other series
r630 return m_data[index.row()]->at(index.column());
Tero Ahola
A few more warnings removed from examples
r613 break;
Marek Rosa
Table model data example
r519 }
}
Marek Rosa
Some more work on mapping with limits
r735 else if (role == Qt::BackgroundRole)
Marek Rosa
PieSeries model support - further refactoring
r1081 {
QRect rect;
foreach(rect, m_mapping)
if(rect.contains(index.column(), index.row()))
return QColor(m_mapping.key(rect));
Marek Rosa
tablemodelchart: added coloring to TableView
r849
Marek Rosa
PieSeries model support - further refactoring
r1081 // cell not mapped return white color
return QColor(Qt::white);
}
Tero Ahola
A few more warnings removed from examples
r613 return QVariant();
Marek Rosa
Table model data example
r519 }
bool CustomTableModel::setData ( const QModelIndex & index, const QVariant & value, int role)
{
if (index.isValid() && role == Qt::EditRole)
{
switch(index.column())
{
Marek Rosa
Model example for the RnD offsite
r1097 case 0:
m_labels.replace(index.row(), value.toString());
break;
Marek Rosa
Table model data example
r519 default:
Marek Rosa
Added support for data from model to QBarSeries. Various fixes and small modifications to data from model support to other series
r630 m_data[index.row()]->replace(index.column(), value.toDouble());
break;
Marek Rosa
Table model data example
r519 }
emit dataChanged(index, index);
return true;
}
return false;
}
Qt::ItemFlags CustomTableModel::flags ( const QModelIndex & index ) const
{
Marek Rosa
Added support for data from model to QBarSeries. Various fixes and small modifications to data from model support to other series
r630 return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
Marek Rosa
Table model data example
r519 }
bool CustomTableModel::insertRows ( int row, int count, const QModelIndex & parent)
{
Tero Ahola
A few more warnings removed from examples
r613 Q_UNUSED(parent)
Marek Rosa
Model data example updated
r546 if (row < 0)
row = 0;
Marek Rosa
Table model data example
r519 beginInsertRows(QModelIndex(), row /*dataTable.count()*/, row + count - 1);
Marek Rosa
Added support for adding and removing data with model. Updated the example
r545 for (int i = row; i < row + count; i++)
Marek Rosa
Table model data example
r519 {
Marek Rosa
Added support for data from model to QBarSeries. Various fixes and small modifications to data from model support to other series
r630 // m_points.insert(row, QPointF(10,20));
Marek Rosa
PieSeries model support - further refactoring
r1081 QVector<qreal>* dataVec = new QVector<qreal>(m_columnCount);
QVector<QColor>* colorVec = new QVector<QColor>(m_columnCount);
Marek Rosa
Added support for data from model to QBarSeries. Various fixes and small modifications to data from model support to other series
r630 for (int k = 0; k < dataVec->size(); k++)
Marek Rosa
Some more work on mapping with limits
r735 {
Marek Rosa
Added support for data from model to QBarSeries. Various fixes and small modifications to data from model support to other series
r630 if (k%2 == 0)
// dataVec->replace(k, i * 50 + qrand()%20);
{
int difference = 0;
Marek Rosa
Some more work on mapping with limits
r735 if (i < m_data.size())
Marek Rosa
Added support for data from model to QBarSeries. Various fixes and small modifications to data from model support to other series
r630 {
Marek Rosa
Some more work on mapping with limits
r735 if (i - 1 >= 0)
Marek Rosa
Added support for data from model to QBarSeries. Various fixes and small modifications to data from model support to other series
r630 {
Marek Rosa
XYSeries model with limits working.
r833 if (row > 0)
difference = (int)((qAbs(m_data[i]->at(k) - m_data[row - 1]->at(k)))/count);
else
difference = (int)((qAbs(m_data[i]->at(k)/count)));
Marek Rosa
Some more work on mapping with limits
r735 dataVec->replace(k, m_data[i - 1]->at(k) + qrand()%qMax(1, difference));
Marek Rosa
Added support for data from model to QBarSeries. Various fixes and small modifications to data from model support to other series
r630 }
else
Marek Rosa
XYSeries: added support for limited mapping area
r734 dataVec->replace(k, qrand()%40 + 10);
Marek Rosa
Added support for data from model to QBarSeries. Various fixes and small modifications to data from model support to other series
r630 }
else
Marek Rosa
Some more work on mapping with limits
r735 {
if (i - 1 >= 0)
Marek Rosa
XYSeries: added support for limited mapping area
r734 {
Marek Rosa
Some more work on mapping with limits
r735 dataVec->replace(k, m_data[i - 1]->at(k) + qrand()%40 + 10);
Marek Rosa
XYSeries: added support for limited mapping area
r734 }
else
{
dataVec->replace(k, qrand()%40 + 10);
}
Marek Rosa
Some more work on mapping with limits
r735 }
Marek Rosa
Added support for data from model to QBarSeries. Various fixes and small modifications to data from model support to other series
r630 }
else
dataVec->replace(k, qrand()%100);
Marek Rosa
Some more work on mapping with limits
r735 colorVec->replace(k, QColor(Qt::white));
}
m_data.insert(i, dataVec);
m_labels.insert(i,(QString("Row: %1").arg(i + 1)));
Marek Rosa
Table model data example
r519 }
endInsertRows();
return true;
}
Marek Rosa
Added support for adding and removing data with model. Updated the example
r545
bool CustomTableModel::removeRows ( int row, int count, const QModelIndex & parent)
{
if (row > this->rowCount() - 1)
return false;
if (row < 0)
row = 0;
if (row + count > rowCount())
return false;
beginRemoveRows(parent, row, row + count - 1);
for (int i = row; i < row + count; i++)
{
Marek Rosa
Added support for data from model to QBarSeries. Various fixes and small modifications to data from model support to other series
r630 QVector<qreal>* item = m_data.at(row);
m_data.removeAt(row);
delete item;
Marek Rosa
Added support for adding and removing data with model. Updated the example
r545 m_labels.removeAt(row);
}
endRemoveRows();
return true;
}
Marek Rosa
tablemodelchart: added coloring to TableView
r849
Marek Rosa
PieSeries model support - further refactoring
r1081 bool CustomTableModel::insertColumns ( int column, int count, const QModelIndex & parent)
{
if (column < 0)
column = 0;
beginInsertColumns(parent, column, column + count - 1);
m_columnCount += count;
for (int i = column; i < column + count; i++)
for (int k = 0; k < rowCount(); k++)
if (k - 1 >= 0) {
m_data[k]->insert(i, m_data[k - 1]->at(i) + qrand()%40 + 10);
} else {
m_data[k]->insert(i, qrand()%40);
}
endInsertColumns();
return true;
}
bool CustomTableModel::removeColumns ( int column, int count, const QModelIndex & parent)
{
if (column > columnCount() - 1)
return false;
if (column < 0)
column = 0;
if (column + count > columnCount())
return false;
beginRemoveColumns(parent, column, column + count -1);
m_columnCount -= count;
for (int i = column; i < column + count; i++)
for (int k = 0; k < rowCount(); k++)
m_data[k]->remove(column);
endRemoveColumns();
return true;
}
Marek Rosa
tablemodelchart: added coloring to TableView
r849 void CustomTableModel::addMapping(QString color, QRect area)
{
Marek Rosa
Small updates to tablemodelchart example
r861 m_mapping.insertMulti(color, area);
Marek Rosa
tablemodelchart: added coloring to TableView
r849 }
void CustomTableModel::addMapping(QString color, int left, int top, int right, int bottom)
{
addMapping(color, QRect(QPoint(left, top), QPoint(right, bottom)));
}