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