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