Salome HOME
new script for testing hypothesis "NbSegments" (PAL8238)
[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 // function : ~SMESHGUI_PatternWidget()
50 // purpose  :
51 //=================================================================================
52 SMESHGUI_PatternWidget::~SMESHGUI_PatternWidget()
53 {
54 }
55
56 //=================================================================================
57 // function : SetPoints()
58 // purpose  :
59 //=================================================================================
60 void SMESHGUI_PatternWidget::SetPoints (PointVector thePoints,
61                                         QValueVector<int> theKeys,
62                                         ConnectivityVector theConnections)
63 {
64   myPoints = thePoints;
65   myKeys = theKeys;
66   myConnections  = theConnections;
67
68   if (!thePoints.size())
69     return;
70
71   myMinU = myMaxU = (thePoints[0]).x;
72   myMinV = myMaxV = (thePoints[0]).y;
73   double x, y;
74
75   for (int i = 1; i < thePoints.size(); i++) {
76     x = (thePoints[i]).x;
77     y = (thePoints[i]).y;
78
79     if (myMinU > x)
80       myMinU = x;
81     if (myMaxU < x)
82       myMaxU = x;
83     if (myMinV > y)
84       myMinV = y;
85     if (myMaxV < y)
86       myMaxV = y;
87   }
88
89   repaint();
90 }
91
92 static const int Shift  = 4; // shift of the point number from point
93 static const int Border = 20;
94
95 //=================================================================================
96 // function : paintEvent()
97 // purpose  :
98 //=================================================================================
99 void SMESHGUI_PatternWidget::paintEvent (QPaintEvent*)
100 {
101   QPainter paint (this);
102   paint.setBrush(Qt::SolidPattern);
103
104   //Draw points
105   const int aRadius = 3; // radius of a point
106
107   for (int i = 0; i < myKeys.size() && i < myPoints.size(); i++) {
108     SMESH::PointStruct aPoint = myPoints[ myKeys[i] ];
109     QPoint aQPnt = MapCoords(aPoint.x, aPoint.y);
110
111     paint.drawPie(aQPnt.x() - aRadius, aQPnt.y() - aRadius, aRadius*2, aRadius*2, 5760, 5760);
112     paint.drawText(aQPnt.x() +  Shift, aQPnt.y() -   Shift, QString::number(i+1));
113   }
114
115   //Draw lines
116   for (int i = 0; i < myConnections.size(); i++) {
117     QValueVector<int> aCVector = myConnections[i];
118
119     if (aCVector.size() == 0)
120       continue;
121
122     SMESH::PointStruct aPoint = myPoints[ aCVector[0] ];
123     const QPoint aBeginPnt = MapCoords(aPoint.x, aPoint.y);
124     QPoint aFirstPnt = aBeginPnt, aSecondPnt;
125
126     for (int j = 1; j < aCVector.size(); j++) {
127       aPoint = myPoints[ aCVector[j] ];
128       aSecondPnt = MapCoords(aPoint.x, aPoint.y);
129       paint.drawLine(aFirstPnt, aSecondPnt);
130       aFirstPnt = aSecondPnt;
131     }
132
133     paint.drawLine(aBeginPnt, aSecondPnt);
134   }
135 }
136
137 //=================================================================================
138 // function : MapCoords()
139 // purpose  :
140 //=================================================================================
141 QPoint SMESHGUI_PatternWidget::MapCoords (const double u, const double v)
142 {
143   int aWidth  = width()  - 2*Border;
144   int aHeight = height() - 2*Border;
145
146   double aUBound = myMaxU - myMinU;
147   double aVBound = myMaxV - myMinV;
148
149   double aUScale = aWidth/aUBound;
150   double aVScale = aHeight/aVBound;
151
152   double aScale;
153   aUScale <= aVScale ? aScale = aUScale : aScale = aVScale;
154
155   double aUMiddle = (myMaxU + myMinU)/2;
156   double aVMiddle = (myMaxV + myMinV)/2;
157
158   int x = int(aWidth/2  + (u - aUMiddle)*aScale + Border - Shift);
159
160   int y = int(aHeight/2 + (aVMiddle - v)*aScale + Border + Shift);
161
162   QPoint aPoint = QPoint(x, y);
163
164   return aPoint;
165 }