* move script file loading from Animator to AnimationScriptEngine

* don't use camel case includes for kdelibs classes in files which are themselves in kdelibs
* add my copyright to these files since they are approaching 100% code written by me

svn path=/trunk/KDE/kdelibs/; revision=1120250
This commit is contained in:
Aaron J. Seigo 2010-04-28 17:51:46 +00:00
parent 0cf62abb09
commit 736c381bbe
3 changed files with 40 additions and 25 deletions

View File

@ -1,5 +1,6 @@
/* /*
* Copyright (C) 2010 Adenilson Cavalcanti <cavalcantii@gmail.com> * Copyright 2010 Aaron Seigo <aseigo@kde.org>
* Copyright 2010 Adenilson Cavalcanti <cavalcantii@gmail.com>
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as * it under the terms of the GNU Library General Public License as
@ -24,6 +25,11 @@
#include "animationscriptengine_p.h" #include "animationscriptengine_p.h"
#include <QFile>
#include <QTextStream>
#include <kdebug.h>
namespace Plasma namespace Plasma
{ {
@ -78,6 +84,33 @@ QScriptEngine *globalEngine()
return inst; return inst;
} }
bool loadScript(const QString &path)
{
QFile file(path);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
kError() << "failed to open script file" << path;
return false;
}
QTextStream buffer(&file);
QString tmp(buffer.readAll());
QScriptEngine *engine = AnimationScriptEngine::globalEngine();
QScriptValue def(engine->evaluate(tmp, path));
if (engine->hasUncaughtException()) {
const QScriptValue error = engine->uncaughtException();
QString file = error.property("fileName").toString();
const QString failureMsg = QString("Error in %1 on line %2.\n%3")
.arg(file)
.arg(error.property("lineNumber").toString())
.arg(error.toString());
kError() << failureMsg;
return false;
}
return true;
}
} // namespace AnimationEngine } // namespace AnimationEngine
} // namespace Plasma } // namespace Plasma

View File

@ -1,5 +1,6 @@
/* /*
* Copyright (C) 2010 Adenilson Cavalcanti <cavalcantii@gmail.com> * Copyright 2010 Aaron Seigo <aseigo@kde.org>
* Copyright 2010 Adenilson Cavalcanti <cavalcantii@gmail.com>
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as * it under the terms of the GNU Library General Public License as
@ -37,6 +38,7 @@ void clearAnimations();
bool isAnimationRegistered(const QString &anim); bool isAnimationRegistered(const QString &anim);
QScriptEngine* globalEngine(); QScriptEngine* globalEngine();
QScriptValue animation(const QString &anim); QScriptValue animation(const QString &anim);
bool loadScript(const QString &path);
} }

View File

@ -18,10 +18,7 @@
#include "animator.h" #include "animator.h"
#include "private/animator_p.h" #include "private/animator_p.h"
#include <QFile> #include <kdebug.h>
#include <QTextStream>
#include <KDebug>
#include "animations/animation.h" #include "animations/animation.h"
#include "animations/animationscriptengine_p.h" #include "animations/animationscriptengine_p.h"
@ -162,30 +159,13 @@ Plasma::Animation *Animator::create(const QString &anim, QObject *parent)
return 0; return 0;
} }
QFile file(path); if (!AnimationScriptEngine::loadScript(path)) {
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
kError() << "failed to open script file" << path;
return 0;
}
QTextStream buffer(&file);
QString tmp(buffer.readAll());
QScriptEngine *engine = AnimationScriptEngine::globalEngine();
QScriptValue def(engine->evaluate(tmp, path));
if (engine->hasUncaughtException()) {
const QScriptValue error = engine->uncaughtException();
QString file = error.property("fileName").toString();
const QString failureMsg = QString("Error in %1 on line %2.\n%3")
.arg(file)
.arg(error.property("lineNumber").toString())
.arg(error.toString());
kError() << failureMsg;
return 0; return 0;
} }
if (!AnimationScriptEngine::isAnimationRegistered(anim)) { if (!AnimationScriptEngine::isAnimationRegistered(anim)) {
kError() << "successfully loaded script file" << path << ", but did not get animation object for" << anim; kError() << "successfully loaded script file" << path << ", but did not get animation object for" << anim;
return false;
} }
} }