Some refactoring regarding saving.

This commit is contained in:
dfighter1985 2014-10-12 21:15:13 +02:00
parent 257b5ac152
commit 47004dc293
4 changed files with 40 additions and 43 deletions

View file

@ -212,6 +212,7 @@ namespace GUIEditor
std::string wnd = d.getWindowName().toUtf8().constData(); std::string wnd = d.getWindowName().toUtf8().constData();
std::string mg = std::string( "ui:" ) + proj; std::string mg = std::string( "ui:" ) + proj;
std::string dir = d.getProjectDirectory().toUtf8().constData(); std::string dir = d.getProjectDirectory().toUtf8().constData();
_lastDir = dir.c_str();
std::string uiFile = "ui_" + proj + ".xml"; std::string uiFile = "ui_" + proj + ".xml";
bool b = GUICtrl->createNewGUI( proj, wnd ); bool b = GUICtrl->createNewGUI( proj, wnd );
@ -228,7 +229,7 @@ namespace GUIEditor
projectFiles.projectName = proj; projectFiles.projectName = proj;
projectFiles.masterGroup = mg; projectFiles.masterGroup = mg;
projectFiles.activeGroup = std::string( "ui:" ) + proj + ":" + wnd; projectFiles.activeGroup = std::string( "ui:" ) + proj + ":" + wnd;
projectFiles.version = NEW; projectFiles.version = SProjectFiles::NEW;
projectFiles.guiFiles.push_back( uiFile ); projectFiles.guiFiles.push_back( uiFile );
projectWindow->setupFiles( projectFiles ); projectWindow->setupFiles( projectFiles );
@ -278,8 +279,23 @@ namespace GUIEditor
// Can't save old projects any further, since the widgets are in multiple files in them // Can't save old projects any further, since the widgets are in multiple files in them
// using templates, styles and whatnot. There's no way to restore the original XML structure // using templates, styles and whatnot. There's no way to restore the original XML structure
// after it's loaded // after it's loaded
if( projectParser.getProjectVersion() == OLD ) if( projectFiles.version == SProjectFiles::OLD )
return; return;
std::string f = _lastDir.toUtf8().constData();
f += "/";
f += projectFiles.guiFiles[ 0 ];
WidgetSerializer widgetSerializer;
widgetSerializer.setFile( f );
widgetSerializer.setActiveGroup( projectFiles.activeGroup );
if( !widgetSerializer.serialize( projectFiles.masterGroup ) )
{
QMessageBox::critical( this,
tr( "Failed to save project" ),
tr( "There was an error while trying to save the project." ) );
return;
}
} }
void GUIEditorWindow::saveAs() void GUIEditorWindow::saveAs()
@ -292,42 +308,22 @@ namespace GUIEditor
if( dir.isEmpty() ) if( dir.isEmpty() )
return; return;
_lastDir = dir;
projectFiles.guiFiles.clear(); if( projectFiles.version == SProjectFiles::OLD )
projectFiles.guiFiles.push_back( "ui_" + projectFiles.projectName + ".xml" );
projectFiles.version = NEW;
QString newFile =
dir + "/" + projectFiles.projectName.c_str() + ".xml";
CProjectFileSerializer serializer;
serializer.setFile( newFile.toUtf8().constData() );
if( !serializer.serialize( projectFiles ) )
{ {
QMessageBox::critical( this, projectFiles.guiFiles.clear();
tr( "Failed to save project" ), projectFiles.guiFiles.push_back( "ui_" + projectFiles.projectName + ".xml" );
tr( "There was an error while trying to save the project." ) ); projectFiles.version = SProjectFiles::NEW;
return;
} }
std::string guiFile = currentProjectFile = _lastDir;
std::string( dir.toUtf8().constData() ) + "/" + "ui_" + projectFiles.projectName + ".xml"; currentProjectFile += "/";
currentProjectFile += projectFiles.projectName.c_str();
WidgetSerializer widgetSerializer; currentProjectFile += ".xml";
widgetSerializer.setFile( guiFile );
widgetSerializer.setActiveGroup( projectFiles.activeGroup );
if( !widgetSerializer.serialize( projectFiles.masterGroup ) )
{
QMessageBox::critical( this,
tr( "Failed to save project" ),
tr( "There was an error while trying to save the project." ) );
return;
}
QMessageBox::information( this,
tr( "Save successful" ),
tr( "Project saved successfully!" ) );
save();
} }
void GUIEditorWindow::reset() void GUIEditorWindow::reset()

View file

@ -63,7 +63,7 @@ namespace GUIEditor
unsigned long CProjectFileParser::getProjectVersion() const unsigned long CProjectFileParser::getProjectVersion() const
{ {
if( !loaded ) if( !loaded )
return OLD; return SProjectFiles::OLD;
return files.version; return files.version;
} }
@ -71,7 +71,7 @@ namespace GUIEditor
void CProjectFileParser::clear() void CProjectFileParser::clear()
{ {
files.projectName = ""; files.projectName = "";
files.version = OLD; files.version = SProjectFiles::OLD;
files.activeGroup = ""; files.activeGroup = "";
files.guiFiles.clear(); files.guiFiles.clear();
files.mapFiles.clear(); files.mapFiles.clear();

View file

@ -25,7 +25,7 @@ namespace GUIEditor
if( fileName.empty() ) if( fileName.empty() )
return false; return false;
if( project.version >= MAX_PROJECTFILE_VERSION ) if( project.version >= SProjectFiles::MAX_PROJECTFILE_VERSION )
return false; return false;
out.open( fileName.c_str() ); out.open( fileName.c_str() );

View file

@ -23,16 +23,17 @@
namespace GUIEditor namespace GUIEditor
{ {
enum ProjectVersion
{
OLD = 0,
NEW = 1,
MAX_PROJECTFILE_VERSION
};
struct SProjectFiles struct SProjectFiles
{ {
public: public:
enum ProjectVersion
{
OLD = 0,
NEW = 1,
MAX_PROJECTFILE_VERSION
};
std::string projectName; std::string projectName;
unsigned long version; unsigned long version;
std::string masterGroup; std::string masterGroup;