Salome HOME
Merge remote-tracking branch 'origin/vsr/fix_single_study_pb' into pre/fix_single_study
[modules/gui.git] / src / GraphicsView / GraphicsView_Object.cxx
1 // Copyright (C) 2013-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 "GraphicsView_Object.h"
21
22 #include "GraphicsView_Scene.h"
23 #include "GraphicsView_ViewPort.h"
24
25 //=======================================================================
26 // Name    : GraphicsView_Object
27 // Purpose : Constructor
28 //=======================================================================
29 GraphicsView_Object::GraphicsView_Object( QGraphicsItem* theParent )
30 : QGraphicsItemGroup( theParent ),
31   myPriority( 0 ),
32   myIsOnTop( false ),
33   myIsHighlighted( false ),
34   myIsSelected( false ),
35   myIsMoving( false )
36 {
37 }
38
39 //=======================================================================
40 // Name    : GraphicsView_Object
41 // Purpose : Destructor
42 //=======================================================================
43 GraphicsView_Object::~GraphicsView_Object()
44 {
45   QListIterator<QGraphicsItem*> aChildIter( children() );
46   while( aChildIter.hasNext() )
47   {
48     if( QGraphicsItem* aChild = aChildIter.next() )
49     {
50       removeFromGroup( aChild );
51       if( QGraphicsScene* aScene = aChild->scene() )
52         aScene->removeItem( aChild );
53       delete aChild;
54       aChild = 0;
55     }
56   }
57 }
58
59 //================================================================
60 // Function : addTo
61 // Purpose  : 
62 //================================================================
63 void GraphicsView_Object::addTo( GraphicsView_ViewPort* theViewPort )
64 {
65   if( QGraphicsScene* aScene = theViewPort->scene() )
66     aScene->addItem( this );
67 }
68
69 //================================================================
70 // Function : removeFrom
71 // Purpose  : 
72 //================================================================
73 void GraphicsView_Object::removeFrom( GraphicsView_ViewPort* theViewPort )
74 {
75   if( QGraphicsScene* aScene = theViewPort->scene() )
76     aScene->removeItem( this );
77 }
78
79 //================================================================
80 // Function : setName
81 // Purpose  : 
82 //================================================================
83 void GraphicsView_Object::setName( const QString& theName )
84 {
85   myName = theName;
86 }
87
88 //================================================================
89 // Function : getRect
90 // Purpose  : 
91 //================================================================
92 QRectF GraphicsView_Object::getRect() const
93 {
94   return sceneBoundingRect();
95 }
96
97 //================================================================
98 // Function : checkHighlight
99 // Purpose  : 
100 //================================================================
101 bool GraphicsView_Object::checkHighlight( double theX, double theY, QCursor& theCursor ) const
102 {
103   return !getRect().isNull() && getRect().contains( theX, theY );
104 }
105
106 //================================================================
107 // Function : highlight
108 // Purpose  : 
109 //================================================================
110 bool GraphicsView_Object::highlight( double theX, double theY )
111 {
112   QCursor aCursor;
113   if( myIsHighlighted = isVisible() )
114     myIsHighlighted = checkHighlight( theX, theY, aCursor );
115   return myIsHighlighted;
116 }
117
118 //================================================================
119 // Function : unhighlight
120 // Purpose  : 
121 //================================================================
122 void GraphicsView_Object::unhighlight()
123 {
124   myIsHighlighted = false;
125 }
126
127 //================================================================
128 // Function : select
129 // Purpose  : 
130 //================================================================
131 bool GraphicsView_Object::select( double theX, double theY, const QRectF& theRect )
132 {
133   QCursor aCursor;
134   if( myIsSelected = isVisible() )
135   {
136     if( !theRect.isNull() )
137       myIsSelected = theRect.contains( getRect() );
138     else
139       myIsSelected = checkHighlight( theX, theY, aCursor );
140   }
141   return myIsSelected;
142 }
143
144 //================================================================
145 // Function : unselect
146 // Purpose  : 
147 //================================================================
148 void GraphicsView_Object::unselect()
149 {
150   myIsSelected = false;
151 }
152
153 //================================================================
154 // Function : move
155 // Purpose  : 
156 //================================================================
157 void GraphicsView_Object::move( double theDX, double theDY, bool theIsAtOnce )
158 {
159   if( theIsAtOnce )
160   {
161     finishMove( true );
162     return;
163   }
164
165   myIsMoving = true;
166   moveBy( theDX, theDY );
167 }
168
169 //================================================================
170 // Function : finishMove
171 // Purpose  : 
172 //================================================================
173 bool GraphicsView_Object::finishMove( bool theStatus )
174 {
175   myIsMoving = false;
176   if( theStatus )
177     if( GraphicsView_Scene* aScene = dynamic_cast<GraphicsView_Scene*>( scene() ) )
178       aScene->processRectChanged();
179   return true;
180 }
181
182 //================================================================
183 // Function : setViewTransform
184 // Purpose  : 
185 //================================================================
186 void GraphicsView_Object::setViewTransform( const QTransform& theTransform )
187 {
188   myViewTransform = theTransform;
189 }