Akonadi Contacts

customfieldeditorwidget.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 "customfieldeditorwidget.h"
11#include <KLineEdit>
12#include <KLocalizedString>
13#include <QCheckBox>
14#include <QComboBox>
15#include <QLabel>
16#include <QPushButton>
17#include <QUuid>
18#include <QVBoxLayout>
19
20using namespace Akonadi;
21
22CustomFieldEditorWidget::CustomFieldEditorWidget(QWidget *parent)
23 : QWidget(parent)
24 , mFieldName(new KLineEdit(this))
25 , mAddField(new QPushButton(i18nc("@action:button", "Add Field"), this))
26 , mUseAllContacts(new QCheckBox(i18nc("@option:check", "Use field for all contacts")))
27 , mFieldType(new QComboBox(this))
28{
29 auto topLayout = new QVBoxLayout(this);
30 auto label = new QLabel(i18nc("@label:textbox", "Custom Field Title"), this);
31 label->setObjectName(QLatin1StringView("labeltitle"));
32
33 topLayout->addWidget(label);
34
35 auto fieldLayout = new QHBoxLayout;
36 topLayout->addLayout(fieldLayout);
37 mFieldName->setPlaceholderText(i18nc("@info:placeholder", "Add name"));
38 mFieldName->setObjectName(QLatin1StringView("fieldname"));
39 mFieldName->setTrapReturnKey(true);
40 fieldLayout->addWidget(mFieldName);
41
42 mFieldType->setObjectName(QLatin1StringView("fieldtype"));
43 fieldLayout->addWidget(mFieldType);
44 fillTypes();
45
46 mAddField->setObjectName(QLatin1StringView("addfield"));
47 fieldLayout->addWidget(mAddField);
48 mAddField->setEnabled(false); // Disable at the beginning
49 connect(mAddField, &QPushButton::clicked, this, &CustomFieldEditorWidget::slotAddField);
50 connect(mFieldName, &KLineEdit::textChanged, this, &CustomFieldEditorWidget::slotFielNameChanged);
51
52 mUseAllContacts->setObjectName(QLatin1StringView("useallcontact"));
53 topLayout->addWidget(mUseAllContacts);
54}
55
56CustomFieldEditorWidget::~CustomFieldEditorWidget() = default;
57
58void CustomFieldEditorWidget::fillTypes()
59{
60 mFieldType->addItem(i18n("Text"), CustomField::TextType);
61 mFieldType->addItem(i18n("Numeric"), CustomField::NumericType);
62 mFieldType->addItem(i18n("Boolean"), CustomField::BooleanType);
63 mFieldType->addItem(i18n("Date"), CustomField::DateType);
64 mFieldType->addItem(i18n("Time"), CustomField::TimeType);
65 mFieldType->addItem(i18n("DateTime"), CustomField::DateTimeType);
66 mFieldType->addItem(i18n("Url"), CustomField::UrlType);
67}
68
69void CustomFieldEditorWidget::setReadOnly(bool readOnly)
70{
71 mFieldName->setReadOnly(readOnly);
72 mAddField->setEnabled(!readOnly && !mFieldName->text().trimmed().isEmpty());
73 mFieldType->setEnabled(!readOnly);
74 mUseAllContacts->setEnabled(!readOnly);
75}
76
77void CustomFieldEditorWidget::slotFielNameChanged(const QString &text)
78{
79 mAddField->setEnabled(!text.trimmed().isEmpty());
80}
81
82void CustomFieldEditorWidget::slotAddField()
83{
84 if (mFieldName->text().trimmed().isEmpty()) {
85 return;
86 }
87 CustomField field;
88 // We use a Uuid as default key, so we won't have any duplicated keys,
89 // the user can still change it to something else in the editor dialog.
90 // Since the key only allows [A-Za-z0-9\-]*, we have to remove the curly
91 // braces as well.
93 key.remove(QLatin1Char('{'));
94 key.remove(QLatin1Char('}'));
95
96 field.setKey(key);
97 field.setTitle(mFieldName->text());
98 field.setType(static_cast<CustomField::Type>(mFieldType->itemData(mFieldType->currentIndex()).toInt()));
99
100 // TODO verify it
101 if (field.scope() != CustomField::ExternalScope) {
102 // do not change the scope for externally defined custom fields
103 field.setScope(mUseAllContacts->isChecked() ? CustomField::GlobalScope : CustomField::LocalScope);
104 }
105 Q_EMIT addNewField(field);
106 mFieldName->clear();
107 mFieldType->setCurrentIndex(0);
108}
109
110#include "moc_customfieldeditorwidget.cpp"
virtual void setReadOnly(bool)
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
A widget for editing the display name of a contact.
QString label(StandardShortcut id)
bool isChecked() const const
void clicked(bool checked)
void addLayout(QLayout *layout, int stretch)
void addItem(const QIcon &icon, const QString &text, const QVariant &userData)
QVariant itemData(int index, int role) const const
void clear()
void textChanged(const QString &text)
Q_EMITQ_EMIT
bool isEmpty() const const
QString & remove(QChar ch, Qt::CaseSensitivity cs)
QString trimmed() const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
QUuid createUuid()
QString toString(StringFormat mode) const const
int toInt(bool *ok) const const
void setEnabled(bool)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:05:46 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.