kjsembed
console.cpp
Go to the documentation of this file.00001 /* This file is part of the KDE libraries 00002 Copyright (C) 2005, 2006 Ian Reinhart Geiser <geiseri@kde.org> 00003 Copyright (C) 2005, 2006 Matt Broadstone <mbroadst@gmail.com> 00004 Copyright (C) 2005, 2006 Richard J. Moore <rich@kde.org> 00005 Copyright (C) 2005, 2006 Erik L. Bunce <kde@bunce.us> 00006 00007 This library is free software; you can redistribute it and/or 00008 modify it under the terms of the GNU Library General Public 00009 License as published by the Free Software Foundation; either 00010 version 2 of the License, or (at your option) any later version. 00011 00012 This library is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 Library General Public License for more details. 00016 00017 You should have received a copy of the GNU Library General Public License 00018 along with this library; see the file COPYING.LIB. If not, write to 00019 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00020 Boston, MA 02110-1301, USA. 00021 */ 00022 00023 #include "console.h" 00024 00025 #include <math.h> 00026 00027 #include <QtCore/QDebug> 00028 #include <QtGui/QFileDialog> 00029 #include <QtCore/QFile> 00030 00031 #include <kjsembed/kjseglobal.h> 00032 #include <kjs/interpreter.h> 00033 #include <kjs/ustring.h> 00034 #include <kjs/object.h> 00035 00036 #include "ui_jsconsole.h" 00037 #include "kjs_object_model.h" 00038 00039 Ui::KJSConsole m_ui; 00040 00041 Console::Console( QWidget *parent ) : 00042 QMainWindow( parent ) 00043 { 00044 KJS::Interpreter *js = mKernel.interpreter(); 00045 KJS::JSObject *obj = js->globalObject(); 00046 m_model = new KJSObjectModel(js, this); 00047 m_ui.setupUi(this); 00048 m_ui.mObjectModel->setModel(m_model); 00049 m_model->updateModel(obj); 00050 00051 connect(m_ui.mCommand, SIGNAL(activated(const QString&)), this, SLOT(on_mExecute_clicked())); 00052 } 00053 00054 Console::~Console() 00055 { 00056 } 00057 00058 QString errorTemplate = "<font color='#FF0000'>%1</font>"; 00059 00060 void Console::on_mExecute_clicked() 00061 { 00062 KJS::Interpreter *js = mKernel.interpreter(); 00063 KJS::ExecState *exec = js->globalExec(); 00064 00065 KJSEmbed::Engine::ExitStatus result = mKernel.execute(m_ui.mCommand->currentText()); 00066 KJS::Completion jsres = mKernel.completion(); 00067 m_ui.mConsole->append(m_ui.mCommand->currentText()); 00068 KJS::JSValue *value = jsres.value(); 00069 if ( result != KJSEmbed::Engine::Success ) 00070 { 00071 m_ui.mConsole->append(errorTemplate.arg( KJSEmbed::toQString(jsres.value()->toString(exec)) )); 00072 } 00073 else 00074 { 00075 if(value) 00076 m_ui.mConsole->append( KJSEmbed::toQString(jsres.value()->toString(exec) )); 00077 } 00078 KJS::JSObject *obj = js->globalObject(); 00079 m_model->updateModel(obj); 00080 m_ui.mCommand->clearEditText(); 00081 } 00082 00083 void Console::on_actionOpenScript_activated() 00084 { 00085 QString m_lastDir; 00086 QString openFile = QFileDialog::getOpenFileName(this, tr("Select script to open..."), 00087 m_lastDir, tr("Scripts (*.js *.kjs *.qjs)")); 00088 00089 if( openFile.isEmpty() ) 00090 return; 00091 00092 QString code; 00093 QFile fIn(openFile); 00094 00095 if (!fIn.open(QIODevice::ReadOnly | QIODevice::Text)) 00096 return; 00097 00098 while (!fIn.atEnd()) 00099 { 00100 QByteArray line = fIn.readLine(); 00101 code += line; 00102 } 00103 00104 m_ui.mInput->setText(code); 00105 } 00106 00107 void Console::on_actionCloseScript_activated() 00108 { 00109 } 00110 00111 void Console::on_actionQuit_activated() 00112 { 00113 close(); 00114 } 00115 00116 void Console::on_actionRun_activated() 00117 { 00118 KJS::Interpreter *js = mKernel.interpreter(); 00119 KJS::ExecState *exec = js->globalExec(); 00120 00121 KJSEmbed::Engine::ExitStatus result = mKernel.execute(m_ui.mInput->text()); 00122 KJS::Completion jsres = mKernel.completion(); 00123 KJS::JSValue *value = jsres.value(); 00124 if ( result != KJSEmbed::Engine::Success ) 00125 { 00126 m_ui.mConsole->append(errorTemplate.arg( KJSEmbed::toQString(jsres.value()->toString(exec)) ) ); 00127 } 00128 else 00129 { 00130 if(value) 00131 m_ui.mConsole->append( KJSEmbed::toQString(jsres.value()->toString(exec))); 00132 } 00133 KJS::JSObject *obj = js->globalObject(); 00134 m_model->updateModel(obj); 00135 } 00136 00137 void Console::on_actionRunTo_activated() 00138 { 00139 } 00140 void Console::on_actionStep_activated() 00141 { 00142 } 00143 00144 void Console::on_actionStop_activated() 00145 { 00146 } 00147 00148 00149 #include "console.moc"