FileInterface.hh 13.8 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 52
#ifndef FILEINTERFACE_HH
#define FILEINTERFACE_HH
Jan Möbius's avatar
 
Jan Möbius committed
53

54 55
 class QWidget;
#include <QString>
Matthias Möller's avatar
Matthias Möller committed
56

Jan Möbius's avatar
 
Jan Möbius committed
57
 #include <OpenFlipper/common/Types.hh>
58

Jan Möbius's avatar
Jan Möbius committed
59 60 61 62 63 64 65 66 67
/** \file FileInterface.hh
*
* Interface class for file handling.\ref fileInterfacePage
*/

/** \brief Interface class for file handling.
  *
  * \ref fileInterfacePage "Detailed description"
  * \n
68
  *
Jan Möbius's avatar
Jan Möbius committed
69 70
  * Loading and saving of files in OpenFlipper is handled via the file plugins.
  * These have to implement this class.
Jan Möbius's avatar
 
Jan Möbius committed
71 72
 */
class FileInterface {
73

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

79 80 81
  public:

    /// Destructor
Jan Möbius's avatar
Jan Möbius committed
82
    virtual ~FileInterface() {};  
Dirk Wilden's avatar
Dirk Wilden committed
83

Jan Möbius's avatar
Jan Möbius committed
84 85 86 87 88
    /** You can provide a special widget showing options for saving your file types
      *  depending on the current filter
      *
      *  _currentFilter: one filter from saveFilters
      *
Jan Möbius's avatar
Jan Möbius committed
89
      *  Return 0 when you don't need one
Jan Möbius's avatar
Jan Möbius committed
90
      */
Jan Möbius's avatar
Jan Möbius committed
91
    virtual QWidget* saveOptionsWidget(QString _currentFilter) = 0;
Dirk Wilden's avatar
Dirk Wilden committed
92

Jan Möbius's avatar
Jan Möbius committed
93 94 95 96 97
    /** You can provide a special widget showing options for loading your file types
      *  depending on the current filter
      *
      *  _currentFilter: one filter from loadFilters
      *
Jan Möbius's avatar
Jan Möbius committed
98
      *  Return 0 when you don't need one
Jan Möbius's avatar
Jan Möbius committed
99
      */
Jan Möbius's avatar
Jan Möbius committed
100
    virtual QWidget* loadOptionsWidget(QString _currentFilter) = 0;
Jan Möbius's avatar
Jan Möbius committed
101 102

/** @} */
103

Jan Möbius's avatar
 
Jan Möbius committed
104 105 106 107 108 109 110
//===========================================================================
/** @name Supported types
* @{ */
//===========================================================================

public:

Jan Möbius's avatar
Docu  
Jan Möbius committed
111
  /** \brief Return your supported object type( e.g. DATA_TRIANGLE_MESH )
Jan Möbius's avatar
 
Jan Möbius committed
112
   *
Jan Möbius's avatar
Jan Möbius committed
113 114 115
   * If you support multiple DataTypes you can combine them bitwise here.
   * The function is used from the load and save code to check if your plugin
   * can handle an object of a given dataType.
Jan Möbius's avatar
 
Jan Möbius committed
116 117 118 119
   */
  virtual DataType supportedType() = 0;


Jan Möbius's avatar
Jan Möbius committed
120
  /** Return the Qt Filters for File dialogs when loading files (e.g. "Off files ( *.off )" )
Jan Möbius's avatar
 
Jan Möbius committed
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
   *
   */
  virtual QString getLoadFilters() = 0;

  /** Get the Qt Filters for Filedialogs when saving files (e.g. "Off files ( *.off )" )
   *
   */
  virtual QString getSaveFilters() = 0;

/** @} */

//===========================================================================
/** @name Load Save Implementation
* @{ */
//===========================================================================

137 138
signals:

Jan Möbius's avatar
Jan Möbius committed
139 140 141
      /** \brief This signal has to be emitted if an object has been loaded from a file.
       *
       * Every time a new object is added to the scene from a file, this signal has to be emitted.
142 143
       * Emit this signal once each time a file has been completely processed.\n
       * This signal can be called from any thread.\n
Jan Möbius's avatar
Jan Möbius committed
144 145 146
       *
       * @param _id Id of the new object
       */
Jan Möbius's avatar
Jan Möbius committed
147
      virtual void openedFile( int _id ) {};
148

149 150
public slots:

Jan Möbius's avatar
 
Jan Möbius committed
151 152 153 154
      /** \brief Load an object from the given file
       *
       * The Core will call this slot if you should load a file. The core will
       * check if you support the given file type depending on the provided
Jan Möbius's avatar
Jan Möbius committed
155 156
       * filters and dataTypes ( see supportedType and getLoadFilters )\n
       *
Jan Möbius's avatar
Jan Möbius committed
157 158 159 160
       * If loading fails, you have to return -1. If you created an empty
       * object and then your load procedure fails, you should remove the newly
       * generated object.\n
       *
Jan Möbius's avatar
Jan Möbius committed
161
       * if you just opened a file and did not create any object, return 0 here,
Jan Möbius's avatar
Jan Möbius committed
162
       * telling the core that it was successfully loaded but no new object
Jan Möbius's avatar
Jan Möbius committed
163
       * has been created!
Jan Möbius's avatar
 
Jan Möbius committed
164
       */
Jan Möbius's avatar
Jan Möbius committed
165
      virtual int loadObject(QString _filename) = 0;
166

Dirk Wilden's avatar
Dirk Wilden committed
167 168 169 170 171 172 173
      /** \brief Load an object from the given file
       *
       * The Core will call this slot if you should load a file. The core will
       * check if you support the given file type depending on the provided
       * filters and dataTypes ( see supportedType and getLoadFilters )\n
       *
       * if you just opened a file and did not create any object, return 0 here,
Jan Möbius's avatar
Jan Möbius committed
174
       * telling the core that it was successfully loaded but no new object
Dirk Wilden's avatar
Dirk Wilden committed
175 176
       * has been created!
       *
Jan Möbius's avatar
Jan Möbius committed
177 178 179 180
       * If loading fails, you have to return -1. If you created an empty
       * object and then your load procedure fails, you should remove the newly
       * generated object.\n
       *
Dirk Wilden's avatar
Dirk Wilden committed
181
       * If the Plugin is able to open the file in different DataTypes
Jan Möbius's avatar
Jan Möbius committed
182 183 184 185
       * one of these DataTypes can be forced with this function.
       *
       * This forcing is used for example by the recent files function for
       * choosing between poly or triangle mesh loading.
Dirk Wilden's avatar
Dirk Wilden committed
186 187
       *
       */
Jan Möbius's avatar
Jan Möbius committed
188
      virtual int loadObject(QString _filename, DataType _type){ return -1;};
Jan Möbius's avatar
Jan Möbius committed
189

Jan Möbius's avatar
 
Jan Möbius committed
190 191 192 193 194 195 196
      /** \brief Save an object from the given file
       *
       * The Core will call this slot if you should save an object to a file.
       * The core will check if you support the given object depending on the
       * provided dataTypes ( see supportedType ).
       * Additionally to the filename you get the id of the object to save
       */
Jan Möbius's avatar
Jan Möbius committed
197
      virtual bool saveObject(int _id, QString _filename) = 0;
198

199 200 201 202 203 204 205
      /** \brief Save multiple objects to one file
       *
       * The Core will call this slot if the user wants to save multiple files in one file.
       * The core will check if you support the given object depending on the
       * provided dataTypes ( see supportedType ).
       * Additionally to the filename you get ids of the objects to save
       */
Jan Möbius's avatar
Jan Möbius committed
206
      virtual bool saveObjects(IdList _ids, QString _filename){return false;};
Jan Möbius's avatar
 
Jan Möbius committed
207
/** @} */
208

Jan Möbius's avatar
 
Jan Möbius committed
209 210
};

Jan Möbius's avatar
Jan Möbius committed
211 212 213 214 215 216 217 218 219
/** \page fileInterfacePage File Interface
 * \n
\image html FileInterface.png
\n


\section fileInterface_Functionality Functionality
This interface class has to be fully implemented. When you want to support save and load
for an object type you have to implement all functions in this class in a file plugin.
Jan Möbius's avatar
Jan Möbius committed
220
The plugin has to be named Plugin-File\<FileExtension\>. The plugins are loaded directly after the
Jan Möbius's avatar
Jan Möbius committed
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 338 339 340 341 342 343 344 345 346 347 348 349
Type Plugins.

OpenFlipper manages loading and saving of files from the core.

\section fileInterface_filters Load/Save filters and Type Support
In these functions you have to return filters for your supported type. Use the standard format of Qt and
return your filters within the functions FileInterface::getLoadFilters() and FileInterface::getSaveFilters().
OpenFlipper will decide based on the filters, if your plugin is used to handle a load or save request.
Additionally you have to define, which DataTypes your plugin supports via FileInterface::supportedType().

\code
// File types you support when loading files
QString ExamplePlugin::getLoadFilters() {
    return QString( tr("Your FileType ( *.ext )") );
}

// File types you support when saving files
QString ExamplePlugin::getSaveFilters() {
    return QString( tr("Your FileType ( *.ext )") );
}

// Data types you support in your plugin
DataType  ExamplePlugin::supportedType() {
    DataType type = DATA_POLY_MESH | DATA_TRIANGLE_MESH | DATA_GROUP;

    return type;
}


\endcode

\section fileInterface_widgets Widgets
When an object is loaded or saved via your file plugin, you can present an additional dialog, used to
control your reader/writer.
You have to implement the following functions. If you don't need such a widget, just return a 0.

\code
// In your plugin initialization
void ExamplePlugin::pluginsInitialized() {
  // Global variable:
  // QWidget* saveOptionsWidget_;

  // Create widget
  saveOptionsWidget_ = new QWidget(0);

  // Setup widget afterwards

  // Global variable:
  // QWidget* loadOptionsWidget_;

  // Create widget
  loadOptionsWidget_ = new QWidget(0);

  // Setup widget afterwards
}


// Return your widget for save options
QWidget* ExamplePlugin::saveOptionsWidget(QString _currentFilter) {

  // Modify widget based on the filter
  // ...

  // Return widget
  return saveOptionsWidget_;
}

// Return your widget for load options
QWidget* ExamplePlugin::loadOptionsWidget(QString _currentFilter) {

  // Modify widget based on the filter
  // ...

  // Return widget
  return loadOptionsWidget_;
}
\endcode

\section fileInterface_loading Loading Files
The FileInterface defines two functions for loading files. The first one ( FileInterface::loadObject(QString _filename) ) has
to be implemented. It has to load the data from the given file. The return value of this function has to be the id of the
new object. Additionally you have to
\code
emit FileInterface::openedFile( int _id );
\endcode
with the id of every object you loaded from the file.

If the file contains multiple objects, create a GroupObject and add all loaded objects to that group. Than return
the id of the group here. Don't forget the FileInterface::openedFile() signal for every newly loaded object from the file.

One function that can be implemented is FileInterface::loadObject(QString _filename, DataType _type). This function gets a
DataType along with the filename. This can be used to force a specific DataType when loading objects. E.g. A plugin loading
obj files which handles triangle and polygonal meshes which can than be forced to triangulate every input data.

\section fileInterface_saving Saving Files
The FileInterface defines two functions for loading files. The first one ( FileInterface::saveObject(int _id, QString _filename) )
has to be implemented. It has to save the object with the given id to the file specified by the filename. The return value of
this function has to be true if it succeeded, otherwise false. OpenFlipper will choose your plugin to save the data if two
prerequisites are fulfilled. Your  FileInterface::supportedTypes() has to support the DataType of the object and
your save filters have to contain the extension of the file to use. These checks are done within OpenFlipper such that
you only get information that can be handled by the plugin.

Additionally there is a function FileInterface::saveObjects(IdList _ids, QString _filename) which does the same as the above
but saves a set of objects to a file.


\section fileInterface_usage Usage
To use the FileInterface:
<ul>
<li> include FileInterface.hh in your plugins header file
<li> derive your plugin from the class FileInterface
<li> add Q_INTERFACES(FileInterface) to your plugin class
<li> And implement the required slots and functions
</ul>



TODO:
// Deprecated here? In TypeInterface?
virtual int addEmpty() { return -1; };

*/






Q_DECLARE_INTERFACE(FileInterface,"OpenFlipper.FileInterface/1.0")
350

Jan Möbius's avatar
 
Jan Möbius committed
351
#endif // FILEINTERFACE_HH