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"
27 #include <SUIT_ResourceMgr.h>
28 #include <SUIT_Session.h>
30 #include <QDoubleValidator>
31 #include <QFileDialog>
38 #include <QPushButton>
39 #include <QToolButton>
41 HYDROGUI_ImportImageDlg::HYDROGUI_ImportImageDlg( HYDROGUI_Module* theModule, const QString& theTitle )
42 : HYDROGUI_InputPanel( theModule, theTitle )
44 SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
46 // Import image from file
47 QGroupBox* aFileGroup = new QGroupBox( tr( "IMPORT_IMAGE_FROM_FILE" ) );
49 QLabel* aFileNameLabel = new QLabel( tr( "FILE_NAME" ), aFileGroup );
51 myFileName = new QLineEdit( aFileGroup );
52 myFileName->setReadOnly( true );
54 QToolButton* aBrowseBtn = new QToolButton( aFileGroup );
55 aBrowseBtn->setIcon( aResMgr->loadPixmap( "HYDRO", tr( "BROWSE_ICO" ) ) );
57 QBoxLayout* aFileLayout = new QHBoxLayout( aFileGroup );
58 aFileLayout->setMargin( 5 );
59 aFileLayout->setSpacing( 5 );
60 aFileLayout->addWidget( aFileNameLabel );
61 aFileLayout->addWidget( myFileName );
62 aFileLayout->addWidget( aBrowseBtn );
65 myMappingGroup = new QGroupBox( tr( "MAPPING" ) );
67 QGridLayout* aMappingLayout = new QGridLayout( myMappingGroup );
68 aMappingLayout->setMargin( 5 );
69 aMappingLayout->setSpacing( 5 );
71 // Make a pixmap for arrow
72 QPixmap anArrowPixmap = aResMgr->loadPixmap( "HYDRO", tr( "ARROW_RIGHT_ICO" ) );
74 QPicture anArrowPicture;
75 anArrowPicture.setBoundingRect( QRect( QPoint( 0, 0 ), anArrowPixmap.size() ) );
78 aPainter.begin( &anArrowPicture );
79 aPainter.drawPixmap( 0, 0, anArrowPixmap );
82 for( int aPointType = HYDROGUI_PrsImage::PointA;
83 aPointType <= HYDROGUI_PrsImage::PointC; aPointType++ )
88 case HYDROGUI_PrsImage::PointA: aPointStr = tr( "POINT_A" ); break;
89 case HYDROGUI_PrsImage::PointB: aPointStr = tr( "POINT_B" ); break;
90 case HYDROGUI_PrsImage::PointC: aPointStr = tr( "POINT_C" ); break;
92 QPushButton* aPointBtn = new QPushButton( aPointStr, myMappingGroup );
93 aPointBtn->setCheckable( true );
95 QLabel* aPointXLabel = new QLabel( tr( "X" ), myMappingGroup );
96 QLabel* aPointYLabel = new QLabel( tr( "Y" ), myMappingGroup );
98 QLineEdit* aPointX1 = new QLineEdit( myMappingGroup );
99 QLineEdit* aPointY1 = new QLineEdit( myMappingGroup );
101 aPointX1->setReadOnly( true );
102 aPointY1->setReadOnly( true );
104 //QLabel* aPointArrowLabel = new QLabel( ">", myMappingGroup );
105 QLabel* aPointArrowLabel = new QLabel( myMappingGroup );
106 aPointArrowLabel->setPicture( anArrowPicture );
108 QLineEdit* aPointX2 = new QLineEdit( myMappingGroup );
109 QLineEdit* aPointY2 = new QLineEdit( myMappingGroup );
111 QDoubleValidator* aDoubleValidator = new QDoubleValidator();
112 aPointX2->setValidator( aDoubleValidator );
113 aPointY2->setValidator( aDoubleValidator );
115 int aRow = 3 * aPointType;
116 aMappingLayout->addWidget( aPointBtn, aRow, 0, 2, 1 );
117 aMappingLayout->addWidget( aPointXLabel, aRow, 1 );
118 aMappingLayout->addWidget( aPointX1, aRow, 2 );
119 aMappingLayout->addWidget( aPointArrowLabel, aRow, 3, 2, 1 );
120 aMappingLayout->addWidget( aPointX2, aRow, 4 );
121 aMappingLayout->addWidget( aPointYLabel, aRow + 1, 1 );
122 aMappingLayout->addWidget( aPointY1, aRow + 1, 2 );
123 aMappingLayout->addWidget( aPointY2, aRow + 1, 4 );
125 if( aPointType != HYDROGUI_PrsImage::PointC )
127 QFrame* aLine = new QFrame( myMappingGroup );
128 aLine->setFrameShape( QFrame::HLine );
129 aLine->setFrameShadow( QFrame::Sunken );
130 aMappingLayout->addWidget( aLine, aRow + 2, 0, 1, 5 );
133 myPointBtnMap[ aPointType ] = aPointBtn;
134 myPointX1Map[ aPointType ] = aPointX1;
135 myPointY1Map[ aPointType ] = aPointY1;
136 myPointX2Map[ aPointType ] = aPointX2;
137 myPointY2Map[ aPointType ] = aPointY2;
139 connect( aPointBtn, SIGNAL( toggled( bool ) ), this, SLOT( onPointBtnToggled( bool ) ) );
143 addWidget( aFileGroup, 0, 0 );
144 addWidget( myMappingGroup, 1, 0 );
148 connect( aBrowseBtn, SIGNAL( clicked() ), this, SLOT( onBrowse() ) );
151 HYDROGUI_ImportImageDlg::~HYDROGUI_ImportImageDlg()
155 void HYDROGUI_ImportImageDlg::reset()
158 for( int aPointType = HYDROGUI_PrsImage::PointA;
159 aPointType <= HYDROGUI_PrsImage::PointC; aPointType++ )
161 QPushButton* aBtn = myPointBtnMap[ aPointType ];
162 bool anIsBlocked = aBtn->blockSignals( true );
163 aBtn->setChecked( false );
164 aBtn->blockSignals( anIsBlocked );
166 myPointX1Map[ aPointType ]->clear();
167 myPointY1Map[ aPointType ]->clear();
168 myPointX2Map[ aPointType ]->clear();
169 myPointY2Map[ aPointType ]->clear();
171 myMappingGroup->setEnabled( false );
174 void HYDROGUI_ImportImageDlg::setTransformationDataMap( const TransformationDataMap& theMap )
176 for( int aPointType = HYDROGUI_PrsImage::PointA;
177 aPointType <= HYDROGUI_PrsImage::PointC; aPointType++ )
179 if( theMap.contains( aPointType ) )
181 const TransformationData& aData = theMap[ aPointType ];
182 myPointX1Map[ aPointType ]->setText( QString::number( aData.first.x() ) );
183 myPointY1Map[ aPointType ]->setText( QString::number( aData.first.y() ) );
188 bool HYDROGUI_ImportImageDlg::getTransformationDataMap( TransformationDataMap& theMap ) const
191 for( int aPointType = HYDROGUI_PrsImage::PointA;
192 aPointType <= HYDROGUI_PrsImage::PointC; aPointType++ )
195 int aX1 = myPointX1Map[ aPointType ]->text().toInt( &anIsOk );
200 int aY1 = myPointY1Map[ aPointType ]->text().toInt( &anIsOk );
205 double aX2 = myPointX2Map[ aPointType ]->text().toDouble( &anIsOk );
210 double aY2 = myPointY2Map[ aPointType ]->text().toDouble( &anIsOk );
214 TransformationData aData( QPoint( aX1, aY1 ), QPointF( aX2, aY2 ) );
215 theMap[ aPointType ] = aData;
220 void HYDROGUI_ImportImageDlg::initializePointSelection()
222 myPointBtnMap[ HYDROGUI_PrsImage::PointA ]->setChecked( true );
225 void HYDROGUI_ImportImageDlg::synchronizeTransformedPoints()
227 for( int aPointType = HYDROGUI_PrsImage::PointA;
228 aPointType <= HYDROGUI_PrsImage::PointC; aPointType++ )
230 myPointX2Map[ aPointType ]->setText( myPointX1Map[ aPointType ]->text() );
231 myPointY2Map[ aPointType ]->setText( myPointY1Map[ aPointType ]->text() );
235 void HYDROGUI_ImportImageDlg::onBrowse()
237 QString aFilter( tr( "IMAGE_FILTER" ) );
238 QString aFileName = QFileDialog::getOpenFileName( this, tr( "BROWSE_IMAGE_FILE" ), "", aFilter );
239 if( !aFileName.isEmpty() )
241 myFileName->setText( aFileName );
242 emit createPreview( aFileName );
243 myMappingGroup->setEnabled( true );
247 void HYDROGUI_ImportImageDlg::onPointBtnToggled( bool theState )
249 int aPointType = HYDROGUI_PrsImage::None;
252 QMapIterator<int, QPushButton*> anIter( myPointBtnMap );
253 while( anIter.hasNext() )
255 int aBtnPointType = anIter.next().key();
256 QPushButton* aBtn = anIter.value();
257 if( aBtn == sender() )
258 aPointType = aBtnPointType;
261 bool anIsBlocked = aBtn->blockSignals( true );
262 aBtn->setChecked( false );
263 aBtn->blockSignals( anIsBlocked );
267 emit activatePointSelection( aPointType );