Developer Documentation
saveFunctions.cc
1 /*===========================================================================*\
2 * *
3 * OpenFlipper *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39 * *
40 \*===========================================================================*/
41 
42 
43 
44 
45 #include "Core.hh"
46 
47 #include "OpenFlipper/widgets/loadWidget/loadWidget.hh"
48 
49 #include <ctime>
50 
51 //========================================================================================
52 // === Public Slots (called by CoreWidget's File-Menu / Scripting / Plugins) =========
53 //========================================================================================
54 
65 bool Core::saveObject( int _id, QString _filename ) {
66  BaseObjectData* object;
67  if ( !PluginFunctions::getObject(_id,object) ) {
68  emit log(LOGERR, tr("saveObject : cannot get object %1").arg(_id) );
69  return false;
70  }
71 
72  QString file_extension = QFileInfo(_filename).suffix();
73 
74  for (int i=0; i < (int)supportedTypes().size(); i++) {
75  if ( supportedTypes()[i].type.contains(object->dataType()) &&
76  ( supportedTypes()[i].saveFilters.contains(file_extension) || file_extension.isEmpty() ) ) {
77 
78  if ( OpenFlipper::Options::gui() ) {
79  coreWidget_->statusMessage( tr("Saving ") + _filename + " ...");
80  if ( !OpenFlipper::Options::savingSettings() )
82  }
83 
84  //save object
85 
86  bool ok = supportedTypes()[i].plugin->saveObject(_id,_filename);
87 
88  if ( OpenFlipper::Options::gui() ) {
89  if (ok)
90  coreWidget_->statusMessage( tr("Saving ") + _filename + tr(" ... done"), 4000 );
91  else{
92  emit log(LOGERR, tr("Unable to save '%1'. Plugin failed. DataType %2").arg(_filename, dataTypeName(object->dataType()) ) );
93  emit log(LOGERR, tr("Plugin was: '%1'. File Extension was: %2").arg(supportedTypes()[i].name, file_extension ) );
94  coreWidget_->statusMessage( tr("Saving ") + _filename + tr(" ... failed!"), 4000 );
95  }
96 
97  if ( !OpenFlipper::Options::savingSettings() )
99  }
100 
101  //add to recent files
102  if (ok && !OpenFlipper::Options::savingSettings()
103  && OpenFlipper::Options::gui() )
104  coreWidget_->addRecent( _filename, object->dataType() );
105 
106  return ok;
107  }
108  }
109 
110  // no plugin found
111  if ( OpenFlipper::Options::gui() ){
112  emit log(LOGERR, tr("Unable to save '%1'. No plugin found. DataType %2").arg(_filename, dataTypeName(object->dataType()) ) );
113  coreWidget_->statusMessage( tr("Saving ") + _filename + tr(" ... failed!"), 4000 );
114  }
115 
116  return false;
117 }
118 
119 //-----------------------------------------------------------------------------------------------------
120 
124 void Core::saveObject( int _id, QString _filename, int _pluginID ) {
125  BaseObjectData* object;
126  if ( !PluginFunctions::getObject(_id,object) ) {
127  emit log(LOGERR, tr("saveObject : cannot get object %1").arg(_id) );
128  }
129 
130 
131  if ( OpenFlipper::Options::gui() ) {
132  coreWidget_->statusMessage( tr("Saving ") + _filename + " ...");
133  if ( !OpenFlipper::Options::savingSettings() )
135  }
136 
137  time_t start = clock();
138  //save object
139  bool ok = supportedTypes()[_pluginID].plugin->saveObject(_id,_filename);
140  time_t end = clock();
141  emit log(LOGINFO,tr("Saving %1 with Plugin %2 took %3 seconds.").arg(_filename).arg(supportedTypes()[_pluginID].name).arg((double)(end-start)/CLOCKS_PER_SEC) );
142 
143  if ( OpenFlipper::Options::gui() ) {
144  if (ok)
145  coreWidget_->statusMessage( tr("Saving ") + _filename + tr(" ... done"), 4000 );
146  else
147  coreWidget_->statusMessage( tr("Saving ") + _filename + tr(" ... failed!"), 4000 );
148 
149  if ( !OpenFlipper::Options::savingSettings() )
151  }
152 
153  //add to recent files
154  if (ok && !OpenFlipper::Options::savingSettings()
155  && OpenFlipper::Options::gui() )
156  coreWidget_->addRecent( _filename, object->dataType() );
157 
158 }
159 
160 //-----------------------------------------------------------------------------------------------------
161 
165 void Core::saveObjects( IdList _ids, QString _filename, int _pluginID ) {
166 
167  DataType type = 0;
168 
169  for (uint i=0; i < _ids.size(); i++){
170 
171  BaseObjectData* object;
172  PluginFunctions::getObject(_ids[i],object);
173 
174  type |= object->dataType();
175  }
176 
177  if ( OpenFlipper::Options::gui() ) {
178  coreWidget_->statusMessage( tr("Saving ") + _filename + " ...");
179  if ( !OpenFlipper::Options::savingSettings() )
181  }
182 
183  //save objects
184  if ( !supportedTypes()[_pluginID].saveMultipleObjects){
185  emit log(LOGERR, tr("Unable to save objects. Plugin does not allow multiple objects."));
186  return;
187  }
188 
189  bool ok = supportedTypes()[_pluginID].plugin->saveObjects(_ids,_filename);
190 
191  if ( OpenFlipper::Options::gui() ) {
192  if (ok)
193  coreWidget_->statusMessage( tr("Saving ") + _filename + tr(" ... done"), 4000 );
194  else
195  coreWidget_->statusMessage( tr("Saving ") + _filename + tr(" ... failed!"), 4000 );
196 
197  if ( !OpenFlipper::Options::savingSettings() )
199  }
200 
201  //add to recent files
202  if (ok && !OpenFlipper::Options::savingSettings()
203  && OpenFlipper::Options::gui() )
204  coreWidget_->addRecent( _filename, type );
205 }
206 
207 //-----------------------------------------------------------------------------------------------------
208 
211 bool Core::saveObjectTo( int _id, QString _filename ) {
212 
213  bool result = false;
214 
215  if ( OpenFlipper::Options::gui() ){
216 
217  //init widget
218  LoadWidget* widget = new LoadWidget(supportedTypes());
219  widget->setWindowIcon( OpenFlipper::Options::OpenFlipperIcon() );
220 
221  connect(widget,SIGNAL(save(int, QString, int)),this,SLOT(saveObject(int, QString, int)));
222 
223  if (supportedTypes().size() != 0)
224  result = widget->showSave(_id,_filename);
225  else
226  emit log(LOGERR,tr("Could not show 'save objects' dialog. Missing file-plugins."));
227 
228  widget->disconnect();
229  delete widget;
230  }
231 
232  return result;
233 }
234 
235 //-----------------------------------------------------------------------------------------------------
236 
239 bool Core::saveObjectsTo( IdList _ids, QString _filename ) {
240 
241  bool result = false;
242 
243  if ( OpenFlipper::Options::gui() ){
244 
245  //init widget
246  LoadWidget* widget = new LoadWidget(supportedTypes());
247  widget->setWindowIcon( OpenFlipper::Options::OpenFlipperIcon() );
248 
249  connect(widget,SIGNAL(save(IdList, QString, int)),this,SLOT(saveObjects(IdList, QString, int)));
250 
251  if (supportedTypes().size() != 0)
252  result = widget->showSave(_ids,_filename);
253  else
254  emit log(LOGERR,tr("Could not show 'save objects' dialog. Missing file-plugins."));
255 
256  widget->disconnect();
257  delete widget;
258  }
259 
260  return result;
261 }
262 
263 //-----------------------------------------------------------------------------------------------------
264 
267 
268  if ( OpenFlipper::Options::gui() ){
269 
270  //ensure that all options are on their default values
271  for (int i=0; i < (int)supportedTypes().size(); i++)
272  supportedTypes()[i].plugin->saveOptionsWidget("");
273 
274  //iterate over all target objects
276 
277  if ( !QDir(o_it->path()).exists() || o_it->path().trimmed() == "" || o_it->path().trimmed() == "." ) // if path isn't valid use 'save object to'
278  saveObjectTo(o_it->id(),o_it->name());
279  else{
280  //save (existing files will be overwritten)
281  QString filename = o_it->path() + OpenFlipper::Options::dirSeparator() + o_it->name() ;
282  saveObject(o_it->id(),filename);
283  }
284  }
285  }
286 }
287 
288 //-----------------------------------------------------------------------------------------------------
289 
292 
293  if ( OpenFlipper::Options::gui() ){
294 
295  //ensure that all options are on their default values
296  for (int i=0; i < (int)supportedTypes().size(); i++)
297  supportedTypes()[i].plugin->saveOptionsWidget("");
298 
299  //get all dataTypes that want to be saved
300  DataType types = DATA_UNKNOWN;
301  IdList ids;
302 
304  types |= o_it->dataType();
305  ids.push_back( o_it->id() );
306  }
307 
308  //check if a plugin can save all types to one file
309  bool multiSave = false;
310 
311  for (int i=0; i < (int)supportedTypes().size(); i++)
312  if ( (supportedTypes()[i].saveMultipleObjects) && (supportedTypes()[i].type.contains(types)) )
313  multiSave = true;
314 
315 
316  if (ids.size() > 1 && multiSave){
317  //save all objets to one file and use name of first object
319 
320  saveObjectsTo(ids,o_it->name());
321 
322  } else {
323  //save each object separately
324 
325  //iterate over all target objects
327  saveObjectTo(o_it->id(),o_it->name());
328  }
329  }
330  }
331 }
332 
333 
334 
void saveAllObjectsTo()
Slot for saving objects to a new location.
Status is ready (green light)
DLLEXPORT QString dataTypeName(DataType _id)
Get DataType Human readable name ( this name might change. Use the typeName instead! ) ...
Definition: Types.cc:252
bool saveObjectsTo(IdList _ids, QString _filename)
bool getObject(const int _identifier, BaseObject *&_object)
Get the object which has the given identifier.
Predefined datatypes.
Definition: DataTypes.hh:83
void addRecent(QString _filename, DataType _type)
Add a recent file and update menu.
Definition: CoreWidget.cc:860
void log(Logtype _type, QString _message)
Logg with OUT,WARN or ERR as type.
bool dataType(DataType _type) const
Definition: BaseObject.cc:221
std::vector< int > IdList
Standard Type for id Lists used for scripting.
Definition: DataTypes.hh:181
CoreWidget * coreWidget_
The main applications widget ( only created in gui mode )
Definition: Core.hh:1596
bool saveObjectTo(int _id, QString _filename)
void saveObjects(IdList _ids, QString _filename, int _pluginID)
int showSave(int _id, QString _filename)
show Widget for saving Files
Definition: loadWidget.cc:386
QString name() const
return the name of the object. The name defaults to NONAME if unset.
Definition: BaseObject.cc:730
const QStringList TARGET_OBJECTS("target")
Iterable object range.
ObjectRange objects(IteratorRestriction _restriction, DataType _dataType)
Iterable object range.
void saveAllObjects()
Slot for saving objects from Menu.
Status is processing but system will allow interaction (yellow light)
bool saveObject(int _id, QString _filename)
Save an object.
const DataType DATA_UNKNOWN(0)
None of the other Objects.