ParseIni.cc 26.4 KB
Newer Older
1
/*===========================================================================*\
Jan Möbius's avatar
Jan Möbius committed
2 3
*                                                                            *
*                              OpenFlipper                                   *
Martin Schultz's avatar
Martin Schultz committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
 *           Copyright (c) 2001-2015, RWTH-Aachen University                 *
 *           Department of Computer Graphics and Multimedia                  *
 *                          All rights reserved.                             *
 *                            www.openflipper.org                            *
 *                                                                           *
 *---------------------------------------------------------------------------*
 * This file is part of OpenFlipper.                                         *
 *---------------------------------------------------------------------------*
 *                                                                           *
 * Redistribution and use in source and binary forms, with or without        *
 * modification, are permitted provided that the following conditions        *
 * are met:                                                                  *
 *                                                                           *
 * 1. Redistributions of source code must retain the above copyright notice, *
 *    this list of conditions and the following disclaimer.                  *
 *                                                                           *
 * 2. Redistributions in binary form must reproduce the above copyright      *
 *    notice, this list of conditions and the following disclaimer in the    *
 *    documentation and/or other materials provided with the distribution.   *
 *                                                                           *
 * 3. Neither the name of the copyright holder nor the names of its          *
 *    contributors may be used to endorse or promote products derived from   *
 *    this software without specific prior written permission.               *
 *                                                                           *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       *
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A           *
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,  *
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,       *
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR        *
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    *
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING      *
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS        *
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.              *
Jan Möbius's avatar
Jan Möbius committed
39
*                                                                            *
40 41 42
\*===========================================================================*/

/*===========================================================================*\
Jan Möbius's avatar
Jan Möbius committed
43 44 45 46 47
*                                                                            *
*   $Revision$                                                       *
*   $LastChangedBy$                                                *
*   $Date$                     *
*                                                                            *
48
\*===========================================================================*/
Jan Möbius's avatar
 
Jan Möbius committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67




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


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

// -------------------- mview
#include "Core.hh"
// -------------------- ACG

#include <OpenFlipper/common/RecentFiles.hh>

Jan Möbius's avatar
Jan Möbius committed
68
//#include <ObjectTypes/Light/Light.hh>
69

Jan Möbius's avatar
 
Jan Möbius committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
#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_default_color = false;
    if ( _ini.get_entry( random_default_color, "Options" , "RandomDefaultColor") )
      OpenFlipper::Options::randomDefaultColor( random_default_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
    //============================================================================
    // 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);
199 200 201
      set[0] = 0.299f;
      set[3] = 0.587f;
      set[6] = 0.114f;
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
      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
    //============================================================================
    // Load the setting for the object color option
    //============================================================================
220 221 222
    unsigned int defaultColor = 0;
    if ( _ini.get_entry( defaultColor, "Options" , "DefaultColor") )
      OpenFlipper::Options::defaultColor(QRgb(defaultColor));
223

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
    //============================================================================

261
    unsigned int 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() );

356
  QString dir = OpenFlipperSettings().value("Core/CurrentDir").toString().toUtf8();
Jan Möbius's avatar
 
Jan Möbius committed
357
  _ini.add_entry("Options","StartupDir",dir);
Dirk Wilden's avatar
Dirk Wilden committed
358 359

  QString scriptDir = OpenFlipper::Options::currentScriptDirStr().toUtf8();
360 361 362 363
  _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
364

365
  _ini.add_entry("Options","RandomDefaultColor", OpenFlipper::Options::randomDefaultColor() );
Jan Möbius's avatar
 
Jan Möbius committed
366 367 368

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

369 370
    _ini.add_entry("Options","DefaultViewerLayout", OpenFlipper::Options::defaultViewerLayout() );

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

Dirk Wilden's avatar
Dirk Wilden committed
373
    for ( unsigned int i = 0 ; i < OpenFlipper::Options::examinerWidgets(); ++i ) {
374 375 376 377 378
      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
379
    }
380

Jan Möbius's avatar
 
Jan Möbius committed
381
    //============================================================================
382
    // Save the current viewer properties
Jan Möbius's avatar
 
Jan Möbius committed
383 384
    //============================================================================
    std::vector< QString > draw_modes;
385 386 387 388 389 390 391

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

    }
392

393
    _ini.add_entry("Options","DefaultColor", (uint)OpenFlipper::Options::defaultColor().rgba ()  );
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
394

395
    _ini.add_entry("Options", "StereoMode",OpenFlipper::Options::stereoMode() );
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
396 397 398

    _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
399 400
  }

401 402
  emit saveOnExit(_ini);

Jan Möbius's avatar
Jan Möbius committed
403
  // _ini.add_entry("Options","Stereo",OpenFlipper::Options::stereo() );
Jan Möbius's avatar
 
Jan Möbius committed
404 405
}

406 407 408 409
void Core::openIniFile( QString _filename,
                        bool    _coreSettings,
                        bool    _perPluginSettings,
                        bool    _loadObjects ){
Jan Möbius's avatar
 
Jan Möbius committed
410 411 412
  INIFile ini;

  if ( ! ini.connect(_filename,false) ) {
413
    emit log(LOGERR,tr("Failed to connect to ini file") + _filename);
Jan Möbius's avatar
 
Jan Möbius committed
414 415
    return;
  }
Jan Möbius's avatar
Jan Möbius committed
416

Jan Möbius's avatar
 
Jan Möbius committed
417
  if ( OpenFlipper::Options::gui() ) {
418
    coreWidget_->statusMessage( tr("Loading ini File ") + _filename + " ...");
Jan Möbius's avatar
 
Jan Möbius committed
419 420 421
    coreWidget_->setStatus(ApplicationStatus::BLOCKED );
  }

422
  // Tell plugins that we are currently reading an ini file
423
  OpenFlipper::Options::blockSceneGraphUpdates();
Jan Möbius's avatar
 
Jan Möbius committed
424

425 426 427
  // Load Core settings only if requested
  if ( _coreSettings )
    readApplicationOptions(ini);
Jan Möbius's avatar
 
Jan Möbius committed
428

429 430 431
  // if requested load per Plugin settings from the settings file
  if ( _perPluginSettings )
    emit iniLoadOptions( ini );
Jan Möbius's avatar
 
Jan Möbius committed
432

433
  if ( _loadObjects ) {
Jan Möbius's avatar
 
Jan Möbius committed
434

435
    QStringList openFiles;
Jan Möbius's avatar
 
Jan Möbius committed
436

437 438
    // 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
439

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

442
        QString sectionName = openFiles[i];
443

444 445 446 447 448 449
        // 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;
        }
450

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

457
        // Get the path for the file which should be opened
458
        QString path;
459 460 461 462
        if ( !ini.get_entry( path, sectionName , "path" ) ) {
          emit log(LOGERR,tr("Error parsing ini file. Section %1 contains no path description!").arg(sectionName));
          continue;
        }
463

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

467
          // check if _filename contains a path by testing if it contains a directory separator
468 469 470 471
          if (_filename.section(OpenFlipper::Options::dirSeparator(), 0, -2) != ""){
            path.remove(0,1); // remove .
            path = _filename.section(OpenFlipper::Options::dirSeparator(), 0, -2) + path;
          }
472

Jan Möbius's avatar
 
Jan Möbius committed
473 474
        }

475
        int tmpType;
476
        DataType type = typeId("TriangleMesh");
477

478
        // First check for old datatype style (Only numbers .. therefore not consistent for runtime added types)
479 480
        if ( ini.get_entry( tmpType, sectionName , "type"  )) {
          type = DataType(tmpType);
481 482
          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 )") );
483
        } else {
484

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

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

496
        // get the new object from the object tree ( If that fails, the object was not loaded correctly)
497 498
        BaseObject* object = objectRoot_->childExists( newObjectId );
        if ( object == 0 )  {
499
          emit log(LOGERR,tr("Unable to open Object ") + path);
500 501 502
          continue;
        }

503
        // Read the target flag setting
504
        bool flag;
505
        if ( ini.get_entry( flag, sectionName , "target" ) )
506 507
          object->target(flag);

508
        // Read the source flag setting
509 510
        if ( ini.get_entry( flag, sectionName , "source" ) )
          object->source(flag);
Jan Möbius's avatar
 
Jan Möbius committed
511

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

515
      }
Jan Möbius's avatar
 
Jan Möbius committed
516 517 518 519
    }

  }

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

524
  // close ini file
Jan Möbius's avatar
 
Jan Möbius committed
525 526
  ini.disconnect();

527
  // As the reading has been completed, tell plugins that we do not read an ini file anymore.
528
  OpenFlipper::Options::unblockSceneGraphUpdates();
Jan Möbius's avatar
 
Jan Möbius committed
529

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

Dirk Wilden's avatar
Dirk Wilden committed
534 535
  if ( OpenFlipper::Options::gui() ){
    for ( unsigned int i = 0 ; i < OpenFlipper::Options::examinerWidgets() ; ++i ) {
536
      PluginFunctions::viewerProperties(i).drawMode( OpenFlipper::Options::defaultDrawMode(i) );
Dirk Wilden's avatar
Dirk Wilden committed
537 538
      coreWidget_->examiner_widgets_[i]->viewAll();
    }
Jan Möbius's avatar
Jan Möbius committed
539

540
    coreWidget_->statusMessage( tr("Loading ini File ") + _filename + tr(" ... Done"), 4000);
Jan Möbius's avatar
 
Jan Möbius committed
541 542 543 544 545
    coreWidget_->setStatus(ApplicationStatus::READY );
  }

}

546 547 548 549 550
void Core::writeIniFile(QString _filename,
                        bool _relativePaths,
                        bool _targetOnly,
                        bool _saveSystemSettings,
                        bool _savePluginSettings ,
551 552
                        bool _saveObjectInfo,
                        std::map<int,QString>& _fileMapping) {
Jan Möbius's avatar
Jan Möbius committed
553

Jan Möbius's avatar
 
Jan Möbius committed
554 555 556
  INIFile ini;

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

Jan Möbius's avatar
 
Jan Möbius committed
561
  if ( OpenFlipper::Options::gui() ) {
562
    coreWidget_->statusMessage( tr("Saving ini File ") + _filename + " ...");
Jan Möbius's avatar
 
Jan Möbius committed
563 564 565
    coreWidget_->setStatus(ApplicationStatus::BLOCKED );
  }

566
  // Only save application settings when requested
567
  if ( _saveSystemSettings )
568
    writeApplicationOptions(ini);
Jan Möbius's avatar
 
Jan Möbius committed
569

570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
  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
587
                                          o_it != PluginFunctions::objectsEnd(); ++o_it) {
588 589 590 591 592 593 594 595

      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;
      }
596
    
Jan Möbius's avatar
Jan Möbius committed
597
      /* @Todo:  This is broken when Light source Object type is not available!
598 599 600 601 602 603
      // Don't save default light source objects
      LightObject* light = 0;
      PluginFunctions::getObject( o_it->id(), light );
      if(light != 0) {
          if(light->defaultLight()) continue;
      }
Jan Möbius's avatar
Jan Möbius committed
604
      */
605
    
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
      if (QFile(file).exists()){
        // Add a section for this object
        sectionName = o_it->name();
        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
622

623
      }
Jan Möbius's avatar
 
Jan Möbius committed
624 625
    }

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

628 629
    // Tell plugins to save their information for the given object
    for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::ALL_OBJECTS) ;
630
                                          o_it != PluginFunctions::objectsEnd(); ++o_it) {
Jan Möbius's avatar
Jan Möbius committed
631 632

      /* @Todo:  This is broken when Light source Object type is not available!
633 634 635 636 637 638
      // Don't save default light source objects
      LightObject* light = 0;
      PluginFunctions::getObject( o_it->id(), light );
      if(light != 0) {
        if(light->defaultLight()) continue;
      }
Jan Möbius's avatar
Jan Möbius committed
639
      */
640
        
641
      emit iniSave(  ini , o_it->id() );
642
    }
643
  }
Jan Möbius's avatar
 
Jan Möbius committed
644 645 646 647



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

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


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