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

kalgebra

  • sources
  • kde-4.14
  • kdeedu
  • kalgebra
  • src
consolehtml.cpp
Go to the documentation of this file.
1 /*************************************************************************************
2  * Copyright (C) 2007 by Aleix Pol <aleixpol@kde.org> *
3  * *
4  * This program is free software; you can redistribute it and/or *
5  * modify it under the terms of the GNU General Public License *
6  * as published by the Free Software Foundation; either version 2 *
7  * of the License, or (at your option) any later version. *
8  * *
9  * This program is distributed in the hope that it will be useful, *
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12  * GNU General Public License for more details. *
13  * *
14  * You should have received a copy of the GNU General Public License *
15  * along with this program; if not, write to the Free Software *
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA *
17  *************************************************************************************/
18 
19 #include "consolehtml.h"
20 #include <QFile>
21 #include <QHeaderView>
22 #include <QClipboard>
23 #include <QApplication>
24 #include <QScrollBar>
25 #include <QTemporaryFile>
26 #include <QTimer>
27 #include <QTextDocument>
28 #include <qevent.h>
29 #include <QDir>
30 #include <QWebFrame>
31 
32 #include <KLocale>
33 #include <KStandardAction>
34 #include <KMenu>
35 #include <KHTMLView>
36 #include <KAction>
37 #include <kio/copyjob.h>
38 #include <kio/netaccess.h>
39 #include <kio/job.h>
40 
41 #include <analitza/variables.h>
42 #include <analitza/expression.h>
43 
44 ConsoleHtml::ConsoleHtml(QWidget *parent)
45  : QWebView(parent), m_mode(Evaluation)
46 {
47  page()->setLinkDelegationPolicy(QWebPage::DelegateAllLinks);
48  setRenderHint(QPainter::TextAntialiasing);
49 
50  connect(this, SIGNAL(linkClicked(QUrl)), SLOT(openClickedUrl(QUrl)));
51 
52  QMetaObject::invokeMethod(this, "initialize", Qt::QueuedConnection);
53 }
54 
55 ConsoleHtml::~ConsoleHtml()
56 {
57  qDeleteAll(m_options);
58 }
59 
60 void ConsoleHtml::initialize()
61 {
62  QPalette p=qApp->palette();
63 
64  m_css ="<style type=\"text/css\">\n";
65  m_css +=QString("\thtml { background-color: %1; }\n").arg(p.color(QPalette::Active, QPalette::Base).name());
66  m_css +="\t.error { border-style: solid; border-width: 1px; border-color: #ff3b21; background-color: #ffe9c4; padding:7px;}\n";
67  m_css +="\t.last { border-style: solid; border-width: 1px; border-color: #2020ff; background-color: #e0e0ff; padding:7px;}\n";
68  m_css +="\t.before { text-align:right; }\n";
69  m_css +="\t.op { font-weight: bold; }\n";
70 // m_css +="\t.normal:hover { border-style: solid; border-width: 1px; border-color: #777; }\n";
71  m_css +="\t.normal:hover { background-color: #f7f7f7; }\n";
72  m_css +="\t.cont { color: #560000; }\n";
73  m_css +="\t.num { color: #0000C4; }\n";
74  m_css +="\t.sep { font-weight: bold; color: #0000FF; }\n";
75  m_css +="\t.var { color: #640000; }\n";
76  m_css +="\t.keyword { color: #000064; }\n";
77  m_css +="\t.func { color: #008600; }\n";
78  m_css +="\t.result { padding-left: 10%; }\n";
79  m_css +="\t.options { font-size: small; text-align:right }\n";
80  m_css +="\t.string { color: #bb0000 }\n";
81  m_css +="\tli { padding-left: 12px; padding-bottom: 4px; list-style-position: inside; }";
82  m_css +="</style>\n";
83 }
84 
85 void ConsoleHtml::openClickedUrl(const QUrl& url)
86 {
87  QString id =url.queryItemValue("id");
88  QString exp=url.queryItemValue("func");
89 
90  foreach(InlineOptions* opt, m_options) {
91  if(opt->id() == id) {
92  opt->triggerOption(Analitza::Expression(exp, false));
93  }
94  }
95 }
96 
97 bool ConsoleHtml::addOperation(const Analitza::Expression& e, const QString& input)
98 {
99  QString result, newEntry;
100  Analitza::Expression res;
101 
102  a.setExpression(e);
103  if(a.isCorrect()) {
104  if(m_mode==Evaluation) {
105  res=a.evaluate();
106  } else {
107  res=a.calculate();
108  }
109  }
110 
111  QString options;
112  if(a.isCorrect()) {
113  result = res.toHtml();
114 
115  Analitza::Analyzer lambdifier(a.variables());
116  lambdifier.setExpression(res);
117  Analitza::Expression lambdaexp = lambdifier.dependenciesToLambda();
118  lambdifier.setExpression(lambdaexp);
119 
120  Analitza::ExpressionType functype = lambdifier.type();
121 
122  foreach(InlineOptions* opt, m_options) {
123  if(opt->matchesExpression(lambdaexp, functype)) {
124  KUrl url("/query");
125  url.addQueryItem("id", opt->id());
126  url.addQueryItem("func", lambdaexp.toString());
127 
128  options += i18n(" <a href='%1'>%2</a>", url.url(), opt->caption());
129  }
130  }
131 
132  if(!options.isEmpty())
133  options = "<div class='options'>"+i18n("Options: %1", options)+"</div>";
134 
135  a.insertVariable("ans", res);
136  m_script += e; //Script won't have the errors
137  newEntry = QString("%1<br />=<span class='result'>%2</span>").arg(e.toHtml()).arg(result);
138  } else {
139  m_htmlLog += i18n("<ul class='error'>Error: <b>%1</b><li>%2</li></ul>", Qt::escape(input), a.errors().join("</li>\n<li>"));
140  }
141 
142  updateView(newEntry, options);
143 
144  return a.isCorrect();
145 }
146 
147 QString temporaryPath()
148 {
149  QTemporaryFile temp("consolelog");
150  temp.open();
151  temp.close();
152  temp.setAutoRemove(false);
153  return QDir::tempPath()+'/'+temp.fileName();
154 }
155 
156 QString ConsoleHtml::retrieve(const KUrl& remoteUrl)
157 {
158  QString path=temporaryPath();
159 
160  KIO::CopyJob* job=KIO::copyAs(remoteUrl, KUrl(path));
161 
162  bool ret = KIO::NetAccess::synchronousRun(job, 0);
163  if(!ret)
164  path.clear();
165 
166  return path;
167 }
168 
169 bool ConsoleHtml::loadScript(const KUrl& path)
170 {
171  Q_ASSERT(!path.isEmpty());
172 
173  //FIXME: We have expression-only script support
174  bool correct=false;
175  QFile file(path.isLocalFile() ? path.toLocalFile() : retrieve(path));
176 
177  if(file.open(QIODevice::ReadOnly | QIODevice::Text)) {
178  QTextStream stream(&file);
179 
180  a.importScript(&stream);
181  correct=a.isCorrect();
182  }
183 
184  if(!correct) {
185  m_htmlLog += i18n("<ul class='error'>Error: Could not load %1. <br /> %2</ul>", path.pathOrUrl(), a.errors().join("<br/>"));
186  updateView(QString(), QString());
187  }
188  else
189  updateView(i18n("Imported: %1", path.pathOrUrl()), QString());
190 
191  return correct;
192 }
193 
194 bool ConsoleHtml::saveScript(const KUrl & path) const
195 {
196  bool correct=false;
197  Q_ASSERT(!path.isEmpty());
198 
199  QString savePath=path.isLocalFile() ? path.toLocalFile() : temporaryPath();
200  QFile file(savePath);
201  correct=file.open(QIODevice::WriteOnly | QIODevice::Text);
202 
203  if(correct) {
204  QTextStream out(&file);
205  foreach(const Analitza::Expression& exp, m_script)
206  out << exp.toString() << endl;
207  }
208 
209  if(!path.isLocalFile()) {
210  KIO::CopyJob* job=KIO::move(savePath, path);
211  correct=KIO::NetAccess::synchronousRun(job, 0);
212  }
213  return correct;
214 }
215 
216 bool ConsoleHtml::saveLog(const KUrl& path) const
217 {
218  Q_ASSERT(!path.isEmpty());
219  //FIXME: We have to choose between txt and html
220  bool correct=false;
221  QString savePath=path.isLocalFile() ? path.toLocalFile() : temporaryPath();
222  QFile file(savePath);
223  correct=file.open(QIODevice::WriteOnly | QIODevice::Text);
224 
225  if(correct) {
226  QTextStream out(&file);
227  out << "<html>\n<head>" << m_css << "</head>" << endl;
228  out << "<body>" << endl;
229  foreach(const QString &entry, m_htmlLog)
230  out << "<p>" << entry << "</p>" << endl;
231  out << "</body>\n</html>" << endl;
232  }
233 
234  if(!path.isLocalFile()) {
235  KIO::CopyJob* job=KIO::move(savePath, path);
236  correct=KIO::NetAccess::synchronousRun(job, 0);
237  }
238  return correct;
239 }
240 
241 void ConsoleHtml::updateView(const QString& newEntry, const QString& options)
242 {
243  QByteArray code;
244  code += "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n";
245  code += "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\">\n<head>\n\t<title> :) </title>\n";
246  code += m_css;
247  code += "</head>\n<body>";
248  foreach(const QString &entry, m_htmlLog)
249  code += "<p class='normal'>"+entry+"</p>";
250 
251  if(!newEntry.isEmpty()) {
252  m_htmlLog += newEntry;
253  code += options;
254  code += "<p class='last'>"+newEntry+"</p>";
255  }
256  code += "</body></html>";
257 
258  setContent(code);
259 
260  emit changed();
261 
262  QWebFrame* mf = page()->mainFrame();
263  mf->setScrollBarValue(Qt::Vertical, mf->scrollBarMaximum(Qt::Vertical));
264 }
265 
266 void ConsoleHtml::copy() const
267 {
268  QApplication::clipboard()->setText(selectedText());
269 }
270 
271 void ConsoleHtml::contextMenuEvent(QContextMenuEvent* ev)
272 {
273  KMenu popup;
274  if(hasSelection()) {
275  popup.addAction(KStandardAction::copy(this, SLOT(copy()), &popup));
276  KAction *act=new KAction(KIcon("edit-paste"), i18n("Paste \"%1\" to input", selectedText()), &popup);
277  connect(act, SIGNAL(triggered()), SLOT(paste()));
278  popup.addAction(act);
279  popup.addSeparator();
280  }
281  popup.addAction(KStandardAction::clear(this, SLOT(clear()), &popup));
282 
283  popup.exec(ev->pos());
284 }
285 
286 void ConsoleHtml::clear()
287 {
288  m_script.clear();
289  m_htmlLog.clear();
290  updateView(QString(), QString());
291 }
292 
293 void ConsoleHtml::modifyVariable(const QString& name, const Analitza::Expression& exp)
294 {
295  a.variables()->modify(name, exp);
296 }
297 
298 void ConsoleHtml::removeVariable(const QString & name)
299 {
300  a.variables()->remove(name);
301 }
302 
303 void ConsoleHtml::paste()
304 {
305  emit paste(selectedText());
306 }
307 
308 #include "consolehtml.moc"
QList::clear
void clear()
QWidget
QWebFrame::scrollBarMaximum
int scrollBarMaximum(Qt::Orientation orientation) const
QWebFrame
QWebView
InlineOptions::id
virtual QString id() const =0
QByteArray
QColor::name
QString name() const
QWebView::hasSelection
bool hasSelection() const
ConsoleHtml::openClickedUrl
void openClickedUrl(const QUrl &url)
Definition: consolehtml.cpp:85
QPalette::color
const QColor & color(ColorGroup group, ColorRole role) const
QWebView::linkClicked
void linkClicked(const QUrl &url)
ConsoleHtml::~ConsoleHtml
virtual ~ConsoleHtml()
Destructor.
Definition: consolehtml.cpp:55
ConsoleHtml::addOperation
bool addOperation(const Analitza::Expression &e, const QString &input)
Adds the operation defined by the expression e.
Definition: consolehtml.cpp:97
QFile
QTextStream
QWebView::selectedText
QString selectedText() const
QString::clear
void clear()
QWebView::setContent
void setContent(const QByteArray &data, const QString &mimeType, const QUrl &baseUrl)
consolehtml.h
QWebView::setRenderHint
void setRenderHint(QPainter::RenderHint hint, bool enabled)
QWebPage::setLinkDelegationPolicy
void setLinkDelegationPolicy(LinkDelegationPolicy policy)
QDir::tempPath
QString tempPath()
QApplication::clipboard
QClipboard * clipboard()
QContextMenuEvent
QTemporaryFile::setAutoRemove
void setAutoRemove(bool b)
QString::isEmpty
bool isEmpty() const
QWebPage::mainFrame
QWebFrame * mainFrame() const
QWebFrame::setScrollBarValue
void setScrollBarValue(Qt::Orientation orientation, int value)
QString
InlineOptions::matchesExpression
virtual bool matchesExpression(const Analitza::Expression &exp, const Analitza::ExpressionType &functype) const =0
QFile::open
virtual bool open(QFlags< QIODevice::OpenModeFlag > mode)
ConsoleHtml::contextMenuEvent
virtual void contextMenuEvent(QContextMenuEvent *ev)
Definition: consolehtml.cpp:271
ConsoleHtml::changed
void changed()
Emits that something has changed.
ConsoleHtml::loadScript
bool loadScript(const KUrl &path)
Loads a script from path.
Definition: consolehtml.cpp:169
ConsoleHtml::saveLog
bool saveLog(const KUrl &path) const
Saves a log to path.
Definition: consolehtml.cpp:216
ConsoleHtml::saveScript
bool saveScript(const KUrl &path) const
Save a script yo path.
Definition: consolehtml.cpp:194
QUrl
ConsoleHtml::copy
void copy() const
Copies the selected text to the clipboard.
Definition: consolehtml.cpp:266
QFile::close
virtual void close()
QMetaObject::invokeMethod
bool invokeMethod(QObject *obj, const char *member, Qt::ConnectionType type, QGenericReturnArgument ret, QGenericArgument val0, QGenericArgument val1, QGenericArgument val2, QGenericArgument val3, QGenericArgument val4, QGenericArgument val5, QGenericArgument val6, QGenericArgument val7, QGenericArgument val8, QGenericArgument val9)
QTemporaryFile::fileName
QString fileName() const
QWebView::url
QUrl url() const
ConsoleHtml::ConsoleHtml
ConsoleHtml(QWidget *parent=0)
Constructor.
Definition: consolehtml.cpp:44
InlineOptions::caption
virtual QString caption() const =0
InlineOptions
Definition: consolehtml.h:28
ConsoleHtml::paste
void paste(const QString &code)
Emits the selected code to be pasted somewhere.
QContextMenuEvent::pos
const QPoint & pos() const
Qt::escape
QString escape(const QString &plain)
InlineOptions::triggerOption
virtual void triggerOption(const Analitza::Expression &exp)=0
QWebView::page
QWebPage * page() const
temporaryPath
QString temporaryPath()
Definition: consolehtml.cpp:147
QClipboard::setText
void setText(const QString &text, Mode mode)
QTemporaryFile
ConsoleHtml::clear
void clear()
Flushes the contents.
Definition: consolehtml.cpp:286
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QTemporaryFile::open
bool open()
QString::arg
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
QUrl::queryItemValue
QString queryItemValue(const QString &key) const
QPalette
ConsoleHtml::Evaluation
Simplifies the expression, tries to simplify when sees a variable not defined.
Definition: consolehtml.h:51
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:11:49 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kalgebra

Skip menu "kalgebra"
  • Main Page
  • Namespace List
  • 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