• 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.12
  • 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())
191  {
192  TextResult* r=dynamic_cast<TextResult*>(result());
193  LatexResult* latex=new LatexResult(r->data().toString().trimmed(), KUrl(renderer->imagePath()), r->plain());
194  setResult( latex );
195  }else
196  {
197  //if rendering with latex was not successfull, just use the plain text version
198  //if available
199  TextResult* r=dynamic_cast<TextResult*>(result());
200  setResult(new TextResult(r->plain()));
201  kDebug()<<"error rendering latex: "<<renderer->errorMessage();
202  }
203 
204  renderer->deleteLater();
205 }
206 
207 //saving code
208 QDomElement Expression::toXml(QDomDocument& doc)
209 {
210  QDomElement expr=doc.createElement( "Expression" );
211  QDomElement cmd=doc.createElement( "Command" );
212  QDomText cmdText=doc.createTextNode( command() );
213  cmd.appendChild( cmdText );
214  expr.appendChild( cmd );
215  if ( result() )
216  {
217  kDebug()<<"result: "<<result();
218  QDomElement resXml=result()->toXml( doc );
219  expr.appendChild( resXml );
220  }
221 
222  return expr;
223 }
224 
225 void Expression::saveAdditionalData(KZip* archive)
226 {
227  //just pass this call to the result
228  if(result())
229  result()->saveAdditionalData(archive);
230 }
231 
232 void Expression::addInformation(const QString& information)
233 {
234  d->information.append(information);
235 }
236 
237 QString Expression::additionalLatexHeaders()
238 {
239  return QString();
240 }
241 
242 int Expression::id()
243 {
244  return d->id;
245 }
246 
247 void Expression::setId(int id)
248 {
249  d->id=id;
250  emit idChanged();
251 }
252 
253 void Expression::setFinishingBehavior(Expression::FinishingBehavior behavior)
254 {
255  d->finishingBehavior=behavior;
256 }
257 
258 Expression::FinishingBehavior Expression::finishingBehavior()
259 {
260  return d->finishingBehavior;
261 }
262 
263 void Expression::setInternal(bool internal)
264 {
265  d->isInternal=internal;
266 }
267 
268 bool Expression::isInternal()
269 {
270  return d->isInternal;
271 }
272 
273 #include "expression.moc"
274 
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:90
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:263
Cantor::Expression::setFinishingBehavior
void setFinishingBehavior(FinishingBehavior behavior)
set the finishing behaviour
Definition: expression.cpp:253
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:232
Cantor::Expression::finishingBehavior
FinishingBehavior finishingBehavior()
get the Expressions finishing behaviour
Definition: expression.cpp:258
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:147
QObject
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:225
Cantor::Expression::id
int id()
Returns the unique id of the Expression.
Definition: expression.cpp:242
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.
expression.h
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:126
Cantor::Result::toXml
virtual QDomElement toXml(QDomDocument &doc)=0
returns a DomElement, containing the information of the result
Cantor::LatexRenderer
Definition: latexrenderer.h:30
Cantor::Expression::setId
void setId(int id)
set the id of the Expression.
Definition: expression.cpp:247
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:268
Cantor::TextResult::plain
QString plain()
Definition: textresult.cpp:72
Cantor::LatexRenderer::render
void render()
Definition: latexrenderer.cpp:152
Cantor::TextResult::Type
Definition: textresult.h:35
Cantor::Expression::gotResult
void gotResult()
A Result of the Expression has arrived.
Cantor::Expression::additionalLatexHeaders
virtual QString additionalLatexHeaders()
Definition: expression.cpp:237
settings.h
Cantor::LatexRenderer::renderingSuccessful
bool renderingSuccessful() const
Definition: latexrenderer.cpp:131
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
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
Cantor::LatexRenderer::setLatexCode
void setLatexCode(const QString &src)
Definition: latexrenderer.cpp:80
Cantor::Expression::setResult
void setResult(Result *result)
Set the result of the Expression.
Definition: expression.cpp:108
Cantor::Expression::toXml
QDomElement toXml(QDomDocument &doc)
returns an xml representation of this expression used for saving the worksheet
Definition: expression.cpp:208
Cantor::LatexResult
Class used for LaTeX results, it is basically an Eps result, but it exports a different type...
Definition: latexresult.h:35
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-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:42:50 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
  • 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