Salome HOME
+//#define MESSAGE(m) {cout<<m<<endl;}
[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
25 //  Module : SMESH
26 //  $Header:
27 //
28 #include "SMESHGUI_PatternWidget.h"
29
30 //Qt includes
31 #include <qpainter.h>
32 #include <qpoint.h>
33
34
35 //=================================================================================
36 // class    : SMESHGUI_PatternWidget()
37 // purpose  :
38 //=================================================================================
39 SMESHGUI_PatternWidget::SMESHGUI_PatternWidget (QWidget* parent, const char* name, WFlags fl)
40      : QFrame(parent, name, WStyle_Customize)
41 {
42   myMinU =  myMinV =  myMaxU =  myMaxV = 0;
43   setMinimumHeight(150);
44   repaint();
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 (PointVector thePoints,
60                                         QValueVector<int> theKeys,
61                                         ConnectivityVector theConnections)
62 {
63   myPoints = thePoints;
64   myKeys = theKeys;
65   myConnections  = theConnections;
66
67   if (!thePoints.size())
68     return;
69
70   myMinU = myMaxU = (thePoints[0]).x;
71   myMinV = myMaxV = (thePoints[0]).y;
72   double x, y;
73
74   for (int i = 1; i < thePoints.size(); i++) {
75     x = (thePoints[i]).x;
76     y = (thePoints[i]).y;
77
78     if (myMinU > x)
79       myMinU = x;
80     if (myMaxU < x)
81       myMaxU = x;
82     if (myMinV > y)
83       myMinV = y;
84     if (myMaxV < y)
85       myMaxV = y;
86   }
87
88   repaint();
89 }
90
91 static const int Shift  = 4; // shift of the point number from point
92 static const int Border = 20;
93
94 //=================================================================================
95 // function : paintEvent()
96 // purpose  :
97 //=================================================================================
98 void SMESHGUI_PatternWidget::paintEvent (QPaintEvent*)
99 {
100   QPainter paint (this);
101   paint.setBrush(Qt::SolidPattern);
102
103   //Draw points
104   const int aRadius = 3; // radius of a point
105
106   for (int i = 0; i < myKeys.size() && i < myPoints.size(); i++) {
107     SMESH::PointStruct aPoint = myPoints[ myKeys[i] ];
108     QPoint aQPnt = MapCoords(aPoint.x, aPoint.y);
109
110     paint.drawPie(aQPnt.x() - aRadius, aQPnt.y() - aRadius, aRadius*2, aRadius*2, 5760, 5760);
111     paint.drawText(aQPnt.x() +  Shift, aQPnt.y() -   Shift, QString::number(i+1));
112   }
113
114   //Draw lines
115   for (int i = 0; i < myConnections.size(); i++) {
116     QValueVector<int> aCVector = myConnections[i];
117
118     if (aCVector.size() == 0)
119       continue;
120
121     SMESH::PointStruct aPoint = myPoints[ aCVector[0] ];
122     const QPoint aBeginPnt = MapCoords(aPoint.x, aPoint.y);
123     QPoint aFirstPnt = aBeginPnt, aSecondPnt;
124
125     for (int j = 1; j < aCVector.size(); j++) {
126       aPoint = myPoints[ aCVector[j] ];
127       aSecondPnt = MapCoords(aPoint.x, aPoint.y);
128       paint.drawLine(aFirstPnt, aSecondPnt);
129       aFirstPnt = aSecondPnt;
130     }
131
132     paint.drawLine(aBeginPnt, aSecondPnt);
133   }
134 }
135
136 //=================================================================================
137 // function : MapCoords()
138 // purpose  :
139 //=================================================================================
140 QPoint SMESHGUI_PatternWidget::MapCoords (const double u, const double v)
141 {
142   int aWidth  = width()  - 2*Border;
143   int aHeight = height() - 2*Border;
144
145   double aUBound = myMaxU - myMinU;
146   double aVBound = myMaxV - myMinV;
147
148   double aUScale = aWidth/aUBound;
149   double aVScale = aHeight/aVBound;
150
151   double aScale;
152   aUScale <= aVScale ? aScale = aUScale : aScale = aVScale;
153
154   double aUMiddle = (myMaxU + myMinU)/2;
155   double aVMiddle = (myMaxV + myMinV)/2;
156
157   int x = int(aWidth/2  + (u - aUMiddle)*aScale + Border - Shift);
158
159   int y = int(aHeight/2 + (aVMiddle - v)*aScale + Border + Shift);
160
161   QPoint aPoint = QPoint(x, y);
162
163   return aPoint;
164 }