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, [email protected]
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 
16 using namespace ContactEditor;
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, qOverload<int>(&QComboBox::activated), this, &SelectAddressTypeComboBox::selected);
32 }
33 
35 {
36 }
37 
39 {
40  if (!mTypeList.contains(static_cast<int>(type))) {
41  // insert at the end, but before the 'Others...' entry
42  mTypeList.insert(mTypeList.count() - 1, static_cast<int>(type));
43  }
44 
45  mType = type;
46  update();
47 }
48 
50 {
51  return mType;
52 }
53 
54 void SelectAddressTypeComboBox::update()
55 {
56  bool blocked = signalsBlocked();
57  blockSignals(true);
58 
59  clear();
60  const int numberOfType(mTypeList.count());
61  for (int i = 0; i < numberOfType; ++i) {
62  if (mTypeList.at(i) == -1) { // "Other..." entry
63  addItem(i18nc("@item:inlistbox Category of contact info field", "Other..."));
64  } else if (mTypeList.at(i) == -2) {
65  addItem(i18nc("@item:inlistbox Category of contact info field", "New Address Type"));
66  } else {
68  }
69  }
70 
71  setCurrentIndex(mLastSelected = mTypeList.indexOf(mType));
72 
73  blockSignals(blocked);
74 }
75 
76 void SelectAddressTypeComboBox::selected(int pos)
77 {
78  if (mTypeList.at(pos) == -1) {
79  otherSelected();
80  } else if (mTypeList.at(pos) == -2) {
81  return;
82  } else {
83  mType = KContacts::Address::Type(mTypeList.at(pos));
84  mLastSelected = pos;
85  }
86 }
87 
88 void SelectAddressTypeComboBox::otherSelected()
89 {
90  QPointer<AddressTypeDialog> dlg = new AddressTypeDialog(mType, this);
91  if (dlg->exec()) {
92  mType = dlg->type();
93  if (!mTypeList.contains(mType)) {
94  mTypeList.insert(mTypeList.count() - 1, mType);
95  }
96  } else {
97  setType(KContacts::Address::Type(mTypeList.at(mLastSelected)));
98  }
99  delete dlg;
100  update();
101 }
void setType(KContacts::Address::Type type)
Sets the type that shall be selected in the combobox.
void reserve(int alloc)
const T & at(int i) const const
void clear()
int indexOf(const T &value, int from) const const
void addItem(const QString &text, const QVariant &userData)
~SelectAddressTypeComboBox()
Destroys the address type combo.
int count(const T &value) const const
int count() const const
void append(const T &value)
QString i18nc(const char *context, const char *text, const TYPE &arg...)
void activated(int index)
QPoint pos() const const
bool signalsBlocked() const const
bool blockSignals(bool block)
bool contains(const T &value) const const
static TypeList typeList()
QString typeLabel() const
KContacts::Address::Type type() const
Returns the type that is currently selected.
SelectAddressTypeComboBox(QWidget *parent=nullptr)
Creates a new address type combo.
void insert(int i, const T &value)
void setCurrentIndex(int index)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
Dialog for creating a new address types.
QFlags< TypeFlag > Type
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Wed Jun 23 2021 23:09:25 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.