Salome HOME
0f85a0405ccbd131f7b3a8ee7ede2272bc1f5bef
[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 #include "DependencyTree_Object.h"
21
22 // GEOM includes
23 #include <GeometryGUI.h>
24 #include <GEOM_BaseObject.hxx>
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 myIsLongName( 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   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     if( myIsLongName )
89       myPolygonItem->setToolTip( getName() );
90   }
91   return GraphicsView_Object::highlight( theX, theY );
92 }
93
94 //=================================================================================
95 // function : unhighlight()
96 // purpose  : set properties for unhighlighted item
97 //=================================================================================
98 void DependencyTree_Object::unhighlight()
99 {
100   if( isSelected() )
101     myPolygonItem->setBrush( mySelectColor );
102   else if( myIsMainObject )
103     myPolygonItem->setBrush( myMainObjectColor );
104   else
105     myPolygonItem->setBrush( myColor );
106
107   myPolygonItem->setPen( getPen( myPolygonItem->brush().color() ) );
108
109   GraphicsView_Object::unhighlight();
110 }
111
112 //=================================================================================
113 // function : select()
114 // purpose  : select current item
115 //=================================================================================
116 bool DependencyTree_Object::select( double theX, double theY, const QRectF& theRect )
117 {
118   myPolygonItem->setBrush( mySelectColor );
119   myPolygonItem->setPen( getPen( mySelectColor ) );
120
121   return GraphicsView_Object::select( theX, theY, theRect );
122 }
123
124 //=================================================================================
125 // function : unselect()
126 // purpose  : set properties for unselected item
127 //=================================================================================
128 void DependencyTree_Object::unselect()
129 {
130   if( myIsMainObject )
131     myPolygonItem->setBrush( myMainObjectColor );
132   else
133     myPolygonItem->setBrush( myColor );
134
135   myPolygonItem->setPen( getPen( myPolygonItem->brush().color() ) );
136
137   GraphicsView_Object::unselect();
138 }
139
140 //=================================================================================
141 // function : getEntry()
142 // purpose  : get entry of current item
143 //=================================================================================
144 std::string DependencyTree_Object::getEntry() const
145 {
146   return myEntry;
147 }
148
149 //=================================================================================
150 // function : updateName()
151 // purpose  : update name of current item using its entry
152 //=================================================================================
153 void DependencyTree_Object::updateName()
154 {
155   SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
156   if ( !app ) return;
157   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>(app->activeStudy());
158   SALOMEDS::Study_var aStudyDS = GeometryGUI::ClientStudyToStudy( study->studyDS());
159   int StudyId = aStudyDS->StudyId();
160   GEOM::_objref_GEOM_BaseObject* object = GeometryGUI::GetGeomGen()->GetObject( StudyId, myEntry.c_str() );
161
162   QString name = object->GetName();
163
164 //      QString name = myEntry.c_str();
165
166   setName( name );
167
168   myTextItem->setText( name );
169   double textWidth = myTextItem->sceneBoundingRect().width();
170   double textHeight = myTextItem->sceneBoundingRect().height();
171
172   double polygonWidth = myPolygonItem->sceneBoundingRect().width();
173   double polygonHeight = myPolygonItem->sceneBoundingRect().height();
174
175   if( ( textWidth - 4 ) > polygonWidth ) {
176     myIsLongName = true;
177     int numberSymbol = int( polygonWidth * name.length() / textWidth );
178     QString newName = name.left( numberSymbol - 3 ) + "...";
179     myTextItem->setText( newName );
180     textWidth = myTextItem->sceneBoundingRect().width();
181   }
182   myTextItem->setPos( ( polygonWidth - textWidth ) / 2 - itemW,
183                       ( polygonHeight - textHeight ) / 2 - itemH );
184 }
185
186 //=================================================================================
187 // function : setColor()
188 // purpose  : set default color of current item
189 //=================================================================================
190 void DependencyTree_Object::setColor( const QColor& theColor )
191 {
192   myColor = theColor;
193 }
194
195 //=================================================================================
196 // function : setSelectColor()
197 // purpose  : set color of selected item
198 //=================================================================================
199 void DependencyTree_Object::setSelectColor( const QColor& theColor )
200 {
201   mySelectColor = theColor;
202 }
203
204 //=================================================================================
205 // function : setMainObjectColor()
206 // purpose  : set color if current item is main object of dependency tree
207 //=================================================================================
208 void DependencyTree_Object::setMainObjectColor( const QColor& theColor )
209 {
210   myMainObjectColor = theColor;
211 }
212
213 //=================================================================================
214 // function : setIsMainObject()
215 // purpose  : set true if current item is main object of dependency tree
216 //=================================================================================
217 void DependencyTree_Object::setIsMainObject( bool theIsMainObject )
218 {
219   myIsMainObject = theIsMainObject;
220
221   if( myIsMainObject )
222     myPolygonItem->setBrush( myMainObjectColor );
223   else
224     myPolygonItem->setBrush( myColor );
225
226   myPolygonItem->setPen( getPen( myPolygonItem->brush().color() ) );
227 }
228
229 //=================================================================================
230 // function : getPen()
231 // purpose  : get pen which is dependent of current color
232 //=================================================================================
233 QPen DependencyTree_Object::getPen( const QColor& theColor )
234 {
235   int value = ( theColor.value() - 100 ) > 10 ? theColor.value() - 100 : 10;
236   QColor penColor;
237   penColor.setHsv( theColor.hsvHue(), theColor.saturation(), value );
238   return QPen( QBrush( penColor ), 4 );
239 }