Commit 19c7a8b8 authored by Dirk Wilden's avatar Dirk Wilden

fixed and cleaned up saveFunctions

git-svn-id: http://www.openflipper.org/svnrepo/OpenFlipper/branches/Free@3580 383ad7c9-94d9-4d36-a494-682f7c89f535
parent ef35d1d0
......@@ -172,12 +172,12 @@ Core::init() {
coreWidget_ = new CoreWidget(viewModes_ , plugins);
connect(coreWidget_, SIGNAL(clearAll()) , this, SLOT(clearAll()));
connect(coreWidget_, SIGNAL(loadMenu()) , this, SLOT(slotLoadMenu()));
connect(coreWidget_, SIGNAL(loadMenu()) , this, SLOT(loadObject()));
connect(coreWidget_, SIGNAL(addEmptyObjectMenu()) , this, SLOT(slotAddEmptyObjectMenu()));
connect(coreWidget_, SIGNAL(saveMenu()) , this, SLOT(slotSaveMenu()));
connect(coreWidget_, SIGNAL(saveToMenu()) , this, SLOT(slotSaveToMenu()));
connect(coreWidget_, SIGNAL(loadIniMenu()) , this, SLOT(slotLoadIniMenu()));
connect(coreWidget_, SIGNAL(saveIniMenu()) , this, SLOT(slotSaveIniMenu()));
connect(coreWidget_, SIGNAL(saveMenu()) , this, SLOT(saveAllObjects()));
connect(coreWidget_, SIGNAL(saveToMenu()) , this, SLOT(saveAllObjectsTo()));
connect(coreWidget_, SIGNAL(loadIniMenu()) , this, SLOT(loadSettings()));
connect(coreWidget_, SIGNAL(saveIniMenu()) , this, SLOT(saveSettings()));
connect(coreWidget_, SIGNAL(applyOptions()) , this, SLOT(applyOptions()));
connect(coreWidget_, SIGNAL(saveOptions()) , this, SLOT(saveOptions()));
connect(coreWidget_, SIGNAL(recentOpen(QAction*)) , this, SLOT(slotRecentOpen(QAction*)));
......@@ -577,7 +577,7 @@ void Core::updateView() {
}
if ( OpenFlipper::Options::gui() && !OpenFlipper::Options::openingIni() && !OpenFlipper::Options::redrawDisabled() ) {
if ( OpenFlipper::Options::gui() && !OpenFlipper::Options::loadingSettings() && !OpenFlipper::Options::redrawDisabled() ) {
coreWidget_->examiner_widget_->sceneGraph(root_node_scenegraph_);
coreWidget_->examiner_widget_->updateGL();
}
......@@ -977,6 +977,24 @@ void Core::setDescriptions(){
emit setSlotDescription("writeVersionNumbers(QString)", "write the current versions of all plugins to INI file",
QStringList("filename"),
QStringList("fullpath to a file where the versions should be written to."));
//save slots
emit setSlotDescription("saveObject(int,QString)", "Save object to file. If the file exists it will be overwritten.",
QString("object-id,filename").split(","),
QString("id of the object, complete path and filename").split(","));
emit setSlotDescription("saveObjectTo(int,QString)", "Save object to file. The location can be chosen in a dialog. "
"(only works if GUI is available)",
QString("object-id,filename").split(","),
QString("id of the object, initial filename for the dialog").split(","));
emit setSlotDescription("saveAllObjects()", "Saves all opened objects. Before overwriting the user is asked. "
"If no filename is available a dialog is shown. (only works if GUI is available)",QStringList(), QStringList());
emit setSlotDescription("saveAllObjectsTo()", "Saves all opened objects. The locations can be chosen in dialogs. "
"(only works if GUI is available)",QStringList(), QStringList());
emit setSlotDescription("saveSettings()", "Show the dialog to save the current setting. (only works if GUI is available)",QStringList(), QStringList());
//load slots
emit setSlotDescription("loadObject()", "Show the dialog to load an object. (only works if GUI is available)",QStringList(), QStringList());
emit setSlotDescription("loadSettings()", "Show the dialog to load settings. (only works if GUI is available)",QStringList(), QStringList());
emit setSlotDescription("loadSettings(QString)", "load settings from file.",QStringList(), QStringList());
}
// //-----------------------------------------------------------------------------
//
......
......@@ -241,9 +241,6 @@ signals:
/// A plugin wants to load a given file
void slotLoad(QString _filename, DataType _type, int& _id);
/// A plugin wants to save an object;
void slotSave(int _id , QString _filename);
/// Called when a file has been opened
void slotObjectOpened ( int _id );
......@@ -350,16 +347,43 @@ private:
*/
int loadObject( DataType _type, QString _filename);
/** Save object with given id
* @param _id id of the object
*/
bool saveObject( int _id, QString _filename );
/** @} */
/** Show dialog for saving an object to a new location
* @param _id id of the object
* @param _filename current filename of the object
*/
bool saveObjectTo( int _id, QString _filename );
//===========================================================================
/** @name Load / Save slots
* @{ */
//===========================================================================
public slots:
/** Save object with given id
* @param _id id of the object
*/
bool saveObject( int _id, QString _filename );
/** Show dialog for saving an object to a new location
* @param _id id of the object
* @param _filename current filename of the object
*/
bool saveObjectTo( int _id, QString _filename );
/// Slot for saving objects from Menu
void saveAllObjects();
/// Slot for saving objects to a new location
void saveAllObjectsTo();
/// Save current status to a settings file
void saveSettings();
/// Open Load Widget
void loadObject();
/// Load status from file
void loadSettings();
/// Load status from file
void loadSettings(QString _filename);
/** @} */
......@@ -384,24 +408,9 @@ private:
/// Slot adding empty object from Menu of a given type
void slotAddEmptyObject( DataType _type , int& _id );
/// Slot for saving objects from Menu
void slotSaveMenu();
/// Slot for saving objects to a new location
void slotSaveToMenu();
/// Load status from ini file
void slotLoadIniMenu();
/// Save current status to ini file
void slotSaveIniMenu();
/// Exit Application
void slotExit();
/// Open Load Widget
void slotLoadMenu();
/// Open Recent file
void slotRecentOpen(QAction* _action);
......
......@@ -427,7 +427,7 @@ void Core::openIniFile(QString _filename) {
coreWidget_->setStatus(ApplicationStatus::BLOCKED );
}
OpenFlipper::Options::openingIni(true);
OpenFlipper::Options::loadingSettings(true);
readApplicationOptions(ini);
......@@ -492,7 +492,7 @@ void Core::openIniFile(QString _filename) {
ini.disconnect();
OpenFlipper::Options::openingIni(false);
OpenFlipper::Options::loadingSettings(false);
resetScenegraph();
......
......@@ -75,7 +75,7 @@ void Core::openObjFile(QString _filename)
coreWidget_->setStatus(ApplicationStatus::BLOCKED );
}
OpenFlipper::Options::openingIni(true);
OpenFlipper::Options::loadingSettings(true);
while(!in.eof())
......@@ -112,7 +112,7 @@ void Core::openObjFile(QString _filename)
}
OpenFlipper::Options::openingIni(false);
OpenFlipper::Options::loadingSettings(false);
resetScenegraph();
......
......@@ -800,7 +800,7 @@ void Core::loadPlugin(QString filename, bool silent){
this , SLOT(slotLoad( QString,DataType,int& )),Qt::DirectConnection );
if ( checkSignal(plugin,"save(int,QString)" ) )
connect(plugin , SIGNAL( save(int,QString) ) ,
this , SLOT( slotSave(int,QString) ), Qt::DirectConnection);
this , SLOT( saveObject(int,QString) ), Qt::DirectConnection);
if ( checkSlot( plugin , "fileOpened(int)" ) )
connect(this , SIGNAL( openedFile( int) ) ,
......
......@@ -44,7 +44,7 @@
#include "OpenFlipper/widgets/addEmptyWidget/addEmptyWidget.hh"
void Core::resetScenegraph() {
if ( OpenFlipper::Options::gui() && !OpenFlipper::Options::openingIni() ) {
if ( OpenFlipper::Options::gui() && !OpenFlipper::Options::loadingSettings() ) {
// update scene graph
coreWidget_->examiner_widget_->lockUpdate();
coreWidget_->examiner_widget_->sceneGraph(root_node_scenegraph_);
......@@ -121,7 +121,7 @@ int Core::loadObject( DataType _type, QString _filename) {
if ( OpenFlipper::Options::gui() ) {
coreWidget_->statusMessage( "Loading " + _filename + " ...");
if ( !OpenFlipper::Options::openingIni() )
if ( !OpenFlipper::Options::loadingSettings() )
coreWidget_->setStatus(ApplicationStatus::PROCESSING );
}
......@@ -134,7 +134,7 @@ int Core::loadObject( DataType _type, QString _filename) {
else
coreWidget_->statusMessage( "Loading " + _filename + " ... failed!", 4000 );
if ( !OpenFlipper::Options::openingIni() )
if ( !OpenFlipper::Options::loadingSettings() )
coreWidget_->setStatus(ApplicationStatus::READY );
}
......@@ -248,45 +248,75 @@ void Core::slotAddEmptyObjectMenu() {
emit log(LOGERR,"Could not show 'add Empty' dialog. Missing file-plugins.");
}
//========================================================================================
// === Public Slots ============================
//========================================================================================
/// Open Load-Object Widget
void Core::slotLoadMenu() {
if (supportedTypes_.size() != 0){
static LoadWidget* widget = 0;
void Core::loadObject() {
if ( OpenFlipper::Options::gui() ){
if (supportedTypes_.size() != 0){
static LoadWidget* widget = 0;
// Open Widget
if ( !widget ){
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)));
}
widget->showLoad();
}else
emit log(LOGERR,"Could not show 'load objects' dialog. Missing file-plugins.");
// Open Widget
if ( !widget ){
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(slotSave(int, QString)));
}
widget->showLoad();
}else
emit log(LOGERR,"Could not show 'load objects' dialog. Missing file-plugins.");
}
}
/// Load settings from ini-file
void Core::slotLoadIniMenu(){
QString complete_name;
/// Load settings from file
void Core::loadSettings(){
if ( OpenFlipper::Options::gui() ){
QString complete_name;
complete_name = ACG::getOpenFileName(coreWidget_,
tr("Load Settings"),
tr("INI files (*.ini);;OBJ files (*.obj)"),
OpenFlipper::Options::currentDirStr());
if (complete_name.isEmpty())
return;
QString newpath = complete_name.section(OpenFlipper::Options::dirSeparator(),0,-2);
OpenFlipper::Options::currentDir(newpath);
if ( complete_name.endsWith("ini") ) {
openIniFile(complete_name);
applyOptions();
} else if ( complete_name.endsWith("obj") ) {
openObjFile(complete_name);
applyOptions();
}
coreWidget_->addRecent(complete_name, DATA_NONE);
}
}
complete_name = ACG::getOpenFileName(coreWidget_,
tr("Load Settings"),
tr("INI files (*.ini);;OBJ files (*.obj)"),
OpenFlipper::Options::currentDirStr());
if (complete_name.isEmpty())
/// Load settings from file
void Core::loadSettings(QString _filename){
if ( !QFile(_filename).exists() )
return;
QString newpath = complete_name.section(OpenFlipper::Options::dirSeparator(),0,-2);
QString newpath = _filename.section(OpenFlipper::Options::dirSeparator(),0,-2);
OpenFlipper::Options::currentDir(newpath);
if ( complete_name.endsWith("ini") ) {
openIniFile(complete_name);
if ( _filename.endsWith("ini") ) {
openIniFile(_filename);
applyOptions();
} else if ( complete_name.endsWith("obj") ) {
openObjFile(complete_name);
} else if ( _filename.endsWith("obj") ) {
openObjFile(_filename);
applyOptions();
}
if ( OpenFlipper::Options::gui() )
coreWidget_->addRecent(complete_name, DATA_NONE);
}
......@@ -30,9 +30,6 @@
//=============================================================================
#include "Core.hh"
#include "OpenFlipper/common/GlobalOptions.hh"
......@@ -41,15 +38,13 @@
#include "OpenFlipper/widgets/loadWidget/loadWidget.hh"
#include <QFileDialog>
#include <QGroupBox>
#include <QBoxLayout>
//========================================================================================
// === Save Functions ============================
// === Public Slots (called by CoreWidget's File-Menu / Scripting / Plugins) =========
//========================================================================================
/// Save an object
/// get object with given id, find a plugin to save dataType, save!
/// (existing files will be overwritten)
bool Core::saveObject( int _id, QString _filename ) {
BaseObjectData* object;
PluginFunctions::get_object(_id,object);
......@@ -59,275 +54,126 @@ bool Core::saveObject( int _id, QString _filename ) {
if ( OpenFlipper::Options::gui() ) {
coreWidget_->statusMessage( "Saving " + _filename + " ...");
if ( !OpenFlipper::Options::openingIni() )
if ( !OpenFlipper::Options::savingSettings() )
coreWidget_->setStatus(ApplicationStatus::PROCESSING );
}
//load file
//save object
bool ok = supportedTypes_[i].plugin->saveObject(_id,_filename);
if ( OpenFlipper::Options::gui() ) {
if (ok)
coreWidget_->statusMessage( "Saving " + _filename + " ... done", 4000 );
else
coreWidget_->statusMessage( "Saving " + _filename + " ... failed!", 4000 );
if ( !OpenFlipper::Options::openingIni() )
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() );
return ok;
}
return false; //no plugin found
// no plugin found
if ( OpenFlipper::Options::gui() )
coreWidget_->statusMessage( "Saving " + _filename + " ... failed!", 4000 );
return false;
}
//-----------------------------------------------------------------------------------------------------
/// Save an object to a new location
/// Show the save-widget to determine a filename, the widget calls slotSave to save the object
bool Core::saveObjectTo( int _id, QString _filename ) {
BaseObjectData* object;
PluginFunctions::get_object(_id,object);
//init widget
static LoadWidget* widget = 0;
if ( !widget ){
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(slotSave(int, QString)));
}
if ( OpenFlipper::Options::gui() ){
BaseObjectData* object;
PluginFunctions::get_object(_id,object);
//init widget
static LoadWidget* widget = 0;
if ( !widget ){
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)));
}
if (supportedTypes_.size() != 0)
return widget->showSave(_id,_filename);
else{
emit log(LOGERR,"Could not show 'save objects' dialog. Missing file-plugins.");
return false;
}
if (supportedTypes_.size() != 0)
return widget->showSave(_id,_filename);
else{
emit log(LOGERR,"Could not show 'save objects' dialog. Missing file-plugins.");
}else
return false;
}
}
//========================================================================================
// === Save Slots ============================
//========================================================================================
/// A plugin wants to save an object
/// existing filename will be overwritten!!!
void Core::slotSave(int _id , QString _filename) {
saveObject(_id,_filename);
BaseObjectData* object;
PluginFunctions::get_object(_id,object);
if ( object != 0 && OpenFlipper::Options::gui() )
coreWidget_->addRecent( _filename, object->dataType() );
}
//========================================================================================
// === Menu Slots ============================
//========================================================================================
//-----------------------------------------------------------------------------------------------------
/// Save changes to objects
void Core::slotSaveMenu(){
//iterate over all target objects
bool overwrite_all = false;
for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::TARGET_OBJECTS) ;
o_it != PluginFunctions::objects_end(); ++o_it) {
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{
QString filename = o_it->path() + OpenFlipper::Options::dirSeparator() + o_it->name() ;
QFile file(filename);
if ( file.exists() && !overwrite_all){
int ret = QMessageBox::warning(coreWidget_, tr("File exists"),QString("The file '" +filename+ "' already exists.\n"+
"Do you want to overwrite the file?"),QMessageBox::YesToAll |
QMessageBox::Yes|QMessageBox::No|QMessageBox::NoToAll,QMessageBox::No);
switch(ret){
case QMessageBox::YesToAll: overwrite_all = true;break;
case QMessageBox::Yes: break;
case QMessageBox::No: continue;
case QMessageBox::NoToAll: return;
}
}
saveObject(o_it->id(),filename);
}
}
}
/// Save changes for all target objects (slot is called from CoreWidget's File-Menu)
void Core::saveAllObjects(){
/// Save object as
void Core::slotSaveToMenu(){
//init widget
static LoadWidget* widget = 0;
if ( !widget ){
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(slotSave(int, QString)));
}
if ( OpenFlipper::Options::gui() ){
if (supportedTypes_.size() != 0){
//ensure that all options are on their default values
for (int i=0; i < (int)supportedTypes_.size(); i++)
supportedTypes_[i].plugin->saveOptionsWidget("");
//iterate over all target objects
for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::TARGET_OBJECTS);
bool overwrite_all = false;
for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::ALL_OBJECTS) ;
o_it != PluginFunctions::objects_end(); ++o_it) {
QString filename = o_it->path() + OpenFlipper::Options::dirSeparator() + o_it->name();
widget->showSave(o_it->id(),filename);
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{
//ask for overwriting
QString filename = o_it->path() + OpenFlipper::Options::dirSeparator() + o_it->name() ;
QFile file(filename);
if ( file.exists() && !overwrite_all){
int ret = QMessageBox::warning(coreWidget_, tr("File exists"),QString("The file '" +filename+ "' already exists.\n"+
"Do you want to overwrite the file?"),QMessageBox::YesToAll |
QMessageBox::Yes|QMessageBox::No|QMessageBox::NoToAll,QMessageBox::No);
switch(ret){
case QMessageBox::YesToAll: overwrite_all = true;break;
case QMessageBox::Yes: break;
case QMessageBox::No: continue;
case QMessageBox::NoToAll: return;
}
}
// and save
saveObject(o_it->id(),filename);
}
}
}else
emit log(LOGERR,"Could not show 'save objects' dialog. Missing file-plugins.");
}
/// Save to Ini File
void Core::slotSaveIniMenu(){
QString complete_name;
QFileDialog fileDialog( coreWidget_,
tr("Save Settings"),
OpenFlipper::Options::currentDirStr(),
tr("INI files (*.ini);;OBJ files (*.obj )") );
fileDialog.setAcceptMode ( QFileDialog::AcceptSave );
fileDialog.setFileMode ( QFileDialog::AnyFile );
fileDialog.setDefaultSuffix("ini");
QGridLayout *layout = (QGridLayout*)fileDialog.layout();
QGroupBox* optionsBox = new QGroupBox( &fileDialog ) ;
// optionsBox->setFrameStyle(QFrame::StyledPanel | QFrame::Raised);
optionsBox->setSizePolicy( QSizePolicy ( QSizePolicy::Expanding , QSizePolicy::Preferred ) );
optionsBox->setTitle("Options");
layout->addWidget( optionsBox, layout->rowCount() , 0 , 1,layout->columnCount() );
QCheckBox *saveAllBox = new QCheckBox(optionsBox);
saveAllBox->setText("Save everything to same folder");
saveAllBox->setToolTip("Save all open files to the same folder as the ini file");
saveAllBox->setCheckState( Qt::Checked );
QCheckBox *askOverwrite = new QCheckBox(optionsBox);
askOverwrite->setText("Ask before overwriting files");
askOverwrite->setToolTip("If a file exists you will get asked what to do");
askOverwrite->setCheckState( Qt::Checked );
QCheckBox *targetOnly = new QCheckBox(optionsBox);
targetOnly->setText("Save only target objects");
targetOnly->setToolTip("Only objects with target flag will be handled");
targetOnly->setCheckState( Qt::Unchecked );
QBoxLayout* frameLayout = new QBoxLayout(QBoxLayout::TopToBottom,optionsBox);
frameLayout->addWidget( saveAllBox , 0 , 0);
frameLayout->addWidget( askOverwrite , 1 , 0);
frameLayout->addWidget( targetOnly , 2 , 0);
frameLayout->addStretch();
QStringList fileNames;
if (fileDialog.exec()) {
fileNames = fileDialog.selectedFiles();
} else {
return;
}
if ( fileNames.size() > 1 ) {
std::cerr << "Too many save filenames selected" << std::endl;
return;
}
}
complete_name = fileNames[0];
QString newpath = complete_name.section(OpenFlipper::Options::dirSeparator(),0,-2);
OpenFlipper::Options::currentDir(newpath);
std::cout << "new path: " << newpath.toStdString() << std::endl;
std::cout << "complete_name: " << complete_name.toStdString() << std::endl;
//init save-as widget
static LoadWidget* widget = 0;
if ( !widget ){
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(slotSave(int, QString)));
}
PluginFunctions::IteratorRestriction restriction;
if ( targetOnly->isChecked() )
restriction = PluginFunctions::TARGET_OBJECTS;
else
restriction = PluginFunctions::ALL_OBJECTS;
//iterate over opened objects and save them
for ( PluginFunctions::ObjectIterator o_it(restriction);
o_it != PluginFunctions::objects_end(); ++o_it)
{
if ( saveAllBox->isChecked() )
{
// Use path of ini file for all objects
QString filename = newpath + OpenFlipper::Options::dirSeparator() + o_it->name();
// enforce that all files end with obj extension
if ( complete_name.endsWith("obj") )
{
if (!filename.endsWith("obj"))
{
std::cout << "\nold filename: " << filename.toStdString() << std::endl;
// remove old extension
int pos = filename.lastIndexOf(".");
filename.remove(pos+1, filename.length() - pos);
std::cout << "cleared filename = " << filename.toStdString() << std::endl;
// add obj extension
filename += "obj";
std::cout << "new filename: " << filename.toStdString() << std::endl;
}
}
// normally the file does not exist when saving all to one folder
if ( !QFile(filename).exists() || !askOverwrite->isChecked() )