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, &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 
48 {
49  return mType;
50 }
51 
52 void 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 
74 void 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 
86 void SelectAddressTypeComboBox::otherSelected()
87 {
88  QPointer<AddressTypeDialog> dlg = new AddressTypeDialog(mType, this);
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 }
void append(const T &value)
QString typeLabel() const
SelectAddressTypeComboBox(QWidget *parent=nullptr)
Creates a new address type combo.
~SelectAddressTypeComboBox() override
Destroys the address type combo.
int count(const T &value) const const
void setCurrentIndex(int index)
bool contains(const T &value) const const
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void reserve(int alloc)
bool signalsBlocked() const const
KContacts::Address::Type type() const
Returns the type that is currently selected.
bool blockSignals(bool block)
int indexOf(const T &value, int from) const const
const T & at(int i) const const
void insert(int i, const T &value)
Dialog for creating a new address types.
void clear()
static TypeList typeList()
QString i18nc(const char *context, const char *text, const TYPE &arg...)
void addItem(const QString &text, const QVariant &userData)
void setType(KContacts::Address::Type type)
Sets the type that shall be selected in the combobox.
void activated(int index)
QFlags< TypeFlag > Type
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Sat Apr 1 2023 04:09:05 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.