From: nds Date: Fri, 4 Dec 2015 04:18:16 +0000 (+0300) Subject: Construction of vertices/edges/faces on the base of sketch: Widget Creator Factory X-Git-Tag: V_2.1.0~216 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=eaedfec1bd44832c5c1b75265a034d5b79340420;p=modules%2Fshaper.git Construction of vertices/edges/faces on the base of sketch: Widget Creator Factory --- diff --git a/src/ModuleBase/CMakeLists.txt b/src/ModuleBase/CMakeLists.txt index ea029d5bd..304feb69c 100644 --- a/src/ModuleBase/CMakeLists.txt +++ b/src/ModuleBase/CMakeLists.txt @@ -4,7 +4,7 @@ INCLUDE(Common) SET(CMAKE_AUTOMOC ON) SET(PROJECT_HEADERS - ModuleBase.h + ModuleBase.h ModuleBase_ActionInfo.h ModuleBase_Definitions.h ModuleBase_DoubleSpinBox.h @@ -40,6 +40,7 @@ SET(PROJECT_HEADERS ModuleBase_ViewerPrs.h ModuleBase_WidgetBoolValue.h ModuleBase_WidgetChoice.h + ModuleBase_WidgetCreatorFactory.h ModuleBase_WidgetDoubleValue.h ModuleBase_WidgetEditor.h ModuleBase_WidgetExprEditor.h @@ -92,6 +93,7 @@ SET(PROJECT_SOURCES ModuleBase_ViewerPrs.cpp ModuleBase_WidgetBoolValue.cpp ModuleBase_WidgetChoice.cpp + ModuleBase_WidgetCreatorFactory.cpp ModuleBase_WidgetDoubleValue.cpp ModuleBase_WidgetEditor.cpp ModuleBase_WidgetExprEditor.cpp diff --git a/src/ModuleBase/ModuleBase_IWidgetCreator.h b/src/ModuleBase/ModuleBase_IWidgetCreator.h index dfff4fa71..d544ecfe7 100755 --- a/src/ModuleBase/ModuleBase_IWidgetCreator.h +++ b/src/ModuleBase/ModuleBase_IWidgetCreator.h @@ -36,5 +36,7 @@ public: QWidget* theParent = NULL) = 0; }; +typedef std::shared_ptr WidgetCreatorPtr; + #endif \ No newline at end of file diff --git a/src/ModuleBase/ModuleBase_WidgetCreatorFactory.cpp b/src/ModuleBase/ModuleBase_WidgetCreatorFactory.cpp new file mode 100755 index 000000000..ff3ee01f9 --- /dev/null +++ b/src/ModuleBase/ModuleBase_WidgetCreatorFactory.cpp @@ -0,0 +1,59 @@ +// Copyright (C) 2014-20xx CEA/DEN, EDF R&D + +// File: ModuleBase_WidgetCreatorFactory.cpp +// Created: 03 Dec 2015 +// Author: Natalia ERMOLAEVA + +#include +#include + +#include + +#include + +/// Factory instance that will be initialized by first getting, one per application +std::shared_ptr MY_WIDGET_CREATOR_FACTORY; + +std::shared_ptr ModuleBase_WidgetCreatorFactory::get() +{ + if (!MY_WIDGET_CREATOR_FACTORY) { + MY_WIDGET_CREATOR_FACTORY = std::shared_ptr(new ModuleBase_WidgetCreatorFactory()); + } + return MY_WIDGET_CREATOR_FACTORY; +} + +ModuleBase_WidgetCreatorFactory::ModuleBase_WidgetCreatorFactory() +{ +} + +ModuleBase_WidgetCreatorFactory::~ModuleBase_WidgetCreatorFactory() +{ +} + +void ModuleBase_WidgetCreatorFactory::registerCreator(const WidgetCreatorPtr& theCreator) +{ + const std::set& aTypes = theCreator->widgetTypes(); + std::set::const_iterator anIt = aTypes.begin(), aLast = aTypes.end(); + for (; anIt != aLast; anIt++) { + std::string aKey = *anIt; + if (!myModelWidgets.contains(aKey)) + myModelWidgets[aKey] = theCreator; + else { + Events_Error::send("The" + aKey + " widget XML definition has been already\ + used by another widget creator"); + } + } +} + +ModuleBase_ModelWidget* ModuleBase_WidgetCreatorFactory::createWidgetByType( + const std::string& theType, QWidget* theParent) +{ + ModuleBase_ModelWidget* aWidget = 0; + + if (myModelWidgets.contains(theType)) { + WidgetCreatorPtr aCreator = myModelWidgets[theType]; + aWidget = aCreator->createWidgetByType(theType, theParent); + } + + return aWidget; +} diff --git a/src/ModuleBase/ModuleBase_WidgetCreatorFactory.h b/src/ModuleBase/ModuleBase_WidgetCreatorFactory.h new file mode 100755 index 000000000..40db32008 --- /dev/null +++ b/src/ModuleBase/ModuleBase_WidgetCreatorFactory.h @@ -0,0 +1,57 @@ +// Copyright (C) 2014-20xx CEA/DEN, EDF R&D + +// File: ModuleBase_WidgetCreatorFactory.cpp +// Created: 03 Dec 2015 +// Author: Natalia ERMOLAEVA + +#ifndef MODULEBASE_WIDGETCREATORFACTORY_H_ +#define MODULEBASE_WIDGETCREATORFACTORY_H_ + +#include + +#include +#include + +#include + +#include + +class ModuleBase_ModelWidget; + +class QWidget; + +/** +* \ingroup GUI +* A class for creation of widgets instances in for property panel using XML deskription of +* a feature +*/ +class MODULEBASE_EXPORT ModuleBase_WidgetCreatorFactory +{ + public: + // Returns an singular instance of the class if it exists or create it + static std::shared_ptr get(); + + /// Destructor + virtual ~ModuleBase_WidgetCreatorFactory(); + + /// The creator is added to the internal container to be used when the createWidgetByType is called + /// \param theCreator a new widget creator + void registerCreator(const WidgetCreatorPtr& theCreator); + + /// Create widget by its type + /// \param theType a type + /// \param theParent a parent widget + ModuleBase_ModelWidget* createWidgetByType(const std::string& theType, + QWidget* theParent = NULL); + +private: + /// Constructor is hidden + ModuleBase_WidgetCreatorFactory(); + + /// List of created model widgets + QMap myModelWidgets; +}; + +typedef std::shared_ptr WidgetCreatorFactoryPtr; + +#endif /* MODULEBASE_WIDGETCREATORFACTORY_H_ */ diff --git a/src/ModuleBase/ModuleBase_WidgetFactory.cpp b/src/ModuleBase/ModuleBase_WidgetFactory.cpp index bedab8cb3..dd1241111 100644 --- a/src/ModuleBase/ModuleBase_WidgetFactory.cpp +++ b/src/ModuleBase/ModuleBase_WidgetFactory.cpp @@ -31,6 +31,7 @@ #include #include #include +#include #include #include @@ -159,15 +160,16 @@ ModuleBase_ModelWidget* ModuleBase_WidgetFactory::createWidgetByType(const std:: result = NULL; } else { result = myWorkshop->module()->createWidgetByType(theType, theParent, myWidgetApi, myParentId); + if (!result) + result = ModuleBase_WidgetCreatorFactory::get()->createWidgetByType(theType, theParent); #ifdef _DEBUG if (!result) { qDebug("ModuleBase_WidgetFactory::fillWidget: find bad widget type %s", theType.c_str()); } #endif } - if (result) { + if (result) myModelWidgets.append(result); - } return result; } diff --git a/src/SketchShapePlugin/SketchShapePlugin_Plugin.cpp b/src/SketchShapePlugin/SketchShapePlugin_Plugin.cpp index 00c5a3604..751e41b82 100755 --- a/src/SketchShapePlugin/SketchShapePlugin_Plugin.cpp +++ b/src/SketchShapePlugin/SketchShapePlugin_Plugin.cpp @@ -15,6 +15,9 @@ #include #include #include + +#include +#include /*#include #include @@ -23,7 +26,7 @@ #include */ // the only created instance of this plugin -//static SketchShapePlugin_Plugin* MY_SKETCH_SHAPE_INSTANCE = new SketchShapePlugin_Plugin(); +static SketchShapePlugin_Plugin* MY_SKETCH_SHAPE_INSTANCE = new SketchShapePlugin_Plugin(); SketchShapePlugin_Plugin::SketchShapePlugin_Plugin() { @@ -32,6 +35,10 @@ SketchShapePlugin_Plugin::SketchShapePlugin_Plugin() aFactory->registerValidator("SketchShapePlugin_FeatureValidator", new SketchShapePlugin_FeatureValidator); + WidgetCreatorFactoryPtr aWidgetCreatorFactory = ModuleBase_WidgetCreatorFactory::get(); + aWidgetCreatorFactory->registerCreator( + std::shared_ptr(new SketchShapePlugin_WidgetCreator())); + // register this plugin ModelAPI_Session::get()->registerPlugin(this); } diff --git a/src/SketchShapePlugin/SketchShapePlugin_WidgetCreator.cpp b/src/SketchShapePlugin/SketchShapePlugin_WidgetCreator.cpp index 89552f187..5c01de063 100755 --- a/src/SketchShapePlugin/SketchShapePlugin_WidgetCreator.cpp +++ b/src/SketchShapePlugin/SketchShapePlugin_WidgetCreator.cpp @@ -18,13 +18,12 @@ ModuleBase_ModelWidget* SketchShapePlugin_WidgetCreator::createWidgetByType( const std::string& theType, QWidget* theParent) { ModuleBase_ModelWidget* aWidget = 0; - if (myTypes.find(theType) != myTypes.end()) + if (myTypes.find(theType) == myTypes.end()) return aWidget; - if (theType == "sketchshape_groupbox") { - //aWidget = - new SketchShapePlugin_PageGroupBox(theParent); - } + //if (theType == "sketchshape_groupbox") { + // aWidget = new SketchShapePlugin_PageGroupBox(theParent); + //} return aWidget; } diff --git a/src/SketchShapePlugin/SketchShapePlugin_WidgetCreator.h b/src/SketchShapePlugin/SketchShapePlugin_WidgetCreator.h index a812b01a2..fe4a44221 100755 --- a/src/SketchShapePlugin/SketchShapePlugin_WidgetCreator.h +++ b/src/SketchShapePlugin/SketchShapePlugin_WidgetCreator.h @@ -38,5 +38,6 @@ private: std::set myTypes; /// types of widgets }; +typedef std::shared_ptr SketchShapePlguinWidgetCreatorPtr; #endif