Salome HOME
refs #661: support of transparency in LCM
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_LandCoverMapPrs.cxx
1 // Copyright (C) 2014-2015  EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
6 //
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10 // Lesser General Public License for more details.
11 //
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include <HYDROGUI_LandCoverMapPrs.h>
20 #include <HYDROData_Iterator.h>
21 #include <HYDROData_Tool.h>
22 #include <AIS_DisplayMode.hxx>
23 #include <Prs3d_IsoAspect.hxx>
24 #include <SelectMgr_SequenceOfOwner.hxx>
25 #include <StdPrs_WFDeflectionShape.hxx>
26 #include <StdSelect_BRepOwner.hxx>
27 #include <TopoDS_Face.hxx>
28 #include <QColor>
29 #include <QString>
30
31 const Quantity_Color EDGES_COLOR = Quantity_NOC_WHITE;
32 const int HILIGHT_ISO_NB = 10;
33
34 IMPLEMENT_STANDARD_HANDLE( HYDROGUI_LandCoverMapPrs, AIS_ColoredShape )
35 IMPLEMENT_STANDARD_RTTIEXT( HYDROGUI_LandCoverMapPrs, AIS_ColoredShape )
36
37 HYDROGUI_LandCoverMapPrs::HYDROGUI_LandCoverMapPrs( const Handle(HYDROData_LandCoverMap)& theMap )
38   : AIS_ColoredShape( theMap->GetShape() )
39 {
40   SetLandCoverMap( theMap );
41   SetAutoHilight( Standard_False );
42   SetHilightAttributes( EDGES_COLOR );
43 }
44
45 HYDROGUI_LandCoverMapPrs::~HYDROGUI_LandCoverMapPrs()
46 {
47 }
48
49 Handle(HYDROData_LandCoverMap) HYDROGUI_LandCoverMapPrs::GetLandCoverMap() const
50 {
51   return myLCMap;
52 }
53
54 void HYDROGUI_LandCoverMapPrs::SetLandCoverMap( const Handle(HYDROData_LandCoverMap)& theMap )
55 {
56   myLCMap = theMap;
57   UpdateColors();
58 }
59
60 void HYDROGUI_LandCoverMapPrs::UpdateColors()
61 {
62   if( !myLCMap.IsNull() )
63     SetTransparency( myLCMap->GetTransparency() );
64
65   Set( myLCMap->GetShape() );
66   SetMaterial( Graphic3d_NOM_PLASTIC );
67   HYDROData_LandCoverMap::Iterator anIt( myLCMap );
68   for( ; anIt.More(); anIt.Next() )
69   {
70     TopoDS_Face aFace = anIt.Face();
71     QString aStricklerType = anIt.StricklerType();
72     Quantity_Color aColor = GetColor( aStricklerType );
73     SetCustomColor( aFace, aColor );
74     SetCustomWidth( aFace, 1.0 );
75   }
76 }
77
78 Handle(Aspect_ColorScale) HYDROGUI_LandCoverMapPrs::GetColorScale() const
79 {
80   return myColorScale;
81 }
82
83 void HYDROGUI_LandCoverMapPrs::SetColorScale( const Handle(Aspect_ColorScale)& theColorScale )
84 {
85   myColorScale = theColorScale;
86   double aMin = 0, aMax = 0;
87   if( myTable.IsNull() )
88   {
89     //TODO: go through all Strickler tables in the document to get the global range
90   }
91   else
92     myTable->GetCoefficientRange( aMin, aMax );
93
94   myColorScale->SetRange( aMin, aMax );
95   UpdateColors();
96 }
97
98 Handle(HYDROData_StricklerTable) HYDROGUI_LandCoverMapPrs::GetTable() const
99 {
100   return myTable;
101 }
102
103 void HYDROGUI_LandCoverMapPrs::SetTable( const Handle(HYDROData_StricklerTable)& theTable )
104 {
105   myTable = theTable;
106 }
107
108 Quantity_Color HYDROGUI_LandCoverMapPrs::GetColor( const QString& theStricklerType ) const
109 {
110   Quantity_Color aResult;
111
112   if( !myColorScale.IsNull() && !myTable.IsNull() )
113   {
114     double aCoeff = myTable->Get( theStricklerType, 0.0 );
115     Standard_Boolean isOK = myColorScale->FindColor( aCoeff, aResult );
116     if( isOK )
117       return aResult;
118     else
119       return Quantity_Color();
120   }
121
122   QColor aColor = HYDROData_Document::Document( myLCMap->Label())->GetAssociatedColor(theStricklerType, myTable);
123
124   if (aColor.isValid())
125     return HYDROData_Tool::toOccColor(aColor);
126   else
127     return Quantity_Color();
128 }
129
130 void HYDROGUI_LandCoverMapPrs::Compute( const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
131                                         const Handle(Prs3d_Presentation)& thePresentation,
132                                         const Standard_Integer theMode )
133 {
134   thePresentation->Clear();
135
136   myDrawer->UIsoAspect()->SetNumber( 0 );
137   myDrawer->VIsoAspect()->SetNumber( 0 );
138   myDrawer->LineAspect()->SetColor( EDGES_COLOR );
139   myDrawer->FaceBoundaryAspect()->SetColor( EDGES_COLOR );
140   myDrawer->FreeBoundaryAspect()->SetColor( EDGES_COLOR );
141
142   switch( theMode )
143   {
144   case AIS_WireFrame:
145   case AIS_Shaded:
146     AIS_ColoredShape::Compute( thePresentationManager, thePresentation, theMode );
147     break;
148   }
149
150   if( theMode==AIS_Shaded )
151     StdPrs_WFDeflectionShape::Add( thePresentation, Shape(), myDrawer );
152 }
153
154 void HYDROGUI_LandCoverMapPrs::HilightSelected( const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
155                                                 const SelectMgr_SequenceOfOwner& theOwners )
156 {
157   Handle(Prs3d_Presentation) aSelectPrs = GetSelectPresentation( thePresentationManager );
158
159   SetHilightAttributes( EDGES_COLOR );
160
161   for( int i=1, n=theOwners.Length(); i<=n; i++ )
162   {
163     Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast( theOwners.Value( i ) );
164     if( !anOwner.IsNull() )
165       StdPrs_WFDeflectionShape::Add( aSelectPrs, anOwner->Shape(), HilightAttributes() );
166   }
167
168   HilightAttributes()->UIsoAspect()->SetNumber( 0 );
169   HilightAttributes()->VIsoAspect()->SetNumber( 0 );
170
171   aSelectPrs->SetDisplayPriority( 9 );
172   aSelectPrs->Display();
173 }
174
175 void HYDROGUI_LandCoverMapPrs::SetHilightAttributes( const Quantity_Color& theEdgesColor )
176 {
177   HilightAttributes()->UIsoAspect()->SetNumber( HILIGHT_ISO_NB );
178   HilightAttributes()->UIsoAspect()->SetColor( theEdgesColor );
179   HilightAttributes()->VIsoAspect()->SetNumber( HILIGHT_ISO_NB );
180   HilightAttributes()->VIsoAspect()->SetColor( theEdgesColor );
181   HilightAttributes()->LineAspect()->SetColor( theEdgesColor );
182   HilightAttributes()->FaceBoundaryAspect()->SetColor( theEdgesColor );
183   HilightAttributes()->FreeBoundaryAspect()->SetColor( theEdgesColor );
184 }