ParseIni.cc 26.1 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 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>

Jan Möbius's avatar
 
Jan Möbius committed
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
Marlin:  
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
Marlin:  
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
Marlin:  
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
Marlin:  
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
Marlin:  
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
Marlin:  
Jan Möbius committed
113
        contextMenus.removeFirst();
Jan Möbius's avatar
Jan Möbius committed
114 115

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

Jan Möbius's avatar
 
Jan Möbius committed
121 122 123 124
        if (!found){
          ViewMode* vm = new ViewMode();
          vm->name = mode;
          vm->custom = true;
Jan Möbius's avatar
Marlin:  
Jan Möbius committed
125 126 127 128
          vm->visibleToolbars     = toolBars;
          vm->visibleToolboxes    = toolBoxes;
          vm->visibleContextMenus = contextMenus;
          vm->icon                = entryIcon;
Jan Möbius's avatar
 
Jan Möbius committed
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

Jan Möbius's avatar
 
Jan Möbius committed
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 );
Jan Möbius's avatar
 
Jan Möbius committed
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);

Jan Möbius's avatar
 
Jan Möbius committed
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 );
Jan Möbius's avatar
 
Jan Möbius committed
166

Dirk Wilden's avatar
 
Dirk Wilden committed
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);

Jan Möbius's avatar
 
Jan Möbius committed
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);
Jan Möbius's avatar
 
Jan Möbius committed
180

Jan Möbius's avatar
Dennis:  
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));

Jan Möbius's avatar
 
Jan Möbius committed
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
Marlin:  
Jan Möbius committed
228 229
    if ( _ini.get_entry( viewmode, "Options" , "CurrentViewMode") )
      OpenFlipper::Options::currentViewMode(viewmode);
Jan Möbius's avatar
 
Jan Möbius committed
230

231 232 233 234 235 236 237
    //============================================================================
    // Load the setting for the viewer layout
    //============================================================================
    int viewerLayout = 0;
    if ( _ini.get_entry( viewerLayout, "Options" , "DefaultViewerLayout") )
      OpenFlipper::Options::defaultViewerLayout(viewerLayout);

Jan Möbius's avatar
 
Jan Möbius committed
238
    //============================================================================
239
    // Load the viewer settings
Jan Möbius's avatar
 
Jan Möbius committed
240 241
    //============================================================================
    std::vector< QString > draw_modes;
242 243 244 245

    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
246
        OpenFlipper::Options::defaultDrawMode( listToDrawMode(draw_modes), i );
247 248

    }
Dirk Wilden's avatar
Dirk Wilden committed
249

Jan Möbius's avatar
Jan Möbius committed
250 251 252 253 254 255 256
    //============================================================================
    // Load slotDebugging state
    //============================================================================
    bool doSlotDebugging = false;
    if( _ini.get_entry(doSlotDebugging, "Options", "SlotDebugging") )
      OpenFlipper::Options::doSlotDebugging(doSlotDebugging);

Dirk Wilden's avatar
Dirk Wilden committed
257 258 259 260
    //============================================================================
    // ViewerProperties
    //============================================================================

Dirk Wilden's avatar
Dirk Wilden committed
261
    uint viewerCount = 0;
Dirk Wilden's avatar
Dirk Wilden committed
262 263 264
    if( _ini.get_entry(viewerCount, "Options", "ViewerCount") ){
    }

265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
    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
283
    }
Dirk Wilden's avatar
Dirk Wilden committed
284

Jan Möbius's avatar
 
Jan Möbius committed
285 286 287
  }
}

Dirk Wilden's avatar
Dirk Wilden committed
288
/** Write the standard options to the given Ini File
Jan Möbius's avatar
 
Jan Möbius committed
289 290
* @param _ini Inifile to use
*/
Dirk Wilden's avatar
Dirk Wilden committed
291
void Core::writeApplicationOptions(INIFile& _ini) {
Jan Möbius's avatar
 
Jan Möbius committed
292 293

  // save ViewModes
Jan Möbius's avatar
Jan Möbius committed
294 295
  QVector< QString > toolboxes;
  QVector< QString > toolbars;
Jan Möbius's avatar
Marlin:  
Jan Möbius committed
296
  QVector< QString > contextmenus;
Jan Möbius's avatar
Jan Möbius committed
297
  QVector< QString > icons;
298

Jan Möbius's avatar
 
Jan Möbius committed
299 300 301 302 303
  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
304
        QString entryToolboxes = coreWidget_->viewModes_[i]->name;
Jan Möbius's avatar
 
Jan Möbius committed
305 306

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

        toolboxes.push_back(entryToolboxes);
Jan Möbius's avatar
 
Jan Möbius committed
311

Jan Möbius's avatar
Jan Möbius committed
312 313
        //store name
        QString entryToolbars = coreWidget_->viewModes_[i]->name;
314

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

Jan Möbius's avatar
Jan Möbius committed
319
        toolbars.push_back(entryToolbars);
Jan Möbius's avatar
Marlin:  
Jan Möbius committed
320 321
	
	QString entryContextMenus = coreWidget_->viewModes_[i]->name;
322

Jan Möbius's avatar
Marlin:  
Jan Möbius committed
323 324 325
        //store widgets
        for (int j=0; j < coreWidget_->viewModes_[i]->visibleContextMenus.size(); j++)
          entryContextMenus += ";" + coreWidget_->viewModes_[i]->visibleContextMenus[j];
326

Jan Möbius's avatar
Marlin:  
Jan Möbius committed
327 328 329
        contextmenus.push_back(entryContextMenus);

        icons.push_back(coreWidget_->viewModes_[i]->icon);
Jan Möbius's avatar
 
Jan Möbius committed
330 331 332
      }

  //save viewmodes to ini
Jan Möbius's avatar
Jan Möbius committed
333 334
  _ini.add_entry("Options","ViewModeCount" ,toolboxes.size());
  for (int i=0; i < toolboxes.size(); i++) {
Jan Möbius's avatar
Marlin:  
Jan Möbius committed
335 336 337 338
    _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
339
  }
Jan Möbius's avatar
 
Jan Möbius committed
340

341 342 343 344
  //save KeyBindings
  if ( OpenFlipper::Options::gui() )
    coreWidget_->saveKeyBindings(_ini);

Jan Möbius's avatar
 
Jan Möbius committed
345 346 347
  //write default dataType to INI
  _ini.add_entry( "Options" , "default_DataType" , OpenFlipper::Options::lastDataType() );

Jan Möbius's avatar
Marlin:  
Jan Möbius committed
348 349
  //write current ViewMode
  _ini.add_entry("Options","CurrentViewMode",OpenFlipper::Options::currentViewMode() );
Jan Möbius's avatar
 
Jan Möbius committed
350

Jan Möbius's avatar
Jan Möbius committed
351 352 353 354 355
  //============================================================================
  // Debugging
  //============================================================================
  _ini.add_entry("Options","SlotDebugging",OpenFlipper::Options::doSlotDebugging() );

Jan Möbius's avatar
 
Jan Möbius committed
356 357 358 359 360 361
  if ( !_ini.section_exists("Plugins") )
    _ini.add_section("Plugins");

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

362
  QString dir = OpenFlipperSettings().value("Core/CurrentDir").toString().toUtf8();
Jan Möbius's avatar
 
Jan Möbius committed
363
  _ini.add_entry("Options","StartupDir",dir);
Dirk Wilden's avatar
Dirk Wilden committed
364 365

  QString scriptDir = OpenFlipper::Options::currentScriptDirStr().toUtf8();
366 367 368 369
  _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
370

371
  _ini.add_entry("Options","RandomBaseColor", OpenFlipper::Options::randomBaseColor() );
Jan Möbius's avatar
 
Jan Möbius committed
372 373 374

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

375 376
    _ini.add_entry("Options","DefaultViewerLayout", OpenFlipper::Options::defaultViewerLayout() );

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

Dirk Wilden's avatar
Dirk Wilden committed
379
    for ( unsigned int i = 0 ; i < OpenFlipper::Options::examinerWidgets(); ++i ) {
380 381 382 383 384
      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
385
    }
386

Jan Möbius's avatar
 
Jan Möbius committed
387
    //============================================================================
388
    // Save the current viewer properties
Jan Möbius's avatar
 
Jan Möbius committed
389 390
    //============================================================================
    std::vector< QString > draw_modes;
391 392 393 394 395 396 397

    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);

    }
398

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

401
    _ini.add_entry("Options", "StereoMode",OpenFlipper::Options::stereoMode() );
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
402 403 404

    _ini.add_entry("Options" , "CustomAnaglyphLeftEye", OpenFlipper::Options::anaglyphLeftEyeColorMatrix() );
    _ini.add_entry("Options" , "CustomAnaglyphRightEye", OpenFlipper::Options::anaglyphRightEyeColorMatrix() );
Jan Möbius's avatar
 
Jan Möbius committed
405 406
  }

407 408
  emit saveOnExit(_ini);

Jan Möbius's avatar
Jan Möbius committed
409
  // _ini.add_entry("Options","Stereo",OpenFlipper::Options::stereo() );
Jan Möbius's avatar
 
Jan Möbius committed
410 411
}

412 413 414 415
void Core::openIniFile( QString _filename,
                        bool    _coreSettings,
                        bool    _perPluginSettings,
                        bool    _loadObjects ){
Jan Möbius's avatar
 
Jan Möbius committed
416 417 418
  INIFile ini;

  if ( ! ini.connect(_filename,false) ) {
419
    emit log(LOGERR,tr("Failed to connect to ini file") + _filename);
Jan Möbius's avatar
 
Jan Möbius committed
420 421
    return;
  }
Jan Möbius's avatar
Jan Möbius committed
422

Jan Möbius's avatar
 
Jan Möbius committed
423
  if ( OpenFlipper::Options::gui() ) {
424
    coreWidget_->statusMessage( tr("Loading ini File ") + _filename + " ...");
Jan Möbius's avatar
 
Jan Möbius committed
425 426 427
    coreWidget_->setStatus(ApplicationStatus::BLOCKED );
  }

428
  // Tell plugins that we are currently reading an ini file
429
  OpenFlipper::Options::loadingSettings(true);
Jan Möbius's avatar
 
Jan Möbius committed
430

431 432 433
  // Load Core settings only if requested
  if ( _coreSettings )
    readApplicationOptions(ini);
Jan Möbius's avatar
 
Jan Möbius committed
434

435 436 437
  // if requested load per Plugin settings from the settings file
  if ( _perPluginSettings )
    emit iniLoadOptions( ini );
Jan Möbius's avatar
 
Jan Möbius committed
438

439
  if ( _loadObjects ) {
Jan Möbius's avatar
 
Jan Möbius committed
440

441
    QStringList openFiles;
Jan Möbius's avatar
 
Jan Möbius committed
442

443 444
    // Parse File section for files to open
    if ( ini.section_exists("OpenFiles") && ini.get_entry(openFiles,"OpenFiles","open") ) {
Jan Möbius's avatar
 
Jan Möbius committed
445

446
      for ( int i = 0 ; i < openFiles.size(); ++i ) {
Jan Möbius's avatar
 
Jan Möbius committed
447

448
        QString sectionName = openFiles[i];
449

450 451 452 453 454 455
        // 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;
        }
456

457 458 459 460 461
        // 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;
        }
462

463
        // Get the path for the file which should be opened
464
        QString path;
465 466 467 468
        if ( !ini.get_entry( path, sectionName , "path" ) ) {
          emit log(LOGERR,tr("Error parsing ini file. Section %1 contains no path description!").arg(sectionName));
          continue;
        }
469

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

473
          // check if _filename contains a path by testing if it contains a directory separator
474 475 476 477
          if (_filename.section(OpenFlipper::Options::dirSeparator(), 0, -2) != ""){
            path.remove(0,1); // remove .
            path = _filename.section(OpenFlipper::Options::dirSeparator(), 0, -2) + path;
          }
478

Jan Möbius's avatar
 
Jan Möbius committed
479 480
        }

481
        int tmpType;
482
        DataType type = typeId("TriangleMesh");
483

484
        // First check for old datatype style (Only numbers .. therefore not consistent for runtime added types)
485 486
        if ( ini.get_entry( tmpType, sectionName , "type"  )) {
          type = DataType(tmpType);
487 488
          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 )") );
489
        } else {
490

491
          // Read new style type. The type is represented by its name (as a QString)
492 493 494 495
          QString typeName="";
          if ( ini.get_entry( typeName, sectionName , "type"  )) {
            type = typeId(typeName);
          } else
496
            emit log(LOGWARN, tr("Unable to get DataType for object %1 assuming Triangle Mesh!").arg(sectionName) );
497
        }
Jan Möbius's avatar
 
Jan Möbius committed
498

499
        // Now the object gets loaded based on the given datatype
500
        int newObjectId = loadObject(type, path);
Jan Möbius's avatar
 
Jan Möbius committed
501

502
        // get the new object from the object tree ( If that fails, the object was not loaded correctly)
503 504
        BaseObject* object = objectRoot_->childExists( newObjectId );
        if ( object == 0 )  {
505
          emit log(LOGERR,tr("Unable to open Object ") + path);
506 507 508
          continue;
        }

509
        // Read the target flag setting
510
        bool flag;
511
        if ( ini.get_entry( flag, sectionName , "target" ) )
512 513
          object->target(flag);

514
        // Read the source flag setting
515 516
        if ( ini.get_entry( flag, sectionName , "source" ) )
          object->source(flag);
Jan Möbius's avatar
 
Jan Möbius committed
517

518
        // Tell plugins to load their per object settings
519
        emit iniLoad( ini,object->id() );
Jan Möbius's avatar
 
Jan Möbius committed
520

521
      }
Jan Möbius's avatar
 
Jan Möbius committed
522 523 524 525
    }

  }

526
  // Tell Plugins that all objects are loaded and they should read the remaining parts if necessary
527
  if ( _perPluginSettings )
528
    emit iniLoadOptionsLast( ini );
Jan Möbius's avatar
 
Jan Möbius committed
529

530
  // close ini file
Jan Möbius's avatar
 
Jan Möbius committed
531 532
  ini.disconnect();

533
  // As the reading has been completed, tell plugins that we do not read an ini file anymore.
534
  OpenFlipper::Options::loadingSettings(false);
Jan Möbius's avatar
 
Jan Möbius committed
535

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

Dirk Wilden's avatar
Dirk Wilden committed
540 541
  if ( OpenFlipper::Options::gui() ){
    for ( unsigned int i = 0 ; i < OpenFlipper::Options::examinerWidgets() ; ++i ) {
542
      PluginFunctions::viewerProperties(i).drawMode( OpenFlipper::Options::defaultDrawMode(i) );
Dirk Wilden's avatar
Dirk Wilden committed
543 544
      coreWidget_->examiner_widgets_[i]->viewAll();
    }
Jan Möbius's avatar
Jan Möbius committed
545

546
    coreWidget_->statusMessage( tr("Loading ini File ") + _filename + tr(" ... Done"), 4000);
Jan Möbius's avatar
 
Jan Möbius committed
547 548 549 550 551
    coreWidget_->setStatus(ApplicationStatus::READY );
  }

}

552 553 554 555 556
void Core::writeIniFile(QString _filename,
                        bool _relativePaths,
                        bool _targetOnly,
                        bool _saveSystemSettings,
                        bool _savePluginSettings ,
557 558
                        bool _saveObjectInfo,
                        std::map<int,QString>& _fileMapping) {
Jan Möbius's avatar
Jan Möbius committed
559

Jan Möbius's avatar
 
Jan Möbius committed
560 561 562
  INIFile ini;

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

Jan Möbius's avatar
 
Jan Möbius committed
567
  if ( OpenFlipper::Options::gui() ) {
568
    coreWidget_->statusMessage( tr("Saving ini File ") + _filename + " ...");
Jan Möbius's avatar
 
Jan Möbius committed
569 570 571
    coreWidget_->setStatus(ApplicationStatus::BLOCKED );
  }

572
  // Only save application settings when requested
573
  if ( _saveSystemSettings )
574
    writeApplicationOptions(ini);
Jan Möbius's avatar
 
Jan Möbius committed
575

576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
  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) ;
Jan Möbius's avatar
 
Jan Möbius committed
593
                                          o_it != PluginFunctions::objectsEnd(); ++o_it) {
594 595 596 597 598 599 600 601

      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;
      }
602 603 604 605 606 607 608 609
    
      // Don't save default light source objects
      LightObject* light = 0;
      PluginFunctions::getObject( o_it->id(), light );
      if(light != 0) {
          if(light->defaultLight()) continue;
      }
    
610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626
      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() );
Jan Möbius's avatar
 
Jan Möbius committed
627

628
      }
Jan Möbius's avatar
 
Jan Möbius committed
629 630
    }

631
    ini.add_entry("OpenFiles","open",openFiles);
Jan Möbius's avatar
 
Jan Möbius committed
632

633 634
    // Tell plugins to save their information for the given object
    for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::ALL_OBJECTS) ;
635 636 637 638 639 640 641 642
                                          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;
      }
        
643
      emit iniSave(  ini , o_it->id() );
644
    }
645
  }
Jan Möbius's avatar
 
Jan Möbius committed
646 647 648 649



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

Jan Möbius's avatar
 
Jan Möbius committed
651
  if ( OpenFlipper::Options::gui() ) {
652
    coreWidget_->statusMessage( tr("Saving ini File ") + _filename + tr(" ... Done"), 4000);
Jan Möbius's avatar
 
Jan Möbius committed
653 654 655 656 657 658
    coreWidget_->setStatus(ApplicationStatus::READY );
  }
}


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