saveFunctions.cc 12.5 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


#include "Core.hh"

#include "OpenFlipper/common/GlobalOptions.hh"

#include "OpenFlipper/BasePlugin/PluginFunctions.hh"

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

//========================================================================================
53
// ===  Public Slots (called by CoreWidget's File-Menu / Scripting / Plugins)    =========
Jan Möbius's avatar
 
Jan Möbius committed
54
55
56
//========================================================================================

/// Save an object
57
58
/// get object with given id, find a plugin to save dataType, save!
/// (existing files will be overwritten)
Jan Möbius's avatar
 
Jan Möbius committed
59
60
bool Core::saveObject( int _id, QString _filename ) {
  BaseObjectData* object;
Jan Möbius's avatar
   
Jan Möbius committed
61
  PluginFunctions::getObject(_id,object);
Jan Möbius's avatar
 
Jan Möbius committed
62

63
  QString file_extension = QFileInfo(_filename).suffix();
64
  
65
  for (int i=0; i < (int)supportedTypes_.size(); i++) {
66
67
    if ( supportedTypes_[i].type.contains(object->dataType()) &&
       ( supportedTypes_[i].saveFilters.contains(file_extension) || file_extension.isEmpty() ) ) {
Jan Möbius's avatar
 
Jan Möbius committed
68
69

      if ( OpenFlipper::Options::gui() ) {
70
        coreWidget_->statusMessage( tr("Saving ") + _filename + " ...");
71
        if ( !OpenFlipper::Options::savingSettings() )
Jan Möbius's avatar
 
Jan Möbius committed
72
73
74
          coreWidget_->setStatus(ApplicationStatus::PROCESSING );
      }

75
      //save object
76
      
Jan Möbius's avatar
 
Jan Möbius committed
77
      bool ok = supportedTypes_[i].plugin->saveObject(_id,_filename);
78
    
Jan Möbius's avatar
 
Jan Möbius committed
79
80
      if ( OpenFlipper::Options::gui() ) {
        if (ok)
81
          coreWidget_->statusMessage( tr("Saving ") + _filename + tr(" ... done"), 4000 );
Dirk Wilden's avatar
Dirk Wilden committed
82
83
        else{
          emit log(LOGERR, tr("Unable to save '%1'. Plugin failed. DataType %2").arg(_filename, dataTypeName(object->dataType()) )  );
84
          emit log(LOGERR, tr("Plugin was: '%1'. File Extension was: %2").arg(supportedTypes_[i].name, file_extension )  );
85
          coreWidget_->statusMessage( tr("Saving ") + _filename + tr(" ... failed!"), 4000 );
Dirk Wilden's avatar
Dirk Wilden committed
86
        }
87
88
    
        if ( !OpenFlipper::Options::savingSettings() )
Jan Möbius's avatar
 
Jan Möbius committed
89
90
91
          coreWidget_->setStatus(ApplicationStatus::READY );
      }

92
93
94
95
96
      //add to recent files
      if (ok && !OpenFlipper::Options::savingSettings()
             &&  OpenFlipper::Options::gui() )
        coreWidget_->addRecent( _filename, object->dataType() );

Jan Möbius's avatar
 
Jan Möbius committed
97
98
      return ok;
    }
99
100
  }
  
101
  // no plugin found
Dirk Wilden's avatar
Dirk Wilden committed
102
103
  if ( OpenFlipper::Options::gui() ){
      emit log(LOGERR, tr("Unable to save '%1'. No plugin found. DataType %2").arg(_filename, dataTypeName(object->dataType()) )  );
104
      coreWidget_->statusMessage( tr("Saving ") + _filename + tr(" ... failed!"), 4000 );
Dirk Wilden's avatar
Dirk Wilden committed
105
  }
106
107

  return false;
Jan Möbius's avatar
 
Jan Möbius committed
108
109
}

110
111
//-----------------------------------------------------------------------------------------------------

Dirk Wilden's avatar
Dirk Wilden committed
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
/// Save an object
/// get object with given id, find a plugin to save dataType, save!
/// (existing files will be overwritten)
void Core::saveObject( int _id, QString _filename, int _pluginID ) {
  BaseObjectData* object;
  PluginFunctions::getObject(_id,object);


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

  //save object
  bool ok = supportedTypes_[_pluginID].plugin->saveObject(_id,_filename);

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

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

  //add to recent files
  if (ok && !OpenFlipper::Options::savingSettings()
          &&  OpenFlipper::Options::gui() )
    coreWidget_->addRecent( _filename, object->dataType() );

}

//-----------------------------------------------------------------------------------------------------

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
/// Save multiple objects to one file
/// get object with given id, find a plugin to save dataType, save!
/// (existing files will be overwritten)
void Core::saveObjects( IdList _ids, QString _filename, int _pluginID ) {
  
  DataType type = 0;
  
  for (uint i=0; i < _ids.size(); i++){
  
    BaseObjectData* object;
    PluginFunctions::getObject(_ids[i],object);

    type |= object->dataType();
  }

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

  //save objects
  if ( !supportedTypes_[_pluginID].saveMultipleObjects){
    emit log(LOGERR, tr("Unable to save objects. Plugin does not allow multiple objects."));
    return;
  }
  
  bool ok = supportedTypes_[_pluginID].plugin->saveObjects(_ids,_filename);

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

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

  //add to recent files
  if (ok && !OpenFlipper::Options::savingSettings()
          &&  OpenFlipper::Options::gui() )
    coreWidget_->addRecent( _filename, type );
}

//-----------------------------------------------------------------------------------------------------

Jan Möbius's avatar
 
Jan Möbius committed
195
/// Save an object to a new location
196
/// Show the save-widget to determine a filename, the widget calls slotSave to save the object
Jan Möbius's avatar
 
Jan Möbius committed
197
198
bool Core::saveObjectTo( int _id, QString _filename ) {

199
200
  bool result = false;

201
  if ( OpenFlipper::Options::gui() ){
202
 
203
    //init widget
204
    LoadWidget* widget = new LoadWidget(supportedTypes_);
205
    widget->setWindowIcon( OpenFlipper::Options::OpenFlipperIcon() );
206

Dirk Wilden's avatar
Dirk Wilden committed
207
    connect(widget,SIGNAL(save(int, QString, int)),this,SLOT(saveObject(int, QString, int)));
208
209
  
    if (supportedTypes_.size() != 0)
210
211
      result = widget->showSave(_id,_filename);
    else
212
      emit log(LOGERR,tr("Could not show 'save objects' dialog. Missing file-plugins."));
Jan Möbius's avatar
 
Jan Möbius committed
213

214
    widget->disconnect();
215
    delete widget;
216
  }
217

218
  return result;
Jan Möbius's avatar
 
Jan Möbius committed
219
220
}

221
//-----------------------------------------------------------------------------------------------------
Jan Möbius's avatar
 
Jan Möbius committed
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
/// Save multiple objects to a new location
/// Show the save-widget to determine a filename, the widget calls slotSave to save the objects
bool Core::saveObjectsTo( IdList _ids, QString _filename ) {

  bool result = false;

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

    //init widget
    LoadWidget* widget = new LoadWidget(supportedTypes_);
    widget->setWindowIcon( OpenFlipper::Options::OpenFlipperIcon() );

    connect(widget,SIGNAL(save(IdList, QString, int)),this,SLOT(saveObjects(IdList, QString, int)));
  
    if (supportedTypes_.size() != 0)
      result = widget->showSave(_ids,_filename);
    else
      emit log(LOGERR,tr("Could not show 'save objects' dialog. Missing file-plugins."));

    widget->disconnect();
    delete widget;
  }

  return result;
}

//-----------------------------------------------------------------------------------------------------

251
252
/// Save changes for all target objects (slot is called from CoreWidget's File-Menu)
void Core::saveAllObjects(){
Jan Möbius's avatar
 
Jan Möbius committed
253

254
  if ( OpenFlipper::Options::gui() ){
Jan Möbius's avatar
 
Jan Möbius committed
255

256
257
258
259
    //ensure that all options are on their default values
    for (int i=0; i < (int)supportedTypes_.size(); i++)
      supportedTypes_[i].plugin->saveOptionsWidget("");
  
Jan Möbius's avatar
 
Jan Möbius committed
260
    //iterate over all target objects
Dirk Wilden's avatar
Dirk Wilden committed
261
    for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::TARGET_OBJECTS) ;
Jan Möbius's avatar
   
Jan Möbius committed
262
          o_it != PluginFunctions::objectsEnd(); ++o_it)  {
263
  
Marlin Frickenschmidt's avatar
Marlin Frickenschmidt committed
264
            if ( !QDir(o_it->path()).exists() || o_it->path().trimmed() == "" || o_it->path().trimmed() == "." ) // if path isn't valid use 'save object to'
265
266
              saveObjectTo(o_it->id(),o_it->name());
            else{
Dirk Wilden's avatar
Dirk Wilden committed
267
              //save (existing files will be overwritten)
268
269
270
              QString filename = o_it->path() + OpenFlipper::Options::dirSeparator() + o_it->name() ;
              saveObject(o_it->id(),filename);
            }
Jan Möbius's avatar
 
Jan Möbius committed
271
272
    }
  }
273
}
Jan Möbius's avatar
 
Jan Möbius committed
274

275
276
277
278
//-----------------------------------------------------------------------------------------------------

/// Save all target objects under new name (slot is called from CoreWidget's File-Menu)
void Core::saveAllObjectsTo(){
279

280
  if ( OpenFlipper::Options::gui() ){
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

    //ensure that all options are on their default values
    for (int i=0; i < (int)supportedTypes_.size(); i++)
      supportedTypes_[i].plugin->saveOptionsWidget("");
  
    //get all dataTypes that want to be saved
    DataType types = DATA_UNKNOWN;
    IdList ids;
    
    for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::TARGET_OBJECTS) ;
          o_it != PluginFunctions::objectsEnd(); ++o_it){
      types |= o_it->dataType();
      ids.push_back( o_it->id() );
    }
    
    //check if a plugin can save all types to one file
    bool multiSave = false;
    
    for (int i=0; i < (int)supportedTypes_.size(); i++)
      if ( (supportedTypes_[i].saveMultipleObjects) && (supportedTypes_[i].type.contains(types)) )
        multiSave = true;

      
    if (ids.size() > 1 && multiSave){
      //save all objets to one file and use name of first object
      PluginFunctions::ObjectIterator o_it(PluginFunctions::TARGET_OBJECTS);
      
      saveObjectsTo(ids,o_it->name());
      
    } else {
      //save each object separately
      
313
      //iterate over all target objects
314
315
316
317
      for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::TARGET_OBJECTS) ;
          o_it != PluginFunctions::objectsEnd(); ++o_it)
            saveObjectTo(o_it->id(),o_it->name());
    }
Jan Möbius's avatar
 
Jan Möbius committed
318
319
  }
}
320
321


322