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 "HYDROGUI_Tool.h"
27 #include <GEOMUtils.hxx>
29 #include <OCCViewer_ViewWindow.h>
30 #include <OCCViewer_ViewManager.h>
31 #include <OCCViewer_ViewPort3d.h>
33 #include <AIS_InteractiveContext.hxx>
35 #include <QTableWidget>
36 #include <QItemDelegate>
37 #include <QHeaderView>
38 #include <QRadioButton>
39 #include <QPushButton>
41 #include <QButtonGroup>
44 #include <QMouseEvent>
46 //! Profile data structre constructor
47 HYDROGUI_GeoreferencementDlg::ProfileGeoData::ProfileGeoData(
48 const QString& theXg, const QString& theYg,
49 const QString& theXd, const QString& theYd)
51 this->isEmpty = theXg.isEmpty() && theYg.isEmpty() &&
52 theXd.isEmpty() && theYd.isEmpty();
53 this->isIncomplete = !isEmpty;
58 this->Xg= theXg.toDouble( &isOk );
60 this->Yg = theYg.toDouble( &isOk );
62 this->Xd = theXd.toDouble( &isOk );
64 this->Yd = theYd.toDouble( &isOk );
65 this->isIncomplete = !isOk;
72 //! Custom item delegate (line edit with double validator)
73 class HYDROGUI_GeoreferencementDlg::Delegate : public QItemDelegate
76 Delegate( QObject* = 0 );
78 QWidget* createEditor( QWidget*, const QStyleOptionViewItem&,
79 const QModelIndex& ) const;
81 void setEditorData( QWidget*, const QModelIndex& ) const;
82 void setModelData( QWidget*, QAbstractItemModel*, const QModelIndex& ) const;
85 HYDROGUI_GeoreferencementDlg::Delegate::Delegate( QObject* theParent )
86 : QItemDelegate( theParent )
90 QWidget* HYDROGUI_GeoreferencementDlg::Delegate::createEditor(
91 QWidget* theParent, const QStyleOptionViewItem& theOption,
92 const QModelIndex& theIndex ) const
94 QWidget* anEditor = 0;
96 if ( theIndex.column() > 0 ) {
97 QLineEdit* aLineEdit = new QLineEdit( theParent );
98 QDoubleValidator* aDoubleValidator = new QDoubleValidator();
99 aDoubleValidator->setNotation( QDoubleValidator::StandardNotation );
100 aDoubleValidator->setDecimals( 2 );
101 aLineEdit->setValidator( aDoubleValidator );
102 anEditor = aLineEdit;
104 anEditor = QItemDelegate::createEditor( theParent, theOption, theIndex );
110 void HYDROGUI_GeoreferencementDlg::Delegate::setEditorData(
111 QWidget* theEditor, const QModelIndex& theIndex ) const
113 if ( QLineEdit* aLineEdit = dynamic_cast<QLineEdit*>( theEditor ) ) {
114 aLineEdit->setText( theIndex.data( Qt::EditRole ).toString() );
116 QItemDelegate::setEditorData( theEditor, theIndex );
120 void HYDROGUI_GeoreferencementDlg::Delegate::setModelData(
121 QWidget* theEditor, QAbstractItemModel* theModel, const QModelIndex& theIndex) const
123 if ( QLineEdit* aLineEdit = dynamic_cast<QLineEdit*>( theEditor ) ) {
124 theModel->setData( theIndex, aLineEdit->text() );
126 QItemDelegate::setModelData( theEditor, theModel, theIndex );
130 HYDROGUI_GeoreferencementDlg::HYDROGUI_GeoreferencementDlg( HYDROGUI_Module* theModule, const QString& theTitle )
131 : HYDROGUI_InputPanel( theModule, theTitle ), myIsModified( false )
133 // Mode selector (all/selected)
134 QGroupBox* aModeGroup = new QGroupBox( tr( "PROFILES" ), this );
136 QRadioButton* anAllRB = new QRadioButton( tr( "ALL_MODE" ), aModeGroup );
137 QRadioButton* aSelectedRB = new QRadioButton( tr( "SELECTED_MODE" ), aModeGroup );
139 myModeButtons = new QButtonGroup( aModeGroup );
140 myModeButtons->addButton( anAllRB, AllProfiles );
141 myModeButtons->addButton( aSelectedRB, SelectedProfiles );
143 QBoxLayout* aModeSelectorLayout = new QVBoxLayout( aModeGroup );
144 aModeSelectorLayout->setMargin( 5 );
145 aModeSelectorLayout->setSpacing( 5 );
146 aModeSelectorLayout->addWidget( anAllRB );
147 aModeSelectorLayout->addWidget( aSelectedRB );
149 // Update selection button
150 myUpdateSelBtn = new QPushButton( mainFrame() );
151 myUpdateSelBtn->setText( tr("UPDATE_SELECTION") );
152 QBoxLayout* anUpdateSelLayout = new QHBoxLayout( mainFrame() );
153 anUpdateSelLayout->addWidget( myUpdateSelBtn );
154 anUpdateSelLayout->addStretch();
157 myTable = new QTableWidget( mainFrame() );
158 myTable->setItemDelegate( new Delegate( this ) );
159 myTable->verticalHeader()->setVisible( false );
160 myTable->setSelectionBehavior( QAbstractItemView::SelectItems );
161 myTable->setSelectionMode( QAbstractItemView::SingleSelection );
162 myTable->setColumnCount( 5 );
163 QStringList aColumnNames;
164 aColumnNames << tr( "PROFILE_HEADER" ) << tr( "XG_HEADER" ) << tr( "YG_HEADER" ) <<
165 tr( "XD_HEADER" ) << tr( "YD_HEADER" );
166 myTable->setHorizontalHeaderLabels( aColumnNames );
169 addWidget( aModeGroup );
170 addLayout( anUpdateSelLayout );
171 addWidget( myTable );
173 // Connect signals and slots
174 connect( myModeButtons, SIGNAL( buttonClicked( int ) ), this, SLOT( onModeActivated( int ) ) );
175 connect( myUpdateSelBtn, SIGNAL( clicked() ), this, SIGNAL( updateSelection() ) );
176 connect( myTable->model(), SIGNAL( dataChanged ( const QModelIndex&, const QModelIndex& ) ),
177 this, SLOT( onDataChanged() ) );
180 HYDROGUI_GeoreferencementDlg::~HYDROGUI_GeoreferencementDlg()
184 void HYDROGUI_GeoreferencementDlg::onModeActivated( int theMode )
186 myUpdateSelBtn->setEnabled( theMode == SelectedProfiles );
187 emit modeActivated( theMode );
190 void HYDROGUI_GeoreferencementDlg::reset()
192 // Activate the "All" mode
193 myModeButtons->button( AllProfiles )->setChecked( true );
195 // Clear the table widget
196 myTable->setRowCount( 0 );
199 void HYDROGUI_GeoreferencementDlg::setMode( const int theMode )
201 bool isBlocked = myModeButtons->blockSignals( true );
203 QAbstractButton* aModeButton = myModeButtons->button( theMode );
205 aModeButton->setChecked( true );
208 myUpdateSelBtn->setEnabled( theMode == SelectedProfiles );
210 myModeButtons->blockSignals( isBlocked );
213 void HYDROGUI_GeoreferencementDlg::setData( const ProfilesGeoDataMap& theMap )
215 disconnect( myTable->model(), SIGNAL( dataChanged ( const QModelIndex&, const QModelIndex& ) ),
216 this, SLOT( onDataChanged() ) );
218 myTable->setRowCount( 0 );
220 foreach ( const QString& aProfileName, theMap.keys() ) {
221 // Check the current profile name
222 if ( aProfileName.isEmpty() ) {
226 // Get georeferencement data for the current profile
227 ProfileGeoData aGeoData = theMap.value( aProfileName );
228 QString aXg, anYg, aXd, anYd;
229 if ( !aGeoData.isEmpty ) {
230 aXg = HYDROGUI_Tool::GetCoordinateString( aGeoData.Xg );
231 anYg = HYDROGUI_Tool::GetCoordinateString( aGeoData.Yg );
232 aXd = HYDROGUI_Tool::GetCoordinateString( aGeoData.Xd );
233 anYd = HYDROGUI_Tool::GetCoordinateString( aGeoData.Yd );
236 // Insert row with the data
237 int aRow = myTable->rowCount();
238 myTable->insertRow( aRow );
241 QTableWidgetItem* aNameItem = new QTableWidgetItem( aProfileName );
242 aNameItem->setFlags( Qt::ItemIsEnabled );
243 QFont aFont = aNameItem->font();
244 aFont.setBold( true );
245 aNameItem->setFont( aFont );
246 myTable->setItem( aRow, 0, aNameItem );
249 myTable->setItem( aRow, 1, new QTableWidgetItem( aXg ) );
252 myTable->setItem( aRow, 2, new QTableWidgetItem( anYg ) );
255 myTable->setItem( aRow, 3, new QTableWidgetItem( aXd ) );
258 myTable->setItem( aRow, 4, new QTableWidgetItem( anYd ) );
261 myTable->resizeColumnToContents( 0 );
262 myTable->resizeRowsToContents();
264 myIsModified = false;
266 connect( myTable->model(), SIGNAL( dataChanged ( const QModelIndex&, const QModelIndex& ) ),
267 this, SLOT( onDataChanged() ) );
270 void HYDROGUI_GeoreferencementDlg::getData( ProfilesGeoDataMap& theMap )
277 QString aXg, anYg, aXd, anYd;
278 for ( int aRow = 0; aRow < myTable->rowCount(); aRow++ ) {
279 QString aProfileName = myTable->item( aRow, 0 )->text();
281 aXg = myTable->item( aRow, 1 )->text();
282 anYg = myTable->item( aRow, 2 )->text();
283 aXd = myTable->item( aRow, 3 )->text();
284 anYd = myTable->item( aRow, 4 )->text();
286 theMap.insert( aProfileName, ProfileGeoData( aXg, anYg, aXd, anYd ) );
290 void HYDROGUI_GeoreferencementDlg::onMousePress(
291 SUIT_ViewWindow* theViewWindow, QMouseEvent* theEvent )
293 // Check the parameters
294 OCCViewer_ViewWindow* anOCCViewWindow =
295 dynamic_cast<OCCViewer_ViewWindow*>(theViewWindow);
296 if ( !anOCCViewWindow || (theEvent->button() != Qt::LeftButton) ) {
300 // Check for current cell
301 int aRow = myTable->currentRow();
302 int aColumn = myTable->currentColumn();
303 if ( aRow < 0 || aColumn <= 0 ) {
307 // Get the selected point coordinates
308 OCCViewer_ViewPort3d* aViewPort = anOCCViewWindow->getViewPort();
309 gp_Pnt aPnt = GEOMUtils::ConvertClickToPoint( theEvent->x(), theEvent->y(),
310 aViewPort->getView() );
312 // Set the coordinates to the corresponding cells of the table
313 int aColumnX = aColumn < 3 ? 1 : 3;
314 int aColumnY = aColumnX + 1;
316 QString aXStr = HYDROGUI_Tool::GetCoordinateString( aPnt.X() );
317 QString anYStr = HYDROGUI_Tool::GetCoordinateString( aPnt.Y() );
318 myTable->item( aRow, aColumnX )->setText( aXStr );
319 myTable->item( aRow, aColumnY )->setText( anYStr );
322 void HYDROGUI_GeoreferencementDlg::onDataChanged()
327 bool HYDROGUI_GeoreferencementDlg::isModified() const