BaseObject.hh 18.5 KB
Newer Older
1
/*===========================================================================*\
Jan Möbius's avatar
Jan Möbius committed
2 3
*                                                                            *
*                              OpenFlipper                                   *
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 43 44 45 46 47 48 49 50 51 52 53

//=============================================================================
//
//  Types
//
//=============================================================================

/**
 * \file BaseObject.hh
 * This File contains the Basic object class for all Objects (Includes also non visual objects such as Groups).
 */

Jan Möbius's avatar
Jan Möbius committed
54
#pragma once
55 56 57

//== INCLUDES =================================================================

Jan Möbius's avatar
Jan Möbius committed
58
#include <OpenFlipper/common/GlobalDefines.hh>
59
#include <OpenFlipper/common/DataTypes.hh>
60
#include <OpenFlipper/common/UpdateType.hh>
Jan Möbius's avatar
Jan Möbius committed
61 62 63 64 65

#include "perObjectData.hh"

#include <vector>

66
#include <QObject>
67 68 69 70
#include <QString>
#include <QList>
#include <QStringList>
#include <QMap>
Jan Möbius's avatar
Jan Möbius committed
71

72 73 74 75 76 77 78 79 80 81

//== TYPEDEFS =================================================================


//== CLASS DEFINITION =========================================================

/**
 * This is the basic Data class providing the functions common to all objects.
 * If the Datacontrol Plugin is available, a Tree structure will be generated allowing groups of objects.
 */
Jan's avatar
Jan committed
82
class DLLEXPORT BaseObject : public QObject {
83 84 85
  
  Q_OBJECT 
  
86
  friend class BaseObjectData;
87
  friend class Core;
88 89

  public :
90 91

    /** Creates a copy of this Object. Currently it will not have any per Object data attached.
92
     *  Its automatically attached to the objectRoot.
93 94 95
     */
    BaseObject(const BaseObject& _object);

96 97 98 99
    /** Creates a new object. If the parent is 0 and the objectroot does not exist, it will have no parent.
    * If the objectroot exists and parent is 0, it will be appended to the objectroot.
    * If a parent is given, it is appended to this object.
    */
100
    explicit BaseObject( BaseObject *_parent = 0);
101 102 103

    virtual ~BaseObject();

104 105 106 107
    /** predefined handle which is returned when objects are not found or not initialized.
     */
    static int NOOBJECT;

108 109 110 111 112 113 114 115
  //===========================================================================
  /** @name Object Identification
   * @{ */
  //===========================================================================
  public:
    /** return the unique id of the object. These ids will be generated every time an object is created.
     * It will stay valid during the runtime of the application.
     */
116
    int id() const;
117 118 119 120 121

    /** return the persistent id of an object ( This id can be managed by a database ). It should
     * be persistent across program starts. It will be -1 if the object has not been registered by
     * a database. This id will only be set if a database plugin manages it.
     */
122
    int persistentId() const;
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

    /** set the persistent id of the object
     */
    void persistentId( int _id );

  private:
    /** \brief Unique ID for this Object
    *
    *  If you need to identify an Object in your plugin then use this id to find it. It does not change during the
    * Objects lifetime. In PluginFunctions.hh are some Functions which help with finding objects using this id.
    */
    int id_;

    /** \brief Persistent ID for this Object
    *
    *  If you need to identify an Object acroos program starts in your plugin then use this id to find it.
    * It will not change across program restarts. This id will only be set if a database plugin manages it.
    */
    int persistentId_;

  //===========================================================================
  /** @name Data Type Handling
    * @{ */
  //===========================================================================

  public:
    /** Check the if the object is of the given type
     * @param _type checks, if the object is of the given type
     */
152
    bool dataType(DataType _type) const;
153 154 155

    /** return the dataType of the object
     */
156
    DataType dataType() const;
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 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

    /** set the object type
     * @param _type the type of the object (if it has a type defined, it will output a warning)
     */
    void setDataType(DataType _type);

  private:
    /** This Field describes the Data Types available in the object. \n
     *  You should check this Field, if your plugin can handle the data in the object.
     */
    DataType objectType_;

  /** @} */

  //===========================================================================
  /** @name Data
   * @{ */
  //===========================================================================

  public:
    /** Clean all data structures of the object
      *
      * */
    virtual void cleanup();

  /** @} */

  //===========================================================================
  /** @name Object Information
   * @{ */
  //===========================================================================
  public:
    /// Get all Info for the Object as a string
    virtual QString getObjectinfo();

    /// Print all information about the object
    virtual void printObjectInfo();

  /** @} */

  //===========================================================================
Jan Möbius's avatar
Jan Möbius committed
198
  /** @name Flag handling (source, target, ...)
199 200 201
   * @{ */
  //===========================================================================

202 203 204
  signals:
    void objectSelectionChanged(int _objectId);
    
205 206
  public:
    /** Is this item selected as a target item?
Jan Möbius's avatar
Jan Möbius committed
207 208 209 210
      * Most algorithms operate on target meshes. These meshes are also considered as active.
      * Blending for inactive meshes is handled by DataControlPlugin so emit objectSelectionChanged
      * if you changed this value.\n
      */
211 212 213 214 215 216 217
    bool target();

    /** Set this item as a target
     */
    void target(bool _target);

    /** Is this item selected as a source item?
Jan Möbius's avatar
Jan Möbius committed
218 219
      * Some algorithms use source meshes to define their input.
      */
220 221 222 223 224 225
    bool source();

    /** Set this item as a source
     */
    void source(bool _source);

Jan Möbius's avatar
Jan Möbius committed
226 227 228
    /** Get a custom flag of this item
     */
    bool flag(QString _flag);
229

Jan Möbius's avatar
Jan Möbius committed
230 231 232
    /** Set a custom flag on this item
     */
    void setFlag(QString _flag, bool _set);
233

Jan Möbius's avatar
Jan Möbius committed
234
    /** Get all flags of this item
235
     */
Jan Möbius's avatar
Jan Möbius committed
236 237 238 239 240 241 242 243 244
    QStringList flags();

  private:

    /** Stores all item flags as strings. Source and target flags are represented
      * as "source" and "target" strings.
      */
    QStringList flags_;

245 246 247 248 249 250 251

  /** @} */

  //===========================================================================
  /** @name Object visualization
   * @{ */
  //===========================================================================
252 253 254 255 256 257 258 259 260
  
  signals:
    /** This slot is emitted when the visibility of the object gets changed.
    *
    * The signal is normally handled by the core to tell the plugins that an object changed its visibility
    *
    */
    void visibilityChanged(int _objectId);

261 262

  public :
263 264 265 266 267
    /// return if object is visible
    virtual bool visible();

    /// Sets visiblity
    virtual void visible(bool _visible);
268

269
  protected :
270 271 272 273 274 275 276 277 278 279 280 281 282 283
    /** Show/hide/ Object\n
    * Visibility is handled by DataControlPlugin so emit updated_objects if you changed this value
    * defaults to visible
    */
    bool visible_;

  /** @} */

  //===========================================================================
  /** @name Content
   * @{ */
  //===========================================================================

  public:
284 285 286 287 288 289 290 291 292 293 294 295 296 297


    /// Debugging function, writing the subtree to output
    void dumpTree();

    /// Returns a full copy of the object
    virtual BaseObject* copy();

  //===========================================================================
  /** @name Update handling
   * @{ */
  //===========================================================================

  protected:
Jan Möbius's avatar
Jan Möbius committed
298 299 300 301 302
    /** \brief  This function is called to update the object
     *
     * If the object changes, the core will call this function. Normally this will update
     * the corresponding scenegraph nodes or trigger other data handling which has to be done
     * when the object changes.
303 304
     *
     * \note Do not call this function yourself to avoid unnecessary overhead(the core will call it when it is required)
Jan Möbius's avatar
Jan Möbius committed
305
     */
306
    virtual void update(UpdateType _type = UPDATE_ALL);
307 308 309 310 311 312 313 314 315 316 317

  /** @} */


  //===========================================================================
    /** @name Tree Structure
    * @{ */
  //===========================================================================

  public:

318 319 320 321
    /** Get the last item of the tree (Preorder traversal of the tree)
     */
    BaseObject* last();

322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
    /** Get the next item of the tree (Preorder traversal of the tree)
     */
    BaseObject* next();

    /** level of the current object ( root node has level 0)
     */
    int level();

  private:
    /// Parent item or 0 if rootnode
    BaseObject *parentItem_;

    /// Children of this node
    QList<BaseObject*> childItems_;

  public:
    //===========================================================================
    /** @name Tree : Parent nodes
    * @{ */
    //===========================================================================

343 344
  public:
    
345 346 347 348 349
    /// get the row of this item from the parent
    int row() const;

    /// Get the parent item ( 0 if rootitem )
    BaseObject *parent();
350
    const BaseObject *parent() const;
351 352 353 354 355 356 357 358 359 360

    /// Set the parent pointer
    void setParent(BaseObject* _parent);

    /** @} */

    //===========================================================================
    /** @name Tree : Children
    * @{ */
    //===========================================================================
361 362 363
    
  public:
    
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
    /// Check if the element exists in the subtree of this element
    BaseObject* childExists(int _objectId);

    /// Check if the element exists in the subtree of this element
    BaseObject* childExists(QString _name);

    /// add a child to this node
    void appendChild(BaseObject *child);

    /// return a child
    BaseObject *child(int row);

    /// get the number of children
    int childCount() const;

    /// Remove a child from this object
    void removeChild( BaseObject* _item );

    /// get all leafes of the tree below this object ( These will be all visible objects )
    QList< BaseObject* > getLeafs();

    /// delete the whole subtree below this item ( The item itself is not touched )
    void deleteSubtree();

    /** @} */

    //===========================================================================
    /** @name Grouping
    * @{ */
    //===========================================================================

395 396
  public:
    
397 398 399 400 401
    /** Return the primary group of this object or -1 if ungrouped.
     * As this is a tree structure this returns the first group of this object.
     * Groups of groups are only suppurted via the other functions.
     * @return Primary group of this object or -1
     */
402
    int group() const;
403 404

    /// Check if object is a group
405
    bool isGroup() const;
406 407 408 409 410

    /** Check if this item belongs to a group with this id
     *
     *  @param _id id of the group
     */
411
    bool isInGroup( int _id ) const;
412 413 414 415 416

    /** Check if this item belongs to a group with this name
      *
      * @param _name Name of the group
      */
417
    bool isInGroup( QString _name ) const;
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434

    /** Get a vector of all Group ids this object belongs to ( this function omits the root object )
    */
    std::vector< int > getGroupIds();

    /** Get a vector of all group names  this object belongs to ( this function omits the root object )
     */
    QStringList getGroupNames();


    /** @} */

    //===========================================================================
    /** @name Name and Path handling
    * @{ */
    //===========================================================================

435
    /** Set the object path and filename from the given parameter.
436 437 438
     *
     * @param _filename path to the file.
     */
439
    void setFromFileName(const QString &_filename);
440 441 442


    /// return the path to the object ( defaults to "." if unset )
443
    QString path() const;
444 445

    /// set the path to the object.
446
    void setPath(const QString &_path);
447

448
    /// return the name of the object. The name defaults to NONAME if unset.
449
    QString name( ) const;
450

451 452 453 454

     /* set the name of the object. ( If you overwrite it, call BaseObject::setName(_name ) it in your funtion first)
     * this is may not the filename of the given object, because one file can have multiple objects
     */
455
    virtual void setName(QString _name );
456 457

    /// return the filename of the object
458
    QString filename() const;
459 460 461

    /// set the filename for this object
    void setFileName(const QString &_filename);
462

463 464 465 466
  private:

    /// path to the file from which the object is loaded ( defaults to "." )
    QString path_;
467
    QString filename_;
468 469 470 471 472 473

  signals:
    /** This signal is emitted when properties of the object have been changed like its name
    * or the parent changed
    */
    void objectPropertiesChanged(int _objectId);
474 475 476 477 478 479 480 481 482 483 484

  private:

    /// Object/FileName ( defaults to NONAME )
    QString name_;


    /** @} */

  //===========================================================================
  /** @name Object Payload
485
   * \anchor baseObjectPerObjectDataFunctions
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
   * @{ */
  //===========================================================================

  public:

    /** Set a pointer to your object data
     * Your data class has to be derived from PerObjectData and should implement a destructor.
     * use dynamic_casts to cast between your object and the Baseclass.
     *
     * @param _dataName Define a name for your data
     * @param _data a pointer to your object data
     */
    void setObjectData( QString _dataName , PerObjectData* _data );

    /// Clear the object data pointer ( this will not delete the object!! )
    void clearObjectData( QString _dataName );

    /// Checks if object data with given name is available
    bool hasObjectData( QString _dataName );

    /// Returns the object data pointer
    PerObjectData* objectData( QString _dataName );

    /// Delete all data attached to this object ( calls delete on each object )
    void deleteData();
511 512 513 514 515 516 517 518
    
    /** @} */
    
    //===========================================================================
    /** @name Object Payload functions for internal use only!
    * @{ */
    //===========================================================================
    
Jan Möbius's avatar
Jan Möbius committed
519
    /** \brief get reference to map of all perObject Datas
520 521 522 523
    *
    * Don't use this function! It's only for the backup Plugin to store and restore
    * perObjectDatas!
    */
Jan Möbius's avatar
Jan Möbius committed
524
    QMap<QString, PerObjectData*>& getPerObjectDataMap();
525 526
    
    
527 528 529 530
  private:

    QMap<QString, PerObjectData* > dataMap_;

531 532 533 534 535 536 537 538 539 540 541 542 543
    /** @} */

    //===========================================================================
    /** @name Object Comment
    * @{ */
    //===========================================================================

  public:
    /** \brief Get comment for the specified key.
    *
    * If no comment with the specified exists, an empty
    * one is created.
    */
544
    QString &getCommentByKey(const QString &key);
545 546 547 548 549 550

    /** \brief Get comment for the specified key.
    *
    * If no comment with the specified exists, an empty comment
    * is returned (but not inserted into the map).
    */
551
    const QString getCommentByKey(const QString &key) const;
552 553

    /** Returns true if a comment for the specified key exists, false otherwise. */
554
    bool hasCommentForKey(const QString &key) const;
555 556

    /** Indicates whether any comment has been supplied for this object. */
557
    bool hasComments() const;
558

559
    void clearComment(const QString &key);
560

561
    void clearAllComments();
562

563
    /** Returns a reference to all comments. */
564
    const QMap<QString, QString> &getAllComments() const;
565 566

    /** Returns a flat, human readable representation of all comments. */
Jan Möbius's avatar
Jan Möbius committed
567
    const QString getAllCommentsFlat() const;
568 569 570 571 572

  private:
    QMap<QString, QString> commentsByKey_;

    /** @} */
573 574 575

};