Salome HOME
Merge branch 'BR_LAND_COVER' of ssh://git.salome-platform.org/modules/hydro into...
[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 #include "HYDROGUI_LineEditDoubleValidator.h"
23
24 #include <CurveCreator_Utils.hxx>
25
26 #include <OCCViewer_ViewWindow.h>
27 #include <OCCViewer_ViewManager.h>
28 #include <OCCViewer_ViewPort3d.h>
29
30 #include <AIS_InteractiveContext.hxx>
31
32 #include <QTableWidget>
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 HYDROGUI_GeoreferencementDlg::HYDROGUI_GeoreferencementDlg( HYDROGUI_Module* theModule, const QString& theTitle )
71 : HYDROGUI_InputPanel( theModule, theTitle ), myIsModified( false )
72 {
73   // Mode selector (all/selected)
74   QGroupBox* aModeGroup = new QGroupBox( tr( "PROFILES" ), this );
75
76   QRadioButton* anAllRB = new QRadioButton( tr( "ALL_MODE" ), aModeGroup );
77   QRadioButton* aSelectedRB = new QRadioButton( tr( "SELECTED_MODE" ), aModeGroup );
78
79   myModeButtons = new QButtonGroup( aModeGroup );
80   myModeButtons->addButton( anAllRB, AllProfiles );
81   myModeButtons->addButton( aSelectedRB, SelectedProfiles );
82
83   QBoxLayout* aModeSelectorLayout = new QVBoxLayout( aModeGroup );
84   aModeSelectorLayout->setMargin( 5 );
85   aModeSelectorLayout->setSpacing( 5 );
86   aModeSelectorLayout->addWidget( anAllRB );
87   aModeSelectorLayout->addWidget( aSelectedRB );
88
89   // Update selection button
90   myUpdateSelBtn = new QPushButton( mainFrame() );
91   myUpdateSelBtn->setText( tr("UPDATE_SELECTION") );
92   QBoxLayout* anUpdateSelLayout = new QHBoxLayout( mainFrame() );
93   anUpdateSelLayout->addWidget( myUpdateSelBtn );
94   anUpdateSelLayout->addStretch();
95
96   // Table
97   myTable = new QTableWidget( mainFrame() );
98   myTable->setItemDelegate( new HYDROGUI_LineEditDoubleValidator( this ) );
99   myTable->verticalHeader()->setVisible( false );
100   myTable->setSelectionBehavior( QAbstractItemView::SelectItems );
101   myTable->setSelectionMode( QAbstractItemView::SingleSelection );
102   myTable->setColumnCount( 5 );
103   QStringList aColumnNames;
104   aColumnNames << tr( "PROFILE_HEADER" ) << tr( "XG_HEADER" ) << tr( "YG_HEADER" ) << 
105                                             tr( "XD_HEADER" ) << tr( "YD_HEADER" );
106   myTable->setHorizontalHeaderLabels( aColumnNames );
107
108   // Layout
109   addWidget( aModeGroup );
110   addLayout( anUpdateSelLayout );
111   addWidget( myTable );
112
113   // Connect signals and slots
114   connect( myModeButtons, SIGNAL( buttonClicked( int ) ), this, SLOT( onModeActivated( int ) ) );
115   connect( myUpdateSelBtn, SIGNAL( clicked() ), this, SIGNAL( updateSelection() ) );
116   connect( myTable->model(), SIGNAL( dataChanged ( const QModelIndex&, const QModelIndex& ) ), 
117            this, SLOT( onDataChanged() ) );
118 }
119
120 HYDROGUI_GeoreferencementDlg::~HYDROGUI_GeoreferencementDlg()
121 {
122 }
123
124 void HYDROGUI_GeoreferencementDlg::onModeActivated( int theMode )
125 {
126   myUpdateSelBtn->setEnabled( theMode == SelectedProfiles );
127   emit modeActivated( theMode );
128 }
129
130 void HYDROGUI_GeoreferencementDlg::reset()
131 {
132   // Activate the "All" mode
133   myModeButtons->button( AllProfiles )->setChecked( true );
134
135   // Clear the table widget
136   myTable->setRowCount( 0 );
137 }
138
139 void HYDROGUI_GeoreferencementDlg::setMode( const int theMode )
140 {
141   bool isBlocked = myModeButtons->blockSignals( true );
142
143   QAbstractButton* aModeButton = myModeButtons->button( theMode );
144   if ( aModeButton ) {
145     aModeButton->setChecked( true );
146   }
147
148   myUpdateSelBtn->setEnabled( theMode == SelectedProfiles );
149
150   myModeButtons->blockSignals( isBlocked );
151 }
152
153 void HYDROGUI_GeoreferencementDlg::setData( const ProfilesGeoDataList& theData )
154 {
155   disconnect( myTable->model(), SIGNAL( dataChanged ( const QModelIndex&, const QModelIndex& ) ), 
156               this, SLOT( onDataChanged() ) );
157
158   myTable->setRowCount( 0 );
159
160   foreach ( const ProfileGeoData& aGeoData, theData ) {
161     // Check the current profile name
162     if ( aGeoData.Name.isEmpty() ) {
163       continue;
164     }
165
166     // Get georeferencement data for the current profile
167     QString aXg, anYg, aXd, anYd;
168     if ( !aGeoData.isEmpty ) {
169       aXg = HYDROGUI_Tool::GetCoordinateString( aGeoData.Xg, false );
170       anYg = HYDROGUI_Tool::GetCoordinateString( aGeoData.Yg, false );
171       aXd = HYDROGUI_Tool::GetCoordinateString( aGeoData.Xd, false );
172       anYd = HYDROGUI_Tool::GetCoordinateString( aGeoData.Yd, false );
173     }
174     
175     // Insert row with the data
176     int aRow = myTable->rowCount();
177     myTable->insertRow( aRow );
178
179     // "Profile" column
180     QTableWidgetItem* aNameItem = new QTableWidgetItem( aGeoData.Name );
181     aNameItem->setFlags( aNameItem->flags() & ~Qt::ItemIsEnabled );
182     /* Bold font is not used in other tables. Keep the common style.
183     QFont aFont = aNameItem->font();
184     aFont.setBold( true );
185     aNameItem->setFont( aFont );
186     */ 
187     myTable->setItem( aRow, 0, aNameItem );
188
189     // "Xg" column
190     myTable->setItem( aRow, 1, new QTableWidgetItem( aXg ) );
191
192     // "Yg" column
193     myTable->setItem( aRow, 2, new QTableWidgetItem( anYg ) );
194
195     // "Xd" column
196     myTable->setItem( aRow, 3, new QTableWidgetItem( aXd ) );
197
198     // "Yd" column
199     myTable->setItem( aRow, 4, new QTableWidgetItem( anYd ) );
200   }
201
202   myTable->resizeColumnToContents( 0 );
203   myTable->resizeRowsToContents();
204
205   myIsModified = false;
206   
207   connect( myTable->model(), SIGNAL( dataChanged ( const QModelIndex&, const QModelIndex& ) ), 
208            this, SLOT( onDataChanged() ) );
209 }
210
211 void HYDROGUI_GeoreferencementDlg::getData( ProfilesGeoDataList& theData ) const
212 {
213   // Clear the list
214   theData.clear();
215
216   // Fill the map
217   bool isOk = false;
218   QString aXg, anYg, aXd, anYd;
219   for ( int aRow = 0; aRow < myTable->rowCount(); aRow++ ) {
220     QString aProfileName = myTable->item( aRow, 0 )->text();
221
222     aXg = myTable->item( aRow, 1 )->text();
223     anYg = myTable->item( aRow, 2 )->text();
224     aXd = myTable->item( aRow, 3 )->text();
225     anYd = myTable->item( aRow, 4 )->text();
226    
227     theData.append( ProfileGeoData( aProfileName, aXg, anYg, aXd, anYd ) );
228   }
229 }
230
231 void HYDROGUI_GeoreferencementDlg::onMousePress( 
232   SUIT_ViewWindow* theViewWindow, QMouseEvent* theEvent )
233 {
234   // Check the parameters
235   OCCViewer_ViewWindow* anOCCViewWindow = 
236     dynamic_cast<OCCViewer_ViewWindow*>(theViewWindow);
237   if ( !anOCCViewWindow || (theEvent->button() != Qt::LeftButton) ) {
238     return;
239   }
240   
241   // Check for current cell
242   int aRow = myTable->currentRow();
243   int aColumn = myTable->currentColumn();
244   if ( aRow < 0 || aColumn <= 0 ) {
245     return;
246   }
247
248   // Get the selected point coordinates
249   OCCViewer_ViewPort3d* aViewPort = anOCCViewWindow->getViewPort();
250   gp_Pnt aPnt = CurveCreator_Utils::ConvertClickToPoint( theEvent->x(), theEvent->y(), 
251                                                          aViewPort->getView() );
252
253   // Set the coordinates to the corresponding cells of the table
254   int aColumnX = aColumn < 3 ? 1 : 3;
255   int aColumnY = aColumnX + 1;
256   
257   QString aXStr = HYDROGUI_Tool::GetCoordinateString( aPnt.X(), false );
258   QString anYStr = HYDROGUI_Tool::GetCoordinateString( aPnt.Y(), false );
259   myTable->item( aRow, aColumnX )->setText( aXStr );
260   myTable->item( aRow, aColumnY )->setText( anYStr );
261 }
262
263 void HYDROGUI_GeoreferencementDlg::onDataChanged()
264 {
265   myIsModified = true;
266 }
267
268 bool HYDROGUI_GeoreferencementDlg::isModified() const
269 {
270   return myIsModified;
271 }