1 // Copyright (C) 2014-2015 EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 // Lesser General Public License for more details.
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include "HYDROGUI_GeoreferencementDlg.h"
21 #include "HYDROGUI_Tool.h"
23 #include <CurveCreator_Utils.hxx>
25 #include <OCCViewer_ViewWindow.h>
26 #include <OCCViewer_ViewManager.h>
27 #include <OCCViewer_ViewPort3d.h>
29 #include <AIS_InteractiveContext.hxx>
31 #include <QTableWidget>
32 #include <QItemDelegate>
33 #include <QHeaderView>
34 #include <QRadioButton>
35 #include <QPushButton>
37 #include <QButtonGroup>
40 #include <QMouseEvent>
42 //! Profile data structre constructor
43 HYDROGUI_GeoreferencementDlg::ProfileGeoData::ProfileGeoData(
44 const QString& theName,
45 const QString& theXg, const QString& theYg,
46 const QString& theXd, const QString& theYd)
49 this->isEmpty = theXg.isEmpty() && theYg.isEmpty() &&
50 theXd.isEmpty() && theYd.isEmpty();
51 this->isIncomplete = !isEmpty;
56 this->Xg= theXg.toDouble( &isOk );
58 this->Yg = theYg.toDouble( &isOk );
60 this->Xd = theXd.toDouble( &isOk );
62 this->Yd = theYd.toDouble( &isOk );
63 this->isIncomplete = !isOk;
70 //! Custom item delegate (line edit with double validator)
71 class HYDROGUI_GeoreferencementDlg::Delegate : public QItemDelegate
74 Delegate( QObject* = 0 );
76 QWidget* createEditor( QWidget*, const QStyleOptionViewItem&,
77 const QModelIndex& ) const;
79 void setEditorData( QWidget*, const QModelIndex& ) const;
80 void setModelData( QWidget*, QAbstractItemModel*, const QModelIndex& ) const;
83 HYDROGUI_GeoreferencementDlg::Delegate::Delegate( QObject* theParent )
84 : QItemDelegate( theParent )
88 QWidget* HYDROGUI_GeoreferencementDlg::Delegate::createEditor(
89 QWidget* theParent, const QStyleOptionViewItem& theOption,
90 const QModelIndex& theIndex ) const
92 QWidget* anEditor = 0;
94 if ( theIndex.column() > 0 ) {
95 QLineEdit* aLineEdit = new QLineEdit( theParent );
96 QDoubleValidator* aDoubleValidator = new QDoubleValidator();
97 aDoubleValidator->setNotation( QDoubleValidator::StandardNotation );
98 aDoubleValidator->setDecimals( 2 );
99 aLineEdit->setValidator( aDoubleValidator );
100 anEditor = aLineEdit;
102 anEditor = QItemDelegate::createEditor( theParent, theOption, theIndex );
108 void HYDROGUI_GeoreferencementDlg::Delegate::setEditorData(
109 QWidget* theEditor, const QModelIndex& theIndex ) const
111 if ( QLineEdit* aLineEdit = dynamic_cast<QLineEdit*>( theEditor ) ) {
112 aLineEdit->setText( theIndex.data( Qt::EditRole ).toString() );
114 QItemDelegate::setEditorData( theEditor, theIndex );
118 void HYDROGUI_GeoreferencementDlg::Delegate::setModelData(
119 QWidget* theEditor, QAbstractItemModel* theModel, const QModelIndex& theIndex) const
121 if ( QLineEdit* aLineEdit = dynamic_cast<QLineEdit*>( theEditor ) ) {
122 theModel->setData( theIndex, aLineEdit->text() );
124 QItemDelegate::setModelData( theEditor, theModel, theIndex );
128 HYDROGUI_GeoreferencementDlg::HYDROGUI_GeoreferencementDlg( HYDROGUI_Module* theModule, const QString& theTitle )
129 : HYDROGUI_InputPanel( theModule, theTitle ), myIsModified( false )
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 );
147 // Update selection button
148 myUpdateSelBtn = new QPushButton( mainFrame() );
149 myUpdateSelBtn->setText( tr("UPDATE_SELECTION") );
150 QBoxLayout* anUpdateSelLayout = new QHBoxLayout( mainFrame() );
151 anUpdateSelLayout->addWidget( myUpdateSelBtn );
152 anUpdateSelLayout->addStretch();
155 myTable = new QTableWidget( mainFrame() );
156 myTable->setItemDelegate( new Delegate( this ) );
157 myTable->verticalHeader()->setVisible( false );
158 myTable->setSelectionBehavior( QAbstractItemView::SelectItems );
159 myTable->setSelectionMode( QAbstractItemView::SingleSelection );
160 myTable->setColumnCount( 5 );
161 QStringList aColumnNames;
162 aColumnNames << tr( "PROFILE_HEADER" ) << tr( "XG_HEADER" ) << tr( "YG_HEADER" ) <<
163 tr( "XD_HEADER" ) << tr( "YD_HEADER" );
164 myTable->setHorizontalHeaderLabels( aColumnNames );
167 addWidget( aModeGroup );
168 addLayout( anUpdateSelLayout );
169 addWidget( myTable );
171 // Connect signals and slots
172 connect( myModeButtons, SIGNAL( buttonClicked( int ) ), this, SLOT( onModeActivated( int ) ) );
173 connect( myUpdateSelBtn, SIGNAL( clicked() ), this, SIGNAL( updateSelection() ) );
174 connect( myTable->model(), SIGNAL( dataChanged ( const QModelIndex&, const QModelIndex& ) ),
175 this, SLOT( onDataChanged() ) );
178 HYDROGUI_GeoreferencementDlg::~HYDROGUI_GeoreferencementDlg()
182 void HYDROGUI_GeoreferencementDlg::onModeActivated( int theMode )
184 myUpdateSelBtn->setEnabled( theMode == SelectedProfiles );
185 emit modeActivated( theMode );
188 void HYDROGUI_GeoreferencementDlg::reset()
190 // Activate the "All" mode
191 myModeButtons->button( AllProfiles )->setChecked( true );
193 // Clear the table widget
194 myTable->setRowCount( 0 );
197 void HYDROGUI_GeoreferencementDlg::setMode( const int theMode )
199 bool isBlocked = myModeButtons->blockSignals( true );
201 QAbstractButton* aModeButton = myModeButtons->button( theMode );
203 aModeButton->setChecked( true );
206 myUpdateSelBtn->setEnabled( theMode == SelectedProfiles );
208 myModeButtons->blockSignals( isBlocked );
211 void HYDROGUI_GeoreferencementDlg::setData( const ProfilesGeoDataList& theData )
213 disconnect( myTable->model(), SIGNAL( dataChanged ( const QModelIndex&, const QModelIndex& ) ),
214 this, SLOT( onDataChanged() ) );
216 myTable->setRowCount( 0 );
218 foreach ( const ProfileGeoData& aGeoData, theData ) {
219 // Check the current profile name
220 if ( aGeoData.Name.isEmpty() ) {
224 // Get georeferencement data for the current profile
225 QString aXg, anYg, aXd, anYd;
226 if ( !aGeoData.isEmpty ) {
227 aXg = HYDROGUI_Tool::GetCoordinateString( aGeoData.Xg, false );
228 anYg = HYDROGUI_Tool::GetCoordinateString( aGeoData.Yg, false );
229 aXd = HYDROGUI_Tool::GetCoordinateString( aGeoData.Xd, false );
230 anYd = HYDROGUI_Tool::GetCoordinateString( aGeoData.Yd, false );
233 // Insert row with the data
234 int aRow = myTable->rowCount();
235 myTable->insertRow( aRow );
238 QTableWidgetItem* aNameItem = new QTableWidgetItem( aGeoData.Name );
239 aNameItem->setFlags( aNameItem->flags() & ~Qt::ItemIsEnabled );
240 /* Bold font is not used in other tables. Keep the common style.
241 QFont aFont = aNameItem->font();
242 aFont.setBold( true );
243 aNameItem->setFont( aFont );
245 myTable->setItem( aRow, 0, aNameItem );
248 myTable->setItem( aRow, 1, new QTableWidgetItem( aXg ) );
251 myTable->setItem( aRow, 2, new QTableWidgetItem( anYg ) );
254 myTable->setItem( aRow, 3, new QTableWidgetItem( aXd ) );
257 myTable->setItem( aRow, 4, new QTableWidgetItem( anYd ) );
260 myTable->resizeColumnToContents( 0 );
261 myTable->resizeRowsToContents();
263 myIsModified = false;
265 connect( myTable->model(), SIGNAL( dataChanged ( const QModelIndex&, const QModelIndex& ) ),
266 this, SLOT( onDataChanged() ) );
269 void HYDROGUI_GeoreferencementDlg::getData( ProfilesGeoDataList& theData ) const
276 QString aXg, anYg, aXd, anYd;
277 for ( int aRow = 0; aRow < myTable->rowCount(); aRow++ ) {
278 QString aProfileName = myTable->item( aRow, 0 )->text();
280 aXg = myTable->item( aRow, 1 )->text();
281 anYg = myTable->item( aRow, 2 )->text();
282 aXd = myTable->item( aRow, 3 )->text();
283 anYd = myTable->item( aRow, 4 )->text();
285 theData.append( ProfileGeoData( aProfileName, aXg, anYg, aXd, anYd ) );
289 void HYDROGUI_GeoreferencementDlg::onMousePress(
290 SUIT_ViewWindow* theViewWindow, QMouseEvent* theEvent )
292 // Check the parameters
293 OCCViewer_ViewWindow* anOCCViewWindow =
294 dynamic_cast<OCCViewer_ViewWindow*>(theViewWindow);
295 if ( !anOCCViewWindow || (theEvent->button() != Qt::LeftButton) ) {
299 // Check for current cell
300 int aRow = myTable->currentRow();
301 int aColumn = myTable->currentColumn();
302 if ( aRow < 0 || aColumn <= 0 ) {
306 // Get the selected point coordinates
307 OCCViewer_ViewPort3d* aViewPort = anOCCViewWindow->getViewPort();
308 gp_Pnt aPnt = CurveCreator_Utils::ConvertClickToPoint( theEvent->x(), theEvent->y(),
309 aViewPort->getView() );
311 // Set the coordinates to the corresponding cells of the table
312 int aColumnX = aColumn < 3 ? 1 : 3;
313 int aColumnY = aColumnX + 1;
315 QString aXStr = HYDROGUI_Tool::GetCoordinateString( aPnt.X(), false );
316 QString anYStr = HYDROGUI_Tool::GetCoordinateString( aPnt.Y(), false );
317 myTable->item( aRow, aColumnX )->setText( aXStr );
318 myTable->item( aRow, aColumnY )->setText( anYStr );
321 void HYDROGUI_GeoreferencementDlg::onDataChanged()
326 bool HYDROGUI_GeoreferencementDlg::isModified() const