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

238
239
240
241
242
243
244
    //============================================================================
    // Load the setting for the gridVisibility
    //============================================================================
    bool visible = false;
    if ( _ini.get_entry( visible, "Options" , "GridVisible") )
      OpenFlipper::Options::gridVisible(visible);

245
246
247
248
249
250
251
    //============================================================================
    // 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
252
    //============================================================================
253
    // Load the viewer settings
Jan Möbius's avatar
 
Jan Möbius committed
254
255
    //============================================================================
    std::vector< QString > draw_modes;
256
257
258
259
260
    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
261
        OpenFlipper::Options::defaultDrawMode( listToDrawMode(draw_modes), i );
262
263

      if( _ini.get_entry(mode, "Options", "DefaultProjectionMode" + QString::number(i) ) )
264
        OpenFlipper::Options::defaultProjectionMode( mode, i );
265
266

      if( _ini.get_entry(mode, "Options", "DefaultViewingDirection" + QString::number(i) ) )
267
        OpenFlipper::Options::defaultViewingDirection( mode, i );
268
    }
Dirk Wilden's avatar
Dirk Wilden committed
269

Jan Möbius's avatar
Jan Möbius committed
270
271
272
273
274
275
276
    //============================================================================
    // Load slotDebugging state
    //============================================================================
    bool doSlotDebugging = false;
    if( _ini.get_entry(doSlotDebugging, "Options", "SlotDebugging") )
      OpenFlipper::Options::doSlotDebugging(doSlotDebugging);

Dirk Wilden's avatar
Dirk Wilden committed
277
278
279
280
    //============================================================================
    // ViewerProperties
    //============================================================================

Dirk Wilden's avatar
Dirk Wilden committed
281
    uint viewerCount = 0;
Dirk Wilden's avatar
Dirk Wilden committed
282
283
284
    if( _ini.get_entry(viewerCount, "Options", "ViewerCount") ){
    }

Dirk Wilden's avatar
Dirk Wilden committed
285
286
    if ( _ini.section_exists("ViewerProperties") ){
      for ( unsigned int i = 0 ; i < viewerCount; ++i ) {
Jan Möbius's avatar
Jan Möbius committed
287

Dirk Wilden's avatar
Dirk Wilden committed
288
289
        if (OpenFlipper::Options::examinerWidgets() < i)
          break;
Jan Möbius's avatar
Jan Möbius committed
290

Dirk Wilden's avatar
Dirk Wilden committed
291
292
293
294
        // Load the animation setting
        bool animation = false;
        if ( _ini.get_entry( animation, "ViewerProperties" , "Animation" + QString::number(i) ) )
          PluginFunctions::viewerProperties(i).animation(animation);
Jan Möbius's avatar
Jan Möbius committed
295

Dirk Wilden's avatar
Dirk Wilden committed
296
297
298
299
        // Load the twoSidedLighting setting
        bool twoSidedLighting = false;
        if ( _ini.get_entry( twoSidedLighting, "ViewerProperties" , "TwoSidedLighting" + QString::number(i)) )
          PluginFunctions::viewerProperties(i).twoSidedLighting(twoSidedLighting);
Jan Möbius's avatar
Jan Möbius committed
300

Dirk Wilden's avatar
Dirk Wilden committed
301
302
303
304
        // Load the backface culling setting
        bool backface_culling = false;
        if ( _ini.get_entry( backface_culling, "ViewerProperties" , "BackfaceCulling" + QString::number(i)) )
          PluginFunctions::viewerProperties(i).backFaceCulling(backface_culling);
Jan Möbius's avatar
Jan Möbius committed
305

Dirk Wilden's avatar
Dirk Wilden committed
306
307
308
309
310
311
        // Load the setting for the background color option
        uint viewerBackground = 0;
        if ( _ini.get_entry( viewerBackground, "ViewerProperties" , "BackgroundColor" + QString::number(i)) )
          PluginFunctions::viewerProperties(i).backgroundColor( QRgb(viewerBackground) );
      }
    }
Dirk Wilden's avatar
Dirk Wilden committed
312

Dirk Wilden's avatar
Dirk Wilden committed
313
314
315
316
317
318
    //============================================================================
    // Update information
    //============================================================================
    QString updateUrl = "";
    if( _ini.get_entry(updateUrl, "Options", "UpdateURL") )
      OpenFlipper::Options::updateUrl(updateUrl);
Dirk Wilden's avatar
Dirk Wilden committed
319

Dirk Wilden's avatar
Dirk Wilden committed
320
321
322
    QString updateUsername = "";
    if( _ini.get_entry(updateUsername, "Options", "UpdateUsername") )
      OpenFlipper::Options::updateUsername(updateUsername);
Dirk Wilden's avatar
Dirk Wilden committed
323

Dirk Wilden's avatar
Dirk Wilden committed
324
325
326
    QString updatePassword = "";
    if( _ini.get_entry(updatePassword, "Options", "UpdatePassword") )
      OpenFlipper::Options::updatePassword(updatePassword);
Jan Möbius's avatar
 
Jan Möbius committed
327
328
329
  }
}

Dirk Wilden's avatar
Dirk Wilden committed
330
/** Write the standard options to the given Ini File
Jan Möbius's avatar
 
Jan Möbius committed
331
332
* @param _ini Inifile to use
*/
Dirk Wilden's avatar
Dirk Wilden committed
333
void Core::writeApplicationOptions(INIFile& _ini) {
Jan Möbius's avatar
 
Jan Möbius committed
334
335

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

  // Write list of recent files to ini
339
340
  QStringList recentFiles = OpenFlipperSettings().value("Core/File/RecentFiles", QStringList()).toStringList();
  QStringList recentTypes = OpenFlipperSettings().value("Core/File/RecentTypes", QStringList()).toStringList();
341
  
Jan Möbius's avatar
 
Jan Möbius committed
342
343
344
  for ( int j = 0 ; j < recentFiles.size(); ++j) {
    // Save filename
    QString key = "recent" + QString::number(j);
345
    QString filename = recentFiles[j];
Jan Möbius's avatar
 
Jan Möbius committed
346
347
348
    _ini.add_entry( "Options" , key , filename );
    // Save DataType
    key = "type" + QString::number(j);
349
    QString type = recentTypes[j];
Jan Möbius's avatar
 
Jan Möbius committed
350
351
352
353
    _ini.add_entry( "Options" , key , type );
  }

  // save ViewModes
Jan Möbius's avatar
Jan Möbius committed
354
355
  QVector< QString > toolboxes;
  QVector< QString > toolbars;
Jan Möbius's avatar
Marlin:    
Jan Möbius committed
356
  QVector< QString > contextmenus;
Jan Möbius's avatar
Jan Möbius committed
357
  QVector< QString > icons;
358

Jan Möbius's avatar
 
Jan Möbius committed
359
360
361
362
363
  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
364
        QString entryToolboxes = coreWidget_->viewModes_[i]->name;
Jan Möbius's avatar
 
Jan Möbius committed
365
366

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

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

Jan Möbius's avatar
Jan Möbius committed
372
373
        //store name
        QString entryToolbars = coreWidget_->viewModes_[i]->name;
374

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

Jan Möbius's avatar
Jan Möbius committed
379
        toolbars.push_back(entryToolbars);
Jan Möbius's avatar
Marlin:    
Jan Möbius committed
380
381
	
	QString entryContextMenus = coreWidget_->viewModes_[i]->name;
382

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

Jan Möbius's avatar
Marlin:    
Jan Möbius committed
387
388
389
        contextmenus.push_back(entryContextMenus);

        icons.push_back(coreWidget_->viewModes_[i]->icon);
Jan Möbius's avatar
 
Jan Möbius committed
390
391
392
      }

  //save viewmodes to ini
Jan Möbius's avatar
Jan Möbius committed
393
394
  _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
395
396
397
398
    _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
399
  }
Jan Möbius's avatar
 
Jan Möbius committed
400

401
402
403
404
  //save KeyBindings
  if ( OpenFlipper::Options::gui() )
    coreWidget_->saveKeyBindings(_ini);

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

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

Jan Möbius's avatar
Jan Möbius committed
411
412
413
414
415
  //============================================================================
  // Debugging
  //============================================================================
  _ini.add_entry("Options","SlotDebugging",OpenFlipper::Options::doSlotDebugging() );

416
417
418
419
420
421
422
  //============================================================================
  // Update information
  //============================================================================
  _ini.add_entry("Options","UpdateURL",OpenFlipper::Options::updateUrl() );
  _ini.add_entry("Options","UpdateUsername",OpenFlipper::Options::updateUsername() );
  _ini.add_entry("Options","UpdatePassword",OpenFlipper::Options::updatePassword() );

Jan Möbius's avatar
 
Jan Möbius committed
423
424
425
426
427
428
  if ( !_ini.section_exists("Plugins") )
    _ini.add_section("Plugins");

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

429
  QString dir = OpenFlipperSettings().value("Core/CurrentDir").toString().toUtf8();
Jan Möbius's avatar
 
Jan Möbius committed
430
  _ini.add_entry("Options","StartupDir",dir);
Dirk Wilden's avatar
Dirk Wilden committed
431
432

  QString scriptDir = OpenFlipper::Options::currentScriptDirStr().toUtf8();
433
434
435
436
  _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
437

438
  _ini.add_entry("Options","RandomBaseColor", OpenFlipper::Options::randomBaseColor() );
Jan Möbius's avatar
 
Jan Möbius committed
439
440
441

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

442
443
    _ini.add_entry("Options","DefaultViewerLayout", OpenFlipper::Options::defaultViewerLayout() );
    _ini.add_entry("Options","Translation", OpenFlipper::Options::translation() );
444
    _ini.add_entry("Options","GridVisible", OpenFlipper::Options::gridVisible() );
445

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

Dirk Wilden's avatar
Dirk Wilden committed
448
449
    if ( !_ini.section_exists("ViewerProperties") )
      _ini.add_section("ViewerProperties");
Jan Möbius's avatar
 
Jan Möbius committed
450

Dirk Wilden's avatar
Dirk Wilden committed
451
    for ( unsigned int i = 0 ; i < OpenFlipper::Options::examinerWidgets(); ++i ) {
Dirk Wilden's avatar
Dirk Wilden committed
452

Dirk Wilden's avatar
Dirk Wilden committed
453
454
455
456
457
458
      _ini.add_entry("ViewerProperties","Animation" + QString::number(i),       PluginFunctions::viewerProperties(i).animation());
      _ini.add_entry("ViewerProperties","BackfaceCulling" + QString::number(i), PluginFunctions::viewerProperties(i).backFaceCulling());
      _ini.add_entry("ViewerProperties","TwoSidedLighting" + QString::number(i),PluginFunctions::viewerProperties(i).twoSidedLighting());
      _ini.add_entry("ViewerProperties","BackgroundColor" + QString::number(i),
                     (uint)PluginFunctions::viewerProperties(i).backgroundColorRgb() );
    }
Jan Möbius's avatar
 
Jan Möbius committed
459
    //============================================================================
460
    // Save the current viewer properties
Jan Möbius's avatar
 
Jan Möbius committed
461
462
    //============================================================================
    std::vector< QString > draw_modes;
463
464
465
466
467
468
469
470
471

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

      _ini.add_entry("Options","DefaultProjectionMode" + QString::number(i), OpenFlipper::Options::defaultProjectionMode(i) );
      _ini.add_entry("Options","DefaultViewingDirection" + QString::number(i), OpenFlipper::Options::defaultViewingDirection(i) );
    }
472

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

475
    _ini.add_entry("Options", "StereoMode",OpenFlipper::Options::stereoMode() );
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
476
477
478

    _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
479
480
  }

481
482
  emit saveOnExit(_ini);

Jan Möbius's avatar
Jan Möbius committed
483
  // _ini.add_entry("Options","Stereo",OpenFlipper::Options::stereo() );
Jan Möbius's avatar
 
Jan Möbius committed
484
485
}

486
487
488
489
void Core::openIniFile( QString _filename,
                        bool    _coreSettings,
                        bool    _perPluginSettings,
                        bool    _loadObjects ){
Jan Möbius's avatar
 
Jan Möbius committed
490
491
492
  INIFile ini;

  if ( ! ini.connect(_filename,false) ) {
493
    emit log(LOGERR,tr("Failed to connect to ini file") + _filename);
Jan Möbius's avatar
 
Jan Möbius committed
494
495
    return;
  }
Jan Möbius's avatar
Jan Möbius committed
496

Jan Möbius's avatar
 
Jan Möbius committed
497
  if ( OpenFlipper::Options::gui() ) {
498
    coreWidget_->statusMessage( tr("Loading ini File ") + _filename + " ...");
Jan Möbius's avatar
 
Jan Möbius committed
499
500
501
    coreWidget_->setStatus(ApplicationStatus::BLOCKED );
  }

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

505
506
507
  // Load Core settings only if requested
  if ( _coreSettings )
    readApplicationOptions(ini);
Jan Möbius's avatar
 
Jan Möbius committed
508

509
510
511
  // if requested load per Plugin settings from the settings file
  if ( _perPluginSettings )
    emit iniLoadOptions( ini );
Jan Möbius's avatar
 
Jan Möbius committed
512

513
  if ( _loadObjects ) {
Jan Möbius's avatar
 
Jan Möbius committed
514

515
    QStringList openFiles;
Jan Möbius's avatar
 
Jan Möbius committed
516

517
518
    // 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
519

520
      bool newActiveObject = false;
Jan Möbius's avatar
 
Jan Möbius committed
521

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

524
        QString sectionName = openFiles[i];
525

526
527
528
529
530
531
        // 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;
        }
532

533
534
535
536
537
        // 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;
        }
538

539
        // Get the path for the file which should be opened
540
        QString path;
541
542
543
544
        if ( !ini.get_entry( path, sectionName , "path" ) ) {
          emit log(LOGERR,tr("Error parsing ini file. Section %1 contains no path description!").arg(sectionName));
          continue;
        }
545

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

549
          // check if _filename contains a path by testing if it contains a directory separator
550
551
552
553
          if (_filename.section(OpenFlipper::Options::dirSeparator(), 0, -2) != ""){
            path.remove(0,1); // remove .
            path = _filename.section(OpenFlipper::Options::dirSeparator(), 0, -2) + path;
          }
554

Jan Möbius's avatar
 
Jan Möbius committed
555
556
        }

557
        int tmpType;
558
        DataType type = typeId("TriangleMesh");
559

560
        // First check for old datatype style (Only numbers .. therefore not consistent for runtime added types)
561
562
        if ( ini.get_entry( tmpType, sectionName , "type"  )) {
          type = DataType(tmpType);
563
564
          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 )") );
565
        } else {
566

567
          // Read new style type. The type is represented by its name (as a QString)
568
569
570
571
          QString typeName="";
          if ( ini.get_entry( typeName, sectionName , "type"  )) {
            type = typeId(typeName);
          } else
572
            emit log(LOGWARN, tr("Unable to get DataType for object %1 assuming Triangle Mesh!").arg(sectionName) );
573
        }
Jan Möbius's avatar
 
Jan Möbius committed
574

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

578
        // get the new object from the object tree ( If that fails, the object was not loaded correctly)
579
580
        BaseObject* object = objectRoot_->childExists( newObjectId );
        if ( object == 0 )  {
581
          emit log(LOGERR,tr("Unable to open Object ") + path);
582
583
584
          continue;
        }

585
        // Read the target flag setting
586
        bool flag;
587
        if ( ini.get_entry( flag, sectionName , "target" ) )
588
589
          object->target(flag);

590
        // Read the source flag setting
591
592
        if ( ini.get_entry( flag, sectionName , "source" ) )
          object->source(flag);
Jan Möbius's avatar
 
Jan Möbius committed
593

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

597
      }
Jan Möbius's avatar
 
Jan Möbius committed
598
599
600
601
    }

  }

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

606
  // close ini file
Jan Möbius's avatar
 
Jan Möbius committed
607
608
  ini.disconnect();

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

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

Dirk Wilden's avatar
Dirk Wilden committed
616
617
  if ( OpenFlipper::Options::gui() ){
    for ( unsigned int i = 0 ; i < OpenFlipper::Options::examinerWidgets() ; ++i ) {
618
      PluginFunctions::viewerProperties(i).drawMode( OpenFlipper::Options::defaultDrawMode(i) );
Dirk Wilden's avatar
Dirk Wilden committed
619
620
      coreWidget_->examiner_widgets_[i]->viewAll();
    }
Jan Möbius's avatar
Jan Möbius committed
621

622
    coreWidget_->statusMessage( tr("Loading ini File ") + _filename + tr(" ... Done"), 4000);
Jan Möbius's avatar
 
Jan Möbius committed
623
624
625
626
627
    coreWidget_->setStatus(ApplicationStatus::READY );
  }

}

628
629
630
631
632
633
void Core::writeIniFile(QString _filename,
                        bool _relativePaths,
                        bool _targetOnly,
                        bool _saveSystemSettings,
                        bool _savePluginSettings ,
                        bool _saveObjectInfo ) {
Jan Möbius's avatar
Jan Möbius committed
634

Jan Möbius's avatar
 
Jan Möbius committed
635
636
637
  INIFile ini;

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

Jan Möbius's avatar
 
Jan Möbius committed
642
  if ( OpenFlipper::Options::gui() ) {
643
    coreWidget_->statusMessage( tr("Saving ini File ") + _filename + " ...");
Jan Möbius's avatar
 
Jan Möbius committed
644
645
646
    coreWidget_->setStatus(ApplicationStatus::BLOCKED );
  }

647
  // Only save application settings when requested
648
  if ( _saveSystemSettings )
649
    writeApplicationOptions(ini);
Jan Möbius's avatar
 
Jan Möbius committed
650

651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
  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
668
                                          o_it != PluginFunctions::objectsEnd(); ++o_it) {
669
      QString file = o_it->path() + OpenFlipper::Options::dirSeparator() + o_it->name();
670
671
672
673
674
675
676
677
    
      // Don't save default light source objects
      LightObject* light = 0;
      PluginFunctions::getObject( o_it->id(), light );
      if(light != 0) {
          if(light->defaultLight()) continue;
      }
    
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
      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
695

696
      }
Jan Möbius's avatar
 
Jan Möbius committed
697
698
    }

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

701
702
    // Tell plugins to save their information for the given object
    for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::ALL_OBJECTS) ;
703
704
705
706
707
708
709
710
                                          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;
      }
        
711
      emit iniSave(  ini , o_it->id() );
712
    }
713
  }
Jan Möbius's avatar
 
Jan Möbius committed
714
715
716
717



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

Jan Möbius's avatar
 
Jan Möbius committed
719
  if ( OpenFlipper::Options::gui() ) {
720
    coreWidget_->statusMessage( tr("Saving ini File ") + _filename + tr(" ... Done"), 4000);
Jan Möbius's avatar
 
Jan Möbius committed
721
722
723
724
725
726
    coreWidget_->setStatus(ApplicationStatus::READY );
  }
}


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