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

0001 /*
0002     SPDX-FileCopyrightText: 2008 Frederik Gladhorn <frederik.gladhorn@kdemail.net>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "synonymwidget.h"
0007 
0008 #include <KEduVocDocument>
0009 #include <KEduVocExpression>
0010 #include <KEduVocTranslation>
0011 
0012 #include <KLocalizedString>
0013 #include <QDragEnterEvent>
0014 #include <QStringListModel>
0015 
0016 using namespace Editor;
0017 
0018 SynonymWidget::SynonymWidget(SynonymWidgetType type, QWidget *parent)
0019     : QWidget(parent)
0020     , m_type{type}
0021 {
0022     setupUi(this);
0023 
0024     connect(synonymButton, &QPushButton::clicked, this, &SynonymWidget::togglePair);
0025     m_listModel = new QStringListModel(this);
0026     listView->setModel(m_listModel);
0027 
0028     updateList();
0029 }
0030 
0031 void SynonymWidget::setDocument(KEduVocDocument *doc)
0032 {
0033     m_doc = doc;
0034 }
0035 
0036 void SynonymWidget::setTranslation(KEduVocExpression *entry, int translation)
0037 {
0038     // ignore zeros
0039     if (entry) {
0040         m_lastTranslation = m_currentTranslation;
0041         m_currentTranslation = entry->translation(translation);
0042         updateList();
0043     } else {
0044         // better play save, the entry most likely has been deleted.
0045         m_lastTranslation = 0;
0046         m_currentTranslation = 0;
0047         updateList();
0048     }
0049 }
0050 
0051 void SynonymWidget::updateList()
0052 {
0053     // clear the list
0054     m_listModel->removeRows(0, m_listModel->rowCount());
0055 
0056     // set the button text
0057     if (!(m_lastTranslation && m_currentTranslation)) {
0058         synonymButton->setEnabled(false);
0059         synonymButton->setText(i18n("Select Synonyms"));
0060     } else {
0061         synonymButton->setEnabled(true);
0062         switch (m_type) {
0063         case Synonym:
0064             if (m_currentTranslation->synonyms().contains(m_lastTranslation)) {
0065                 synonymButton->setText(i18n("%1 and %2 are not Synonyms", m_currentTranslation->text(), m_lastTranslation->text()));
0066             } else {
0067                 synonymButton->setText(i18n("%1 and %2 are Synonyms", m_currentTranslation->text(), m_lastTranslation->text()));
0068             }
0069             break;
0070         case Antonym:
0071             if (m_currentTranslation->antonyms().contains(m_lastTranslation)) {
0072                 synonymButton->setText(i18n("%1 and %2 are not Antonyms", m_currentTranslation->text(), m_lastTranslation->text()));
0073             } else {
0074                 synonymButton->setText(i18n("%1 and %2 are Antonyms", m_currentTranslation->text(), m_lastTranslation->text()));
0075             }
0076             break;
0077         case FalseFriend:
0078             if (m_currentTranslation->falseFriends().contains(m_lastTranslation)) {
0079                 synonymButton->setText(i18n("%1 and %2 are not False Friends", m_currentTranslation->text(), m_lastTranslation->text()));
0080             } else {
0081                 synonymButton->setText(i18n("%1 and %2 are False Friends", m_currentTranslation->text(), m_lastTranslation->text()));
0082             }
0083             break;
0084         }
0085     }
0086 
0087     if (m_currentTranslation) {
0088         switch (m_type) {
0089         case Synonym:
0090             synonymLabel->setText(i18nc("Title for a list of synonyms for a word", "Synonyms of %1:", m_currentTranslation->text()));
0091             break;
0092         case Antonym:
0093             synonymLabel->setText(i18nc("Title for a list of antonyms (opposites) for a word", "Antonyms of %1:", m_currentTranslation->text()));
0094             break;
0095         case FalseFriend:
0096             synonymLabel->setText(i18nc("Title for a list of false friend (things that sound similar but have different meanings) for a word",
0097                                         "False Friends of %1:",
0098                                         m_currentTranslation->text()));
0099             break;
0100         }
0101 
0102         // load list of synonyms/antonyms/ffs
0103         QList<KEduVocTranslation *> list;
0104         switch (m_type) {
0105         case Synonym:
0106             list = m_currentTranslation->synonyms();
0107             break;
0108         case Antonym:
0109             list = m_currentTranslation->antonyms();
0110             break;
0111         case FalseFriend:
0112             list = m_currentTranslation->falseFriends();
0113             break;
0114         }
0115         for (KEduVocTranslation *translation : qAsConst(list)) {
0116             int row = m_listModel->rowCount();
0117             m_listModel->insertRow(row);
0118             m_listModel->setData(m_listModel->index(row), translation->text());
0119         }
0120     } else {
0121         synonymLabel->clear();
0122     }
0123 }
0124 
0125 void SynonymWidget::togglePair()
0126 {
0127     // pair them up
0128     switch (m_type) {
0129     case Synonym:
0130         if (m_currentTranslation->synonyms().contains(m_lastTranslation)) {
0131             m_currentTranslation->removeSynonym(m_lastTranslation);
0132             m_lastTranslation->removeSynonym(m_currentTranslation);
0133         } else {
0134             m_currentTranslation->addSynonym(m_lastTranslation);
0135             m_lastTranslation->addSynonym(m_currentTranslation);
0136         }
0137         break;
0138     case Antonym:
0139         if (m_currentTranslation->antonyms().contains(m_lastTranslation)) {
0140             m_currentTranslation->removeAntonym(m_lastTranslation);
0141             m_lastTranslation->removeAntonym(m_currentTranslation);
0142         } else {
0143             m_currentTranslation->addAntonym(m_lastTranslation);
0144             m_lastTranslation->addAntonym(m_currentTranslation);
0145         }
0146         break;
0147     case FalseFriend:
0148         if (m_currentTranslation->falseFriends().contains(m_lastTranslation)) {
0149             m_currentTranslation->removeFalseFriend(m_lastTranslation);
0150             m_lastTranslation->removeFalseFriend(m_currentTranslation);
0151         } else {
0152             m_currentTranslation->addFalseFriend(m_lastTranslation);
0153             m_lastTranslation->addFalseFriend(m_currentTranslation);
0154         }
0155         break;
0156     }
0157 
0158     m_doc->setModified(true);
0159 
0160     updateList();
0161 }