0031682: Visualization - Prs3d_ShadingAspect::SetTransparency() has no effect with...
[occt.git] / src / XCAFPrs / XCAFPrs_Style.hxx
1 // Created on: 2000-08-11
2 // Created by: Andrey BETENEV
3 // Copyright (c) 2000-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #ifndef _XCAFPrs_Style_HeaderFile
17 #define _XCAFPrs_Style_HeaderFile
18
19 #include <Standard.hxx>
20 #include <Standard_DefineAlloc.hxx>
21 #include <Standard_Handle.hxx>
22 #include <Quantity_ColorRGBAHasher.hxx>
23 #include <XCAFDoc_VisMaterial.hxx>
24
25 //! Represents a set of styling settings applicable to a (sub)shape
26 class XCAFPrs_Style 
27 {
28 public:
29
30   DEFINE_STANDARD_ALLOC
31
32   //! Empty constructor - colors are unset, visibility is TRUE.
33   Standard_EXPORT XCAFPrs_Style();
34
35   //! Return TRUE if style is empty - does not override any properties.
36   Standard_Boolean IsEmpty() const
37   {
38     return !myHasColorSurf
39         && !myHasColorCurv
40         &&  myMaterial.IsNull()
41         &&  myIsVisible;
42   }
43
44   //! Return material.
45   const Handle(XCAFDoc_VisMaterial)& Material() const { return myMaterial; }
46
47   //! Set material.
48   void SetMaterial (const Handle(XCAFDoc_VisMaterial)& theMaterial) { myMaterial = theMaterial; }
49
50   //! Return TRUE if surface color has been defined.
51   Standard_Boolean IsSetColorSurf() const { return myHasColorSurf; }
52
53   //! Return surface color.
54   const Quantity_Color& GetColorSurf() const { return myColorSurf.GetRGB(); }
55
56   //! Set surface color.
57   void SetColorSurf (const Quantity_Color& theColor) { SetColorSurf  (Quantity_ColorRGBA (theColor)); }
58
59   //! Return surface color.
60   const Quantity_ColorRGBA& GetColorSurfRGBA() const { return myColorSurf; }
61
62   //! Set surface color.
63   Standard_EXPORT void SetColorSurf  (const Quantity_ColorRGBA& theColor);
64
65   //! Manage surface color setting
66   Standard_EXPORT void UnSetColorSurf();
67   
68   //! Return TRUE if curve color has been defined.
69   Standard_Boolean IsSetColorCurv() const { return myHasColorCurv; }
70
71   //! Return curve color.
72   const Quantity_Color& GetColorCurv() const { return myColorCurv; }
73
74   //! Set curve color.
75   Standard_EXPORT void SetColorCurv (const Quantity_Color& col);
76   
77   //! Manage curve color setting
78   Standard_EXPORT void UnSetColorCurv();
79
80   //! Assign visibility.
81   void SetVisibility (const Standard_Boolean theVisibility) { myIsVisible = theVisibility; }
82
83   //! Manage visibility.
84   Standard_Boolean IsVisible() const { return myIsVisible; }
85
86   //! Returns True if styles are the same
87   //! Methods for using Style as key in maps
88   Standard_Boolean IsEqual (const XCAFPrs_Style& theOther) const
89   {
90     if (myIsVisible != theOther.myIsVisible)
91     {
92       return false;
93     }
94     else if (!myIsVisible)
95     {
96       return true;
97     }
98
99     return myHasColorSurf == theOther.myHasColorSurf
100         && myHasColorCurv == theOther.myHasColorCurv
101         && myMaterial == theOther.myMaterial
102         && (!myHasColorSurf || myColorSurf == theOther.myColorSurf)
103         && (!myHasColorCurv || myColorCurv == theOther.myColorCurv);
104   }
105
106   //! Returns True if styles are the same.
107   Standard_Boolean operator== (const XCAFPrs_Style& theOther) const
108   {
109     return IsEqual (theOther);
110   }
111
112   //! Computes a hash code for the given set of styling settings, in the range [1, theUpperBound]
113   //! @param theStyle the set of styling settings which hash code is to be computed
114   //! @param theUpperBound the upper bound of the range a computing hash code must be within
115   //! @return a computed hash code, in the range [1, theUpperBound]
116   static Standard_Integer HashCode (const XCAFPrs_Style& theStyle, const Standard_Integer theUpperBound)
117   {
118     if (!theStyle.myIsVisible)
119     {
120       return 1;
121     }
122
123     Standard_Integer aHashCode = 0;
124     if (theStyle.myHasColorSurf)
125     {
126       aHashCode = aHashCode ^ Quantity_ColorRGBAHasher::HashCode (theStyle.myColorSurf, theUpperBound);
127     }
128     if (theStyle.myHasColorCurv)
129     {
130       aHashCode = aHashCode ^ Quantity_ColorHasher::HashCode (theStyle.myColorCurv, theUpperBound);
131     }
132     if (!theStyle.myMaterial.IsNull())
133     {
134       aHashCode = aHashCode ^ ::HashCode (theStyle.myMaterial, theUpperBound);
135     }
136     return ::HashCode (aHashCode, theUpperBound);
137   }
138
139   //! Returns True when the two keys are the same.
140   static Standard_Boolean IsEqual (const XCAFPrs_Style& theS1, const XCAFPrs_Style& theS2)
141   {
142     return theS1.IsEqual (theS2);
143   }
144
145   //! Dumps the content of me into the stream
146   Standard_EXPORT void DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth = -1) const;
147
148 protected:
149
150   Handle(XCAFDoc_VisMaterial) myMaterial;
151   Quantity_ColorRGBA myColorSurf;
152   Quantity_Color     myColorCurv;
153   Standard_Boolean   myHasColorSurf;
154   Standard_Boolean   myHasColorCurv;
155   Standard_Boolean   myIsVisible;
156
157 };
158
159 #endif // _XCAFPrs_Style_HeaderFile