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 //! Custom item delegate (line edit with double validator)
44 class HYDROGUI_GeoreferencementDlg::Delegate : public QItemDelegate
47 Delegate( QObject* = 0 );
49 QWidget* createEditor( QWidget*, const QStyleOptionViewItem&,
50 const QModelIndex& ) const;
52 void setEditorData( QWidget*, const QModelIndex& ) const;
53 void setModelData( QWidget*, QAbstractItemModel*, const QModelIndex& ) const;
56 HYDROGUI_GeoreferencementDlg::Delegate::Delegate( QObject* theParent )
57 : QItemDelegate( theParent )
61 QWidget* HYDROGUI_GeoreferencementDlg::Delegate::createEditor(
62 QWidget* theParent, const QStyleOptionViewItem& theOption,
63 const QModelIndex& theIndex ) const
65 QWidget* anEditor = 0;
67 if ( theIndex.column() > 0 ) {
68 QVariant aData = theIndex.data( Qt::DisplayRole );
70 QLineEdit* aLineEdit = new QLineEdit( theParent );
71 aLineEdit->setValidator( new QDoubleValidator( aLineEdit ) );
72 aLineEdit->setText( aData.toString() );
78 anEditor = QItemDelegate::createEditor( theParent, theOption, theIndex );
84 void HYDROGUI_GeoreferencementDlg::Delegate::setEditorData(
85 QWidget* theEditor, const QModelIndex& theIndex ) const
87 QLineEdit* anEditor = qobject_cast<QLineEdit*>( theEditor );
89 anEditor->setText( theIndex.data( Qt::EditRole ).toString() );
93 void HYDROGUI_GeoreferencementDlg::Delegate::setModelData(
94 QWidget* theEditor, QAbstractItemModel* theModel, const QModelIndex& theIndex) const
96 QLineEdit* anEditor = qobject_cast<QLineEdit*>( theEditor );
98 theModel->setData( theIndex, anEditor->text() );
102 HYDROGUI_GeoreferencementDlg::HYDROGUI_GeoreferencementDlg( HYDROGUI_Module* theModule, const QString& theTitle )
103 : HYDROGUI_InputPanel( theModule, theTitle )
105 // Mode selector (all/selected)
106 QGroupBox* aModeGroup = new QGroupBox( tr( "PROFILES" ), this );
108 QRadioButton* anAllRB = new QRadioButton( tr( "ALL_MODE" ), aModeGroup );
109 QRadioButton* aSelectedRB = new QRadioButton( tr( "SELECTED_MODE" ), aModeGroup );
111 myModeButtons = new QButtonGroup( aModeGroup );
112 myModeButtons->addButton( anAllRB, AllProfiles );
113 myModeButtons->addButton( aSelectedRB, SelectedProfiles );
115 QBoxLayout* aModeSelectorLayout = new QVBoxLayout( aModeGroup );
116 aModeSelectorLayout->setMargin( 5 );
117 aModeSelectorLayout->setSpacing( 5 );
118 aModeSelectorLayout->addWidget( anAllRB );
119 aModeSelectorLayout->addWidget( aSelectedRB );
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 );
134 addWidget( aModeGroup );
135 addWidget( myTable );
137 // Connect signals and slots
138 connect( myModeButtons, SIGNAL( buttonClicked( int ) ), this, SLOT( onModeActivated( int ) ) );
141 HYDROGUI_GeoreferencementDlg::~HYDROGUI_GeoreferencementDlg()
145 void HYDROGUI_GeoreferencementDlg::onModeActivated( int theMode )
147 emit modeActivated( theMode );
150 void HYDROGUI_GeoreferencementDlg::reset()
152 // Activate the "All" mode
153 myModeButtons->button( AllProfiles )->setChecked( true );
155 // Clear the table widget
156 myTable->setRowCount( 0 );
159 void HYDROGUI_GeoreferencementDlg::setMode( const int theMode )
161 bool isBlocked = myModeButtons->blockSignals( true );
163 QAbstractButton* aModeButton = myModeButtons->button( theMode );
165 aModeButton->setChecked( true );
168 myModeButtons->blockSignals( isBlocked );
171 void HYDROGUI_GeoreferencementDlg::setData( const ProfilesGeoDataMap& theMap )
173 myTable->setRowCount( 0 );
175 foreach ( const QString& aProfileName, theMap.keys() ) {
176 // Check the current profile name
177 if ( aProfileName.isEmpty() ) {
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 );
191 // Insert row with the data
192 int aRow = myTable->rowCount();
193 myTable->insertRow( aRow );
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 );
204 myTable->setItem( aRow, 1, new QTableWidgetItem( aXg ) );
207 myTable->setItem( aRow, 2, new QTableWidgetItem( anYg ) );
210 myTable->setItem( aRow, 3, new QTableWidgetItem( aXd ) );
213 myTable->setItem( aRow, 4, new QTableWidgetItem( anYd ) );
216 myTable->resizeColumnToContents( 0 );
219 void HYDROGUI_GeoreferencementDlg::getData( ProfilesGeoDataMap& theMap )
226 for ( int aRow = 0; aRow < myTable->rowCount(); aRow++ ) {
227 QString aProfileName = myTable->item( aRow, 0 )->text();
229 theMap.insert( aProfileName, ProfileGeoData() );
231 double aXg, anYg, aXd, anYd;
233 aXg = myTable->item( aRow, 1 )->text().toDouble( &isOk );
234 if ( !isOk ) continue;
236 anYg = myTable->item( aRow, 2 )->text().toDouble( &isOk );
237 if ( !isOk ) continue;
239 aXd = myTable->item( aRow, 3 )->text().toDouble( &isOk );
240 if ( !isOk ) continue;
242 anYd = myTable->item( aRow, 4 )->text().toDouble( &isOk );
243 if ( !isOk ) continue;
245 theMap[aProfileName] = ProfileGeoData( aXg, anYg, aXd, anYd );
249 void HYDROGUI_GeoreferencementDlg::onMousePress(
250 SUIT_ViewWindow* theViewWindow, QMouseEvent* theEvent )
252 // Check the parameters
253 OCCViewer_ViewWindow* anOCCViewWindow =
254 dynamic_cast<OCCViewer_ViewWindow*>(theViewWindow);
255 if ( !anOCCViewWindow || (theEvent->button() != Qt::LeftButton) ) {
259 // Check for current cell
260 int aRow = myTable->currentRow();
261 int aColumn = myTable->currentColumn();
262 if ( aRow < 0 || aColumn <= 0 ) {
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() );
271 // Set the coordinates to the corresponding cells of the table
272 int aColumnX = aColumn < 3 ? 1 : 3;
273 int aColumnY = aColumnX + 1;
275 myTable->item( aRow, aColumnX )->setText( getString( aPnt.X() ) );
276 myTable->item( aRow, aColumnY )->setText( getString( aPnt.Y() ) );
279 QString HYDROGUI_GeoreferencementDlg::getString( const double theNumber ) const
281 return QString::number( theNumber, 'g', 12 );