saveFunctions.cc 11.6 KB
Newer Older
Jan Möbius's avatar
   
Jan Möbius committed
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
//=============================================================================
//
//                               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/>.
//
//-----------------------------------------------------------------------------
//
//   $Revision$
//   $Author$
//   $Date$
//
//=============================================================================





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

/// Save an object
bool Core::saveObject( int _id, QString _filename ) {
  BaseObjectData* object;
  PluginFunctions::get_object(_id,object);

  for (int i=0; i < (int)supportedTypes_.size(); i++)
    if (object->dataType() == supportedTypes_[i].type) {

      if ( OpenFlipper::Options::gui() ) {
        coreWidget_->statusMessage( "Saving " + _filename + " ...");
        if ( !OpenFlipper::Options::openingIni() )
          coreWidget_->setStatus(ApplicationStatus::PROCESSING );
      }

      //load file
      bool ok = supportedTypes_[i].plugin->saveObject(_id,_filename);

      if ( OpenFlipper::Options::gui() ) {
        if (ok)
          coreWidget_->statusMessage( "Saving " + _filename + " ... done", 4000 );
        else
          coreWidget_->statusMessage( "Saving " + _filename + " ... failed!", 4000 );

        if ( !OpenFlipper::Options::openingIni() )
          coreWidget_->setStatus(ApplicationStatus::READY );
      }

      return ok;
    }

  return false; //no plugin found
}

/// Save an object to a new location
bool Core::saveObjectTo( int _id, QString _filename ) {
  BaseObjectData* object;
  PluginFunctions::get_object(_id,object);

   //init widget
  static LoadWidget* widget = 0;
  if ( !widget ){
    widget = new LoadWidget(supportedTypes_);
    connect(widget,SIGNAL(load(QString, DataType, int&)),this,SLOT(slotLoad(QString, DataType, int&)));
    connect(widget,SIGNAL(save(int, QString)),this,SLOT(slotSave(int, QString)));
  }

  if (supportedTypes_.size() != 0)
    return widget->showSave(_id,_filename);
  else{
    emit log(LOGERR,"Could not show 'save objects' dialog. Missing file-plugins.");
    return false;
  }
}

//========================================================================================
// ===             Save Slots                                 ============================
//========================================================================================

/// A plugin wants to save an object
/// existing filename will be overwritten!!!
void Core::slotSave(int _id , QString _filename) {
  saveObject(_id,_filename);

  BaseObjectData* object;
  PluginFunctions::get_object(_id,object);
  if ( object != 0 && OpenFlipper::Options::gui() )
    coreWidget_->addRecent( _filename, object->dataType() );
}

//========================================================================================
// ===             Menu Slots                                 ============================
//========================================================================================

/// Save changes to objects
void Core::slotSaveMenu(){
  //iterate over all target objects
  bool overwrite_all = false;
  for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::TARGET_OBJECTS) ;
        o_it != PluginFunctions::objects_end(); ++o_it)  {
          if ( !QDir(o_it->path()).exists() || o_it->path().trimmed() == "" ) // if path isn't valid use 'save object to'
            saveObjectTo(o_it->id(),o_it->name());
          else{
            QString filename = o_it->path() + OpenFlipper::Options::dirSeparator() + o_it->name() ;
            QFile file(filename);
            if ( file.exists() && !overwrite_all){
              int ret = QMessageBox::warning(coreWidget_, tr("File exists"),QString("The file '" +filename+ "' already exists.\n"+
                  "Do you want to overwrite the file?"),QMessageBox::YesToAll |
                      QMessageBox::Yes|QMessageBox::No|QMessageBox::NoToAll,QMessageBox::No);
              switch(ret){
                case QMessageBox::YesToAll: overwrite_all = true;break;
                case QMessageBox::Yes: break;
                case QMessageBox::No: continue;
                case QMessageBox::NoToAll: return;
              }
            }
            saveObject(o_it->id(),filename);
          }
        }
}

/// Save object as
void Core::slotSaveToMenu(){
  //init widget
  static LoadWidget* widget = 0;
  if ( !widget ){
    widget = new LoadWidget(supportedTypes_);
    connect(widget,SIGNAL(load(QString, DataType, int&)),this,SLOT(slotLoad(QString, DataType, int&)));
    connect(widget,SIGNAL(save(int, QString)),this,SLOT(slotSave(int, QString)));
  }

  if (supportedTypes_.size() != 0){
    //iterate over all target objects
    for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::TARGET_OBJECTS);
          o_it != PluginFunctions::objects_end(); ++o_it)  {
      QString filename = o_it->path() + OpenFlipper::Options::dirSeparator() + o_it->name();
      widget->showSave(o_it->id(),filename);
    }
  }else
    emit log(LOGERR,"Could not show 'save objects' dialog. Missing file-plugins.");
}

/// Save to Ini File
void Core::slotSaveIniMenu(){

  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 );
  fileDialog.setDefaultSuffix("ini");
  QGridLayout *layout = (QGridLayout*)fileDialog.layout();

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

  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");
Dirk Wilden's avatar
Dirk Wilden committed
197
  saveAllBox->setCheckState( Qt::Checked );
Jan Möbius's avatar
   
Jan Möbius committed
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271

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

  QBoxLayout* frameLayout = new QBoxLayout(QBoxLayout::TopToBottom,optionsBox);
  frameLayout->addWidget( saveAllBox   , 0 , 0);
  frameLayout->addWidget( askOverwrite , 1 , 0);
  frameLayout->addStretch();

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

  QString newpath = complete_name.section(OpenFlipper::Options::dirSeparator(),0,-2);
  OpenFlipper::Options::currentDir(newpath);

  std::cout << "new path: " << newpath.toStdString() << std::endl;
  std::cout << "complete_name: " << complete_name.toStdString() << std::endl;

  //init save-as widget
  static LoadWidget* widget = 0;
  if ( !widget ){
    widget = new LoadWidget(supportedTypes_);
    connect(widget,SIGNAL(load(QString, DataType, int&)),this,SLOT(slotLoad(QString, DataType, int&)));
    connect(widget,SIGNAL(save(int, QString)),this,SLOT(slotSave(int, QString)));
  }

  //iterate over opened objects and save them
  for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::ALL_OBJECTS) ;
                                        o_it != PluginFunctions::objects_end(); ++o_it)
  {
      if ( saveAllBox->isChecked() )
      {
        // Use path of ini file for all objects
        QString filename = newpath + OpenFlipper::Options::dirSeparator() + o_it->name();

        // enforce that all files end with obj extension
        if ( complete_name.endsWith("obj") )
        {
          if (!filename.endsWith("obj"))
          {
            std::cout << "\nold filename: " << filename.toStdString() << std::endl;
            // remove old extension
            int pos = filename.lastIndexOf(".");
            filename.remove(pos+1, filename.length() - pos);
            std::cout << "cleared filename = " << filename.toStdString() << std::endl;
            // add obj extension
            filename += "obj";
            std::cout << "new filename: " << filename.toStdString() << std::endl;
          }
        }


        // normally the file does not exist when saving all to one folder
        if ( !QFile(filename).exists() || !askOverwrite->isChecked() )
          saveObject( o_it->id(), filename);
        else
          widget->showSave(o_it->id(), filename);

      }
      else
      {
Dirk Wilden's avatar
Dirk Wilden committed
272
273
//         QString filename = o_it->path() + OpenFlipper::Options::dirSeparator() + o_it->name();
        QString filename = newpath + OpenFlipper::Options::dirSeparator() + o_it->name();
Jan Möbius's avatar
   
Jan Möbius committed
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319

        std::cerr << "o_it->path  : " << o_it->path().toStdString() << std::endl;

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

        }

        // enforce that all files end with obj extension
        if ( complete_name.endsWith("obj") )
        {
          if (!filename.endsWith("obj"))
          {
            std::cout << "\nold filename: " << filename.toStdString() << std::endl;
            // remove old extension
            int pos = filename.lastIndexOf(".");
            filename.remove(pos+1, filename.length() - pos);
            std::cout << "cleared filename = " << filename.toStdString() << std::endl;
            // add obj extension
            filename += "obj";
            std::cout << "new filename: " << filename.toStdString() << std::endl;
          }
        }

        if ( QFile(filename).exists() && !askOverwrite->isChecked() )
          saveObject( o_it->id(), filename);
        else
          widget->showSave(o_it->id(), filename);
      }

  }


  if ( complete_name.endsWith("ini") ) {
    //write to ini
    writeIniFile(complete_name, saveAllBox->isChecked());
  } else if ( complete_name.endsWith("obj") ) {
    //write to obj
    writeObjFile(complete_name, saveAllBox->isChecked());
  }


}
320
321