1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "HYDROGUI_GeoreferencementDlg.h"
25 #include <GEOMUtils.hxx>
27 #include <OCCViewer_ViewWindow.h>
28 #include <OCCViewer_ViewManager.h>
29 #include <OCCViewer_ViewPort3d.h>
31 #include <AIS_InteractiveContext.hxx>
33 #include <QTableWidget>
34 #include <QItemDelegate>
35 #include <QHeaderView>
36 #include <QRadioButton>
38 #include <QButtonGroup>
41 #include <QMouseEvent>
43 //! Profile data structre constructor
44 HYDROGUI_GeoreferencementDlg::ProfileGeoData::ProfileGeoData(
45 const QString& theXg, const QString& theYg,
46 const QString& theXd, const QString& theYd)
48 this->isEmpty = theXg.isEmpty() && theYg.isEmpty() &&
49 theXd.isEmpty() && theYd.isEmpty();
50 this->isIncomplete = !isEmpty;
55 this->Xg= theXg.toDouble( &isOk );
57 this->Yg = theYg.toDouble( &isOk );
59 this->Xd = theXd.toDouble( &isOk );
61 this->Yd = theYd.toDouble( &isOk );
62 this->isIncomplete = !isOk;
69 //! Custom item delegate (line edit with double validator)
70 class HYDROGUI_GeoreferencementDlg::Delegate : public QItemDelegate
73 Delegate( QObject* = 0 );
75 QWidget* createEditor( QWidget*, const QStyleOptionViewItem&,
76 const QModelIndex& ) const;
78 void setEditorData( QWidget*, const QModelIndex& ) const;
79 void setModelData( QWidget*, QAbstractItemModel*, const QModelIndex& ) const;
82 HYDROGUI_GeoreferencementDlg::Delegate::Delegate( QObject* theParent )
83 : QItemDelegate( theParent )
87 QWidget* HYDROGUI_GeoreferencementDlg::Delegate::createEditor(
88 QWidget* theParent, const QStyleOptionViewItem& theOption,
89 const QModelIndex& theIndex ) const
91 QWidget* anEditor = 0;
93 if ( theIndex.column() > 0 ) {
94 QLineEdit* aLineEdit = new QLineEdit( theParent );
95 aLineEdit->setValidator( new QDoubleValidator( aLineEdit ) );
98 anEditor = QItemDelegate::createEditor( theParent, theOption, theIndex );
104 void HYDROGUI_GeoreferencementDlg::Delegate::setEditorData(
105 QWidget* theEditor, const QModelIndex& theIndex ) const
107 if ( QLineEdit* aLineEdit = dynamic_cast<QLineEdit*>( theEditor ) ) {
108 aLineEdit->setText( theIndex.data( Qt::EditRole ).toString() );
110 QItemDelegate::setEditorData( theEditor, theIndex );
114 void HYDROGUI_GeoreferencementDlg::Delegate::setModelData(
115 QWidget* theEditor, QAbstractItemModel* theModel, const QModelIndex& theIndex) const
117 if ( QLineEdit* aLineEdit = dynamic_cast<QLineEdit*>( theEditor ) ) {
118 theModel->setData( theIndex, aLineEdit->text() );
120 QItemDelegate::setModelData( theEditor, theModel, theIndex );
124 HYDROGUI_GeoreferencementDlg::HYDROGUI_GeoreferencementDlg( HYDROGUI_Module* theModule, const QString& theTitle )
125 : HYDROGUI_InputPanel( theModule, theTitle )
127 // Mode selector (all/selected)
128 QGroupBox* aModeGroup = new QGroupBox( tr( "PROFILES" ), this );
130 QRadioButton* anAllRB = new QRadioButton( tr( "ALL_MODE" ), aModeGroup );
131 QRadioButton* aSelectedRB = new QRadioButton( tr( "SELECTED_MODE" ), aModeGroup );
133 myModeButtons = new QButtonGroup( aModeGroup );
134 myModeButtons->addButton( anAllRB, AllProfiles );
135 myModeButtons->addButton( aSelectedRB, SelectedProfiles );
137 QBoxLayout* aModeSelectorLayout = new QVBoxLayout( aModeGroup );
138 aModeSelectorLayout->setMargin( 5 );
139 aModeSelectorLayout->setSpacing( 5 );
140 aModeSelectorLayout->addWidget( anAllRB );
141 aModeSelectorLayout->addWidget( aSelectedRB );
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 );
156 addWidget( aModeGroup );
157 addWidget( myTable );
159 // Connect signals and slots
160 connect( myModeButtons, SIGNAL( buttonClicked( int ) ), this, SLOT( onModeActivated( int ) ) );
163 HYDROGUI_GeoreferencementDlg::~HYDROGUI_GeoreferencementDlg()
167 void HYDROGUI_GeoreferencementDlg::onModeActivated( int theMode )
169 emit modeActivated( theMode );
172 void HYDROGUI_GeoreferencementDlg::reset()
174 // Activate the "All" mode
175 myModeButtons->button( AllProfiles )->setChecked( true );
177 // Clear the table widget
178 myTable->setRowCount( 0 );
181 void HYDROGUI_GeoreferencementDlg::setMode( const int theMode )
183 bool isBlocked = myModeButtons->blockSignals( true );
185 QAbstractButton* aModeButton = myModeButtons->button( theMode );
187 aModeButton->setChecked( true );
190 myModeButtons->blockSignals( isBlocked );
193 void HYDROGUI_GeoreferencementDlg::setData( const ProfilesGeoDataMap& theMap )
195 myTable->setRowCount( 0 );
197 foreach ( const QString& aProfileName, theMap.keys() ) {
198 // Check the current profile name
199 if ( aProfileName.isEmpty() ) {
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 );
213 // Insert row with the data
214 int aRow = myTable->rowCount();
215 myTable->insertRow( aRow );
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 );
226 myTable->setItem( aRow, 1, new QTableWidgetItem( aXg ) );
229 myTable->setItem( aRow, 2, new QTableWidgetItem( anYg ) );
232 myTable->setItem( aRow, 3, new QTableWidgetItem( aXd ) );
235 myTable->setItem( aRow, 4, new QTableWidgetItem( anYd ) );
238 myTable->resizeColumnToContents( 0 );
241 void HYDROGUI_GeoreferencementDlg::getData( ProfilesGeoDataMap& theMap )
248 QString aXg, anYg, aXd, anYd;
249 for ( int aRow = 0; aRow < myTable->rowCount(); aRow++ ) {
250 QString aProfileName = myTable->item( aRow, 0 )->text();
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();
257 theMap.insert( aProfileName, ProfileGeoData( aXg, anYg, aXd, anYd ) );
261 void HYDROGUI_GeoreferencementDlg::onMousePress(
262 SUIT_ViewWindow* theViewWindow, QMouseEvent* theEvent )
264 // Check the parameters
265 OCCViewer_ViewWindow* anOCCViewWindow =
266 dynamic_cast<OCCViewer_ViewWindow*>(theViewWindow);
267 if ( !anOCCViewWindow || (theEvent->button() != Qt::LeftButton) ) {
271 // Check for current cell
272 int aRow = myTable->currentRow();
273 int aColumn = myTable->currentColumn();
274 if ( aRow < 0 || aColumn <= 0 ) {
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() );
283 // Set the coordinates to the corresponding cells of the table
284 int aColumnX = aColumn < 3 ? 1 : 3;
285 int aColumnY = aColumnX + 1;
287 myTable->item( aRow, aColumnX )->setText( getString( aPnt.X() ) );
288 myTable->item( aRow, aColumnY )->setText( getString( aPnt.Y() ) );
291 QString HYDROGUI_GeoreferencementDlg::getString( const double theNumber ) const
293 return QString::number( theNumber, 'g', 12 );