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"
22 #include "HYDROGUI_LineEditDoubleValidator.h"
24 #include <CurveCreator_Utils.hxx>
26 #include <OCCViewer_ViewWindow.h>
27 #include <OCCViewer_ViewManager.h>
28 #include <OCCViewer_ViewPort3d.h>
30 #include <AIS_InteractiveContext.hxx>
32 #include <QTableWidget>
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 HYDROGUI_GeoreferencementDlg::HYDROGUI_GeoreferencementDlg( HYDROGUI_Module* theModule, const QString& theTitle )
71 : HYDROGUI_InputPanel( theModule, theTitle ), myIsModified( false )
73 // Mode selector (all/selected)
74 QGroupBox* aModeGroup = new QGroupBox( tr( "PROFILES" ), this );
76 QRadioButton* anAllRB = new QRadioButton( tr( "ALL_MODE" ), aModeGroup );
77 QRadioButton* aSelectedRB = new QRadioButton( tr( "SELECTED_MODE" ), aModeGroup );
79 myModeButtons = new QButtonGroup( aModeGroup );
80 myModeButtons->addButton( anAllRB, AllProfiles );
81 myModeButtons->addButton( aSelectedRB, SelectedProfiles );
83 QBoxLayout* aModeSelectorLayout = new QVBoxLayout( aModeGroup );
84 aModeSelectorLayout->setMargin( 5 );
85 aModeSelectorLayout->setSpacing( 5 );
86 aModeSelectorLayout->addWidget( anAllRB );
87 aModeSelectorLayout->addWidget( aSelectedRB );
89 // Update selection button
90 myUpdateSelBtn = new QPushButton( mainFrame() );
91 myUpdateSelBtn->setText( tr("UPDATE_SELECTION") );
92 QBoxLayout* anUpdateSelLayout = new QHBoxLayout( mainFrame() );
93 anUpdateSelLayout->addWidget( myUpdateSelBtn );
94 anUpdateSelLayout->addStretch();
97 myTable = new QTableWidget( mainFrame() );
98 myTable->setItemDelegate( new HYDROGUI_LineEditDoubleValidator( this ) );
99 myTable->verticalHeader()->setVisible( false );
100 myTable->setSelectionBehavior( QAbstractItemView::SelectItems );
101 myTable->setSelectionMode( QAbstractItemView::SingleSelection );
102 myTable->setColumnCount( 5 );
103 QStringList aColumnNames;
104 aColumnNames << tr( "PROFILE_HEADER" ) << tr( "XG_HEADER" ) << tr( "YG_HEADER" ) <<
105 tr( "XD_HEADER" ) << tr( "YD_HEADER" );
106 myTable->setHorizontalHeaderLabels( aColumnNames );
109 addWidget( aModeGroup );
110 addLayout( anUpdateSelLayout );
111 addWidget( myTable );
113 // Connect signals and slots
114 connect( myModeButtons, SIGNAL( buttonClicked( int ) ), this, SLOT( onModeActivated( int ) ) );
115 connect( myUpdateSelBtn, SIGNAL( clicked() ), this, SIGNAL( updateSelection() ) );
116 connect( myTable->model(), SIGNAL( dataChanged ( const QModelIndex&, const QModelIndex& ) ),
117 this, SLOT( onDataChanged() ) );
120 HYDROGUI_GeoreferencementDlg::~HYDROGUI_GeoreferencementDlg()
124 void HYDROGUI_GeoreferencementDlg::onModeActivated( int theMode )
126 myUpdateSelBtn->setEnabled( theMode == SelectedProfiles );
127 emit modeActivated( theMode );
130 void HYDROGUI_GeoreferencementDlg::reset()
132 // Activate the "All" mode
133 myModeButtons->button( AllProfiles )->setChecked( true );
135 // Clear the table widget
136 myTable->setRowCount( 0 );
139 void HYDROGUI_GeoreferencementDlg::setMode( const int theMode )
141 bool isBlocked = myModeButtons->blockSignals( true );
143 QAbstractButton* aModeButton = myModeButtons->button( theMode );
145 aModeButton->setChecked( true );
148 myUpdateSelBtn->setEnabled( theMode == SelectedProfiles );
150 myModeButtons->blockSignals( isBlocked );
153 void HYDROGUI_GeoreferencementDlg::setData( const ProfilesGeoDataList& theData )
155 disconnect( myTable->model(), SIGNAL( dataChanged ( const QModelIndex&, const QModelIndex& ) ),
156 this, SLOT( onDataChanged() ) );
158 myTable->setRowCount( 0 );
160 foreach ( const ProfileGeoData& aGeoData, theData ) {
161 // Check the current profile name
162 if ( aGeoData.Name.isEmpty() ) {
166 // Get georeferencement data for the current profile
167 QString aXg, anYg, aXd, anYd;
168 if ( !aGeoData.isEmpty ) {
169 aXg = HYDROGUI_Tool::GetCoordinateString( aGeoData.Xg, false );
170 anYg = HYDROGUI_Tool::GetCoordinateString( aGeoData.Yg, false );
171 aXd = HYDROGUI_Tool::GetCoordinateString( aGeoData.Xd, false );
172 anYd = HYDROGUI_Tool::GetCoordinateString( aGeoData.Yd, false );
175 // Insert row with the data
176 int aRow = myTable->rowCount();
177 myTable->insertRow( aRow );
180 QTableWidgetItem* aNameItem = new QTableWidgetItem( aGeoData.Name );
181 aNameItem->setFlags( aNameItem->flags() & ~Qt::ItemIsEnabled );
182 /* Bold font is not used in other tables. Keep the common style.
183 QFont aFont = aNameItem->font();
184 aFont.setBold( true );
185 aNameItem->setFont( aFont );
187 myTable->setItem( aRow, 0, aNameItem );
190 myTable->setItem( aRow, 1, new QTableWidgetItem( aXg ) );
193 myTable->setItem( aRow, 2, new QTableWidgetItem( anYg ) );
196 myTable->setItem( aRow, 3, new QTableWidgetItem( aXd ) );
199 myTable->setItem( aRow, 4, new QTableWidgetItem( anYd ) );
202 myTable->resizeColumnToContents( 0 );
203 myTable->resizeRowsToContents();
205 myIsModified = false;
207 connect( myTable->model(), SIGNAL( dataChanged ( const QModelIndex&, const QModelIndex& ) ),
208 this, SLOT( onDataChanged() ) );
211 void HYDROGUI_GeoreferencementDlg::getData( ProfilesGeoDataList& theData ) const
218 QString aXg, anYg, aXd, anYd;
219 for ( int aRow = 0; aRow < myTable->rowCount(); aRow++ ) {
220 QString aProfileName = myTable->item( aRow, 0 )->text();
222 aXg = myTable->item( aRow, 1 )->text();
223 anYg = myTable->item( aRow, 2 )->text();
224 aXd = myTable->item( aRow, 3 )->text();
225 anYd = myTable->item( aRow, 4 )->text();
227 theData.append( ProfileGeoData( aProfileName, aXg, anYg, aXd, anYd ) );
231 void HYDROGUI_GeoreferencementDlg::onMousePress(
232 SUIT_ViewWindow* theViewWindow, QMouseEvent* theEvent )
234 // Check the parameters
235 OCCViewer_ViewWindow* anOCCViewWindow =
236 dynamic_cast<OCCViewer_ViewWindow*>(theViewWindow);
237 if ( !anOCCViewWindow || (theEvent->button() != Qt::LeftButton) ) {
241 // Check for current cell
242 int aRow = myTable->currentRow();
243 int aColumn = myTable->currentColumn();
244 if ( aRow < 0 || aColumn <= 0 ) {
248 // Get the selected point coordinates
249 OCCViewer_ViewPort3d* aViewPort = anOCCViewWindow->getViewPort();
250 gp_Pnt aPnt = CurveCreator_Utils::ConvertClickToPoint( theEvent->x(), theEvent->y(),
251 aViewPort->getView() );
253 // Set the coordinates to the corresponding cells of the table
254 int aColumnX = aColumn < 3 ? 1 : 3;
255 int aColumnY = aColumnX + 1;
257 QString aXStr = HYDROGUI_Tool::GetCoordinateString( aPnt.X(), false );
258 QString anYStr = HYDROGUI_Tool::GetCoordinateString( aPnt.Y(), false );
259 myTable->item( aRow, aColumnX )->setText( aXStr );
260 myTable->item( aRow, aColumnY )->setText( anYStr );
263 void HYDROGUI_GeoreferencementDlg::onDataChanged()
268 bool HYDROGUI_GeoreferencementDlg::isModified() const