customtablemodel.cpp
276 lines
| 8.2 KiB
| text/x-c
|
CppLexer
|
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$ | ||||
** | ||||
****************************************************************************/ | ||||
|
r519 | #include "customtablemodel.h" | ||
|
r630 | #include <QVector> | ||
|
r734 | #include <QTime> | ||
|
r849 | #include <QRect> | ||
#include <QColor> | ||||
|
r519 | |||
CustomTableModel::CustomTableModel(QObject *parent) : | ||||
QAbstractTableModel(parent) | ||||
{ | ||||
|
r734 | // m_points.append(QPointF(10, 50)); | ||
// m_labels.append("Apples"); | ||||
// m_points.append(QPointF(60, 70)); | ||||
// m_labels.append("Oranges"); | ||||
// m_points.append(QPointF(110, 50)); | ||||
// m_labels.append("Bananas"); | ||||
// m_points.append(QPointF(140, 40)); | ||||
// m_labels.append("Lemons"); | ||||
// m_points.append(QPointF(200, 150)); | ||||
// m_labels.append("Plums"); | ||||
// m_points.append(QPointF(225, 75)); | ||||
|
r630 | // m_labels.append("Pearls"); | ||
|
r734 | qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); | ||
|
r630 | // m_data | ||
|
r1056 | for (int i = 0; i < 8; i++) | ||
|
r630 | { | ||
QVector<qreal>* dataVec = new QVector<qreal>(6); | ||||
|
r849 | // QVector<QColor>* colorVec = new QVector<QColor>(6); | ||
|
r630 | for (int k = 0; k < dataVec->size(); k++) | ||
|
r735 | { | ||
|
r630 | if (k%2 == 0) | ||
dataVec->replace(k, i * 50 + qrand()%20); | ||||
else | ||||
dataVec->replace(k, qrand()%100); | ||||
|
r849 | // colorVec->replace(k, QColor(Qt::white)); | ||
|
r735 | } | ||
|
r630 | m_data.append(dataVec); | ||
m_labels.append(QString("Row: %1").arg((i + 1))); | ||||
|
r849 | // m_rowsColors.append(colorVec); | ||
|
r630 | } | ||
|
r519 | } | ||
int CustomTableModel::rowCount(const QModelIndex & parent) const | ||||
{ | ||||
|
r613 | Q_UNUSED(parent) | ||
|
r630 | // return m_points.count(); | ||
return m_data.count(); | ||||
|
r519 | } | ||
int CustomTableModel::columnCount(const QModelIndex & parent) const | ||||
{ | ||||
|
r613 | Q_UNUSED(parent) | ||
|
r630 | // return 3; | ||
return 6; | ||||
|
r519 | } | ||
QVariant CustomTableModel::headerData (int section, Qt::Orientation orientation, int role ) const | ||||
{ | ||||
if (role != Qt::DisplayRole) | ||||
return QVariant(); | ||||
if (orientation == Qt::Horizontal) | ||||
{ | ||||
switch(section) | ||||
{ | ||||
|
r630 | // case 0: | ||
// return "x"; | ||||
// case 1: | ||||
// return "y"; | ||||
// case 2: | ||||
case 6: | ||||
|
r519 | return "Fruit"; | ||
|
r527 | default: | ||
|
r630 | if (section%2 == 0) | ||
return "x"; | ||||
else | ||||
return "y"; | ||||
// return "What?"; | ||||
|
r519 | } | ||
} | ||||
else | ||||
|
r833 | return QString("%1").arg(section /*+ 1*/); | ||
|
r519 | } | ||
QVariant CustomTableModel::data(const QModelIndex & index, int role) const | ||||
{ | ||||
if (role == Qt::DisplayRole) | ||||
{ | ||||
switch(index.column()) | ||||
{ | ||||
|
r630 | // case 0: | ||
// return m_points[index.row()].x(); | ||||
// case 1: | ||||
// return m_points[index.row()].y(); | ||||
// case 2: | ||||
case 6: | ||||
|
r519 | return m_labels[index.row()]; | ||
default: | ||||
|
r630 | return m_data[index.row()]->at(index.column()); | ||
|
r613 | break; | ||
|
r519 | } | ||
} | ||||
else if (role == Qt::EditRole) | ||||
{ | ||||
switch(index.column()) | ||||
{ | ||||
|
r630 | // case 0: | ||
// return m_points[index.row()].x(); | ||||
// case 1: | ||||
// return m_points[index.row()].y(); | ||||
// case 2: | ||||
case 6: | ||||
|
r519 | return m_labels[index.row()]; | ||
default: | ||||
|
r630 | return m_data[index.row()]->at(index.column()); | ||
|
r613 | break; | ||
|
r519 | } | ||
} | ||||
|
r735 | else if (role == Qt::BackgroundRole) | ||
{ | ||||
|
r849 | 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); | ||||
|
r735 | } | ||
|
r613 | return QVariant(); | ||
|
r519 | } | ||
bool CustomTableModel::setData ( const QModelIndex & index, const QVariant & value, int role) | ||||
{ | ||||
if (index.isValid() && role == Qt::EditRole) | ||||
{ | ||||
switch(index.column()) | ||||
{ | ||||
|
r630 | // case 0: | ||
// m_points[index.row()].setX(value.toDouble()); | ||||
// break; | ||||
// case 1: | ||||
// m_points[index.row()].setY(value.toDouble()); | ||||
// break; | ||||
// case 2: | ||||
case 6: | ||||
|
r519 | m_labels.replace(index.row(), value.toString()); | ||
break; | ||||
default: | ||||
|
r630 | m_data[index.row()]->replace(index.column(), value.toDouble()); | ||
break; | ||||
// return false; | ||||
|
r519 | } | ||
emit dataChanged(index, index); | ||||
return true; | ||||
} | ||||
|
r849 | // else if (role == Qt::BackgroundRole) | ||
// { | ||||
// m_rowsColors[index.row()]->replace(index.column(), value.value<QColor>()); | ||||
// return true; | ||||
// } | ||||
|
r519 | return false; | ||
} | ||||
Qt::ItemFlags CustomTableModel::flags ( const QModelIndex & index ) const | ||||
{ | ||||
|
r630 | // if (!index.isValid()) | ||
// return Qt::ItemIsEnabled; | ||||
return QAbstractItemModel::flags(index) | Qt::ItemIsEditable; | ||||
|
r519 | } | ||
bool CustomTableModel::insertRows ( int row, int count, const QModelIndex & parent) | ||||
{ | ||||
|
r613 | Q_UNUSED(parent) | ||
|
r546 | if (row < 0) | ||
row = 0; | ||||
|
r519 | beginInsertRows(QModelIndex(), row /*dataTable.count()*/, row + count - 1); | ||
|
r545 | for (int i = row; i < row + count; i++) | ||
|
r519 | { | ||
|
r630 | // m_points.insert(row, QPointF(10,20)); | ||
|
r735 | QVector<qreal>* dataVec = new QVector<qreal>(6); | ||
QVector<QColor>* colorVec = new QVector<QColor>(6); | ||||
|
r630 | for (int k = 0; k < dataVec->size(); k++) | ||
|
r735 | { | ||
|
r630 | if (k%2 == 0) | ||
// dataVec->replace(k, i * 50 + qrand()%20); | ||||
{ | ||||
int difference = 0; | ||||
|
r735 | if (i < m_data.size()) | ||
|
r630 | { | ||
|
r735 | if (i - 1 >= 0) | ||
|
r630 | { | ||
|
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))); | ||||
|
r735 | dataVec->replace(k, m_data[i - 1]->at(k) + qrand()%qMax(1, difference)); | ||
|
r630 | } | ||
else | ||||
|
r734 | dataVec->replace(k, qrand()%40 + 10); | ||
|
r630 | } | ||
else | ||||
|
r735 | { | ||
if (i - 1 >= 0) | ||||
|
r734 | { | ||
|
r735 | dataVec->replace(k, m_data[i - 1]->at(k) + qrand()%40 + 10); | ||
|
r734 | } | ||
else | ||||
{ | ||||
dataVec->replace(k, qrand()%40 + 10); | ||||
} | ||||
|
r735 | } | ||
|
r630 | } | ||
else | ||||
dataVec->replace(k, qrand()%100); | ||||
|
r735 | colorVec->replace(k, QColor(Qt::white)); | ||
} | ||||
m_data.insert(i, dataVec); | ||||
m_labels.insert(i,(QString("Row: %1").arg(i + 1))); | ||||
|
r849 | // m_rowsColors.insert(i, colorVec); | ||
|
r519 | } | ||
endInsertRows(); | ||||
return true; | ||||
} | ||||
|
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++) | ||||
{ | ||||
|
r734 | // m_points.removeAt(row); | ||
|
r630 | QVector<qreal>* item = m_data.at(row); | ||
m_data.removeAt(row); | ||||
delete item; | ||||
|
r545 | m_labels.removeAt(row); | ||
} | ||||
endRemoveRows(); | ||||
return true; | ||||
} | ||||
|
r849 | |||
void CustomTableModel::addMapping(QString color, QRect area) | ||||
{ | ||||
|
r861 | m_mapping.insertMulti(color, area); | ||
|
r849 | } | ||
void CustomTableModel::addMapping(QString color, int left, int top, int right, int bottom) | ||||
{ | ||||
addMapping(color, QRect(QPoint(left, top), QPoint(right, bottom))); | ||||
} | ||||