Salome HOME
1) HYDRO: Import Image operation.
[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
27 #include <SUIT_ResourceMgr.h>
28 #include <SUIT_Session.h>
29
30 #include <QDoubleValidator>
31 #include <QFileDialog>
32 #include <QGroupBox>
33 #include <QLabel>
34 #include <QLayout>
35 #include <QLineEdit>
36 #include <QPainter>
37 #include <QPicture>
38 #include <QPushButton>
39 #include <QToolButton>
40
41 HYDROGUI_ImportImageDlg::HYDROGUI_ImportImageDlg( HYDROGUI_Module* theModule, const QString& theTitle )
42 : HYDROGUI_InputPanel( theModule, theTitle )
43 {
44   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
45
46   // Import image from file
47   QGroupBox* aFileGroup = new QGroupBox( tr( "IMPORT_IMAGE_FROM_FILE" ) );
48
49   QLabel* aFileNameLabel = new QLabel( tr( "FILE_NAME" ), aFileGroup );
50
51   myFileName = new QLineEdit( aFileGroup );
52   myFileName->setReadOnly( true );
53
54   QToolButton* aBrowseBtn = new QToolButton( aFileGroup );
55   aBrowseBtn->setIcon( aResMgr->loadPixmap( "HYDRO", tr( "BROWSE_ICO" ) ) );
56
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 );
63
64   // Mapping
65   myMappingGroup = new QGroupBox( tr( "MAPPING" ) );
66
67   QGridLayout* aMappingLayout = new QGridLayout( myMappingGroup );
68   aMappingLayout->setMargin( 5 );
69   aMappingLayout->setSpacing( 5 );
70
71   // Make a pixmap for arrow
72   QPixmap anArrowPixmap = aResMgr->loadPixmap( "HYDRO", tr( "ARROW_RIGHT_ICO" ) );
73
74   QPicture anArrowPicture;
75   anArrowPicture.setBoundingRect( QRect( QPoint( 0, 0 ), anArrowPixmap.size() ) );
76
77   QPainter aPainter;
78   aPainter.begin( &anArrowPicture );
79   aPainter.drawPixmap( 0, 0, anArrowPixmap );
80   aPainter.end();
81
82   for( int aPointType = HYDROGUI_PrsImage::PointA;
83        aPointType <= HYDROGUI_PrsImage::PointC; aPointType++ )
84   {
85     QString aPointStr;
86     switch( aPointType )
87     {
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;
91     }
92     QPushButton* aPointBtn = new QPushButton( aPointStr, myMappingGroup );
93     aPointBtn->setCheckable( true );
94
95     QLabel* aPointXLabel = new QLabel( tr( "X" ), myMappingGroup );
96     QLabel* aPointYLabel = new QLabel( tr( "Y" ), myMappingGroup );
97
98     QLineEdit* aPointX1 = new QLineEdit( myMappingGroup );
99     QLineEdit* aPointY1 = new QLineEdit( myMappingGroup );
100
101     aPointX1->setReadOnly( true );
102     aPointY1->setReadOnly( true );
103
104     //QLabel* aPointArrowLabel = new QLabel( ">", myMappingGroup );
105     QLabel* aPointArrowLabel = new QLabel( myMappingGroup );
106     aPointArrowLabel->setPicture( anArrowPicture );
107
108     QLineEdit* aPointX2 = new QLineEdit( myMappingGroup );
109     QLineEdit* aPointY2 = new QLineEdit( myMappingGroup );
110
111     QDoubleValidator* aDoubleValidator = new QDoubleValidator();
112     aPointX2->setValidator( aDoubleValidator );
113     aPointY2->setValidator( aDoubleValidator );
114
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 );
124
125     if( aPointType != HYDROGUI_PrsImage::PointC )
126     {
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 );
131     }
132
133     myPointBtnMap[ aPointType ] = aPointBtn;
134     myPointX1Map[ aPointType ] = aPointX1;
135     myPointY1Map[ aPointType ] = aPointY1;
136     myPointX2Map[ aPointType ] = aPointX2;
137     myPointY2Map[ aPointType ] = aPointY2;
138
139     connect( aPointBtn, SIGNAL( toggled( bool ) ), this, SLOT( onPointBtnToggled( bool ) ) );
140   }
141
142   // Common
143   addWidget( aFileGroup, 0, 0 );
144   addWidget( myMappingGroup, 1, 0 );
145
146   setRowStretch();
147
148   connect( aBrowseBtn, SIGNAL( clicked() ), this, SLOT( onBrowse() ) );
149 }
150
151 HYDROGUI_ImportImageDlg::~HYDROGUI_ImportImageDlg()
152 {
153 }
154
155 void HYDROGUI_ImportImageDlg::reset()
156 {
157   myFileName->clear();
158   for( int aPointType = HYDROGUI_PrsImage::PointA;
159        aPointType <= HYDROGUI_PrsImage::PointC; aPointType++ )
160   {
161     QPushButton* aBtn = myPointBtnMap[ aPointType ];
162     bool anIsBlocked = aBtn->blockSignals( true );
163     aBtn->setChecked( false );
164     aBtn->blockSignals( anIsBlocked );
165
166     myPointX1Map[ aPointType ]->clear();
167     myPointY1Map[ aPointType ]->clear();
168     myPointX2Map[ aPointType ]->clear();
169     myPointY2Map[ aPointType ]->clear();
170   }
171   myMappingGroup->setEnabled( false );
172 }
173
174 void HYDROGUI_ImportImageDlg::setTransformationDataMap( const TransformationDataMap& theMap )
175 {
176   for( int aPointType = HYDROGUI_PrsImage::PointA;
177        aPointType <= HYDROGUI_PrsImage::PointC; aPointType++ )
178   {
179     if( theMap.contains( aPointType ) )
180     {
181       const TransformationData& aData = theMap[ aPointType ];
182       myPointX1Map[ aPointType ]->setText( QString::number( aData.first.x() ) );
183       myPointY1Map[ aPointType ]->setText( QString::number( aData.first.y() ) );
184     }
185   }
186 }
187
188 bool HYDROGUI_ImportImageDlg::getTransformationDataMap( TransformationDataMap& theMap ) const
189 {
190   theMap.clear();
191   for( int aPointType = HYDROGUI_PrsImage::PointA;
192        aPointType <= HYDROGUI_PrsImage::PointC; aPointType++ )
193   {
194     bool anIsOk = false;
195     int aX1 = myPointX1Map[ aPointType ]->text().toInt( &anIsOk );
196     if( !anIsOk )
197       return false;
198
199     anIsOk = false;
200     int aY1 = myPointY1Map[ aPointType ]->text().toInt( &anIsOk );
201     if( !anIsOk )
202       return false;
203
204     anIsOk = false;
205     double aX2 = myPointX2Map[ aPointType ]->text().toDouble( &anIsOk );
206     if( !anIsOk )
207       return false;
208
209     anIsOk = false;
210     double aY2 = myPointY2Map[ aPointType ]->text().toDouble( &anIsOk );
211     if( !anIsOk )
212       return false;
213
214     TransformationData aData( QPoint( aX1, aY1 ), QPointF( aX2, aY2 ) );
215     theMap[ aPointType ] = aData;
216   }
217   return true;
218 }
219
220 void HYDROGUI_ImportImageDlg::initializePointSelection()
221 {
222   myPointBtnMap[ HYDROGUI_PrsImage::PointA ]->setChecked( true );
223 }
224
225 void HYDROGUI_ImportImageDlg::synchronizeTransformedPoints()
226 {
227   for( int aPointType = HYDROGUI_PrsImage::PointA;
228        aPointType <= HYDROGUI_PrsImage::PointC; aPointType++ )
229   {
230     myPointX2Map[ aPointType ]->setText( myPointX1Map[ aPointType ]->text() );
231     myPointY2Map[ aPointType ]->setText( myPointY1Map[ aPointType ]->text() );
232   }
233 }
234
235 void HYDROGUI_ImportImageDlg::onBrowse()
236 {
237   QString aFilter( tr( "IMAGE_FILTER" ) );
238   QString aFileName = QFileDialog::getOpenFileName( this, tr( "BROWSE_IMAGE_FILE" ), "", aFilter );
239   if( !aFileName.isEmpty() )
240   {
241     myFileName->setText( aFileName );
242     emit createPreview( aFileName );
243     myMappingGroup->setEnabled( true );
244   }
245 }
246
247 void HYDROGUI_ImportImageDlg::onPointBtnToggled( bool theState )
248 {
249   int aPointType = HYDROGUI_PrsImage::None;
250   if( theState )
251   {
252     QMapIterator<int, QPushButton*> anIter( myPointBtnMap );
253     while( anIter.hasNext() )
254     {
255       int aBtnPointType = anIter.next().key();
256       QPushButton* aBtn = anIter.value();
257       if( aBtn == sender() )
258         aPointType = aBtnPointType;
259       else
260       {
261         bool anIsBlocked = aBtn->blockSignals( true );
262         aBtn->setChecked( false );
263         aBtn->blockSignals( anIsBlocked );
264       }
265     }
266   }
267   emit activatePointSelection( aPointType );
268 }