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() )
81 coreWidget_->setStatus(ApplicationStatus::PROCESSING );
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() )
98 coreWidget_->setStatus(ApplicationStatus::READY );
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() )
134 coreWidget_->setStatus(ApplicationStatus::PROCESSING );
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() )
150 coreWidget_->setStatus(ApplicationStatus::READY );
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() )
180 coreWidget_->setStatus(ApplicationStatus::PROCESSING );
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() )
198 coreWidget_->setStatus(ApplicationStatus::READY );
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
266 void Core::saveAllObjects(){
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
275 for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::TARGET_OBJECTS) ;
276 o_it != PluginFunctions::objectsEnd(); ++o_it) {
277
278 if ( !QDir(o_it->path()).exists() || o_it->path().trimmed() == "" || o_it->path().trimmed() == "." ) // if path isn't valid use 'save object to'
279 saveObjectTo(o_it->id(),o_it->name());
280 else{
281 //save (existing files will be overwritten)
282 QString filename = o_it->path() + OpenFlipper::Options::dirSeparator() + o_it->name() ;
283 saveObject(o_it->id(),filename);
284 }
285 }
286 }
287 }
288
289 //-----------------------------------------------------------------------------------------------------
290
292 void Core::saveAllObjectsTo(){
293
294 if ( OpenFlipper::Options::gui() ){
295
296 //ensure that all options are on their default values
297 for (int i=0; i < (int)supportedTypes().size(); i++)
298 supportedTypes()[i].plugin->saveOptionsWidget("");
299
300 //get all dataTypes that want to be saved
301 DataType types = DATA_UNKNOWN;
302 IdList ids;
303
304 for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::TARGET_OBJECTS) ;
305 o_it != PluginFunctions::objectsEnd(); ++o_it){
306 types |= o_it->dataType();
307 ids.push_back( o_it->id() );
308 }
309
310 //check if a plugin can save all types to one file
311 bool multiSave = false;
312
313 for (int i=0; i < (int)supportedTypes().size(); i++)
314 if ( (supportedTypes()[i].saveMultipleObjects) && (supportedTypes()[i].type.contains(types)) )
315 multiSave = true;
316
317
318 if (ids.size() > 1 && multiSave){
319 //save all objets to one file and use name of first object
320 PluginFunctions::ObjectIterator o_it(PluginFunctions::TARGET_OBJECTS);
321
322 saveObjectsTo(ids,o_it->name());
323
324 } else {
325 //save each object separately
326
327 //iterate over all target objects
328 for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::TARGET_OBJECTS) ;
329 o_it != PluginFunctions::objectsEnd(); ++o_it)
330 saveObjectTo(o_it->id(),o_it->name());
331 }
332 }
333 }
334
335
336
Core::saveAllObjectsTo
void saveAllObjectsTo()
Slot for saving objects to a new location.
Definition: saveFunctions.cc:292
LOGERR
Definition: LoggingInterface.hh:96
ApplicationStatus::READY
Status is ready (green light)
Definition: StatusbarInterface.hh:65
dataTypeName
DLLEXPORT QString dataTypeName(DataType _id)
Get DataType Human readable name ( this name might change. Use the typeName instead! ) ...
Definition: Types.cc:252
Core::saveObjectsTo
bool saveObjectsTo(IdList _ids, QString _filename)
Definition: saveFunctions.cc:239
PluginFunctions::getObject
bool getObject(const int _identifier, BaseObject *&_object)
Get the object which has the given identifier.
Definition: PluginFunctions.cc:296
DataType
Predefined datatypes.
Definition: DataTypes.hh:83
CoreWidget::addRecent
void addRecent(QString _filename, DataType _type)
Add a recent file and update menu.
Definition: CoreWidget.cc:860
Core::log
void log(Logtype _type, QString _message)
Logg with OUT,WARN or ERR as type.
BaseObject::dataType
bool dataType(DataType _type) const
Definition: BaseObject.cc:221
IdList
std::vector< int > IdList
Standard Type for id Lists used for scripting.
Definition: DataTypes.hh:179
PluginFunctions::objectsEnd
DLLEXPORT ObjectIterator objectsEnd()
Return Iterator to Object End.
Definition: PluginFunctionsIterator.cc:177
Core::coreWidget_
CoreWidget * coreWidget_
The main applications widget ( only created in gui mode )
Definition: Core.hh:1596
Core::saveObjectTo
bool saveObjectTo(int _id, QString _filename)
Definition: saveFunctions.cc:211
Core::saveObjects
void saveObjects(IdList _ids, QString _filename, int _pluginID)
Definition: saveFunctions.cc:165
LoadWidget
Definition: loadWidget.hh:56
PluginFunctions::ObjectIterator
Core Data Iterator.
Definition: PluginFunctions.hh:441
LoadWidget::showSave
int showSave(int _id, QString _filename)
show Widget for saving Files
Definition: loadWidget.cc:382
BaseObject::name
QString name() const
return the name of the object. The name defaults to NONAME if unset.
Definition: BaseObject.cc:730
BaseObjectData
Definition: BaseObjectData.hh:102
Core.hh
PluginFunctions::TARGET_OBJECTS
const QStringList TARGET_OBJECTS("target")
Iterable object range.
Core::saveAllObjects
void saveAllObjects()
Slot for saving objects from Menu.
Definition: saveFunctions.cc:266
ApplicationStatus::PROCESSING
Status is processing but system will allow interaction (yellow light)
Definition: StatusbarInterface.hh:66
Core::saveObject
bool saveObject(int _id, QString _filename)
Save an object.
Definition: saveFunctions.cc:65
DATA_UNKNOWN
const DataType DATA_UNKNOWN(0)
None of the other Objects.
LOGINFO
Definition: LoggingInterface.hh:94