File indexing completed on 2024-05-19 05:08:26

0001 /*
0002     SPDX-FileCopyrightText: 2014 Christian Dávid <christian-david@web.de>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "payeeidentifierselectiondelegate.h"
0007 
0008 #include <KLocalizedString>
0009 
0010 #include "models/payeeidentifiercontainermodel.h"
0011 #include "payeeidentifier/ibanbic/ibanbic.h"
0012 #include "payeeidentifier/nationalaccount/nationalaccount.h"
0013 
0014 
0015 payeeIdentifierTypeSelectionWidget::payeeIdentifierTypeSelectionWidget(QWidget* parent)
0016     : QComboBox(parent)
0017 {
0018     connect(this, QOverload<int>::of(&payeeIdentifierTypeSelectionWidget::activated), this, &payeeIdentifierTypeSelectionWidget::itemSelected);
0019 }
0020 
0021 void payeeIdentifierTypeSelectionWidget::itemSelected(int index)
0022 {
0023     if (index != 0) {
0024         Q_EMIT commitData(this);
0025         setCurrentIndex(0);
0026     }
0027 }
0028 
0029 payeeIdentifierSelectionDelegate::payeeIdentifierSelectionDelegate(QObject* parent)
0030     : QStyledItemDelegate(parent)
0031 {
0032 }
0033 
0034 QWidget* payeeIdentifierSelectionDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const
0035 {
0036     Q_UNUSED(option);
0037     Q_UNUSED(index);
0038 
0039     payeeIdentifierTypeSelectionWidget* comboBox = new payeeIdentifierTypeSelectionWidget(parent);
0040     comboBox->setFrame(false);
0041     connect(comboBox, &payeeIdentifierTypeSelectionWidget::commitData, this, &payeeIdentifierSelectionDelegate::commitData);
0042     comboBox->addItem(i18n("Please select the account number type"));
0043 
0044     const QMap<QString, QString> availableDelegates {
0045         {payeeIdentifiers::ibanBic::staticPayeeIdentifierIid(),         i18n("IBAN and BIC")},
0046         {payeeIdentifiers::nationalAccount::staticPayeeIdentifierIid(), i18n("National Account Number")}
0047     };
0048 
0049     for (auto delegate = availableDelegates.cbegin(); delegate != availableDelegates.cend(); ++delegate )
0050         comboBox->addItem(delegate.value(), delegate.key());
0051 
0052     return comboBox;
0053 }
0054 
0055 void payeeIdentifierSelectionDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const
0056 {
0057     QComboBox *const comboBox = qobject_cast<QComboBox*>(editor);
0058     const QString selectedPidType = comboBox->model()->data(comboBox->model()->index(comboBox->currentIndex(), 0), Qt::UserRole).toString();
0059     payeeIdentifier orig = model->data(index, payeeIdentifierContainerModel::payeeIdentifier).value<payeeIdentifier>();
0060 
0061     payeeIdentifier ident;
0062     if (selectedPidType == payeeIdentifiers::ibanBic::staticPayeeIdentifierIid())
0063         ident = payeeIdentifier(orig.id(), new payeeIdentifiers::ibanBic());
0064     else if (selectedPidType == payeeIdentifiers::nationalAccount::staticPayeeIdentifierIid())
0065         ident = payeeIdentifier(orig.id(), new payeeIdentifiers::nationalAccount());
0066 
0067     model->setData(index, QVariant::fromValue<payeeIdentifier>(ident), payeeIdentifierContainerModel::payeeIdentifier);
0068 }
0069 
0070 void payeeIdentifierSelectionDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& /*index*/) const
0071 {
0072     editor->setGeometry(option.rect);
0073 }
0074 
0075 QSize payeeIdentifierSelectionDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
0076 {
0077     return QStyledItemDelegate::sizeHint(option, index);
0078 }