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

0001 /*
0002     SPDX-FileCopyrightText: 2010 Daniel Laidig <laidig@kde.org>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 #include "latexwidget.h"
0006 
0007 #include "practice/latexrenderer.h"
0008 #include "vocabularyfilter.h"
0009 #include "vocabularymodel.h"
0010 
0011 #include <KLocalizedString>
0012 #include <QDataWidgetMapper>
0013 
0014 using namespace Editor;
0015 
0016 LatexWidget::LatexWidget(VocabularyFilter *model, KEduVocDocument *doc, QWidget *parent)
0017     : QWidget(parent)
0018     , m_model{model}
0019     , m_doc{doc}
0020 {
0021     setupUi(this);
0022     lineEdit->setPlaceholderText(i18n("Enter LaTeX code here."));
0023     m_mapper = new QDataWidgetMapper(this);
0024     m_mapper->setModel(model);
0025     LatexDelegate *delegate = new LatexDelegate(this);
0026     delegate->setMathModeCheckBox(mathModeCheckBox);
0027     m_mapper->setItemDelegate(delegate);
0028     connect(mathModeCheckBox, &QCheckBox::stateChanged, this, &LatexWidget::checkBoxToggled);
0029     connect(lineEdit, &QLineEdit::editingFinished, this, &LatexWidget::updateLatex, Qt::QueuedConnection);
0030 }
0031 
0032 LatexWidget::~LatexWidget()
0033 {
0034     delete m_mapper;
0035 }
0036 
0037 void LatexWidget::setTranslation(KEduVocExpression *entry, int translation)
0038 {
0039     previewLabel->clear();
0040     previewLabel->setMinimumSize(QSize(1, 1));
0041 
0042     if (entry) {
0043         // we need to map the widgets relative to the translation (each translation has 9 columns)
0044         m_mapper->clearMapping();
0045 
0046         m_mapper->addMapping(lineEdit, VocabularyModel::EntryColumnsMAX * translation + VocabularyModel::Translation);
0047         m_translation = entry->translation(translation);
0048         updateLatex();
0049     }
0050 }
0051 
0052 void LatexWidget::slotDocumentChanged(KEduVocDocument *doc)
0053 {
0054     m_doc = doc;
0055 }
0056 
0057 void LatexWidget::slotSelectionChanged(const QItemSelection &itemSelected, const QItemSelection &itemDeselected)
0058 {
0059     Q_UNUSED(itemDeselected)
0060 
0061     if (itemSelected.indexes().size() >= 1) {
0062         // the selected index belongs to VocabularyFilter, when we need it from the vocabulary model
0063         QModelIndex index = m_model->index(itemSelected.indexes().at(0).row(), itemSelected.indexes().at(0).column());
0064         m_mapper->setCurrentModelIndex(index);
0065     }
0066 }
0067 
0068 void LatexWidget::checkBoxToggled()
0069 {
0070     // emulate editing of the line edit
0071     lineEdit->setFocus();
0072     mathModeCheckBox->setFocus();
0073 }
0074 
0075 void LatexWidget::updateLatex()
0076 {
0077     if (!m_translation) {
0078         return;
0079     }
0080     if (Practice::LatexRenderer::isLatex(m_translation->text())) {
0081         if (!m_renderer) {
0082             m_renderer = new Practice::LatexRenderer(this);
0083             m_renderer->setResultLabel(previewLabel);
0084         }
0085         m_renderer->renderLatex(m_translation->text());
0086     }
0087 }
0088 
0089 LatexDelegate::LatexDelegate(QObject *parent)
0090     : QItemDelegate(parent)
0091 {
0092 }
0093 
0094 void LatexDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
0095 {
0096     if (!index.isValid()) {
0097         return;
0098     }
0099 
0100     if (editor) {
0101         QLineEdit *entry = static_cast<QLineEdit *>(editor);
0102         if (entry) {
0103             QString text = index.model()->data(index).toString();
0104             if (text.startsWith(QLatin1String("$$")) && text.endsWith(QLatin1String("$$"))) {
0105                 entry->setText(text.mid(2, text.count() - 4));
0106                 m_checkBox->setChecked(true);
0107             } else if (text.startsWith(QStringLiteral("§§")) && text.endsWith(QStringLiteral("§§"))) {
0108                 entry->setText(text.mid(2, text.count() - 4));
0109                 m_checkBox->setChecked(false);
0110             } else {
0111                 entry->setText(QString());
0112                 return;
0113             }
0114         }
0115     }
0116 }
0117 
0118 void LatexDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
0119 {
0120     if (!index.isValid()) {
0121         return;
0122     }
0123 
0124     if (editor) {
0125         QLineEdit *entry = static_cast<QLineEdit *>(editor);
0126         if (entry) {
0127             QString text = entry->text();
0128             if (m_checkBox->isChecked()) {
0129                 model->setData(index, QString("$$" + text + "$$"));
0130             } else {
0131                 model->setData(index, QString(QStringLiteral("§§") + text + QStringLiteral("§§")));
0132             }
0133         }
0134     }
0135 }