1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D, OPEN CASCADE
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 #include "DependencyTree_Object.h"
24 #include <GeometryGUI.h>
27 #include <SUIT_Session.h>
28 #include <SUIT_ResourceMgr.h>
29 #include <SalomeApp_Application.h>
30 #include <SalomeApp_Study.h>
38 DependencyTree_Object::DependencyTree_Object( const std::string& theEntry, QGraphicsItem* theParent )
39 :GraphicsView_Object( theParent ),
40 myIsMainObject( false )
42 SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
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 ) );
49 myPolygonItem = new QGraphicsPolygonItem();
51 myPolygon << QPointF( -itemW, -itemH ) << QPointF( itemW, -itemH ) << QPointF( itemW, itemH )
52 << QPointF( -itemW, itemH ) << QPointF( -itemW, -itemH );
54 myPolygonItem->setPolygon( myPolygon );
56 myTextItem = new QGraphicsSimpleTextItem();
58 textFont.setPointSize( itemH );
59 myTextItem->setFont( textFont );
62 myGeomObject = GeometryGUI::GetGeomGen()->GetObject( myEntry.c_str() );
66 addToGroup( myPolygonItem );
67 addToGroup( myTextItem );
70 DependencyTree_Object::~DependencyTree_Object()
74 //=================================================================================
75 // function : highlight()
76 // purpose : highlight current item
77 //=================================================================================
78 bool DependencyTree_Object::highlight( double theX, double theY )
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() );
85 myPolygonItem->setBrush( color );
86 myPolygonItem->setPen( getPen( color ) );
88 myPolygonItem->setToolTip( getName() );
90 return GraphicsView_Object::highlight( theX, theY );
93 //=================================================================================
94 // function : unhighlight()
95 // purpose : set properties for unhighlighted item
96 //=================================================================================
97 void DependencyTree_Object::unhighlight()
100 myPolygonItem->setBrush( mySelectColor );
101 else if( myIsMainObject )
102 myPolygonItem->setBrush( myMainObjectColor );
104 myPolygonItem->setBrush( myColor );
106 myPolygonItem->setPen( getPen( myPolygonItem->brush().color() ) );
108 GraphicsView_Object::unhighlight();
111 //=================================================================================
112 // function : select()
113 // purpose : select current item
114 //=================================================================================
115 bool DependencyTree_Object::select( double theX, double theY, const QRectF& theRect )
117 myPolygonItem->setBrush( mySelectColor );
118 myPolygonItem->setPen( getPen( mySelectColor ) );
120 return GraphicsView_Object::select( theX, theY, theRect );
123 //=================================================================================
124 // function : unselect()
125 // purpose : set properties for unselected item
126 //=================================================================================
127 void DependencyTree_Object::unselect()
130 myPolygonItem->setBrush( myMainObjectColor );
132 myPolygonItem->setBrush( myColor );
134 myPolygonItem->setPen( getPen( myPolygonItem->brush().color() ) );
136 GraphicsView_Object::unselect();
139 //=================================================================================
140 // function : getEntry()
141 // purpose : get entry of current item
142 //=================================================================================
143 std::string DependencyTree_Object::getEntry() const
148 //=================================================================================
149 // function : getGeomObject()
150 // purpose : get geometry object of current item
151 //=================================================================================
152 GEOM::GEOM_BaseObject_var DependencyTree_Object::getGeomObject() const
157 //=================================================================================
158 // function : updateName()
159 // purpose : update name of current item using its entry
160 //=================================================================================
161 void DependencyTree_Object::updateName()
164 QString name = myGeomObject->GetName();
165 CORBA::String_var studyEntryVar = myGeomObject->GetStudyEntry();
167 if( QString( studyEntryVar.in() ).isEmpty() ) {
169 name = "unpublished";
170 myColor = myUnpublishObjectColor;
173 myPolygonItem->setBrush( myColor );
174 myPolygonItem->setPen( getPen( myColor ) );
178 myTextItem->setText( name );
179 double textWidth = myTextItem->sceneBoundingRect().width();
180 double textHeight = myTextItem->sceneBoundingRect().height();
182 double polygonWidth = myPolygonItem->sceneBoundingRect().width();
183 double polygonHeight = myPolygonItem->sceneBoundingRect().height();
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();
191 myTextItem->setPos( ( polygonWidth - textWidth ) / 2 - itemW,
192 ( polygonHeight - textHeight ) / 2 - itemH );
195 //=================================================================================
196 // function : setColor()
197 // purpose : set default color of current item
198 //=================================================================================
199 void DependencyTree_Object::setColor( const QColor& theColor )
201 CORBA::String_var studyEntryVar = myGeomObject->GetStudyEntry();
202 if( QString( studyEntryVar.in() ).isEmpty() )
203 myColor = myUnpublishObjectColor;
209 //=================================================================================
210 // function : setSelectColor()
211 // purpose : set color of selected item
212 //=================================================================================
213 void DependencyTree_Object::setSelectColor( const QColor& theColor )
215 mySelectColor = theColor;
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 )
224 myMainObjectColor = theColor;
227 //=================================================================================
228 // function : setUnpublishObjectColor()
229 // purpose : set color if current item is unpublished object
230 //=================================================================================
231 void DependencyTree_Object::setUnpublishObjectColor( const QColor& theColor )
233 myUnpublishObjectColor = theColor;
234 CORBA::String_var studyEntryVar = myGeomObject->GetStudyEntry();
235 if( QString( studyEntryVar.in() ).isEmpty() )
236 myColor = myUnpublishObjectColor;
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 )
245 myIsMainObject = theIsMainObject;
248 myPolygonItem->setBrush( myMainObjectColor );
250 myPolygonItem->setBrush( myColor );
252 myPolygonItem->setPen( getPen( myPolygonItem->brush().color() ) );
255 //=================================================================================
256 // function : getPen()
257 // purpose : get pen which is dependent of current color
258 //=================================================================================
259 QPen DependencyTree_Object::getPen( const QColor& theColor )
261 int value = ( theColor.value() - 100 ) > 10 ? theColor.value() - 100 : 10;
263 penColor.setHsv( theColor.hsvHue(), theColor.saturation(), value );
264 return QPen( QBrush( penColor ), 4 );