Akonadi Contacts

addressmodel.cpp
1/*
2 This file is part of Contact Editor.
3
4 SPDX-FileCopyrightText: 2019 Volker Krause <vkrause@kde.org>
5
6 SPDX-License-Identifier: LGPL-2.0-or-later
7*/
8
9#include "addressmodel.h"
10
11using namespace Akonadi;
12
13AddressModel::AddressModel(QObject *parent)
14 : QAbstractListModel(parent)
15{
16}
17
18AddressModel::~AddressModel() = default;
19
20KContacts::Address::List AddressModel::addresses() const
21{
22 return mAddresses;
23}
24
25void AddressModel::setAddresses(const KContacts::Address::List &addresses)
26{
28 mAddresses = addresses;
30}
31
32void AddressModel::addAddress(const KContacts::Address &address)
33{
34 if (!address.isEmpty()) {
35 beginInsertRows({}, mAddresses.size(), mAddresses.size());
36 mAddresses.push_back(address);
38 }
39}
40
41void AddressModel::replaceAddress(const KContacts::Address &address, int row)
42{
43 if (row < 0 || row >= mAddresses.size()) {
44 return;
45 }
46
47 mAddresses[row] = address;
48 Q_EMIT dataChanged(index(row, 0), index(row, 0));
49}
50
51void AddressModel::removeAddress(int row)
52{
53 if (row < 0 || row >= mAddresses.size()) {
54 return;
55 }
56
57 beginRemoveRows({}, row, row);
58 mAddresses.remove(row);
60}
61
62int AddressModel::rowCount(const QModelIndex &parent) const
63{
64 if (parent.isValid()) {
65 return 0;
66 }
67 return mAddresses.size();
68}
69
70QVariant AddressModel::data(const QModelIndex &index, int role) const
71{
72 if (!index.isValid()) {
73 return {};
74 }
75
76 switch (role) {
77 case Qt::DisplayRole: {
78 const auto addr = mAddresses.at(index.row());
79 QString str = QLatin1StringView("<b>") + KContacts::Address::typeLabel(addr.type()) + QLatin1StringView("</b><br/>");
80 const QString label = addr.label();
81 if (!label.isEmpty()) {
83 } else {
84 str += addr.formatted(KContacts::AddressFormatStyle::Postal).trimmed().toHtmlEscaped().replace(QLatin1Char('\n'), QLatin1StringView("<br/>"));
85 }
86 return str;
87 }
88 case Qt::UserRole:
89 return QVariant::fromValue(mAddresses.at(index.row()));
90 }
91
92 return {};
93}
94
95#include "moc_addressmodel.cpp"
QString typeLabel() const
A widget for editing the display name of a contact.
PostalAddress address(const QVariant &location)
QString label(StandardShortcut id)
void beginInsertRows(const QModelIndex &parent, int first, int last)
void beginRemoveRows(const QModelIndex &parent, int first, int last)
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QList< int > &roles)
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
const_reference at(qsizetype i) const const
void push_back(parameter_type value)
void remove(qsizetype i, qsizetype n)
qsizetype size() const const
bool isValid() const const
int row() const const
Q_EMITQ_EMIT
QObject * parent() const const
bool isEmpty() const const
QString & replace(QChar before, QChar after, Qt::CaseSensitivity cs)
QString toHtmlEscaped() const const
QString trimmed() const const
DisplayRole
QVariant fromValue(T &&value)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:13:20 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.