]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDROGUI/HYDROGUI_GeoreferencementDlg.cxx
Salome HOME
Reference objetcs are added for chanel.
[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 //! Profile data structre constructor
44 HYDROGUI_GeoreferencementDlg::ProfileGeoData::ProfileGeoData( 
45   const QString& theXg, const QString& theYg, 
46   const QString& theXd, const QString& theYd)
47 {
48   this->isEmpty = theXg.isEmpty() && theYg.isEmpty() &&
49                  theXd.isEmpty() && theYd.isEmpty();
50   this->isIncomplete = !isEmpty;
51
52   if ( isIncomplete ) {
53     bool isOk = false;
54
55     this->Xg= theXg.toDouble( &isOk );
56     if ( isOk ) {
57       this->Yg = theYg.toDouble( &isOk );
58       if ( isOk ) {
59         this->Xd = theXd.toDouble( &isOk );
60         if ( isOk ) {
61           this->Yd = theYd.toDouble( &isOk );
62           this->isIncomplete = !isOk;
63         }
64       }
65     }
66   }
67 }
68
69 //! Custom item delegate (line edit with double validator)
70 class HYDROGUI_GeoreferencementDlg::Delegate : public QItemDelegate
71 {
72 public:
73   Delegate( QObject* = 0 );
74   
75   QWidget* createEditor( QWidget*, const QStyleOptionViewItem&,
76                          const QModelIndex& ) const;
77   
78   void setEditorData( QWidget*, const QModelIndex& ) const;
79   void setModelData( QWidget*, QAbstractItemModel*, const QModelIndex& ) const;
80 };
81
82 HYDROGUI_GeoreferencementDlg::Delegate::Delegate( QObject* theParent )
83  : QItemDelegate( theParent )
84 {
85 }
86
87 QWidget* HYDROGUI_GeoreferencementDlg::Delegate::createEditor( 
88   QWidget* theParent, const QStyleOptionViewItem& theOption,
89   const QModelIndex& theIndex ) const
90 {
91   QWidget* anEditor = 0;
92
93   if ( theIndex.column() > 0 ) {
94     QVariant aData = theIndex.data( Qt::DisplayRole );
95     
96     QLineEdit* aLineEdit = new QLineEdit( theParent );
97     aLineEdit->setValidator( new QDoubleValidator( aLineEdit ) );
98     aLineEdit->setText( aData.toString() );
99
100     anEditor = aLineEdit;
101   }
102
103   if ( !anEditor) {
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   QLineEdit* anEditor = qobject_cast<QLineEdit*>( theEditor );
114   if ( anEditor ) {
115     anEditor->setText( theIndex.data( Qt::EditRole ).toString() );
116   }
117 }
118
119 void HYDROGUI_GeoreferencementDlg::Delegate::setModelData( 
120   QWidget* theEditor, QAbstractItemModel* theModel, const QModelIndex& theIndex) const
121 {
122   QLineEdit* anEditor = qobject_cast<QLineEdit*>( theEditor );
123   if ( anEditor ) {
124     theModel->setData( theIndex, anEditor->text() );
125   }
126 }
127
128 HYDROGUI_GeoreferencementDlg::HYDROGUI_GeoreferencementDlg( HYDROGUI_Module* theModule, const QString& theTitle )
129 : HYDROGUI_InputPanel( theModule, theTitle )
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   // Table
148   myTable = new QTableWidget( mainFrame() );
149   myTable->setItemDelegate( new Delegate( this ) );
150   myTable->verticalHeader()->setVisible( false );
151   myTable->setSelectionBehavior( QAbstractItemView::SelectItems );
152   myTable->setSelectionMode( QAbstractItemView::SingleSelection );
153   myTable->setColumnCount( 5 );
154   QStringList aColumnNames;
155   aColumnNames << tr( "PROFILE_HEADER" ) << tr( "XG_HEADER" ) << tr( "YG_HEADER" ) << 
156                                             tr( "XD_HEADER" ) << tr( "YD_HEADER" );
157   myTable->setHorizontalHeaderLabels( aColumnNames );
158
159   // Layout
160   addWidget( aModeGroup );
161   addWidget( myTable );
162
163   // Connect signals and slots
164   connect( myModeButtons, SIGNAL( buttonClicked( int ) ), this, SLOT( onModeActivated( int ) ) );
165 }
166
167 HYDROGUI_GeoreferencementDlg::~HYDROGUI_GeoreferencementDlg()
168 {
169 }
170
171 void HYDROGUI_GeoreferencementDlg::onModeActivated( int theMode )
172 {
173   emit modeActivated( theMode );
174 }
175
176 void HYDROGUI_GeoreferencementDlg::reset()
177 {
178   // Activate the "All" mode
179   myModeButtons->button( AllProfiles )->setChecked( true );
180
181   // Clear the table widget
182   myTable->setRowCount( 0 );
183 }
184
185 void HYDROGUI_GeoreferencementDlg::setMode( const int theMode )
186 {
187   bool isBlocked = myModeButtons->blockSignals( true );
188
189   QAbstractButton* aModeButton = myModeButtons->button( theMode );
190   if ( aModeButton ) {
191     aModeButton->setChecked( true );
192   }
193
194   myModeButtons->blockSignals( isBlocked );
195 }
196
197 void HYDROGUI_GeoreferencementDlg::setData( const ProfilesGeoDataMap& theMap )
198 {
199   myTable->setRowCount( 0 );
200
201   foreach ( const QString& aProfileName, theMap.keys() ) {
202     // Check the current profile name
203     if ( aProfileName.isEmpty() ) {
204       continue;
205     }
206
207     // Get georeferencement data for the current profile
208     ProfileGeoData aGeoData = theMap.value( aProfileName );
209     QString aXg, anYg, aXd, anYd;
210     if ( !aGeoData.isEmpty ) {
211       aXg = getString( aGeoData.Xg );
212       anYg = getString( aGeoData.Yg );
213       aXd = getString( aGeoData.Xd );
214       anYd = getString( aGeoData.Yd );
215     }
216     
217     // Insert row with the data
218     int aRow = myTable->rowCount();
219     myTable->insertRow( aRow );
220
221     // "Profile" column
222     QTableWidgetItem* aNameItem = new QTableWidgetItem( aProfileName );
223     aNameItem->setFlags( Qt::ItemIsEnabled );
224     QFont aFont = aNameItem->font();
225     aFont.setBold( true );
226     aNameItem->setFont( aFont ); 
227     myTable->setItem( aRow, 0, aNameItem );
228
229     // "Xg" column
230     myTable->setItem( aRow, 1, new QTableWidgetItem( aXg ) );
231
232     // "Yg" column
233     myTable->setItem( aRow, 2, new QTableWidgetItem( anYg ) );
234
235     // "Xd" column
236     myTable->setItem( aRow, 3, new QTableWidgetItem( aXd ) );
237
238     // "Yd" column
239     myTable->setItem( aRow, 4, new QTableWidgetItem( anYd ) );
240   }
241
242   myTable->resizeColumnToContents( 0 );
243 }
244
245 void HYDROGUI_GeoreferencementDlg::getData( ProfilesGeoDataMap& theMap )
246 {
247   // Clear the map
248   theMap.clear();
249
250   // Fill the map
251   bool isOk = false;
252   QString aXg, anYg, aXd, anYd;
253   for ( int aRow = 0; aRow < myTable->rowCount(); aRow++ ) {
254     QString aProfileName = myTable->item( aRow, 0 )->text();
255
256     aXg = myTable->item( aRow, 1 )->text();
257     anYg = myTable->item( aRow, 2 )->text();
258     aXd = myTable->item( aRow, 3 )->text();
259     anYd = myTable->item( aRow, 4 )->text();
260    
261     theMap.insert( aProfileName, ProfileGeoData( aXg, anYg, aXd, anYd ) );
262   }
263 }
264
265 void HYDROGUI_GeoreferencementDlg::onMousePress( 
266   SUIT_ViewWindow* theViewWindow, QMouseEvent* theEvent )
267 {
268   // Check the parameters
269   OCCViewer_ViewWindow* anOCCViewWindow = 
270     dynamic_cast<OCCViewer_ViewWindow*>(theViewWindow);
271   if ( !anOCCViewWindow || (theEvent->button() != Qt::LeftButton) ) {
272     return;
273   }
274   
275   // Check for current cell
276   int aRow = myTable->currentRow();
277   int aColumn = myTable->currentColumn();
278   if ( aRow < 0 || aColumn <= 0 ) {
279     return;
280   }
281
282   // Get the selected point coordinates
283   OCCViewer_ViewPort3d* aViewPort = anOCCViewWindow->getViewPort();
284   gp_Pnt aPnt = GEOMUtils::ConvertClickToPoint( theEvent->x(), theEvent->y(), 
285                                                 aViewPort->getView() );
286
287   // Set the coordinates to the corresponding cells of the table
288   int aColumnX = aColumn < 3 ? 1 : 3;
289   int aColumnY = aColumnX + 1;
290   
291   myTable->item( aRow, aColumnX )->setText( getString( aPnt.X() ) );
292   myTable->item( aRow, aColumnY )->setText( getString( aPnt.Y() ) );
293 }
294
295 QString HYDROGUI_GeoreferencementDlg::getString( const double theNumber ) const
296 {
297   return QString::number( theNumber, 'g', 12 );
298 }