ScriptInterface.hh 10.9 KB
Newer Older
1
/*===========================================================================*\
Jan Möbius's avatar
Jan Möbius committed
2 3
*                                                                            *
*                              OpenFlipper                                   *
Martin Schultz's avatar
Martin Schultz committed
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
 *           Copyright (c) 2001-2015, RWTH-Aachen University                 *
 *           Department of Computer Graphics and Multimedia                  *
 *                          All rights reserved.                             *
 *                            www.openflipper.org                            *
 *                                                                           *
 *---------------------------------------------------------------------------*
 * This file is part of OpenFlipper.                                         *
 *---------------------------------------------------------------------------*
 *                                                                           *
 * Redistribution and use in source and binary forms, with or without        *
 * modification, are permitted provided that the following conditions        *
 * are met:                                                                  *
 *                                                                           *
 * 1. Redistributions of source code must retain the above copyright notice, *
 *    this list of conditions and the following disclaimer.                  *
 *                                                                           *
 * 2. Redistributions in binary form must reproduce the above copyright      *
 *    notice, this list of conditions and the following disclaimer in the    *
 *    documentation and/or other materials provided with the distribution.   *
 *                                                                           *
 * 3. Neither the name of the copyright holder nor the names of its          *
 *    contributors may be used to endorse or promote products derived from   *
 *    this software without specific prior written permission.               *
 *                                                                           *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       *
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A           *
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,  *
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,       *
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR        *
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    *
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING      *
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS        *
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.              *
Jan Möbius's avatar
Jan Möbius committed
39
*                                                                            *
40 41 42
\*===========================================================================*/

/*===========================================================================*\
Jan Möbius's avatar
Jan Möbius committed
43 44 45 46 47
*                                                                            *
*   $Revision$                                                       *
*   $LastChangedBy$                                                *
*   $Date$                     *
*                                                                            *
48
\*===========================================================================*/
Jan Möbius's avatar
 
Jan Möbius committed
49

50 51
#ifndef SCRIPTINTERFACE_HH
#define SCRIPTINTERFACE_HH
Jan Möbius's avatar
 
Jan Möbius committed
52 53 54

#include <QtScript/QScriptEngine>

Jan Möbius's avatar
Jan Möbius committed
55 56 57 58 59
/** \file ScriptInterface.hh
*
* Interface for controlling OpenFlippers scripting system. \ref scriptInterfacePage
*/

Jan Möbius's avatar
Jan Möbius committed
60
/** \brief Interface for all Plugins which provide scriptable Functions.
Jan Möbius's avatar
Jan Möbius committed
61 62 63 64 65
 *
 * \ref scriptInterfacePage "Detailed description"
 * \n
 *
 *
Jan Möbius's avatar
 
Jan Möbius committed
66 67
*/
class ScriptInterface {
68

69 70 71 72
   public:
      /// Destructor
      virtual ~ScriptInterface() {};

73

Jan Möbius's avatar
Jan Möbius committed
74 75 76 77
  //===========================================================================
  /** @name Script execution
   * @{ */
  //===========================================================================
78

Jan Möbius's avatar
Jan Möbius committed
79
  signals:
Jan Möbius's avatar
 
Jan Möbius committed
80
    /** Emit this signal to execute a script
Jan Möbius's avatar
Jan Möbius committed
81 82 83 84 85 86 87 88 89 90 91
     *
     * @param _script The script to execute
     */
    virtual void executeScript( QString _script  ) {};

    /** Emit this signal to execute a script from a file
      * @param _filename Filename of the script to execute
     */
    virtual void executeFileScript( QString _filename  ) {};

  /** @} */
92

Jan Möbius's avatar
Jan Möbius committed
93 94 95 96 97 98
  //===========================================================================
  /** @name Function Information
  * @{ */
  //===========================================================================

  signals:
99

Jan Möbius's avatar
 
Jan Möbius committed
100
    /** With this signal you can get a list of all available scripting functions
Jan Möbius's avatar
Jan Möbius committed
101
      * @param _functions All available functions
Jan Möbius's avatar
 
Jan Möbius committed
102
     */
Jan Möbius's avatar
Jan Möbius committed
103
    virtual void getAvailableFunctions(QStringList& _functions) { };
104 105 106 107 108 109 110 111

    /** With this signal you can get descriptions about a given function if available
     *
     *   @param _function name of the function (e.g. plugin.function)
     *   @param _description a description for the function
     *   @param _parameters list of names for the parameters
     *   @param _descriptions list of descriptions for the parameters (_descriptions[i] corresponds to _parameters[i])
     */
Jan Möbius's avatar
Jan Möbius committed
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
    virtual void getDescription(QString _function, QString& _description, QStringList& _parameters, QStringList& _descriptions) { };

  /** @} */

  //===========================================================================
  /** @name Scripting History
  * @{ */
  //===========================================================================
  signals:

       /** \brief Emit this signal if a scriptable function is executed
         *
         * e.g. selectAllVertices( ObjectId )
         *
         * If you execute this function, you can append your functions to the script
         * execution history in OpenFlipper. In this history OpenFlipper records operations
         * that are scriptable. Later this list can be executed again and therefore
         * reruns the recorded operations.
         *
         * The following example code shows a function called translate in a plugin,
         * that gets an ObjectId as a parameter. The plugin which emits this function
         * will automatically be added in the history ( e.g. \code move.translate( ObjectId , Vector(1 , 0 , 0 ) ) \endcode )
         * \code
         *   emit scriptInfo( "translate( ObjectId , Vector(1 , 0 , 0 ) )" );
         * \endcode
         *
         * @param _functionWithParameters The called function with all parameters
         */
      virtual void scriptInfo( QString _functionWithParameters  ) {};


  /** @} */

  //===========================================================================
  /** @name Others
  * @{ */
  //===========================================================================

  signals:
      /** Emit this signal to get a reference to the core scripting engine
        *
        * @param _engine The scripting Engine
        */
      virtual void getScriptingEngine( QScriptEngine*& _engine  ) {};

  /** @} */


  //===========================================================================
  /** @name Slots for a scripting control plugin like Plugin-Script
    * @{ */
  //===========================================================================
164

Jan Möbius's avatar
 
Jan Möbius committed
165
  private slots:
166

Jan Möbius's avatar
 
Jan Möbius committed
167
    /** Slot for a scripting plugin. Gets the Script and executes it.
168
     *
Jan Möbius's avatar
 
Jan Möbius committed
169
     * @param _pluginName Name of the plugin that executed a scriptable function
Jan Möbius's avatar
Jan Möbius committed
170
     * @param _functionWithParameters Name with parameters (values!) of the function to call.
Jan Möbius's avatar
 
Jan Möbius committed
171
     */
Jan Möbius's avatar
Jan Möbius committed
172
    virtual void slotScriptInfo( QString _pluginName , QString _functionWithParameters  ) {};
173 174


Jan Möbius's avatar
Jan Möbius committed
175
    /** Slot for a scripting plugin. Gets the Script as QString and executes it.
176
     *
Jan Möbius's avatar
Jan Möbius committed
177
     * @param _script Script to execute as a QString
Jan Möbius's avatar
 
Jan Möbius committed
178
     */
Jan Möbius's avatar
Jan Möbius committed
179
    virtual void slotExecuteScript( QString _script ) {};
180 181


Jan Möbius's avatar
Jan Möbius committed
182
    /** Slot for a scripting plugin. Gets the Script as a filename, opens and executes it.
Jan Möbius's avatar
 
Jan Möbius committed
183
     */
Jan Möbius's avatar
Jan Möbius committed
184
    virtual void slotExecuteFileScript( QString _filename ) {};
185

Jan Möbius's avatar
Jan Möbius committed
186
  /** @} */
187

Jan Möbius's avatar
 
Jan Möbius committed
188 189
};

Jan Möbius's avatar
Jan Möbius committed
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
/** \page scriptInterfacePage Scripting Interface
\n
\image html ScriptInterface.png
\n

OpenFlipper uses QTs scripting system to provide scripting functions to the user and to the plugins.
It also includes a batch mode where OpenFlipper is started without an user interface. All plugins
which support this mode can than be controlled by command line supplied batch scripts without any user
interaction.

The ScriptingInterface has several functions to support scripting.

<ul>
<li> Execute scripts which are provided in a simple QString (ScriptInterface::executeScript()).
     \code
       emit executeScript( "translate( 5 , Vector(1 , 0 , 0 ) )" );
     \endcode
     This will translate The object with id 6 by the given vector.
<li> Execute scripts which are provided in a file (ScriptInterface::executeFileScript()).
     \code
       emit executeFileScript( "/home/user/script.ofs" );
     \endcode
<li> If a scriptable slot is executed, it can add an entry to OpenFlippers script history. This history can
     later be used to rerun a process in batch mode ( ScriptInterface::scriptInfo() ).

     The following example code shows a function called translate in a plugin,
     that gets an ObjectId as a parameter. The plugin which emits this function
     will automatically be added in the history ( e.g. <tt> move.translate( ObjectId , Vector(1 , 0 , 0 ) ) </tt> )

     \code
       emit scriptInfo( "translate( ObjectId , Vector(1 , 0 , 0 ) )" );
     \endcode
<li> Get Information about scriptable functions ( ScriptInterface::getAvailableFunctions(),ScriptInterface::getDescription() ).

     The first function ( ScriptInterface::getAvailableFunctions() ) can be used to get a QStringlist of all available
     functions. Each string is of the form <tt>pluginname.functionname</tt>
     \code
       // Update list of available functions
       QStringList completeList;
       emit getAvailableFunctions( completeList  );
     \endcode

     The second function ( ScriptInterface::getDescription() ) can be used to get information about a function, if it was
     provided by the developer.
</ul>


To use the ScriptInterface:
<ul>
<li> include ScriptInterface.hh in your plugins header file
<li> derive your plugin from the class ScriptInterface
<li> add Q_INTERFACES(ScriptInterface) to your plugin class
<li> And add the signals or slots you want to use to your plugin class (You don't need to implement all of them)
</ul>

An easier interface to call functions is available by the \ref RPCInterfacePage "RPC Interface". Additionally it is
246
possible to connect signals and slots across plugins via the  \ref pluginConnectionInterfacePage "Plugin Connection Interface"
Jan Möbius's avatar
Jan Möbius committed
247 248 249 250

*/


251 252
Q_DECLARE_INTERFACE(ScriptInterface,"OpenFlipper.ScriptInterface/1.1")

Jan Möbius's avatar
 
Jan Möbius committed
253
#endif // SCRIPTINTERFACE_HH