customtablemodel.cpp
117 lines
| 3.1 KiB
| text/x-c
|
CppLexer
Tero Ahola
|
r1130 | /**************************************************************************** | ||
** | ||||
** 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$ | ||||
** | ||||
****************************************************************************/ | ||||
Tero Ahola
|
r1272 | #include "customtablemodel.h" | ||
Tero Ahola
|
r1130 | #include <QVector> | ||
#include <QRect> | ||||
#include <QColor> | ||||
Tero Ahola
|
r1272 | CustomTableModel::CustomTableModel(QObject *parent) : | ||
Tero Ahola
|
r1265 | QAbstractTableModel(parent), | ||
m_columnCount(0), | ||||
m_rowCount(0) | ||||
Tero Ahola
|
r1130 | { | ||
} | ||||
Tero Ahola
|
r1272 | int CustomTableModel::rowCount(const QModelIndex & parent) const | ||
Tero Ahola
|
r1130 | { | ||
Q_UNUSED(parent) | ||||
return m_data.count(); | ||||
} | ||||
Tero Ahola
|
r1272 | int CustomTableModel::columnCount(const QModelIndex & parent) const | ||
Tero Ahola
|
r1130 | { | ||
Q_UNUSED(parent) | ||||
return m_columnCount; | ||||
} | ||||
Tero Ahola
|
r1272 | QVariant CustomTableModel::headerData (int section, Qt::Orientation orientation, int role ) const | ||
Tero Ahola
|
r1130 | { | ||
if (role != Qt::DisplayRole) | ||||
return QVariant(); | ||||
Tero Ahola
|
r1162 | if (orientation == Qt::Horizontal) { | ||
if (section % 2 == 0) | ||||
Tero Ahola
|
r1130 | return "x"; | ||
else | ||||
return "y"; | ||||
Tero Ahola
|
r1162 | } else { | ||
Tero Ahola
|
r1130 | return QString("%1").arg(section + 1); | ||
Tero Ahola
|
r1162 | } | ||
Tero Ahola
|
r1130 | } | ||
Tero Ahola
|
r1272 | QVariant CustomTableModel::data(const QModelIndex &index, int role) const | ||
Tero Ahola
|
r1130 | { | ||
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()); | ||||
} | ||||
return QVariant(); | ||||
} | ||||
Tero Ahola
|
r1272 | bool CustomTableModel::setData(const QModelIndex &index, const QVariant &value, int role) | ||
Tero Ahola
|
r1130 | { | ||
if (index.isValid() && role == Qt::EditRole) { | ||||
m_data[index.row()]->replace(index.column(), value); | ||||
emit dataChanged(index, index); | ||||
return true; | ||||
} | ||||
return false; | ||||
} | ||||
Tero Ahola
|
r1272 | void CustomTableModel::insertColumn(int column, const QModelIndex &parent) | ||
Tero Ahola
|
r1130 | { | ||
Tero Ahola
|
r1169 | beginInsertColumns(parent, column, column); | ||
m_columnCount++; | ||||
endInsertColumns(); | ||||
} | ||||
Tero Ahola
|
r1130 | |||
Tero Ahola
|
r1272 | void CustomTableModel::insertRow(int row, const QModelIndex &parent) | ||
Tero Ahola
|
r1169 | { | ||
beginInsertRows(parent, row, row); | ||||
Tero Ahola
|
r1130 | QVector<QVariant>* dataVec = new QVector<QVariant>(m_columnCount); | ||
m_data.insert(row, dataVec); | ||||
endInsertRows(); | ||||
} | ||||
Tero Ahola
|
r1272 | bool CustomTableModel::removeRow(int row, const QModelIndex &parent) | ||
Tero Ahola
|
r1130 | { | ||
return QAbstractTableModel::removeRow(row, parent); | ||||
} | ||||
Tero Ahola
|
r1272 | bool CustomTableModel::removeRows(int row, int count, const QModelIndex &parent) | ||
Tero Ahola
|
r1130 | { | ||
beginRemoveRows(parent, row, row + count - 1); | ||||
bool removed(false); | ||||
for (int i(row); i < (row + count); i++) { | ||||
m_data.removeAt(i); | ||||
removed = true; | ||||
} | ||||
endRemoveRows(); | ||||
return removed; | ||||
} | ||||
Tero Ahola
|
r1272 | Qt::ItemFlags CustomTableModel::flags ( const QModelIndex & index ) const | ||
Tero Ahola
|
r1130 | { | ||
return QAbstractItemModel::flags(index) | Qt::ItemIsEditable; | ||||
} | ||||
Tero Ahola
|
r1272 | #include "moc_customtablemodel.cpp" | ||