/************************************************************************************* * Copyright (C) 2007 by Aleix Pol * * * * This program is free software; you can redistribute it and/or * * modify it under the terms of the GNU General Public License * * as published by the Free Software Foundation; either version 2 * * of the License, or (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the Free Software * * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * *************************************************************************************/ #include "consolehtml.h" #include #include #include #include #include #include #include #include #include #include #include "explexer.h" #include "expressionparser.h" #include "variables.h" #include "expression.h" #include ConsoleHtml::ConsoleHtml(QWidget *parent) : KHTMLPart(parent), m_mode(Evaluation) { QPalette p=qApp->palette(); setJScriptEnabled(false); setJavaEnabled(false); setMetaRefreshEnabled(false); setPluginsEnabled(false); setOnlyLocalReferences(true); m_css ="\n"; begin(); write("\n"+m_css+""); end(); connect(this, SIGNAL(popupMenu(const QString &, const QPoint &)), this, SLOT(context(const QString &, const QPoint &))); } ConsoleHtml::~ConsoleHtml() {} /*QString printable(const QString &s) { QString s1(s); s1.replace("<", "<"); s1.replace(">", ">"); return s1; }*/ bool ConsoleHtml::addOperation(const QString& op, bool mathml) { QString result, newEntry; Analitza::Expression res; Analitza::Expression e(op, mathml); a.setExpression(e); if(a.isCorrect()) { if(m_mode==Evaluation) { res=a.evaluate(); } else { res=a.calculate(); } } if(a.isCorrect()) { result = res.toHtml(); a.insertVariable("ans", res); m_script += op; //Script won't have the errors newEntry = QString("%1
=%2").arg(e.toHtml()).arg(result); } else { QString operation=op; operation.replace('%', " % "); //To avoid %1 or %2 constructions m_htmlLog += i18n("
    Error: %1
  • %2
", operation, a.errors().join("\n
  • ")); } updateView(newEntry); return a.isCorrect(); } bool ConsoleHtml::loadScript(const QString& path) { //FIXME: We have expression-only script support bool correct=false; if(!path.isEmpty()) { QFile file(path); QString line; if(file.open(QIODevice::ReadOnly | QIODevice::Text)) { QTextStream stream(&file); correct=true; while (correct && !stream.atEnd()) { line += stream.readLine(); // line of text excluding '\n' ExpLexer lex(line); ExpressionParser parser; parser.parse(&lex); if(!line.isEmpty() && lex.isCompletelyRead()) { correct &= addOperation(line, Analitza::Expression::isMathML(line)); line.clear(); } } file.close(); } } return correct; } bool ConsoleHtml::saveScript(const QString & path) const { bool correct=false; if(!path.isEmpty()) { QFile file(path); correct=file.open(QIODevice::WriteOnly | QIODevice::Text); if(correct) { QTextStream out(&file); QStringList::const_iterator it = m_script.begin(); for(; it!=m_script.end(); ++it) out << *it << endl; } file.close(); } return correct; } bool ConsoleHtml::saveLog(const QString& path) const { //FIXME: We have to choose between txt and html bool correct=false; if(!path.isEmpty()) { QFile file(path); correct=file.open(QIODevice::WriteOnly | QIODevice::Text); if(correct) { QTextStream out(&file); out << "\n" << m_css << "" << endl; out << "" << endl; foreach(const QString &entry, m_htmlLog) out << "

    " << entry << "

    " << endl; out << "\n" << endl; } file.close(); } return correct; } void ConsoleHtml::updateView(const QString& newEntry) { //FIXME: Check that the output html is not correct begin(); write("\n"); write("\n\n\t :) \n"); write(m_css); write("\n"); foreach(const QString &entry, m_htmlLog) { write("

    "+entry+"

    "); } if(!newEntry.isEmpty()) { m_htmlLog += newEntry; write("

    "+newEntry+"

    "); } write(""); end(); emit changed(); QTimer::singleShot(0, this, SLOT(scrollDown())); } void ConsoleHtml::scrollDown() { view()->verticalScrollBar()->setValue(view()->verticalScrollBar()->maximum()); } void ConsoleHtml::copy() const { QClipboard *clipboard = QApplication::clipboard(); clipboard->setText(selectedText()); } void ConsoleHtml::context(const QString & /*url*/, const QPoint & p) { KMenu popup; if(hasSelection()) { popup.addAction(KStandardAction::copy(this, SLOT(copy()), &popup)); KAction *act=new KAction(KIcon("edit-paste"), i18n("Paste \"%1\" to input", selectedText()), &popup); connect(act, SIGNAL(triggered()), SLOT(paste())); popup.addAction(act); popup.addSeparator(); } popup.addAction(KStandardAction::clear(this, SLOT(clear()), &popup)); popup.exec(p); } void ConsoleHtml::clear() { m_script.clear(); m_htmlLog.clear(); updateView(QString()); } void ConsoleHtml::modifyVariable(const QString& name, const Analitza::Expression& exp) { a.variables()->modify(name, exp); } void ConsoleHtml::removeVariable(const QString & name) { a.variables()->remove(name); } void ConsoleHtml::paste() { emit paste(selectedText()); } #include "consolehtml.moc"