ParseIni.cc 27.3 KB
Newer Older
1
/*===========================================================================*\
Jan Möbius's avatar
Jan Möbius committed
2 3
*                                                                            *
*                              OpenFlipper                                   *
Jan Möbius's avatar
Jan Möbius committed
4
*      Copyright (C) 2001-2011 by Computer Graphics Group, RWTH Aachen       *
Jan Möbius's avatar
Jan Möbius committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
*                           www.openflipper.org                              *
*                                                                            *
*--------------------------------------------------------------------------- *
*  This file is part of OpenFlipper.                                         *
*                                                                            *
*  OpenFlipper is free software: you can redistribute it and/or modify       *
*  it under the terms of the GNU Lesser General Public License as            *
*  published by the Free Software Foundation, either version 3 of            *
*  the License, or (at your option) any later version with the               *
*  following exceptions:                                                     *
*                                                                            *
*  If other files instantiate templates or use macros                        *
*  or inline functions from this file, or you compile this file and          *
*  link it with other files to produce an executable, this file does         *
*  not by itself cause the resulting executable to be covered by the         *
*  GNU Lesser General Public License. This exception does not however        *
*  invalidate any other reasons why the executable file might be             *
*  covered by the GNU Lesser General Public License.                         *
*                                                                            *
*  OpenFlipper is distributed in the hope that it will be useful,            *
*  but WITHOUT ANY WARRANTY; without even the implied warranty of            *
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
*  GNU Lesser General Public License for more details.                       *
*                                                                            *
*  You should have received a copy of the GNU LesserGeneral Public           *
*  License along with OpenFlipper. If not,                                   *
*  see <http://www.gnu.org/licenses/>.                                       *
*                                                                            *
33 34 35
\*===========================================================================*/

/*===========================================================================*\
Jan Möbius's avatar
Jan Möbius committed
36 37 38 39 40
*                                                                            *
*   $Revision$                                                       *
*   $LastChangedBy$                                                *
*   $Date$                     *
*                                                                            *
41
\*===========================================================================*/
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63




//=============================================================================
//
//  CLASS Core - IMPLEMENTATION
//
//=============================================================================


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

// -------------------- mview
#include "Core.hh"
// -------------------- ACG
#include <ACG/Scenegraph/DrawModes.hh>

#include <OpenFlipper/INIFile/INIFile.hh>
#include <OpenFlipper/common/GlobalOptions.hh>
#include <OpenFlipper/common/RecentFiles.hh>

64 65
#include <ObjectTypes/Light/Light.hh>

66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
#include <OpenFlipper/BasePlugin/PluginFunctions.hh>

#include <QFile>

#include <OpenFlipper/ACGHelper/DrawModeConverter.hh>

//== IMPLEMENTATION ==========================================================


/** Load the standard options from the given Ini File
* @param _ini Inifile to use
*/
void Core::readApplicationOptions(INIFile& _ini) {
  // Parse standard options
  if ( _ini.section_exists("Options") ) {

    // load ViewModes
    int viewModeCount;
    if (_ini.get_entry(viewModeCount,"Options","ViewModeCount") )
      for (int i=0; i < viewModeCount; i++){
86

Jan Möbius's avatar
Jan Möbius committed
87 88
        QString entryToolbars;
        QString entryToolboxes;
Jan Möbius's avatar
Jan Möbius committed
89
        QString entryContextMenus;
Jan Möbius's avatar
Jan Möbius committed
90
        QString entryIcon;
91

Jan Möbius's avatar
Jan Möbius committed
92 93 94 95
        QString keyToolbars     = "ViewModeToolbars"     + QString::number(i);
        QString keyToolboxes    = "ViewModeToolboxes"    + QString::number(i);
        QString keyContextMenus = "ViewModeContextMenus" + QString::number(i);
        QString keyIcon         = "ViewModeIcon"         + QString::number(i);
Jan Möbius's avatar
Jan Möbius committed
96 97

        // Read the entries
Jan Möbius's avatar
Jan Möbius committed
98 99 100 101
        if ( !_ini.get_entry( entryToolbars     , "Options" , keyToolbars ) )     continue;
        if ( !_ini.get_entry( entryToolboxes    , "Options" , keyToolboxes ) )    continue;
        if ( !_ini.get_entry( entryContextMenus , "Options" , keyContextMenus ) ) continue;
        if ( !_ini.get_entry( entryIcon         , "Options" , keyIcon ) )         continue;
Jan Möbius's avatar
Jan Möbius committed
102

Jan Möbius's avatar
Jan Möbius committed
103 104 105
        QStringList toolBars     = entryToolbars.split(";");
        QStringList toolBoxes    = entryToolboxes.split(";");
        QStringList contextMenus = entryContextMenus.split(";");
106

Jan Möbius's avatar
Jan Möbius committed
107
        // Get Mode name ( prepended to all toolbox/toolbar/context menu lists )
Jan Möbius's avatar
Jan Möbius committed
108
        QString mode = toolBoxes.first();
109

Jan Möbius's avatar
Jan Möbius committed
110 111 112
        // Remove leading Modes
        toolBoxes.removeFirst();
        toolBars.removeFirst();
Jan Möbius's avatar
Jan Möbius committed
113
        contextMenus.removeFirst();
Jan Möbius's avatar
Jan Möbius committed
114 115

        // Check if the mode already exists
116 117 118 119
        bool found = false;
        for (int i=0; i < viewModes_.size(); i++)
          if (viewModes_[i]->name == mode)
            found = true;
120

121 122 123 124
        if (!found){
          ViewMode* vm = new ViewMode();
          vm->name = mode;
          vm->custom = true;
Jan Möbius's avatar
Jan Möbius committed
125 126 127 128
          vm->visibleToolbars     = toolBars;
          vm->visibleToolboxes    = toolBoxes;
          vm->visibleContextMenus = contextMenus;
          vm->icon                = entryIcon;
129 130 131 132 133 134 135 136 137
          viewModes_.push_back(vm);
        }

      }

    //load default dataType
    QString type;
    if (_ini.get_entry(type, "Options" , "default_DataType" ))
      OpenFlipper::Options::lastDataType(type);
Jan Möbius's avatar
Jan Möbius committed
138

139 140 141 142 143
    //============================================================================
    // Load the startup dir for file dialogs
    //============================================================================
    QString startup_dir;
    if( _ini.get_entry(startup_dir, "Options", "StartupDir") )
144
      OpenFlipperSettings().setValue("Core/CurrentDir", startup_dir );
145

Dirk Wilden's avatar
Dirk Wilden committed
146 147 148 149
    //============================================================================
    // Load the default script directory
    //============================================================================
    QString script_dir;
150
    if( _ini.get_entry(script_dir, "Options", "CurrentScriptDir") )
Dirk Wilden's avatar
Dirk Wilden committed
151 152
      OpenFlipper::Options::currentScriptDir(script_dir);

153 154 155 156 157 158 159
    //============================================================================
    // Load the default texture directory
    //============================================================================
    QString current_texture_dir;
    if( _ini.get_entry(current_texture_dir, "Options", "CurrentTextureDir") )
      OpenFlipper::Options::currentTextureDir(current_texture_dir);

160 161 162
    //============================================================================
    // Load the random base color setting
    //============================================================================
163 164 165
    bool random_base_color = false;
    if ( _ini.get_entry( random_base_color, "Options" , "RandomBaseColor") )
      OpenFlipper::Options::randomBaseColor( random_base_color );
166

167 168 169 170 171 172 173
    //============================================================================
    // Load the synchronization setting
    //============================================================================
    bool synchronization = false;
    if ( _ini.get_entry( synchronization, "Options" , "Synchronization") )
      OpenFlipper::Options::synchronization(synchronization);

174 175 176
    //============================================================================
    // Load the stereo mode setting
    //============================================================================
Jan Möbius's avatar
Jan Möbius committed
177 178 179
    //bool stereo = false;
    //if ( _ini.get_entry( stereo, "Options" , "Stereo") )
    //  OpenFlipper::Options::stereo(stereo);
180

Jan Möbius's avatar
Jan Möbius committed
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
    //============================================================================
    // Load the stereo mode setting
    //============================================================================
    int stereoMode = 0;
    if ( _ini.get_entry( stereoMode, "Options" , "StereoMode") )
      OpenFlipper::Options::stereoMode(static_cast<OpenFlipper::Options::StereoMode> (stereoMode));

    //============================================================================
    // Load the custom anaglyph stereo mode color matrices
    //============================================================================
    std::vector<float> mat;
    if ( _ini.get_entry( mat, "Options" , "CustomAnaglyphLeftEye") && mat.size () == 9)
    {
      OpenFlipper::Options::anaglyphLeftEyeColorMatrix(mat);
    }
    else
    {
      std::vector<float> set (9,0.0);
      set[0] = 0.299;
      set[3] = 0.587;
      set[6] = 0.114;
      OpenFlipper::Options::anaglyphLeftEyeColorMatrix(set);
    }

    if ( _ini.get_entry( mat, "Options" , "CustomAnaglyphRightEye") && mat.size () == 9)
    {
      OpenFlipper::Options::anaglyphRightEyeColorMatrix(mat);
    }
    else
    {
      std::vector<float> set (9,0.0);
      set[4] = 1.0;
      set[8] = 1.0;
      OpenFlipper::Options::anaglyphRightEyeColorMatrix(set);
    }

217 218 219 220 221 222 223
    //============================================================================
    // Load the setting for the object color option
    //============================================================================
    uint baseColor = 0;
    if ( _ini.get_entry( baseColor, "Options" , "DefaultBaseColor") )
      OpenFlipper::Options::defaultBaseColor(QRgb(baseColor));

224 225 226
    //============================================================================
    // Load the setting for the default Toolbox mode
    //============================================================================
Jan Möbius's avatar
Jan Möbius committed
227
    QString viewmode = "";
Jan Möbius's avatar
Jan Möbius committed
228 229
    if ( _ini.get_entry( viewmode, "Options" , "CurrentViewMode") )
      OpenFlipper::Options::currentViewMode(viewmode);
230

231 232 233 234 235 236 237
    //============================================================================
    // Load the setting for the gridVisibility
    //============================================================================
    bool visible = false;
    if ( _ini.get_entry( visible, "Options" , "GridVisible") )
      OpenFlipper::Options::gridVisible(visible);

238 239 240 241 242 243 244
    //============================================================================
    // Load the setting for the viewer layout
    //============================================================================
    int viewerLayout = 0;
    if ( _ini.get_entry( viewerLayout, "Options" , "DefaultViewerLayout") )
      OpenFlipper::Options::defaultViewerLayout(viewerLayout);

245
    //============================================================================
246
    // Load the viewer settings
247 248
    //============================================================================
    std::vector< QString > draw_modes;
249 250 251 252 253
    int mode;

    for (int i=0; i < 4/*PluginFunctions::viewers()*/; i++ ){

      if( _ini.get_entry(draw_modes, "Options", "DefaultDrawModes" + QString::number(i) ) )
Jan Möbius's avatar
Jan Möbius committed
254
        OpenFlipper::Options::defaultDrawMode( listToDrawMode(draw_modes), i );
255 256

    }
Dirk Wilden's avatar
Dirk Wilden committed
257

Jan Möbius's avatar
Jan Möbius committed
258 259 260 261 262 263 264
    //============================================================================
    // Load slotDebugging state
    //============================================================================
    bool doSlotDebugging = false;
    if( _ini.get_entry(doSlotDebugging, "Options", "SlotDebugging") )
      OpenFlipper::Options::doSlotDebugging(doSlotDebugging);

Dirk Wilden's avatar
Dirk Wilden committed
265 266 267 268
    //============================================================================
    // ViewerProperties
    //============================================================================

Dirk Wilden's avatar
Dirk Wilden committed
269
    uint viewerCount = 0;
Dirk Wilden's avatar
Dirk Wilden committed
270 271 272
    if( _ini.get_entry(viewerCount, "Options", "ViewerCount") ){
    }

273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
    for ( unsigned int i = 0 ; i < viewerCount; ++i ) {

      if (OpenFlipper::Options::examinerWidgets() < i)
        break;

      QString entryHeader = "Viewer" + QString::number(i) + "/";

      // Load the animation setting
      PluginFunctions::viewerProperties(i).animation(OpenFlipperSettings().value(entryHeader+"Animation",false).toBool());

      // Load the twoSidedLighting setting
      PluginFunctions::viewerProperties(i).twoSidedLighting(OpenFlipperSettings().value(entryHeader+"TwoSidedLighting",false).toBool());

      // Load the backface culling setting
      PluginFunctions::viewerProperties(i).backFaceCulling(OpenFlipperSettings().value(entryHeader+"BackfaceCulling",false).toBool());

      // Load the setting for the background color option
      PluginFunctions::viewerProperties(i).backgroundColor(OpenFlipperSettings().value(entryHeader+"BackgroundColor",QColor(0,0,0)).value< QColor >());
Dirk Wilden's avatar
Dirk Wilden committed
291
    }
Dirk Wilden's avatar
Dirk Wilden committed
292

293 294 295
  }
}

296
/** Write the standard options to the given Ini File
297 298
* @param _ini Inifile to use
*/
299
void Core::writeApplicationOptions(INIFile& _ini) {
300 301

  // Write maximum recent file count to ini
Jan Möbius's avatar
Jan Möbius committed
302
  _ini.add_entry("Options","MaxRecent", OpenFlipperSettings().value("Core/File/MaxRecent",15).toInt() );
303 304

  // Write list of recent files to ini
305 306
  QStringList recentFiles = OpenFlipperSettings().value("Core/File/RecentFiles", QStringList()).toStringList();
  QStringList recentTypes = OpenFlipperSettings().value("Core/File/RecentTypes", QStringList()).toStringList();
307
  
308 309 310
  for ( int j = 0 ; j < recentFiles.size(); ++j) {
    // Save filename
    QString key = "recent" + QString::number(j);
311
    QString filename = recentFiles[j];
312 313 314
    _ini.add_entry( "Options" , key , filename );
    // Save DataType
    key = "type" + QString::number(j);
315
    QString type = recentTypes[j];
316 317 318 319
    _ini.add_entry( "Options" , key , type );
  }

  // save ViewModes
Jan Möbius's avatar
Jan Möbius committed
320 321
  QVector< QString > toolboxes;
  QVector< QString > toolbars;
Jan Möbius's avatar
Jan Möbius committed
322
  QVector< QString > contextmenus;
Jan Möbius's avatar
Jan Möbius committed
323
  QVector< QString > icons;
324

325 326 327 328 329
  if ( OpenFlipper::Options::gui() )
    for (int i=0; i < coreWidget_->viewModes_.size(); i++)
      if (coreWidget_->viewModes_[i]->custom){

        //store name
Jan Möbius's avatar
Jan Möbius committed
330
        QString entryToolboxes = coreWidget_->viewModes_[i]->name;
331 332

        //store widgets
Jan Möbius's avatar
Jan Möbius committed
333
        for (int j=0; j < coreWidget_->viewModes_[i]->visibleToolboxes.size(); j++)
Jan Möbius's avatar
Jan Möbius committed
334 335 336
          entryToolboxes += ";" + coreWidget_->viewModes_[i]->visibleToolboxes[j];

        toolboxes.push_back(entryToolboxes);
337

Jan Möbius's avatar
Jan Möbius committed
338 339
        //store name
        QString entryToolbars = coreWidget_->viewModes_[i]->name;
340

Jan Möbius's avatar
Jan Möbius committed
341 342 343
        //store widgets
        for (int j=0; j < coreWidget_->viewModes_[i]->visibleToolbars.size(); j++)
          entryToolbars += ";" + coreWidget_->viewModes_[i]->visibleToolbars[j];
344

Jan Möbius's avatar
Jan Möbius committed
345
        toolbars.push_back(entryToolbars);
Jan Möbius's avatar
Jan Möbius committed
346 347
	
	QString entryContextMenus = coreWidget_->viewModes_[i]->name;
348

Jan Möbius's avatar
Jan Möbius committed
349 350 351
        //store widgets
        for (int j=0; j < coreWidget_->viewModes_[i]->visibleContextMenus.size(); j++)
          entryContextMenus += ";" + coreWidget_->viewModes_[i]->visibleContextMenus[j];
352

Jan Möbius's avatar
Jan Möbius committed
353 354 355
        contextmenus.push_back(entryContextMenus);

        icons.push_back(coreWidget_->viewModes_[i]->icon);
356 357 358
      }

  //save viewmodes to ini
Jan Möbius's avatar
Jan Möbius committed
359 360
  _ini.add_entry("Options","ViewModeCount" ,toolboxes.size());
  for (int i=0; i < toolboxes.size(); i++) {
Jan Möbius's avatar
Jan Möbius committed
361 362 363 364
    _ini.add_entry("Options","ViewModeToolboxes"     + QString::number(i) ,toolboxes[i]);
    _ini.add_entry("Options","ViewModeToolbars"      + QString::number(i) ,toolbars[i] );
    _ini.add_entry("Options","ViewModeContextMenus"  + QString::number(i) ,contextmenus[i] );
    _ini.add_entry("Options","ViewModeIcon"          + QString::number(i) ,icons[i] );
Jan Möbius's avatar
Jan Möbius committed
365
  }
366

367 368 369 370
  //save KeyBindings
  if ( OpenFlipper::Options::gui() )
    coreWidget_->saveKeyBindings(_ini);

371 372 373
  //write default dataType to INI
  _ini.add_entry( "Options" , "default_DataType" , OpenFlipper::Options::lastDataType() );

Jan Möbius's avatar
Jan Möbius committed
374 375
  //write current ViewMode
  _ini.add_entry("Options","CurrentViewMode",OpenFlipper::Options::currentViewMode() );
376

Jan Möbius's avatar
Jan Möbius committed
377 378 379 380 381
  //============================================================================
  // Debugging
  //============================================================================
  _ini.add_entry("Options","SlotDebugging",OpenFlipper::Options::doSlotDebugging() );

382 383 384 385 386 387
  if ( !_ini.section_exists("Plugins") )
    _ini.add_section("Plugins");

  if ( !_ini.section_exists("Options") )
    _ini.add_section("Options");

388
  QString dir = OpenFlipperSettings().value("Core/CurrentDir").toString().toUtf8();
389
  _ini.add_entry("Options","StartupDir",dir);
Dirk Wilden's avatar
Dirk Wilden committed
390 391

  QString scriptDir = OpenFlipper::Options::currentScriptDirStr().toUtf8();
392 393 394 395
  _ini.add_entry("Options","CurrentScriptDir",scriptDir);

  QString current_texture_dir = OpenFlipper::Options::currentTextureDirStr().toUtf8();
  _ini.add_entry("Options","CurrentTextureDir",current_texture_dir);
Dirk Wilden's avatar
Dirk Wilden committed
396

397
  _ini.add_entry("Options","RandomBaseColor", OpenFlipper::Options::randomBaseColor() );
398 399 400

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

401
    _ini.add_entry("Options","DefaultViewerLayout", OpenFlipper::Options::defaultViewerLayout() );
402
    _ini.add_entry("Options","GridVisible", OpenFlipper::Options::gridVisible() );
403

Dirk Wilden's avatar
Dirk Wilden committed
404
    _ini.add_entry("Options","ViewerCount", OpenFlipper::Options::examinerWidgets() );
Jan Möbius's avatar
Jan Möbius committed
405

Dirk Wilden's avatar
Dirk Wilden committed
406
    for ( unsigned int i = 0 ; i < OpenFlipper::Options::examinerWidgets(); ++i ) {
407 408 409 410 411
      QString entryHead = "Viewer" + QString::number(i) + "/";
      OpenFlipperSettings().setValue(entryHead + "Animation", PluginFunctions::viewerProperties(i).animation());
      OpenFlipperSettings().setValue(entryHead + "BackfaceCulling", PluginFunctions::viewerProperties(i).backFaceCulling());
      OpenFlipperSettings().setValue(entryHead + "TwoSidedLighting", PluginFunctions::viewerProperties(i).twoSidedLighting());
      OpenFlipperSettings().setValue(entryHead + "BackgroundColor", PluginFunctions::viewerProperties(i).backgroundQColor());
Dirk Wilden's avatar
Dirk Wilden committed
412
    }
413

414
    //============================================================================
415
    // Save the current viewer properties
416 417
    //============================================================================
    std::vector< QString > draw_modes;
418 419 420 421 422 423 424

    for (int i=0; i < PluginFunctions::viewers(); i++ ){

      draw_modes = drawModeToList( OpenFlipper::Options::defaultDrawMode(i) );
      _ini.add_entry("Options","DefaultDrawModes" + QString::number(i), draw_modes);

    }
425

426
    _ini.add_entry("Options","DefaultBaseColor", (uint)OpenFlipper::Options::defaultBaseColor().rgba ()  );
Jan Möbius's avatar
Jan Möbius committed
427

428
    _ini.add_entry("Options", "StereoMode",OpenFlipper::Options::stereoMode() );
Jan Möbius's avatar
Jan Möbius committed
429 430 431

    _ini.add_entry("Options" , "CustomAnaglyphLeftEye", OpenFlipper::Options::anaglyphLeftEyeColorMatrix() );
    _ini.add_entry("Options" , "CustomAnaglyphRightEye", OpenFlipper::Options::anaglyphRightEyeColorMatrix() );
432 433
  }

434 435
  emit saveOnExit(_ini);

Jan Möbius's avatar
Jan Möbius committed
436
  // _ini.add_entry("Options","Stereo",OpenFlipper::Options::stereo() );
437 438
}

439 440 441 442
void Core::openIniFile( QString _filename,
                        bool    _coreSettings,
                        bool    _perPluginSettings,
                        bool    _loadObjects ){
443 444 445
  INIFile ini;

  if ( ! ini.connect(_filename,false) ) {
446
    emit log(LOGERR,tr("Failed to connect to ini file") + _filename);
447 448
    return;
  }
Jan Möbius's avatar
Jan Möbius committed
449

450
  if ( OpenFlipper::Options::gui() ) {
451
    coreWidget_->statusMessage( tr("Loading ini File ") + _filename + " ...");
452 453 454
    coreWidget_->setStatus(ApplicationStatus::BLOCKED );
  }

455
  // Tell plugins that we are currently reading an ini file
456
  OpenFlipper::Options::loadingSettings(true);
457

458 459 460
  // Load Core settings only if requested
  if ( _coreSettings )
    readApplicationOptions(ini);
461

462 463 464
  // if requested load per Plugin settings from the settings file
  if ( _perPluginSettings )
    emit iniLoadOptions( ini );
465

466
  if ( _loadObjects ) {
467

468
    QStringList openFiles;
469

470 471
    // Parse File section for files to open
    if ( ini.section_exists("OpenFiles") && ini.get_entry(openFiles,"OpenFiles","open") ) {
472

473
      for ( int i = 0 ; i < openFiles.size(); ++i ) {
474

475
        QString sectionName = openFiles[i];
476

477 478 479 480 481 482
        // Check if the string read is empty (e.g. colon at the end of the line ...)
        // So skip trying to read files without a filename.
        if ( sectionName.isEmpty() ) {
          emit log(LOGWARN,tr("Warning from ini file parser: OpenFiles list contains empty string.") );
          continue;
        }
483

484 485 486 487 488
        // Check if the specified section exists
        if ( !ini.section_exists(sectionName) ) {
          emit log(LOGERR,tr("Error parsing ini file. OpenFiles section %1 not found in File!").arg(sectionName));
          continue;
        }
489

490
        // Get the path for the file which should be opened
491
        QString path;
492 493 494 495
        if ( !ini.get_entry( path, sectionName , "path" ) ) {
          emit log(LOGERR,tr("Error parsing ini file. Section %1 contains no path description!").arg(sectionName));
          continue;
        }
496

497
        // Check if path is relative ( The path is considered to be relative if the first character is a ".")
498
        if (path.startsWith( "." + OpenFlipper::Options::dirSeparator() )){
499

500
          // check if _filename contains a path by testing if it contains a directory separator
501 502 503 504
          if (_filename.section(OpenFlipper::Options::dirSeparator(), 0, -2) != ""){
            path.remove(0,1); // remove .
            path = _filename.section(OpenFlipper::Options::dirSeparator(), 0, -2) + path;
          }
505

506 507
        }

508
        int tmpType;
509
        DataType type = typeId("TriangleMesh");
510

511
        // First check for old datatype style (Only numbers .. therefore not consistent for runtime added types)
512 513
        if ( ini.get_entry( tmpType, sectionName , "type"  )) {
          type = DataType(tmpType);
514 515
          emit log(LOGWARN, tr("This ini file uses old int style ObjectType fields!") );
          emit log(LOGWARN, tr("Please convert it to new format! ( ... just save it )") );
516
        } else {
517

518
          // Read new style type. The type is represented by its name (as a QString)
519 520 521 522
          QString typeName="";
          if ( ini.get_entry( typeName, sectionName , "type"  )) {
            type = typeId(typeName);
          } else
523
            emit log(LOGWARN, tr("Unable to get DataType for object %1 assuming Triangle Mesh!").arg(sectionName) );
524
        }
525

526
        // Now the object gets loaded based on the given datatype
527
        int newObjectId = loadObject(type, path);
528

529
        // get the new object from the object tree ( If that fails, the object was not loaded correctly)
530 531
        BaseObject* object = objectRoot_->childExists( newObjectId );
        if ( object == 0 )  {
532
          emit log(LOGERR,tr("Unable to open Object ") + path);
533 534 535
          continue;
        }

536
        // Read the target flag setting
537
        bool flag;
538
        if ( ini.get_entry( flag, sectionName , "target" ) )
539 540
          object->target(flag);

541
        // Read the source flag setting
542 543
        if ( ini.get_entry( flag, sectionName , "source" ) )
          object->source(flag);
544

545
        // Tell plugins to load their per object settings
546
        emit iniLoad( ini,object->id() );
547

548
      }
549 550 551 552
    }

  }

553
  // Tell Plugins that all objects are loaded and they should read the remaining parts if necessary
554
  if ( _perPluginSettings )
555
    emit iniLoadOptionsLast( ini );
556

557
  // close ini file
558 559
  ini.disconnect();

560
  // As the reading has been completed, tell plugins that we do not read an ini file anymore.
561
  OpenFlipper::Options::loadingSettings(false);
562

563
  // Reset scenegraph and reset trackball center
564
  // This will also recompute the bounding boxes as well as the near and far plane
565
  resetScenegraph(true);
Jan Möbius's avatar
Jan Möbius committed
566

Dirk Wilden's avatar
Dirk Wilden committed
567 568
  if ( OpenFlipper::Options::gui() ){
    for ( unsigned int i = 0 ; i < OpenFlipper::Options::examinerWidgets() ; ++i ) {
569
      PluginFunctions::viewerProperties(i).drawMode( OpenFlipper::Options::defaultDrawMode(i) );
Dirk Wilden's avatar
Dirk Wilden committed
570 571
      coreWidget_->examiner_widgets_[i]->viewAll();
    }
Jan Möbius's avatar
Jan Möbius committed
572

573
    coreWidget_->statusMessage( tr("Loading ini File ") + _filename + tr(" ... Done"), 4000);
574 575 576 577 578
    coreWidget_->setStatus(ApplicationStatus::READY );
  }

}

579 580 581 582 583
void Core::writeIniFile(QString _filename,
                        bool _relativePaths,
                        bool _targetOnly,
                        bool _saveSystemSettings,
                        bool _savePluginSettings ,
584 585
                        bool _saveObjectInfo,
                        std::map<int,QString>& _fileMapping) {
Jan Möbius's avatar
Jan Möbius committed
586

587 588 589
  INIFile ini;

  if ( ! ini.connect(_filename,true) ) {
590
    emit log(LOGERR,tr("Failed to connect to _ini file") + _filename);
591 592
      return;
  }
Jan Möbius's avatar
Jan Möbius committed
593

594
  if ( OpenFlipper::Options::gui() ) {
595
    coreWidget_->statusMessage( tr("Saving ini File ") + _filename + " ...");
596 597 598
    coreWidget_->setStatus(ApplicationStatus::BLOCKED );
  }

599
  // Only save application settings when requested
600
  if ( _saveSystemSettings )
601
    writeApplicationOptions(ini);
602

603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619
  if ( _savePluginSettings )
    emit iniSaveOptions( ini );

  if ( _saveObjectInfo ) {
    // This vector will hold the file sections to open
    QStringList openFiles;


    PluginFunctions::IteratorRestriction restriction;
    if ( _targetOnly )
      restriction = PluginFunctions::TARGET_OBJECTS;
    else
      restriction = PluginFunctions::ALL_OBJECTS;

    QString keyName;
    QString sectionName;
    for ( PluginFunctions::ObjectIterator o_it(restriction) ;
620
                                          o_it != PluginFunctions::objectsEnd(); ++o_it) {
621 622 623 624 625 626 627 628

      QString file;
      std::map<int,QString>::iterator f = _fileMapping.find(o_it->id());
      if(f == _fileMapping.end()) {
          file = o_it->path() + OpenFlipper::Options::dirSeparator() + o_it->name();
      } else {
          file = f->second;
      }
629 630 631 632 633 634 635 636
    
      // Don't save default light source objects
      LightObject* light = 0;
      PluginFunctions::getObject( o_it->id(), light );
      if(light != 0) {
          if(light->defaultLight()) continue;
      }
    
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
      if (QFile(file).exists()){
        // Add a section for this object
        sectionName = o_it->name();
        ini.add_section( sectionName );
        openFiles.push_back( sectionName );

        //modify filename if relativePaths are wanted
        if (_relativePaths){
          int prefixLen = _filename.section(OpenFlipper::Options::dirSeparator(),0,-2).length();
          file.remove(0, prefixLen);
          file = "." + file;
        }
        // Add the path of this object to the section
        ini.add_entry( sectionName , "path" , file );
        ini.add_entry( sectionName , "type" , typeName(o_it->dataType() ) );
        ini.add_entry( sectionName , "target" , o_it->target() );
        ini.add_entry( sectionName , "source" , o_it->source() );
654

655
      }
656 657
    }

658
    ini.add_entry("OpenFiles","open",openFiles);
659

660 661
    // Tell plugins to save their information for the given object
    for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::ALL_OBJECTS) ;
662 663 664 665 666 667 668 669
                                          o_it != PluginFunctions::objectsEnd(); ++o_it) {
      // Don't save default light source objects
      LightObject* light = 0;
      PluginFunctions::getObject( o_it->id(), light );
      if(light != 0) {
        if(light->defaultLight()) continue;
      }
        
670
      emit iniSave(  ini , o_it->id() );
671
    }
672
  }
673 674 675 676



  ini.disconnect();
Jan Möbius's avatar
Jan Möbius committed
677

678
  if ( OpenFlipper::Options::gui() ) {
679
    coreWidget_->statusMessage( tr("Saving ini File ") + _filename + tr(" ... Done"), 4000);
680 681 682 683 684 685
    coreWidget_->setStatus(ApplicationStatus::READY );
  }
}


//=============================================================================