File indexing completed on 2024-05-12 16:44:04
0001 /* 0002 SPDX-FileCopyrightText: 2010-2018 Thomas Baumgart <tbaumgart@kde.org> 0003 SPDX-FileCopyrightText: 2010-2016 Cristian Oneț <onet.cristian@gmail.com> 0004 SPDX-FileCopyrightText: 2010 Alvaro Soliverez <asoliverez@gmail.com> 0005 SPDX-FileCopyrightText: 2017-2018 Łukasz Wojniłowicz <lukasz.wojnilowicz@gmail.com> 0006 SPDX-License-Identifier: GPL-2.0-or-later 0007 */ 0008 0009 #include "kmymoneytagcombo.h" 0010 #include "kmymoneymvccombo_p.h" 0011 0012 // ---------------------------------------------------------------------------- 0013 // QT Includes 0014 0015 // ---------------------------------------------------------------------------- 0016 // KDE Includes 0017 0018 #include <KLocalizedString> 0019 #include <KMessageBox> 0020 0021 // ---------------------------------------------------------------------------- 0022 // Project Includes 0023 0024 #include "mymoneytag.h" 0025 0026 class KMyMoneyTagComboPrivate : public KMyMoneyMVCComboPrivate 0027 { 0028 Q_DISABLE_COPY(KMyMoneyTagComboPrivate) 0029 0030 public: 0031 KMyMoneyTagComboPrivate() : 0032 KMyMoneyMVCComboPrivate() 0033 { 0034 } 0035 0036 QList<QString> m_usedIdList; 0037 QList<QString> m_usedTagNameList; 0038 QList<QString> m_closedIdList; 0039 QList<QString> m_closedTagNameList; 0040 }; 0041 0042 KMyMoneyTagCombo::KMyMoneyTagCombo(QWidget* parent) : 0043 KMyMoneyMVCCombo(*new KMyMoneyTagComboPrivate, true, parent) 0044 { 0045 } 0046 0047 KMyMoneyTagCombo::~KMyMoneyTagCombo() 0048 { 0049 } 0050 0051 void KMyMoneyTagCombo::loadTags(const QList<MyMoneyTag>& list) 0052 { 0053 Q_D(KMyMoneyTagCombo); 0054 clear(); 0055 0056 //add a blank item, since the field is optional 0057 addItem(QString(), QVariant(QString())); 0058 0059 //add all not closed tags 0060 QList<MyMoneyTag>::const_iterator it; 0061 for (it = list.constBegin(); it != list.constEnd(); ++it) { 0062 if (!(*it).isClosed()) 0063 addItem((*it).name(), QVariant((*it).id())); 0064 else { 0065 d->m_closedIdList.append((*it).id()); 0066 d->m_closedTagNameList.append((*it).name()); 0067 } 0068 } 0069 0070 //sort the model, which will sort the list in the combo 0071 model()->sort(Qt::DisplayRole, Qt::AscendingOrder); 0072 0073 //set the text to empty and the index to the first item on the list 0074 setCurrentIndex(0); 0075 clearEditText(); 0076 } 0077 0078 void KMyMoneyTagCombo::setUsedTagList(QList<QString>& usedIdList, QList<QString>& usedTagNameList) 0079 { 0080 Q_D(KMyMoneyTagCombo); 0081 d->m_usedIdList = usedIdList; 0082 d->m_usedTagNameList = usedTagNameList; 0083 for (auto i = 0; i < d->m_usedIdList.size(); ++i) { 0084 int index = findData(QVariant(d->m_usedIdList.at(i)), Qt::UserRole, Qt::MatchExactly); 0085 if (index != -1) removeItem(index); 0086 } 0087 } 0088 0089 void KMyMoneyTagCombo::checkCurrentText() 0090 { 0091 Q_D(KMyMoneyTagCombo); 0092 if (!contains(currentText())) { 0093 if (d->m_closedTagNameList.contains(currentText())) { 0094 // Tell the user what's happened 0095 QString msg = QString("<qt>") + i18n("Closed tags cannot be used.") + QString("</qt>"); 0096 KMessageBox::information(this, msg, i18n("Closed tag"), "Closed tag"); 0097 setCurrentText(); 0098 return; 0099 } else if (d->m_usedTagNameList.contains(currentText())) { 0100 // Tell the user what's happened 0101 QString msg = QString("<qt>") + i18n("The tag is already present.") + QString("</qt>"); 0102 KMessageBox::information(this, msg, i18n("Duplicate tag"), "Duplicate tag"); 0103 setCurrentText(); 0104 return; 0105 } 0106 QString id; 0107 // announce that we go into a possible dialog to create an object 0108 // This can be used by upstream widgets to disable filters etc. 0109 emit objectCreation(true); 0110 0111 emit createItem(currentText(), id); 0112 0113 // Announce that we return from object creation 0114 emit objectCreation(false); 0115 0116 // update the field to a possibly created object 0117 //m_id = id; 0118 addEntry(currentText(), id); 0119 setCurrentTextById(id); 0120 } 0121 }