Salome HOME
2f839df33183cb823c1098466f116fe396c3a1bb
[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   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
156   SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
157   if ( !app ) return;
158   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>(app->activeStudy());
159   SALOMEDS::Study_var aStudyDS = GeometryGUI::ClientStudyToStudy( study->studyDS());
160   int StudyId = aStudyDS->StudyId();
161   GEOM::_objref_GEOM_BaseObject* object = GeometryGUI::GetGeomGen()->GetObject( StudyId, myEntry.c_str() );
162
163   QString name = object->GetName();
164   QString StudyEntry = object->GetStudyEntry();
165   std::cout << "\n\n\n StudyEntry = " << StudyEntry.toStdString() << "  " << StudyEntry.isEmpty() <<  std::endl;
166
167
168   if( StudyEntry.isEmpty() ) {
169         if( name.isEmpty() )
170       name = "unpublished";
171     myColor = resMgr->colorValue( "Geometry", "dependency_tree_background_color", QColor( 255, 255, 255 ) );
172   }
173
174   setName( name );
175
176   myTextItem->setText( name );
177   double textWidth = myTextItem->sceneBoundingRect().width();
178   double textHeight = myTextItem->sceneBoundingRect().height();
179
180   double polygonWidth = myPolygonItem->sceneBoundingRect().width();
181   double polygonHeight = myPolygonItem->sceneBoundingRect().height();
182
183   if( ( textWidth - 4 ) > polygonWidth ) {
184     myIsLongName = true;
185     int numberSymbol = int( polygonWidth * name.length() / textWidth );
186     QString newName = name.left( numberSymbol - 3 ) + "...";
187     myTextItem->setText( newName );
188     textWidth = myTextItem->sceneBoundingRect().width();
189   }
190   myTextItem->setPos( ( polygonWidth - textWidth ) / 2 - itemW,
191                       ( polygonHeight - textHeight ) / 2 - itemH );
192 }
193
194 //=================================================================================
195 // function : setColor()
196 // purpose  : set default color of current item
197 //=================================================================================
198 void DependencyTree_Object::setColor( const QColor& theColor )
199 {
200   myColor = theColor;
201 }
202
203 //=================================================================================
204 // function : setSelectColor()
205 // purpose  : set color of selected item
206 //=================================================================================
207 void DependencyTree_Object::setSelectColor( const QColor& theColor )
208 {
209   mySelectColor = theColor;
210 }
211
212 //=================================================================================
213 // function : setMainObjectColor()
214 // purpose  : set color if current item is main object of dependency tree
215 //=================================================================================
216 void DependencyTree_Object::setMainObjectColor( const QColor& theColor )
217 {
218   myMainObjectColor = theColor;
219 }
220
221 //=================================================================================
222 // function : setIsMainObject()
223 // purpose  : set true if current item is main object of dependency tree
224 //=================================================================================
225 void DependencyTree_Object::setIsMainObject( bool theIsMainObject )
226 {
227   myIsMainObject = theIsMainObject;
228
229   if( myIsMainObject )
230     myPolygonItem->setBrush( myMainObjectColor );
231   else
232     myPolygonItem->setBrush( myColor );
233
234   myPolygonItem->setPen( getPen( myPolygonItem->brush().color() ) );
235 }
236
237 //=================================================================================
238 // function : getPen()
239 // purpose  : get pen which is dependent of current color
240 //=================================================================================
241 QPen DependencyTree_Object::getPen( const QColor& theColor )
242 {
243   int value = ( theColor.value() - 100 ) > 10 ? theColor.value() - 100 : 10;
244   QColor penColor;
245   penColor.setHsv( theColor.hsvHue(), theColor.saturation(), value );
246   return QPen( QBrush( penColor ), 4 );
247 }