Akonadi Contacts

selectaddresstypecombobox.cpp
1/*
2 This file is part of Contact Editor.
3
4 SPDX-FileCopyrightText: 2016 eyeOS S.L.U., a Telefonica company, sales@eyeos.com
5 SPDX-FileCopyrightText: 2016-2020 Laurent Montel <montel.org>
6
7 SPDX-License-Identifier: LGPL-2.0-or-later
8*/
9
10#include "selectaddresstypecombobox.h"
11#include "addresstypedialog.h"
12#include <QPointer>
13
14#include <KLocalizedString>
15
16using namespace Akonadi;
17
19 : QComboBox(parent)
20{
22 mTypeList.reserve(count + 2);
23 mTypeList.append(-2); // Add initial message
24 for (int i = 0; i < count; ++i) {
25 mTypeList.append(KContacts::Address::typeList().at(i));
26 }
27 mTypeList.append(-1); // Others...
28
29 update();
30
31 connect(this, &QComboBox::activated, this, &SelectAddressTypeComboBox::selected);
32}
33
35
37{
38 if (!mTypeList.contains(static_cast<int>(type))) {
39 // insert at the end, but before the 'Others...' entry
40 mTypeList.insert(mTypeList.count() - 1, static_cast<int>(type));
41 }
42
43 mType = type;
44 update();
45}
46
51
52void SelectAddressTypeComboBox::update()
53{
54 bool blocked = signalsBlocked();
55 blockSignals(true);
56
57 clear();
58 const int numberOfType(mTypeList.count());
59 for (int i = 0; i < numberOfType; ++i) {
60 if (mTypeList.at(i) == -1) { // "Other..." entry
61 addItem(i18nc("@item:inlistbox Category of contact info field", "Other..."));
62 } else if (mTypeList.at(i) == -2) {
63 addItem(i18nc("@item:inlistbox Category of contact info field", "New Address Type"));
64 } else {
66 }
67 }
68
69 setCurrentIndex(mLastSelected = mTypeList.indexOf(mType));
70
71 blockSignals(blocked);
72}
73
74void SelectAddressTypeComboBox::selected(int pos)
75{
76 if (mTypeList.at(pos) == -1) {
77 otherSelected();
78 } else if (mTypeList.at(pos) == -2) {
79 return;
80 } else {
81 mType = KContacts::Address::Type(mTypeList.at(pos));
82 mLastSelected = pos;
83 }
84}
85
86void SelectAddressTypeComboBox::otherSelected()
87{
89 if (dlg->exec()) {
90 mType = dlg->type();
91 if (!mTypeList.contains(mType)) {
92 mTypeList.insert(mTypeList.count() - 1, mType);
93 }
94 } else {
95 setType(KContacts::Address::Type(mTypeList.at(mLastSelected)));
96 }
97 delete dlg;
98 update();
99}
100
101#include "moc_selectaddresstypecombobox.cpp"
Dialog for creating a new address types.
~SelectAddressTypeComboBox() override
Destroys the address type combo.
void setType(KContacts::Address::Type type)
Sets the type that shall be selected in the combobox.
KContacts::Address::Type type() const
Returns the type that is currently selected.
SelectAddressTypeComboBox(QWidget *parent=nullptr)
Creates a new address type combo.
static TypeList typeList()
QString typeLabel() const
QFlags< TypeFlag > Type
QString i18nc(const char *context, const char *text, const TYPE &arg...)
A widget for editing the display name of a contact.
void activated(int index)
void addItem(const QIcon &icon, const QString &text, const QVariant &userData)
void clear()
void setCurrentIndex(int index)
void append(QList< T > &&value)
const_reference at(qsizetype i) const const
bool contains(const AT &value) const const
qsizetype count() const const
qsizetype indexOf(const AT &value, qsizetype from) const const
iterator insert(const_iterator before, parameter_type value)
void reserve(qsizetype size)
bool blockSignals(bool block)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool signalsBlocked() const const
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.