• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

parley

  • sources
  • kde-4.12
  • kdeedu
  • parley
  • src
  • editor
latexwidget.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  Copyright 2010 Daniel Laidig <laidig@kde.org>
3  ***************************************************************************/
4 
5 /***************************************************************************
6  * *
7  * This program is free software; you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation; either version 2 of the License, or *
10  * (at your option) any later version. *
11  * *
12  ***************************************************************************/
13 #include "latexwidget.h"
14 
15 #include "vocabulary/vocabularymodel.h"
16 #include "vocabulary/vocabularyfilter.h"
17 #include "practice/latexrenderer.h"
18 
19 #include <QDataWidgetMapper>
20 
21 using namespace Editor;
22 
23 LatexWidget::LatexWidget(VocabularyFilter *model, KEduVocDocument *doc, QWidget *parent) :
24  QWidget(parent), m_translation(0), m_renderer(0)
25 {
26  m_doc = doc;
27  m_model = model;
28 
29  setupUi(this);
30  lineEdit->setClickMessage(i18n("Enter LaTeX code here."));
31  m_mapper = new QDataWidgetMapper(this);
32  m_mapper->setModel(model);
33  LatexDelegate *delegate = new LatexDelegate(this);
34  delegate->setMathModeCheckBox(mathModeCheckBox);
35  m_mapper->setItemDelegate(delegate);
36  connect(mathModeCheckBox, SIGNAL(stateChanged(int)), this, SLOT(checkBoxToggled()));
37  connect(lineEdit, SIGNAL(editingFinished()), this, SLOT(updateLatex()), Qt::QueuedConnection);
38 }
39 
40 LatexWidget::~LatexWidget()
41 {
42  delete m_mapper;
43 }
44 
45 void LatexWidget::setTranslation(KEduVocExpression *entry, int translation)
46 {
47  previewLabel->clear();
48  previewLabel->setMinimumSize(QSize(1, 1));
49 
50  if (entry) {
51  // we need to map the widgets relative to the translation (each translation has 9 columns)
52  m_mapper->clearMapping();
53 
54  m_mapper->addMapping(lineEdit,
55  VocabularyModel::EntryColumnsMAX * translation + VocabularyModel::Translation);
56  m_translation = entry->translation(translation);
57  updateLatex();
58  }
59 }
60 
61 void LatexWidget::slotDocumentChanged(KEduVocDocument *doc)
62 {
63  m_doc = doc;
64 }
65 
66 void LatexWidget::slotSelectionChanged(const QItemSelection &itemSelected,
67  const QItemSelection &itemDeselected)
68 {
69  Q_UNUSED(itemDeselected)
70 
71  if (itemSelected.indexes().size() >= 1) {
72  // the selected index belongs to VocabularyFilter, when we need it from the vocabulary model
73  QModelIndex index = m_model->index(itemSelected.indexes().at(0).row(),
74  itemSelected.indexes().at(0).column());
75  m_mapper->setCurrentModelIndex(index);
76  }
77 }
78 
79 void LatexWidget::checkBoxToggled()
80 {
81  // emulate editing of the line edit
82  lineEdit->setFocus();
83  mathModeCheckBox->setFocus();
84 }
85 
86 void LatexWidget::updateLatex()
87 {
88  if (!m_translation) {
89  return;
90  }
91  if (Practice::LatexRenderer::isLatex(m_translation->text())) {
92  if (!m_renderer) {
93  m_renderer = new Practice::LatexRenderer(this);
94  m_renderer->setResultLabel(previewLabel);
95  }
96  m_renderer->renderLatex(m_translation->text());
97  }
98 }
99 
100 
101 LatexDelegate::LatexDelegate(QObject *parent) : QItemDelegate(parent), m_checkBox(0)
102 {
103 }
104 
105 void LatexDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
106 {
107  if (!index.isValid()) {
108  return;
109  }
110 
111  if (editor) {
112  KLineEdit *entry = static_cast <KLineEdit *> (editor);
113  if (entry) {
114  QString text = index.model()->data(index).toString();
115  if (text.startsWith(QLatin1String("$$")) && text.endsWith(QLatin1String("$$"))) {
116  entry->setText(text.mid(2, text.count()-4));
117  m_checkBox->setChecked(true);
118  } else if(text.startsWith(QString::fromUtf8("§§")) && text.endsWith(QString::fromUtf8("§§"))) {
119  entry->setText(text.mid(2, text.count()-4));
120  m_checkBox->setChecked(false);
121  } else {
122  entry->setText(QString());
123  return;
124  }
125  }
126  }
127 }
128 
129 void LatexDelegate::setModelData(QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const
130 {
131  if (!index.isValid()) {
132  return;
133  }
134 
135  if (editor) {
136  KLineEdit *entry = static_cast <KLineEdit *> (editor);
137  if (entry) {
138  QString text = entry->text();
139  if (m_checkBox->isChecked()) {
140  model->setData(index, QString("$$"+text+"$$"));
141  } else {
142  model->setData(index, QString(QString::fromUtf8("§§")+text+QString::fromUtf8("§§")));
143  }
144  }
145  }
146 }
147 
148 #include "latexwidget.moc"
Editor::LatexWidget::slotDocumentChanged
void slotDocumentChanged(KEduVocDocument *doc)
Called when a KEduVocDocument change happened.
Definition: latexwidget.cpp:61
Editor::LatexDelegate::setMathModeCheckBox
void setMathModeCheckBox(QCheckBox *checkBox)
Definition: latexwidget.h:78
Editor::LatexDelegate::setEditorData
void setEditorData(QWidget *editor, const QModelIndex &index) const
Definition: latexwidget.cpp:105
QWidget
Practice::LatexRenderer
Definition: latexrenderer.h:26
Editor::LatexWidget::~LatexWidget
~LatexWidget()
Definition: latexwidget.cpp:40
Editor::LatexDelegate
Definition: latexwidget.h:70
QObject
Editor::LatexDelegate::LatexDelegate
LatexDelegate(QObject *parent=0)
Definition: latexwidget.cpp:101
Practice::LatexRenderer::setResultLabel
void setResultLabel(QLabel *label)
Definition: latexrenderer.h:32
Editor::LatexWidget::LatexWidget
LatexWidget(VocabularyFilter *model, KEduVocDocument *doc, QWidget *parent=0)
Definition: latexwidget.cpp:23
Editor::VocabularyFilter
Definition: vocabularyfilter.h:27
Editor::VocabularyModel::EntryColumnsMAX
Definition: vocabularymodel.h:45
Editor::LatexWidget::slotSelectionChanged
void slotSelectionChanged(const QItemSelection &, const QItemSelection &)
Called when the selection changed in the vocabulary view.
Definition: latexwidget.cpp:66
QAbstractItemModel
QItemDelegate
Editor::LatexWidget::updateLatex
void updateLatex()
Definition: latexwidget.cpp:86
Editor::VocabularyModel::Translation
Definition: vocabularymodel.h:35
Editor::LatexDelegate::setModelData
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
Definition: latexwidget.cpp:129
latexwidget.h
latexrenderer.h
Editor::LatexWidget::setTranslation
void setTranslation(KEduVocExpression *entry, int translation)
Sets the selected word (KEduVocExpression) from the vocabularyView.
Definition: latexwidget.cpp:45
vocabularyfilter.h
Practice::LatexRenderer::renderLatex
void renderLatex(QString tex)
Definition: latexrenderer.cpp:51
Practice::LatexRenderer::isLatex
static bool isLatex(const QString &tex)
Definition: latexrenderer.cpp:96
vocabularymodel.h
Editor::LatexWidget::checkBoxToggled
void checkBoxToggled()
Definition: latexwidget.cpp:79
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:42:06 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

parley

Skip menu "parley"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal