File indexing completed on 2024-11-24 04:39:32

0001 /*
0002     This file is part of Contact Editor.
0003 
0004     SPDX-FileCopyrightText: 2010 Tobias Koenig <tokoe@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #include "nameeditdialog.h"
0010 
0011 #include <KComboBox>
0012 #include <KLineEdit>
0013 #include <KLocalizedString>
0014 
0015 #include <QDialogButtonBox>
0016 #include <QFormLayout>
0017 #include <QPushButton>
0018 using namespace Akonadi;
0019 NameEditDialog::NameEditDialog(QWidget *parent)
0020     : QDialog(parent)
0021 {
0022     setWindowTitle(i18nc("@title:window", "Edit Contact Name"));
0023 
0024     auto mainLayout = new QVBoxLayout(this);
0025 
0026     auto layout = new QFormLayout;
0027     mainLayout->addLayout(layout);
0028 
0029     mPrefixCombo = new KComboBox(this);
0030     mPrefixCombo->setDuplicatesEnabled(false);
0031     mPrefixCombo->setEditable(true);
0032 
0033     mGivenNameEdit = new KLineEdit(this);
0034     mGivenNameEdit->setTrapReturnKey(true);
0035 
0036     mAdditionalNameEdit = new KLineEdit(this);
0037     mAdditionalNameEdit->setTrapReturnKey(true);
0038 
0039     mFamilyNameEdit = new KLineEdit(this);
0040     mFamilyNameEdit->setTrapReturnKey(true);
0041 
0042     mSuffixCombo = new KComboBox(this);
0043     mSuffixCombo->setDuplicatesEnabled(false);
0044     mSuffixCombo->setEditable(true);
0045 
0046     mDisplayNameEdit = new DisplayNameEditWidget(this);
0047 
0048     layout->addRow(i18n("Honorific prefixes:"), mPrefixCombo);
0049     layout->addRow(i18n("Given name:"), mGivenNameEdit);
0050     layout->addRow(i18n("Additional names:"), mAdditionalNameEdit);
0051     layout->addRow(i18n("Family names:"), mFamilyNameEdit);
0052     layout->addRow(i18n("Honorific suffixes:"), mSuffixCombo);
0053     layout->addRow(i18n("Display:"), mDisplayNameEdit);
0054 
0055     QStringList prefixList;
0056     prefixList += QString();
0057     prefixList += i18n("Dr.");
0058     prefixList += i18n("Miss");
0059     prefixList += i18n("Mr.");
0060     prefixList += i18n("Mrs.");
0061     prefixList += i18n("Ms.");
0062     prefixList += i18n("Prof.");
0063     prefixList.sort();
0064 
0065     QStringList suffixList;
0066     suffixList += QString();
0067     suffixList += i18n("I");
0068     suffixList += i18n("II");
0069     suffixList += i18n("III");
0070     suffixList += i18n("Jr.");
0071     suffixList += i18n("Sr.");
0072     suffixList.sort();
0073 
0074     mPrefixCombo->addItems(prefixList);
0075     mSuffixCombo->addItems(suffixList);
0076 
0077     mPrefixCombo->lineEdit()->setFocus();
0078     auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this);
0079     QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
0080     okButton->setDefault(true);
0081     okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
0082     connect(buttonBox, &QDialogButtonBox::accepted, this, &NameEditDialog::accept);
0083     connect(buttonBox, &QDialogButtonBox::rejected, this, &NameEditDialog::reject);
0084     mainLayout->addWidget(buttonBox);
0085 }
0086 
0087 void NameEditDialog::setFamilyName(const QString &name)
0088 {
0089     mFamilyNameEdit->setText(name);
0090 }
0091 
0092 QString NameEditDialog::familyName() const
0093 {
0094     return mFamilyNameEdit->text();
0095 }
0096 
0097 void NameEditDialog::setGivenName(const QString &name)
0098 {
0099     mGivenNameEdit->setText(name);
0100 }
0101 
0102 QString NameEditDialog::givenName() const
0103 {
0104     return mGivenNameEdit->text();
0105 }
0106 
0107 void NameEditDialog::setPrefix(const QString &prefix)
0108 {
0109     mPrefixCombo->setItemText(mPrefixCombo->currentIndex(), prefix);
0110 }
0111 
0112 QString NameEditDialog::prefix() const
0113 {
0114     return mPrefixCombo->currentText();
0115 }
0116 
0117 void NameEditDialog::setSuffix(const QString &suffix)
0118 {
0119     mSuffixCombo->setItemText(mSuffixCombo->currentIndex(), suffix);
0120 }
0121 
0122 QString NameEditDialog::suffix() const
0123 {
0124     return mSuffixCombo->currentText();
0125 }
0126 
0127 void NameEditDialog::setAdditionalName(const QString &name)
0128 {
0129     mAdditionalNameEdit->setText(name);
0130 }
0131 
0132 QString NameEditDialog::additionalName() const
0133 {
0134     return mAdditionalNameEdit->text();
0135 }
0136 
0137 void NameEditDialog::loadContact(const KContacts::Addressee &contact)
0138 {
0139     setPrefix(contact.prefix());
0140     setGivenName(contact.givenName());
0141     setAdditionalName(contact.additionalName());
0142     setFamilyName(contact.familyName());
0143     setSuffix(contact.suffix());
0144     mDisplayNameEdit->loadContact(contact);
0145 }
0146 
0147 void NameEditDialog::storeContact(KContacts::Addressee &contact) const
0148 {
0149     contact.setPrefix(prefix());
0150     contact.setGivenName(givenName());
0151     contact.setAdditionalName(additionalName());
0152     contact.setFamilyName(familyName());
0153     contact.setSuffix(suffix());
0154 
0155     mDisplayNameEdit->changeName(contact);
0156     mDisplayNameEdit->storeContact(contact);
0157 }
0158 
0159 void NameEditDialog::setDisplayType(DisplayNameEditWidget::DisplayType type)
0160 {
0161     mDisplayNameEdit->setDisplayType(type);
0162 }
0163 
0164 DisplayNameEditWidget::DisplayType NameEditDialog::displayType() const
0165 {
0166     return mDisplayNameEdit->displayType();
0167 }
0168 
0169 #include "moc_nameeditdialog.cpp"