Salome HOME
Fix for the bug #45: check and warning when the same image is used in 2 arguments.
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_GeoreferencementDlg.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_GeoreferencementDlg.h"
24
25 #include "HYDROGUI_Tool.h"
26
27 #include <GEOMUtils.hxx>
28
29 #include <OCCViewer_ViewWindow.h>
30 #include <OCCViewer_ViewManager.h>
31 #include <OCCViewer_ViewPort3d.h>
32
33 #include <AIS_InteractiveContext.hxx>
34
35 #include <QTableWidget>
36 #include <QItemDelegate>
37 #include <QHeaderView>
38 #include <QRadioButton>
39 #include <QPushButton>
40 #include <QLineEdit>
41 #include <QButtonGroup>
42 #include <QGroupBox>
43 #include <QLayout>
44 #include <QMouseEvent>
45
46 //! Profile data structre constructor
47 HYDROGUI_GeoreferencementDlg::ProfileGeoData::ProfileGeoData( 
48   const QString& theXg, const QString& theYg, 
49   const QString& theXd, const QString& theYd)
50 {
51   this->isEmpty = theXg.isEmpty() && theYg.isEmpty() &&
52                  theXd.isEmpty() && theYd.isEmpty();
53   this->isIncomplete = !isEmpty;
54
55   if ( isIncomplete ) {
56     bool isOk = false;
57
58     this->Xg= theXg.toDouble( &isOk );
59     if ( isOk ) {
60       this->Yg = theYg.toDouble( &isOk );
61       if ( isOk ) {
62         this->Xd = theXd.toDouble( &isOk );
63         if ( isOk ) {
64           this->Yd = theYd.toDouble( &isOk );
65           this->isIncomplete = !isOk;
66         }
67       }
68     }
69   }
70 }
71
72 //! Custom item delegate (line edit with double validator)
73 class HYDROGUI_GeoreferencementDlg::Delegate : public QItemDelegate
74 {
75 public:
76   Delegate( QObject* = 0 );
77   
78   QWidget* createEditor( QWidget*, const QStyleOptionViewItem&,
79                          const QModelIndex& ) const;
80   
81   void setEditorData( QWidget*, const QModelIndex& ) const;
82   void setModelData( QWidget*, QAbstractItemModel*, const QModelIndex& ) const;
83 };
84
85 HYDROGUI_GeoreferencementDlg::Delegate::Delegate( QObject* theParent )
86  : QItemDelegate( theParent )
87 {
88 }
89
90 QWidget* HYDROGUI_GeoreferencementDlg::Delegate::createEditor( 
91   QWidget* theParent, const QStyleOptionViewItem& theOption,
92   const QModelIndex& theIndex ) const
93 {
94   QWidget* anEditor = 0;
95
96   if ( theIndex.column() > 0 ) {
97     QLineEdit* aLineEdit = new QLineEdit( theParent );
98     QDoubleValidator* aDoubleValidator = new QDoubleValidator();
99     aDoubleValidator->setNotation( QDoubleValidator::StandardNotation );
100     aDoubleValidator->setDecimals( 2 );
101     aLineEdit->setValidator( aDoubleValidator );
102     anEditor = aLineEdit;
103   } else {
104     anEditor = QItemDelegate::createEditor( theParent, theOption, theIndex );
105   }
106
107   return anEditor;
108 }
109
110 void HYDROGUI_GeoreferencementDlg::Delegate::setEditorData( 
111   QWidget* theEditor, const QModelIndex& theIndex ) const
112 {
113   if ( QLineEdit* aLineEdit = dynamic_cast<QLineEdit*>( theEditor ) ) {
114     aLineEdit->setText( theIndex.data( Qt::EditRole ).toString() );
115   } else {
116     QItemDelegate::setEditorData( theEditor, theIndex );
117   }
118 }
119
120 void HYDROGUI_GeoreferencementDlg::Delegate::setModelData( 
121   QWidget* theEditor, QAbstractItemModel* theModel, const QModelIndex& theIndex) const
122 {
123   if ( QLineEdit* aLineEdit = dynamic_cast<QLineEdit*>( theEditor ) ) {
124     theModel->setData( theIndex, aLineEdit->text() );
125   } else {
126     QItemDelegate::setModelData( theEditor, theModel, theIndex );
127   }
128 }
129
130 HYDROGUI_GeoreferencementDlg::HYDROGUI_GeoreferencementDlg( HYDROGUI_Module* theModule, const QString& theTitle )
131 : HYDROGUI_InputPanel( theModule, theTitle ), myIsModified( false )
132 {
133   // Mode selector (all/selected)
134   QGroupBox* aModeGroup = new QGroupBox( tr( "PROFILES" ), this );
135
136   QRadioButton* anAllRB = new QRadioButton( tr( "ALL_MODE" ), aModeGroup );
137   QRadioButton* aSelectedRB = new QRadioButton( tr( "SELECTED_MODE" ), aModeGroup );
138
139   myModeButtons = new QButtonGroup( aModeGroup );
140   myModeButtons->addButton( anAllRB, AllProfiles );
141   myModeButtons->addButton( aSelectedRB, SelectedProfiles );
142
143   QBoxLayout* aModeSelectorLayout = new QVBoxLayout( aModeGroup );
144   aModeSelectorLayout->setMargin( 5 );
145   aModeSelectorLayout->setSpacing( 5 );
146   aModeSelectorLayout->addWidget( anAllRB );
147   aModeSelectorLayout->addWidget( aSelectedRB );
148
149   // Update selection button
150   myUpdateSelBtn = new QPushButton( mainFrame() );
151   myUpdateSelBtn->setText( tr("UPDATE_SELECTION") );
152   QBoxLayout* anUpdateSelLayout = new QHBoxLayout( mainFrame() );
153   anUpdateSelLayout->addWidget( myUpdateSelBtn );
154   anUpdateSelLayout->addStretch();
155
156   // Table
157   myTable = new QTableWidget( mainFrame() );
158   myTable->setItemDelegate( new Delegate( this ) );
159   myTable->verticalHeader()->setVisible( false );
160   myTable->setSelectionBehavior( QAbstractItemView::SelectItems );
161   myTable->setSelectionMode( QAbstractItemView::SingleSelection );
162   myTable->setColumnCount( 5 );
163   QStringList aColumnNames;
164   aColumnNames << tr( "PROFILE_HEADER" ) << tr( "XG_HEADER" ) << tr( "YG_HEADER" ) << 
165                                             tr( "XD_HEADER" ) << tr( "YD_HEADER" );
166   myTable->setHorizontalHeaderLabels( aColumnNames );
167
168   // Layout
169   addWidget( aModeGroup );
170   addLayout( anUpdateSelLayout );
171   addWidget( myTable );
172
173   // Connect signals and slots
174   connect( myModeButtons, SIGNAL( buttonClicked( int ) ), this, SLOT( onModeActivated( int ) ) );
175   connect( myUpdateSelBtn, SIGNAL( clicked() ), this, SIGNAL( updateSelection() ) );
176   connect( myTable->model(), SIGNAL( dataChanged ( const QModelIndex&, const QModelIndex& ) ), 
177            this, SLOT( onDataChanged() ) );
178 }
179
180 HYDROGUI_GeoreferencementDlg::~HYDROGUI_GeoreferencementDlg()
181 {
182 }
183
184 void HYDROGUI_GeoreferencementDlg::onModeActivated( int theMode )
185 {
186   myUpdateSelBtn->setEnabled( theMode == SelectedProfiles );
187   emit modeActivated( theMode );
188 }
189
190 void HYDROGUI_GeoreferencementDlg::reset()
191 {
192   // Activate the "All" mode
193   myModeButtons->button( AllProfiles )->setChecked( true );
194
195   // Clear the table widget
196   myTable->setRowCount( 0 );
197 }
198
199 void HYDROGUI_GeoreferencementDlg::setMode( const int theMode )
200 {
201   bool isBlocked = myModeButtons->blockSignals( true );
202
203   QAbstractButton* aModeButton = myModeButtons->button( theMode );
204   if ( aModeButton ) {
205     aModeButton->setChecked( true );
206   }
207
208   myUpdateSelBtn->setEnabled( theMode == SelectedProfiles );
209
210   myModeButtons->blockSignals( isBlocked );
211 }
212
213 void HYDROGUI_GeoreferencementDlg::setData( const ProfilesGeoDataMap& theMap )
214 {
215   disconnect( myTable->model(), SIGNAL( dataChanged ( const QModelIndex&, const QModelIndex& ) ), 
216               this, SLOT( onDataChanged() ) );
217
218   myTable->setRowCount( 0 );
219
220   foreach ( const QString& aProfileName, theMap.keys() ) {
221     // Check the current profile name
222     if ( aProfileName.isEmpty() ) {
223       continue;
224     }
225
226     // Get georeferencement data for the current profile
227     ProfileGeoData aGeoData = theMap.value( aProfileName );
228     QString aXg, anYg, aXd, anYd;
229     if ( !aGeoData.isEmpty ) {
230       aXg = HYDROGUI_Tool::GetCoordinateString( aGeoData.Xg );
231       anYg = HYDROGUI_Tool::GetCoordinateString( aGeoData.Yg );
232       aXd = HYDROGUI_Tool::GetCoordinateString( aGeoData.Xd );
233       anYd = HYDROGUI_Tool::GetCoordinateString( aGeoData.Yd );
234     }
235     
236     // Insert row with the data
237     int aRow = myTable->rowCount();
238     myTable->insertRow( aRow );
239
240     // "Profile" column
241     QTableWidgetItem* aNameItem = new QTableWidgetItem( aProfileName );
242     aNameItem->setFlags( Qt::ItemIsEnabled );
243     QFont aFont = aNameItem->font();
244     aFont.setBold( true );
245     aNameItem->setFont( aFont ); 
246     myTable->setItem( aRow, 0, aNameItem );
247
248     // "Xg" column
249     myTable->setItem( aRow, 1, new QTableWidgetItem( aXg ) );
250
251     // "Yg" column
252     myTable->setItem( aRow, 2, new QTableWidgetItem( anYg ) );
253
254     // "Xd" column
255     myTable->setItem( aRow, 3, new QTableWidgetItem( aXd ) );
256
257     // "Yd" column
258     myTable->setItem( aRow, 4, new QTableWidgetItem( anYd ) );
259   }
260
261   myTable->resizeColumnToContents( 0 );
262   myTable->resizeRowsToContents();
263
264   myIsModified = false;
265   
266   connect( myTable->model(), SIGNAL( dataChanged ( const QModelIndex&, const QModelIndex& ) ), 
267            this, SLOT( onDataChanged() ) );
268 }
269
270 void HYDROGUI_GeoreferencementDlg::getData( ProfilesGeoDataMap& theMap )
271 {
272   // Clear the map
273   theMap.clear();
274
275   // Fill the map
276   bool isOk = false;
277   QString aXg, anYg, aXd, anYd;
278   for ( int aRow = 0; aRow < myTable->rowCount(); aRow++ ) {
279     QString aProfileName = myTable->item( aRow, 0 )->text();
280
281     aXg = myTable->item( aRow, 1 )->text();
282     anYg = myTable->item( aRow, 2 )->text();
283     aXd = myTable->item( aRow, 3 )->text();
284     anYd = myTable->item( aRow, 4 )->text();
285    
286     theMap.insert( aProfileName, ProfileGeoData( aXg, anYg, aXd, anYd ) );
287   }
288 }
289
290 void HYDROGUI_GeoreferencementDlg::onMousePress( 
291   SUIT_ViewWindow* theViewWindow, QMouseEvent* theEvent )
292 {
293   // Check the parameters
294   OCCViewer_ViewWindow* anOCCViewWindow = 
295     dynamic_cast<OCCViewer_ViewWindow*>(theViewWindow);
296   if ( !anOCCViewWindow || (theEvent->button() != Qt::LeftButton) ) {
297     return;
298   }
299   
300   // Check for current cell
301   int aRow = myTable->currentRow();
302   int aColumn = myTable->currentColumn();
303   if ( aRow < 0 || aColumn <= 0 ) {
304     return;
305   }
306
307   // Get the selected point coordinates
308   OCCViewer_ViewPort3d* aViewPort = anOCCViewWindow->getViewPort();
309   gp_Pnt aPnt = GEOMUtils::ConvertClickToPoint( theEvent->x(), theEvent->y(), 
310                                                 aViewPort->getView() );
311
312   // Set the coordinates to the corresponding cells of the table
313   int aColumnX = aColumn < 3 ? 1 : 3;
314   int aColumnY = aColumnX + 1;
315   
316   QString aXStr = HYDROGUI_Tool::GetCoordinateString( aPnt.X() );
317   QString anYStr = HYDROGUI_Tool::GetCoordinateString( aPnt.Y() );
318   myTable->item( aRow, aColumnX )->setText( aXStr );
319   myTable->item( aRow, aColumnY )->setText( anYStr );
320 }
321
322 void HYDROGUI_GeoreferencementDlg::onDataChanged()
323 {
324   myIsModified = true;
325 }
326
327 bool HYDROGUI_GeoreferencementDlg::isModified() const
328 {
329   return myIsModified;
330 }