Salome HOME
Support of wide string
[modules/shaper.git] / src / Model / Model_AttributeSelection.h
1 // Copyright (C) 2014-2019  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #ifndef Model_AttributeSelection_H_
21 #define Model_AttributeSelection_H_
22
23 #include "Model.h"
24 #include "Model_AttributeReference.h"
25 #include <ModelAPI_AttributeSelection.h>
26 #include <Selector_NameGenerator.h>
27 #include <TDF_LabelMap.hxx>
28 #include <TopoDS_Shape.hxx>
29 #include <TopTools_ListOfShape.hxx>
30
31 class Model_AttributeSelectionList;
32 class Model_Document;
33
34 /**\class Model_AttributeSelection
35  * \ingroup DataModel
36  * \brief Attribute that contains reference to the sub-shape of some result, the selected shape.
37  */
38 class Model_AttributeSelection : public ModelAPI_AttributeSelection,
39                                  public Selector_NameGenerator
40 {
41   Model_AttributeReference myRef;  ///< The reference functionality re-usage
42   /// temporarily storages to avoid keeping in the data structure if not needed
43   ObjectPtr myTmpContext;
44   /// temporarily storages to avoid keeping in the data structure if not needed
45   std::shared_ptr<GeomAPI_Shape> myTmpSubShape;
46   /// temporarily storages to avoid keeping in the data structure if not needed
47   CenterType myTmpCenterType;
48   /// Reference to the parent attribute, if any (to split selection compounds in issue 1799)
49   Model_AttributeSelectionList* myParent;
50
51   std::shared_ptr<Model_Document> myRestoreDocument; // current document to restore by name
52   /// If true attribute selects geometry instead of shape.
53   bool myIsGeometricalSelection;
54
55 public:
56   /// Defines the result and its selected sub-shape
57   /// \param theContext object where the sub-shape was selected
58   /// \param theSubShape selected sub-shape (if null, the whole context is selected)
59   /// \param theTemporarily if it is true, do not store and name the added in the data framework
60   ///           (used to remove immediately, without the following updates)
61   /// \returns true if attribute was updated
62   MODEL_EXPORT virtual bool setValue(
63     const ObjectPtr& theContext, const std::shared_ptr<GeomAPI_Shape>& theSubShape,
64     const bool theTemporarily = false);
65
66   /// Same as SetValue, but it takes an edge (on circular or elliptic curve)
67   /// and stores the vertex of the central point (for ellipse the first or the second focus point)
68   MODEL_EXPORT virtual void setValueCenter(
69     const ObjectPtr& theContext, const std::shared_ptr<GeomAPI_Edge>& theEdge,
70     const CenterType theCenterType,
71     const bool theTemporarily = false);
72
73   /// Makes this selection attribute selects the same as in theSource selection
74   MODEL_EXPORT virtual void selectValue(
75     const std::shared_ptr<ModelAPI_AttributeSelection>& theSource);
76
77   /// Reset temporary stored values
78   virtual void removeTemporaryValues();
79
80   /// Returns the selected subshape
81   MODEL_EXPORT virtual std::shared_ptr<GeomAPI_Shape> value();
82
83   /// Returns the context of the selection (the whole shape owner)
84   MODEL_EXPORT virtual ResultPtr context();
85
86   /// Returns the context of the selection if the whole feature was selected
87   MODEL_EXPORT virtual FeaturePtr contextFeature();
88   /// Returns the context of the selection : result or feature
89   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Object> contextObject();
90
91   /// Sets the feature object
92   MODEL_EXPORT virtual void setObject(const std::shared_ptr<ModelAPI_Object>& theObject);
93
94   /// Updates the selection due to the changes in the referenced objects
95   /// \returns false if update is failed
96   MODEL_EXPORT virtual bool update();
97
98   /// Returns a textual string of the selection
99   /// \param theDefaultValue a name, which is returned if the naming name can not be obtained
100   MODEL_EXPORT virtual std::wstring namingName(const std::wstring& theDefaultValue = L"");
101
102   /// Defines the sub-shape by Id
103   MODEL_EXPORT virtual void setId(int theID);
104
105   /// Selects (i.e. creates Naming data structure) of sub-shape specified by textual name
106   MODEL_EXPORT virtual void selectSubShape(const std::string& theType,
107                                            const std::wstring& theSubShapeName);
108
109   /// Selects sub-shape by its inner point
110   MODEL_EXPORT virtual void selectSubShape(const std::string& theType,
111                                            const std::shared_ptr<GeomAPI_Pnt>& thePoint);
112   /// Selects sub-shape by weak naming index
113   MODEL_EXPORT virtual void selectSubShape(const std::string& theType,
114     const std::wstring& theContextName, const int theIndex);
115
116   /// Returns true if attribute was  initialized by some value
117   MODEL_EXPORT virtual bool isInitialized();
118
119   /// Returns true if recompute of selection become impossible
120   MODEL_EXPORT virtual bool isInvalid();
121
122   /// Updates the arguments of selection if something was affected by creation
123   /// or reorder of features upper in the history line (issue #1757)
124   /// Returns theRemove true if this attribute must be removed (become deleted)
125   MODEL_EXPORT virtual void updateInHistory(bool& theRemove);
126
127   // Implementation of the name generator method from the Selector package
128   // This method returns the context name by the label of the sub-selected shape
129   MODEL_EXPORT virtual std::wstring contextName(const TDF_Label theSelectionLab) override;
130
131   /// This method restores by the context and value name the context label and
132   /// sub-label where the value is. Returns true if it is valid.
133   MODEL_EXPORT virtual bool restoreContext(std::wstring theName,
134     TDF_Label& theContext, TDF_Label& theValue) override;
135
136   /// Returns true if the first result is newer than the second one in the tree of features
137   MODEL_EXPORT virtual bool isLater(const TDF_Label theResult1, const TDF_Label theResult2)
138     const override;
139
140   /// Returns the name by context. Adds the part name if the context is located in other document
141   MODEL_EXPORT virtual std::wstring contextName(const ResultPtr& theContext) const;
142
143   /// Makes the current local selection becomes all sub-shapes with same base geometry.
144   MODEL_EXPORT virtual void combineGeometrical();
145
146   /// Resets attribute to deafult state
147   MODEL_EXPORT virtual void reset();
148
149 protected:
150   /// Objects are created for features automatically
151   MODEL_EXPORT Model_AttributeSelection(TDF_Label& theLabel);
152
153   /// Returns the selected subshape, internal method that works without knowledge
154   /// about special selection of circle and ellipse focuses (for that the public value
155   /// method calls this and makes additional processing).
156   /// Returns theType type of the center, or NOT_CENTER if it is not.
157   std::shared_ptr<GeomAPI_Shape> internalValue(CenterType& theType);
158
159   /// Performs the selection for the body result (TNaming selection)
160   virtual void selectBody(
161     const ResultPtr& theContext, const std::shared_ptr<GeomAPI_Shape>& theSubShape);
162
163   /// Performs the selection for the part result (selection by name of body result inside of part)
164   /// \param theContext the result - owner of the selection
165   /// \param theSubShape selected shape
166   /// \param theUpdate flag that shows that it must be just update, theShape is null
167   /// \returns true if everything is selected correctly
168   virtual bool selectPart(
169     const ResultPtr& theContext, const std::shared_ptr<GeomAPI_Shape>& theSubShape,
170     const bool theUpdate = false);
171
172   /// Returns the label where TNaming_Selection results are stored
173   /// Note: there must be no attributes stored at the same label because Selector clears this lab
174   TDF_Label selectionLabel();
175
176   /// Sets the ID of the attribute in Data (called from Data): here it is used for myRef ID setting
177   MODEL_EXPORT virtual void setID(const std::string theID);
178
179   /// Sets the parent attribute
180   void setParent(Model_AttributeSelectionList* theParent);
181
182   /// Splits theNewShape into sub-shapes of theType type (for the list parent of this attribute)
183   void split(ResultPtr theContext, TopoDS_Shape theNewShape, TopAbs_ShapeEnum theType);
184
185   /// When group position is updated, searches the new context and new values
186   bool searchNewContext(std::shared_ptr<Model_Document> theDoc, const TopoDS_Shape theContShape,
187                         ResultPtr theContext, TopoDS_Shape theValShape, TDF_Label theAccessLabel,
188                         std::list<ResultPtr>& theResults, TopTools_ListOfShape& theValShapes);
189
190   /// Searches for the newest context, modification of the current, that contains theValue
191   ResultPtr newestContext(
192     const ResultPtr theCurrent, const std::shared_ptr<GeomAPI_Shape> theValue);
193
194   /// computes theShapes list - shapes that were generated/modified/deleted the theValShape
195   /// during creation from new to old context
196   void computeValues(ResultPtr theOldContext, ResultPtr theNewContext, TopoDS_Shape theValShape,
197     TopTools_ListOfShape& theShapes);
198
199   /// Returns true if is geometrical selection.
200   virtual bool isGeometricalSelection() const {
201     return myIsGeometricalSelection;
202   };
203
204   /// Returns the module document label if this selection attribute is not in this document.
205   /// Returns null label otherwise.
206   TDF_Label baseDocumentLab();
207
208   /// Returns features that conceals theFeature and located in history before theStop
209   /// theResultOfFeature if not null defines exact referenced result of a feature
210   void concealedFeature(
211     const FeaturePtr theFeature, const FeaturePtr theStop, const bool theCheckCopy,
212     std::list<FeaturePtr>& theConcealers, const ResultPtr theResultOfFeature);
213
214   friend class Model_Data;
215   friend class Model_AttributeSelectionList;
216 };
217
218 #endif