TypeInterface.hh 8.72 KB
Newer Older
1
/*===========================================================================*\
Jan Möbius's avatar
Jan Möbius committed
2
3
*                                                                            *
*                              OpenFlipper                                   *
Jan Möbius's avatar
Jan Möbius committed
4
*      Copyright (C) 2001-2011 by Computer Graphics Group, RWTH Aachen       *
Jan Möbius's avatar
Jan Möbius committed
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
*                           www.openflipper.org                              *
*                                                                            *
*--------------------------------------------------------------------------- *
*  This file is part of OpenFlipper.                                         *
*                                                                            *
*  OpenFlipper is free software: you can redistribute it and/or modify       *
*  it under the terms of the GNU Lesser General Public License as            *
*  published by the Free Software Foundation, either version 3 of            *
*  the License, or (at your option) any later version with the               *
*  following exceptions:                                                     *
*                                                                            *
*  If other files instantiate templates or use macros                        *
*  or inline functions from this file, or you compile this file and          *
*  link it with other files to produce an executable, this file does         *
*  not by itself cause the resulting executable to be covered by the         *
*  GNU Lesser General Public License. This exception does not however        *
*  invalidate any other reasons why the executable file might be             *
*  covered by the GNU Lesser General Public License.                         *
*                                                                            *
*  OpenFlipper 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 Lesser General Public License for more details.                       *
*                                                                            *
*  You should have received a copy of the GNU LesserGeneral Public           *
*  License along with OpenFlipper. If not,                                   *
*  see <http://www.gnu.org/licenses/>.                                       *
*                                                                            *
33
34
35
\*===========================================================================*/

/*===========================================================================*\
Jan Möbius's avatar
Jan Möbius committed
36
37
38
39
40
*                                                                            *
*   $Revision$                                                       *
*   $LastChangedBy$                                                *
*   $Date$                     *
*                                                                            *
41
42
43
44
45
46
47
48
\*===========================================================================*/


#ifndef TYPEINTERFACE_HH
#define TYPEINTERFACE_HH

#include <OpenFlipper/common/Types.hh>

Jan Möbius's avatar
Jan Möbius committed
49
50
51
52
53
54
/** \file TypeInterface.hh
*
* Interface for registering types in OpenFlipper. \ref typeInterfacePage
*/


55
 /** \brief Interface class for type definitions
Jan Möbius's avatar
Jan Möbius committed
56
57
58
59
60
61
62
63
64
 *
 * \n
 * \ref typeInterfacePage "Detailed description"
 * \n
 *
 * This interface is used to register new types in OpenFlipper. The type plugins are loaded before all other plugins.
 * They have only the registerType function which registers the type to the core and a function to add new
 * objects at runtime.
 */
65
66
67

class TypeInterface {

68
  signals:
69

70
    /** \brief Emit this signal if an empty object has been created
Jan Möbius's avatar
Jan Möbius committed
71
72
73
74
     *
     * @param _id Id of the added object
     */
    virtual void emptyObjectAdded( int _id ) {};
75

76
77
78
  public:

    /// Destructor
79
    virtual ~TypeInterface() {};
80

81
  public slots:
82
83

    virtual bool registerType() = 0;
84
85
86
87
88
89
90
91
92
93
    
    /** \brief Create an empty object
       *
       * When this slot is called you have to create an object of your supported type.
       * @return Id of the new Object
       */
    virtual int addEmpty() = 0;
    
    /** \brief Return your supported object type( e.g. DATA_TRIANGLE_MESH )
    *
Jan Möbius's avatar
Jan Möbius committed
94
95
    * The function is used from addEmpty in the core to check if your plugin can create an object of
    * a given dataType. If so, your addEmpty function will be invoked to create it.
96
97
    */
    virtual DataType supportedType() = 0;
98
99
100
101
102
103
104
105
106

    /** \brief This slot should be implemented in a TypePlugin to generate type specific backups
     *
     * @param _id Id of the added object
     * @param _name name of the backup
     * @param _type the type of backup that needs to be done
     */
    virtual void generateBackup( int _id, QString _name, UpdateType _type ){};

107
108
};

Jan Möbius's avatar
Jan Möbius committed
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

/** \page typeInterfacePage Type Interface
\n
\image html TypeInterface.png
\n

This interface is used to register new types in OpenFlipper. The type plugins are loaded before all other plugins.
They have only the registerType function which registers the type to the core and a function to create objects
of the new type. The type itself has to be defined in the ObjectTypes subdirectory.

 \section TypeExample Example using custom data types

 Adding a custom data type to %OpenFlipper needs the following requirements in order to work:

 - The definition of the typeId constant, e.g.:
 \code
 #define DATA_MY_DATA typeId("MyDataType")
 \endcode
 Note: Your data type is then referenced as DATA_MY_DATA during runtime.
 - The specification of an object class for your object type that is derived from
   BaseObjectData.
 - The specification of helper functions (usually within the PluginFunctions namespace)
   allowing the casting from BaseObjectData to your object type class.

 See detailed examples for each of the three points for already existing data types in
 OpenFlipperRoot/ObjectTypes.

 Once the object class is specified, the type plugin will be responsible for its handling including

 - Adding new objects to the scenegraph
 - Setting the initial name of an object
 - Etc.

 So, type plugins usually consist of only few lines of code. Here an example of
 a type plugin handling an example object data type as mentioned above:

 \code
 bool MyDataTypePlugin::registerType() {

     addDataType("MyDataType",tr("MyDataType"));
     setTypeIcon( "MyDataType", "myDataType.png");

     return true;
 }

 int MyDataTypePlugin::addEmpty() {

     // Create new object
     MyObject* object = new MyObject();

     object->setName( QString("My Object %1.mob").arg(objectCount) );
     object->update();
     object->show();

     // Tell core that an object has been added
     emit emptyObjectAdded ( object->id() );

     return object->id();
 }
 \endcode

 Now, each time a plugin emits addEmptyObject(DATA_MY_DATA), the addEmpty() function will
 add the object to the scenegraph and return the newly created object's id.

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
 \section Backups
 Backups are very specific to the underlying data structures of certain types. Therefore the type
 plugins also manage backups. Backups itself are managed by perObjectData objects based on
 the BackupData class.
 When the slot gets called, we first have to check, if an BackupData is already available. If not,
 we create one. Than the backup is generated and passed to the backup data attached to the object.

 You have to derive your backups from the BaseBackup class, where you only need to implement the
 apply function and your constructor. In the following example, this class is named TriMeshBackup.

 \code
 void TypeTriangleMeshPlugin::generateBackup( int _id, QString _name, UpdateType _type ){

  // Get the object corresponding to the id
  BaseObjectData* object = 0;
  PluginFunctions::getObject(_id, object);
  TriMeshObject* meshObj = PluginFunctions::triMeshObject(object);

  // Safety check
  if ( meshObj != 0 ){

    //get backup object data
    BackupData* backupData = 0;

    // If a backup data has already been attached, get it, otherwise create it.
    if ( object->hasObjectData( OBJECT_BACKUPS ) )
      backupData = dynamic_cast< BackupData* >(object->objectData(OBJECT_BACKUPS));
    else{
      //add backup data
      backupData = new BackupData(object);
      object->setObjectData(OBJECT_BACKUPS, backupData);
    }

    // Create a new backup
    TriMeshBackup* backup = new TriMeshBackup(meshObj, _name, _type);

    // Store it in the backup data
    backupData->storeBackup( backup );
  }
 }
 \endcode

Jan Möbius's avatar
Jan Möbius committed
215
216
217
218
219
220
221
222
223
224
225
226
227
228
To use the TypeInterface:
<ul>
<li> include TypeInterface.hh in your plugins header file
<li> derive your plugin from the class TypeInterface
<li> add Q_INTERFACES(TypeInterface) to your plugin class
<li> Implement all slots of this Interface
</ul>

*/





229
Q_DECLARE_INTERFACE(TypeInterface,"OpenFlipper.TypeInterface/1.1")
230
231

#endif // TYPEINTERFACE_HH