Salome HOME
Set 2d mode for OCC viewer.
[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 <QTableWidget>
26 #include <QHeaderView>
27 #include <QRadioButton>
28 #include <QLineEdit>
29 #include <QButtonGroup>
30 #include <QGroupBox>
31 #include <QLayout>
32
33 HYDROGUI_GeoreferencementDlg::HYDROGUI_GeoreferencementDlg( HYDROGUI_Module* theModule, const QString& theTitle )
34 : HYDROGUI_InputPanel( theModule, theTitle )
35 {
36   // Mode selector (all/selected)
37   QGroupBox* aModeGroup = new QGroupBox( tr( "PROFILES" ), this );
38
39   QRadioButton* anAllRB = new QRadioButton( tr( "ALL_MODE" ), aModeGroup );
40   QRadioButton* aSelectedRB = new QRadioButton( tr( "SELECTED_MODE" ), aModeGroup );
41
42   myModeButtons = new QButtonGroup( aModeGroup );
43   myModeButtons->addButton( anAllRB, AllProfiles );
44   myModeButtons->addButton( aSelectedRB, SelectedProfiles );
45
46   QBoxLayout* aModeSelectorLayout = new QVBoxLayout( aModeGroup );
47   aModeSelectorLayout->setMargin( 5 );
48   aModeSelectorLayout->setSpacing( 5 );
49   aModeSelectorLayout->addWidget( anAllRB );
50   aModeSelectorLayout->addWidget( aSelectedRB );
51
52   // Table
53   myTable = new QTableWidget( mainFrame() );
54   myTable->verticalHeader()->setVisible( false );
55   myTable->setSelectionBehavior( QAbstractItemView::SelectItems );
56   myTable->setSelectionMode( QAbstractItemView::SingleSelection );
57   myTable->setColumnCount( 5 );
58   QStringList aColumnNames;
59   aColumnNames << tr( "PROFILE_HEADER" ) << tr( "XG_HEADER" ) << tr( "YG_HEADER" ) << 
60                                             tr( "XD_HEADER" ) << tr( "YD_HEADER" );
61   myTable->setHorizontalHeaderLabels( aColumnNames );
62
63   // Layout
64   addWidget( aModeGroup );
65   addWidget( myTable );
66
67   // Connect signals and slots
68   connect( myModeButtons, SIGNAL( buttonClicked( int ) ), this, SLOT( onModeActivated( int ) ) );
69 }
70
71 HYDROGUI_GeoreferencementDlg::~HYDROGUI_GeoreferencementDlg()
72 {
73 }
74
75 void HYDROGUI_GeoreferencementDlg::onModeActivated( int theMode )
76 {
77   emit modeActivated( theMode );
78 }
79
80 void HYDROGUI_GeoreferencementDlg::reset()
81 {
82   // Activate the "All" mode
83   myModeButtons->button( AllProfiles )->setChecked( true );
84
85   // Clear the table widget
86   myTable->setRowCount( 0 );
87 }
88
89 void HYDROGUI_GeoreferencementDlg::onProfilesSelectionChanged()
90 {
91   // MZN TODO
92 }
93
94 void HYDROGUI_GeoreferencementDlg::setMode( const int theMode )
95 {
96   bool isBlocked = myModeButtons->blockSignals( true );
97
98   QAbstractButton* aModeButton = myModeButtons->button( theMode );
99   if ( aModeButton ) {
100     aModeButton->setChecked( true );
101   }
102
103   myModeButtons->blockSignals( isBlocked );
104 }
105
106 void HYDROGUI_GeoreferencementDlg::setData( const ProfilesGeoDataMap& theMap )
107 {
108   myTable->setRowCount( 0 );
109
110   foreach ( const QString& aProfileName, theMap.keys() ) {
111     // Check the current profile name
112     if ( aProfileName.isEmpty() ) {
113       continue;
114     }
115
116     // Get georeferencement data for the current profile
117     ProfileGeoData aGeoData = theMap.value( aProfileName );
118     QString aXg, anYg, aXd, anYd;
119     if ( aGeoData.isValid ) {
120       aXg = QString::number( aGeoData.Xg );
121       anYg = QString::number( aGeoData.Yg );
122       aXd = QString::number( aGeoData.Xd );
123       anYd = QString::number( aGeoData.Yd );
124     }
125     
126     // Insert row with the data
127     int aRow = myTable->rowCount();
128     myTable->insertRow( aRow );
129
130     // "Profile" column
131     QTableWidgetItem* aNameItem = new QTableWidgetItem( aProfileName );
132     aNameItem->setFlags( Qt::ItemIsEnabled );
133     myTable->setItem( aRow, 0, aNameItem );
134
135     // "Xg" column
136     QLineEdit* aXgEdit = new QLineEdit( aXg );
137     aXgEdit->setValidator( new QDoubleValidator( aXgEdit ) );
138     myTable->setCellWidget( aRow, 1, aXgEdit );
139
140     // "Yg" column
141     QLineEdit* anYgEdit = new QLineEdit( anYg );
142     anYgEdit->setValidator( new QDoubleValidator( anYgEdit ) );
143     myTable->setCellWidget( aRow, 2, anYgEdit );
144
145     // "Xd" column
146     QLineEdit* aXdEdit = new QLineEdit( aXd );
147     aXdEdit->setValidator( new QDoubleValidator( aXdEdit ) );
148     myTable->setCellWidget( aRow, 3, aXdEdit );
149
150     // "Yd" column
151     QLineEdit* anYdEdit = new QLineEdit( anYd );
152     anYdEdit->setValidator( new QDoubleValidator( anYdEdit ) );
153     myTable->setCellWidget( aRow, 4, anYdEdit );
154   }
155 }
156
157 void HYDROGUI_GeoreferencementDlg::getData( ProfilesGeoDataMap& theMap )
158 {
159   // Clear the map
160   theMap.clear();
161
162   // Fill the map
163   bool isOk = false;
164   for ( int aRow = 0; aRow < myTable->rowCount(); aRow++ ) {
165     QString aProfileName = myTable->item( aRow, 0 )->text();
166
167     theMap.insert( aProfileName, ProfileGeoData() );
168
169     double aXg, anYg, aXd, anYd;
170     
171     QLineEdit* aLineEdit = qobject_cast<QLineEdit*>( myTable->cellWidget( aRow, 1 ) );
172     QString aCellText = aLineEdit ? aLineEdit->text() : "";
173     aXg = aCellText.toDouble( &isOk );
174     if ( !isOk ) continue;
175
176     aLineEdit = qobject_cast<QLineEdit*>( myTable->cellWidget( aRow, 2 ) );
177     aCellText = aLineEdit ? aLineEdit->text() : "";
178     anYg = aCellText.toDouble( &isOk );
179     if ( !isOk ) continue;
180
181     aLineEdit = qobject_cast<QLineEdit*>( myTable->cellWidget( aRow, 3 ) );
182     aCellText = aLineEdit ? aLineEdit->text() : "";
183     aXd = aCellText.toDouble( &isOk );
184     if ( !isOk ) continue;
185
186     aLineEdit = qobject_cast<QLineEdit*>( myTable->cellWidget( aRow, 4 ) );
187     aCellText = aLineEdit ? aLineEdit->text() : "";
188     anYd = aCellText.toDouble( &isOk );
189     if ( !isOk ) continue;
190    
191     theMap[aProfileName] = ProfileGeoData( aXg, anYg, aXd, anYd );
192   }
193 }