summaryrefslogtreecommitdiffstats
path: root/src/gui/addresstablemodel.cpp
blob: 620b168a301dae1c35dc93ce2e03fc2470f764a0 (plain)
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
	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, see <https://www.gnu.org/licenses/>.
*/

#include "addresstablemodel.h"
#include <QtDebug>

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));
}

void AddressTableModel::sort(int column, Qt::SortOrder order)
{
	qSort(m_data.begin(), m_data.end(), [=](const t_address_card& a1, const t_address_card& a2) -> bool {
		bool retval = false;

		switch (column)
		{
		case COL_ADDR_NAME:
            if (order == Qt::DescendingOrder)
                retval = QString::fromStdString(a1.get_display_name()) > QString::fromStdString(a2.get_display_name());
            else
                retval = QString::fromStdString(a1.get_display_name()) < QString::fromStdString(a2.get_display_name());
			break;
		case COL_ADDR_PHONE:
            if (order == Qt::DescendingOrder)
                retval = a1.sip_address.compare(a2.sip_address) > 0;
            else
                retval = a1.sip_address.compare(a2.sip_address) < 0;
			break;
		case COL_ADDR_REMARK:
            if (order == Qt::DescendingOrder)
                retval = QString::fromStdString(a1.remark) > QString::fromStdString(a2.remark);
            else
                retval = QString::fromStdString(a1.remark) < QString::fromStdString(a2.remark);
			break;
		}

		return retval;
	});

	emit dataChanged(createIndex(0, 0), createIndex(m_data.size()-1, 2));
}