openFunctions.cc 28.5 KB
Newer Older
1 2 3 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 39 40 41
/*===========================================================================*\
 *                                                                           *
 *                              OpenFlipper                                  *
 *      Copyright (C) 2001-2009 by Computer Graphics Group, RWTH Aachen      *
 *                           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/>.                                      *
 *                                                                           *
\*===========================================================================*/

/*===========================================================================*\
 *                                                                           *
 *   $Revision$                                                         *
 *   $Author$                                                      *
 *   $Date$                   *
 *                                                                           *
\*===========================================================================*/
Jan Möbius's avatar
 
Jan Möbius committed
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56





#include "Core.hh"

#include <ACG/QtWidgets/QtFileDialog.hh>

#include "OpenFlipper/common/GlobalOptions.hh"
#include "OpenFlipper/BasePlugin/PluginFunctions.hh"

#include "OpenFlipper/widgets/loadWidget/loadWidget.hh"
#include "OpenFlipper/widgets/addEmptyWidget/addEmptyWidget.hh"

57 58 59
#include <OpenFlipper/common/Types.hh>
#include <ObjectTypes/PolyMesh/PolyMesh.hh>

60 61
#include <time.h>

62
void Core::resetScenegraph( bool _resetTrackBall  ) {
63
  if ( OpenFlipper::Options::gui() && !OpenFlipper::Options::loadingSettings() ) {
Jan Möbius's avatar
Jan Möbius committed
64 65

    for ( unsigned int i = 0 ; i < OpenFlipper::Options::examinerWidgets() ; ++i ) {
66
      // update scene graph (get new bounding box and set projection right, including near and far plane)
67
      PluginFunctions::viewerProperties(i).lockUpdate();
68
      coreWidget_->examiner_widgets_[i]->sceneGraph(root_node_scenegraph_, _resetTrackBall );
69
      PluginFunctions::viewerProperties(i).unLockUpdate();
Jan Möbius's avatar
Jan Möbius committed
70 71 72
      coreWidget_->examiner_widgets_[i]->updateGL();
    }

Jan Möbius's avatar
 
Jan Möbius committed
73
  }
74

Jan Möbius's avatar
Jan Möbius committed
75
  // Update the draw Modes Menu
Dirk Wilden's avatar
Dirk Wilden committed
76 77
  if ( OpenFlipper::Options::gui() )
    coreWidget_->slotUpdateGlobalDrawMenu();
Jan Möbius's avatar
 
Jan Möbius committed
78 79 80 81 82 83 84 85 86
}

//========================================================================================
// ===            Open/Add-Empty Functions                    ============================
//========================================================================================


void Core::slotGetAllFilters ( QStringList& _list){

87
  /// \todo check why the supported Type is used here!
Jan Möbius's avatar
Jan Möbius committed
88
  // Iterate over all types
Jan Möbius's avatar
 
Jan Möbius committed
89 90 91 92 93 94 95
  for (int i=0; i < (int)supportedTypes_.size(); i++){
    QString f = supportedTypes_[i].plugin->getLoadFilters();
    f = f.section(")",0,0).section("(",1,1).trimmed();
    _list << (QString::number(supportedTypes_[i].plugin->supportedType()) + " " + f);
  }
}

96
void Core::commandLineOpen(const char* _filename, bool _asPolyMesh ){
97

Jan Möbius's avatar
Jan Möbius committed
98
  QString file(_filename);
99

100
  // Modify filename to contain full paths if they were given as relative paths
Jan Möbius's avatar
Jan Möbius committed
101 102 103 104 105
  if ( !file.startsWith("/") && !file.contains(":") ) {
    file = QDir::currentPath();
    file += OpenFlipper::Options::dirSeparator();
    file += _filename;
  }
106

Jan Möbius's avatar
Jan Möbius committed
107
  // Add to the open list
108
  commandLineFileNames_.push_back(std::pair< std::string , bool >(file.toStdString(), _asPolyMesh));
Jan Möbius's avatar
 
Jan Möbius committed
109 110 111
}

void Core::commandLineScript(const char* _filename ) {
112 113 114 115 116 117 118 119 120 121 122 123

  QString file(_filename);
  
  // Modify filename to contain full paths if they were given as relative paths
  if ( !file.startsWith("/") && !file.contains(":") ) {
    file = QDir::currentPath();
    file += OpenFlipper::Options::dirSeparator();
    file += _filename;
  }
  
  // Add to the open list
  commandLineScriptNames_.push_back(file.toStdString());
124 125
}

Jan Möbius's avatar
Jan Möbius committed
126 127 128 129 130 131
void Core::slotExecuteAfterStartup() {

  //check if we have scripting support:
  bool scriptingSupport = false;
  slotPluginExists("scripting",scriptingSupport);
  if ( ! scriptingSupport ) {
132
    emit log(LOGERR ,tr("No scripting support available, please check if we load a scripting plugin .. Skipping script execution on startup"));
Jan Möbius's avatar
Jan Möbius committed
133 134
  }

Jan Möbius's avatar
Jan Möbius committed
135 136 137
  // Collect all script files from the scripting subdirectory and execute them if possible.
  // You can use this directory to execute scipts that modify for example modify the ui at
  // every startup.
Jan Möbius's avatar
Jan Möbius committed
138 139
  if ( scriptingSupport ) {

Jan Möbius's avatar
Jan Möbius committed
140
    // Get the files in the directory
Jan Möbius's avatar
Jan Möbius committed
141 142 143
    QDir scriptDir = OpenFlipper::Options::scriptDir();
    QStringList scriptFiles = scriptDir.entryList(QDir::Files,QDir::Name);

Jan Möbius's avatar
Jan Möbius committed
144
    // Execute all files ending with ofs
Jan Möbius's avatar
Jan Möbius committed
145
    for ( int i = 0 ; i  < scriptFiles.size(); ++i )
Jan Möbius's avatar
Jan Möbius committed
146 147
      if ( scriptFiles[i].endsWith("ofs") )
        emit executeFileScript(scriptDir.path() + QDir::separator() + scriptFiles[i]);
Jan Möbius's avatar
Jan Möbius committed
148 149 150

  }

Jan Möbius's avatar
Jan Möbius committed
151
  // Open all files given at the commandline
152
  for ( uint i = 0 ; i < commandLineFileNames_.size() ; ++i ) {
153 154

    // Skip scripts here as they will be handled by a different function
155
    QString tmp = QString::fromStdString(commandLineFileNames_[i].first);
156 157 158 159 160
    if ( tmp.endsWith("ofs") ) {
      commandLineScriptNames_.push_back(commandLineFileNames_[i].first);
      continue;
    }

Jan Möbius's avatar
Jan Möbius committed
161
    // If the file was given with the polymesh option, open them as polymeshes.
162
    if (commandLineFileNames_[i].second)
163 164 165 166
      loadObject(DATA_POLY_MESH, QString::fromStdString(commandLineFileNames_[i].first));
    else {
      loadObject(QString::fromStdString(commandLineFileNames_[i].first));
    }
Jan Möbius's avatar
 
Jan Möbius committed
167 168
  }

Jan Möbius's avatar
Jan Möbius committed
169
  // If we have scripting support, execute the scripts given at the commandline.
Jan Möbius's avatar
Jan Möbius committed
170
  if ( scriptingSupport )
171
    for ( uint i = 0 ; i < commandLineScriptNames_.size() ; ++i ) {
172
      emit executeFileScript(QString::fromStdString(commandLineScriptNames_[i]));
173
    }
174

Jan Möbius's avatar
Jan Möbius committed
175 176
  // If we don't have a gui and we are not under remote control,
  // exit the application as there would be no way to execute further commands
Dirk Wilden's avatar
Dirk Wilden committed
177 178
  if ( !OpenFlipper::Options::gui() && !OpenFlipper::Options::remoteControl())
    exitApplication();
Jan Möbius's avatar
 
Jan Möbius committed
179 180
}

181

Jan Möbius's avatar
Jan Möbius committed
182 183 184 185 186 187 188
int Core::loadObject ( QString _filename ) {
  /** \todo Check if this function is ok. It should check all plugins for the given files and do not depend
            on Triangle meshes only! 
            Rewrite function to get the plugin only and then open the file. So iterate over all plugins and find the
            matching ones. open it with this plugin.
  */
  
Jan Möbius's avatar
 
Jan Möbius committed
189
  if (_filename.endsWith(".ini")) {
190 191

    // Load all information from the given ini file
192
    openIniFile(_filename,true,true,true);
193

Jan Möbius's avatar
 
Jan Möbius committed
194
    if ( OpenFlipper::Options::gui() )
195
      coreWidget_->addRecent(_filename, DATA_UNKNOWN);
196

Jan Möbius's avatar
 
Jan Möbius committed
197 198 199
    return -2;
  } else
  if (_filename.endsWith(".ofs")) {
Jan Möbius's avatar
Jan Möbius committed
200
     emit log(LOGINFO ,tr("Starting script execution of %1.").arg( _filename)) ;
Jan Möbius's avatar
 
Jan Möbius committed
201 202 203 204 205 206 207 208 209
     emit executeFileScript(_filename);
  } else
    return loadObject( DATA_TRIANGLE_MESH, _filename);

  return -1;
}

/// Function for loading a given file
int Core::loadObject( DataType _type, QString _filename) {
Jan Möbius's avatar
Jan Möbius committed
210 211 212
  /** \todo this function has to be checked. test for the plugin which can handle 
            the given file and then use it. 
  */
Jan Möbius's avatar
 
Jan Möbius committed
213

214
  if (_type == DATA_UNKNOWN)
Jan Möbius's avatar
 
Jan Möbius committed
215 216 217 218 219 220 221
    return loadObject(_filename);

  for (int i=0; i < (int)supportedTypes_.size(); i++)
    if (supportedTypes_[i].type == _type) {


      if ( OpenFlipper::Options::gui() ) {
Jan Möbius's avatar
Jan Möbius committed
222
        coreWidget_->statusMessage( tr("Loading %1 ... ").arg(_filename));
223
        if ( !OpenFlipper::Options::loadingSettings() )
Jan Möbius's avatar
 
Jan Möbius committed
224 225 226 227 228 229 230 231
          coreWidget_->setStatus(ApplicationStatus::PROCESSING );
      }

      //load file
      int id = supportedTypes_[i].plugin->loadObject(_filename);

      if ( OpenFlipper::Options::gui() ) {
        if ( id != -1 )
Jan Möbius's avatar
Jan Möbius committed
232
          coreWidget_->statusMessage( tr("Loading %1 ... done").arg(_filename), 4000 );
Jan Möbius's avatar
 
Jan Möbius committed
233
        else
Jan Möbius's avatar
Jan Möbius committed
234
          coreWidget_->statusMessage( tr("Loading %1 ... failed!").arg(_filename), 4000 );
Jan Möbius's avatar
 
Jan Möbius committed
235

236
        if ( !OpenFlipper::Options::loadingSettings() )
Jan Möbius's avatar
 
Jan Möbius committed
237 238 239 240 241 242 243 244
          coreWidget_->setStatus(ApplicationStatus::READY );
      }

      return id;
    }
  return -1; //no plugin found
}

Jan Möbius's avatar
Jan Möbius committed
245

Jan Möbius's avatar
 
Jan Möbius committed
246
int Core::addEmptyObject( DataType _type ) {
247 248
  // Iterate over all plugins. The first plugin supporting the addEmpty function for the
  // specified type will be used to create the new object.
Jan Möbius's avatar
 
Jan Möbius committed
249
  for (int i=0; i < (int)supportedTypes_.size(); i++)
250
    if ( supportedTypes_[i].type & _type )
Jan Möbius's avatar
 
Jan Möbius committed
251 252 253 254 255 256 257 258 259 260 261 262 263
      return supportedTypes_[i].plugin->addEmpty();
  return -1; //no plugin found
}

//========================================================================================
// ===             Open/Add-Empty Slots                       ============================
//========================================================================================

/// Slot for adding an empty object of given DataType
void Core::slotAddEmptyObject( DataType _type , int& _id ) {
  _id = addEmptyObject( _type );
}

264
/// Slot creating a copy of an existing object
Jan Möbius's avatar
Jan Möbius committed
265
void Core::slotCopyObject( int _oldId , int& _newId ) {
266

Jan Möbius's avatar
Jan Möbius committed
267
  if ( _oldId == -1 ) {
Jan Möbius's avatar
Jan Möbius committed
268
    emit log(LOGERR,tr("Requested copy for illegal Object id: %1").arg(_oldId) );
Jan Möbius's avatar
Jan Möbius committed
269 270 271 272 273 274 275 276
    _newId = -1;
    return;
  }

  // get the node
  BaseObject* object = objectRoot_->childExists(_oldId);

  if ( !object ) {
Jan Möbius's avatar
Jan Möbius committed
277
    emit log(LOGERR,tr("Requested copy for unknown Object id: %1 ").arg(_oldId) );
Jan Möbius's avatar
Jan Möbius committed
278 279 280 281 282 283 284 285
    _newId = -1;
    return ;
  }

  // Copy the item
  BaseObject* copy = object->copy();

  if ( copy == 0 ) {
286
    emit log(LOGERR,tr("Unable to create a copy of the object."));
Jan Möbius's avatar
Jan Möbius committed
287 288 289 290 291 292 293 294 295 296
    return;
  }

  // Integrate into object tree
  copy->setParent( object->parent() );
  if ( object->parent() )
    object->parent()->appendChild(copy);
  else
    std::cerr << "Unable to add copy to object list" << std::endl;

297
  // return the new id
Jan Möbius's avatar
Jan Möbius committed
298
  _newId = copy->id();
299

300 301 302 303 304 305
  // tell plugins that a new object has been created
  slotEmptyObjectAdded(_newId);

  // tell plugins that the object has been updated
  slotObjectUpdated(_newId);

306 307
}

Dirk Wilden's avatar
Dirk Wilden committed
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
/// Function for loading a given file
void Core::slotLoad(QString _filename, int _pluginID) {

  if ( OpenFlipper::Options::gui() ) {
    coreWidget_->statusMessage( tr("Loading %1 ... ").arg(_filename));
    if ( !OpenFlipper::Options::loadingSettings() )
      coreWidget_->setStatus(ApplicationStatus::PROCESSING );
  }

  //load file
  int id = supportedTypes_[_pluginID].plugin->loadObject(_filename);

  if ( OpenFlipper::Options::gui() ) {
    if ( id != -1 )
      coreWidget_->statusMessage( tr("Loading %1 ... done").arg(_filename), 4000 );
    else
      coreWidget_->statusMessage( tr("Loading %1 ... failed!").arg(_filename), 4000 );

    if ( !OpenFlipper::Options::loadingSettings() )
      coreWidget_->setStatus(ApplicationStatus::READY );
  }
  
330 331 332 333 334
  // Initialize as unknown type
  DataType type = DATA_UNKNOWN;

  // An object has been added. Get it and do some processing!
  if ( id > 0 ) {
Jan Möbius's avatar
Jan Möbius committed
335
    /// \todo Move this code to polymesh plugin and do the check there!
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
    
    BaseObjectData* object;
    PluginFunctions::getObject(id,object);

    if ( !object ) {
      emit log(LOGERR,tr("Object id returned but no object with this id has been found! Error in one of the file plugins!"));
      return;
    }
    
    // Get the objects type
    type = object->dataType();
    
    // Check if it is a polymesh
    if ( object != 0 && type == DATA_POLY_MESH ) {

      PolyMeshObject* poly = 0;
      PluginFunctions::getObject(id,poly);
Dirk Wilden's avatar
Dirk Wilden committed
353

354 355
      if ( poly != 0 ) {
          PolyMesh& mesh = *poly->mesh();
Dirk Wilden's avatar
Dirk Wilden committed
356

357
          bool isTriangleMesh = true;
Dirk Wilden's avatar
Dirk Wilden committed
358

359
          for ( PolyMesh::FaceIter f_it = mesh.faces_begin(); f_it != mesh.faces_end() ; ++f_it) {
Dirk Wilden's avatar
Dirk Wilden committed
360

361 362 363 364
            // Count number of vertices for the current face
            uint count = 0;
            for ( PolyMesh::FaceVertexIter fv_it( mesh,f_it); fv_it; ++fv_it )
              ++count;
Dirk Wilden's avatar
Dirk Wilden committed
365

366 367 368 369 370
            // Check if it is a triangle. If not, this is really a poly mesh
            if ( count != 3 ) {
              isTriangleMesh = false;
              break;
            }
Dirk Wilden's avatar
Dirk Wilden committed
371 372 373

          }

374 375 376 377 378 379 380 381 382 383 384 385 386
          // Mesh loaded as polymesh is actually a triangle mesh. Ask the user to reload as triangle mesh or keep it as poly mesh.
          if ( isTriangleMesh ) {
            QMessageBox::StandardButton result = QMessageBox::question ( 0,
                                                                        tr("TriMesh loaded as PolyMesh"),
                                                                        tr("You opened the mesh as a poly mesh but actually its a triangle mesh. \nShould it be opened as a triangle mesh?"),
                                                                        (QMessageBox::Yes | QMessageBox::No ),
                                                                        QMessageBox::Yes );
            // User decided to reload as triangle mesh
            if ( result == QMessageBox::Yes ) {
              slotDeleteObject(id);
              id = loadObject(DATA_TRIANGLE_MESH ,_filename);
              type = DATA_TRIANGLE_MESH;
            }
Dirk Wilden's avatar
Dirk Wilden committed
387 388
          }

389
      }
Dirk Wilden's avatar
Dirk Wilden committed
390 391
    }
  }
392 393
  
  // If the id was greater than zero, add the file to the recent files.
Dirk Wilden's avatar
Dirk Wilden committed
394 395 396 397 398
  if ( id >= 0 )
    if ( OpenFlipper::Options::gui() )
      coreWidget_->addRecent(_filename, type);
}

Jan Möbius's avatar
 
Jan Möbius committed
399 400 401
/// Slot for loading a given file
void Core::slotLoad(QString _filename, DataType _type, int& _id) {
  _id = loadObject(_type,_filename);
402

403 404
  // Check if it is a polymesh
  if ( _type == DATA_POLY_MESH ) {
405

406 407
    PolyMeshObject* poly = 0;
    PluginFunctions::getObject(_id,poly);
408

409
    if ( poly != 0 ) {
Jan Möbius's avatar
Jan Möbius committed
410
        /// \todo Move this code to polymesh plugin and do the check there!
411
        PolyMesh& mesh = *poly->mesh();
412

413
        bool isTriangleMesh = true;
414

415
        for ( PolyMesh::FaceIter f_it = mesh.faces_begin(); f_it != mesh.faces_end() ; ++f_it) {
416

417 418
          // Count number of vertices for the current face
          uint count = 0;
419
          for ( PolyMesh::FaceVertexIter fv_it( mesh,f_it); fv_it; ++fv_it )
420
            ++count;
421

422 423 424 425 426
          // Check if it is a triangle. If not, this is really a poly mesh
          if ( count != 3 ) {
            isTriangleMesh = false;
            break;
          }
427

428
        }
429

430 431
        // Mesh loaded as polymesh is actually a triangle mesh. Ask the user to reload as triangle mesh or keep it as poly mesh.
        if ( isTriangleMesh ) {
432 433 434 435
          QMessageBox::StandardButton result = QMessageBox::question ( 0,
                                                                      tr("TriMesh loaded as PolyMesh"),
                                                                      tr("You opened the mesh as a poly mesh but actually its a triangle mesh. \nShould it be opened as a triangle mesh?"),
                                                                      (QMessageBox::Yes | QMessageBox::No ),
436
                                                                      QMessageBox::Yes );
437
          // User decided to reload as triangle mesh
438 439 440 441 442 443
          if ( result == QMessageBox::Yes ) {
            slotDeleteObject(_id);
            _id = loadObject(DATA_TRIANGLE_MESH ,_filename);
            _type = DATA_TRIANGLE_MESH;
          }
        }
444

445 446
    }
  }
Jan Möbius's avatar
 
Jan Möbius committed
447 448 449
  if ( _id < 0 )
    _id = -1;
  else
450
    if ( OpenFlipper::Options::gui() )
Jan Möbius's avatar
 
Jan Möbius committed
451 452 453 454
      coreWidget_->addRecent(_filename,_type);
}

/// Slot gets called after a file-plugin has opened an object
455 456 457 458
void Core::slotObjectOpened ( int _id ) {
  if ( OpenFlipper::Options::doSlotDebugging() ) {
    if ( sender() != 0 ) {
      if ( sender()->metaObject() != 0 ) {
459
        emit log(LOGINFO,tr("slotObjectOpened( ") + QString::number(_id) + tr(" ) called by ") +
460 461 462
                  QString( sender()->metaObject()->className() ) );
      }
    } else {
463
      emit log(LOGINFO,tr("slotObjectOpened( ") + QString::number(_id) + tr(" ) called by Core") );
464 465
    }
  }
466 467

  // get the opened object
468 469
  BaseObjectData* object;
  PluginFunctions::getObject(_id,object);
470

471
  QColor color;
472

473
  if ( OpenFlipper::Options::randomBaseColor() ){
474 475 476 477 478
      //init random seed
      srand ( time(NULL) );

      QColor bckgrnd = OpenFlipper::Options::defaultBackgroundColor();
      int diff;
479

480 481
      do{
        color.setRgb(rand()%255, rand()%255, rand()%255);
482

483 484 485
        diff = (bckgrnd.red()   - color.red())  *(bckgrnd.red()   - color.red())
              +(bckgrnd.green() - color.green())*(bckgrnd.green() - color.green())
              +(bckgrnd.blue()  - color.blue()) *(bckgrnd.blue()  - color.blue());
486
      }while (diff < 70000);
487 488
  }
  else{
489
      color = OpenFlipper::Options::defaultBaseColor();
490
  }
491

492 493 494 495 496
  ACG::Vec4f colorV;
  colorV[0] = color.redF();
  colorV[1] = color.greenF();
  colorV[2] = color.blueF();
  colorV[3] = color.alphaF();
497

498
  object->setBaseColor( colorV );
Jan Möbius's avatar
 
Jan Möbius committed
499

Jan Möbius's avatar
Jan Möbius committed
500
  // ================================================================================
501
  // Set defaults for DrawMode, ViewingDirection and Projection if this is the first opened object
Jan Möbius's avatar
Jan Möbius committed
502 503 504
  // If a plugin changes the drawmode later, this setting will be overridden!
  // ================================================================================
  if ( PluginFunctions::objectCount() == 1 && OpenFlipper::Options::gui() && !OpenFlipper::Options::loadingSettings() )
505 506
    for ( unsigned int i = 0 ; i < OpenFlipper::Options::examinerWidgets() ; ++i ){
      PluginFunctions::viewerProperties(i).drawMode( OpenFlipper::Options::defaultDrawMode(i) );
507

508 509 510 511 512 513 514
      if ( OpenFlipper::Options::defaultProjectionMode(i) == 0 )
	PluginFunctions::orthographicProjection(i);
      else
	PluginFunctions::perspectiveProjection(i);

      PluginFunctions::setFixedView(OpenFlipper::Options::defaultViewingDirection(i), i );
    }
Jan Möbius's avatar
Jan Möbius committed
515 516 517

  // ================================================================================
  // Recompute bounding box and scenegraph info
518
  // Reset scene center here to include new object
Jan Möbius's avatar
Jan Möbius committed
519
  // ================================================================================
520
  resetScenegraph(true);
Jan Möbius's avatar
 
Jan Möbius committed
521

Jan Möbius's avatar
Jan Möbius committed
522 523 524
  // ================================================================================
  // Tell plugins, that a file has been opened
  // ================================================================================
525
  emit openedFile( _id );
Jan Möbius's avatar
 
Jan Möbius committed
526

Jan Möbius's avatar
Jan Möbius committed
527 528 529
  // ================================================================================
  // Tell plugins, that the Object is updated and the active object has changed
  // ================================================================================
530 531
  emit signalObjectUpdated(_id);
  emit objectSelectionChanged( _id );
Jan Möbius's avatar
 
Jan Möbius committed
532

Jan Möbius's avatar
Jan Möbius committed
533 534 535
  // ================================================================================
  // Create initial backup
  // ================================================================================
536
  backupRequest(_id,"Original Object");
Jan Möbius's avatar
 
Jan Möbius committed
537

538 539 540
  // ================================================================================
  // Add the file to the recent files menu
  // ================================================================================
541 542 543 544
  QString filename = object->path() + OpenFlipper::Options::dirSeparator() + object->name();
  BaseObject* object2;
  PluginFunctions::getObject(_id,object2);
  if ( OpenFlipper::Options::gui() )
Jan Möbius's avatar
 
Jan Möbius committed
545 546
    coreWidget_->addRecent( filename, object2->dataType() );

Jan Möbius's avatar
Jan Möbius committed
547 548 549
  // ================================================================================
  // if this is the first object opend, reset the global view
  // ================================================================================
550
  if ( PluginFunctions::objectCount() == 1 && OpenFlipper::Options::gui() && !OpenFlipper::Options::loadingSettings() )
551 552 553
    for ( unsigned int i = 0 ; i < OpenFlipper::Options::examinerWidgets() ; ++i ) {
      coreWidget_->examiner_widgets_[i]->viewAll();
    }
554

Jan Möbius's avatar
 
Jan Möbius committed
555
  // objectRoot_->dumpTree();
556
}
Jan Möbius's avatar
 
Jan Möbius committed
557 558

 /// Slot gets called after a file-plugin has opened an object
559 560 561 562
void Core::slotEmptyObjectAdded ( int _id ) {
  if ( OpenFlipper::Options::doSlotDebugging() ) {
    if ( sender() != 0 ) {
      if ( sender()->metaObject() != 0 ) {
563
        emit log(LOGINFO,tr("slotEmptyObjectAdded( ") + QString::number(_id) + tr(" ) called by ") +
564 565 566
                  QString( sender()->metaObject()->className() ) );
      }
    } else {
567
      emit log(LOGINFO,tr("slotEmptyObjectAdded( ") + QString::number(_id) + tr(" ) called by Core") );
568 569 570
    }
  }

571 572 573 574 575
  // get the opened object
  BaseObjectData* object;
  PluginFunctions::getObject(_id,object);

  emit emptyObjectAdded( _id );
576

Jan Möbius's avatar
 
Jan Möbius committed
577
  // Tell the Plugins that the Object List and the active object have changed
578 579
  emit signalObjectUpdated(_id);
  emit objectSelectionChanged(_id);
Jan Möbius's avatar
 
Jan Möbius committed
580

581
  backupRequest(_id,"Original Object");
Jan Möbius's avatar
 
Jan Möbius committed
582

583
  ///@todo : set a default path for new objects
Jan Möbius's avatar
 
Jan Möbius committed
584 585 586
//    QString filename = object->path() + OpenFlipper::Options::dirSeparator() + object->name();

//    addRecent(filename);
587
}
Jan Möbius's avatar
 
Jan Möbius committed
588 589 590 591 592 593 594

//========================================================================================
// ===             Menu Slots                                 ============================
//========================================================================================

/// Opens AddEmpty-Object widget
void Core::slotAddEmptyObjectMenu() {
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619
  std::vector< DataType > types;
  QStringList             typeNames;
  
  DataType currentType = 2;
  
  // Iterate over all Types known to the core
  // Start at 2:
  // 0 type is defined as DATA_UNKNOWN
  // 1 type is defined as DATA_GROUP
  // Therefore we have two types less then reported
  // 
  for ( uint i = 0 ; i < typeCount() - 2  ; ++i) {
    
    // iterate over all support types (created from plugins on load.
    // Check if a plugin supports addEmpty for the current type.
    // Only if the type is supported, add it to the addEmpty Dialog
    for ( uint j = 0 ; j < supportedTypes_.size(); j++) {
      
      // Check if a plugin supports the current Type
      if ( supportedTypes_[j].type & currentType ) {
        types.push_back(currentType);
        typeNames.push_back( typeName( currentType ) );
        
        // Stop here as we need only one plugin supporting addEmpty for a given type
        break;
Jan Möbius's avatar
 
Jan Möbius committed
620
      }
621 622 623 624 625 626 627 628 629 630 631
    }
    
    // Advance to next type ( Indices are bits so multiply by to to get next bit)
    currentType *= 2;
  }
  
  static addEmptyWidget* widget = 0;
  
  if (supportedTypes_.size() != 0) {
    
    if ( !widget ){
Jan Möbius's avatar
 
Jan Möbius committed
632
      widget = new addEmptyWidget(types,typeNames);
633
      widget->setWindowIcon( OpenFlipper::Options::OpenFlipperIcon() );
Jan Möbius's avatar
 
Jan Möbius committed
634 635
      connect(widget,SIGNAL(chosen(DataType, int&)),this,SLOT(slotAddEmptyObject(DataType, int&)));
    }
636
    
Jan Möbius's avatar
 
Jan Möbius committed
637
    widget->show();
638 639 640 641
    
  } else
    emit log(LOGERR,tr("Could not show 'add Empty' dialog. Missing file-plugins ?"));
  
Jan Möbius's avatar
 
Jan Möbius committed
642 643
}

644 645 646 647
//========================================================================================
// ===             Public Slots                                 ============================
//========================================================================================

Jan Möbius's avatar
 
Jan Möbius committed
648
/// Open Load-Object Widget
649 650 651 652 653
void Core::loadObject() {

  if ( OpenFlipper::Options::gui() ){

    if (supportedTypes_.size() != 0){
654
      LoadWidget* widget = new LoadWidget(supportedTypes_);
Dirk Wilden's avatar
Dirk Wilden committed
655 656
      connect(widget,SIGNAL(load(QString, int)),this,SLOT(slotLoad(QString, int)));
      connect(widget,SIGNAL(save(int, QString, int)),this,SLOT(saveObject(int, QString, int)));
Jan Möbius's avatar
Jan Möbius committed
657

658 659
      widget->setWindowIcon( OpenFlipper::Options::OpenFlipperIcon() );

660
      widget->showLoad();
661 662 663 664

      widget->disconnect();
      delete widget;

665
    }else
666
      emit log(LOGERR,tr("Could not show 'load objects' dialog. Missing file-plugins."));
Jan Möbius's avatar
 
Jan Möbius committed
667

668
  }
Jan Möbius's avatar
 
Jan Möbius committed
669 670
}

671 672 673 674 675 676
/// Load settings from file
void Core::loadSettings(){

  if ( OpenFlipper::Options::gui() ){

    QString complete_name;
Jan Möbius's avatar
Jan Möbius committed
677

678 679 680 681 682 683

    QFileDialog fileDialog( coreWidget_,
                            tr("Load Settings"),
                            OpenFlipper::Options::currentDirStr(),
                            tr("INI files (*.ini);;OBJ files (*.obj )") );

684
    fileDialog.setOption (QFileDialog::DontUseNativeDialog, true);
685 686 687 688 689 690 691
    fileDialog.setAcceptMode ( QFileDialog::AcceptOpen );
    fileDialog.setFileMode ( QFileDialog::AnyFile );

    QGridLayout *layout = (QGridLayout*)fileDialog.layout();

    QGroupBox* optionsBox = new QGroupBox( &fileDialog ) ;
    optionsBox->setSizePolicy( QSizePolicy ( QSizePolicy::Expanding , QSizePolicy::Preferred ) );
692
    optionsBox->setTitle(tr("Options"));
693 694 695
    layout->addWidget( optionsBox, layout->rowCount() , 0 , 1,layout->columnCount() );

    QCheckBox *loadProgramSettings = new QCheckBox(optionsBox);
696 697
    loadProgramSettings->setText(tr("Load program settings"));
    loadProgramSettings->setToolTip(tr("Load all current program settings from the file ( This will include view settings, colors,...) "));
698 699 700
    loadProgramSettings->setCheckState( Qt::Unchecked );

    QCheckBox *loadPluginSettings = new QCheckBox(optionsBox);
701 702
    loadPluginSettings->setText(tr("Load per Plugin Settings"));
    loadPluginSettings->setToolTip(tr("Plugins should load their current global settings from the file"));
703 704 705
    loadPluginSettings->setCheckState( Qt::Checked );

    QCheckBox *loadObjectInfo = new QCheckBox(optionsBox);
706 707
    loadObjectInfo->setText(tr("Load all objects defined in the file"));
    loadObjectInfo->setToolTip(tr("Load all objects which are defined in the file"));
708 709 710 711 712 713 714 715
    loadObjectInfo->setCheckState( Qt::Checked );

    QBoxLayout* frameLayout = new QBoxLayout(QBoxLayout::TopToBottom,optionsBox);
    frameLayout->addWidget( loadProgramSettings , 0 , 0);
    frameLayout->addWidget( loadPluginSettings  , 1 , 0);
    frameLayout->addWidget( loadObjectInfo      , 2 , 0);
    frameLayout->addStretch();

Jan Möbius's avatar
Jan Möbius committed
716 717
    fileDialog.resize(550 ,500);

718 719 720 721 722 723 724 725 726 727 728 729
    // ========================================================================================
    // show the saveSettings-Dialog and get the target file
    // ========================================================================================
    QStringList fileNames;
    if (fileDialog.exec()) {
      fileNames = fileDialog.selectedFiles();
    } else {
      return;
    }

    if ( fileNames.size() > 1 ) {
      std::cerr << "Too many save filenames selected" << std::endl;
730
      return;
731 732 733 734
    }

    complete_name = fileNames[0];

Jan Möbius's avatar
Jan Möbius committed
735

736 737
    QString newpath = complete_name.section(OpenFlipper::Options::dirSeparator(),0,-2);
    OpenFlipper::Options::currentDir(newpath);
Jan Möbius's avatar
Jan Möbius committed
738

739
    if ( complete_name.endsWith("ini") ) {
740 741 742 743
      openIniFile( complete_name,
                   loadProgramSettings->isChecked(),
                   loadPluginSettings->isChecked(),
                   loadObjectInfo->isChecked());
744 745
      if ( loadProgramSettings->isChecked() )
        applyOptions();
746 747
    } else if ( complete_name.endsWith("obj") ) {
      openObjFile(complete_name);
748 749
      if ( loadProgramSettings->isChecked() )
        applyOptions();
750
    }
Jan Möbius's avatar
Jan Möbius committed
751

752
    coreWidget_->addRecent(complete_name, DATA_UNKNOWN);
753 754
  }
}
Jan Möbius's avatar
 
Jan Möbius committed
755

756 757
/// Load settings from file
void Core::loadSettings(QString _filename){
Jan Möbius's avatar
Jan Möbius committed
758

759
  if ( !QFile(_filename).exists() )
Jan Möbius's avatar
 
Jan Möbius committed
760 761
    return;

762
  QString newpath = _filename.section(OpenFlipper::Options::dirSeparator(),0,-2);
Jan Möbius's avatar
 
Jan Möbius committed
763 764
  OpenFlipper::Options::currentDir(newpath);

765
  if ( _filename.endsWith("ini") ) {
766
    // Loaded function for recent files. Load everything.
767
    openIniFile(_filename,true,true,true);
Jan Möbius's avatar
 
Jan Möbius committed
768
    applyOptions();
769 770
  } else if ( _filename.endsWith("obj") ) {
    openObjFile(_filename);
Jan Möbius's avatar
 
Jan Möbius committed
771 772 773 774
    applyOptions();
  }

}