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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
/*
Copyright (C) 2015 Lubos Dolezel <lubos@dolezel.info>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "addresstablemodel.h"
#include <QtDebug>
// Columns
#define COL_ADDR_NAME 0
#define COL_ADDR_PHONE 1
#define COL_ADDR_REMARK 2
AddressTableModel::AddressTableModel(QObject *parent, const list<t_address_card>& data)
: QAbstractTableModel(parent)
{
m_data = QList<t_address_card>::fromStdList(data);
}
int AddressTableModel::rowCount(const QModelIndex &parent) const
{
return m_data.size();
}
int AddressTableModel::columnCount(const QModelIndex &parent) const
{
return 3;
}
QVariant AddressTableModel::data(const QModelIndex &index, int role) const
{
if (role != Qt::DisplayRole)
return QVariant();
const int row = index.row();
switch (index.column())
{
case COL_ADDR_NAME:
return QString::fromStdString(m_data[row].get_display_name());
case COL_ADDR_PHONE:
return QString::fromStdString(m_data[row].sip_address);
case COL_ADDR_REMARK:
return QString::fromStdString(m_data[row].remark);
default:
return QVariant();
}
}
QVariant AddressTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role != Qt::DisplayRole)
return QVariant();
switch (section)
{
case COL_ADDR_NAME:
return tr("Name");
case COL_ADDR_PHONE:
return tr("Phone");
case COL_ADDR_REMARK:
return tr("Remark");
default:
return QVariant();
}
}
void AddressTableModel::appendAddress(const t_address_card& card)
{
qDebug() << "Appending contact" << QString::fromStdString(card.get_display_name());
beginInsertRows(QModelIndex(), m_data.size(), m_data.size());
m_data << card;
endInsertRows();
}
void AddressTableModel::removeAddress(int index)
{
beginRemoveRows(QModelIndex(), index, index);
m_data.removeAt(index);
endRemoveRows();
}
void AddressTableModel::modifyAddress(int index, const t_address_card& card)
{
m_data[index] = card;
emit dataChanged(createIndex(index, 0), createIndex(index, 2));
}
|