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 QVariant aData = theIndex.data( Qt::DisplayRole );
96 QLineEdit* aLineEdit = new QLineEdit( theParent );
97 aLineEdit->setValidator( new QDoubleValidator( aLineEdit ) );
98 aLineEdit->setText( aData.toString() );
100 anEditor = aLineEdit;
104 anEditor = QItemDelegate::createEditor( theParent, theOption, theIndex );
110 void HYDROGUI_GeoreferencementDlg::Delegate::setEditorData(
111 QWidget* theEditor, const QModelIndex& theIndex ) const
113 QLineEdit* anEditor = qobject_cast<QLineEdit*>( theEditor );
115 anEditor->setText( theIndex.data( Qt::EditRole ).toString() );
119 void HYDROGUI_GeoreferencementDlg::Delegate::setModelData(
120 QWidget* theEditor, QAbstractItemModel* theModel, const QModelIndex& theIndex) const
122 QLineEdit* anEditor = qobject_cast<QLineEdit*>( theEditor );
124 theModel->setData( theIndex, anEditor->text() );
128 HYDROGUI_GeoreferencementDlg::HYDROGUI_GeoreferencementDlg( HYDROGUI_Module* theModule, const QString& theTitle )
129 : HYDROGUI_InputPanel( theModule, theTitle )
131 // Mode selector (all/selected)
132 QGroupBox* aModeGroup = new QGroupBox( tr( "PROFILES" ), this );
134 QRadioButton* anAllRB = new QRadioButton( tr( "ALL_MODE" ), aModeGroup );
135 QRadioButton* aSelectedRB = new QRadioButton( tr( "SELECTED_MODE" ), aModeGroup );
137 myModeButtons = new QButtonGroup( aModeGroup );
138 myModeButtons->addButton( anAllRB, AllProfiles );
139 myModeButtons->addButton( aSelectedRB, SelectedProfiles );
141 QBoxLayout* aModeSelectorLayout = new QVBoxLayout( aModeGroup );
142 aModeSelectorLayout->setMargin( 5 );
143 aModeSelectorLayout->setSpacing( 5 );
144 aModeSelectorLayout->addWidget( anAllRB );
145 aModeSelectorLayout->addWidget( aSelectedRB );
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 );
160 addWidget( aModeGroup );
161 addWidget( myTable );
163 // Connect signals and slots
164 connect( myModeButtons, SIGNAL( buttonClicked( int ) ), this, SLOT( onModeActivated( int ) ) );
167 HYDROGUI_GeoreferencementDlg::~HYDROGUI_GeoreferencementDlg()
171 void HYDROGUI_GeoreferencementDlg::onModeActivated( int theMode )
173 emit modeActivated( theMode );
176 void HYDROGUI_GeoreferencementDlg::reset()
178 // Activate the "All" mode
179 myModeButtons->button( AllProfiles )->setChecked( true );
181 // Clear the table widget
182 myTable->setRowCount( 0 );
185 void HYDROGUI_GeoreferencementDlg::setMode( const int theMode )
187 bool isBlocked = myModeButtons->blockSignals( true );
189 QAbstractButton* aModeButton = myModeButtons->button( theMode );
191 aModeButton->setChecked( true );
194 myModeButtons->blockSignals( isBlocked );
197 void HYDROGUI_GeoreferencementDlg::setData( const ProfilesGeoDataMap& theMap )
199 myTable->setRowCount( 0 );
201 foreach ( const QString& aProfileName, theMap.keys() ) {
202 // Check the current profile name
203 if ( aProfileName.isEmpty() ) {
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 );
217 // Insert row with the data
218 int aRow = myTable->rowCount();
219 myTable->insertRow( aRow );
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 );
230 myTable->setItem( aRow, 1, new QTableWidgetItem( aXg ) );
233 myTable->setItem( aRow, 2, new QTableWidgetItem( anYg ) );
236 myTable->setItem( aRow, 3, new QTableWidgetItem( aXd ) );
239 myTable->setItem( aRow, 4, new QTableWidgetItem( anYd ) );
242 myTable->resizeColumnToContents( 0 );
245 void HYDROGUI_GeoreferencementDlg::getData( ProfilesGeoDataMap& theMap )
252 QString aXg, anYg, aXd, anYd;
253 for ( int aRow = 0; aRow < myTable->rowCount(); aRow++ ) {
254 QString aProfileName = myTable->item( aRow, 0 )->text();
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();
261 theMap.insert( aProfileName, ProfileGeoData( aXg, anYg, aXd, anYd ) );
265 void HYDROGUI_GeoreferencementDlg::onMousePress(
266 SUIT_ViewWindow* theViewWindow, QMouseEvent* theEvent )
268 // Check the parameters
269 OCCViewer_ViewWindow* anOCCViewWindow =
270 dynamic_cast<OCCViewer_ViewWindow*>(theViewWindow);
271 if ( !anOCCViewWindow || (theEvent->button() != Qt::LeftButton) ) {
275 // Check for current cell
276 int aRow = myTable->currentRow();
277 int aColumn = myTable->currentColumn();
278 if ( aRow < 0 || aColumn <= 0 ) {
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() );
287 // Set the coordinates to the corresponding cells of the table
288 int aColumnX = aColumn < 3 ? 1 : 3;
289 int aColumnY = aColumnX + 1;
291 myTable->item( aRow, aColumnX )->setText( getString( aPnt.X() ) );
292 myTable->item( aRow, aColumnY )->setText( getString( aPnt.Y() ) );
295 QString HYDROGUI_GeoreferencementDlg::getString( const double theNumber ) const
297 return QString::number( theNumber, 'g', 12 );