saveSettings.cc

00001 /*===========================================================================*\
00002  *                                                                           *
00003  *                              OpenFlipper                                  *
00004  *      Copyright (C) 2001-2009 by Computer Graphics Group, RWTH Aachen      *
00005  *                           www.openflipper.org                             *
00006  *                                                                           *
00007  *---------------------------------------------------------------------------*
00008  *  This file is part of OpenFlipper.                                        *
00009  *                                                                           *
00010  *  OpenFlipper is free software: you can redistribute it and/or modify      *
00011  *  it under the terms of the GNU Lesser General Public License as           *
00012  *  published by the Free Software Foundation, either version 3 of           *
00013  *  the License, or (at your option) any later version with the              *
00014  *  following exceptions:                                                    *
00015  *                                                                           *
00016  *  If other files instantiate templates or use macros                       *
00017  *  or inline functions from this file, or you compile this file and         *
00018  *  link it with other files to produce an executable, this file does        *
00019  *  not by itself cause the resulting executable to be covered by the        *
00020  *  GNU Lesser General Public License. This exception does not however       *
00021  *  invalidate any other reasons why the executable file might be            *
00022  *  covered by the GNU Lesser General Public License.                        *
00023  *                                                                           *
00024  *  OpenFlipper is distributed in the hope that it will be useful,           *
00025  *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
00026  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
00027  *  GNU Lesser General Public License for more details.                      *
00028  *                                                                           *
00029  *  You should have received a copy of the GNU LesserGeneral Public          *
00030  *  License along with OpenFlipper. If not,                                  *
00031  *  see <http://www.gnu.org/licenses/>.                                      *
00032  *                                                                           *
00033 \*===========================================================================*/
00034 
00035 /*===========================================================================*\
00036  *                                                                           *
00037  *   $Revision: 7306 $                                                         *
00038  *   $Author: kremer $                                                      *
00039  *   $Date: 2009-10-02 15:25:40 +0200 (Fr, 02. Okt 2009) $                   *
00040  *                                                                           *
00041 \*===========================================================================*/
00042 
00043 
00044 #include "Core.hh"
00045 
00046 #include "OpenFlipper/common/GlobalOptions.hh"
00047 
00048 #include "OpenFlipper/BasePlugin/PluginFunctions.hh"
00049 
00050 #include "OpenFlipper/widgets/loadWidget/loadWidget.hh"
00051 
00052 #include <QFileDialog>
00053 #include <QGroupBox>
00054 #include <QBoxLayout>
00055 
00057 void Core::saveSettings(){
00058 
00059   // ========================================================================================
00060   // generate the saveSettings-Dialog
00061   // ========================================================================================
00062 
00063   QString complete_name;
00064 
00065   QFileDialog fileDialog( coreWidget_,
00066                           tr("Save Settings"),
00067                           OpenFlipper::Options::currentDirStr(),
00068                           tr("INI files (*.ini);;OBJ files (*.obj )") );
00069 
00070   fileDialog.setOption (QFileDialog::DontUseNativeDialog, true);
00071   fileDialog.setAcceptMode ( QFileDialog::AcceptSave );
00072   fileDialog.setFileMode ( QFileDialog::AnyFile );
00073 
00074   QGridLayout *layout = (QGridLayout*)fileDialog.layout();
00075 
00076   QGroupBox* optionsBox = new QGroupBox( &fileDialog ) ;
00077   optionsBox->setSizePolicy( QSizePolicy ( QSizePolicy::Expanding , QSizePolicy::Preferred ) );
00078   optionsBox->setTitle(tr("Options"));
00079   layout->addWidget( optionsBox, layout->rowCount() , 0 , 1,layout->columnCount() );
00080 
00081   QCheckBox *saveProgramSettings = new QCheckBox(optionsBox);
00082   saveProgramSettings->setText(tr("Save program settings"));
00083   saveProgramSettings->setToolTip(tr("Save all current program settings to the file ( This will include view settings, colors,...) "));
00084   saveProgramSettings->setCheckState( Qt::Unchecked );
00085 
00086   QCheckBox *savePluginSettings = new QCheckBox(optionsBox);
00087   savePluginSettings->setText(tr("Save per Plugin Settings"));
00088   savePluginSettings->setToolTip(tr("Plugins should add their current global settings to the file"));
00089   savePluginSettings->setCheckState( Qt::Checked );
00090 
00091   QCheckBox *saveObjectInfo = new QCheckBox(optionsBox);
00092   saveObjectInfo->setText(tr("Save open object information to the file"));
00093   saveObjectInfo->setToolTip(tr("Save all open Objects and add them to the settings file ( they will be loaded if opening the settings file"));
00094   saveObjectInfo->setCheckState( Qt::Checked );
00095 
00096   QCheckBox *saveAllBox = new QCheckBox(optionsBox);
00097   saveAllBox->setText(tr("Save everything to same folder"));
00098   saveAllBox->setToolTip(tr("Save all open files to the same folder as the ini file"));
00099   saveAllBox->setCheckState( Qt::Checked );
00100 
00101   QCheckBox *askOverwrite = new QCheckBox(optionsBox);
00102   askOverwrite->setText(tr("Ask before overwriting files"));
00103   askOverwrite->setToolTip(tr("If a file exists you will get asked what to do"));
00104   askOverwrite->setCheckState( Qt::Checked );
00105 
00106   QCheckBox *targetOnly = new QCheckBox(optionsBox);
00107   targetOnly->setText(tr("Save only target objects"));
00108   targetOnly->setToolTip(tr("Only objects with target flag will be handled"));
00109   targetOnly->setCheckState( Qt::Unchecked );
00110 
00111   QBoxLayout* frameLayout = new QBoxLayout(QBoxLayout::TopToBottom,optionsBox);
00112   frameLayout->addWidget( saveProgramSettings , 0 , 0);
00113   frameLayout->addWidget( savePluginSettings  , 1 , 0);
00114   frameLayout->addWidget( saveObjectInfo      , 2 , 0);
00115   frameLayout->addWidget( saveAllBox          , 3 , 0);
00116   frameLayout->addWidget( askOverwrite        , 4 , 0);
00117   frameLayout->addWidget( targetOnly          , 5 , 0);
00118   frameLayout->addStretch();
00119 
00120   fileDialog.resize(550 ,600);
00121 
00122   // ========================================================================================
00123   // show the saveSettings-Dialog and get the target file
00124   // ========================================================================================
00125   QStringList fileNames;
00126   if (fileDialog.exec()) {
00127     fileNames = fileDialog.selectedFiles();
00128   } else {
00129     return;
00130   }
00131 
00132   if ( fileNames.size() > 1 ) {
00133     std::cerr << "Too many save filenames selected" << std::endl;
00134     return;
00135   }
00136 
00137   complete_name = fileNames[0];
00138 
00139   //check the extension if its a known one
00140   if ( !complete_name.endsWith(".ini", Qt::CaseInsensitive) && !complete_name.endsWith(".obj", Qt::CaseInsensitive) ){
00141 
00142     // If its unknown, get the type from the currently selected filter and add this extension to the filename
00143     if ( fileDialog.selectedNameFilter().contains(tr("INI files (*.ini)")) )
00144       complete_name += ".ini";
00145     else
00146       complete_name += ".obj";
00147 
00148   }
00149 
00150   // Get the chosen directory and remember it.
00151   QString newpath = complete_name.section(OpenFlipper::Options::dirSeparator(),0,-2);
00152   OpenFlipper::Options::currentDir(newpath);
00153 
00154   // ========================================================================================
00155   // update status information
00156   // ========================================================================================
00157   OpenFlipper::Options::savingSettings(true);
00158 
00159   if ( OpenFlipper::Options::gui() ) {
00160     coreWidget_->statusMessage( tr("Saving Settings to ") + complete_name + " ...");
00161     coreWidget_->setStatus(ApplicationStatus::BLOCKED );
00162   }
00163 
00164   // ========================================================================================
00165   // Save the objects itself
00166   // ========================================================================================
00167   // Depending on the checkbox iterate over all objects or only the selected ones.
00168 
00169   if ( saveObjectInfo->isChecked() ) {
00170 
00171     PluginFunctions::IteratorRestriction restriction;
00172     if ( targetOnly->isChecked() )
00173       restriction = PluginFunctions::TARGET_OBJECTS;
00174     else
00175       restriction = PluginFunctions::ALL_OBJECTS;
00176 
00177     //Iterate over opened objects and save them
00178     for ( PluginFunctions::ObjectIterator o_it(restriction);
00179                                           o_it != PluginFunctions::objectsEnd(); ++o_it)
00180     {
00181       QString filename;
00182 
00183       if ( saveAllBox->isChecked() )
00184       {
00185         // Use path of settings file for all objects
00186         filename = newpath + OpenFlipper::Options::dirSeparator() + o_it->name();
00187       }
00188       else
00189       {
00190         // Use objects own path if it has one. Otherwise also use path of settings file
00191         filename = o_it->path() + OpenFlipper::Options::dirSeparator() + o_it->name();
00192 
00193         // handle the case that the object was created in current session and not loaded from disk
00194         if (o_it->path() == ".") {
00195           filename = newpath + OpenFlipper::Options::dirSeparator() + o_it->name();
00196           std::cerr << "newpath : " << newpath.toStdString() << std::endl;
00197           std::cerr << "name  : " << o_it->name().toStdString() << std::endl;
00198         }
00199       }
00200 
00201       // enforce that all files end with obj extension if its an obj-settings file
00202       if ( complete_name.endsWith("obj") )
00203       {
00204         if (!filename.endsWith("obj"))
00205         {
00206           // remove old extension
00207           int pos = filename.lastIndexOf(".");
00208           filename.remove(pos+1, filename.length() - pos);
00209           // add obj extension
00210           filename += "obj";
00211         }
00212       }
00213 
00214       // decide whether to use saveObject or saveObjectTo
00215       if ( !QFile(filename).exists() || !askOverwrite->isChecked() )
00216         saveObject( o_it->id(), filename);
00217       else
00218         saveObjectTo(o_it->id(), filename);
00219 
00220     }
00221   }
00222 
00223 
00224   // ========================================================================================
00225   // Finally save all Settings
00226   // ========================================================================================
00227   if ( complete_name.endsWith("ini") ) {
00228 
00229     // write to ini
00230     writeIniFile( complete_name,
00231                   saveAllBox->isChecked(),
00232                   targetOnly->isChecked(),
00233                   saveProgramSettings->isChecked(),
00234                   savePluginSettings->isChecked(),
00235                   saveObjectInfo->isChecked());
00236 
00237   } else if ( complete_name.endsWith("obj") ) {
00238 
00239     //write to obj
00240     writeObjFile(complete_name, saveAllBox->isChecked(), targetOnly->isChecked() );
00241 
00242   }
00243 
00244   // update status
00245   OpenFlipper::Options::savingSettings(false);
00246 
00247   if ( OpenFlipper::Options::gui() ) {
00248     coreWidget_->statusMessage( tr("Saving Settings to ") + complete_name + tr(" ... Done"), 4000);
00249     coreWidget_->setStatus(ApplicationStatus::READY );
00250   }
00251 
00252   //add to recent files
00253   if ( OpenFlipper::Options::gui() )
00254     coreWidget_->addRecent( complete_name, DATA_UNKNOWN );
00255 
00256 }

acg pic Project OpenFlipper, ©  Computer Graphics Group, RWTH Aachen. Documentation generated using doxygen .