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

0001 /*
0002     SPDX-FileCopyrightText: 2008 Frederik Gladhorn <frederik.gladhorn@kdemail.net>
0003     SPDX-FileCopyrightText: 2011 Jan Gerrit Marker <jangerrit@weiler-marker.com>
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "declensionwidget.h"
0008 
0009 #include <KEduVocDocument>
0010 #include <KEduVocExpression>
0011 #include <KEduVocWordtype>
0012 
0013 #include <KLocalizedString>
0014 #include <KMessageBox>
0015 
0016 using namespace Editor;
0017 
0018 DeclensionWidget::DeclensionWidget(QWidget *parent)
0019     : QWidget(parent)
0020 {
0021     setupUi(this);
0022 
0023     numberSelection->setItemData(0, KEduVocWordFlag::Singular);
0024     numberSelection->setItemData(1, KEduVocWordFlag::Dual);
0025     numberSelection->setItemData(2, KEduVocWordFlag::Plural);
0026     connect(numberSelection, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, &DeclensionWidget::updateEntries);
0027 
0028     connect(nextButton, &QPushButton::clicked, this, &DeclensionWidget::nextNumber);
0029 
0030     setupLineEdits();
0031 
0032     for (auto iter = m_DeclensionLineEdits.begin(); iter != m_DeclensionLineEdits.end(); ++iter) {
0033         connect(iter.value(), &QLineEdit::textChanged, this, &DeclensionWidget::textChanged);
0034     }
0035 }
0036 
0037 void DeclensionWidget::textChanged(const QString &text)
0038 {
0039     int key = m_DeclensionLineEdits.key(qobject_cast<QLineEdit *>(sender())) | currentAdditionalWordFlag();
0040     m_entry->translation(m_identifier)->declension()->setDeclension(text, (KEduVocWordFlag::Flags)key);
0041     Q_EMIT sigModified();
0042 }
0043 
0044 void DeclensionWidget::updateEntries()
0045 {
0046     for (auto iter = m_DeclensionLineEdits.cbegin(); iter != m_DeclensionLineEdits.cend(); ++iter) {
0047         m_DeclensionLineEdits.value(iter.key())
0048             ->setText(m_entry->translation(m_identifier)->declension()->declension((KEduVocWordFlag::Flags)(iter.key() | currentAdditionalWordFlag())).text());
0049     }
0050 }
0051 
0052 void DeclensionWidget::setTranslation(KEduVocExpression *entry, int identifier)
0053 {
0054     if (!m_doc) {
0055         return;
0056     }
0057 
0058     m_entry = entry;
0059     if (m_identifier != identifier) {
0060         m_identifier = identifier;
0061     }
0062 
0063     if (!entry) {
0064         setEnabled(false);
0065         return;
0066     }
0067 
0068     setupLineEdits();
0069 
0070     setEnabled(true);
0071     if (entry->translation(m_identifier)->wordType()) {
0072         KEduVocWordFlags wordType = entry->translation(m_identifier)->wordType()->wordType();
0073         if (wordType & KEduVocWordFlag::Noun || wordType & KEduVocWordFlag::Adjective) {
0074             // we create declensions on demand. if empty it will simply not be saved.
0075             // very little memory overhead, comfy to use ;)
0076             if (!entry->translation(m_identifier)->declension()) {
0077                 entry->translation(m_identifier)->setDeclension(new KEduVocDeclension);
0078             }
0079 
0080             updateEntries();
0081         }
0082     }
0083 }
0084 
0085 void DeclensionWidget::setDocument(KEduVocDocument *doc)
0086 {
0087     m_doc = doc;
0088 }
0089 
0090 int DeclensionWidget::currentAdditionalWordFlag()
0091 {
0092     if (m_entry->translation(m_identifier)->wordType()->wordType() & KEduVocWordFlag::Noun) {
0093         ///@todo easier and better way to get gender?
0094         if (m_entry->translation(m_identifier)->wordType()->wordType() & KEduVocWordFlag::Feminine)
0095             return KEduVocWordFlag::Feminine;
0096         if (m_entry->translation(m_identifier)->wordType()->wordType() & KEduVocWordFlag::Masculine)
0097             return KEduVocWordFlag::Masculine;
0098         if (m_entry->translation(m_identifier)->wordType()->wordType() & KEduVocWordFlag::Neuter)
0099             return KEduVocWordFlag::Neuter;
0100     }
0101 
0102     return numberSelection->itemData(numberSelection->currentIndex()).toInt();
0103 }
0104 
0105 void DeclensionWidget::nextNumber()
0106 {
0107     int newIndex = numberSelection->currentIndex() + 1;
0108     if (newIndex >= numberSelection->count())
0109         newIndex = 0;
0110 
0111     numberSelection->setCurrentIndex(newIndex);
0112 }
0113 
0114 void DeclensionWidget::setupLineEdits()
0115 {
0116     m_DeclensionLineEdits.clear();
0117 
0118     if (m_entry == 0 || !m_entry->translation(m_identifier) || !m_entry->translation(m_identifier)->wordType()
0119         || m_entry->translation(m_identifier)->wordType()->wordType() & KEduVocWordFlag::Noun) {
0120         label_3->setText(i18n("Singular"));
0121         label_4->setText(i18n("Dual"));
0122         label_5->setText(i18n("Plural"));
0123 
0124         /* Hide the GUI elements which are needed to switch the number */
0125         for (int i = 0; i < numberLayout->count(); ++i) {
0126             numberLayout->itemAt(i)->widget()->hide();
0127         }
0128 
0129         m_DeclensionLineEdits[KEduVocWordFlag::Singular | KEduVocWordFlag::Nominative] = masculine_1;
0130         m_DeclensionLineEdits[KEduVocWordFlag::Singular | KEduVocWordFlag::Genitive] = masculine_2;
0131         m_DeclensionLineEdits[KEduVocWordFlag::Singular | KEduVocWordFlag::Dative] = masculine_3;
0132         m_DeclensionLineEdits[KEduVocWordFlag::Singular | KEduVocWordFlag::Accusative] = masculine_4;
0133         m_DeclensionLineEdits[KEduVocWordFlag::Singular | KEduVocWordFlag::Ablative] = masculine_5;
0134         m_DeclensionLineEdits[KEduVocWordFlag::Singular | KEduVocWordFlag::Locative] = masculine_6;
0135         m_DeclensionLineEdits[KEduVocWordFlag::Singular | KEduVocWordFlag::Vocative] = masculine_7;
0136 
0137         m_DeclensionLineEdits[KEduVocWordFlag::Dual | KEduVocWordFlag::Nominative] = feminine_1;
0138         m_DeclensionLineEdits[KEduVocWordFlag::Dual | KEduVocWordFlag::Genitive] = feminine_2;
0139         m_DeclensionLineEdits[KEduVocWordFlag::Dual | KEduVocWordFlag::Dative] = feminine_3;
0140         m_DeclensionLineEdits[KEduVocWordFlag::Dual | KEduVocWordFlag::Accusative] = feminine_4;
0141         m_DeclensionLineEdits[KEduVocWordFlag::Dual | KEduVocWordFlag::Ablative] = feminine_5;
0142         m_DeclensionLineEdits[KEduVocWordFlag::Dual | KEduVocWordFlag::Locative] = feminine_6;
0143         m_DeclensionLineEdits[KEduVocWordFlag::Dual | KEduVocWordFlag::Vocative] = feminine_7;
0144 
0145         m_DeclensionLineEdits[KEduVocWordFlag::Plural | KEduVocWordFlag::Nominative] = neuter_1;
0146         m_DeclensionLineEdits[KEduVocWordFlag::Plural | KEduVocWordFlag::Genitive] = neuter_2;
0147         m_DeclensionLineEdits[KEduVocWordFlag::Plural | KEduVocWordFlag::Dative] = neuter_3;
0148         m_DeclensionLineEdits[KEduVocWordFlag::Plural | KEduVocWordFlag::Accusative] = neuter_4;
0149         m_DeclensionLineEdits[KEduVocWordFlag::Plural | KEduVocWordFlag::Ablative] = neuter_5;
0150         m_DeclensionLineEdits[KEduVocWordFlag::Plural | KEduVocWordFlag::Locative] = neuter_6;
0151         m_DeclensionLineEdits[KEduVocWordFlag::Plural | KEduVocWordFlag::Vocative] = neuter_7;
0152     } else {
0153         label_3->setText(i18n("Masculine"));
0154         label_4->setText(i18n("Feminine"));
0155         label_5->setText(i18n("Neuter"));
0156 
0157         /* Show the GUI elements which are needed to switch the number */
0158         for (int i = 0; i < numberLayout->count(); ++i) {
0159             numberLayout->itemAt(i)->widget()->show();
0160         }
0161 
0162         m_DeclensionLineEdits[KEduVocWordFlag::Masculine | KEduVocWordFlag::Nominative] = masculine_1;
0163         m_DeclensionLineEdits[KEduVocWordFlag::Masculine | KEduVocWordFlag::Genitive] = masculine_2;
0164         m_DeclensionLineEdits[KEduVocWordFlag::Masculine | KEduVocWordFlag::Dative] = masculine_3;
0165         m_DeclensionLineEdits[KEduVocWordFlag::Masculine | KEduVocWordFlag::Accusative] = masculine_4;
0166         m_DeclensionLineEdits[KEduVocWordFlag::Masculine | KEduVocWordFlag::Ablative] = masculine_5;
0167         m_DeclensionLineEdits[KEduVocWordFlag::Masculine | KEduVocWordFlag::Locative] = masculine_6;
0168         m_DeclensionLineEdits[KEduVocWordFlag::Masculine | KEduVocWordFlag::Vocative] = masculine_7;
0169 
0170         m_DeclensionLineEdits[KEduVocWordFlag::Feminine | KEduVocWordFlag::Nominative] = feminine_1;
0171         m_DeclensionLineEdits[KEduVocWordFlag::Feminine | KEduVocWordFlag::Genitive] = feminine_2;
0172         m_DeclensionLineEdits[KEduVocWordFlag::Feminine | KEduVocWordFlag::Dative] = feminine_3;
0173         m_DeclensionLineEdits[KEduVocWordFlag::Feminine | KEduVocWordFlag::Accusative] = feminine_4;
0174         m_DeclensionLineEdits[KEduVocWordFlag::Feminine | KEduVocWordFlag::Ablative] = feminine_5;
0175         m_DeclensionLineEdits[KEduVocWordFlag::Feminine | KEduVocWordFlag::Locative] = feminine_6;
0176         m_DeclensionLineEdits[KEduVocWordFlag::Feminine | KEduVocWordFlag::Vocative] = feminine_7;
0177 
0178         m_DeclensionLineEdits[KEduVocWordFlag::Neuter | KEduVocWordFlag::Nominative] = neuter_1;
0179         m_DeclensionLineEdits[KEduVocWordFlag::Neuter | KEduVocWordFlag::Genitive] = neuter_2;
0180         m_DeclensionLineEdits[KEduVocWordFlag::Neuter | KEduVocWordFlag::Dative] = neuter_3;
0181         m_DeclensionLineEdits[KEduVocWordFlag::Neuter | KEduVocWordFlag::Accusative] = neuter_4;
0182         m_DeclensionLineEdits[KEduVocWordFlag::Neuter | KEduVocWordFlag::Ablative] = neuter_5;
0183         m_DeclensionLineEdits[KEduVocWordFlag::Neuter | KEduVocWordFlag::Locative] = neuter_6;
0184         m_DeclensionLineEdits[KEduVocWordFlag::Neuter | KEduVocWordFlag::Vocative] = neuter_7;
0185     }
0186 }