saveFunctions.cc 13 KB
Newer Older
1
/*===========================================================================*\
Jan Möbius's avatar
Jan Möbius committed
2 3
*                                                                            *
*                              OpenFlipper                                   *
Martin Schultz's avatar
Martin Schultz committed
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
 *           Copyright (c) 2001-2015, RWTH-Aachen University                 *
 *           Department of Computer Graphics and Multimedia                  *
 *                          All rights reserved.                             *
 *                            www.openflipper.org                            *
 *                                                                           *
 *---------------------------------------------------------------------------*
 * This file is part of OpenFlipper.                                         *
 *---------------------------------------------------------------------------*
 *                                                                           *
 * Redistribution and use in source and binary forms, with or without        *
 * modification, are permitted provided that the following conditions        *
 * are met:                                                                  *
 *                                                                           *
 * 1. Redistributions of source code must retain the above copyright notice, *
 *    this list of conditions and the following disclaimer.                  *
 *                                                                           *
 * 2. Redistributions in binary form must reproduce the above copyright      *
 *    notice, this list of conditions and the following disclaimer in the    *
 *    documentation and/or other materials provided with the distribution.   *
 *                                                                           *
 * 3. Neither the name of the copyright holder nor the names of its          *
 *    contributors may be used to endorse or promote products derived from   *
 *    this software without specific prior written permission.               *
 *                                                                           *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       *
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A           *
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,  *
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,       *
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR        *
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    *
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING      *
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS        *
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.              *
Jan Möbius's avatar
Jan Möbius committed
39
*                                                                            *
40 41
\*===========================================================================*/

42

Jan Möbius's avatar
 
Jan Möbius committed
43 44 45 46 47 48


#include "Core.hh"

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

Jan Möbius's avatar
Jan Möbius committed
49 50
#include <ctime>

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

55 56
/** \brief Save an object
 *
Jan Möbius's avatar
Jan Möbius committed
57 58 59
 * Get object with given id, find a plugin to save dataType, save
 *
 * \note Existing files will be overwritten
60 61 62
 *
 * @param _id       Id of the object that should be saved
 * @param _filename Filename to save to
Jan Möbius's avatar
Jan Möbius committed
63
 * @return Successful
64
 */
Jan Möbius's avatar
 
Jan Möbius committed
65 66
bool Core::saveObject( int _id, QString _filename ) {
  BaseObjectData* object;
67 68 69 70
  if ( !PluginFunctions::getObject(_id,object) ) {
    emit log(LOGERR, tr("saveObject : cannot get object %1").arg(_id) );
    return false;
  }
Jan Möbius's avatar
 
Jan Möbius committed
71

72
  QString file_extension = QFileInfo(_filename).suffix();
73
  
74 75 76
  for (int i=0; i < (int)supportedTypes().size(); i++) {
    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
77 78

      if ( OpenFlipper::Options::gui() ) {
79
        coreWidget_->statusMessage( tr("Saving ") + _filename + " ...");
80
        if ( !OpenFlipper::Options::savingSettings() )
Jan Möbius's avatar
 
Jan Möbius committed
81 82 83
          coreWidget_->setStatus(ApplicationStatus::PROCESSING );
      }

84
      //save object
85
      
86
      bool ok = supportedTypes()[i].plugin->saveObject(_id,_filename);
87
    
Jan Möbius's avatar
 
Jan Möbius committed
88 89
      if ( OpenFlipper::Options::gui() ) {
        if (ok)
90
          coreWidget_->statusMessage( tr("Saving ") + _filename + tr(" ... done"), 4000 );
Dirk Wilden's avatar
Dirk Wilden committed
91 92
        else{
          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 );
Dirk Wilden's avatar
Dirk Wilden committed
95
        }
96 97
    
        if ( !OpenFlipper::Options::savingSettings() )
Jan Möbius's avatar
 
Jan Möbius committed
98 99 100
          coreWidget_->setStatus(ApplicationStatus::READY );
      }

101 102 103 104 105
      //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
106 107
      return ok;
    }
108 109
  }
  
110
  // no plugin found
Dirk Wilden's avatar
Dirk Wilden committed
111 112
  if ( OpenFlipper::Options::gui() ){
      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 );
Dirk Wilden's avatar
Dirk Wilden committed
114
  }
115 116

  return false;
Jan Möbius's avatar
 
Jan Möbius committed
117 118
}

119 120
//-----------------------------------------------------------------------------------------------------

Dirk Wilden's avatar
Dirk Wilden committed
121 122 123 124 125
/// 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;
126 127 128
  if ( !PluginFunctions::getObject(_id,object) ) {
    emit log(LOGERR, tr("saveObject : cannot get object %1").arg(_id) );
  }
Dirk Wilden's avatar
Dirk Wilden committed
129 130 131 132 133 134 135 136


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

Martin Schultz's avatar
Martin Schultz committed
137
  time_t start = clock();
Dirk Wilden's avatar
Dirk Wilden committed
138
  //save object
139
  bool ok = supportedTypes()[_pluginID].plugin->saveObject(_id,_filename);
Martin Schultz's avatar
Martin Schultz committed
140 141
  time_t end = clock();
  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) );
Dirk Wilden's avatar
Dirk Wilden committed
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161

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

}

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

162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
/// 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
184
  if ( !supportedTypes()[_pluginID].saveMultipleObjects){
185 186 187 188
    emit log(LOGERR, tr("Unable to save objects. Plugin does not allow multiple objects."));
    return;
  }
  
189
  bool ok = supportedTypes()[_pluginID].plugin->saveObjects(_ids,_filename);
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208

  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
209
/// Save an object to a new location
210
/// 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
211 212
bool Core::saveObjectTo( int _id, QString _filename ) {

213 214
  bool result = false;

215
  if ( OpenFlipper::Options::gui() ){
216
 
217
    //init widget
218
    LoadWidget* widget = new LoadWidget(supportedTypes());
219
    widget->setWindowIcon( OpenFlipper::Options::OpenFlipperIcon() );
220

Dirk Wilden's avatar
Dirk Wilden committed
221
    connect(widget,SIGNAL(save(int, QString, int)),this,SLOT(saveObject(int, QString, int)));
222
  
223
    if (supportedTypes().size() != 0)
224 225
      result = widget->showSave(_id,_filename);
    else
226
      emit log(LOGERR,tr("Could not show 'save objects' dialog. Missing file-plugins."));
Jan Möbius's avatar
 
Jan Möbius committed
227

228
    widget->disconnect();
229
    delete widget;
230
  }
231

232
  return result;
Jan Möbius's avatar
 
Jan Möbius committed
233 234
}

235
//-----------------------------------------------------------------------------------------------------
Jan Möbius's avatar
 
Jan Möbius committed
236

237 238 239 240 241 242 243 244 245
/// 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
246
    LoadWidget* widget = new LoadWidget(supportedTypes());
247 248 249 250
    widget->setWindowIcon( OpenFlipper::Options::OpenFlipperIcon() );

    connect(widget,SIGNAL(save(IdList, QString, int)),this,SLOT(saveObjects(IdList, QString, int)));
  
251
    if (supportedTypes().size() != 0)
252 253 254 255 256 257 258 259 260 261 262 263 264
      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;
}

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

265 266
/// 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
267

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

270
    //ensure that all options are on their default values
271 272
    for (int i=0; i < (int)supportedTypes().size(); i++)
      supportedTypes()[i].plugin->saveOptionsWidget("");
273
  
Jan Möbius's avatar
 
Jan Möbius committed
274
    //iterate over all target objects
Dirk Wilden's avatar
Dirk Wilden committed
275
    for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::TARGET_OBJECTS) ;
Jan Möbius's avatar
 
Jan Möbius committed
276
          o_it != PluginFunctions::objectsEnd(); ++o_it)  {
277
  
Marlin Frickenschmidt's avatar
Marlin Frickenschmidt committed
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 280
              saveObjectTo(o_it->id(),o_it->name());
            else{
Dirk Wilden's avatar
Dirk Wilden committed
281
              //save (existing files will be overwritten)
282 283 284
              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
285 286
    }
  }
287
}
Jan Möbius's avatar
 
Jan Möbius committed
288

289 290 291 292
//-----------------------------------------------------------------------------------------------------

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

294
  if ( OpenFlipper::Options::gui() ){
295 296

    //ensure that all options are on their default values
297 298
    for (int i=0; i < (int)supportedTypes().size(); i++)
      supportedTypes()[i].plugin->saveOptionsWidget("");
299 300 301 302 303 304 305 306 307 308 309 310 311 312
  
    //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;
    
313 314
    for (int i=0; i < (int)supportedTypes().size(); i++)
      if ( (supportedTypes()[i].saveMultipleObjects) && (supportedTypes()[i].type.contains(types)) )
315 316 317 318 319 320 321 322 323 324 325 326
        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
      
327
      //iterate over all target objects
328 329 330 331
      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
332 333
  }
}
334 335


336