]> SALOME platform Git repositories - modules/shaper.git/commitdiff
Salome HOME
Construction of vertices/edges/faces on the base of sketch: Widget Creator Factory
authornds <nds@opencascade.com>
Fri, 4 Dec 2015 04:18:16 +0000 (07:18 +0300)
committerdbv <dbv@opencascade.com>
Tue, 8 Dec 2015 08:50:41 +0000 (11:50 +0300)
src/ModuleBase/CMakeLists.txt
src/ModuleBase/ModuleBase_IWidgetCreator.h
src/ModuleBase/ModuleBase_WidgetCreatorFactory.cpp [new file with mode: 0755]
src/ModuleBase/ModuleBase_WidgetCreatorFactory.h [new file with mode: 0755]
src/ModuleBase/ModuleBase_WidgetFactory.cpp
src/SketchShapePlugin/SketchShapePlugin_Plugin.cpp
src/SketchShapePlugin/SketchShapePlugin_WidgetCreator.cpp
src/SketchShapePlugin/SketchShapePlugin_WidgetCreator.h

index ea029d5bd10aca574a98ece6176dd9cd94d03773..304feb69c80f146661e1e9d65a651eb9f4c20403 100644 (file)
@@ -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
index dfff4fa71ba20e762e93dd89ebbbf2bfe4319c29..d544ecfe70f69951e18e2c5c71bca55f3e495191 100755 (executable)
@@ -36,5 +36,7 @@ public:
                                                      QWidget* theParent = NULL) = 0;
 };
 
+typedef std::shared_ptr<ModuleBase_IWidgetCreator> 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 (executable)
index 0000000..ff3ee01
--- /dev/null
@@ -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 <ModuleBase_WidgetCreatorFactory.h>
+#include <ModuleBase_IWidgetCreator.h>
+
+#include <Events_Error.h>
+
+#include <QStringList>
+
+/// Factory instance that will be initialized by first getting, one per application
+std::shared_ptr<ModuleBase_WidgetCreatorFactory> MY_WIDGET_CREATOR_FACTORY;
+
+std::shared_ptr<ModuleBase_WidgetCreatorFactory> ModuleBase_WidgetCreatorFactory::get()
+{
+  if (!MY_WIDGET_CREATOR_FACTORY) {
+    MY_WIDGET_CREATOR_FACTORY =  std::shared_ptr<ModuleBase_WidgetCreatorFactory>(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<std::string>& aTypes = theCreator->widgetTypes();
+  std::set<std::string>::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 (executable)
index 0000000..40db320
--- /dev/null
@@ -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 <ModuleBase.h>
+
+#include <memory>
+#include <string>
+
+#include <QMap>
+
+#include <ModuleBase_IWidgetCreator.h>
+
+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<ModuleBase_WidgetCreatorFactory> 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<std::string, WidgetCreatorPtr> myModelWidgets;
+};
+
+typedef std::shared_ptr<ModuleBase_WidgetCreatorFactory> WidgetCreatorFactoryPtr;
+
+#endif /* MODULEBASE_WIDGETCREATORFACTORY_H_ */
index bedab8cb3d1856681c88eb3140678b9c0f70e926..dd124111185c05c0e5e379ae5ea0eac684193575 100644 (file)
@@ -31,6 +31,7 @@
 #include <ModuleBase_PageGroupBox.h>
 #include <ModuleBase_PageWidget.h>
 #include <ModuleBase_WidgetExprEditor.h>
+#include <ModuleBase_WidgetCreatorFactory.h>
 
 #include <ModelAPI_Validator.h>
 #include <ModelAPI_Session.h>
@@ -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;
 }
 
index 00c5a360460cf63a54bde07d33b642ece088c73f..751e41b82d4cb9d17019b52b7ce765001f07947f 100755 (executable)
@@ -15,6 +15,9 @@
 #include <ModelAPI_Session.h>
 #include <ModelAPI_Document.h>
 #include <ModelAPI_Validator.h>
+
+#include <ModuleBase_WidgetCreatorFactory.h>
+#include <SketchShapePlugin_WidgetCreator.h>
 /*#include <ModelAPI_Data.h>
 
 #include <ModuleBase_ModelWidget.h>
@@ -23,7 +26,7 @@
 #include <memory>*/
 
 // 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<SketchShapePlugin_WidgetCreator>(new SketchShapePlugin_WidgetCreator()));
+
   // register this plugin
   ModelAPI_Session::get()->registerPlugin(this);
 }
index 89552f187a47e4d60f53db5cf148e148da4f1ce5..5c01de0632ed1ccd14d73b5f8a4788ecccf59019 100755 (executable)
@@ -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;
 }
index a812b01a2ad82331de148bdc0c5480e985d8cffc..fe4a442217d11234157c3274cd40edeb2f577d37 100755 (executable)
@@ -38,5 +38,6 @@ private:
   std::set<std::string> myTypes; /// types of widgets
 };
 
+typedef std::shared_ptr<SketchShapePlugin_WidgetCreator> SketchShapePlguinWidgetCreatorPtr;
 
 #endif