File indexing completed on 2022-11-29 13:00:34

0001 /*
0002     SPDX-FileCopyrightText: 2008 Frederik Gladhorn <frederik.gladhorn@kdemail.net>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "conjugationwidget.h"
0007 
0008 #include <KEduVocDocument>
0009 #include <KEduVocExpression>
0010 #include <KEduVocWordtype>
0011 #include <KMessageBox>
0012 
0013 using namespace Editor;
0014 
0015 ConjugationWidget::ConjugationWidget(QWidget *parent)
0016     : QWidget(parent)
0017 {
0018     setupUi(this);
0019 
0020     connect(nextTenseButton, &QPushButton::clicked, this, &ConjugationWidget::slotNextTense);
0021     connect(tenseComboBox, static_cast<void (KComboBox::*)(int)>(&KComboBox::activated), this, &ConjugationWidget::slotTenseSelected);
0022     connect(tenseComboBox->lineEdit(), SIGNAL(editingFinished()), SLOT(tenseEditingFinished()));
0023 
0024     m_conjugationLineEdits[KEduVocWordFlag::First | KEduVocWordFlag::Singular] = singularFirstPersonLineEdit;
0025     m_conjugationLineEdits[KEduVocWordFlag::Second | KEduVocWordFlag::Singular] = singularSecondPersonLineEdit;
0026 
0027     m_conjugationLineEdits[KEduVocWordFlag::Third | KEduVocWordFlag::Masculine | KEduVocWordFlag::Singular] = singularThirdMalePersonLineEdit;
0028     m_conjugationLineEdits[KEduVocWordFlag::Third | KEduVocWordFlag::Feminine | KEduVocWordFlag::Singular] = singularThirdFemalePersonLineEdit;
0029     m_conjugationLineEdits[KEduVocWordFlag::Third | KEduVocWordFlag::Neuter | KEduVocWordFlag::Singular] = singularThirdNeutralPersonLineEdit;
0030 
0031     m_conjugationLineEdits[KEduVocWordFlag::First | KEduVocWordFlag::Dual] = dualFirstPersonLineEdit;
0032     m_conjugationLineEdits[KEduVocWordFlag::Second | KEduVocWordFlag::Dual] = dualSecondPersonLineEdit;
0033 
0034     m_conjugationLineEdits[KEduVocWordFlag::Third | KEduVocWordFlag::Masculine | KEduVocWordFlag::Dual] = dualThirdMalePersonLineEdit;
0035     m_conjugationLineEdits[KEduVocWordFlag::Third | KEduVocWordFlag::Feminine | KEduVocWordFlag::Dual] = dualThirdFemalePersonLineEdit;
0036     m_conjugationLineEdits[KEduVocWordFlag::Third | KEduVocWordFlag::Neuter | KEduVocWordFlag::Dual] = dualThirdNeutralPersonLineEdit;
0037 
0038     m_conjugationLineEdits[KEduVocWordFlag::First | KEduVocWordFlag::Plural] = pluralFirstPersonLineEdit;
0039     m_conjugationLineEdits[KEduVocWordFlag::Second | KEduVocWordFlag::Plural] = pluralSecondPersonLineEdit;
0040 
0041     m_conjugationLineEdits[KEduVocWordFlag::Third | KEduVocWordFlag::Masculine | KEduVocWordFlag::Plural] = pluralThirdMalePersonLineEdit;
0042     m_conjugationLineEdits[KEduVocWordFlag::Third | KEduVocWordFlag::Feminine | KEduVocWordFlag::Plural] = pluralThirdFemalePersonLineEdit;
0043     m_conjugationLineEdits[KEduVocWordFlag::Third | KEduVocWordFlag::Neuter | KEduVocWordFlag::Plural] = pluralThirdNeutralPersonLineEdit;
0044 
0045     for (auto iter = m_conjugationLineEdits.cbegin(); iter != m_conjugationLineEdits.cend(); ++iter) {
0046         connect(m_conjugationLineEdits.value(iter.key()), &QLineEdit::textChanged, this, &ConjugationWidget::textChanged);
0047     }
0048 }
0049 
0050 void ConjugationWidget::textChanged(const QString &text)
0051 {
0052     int key = m_conjugationLineEdits.key(qobject_cast<QLineEdit *>(sender()));
0053     KEduVocTranslation *translation = m_entry->translation(m_identifier);
0054     KEduVocConjugation conjugation = translation->getConjugation(tenseComboBox->currentText());
0055     conjugation.setConjugation(text, (KEduVocWordFlag::Flags)key);
0056     translation->setConjugation(tenseComboBox->currentText(), conjugation);
0057 }
0058 
0059 void ConjugationWidget::slotTenseSelected(int sel)
0060 {
0061     Q_UNUSED(sel);
0062     updateEntries();
0063 }
0064 
0065 void ConjugationWidget::updateEntries()
0066 {
0067     m_lastTenseSelection = tenseComboBox->currentText();
0068     KEduVocConjugation conjugation = m_entry->translation(m_identifier)->getConjugation(m_lastTenseSelection);
0069     for (auto iter = m_conjugationLineEdits.cbegin(); iter != m_conjugationLineEdits.cend(); ++iter) {
0070         QString text;
0071         if (conjugation.keys().contains(iter.key())) {
0072             text = conjugation.conjugation(iter.key()).text();
0073         }
0074 
0075         m_conjugationLineEdits[iter.key()]->setText(text);
0076     }
0077 }
0078 
0079 void ConjugationWidget::setTranslation(KEduVocExpression *entry, int identifier)
0080 {
0081     tenseComboBox->clear();
0082     tenseComboBox->completionObject()->clear();
0083 
0084     m_entry = entry;
0085     m_identifier = identifier;
0086 
0087     if (!m_doc || !entry) {
0088         setEnabled(false);
0089         return;
0090     }
0091 
0092     // init tenses per language
0093     tenseComboBox->addItems(m_doc->identifier(identifier).tenseList());
0094     tenseComboBox->completionObject()->insertItems(m_doc->identifier(identifier).tenseList());
0095     if (tenseComboBox->contains(m_lastTenseSelection)) {
0096         tenseComboBox->setCurrentItem(m_lastTenseSelection);
0097     } else {
0098         tenseComboBox->setCurrentIndex(0);
0099     }
0100 
0101     setEnabled(true);
0102     if (entry->translation(m_identifier)->wordType() && entry->translation(m_identifier)->wordType()->wordType() & KEduVocWordFlag::Verb) {
0103         updateVisiblePersons();
0104         updateEntries();
0105     }
0106 }
0107 
0108 void ConjugationWidget::setDocument(KEduVocDocument *doc)
0109 {
0110     m_doc = doc;
0111     tenseComboBox->clear();
0112     tenseComboBox->completionObject()->clear();
0113 }
0114 
0115 void ConjugationWidget::slotNextTense()
0116 {
0117     if (tenseComboBox->currentIndex() + 1 < tenseComboBox->count()) {
0118         tenseComboBox->setCurrentIndex(tenseComboBox->currentIndex() + 1);
0119     } else {
0120         tenseComboBox->setCurrentIndex(0);
0121     }
0122     updateEntries();
0123 }
0124 
0125 void ConjugationWidget::updateVisiblePersons()
0126 {
0127     if (m_identifier < 0) {
0128         showWidgets(false, false, false, false, false, false, false);
0129         return;
0130     }
0131 
0132     bool dualVisible = m_doc->identifier(m_identifier).personalPronouns().dualExists();
0133     bool maleFemaleDifferent = m_doc->identifier(m_identifier).personalPronouns().maleFemaleDifferent();
0134     bool neutralExists = m_doc->identifier(m_identifier).personalPronouns().neutralExists();
0135 
0136     showWidgets(true, true, dualVisible, true, maleFemaleDifferent, maleFemaleDifferent, neutralExists || (!maleFemaleDifferent));
0137 
0138     // set up the personal pronouns
0139     KEduVocPersonalPronoun pron = m_doc->identifier(m_identifier).personalPronouns();
0140 
0141     singularFirstPersonLabel->setText(pron.personalPronoun(KEduVocWordFlag::First | KEduVocWordFlag::Singular));
0142     singularSecondPersonLabel->setText(pron.personalPronoun(KEduVocWordFlag::Second | KEduVocWordFlag::Singular));
0143     singularThirdMalePersonLabel->setText(pron.personalPronoun(KEduVocWordFlag::Third | KEduVocWordFlag::Masculine | KEduVocWordFlag::Singular));
0144     singularThirdFemalePersonLabel->setText(pron.personalPronoun(KEduVocWordFlag::Third | KEduVocWordFlag::Feminine | KEduVocWordFlag::Singular));
0145     singularThirdNeutralPersonLabel->setText(pron.personalPronoun(KEduVocWordFlag::Third | KEduVocWordFlag::Neuter | KEduVocWordFlag::Singular));
0146 
0147     dualFirstPersonLabel->setText(pron.personalPronoun(KEduVocWordFlag::First | KEduVocWordFlag::Dual));
0148     dualSecondPersonLabel->setText(pron.personalPronoun(KEduVocWordFlag::Second | KEduVocWordFlag::Dual));
0149     dualThirdMalePersonLabel->setText(pron.personalPronoun(KEduVocWordFlag::Third | KEduVocWordFlag::Masculine | KEduVocWordFlag::Dual));
0150     dualThirdFemalePersonLabel->setText(pron.personalPronoun(KEduVocWordFlag::Third | KEduVocWordFlag::Feminine | KEduVocWordFlag::Dual));
0151     dualThirdNeutralPersonLabel->setText(pron.personalPronoun(KEduVocWordFlag::Third | KEduVocWordFlag::Neuter | KEduVocWordFlag::Dual));
0152 
0153     pluralFirstPersonLabel->setText(pron.personalPronoun(KEduVocWordFlag::First | KEduVocWordFlag::Plural));
0154     pluralSecondPersonLabel->setText(pron.personalPronoun(KEduVocWordFlag::Second | KEduVocWordFlag::Plural));
0155     pluralThirdMalePersonLabel->setText(pron.personalPronoun(KEduVocWordFlag::Third | KEduVocWordFlag::Masculine | KEduVocWordFlag::Plural));
0156     pluralThirdFemalePersonLabel->setText(pron.personalPronoun(KEduVocWordFlag::Third | KEduVocWordFlag::Feminine | KEduVocWordFlag::Plural));
0157     pluralThirdNeutralPersonLabel->setText(pron.personalPronoun(KEduVocWordFlag::Third | KEduVocWordFlag::Neuter | KEduVocWordFlag::Plural));
0158 }
0159 
0160 void ConjugationWidget::showWidgets(bool tenses, bool singular, bool dual, bool plural, bool maleVisible, bool femaleVisible, bool neuterVisible)
0161 {
0162     tenselabel->setVisible(tenses);
0163     tenseComboBox->setVisible(tenses);
0164     nextTenseButton->setVisible(tenses);
0165 
0166     singularLabel->setVisible(singular);
0167     singularFirstPersonLabel->setVisible(singular);
0168     singularFirstPersonLineEdit->setVisible(singular);
0169     singularSecondPersonLabel->setVisible(singular);
0170     singularSecondPersonLineEdit->setVisible(singular);
0171 
0172     singularThirdMalePersonLabel->setVisible(singular && maleVisible);
0173     singularThirdMalePersonLineEdit->setVisible(singular && maleVisible);
0174     singularThirdFemalePersonLabel->setVisible(singular && femaleVisible);
0175     singularThirdFemalePersonLineEdit->setVisible(singular && femaleVisible);
0176     singularThirdNeutralPersonLabel->setVisible(singular && neuterVisible);
0177     singularThirdNeutralPersonLineEdit->setVisible(singular && neuterVisible);
0178 
0179     dualLabel->setVisible(dual);
0180     dualFirstPersonLabel->setVisible(dual);
0181     dualFirstPersonLineEdit->setVisible(dual);
0182     dualSecondPersonLabel->setVisible(dual);
0183     dualSecondPersonLineEdit->setVisible(dual);
0184 
0185     dualThirdMalePersonLabel->setVisible(dual && maleVisible);
0186     dualThirdMalePersonLineEdit->setVisible(dual && maleVisible);
0187     dualThirdFemalePersonLabel->setVisible(dual && femaleVisible);
0188     dualThirdFemalePersonLineEdit->setVisible(dual && femaleVisible);
0189     dualThirdNeutralPersonLabel->setVisible(dual && neuterVisible);
0190     dualThirdNeutralPersonLineEdit->setVisible(dual && neuterVisible);
0191 
0192     pluralLabel->setVisible(plural);
0193     pluralFirstPersonLabel->setVisible(plural);
0194     pluralFirstPersonLineEdit->setVisible(plural);
0195     pluralSecondPersonLabel->setVisible(plural);
0196     pluralSecondPersonLineEdit->setVisible(plural);
0197 
0198     pluralThirdMalePersonLabel->setVisible(plural && maleVisible);
0199     pluralThirdMalePersonLineEdit->setVisible(plural && maleVisible);
0200     pluralThirdFemalePersonLabel->setVisible(plural && femaleVisible);
0201     pluralThirdFemalePersonLineEdit->setVisible(plural && femaleVisible);
0202     pluralThirdNeutralPersonLabel->setVisible(plural && neuterVisible);
0203     pluralThirdNeutralPersonLineEdit->setVisible(plural && neuterVisible);
0204 }
0205 
0206 void ConjugationWidget::tenseEditingFinished()
0207 {
0208     const QStringList &oldTenses = m_doc->identifier(m_identifier).tenseList();
0209     if (!oldTenses.contains(tenseComboBox->currentText())) {
0210         // add a new tense
0211         m_doc->identifier(m_identifier).setTense(oldTenses.count(), tenseComboBox->currentText());
0212         // put it into the completion
0213         tenseComboBox->completionObject()->addItem(tenseComboBox->currentText());
0214     }
0215 }