Salome HOME
Profile object creation.
[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     QLineEdit* aLineEdit = new QLineEdit( theParent );
95     aLineEdit->setValidator( new QDoubleValidator( aLineEdit ) );
96     anEditor = aLineEdit;
97   } else {
98     anEditor = QItemDelegate::createEditor( theParent, theOption, theIndex );
99   }
100
101   return anEditor;
102 }
103
104 void HYDROGUI_GeoreferencementDlg::Delegate::setEditorData( 
105   QWidget* theEditor, const QModelIndex& theIndex ) const
106 {
107   if ( QLineEdit* aLineEdit = dynamic_cast<QLineEdit*>( theEditor ) ) {
108     aLineEdit->setText( theIndex.data( Qt::EditRole ).toString() );
109   } else {
110     QItemDelegate::setEditorData( theEditor, theIndex );
111   }
112 }
113
114 void HYDROGUI_GeoreferencementDlg::Delegate::setModelData( 
115   QWidget* theEditor, QAbstractItemModel* theModel, const QModelIndex& theIndex) const
116 {
117   if ( QLineEdit* aLineEdit = dynamic_cast<QLineEdit*>( theEditor ) ) {
118     theModel->setData( theIndex, aLineEdit->text() );
119   } else {
120     QItemDelegate::setModelData( theEditor, theModel, theIndex );
121   }
122 }
123
124 HYDROGUI_GeoreferencementDlg::HYDROGUI_GeoreferencementDlg( HYDROGUI_Module* theModule, const QString& theTitle )
125 : HYDROGUI_InputPanel( theModule, theTitle )
126 {
127   // Mode selector (all/selected)
128   QGroupBox* aModeGroup = new QGroupBox( tr( "PROFILES" ), this );
129
130   QRadioButton* anAllRB = new QRadioButton( tr( "ALL_MODE" ), aModeGroup );
131   QRadioButton* aSelectedRB = new QRadioButton( tr( "SELECTED_MODE" ), aModeGroup );
132
133   myModeButtons = new QButtonGroup( aModeGroup );
134   myModeButtons->addButton( anAllRB, AllProfiles );
135   myModeButtons->addButton( aSelectedRB, SelectedProfiles );
136
137   QBoxLayout* aModeSelectorLayout = new QVBoxLayout( aModeGroup );
138   aModeSelectorLayout->setMargin( 5 );
139   aModeSelectorLayout->setSpacing( 5 );
140   aModeSelectorLayout->addWidget( anAllRB );
141   aModeSelectorLayout->addWidget( aSelectedRB );
142
143   // Table
144   myTable = new QTableWidget( mainFrame() );
145   myTable->setItemDelegate( new Delegate( this ) );
146   myTable->verticalHeader()->setVisible( false );
147   myTable->setSelectionBehavior( QAbstractItemView::SelectItems );
148   myTable->setSelectionMode( QAbstractItemView::SingleSelection );
149   myTable->setColumnCount( 5 );
150   QStringList aColumnNames;
151   aColumnNames << tr( "PROFILE_HEADER" ) << tr( "XG_HEADER" ) << tr( "YG_HEADER" ) << 
152                                             tr( "XD_HEADER" ) << tr( "YD_HEADER" );
153   myTable->setHorizontalHeaderLabels( aColumnNames );
154
155   // Layout
156   addWidget( aModeGroup );
157   addWidget( myTable );
158
159   // Connect signals and slots
160   connect( myModeButtons, SIGNAL( buttonClicked( int ) ), this, SLOT( onModeActivated( int ) ) );
161 }
162
163 HYDROGUI_GeoreferencementDlg::~HYDROGUI_GeoreferencementDlg()
164 {
165 }
166
167 void HYDROGUI_GeoreferencementDlg::onModeActivated( int theMode )
168 {
169   emit modeActivated( theMode );
170 }
171
172 void HYDROGUI_GeoreferencementDlg::reset()
173 {
174   // Activate the "All" mode
175   myModeButtons->button( AllProfiles )->setChecked( true );
176
177   // Clear the table widget
178   myTable->setRowCount( 0 );
179 }
180
181 void HYDROGUI_GeoreferencementDlg::setMode( const int theMode )
182 {
183   bool isBlocked = myModeButtons->blockSignals( true );
184
185   QAbstractButton* aModeButton = myModeButtons->button( theMode );
186   if ( aModeButton ) {
187     aModeButton->setChecked( true );
188   }
189
190   myModeButtons->blockSignals( isBlocked );
191 }
192
193 void HYDROGUI_GeoreferencementDlg::setData( const ProfilesGeoDataMap& theMap )
194 {
195   myTable->setRowCount( 0 );
196
197   foreach ( const QString& aProfileName, theMap.keys() ) {
198     // Check the current profile name
199     if ( aProfileName.isEmpty() ) {
200       continue;
201     }
202
203     // Get georeferencement data for the current profile
204     ProfileGeoData aGeoData = theMap.value( aProfileName );
205     QString aXg, anYg, aXd, anYd;
206     if ( !aGeoData.isEmpty ) {
207       aXg = getString( aGeoData.Xg );
208       anYg = getString( aGeoData.Yg );
209       aXd = getString( aGeoData.Xd );
210       anYd = getString( aGeoData.Yd );
211     }
212     
213     // Insert row with the data
214     int aRow = myTable->rowCount();
215     myTable->insertRow( aRow );
216
217     // "Profile" column
218     QTableWidgetItem* aNameItem = new QTableWidgetItem( aProfileName );
219     aNameItem->setFlags( Qt::ItemIsEnabled );
220     QFont aFont = aNameItem->font();
221     aFont.setBold( true );
222     aNameItem->setFont( aFont ); 
223     myTable->setItem( aRow, 0, aNameItem );
224
225     // "Xg" column
226     myTable->setItem( aRow, 1, new QTableWidgetItem( aXg ) );
227
228     // "Yg" column
229     myTable->setItem( aRow, 2, new QTableWidgetItem( anYg ) );
230
231     // "Xd" column
232     myTable->setItem( aRow, 3, new QTableWidgetItem( aXd ) );
233
234     // "Yd" column
235     myTable->setItem( aRow, 4, new QTableWidgetItem( anYd ) );
236   }
237
238   myTable->resizeColumnToContents( 0 );
239 }
240
241 void HYDROGUI_GeoreferencementDlg::getData( ProfilesGeoDataMap& theMap )
242 {
243   // Clear the map
244   theMap.clear();
245
246   // Fill the map
247   bool isOk = false;
248   QString aXg, anYg, aXd, anYd;
249   for ( int aRow = 0; aRow < myTable->rowCount(); aRow++ ) {
250     QString aProfileName = myTable->item( aRow, 0 )->text();
251
252     aXg = myTable->item( aRow, 1 )->text();
253     anYg = myTable->item( aRow, 2 )->text();
254     aXd = myTable->item( aRow, 3 )->text();
255     anYd = myTable->item( aRow, 4 )->text();
256    
257     theMap.insert( aProfileName, ProfileGeoData( aXg, anYg, aXd, anYd ) );
258   }
259 }
260
261 void HYDROGUI_GeoreferencementDlg::onMousePress( 
262   SUIT_ViewWindow* theViewWindow, QMouseEvent* theEvent )
263 {
264   // Check the parameters
265   OCCViewer_ViewWindow* anOCCViewWindow = 
266     dynamic_cast<OCCViewer_ViewWindow*>(theViewWindow);
267   if ( !anOCCViewWindow || (theEvent->button() != Qt::LeftButton) ) {
268     return;
269   }
270   
271   // Check for current cell
272   int aRow = myTable->currentRow();
273   int aColumn = myTable->currentColumn();
274   if ( aRow < 0 || aColumn <= 0 ) {
275     return;
276   }
277
278   // Get the selected point coordinates
279   OCCViewer_ViewPort3d* aViewPort = anOCCViewWindow->getViewPort();
280   gp_Pnt aPnt = GEOMUtils::ConvertClickToPoint( theEvent->x(), theEvent->y(), 
281                                                 aViewPort->getView() );
282
283   // Set the coordinates to the corresponding cells of the table
284   int aColumnX = aColumn < 3 ? 1 : 3;
285   int aColumnY = aColumnX + 1;
286   
287   myTable->item( aRow, aColumnX )->setText( getString( aPnt.X() ) );
288   myTable->item( aRow, aColumnY )->setText( getString( aPnt.Y() ) );
289 }
290
291 QString HYDROGUI_GeoreferencementDlg::getString( const double theNumber ) const
292 {
293   return QString::number( theNumber, 'g', 12 );
294 }