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

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 "inflectionwidget.h"
0008 
0009 #include <KEduVocDocument>
0010 #include <KEduVocExpression>
0011 #include <KEduVocWordtype>
0012 
0013 #include "conjugationwidget.h"
0014 #include "declensionwidget.h"
0015 
0016 #include <KLocalizedString>
0017 
0018 #include <QDialog>
0019 #include <QDialogButtonBox>
0020 
0021 using namespace Editor;
0022 
0023 InflectionWidget::InflectionWidget(QWidget *parent)
0024     : QStackedWidget(parent)
0025 {
0026     m_wordTypeWidget = new QWidget(this);
0027     QVBoxLayout *wordTypeLayout = new QVBoxLayout();
0028 
0029     QLabel *label = new QLabel();
0030     wordTypeLayout->addWidget(label);
0031 
0032     QPushButton *toVerb = new QPushButton(i18n("Verb"));
0033     toVerb->setObjectName(QStringLiteral("toVerb"));
0034     connect(toVerb, &QPushButton::pressed, this, &InflectionWidget::setWordType);
0035     wordTypeLayout->addWidget(toVerb, 1);
0036 
0037     QPushButton *toNoun = new QPushButton(i18n("Noun"));
0038     toNoun->setObjectName(QStringLiteral("toNoun"));
0039     connect(toNoun, &QPushButton::pressed, this, &InflectionWidget::setWordType);
0040     wordTypeLayout->addWidget(toNoun, 1);
0041 
0042     QPushButton *toAdjective = new QPushButton(i18n("Adjective"));
0043     toAdjective->setObjectName(QStringLiteral("toAdjective"));
0044     connect(toAdjective, &QPushButton::pressed, this, &InflectionWidget::setWordType);
0045     wordTypeLayout->addWidget(toAdjective, 1);
0046 
0047     wordTypeLayout->addStretch();
0048 
0049     m_wordTypeWidget->setDisabled(true);
0050     m_wordTypeWidget->setLayout(wordTypeLayout);
0051 
0052     m_conjugationWidget = new ConjugationWidget(this);
0053     m_declensionWidget = new DeclensionWidget(this);
0054 
0055     addWidget(m_wordTypeWidget);
0056     addWidget(m_conjugationWidget);
0057     addWidget(m_declensionWidget);
0058 }
0059 
0060 void InflectionWidget::setDocument(const std::shared_ptr<KEduVocDocument> &doc)
0061 {
0062     m_doc = doc;
0063     m_conjugationWidget->setDocument(doc.get());
0064     m_declensionWidget->setDocument(doc.get());
0065 }
0066 
0067 void InflectionWidget::setTranslation(KEduVocExpression *entry, int translation)
0068 {
0069     m_entry = entry;
0070     m_translation = translation;
0071 
0072     if (!entry || !entry->translation(translation)) {
0073         m_wordTypeWidget->setDisabled(true);
0074         setCurrentWidget(m_wordTypeWidget);
0075         return;
0076     }
0077     if (!entry->translation(translation)->wordType()
0078         || !(entry->translation(translation)->wordType()->wordType() & KEduVocWordFlag::Verb
0079              || entry->translation(translation)->wordType()->wordType() & KEduVocWordFlag::Noun
0080              || entry->translation(translation)->wordType()->wordType() & KEduVocWordFlag::Adjective)) {
0081         qobject_cast<QLabel *>(m_wordTypeWidget->layout()->itemAt(0)->widget())->setText(i18n("\"%1\" is a:", entry->translation(translation)->text()));
0082         m_wordTypeWidget->setEnabled(true);
0083         setCurrentWidget(m_wordTypeWidget);
0084         return;
0085     }
0086 
0087     m_conjugationWidget->setTranslation(entry, translation);
0088     m_declensionWidget->setTranslation(entry, translation);
0089 
0090     if (entry->translation(translation)->wordType()->wordType() & KEduVocWordFlag::Verb) {
0091         setCurrentWidget(m_conjugationWidget);
0092     } else {
0093         setCurrentWidget(m_declensionWidget);
0094     }
0095 }
0096 
0097 void InflectionWidget::setWordType()
0098 {
0099     if (!sender()) {
0100         return;
0101     }
0102 
0103     KEduVocWordType *container = 0;
0104 
0105     if (sender()->objectName() == QLatin1String("toVerb")) {
0106         container = m_doc->wordTypeContainer()->childOfType(KEduVocWordFlag::Verb);
0107     } else if (sender()->objectName() == QLatin1String("toNoun")) {
0108         KEduVocWordFlags type = KEduVocWordFlag::Noun;
0109 
0110         QPointer<QDialogButtonBox> getGenderDialog = new QDialogButtonBox;
0111         getGenderDialog->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
0112 
0113         KComboBox *genderComboBox = new KComboBox;
0114         genderComboBox->addItem(i18n("Masculine"), KEduVocWordFlag::Masculine);
0115         genderComboBox->addItem(i18n("Neuter"), KEduVocWordFlag::Neuter);
0116         genderComboBox->addItem(i18n("Feminine"), KEduVocWordFlag::Feminine);
0117 
0118         QVBoxLayout *layout = new QVBoxLayout;
0119         layout->addWidget(genderComboBox);
0120         layout->addWidget(getGenderDialog);
0121 
0122         QPointer<QDialog> dialog = new QDialog(this);
0123         dialog->setLayout(layout);
0124         dialog->setWindowTitle(i18nc("@title:window", "Please select the noun's gender"));
0125 
0126         connect(getGenderDialog.data(), &QDialogButtonBox::accepted, dialog.data(), &QDialog::accept);
0127         connect(getGenderDialog.data(), &QDialogButtonBox::rejected, dialog.data(), &QDialog::reject);
0128 
0129         if (dialog->exec() == QDialog::Accepted) {
0130             type = (KEduVocWordFlags)(type | genderComboBox->itemData(genderComboBox->currentIndex()).toInt());
0131         }
0132 
0133         delete dialog;
0134 
0135         container = m_doc->wordTypeContainer()->childOfType(type);
0136     } else if (sender()->objectName() == QLatin1String("toAdjective")) {
0137         container = m_doc->wordTypeContainer()->childOfType(KEduVocWordFlag::Adjective);
0138     }
0139 
0140     if (container) {
0141         m_entry->translation(m_translation)->setWordType(container);
0142         setTranslation(m_entry, m_translation);
0143     }
0144 }