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