21 #include <QHeaderView>
23 #include <QApplication>
25 #include <QTemporaryFile>
27 #include <QTextDocument>
33 #include <KStandardAction>
37 #include <kio/copyjob.h>
38 #include <kio/netaccess.h>
41 #include <analitza/variables.h>
42 #include <analitza/expression.h>
45 :
QWebView(parent), m_mode(Evaluation)
47 page()->setLinkDelegationPolicy(QWebPage::DelegateAllLinks);
48 setRenderHint(QPainter::TextAntialiasing);
50 connect(
this, SIGNAL(linkClicked(QUrl)), SLOT(
openClickedUrl(QUrl)));
52 QMetaObject::invokeMethod(
this,
"initialize", Qt::QueuedConnection);
57 qDeleteAll(m_options);
60 void ConsoleHtml::initialize()
62 QPalette p=qApp->palette();
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";
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; }";
87 QString
id =url.queryItemValue(
"id");
88 QString exp=url.queryItemValue(
"func");
99 QString result, newEntry;
100 Analitza::Expression res;
113 result = res.toHtml();
115 Analitza::Analyzer lambdifier(a.variables());
116 lambdifier.setExpression(res);
117 Analitza::Expression lambdaexp = lambdifier.dependenciesToLambda();
118 lambdifier.setExpression(lambdaexp);
120 Analitza::ExpressionType functype = lambdifier.type();
125 url.addQueryItem(
"id", opt->
id());
126 url.addQueryItem(
"func", lambdaexp.toString());
128 options += i18n(
" <a href='%1'>%2</a>", url.url(), opt->
caption());
132 if(!options.isEmpty())
133 options =
"<div class='options'>"+i18n(
"Options: %1", options)+
"</div>";
135 a.insertVariable(
"ans", res);
137 newEntry = QString(
"%1<br />=<span class='result'>%2</span>").arg(e.toHtml()).arg(result);
139 m_htmlLog += i18n(
"<ul class='error'>Error: <b>%1</b><li>%2</li></ul>", Qt::escape(input), a.errors().join(
"</li>\n<li>"));
142 updateView(newEntry, options);
144 return a.isCorrect();
149 QTemporaryFile temp(
"consolelog");
152 temp.setAutoRemove(
false);
153 return QDir::tempPath()+
'/'+temp.fileName();
156 QString ConsoleHtml::retrieve(
const KUrl& remoteUrl)
160 KIO::CopyJob* job=KIO::copyAs(remoteUrl, KUrl(path));
162 bool ret = KIO::NetAccess::synchronousRun(job, 0);
171 Q_ASSERT(!path.isEmpty());
175 QFile file(path.isLocalFile() ? path.toLocalFile() : retrieve(path));
177 if(file.open(QIODevice::ReadOnly | QIODevice::Text)) {
178 QTextStream stream(&file);
180 a.importScript(&stream);
181 correct=a.isCorrect();
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());
189 updateView(i18n(
"Imported: %1", path.pathOrUrl()), QString());
197 Q_ASSERT(!path.isEmpty());
199 QString savePath=path.isLocalFile() ? path.toLocalFile() :
temporaryPath();
200 QFile file(savePath);
201 correct=file.open(QIODevice::WriteOnly | QIODevice::Text);
204 QTextStream out(&file);
205 foreach(
const Analitza::Expression& exp, m_script)
206 out << exp.toString() << endl;
209 if(!path.isLocalFile()) {
210 KIO::CopyJob* job=KIO::move(savePath, path);
211 correct=KIO::NetAccess::synchronousRun(job, 0);
218 Q_ASSERT(!path.isEmpty());
221 QString savePath=path.isLocalFile() ? path.toLocalFile() :
temporaryPath();
222 QFile file(savePath);
223 correct=file.open(QIODevice::WriteOnly | QIODevice::Text);
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;
234 if(!path.isLocalFile()) {
235 KIO::CopyJob* job=KIO::move(savePath, path);
236 correct=KIO::NetAccess::synchronousRun(job, 0);
241 void ConsoleHtml::updateView(
const QString& newEntry,
const QString& options)
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";
247 code +=
"</head>\n<body>";
248 foreach(
const QString &entry, m_htmlLog)
249 code +=
"<p class='normal'>"+entry+
"</p>";
251 if(!newEntry.isEmpty()) {
252 m_htmlLog += newEntry;
254 code +=
"<p class='last'>"+newEntry+
"</p>";
256 code +=
"</body></html>";
262 QWebFrame* mf = page()->mainFrame();
263 mf->setScrollBarValue(Qt::Vertical, mf->scrollBarMaximum(Qt::Vertical));
268 QApplication::clipboard()->setText(selectedText());
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();
281 popup.addAction(KStandardAction::clear(
this, SLOT(
clear()), &popup));
283 popup.exec(ev->pos());
290 updateView(QString(), QString());
293 void ConsoleHtml::modifyVariable(
const QString& name,
const Analitza::Expression& exp)
295 a.variables()->modify(name, exp);
298 void ConsoleHtml::removeVariable(
const QString & name)
300 a.variables()->remove(name);
305 emit
paste(selectedText());
308 #include "consolehtml.moc"
virtual QString id() const =0
void openClickedUrl(const QUrl &url)
virtual ~ConsoleHtml()
Destructor.
bool addOperation(const Analitza::Expression &e, const QString &input)
Adds the operation defined by the expression e.
virtual bool matchesExpression(const Analitza::Expression &exp, const Analitza::ExpressionType &functype) const =0
virtual void contextMenuEvent(QContextMenuEvent *ev)
void changed()
Emits that something has changed.
bool loadScript(const KUrl &path)
Loads a script from path.
bool saveLog(const KUrl &path) const
Saves a log to path.
bool saveScript(const KUrl &path) const
Save a script yo path.
void copy() const
Copies the selected text to the clipboard.
ConsoleHtml(QWidget *parent=0)
Constructor.
virtual QString caption() const =0
void paste(const QString &code)
Emits the selected code to be pasted somewhere.
virtual void triggerOption(const Analitza::Expression &exp)=0
void clear()
Flushes the contents.
Simplifies the expression, tries to simplify when sees a variable not defined.