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

cantor/src/lib

  • sources
  • kde-4.14
  • kdeedu
  • cantor
  • src
  • lib
expression.cpp
Go to the documentation of this file.
1 /*
2  This program is free software; you can redistribute it and/or
3  modify it under the terms of the GNU General Public License
4  as published by the Free Software Foundation; either version 2
5  of the License, or (at your option) any later version.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  GNU General Public License for more details.
11 
12  You should have received a copy of the GNU General Public License
13  along with this program; if not, write to the Free Software
14  Foundation, Inc., 51 Franklin Street, Fifth Floor,
15  Boston, MA 02110-1301, USA.
16 
17  ---
18  Copyright (C) 2009 Alexander Rieder <alexanderrieder@gmail.com>
19  */
20 
21 #include "expression.h"
22 using namespace Cantor;
23 
24 #include <config-cantorlib.h>
25 
26 #include "session.h"
27 #include "result.h"
28 #include "textresult.h"
29 #include "imageresult.h"
30 #include "latexresult.h"
31 #include "settings.h"
32 #include "latexrenderer.h"
33 
34 #include <QFileInfo>
35 
36 #include <kstandarddirs.h>
37 #include <kprocess.h>
38 #include <ktemporaryfile.h>
39 #include <kdebug.h>
40 #include <kzip.h>
41 
42 
43 class Cantor::ExpressionPrivate
44 {
45 public:
46  ExpressionPrivate() {
47  result=0;
48  session=0;
49  isInternal=false;
50  }
51 
52  int id;
53  QString command;
54  QString error;
55  QList<QString> information;
56  Result* result;
57  Expression::Status status;
58  Session* session;
59  Expression::FinishingBehavior finishingBehavior;
60  bool isInternal;
61 };
62 
63 static const QString tex="\\documentclass[12pt,fleqn]{article} \n "\
64  "\\usepackage{latexsym,amsfonts,amssymb,ulem} \n "\
65  "\\usepackage[dvips]{graphicx} \n "\
66  "\\setlength\\textwidth{5in} \n "\
67  "\\setlength{\\parindent}{0pt} \n "\
68  "%1 \n "\
69  "\\pagestyle{empty} \n "\
70  "\\begin{document} \n "\
71  "%2 \n "\
72  "\\end{document}\n";
73 
74 
75 Expression::Expression( Session* session ) : QObject( session ),
76  d(new ExpressionPrivate)
77 {
78  d->session=session;
79  d->id=session->nextExpressionId();
80 }
81 
82 Expression::~Expression()
83 {
84  delete d->result;
85  delete d;
86 }
87 
88 void Expression::setCommand(const QString& command)
89 {
90  d->command=command;
91 }
92 
93 QString Expression::command()
94 {
95  return d->command;
96 }
97 
98 void Expression::setErrorMessage(const QString& error)
99 {
100  d->error=error;
101 }
102 
103 QString Expression::errorMessage()
104 {
105  return d->error;
106 }
107 
108 void Expression::setResult(Result* result)
109 {
110 
111  if(d->result)
112  delete d->result;
113 
114  d->result=result;
115 
116  if(result!=0)
117  {
118  kDebug()<<"settting result to a type "<<result->type()<<" result";
119  #ifdef WITH_EPS
120  //If it's text, and latex typesetting is enabled, render it
121  if ( session()->isTypesettingEnabled()&&
122  result->type()==TextResult::Type &&
123  dynamic_cast<TextResult*>(result)->format()==TextResult::LatexFormat &&
124  !result->toHtml().trimmed().isEmpty() &&
125  finishingBehavior()!=DeleteOnFinish &&
126  !isInternal()
127  )
128  {
129  renderResultAsLatex();
130  }
131  #endif
132  }
133 
134  emit gotResult();
135 }
136 
137 Result* Expression::result()
138 {
139  return d->result;
140 }
141 
142 void Expression::clearResult()
143 {
144  if(d->result)
145  delete d->result;
146 
147  d->result=0;
148 }
149 
150 void Expression::setStatus(Expression::Status status)
151 {
152  d->status=status;
153  emit statusChanged(status);
154 
155  if(status==Expression::Done&&d->finishingBehavior==Expression::DeleteOnFinish)
156  deleteLater();
157 }
158 
159 Expression::Status Expression::status()
160 {
161  return d->status;
162 }
163 
164 Session* Expression::session()
165 {
166  return d->session;
167 }
168 void Expression::renderResultAsLatex()
169 {
170  kDebug()<<"rendering as latex";
171  kDebug()<<"checking if it really is a formula that can be typeset";
172 
173  LatexRenderer* renderer=new LatexRenderer(this);
174  renderer->setLatexCode(result()->data().toString().trimmed());
175  renderer->addHeader(additionalLatexHeaders());
176 
177  connect(renderer, SIGNAL(done()), this, SLOT(latexRendered()));
178  connect(renderer, SIGNAL(error()), this, SLOT(latexRendered()));
179 
180  renderer->render();
181 }
182 
183 void Expression::latexRendered()
184 {
185  LatexRenderer* renderer=qobject_cast<LatexRenderer*>(sender());
186 
187  kDebug()<<"rendered a result to "<<renderer->imagePath();
188  //replace the textresult with the rendered latex image result
189  //ImageResult* latex=new ImageResult( d->latexFilename );
190  if(renderer->renderingSuccessful()&&result())
191  {
192  if (result()->type() == TextResult::Type)
193  {
194  TextResult* r=dynamic_cast<TextResult*>(result());
195  LatexResult* latex=new LatexResult(r->data().toString().trimmed(), KUrl(renderer->imagePath()), r->plain());
196  setResult( latex );
197  }
198  else if (result()->type() == LatexResult::Type)
199  {
200  LatexResult* previousLatexResult=dynamic_cast<LatexResult*>(result());
201  LatexResult* latex=new LatexResult(previousLatexResult->data().toString().trimmed(), KUrl(renderer->imagePath()), previousLatexResult->plain());
202  setResult( latex );
203  }
204  }else
205  {
206  //if rendering with latex was not successfull, just
207  //do nothing and keep the old result.
208  kDebug()<<"error rendering latex: "<<renderer->errorMessage();
209  }
210 
211  renderer->deleteLater();
212 }
213 
214 //saving code
215 QDomElement Expression::toXml(QDomDocument& doc)
216 {
217  QDomElement expr=doc.createElement( "Expression" );
218  QDomElement cmd=doc.createElement( "Command" );
219  QDomText cmdText=doc.createTextNode( command() );
220  cmd.appendChild( cmdText );
221  expr.appendChild( cmd );
222  if ( result() )
223  {
224  kDebug()<<"result: "<<result();
225  QDomElement resXml=result()->toXml( doc );
226  expr.appendChild( resXml );
227  }
228 
229  return expr;
230 }
231 
232 void Expression::saveAdditionalData(KZip* archive)
233 {
234  //just pass this call to the result
235  if(result())
236  result()->saveAdditionalData(archive);
237 }
238 
239 void Expression::addInformation(const QString& information)
240 {
241  d->information.append(information);
242 }
243 
244 QString Expression::additionalLatexHeaders()
245 {
246  return QString();
247 }
248 
249 int Expression::id()
250 {
251  return d->id;
252 }
253 
254 void Expression::setId(int id)
255 {
256  d->id=id;
257  emit idChanged();
258 }
259 
260 void Expression::setFinishingBehavior(Expression::FinishingBehavior behavior)
261 {
262  d->finishingBehavior=behavior;
263 }
264 
265 Expression::FinishingBehavior Expression::finishingBehavior()
266 {
267  return d->finishingBehavior;
268 }
269 
270 void Expression::setInternal(bool internal)
271 {
272  d->isInternal=internal;
273 }
274 
275 bool Expression::isInternal()
276 {
277  return d->isInternal;
278 }
279 
280 #include "expression.moc"
281 
Cantor::Expression::errorMessage
QString errorMessage()
returns the Error message, if an error occurred during the evaluation of the expression.
Definition: expression.cpp:103
Cantor::Expression::~Expression
virtual ~Expression()
destructor
Definition: expression.cpp:82
Cantor::Expression::Status
Status
Definition: expression.h:53
Cantor::LatexRenderer::addHeader
void addHeader(const QString &header)
Definition: latexrenderer.cpp:91
Cantor::Expression::setInternal
void setInternal(bool internal)
mark this expression as an internal expression, so for example latex will not be run on it ...
Definition: expression.cpp:270
QDomNode::appendChild
QDomNode appendChild(const QDomNode &newChild)
Cantor::Expression::setFinishingBehavior
void setFinishingBehavior(FinishingBehavior behavior)
set the finishing behaviour
Definition: expression.cpp:260
Cantor::Expression::statusChanged
void statusChanged(Cantor::Expression::Status status)
the status of the Expression has changed.
Cantor::Expression::addInformation
virtual void addInformation(const QString &information)
Adds some additional information/input to this expression.
Definition: expression.cpp:239
QObject::sender
QObject * sender() const
Cantor::Expression::finishingBehavior
FinishingBehavior finishingBehavior()
get the Expressions finishing behaviour
Definition: expression.cpp:265
imageresult.h
Cantor::TextResult::LatexFormat
Definition: textresult.h:36
session.h
Cantor::Result
Base class for different results, like text, image, animation.
Definition: result.h:39
Cantor::Expression::command
QString command()
Returns the command, represented by this Expression.
Definition: expression.cpp:93
Cantor::TextResult::data
QVariant data()
returns data associated with this result (text/images/etc)
Definition: textresult.cpp:67
Cantor::Expression::FinishingBehavior
FinishingBehavior
Enum indicating how this Expression behaves on finishing.
Definition: expression.h:62
Cantor::LatexRenderer::imagePath
QString imagePath() const
Definition: latexrenderer.cpp:148
Cantor::Expression::Expression
Expression(Session *session)
Expression constructor.
Definition: expression.cpp:75
tex
static const QString tex
Definition: expression.cpp:63
Cantor::Expression::saveAdditionalData
void saveAdditionalData(KZip *archive)
saves all the data, that can't be saved in xml in an extra file in the archive.
Definition: expression.cpp:232
Cantor::Expression::id
int id()
Returns the unique id of the Expression.
Definition: expression.cpp:249
textresult.h
Cantor::Expression::clearResult
void clearResult()
Deletes the result of this expression.
Definition: expression.cpp:142
Cantor::Expression::DeleteOnFinish
< The Object will delete itself when finished.
Definition: expression.h:64
result.h
Cantor::Session::nextExpressionId
int nextExpressionId()
Returns the next available Expression id It is basically a counter, incremented for each new Expressi...
Definition: session.cpp:119
Cantor::Expression::idChanged
void idChanged()
the Id of this Expression changed
Cantor::TextResult
Definition: textresult.h:32
Cantor::Result::toHtml
virtual QString toHtml()=0
returns html code, that represents this result, e.g.
QObject
expression.h
QString::isEmpty
bool isEmpty() const
QString::trimmed
QString trimmed() const
Cantor::Expression::setErrorMessage
void setErrorMessage(const QString &cmd)
Sets the error message.
Definition: expression.cpp:98
Cantor::Expression::status
Status status()
Returns the status of this Expression.
Definition: expression.cpp:159
Cantor::LatexRenderer::errorMessage
QString errorMessage() const
Definition: latexrenderer.cpp:127
Cantor::LatexResult::data
QVariant data()
returns data associated with this result (text/images/etc)
Definition: latexresult.cpp:91
Cantor::Result::toXml
virtual QDomElement toXml(QDomDocument &doc)=0
returns a DomElement, containing the information of the result
Cantor::LatexRenderer
Definition: latexrenderer.h:30
QObject::deleteLater
void deleteLater()
Cantor::Expression::setId
void setId(int id)
set the id of the Expression.
Definition: expression.cpp:254
QString
QList< QString >
Cantor::Expression::result
Result * result()
The result of this Expression.
Definition: expression.cpp:137
Cantor::Expression::isInternal
bool isInternal()
returns whether or not this expression is internal, or comes from the user
Definition: expression.cpp:275
QDomDocument::createTextNode
QDomText createTextNode(const QString &value)
Cantor::TextResult::plain
QString plain()
Definition: textresult.cpp:72
Cantor::LatexRenderer::render
void render()
Definition: latexrenderer.cpp:153
QDomDocument
Cantor::TextResult::Type
Definition: textresult.h:35
Cantor::Expression::gotResult
void gotResult()
A Result of the Expression has arrived.
Cantor::LatexResult::plain
QString plain()
Definition: latexresult.cpp:71
Cantor::Expression::additionalLatexHeaders
virtual QString additionalLatexHeaders()
Definition: expression.cpp:244
settings.h
Cantor::LatexRenderer::renderingSuccessful
bool renderingSuccessful() const
Definition: latexrenderer.cpp:132
Cantor::Result::type
virtual int type()=0
returns an unique number, representing the type of this result.
Cantor::Expression::setCommand
void setCommand(const QString &cmd)
Sets the command, represented by this Expression.
Definition: expression.cpp:88
latexresult.h
latexrenderer.h
Cantor::Expression::session
Session * session()
Returns the Session, this Expression belongs to.
Definition: expression.cpp:164
QDomDocument::createElement
QDomElement createElement(const QString &tagName)
Cantor::Expression::Done
The Running of the Expression is finished sucessfully.
Definition: expression.h:54
Cantor::Result::saveAdditionalData
virtual void saveAdditionalData(KZip *archive)
saves all the data, that can't be saved in xml in an extra file in the archive.
Definition: result.cpp:58
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
Cantor::LatexRenderer::setLatexCode
void setLatexCode(const QString &src)
Definition: latexrenderer.cpp:81
QDomElement
QVariant::toString
QString toString() const
Cantor::Expression::setResult
void setResult(Result *result)
Set the result of the Expression.
Definition: expression.cpp:108
QDomText
Cantor::Expression::toXml
QDomElement toXml(QDomDocument &doc)
returns an xml representation of this expression used for saving the worksheet
Definition: expression.cpp:215
Cantor::LatexResult
Class used for LaTeX results, it is basically an Eps result, but it exports a different type...
Definition: latexresult.h:35
Cantor::LatexResult::Type
Definition: latexresult.h:38
Cantor::Expression::setStatus
void setStatus(Status status)
Set the status statusChanged will be emitted.
Definition: expression.cpp:150
Cantor::Session
The Session object is the main class used to interact with a Backend.
Definition: session.h:50
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:16:33 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

cantor/src/lib

Skip menu "cantor/src/lib"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • 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