customtablemodel.cpp
136 lines
| 3.8 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
|
r1387 | #include <QDebug> | ||
Tero Ahola
|
r1130 | |||
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
|
r1387 | QVariant CustomTableModel::headerData(int section, Qt::Orientation orientation, int role) const | ||
Tero Ahola
|
r1130 | { | ||
if (role != Qt::DisplayRole) | ||||
return QVariant(); | ||||
Tero Ahola
|
r1395 | if (orientation == Qt::Vertical) { | ||
if (m_verticalHeaders.count() > section) | ||||
return m_verticalHeaders[section]; | ||||
Tero Ahola
|
r1130 | else | ||
Tero Ahola
|
r1387 | return QAbstractTableModel::headerData(section, orientation, role); | ||
Tero Ahola
|
r1162 | } else { | ||
Tero Ahola
|
r1387 | return QAbstractTableModel::headerData(section, orientation, role); | ||
Tero Ahola
|
r1162 | } | ||
Tero Ahola
|
r1130 | } | ||
Tero Ahola
|
r1387 | bool CustomTableModel::setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role) | ||
{ | ||||
Tero Ahola
|
r1395 | if (orientation == Qt::Vertical) { | ||
while (m_verticalHeaders.count() <= section) | ||||
m_verticalHeaders.append(QVariant()); | ||||
m_verticalHeaders.replace(section, value); | ||||
Tero Ahola
|
r1387 | } else { | ||
return QAbstractTableModel::setHeaderData(section, orientation, value, role); | ||||
} | ||||
emit headerDataChanged(orientation, section, section); | ||||
return true; | ||||
} | ||||
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
|
r1329 | QVariant CustomTableModel::at(int row, int column) | ||
{ | ||||
return data(index(row, column)); | ||||
} | ||||
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" | ||