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 myFileGroup = new QGroupBox( tr( "IMPORT_IMAGE_FROM_FILE" ) );
51 QLabel* aFileNameLabel = new QLabel( tr( "FILE_NAME" ), myFileGroup );
53 myFileName = new QLineEdit( myFileGroup );
54 myFileName->setReadOnly( true );
56 QToolButton* aBrowseBtn = new QToolButton( myFileGroup );
57 aBrowseBtn->setIcon( aResMgr->loadPixmap( "HYDRO", tr( "BROWSE_ICO" ) ) );
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 );
67 myMappingGroup = new QGroupBox( tr( "MAPPING" ) );
69 QGridLayout* aMappingLayout = new QGridLayout( myMappingGroup );
70 aMappingLayout->setMargin( 5 );
71 aMappingLayout->setSpacing( 5 );
73 // Make a pixmap for arrow
74 QPixmap anArrowPixmap = aResMgr->loadPixmap( "HYDRO", tr( "ARROW_RIGHT_ICO" ) );
76 QPicture anArrowPicture;
77 anArrowPicture.setBoundingRect( QRect( QPoint( 0, 0 ), anArrowPixmap.size() ) );
80 aPainter.begin( &anArrowPicture );
81 aPainter.drawPixmap( 0, 0, anArrowPixmap );
84 for( int aPointType = HYDROGUI_PrsImage::PointA;
85 aPointType <= HYDROGUI_PrsImage::PointC; aPointType++ )
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;
94 QPushButton* aPointBtn = new QPushButton( aPointStr, myMappingGroup );
95 aPointBtn->setCheckable( true );
97 QLabel* aPointXLabel = new QLabel( "X", myMappingGroup );
98 QLabel* aPointYLabel = new QLabel( "Y", myMappingGroup );
100 QLineEdit* aPointX = new QLineEdit( myMappingGroup );
101 QLineEdit* aPointY = new QLineEdit( myMappingGroup );
103 aPointX->setReadOnly( true );
104 aPointY->setReadOnly( true );
106 //QLabel* aPointArrowLabel = new QLabel( ">", myMappingGroup );
107 QLabel* aPointArrowLabel = new QLabel( myMappingGroup );
108 aPointArrowLabel->setPicture( anArrowPicture );
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 );
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 );
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 );
129 aPointXDeg->setValidator( aXDegValidator );
130 aPointYDeg->setValidator( aYDegValidator );
131 aPointXMin->setValidator( aMinValidator );
132 aPointYMin->setValidator( aMinValidator );
133 aPointXSec->setValidator( aSecValidator );
134 aPointYSec->setValidator( aSecValidator );
136 int aRow = 4 * aPointType;
137 aMappingLayout->addWidget( aPointBtn, aRow, 0, 1, 9 );
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 );
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 );
158 if( aPointType != HYDROGUI_PrsImage::PointC )
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 );
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;
176 connect( aPointBtn, SIGNAL( toggled( bool ) ), this, SLOT( onPointBtnToggled( bool ) ) );
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)
184 addWidget( myFileGroup, 0, 0 );
185 addWidget( myMappingGroup, 1, 0 );
189 connect( aBrowseBtn, SIGNAL( clicked() ), this, SLOT( onBrowse() ) );
192 HYDROGUI_ImportImageDlg::~HYDROGUI_ImportImageDlg()
196 void HYDROGUI_ImportImageDlg::setIsEdit( const bool theIsEdit )
198 myFileGroup->setVisible( !theIsEdit );
199 myMappingGroup->setEnabled( theIsEdit );
202 void HYDROGUI_ImportImageDlg::reset()
205 for( int aPointType = HYDROGUI_PrsImage::PointA;
206 aPointType <= HYDROGUI_PrsImage::PointC; aPointType++ )
208 QPushButton* aBtn = myPointBtnMap[ aPointType ];
209 bool anIsBlocked = aBtn->blockSignals( true );
210 aBtn->setChecked( false );
211 aBtn->blockSignals( anIsBlocked );
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();
222 myMappingGroup->setEnabled( false );
225 void HYDROGUI_ImportImageDlg::setTransformationDataMap( const TransformationDataMap& theMap,
226 const bool theIsOnlyInput )
228 for( int aPointType = HYDROGUI_PrsImage::PointA;
229 aPointType <= HYDROGUI_PrsImage::PointC; aPointType++ )
231 if( theMap.contains( aPointType ) )
233 const TransformationData& aData = theMap[ aPointType ];
234 myPointXMap[ aPointType ]->setText( QString::number( aData.first.x() ) );
235 myPointYMap[ aPointType ]->setText( QString::number( aData.first.y() ) );
237 if( !theIsOnlyInput )
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 );
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 ) );
257 bool HYDROGUI_ImportImageDlg::getTransformationDataMap( TransformationDataMap& theMap ) const
260 for( int aPointType = HYDROGUI_PrsImage::PointA;
261 aPointType <= HYDROGUI_PrsImage::PointC; aPointType++ )
264 for( int i = 0; i < 8; i++ )
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] );
276 for( int i = 0; i < 8; i++ )
280 double aX2 = 0, aY2 = 0;
281 HYDROGUI_Tool::LambertToDouble( aXDeg, aXMin, aXSec, aX2 );
282 HYDROGUI_Tool::LambertToDouble( aYDeg, aYMin, aYSec, aY2 );
284 TransformationData aData( QPoint( aX1, aY1 ), QPointF( aX2, aY2 ) );
285 theMap[ aPointType ] = aData;
290 void HYDROGUI_ImportImageDlg::initializePointSelection()
292 myPointBtnMap[ HYDROGUI_PrsImage::PointA ]->setChecked( true );
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" );
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" );
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" );
317 void HYDROGUI_ImportImageDlg::onBrowse()
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() )
324 QImage anImage( aFileName );
325 if( !anImage.isNull() )
327 myFileName->setText( aFileName );
328 emit createPreview( anImage );
329 myMappingGroup->setEnabled( true );
334 void HYDROGUI_ImportImageDlg::onPointBtnToggled( bool theState )
336 int aPointType = HYDROGUI_PrsImage::None;
339 QMapIterator<int, QPushButton*> anIter( myPointBtnMap );
340 while( anIter.hasNext() )
342 int aBtnPointType = anIter.next().key();
343 QPushButton* aBtn = anIter.value();
344 if( aBtn == sender() )
345 aPointType = aBtnPointType;
348 bool anIsBlocked = aBtn->blockSignals( true );
349 aBtn->setChecked( false );
350 aBtn->blockSignals( anIsBlocked );
354 emit activatePointSelection( aPointType );