Back to index

salome-smesh  6.5.0
SMESHGUI_PatternWidget.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 // SMESH SMESHGUI : GUI for SMESH component
00024 // File   : SMESHGUI_PatternWidget.cxx
00025 // Author : Michael ZORIN, Open CASCADE S.A.S.
00026 // SMESH includes
00027 //
00028 #include "SMESHGUI_PatternWidget.h"
00029 
00030 // Qt includes
00031 #include <QPainter>
00032 
00033 const int Shift  = 4;  // shift of the point number from point
00034 const int Border = 20; // border size
00035 const int Radius = 3;  // radius of a point
00036 
00037 //=================================================================================
00038 // class    : SMESHGUI_PatternWidget()
00039 // purpose  :
00040 //=================================================================================
00041 SMESHGUI_PatternWidget::SMESHGUI_PatternWidget( QWidget* parent )
00042   : QFrame( parent )
00043 {
00044   myMinU = myMinV = myMaxU = myMaxV = 0;
00045   setMinimumHeight( 150 );
00046 }
00047 
00048 //=================================================================================
00049 // function : ~SMESHGUI_PatternWidget()
00050 // purpose  :
00051 //=================================================================================
00052 SMESHGUI_PatternWidget::~SMESHGUI_PatternWidget()
00053 {
00054 }
00055 
00056 //=================================================================================
00057 // function : SetPoints()
00058 // purpose  :
00059 //=================================================================================
00060 void SMESHGUI_PatternWidget::SetPoints( const PointVector&        thePoints,
00061                                         const QVector<int>&       theKeys,
00062                                         const ConnectivityVector& theConnections )
00063 {
00064   myPoints      = thePoints;
00065   myKeys        = theKeys;
00066   myConnections = theConnections;
00067 
00068   if ( myPoints.isEmpty() )
00069     return;
00070 
00071   myMinU = myMaxU = myPoints[0].x;
00072   myMinV = myMaxV = myPoints[0].y;
00073 
00074   for ( int i = 1; i < myPoints.size(); i++ ) {
00075     myMinU = qMin( myPoints[i].x, myMinU );
00076     myMaxU = qMax( myPoints[i].x, myMaxU );
00077     myMinV = qMin( myPoints[i].y, myMinV );
00078     myMaxV = qMax( myPoints[i].y, myMaxV );
00079   }
00080 
00081   repaint();
00082 }
00083 
00084 //=================================================================================
00085 // function : paintEvent()
00086 // purpose  :
00087 //=================================================================================
00088 void SMESHGUI_PatternWidget::paintEvent( QPaintEvent* )
00089 {
00090   QPainter painter( this );
00091   painter.setBrush( Qt::SolidPattern );
00092 
00093   // Draw points
00094   for ( int i = 0; i < myKeys.size() && i < myPoints.size(); i++ ) {
00095     SMESH::PointStruct aPoint = myPoints[ myKeys[i] ];
00096     QPoint aQPnt = mapCoords( aPoint.x, aPoint.y );
00097 
00098     painter.drawPie( aQPnt.x() - Radius, aQPnt.y() - Radius, 
00099                      Radius * 2, Radius * 2, 0, 360 * 16 );
00100     painter.drawText( aQPnt.x() + Shift, aQPnt.y() - Shift, 
00101                       QString::number( i+1 ) );
00102   }
00103 
00104   // Draw lines
00105   for ( int i = 0; i < myConnections.size(); i++ ) {
00106     QVector<int> aCVector = myConnections[i];
00107 
00108     if ( aCVector.isEmpty() )
00109       continue;
00110 
00111     SMESH::PointStruct aPoint = myPoints[ aCVector[0] ];
00112     const QPoint aBeginPnt = mapCoords( aPoint.x, aPoint.y );
00113     QPoint aFirstPnt = aBeginPnt, aSecondPnt;
00114 
00115     for ( int j = 1; j < aCVector.size(); j++ ) {
00116       aPoint = myPoints[ aCVector[j] ];
00117       aSecondPnt = mapCoords( aPoint.x, aPoint.y );
00118       painter.drawLine( aFirstPnt, aSecondPnt );
00119       aFirstPnt = aSecondPnt;
00120     }
00121 
00122     painter.drawLine( aBeginPnt, aSecondPnt );
00123   }
00124 }
00125 
00126 //=================================================================================
00127 // function : mapCoords()
00128 // purpose  :
00129 //=================================================================================
00130 QPoint SMESHGUI_PatternWidget::mapCoords( const double u, const double v )
00131 {
00132   int aWidth  = width()  - 2 * Border;
00133   int aHeight = height() - 2 * Border;
00134 
00135   double aUBound = myMaxU - myMinU;
00136   double aVBound = myMaxV - myMinV;
00137 
00138   double aUScale = aWidth  / aUBound;
00139   double aVScale = aHeight / aVBound;
00140 
00141   double aScale;
00142   aUScale <= aVScale ? aScale = aUScale : aScale = aVScale;
00143 
00144   double aUMiddle = ( myMaxU + myMinU ) / 2;
00145   double aVMiddle = ( myMaxV + myMinV ) / 2;
00146 
00147   int x = int( aWidth  / 2 + ( u - aUMiddle ) * aScale + Border - Shift );
00148 
00149   int y = int( aHeight / 2 + ( aVMiddle - v ) * aScale + Border + Shift );
00150 
00151   return QPoint( x, y );
00152 }