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