Salome HOME
selection on object browser for shapes in fitShapesPointsToMesheEdges dialog
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_PrsImageFrame.cxx
1 // Copyright (C) 2014-2015  EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
6 //
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10 // Lesser General Public License for more details.
11 //
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include "HYDROGUI_PrsImageFrame.h"
20
21 #include "HYDROGUI_PrsImage.h"
22
23 #include <QPainter>
24
25 //#define _DEVDEBUG_
26 #include "HYDRO_trace.hxx"
27
28 #define FRAME_Z_VALUE   1000
29 #define ANCHOR_RADIUS   3
30 #define EPSILON         1e-6
31
32 //=======================================================================
33 // name    : HYDROGUI_PrsImageFrame
34 // Purpose : Constructor
35 //=======================================================================
36 HYDROGUI_PrsImageFrame::HYDROGUI_PrsImageFrame( HYDROGUI_PrsImage* thePrsImage )
37 : GraphicsView_Object( thePrsImage ),
38   myPrsImage( thePrsImage )
39 {
40 }
41
42 //=======================================================================
43 // name    : HYDROGUI_PrsImageFrame
44 // Purpose : Destructor
45 //=======================================================================
46 HYDROGUI_PrsImageFrame::~HYDROGUI_PrsImageFrame()
47 {
48 }
49
50 //================================================================
51 // Function : boundingRect
52 // Purpose  : 
53 //================================================================
54 QRectF HYDROGUI_PrsImageFrame::boundingRect() const
55 {
56   QRectF aRect;
57   AnchorMapIterator anIter( myAnchorMap );
58   while( anIter.hasNext() )
59   {
60     if( QGraphicsEllipseItem* anAnchorItem = anIter.next().value() )
61     {
62       QRectF anAnchorRect = anAnchorItem->boundingRect();
63       if( !anAnchorRect.isNull() )
64       {
65         if( aRect.isNull() )
66           aRect = anAnchorRect;
67         else
68           aRect |= anAnchorRect;
69       }
70     }
71   }
72   return aRect;
73 }
74
75 //================================================================
76 // Function : compute
77 // Purpose  : 
78 //================================================================
79 void HYDROGUI_PrsImageFrame::compute()
80 {
81   DEBTRACE("compute");
82   if( myAnchorMap.isEmpty() )
83   {
84     for( int aType = TopLeft; aType <= BottomRight; aType++ )
85     {
86       UnscaledGraphicsEllipseItem* anAnchorItem = new UnscaledGraphicsEllipseItem( this );
87       anAnchorItem->setBrush( QBrush( Qt::white ) );
88       myAnchorMap.insert( aType, anAnchorItem );
89       addToGroup( anAnchorItem );
90     }
91   }
92
93   setZValue( FRAME_Z_VALUE );
94
95   computeAnchorItems();
96 //  QTransform transform;
97 //  transform.scale(-0.5, 1.0);
98 //  this->setTransform(transform, false);
99   updateVisibility();
100 }
101
102 //================================================================
103 // Function : computeAnchorItems
104 // Purpose  : 
105 //================================================================
106 void HYDROGUI_PrsImageFrame::computeAnchorItems()
107 {
108   if( !myPrsImage )
109     return;
110
111   QRectF aRect = myPrsImage->boundingRect();
112
113   QMap<int, QPointF> anAnchorPointMap;
114   anAnchorPointMap[ TopLeft ] = aRect.topLeft();
115   anAnchorPointMap[ TopRight ] = aRect.topRight();
116   anAnchorPointMap[ BottomLeft ] = aRect.bottomLeft();
117   anAnchorPointMap[ BottomRight ] = aRect.bottomRight();
118
119   qreal ar = ANCHOR_RADIUS;
120   QMapIterator<int, QPointF> anIter( anAnchorPointMap );
121   while( anIter.hasNext() )
122   {
123     int anAnchorType = anIter.next().key();
124     const QPointF& anAnchorPoint = anIter.value();
125
126     QRectF anAnchorRect( anAnchorPoint - QPointF( ar, ar ), QSizeF( ar * 2, ar * 2 ) );
127     myAnchorMap[ anAnchorType ]->setRect( anAnchorRect );
128     myAnchorMap[ anAnchorType ]->setBasePoint( anAnchorPoint );
129   }
130 }
131
132 //================================================================
133 // Function : updateVisibility
134 // Purpose  : 
135 //================================================================
136 void HYDROGUI_PrsImageFrame::updateVisibility()
137 {
138   if (myPrsImage)
139     DEBTRACE("updateVisibility " << myPrsImage->isSelected());
140   setVisible( myPrsImage && myPrsImage->isSelected() );
141 }
142
143 //=======================================================================
144 // name    : HYDROGUI_PrsImageFrame::UnscaledGraphicsEllipseItem
145 // Purpose : Constructor
146 //=======================================================================
147 HYDROGUI_PrsImageFrame::UnscaledGraphicsEllipseItem::UnscaledGraphicsEllipseItem( QGraphicsItem* theParent )
148 : QGraphicsEllipseItem( theParent )
149 {
150 }
151
152 //=======================================================================
153 // name    : HYDROGUI_PrsImageFrame::UnscaledGraphicsEllipseItem
154 // Purpose : Destructor
155 //=======================================================================
156 HYDROGUI_PrsImageFrame::UnscaledGraphicsEllipseItem::~UnscaledGraphicsEllipseItem()
157 {
158 }
159
160 //================================================================
161 // Function : boundingRect
162 // Purpose  : 
163 //================================================================
164 QRectF HYDROGUI_PrsImageFrame::UnscaledGraphicsEllipseItem::boundingRect() const
165 {
166   QRectF aRect = QGraphicsEllipseItem::boundingRect();
167
168   GraphicsView_Object* aParent = dynamic_cast<GraphicsView_Object*>( parentItem() );
169   if( !aParent )
170     return aRect;
171
172   // take into account a transformation of the base image item
173   double aXScale = 1.0;
174   double aYScale = 1.0;
175   if( QGraphicsItem* aGrandParent = aParent->parentItem() )
176   {
177     QTransform aTransform = aGrandParent->transform();
178     QLineF aLine( 0, 0, 1, 1 );
179     aLine = aTransform.map( aLine );
180     aXScale = aLine.dx();
181     aYScale = aLine.dy();
182   }
183
184   QTransform aViewTransform = aParent->getViewTransform();
185   double aScale = aViewTransform.m11(); // same as m22(), viewer specific
186   if( fabs( aScale ) < EPSILON || fabs( aXScale ) < EPSILON || fabs( aYScale ) < EPSILON)
187     return aRect;
188
189   QPointF aCenter = aRect.center();
190   double aWidth = aRect.width() / aScale / aXScale;
191   double aHeight = aRect.height() / aScale / aYScale;
192
193   aRect = QRectF( aCenter.x() - aWidth / 2, aCenter.y() - aHeight / 2, aWidth, aHeight );
194   return aRect;
195 }
196
197 //================================================================
198 // Function : GenerateTranslationOnlyTransform
199 // Purpose  : 
200 //================================================================
201 static QTransform GenerateTranslationOnlyTransform( const QTransform &theOriginalTransform,
202                                                     const QPointF &theTargetPoint )
203 {
204   qreal dx = theOriginalTransform.m11() * theTargetPoint.x() - theTargetPoint.x() +
205              theOriginalTransform.m21() * theTargetPoint.y() +
206              theOriginalTransform.m31();
207   qreal dy = theOriginalTransform.m22() * theTargetPoint.y() - theTargetPoint.y() +
208              theOriginalTransform.m12() * theTargetPoint.x() +
209              theOriginalTransform.m32();
210   return QTransform::fromTranslate( dx, dy );
211 }
212
213 //================================================================
214 // Function : paint
215 // Purpose  : 
216 //================================================================
217 void HYDROGUI_PrsImageFrame::UnscaledGraphicsEllipseItem::paint(
218   QPainter* thePainter,
219   const QStyleOptionGraphicsItem* theOption,
220   QWidget* theWidget )
221 {
222   thePainter->save();
223   thePainter->setTransform( GenerateTranslationOnlyTransform( thePainter->transform(),
224                                                               myBasePoint ) );
225   QGraphicsEllipseItem::paint( thePainter, theOption, theWidget );
226   thePainter->restore();
227 }