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

Kate

  • sources
  • kde-4.12
  • applications
  • kate
  • part
  • script
katescriptconsole.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE libraries
2  Copyright (C) 2010 Miquel Sabaté <mikisabate@gmail.com>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License as published by the Free Software Foundation; either
7  version 2 of the License, or (at your option) any later version.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18 */
19 
20 
21 //BEGIN Includes
22 // Qt
23 #include <QtCore/QFile>
24 #include <QtGui/QLabel>
25 #include <QtGui/QVBoxLayout>
26 #include <QtGui/QPushButton>
27 #include <QtGui/QTextEdit>
28 
29 // KDE
30 #include <KStandardDirs>
31 #include <KLocale>
32 
33 // Kate
34 #include "katescriptconsole.h"
35 #include "katetemplatescript.h"
36 //END Includes
37 
38 
39 //BEGIN KateScriptConsoleEngine
40 KateScriptConsoleEngine::KateScriptConsoleEngine(KateView * view)
41  : m_view (view)
42 {
43  m_utilsUrl = KGlobal::dirs()->findResource("data", "katepart/script/commands/utils.js");
44 }
45 
46 KateScriptConsoleEngine::~KateScriptConsoleEngine()
47 {
48  /* There's nothing to do here */
49 }
50 
51 const QString & KateScriptConsoleEngine::execute(const QString & text)
52 {
53  static QString msg;
54  msg = "";
55  QString name = getFirstFunctionName(text, msg);
56  if (name.isEmpty() && !msg.isEmpty()) // Error
57  return msg;
58 
59  QFile file(m_utilsUrl);
60  if (!file.open(QFile::ReadOnly)) {
61  msg = i18n("Error: cannot open utils.js");
62  return msg;
63  }
64  QString utilsCode = file.readAll();
65  file.close();
66 
67  QString funcCode;
68  if (name.isEmpty()) { // It's a command
69  name = "foo";
70  funcCode = utilsCode + "function foo() { " + text + " }";
71  } else // It's a set of functions
72  funcCode = utilsCode + text;
73  KateTemplateScript script(funcCode);
74  msg = script.invoke(m_view, name, "");
75  if (msg.isEmpty())
76  msg = i18n("Syntax Error: Parse error");
77  return msg;
78 }
79 
80 const QString KateScriptConsoleEngine::getFirstFunctionName(const QString & text, QString & msg)
81 {
82  QString name = "";
83  QRegExp reg("(function)");
84  int i = reg.indexIn(text);
85  if (i < 0) // there's no defined functions
86  return "";
87  i += 8; // "function"
88  for (; text[i] != '('; ++i) {
89  if (text[i] == ' ') // avoid blank spaces
90  continue;
91  if (text[i] == '{' || text[i] == '}' || text[i] == ')') { // bad ...
92  msg = i18n("Error: There are bad defined functions");
93  return "";
94  }
95  name.append(text[i]);
96  }
97  return name;
98 }
99 //END KateScriptConsoleEngine
100 
101 
102 //BEGIN KateScriptConsole
103 KateScriptConsole::KateScriptConsole(KateView * view, QWidget * parent)
104  : KateViewBarWidget (true, parent)
105  , m_view (view)
106 {
107  Q_ASSERT(m_view != NULL);
108 
109  layout = new QVBoxLayout();
110  centralWidget()->setLayout(layout);
111  layout->setMargin(0);
112  hLayout = new QHBoxLayout;
113  m_result = new QLabel(this);
114  m_edit = new QTextEdit(this);
115  m_execute = new QPushButton(i18n("Execute"), this);
116  m_execute->setIcon(KIcon("quickopen"));
117  connect(m_execute, SIGNAL(clicked()), this, SLOT(executePressed()));
118 
119  layout->addWidget(m_edit);
120  hLayout->addWidget(m_result);
121  hLayout->addWidget(m_execute, 1, Qt::AlignRight);
122  layout->addLayout(hLayout);
123 
124  m_engine = new KateScriptConsoleEngine(m_view);
125 }
126 
127 KateScriptConsole::~KateScriptConsole()
128 {
129  delete m_engine;
130 }
131 
132 void KateScriptConsole::closed()
133 {
134  if (viewBar())
135  viewBar()->removeBarWidget(this);
136 }
137 
138 void KateScriptConsole::executePressed()
139 {
140  QString text = m_edit->toPlainText();
141  QString msg;
142  if (!text.isEmpty()) {
143  msg = m_engine->execute(text);
144  m_result->setText("<b>" + msg + "</b>");
145  } else
146  m_result->setText("<b>" + i18n("There's no code to execute") + "</b>");
147 }
148 //END KateScriptConsole
149 
150 
151 #include "katescriptconsole.moc"
152 
153 
154 // kate: space-indent on; indent-width 2; replace-tabs on;
155 
KateViewBarWidget::viewBar
KateViewBar * viewBar()
returns the currently associated KateViewBar and 0, if it is not associated
Definition: kateviewhelpers.h:299
Kate::Script::i18n
QScriptValue i18n(QScriptContext *context, QScriptEngine *engine)
i18n("text", arguments [optional])
Definition: katescripthelpers.cpp:186
KateScriptConsoleEngine
Manage JavaScript, allowing the user directly type commands as in KateCommnadLineBar environment...
Definition: katescriptconsole.h:39
KateScriptConsole::closed
virtual void closed()
Definition: katescriptconsole.cpp:132
katetemplatescript.h
QWidget
KateScriptConsoleEngine::KateScriptConsoleEngine
KateScriptConsoleEngine(KateView *view)
Constructor + Destructor.
Definition: katescriptconsole.cpp:40
KGlobal::dirs
KStandardDirs * dirs()
name
const char * name(StandardAction id)
QPushButton
QString
KateScriptConsoleEngine::execute
const QString & execute(const QString &text)
Execute a command or a set of functions.
Definition: katescriptconsole.cpp:51
KateViewBarWidget
Definition: kateviewhelpers.h:288
katescriptconsole.h
KateViewBarWidget::centralWidget
QWidget * centralWidget()
Definition: kateviewhelpers.h:305
KateViewBar::removeBarWidget
void removeBarWidget(KateViewBarWidget *barWidget)
Removes a widget from this viewbar.
Definition: kateviewhelpers.cpp:2452
KIcon
KateView
Definition: kateview.h:78
KateTemplateScript::invoke
QString invoke(KateView *view, const QString &functionName, const QString &srcText)
Definition: katetemplatescript.cpp:33
KateScriptConsole::~KateScriptConsole
virtual ~KateScriptConsole()
Definition: katescriptconsole.cpp:127
QLabel
KateScriptConsoleEngine::~KateScriptConsoleEngine
virtual ~KateScriptConsoleEngine()
Definition: katescriptconsole.cpp:46
KStandardDirs::findResource
QString findResource(const char *type, const QString &filename) const
KateScriptConsole::KateScriptConsole
KateScriptConsole(KateView *view, QWidget *parent=NULL)
Definition: katescriptconsole.cpp:103
KateScriptConsole::executePressed
void executePressed()
Definition: katescriptconsole.cpp:138
KateTemplateScript
Definition: katetemplatescript.h:28
QTextEdit
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:31:52 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Kate

Skip menu "Kate"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

applications API Reference

Skip menu "applications API Reference"
  •   kate
  •       kate
  •   KTextEditor
  •   Kate
  • Applications
  •   Libraries
  •     libkonq
  • Konsole

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