1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
|
/* This file is part of the KDE project
Copyright (C) 2003 Lucijan Busch <lucijan@gmx.at>
Copyright (C) 2004-2005 Jaroslaw Staniek <js@iidea.pl>
Copyright (C) 2005 Cedric Pasteur <cedric.pasteur@free.fr>
Copyright (C) 2005 Sebastian Sauer <mail@dipe.org>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU Library 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this program; see the file COPYING. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "kexiscriptdesignview.h"
#include "kexiscripteditor.h"
#include <kross/main/manager.h>
#include <kross/main/scriptcontainer.h>
#include <kross/main/scriptaction.h>
#include <kross/api/interpreter.h>
#include <layout.h>
#include <tqsplitter.h>
#include <tqtimer.h>
#include <tqdatetime.h>
#include <tqdom.h>
#include <stylesheet.h>
#include <ktextbrowser.h>
#include <kdebug.h>
#include <kexidialogbase.h>
#include <kexidb/connection.h>
/// @internal
class KexiScriptDesignViewPrivate
{
public:
/**
* The \a Kross::Api::ScriptAction instance which provides
* us access to the scripting framework Kross.
*/
Kross::Api::ScriptAction* scriptaction;
/// The \a KexiScriptEditor to edit the scripting code.
KexiScriptEditor* editor;
/// The \a KoProperty::Set used in the propertyeditor.
KoProperty::Set* properties;
/// Boolean flag to avoid infinite recursion.
bool updatesProperties;
/// Used to display statusmessages.
KTextBrowser* statusbrowser;
};
KexiScriptDesignView::KexiScriptDesignView(KexiMainWindow *mainWin, TQWidget *parent, Kross::Api::ScriptAction* scriptaction)
: KexiViewBase(mainWin, parent, "KexiScriptDesignView")
, d( new KexiScriptDesignViewPrivate() )
{
d->scriptaction = scriptaction;
d->updatesProperties = false;
TQSplitter* splitter = new TQSplitter(this);
splitter->setOrientation(Qt::Vertical);
TQHBoxLayout* layout = new TQHBoxLayout(this);
layout->addWidget(splitter);
d->editor = new KexiScriptEditor(mainWin, splitter, "ScriptEditor");
splitter->setFocusProxy(d->editor);
addChildView(d->editor);
setViewWidget(d->editor);
d->statusbrowser = new KTextBrowser(splitter, "ScriptStatusBrowser");
d->statusbrowser->setReadOnly(true);
d->statusbrowser->setTextFormat(TQTextBrowser::RichText);
//d->browser->setWordWrap(TQTextEdit::WidgetWidth);
d->statusbrowser->installEventFilter(this);
splitter->setResizeMode(d->statusbrowser, TQSplitter::KeepSize);
plugSharedAction( "data_execute", TQT_TQOBJECT(this), TQT_SLOT(execute()) );
if(KexiEditor::isAdvancedEditor()) // the configeditor is only in advanced mode avaiable.
plugSharedAction( "script_config_editor", TQT_TQOBJECT(d->editor), TQT_SLOT(slotConfigureEditor()) );
loadData();
d->properties = new KoProperty::Set(TQT_TQOBJECT(this), "KexiScripting");
connect(d->properties, TQT_SIGNAL( propertyChanged(KoProperty::Set&, KoProperty::Property&) ),
this, TQT_SLOT( slotPropertyChanged(KoProperty::Set&, KoProperty::Property&) ));
// To schedule the initialize fixes a crasher in Kate.
TQTimer::singleShot(50, this, TQT_SLOT( initialize() ));
}
KexiScriptDesignView::~KexiScriptDesignView()
{
delete d->properties;
delete d;
}
Kross::Api::ScriptAction* KexiScriptDesignView::scriptAction() const
{
return d->scriptaction;
}
void KexiScriptDesignView::initialize()
{
updateProperties();
d->editor->initialize( d->scriptaction );
}
void KexiScriptDesignView::updateProperties()
{
if(d->updatesProperties)
return;
d->updatesProperties = true;
Kross::Api::Manager* manager = Kross::Api::Manager::scriptManager();
TQString interpretername = d->scriptaction->getInterpreterName();
Kross::Api::InterpreterInfo* info = interpretername.isEmpty() ? 0 : manager->getInterpreterInfo(interpretername);
{
// if interpreter isn't defined or invalid, try to fallback.
TQStringList list;
list << "python" << "ruby";
TQStringList::ConstIterator it( list.constBegin() ), end( list.constEnd() );
while( (! info) && (it != end) ) {
interpretername = (*it);
info = manager->getInterpreterInfo(interpretername);
if(info)
d->scriptaction->setInterpreterName(interpretername);
++it;
}
}
if(info) {
d->properties->clear();
TQStringList interpreters = manager->getInterpreters();
KoProperty::Property::ListData* proplist = new KoProperty::Property::ListData(interpreters, interpreters);
KoProperty::Property* prop = new KoProperty::Property(
"language", // name
proplist, // ListData
d->scriptaction->getInterpreterName(), // value
i18n("Interpreter"), // caption
i18n("The used scripting interpreter."), // description
KoProperty::List // type
);
d->properties->addProperty(prop);
Kross::Api::InterpreterInfo::Option::Map options = info->getOptions();
Kross::Api::InterpreterInfo::Option::Map::ConstIterator it, end( options.constEnd() );
for( it = options.constBegin(); it != end; ++it) {
Kross::Api::InterpreterInfo::Option* option = it.data();
KoProperty::Property* prop = new KoProperty::Property(
it.key().latin1(), // name
d->scriptaction->getOption(it.key(), option->value), // value
option->name, // caption
option->comment, // description
KoProperty::Auto // type
);
d->properties->addProperty(prop);
}
}
//propertySetSwitched();
propertySetReloaded(true);
d->updatesProperties = false;
}
KoProperty::Set* KexiScriptDesignView::propertySet()
{
return d->properties;
}
void KexiScriptDesignView::slotPropertyChanged(KoProperty::Set& /*set*/, KoProperty::Property& property)
{
if(property.isNull())
return;
if(property.name() == "language") {
TQString language = property.value().toString();
kdDebug() << TQString("KexiScriptDesignView::slotPropertyChanged() language=%1").arg(language) << endl;
d->scriptaction->setInterpreterName( language );
// We assume Kross and the HighlightingInterface are using same
// names for the support languages...
d->editor->setHighlightMode( language );
updateProperties();
}
else {
bool ok = d->scriptaction->setOption( property.name(), property.value() );
if(! ok) {
kdWarning() << TQString("KexiScriptDesignView::slotPropertyChanged() unknown property '%1'.").arg(TQString(property.name())) << endl;
return;
}
}
setDirty(true);
}
void KexiScriptDesignView::execute()
{
d->statusbrowser->clear();
TQTime time;
time.start();
d->statusbrowser->append( i18n("Execution of the script \"%1\" started.").arg(d->scriptaction->name()) );
d->scriptaction->activate();
if( d->scriptaction->hadException() ) {
TQString errormessage = d->scriptaction->getException()->getError();
d->statusbrowser->append(TQString("<b>%2</b><br>").arg(TQStyleSheet::escape(errormessage)) );
TQString tracedetails = d->scriptaction->getException()->getTrace();
d->statusbrowser->append( TQStyleSheet::escape(tracedetails) );
long lineno = d->scriptaction->getException()->getLineNo();
if(lineno >= 0)
d->editor->setLineNo(lineno);
}
else {
d->statusbrowser->append( i18n("Successfully executed. Time elapsed: %1ms").arg(time.elapsed()) );
}
}
bool KexiScriptDesignView::loadData()
{
TQString data;
if(! loadDataBlock(data)) {
kexipluginsdbg << "KexiScriptDesignView::loadData(): no DataBlock" << endl;
return false;
}
TQString errMsg;
int errLine;
int errCol;
TQDomDocument domdoc;
bool parsed = domdoc.setContent(data, false, &errMsg, &errLine, &errCol);
if(! parsed) {
kexipluginsdbg << "KexiScriptDesignView::loadData() XML parsing error line: " << errLine << " col: " << errCol << " message: " << errMsg << endl;
return false;
}
TQDomElement scriptelem = domdoc.namedItem("script").toElement();
if(scriptelem.isNull()) {
kexipluginsdbg << "KexiScriptDesignView::loadData(): script domelement is null" << endl;
return false;
}
TQString interpretername = scriptelem.attribute("language");
Kross::Api::Manager* manager = Kross::Api::Manager::scriptManager();
Kross::Api::InterpreterInfo* info = interpretername.isEmpty() ? 0 : manager->getInterpreterInfo(interpretername);
if(info) {
d->scriptaction->setInterpreterName(interpretername);
Kross::Api::InterpreterInfo::Option::Map options = info->getOptions();
Kross::Api::InterpreterInfo::Option::Map::ConstIterator it, end = options.constEnd();
for( it = options.constBegin(); it != end; ++it) {
TQString value = scriptelem.attribute( it.data()->name );
if(! value.isNull()) {
TQVariant v(value);
if( v.cast( it.data()->value.type() ) ) // preserve the TQVariant's type
d->scriptaction->setOption(it.data()->name, v);
}
}
}
d->scriptaction->setCode( scriptelem.text() );
return true;
}
KexiDB::SchemaData* KexiScriptDesignView::storeNewData(const KexiDB::SchemaData& sdata, bool &cancel)
{
KexiDB::SchemaData *s = KexiViewBase::storeNewData(sdata, cancel);
kexipluginsdbg << "KexiScriptDesignView::storeNewData(): new id:" << s->id() << endl;
if(!s || cancel) {
delete s;
return 0;
}
if(! storeData()) {
kdWarning() << "KexiScriptDesignView::storeNewData Failed to store the data." << endl;
//failure: remove object's schema data to avoid garbage
KexiDB::Connection *conn = parentDialog()->mainWin()->project()->dbConnection();
conn->removeObject( s->id() );
delete s;
return 0;
}
return s;
}
tristate KexiScriptDesignView::storeData(bool /*dontAsk*/)
{
kexipluginsdbg << "KexiScriptDesignView::storeData(): " << parentDialog()->partItem()->name() << " [" << parentDialog()->id() << "]" << endl;
TQDomDocument domdoc("script");
TQDomElement scriptelem = domdoc.createElement("script");
domdoc.appendChild(scriptelem);
TQString language = d->scriptaction->getInterpreterName();
scriptelem.setAttribute("language", language);
Kross::Api::InterpreterInfo* info = Kross::Api::Manager::scriptManager()->getInterpreterInfo(language);
if(info) {
Kross::Api::InterpreterInfo::Option::Map defoptions = info->getOptions();
TQMap<TQString, TQVariant>& options = d->scriptaction->getOptions();
TQMap<TQString, TQVariant>::ConstIterator it, end( options.constEnd() );
for( it = options.constBegin(); it != end; ++it) {
if( defoptions.contains(it.key()) ) { // only remember options which the InterpreterInfo knows about...
scriptelem.setAttribute(it.key(), it.data().toString());
}
}
}
TQDomText scriptcode = domdoc.createTextNode(d->scriptaction->getCode());
scriptelem.appendChild(scriptcode);
return storeDataBlock( domdoc.toString() );
}
#include "kexiscriptdesignview.moc"
|