Salome HOME
ebe0cfe90ccf44f856e12f937debe8ce8947f3e5
[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 #include <GEOM_BaseObject.hxx>
26
27 // GUI includes
28 #include <SUIT_Session.h>
29 #include <SUIT_ResourceMgr.h>
30 #include <SalomeApp_Application.h>
31 #include <SalomeApp_Study.h>
32
33 // Qt includes
34 #include <QFont>
35
36 const int itemH = 20;
37 const int itemW = 90;
38
39 DependencyTree_Object::DependencyTree_Object( const std::string& theEntry, QGraphicsItem* theParent )
40 :GraphicsView_Object( theParent ),
41 myIsMainObject( false )
42 {
43   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
44
45   myColor = resMgr->colorValue( "Geometry", "dependency_tree_node_color", QColor( 62, 180, 238 ) );
46   mySelectColor = resMgr->colorValue( "Geometry", "dependency_tree_select_node_color", QColor( 237, 243, 58 ) );
47   myMainObjectColor = resMgr->colorValue( "Geometry", "dependency_tree_main_node_color", QColor( 238, 90, 125 ) );
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   myPolygonItem->setBrush( myColor );
56   myPolygonItem->setPen( getPen( myColor ) );
57
58   myTextItem = new QGraphicsSimpleTextItem();
59   QFont textFont;
60   textFont.setPointSize( itemH );
61   myTextItem->setFont( textFont );
62
63   myEntry = theEntry;
64
65   SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
66   if ( !app ) return;
67   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>(app->activeStudy());
68   int studyId = GeometryGUI::ClientStudyToStudy( study->studyDS())->StudyId();
69   myGeomObject = GeometryGUI::GetGeomGen()->GetObject( studyId, myEntry.c_str() );
70
71   updateName();
72
73   addToGroup( myPolygonItem );
74   addToGroup( myTextItem );
75 }
76
77 DependencyTree_Object::~DependencyTree_Object()
78 {
79 }
80
81 //=================================================================================
82 // function : highlight()
83 // purpose  : highlight current item
84 //=================================================================================
85 bool DependencyTree_Object::highlight( double theX, double theY )
86 {
87   if( !isHighlighted() ) {
88     QColor color = myPolygonItem->brush().color();
89     int saturation = ( color.saturation() - 100 ) > 10 ? color.saturation() - 100 : 10;
90     color.setHsv( color.hsvHue(), saturation, color.value() );
91
92     myPolygonItem->setBrush( color );
93     myPolygonItem->setPen( getPen( color ) );
94
95     myPolygonItem->setToolTip( getName() );
96   }
97   return GraphicsView_Object::highlight( theX, theY );
98 }
99
100 //=================================================================================
101 // function : unhighlight()
102 // purpose  : set properties for unhighlighted item
103 //=================================================================================
104 void DependencyTree_Object::unhighlight()
105 {
106   if( isSelected() )
107     myPolygonItem->setBrush( mySelectColor );
108   else if( myIsMainObject )
109     myPolygonItem->setBrush( myMainObjectColor );
110   else
111     myPolygonItem->setBrush( myColor );
112
113   myPolygonItem->setPen( getPen( myPolygonItem->brush().color() ) );
114
115   GraphicsView_Object::unhighlight();
116 }
117
118 //=================================================================================
119 // function : select()
120 // purpose  : select current item
121 //=================================================================================
122 bool DependencyTree_Object::select( double theX, double theY, const QRectF& theRect )
123 {
124   myPolygonItem->setBrush( mySelectColor );
125   myPolygonItem->setPen( getPen( mySelectColor ) );
126
127   return GraphicsView_Object::select( theX, theY, theRect );
128 }
129
130 //=================================================================================
131 // function : unselect()
132 // purpose  : set properties for unselected item
133 //=================================================================================
134 void DependencyTree_Object::unselect()
135 {
136   if( myIsMainObject )
137     myPolygonItem->setBrush( myMainObjectColor );
138   else
139     myPolygonItem->setBrush( myColor );
140
141   myPolygonItem->setPen( getPen( myPolygonItem->brush().color() ) );
142
143   GraphicsView_Object::unselect();
144 }
145
146 //=================================================================================
147 // function : getEntry()
148 // purpose  : get entry of current item
149 //=================================================================================
150 std::string DependencyTree_Object::getEntry() const
151 {
152   return myEntry;
153 }
154
155 //=================================================================================
156 // function : getGeomObject()
157 // purpose  : get geometry object of current item
158 //=================================================================================
159 GEOM::GEOM_BaseObject_var DependencyTree_Object::getGeomObject() const
160 {
161   return myGeomObject;
162 }
163
164 //=================================================================================
165 // function : updateName()
166 // purpose  : update name of current item using its entry
167 //=================================================================================
168 void DependencyTree_Object::updateName()
169 {
170
171   QString name = myGeomObject->GetName();
172   QString studyEntry = myGeomObject->GetStudyEntry();
173
174   if( studyEntry.isEmpty() ) {
175         if( name.isEmpty() )
176       name = "unpublished";
177     myColor = QColor( 255, 255, 255 );
178     myPolygonItem->setBrush( myColor );
179     myPolygonItem->setPen( getPen( myColor ) );
180   }
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   myColor = theColor;
208 }
209
210 //=================================================================================
211 // function : setSelectColor()
212 // purpose  : set color of selected item
213 //=================================================================================
214 void DependencyTree_Object::setSelectColor( const QColor& theColor )
215 {
216   mySelectColor = theColor;
217 }
218
219 //=================================================================================
220 // function : setMainObjectColor()
221 // purpose  : set color if current item is main object of dependency tree
222 //=================================================================================
223 void DependencyTree_Object::setMainObjectColor( const QColor& theColor )
224 {
225   myMainObjectColor = theColor;
226 }
227
228 //=================================================================================
229 // function : setIsMainObject()
230 // purpose  : set true if current item is main object of dependency tree
231 //=================================================================================
232 void DependencyTree_Object::setIsMainObject( bool theIsMainObject )
233 {
234   myIsMainObject = theIsMainObject;
235
236   if( myIsMainObject )
237     myPolygonItem->setBrush( myMainObjectColor );
238   else
239     myPolygonItem->setBrush( myColor );
240
241   myPolygonItem->setPen( getPen( myPolygonItem->brush().color() ) );
242 }
243
244 //=================================================================================
245 // function : getPen()
246 // purpose  : get pen which is dependent of current color
247 //=================================================================================
248 QPen DependencyTree_Object::getPen( const QColor& theColor )
249 {
250   int value = ( theColor.value() - 100 ) > 10 ? theColor.value() - 100 : 10;
251   QColor penColor;
252   penColor.setHsv( theColor.hsvHue(), theColor.saturation(), value );
253   return QPen( QBrush( penColor ), 4 );
254 }