Commit 58ad738f authored by Jan Möbius's avatar Jan Möbius
Browse files

Extended ini interface and load settings dialog

git-svn-id: http://www.openflipper.org/svnrepo/OpenFlipper/branches/Free@4583 383ad7c9-94d9-4d36-a494-682f7c89f535
parent 4cfd50a1
...@@ -419,7 +419,7 @@ Core::init() { ...@@ -419,7 +419,7 @@ Core::init() {
} }
// Load global ini files. Use plugin and global options from these files // Load global ini files. Use plugin and global options from these files
openIniFile( optionFiles[i] ,true,true); openIniFile( optionFiles[i] ,true,true,false);
} }
......
...@@ -180,6 +180,9 @@ signals: ...@@ -180,6 +180,9 @@ signals:
/// This signal is used to tell the plugins to load their new status /// This signal is used to tell the plugins to load their new status
void iniLoadOptions( INIFile& _ini ); void iniLoadOptions( INIFile& _ini );
/// This signal is used to tell the plugins to load their new status after objects are loaded
void iniLoadOptionsLast( INIFile& _ini );
/// Tell plugins to create a backup /// Tell plugins to create a backup
void createBackup( int _id , QString _name , int nextBackupId_); void createBackup( int _id , QString _name , int nextBackupId_);
...@@ -451,11 +454,15 @@ private: ...@@ -451,11 +454,15 @@ private:
* This function will open an ini file and load the information in it. * This function will open an ini file and load the information in it.
* *
* @param _filename Name of the ini file * @param _filename Name of the ini file
* @param _coreSettings Load core settings from the file
* @param _perPluginSettings Load per plugin settings from the file * @param _perPluginSettings Load per plugin settings from the file
* @param _coreSettings Load core settings from the file * @param _loadObjects Load objects defined in the ini file
* *
*/ */
void openIniFile(QString _filename, bool _coreSettings , bool _perPluginSettings); void openIniFile( QString _filename,
bool _coreSettings ,
bool _perPluginSettings,
bool _loadObjects );
/// Open an obj file and load everything in it ( Application/File Options ) /// Open an obj file and load everything in it ( Application/File Options )
void openObjFile(QString _filename); void openObjFile(QString _filename);
...@@ -472,9 +479,16 @@ private: ...@@ -472,9 +479,16 @@ private:
* @param _filename filename of the ini file to write with full path * @param _filename filename of the ini file to write with full path
* @param _relativePaths This defines if the paths to the objects should be made relative * @param _relativePaths This defines if the paths to the objects should be made relative
* @param _targetOnly Select if we want all open objects or only the ones which are selected as target. * @param _targetOnly Select if we want all open objects or only the ones which are selected as target.
* @param _systemSettings Choose if you also want to save system settings into the ini file. * @param _saveSystemSettings Choose if you also want to save system settings into the ini file.
* @param _savePluginSettings Choose if you want to save per Plugin global settings into the ini file.
* @param _saveObjectInfo If you want to store information about all open objects this has to be true
*/ */
void writeIniFile(QString _filename, bool _relativePaths, bool _targetOnly, bool _systemSettings); void writeIniFile( QString _filename,
bool _relativePaths,
bool _targetOnly,
bool _saveSystemSettings,
bool _savePluginSettings ,
bool _saveObjectInfo );
/** \brief Write current status to obj file (Application and File Options) /** \brief Write current status to obj file (Application and File Options)
* *
......
...@@ -457,7 +457,10 @@ void Core::writeApplicationOptions(INIFile& _ini) { ...@@ -457,7 +457,10 @@ void Core::writeApplicationOptions(INIFile& _ini) {
_ini.add_entry("Options","Stereo",OpenFlipper::Options::stereo() ); _ini.add_entry("Options","Stereo",OpenFlipper::Options::stereo() );
} }
void Core::openIniFile(QString _filename, bool _coreSettings, bool _perPluginSettings ){ void Core::openIniFile( QString _filename,
bool _coreSettings,
bool _perPluginSettings,
bool _loadObjects ){
INIFile ini; INIFile ini;
if ( ! ini.connect(_filename,false) ) { if ( ! ini.connect(_filename,false) ) {
...@@ -476,74 +479,82 @@ void Core::openIniFile(QString _filename, bool _coreSettings, bool _perPluginSet ...@@ -476,74 +479,82 @@ void Core::openIniFile(QString _filename, bool _coreSettings, bool _perPluginSet
if ( _coreSettings ) if ( _coreSettings )
readApplicationOptions(ini); readApplicationOptions(ini);
QStringList openFiles; // if requested load per Plugin settings from the settings file
if ( _perPluginSettings )
emit iniLoadOptions( ini );
// Parse File section for files to open if ( _loadObjects ) {
if ( ini.section_exists("OpenFiles") && ini.get_entry(openFiles,"OpenFiles","open") ) {
bool newActiveObject = false; QStringList openFiles;
for ( int i = 0 ; i < openFiles.size(); ++i ) { // Parse File section for files to open
if ( ini.section_exists("OpenFiles") && ini.get_entry(openFiles,"OpenFiles","open") ) {
QString sectionName = openFiles[i]; bool newActiveObject = false;
QString path; for ( int i = 0 ; i < openFiles.size(); ++i ) {
ini.get_entry( path, sectionName , "path" );
//check if path is relative QString sectionName = openFiles[i];
if (path.startsWith( "." + OpenFlipper::Options::dirSeparator() )){
//check if _filename contains a path QString path;
if (_filename.section(OpenFlipper::Options::dirSeparator(), 0, -2) != ""){ ini.get_entry( path, sectionName , "path" );
path.remove(0,1); // remove .
path = _filename.section(OpenFlipper::Options::dirSeparator(), 0, -2) + path; //check if path is relative
if (path.startsWith( "." + OpenFlipper::Options::dirSeparator() )){
//check if _filename contains a path
if (_filename.section(OpenFlipper::Options::dirSeparator(), 0, -2) != ""){
path.remove(0,1); // remove .
path = _filename.section(OpenFlipper::Options::dirSeparator(), 0, -2) + path;
}
} }
}
int tmpType; int tmpType;
DataType type = DATA_TRIANGLE_MESH; DataType type = DATA_TRIANGLE_MESH;
if ( ini.get_entry( tmpType, sectionName , "type" )) { if ( ini.get_entry( tmpType, sectionName , "type" )) {
type = DataType(tmpType); type = DataType(tmpType);
emit log(LOGWARN, "This ini file uses old int style ObjectType fields!" ); emit log(LOGWARN, "This ini file uses old int style ObjectType fields!" );
emit log(LOGWARN, "Please convert it to new format! ( ... just save it )" ); emit log(LOGWARN, "Please convert it to new format! ( ... just save it )" );
} else { } else {
QString typeName=""; QString typeName="";
if ( ini.get_entry( typeName, sectionName , "type" )) { if ( ini.get_entry( typeName, sectionName , "type" )) {
type = typeId(typeName); type = typeId(typeName);
} else } else
emit log(LOGWARN, "Unable to get DataType for object " + sectionName + " assuming Triangle Mesh" ); emit log(LOGWARN, "Unable to get DataType for object " + sectionName + " assuming Triangle Mesh" );
} }
int newObjectId = loadObject(type, path); int newObjectId = loadObject(type, path);
BaseObject* object = objectRoot_->childExists( newObjectId ); BaseObject* object = objectRoot_->childExists( newObjectId );
if ( object == 0 ) { if ( object == 0 ) {
emit log(LOGERR,"Unable to open Object " + path); emit log(LOGERR,"Unable to open Object " + path);
continue; continue;
} }
bool flag;
if ( ini.get_entry( flag, sectionName , "target" ) ) {
object->target(flag);
newActiveObject = true;
}
if ( ini.get_entry( flag, sectionName , "source" ) )
object->source(flag);
bool flag; emit iniLoad( ini,object->id() );
if ( ini.get_entry( flag, sectionName , "target" ) ) {
object->target(flag);
newActiveObject = true;
} }
if ( ini.get_entry( flag, sectionName , "source" ) ) if ( newActiveObject )
object->source(flag); emit activeObjectChanged();
emit iniLoad( ini,object->id() );
} }
if ( newActiveObject )
emit activeObjectChanged();
} }
// if requested load per Plugin settings from the settings file // Tell Plugins that all objects are
if ( _perPluginSettings ) if ( _perPluginSettings )
emit iniLoadOptions( ini ); emit iniLoadOptionsLast( ini );
ini.disconnect(); ini.disconnect();
...@@ -558,10 +569,12 @@ void Core::openIniFile(QString _filename, bool _coreSettings, bool _perPluginSet ...@@ -558,10 +569,12 @@ void Core::openIniFile(QString _filename, bool _coreSettings, bool _perPluginSet
} }
void Core::writeIniFile(QString _filename, bool _relativePaths, bool _targetOnly, bool _systemSettings ) { void Core::writeIniFile(QString _filename,
bool _relativePaths,
bool _targetOnly,
std::cerr << "Currently unsupported : Saving ini files without including global settings" << std::endl; bool _saveSystemSettings,
bool _savePluginSettings ,
bool _saveObjectInfo ) {
INIFile ini; INIFile ini;
...@@ -576,57 +589,58 @@ void Core::writeIniFile(QString _filename, bool _relativePaths, bool _targetOnly ...@@ -576,57 +589,58 @@ void Core::writeIniFile(QString _filename, bool _relativePaths, bool _targetOnly
} }
// Only save application settings when requested // Only save application settings when requested
if ( _systemSettings ) if ( _saveSystemSettings )
writeApplicationOptions(ini); writeApplicationOptions(ini);
// This vector will hold the file sections to open if ( _savePluginSettings )
QStringList openFiles; emit iniSaveOptions( ini );
if ( _saveObjectInfo ) {
PluginFunctions::IteratorRestriction restriction; // This vector will hold the file sections to open
if ( _targetOnly ) QStringList openFiles;
restriction = PluginFunctions::TARGET_OBJECTS;
else
restriction = PluginFunctions::ALL_OBJECTS; PluginFunctions::IteratorRestriction restriction;
if ( _targetOnly )
QString keyName; restriction = PluginFunctions::TARGET_OBJECTS;
QString sectionName; else
for ( PluginFunctions::ObjectIterator o_it(restriction) ; restriction = PluginFunctions::ALL_OBJECTS;
o_it != PluginFunctions::objects_end(); ++o_it) {
QString file = o_it->path() + OpenFlipper::Options::dirSeparator() + o_it->name(); QString keyName;
if (QFile(file).exists()){ QString sectionName;
// Add a section for this object for ( PluginFunctions::ObjectIterator o_it(restriction) ;
sectionName = o_it->name(); o_it != PluginFunctions::objects_end(); ++o_it) {
ini.add_section( sectionName ); QString file = o_it->path() + OpenFlipper::Options::dirSeparator() + o_it->name();
openFiles.push_back( sectionName ); if (QFile(file).exists()){
// Add a section for this object
//modify filename if relativePaths are wanted sectionName = o_it->name();
if (_relativePaths){ ini.add_section( sectionName );
int prefixLen = _filename.section(OpenFlipper::Options::dirSeparator(),0,-2).length(); openFiles.push_back( sectionName );
file.remove(0, prefixLen);
file = "." + file; //modify filename if relativePaths are wanted
} if (_relativePaths){
// Add the path of this object to the section int prefixLen = _filename.section(OpenFlipper::Options::dirSeparator(),0,-2).length();
ini.add_entry( sectionName , "path" , file ); file.remove(0, prefixLen);
ini.add_entry( sectionName , "type" , typeName(o_it->dataType() ) ); file = "." + file;
ini.add_entry( sectionName , "target" , o_it->target() ); }
ini.add_entry( sectionName , "source" , o_it->source() ); // Add the path of this object to the section
ini.add_entry( sectionName , "path" , file );
ini.add_entry( sectionName , "type" , typeName(o_it->dataType() ) );
ini.add_entry( sectionName , "target" , o_it->target() );
ini.add_entry( sectionName , "source" , o_it->source() );
}
} }
}
ini.add_entry("OpenFiles","open",openFiles); ini.add_entry("OpenFiles","open",openFiles);
///@todo :write ini file ( Ask for objects which are not written yet // Tell plugins to save their information for the given object
std::cerr << "Todo : write ini file ( Ask for objects which are not written yet )" << std::endl; for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::ALL_OBJECTS) ;
o_it != PluginFunctions::objects_end(); ++o_it)
emit iniSave( ini , o_it->id() );
}
// Tell plugins to save their information for the given object
for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::ALL_OBJECTS) ;
o_it != PluginFunctions::objects_end(); ++o_it)
emit iniSave( ini , o_it->id() );
// TODO : Save per plugin options only if requested
emit iniSaveOptions( ini );
ini.disconnect(); ini.disconnect();
......
...@@ -756,6 +756,10 @@ void Core::loadPlugin(QString filename, bool silent){ ...@@ -756,6 +756,10 @@ void Core::loadPlugin(QString filename, bool silent){
if ( checkSlot( plugin , "loadIniFileOptions(INIFile&)" ) ) if ( checkSlot( plugin , "loadIniFileOptions(INIFile&)" ) )
connect(this , SIGNAL(iniLoadOptions( INIFile& )), connect(this , SIGNAL(iniLoadOptions( INIFile& )),
plugin , SLOT( loadIniFileOptions( INIFile& ) ),Qt::DirectConnection); plugin , SLOT( loadIniFileOptions( INIFile& ) ),Qt::DirectConnection);
if ( checkSlot( plugin , "loadIniFileOptionsLast(INIFile&)" ) )
connect(this , SIGNAL(iniLoadOptionsLast( INIFile& )),
plugin , SLOT( loadIniFileOptionsLast( INIFile& ) ),Qt::DirectConnection);
} }
//Check if the plugin supports Texture-Interface //Check if the plugin supports Texture-Interface
......
...@@ -101,7 +101,7 @@ int Core::loadObject ( QString _filename ) { ...@@ -101,7 +101,7 @@ int Core::loadObject ( QString _filename ) {
if (_filename.endsWith(".ini")) { if (_filename.endsWith(".ini")) {
// Load all information from the given ini file // Load all information from the given ini file
openIniFile(_filename,true,true); openIniFile(_filename,true,true,true);
if ( OpenFlipper::Options::gui() ) if ( OpenFlipper::Options::gui() )
coreWidget_->addRecent(_filename, DATA_NONE); coreWidget_->addRecent(_filename, DATA_NONE);
...@@ -297,19 +297,70 @@ void Core::loadSettings(){ ...@@ -297,19 +297,70 @@ void Core::loadSettings(){
QString complete_name; QString complete_name;
complete_name = ACG::getOpenFileName(coreWidget_,
tr("Load Settings"), QFileDialog fileDialog( coreWidget_,
tr("INI files (*.ini);;OBJ files (*.obj)"), tr("Load Settings"),
OpenFlipper::Options::currentDirStr()); OpenFlipper::Options::currentDirStr(),
if (complete_name.isEmpty()) tr("INI files (*.ini);;OBJ files (*.obj )") );
fileDialog.setAcceptMode ( QFileDialog::AcceptOpen );
fileDialog.setFileMode ( QFileDialog::AnyFile );
QGridLayout *layout = (QGridLayout*)fileDialog.layout();
QGroupBox* optionsBox = new QGroupBox( &fileDialog ) ;
optionsBox->setSizePolicy( QSizePolicy ( QSizePolicy::Expanding , QSizePolicy::Preferred ) );
optionsBox->setTitle("Options");
layout->addWidget( optionsBox, layout->rowCount() , 0 , 1,layout->columnCount() );
QCheckBox *loadProgramSettings = new QCheckBox(optionsBox);
loadProgramSettings->setText("Load program settings");
loadProgramSettings->setToolTip("Load all current program settings from the file ( This will include view settings, colors,...) ");
loadProgramSettings->setCheckState( Qt::Unchecked );
QCheckBox *loadPluginSettings = new QCheckBox(optionsBox);
loadPluginSettings->setText("Load per Plugin Settings");
loadPluginSettings->setToolTip("Plugins should load their current global settings from the file");
loadPluginSettings->setCheckState( Qt::Checked );
QCheckBox *loadObjectInfo = new QCheckBox(optionsBox);
loadObjectInfo->setText("Load all objects defined in the file");
loadObjectInfo->setToolTip("Load all objects which are defined in the file");
loadObjectInfo->setCheckState( Qt::Checked );
QBoxLayout* frameLayout = new QBoxLayout(QBoxLayout::TopToBottom,optionsBox);
frameLayout->addWidget( loadProgramSettings , 0 , 0);
frameLayout->addWidget( loadPluginSettings , 1 , 0);
frameLayout->addWidget( loadObjectInfo , 2 , 0);
frameLayout->addStretch();
// ========================================================================================
// show the saveSettings-Dialog and get the target file
// ========================================================================================
QStringList fileNames;
if (fileDialog.exec()) {
fileNames = fileDialog.selectedFiles();
} else {
return;
}
if ( fileNames.size() > 1 ) {
std::cerr << "Too many save filenames selected" << std::endl;
return; return;
}
complete_name = fileNames[0];
QString newpath = complete_name.section(OpenFlipper::Options::dirSeparator(),0,-2); QString newpath = complete_name.section(OpenFlipper::Options::dirSeparator(),0,-2);
OpenFlipper::Options::currentDir(newpath); OpenFlipper::Options::currentDir(newpath);
if ( complete_name.endsWith("ini") ) { if ( complete_name.endsWith("ini") ) {
// TODO OptionsDialog for choosing which information should be read // TODO OptionsDialog for choosing which information should be read
openIniFile(complete_name,true,true); openIniFile( complete_name,
loadProgramSettings->isChecked(),
loadPluginSettings->isChecked(),
loadObjectInfo->isChecked());
applyOptions(); applyOptions();
} else if ( complete_name.endsWith("obj") ) { } else if ( complete_name.endsWith("obj") ) {
openObjFile(complete_name); openObjFile(complete_name);
...@@ -331,7 +382,7 @@ void Core::loadSettings(QString _filename){ ...@@ -331,7 +382,7 @@ void Core::loadSettings(QString _filename){
if ( _filename.endsWith("ini") ) { if ( _filename.endsWith("ini") ) {
// Loaded function for recent files. Load everything. // Loaded function for recent files. Load everything.
openIniFile(_filename,true,true); openIniFile(_filename,true,true,true);
applyOptions(); applyOptions();
} else if ( _filename.endsWith("obj") ) { } else if ( _filename.endsWith("obj") ) {
openObjFile(_filename); openObjFile(_filename);
......
...@@ -71,6 +71,16 @@ void Core::saveSettings(){ ...@@ -71,6 +71,16 @@ void Core::saveSettings(){
saveProgramSettings->setToolTip("Save all current program settings to the file ( This will include view settings, colors,...) "); saveProgramSettings->setToolTip("Save all current program settings to the file ( This will include view settings, colors,...) ");
saveProgramSettings->setCheckState( Qt::Unchecked ); saveProgramSettings->setCheckState( Qt::Unchecked );
QCheckBox *savePluginSettings = new QCheckBox(optionsBox);
savePluginSettings->setText("Save per Plugin Settings");
savePluginSettings->setToolTip("Plugins should add their current global settings to the file");
savePluginSettings->setCheckState( Qt::Checked );
QCheckBox *saveObjectInfo = new QCheckBox(optionsBox);
saveObjectInfo->setText("Save open object information to the file");
saveObjectInfo->setToolTip("Save all open Objects and add them to the settings file ( they will be loaded if opening the settings file");
saveObjectInfo->setCheckState( Qt::Checked );
QCheckBox *saveAllBox = new QCheckBox(optionsBox); QCheckBox *saveAllBox = new QCheckBox(optionsBox);
saveAllBox->setText("Save everything to same folder"); saveAllBox->setText("Save everything to same folder");
saveAllBox->setToolTip("Save all open files to the same folder as the ini file"); saveAllBox->setToolTip("Save all open files to the same folder as the ini file");
...@@ -87,10 +97,12 @@ void Core::saveSettings(){ ...@@ -87,10 +97,12 @@ void Core::saveSettings(){
targetOnly->setCheckState( Qt::Unchecked ); targetOnly->setCheckState( Qt::Unchecked );
QBoxLayout* frameLayout = new QBoxLayout(QBoxLayout::TopToBottom,optionsBox); QBoxLayout* frameLayout = new QBoxLayout(QBoxLayout::TopToBottom,optionsBox);
frameLayout->addWidget( saveProgramSettings , 0 , 0); frameLayout->addWidget( saveProgramSettings , 0 , 0);
frameLayout->addWidget( saveAllBox , 1 , 0); frameLayout->addWidget( savePluginSettings , 1 , 0);
frameLayout->addWidget( askOverwrite , 2 , 0); frameLayout->addWidget( saveObjectInfo , 2 , 0);
frameLayout->addWidget( targetOnly , 3 , 0); frameLayout->addWidget( saveAllBox , 3 , 0);
frameLayout->addWidget( askOverwrite , 4 , 0);
frameLayout->addWidget( targetOnly , 5 , 0);
frameLayout->addStretch(); frameLayout->addStretch();
...@@ -141,55 +153,58 @@ void Core::saveSettings(){ ...@@ -141,55 +153,58 @@ void Core::saveSettings(){
// ======================================================================================== // ========================================================================================
// Depending on the checkbox iterate over all objects or only the selected ones. // Depending on the checkbox iterate over all objects or only the selected ones.
PluginFunctions::IteratorRestriction restriction; if ( saveObjectInfo->isChecked() ) {
if ( targetOnly->isChecked() )
restriction = PluginFunctions::TARGET_OBJECTS;
else
restriction = PluginFunctions::ALL_OBJECTS;
//Iterate over opened objects and save them PluginFunctions::IteratorRestriction restriction;
for ( PluginFunctions::ObjectIterator o_it(restriction); if ( targetOnly->isChecked() )
o_it != PluginFunctions::objects_end(); ++o_it) restriction = PluginFunctions::TARGET_OBJECTS;
{
QString filename;
if ( saveAllBox->isChecked() )
{
// Use path of settings file for all objects
filename = newpath + OpenFlipper::Options::dirSeparator() + o_it->name();
}
else 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)
{ {
// Use objects own path if it has one. Otherwise also use path of settings file QString filename;
filename = o_it->path() + OpenFlipper::Options::dirSeparator() + o_it->name();
// handle the case that the object was created in current session and not loaded from disk if ( saveAllBox->isChecked() )
if (o_it->path() == ".") { {
// Use path of settings file for all objects
filename = newpath + OpenFlipper::Options::dirSeparator() + o_it->name(); filename = newpath + OpenFlipper::Options::dirSeparator() + o_it->name();
std::cerr << "newpath : " << newpath.toStdString() << std::endl;
std::cerr << "name : " << o_it->name().toStdString() << std::endl;
} }
} else
{
// Use objects own path if it has one. Otherwise also use path of settings file
filename = o_it->path() + OpenFlipper::Options::dirSeparator() + o_it->name();
// handle the case that the object was created in current session and not loaded from disk
if (o_it->path() == ".") {
filename = newpath + OpenFlipper::Options::dirSeparator() + o_it->name();
std::cerr << "newpath : " << newpath.toStdString() << std::endl;
std::cerr << "name : " << o_it->name().toStdString() << std::endl;
}
}
// enforce that all files end with obj extension if its an obj-settings file // enforce that all files end with obj extension if its an obj-settings file
if ( complete_name.endsWith("obj") ) if ( complete_name.endsWith("obj") )
{
if (!filename.endsWith("obj"))
{ {
// remove old extension if (!filename.endsWith("obj"))
int pos = filename.lastIndexOf("."); {