saveSettings.cc 9.22 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
//=============================================================================
//
//                               OpenFlipper
//        Copyright (C) 2008 by Computer Graphics Group, RWTH Aachen
//                           www.openflipper.org
//
//-----------------------------------------------------------------------------
//
//                                License
//
//  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.
//
//  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 Lesser General Public License
//  along with OpenFlipper.  If not, see <http://www.gnu.org/licenses/>.
//
//-----------------------------------------------------------------------------
//
Jan Möbius's avatar
Jan Möbius committed
26 27 28
//   $Revision$
//   $Author$
//   $Date$
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
//
//=============================================================================


#include "Core.hh"

#include "OpenFlipper/common/GlobalOptions.hh"

#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
48
  // ========================================================================================
49
  // generate the saveSettings-Dialog
Jan Möbius's avatar
Jan Möbius committed
50
  // ========================================================================================
51 52 53 54 55 56 57 58 59 60

  QString complete_name;

  QFileDialog fileDialog( coreWidget_,
                          tr("Save Settings"),
                          OpenFlipper::Options::currentDirStr(),
                          tr("INI files (*.ini);;OBJ files (*.obj )") );

  fileDialog.setAcceptMode ( QFileDialog::AcceptSave );
  fileDialog.setFileMode ( QFileDialog::AnyFile );
61

62 63 64 65 66 67 68
  QGridLayout *layout = (QGridLayout*)fileDialog.layout();

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

Jan Möbius's avatar
Jan Möbius committed
69 70 71 72 73
  QCheckBox *saveProgramSettings = new QCheckBox(optionsBox);
  saveProgramSettings->setText("Save program settings");
  saveProgramSettings->setToolTip("Save all current program settings to the file ( This will include view settings, colors,...) ");
  saveProgramSettings->setCheckState( Qt::Unchecked );

74 75 76 77 78 79 80 81 82 83
  QCheckBox *savePluginSettings = new QCheckBox(optionsBox);
  savePluginSettings->setText("Save per Plugin Settings");
  savePluginSettings->setToolTip("Plugins should add their current global settings to the file");
  savePluginSettings->setCheckState( Qt::Checked );

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

84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
  QCheckBox *saveAllBox = new QCheckBox(optionsBox);
  saveAllBox->setText("Save everything to same folder");
  saveAllBox->setToolTip("Save all open files to the same folder as the ini file");
  saveAllBox->setCheckState( Qt::Checked );

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

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

  QBoxLayout* frameLayout = new QBoxLayout(QBoxLayout::TopToBottom,optionsBox);
100 101 102 103 104 105
  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);
106 107
  frameLayout->addStretch();

Jan Möbius's avatar
Jan Möbius committed
108
  fileDialog.resize(550 ,600);
109

Jan Möbius's avatar
Jan Möbius committed
110 111 112
  // ========================================================================================
  // show the saveSettings-Dialog and get the target file
  // ========================================================================================
113 114 115 116 117 118 119 120 121 122 123 124 125 126
  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
127
  //check the extension if its a known one
128
  if ( !complete_name.endsWith(".ini", Qt::CaseInsensitive) && !complete_name.endsWith(".obj", Qt::CaseInsensitive) ){
Jan Möbius's avatar
Jan Möbius committed
129 130

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

136 137
  }

Jan Möbius's avatar
Jan Möbius committed
138
  // Get the chosen directory and remember it.
139 140 141
  QString newpath = complete_name.section(OpenFlipper::Options::dirSeparator(),0,-2);
  OpenFlipper::Options::currentDir(newpath);

Jan Möbius's avatar
Jan Möbius committed
142 143 144
  // ========================================================================================
  // update status information
  // ========================================================================================
145 146 147 148 149 150 151
  OpenFlipper::Options::savingSettings(true);

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

Jan Möbius's avatar
Jan Möbius committed
152 153 154 155
  // ========================================================================================
  // Save the objects itself
  // ========================================================================================
  // Depending on the checkbox iterate over all objects or only the selected ones.
156

157
  if ( saveObjectInfo->isChecked() ) {
158

159 160 161
    PluginFunctions::IteratorRestriction restriction;
    if ( targetOnly->isChecked() )
      restriction = PluginFunctions::TARGET_OBJECTS;
162
    else
163 164 165 166
      restriction = PluginFunctions::ALL_OBJECTS;

    //Iterate over opened objects and save them
    for ( PluginFunctions::ObjectIterator o_it(restriction);
Jan Möbius's avatar
 
Jan Möbius committed
167
                                          o_it != PluginFunctions::objectsEnd(); ++o_it)
168
    {
169
      QString filename;
170

171 172 173
      if ( saveAllBox->isChecked() )
      {
        // Use path of settings file for all objects
174 175
        filename = newpath + OpenFlipper::Options::dirSeparator() + o_it->name();
      }
176 177 178 179 180 181 182 183 184 185 186 187
      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;
        }
      }
188

189 190
      // enforce that all files end with obj extension if its an obj-settings file
      if ( complete_name.endsWith("obj") )
191
      {
192 193 194 195 196 197 198 199
        if (!filename.endsWith("obj"))
        {
          // remove old extension
          int pos = filename.lastIndexOf(".");
          filename.remove(pos+1, filename.length() - pos);
          // add obj extension
          filename += "obj";
        }
200 201
      }

202 203 204 205 206
      // decide whether to use saveObject or saveObjectTo
      if ( !QFile(filename).exists() || !askOverwrite->isChecked() )
        saveObject( o_it->id(), filename);
      else
        saveObjectTo(o_it->id(), filename);
207

208
    }
209 210 211
  }


Jan Möbius's avatar
Jan Möbius committed
212 213 214
  // ========================================================================================
  // Finally save all Settings
  // ========================================================================================
215
  if ( complete_name.endsWith("ini") ) {
Jan Möbius's avatar
Jan Möbius committed
216 217

    // write to ini
218 219 220 221 222 223
    writeIniFile( complete_name,
                  saveAllBox->isChecked(),
                  targetOnly->isChecked(),
                  saveProgramSettings->isChecked(),
                  savePluginSettings->isChecked(),
                  saveObjectInfo->isChecked());
Jan Möbius's avatar
Jan Möbius committed
224

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

227 228
    //write to obj
    writeObjFile(complete_name, saveAllBox->isChecked(), targetOnly->isChecked() );
Jan Möbius's avatar
Jan Möbius committed
229

230 231
  }

Jan Möbius's avatar
Jan Möbius committed
232
  // update status
233 234 235 236 237 238 239 240 241 242 243 244
  OpenFlipper::Options::savingSettings(false);

  if ( OpenFlipper::Options::gui() ) {
    coreWidget_->statusMessage( "Saving Settings to " + complete_name + " ... Done", 4000);
    coreWidget_->setStatus(ApplicationStatus::READY );
  }

  //add to recent files
  if ( OpenFlipper::Options::gui() )
    coreWidget_->addRecent( complete_name, DATA_NONE );

}