Commit d92aa726 authored by Jan Möbius's avatar Jan Möbius

Added Help function to scripting

git-svn-id: http://www.openflipper.org/svnrepo/OpenFlipper/branches/Free@12678 383ad7c9-94d9-4d36-a494-682f7c89f535
parent 0ae7b69e
......@@ -250,7 +250,7 @@ Core::init() {
Qt::AlignBottom | Qt::AlignLeft , Qt::white);
}
coreWidget_ = new CoreWidget(viewModes_ , plugins, coreSlots_);
coreWidget_ = new CoreWidget(viewModes_ , plugins_, coreSlots_);
connect(coreWidget_, SIGNAL(clearAll()) , this, SLOT(clearAll()));
connect(coreWidget_, SIGNAL(loadMenu()) , this, SLOT(loadObject()));
......@@ -434,6 +434,12 @@ Core::init() {
scriptEngine_.globalObject().setProperty("printToFile", printToFileFunc);
scriptingFunctions_.push_back( "-.printToFile(QString,QString)" );
// Register help function :
QScriptValue helpFunc = scriptEngine_.newFunction(helpFunction);
helpFunc.setProperty("core",scriptEngine_.newQObject(this));
scriptEngine_.globalObject().setProperty("help", helpFunc);
scriptingFunctions_.push_back( "-.help(QString)" );
// Register IdList Type to scripting Engine
qScriptRegisterSequenceMetaType< IdList >(&scriptEngine_);
......@@ -614,13 +620,13 @@ Core::init() {
Core::~Core()
{
for ( uint i = 0 ; i < plugins.size() ; ++i ){
BaseInterface* basePlugin = qobject_cast< BaseInterface * >(plugins[i].plugin);
for ( uint i = 0 ; i < plugins_.size() ; ++i ){
BaseInterface* basePlugin = qobject_cast< BaseInterface * >(plugins_[i].plugin);
// Dont call exit if we cannot get the Plugin
if ( basePlugin )
if ( checkSlot( plugins[i].plugin , "exit()" ) )
QMetaObject::invokeMethod(plugins[i].plugin, "exit", Qt::DirectConnection);
if ( checkSlot( plugins_[i].plugin , "exit()" ) )
QMetaObject::invokeMethod(plugins_[i].plugin, "exit", Qt::DirectConnection);
}
// Delete the objectRoot if it was constructed
......@@ -1076,11 +1082,11 @@ Core::writeOnExit() {
}
// Call exit for all plugins
for (uint i = 0 ; i < plugins.size() ; ++i) {
BaseInterface* basePlugin = qobject_cast< BaseInterface * >(plugins[i].plugin);
for (uint i = 0 ; i < plugins_.size() ; ++i) {
BaseInterface* basePlugin = qobject_cast< BaseInterface * >(plugins_[i].plugin);
if ( basePlugin )
if ( checkSlot( plugins[i].plugin , "exit()" ) )
QMetaObject::invokeMethod(plugins[i].plugin, "exit", Qt::DirectConnection);
if ( checkSlot( plugins_[i].plugin , "exit()" ) )
QMetaObject::invokeMethod(plugins_[i].plugin, "exit", Qt::DirectConnection);
}
}
......@@ -1185,9 +1191,9 @@ void Core::slotSetSlotDescription(QString _slotName, QString _slotDescrip
//find plugin
PluginInfo* pluginInfo = 0;
for (uint i=0; i < plugins.size(); i++)
if (plugins[i].plugin == sender())
pluginInfo = &plugins[i];
for (uint i=0; i < plugins_.size(); i++)
if (plugins_[i].plugin == sender())
pluginInfo = &plugins_[i];
if (pluginInfo == 0){
emit log(LOGERR, tr("Unable to set slot-description. Plugin not found!"));
......@@ -1263,9 +1269,9 @@ void Core::slotGetDescription(QString _function, QString& _fnDescript
//find plugin
PluginInfo* pluginInfo = 0;
for (uint i=0; i < plugins.size(); i++)
if (plugins[i].rpcName == pluginName)
pluginInfo = &plugins[i];
for (uint i=0; i < plugins_.size(); i++)
if (plugins_[i].rpcName == pluginName)
pluginInfo = &plugins_[i];
if (pluginInfo == 0){
emit log(LOGERR, tr("Unable to get slot-description. Plugin not found!"));
......@@ -1395,13 +1401,13 @@ INIFile ini;
ini.add_entry( "Core" , "VersionLinux" , OpenFlipper::Options::coreVersion() );
//add pluginVersions
for (uint i=0; i < plugins.size(); i++){
ini.add_section( plugins[i].name );
for (uint i=0; i < plugins_.size(); i++){
ini.add_section( plugins_[i].name );
if ( OpenFlipper::Options::isWindows() )
ini.add_entry( plugins[i].name , "VersionWindows" , plugins[i].version );
ini.add_entry( plugins_[i].name , "VersionWindows" , plugins_[i].version );
else
ini.add_entry( plugins[i].name , "VersionLinux" , plugins[i].version );
ini.add_entry( plugins_[i].name , "VersionLinux" , plugins_[i].version );
}
ini.disconnect();
......@@ -1434,6 +1440,8 @@ void Core::setDescriptions(){
this, SLOT(slotSetSlotDescription(QString,QString,QStringList,QStringList)) );
emit slotSetSlotDescriptionGlobalFunction("printToFile(QString,QString)", tr("Print a message to a file"), QStringList(QString("Filename;Values").split(";")), QStringList(QString("Filename to print into;Arbitrary number of arguments").split(";")));
emit slotSetSlotDescriptionGlobalFunction("help(QString)", tr("Print help about something"), QStringList("Help Entry"), QStringList("help about what?"));
emit setSlotDescription("deleteObject(int)", tr("Delete an object from the scene."), QStringList("ObjectId"), QStringList(tr("Id of the object to delete")));
emit setSlotDescription("updateView()", tr("Redraw the contents of the viewer."), QStringList(), QStringList());
......
......@@ -1104,9 +1104,12 @@ private slots:
* @{ */
//===========================================================================
public :
const std::vector<PluginInfo> plugins() const {return plugins_; };
private:
/// List of all loaded plugins
std::vector<PluginInfo> plugins;
/// List of all loaded plugins_
std::vector<PluginInfo> plugins_;
/// Index of Plugins toolbox widget
int toolboxindex_;
......@@ -1404,6 +1407,9 @@ QScriptValue myPrintFunction(QScriptContext *context, QScriptEngine *engine);
/// Special print function for sending output to a file
QScriptValue printToFileFunction(QScriptContext *context, QScriptEngine *engine);
/// Function to print help about scripting functions
QScriptValue helpFunction(QScriptContext *context, QScriptEngine *engine);
//=============================================================================
#endif // MVIEWWIDGET_HH defined
//=============================================================================
......
......@@ -378,12 +378,12 @@ void Core::slotCrossPluginConnect( QString _pluginName1, const char* _signal, QS
QObject* plugin1 = 0;
QObject* plugin2 = 0;
for ( int i = 0 ; i < (int)plugins.size(); ++i ) {
if ( plugins[i].rpcName == _pluginName1 ) {
plugin1 = plugins[i].plugin;
for ( int i = 0 ; i < (int)plugins_.size(); ++i ) {
if ( plugins_[i].rpcName == _pluginName1 ) {
plugin1 = plugins_[i].plugin;
}
if ( plugins[i].rpcName == _pluginName2 ) {
plugin2 = plugins[i].plugin;
if ( plugins_[i].rpcName == _pluginName2 ) {
plugin2 = plugins_[i].plugin;
}
}
......
......@@ -533,7 +533,7 @@ void Core::loadPlugins()
emit pluginsInitialized();
emit log(LOGOUT,tr("Loaded %n Plugin(s)","",plugins.size()) );
emit log(LOGOUT,tr("Loaded %n Plugin(s)","",plugins_.size()) );
}
/** @brief slot for loading Plugins
......@@ -631,7 +631,7 @@ void Core::slotShowPlugins(){
while (ret == 0){
PluginDialog* dialog = new PluginDialog(plugins, coreWidget_);
PluginDialog* dialog = new PluginDialog(plugins_, coreWidget_);
//connect signals
connect(dialog, SIGNAL(unloadPlugin(QString)), this, SLOT(unloadPlugin(QString)));
......@@ -649,26 +649,26 @@ void Core::slotShowPlugins(){
* @param name plugin name
*/
void Core::unloadPlugin(QString name){
for (uint i=0; i < plugins.size(); i++)
if (plugins[i].rpcName == name){
if ( checkSlot( plugins[i].plugin , "exit()" ) )
QMetaObject::invokeMethod(plugins[i].plugin, "exit", Qt::DirectConnection);
for (uint i=0; i < plugins_.size(); i++)
if (plugins_[i].rpcName == name){
if ( checkSlot( plugins_[i].plugin , "exit()" ) )
QMetaObject::invokeMethod(plugins_[i].plugin, "exit", Qt::DirectConnection);
//remove toolbox widget
QString name_nospace = name;
name_nospace.remove(" ");
if ( coreWidget_->viewModes_[0]->visibleToolboxes.contains(name_nospace) )
coreWidget_->viewModes_[0]->visibleToolboxes.removeAt(coreWidget_->viewModes_[0]->visibleToolboxes.indexOf(name_nospace));
for ( uint j = 0 ; j < plugins[i].toolboxWidgets.size() ; ++j )
if (plugins[i].toolboxWidgets[j].second ){
plugins[i].toolboxWidgets[j].second->setVisible(false);
delete plugins[i].toolboxWidgets[j].second;
for ( uint j = 0 ; j < plugins_[i].toolboxWidgets.size() ; ++j )
if (plugins_[i].toolboxWidgets[j].second ){
plugins_[i].toolboxWidgets[j].second->setVisible(false);
delete plugins_[i].toolboxWidgets[j].second;
if( plugins[i].toolboxIcons[j] != 0 )
delete plugins[i].toolboxIcons[j];
if( plugins_[i].toolboxIcons[j] != 0 )
delete plugins_[i].toolboxIcons[j];
}
plugins.erase(plugins.begin() + i);
plugins_.erase(plugins_.begin() + i);
emit log(LOGOUT,tr("Unloaded Plugin :\t\t %1").arg( name) );
......@@ -738,14 +738,14 @@ void Core::loadPlugin(QString filename, bool silent, QString& _licenseErrors, QO
}
//Check if plugin is already loaded
for (uint k=0; k < plugins.size(); k++){
for (uint k=0; k < plugins_.size(); k++){
QString name_nospace = basePlugin->name();
name_nospace.remove(" ");
if (plugins[k].name == name_nospace){
if (plugins_[k].name == name_nospace){
if (silent || OpenFlipper::Options::nogui() ){ //dont load the plugin
emit log(LOGWARN, tr("\t\t\t Already loaded from %1").arg( plugins[k].path) );
emit log(LOGWARN, tr("\t\t\t Already loaded from %1").arg( plugins_[k].path) );
emit log(LOGOUT,"================================================================================");
return;
}else{ //ask the user
......@@ -753,12 +753,12 @@ void Core::loadPlugin(QString filename, bool silent, QString& _licenseErrors, QO
tr("Plugin already loaded"),
tr("A Plugin with the same name was already loaded from %1.\n"
"You can only load the new plugin if you unload the existing one first.\n\n"
"Do you want to unload the existing plugin first?").arg( plugins[k].path),
"Do you want to unload the existing plugin first?").arg( plugins_[k].path),
QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
if (ret == QMessageBox::Yes)
unloadPlugin(plugins[k].name);
unloadPlugin(plugins_[k].name);
else{
emit log(LOGWARN, tr("\t\t\t Already loaded from %1.").arg( plugins[k].path));
emit log(LOGWARN, tr("\t\t\t Already loaded from %1.").arg( plugins_[k].path));
emit log(LOGOUT,"================================================================================");
return;
}
......@@ -2065,7 +2065,7 @@ void Core::loadPlugin(QString filename, bool silent, QString& _licenseErrors, QO
//========================================================================================
//========================================================================================
plugins.push_back(info);
plugins_.push_back(info);
// Initialize Plugin
if ( basePlugin ) {
......
......@@ -62,8 +62,8 @@
void Core::slotPluginExists( QString _pluginName , bool& _exists ) {
for ( int i = 0 ; i < (int)plugins.size(); ++i ) {
if ( plugins[i].rpcName == _pluginName ) {
for ( int i = 0 ; i < (int)plugins_.size(); ++i ) {
if ( plugins_[i].rpcName == _pluginName ) {
_exists = true;
return;
}
......@@ -76,8 +76,8 @@ void Core::slotFunctionExists( QString _pluginName , QString _functionName , boo
//Find plugin
int plugin = -1;
for ( int i = 0 ; i < (int)plugins.size(); ++i ) {
if ( plugins[i].rpcName == _pluginName ) {
for ( int i = 0 ; i < (int)plugins_.size(); ++i ) {
if ( plugins_[i].rpcName == _pluginName ) {
plugin = i;
break;
}
......@@ -88,15 +88,15 @@ void Core::slotFunctionExists( QString _pluginName , QString _functionName , boo
return;
}
_exists = plugins[plugin].rpcFunctions.contains(_functionName);
_exists = plugins_[plugin].rpcFunctions.contains(_functionName);
}
void Core::slotCall( QString _pluginName , QString _functionName , bool& _success ) {
//Find plugin
int plugin = -1;
for ( int i = 0 ; i < (int)plugins.size(); ++i ) {
if ( plugins[i].rpcName == _pluginName ) {
for ( int i = 0 ; i < (int)plugins_.size(); ++i ) {
if ( plugins_[i].rpcName == _pluginName ) {
plugin = i;
break;
}
......@@ -108,7 +108,7 @@ void Core::slotCall( QString _pluginName , QString _functionName , bool& _succes
return;
}
if ( !plugins[plugin].rpcFunctions.contains(_functionName) ) {
if ( !plugins_[plugin].rpcFunctions.contains(_functionName) ) {
_success = false;
emit log(LOGERR, tr("Unable to call function from Plugin : ") + _pluginName);
emit log(LOGERR, tr("Function ") + _functionName + tr(" not found!"));
......
......@@ -221,8 +221,8 @@ void Core::addToolbox(QString _name ,QWidget* _widget) {
int id = -1;
// Find the plugin which added this Toolbox
for ( uint i = 0 ; i < plugins.size(); ++i ) {
if ( plugins[i].plugin == sender() ) {
for ( uint i = 0 ; i < plugins_.size(); ++i ) {
if ( plugins_[i].plugin == sender() ) {
id = i;
break;
}
......@@ -230,8 +230,8 @@ void Core::addToolbox(QString _name ,QWidget* _widget) {
// Find the scripting plugin because we assign this toolBox to it as we did not find the original sender
if ( id == -1 ) {
for ( uint i = 0 ; i < plugins.size(); ++i ) {
if ( plugins[i].name == "Scripting" ) {
for ( uint i = 0 ; i < plugins_.size(); ++i ) {
if ( plugins_[i].name == "Scripting" ) {
id = i;
break;
}
......@@ -244,8 +244,8 @@ void Core::addToolbox(QString _name ,QWidget* _widget) {
}
}
plugins[id].toolboxWidgets.push_back( std::pair< QString,QWidget* >( _name , _widget) );
plugins[id].toolboxIcons.push_back( 0 );
plugins_[id].toolboxWidgets.push_back( std::pair< QString,QWidget* >( _name , _widget) );
plugins_[id].toolboxIcons.push_back( 0 );
// add widget name to viewMode 'all'
if ( !viewModes_[0]->visibleToolboxes.contains(_name) ){
......@@ -262,8 +262,8 @@ void Core::addToolbox(QString _name ,QWidget* _widget, QIcon* _icon) {
int id = -1;
// Find the plugin which added this Toolbox
for ( uint i = 0 ; i < plugins.size(); ++i ) {
if ( plugins[i].plugin == sender() ) {
for ( uint i = 0 ; i < plugins_.size(); ++i ) {
if ( plugins_[i].plugin == sender() ) {
id = i;
break;
}
......@@ -271,8 +271,8 @@ void Core::addToolbox(QString _name ,QWidget* _widget, QIcon* _icon) {
// Find the scripting plugin because we assign this toolBox to it as we did not find the original sender
if ( id == -1 ) {
for ( uint i = 0 ; i < plugins.size(); ++i ) {
if ( plugins[i].name == "Scripting" ) {
for ( uint i = 0 ; i < plugins_.size(); ++i ) {
if ( plugins_[i].name == "Scripting" ) {
id = i;
break;
}
......@@ -285,8 +285,8 @@ void Core::addToolbox(QString _name ,QWidget* _widget, QIcon* _icon) {
}
}
plugins[id].toolboxWidgets.push_back( std::pair< QString,QWidget* >( _name , _widget) );
plugins[id].toolboxIcons.push_back( _icon );
plugins_[id].toolboxWidgets.push_back( std::pair< QString,QWidget* >( _name , _widget) );
plugins_[id].toolboxIcons.push_back( _icon );
// add widget name to viewMode 'all'
if ( !viewModes_[0]->visibleToolboxes.contains(_name) ){
......@@ -352,3 +352,39 @@ QScriptValue printToFileFunction(QScriptContext *context, QScriptEngine *engine)
return engine->undefinedValue();
}
QScriptValue helpFunction(QScriptContext *context, QScriptEngine *engine)
{
if ( context->argumentCount() != 1 ) {
context->throwError( QScriptContext::SyntaxError, "Error! helpFunction needs one argument" );
return engine->undefinedValue();
}
QString helpString = context->argument(0).toString();
// Get the corewidget poiter ( Set in Core.cc )
QScriptValue calleeData = context->callee().property("core");
Core *core = qobject_cast<Core*>(calleeData.toQObject());
const std::vector<PluginInfo> plugins = core->plugins();
for (unsigned int i=0; i < plugins.size(); i++) {
if (plugins[i].rpcName == helpString) {
core->scriptLogFunction( "=======================================================\n" );
core->scriptLogFunction( "Found Plugin \"" + plugins[i].name + "\" \n" );
core->scriptLogFunction( "Description: " + plugins[i].description + " \n");
core->scriptLogFunction( "=======================================================\n" );
core->scriptLogFunction( "Scripting functions: \n");
for ( int j = 0 ; j < plugins[i].rpcFunctions.size() ; ++j ) {
core->scriptLogFunction( plugins[i].rpcFunctions[j]+"\n");
}
core->scriptLogFunction( "\n\n");
}
}
return engine->undefinedValue();
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment