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