File indexing completed on 2024-04-21 03:51:02

0001 /*
0002     SPDX-FileCopyrightText: 2007 Frederik Gladhorn <frederik.gladhorn@kdemail.net>
0003     SPDX-FileCopyrightText: 2008 Javier Goday <jgoday@gmail.com>
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "summarywordwidget.h"
0008 
0009 #include "languagesettings.h"
0010 
0011 #include "lessonmodel.h"
0012 #include "vocabularyfilter.h"
0013 #include "vocabularymodel.h"
0014 #include "wordclassmodel.h"
0015 
0016 // Qt headers
0017 #include <QAbstractItemModel>
0018 #include <QDataWidgetMapper>
0019 #include <QHeaderView>
0020 #include <QItemSelection>
0021 #include <QLabel>
0022 #include <QTreeView>
0023 
0024 // KEduVocDocument
0025 #include <KEduVocContainer>
0026 #include <KEduVocDocument>
0027 #include <KEduVocExpression>
0028 #include <KEduVocWordtype>
0029 
0030 using namespace Editor;
0031 
0032 SummaryWordWidget::SummaryWordWidget(VocabularyFilter *model, const std::shared_ptr<KEduVocDocument> &doc, QWidget *parent)
0033     : QWidget(parent)
0034     , m_model{model}
0035     , m_doc{doc}
0036 {
0037     Q_ASSERT(model);
0038     Q_ASSERT(m_doc);
0039     setupUi(this);
0040     slotDocumentChanged(m_doc);
0041 
0042     m_mapper = new QDataWidgetMapper(this);
0043     m_mapper->setModel(model);
0044     m_mapper->setItemDelegate(new SummaryWordDelegate(this));
0045 
0046     connect(wordTypeComboBox, QOverload<const QString &>::of(&KComboBox::currentTextChanged), this, &SummaryWordWidget::wordTypeSelected);
0047 }
0048 
0049 void SummaryWordWidget::setTranslation(KEduVocExpression *entry, int translation)
0050 {
0051     if (entry) {
0052         // we need to map the widgets relative to the translation (each translation has 9 columns)
0053         m_mapper->clearMapping();
0054 
0055         m_mapper->addMapping(wordEntry, VocabularyModel::EntryColumnsMAX * translation + VocabularyModel::Translation);
0056         // m_mapper->addMapping(wordTypeComboBox,
0057         //                    VocabularyModel::EntryColumnsMAX * translation + VocabularyModel::WordType);
0058         m_mapper->addMapping(pronunciationEntry, VocabularyModel::EntryColumnsMAX * translation + VocabularyModel::Pronunciation);
0059         m_mapper->addMapping(exampleEntry, VocabularyModel::EntryColumnsMAX * translation + VocabularyModel::Example);
0060         m_mapper->addMapping(paraphraseEntry, VocabularyModel::EntryColumnsMAX * translation + VocabularyModel::Paraphrase);
0061         m_mapper->addMapping(commentEntry, VocabularyModel::EntryColumnsMAX * translation + VocabularyModel::Comment);
0062 
0063         languageLabel->setText("<b>" + m_doc->identifier(translation).name() + "</b>");
0064         lessonLabel->setText(entry->lesson()->name());
0065 
0066         setCurrentWordType(entry, translation);
0067     } else {
0068         clear();
0069     }
0070 
0071     m_entry = entry;
0072     m_translationId = translation;
0073 }
0074 
0075 void SummaryWordWidget::slotDocumentChanged(const std::shared_ptr<KEduVocDocument> &doc)
0076 {
0077     m_doc = doc;
0078     if (!m_doc) {
0079         qDebug() << "Set invalid document";
0080         delete m_wordTypeModel;
0081         m_wordTypeModel = nullptr;
0082     } else {
0083         delete m_wordTypeView;
0084         if (!m_wordTypeModel) {
0085             qDebug() << "Create word type model for summary view";
0086             m_wordTypeModel = new WordClassModel(this);
0087         }
0088         m_wordTypeModel->setDocument(m_doc);
0089         m_wordTypeView = new QTreeView(this);
0090         m_wordTypeView->setModel(m_wordTypeModel);
0091         wordTypeComboBox->setModel(m_wordTypeModel);
0092         wordTypeComboBox->setView(m_wordTypeView);
0093 
0094         m_wordTypeView->setColumnHidden(1, true);
0095         m_wordTypeView->header()->setVisible(false);
0096         m_wordTypeView->setRootIsDecorated(true);
0097         m_wordTypeView->expandAll();
0098     }
0099 }
0100 
0101 void SummaryWordWidget::slotSelectionChanged(const QItemSelection &itemSelected, const QItemSelection &itemDeselected)
0102 {
0103     Q_UNUSED(itemDeselected)
0104 
0105     if (itemSelected.indexes().size() >= 1) {
0106         // the selected index belongs to VocabularyFilter, when we need it from the vocabulary model
0107         QModelIndex index = m_model->index(itemSelected.indexes().at(0).row(), itemSelected.indexes().at(0).column());
0108         m_mapper->setCurrentModelIndex(index);
0109     }
0110 }
0111 
0112 /*
0113 void SummaryWordWidget::populateLessonList(KEduVocExpression *entry)
0114 {
0115     lessonComboBox->clear();
0116 
0117     LessonModel *basicLessonModel = new LessonModel(this);
0118     lessonComboBox->setModel(basicLessonModel);
0119     QTreeView *view = new QTreeView(this);
0120 
0121     view->setModel(basicLessonModel);
0122     lessonComboBox->setView(view);
0123 
0124     basicLessonModel->setDocument(m_doc);
0125 
0126     view->header()->setVisible(false);
0127     view->setRootIsDecorated(true);
0128     view->expandAll();
0129 
0130     view->setCurrentIndex(basicLessonModel->index(entry->lesson()));
0131 }
0132 */
0133 
0134 void SummaryWordWidget::setCurrentWordType(KEduVocExpression *entry, int translation)
0135 {
0136     if (entry && entry->translation(translation)->wordType()) {
0137         qDebug() << "Set current word type: " << entry->translation(translation)->wordType()->name();
0138         // select the right word type
0139         m_wordTypeView->setCurrentIndex(m_wordTypeModel->index(entry->translation(translation)->wordType()));
0140     } else {
0141         wordTypeComboBox->setCurrentIndex(-1);
0142     }
0143 }
0144 
0145 void SummaryWordWidget::clear()
0146 {
0147     qDebug() << "Clear summary widget";
0148 
0149     languageLabel->setText(QString());
0150     wordEntry->setText(QString());
0151 
0152     // lessonComboBox->clear();
0153     lessonLabel->setText(QString());
0154 
0155     pronunciationEntry->setText(QString());
0156     exampleEntry->setText(QString());
0157     paraphraseEntry->setText(QString());
0158     commentEntry->setText(QString());
0159 }
0160 
0161 SummaryWordDelegate::SummaryWordDelegate(QObject *parent)
0162     : QItemDelegate(parent)
0163 {
0164 }
0165 
0166 void SummaryWordDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
0167 {
0168     if (!index.isValid()) {
0169         return;
0170     }
0171 
0172     if (editor) {
0173         switch (VocabularyModel::columnType(index.column())) {
0174         case VocabularyModel::WordClass:
0175             break;
0176 
0177         case VocabularyModel::Comment:
0178         case VocabularyModel::Pronunciation:
0179         case VocabularyModel::Translation:
0180         case VocabularyModel::Example:
0181         case VocabularyModel::Paraphrase:
0182 
0183             QLineEdit *entry = static_cast<QLineEdit *>(editor);
0184             if (entry) {
0185                 entry->setText(index.model()->data(index).toString());
0186             }
0187             break;
0188         }
0189     }
0190 }
0191 
0192 void SummaryWordWidget::wordTypeSelected(const QString &wordTypeName)
0193 {
0194     if (!m_doc || !m_entry) {
0195         return;
0196     }
0197 
0198     KEduVocContainer *container = m_doc->wordTypeContainer()->childContainer(wordTypeName);
0199     if (container) {
0200         KEduVocWordType *wordType = static_cast<KEduVocWordType *>(container);
0201         if (wordType) {
0202             m_entry->translation(m_translationId)->setWordType(wordType);
0203         }
0204     }
0205 }
0206 
0207 #include "moc_summarywordwidget.cpp"