Salome HOME
7a5d089e663bbd342b14755e19d7314a9ef64f85
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ImportImageDlg.cxx
1 // Copyright (C) 2007-2013  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
23 #include "HYDROGUI_ImportImageDlg.h"
24
25 #include "HYDROGUI_PrsImage.h"
26 #include "HYDROGUI_Tool.h"
27
28 #include <SUIT_ResourceMgr.h>
29 #include <SUIT_Session.h>
30
31 #include <QDoubleValidator>
32 #include <QFileDialog>
33 #include <QGroupBox>
34 #include <QIntValidator>
35 #include <QLabel>
36 #include <QLayout>
37 #include <QLineEdit>
38 #include <QPainter>
39 #include <QPicture>
40 #include <QPushButton>
41 #include <QToolButton>
42
43 HYDROGUI_ImportImageDlg::HYDROGUI_ImportImageDlg( HYDROGUI_Module* theModule, const QString& theTitle )
44 : HYDROGUI_InputPanel( theModule, theTitle )
45 {
46   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
47
48   // Import image from file
49   myFileGroup = new QGroupBox( tr( "IMPORT_IMAGE_FROM_FILE" ) );
50
51   QLabel* aFileNameLabel = new QLabel( tr( "FILE_NAME" ), myFileGroup );
52
53   myFileName = new QLineEdit( myFileGroup );
54   myFileName->setReadOnly( true );
55
56   QToolButton* aBrowseBtn = new QToolButton( myFileGroup );
57   aBrowseBtn->setIcon( aResMgr->loadPixmap( "HYDRO", tr( "BROWSE_ICO" ) ) );
58
59   QBoxLayout* aFileLayout = new QHBoxLayout( myFileGroup );
60   aFileLayout->setMargin( 5 );
61   aFileLayout->setSpacing( 5 );
62   aFileLayout->addWidget( aFileNameLabel );
63   aFileLayout->addWidget( myFileName );
64   aFileLayout->addWidget( aBrowseBtn );
65
66   // Mapping
67   myMappingGroup = new QGroupBox( tr( "MAPPING" ) );
68
69   QGridLayout* aMappingLayout = new QGridLayout( myMappingGroup );
70   aMappingLayout->setMargin( 5 );
71   aMappingLayout->setSpacing( 5 );
72
73   // Make a pixmap for arrow
74   QPixmap anArrowPixmap = aResMgr->loadPixmap( "HYDRO", tr( "ARROW_RIGHT_ICO" ) );
75
76   QPicture anArrowPicture;
77   anArrowPicture.setBoundingRect( QRect( QPoint( 0, 0 ), anArrowPixmap.size() ) );
78
79   QPainter aPainter;
80   aPainter.begin( &anArrowPicture );
81   aPainter.drawPixmap( 0, 0, anArrowPixmap );
82   aPainter.end();
83
84   for( int aPointType = HYDROGUI_PrsImage::PointA;
85        aPointType <= HYDROGUI_PrsImage::PointC; aPointType++ )
86   {
87     QString aPointStr;
88     switch( aPointType )
89     {
90       case HYDROGUI_PrsImage::PointA: aPointStr = tr( "ACTIVATE_POINT_A_SELECTION" ); break;
91       case HYDROGUI_PrsImage::PointB: aPointStr = tr( "ACTIVATE_POINT_B_SELECTION" ); break;
92       case HYDROGUI_PrsImage::PointC: aPointStr = tr( "ACTIVATE_POINT_C_SELECTION" ); break;
93     }
94     QPushButton* aPointBtn = new QPushButton( aPointStr, myMappingGroup );
95     aPointBtn->setCheckable( true );
96
97     QLabel* aPointXLabel = new QLabel( "X", myMappingGroup );
98     QLabel* aPointYLabel = new QLabel( "Y", myMappingGroup );
99
100     QLineEdit* aPointX = new QLineEdit( myMappingGroup );
101     QLineEdit* aPointY = new QLineEdit( myMappingGroup );
102
103     aPointX->setReadOnly( true );
104     aPointY->setReadOnly( true );
105
106     //QLabel* aPointArrowLabel = new QLabel( ">", myMappingGroup );
107     QLabel* aPointArrowLabel = new QLabel( myMappingGroup );
108     aPointArrowLabel->setPicture( anArrowPicture );
109
110     QLabel* aPointXDegLabel = new QLabel( QChar( 0x00B0 ), myMappingGroup );
111     QLabel* aPointYDegLabel = new QLabel( QChar( 0x00B0 ), myMappingGroup );
112     QLabel* aPointXMinLabel = new QLabel( "'", myMappingGroup );
113     QLabel* aPointYMinLabel = new QLabel( "'", myMappingGroup );
114     QLabel* aPointXSecLabel = new QLabel( "\"", myMappingGroup );
115     QLabel* aPointYSecLabel = new QLabel( "\"", myMappingGroup );
116
117     QLineEdit* aPointXDeg = new QLineEdit( myMappingGroup );
118     QLineEdit* aPointYDeg = new QLineEdit( myMappingGroup );
119     QLineEdit* aPointXMin = new QLineEdit( myMappingGroup );
120     QLineEdit* aPointYMin = new QLineEdit( myMappingGroup );
121     QLineEdit* aPointXSec = new QLineEdit( myMappingGroup );
122     QLineEdit* aPointYSec = new QLineEdit( myMappingGroup );
123
124     QIntValidator* aXDegValidator = new QIntValidator( -180, 180, this );
125     QIntValidator* aYDegValidator = new QIntValidator( -90, 90, this );
126     QIntValidator* aMinValidator = new QIntValidator( 0, 59, this );
127     QDoubleValidator* aSecValidator = new QDoubleValidator( 0, 59.9999, 4, this );
128
129     aPointXDeg->setValidator( aXDegValidator );
130     aPointYDeg->setValidator( aYDegValidator );
131     aPointXMin->setValidator( aMinValidator );
132     aPointYMin->setValidator( aMinValidator );
133     aPointXSec->setValidator( aSecValidator );
134     aPointYSec->setValidator( aSecValidator );
135
136     int aRow = 4 * aPointType;
137     aMappingLayout->addWidget( aPointBtn,        aRow,     0, 1, 9 );
138
139     aMappingLayout->addWidget( aPointXLabel,     aRow + 1, 0 );
140     aMappingLayout->addWidget( aPointX,          aRow + 1, 1 );
141     aMappingLayout->addWidget( aPointArrowLabel, aRow + 1, 2, 2, 1 );
142     aMappingLayout->addWidget( aPointXDeg,       aRow + 1, 3 );
143     aMappingLayout->addWidget( aPointXDegLabel,  aRow + 1, 4 );
144     aMappingLayout->addWidget( aPointXMin,       aRow + 1, 5 );
145     aMappingLayout->addWidget( aPointXMinLabel,  aRow + 1, 6 );
146     aMappingLayout->addWidget( aPointXSec,       aRow + 1, 7 );
147     aMappingLayout->addWidget( aPointXSecLabel,  aRow + 1, 8 );
148
149     aMappingLayout->addWidget( aPointYLabel,     aRow + 2, 0 );
150     aMappingLayout->addWidget( aPointY,          aRow + 2, 1 );
151     aMappingLayout->addWidget( aPointYDeg,       aRow + 2, 3 );
152     aMappingLayout->addWidget( aPointYDegLabel,  aRow + 2, 4 );
153     aMappingLayout->addWidget( aPointYMin,       aRow + 2, 5 );
154     aMappingLayout->addWidget( aPointYMinLabel,  aRow + 2, 6 );
155     aMappingLayout->addWidget( aPointYSec,       aRow + 2, 7 );
156     aMappingLayout->addWidget( aPointYSecLabel,  aRow + 2, 8 );
157
158     if( aPointType != HYDROGUI_PrsImage::PointC )
159     {
160       QFrame* aLine = new QFrame( myMappingGroup );
161       aLine->setFrameShape( QFrame::HLine );
162       aLine->setFrameShadow( QFrame::Sunken );
163       aMappingLayout->addWidget( aLine, aRow + 3, 0, 1, 9 );
164     }
165
166     myPointBtnMap[ aPointType ] = aPointBtn;
167     myPointXMap[ aPointType ] = aPointX;
168     myPointYMap[ aPointType ] = aPointY;
169     myPointXDegMap[ aPointType ] = aPointXDeg;
170     myPointYDegMap[ aPointType ] = aPointYDeg;
171     myPointXMinMap[ aPointType ] = aPointXMin;
172     myPointYMinMap[ aPointType ] = aPointYMin;
173     myPointXSecMap[ aPointType ] = aPointXSec;
174     myPointYSecMap[ aPointType ] = aPointYSec;
175
176     connect( aPointBtn, SIGNAL( toggled( bool ) ), this, SLOT( onPointBtnToggled( bool ) ) );
177   }
178   aMappingLayout->setColumnStretch( 1, 1 ); // x
179   aMappingLayout->setColumnStretch( 3, 1 ); // degrees
180   aMappingLayout->setColumnStretch( 5, 1 ); // minutes
181   aMappingLayout->setColumnStretch( 7, 2 ); // seconds (double with 4 digits)
182
183   // Common
184   addWidget( myFileGroup, 0, 0 );
185   addWidget( myMappingGroup, 1, 0 );
186
187   setRowStretch();
188
189   connect( aBrowseBtn, SIGNAL( clicked() ), this, SLOT( onBrowse() ) );
190 }
191
192 HYDROGUI_ImportImageDlg::~HYDROGUI_ImportImageDlg()
193 {
194 }
195
196 void HYDROGUI_ImportImageDlg::setIsEdit( const bool theIsEdit )
197 {
198   myFileGroup->setVisible( !theIsEdit );
199   myMappingGroup->setEnabled( theIsEdit );
200 }
201
202 void HYDROGUI_ImportImageDlg::reset()
203 {
204   myFileName->clear();
205   for( int aPointType = HYDROGUI_PrsImage::PointA;
206        aPointType <= HYDROGUI_PrsImage::PointC; aPointType++ )
207   {
208     QPushButton* aBtn = myPointBtnMap[ aPointType ];
209     bool anIsBlocked = aBtn->blockSignals( true );
210     aBtn->setChecked( false );
211     aBtn->blockSignals( anIsBlocked );
212
213     myPointXMap[ aPointType ]->clear();
214     myPointYMap[ aPointType ]->clear();
215     myPointXDegMap[ aPointType ]->clear();
216     myPointYDegMap[ aPointType ]->clear();
217     myPointXMinMap[ aPointType ]->clear();
218     myPointYMinMap[ aPointType ]->clear();
219     myPointXSecMap[ aPointType ]->clear();
220     myPointYSecMap[ aPointType ]->clear();
221   }
222   myMappingGroup->setEnabled( false );
223 }
224
225 void HYDROGUI_ImportImageDlg::setTransformationDataMap( const TransformationDataMap& theMap,
226                                                         const bool theIsOnlyInput )
227 {
228   for( int aPointType = HYDROGUI_PrsImage::PointA;
229        aPointType <= HYDROGUI_PrsImage::PointC; aPointType++ )
230   {
231     if( theMap.contains( aPointType ) )
232     {
233       const TransformationData& aData = theMap[ aPointType ];
234       myPointXMap[ aPointType ]->setText( QString::number( aData.first.x() ) );
235       myPointYMap[ aPointType ]->setText( QString::number( aData.first.y() ) );
236
237       if( !theIsOnlyInput )
238       {
239         QPointF aPoint = aData.second;
240         int aXDeg = 0, aYDeg = 0;
241         int aXMin = 0, aYMin = 0;
242         double aXSec = 0, aYSec = 0;
243         HYDROGUI_Tool::DoubleToLambert( aPoint.x(), aXDeg, aXMin, aXSec );
244         HYDROGUI_Tool::DoubleToLambert( aPoint.y(), aYDeg, aYMin, aYSec );
245
246         myPointXDegMap[ aPointType ]->setText( QString::number( aXDeg ) );
247         myPointYDegMap[ aPointType ]->setText( QString::number( aYDeg ) );
248         myPointXMinMap[ aPointType ]->setText( QString::number( aXMin ) );
249         myPointYMinMap[ aPointType ]->setText( QString::number( aYMin ) );
250         myPointXSecMap[ aPointType ]->setText( QString::number( aXSec ) );
251         myPointYSecMap[ aPointType ]->setText( QString::number( aYSec ) );
252       }
253     }
254   }
255 }
256
257 bool HYDROGUI_ImportImageDlg::getTransformationDataMap( TransformationDataMap& theMap ) const
258 {
259   theMap.clear();
260   for( int aPointType = HYDROGUI_PrsImage::PointA;
261        aPointType <= HYDROGUI_PrsImage::PointC; aPointType++ )
262   {
263     bool anIsOk[8];
264     for( int i = 0; i < 8; i++ )
265       anIsOk[ i ] = false;
266
267     int aX1 = myPointXMap[ aPointType ]->text().toInt( &anIsOk[0] );
268     int aY1 = myPointYMap[ aPointType ]->text().toInt( &anIsOk[1] );
269     int aXDeg = myPointXDegMap[ aPointType ]->text().toInt( &anIsOk[2] );
270     int aYDeg = myPointYDegMap[ aPointType ]->text().toInt( &anIsOk[3] );
271     int aXMin = myPointXMinMap[ aPointType ]->text().toInt( &anIsOk[4] );
272     int aYMin = myPointYMinMap[ aPointType ]->text().toInt( &anIsOk[5] );
273     double aXSec = myPointXSecMap[ aPointType ]->text().toDouble( &anIsOk[6] );
274     double aYSec = myPointYSecMap[ aPointType ]->text().toDouble( &anIsOk[7] );
275
276     for( int i = 0; i < 8; i++ )
277       if( !anIsOk[ i ] )
278         return false;
279
280     double aX2 = 0, aY2 = 0;
281     HYDROGUI_Tool::LambertToDouble( aXDeg, aXMin, aXSec, aX2 );
282     HYDROGUI_Tool::LambertToDouble( aYDeg, aYMin, aYSec, aY2 );
283
284     TransformationData aData( QPoint( aX1, aY1 ), QPointF( aX2, aY2 ) );
285     theMap[ aPointType ] = aData;
286   }
287   return true;
288 }
289
290 void HYDROGUI_ImportImageDlg::initializePointSelection()
291 {
292   myPointBtnMap[ HYDROGUI_PrsImage::PointA ]->setChecked( true );
293
294   // ouv: tmp
295   myPointXDegMap[ HYDROGUI_PrsImage::PointA ]->setText( "50" );
296   myPointXMinMap[ HYDROGUI_PrsImage::PointA ]->setText( "0" );
297   myPointXSecMap[ HYDROGUI_PrsImage::PointA ]->setText( "0" );
298   myPointYDegMap[ HYDROGUI_PrsImage::PointA ]->setText( "50" );
299   myPointYMinMap[ HYDROGUI_PrsImage::PointA ]->setText( "0" );
300   myPointYSecMap[ HYDROGUI_PrsImage::PointA ]->setText( "0" );
301
302   myPointXDegMap[ HYDROGUI_PrsImage::PointB ]->setText( "50" );
303   myPointXMinMap[ HYDROGUI_PrsImage::PointB ]->setText( "1" );
304   myPointXSecMap[ HYDROGUI_PrsImage::PointB ]->setText( "0" );
305   myPointYDegMap[ HYDROGUI_PrsImage::PointB ]->setText( "50" );
306   myPointYMinMap[ HYDROGUI_PrsImage::PointB ]->setText( "0" );
307   myPointYSecMap[ HYDROGUI_PrsImage::PointB ]->setText( "0" );
308
309   myPointXDegMap[ HYDROGUI_PrsImage::PointC ]->setText( "50" );
310   myPointXMinMap[ HYDROGUI_PrsImage::PointC ]->setText( "0" );
311   myPointXSecMap[ HYDROGUI_PrsImage::PointC ]->setText( "0" );
312   myPointYDegMap[ HYDROGUI_PrsImage::PointC ]->setText( "50" );
313   myPointYMinMap[ HYDROGUI_PrsImage::PointC ]->setText( "1" );
314   myPointYSecMap[ HYDROGUI_PrsImage::PointC ]->setText( "0" );
315 }
316
317 void HYDROGUI_ImportImageDlg::onBrowse()
318 {
319   QString aFilter( tr( "IMAGE_FILTER" ) );
320   //QString aFileName = QFileDialog::getOpenFileName( this, tr( "BROWSE_IMAGE_FILE" ), "", aFilter );
321   QString aFileName = "W:/Work/HYDRO/doc/samples/1.bmp";
322   if( !aFileName.isEmpty() )
323   {
324     QImage anImage( aFileName );
325     if( !anImage.isNull() )
326     {
327       myFileName->setText( aFileName );
328       emit createPreview( anImage );
329       myMappingGroup->setEnabled( true );
330     }
331   }
332 }
333
334 void HYDROGUI_ImportImageDlg::onPointBtnToggled( bool theState )
335 {
336   int aPointType = HYDROGUI_PrsImage::None;
337   if( theState )
338   {
339     QMapIterator<int, QPushButton*> anIter( myPointBtnMap );
340     while( anIter.hasNext() )
341     {
342       int aBtnPointType = anIter.next().key();
343       QPushButton* aBtn = anIter.value();
344       if( aBtn == sender() )
345         aPointType = aBtnPointType;
346       else
347       {
348         bool anIsBlocked = aBtn->blockSignals( true );
349         aBtn->setChecked( false );
350         aBtn->blockSignals( anIsBlocked );
351       }
352     }
353   }
354   emit activatePointSelection( aPointType );
355 }