Salome HOME
fix PAL8469. Updating the global mesh icon when local hypotheses are edited
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_PatternWidget.cxx
1 //  SMESH SMESHGUI : GUI for SMESH component
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
6 //  This library is free software; you can redistribute it and/or 
7 //  modify it under the terms of the GNU Lesser General Public 
8 //  License as published by the Free Software Foundation; either 
9 //  version 2.1 of the License. 
10 // 
11 //  This library is distributed in the hope that it will be useful, 
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
14 //  Lesser General Public License for more details. 
15 // 
16 //  You should have received a copy of the GNU Lesser General Public 
17 //  License along with this library; if not, write to the Free Software 
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
19 // 
20 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : SMESHGUI_PatternWidget.cxx
25 //  Author : Michael ZORIN
26 //  Module : SMESH
27 //  $Header: 
28
29 #include "SMESHGUI_PatternWidget.h"
30
31 //Qt includes
32 #include <qpainter.h>
33 #include <qpoint.h>
34
35
36 //=================================================================================
37 // class    : SMESHGUI_PatternWidget()
38 // purpose  : 
39 //=================================================================================
40 SMESHGUI_PatternWidget::SMESHGUI_PatternWidget( QWidget* parent, const char* name, WFlags fl )
41                                   : QFrame( parent, name, WStyle_Customize)
42 {
43   myMinU =  myMinV =  myMaxU =  myMaxV = 0;
44   setMinimumHeight(150);
45   repaint();
46 }
47
48
49
50 //=================================================================================
51 // function : ~SMESHGUI_PatternWidget()
52 // purpose  : 
53 //=================================================================================
54 SMESHGUI_PatternWidget::~SMESHGUI_PatternWidget()
55 {
56 }
57
58
59 //=================================================================================
60 // function : SMESHGUI_PatternWidget::SetPoints
61 // purpose  : 
62 //=================================================================================
63 void SMESHGUI_PatternWidget::SetPoints( PointVector thePoints,  QValueVector<int> theKeys, ConnectivityVector theConnections )
64 {
65   myPoints = thePoints;
66   myKeys = theKeys;
67   myConnections  = theConnections;
68   
69   if (!thePoints.size())
70     return;
71   
72   myMinU = myMaxU = (thePoints[0]).x;
73   myMinV = myMaxV = (thePoints[0]).y;
74   double x, y;
75   
76   for (int i = 1; i < thePoints.size(); i++)
77     {
78       x = (thePoints[i]).x;
79       y = (thePoints[i]).y;
80       
81       if ( myMinU > x )
82         myMinU = x;
83       if ( myMaxU < x)
84         myMaxU = x;
85       if ( myMinV > y )
86         myMinV = y;
87       if ( myMaxV < y)
88         myMaxV = y;
89     }
90   
91   repaint();
92 }
93
94 static const int Shift  = 4; // shift of the point number from point
95 static const int Border = 20;
96
97 //=================================================================================
98 // function : SMESHGUI_PatternWidget::paintEvent
99 // purpose  : 
100 //=================================================================================
101 void SMESHGUI_PatternWidget::paintEvent( QPaintEvent * )
102 {
103   QPainter paint( this );
104   paint.setBrush (Qt::SolidPattern ); 
105
106   //Draw points
107   const int aRadius = 3; // radius of a point
108   
109   for (int i = 0; i < myKeys.size() && i < myPoints.size(); i++)
110     {
111       SMESH::PointStruct aPoint = myPoints[ myKeys[i] ];
112       QPoint aQPnt = MapCoords( aPoint.x, aPoint.y);
113       
114       paint.drawPie( aQPnt.x() - aRadius, aQPnt.y() - aRadius, aRadius*2, aRadius*2, 5760, 5760 );
115       paint.drawText( aQPnt.x() +  Shift, aQPnt.y() -   Shift, QString::number( i+1 ) );
116     }
117   
118   //Draw lines
119   for (int i = 0; i < myConnections.size(); i++)
120     {
121       QValueVector<int> aCVector = myConnections[i];
122
123       if ( aCVector.size() == 0 )
124         continue;
125       
126       SMESH::PointStruct aPoint = myPoints[ aCVector[0] ];
127       const QPoint aBeginPnt = MapCoords( aPoint.x, aPoint.y);
128       QPoint aFirstPnt = aBeginPnt, aSecondPnt;  
129       
130       for (int j = 1; j < aCVector.size(); j++)
131         {
132           aPoint = myPoints[ aCVector[j] ];
133           aSecondPnt = MapCoords( aPoint.x, aPoint.y);
134           paint.drawLine(aFirstPnt, aSecondPnt);
135           aFirstPnt = aSecondPnt;
136         }
137       
138       paint.drawLine(aBeginPnt, aSecondPnt);
139     }
140 }
141
142
143 //=================================================================================
144 // function : SMESHGUI_PatternWidget::MapCoords
145 // purpose  : 
146 //=================================================================================
147 QPoint SMESHGUI_PatternWidget::MapCoords( const double u, const double v )
148 {
149   int aWidth  = width()  - 2*Border;
150   int aHeight = height() - 2*Border;
151
152   double aUBound = myMaxU - myMinU;
153   double aVBound = myMaxV - myMinV;
154   
155   double aUScale = aWidth/aUBound;
156   double aVScale = aHeight/aVBound;
157   
158   double aScale;
159   aUScale <= aVScale ? aScale = aUScale : aScale = aVScale;
160
161   double aUMiddle = ( myMaxU + myMinU )/2;
162   double aVMiddle = ( myMaxV + myMinV )/2;
163   
164   int x = int(  aWidth/2  + (u - aUMiddle)*aScale + Border - Shift);
165   
166   int y = int(  aHeight/2 + (aVMiddle - v)*aScale + Border + Shift);
167   
168   QPoint aPoint = QPoint(x, y);
169   
170   return aPoint;
171 }