1 // Copyright (C) 2014 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>
25 #include <GEOM_BaseObject.hxx>
28 #include <SUIT_Session.h>
29 #include <SUIT_ResourceMgr.h>
30 #include <SalomeApp_Application.h>
31 #include <SalomeApp_Study.h>
39 DependencyTree_Object::DependencyTree_Object( const std::string& theEntry, QGraphicsItem* theParent )
40 :GraphicsView_Object( theParent ),
41 myIsMainObject( false )
43 SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
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 ) );
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 );
55 myPolygonItem->setBrush( myColor );
56 myPolygonItem->setPen( getPen( myColor ) );
58 myTextItem = new QGraphicsSimpleTextItem();
60 textFont.setPointSize( itemH );
61 myTextItem->setFont( textFont );
65 SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
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() );
73 addToGroup( myPolygonItem );
74 addToGroup( myTextItem );
77 DependencyTree_Object::~DependencyTree_Object()
81 //=================================================================================
82 // function : highlight()
83 // purpose : highlight current item
84 //=================================================================================
85 bool DependencyTree_Object::highlight( double theX, double theY )
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() );
92 myPolygonItem->setBrush( color );
93 myPolygonItem->setPen( getPen( color ) );
95 myPolygonItem->setToolTip( getName() );
97 return GraphicsView_Object::highlight( theX, theY );
100 //=================================================================================
101 // function : unhighlight()
102 // purpose : set properties for unhighlighted item
103 //=================================================================================
104 void DependencyTree_Object::unhighlight()
107 myPolygonItem->setBrush( mySelectColor );
108 else if( myIsMainObject )
109 myPolygonItem->setBrush( myMainObjectColor );
111 myPolygonItem->setBrush( myColor );
113 myPolygonItem->setPen( getPen( myPolygonItem->brush().color() ) );
115 GraphicsView_Object::unhighlight();
118 //=================================================================================
119 // function : select()
120 // purpose : select current item
121 //=================================================================================
122 bool DependencyTree_Object::select( double theX, double theY, const QRectF& theRect )
124 myPolygonItem->setBrush( mySelectColor );
125 myPolygonItem->setPen( getPen( mySelectColor ) );
127 return GraphicsView_Object::select( theX, theY, theRect );
130 //=================================================================================
131 // function : unselect()
132 // purpose : set properties for unselected item
133 //=================================================================================
134 void DependencyTree_Object::unselect()
137 myPolygonItem->setBrush( myMainObjectColor );
139 myPolygonItem->setBrush( myColor );
141 myPolygonItem->setPen( getPen( myPolygonItem->brush().color() ) );
143 GraphicsView_Object::unselect();
146 //=================================================================================
147 // function : getEntry()
148 // purpose : get entry of current item
149 //=================================================================================
150 std::string DependencyTree_Object::getEntry() const
155 //=================================================================================
156 // function : getGeomObject()
157 // purpose : get geometry object of current item
158 //=================================================================================
159 GEOM::GEOM_BaseObject_var DependencyTree_Object::getGeomObject() const
164 //=================================================================================
165 // function : updateName()
166 // purpose : update name of current item using its entry
167 //=================================================================================
168 void DependencyTree_Object::updateName()
171 QString name = myGeomObject->GetName();
172 QString studyEntry = myGeomObject->GetStudyEntry();
174 if( studyEntry.isEmpty() ) {
176 name = "unpublished";
177 myColor = QColor( 255, 255, 255 );
178 myPolygonItem->setBrush( myColor );
179 myPolygonItem->setPen( getPen( myColor ) );
184 myTextItem->setText( name );
185 double textWidth = myTextItem->sceneBoundingRect().width();
186 double textHeight = myTextItem->sceneBoundingRect().height();
188 double polygonWidth = myPolygonItem->sceneBoundingRect().width();
189 double polygonHeight = myPolygonItem->sceneBoundingRect().height();
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();
197 myTextItem->setPos( ( polygonWidth - textWidth ) / 2 - itemW,
198 ( polygonHeight - textHeight ) / 2 - itemH );
201 //=================================================================================
202 // function : setColor()
203 // purpose : set default color of current item
204 //=================================================================================
205 void DependencyTree_Object::setColor( const QColor& theColor )
210 //=================================================================================
211 // function : setSelectColor()
212 // purpose : set color of selected item
213 //=================================================================================
214 void DependencyTree_Object::setSelectColor( const QColor& theColor )
216 mySelectColor = theColor;
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 )
225 myMainObjectColor = theColor;
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 )
234 myIsMainObject = theIsMainObject;
237 myPolygonItem->setBrush( myMainObjectColor );
239 myPolygonItem->setBrush( myColor );
241 myPolygonItem->setPen( getPen( myPolygonItem->brush().color() ) );
244 //=================================================================================
245 // function : getPen()
246 // purpose : get pen which is dependent of current color
247 //=================================================================================
248 QPen DependencyTree_Object::getPen( const QColor& theColor )
250 int value = ( theColor.value() - 100 ) > 10 ? theColor.value() - 100 : 10;
252 penColor.setHsv( theColor.hsvHue(), theColor.saturation(), value );
253 return QPen( QBrush( penColor ), 4 );