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 * $Revision$ *
45 * $LastChangedBy$ *
46 * $Date$ *
47 * *
48 \*===========================================================================*/
49 
50 
51 #include "Core.hh"
52 
53 #include "OpenFlipper/widgets/loadWidget/loadWidget.hh"
54 
55 #include <ctime>
56 
57 //========================================================================================
58 // === Public Slots (called by CoreWidget's File-Menu / Scripting / Plugins) =========
59 //========================================================================================
60 
71 bool Core::saveObject( int _id, QString _filename ) {
72  BaseObjectData* object;
73  if ( !PluginFunctions::getObject(_id,object) ) {
74  emit log(LOGERR, tr("saveObject : cannot get object %1").arg(_id) );
75  return false;
76  }
77 
78  QString file_extension = QFileInfo(_filename).suffix();
79 
80  for (int i=0; i < (int)supportedTypes().size(); i++) {
81  if ( supportedTypes()[i].type.contains(object->dataType()) &&
82  ( supportedTypes()[i].saveFilters.contains(file_extension) || file_extension.isEmpty() ) ) {
83 
84  if ( OpenFlipper::Options::gui() ) {
85  coreWidget_->statusMessage( tr("Saving ") + _filename + " ...");
86  if ( !OpenFlipper::Options::savingSettings() )
88  }
89 
90  //save object
91 
92  bool ok = supportedTypes()[i].plugin->saveObject(_id,_filename);
93 
94  if ( OpenFlipper::Options::gui() ) {
95  if (ok)
96  coreWidget_->statusMessage( tr("Saving ") + _filename + tr(" ... done"), 4000 );
97  else{
98  emit log(LOGERR, tr("Unable to save '%1'. Plugin failed. DataType %2").arg(_filename, dataTypeName(object->dataType()) ) );
99  emit log(LOGERR, tr("Plugin was: '%1'. File Extension was: %2").arg(supportedTypes()[i].name, file_extension ) );
100  coreWidget_->statusMessage( tr("Saving ") + _filename + tr(" ... failed!"), 4000 );
101  }
102 
103  if ( !OpenFlipper::Options::savingSettings() )
105  }
106 
107  //add to recent files
108  if (ok && !OpenFlipper::Options::savingSettings()
109  && OpenFlipper::Options::gui() )
110  coreWidget_->addRecent( _filename, object->dataType() );
111 
112  return ok;
113  }
114  }
115 
116  // no plugin found
117  if ( OpenFlipper::Options::gui() ){
118  emit log(LOGERR, tr("Unable to save '%1'. No plugin found. DataType %2").arg(_filename, dataTypeName(object->dataType()) ) );
119  coreWidget_->statusMessage( tr("Saving ") + _filename + tr(" ... failed!"), 4000 );
120  }
121 
122  return false;
123 }
124 
125 //-----------------------------------------------------------------------------------------------------
126 
130 void Core::saveObject( int _id, QString _filename, int _pluginID ) {
131  BaseObjectData* object;
132  if ( !PluginFunctions::getObject(_id,object) ) {
133  emit log(LOGERR, tr("saveObject : cannot get object %1").arg(_id) );
134  }
135 
136 
137  if ( OpenFlipper::Options::gui() ) {
138  coreWidget_->statusMessage( tr("Saving ") + _filename + " ...");
139  if ( !OpenFlipper::Options::savingSettings() )
141  }
142 
143  time_t start = clock();
144  //save object
145  bool ok = supportedTypes()[_pluginID].plugin->saveObject(_id,_filename);
146  time_t end = clock();
147  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) );
148 
149  if ( OpenFlipper::Options::gui() ) {
150  if (ok)
151  coreWidget_->statusMessage( tr("Saving ") + _filename + tr(" ... done"), 4000 );
152  else
153  coreWidget_->statusMessage( tr("Saving ") + _filename + tr(" ... failed!"), 4000 );
154 
155  if ( !OpenFlipper::Options::savingSettings() )
157  }
158 
159  //add to recent files
160  if (ok && !OpenFlipper::Options::savingSettings()
161  && OpenFlipper::Options::gui() )
162  coreWidget_->addRecent( _filename, object->dataType() );
163 
164 }
165 
166 //-----------------------------------------------------------------------------------------------------
167 
171 void Core::saveObjects( IdList _ids, QString _filename, int _pluginID ) {
172 
173  DataType type = 0;
174 
175  for (uint i=0; i < _ids.size(); i++){
176 
177  BaseObjectData* object;
178  PluginFunctions::getObject(_ids[i],object);
179 
180  type |= object->dataType();
181  }
182 
183  if ( OpenFlipper::Options::gui() ) {
184  coreWidget_->statusMessage( tr("Saving ") + _filename + " ...");
185  if ( !OpenFlipper::Options::savingSettings() )
187  }
188 
189  //save objects
190  if ( !supportedTypes()[_pluginID].saveMultipleObjects){
191  emit log(LOGERR, tr("Unable to save objects. Plugin does not allow multiple objects."));
192  return;
193  }
194 
195  bool ok = supportedTypes()[_pluginID].plugin->saveObjects(_ids,_filename);
196 
197  if ( OpenFlipper::Options::gui() ) {
198  if (ok)
199  coreWidget_->statusMessage( tr("Saving ") + _filename + tr(" ... done"), 4000 );
200  else
201  coreWidget_->statusMessage( tr("Saving ") + _filename + tr(" ... failed!"), 4000 );
202 
203  if ( !OpenFlipper::Options::savingSettings() )
205  }
206 
207  //add to recent files
208  if (ok && !OpenFlipper::Options::savingSettings()
209  && OpenFlipper::Options::gui() )
210  coreWidget_->addRecent( _filename, type );
211 }
212 
213 //-----------------------------------------------------------------------------------------------------
214 
217 bool Core::saveObjectTo( int _id, QString _filename ) {
218 
219  bool result = false;
220 
221  if ( OpenFlipper::Options::gui() ){
222 
223  //init widget
224  LoadWidget* widget = new LoadWidget(supportedTypes());
225  widget->setWindowIcon( OpenFlipper::Options::OpenFlipperIcon() );
226 
227  connect(widget,SIGNAL(save(int, QString, int)),this,SLOT(saveObject(int, QString, int)));
228 
229  if (supportedTypes().size() != 0)
230  result = widget->showSave(_id,_filename);
231  else
232  emit log(LOGERR,tr("Could not show 'save objects' dialog. Missing file-plugins."));
233 
234  widget->disconnect();
235  delete widget;
236  }
237 
238  return result;
239 }
240 
241 //-----------------------------------------------------------------------------------------------------
242 
245 bool Core::saveObjectsTo( IdList _ids, QString _filename ) {
246 
247  bool result = false;
248 
249  if ( OpenFlipper::Options::gui() ){
250 
251  //init widget
252  LoadWidget* widget = new LoadWidget(supportedTypes());
253  widget->setWindowIcon( OpenFlipper::Options::OpenFlipperIcon() );
254 
255  connect(widget,SIGNAL(save(IdList, QString, int)),this,SLOT(saveObjects(IdList, QString, int)));
256 
257  if (supportedTypes().size() != 0)
258  result = widget->showSave(_ids,_filename);
259  else
260  emit log(LOGERR,tr("Could not show 'save objects' dialog. Missing file-plugins."));
261 
262  widget->disconnect();
263  delete widget;
264  }
265 
266  return result;
267 }
268 
269 //-----------------------------------------------------------------------------------------------------
270 
273 
274  if ( OpenFlipper::Options::gui() ){
275 
276  //ensure that all options are on their default values
277  for (int i=0; i < (int)supportedTypes().size(); i++)
278  supportedTypes()[i].plugin->saveOptionsWidget("");
279 
280  //iterate over all target objects
282  o_it != PluginFunctions::objectsEnd(); ++o_it) {
283 
284  if ( !QDir(o_it->path()).exists() || o_it->path().trimmed() == "" || o_it->path().trimmed() == "." ) // if path isn't valid use 'save object to'
285  saveObjectTo(o_it->id(),o_it->name());
286  else{
287  //save (existing files will be overwritten)
288  QString filename = o_it->path() + OpenFlipper::Options::dirSeparator() + o_it->name() ;
289  saveObject(o_it->id(),filename);
290  }
291  }
292  }
293 }
294 
295 //-----------------------------------------------------------------------------------------------------
296 
299 
300  if ( OpenFlipper::Options::gui() ){
301 
302  //ensure that all options are on their default values
303  for (int i=0; i < (int)supportedTypes().size(); i++)
304  supportedTypes()[i].plugin->saveOptionsWidget("");
305 
306  //get all dataTypes that want to be saved
307  DataType types = DATA_UNKNOWN;
308  IdList ids;
309 
311  o_it != PluginFunctions::objectsEnd(); ++o_it){
312  types |= o_it->dataType();
313  ids.push_back( o_it->id() );
314  }
315 
316  //check if a plugin can save all types to one file
317  bool multiSave = false;
318 
319  for (int i=0; i < (int)supportedTypes().size(); i++)
320  if ( (supportedTypes()[i].saveMultipleObjects) && (supportedTypes()[i].type.contains(types)) )
321  multiSave = true;
322 
323 
324  if (ids.size() > 1 && multiSave){
325  //save all objets to one file and use name of first object
327 
328  saveObjectsTo(ids,o_it->name());
329 
330  } else {
331  //save each object separately
332 
333  //iterate over all target objects
335  o_it != PluginFunctions::objectsEnd(); ++o_it)
336  saveObjectTo(o_it->id(),o_it->name());
337  }
338  }
339 }
340 
341 
342 
Predefined datatypes.
Definition: DataTypes.hh:96
void addRecent(QString _filename, DataType _type)
Add a recent file and update menu.
Definition: CoreWidget.cc:893
CoreWidget * coreWidget_
The main applications widget ( only created in gui mode )
Definition: Core.hh:1553
void saveObjects(IdList _ids, QString _filename, int _pluginID)
int showSave(int _id, QString _filename)
show Widget for saving Files
Definition: loadWidget.cc:388
bool getObject(int _identifier, BSplineCurveObject *&_object)
bool dataType(DataType _type) const
Definition: BaseObject.cc:232
bool saveObject(int _id, QString _filename)
Save an object.
const QStringList TARGET_OBJECTS("target")
Iterable object range.
Status is ready (green light)
bool saveObjectTo(int _id, QString _filename)
const DataType DATA_UNKNOWN(0)
None of the other Objects.
DLLEXPORT QString dataTypeName(DataType _id)
Get DataType Human readable name ( this name might change. Use the typeName instead! ) ...
Definition: Types.cc:263
void saveAllObjectsTo()
Slot for saving objects to a new location.
bool saveObjectsTo(IdList _ids, QString _filename)
Status is processing but system will allow interaction (yellow light)
std::vector< int > IdList
Standard Type for id Lists used for scripting.
Definition: DataTypes.hh:192
QString name() const
return the name of the object. The name defaults to NONAME if unset.
Definition: BaseObject.cc:741
void log(Logtype _type, QString _message)
Logg with OUT,WARN or ERR as type.
void saveAllObjects()
Slot for saving objects from Menu.
DLLEXPORT ObjectIterator objectsEnd()
Return Iterator to Object End.