saveSettings.cc 11.4 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
\*===========================================================================*/
42 43 44 45 46 47


#include "Core.hh"

#include "OpenFlipper/common/GlobalOptions.hh"

48 49
#include <ObjectTypes/Light/Light.hh>

50 51 52 53 54 55 56 57 58 59 60
#include "OpenFlipper/BasePlugin/PluginFunctions.hh"

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

#include <QFileDialog>
#include <QGroupBox>
#include <QBoxLayout>

/// Save Settings (slot is called from CoreWidget's File-Menu)
void Core::saveSettings(){

Jan Möbius's avatar
Jan Möbius committed
61
  // ========================================================================================
62
  // generate the saveSettings-Dialog
Jan Möbius's avatar
Jan Möbius committed
63
  // ========================================================================================
64 65 66 67 68

  QString complete_name;

  QFileDialog fileDialog( coreWidget_,
                          tr("Save Settings"),
69
                          OpenFlipperSettings().value("Core/CurrentDir").toString(),
70 71
                          tr("INI files (*.ini);;OBJ files (*.obj )") );

72
  fileDialog.setOption (QFileDialog::DontUseNativeDialog, true);
73 74
  fileDialog.setAcceptMode ( QFileDialog::AcceptSave );
  fileDialog.setFileMode ( QFileDialog::AnyFile );
75

76 77 78 79
  QGridLayout *layout = (QGridLayout*)fileDialog.layout();

  QGroupBox* optionsBox = new QGroupBox( &fileDialog ) ;
  optionsBox->setSizePolicy( QSizePolicy ( QSizePolicy::Expanding , QSizePolicy::Preferred ) );
80
  optionsBox->setTitle(tr("Options"));
81 82
  layout->addWidget( optionsBox, layout->rowCount() , 0 , 1,layout->columnCount() );

Jan Möbius's avatar
Jan Möbius committed
83
  QCheckBox *saveProgramSettings = new QCheckBox(optionsBox);
84 85
  saveProgramSettings->setText(tr("Save program settings"));
  saveProgramSettings->setToolTip(tr("Save all current program settings to the file ( This will include view settings, colors,...) "));
Jan Möbius's avatar
Jan Möbius committed
86 87
  saveProgramSettings->setCheckState( Qt::Unchecked );

88
  QCheckBox *savePluginSettings = new QCheckBox(optionsBox);
89 90
  savePluginSettings->setText(tr("Save per Plugin Settings"));
  savePluginSettings->setToolTip(tr("Plugins should add their current global settings to the file"));
91 92 93
  savePluginSettings->setCheckState( Qt::Checked );

  QCheckBox *saveObjectInfo = new QCheckBox(optionsBox);
94 95
  saveObjectInfo->setText(tr("Save open object information to the file"));
  saveObjectInfo->setToolTip(tr("Save all open Objects and add them to the settings file ( they will be loaded if opening the settings file"));
96 97
  saveObjectInfo->setCheckState( Qt::Checked );

98
  QCheckBox *saveAllBox = new QCheckBox(optionsBox);
99 100
  saveAllBox->setText(tr("Save everything to same folder"));
  saveAllBox->setToolTip(tr("Save all open files to the same folder as the ini file"));
101 102 103
  saveAllBox->setCheckState( Qt::Checked );

  QCheckBox *askOverwrite = new QCheckBox(optionsBox);
104 105
  askOverwrite->setText(tr("Ask before overwriting files"));
  askOverwrite->setToolTip(tr("If a file exists you will get asked what to do"));
106 107 108
  askOverwrite->setCheckState( Qt::Checked );

  QCheckBox *targetOnly = new QCheckBox(optionsBox);
109 110
  targetOnly->setText(tr("Save only target objects"));
  targetOnly->setToolTip(tr("Only objects with target flag will be handled"));
111 112 113
  targetOnly->setCheckState( Qt::Unchecked );

  QBoxLayout* frameLayout = new QBoxLayout(QBoxLayout::TopToBottom,optionsBox);
114 115 116 117 118 119
  frameLayout->addWidget( saveProgramSettings , 0 , 0);
  frameLayout->addWidget( savePluginSettings  , 1 , 0);
  frameLayout->addWidget( saveObjectInfo      , 2 , 0);
  frameLayout->addWidget( saveAllBox          , 3 , 0);
  frameLayout->addWidget( askOverwrite        , 4 , 0);
  frameLayout->addWidget( targetOnly          , 5 , 0);
120 121
  frameLayout->addStretch();

122
  fileDialog.resize(550 ,600);
123

Jan Möbius's avatar
Jan Möbius committed
124 125 126
  // ========================================================================================
  // show the saveSettings-Dialog and get the target file
  // ========================================================================================
127 128 129 130 131 132 133 134 135 136 137 138 139 140
  QStringList fileNames;
  if (fileDialog.exec()) {
    fileNames = fileDialog.selectedFiles();
  } else {
    return;
  }

  if ( fileNames.size() > 1 ) {
    std::cerr << "Too many save filenames selected" << std::endl;
    return;
  }

  complete_name = fileNames[0];

Jan Möbius's avatar
Jan Möbius committed
141
  //check the extension if its a known one
142
  if ( !complete_name.endsWith(".ini", Qt::CaseInsensitive) && !complete_name.endsWith(".obj", Qt::CaseInsensitive) ){
Jan Möbius's avatar
Jan Möbius committed
143 144

    // If its unknown, get the type from the currently selected filter and add this extension to the filename
145
    if ( fileDialog.selectedNameFilter().contains(tr("INI files (*.ini)")) )
146 147 148
      complete_name += ".ini";
    else
      complete_name += ".obj";
Jan Möbius's avatar
Jan Möbius committed
149

150 151
  }

Jan Möbius's avatar
Jan Möbius committed
152
  // Get the chosen directory and remember it.
153
  QString newpath = complete_name.section(OpenFlipper::Options::dirSeparator(),0,-2);
154
  OpenFlipperSettings().setValue("Core/CurrentDir", newpath);
155

Jan Möbius's avatar
Jan Möbius committed
156 157 158
  // ========================================================================================
  // update status information
  // ========================================================================================
159 160 161
  OpenFlipper::Options::savingSettings(true);

  if ( OpenFlipper::Options::gui() ) {
162
    coreWidget_->statusMessage( tr("Saving Settings to ") + complete_name + " ...");
163 164 165
    coreWidget_->setStatus(ApplicationStatus::BLOCKED );
  }

Jan Möbius's avatar
Jan Möbius committed
166 167 168 169
  // ========================================================================================
  // Save the objects itself
  // ========================================================================================
  // Depending on the checkbox iterate over all objects or only the selected ones.
170

171
  if ( saveObjectInfo->isChecked() ) {
172

173 174 175
    PluginFunctions::IteratorRestriction restriction;
    if ( targetOnly->isChecked() )
      restriction = PluginFunctions::TARGET_OBJECTS;
176
    else
177 178 179 180
      restriction = PluginFunctions::ALL_OBJECTS;

    //Iterate over opened objects and save them
    for ( PluginFunctions::ObjectIterator o_it(restriction);
181
                                          o_it != PluginFunctions::objectsEnd(); ++o_it)
182
    {
183
      QString filename;
184

185 186 187
      if ( saveAllBox->isChecked() )
      {
        // Use path of settings file for all objects
188 189
        filename = newpath + OpenFlipper::Options::dirSeparator() + o_it->name();
      }
190 191 192 193 194 195 196 197 198 199 200 201
      else
      {
        // Use objects own path if it has one. Otherwise also use path of settings file
        filename = o_it->path() + OpenFlipper::Options::dirSeparator() + o_it->name();

        // handle the case that the object was created in current session and not loaded from disk
        if (o_it->path() == ".") {
          filename = newpath + OpenFlipper::Options::dirSeparator() + o_it->name();
          std::cerr << "newpath : " << newpath.toStdString() << std::endl;
          std::cerr << "name  : " << o_it->name().toStdString() << std::endl;
        }
      }
202

203 204
      // enforce that all files end with obj extension if its an obj-settings file
      if ( complete_name.endsWith("obj") )
205
      {
206 207 208 209 210 211 212 213
        if (!filename.endsWith("obj"))
        {
          // remove old extension
          int pos = filename.lastIndexOf(".");
          filename.remove(pos+1, filename.length() - pos);
          // add obj extension
          filename += "obj";
        }
214
      }
215 216 217 218 219 220 221
      
      // Don't save default light source objects
      LightObject* light = 0;
      PluginFunctions::getObject( o_it->id(), light );
      if(light != 0) {
          if(light->defaultLight()) continue;
      }
222

223 224 225 226 227
      // decide whether to use saveObject or saveObjectTo
      if ( !QFile(filename).exists() || !askOverwrite->isChecked() )
        saveObject( o_it->id(), filename);
      else
        saveObjectTo(o_it->id(), filename);
228

229
    }
230 231 232
  }


Jan Möbius's avatar
Jan Möbius committed
233 234 235
  // ========================================================================================
  // Finally save all Settings
  // ========================================================================================
236
  if ( complete_name.endsWith("ini") ) {
Jan Möbius's avatar
Jan Möbius committed
237 238

    // write to ini
239 240 241 242 243 244
    writeIniFile( complete_name,
                  saveAllBox->isChecked(),
                  targetOnly->isChecked(),
                  saveProgramSettings->isChecked(),
                  savePluginSettings->isChecked(),
                  saveObjectInfo->isChecked());
Jan Möbius's avatar
Jan Möbius committed
245

246
  } else if ( complete_name.endsWith("obj") ) {
Jan Möbius's avatar
Jan Möbius committed
247

248 249
    //write to obj
    writeObjFile(complete_name, saveAllBox->isChecked(), targetOnly->isChecked() );
Jan Möbius's avatar
Jan Möbius committed
250

251 252
  }

Jan Möbius's avatar
Jan Möbius committed
253
  // update status
254 255 256
  OpenFlipper::Options::savingSettings(false);

  if ( OpenFlipper::Options::gui() ) {
257
    coreWidget_->statusMessage( tr("Saving Settings to ") + complete_name + tr(" ... Done"), 4000);
258 259 260 261 262
    coreWidget_->setStatus(ApplicationStatus::READY );
  }

  //add to recent files
  if ( OpenFlipper::Options::gui() )
263
    coreWidget_->addRecent( complete_name, DATA_UNKNOWN );
264 265

}