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

kjsembed

  • sources
  • kde-4.12
  • kdelibs
  • kjsembed
  • examples
  • kjsconsole
console.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE libraries
2  Copyright (C) 2005, 2006 Ian Reinhart Geiser <geiseri@kde.org>
3  Copyright (C) 2005, 2006 Matt Broadstone <mbroadst@gmail.com>
4  Copyright (C) 2005, 2006 Richard J. Moore <rich@kde.org>
5  Copyright (C) 2005, 2006 Erik L. Bunce <kde@bunce.us>
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Library General Public
9  License as published by the Free Software Foundation; either
10  version 2 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Library General Public License for more details.
16 
17  You should have received a copy of the GNU Library General Public License
18  along with this library; see the file COPYING.LIB. If not, write to
19  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  Boston, MA 02110-1301, USA.
21 */
22 
23 #include "console.h"
24 
25 #include <math.h>
26 
27 #include <QtCore/QDebug>
28 #include <QtGui/QFileDialog>
29 #include <QtCore/QFile>
30 
31 #include <kjsembed/kjseglobal.h>
32 #include <kjs/interpreter.h>
33 #include <kjs/ustring.h>
34 #include <kjs/object.h>
35 
36 #include "ui_jsconsole.h"
37 #include "kjs_object_model.h"
38 
39 Ui::KJSConsole m_ui;
40 
41 Console::Console( QWidget *parent ) :
42  QMainWindow( parent )
43 {
44  KJS::Interpreter *js = mKernel.interpreter();
45  KJS::JSObject *obj = js->globalObject();
46  m_model = new KJSObjectModel(js, this);
47  m_ui.setupUi(this);
48  m_ui.mObjectModel->setModel(m_model);
49  m_model->updateModel(obj);
50 
51  connect(m_ui.mCommand, SIGNAL(activated(QString)), this, SLOT(on_mExecute_clicked()));
52 }
53 
54 Console::~Console()
55 {
56 }
57 
58 QString errorTemplate = "<font color='#FF0000'>%1</font>";
59 
60 void Console::on_mExecute_clicked()
61 {
62  KJS::Interpreter *js = mKernel.interpreter();
63  KJS::ExecState *exec = js->globalExec();
64 
65  KJSEmbed::Engine::ExitStatus result = mKernel.execute(m_ui.mCommand->currentText());
66  KJS::Completion jsres = mKernel.completion();
67  m_ui.mConsole->append(m_ui.mCommand->currentText());
68  KJS::JSValue *value = jsres.value();
69  if ( result != KJSEmbed::Engine::Success )
70  {
71  m_ui.mConsole->append(errorTemplate.arg( KJSEmbed::toQString(jsres.value()->toString(exec)) ));
72  }
73  else
74  {
75  if(value)
76  m_ui.mConsole->append( KJSEmbed::toQString(jsres.value()->toString(exec) ));
77  }
78  KJS::JSObject *obj = js->globalObject();
79  m_model->updateModel(obj);
80  m_ui.mCommand->clearEditText();
81 }
82 
83 void Console::on_actionOpenScript_activated()
84 {
85  QString m_lastDir;
86  QString openFile = QFileDialog::getOpenFileName(this, tr("Select script to open..."),
87  m_lastDir, tr("Scripts (*.js *.kjs *.qjs)"));
88 
89  if( openFile.isEmpty() )
90  return;
91 
92  QString code;
93  QFile fIn(openFile);
94 
95  if (!fIn.open(QIODevice::ReadOnly | QIODevice::Text))
96  return;
97 
98  while (!fIn.atEnd())
99  {
100  QByteArray line = fIn.readLine();
101  code += line;
102  }
103 
104  m_ui.mInput->setText(code);
105 }
106 
107 void Console::on_actionCloseScript_activated()
108 {
109 }
110 
111 void Console::on_actionQuit_activated()
112 {
113  close();
114 }
115 
116 void Console::on_actionRun_activated()
117 {
118  KJS::Interpreter *js = mKernel.interpreter();
119  KJS::ExecState *exec = js->globalExec();
120 
121  KJSEmbed::Engine::ExitStatus result = mKernel.execute(m_ui.mInput->text());
122  KJS::Completion jsres = mKernel.completion();
123  KJS::JSValue *value = jsres.value();
124  if ( result != KJSEmbed::Engine::Success )
125  {
126  m_ui.mConsole->append(errorTemplate.arg( KJSEmbed::toQString(jsres.value()->toString(exec)) ) );
127  }
128  else
129  {
130  if(value)
131  m_ui.mConsole->append( KJSEmbed::toQString(jsres.value()->toString(exec)));
132  }
133  KJS::JSObject *obj = js->globalObject();
134  m_model->updateModel(obj);
135 }
136 
137 void Console::on_actionRunTo_activated()
138 {
139 }
140 void Console::on_actionStep_activated()
141 {
142 }
143 
144 void Console::on_actionStop_activated()
145 {
146 }
147 
148 
149 #include "console.moc"
Console::on_actionQuit_activated
void on_actionQuit_activated()
Definition: console.cpp:111
m_ui
Ui::KJSConsole m_ui
Definition: console.cpp:39
KJSEmbed::Engine::execute
ExitStatus execute(const KJS::UString &code)
Execute a code string using the current interpreter.
Definition: kjsembed.cpp:245
KJSObjectModel
Definition: kjs_object_model.h:35
Console::on_actionStep_activated
void on_actionStep_activated()
Definition: console.cpp:140
QWidget
Console::on_mExecute_clicked
void on_mExecute_clicked()
Definition: console.cpp:60
errorTemplate
QString errorTemplate
Definition: console.cpp:58
KJSEmbed::Engine::ExitStatus
ExitStatus
Status codes for script execution.
Definition: kjsembed.h:65
KJSEmbed::Engine::interpreter
KJS::Interpreter * interpreter() const
Returns the current interpreter.
Definition: kjsembed.cpp:200
KJSEmbed::Engine::completion
KJS::Completion completion() const
Returns the Completion object for the last script executed.
Definition: kjsembed.cpp:195
parent
QObject * parent
Definition: qaction_binding.cpp:48
kjs_object_model.h
Console::on_actionOpenScript_activated
void on_actionOpenScript_activated()
Definition: console.cpp:83
Console::Console
Console(QWidget *parent=0)
Definition: console.cpp:41
BrushNS::result
result
Definition: brush.cpp:48
Console::on_actionCloseScript_activated
void on_actionCloseScript_activated()
Definition: console.cpp:107
KJSEmbed::Engine::Success
Definition: kjsembed.h:65
Console::on_actionRunTo_activated
void on_actionRunTo_activated()
Definition: console.cpp:137
Console::~Console
~Console()
Definition: console.cpp:54
kjseglobal.h
Console::on_actionStop_activated
void on_actionStop_activated()
Definition: console.cpp:144
QMainWindow
console.h
close
END_OBJECT_METHOD object close()
Console::on_actionRun_activated
void on_actionRun_activated()
Definition: console.cpp:116
value
QVariant value
Definition: settings.cpp:35
KJSObjectModel::updateModel
void updateModel(KJS::JSObject *m_root)
Definition: kjs_object_model.cpp:43
KJSEmbed::toQString
QString toQString(const KJS::UString &u)
Definition: kjseglobal.h:58
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:47:53 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kjsembed

Skip menu "kjsembed"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

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