1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "HYDROGUI_ImportImageDlg.h"
25 #include "HYDROGUI_PrsImage.h"
26 #include "HYDROGUI_Tool.h"
28 #include <SUIT_ResourceMgr.h>
29 #include <SUIT_Session.h>
31 #include <QDoubleValidator>
32 #include <QFileDialog>
34 #include <QIntValidator>
40 #include <QPushButton>
41 #include <QToolButton>
43 HYDROGUI_ImportImageDlg::HYDROGUI_ImportImageDlg( HYDROGUI_Module* theModule, const QString& theTitle )
44 : HYDROGUI_InputPanel( theModule, theTitle )
46 SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
48 // Import image from file
49 myFileNameGroup = new QGroupBox( tr( "IMPORT_IMAGE_FROM_FILE" ) );
51 QLabel* aFileNameLabel = new QLabel( tr( "FILE_NAME" ), myFileNameGroup );
53 myFileName = new QLineEdit( myFileNameGroup );
54 myFileName->setReadOnly( true );
56 QToolButton* aBrowseBtn = new QToolButton( myFileNameGroup );
57 aBrowseBtn->setIcon( aResMgr->loadPixmap( "HYDRO", tr( "BROWSE_ICO" ) ) );
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 );
67 myImageNameGroup = new QGroupBox( tr( "IMAGE_NAME" ) );
69 QLabel* anImageNameLabel = new QLabel( tr( "NAME" ), myImageNameGroup );
70 myImageName = new QLineEdit( myImageNameGroup );
72 QBoxLayout* anImageNameLayout = new QHBoxLayout( myImageNameGroup );
73 anImageNameLayout->setMargin( 5 );
74 anImageNameLayout->setSpacing( 5 );
75 anImageNameLayout->addWidget( anImageNameLabel );
76 anImageNameLayout->addWidget( myImageName );
79 myMappingGroup = new QGroupBox( tr( "MAPPING" ) );
81 QGridLayout* aMappingLayout = new QGridLayout( myMappingGroup );
82 aMappingLayout->setMargin( 5 );
83 aMappingLayout->setSpacing( 5 );
85 // Make a pixmap for arrow
86 QPixmap anArrowPixmap = aResMgr->loadPixmap( "HYDRO", tr( "ARROW_RIGHT_ICO" ) );
88 QPicture anArrowPicture;
89 anArrowPicture.setBoundingRect( QRect( QPoint( 0, 0 ), anArrowPixmap.size() ) );
92 aPainter.begin( &anArrowPicture );
93 aPainter.drawPixmap( 0, 0, anArrowPixmap );
96 for( int aPointType = HYDROGUI_PrsImage::PointA;
97 aPointType <= HYDROGUI_PrsImage::PointC; aPointType++ )
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;
106 QPushButton* aPointBtn = new QPushButton( aPointStr, myMappingGroup );
107 aPointBtn->setCheckable( true );
109 QLabel* aPointXLabel = new QLabel( "X", myMappingGroup );
110 QLabel* aPointYLabel = new QLabel( "Y", myMappingGroup );
112 QLineEdit* aPointX = new QLineEdit( myMappingGroup );
113 QLineEdit* aPointY = new QLineEdit( myMappingGroup );
115 aPointX->setReadOnly( true );
116 aPointY->setReadOnly( true );
118 //QLabel* aPointArrowLabel = new QLabel( ">", myMappingGroup );
119 QLabel* aPointArrowLabel = new QLabel( myMappingGroup );
120 aPointArrowLabel->setPicture( anArrowPicture );
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 );
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 );
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 );
141 aPointXDeg->setValidator( aXDegValidator );
142 aPointYDeg->setValidator( aYDegValidator );
143 aPointXMin->setValidator( aMinValidator );
144 aPointYMin->setValidator( aMinValidator );
145 aPointXSec->setValidator( aSecValidator );
146 aPointYSec->setValidator( aSecValidator );
148 int aRow = 4 * aPointType;
149 aMappingLayout->addWidget( aPointBtn, aRow, 0, 1, 9 );
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 );
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 );
170 if( aPointType != HYDROGUI_PrsImage::PointC )
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 );
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;
188 connect( aPointBtn, SIGNAL( toggled( bool ) ), this, SLOT( onPointBtnToggled( bool ) ) );
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)
196 addWidget( myFileNameGroup );
197 addWidget( myImageNameGroup );
198 addWidget( myMappingGroup );
201 connect( aBrowseBtn, SIGNAL( clicked() ), this, SLOT( onBrowse() ) );
204 HYDROGUI_ImportImageDlg::~HYDROGUI_ImportImageDlg()
208 void HYDROGUI_ImportImageDlg::setIsEdit( const bool theIsEdit )
210 myFileNameGroup->setVisible( !theIsEdit );
211 myImageNameGroup->setEnabled( theIsEdit );
212 myMappingGroup->setEnabled( theIsEdit );
215 void HYDROGUI_ImportImageDlg::reset()
218 myImageName->clear();
219 myImageNameGroup->setEnabled( false );
220 for( int aPointType = HYDROGUI_PrsImage::PointA;
221 aPointType <= HYDROGUI_PrsImage::PointC; aPointType++ )
223 QPushButton* aBtn = myPointBtnMap[ aPointType ];
224 bool anIsBlocked = aBtn->blockSignals( true );
225 aBtn->setChecked( false );
226 aBtn->blockSignals( anIsBlocked );
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();
237 myMappingGroup->setEnabled( false );
240 void HYDROGUI_ImportImageDlg::setImageName( const QString& theName )
242 myImageName->setText( theName );
245 QString HYDROGUI_ImportImageDlg::getImageName() const
247 return myImageName->text();
250 void HYDROGUI_ImportImageDlg::setTransformationDataMap( const TransformationDataMap& theMap,
251 const bool theIsOnlyInput )
253 for( int aPointType = HYDROGUI_PrsImage::PointA;
254 aPointType <= HYDROGUI_PrsImage::PointC; aPointType++ )
256 if( theMap.contains( aPointType ) )
258 const TransformationData& aData = theMap[ aPointType ];
259 myPointXMap[ aPointType ]->setText( QString::number( aData.first.x() ) );
260 myPointYMap[ aPointType ]->setText( QString::number( aData.first.y() ) );
262 if( !theIsOnlyInput )
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 );
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 ) );
282 bool HYDROGUI_ImportImageDlg::getTransformationDataMap( TransformationDataMap& theMap ) const
285 for( int aPointType = HYDROGUI_PrsImage::PointA;
286 aPointType <= HYDROGUI_PrsImage::PointC; aPointType++ )
289 for( int i = 0; i < 8; i++ )
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] );
301 for( int i = 0; i < 8; i++ )
305 double aX2 = 0, aY2 = 0;
306 HYDROGUI_Tool::LambertToDouble( aXDeg, aXMin, aXSec, aX2 );
307 HYDROGUI_Tool::LambertToDouble( aYDeg, aYMin, aYSec, aY2 );
309 TransformationData aData( QPoint( aX1, aY1 ), QPointF( aX2, aY2 ) );
310 theMap[ aPointType ] = aData;
315 void HYDROGUI_ImportImageDlg::initializePointSelection()
317 myPointBtnMap[ HYDROGUI_PrsImage::PointA ]->setChecked( true );
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" );
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" );
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" );
342 void HYDROGUI_ImportImageDlg::onBrowse()
344 QString aFilter( tr( "IMAGE_FILTER" ) );
345 #ifdef OUV_DEBUG // to remove
346 QString aFileName = "W:/Work/HYDRO/doc/samples/1.bmp";
348 QString aFileName = QFileDialog::getOpenFileName( this, tr( "IMPORT_IMAGE_FROM_FILE" ), "", aFilter );
350 if( !aFileName.isEmpty() )
352 QImage anImage( aFileName );
353 if( !anImage.isNull() )
355 myFileName->setText( aFileName );
356 emit createPreview( anImage );
357 myImageNameGroup->setEnabled( true );
358 myMappingGroup->setEnabled( true );
363 void HYDROGUI_ImportImageDlg::onPointBtnToggled( bool theState )
365 int aPointType = HYDROGUI_PrsImage::None;
368 QMapIterator<int, QPushButton*> anIter( myPointBtnMap );
369 while( anIter.hasNext() )
371 int aBtnPointType = anIter.next().key();
372 QPushButton* aBtn = anIter.value();
373 if( aBtn == sender() )
374 aPointType = aBtnPointType;
377 bool anIsBlocked = aBtn->blockSignals( true );
378 aBtn->setChecked( false );
379 aBtn->blockSignals( anIsBlocked );
383 emit activatePointSelection( aPointType );