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