File indexing completed on 2024-04-28 11:43:19

0001 /* This file is part of the KDE libraries
0002     Copyright (C) 2005, 2006 Ian Reinhart Geiser <geiseri@kde.org>
0003     Copyright (C) 2005, 2006 Matt Broadstone <mbroadst@gmail.com>
0004     Copyright (C) 2005, 2006 Richard J. Moore <rich@kde.org>
0005     Copyright (C) 2005, 2006 Erik L. Bunce <kde@bunce.us>
0006 
0007     This library is free software; you can redistribute it and/or
0008     modify it under the terms of the GNU Library General Public
0009     License as published by the Free Software Foundation; either
0010     version 2 of the License, or (at your option) any later version.
0011 
0012     This library is distributed in the hope that it will be useful,
0013     but WITHOUT ANY WARRANTY; without even the implied warranty of
0014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0015     Library General Public License for more details.
0016 
0017     You should have received a copy of the GNU Library General Public License
0018     along with this library; see the file COPYING.LIB.  If not, write to
0019     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0020     Boston, MA 02110-1301, USA.
0021 */
0022 
0023 #include "console.h"
0024 
0025 #include <math.h>
0026 
0027 #include <QDebug>
0028 #include <QFileDialog>
0029 #include <QFile>
0030 
0031 #include <kjsembed/kjseglobal.h>
0032 #include <kjs/interpreter.h>
0033 #include <kjs/ustring.h>
0034 #include <kjs/object.h>
0035 #include <kjs/JSVariableObject.h>
0036 
0037 #include "ui_jsconsole.h"
0038 #include "kjs_object_model.h"
0039 
0040 Ui::KJSConsole m_ui;
0041 
0042 Console::Console(QWidget *parent) :
0043     QMainWindow(parent)
0044 {
0045     KJS::Interpreter *js = mKernel.interpreter();
0046     KJS::JSObject *obj = js->globalObject();
0047     m_model = new KJSObjectModel(js, this);
0048     m_ui.setupUi(this);
0049     m_ui.mObjectModel->setModel(m_model);
0050     m_model->updateModel(obj);
0051 
0052     connect(m_ui.mCommand, SIGNAL(activated(QString)), this, SLOT(on_mExecute_clicked()));
0053 }
0054 
0055 Console::~Console()
0056 {
0057 }
0058 
0059 QString errorTemplate = "<font color='#FF0000'>%1</font>";
0060 
0061 void Console::on_mExecute_clicked()
0062 {
0063     KJS::Interpreter *js = mKernel.interpreter();
0064     KJS::ExecState *exec = js->globalExec();
0065 
0066     KJSEmbed::Engine::ExitStatus result = mKernel.execute(m_ui.mCommand->currentText());
0067     KJS::Completion jsres = mKernel.completion();
0068     m_ui.mConsole->append(m_ui.mCommand->currentText());
0069     KJS::JSValue *value = jsres.value();
0070     if (result != KJSEmbed::Engine::Success) {
0071         m_ui.mConsole->append(errorTemplate.arg(KJSEmbed::toQString(jsres.value()->toString(exec))));
0072     } else {
0073         if (value) {
0074             m_ui.mConsole->append(KJSEmbed::toQString(jsres.value()->toString(exec)));
0075         }
0076     }
0077     KJS::JSObject *obj = js->globalObject();
0078     m_model->updateModel(obj);
0079     m_ui.mCommand->clearEditText();
0080 }
0081 
0082 void Console::on_actionOpenScript_activated()
0083 {
0084     QString m_lastDir;
0085     QString openFile = QFileDialog::getOpenFileName(this, tr("Select script to open..."),
0086                        m_lastDir, tr("Scripts (*.js *.kjs *.qjs)"));
0087 
0088     if (openFile.isEmpty()) {
0089         return;
0090     }
0091 
0092     QString code;
0093     QFile fIn(openFile);
0094 
0095     if (!fIn.open(QIODevice::ReadOnly | QIODevice::Text)) {
0096         return;
0097     }
0098 
0099     while (!fIn.atEnd()) {
0100         QByteArray line = fIn.readLine();
0101         code += line;
0102     }
0103 
0104     m_ui.mInput->setText(code);
0105 }
0106 
0107 void Console::on_actionCloseScript_activated()
0108 {
0109 }
0110 
0111 void Console::on_actionQuit_activated()
0112 {
0113     close();
0114 }
0115 
0116 void Console::on_actionRun_activated()
0117 {
0118     KJS::Interpreter *js = mKernel.interpreter();
0119     KJS::ExecState *exec = js->globalExec();
0120 
0121     KJSEmbed::Engine::ExitStatus result = mKernel.execute(m_ui.mInput->text());
0122     KJS::Completion jsres = mKernel.completion();
0123     KJS::JSValue *value = jsres.value();
0124     if (result != KJSEmbed::Engine::Success) {
0125         m_ui.mConsole->append(errorTemplate.arg(KJSEmbed::toQString(jsres.value()->toString(exec))));
0126     } else {
0127         if (value) {
0128             m_ui.mConsole->append(KJSEmbed::toQString(jsres.value()->toString(exec)));
0129         }
0130     }
0131     KJS::JSObject *obj = js->globalObject();
0132     m_model->updateModel(obj);
0133 }
0134 
0135 void Console::on_actionRunTo_activated()
0136 {
0137 }
0138 void Console::on_actionStep_activated()
0139 {
0140 }
0141 
0142 void Console::on_actionStop_activated()
0143 {
0144 }
0145 
0146 #include "moc_console.cpp"