]> SALOME platform Git repositories - modules/geom.git/blob - src/DependencyTree/DependencyTree_Object.cxx
Salome HOME
Merge changes for HYDRO project : hydro/imps_2017_salome_84 branch.
[modules/geom.git] / src / DependencyTree / DependencyTree_Object.cxx
1 // Copyright (C) 2014-2016  CEA/DEN, EDF R&D, OPEN CASCADE
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 // internal includes
21 #include "DependencyTree_Object.h"
22
23 // GEOM includes
24 #include <GeometryGUI.h>
25
26 // GUI includes
27 #include <SUIT_Session.h>
28 #include <SUIT_ResourceMgr.h>
29 #include <SalomeApp_Application.h>
30 #include <SalomeApp_Study.h>
31
32 // Qt includes
33 #include <QFont>
34
35 const int itemH = 20;
36 const int itemW = 90;
37
38 DependencyTree_Object::DependencyTree_Object( const std::string& theEntry, QGraphicsItem* theParent )
39 :GraphicsView_Object( theParent ),
40 myIsMainObject( false )
41 {
42   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
43
44   myColor = resMgr->colorValue( "Geometry", "dependency_tree_node_color", QColor( 62, 180, 238 ) );
45   mySelectColor = resMgr->colorValue( "Geometry", "dependency_tree_select_node_color", QColor( 237, 243, 58 ) );
46   myMainObjectColor = resMgr->colorValue( "Geometry", "dependency_tree_main_node_color", QColor( 238, 90, 125 ) );
47   myUnpublishObjectColor = resMgr->colorValue( "Geometry", "dependency_tree_unpublish_node_color", QColor( 255, 255, 255 ) );
48
49   myPolygonItem = new QGraphicsPolygonItem();
50   QPolygonF myPolygon;
51   myPolygon << QPointF( -itemW, -itemH ) << QPointF( itemW, -itemH ) << QPointF( itemW, itemH )
52             << QPointF( -itemW, itemH )  << QPointF( -itemW, -itemH );
53
54   myPolygonItem->setPolygon( myPolygon );
55
56   myTextItem = new QGraphicsSimpleTextItem();
57   QFont textFont;
58   textFont.setPointSize( itemH );
59   myTextItem->setFont( textFont );
60
61   myEntry = theEntry;
62
63   SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
64   if ( !app ) return;
65   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>(app->activeStudy());
66   SALOMEDS::Study_var aStudyDS = GeometryGUI::ClientStudyToStudy( study->studyDS() );
67   int studyId = aStudyDS->StudyId();
68   myGeomObject = GeometryGUI::GetGeomGen()->GetObject( studyId, myEntry.c_str() );
69
70   updateName();
71
72   addToGroup( myPolygonItem );
73   addToGroup( myTextItem );
74 }
75
76 DependencyTree_Object::~DependencyTree_Object()
77 {
78 }
79
80 //=================================================================================
81 // function : highlight()
82 // purpose  : highlight current item
83 //=================================================================================
84 bool DependencyTree_Object::highlight( double theX, double theY )
85 {
86   if( !isHighlighted() ) {
87     QColor color = myPolygonItem->brush().color();
88     int saturation = ( color.saturation() - 100 ) > 10 ? color.saturation() - 100 : 10;
89     color.setHsv( color.hsvHue(), saturation, color.value() );
90
91     myPolygonItem->setBrush( color );
92     myPolygonItem->setPen( getPen( color ) );
93
94     myPolygonItem->setToolTip( getName() );
95   }
96   return GraphicsView_Object::highlight( theX, theY );
97 }
98
99 //=================================================================================
100 // function : unhighlight()
101 // purpose  : set properties for unhighlighted item
102 //=================================================================================
103 void DependencyTree_Object::unhighlight()
104 {
105   if( isSelected() )
106     myPolygonItem->setBrush( mySelectColor );
107   else if( myIsMainObject )
108     myPolygonItem->setBrush( myMainObjectColor );
109   else
110     myPolygonItem->setBrush( myColor );
111
112   myPolygonItem->setPen( getPen( myPolygonItem->brush().color() ) );
113
114   GraphicsView_Object::unhighlight();
115 }
116
117 //=================================================================================
118 // function : select()
119 // purpose  : select current item
120 //=================================================================================
121 bool DependencyTree_Object::select( double theX, double theY, const QRectF& theRect )
122 {
123   myPolygonItem->setBrush( mySelectColor );
124   myPolygonItem->setPen( getPen( mySelectColor ) );
125
126   return GraphicsView_Object::select( theX, theY, theRect );
127 }
128
129 //=================================================================================
130 // function : unselect()
131 // purpose  : set properties for unselected item
132 //=================================================================================
133 void DependencyTree_Object::unselect()
134 {
135   if( myIsMainObject )
136     myPolygonItem->setBrush( myMainObjectColor );
137   else
138     myPolygonItem->setBrush( myColor );
139
140   myPolygonItem->setPen( getPen( myPolygonItem->brush().color() ) );
141
142   GraphicsView_Object::unselect();
143 }
144
145 //=================================================================================
146 // function : getEntry()
147 // purpose  : get entry of current item
148 //=================================================================================
149 std::string DependencyTree_Object::getEntry() const
150 {
151   return myEntry;
152 }
153
154 //=================================================================================
155 // function : getGeomObject()
156 // purpose  : get geometry object of current item
157 //=================================================================================
158 GEOM::GEOM_BaseObject_var DependencyTree_Object::getGeomObject() const
159 {
160   return myGeomObject;
161 }
162
163 //=================================================================================
164 // function : updateName()
165 // purpose  : update name of current item using its entry
166 //=================================================================================
167 void DependencyTree_Object::updateName()
168 {
169
170   QString name = myGeomObject->GetName();
171   CORBA::String_var studyEntryVar = myGeomObject->GetStudyEntry();
172
173   if( QString( studyEntryVar.in() ).isEmpty() ) {
174         if( name.isEmpty() )
175       name = "unpublished";
176     myColor = myUnpublishObjectColor;
177   }
178
179   myPolygonItem->setBrush( myColor );
180   myPolygonItem->setPen( getPen( myColor ) );
181
182   setName( name );
183
184   myTextItem->setText( name );
185   double textWidth = myTextItem->sceneBoundingRect().width();
186   double textHeight = myTextItem->sceneBoundingRect().height();
187
188   double polygonWidth = myPolygonItem->sceneBoundingRect().width();
189   double polygonHeight = myPolygonItem->sceneBoundingRect().height();
190
191   if( ( textWidth - 4 ) > polygonWidth ) {
192     int numberSymbol = int( polygonWidth * name.length() / textWidth );
193     QString newName = name.left( numberSymbol - 3 ) + "...";
194     myTextItem->setText( newName );
195     textWidth = myTextItem->sceneBoundingRect().width();
196   }
197   myTextItem->setPos( ( polygonWidth - textWidth ) / 2 - itemW,
198                       ( polygonHeight - textHeight ) / 2 - itemH );
199 }
200
201 //=================================================================================
202 // function : setColor()
203 // purpose  : set default color of current item
204 //=================================================================================
205 void DependencyTree_Object::setColor( const QColor& theColor )
206 {
207   CORBA::String_var studyEntryVar = myGeomObject->GetStudyEntry();
208   if( QString( studyEntryVar.in() ).isEmpty() )
209     myColor = myUnpublishObjectColor;
210   else
211     myColor = theColor;
212
213 }
214
215 //=================================================================================
216 // function : setSelectColor()
217 // purpose  : set color of selected item
218 //=================================================================================
219 void DependencyTree_Object::setSelectColor( const QColor& theColor )
220 {
221   mySelectColor = theColor;
222 }
223
224 //=================================================================================
225 // function : setMainObjectColor()
226 // purpose  : set color if current item is main object of dependency tree
227 //=================================================================================
228 void DependencyTree_Object::setMainObjectColor( const QColor& theColor )
229 {
230   myMainObjectColor = theColor;
231 }
232
233 //=================================================================================
234 // function : setUnpublishObjectColor()
235 // purpose  : set color if current item is unpublished object
236 //=================================================================================
237 void DependencyTree_Object::setUnpublishObjectColor( const QColor& theColor )
238 {
239   myUnpublishObjectColor = theColor;
240   CORBA::String_var studyEntryVar = myGeomObject->GetStudyEntry();
241   if( QString( studyEntryVar.in() ).isEmpty() )
242     myColor = myUnpublishObjectColor;
243 }
244
245 //=================================================================================
246 // function : setIsMainObject()
247 // purpose  : set true if current item is main object of dependency tree
248 //=================================================================================
249 void DependencyTree_Object::setIsMainObject( bool theIsMainObject )
250 {
251   myIsMainObject = theIsMainObject;
252
253   if( myIsMainObject )
254     myPolygonItem->setBrush( myMainObjectColor );
255   else
256     myPolygonItem->setBrush( myColor );
257
258   myPolygonItem->setPen( getPen( myPolygonItem->brush().color() ) );
259 }
260
261 //=================================================================================
262 // function : getPen()
263 // purpose  : get pen which is dependent of current color
264 //=================================================================================
265 QPen DependencyTree_Object::getPen( const QColor& theColor )
266 {
267   int value = ( theColor.value() - 100 ) > 10 ? theColor.value() - 100 : 10;
268   QColor penColor;
269   penColor.setHsv( theColor.hsvHue(), theColor.saturation(), value );
270   return QPen( QBrush( penColor ), 4 );
271 }