From 4c43870a4781f8bc15546e8614d18256334972e5 Mon Sep 17 00:00:00 2001 From: dfighter1985 Date: Fri, 10 Aug 2012 01:21:20 +0200 Subject: [PATCH] ADDED: #1471 CWidgetSerializer class, that serializes the widgets into a single XML file. Also CInterfaceElement fields can now be serializes. --HG-- branch : gsoc2012-gui-editor --- code/nel/include/nel/gui/interface_element.h | 3 + code/nel/src/gui/interface_element.cpp | 38 ++++++++ .../plugins/gui_editor/gui_editor_window.cpp | 17 +++- .../plugins/gui_editor/widget_serializer.cpp | 92 +++++++++++++++++++ .../plugins/gui_editor/widget_serializer.h | 48 ++++++++++ 5 files changed, 197 insertions(+), 1 deletion(-) create mode 100644 code/nel/tools/3d/object_viewer_qt/src/plugins/gui_editor/widget_serializer.cpp create mode 100644 code/nel/tools/3d/object_viewer_qt/src/plugins/gui_editor/widget_serializer.h diff --git a/code/nel/include/nel/gui/interface_element.h b/code/nel/include/nel/gui/interface_element.h index a9e0595b4..7610ccff8 100644 --- a/code/nel/include/nel/gui/interface_element.h +++ b/code/nel/include/nel/gui/interface_element.h @@ -127,6 +127,8 @@ namespace NLGUI virtual void setProperty( const std::string &name, const std::string &value ); + virtual bool serialize( xmlNodePtr parentNode, const char *type ) const; + /// Parse the element and initalize it virtual bool parse (xmlNodePtr cur, CInterfaceGroup *parentGroup); @@ -292,6 +294,7 @@ namespace NLGUI // Parse tools static std::string HotSpotToString( THotSpot spot ); + static std::string HotSpotCoupleToString( THotSpot parentPosRef, THotSpot posRef ); static THotSpot convertHotSpot (const char *ptr); // static void convertHotSpotCouple (const char *ptr, THotSpot &parentPosRef, THotSpot &posRef); static NLMISC::CRGBA convertColor (const char *ptr); diff --git a/code/nel/src/gui/interface_element.cpp b/code/nel/src/gui/interface_element.cpp index 8660ce080..da466d529 100644 --- a/code/nel/src/gui/interface_element.cpp +++ b/code/nel/src/gui/interface_element.cpp @@ -259,6 +259,34 @@ namespace NLGUI nlwarning( "Tried to set invalid property '%s' for widget '%s'", name.c_str(), _Id.c_str() ); } + bool CInterfaceElement::serialize( xmlNodePtr parentNode, const char *type ) const + { + xmlNodePtr node = xmlNewNode( NULL, BAD_CAST type ); + if( node == NULL ) + return false; + + xmlAddChild( parentNode, node ); + + xmlNewProp( node, BAD_CAST "id", BAD_CAST stripId( getId() ).c_str() ); + xmlNewProp( node, BAD_CAST "active", BAD_CAST toString( _Active ).c_str() ); + xmlNewProp( node, BAD_CAST "x", BAD_CAST toString( _X ).c_str() ); + xmlNewProp( node, BAD_CAST "y", BAD_CAST toString( _Y ).c_str() ); + xmlNewProp( node, BAD_CAST "w", BAD_CAST toString( _W ).c_str() ); + xmlNewProp( node, BAD_CAST "h", BAD_CAST toString( _H ).c_str() ); + xmlNewProp( node, BAD_CAST "posref", BAD_CAST HotSpotCoupleToString( _ParentPosRef, _PosRef ).c_str() ); + xmlNewProp( node, BAD_CAST "posparent", + BAD_CAST CWidgetManager::getInstance()->getParser()->getParentPosAssociation( (CInterfaceElement*)this ).c_str() ); + xmlNewProp( node, BAD_CAST "sizeref", BAD_CAST getSizeRefAsString().c_str() ); + xmlNewProp( node, BAD_CAST "sizeparent", + BAD_CAST CWidgetManager::getInstance()->getParser()->getParentSizeAssociation( (CInterfaceElement*)this ).c_str() ); + + xmlNewProp( node, BAD_CAST "global_color", BAD_CAST toString( _ModulateGlobalColor ).c_str() ); + xmlNewProp( node, BAD_CAST "render_layer", BAD_CAST toString( _RenderLayer ).c_str() ); + xmlNewProp( node, BAD_CAST "avoid_resize_parent", BAD_CAST toString( _AvoidResizeParent ).c_str() ); + + return true; + } + // ------------------------------------------------------------------------------------------------ bool CInterfaceElement::parse(xmlNodePtr cur, CInterfaceGroup * parentGroup) { @@ -884,6 +912,16 @@ namespace NLGUI return ""; } + std::string CInterfaceElement::HotSpotCoupleToString( THotSpot parentPosRef, THotSpot posRef ) + { + std::string hs; + hs = HotSpotToString( parentPosRef ); + hs += " "; + hs += HotSpotToString( posRef ); + + return hs; + } + // ------------------------------------------------------------------------------------------------ THotSpot CInterfaceElement::convertHotSpot (const char *ptr) { diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/gui_editor/gui_editor_window.cpp b/code/nel/tools/3d/object_viewer_qt/src/plugins/gui_editor/gui_editor_window.cpp index 3f8f8c823..d615f078c 100644 --- a/code/nel/tools/3d/object_viewer_qt/src/plugins/gui_editor/gui_editor_window.cpp +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/gui_editor/gui_editor_window.cpp @@ -33,6 +33,7 @@ #include "widget_properties.h" #include "widget_properties_parser.h" #include "widget_hierarchy.h" +#include "widget_serializer.h" #include "link_list.h" #include "proc_list.h" #include "project_file_parser.h" @@ -214,7 +215,9 @@ namespace GUIEditor projectFiles.guiFiles.push_back( "ui_" + projectFiles.projectName + ".xml" ); projectFiles.version = NEW; - QString newFile = dir + "/" + projectFiles.projectName.c_str() + ".xml"; + QString newFile = + dir + "/" + projectFiles.projectName.c_str() + ".xml"; + CProjectFileSerializer serializer; serializer.setFile( newFile.toStdString() ); if( !serializer.serialize( projectFiles ) ) @@ -225,6 +228,18 @@ namespace GUIEditor return; } + std::string guiFile = + dir.toStdString() + "/" + "ui_" + projectFiles.projectName + ".xml"; + + WidgetSerializer widgetSerializer; + widgetSerializer.setFile( guiFile ); + 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::close() diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/gui_editor/widget_serializer.cpp b/code/nel/tools/3d/object_viewer_qt/src/plugins/gui_editor/widget_serializer.cpp new file mode 100644 index 000000000..55dffa7d7 --- /dev/null +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/gui_editor/widget_serializer.cpp @@ -0,0 +1,92 @@ +// Object Viewer Qt GUI Editor plugin +// Copyright (C) 2010 Winch Gate Property Limited +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program 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 Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + + +#include "widget_serializer.h" +#include "nel/gui/interface_group.h" +#include "nel/gui/widget_manager.h" + +namespace GUIEditor +{ + bool WidgetSerializer::serialize( const std::string &masterGroup ) + { + if( fileName.empty() ) + return false; + + CInterfaceGroup *mg = + CWidgetManager::getInstance()->getMasterGroupFromId( masterGroup ); + + if( mg == NULL ) + return false; + + out.open( fileName.c_str() ); + if( !out.is_open() ) + return false; + + xmlNodePtr root = xmlNewNode( NULL, BAD_CAST "interface_config" ); + if( root == NULL ) + { + out.close(); + return false; + } + + if( !mg->serialize( root, "root" ) ) + { + xmlFreeNode( root ); + out.close(); + return false; + } + + serializeTree( root ); + + + xmlFreeNode( root ); + out.close(); + + return true; + } + + bool WidgetSerializer::serializeTree( _xmlNode *node ) + { + out << "<" << node->name; + + xmlAttrPtr prop = node->properties; + while( prop != NULL ) + { + std::string name = std::string( (const char*)prop->name ); + std::string value = std::string( (const char*)xmlGetProp( node, BAD_CAST name.c_str() ) ); + out << " " << name << "=\"" << value << "\"" << std::endl; + + prop = prop->next; + } + + out << ">" << std::endl; + + xmlNodePtr child = node->children; + while( child != NULL ) + { + serializeTree( child ); + child = child->next; + } + + out << "name << ">" << std::endl; + + return true; + } +} + + + diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/gui_editor/widget_serializer.h b/code/nel/tools/3d/object_viewer_qt/src/plugins/gui_editor/widget_serializer.h new file mode 100644 index 000000000..16f88cdd4 --- /dev/null +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/gui_editor/widget_serializer.h @@ -0,0 +1,48 @@ +// Object Viewer Qt GUI Editor plugin +// Copyright (C) 2010 Winch Gate Property Limited +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program 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 Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + + +#ifndef WIDGET_SERIALIZER_H +#define WIDGET_SERIALIZER_H + +#include +#include + +struct _xmlNode; + +namespace GUIEditor +{ + + class WidgetSerializer + { + public: + WidgetSerializer(){} + ~WidgetSerializer(){} + + void setFile( const std::string &name ){ fileName = name; } + bool serialize( const std::string &masterGroup ); + + private: + bool serializeTree( _xmlNode *node ); + + std::string fileName; + std::ofstream out; + }; + +} + +#endif +