From f27f3917243021f5add966f0072712b0f10e86f2 Mon Sep 17 00:00:00 2001 From: dfighter1985 Date: Sun, 12 Oct 2014 21:15:13 +0200 Subject: [PATCH] Some refactoring regarding saving. --- .../plugins/gui_editor/gui_editor_window.cpp | 62 +++++++++---------- .../gui_editor/project_file_parser.cpp | 4 +- .../gui_editor/project_file_serializer.cpp | 2 +- .../src/plugins/gui_editor/project_files.h | 15 ++--- 4 files changed, 40 insertions(+), 43 deletions(-) diff --git a/code/studio/src/plugins/gui_editor/gui_editor_window.cpp b/code/studio/src/plugins/gui_editor/gui_editor_window.cpp index 70f206b3b..aad19c2ac 100644 --- a/code/studio/src/plugins/gui_editor/gui_editor_window.cpp +++ b/code/studio/src/plugins/gui_editor/gui_editor_window.cpp @@ -212,6 +212,7 @@ namespace GUIEditor std::string wnd = d.getWindowName().toUtf8().constData(); std::string mg = std::string( "ui:" ) + proj; std::string dir = d.getProjectDirectory().toUtf8().constData(); + _lastDir = dir.c_str(); std::string uiFile = "ui_" + proj + ".xml"; bool b = GUICtrl->createNewGUI( proj, wnd ); @@ -228,7 +229,7 @@ namespace GUIEditor projectFiles.projectName = proj; projectFiles.masterGroup = mg; projectFiles.activeGroup = std::string( "ui:" ) + proj + ":" + wnd; - projectFiles.version = NEW; + projectFiles.version = SProjectFiles::NEW; projectFiles.guiFiles.push_back( uiFile ); 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 // using templates, styles and whatnot. There's no way to restore the original XML structure // after it's loaded - if( projectParser.getProjectVersion() == OLD ) + if( projectFiles.version == SProjectFiles::OLD ) 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() @@ -292,42 +308,22 @@ namespace GUIEditor if( dir.isEmpty() ) return; + _lastDir = dir; - projectFiles.guiFiles.clear(); - 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 ) ) + if( projectFiles.version == SProjectFiles::OLD ) { - QMessageBox::critical( this, - tr( "Failed to save project" ), - tr( "There was an error while trying to save the project." ) ); - return; + projectFiles.guiFiles.clear(); + projectFiles.guiFiles.push_back( "ui_" + projectFiles.projectName + ".xml" ); + projectFiles.version = SProjectFiles::NEW; + } - std::string guiFile = - std::string( dir.toUtf8().constData() ) + "/" + "ui_" + projectFiles.projectName + ".xml"; - - WidgetSerializer widgetSerializer; - 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!" ) ); + currentProjectFile = _lastDir; + currentProjectFile += "/"; + currentProjectFile += projectFiles.projectName.c_str(); + currentProjectFile += ".xml"; + save(); } void GUIEditorWindow::reset() diff --git a/code/studio/src/plugins/gui_editor/project_file_parser.cpp b/code/studio/src/plugins/gui_editor/project_file_parser.cpp index a5646c963..a10f5d783 100644 --- a/code/studio/src/plugins/gui_editor/project_file_parser.cpp +++ b/code/studio/src/plugins/gui_editor/project_file_parser.cpp @@ -63,7 +63,7 @@ namespace GUIEditor unsigned long CProjectFileParser::getProjectVersion() const { if( !loaded ) - return OLD; + return SProjectFiles::OLD; return files.version; } @@ -71,7 +71,7 @@ namespace GUIEditor void CProjectFileParser::clear() { files.projectName = ""; - files.version = OLD; + files.version = SProjectFiles::OLD; files.activeGroup = ""; files.guiFiles.clear(); files.mapFiles.clear(); diff --git a/code/studio/src/plugins/gui_editor/project_file_serializer.cpp b/code/studio/src/plugins/gui_editor/project_file_serializer.cpp index 4eb442d3f..0b34f511b 100644 --- a/code/studio/src/plugins/gui_editor/project_file_serializer.cpp +++ b/code/studio/src/plugins/gui_editor/project_file_serializer.cpp @@ -25,7 +25,7 @@ namespace GUIEditor if( fileName.empty() ) return false; - if( project.version >= MAX_PROJECTFILE_VERSION ) + if( project.version >= SProjectFiles::MAX_PROJECTFILE_VERSION ) return false; out.open( fileName.c_str() ); diff --git a/code/studio/src/plugins/gui_editor/project_files.h b/code/studio/src/plugins/gui_editor/project_files.h index 4ae58493b..786bd22e4 100644 --- a/code/studio/src/plugins/gui_editor/project_files.h +++ b/code/studio/src/plugins/gui_editor/project_files.h @@ -23,16 +23,17 @@ namespace GUIEditor { - enum ProjectVersion - { - OLD = 0, - NEW = 1, - MAX_PROJECTFILE_VERSION - }; - struct SProjectFiles { public: + + enum ProjectVersion + { + OLD = 0, + NEW = 1, + MAX_PROJECTFILE_VERSION + }; + std::string projectName; unsigned long version; std::string masterGroup;