forked from openwall/johnny
-
Notifications
You must be signed in to change notification settings - Fork 5
/
tablemodel.cpp
82 lines (67 loc) · 1.89 KB
/
tablemodel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
* Copyright (c) 2011 Shinnok <raydenxy at gmail.com>. See LICENSE.
*/
#include <QtGui>
#include "tablemodel.h"
TableModel::TableModel(QObject *parent)
: QAbstractTableModel(parent)
{
for (int i = 0; i < TABLE_ROWS; i++) {
for (int j = 0; j < TABLE_COLUMNS; j++)
m_table.append("");
}
}
TableModel::~TableModel()
{
m_table.empty();
}
int TableModel::rowCount(const QModelIndex & /* parent */) const
{
return TABLE_ROWS;
}
int TableModel::columnCount(const QModelIndex & /* parent */) const
{
return TABLE_COLUMNS;
}
QVariant TableModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid() || role != Qt::DisplayRole)
return QVariant();
return m_table.at(index.row() * TABLE_COLUMNS + index.column());
}
bool TableModel::setData(const QModelIndex &index,
const QVariant &value,
int role)
{
if (!index.isValid() || role != Qt::EditRole)
return false;
m_table.replace(index.row() * TABLE_COLUMNS + index.column(), value.toString());
emit TableModel::dataChanged(index, index);
return true;
}
QVariant TableModel::headerData(int section/* section */,
Qt::Orientation orientation/* orientation */,
int role) const
{
//if (role == Qt::SizeHintRole)
// return QSize(1, 1);
if (role != Qt::DisplayRole)
return QVariant();
if (orientation == Qt::Vertical)
// TODO: Row numbers starting from 0 seems to not be user
// friendly.
return QString("%1").arg(section);
if (orientation == Qt::Horizontal) {
switch (section) {
case 0:
return QString(tr("User"));
break;
case 1:
return QString(tr("Hash"));
break;
default:
break;
}
}
return QVariant();
}