Salome HOME
Update copyrights
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_PatternWidget.cxx
1 // Copyright (C) 2007-2019  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  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, or (at your option) any later version.
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.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 // SMESH SMESHGUI : GUI for SMESH component
24 // File   : SMESHGUI_PatternWidget.cxx
25 // Author : Michael ZORIN, Open CASCADE S.A.S.
26 // SMESH includes
27 //
28 #include "SMESHGUI_PatternWidget.h"
29
30 // Qt includes
31 #include <QPainter>
32
33 const int Shift  = 4;  // shift of the point number from point
34 const int Border = 20; // border size
35 const int Radius = 3;  // radius of a point
36
37 //=================================================================================
38 // class    : SMESHGUI_PatternWidget()
39 // purpose  :
40 //=================================================================================
41 SMESHGUI_PatternWidget::SMESHGUI_PatternWidget( QWidget* parent )
42   : QFrame( parent )
43 {
44   myMinU = myMinV = myMaxU = myMaxV = 0;
45   //setMinimumHeight( 150 );
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( const PointVector&        thePoints,
61                                         const QVector<int>&       theKeys,
62                                         const ConnectivityVector& theConnections )
63 {
64   myPoints      = thePoints;
65   myKeys        = theKeys;
66   myConnections = theConnections;
67
68   if ( myPoints.isEmpty() )
69     return;
70
71   myMinU = myMaxU = myPoints[0].x;
72   myMinV = myMaxV = myPoints[0].y;
73
74   for ( int i = 1; i < myPoints.size(); i++ ) {
75     myMinU = qMin( myPoints[i].x, myMinU );
76     myMaxU = qMax( myPoints[i].x, myMaxU );
77     myMinV = qMin( myPoints[i].y, myMinV );
78     myMaxV = qMax( myPoints[i].y, myMaxV );
79   }
80
81   repaint();
82 }
83
84 //=================================================================================
85 // function : paintEvent()
86 // purpose  :
87 //=================================================================================
88 void SMESHGUI_PatternWidget::paintEvent( QPaintEvent* )
89 {
90   QPainter painter( this );
91   painter.setBrush( Qt::SolidPattern );
92
93   // Draw points
94   for ( int i = 0; i < myKeys.size() && i < myPoints.size(); i++ ) {
95     SMESH::PointStruct aPoint = myPoints[ myKeys[i] ];
96     QPoint aQPnt = mapCoords( aPoint.x, aPoint.y );
97
98     painter.drawPie( aQPnt.x() - Radius, aQPnt.y() - Radius, 
99                      Radius * 2, Radius * 2, 0, 360 * 16 );
100     painter.drawText( aQPnt.x() + Shift, aQPnt.y() - Shift, 
101                       QString::number( i+1 ) );
102   }
103
104   // Draw lines
105   for ( int i = 0; i < myConnections.size(); i++ ) {
106     QVector<int> aCVector = myConnections[i];
107
108     if ( aCVector.isEmpty() )
109       continue;
110
111     SMESH::PointStruct aPoint = myPoints[ aCVector[0] ];
112     const QPoint aBeginPnt = mapCoords( aPoint.x, aPoint.y );
113     QPoint aFirstPnt = aBeginPnt, aSecondPnt;
114
115     for ( int j = 1; j < aCVector.size(); j++ ) {
116       aPoint = myPoints[ aCVector[j] ];
117       aSecondPnt = mapCoords( aPoint.x, aPoint.y );
118       painter.drawLine( aFirstPnt, aSecondPnt );
119       aFirstPnt = aSecondPnt;
120     }
121
122     painter.drawLine( aBeginPnt, aSecondPnt );
123   }
124 }
125
126 //=================================================================================
127 // function : mapCoords()
128 // purpose  :
129 //=================================================================================
130 QPoint SMESHGUI_PatternWidget::mapCoords( const double u, const double v )
131 {
132   int aWidth  = width()  - 2 * Border;
133   int aHeight = height() - 2 * Border;
134
135   double aUBound = myMaxU - myMinU;
136   double aVBound = myMaxV - myMinV;
137
138   double aUScale = aWidth  / aUBound;
139   double aVScale = aHeight / aVBound;
140
141   double aScale;
142   aUScale <= aVScale ? aScale = aUScale : aScale = aVScale;
143
144   double aUMiddle = ( myMaxU + myMinU ) / 2;
145   double aVMiddle = ( myMaxV + myMinV ) / 2;
146
147   int x = int( aWidth  / 2 + ( u - aUMiddle ) * aScale + Border - Shift );
148
149   int y = int( aHeight / 2 + ( aVMiddle - v ) * aScale + Border + Shift );
150
151   return QPoint( x, y );
152 }