neobytes/transactiontablemodel.cpp

84 lines
2.1 KiB
C++
Raw Normal View History

2011-05-12 14:49:42 +02:00
#include "transactiontablemodel.h"
2011-05-08 16:30:10 +02:00
2011-05-10 19:03:10 +02:00
#include <QLocale>
2011-05-08 22:23:31 +02:00
/* Credit and Debit columns are right-aligned as they contain numbers */
2011-05-10 19:03:10 +02:00
static int column_alignments[] = {
Qt::AlignLeft|Qt::AlignVCenter,
Qt::AlignLeft|Qt::AlignVCenter,
Qt::AlignLeft|Qt::AlignVCenter,
Qt::AlignRight|Qt::AlignVCenter,
Qt::AlignRight|Qt::AlignVCenter
2011-05-08 22:23:31 +02:00
};
2011-05-08 16:30:10 +02:00
TransactionTableModel::TransactionTableModel(QObject *parent):
QAbstractTableModel(parent)
{
2011-05-10 13:32:56 +02:00
columns << tr("Status") << tr("Date") << tr("Description") << tr("Debit") << tr("Credit");
2011-05-08 16:30:10 +02:00
}
int TransactionTableModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return 5;
}
int TransactionTableModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return columns.length();
}
QVariant TransactionTableModel::data(const QModelIndex &index, int role) const
{
if(!index.isValid())
return QVariant();
if(role == Qt::DisplayRole)
{
/* index.row(), index.column() */
/* Return QString */
2011-05-10 19:03:10 +02:00
return QLocale::system().toString(index.row());
2011-05-08 22:23:31 +02:00
} else if (role == Qt::TextAlignmentRole)
{
return column_alignments[index.column()];
2011-05-10 19:03:10 +02:00
} else if (role == Qt::UserRole)
{
/* user role: transaction type for filtering
"s" (sent)
"r" (received)
"g" (generated)
*/
switch(index.row() % 3)
{
case 0: return QString("s");
case 1: return QString("r");
case 2: return QString("o");
}
2011-05-08 16:30:10 +02:00
}
return QVariant();
}
QVariant TransactionTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
2011-05-08 22:23:31 +02:00
if(role == Qt::DisplayRole)
{
if(orientation == Qt::Horizontal)
{
return columns[section];
}
} else if (role == Qt::TextAlignmentRole)
2011-05-08 16:30:10 +02:00
{
2011-05-08 22:23:31 +02:00
return column_alignments[section];
2011-05-08 16:30:10 +02:00
}
return QVariant();
}
Qt::ItemFlags TransactionTableModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return Qt::ItemIsEnabled;
return QAbstractTableModel::flags(index);
}