saveFunctions.cc 5.76 KB
Newer Older
Jan Möbius's avatar
 
Jan Möbius committed
1 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 33 34 35 36 37 38 39 40 41
//=============================================================================
//
//                               OpenFlipper
//        Copyright (C) 2008 by Computer Graphics Group, RWTH Aachen
//                           www.openflipper.org
//
//-----------------------------------------------------------------------------
//
//                                License
//
//  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.
//
//  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 Lesser General Public License
//  along with OpenFlipper.  If not, see <http://www.gnu.org/licenses/>.
//
//-----------------------------------------------------------------------------
//
//   $Revision$
//   $Author$
//   $Date$
//
//=============================================================================


#include "Core.hh"

#include "OpenFlipper/common/GlobalOptions.hh"

#include "OpenFlipper/BasePlugin/PluginFunctions.hh"

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

//========================================================================================
42
// ===  Public Slots (called by CoreWidget's File-Menu / Scripting / Plugins)    =========
Jan Möbius's avatar
 
Jan Möbius committed
43 44 45
//========================================================================================

/// Save an object
46 47
/// 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
48 49
bool Core::saveObject( int _id, QString _filename ) {
  BaseObjectData* object;
Jan Möbius's avatar
 
Jan Möbius committed
50
  PluginFunctions::getObject(_id,object);
Jan Möbius's avatar
 
Jan Möbius committed
51 52 53 54 55 56

  for (int i=0; i < (int)supportedTypes_.size(); i++)
    if (object->dataType() == supportedTypes_[i].type) {

      if ( OpenFlipper::Options::gui() ) {
        coreWidget_->statusMessage( "Saving " + _filename + " ...");
57
        if ( !OpenFlipper::Options::savingSettings() )
Jan Möbius's avatar
 
Jan Möbius committed
58 59 60
          coreWidget_->setStatus(ApplicationStatus::PROCESSING );
      }

61
      //save object
Jan Möbius's avatar
 
Jan Möbius committed
62
      bool ok = supportedTypes_[i].plugin->saveObject(_id,_filename);
63
    
Jan Möbius's avatar
 
Jan Möbius committed
64 65 66 67 68
      if ( OpenFlipper::Options::gui() ) {
        if (ok)
          coreWidget_->statusMessage( "Saving " + _filename + " ... done", 4000 );
        else
          coreWidget_->statusMessage( "Saving " + _filename + " ... failed!", 4000 );
69 70
    
        if ( !OpenFlipper::Options::savingSettings() )
Jan Möbius's avatar
 
Jan Möbius committed
71 72 73
          coreWidget_->setStatus(ApplicationStatus::READY );
      }

74 75 76 77 78
      //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
79 80 81
      return ok;
    }

82 83 84 85 86
  // no plugin found
  if ( OpenFlipper::Options::gui() )
      coreWidget_->statusMessage( "Saving " + _filename + " ... failed!", 4000 );

  return false;
Jan Möbius's avatar
 
Jan Möbius committed
87 88
}

89 90
//-----------------------------------------------------------------------------------------------------

Jan Möbius's avatar
 
Jan Möbius committed
91
/// Save an object to a new location
92
/// 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
93 94
bool Core::saveObjectTo( int _id, QString _filename ) {

95 96
  bool result = false;

97 98 99
  if ( OpenFlipper::Options::gui() ){

    BaseObjectData* object;
Jan Möbius's avatar
 
Jan Möbius committed
100
    PluginFunctions::getObject(_id,object);
101 102
  
    //init widget
103 104 105 106
    LoadWidget* widget = new LoadWidget(supportedTypes_);

    connect(widget,SIGNAL(load(QString, DataType, int&)),this,SLOT(slotLoad(QString, DataType, int&)));
    connect(widget,SIGNAL(save(int, QString)),this,SLOT(saveObject(int, QString)));
107 108
  
    if (supportedTypes_.size() != 0)
109 110
      result = widget->showSave(_id,_filename);
    else
111
      emit log(LOGERR,"Could not show 'save objects' dialog. Missing file-plugins.");
Jan Möbius's avatar
 
Jan Möbius committed
112

113
    widget->disconnect();
114
    delete widget;
115
  }
116

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

120
//-----------------------------------------------------------------------------------------------------
Jan Möbius's avatar
 
Jan Möbius committed
121

122 123
/// 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
124

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

127 128 129 130
    //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
131
    //iterate over all target objects
Dirk Wilden's avatar
Dirk Wilden committed
132
    for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::TARGET_OBJECTS) ;
Jan Möbius's avatar
 
Jan Möbius committed
133
          o_it != PluginFunctions::objectsEnd(); ++o_it)  {
134 135 136 137
  
            if ( !QDir(o_it->path()).exists() || o_it->path().trimmed() == "" ) // if path isn't valid use 'save object to'
              saveObjectTo(o_it->id(),o_it->name());
            else{
Dirk Wilden's avatar
Dirk Wilden committed
138
              //save (existing files will be overwritten)
139 140 141
              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
142 143
    }
  }
144
}
Jan Möbius's avatar
 
Jan Möbius committed
145

146 147 148 149 150 151 152
//-----------------------------------------------------------------------------------------------------

/// Save all target objects under new name (slot is called from CoreWidget's File-Menu)
void Core::saveAllObjectsTo(){
  if ( OpenFlipper::Options::gui() ){
    if (supportedTypes_.size() != 0){
      //iterate over all target objects
Dirk Wilden's avatar
Dirk Wilden committed
153
      for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::TARGET_OBJECTS);
Jan Möbius's avatar
 
Jan Möbius committed
154
            o_it != PluginFunctions::objectsEnd(); ++o_it)  {
155 156
        QString filename = o_it->path() + OpenFlipper::Options::dirSeparator() + o_it->name();
        saveObjectTo(o_it->id(),filename);
Jan Möbius's avatar
 
Jan Möbius committed
157
      }
158 159
    }else
      emit log(LOGERR,"Could not show 'save objects' dialog. Missing file-plugins.");
Jan Möbius's avatar
 
Jan Möbius committed
160 161
  }
}
162 163


164