openFunctions.cc 29.5 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
\*===========================================================================*/
Jan Möbius's avatar
 
Jan Möbius committed
42
43
44
45
46
47
48
49
50
51





#include "Core.hh"

#include <ACG/QtWidgets/QtFileDialog.hh>

#include "OpenFlipper/common/GlobalOptions.hh"
52
#include <OpenFlipper/common/RecentFiles.hh>
Jan Möbius's avatar
 
Jan Möbius committed
53
54
55
56
57
#include "OpenFlipper/BasePlugin/PluginFunctions.hh"

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

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

61
#include <OpenFlipper/common/DataTypes.hh>
62

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

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

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

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

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



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

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

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

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

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

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

  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());
125
126
}

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

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

130
  // Update logger
Jan Möbius's avatar
Jan Möbius committed
131
132
  if ( OpenFlipper::Options::gui())
    coreWidget_->updateInSceneLoggerGeometry();
133

Jan Möbius's avatar
Jan Möbius committed
134
135
136
137
  //check if we have scripting support:
  bool scriptingSupport = false;
  slotPluginExists("scripting",scriptingSupport);
  if ( ! scriptingSupport ) {
138
    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
139
140
  }

Jan Möbius's avatar
Jan Möbius committed
141
142
143
  // 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
144
145
  if ( scriptingSupport ) {

Jan Möbius's avatar
Jan Möbius committed
146
    // Get the files in the directory
Jan Möbius's avatar
Jan Möbius committed
147
148
149
    QDir scriptDir = OpenFlipper::Options::scriptDir();
    QStringList scriptFiles = scriptDir.entryList(QDir::Files,QDir::Name);

Jan Möbius's avatar
Jan Möbius committed
150
    // Execute all files ending with ofs
Jan Möbius's avatar
Jan Möbius committed
151
    for ( int i = 0 ; i  < scriptFiles.size(); ++i )
152
      if ( scriptFiles[i].endsWith("ofs",Qt::CaseInsensitive) )
153
        emit executeFileScript(scriptDir.path() + "/" + scriptFiles[i]);
Jan Möbius's avatar
Jan Möbius committed
154

155
156
157
    // Clear scripting window afterexecuting the coresubdir scripts
    bool ok = false;
    slotCall( "scripting" ,"clearEditor()",ok);
Jan Möbius's avatar
Jan Möbius committed
158
159
  }

Jan Möbius's avatar
Jan Möbius committed
160
  // Open all files given at the commandline
161
  for ( uint i = 0 ; i < commandLineFileNames_.size() ; ++i ) {
162
163

    // Skip scripts here as they will be handled by a different function
164
    QString tmp = QString::fromStdString(commandLineFileNames_[i].first);
165
    if ( tmp.endsWith("ofs",Qt::CaseInsensitive) ) {
166
167
168
169
      commandLineScriptNames_.push_back(commandLineFileNames_[i].first);
      continue;
    }

Jan Möbius's avatar
Jan Möbius committed
170
    // If the file was given with the polymesh option, open them as polymeshes.
171
    if (commandLineFileNames_[i].second)
172
173
174
175
      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
176
177
  }

Jan Möbius's avatar
Jan Möbius committed
178
  // If we have scripting support, execute the scripts given at the commandline.
Jan Möbius's avatar
Jan Möbius committed
179
  if ( scriptingSupport )
180
    for ( uint i = 0 ; i < commandLineScriptNames_.size() ; ++i ) {
181
      emit executeFileScript(QString::fromStdString(commandLineScriptNames_[i]));
182
    }
183

Jan Möbius's avatar
Jan Möbius committed
184
185
  // 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
186
187
  if ( !OpenFlipper::Options::gui() && !OpenFlipper::Options::remoteControl())
    exitApplication();
Jan Möbius's avatar
 
Jan Möbius committed
188
189
}

190

Jan Möbius's avatar
Jan Möbius committed
191
192
193
194
195
196
197
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.
  */
  
198
  if (_filename.endsWith(".ini",Qt::CaseInsensitive)) {
199
200

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

Jan Möbius's avatar
 
Jan Möbius committed
203
    if ( OpenFlipper::Options::gui() )
204
      coreWidget_->addRecent(_filename, DATA_UNKNOWN);
205

Jan Möbius's avatar
 
Jan Möbius committed
206
    return -2;
207
  } else if (_filename.endsWith(".ofs",Qt::CaseInsensitive)) {
Jan Möbius's avatar
Jan Möbius committed
208
     emit log(LOGINFO ,tr("Starting script execution of %1.").arg( _filename)) ;
Jan Möbius's avatar
 
Jan Möbius committed
209
     emit executeFileScript(_filename);
Dirk Wilden's avatar
Dirk Wilden committed
210
211
212
  } else {
    
    QFileInfo fi(_filename);
213

214
    for (int i=0; i < (int)supportedTypes().size(); i++){
Dirk Wilden's avatar
Dirk Wilden committed
215

216
      QString filters = supportedTypes()[i].plugin->getLoadFilters();
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233

      // Only take the parts inside the brackets
      filters = filters.section("(",1).section(")",0,0);

      // Split into blocks
      QStringList separateFilters = filters.split(" ");

      bool found = false;

      // for all filters associated with this plugin
      for ( int filterId = 0 ; filterId < separateFilters.size(); ++filterId ) {
        separateFilters[filterId] = separateFilters[filterId].trimmed();

        //check extension
        if ( separateFilters[filterId].endsWith( "*." + fi.completeSuffix() , Qt::CaseInsensitive) ) {
          found = true;
          break;
234
        }
235
236
237
238
239
240
241

        if (  separateFilters[filterId].endsWith( "*." + fi.suffix() , Qt::CaseInsensitive) ) {
          found = true;
          emit log(LOGWARN,"Found supported datatype but only the suffix is matched not the complete suffix!");
          break;
        }

Jan Möbius's avatar
Jan Möbius committed
242
      }
Dirk Wilden's avatar
Dirk Wilden committed
243

244
245
246
247
      // continue processing only if found
      if ( ! found )
        continue;

Dirk Wilden's avatar
Dirk Wilden committed
248
249
250
251
252
253
254
      if ( OpenFlipper::Options::gui() ) {
        coreWidget_->statusMessage( tr("Loading %1 ... ").arg(_filename));
        if ( !OpenFlipper::Options::loadingSettings() )
          coreWidget_->setStatus(ApplicationStatus::PROCESSING );
      }

      //load file
255
      int id = supportedTypes()[i].plugin->loadObject(_filename);
Dirk Wilden's avatar
Dirk Wilden committed
256
257

      if ( OpenFlipper::Options::gui() ) {
258
        if ( id != -1 ) {
Dirk Wilden's avatar
Dirk Wilden committed
259
          coreWidget_->statusMessage( tr("Loading %1 ... done").arg(_filename), 4000 );
260
261
262
263
          
          // Get the object to figure out the data type
          BaseObject* object;
          PluginFunctions::getObject(id,object);
264
265
266
267
268
269
270
271
272
273

          // Security check, if object really exists         
          if ( object != 0 ) { 

            // Add to recent files with the given datatype
            if ( OpenFlipper::Options::gui() )
              coreWidget_->addRecent(_filename, object->dataType());
          } else {
            emit log(LOGERR, tr("Unable to add recent as object with id %1 could not be found!").arg(id) );
          }
274
275
          
        } else
Dirk Wilden's avatar
Dirk Wilden committed
276
277
278
279
280
281
282
283
284
285
286
          coreWidget_->statusMessage( tr("Loading %1 ... failed!").arg(_filename), 4000 );

        if ( !OpenFlipper::Options::loadingSettings() )
          coreWidget_->setStatus(ApplicationStatus::READY );
      }

      return id;
    }
  }

  emit log(LOGERR, tr("Unable to load object (type unknown). No suitable plugin found!") );
Jan Möbius's avatar
 
Jan Möbius committed
287
288
289
290
291
292

  return -1;
}

/// Function for loading a given file
int Core::loadObject( DataType _type, QString _filename) {
Jan Möbius's avatar
Jan Möbius committed
293
294
295
  /** \todo this function has to be checked. test for the plugin which can handle 
            the given file and then use it. 
  */
296
  
297
  if (_type == DATA_UNKNOWN)
Jan Möbius's avatar
 
Jan Möbius committed
298
299
    return loadObject(_filename);

Dirk Wilden's avatar
Dirk Wilden committed
300
301
  QFileInfo fi(_filename);
  
302
303
  for (int i=0; i < (int)supportedTypes().size(); i++)
    if (supportedTypes()[i].type & _type || supportedTypes()[i].type == _type) {
Jan Möbius's avatar
 
Jan Möbius committed
304

305
      QString filters = supportedTypes()[i].plugin->getLoadFilters();
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

      // Only take the parts inside the brackets
      filters = filters.section("(",1).section(")",0,0);

      // Split into blocks
      QStringList separateFilters = filters.split(" ");

      bool found = false;

      // for all filters associated with this plugin
      for ( int filterId = 0 ; filterId < separateFilters.size(); ++filterId ) {
        separateFilters[filterId] = separateFilters[filterId].trimmed();

        //check extension
        if ( separateFilters[filterId].endsWith( "*." + fi.completeSuffix() , Qt::CaseInsensitive) ) {
          found = true;
          break;
        }

        if (  separateFilters[filterId].endsWith( "*." + fi.suffix() , Qt::CaseInsensitive) ) {
          found = true;
          emit log(LOGWARN,"Found supported datatype but only the suffix is matched not the complete suffix!");
          break;
        }

      }

      // continue processing only if found
      if ( ! found )
Dirk Wilden's avatar
Dirk Wilden committed
335
336
        continue;
      
Jan Möbius's avatar
 
Jan Möbius committed
337
      if ( OpenFlipper::Options::gui() ) {
Jan Möbius's avatar
Jan Möbius committed
338
        coreWidget_->statusMessage( tr("Loading %1 ... ").arg(_filename));
339
        if ( !OpenFlipper::Options::loadingSettings() )
Jan Möbius's avatar
 
Jan Möbius committed
340
341
342
          coreWidget_->setStatus(ApplicationStatus::PROCESSING );
      }

Dirk Wilden's avatar
Dirk Wilden committed
343
344
      int id = -1;

Jan Möbius's avatar
 
Jan Möbius committed
345
      //load file
346
347
      if ( checkSlot( supportedTypes()[i].object , "loadObject(QString,DataType)" ) )
        id = supportedTypes()[i].plugin->loadObject(_filename, _type);
Dirk Wilden's avatar
Dirk Wilden committed
348
      else
349
        id = supportedTypes()[i].plugin->loadObject(_filename);
Jan Möbius's avatar
 
Jan Möbius committed
350
351

      if ( OpenFlipper::Options::gui() ) {
352
        if ( id != -1 ) {
Jan Möbius's avatar
Jan Möbius committed
353
          coreWidget_->statusMessage( tr("Loading %1 ... done").arg(_filename), 4000 );
354
355
356
357
358
359
360
361
362
363
          
          // Get the object to figure out the data type
          BaseObject* object;
          PluginFunctions::getObject(id,object);
          
          // Add to recent files with the given datatype
          if ( OpenFlipper::Options::gui() )
            coreWidget_->addRecent(_filename, object->dataType());
          
        } else
Jan Möbius's avatar
Jan Möbius committed
364
          coreWidget_->statusMessage( tr("Loading %1 ... failed!").arg(_filename), 4000 );
Jan Möbius's avatar
 
Jan Möbius committed
365

366
        if ( !OpenFlipper::Options::loadingSettings() )
Jan Möbius's avatar
 
Jan Möbius committed
367
368
369
370
371
          coreWidget_->setStatus(ApplicationStatus::READY );
      }

      return id;
    }
Dirk Wilden's avatar
Dirk Wilden committed
372
373
374
    
  emit log(LOGERR, tr("Unable to load object. No suitable plugin found!") );
    
Jan Möbius's avatar
 
Jan Möbius committed
375
376
377
  return -1; //no plugin found
}

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

Jan Möbius's avatar
 
Jan Möbius committed
379
int Core::addEmptyObject( DataType _type ) {
380
381
  // Iterate over all plugins. The first plugin supporting the addEmpty function for the
  // specified type will be used to create the new object.
382
383
384
385
386
387
388
389
390
391
392
  
  int retCode = -1;
  
  // Type plugins
  for (int i=0; i < (int)supportedDataTypes_.size(); i++)
    if ( supportedDataTypes_[i].type & _type )
      retCode = supportedDataTypes_[i].plugin->addEmpty();
  
  if(retCode != -1) return retCode;
    
  // File plugins
393
394
  for (int i=0; i < (int)supportedTypes().size(); i++)
    if ( supportedTypes()[i].type & _type ) {
395
      emit log(LOGERR, tr("File Plugins are not allowed to create empty objects anymore! Use the addEmpty call instead!") );
396
      retCode = supportedTypes()[i].plugin->addEmpty();
Jan Möbius's avatar
Jan Möbius committed
397
    }
398
399
  
  return retCode; // -1 if no plugin found
Jan Möbius's avatar
 
Jan Möbius committed
400
401
402
403
404
405
406
407
}

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

/// Slot for adding an empty object of given DataType
void Core::slotAddEmptyObject( DataType _type , int& _id ) {
408
409
410
  
  _id = addEmptyObject( _type );
  
Jan Möbius's avatar
Jan Möbius committed
411
412
413
  if ( OpenFlipper::Options::doSlotDebugging() ) {
    if ( sender() != 0 ) {
      if ( sender()->metaObject() != 0 ) {
414
        emit log(LOGINFO,"slotAddEmptyObject( " + _type.name() + "," + QString::number(_id) +  tr(" ) called by ") +
Jan Möbius's avatar
Jan Möbius committed
415
416
417
        QString( sender()->metaObject()->className() ) );
      }
    } else {
418
      emit log(LOGINFO,"slotAddEmptyObject( " + _type.name() + ","  + QString::number(_id) +  tr(" ) called by Core") );
Jan Möbius's avatar
Jan Möbius committed
419
420
    }
  }
Jan Möbius's avatar
 
Jan Möbius committed
421
422
}

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

Jan Möbius's avatar
Jan Möbius committed
426
  if ( _oldId == -1 ) {
Jan Möbius's avatar
Jan Möbius committed
427
    emit log(LOGERR,tr("Requested copy for illegal Object id: %1").arg(_oldId) );
Jan Möbius's avatar
Jan Möbius committed
428
429
430
431
432
433
434
435
    _newId = -1;
    return;
  }

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

  if ( !object ) {
Jan Möbius's avatar
Jan Möbius committed
436
    emit log(LOGERR,tr("Requested copy for unknown Object id: %1 ").arg(_oldId) );
Jan Möbius's avatar
Jan Möbius committed
437
438
439
440
441
442
443
444
    _newId = -1;
    return ;
  }

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

  if ( copy == 0 ) {
445
    emit log(LOGERR,tr("Unable to create a copy of the object."));
Jan Möbius's avatar
Jan Möbius committed
446
447
448
449
450
451
    return;
  }

  // Integrate into object tree
  copy->setParent( object->parent() );

452
  // return the new id
Jan Möbius's avatar
Jan Möbius committed
453
  _newId = copy->id();
454

455
456
457
458
459
460
  // tell plugins that a new object has been created
  slotEmptyObjectAdded(_newId);

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

461
462
}

Dirk Wilden's avatar
Dirk Wilden committed
463
464
465
466
467
468
469
470
471
472
/// 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
473
  int id = supportedTypes()[_pluginID].plugin->loadObject(_filename);
Dirk Wilden's avatar
Dirk Wilden committed
474
475
476
477
478
479
480
481
482
483
484

  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 );
  }
  
485
486
487
488
489
  // Initialize as unknown type
  DataType type = DATA_UNKNOWN;

  // An object has been added. Get it and do some processing!
  if ( id > 0 ) {
490

491
492
    BaseObjectData* object;
    PluginFunctions::getObject(id,object);
Dirk Wilden's avatar
Dirk Wilden committed
493
    
494
    if ( !object ) {
Dirk Wilden's avatar
Dirk Wilden committed
495
496
497
498
499
500
501
502
503
504
505

      BaseObject* baseObj = 0;
      GroupObject* group = 0;
      
      PluginFunctions::getObject(id,baseObj);
      
      if (baseObj){

        group = dynamic_cast< GroupObject* > (baseObj);

        if (group)
506
          type = DATA_GROUP;
Dirk Wilden's avatar
Dirk Wilden committed
507
508
509
510
511
512
      }
      
      if ( group == 0 ){
        emit log(LOGERR,tr("Object id returned but no object with this id has been found! Error in one of the file plugins!"));
        return;
      }
513
514
515
    }
    
    // Get the objects type
Dirk Wilden's avatar
Dirk Wilden committed
516
517
    if (object)
      type = object->dataType();
Dirk Wilden's avatar
Dirk Wilden committed
518
  }
519
520
  
  // If the id was greater than zero, add the file to the recent files.
Dirk Wilden's avatar
Dirk Wilden committed
521
522
523
524
525
  if ( id >= 0 )
    if ( OpenFlipper::Options::gui() )
      coreWidget_->addRecent(_filename, type);
}

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

Jan Möbius's avatar
 
Jan Möbius committed
530
531
532
  if ( _id < 0 )
    _id = -1;
  else
533
    if ( OpenFlipper::Options::gui() )
Jan Möbius's avatar
 
Jan Möbius committed
534
535
536
537
      coreWidget_->addRecent(_filename,_type);
}

/// Slot gets called after a file-plugin has opened an object
538
void Core::slotFileOpened ( int _id ) {
539
540
541
  if ( OpenFlipper::Options::doSlotDebugging() ) {
    if ( sender() != 0 ) {
      if ( sender()->metaObject() != 0 ) {
542
        emit log(LOGINFO,tr("slotObjectOpened( ") + QString::number(_id) + tr(" ) called by ") +
543
544
545
                  QString( sender()->metaObject()->className() ) );
      }
    } else {
546
      emit log(LOGINFO,tr("slotObjectOpened( ") + QString::number(_id) + tr(" ) called by Core") );
547
548
    }
  }
549
550

  // get the opened object
551
552
  BaseObjectData* object;
  PluginFunctions::getObject(_id,object);
553

Jan Möbius's avatar
Jan Möbius committed
554
555
  // ================================================================================
  // Recompute bounding box and scenegraph info
556
  // Reset scene center here to include new object
Jan Möbius's avatar
Jan Möbius committed
557
  // ================================================================================
558
  resetScenegraph(true);
Jan Möbius's avatar
 
Jan Möbius committed
559

Jan Möbius's avatar
Jan Möbius committed
560
561
562
  // ================================================================================
  // Tell plugins, that a file has been opened
  // ================================================================================
563
  emit openedFile( _id );
564
565
566
567
568
  
  // ================================================================================
  // Print Info to logger
  // ================================================================================
  emit log( LOGINFO ,object->getObjectinfo() );
Jan Möbius's avatar
 
Jan Möbius committed
569

Jan Möbius's avatar
Jan Möbius committed
570
571
572
  // ================================================================================
  // Tell plugins, that the Object is updated and the active object has changed
  // ================================================================================
Jan Möbius's avatar
Jan Möbius committed
573
574
  emit signalObjectUpdated(_id );
  emit signalObjectUpdated(_id, UPDATE_ALL);
Jan Möbius's avatar
 
Jan Möbius committed
575

Jan Möbius's avatar
Jan Möbius committed
576
577
578
  // ================================================================================
  // Create initial backup
  // ================================================================================
579
  emit createBackup(_id,"Original Object");
Jan Möbius's avatar
 
Jan Möbius committed
580

581
582
583
  // ================================================================================
  // Add the file to the recent files menu
  // ================================================================================
584
585
586
  QString filename = object->path() + OpenFlipper::Options::dirSeparator() + object->name();
  BaseObject* object2;
  PluginFunctions::getObject(_id,object2);
Jan Möbius's avatar
 
Jan Möbius committed
587

Jan Möbius's avatar
Jan Möbius committed
588
589
590
  // ================================================================================
  // if this is the first object opend, reset the global view
  // ================================================================================
591
  if ( PluginFunctions::objectCount() == 1 && OpenFlipper::Options::gui() && !OpenFlipper::Options::loadingSettings() )
592
593
594
    for ( unsigned int i = 0 ; i < OpenFlipper::Options::examinerWidgets() ; ++i ) {
      coreWidget_->examiner_widgets_[i]->viewAll();
    }
595

Jan Möbius's avatar
 
Jan Möbius committed
596
  // objectRoot_->dumpTree();
597
}
Jan Möbius's avatar
 
Jan Möbius committed
598
599

 /// Slot gets called after a file-plugin has opened an object
600
void Core::slotEmptyObjectAdded ( int _id ) {
601

602
603
604
  if ( OpenFlipper::Options::doSlotDebugging() ) {
    if ( sender() != 0 ) {
      if ( sender()->metaObject() != 0 ) {
605
        emit log(LOGINFO,tr("slotEmptyObjectAdded( ") + QString::number(_id) + tr(" ) called by ") +
606
607
608
                  QString( sender()->metaObject()->className() ) );
      }
    } else {
609
      emit log(LOGINFO,tr("slotEmptyObjectAdded( ") + QString::number(_id) + tr(" ) called by Core") );
610
611
612
    }
  }

613
614
615
616
  // get the opened object
  BaseObjectData* object;
  PluginFunctions::getObject(_id,object);

617
  
618
  emit emptyObjectAdded( _id );
619

Jan Möbius's avatar
 
Jan Möbius committed
620
  // Tell the Plugins that the Object List and the active object have changed
621
  emit signalObjectUpdated(_id);
Jan Möbius's avatar
Jan Möbius committed
622
  emit signalObjectUpdated(_id,UPDATE_ALL);
Jan Möbius's avatar
 
Jan Möbius committed
623

624
625
  resetScenegraph(false);

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

//    addRecent(filename);
630
}
Jan Möbius's avatar
 
Jan Möbius committed
631
632
633
634
635
636
637

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

/// Opens AddEmpty-Object widget
void Core::slotAddEmptyObjectMenu() {
638
639
640
641
642
643
644
645
646
647
648
649
650
  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) {
    
651
    // Iterate over all supported types (created from plugins on load)
652
653
    // Check if a plugin supports addEmpty for the current type.
    // Only if the type is supported, add it to the addEmpty Dialog
654
655
656
    
    // typePlugin
    for ( uint j = 0 ; j < supportedDataTypes_.size(); j++) {
657
      
658
659
      // Check if a plugin supports the current type
      if ( supportedDataTypes_[j].type & currentType ) {
660
661
662
663
664
        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
665
      }
666
667
    }
    
668
    // filePlugin
669
    for ( uint j = 0 ; j < supportedTypes().size(); j++) {
670
671
      
      // Check if a plugin supports the current Type
672
      if ( supportedTypes()[j].type & currentType ) {
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
	
	// Avoid duplicates
	bool duplicate = false;
	for(std::vector< DataType >::iterator it = types.begin(); it != types.end(); ++it) {
	  if(*it == currentType) {
	    duplicate = true;
	    break;
	  }
	}
	  
	if(!duplicate) {
	  types.push_back(currentType);
	  typeNames.push_back( typeName( currentType ) );
        
	  // Stop here as we need only one plugin supporting addEmpty for a given type
	  break;
	}
      }
    }
    
693
694
    // Advance to next type ( Indices are bits so multiply by two to get next bit)
    ++currentType;
695
696
697
698
  }
  
  static addEmptyWidget* widget = 0;
  
699
  if (supportedTypes().size() != 0) {
700
701
    
    if ( !widget ){
Jan Möbius's avatar
 
Jan Möbius committed
702
      widget = new addEmptyWidget(types,typeNames);
703
      widget->setWindowIcon( OpenFlipper::Options::OpenFlipperIcon() );
Jan Möbius's avatar
 
Jan Möbius committed
704
705
      connect(widget,SIGNAL(chosen(DataType, int&)),this,SLOT(slotAddEmptyObject(DataType, int&)));
    }
706
    
Jan Möbius's avatar
 
Jan Möbius committed
707
    widget->show();
708
709
710
711
    
  } else
    emit log(LOGERR,tr("Could not show 'add Empty' dialog. Missing file-plugins ?"));
  
Jan Möbius's avatar
 
Jan Möbius committed
712
713
}

714
715
716
717
//========================================================================================
// ===             Public Slots                                 ============================
//========================================================================================

Jan Möbius's avatar
 
Jan Möbius committed
718
/// Open Load-Object Widget
719
720
721
722
void Core::loadObject() {

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

723
724
    if (supportedTypes().size() != 0){
      LoadWidget* widget = new LoadWidget(supportedTypes());
Dirk Wilden's avatar
Dirk Wilden committed
725
726
      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
727

728
729
      widget->setWindowIcon( OpenFlipper::Options::OpenFlipperIcon() );

730
      widget->showLoad();
731
732
733
734

      widget->disconnect();
      delete widget;

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

738
  }
Jan Möbius's avatar
 
Jan Möbius committed
739
740
}

741
742
743
744
745
746
/// Load settings from file
void Core::loadSettings(){

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

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

748
749
750

    QFileDialog fileDialog( coreWidget_,
                            tr("Load Settings"),
751
                            OpenFlipperSettings().value("Core/CurrentDir").toString(),
752
                            tr("INI files (*.ini)") );
753

754
    fileDialog.setOption (QFileDialog::DontUseNativeDialog, true);
755
756
757
758
759
760
761
    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 ) );
762
    optionsBox->setTitle(tr("Options"));
763
764
765
    layout->addWidget( optionsBox, layout->rowCount() , 0 , 1,layout->columnCount() );

    QCheckBox *loadProgramSettings = new QCheckBox(optionsBox);
766
767
    loadProgramSettings->setText(tr("Load program settings"));
    loadProgramSettings->setToolTip(tr("Load all current program settings from the file ( This will include view settings, colors,...) "));
768
769
770
    loadProgramSettings->setCheckState( Qt::Unchecked );

    QCheckBox *loadPluginSettings = new QCheckBox(optionsBox);
771
772
    loadPluginSettings->setText(tr("Load per Plugin Settings"));
    loadPluginSettings->setToolTip(tr("Plugins should load their current global settings from the file"));
773
774
775
    loadPluginSettings->setCheckState( Qt::Checked );

    QCheckBox *loadObjectInfo = new QCheckBox(optionsBox);
776
777
    loadObjectInfo->setText(tr("Load all objects defined in the file"));
    loadObjectInfo->setToolTip(tr("Load all objects which are defined in the file"));
778
779
780
781
782
783
784
785
    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
786
787
    fileDialog.resize(550 ,500);

788
789
790
791
792
793
794
795
796
797
798
799
    // ========================================================================================
    // 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;
800
      return;
801
802
803
804
    }

    complete_name = fileNames[0];

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

806
    QString newpath = complete_name.section(OpenFlipper::Options::dirSeparator(),0,-2);
807
    OpenFlipperSettings().setValue("Core/CurrentDir", newpath);
Jan Möbius's avatar
Jan Möbius committed
808

809
    if ( complete_name.endsWith("ini",Qt::CaseInsensitive) ) {
810
811
812
813
      openIniFile( complete_name,
                   loadProgramSettings->isChecked(),
                   loadPluginSettings->isChecked(),
                   loadObjectInfo->isChecked());
814
815
      if ( loadProgramSettings->isChecked() )
        applyOptions();
816
817
    } 
    
818
    coreWidget_->addRecent(complete_name, DATA_UNKNOWN);
819
820
  }
}
Jan Möbius's avatar
 
Jan Möbius committed
821

822
823
/// Load settings from file
void Core::loadSettings(QString _filename){
Jan Möbius's avatar
Jan Möbius committed
824

825
  if ( !QFile(_filename).exists() )
Jan Möbius's avatar
 
Jan Möbius committed
826
827
    return;

828
  QString newpath = _filename.section(OpenFlipper::Options::dirSeparator(),0,-2);
829
  OpenFlipperSettings().setValue("Core/CurrentDir", newpath);
Jan Möbius's avatar
 
Jan Möbius committed
830

831
  if ( _filename.endsWith("ini",Qt::CaseInsensitive) ) {
832
    // Loaded function for recent files. Load everything.
833
    openIniFile(_filename,true,true,true);
Jan Möbius's avatar
 
Jan Möbius committed
834
    applyOptions();
835
  } else if ( _filename.endsWith("obj",Qt::CaseInsensitive) ) {
Dirk Wilden's avatar
Dirk Wilden committed
836
    loadObject(_filename);
Jan Möbius's avatar
 
Jan Möbius committed
837
838
839
840
    applyOptions();
  }

}