Changeset View
Changeset View
Standalone View
Standalone View
src/lib/expression.cpp
Show All 13 Lines | 1 | /* | |||
---|---|---|---|---|---|
14 | Foundation, Inc., 51 Franklin Street, Fifth Floor, | 14 | Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||
15 | Boston, MA 02110-1301, USA. | 15 | Boston, MA 02110-1301, USA. | ||
16 | 16 | | |||
17 | --- | 17 | --- | ||
18 | Copyright (C) 2009 Alexander Rieder <alexanderrieder@gmail.com> | 18 | Copyright (C) 2009 Alexander Rieder <alexanderrieder@gmail.com> | ||
19 | */ | 19 | */ | ||
20 | 20 | | |||
21 | #include "expression.h" | 21 | #include "expression.h" | ||
22 | #include "latexrenderer.h" | ||||
22 | using namespace Cantor; | 23 | using namespace Cantor; | ||
23 | 24 | | |||
24 | #include <config-cantorlib.h> | 25 | #include <config-cantorlib.h> | ||
25 | 26 | | |||
26 | #include "session.h" | 27 | #include "session.h" | ||
27 | #include "result.h" | 28 | #include "result.h" | ||
28 | #include "textresult.h" | 29 | #include "textresult.h" | ||
29 | #include "imageresult.h" | 30 | #include "imageresult.h" | ||
30 | #include "latexresult.h" | 31 | #include "latexresult.h" | ||
31 | #include "settings.h" | 32 | #include "settings.h" | ||
32 | #include "latexrenderer.h" | | |||
33 | 33 | | |||
34 | #include <QFileInfo> | 34 | #include <QFileInfo> | ||
35 | #include <QString> | 35 | #include <QString> | ||
36 | 36 | | |||
37 | #include <KProcess> | 37 | #include <KProcess> | ||
38 | #include <QDebug> | 38 | #include <QDebug> | ||
39 | #include <KZip> | 39 | #include <KZip> | ||
40 | 40 | | |||
▲ Show 20 Lines • Show All 58 Lines • ▼ Show 20 Line(s) | |||||
99 | 99 | | |||
100 | QString Expression::errorMessage() | 100 | QString Expression::errorMessage() | ||
101 | { | 101 | { | ||
102 | return d->error; | 102 | return d->error; | ||
103 | } | 103 | } | ||
104 | 104 | | |||
105 | void Expression::setResult(Result* result) | 105 | void Expression::setResult(Result* result) | ||
106 | { | 106 | { | ||
107 | | ||||
108 | if(d->result) | | |||
109 | delete d->result; | | |||
110 | | ||||
111 | d->result=result; | | |||
112 | | ||||
113 | if(result!=nullptr) | 107 | if(result!=nullptr) | ||
114 | { | 108 | { | ||
115 | qDebug()<<"settting result to a type "<<result->type()<<" result"; | 109 | qDebug()<<"settting result to a type "<<result->type()<<" result"; | ||
116 | #ifdef WITH_EPS | 110 | #ifdef WITH_EPS | ||
117 | //If it's text, and latex typesetting is enabled, render it | 111 | //If it's text, and latex typesetting is enabled, render it | ||
118 | if ( session()->isTypesettingEnabled()&& | 112 | if ( session()->isTypesettingEnabled()&& | ||
119 | result->type()==TextResult::Type && | 113 | result->type()==TextResult::Type && | ||
120 | dynamic_cast<TextResult*>(result)->format()==TextResult::LatexFormat && | 114 | dynamic_cast<TextResult*>(result)->format()==TextResult::LatexFormat && | ||
121 | !result->toHtml().trimmed().isEmpty() && | 115 | !result->toHtml().trimmed().isEmpty() && | ||
122 | finishingBehavior()!=DeleteOnFinish && | 116 | finishingBehavior()!=DeleteOnFinish && | ||
123 | !isInternal() | 117 | !isInternal() | ||
124 | ) | 118 | ) | ||
125 | { | 119 | { | ||
126 | renderResultAsLatex(); | 120 | renderResultAsLatex(result); | ||
121 | return; | ||||
127 | } | 122 | } | ||
128 | #endif | 123 | #endif | ||
129 | } | 124 | } | ||
130 | 125 | | |||
126 | if(d->result) | ||||
127 | delete d->result; | ||||
128 | | ||||
129 | d->result=result; | ||||
130 | | ||||
131 | emit gotResult(); | 131 | emit gotResult(); | ||
132 | } | 132 | } | ||
133 | 133 | | |||
134 | Result* Expression::result() | 134 | Result* Expression::result() | ||
135 | { | 135 | { | ||
136 | return d->result; | 136 | return d->result; | ||
137 | } | 137 | } | ||
138 | 138 | | |||
Show All 18 Lines | |||||
157 | { | 157 | { | ||
158 | return d->status; | 158 | return d->status; | ||
159 | } | 159 | } | ||
160 | 160 | | |||
161 | Session* Expression::session() | 161 | Session* Expression::session() | ||
162 | { | 162 | { | ||
163 | return d->session; | 163 | return d->session; | ||
164 | } | 164 | } | ||
165 | void Expression::renderResultAsLatex() | 165 | void Expression::renderResultAsLatex(Result* result) | ||
166 | { | 166 | { | ||
167 | qDebug()<<"rendering as latex"; | 167 | qDebug()<<"rendering as latex"; | ||
168 | qDebug()<<"checking if it really is a formula that can be typeset"; | 168 | qDebug()<<"checking if it really is a formula that can be typeset"; | ||
169 | 169 | | |||
170 | LatexRenderer* renderer=new LatexRenderer(this); | 170 | LatexRenderer* renderer=new LatexRenderer(this); | ||
171 | renderer->setLatexCode(result()->data().toString().trimmed()); | 171 | renderer->setLatexCode(result->data().toString().trimmed()); | ||
172 | renderer->addHeader(additionalLatexHeaders()); | 172 | renderer->addHeader(additionalLatexHeaders()); | ||
173 | 173 | | |||
174 | connect(renderer, &LatexRenderer::done, this, &Expression::latexRendered); | 174 | connect(renderer, &LatexRenderer::done, [=] { latexRendered(renderer, result); }); | ||
175 | connect(renderer, &LatexRenderer::error, this, &Expression::latexRendered); | 175 | connect(renderer, &LatexRenderer::error, [=] { latexRendered(renderer, result); }); | ||
asemke: why do we need these connects here at all? The rendering is blocking the GUI anyway if I see it… | |||||
Renderer use latex with KProcess, so while latex works, we can do something else. For this reason, we use signal and slots with LatexRenderer, as I see, there isn't GUI blocking here. sirgienko: Renderer use `latex` with `KProcess`, so while `latex` works, we can do something else. For… | |||||
176 | 176 | | |||
177 | renderer->render(); | 177 | renderer->render(); | ||
178 | } | 178 | } | ||
179 | 179 | | |||
180 | void Expression::latexRendered() | 180 | void Expression::latexRendered(LatexRenderer* renderer, Result* result) | ||
181 | { | 181 | { | ||
182 | LatexRenderer* renderer=qobject_cast<LatexRenderer*>(sender()); | | |||
183 | | ||||
184 | qDebug()<<"rendered a result to "<<renderer->imagePath(); | 182 | qDebug()<<"rendered a result to "<<renderer->imagePath(); | ||
185 | //replace the textresult with the rendered latex image result | 183 | //replace the textresult with the rendered latex image result | ||
186 | //ImageResult* latex=new ImageResult( d->latexFilename ); | 184 | //ImageResult* latex=new ImageResult( d->latexFilename ); | ||
187 | if(renderer->renderingSuccessful()&&result()) | 185 | if(renderer->renderingSuccessful()) | ||
188 | { | 186 | { | ||
189 | if (result()->type() == TextResult::Type) | 187 | if (result->type() == TextResult::Type) | ||
190 | { | 188 | { | ||
191 | TextResult* r=dynamic_cast<TextResult*>(result()); | 189 | TextResult* r=dynamic_cast<TextResult*>(result); | ||
192 | LatexResult* latex=new LatexResult(r->data().toString().trimmed(), QUrl::fromLocalFile(renderer->imagePath()), r->plain()); | 190 | LatexResult* latex=new LatexResult(r->data().toString().trimmed(), QUrl::fromLocalFile(renderer->imagePath()), r->plain()); | ||
193 | setResult( latex ); | 191 | setResult( latex ); | ||
194 | } | 192 | } | ||
195 | else if (result()->type() == LatexResult::Type) | 193 | else if (result->type() == LatexResult::Type) | ||
196 | { | 194 | { | ||
197 | LatexResult* previousLatexResult=dynamic_cast<LatexResult*>(result()); | 195 | LatexResult* previousLatexResult=dynamic_cast<LatexResult*>(result); | ||
198 | LatexResult* latex=new LatexResult(previousLatexResult->data().toString().trimmed(), QUrl::fromLocalFile(renderer->imagePath()), previousLatexResult->plain()); | 196 | LatexResult* latex=new LatexResult(previousLatexResult->data().toString().trimmed(), QUrl::fromLocalFile(renderer->imagePath()), previousLatexResult->plain()); | ||
199 | setResult( latex ); | 197 | setResult( latex ); | ||
200 | } | 198 | } | ||
201 | }else | 199 | }else | ||
202 | { | 200 | { | ||
203 | //if rendering with latex was not successful, just use the plain text version | 201 | //if rendering with latex was not successful, just use the plain text version | ||
204 | //if available | 202 | //if available | ||
205 | TextResult* r=dynamic_cast<TextResult*>(result()); | 203 | TextResult* r=dynamic_cast<TextResult*>(result); | ||
206 | setResult(new TextResult(r->plain())); | 204 | setResult(new TextResult(r->plain())); | ||
207 | qDebug()<<"error rendering latex: "<<renderer->errorMessage(); | 205 | qDebug()<<"error rendering latex: "<<renderer->errorMessage(); | ||
208 | } | 206 | } | ||
209 | 207 | | |||
208 | delete result; | ||||
asemke: why this delete here? | |||||
Because result ptr never used, exept of this function, so remove it for avoiding memory leacks. sirgienko: Because result ptr never used, exept of this function, so remove it for avoiding memory leacks. | |||||
209 | | ||||
210 | renderer->deleteLater(); | 210 | renderer->deleteLater(); | ||
211 | } | 211 | } | ||
212 | 212 | | |||
213 | //saving code | 213 | //saving code | ||
214 | QDomElement Expression::toXml(QDomDocument& doc) | 214 | QDomElement Expression::toXml(QDomDocument& doc) | ||
215 | { | 215 | { | ||
216 | QDomElement expr=doc.createElement( QLatin1String("Expression") ); | 216 | QDomElement expr=doc.createElement( QLatin1String("Expression") ); | ||
217 | QDomElement cmd=doc.createElement( QLatin1String("Command") ); | 217 | QDomElement cmd=doc.createElement( QLatin1String("Command") ); | ||
▲ Show 20 Lines • Show All 63 Lines • Show Last 20 Lines |
why do we need these connects here at all? The rendering is blocking the GUI anyway if I see it correctly. Why not to simply call renderer->render() and then process the result in the same function? With this we can also create LatexRenderer on the stack, no need for new and deleteLater().
Ideally the rendering of the result shouldn't be blocking. Do you see what is blocking here?