Salome HOME
- clean programming code
[modules/geom.git] / src / DependencyTree / DependencyTree_Object.cxx
1 // Copyright (C) 2014  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   int studyId = GeometryGUI::ClientStudyToStudy( study->studyDS())->StudyId();
67   myGeomObject = GeometryGUI::GetGeomGen()->GetObject( studyId, myEntry.c_str() );
68
69   updateName();
70
71   addToGroup( myPolygonItem );
72   addToGroup( myTextItem );
73 }
74
75 DependencyTree_Object::~DependencyTree_Object()
76 {
77 }
78
79 //=================================================================================
80 // function : highlight()
81 // purpose  : highlight current item
82 //=================================================================================
83 bool DependencyTree_Object::highlight( double theX, double theY )
84 {
85   if( !isHighlighted() ) {
86     QColor color = myPolygonItem->brush().color();
87     int saturation = ( color.saturation() - 100 ) > 10 ? color.saturation() - 100 : 10;
88     color.setHsv( color.hsvHue(), saturation, color.value() );
89
90     myPolygonItem->setBrush( color );
91     myPolygonItem->setPen( getPen( color ) );
92
93     myPolygonItem->setToolTip( getName() );
94   }
95   return GraphicsView_Object::highlight( theX, theY );
96 }
97
98 //=================================================================================
99 // function : unhighlight()
100 // purpose  : set properties for unhighlighted item
101 //=================================================================================
102 void DependencyTree_Object::unhighlight()
103 {
104   if( isSelected() )
105     myPolygonItem->setBrush( mySelectColor );
106   else if( myIsMainObject )
107     myPolygonItem->setBrush( myMainObjectColor );
108   else
109     myPolygonItem->setBrush( myColor );
110
111   myPolygonItem->setPen( getPen( myPolygonItem->brush().color() ) );
112
113   GraphicsView_Object::unhighlight();
114 }
115
116 //=================================================================================
117 // function : select()
118 // purpose  : select current item
119 //=================================================================================
120 bool DependencyTree_Object::select( double theX, double theY, const QRectF& theRect )
121 {
122   myPolygonItem->setBrush( mySelectColor );
123   myPolygonItem->setPen( getPen( mySelectColor ) );
124
125   return GraphicsView_Object::select( theX, theY, theRect );
126 }
127
128 //=================================================================================
129 // function : unselect()
130 // purpose  : set properties for unselected item
131 //=================================================================================
132 void DependencyTree_Object::unselect()
133 {
134   if( myIsMainObject )
135     myPolygonItem->setBrush( myMainObjectColor );
136   else
137     myPolygonItem->setBrush( myColor );
138
139   myPolygonItem->setPen( getPen( myPolygonItem->brush().color() ) );
140
141   GraphicsView_Object::unselect();
142 }
143
144 //=================================================================================
145 // function : getEntry()
146 // purpose  : get entry of current item
147 //=================================================================================
148 std::string DependencyTree_Object::getEntry() const
149 {
150   return myEntry;
151 }
152
153 //=================================================================================
154 // function : getGeomObject()
155 // purpose  : get geometry object of current item
156 //=================================================================================
157 GEOM::GEOM_BaseObject_var DependencyTree_Object::getGeomObject() const
158 {
159   return myGeomObject;
160 }
161
162 //=================================================================================
163 // function : updateName()
164 // purpose  : update name of current item using its entry
165 //=================================================================================
166 void DependencyTree_Object::updateName()
167 {
168
169   QString name = myGeomObject->GetName();
170   QString studyEntry = myGeomObject->GetStudyEntry();
171
172   if( studyEntry.isEmpty() ) {
173         if( name.isEmpty() )
174       name = "unpublished";
175     myColor = myUnpublishObjectColor;
176   }
177
178   myPolygonItem->setBrush( myColor );
179   myPolygonItem->setPen( getPen( myColor ) );
180
181   setName( name );
182
183   myTextItem->setText( name );
184   double textWidth = myTextItem->sceneBoundingRect().width();
185   double textHeight = myTextItem->sceneBoundingRect().height();
186
187   double polygonWidth = myPolygonItem->sceneBoundingRect().width();
188   double polygonHeight = myPolygonItem->sceneBoundingRect().height();
189
190   if( ( textWidth - 4 ) > polygonWidth ) {
191     int numberSymbol = int( polygonWidth * name.length() / textWidth );
192     QString newName = name.left( numberSymbol - 3 ) + "...";
193     myTextItem->setText( newName );
194     textWidth = myTextItem->sceneBoundingRect().width();
195   }
196   myTextItem->setPos( ( polygonWidth - textWidth ) / 2 - itemW,
197                       ( polygonHeight - textHeight ) / 2 - itemH );
198 }
199
200 //=================================================================================
201 // function : setColor()
202 // purpose  : set default color of current item
203 //=================================================================================
204 void DependencyTree_Object::setColor( const QColor& theColor )
205 {
206   QString studyEntry = myGeomObject->GetStudyEntry();
207   if( studyEntry.isEmpty() )
208     myColor = myUnpublishObjectColor;
209   else
210     myColor = theColor;
211
212 }
213
214 //=================================================================================
215 // function : setSelectColor()
216 // purpose  : set color of selected item
217 //=================================================================================
218 void DependencyTree_Object::setSelectColor( const QColor& theColor )
219 {
220   mySelectColor = theColor;
221 }
222
223 //=================================================================================
224 // function : setMainObjectColor()
225 // purpose  : set color if current item is main object of dependency tree
226 //=================================================================================
227 void DependencyTree_Object::setMainObjectColor( const QColor& theColor )
228 {
229   myMainObjectColor = theColor;
230 }
231
232 //=================================================================================
233 // function : setUnpublishObjectColor()
234 // purpose  : set color if current item is unpublished object
235 //=================================================================================
236 void DependencyTree_Object::setUnpublishObjectColor( const QColor& theColor )
237 {
238   myUnpublishObjectColor = theColor;
239   QString studyEntry = myGeomObject->GetStudyEntry();
240   if( studyEntry.isEmpty() )
241     myColor = myUnpublishObjectColor;
242 }
243
244 //=================================================================================
245 // function : setIsMainObject()
246 // purpose  : set true if current item is main object of dependency tree
247 //=================================================================================
248 void DependencyTree_Object::setIsMainObject( bool theIsMainObject )
249 {
250   myIsMainObject = theIsMainObject;
251
252   if( myIsMainObject )
253     myPolygonItem->setBrush( myMainObjectColor );
254   else
255     myPolygonItem->setBrush( myColor );
256
257   myPolygonItem->setPen( getPen( myPolygonItem->brush().color() ) );
258 }
259
260 //=================================================================================
261 // function : getPen()
262 // purpose  : get pen which is dependent of current color
263 //=================================================================================
264 QPen DependencyTree_Object::getPen( const QColor& theColor )
265 {
266   int value = ( theColor.value() - 100 ) > 10 ? theColor.value() - 100 : 10;
267   QColor penColor;
268   penColor.setHsv( theColor.hsvHue(), theColor.saturation(), value );
269   return QPen( QBrush( penColor ), 4 );
270 }