1 #include "OCCViewer_ClippingDlg.h"
3 #include <QtxDblSpinBox.h>
5 #include "SUIT_Session.h"
6 #include "SUIT_ViewWindow.h"
7 #include "OCCViewer_ViewWindow.h"
8 #include "OCCViewer_ViewPort3d.h"
10 //#include "utilities.h"
12 #include <V3d_View.hxx>
14 #include <V3d_Plane.hxx>
15 #include <Geom_Plane.hxx>
16 #include <Prs3d_Presentation.hxx>
17 #include <AIS_ListIteratorOfListOfInteractive.hxx>
18 #include <AIS_ListOfInteractive.hxx>
19 #include <AIS_InteractiveObject.hxx>
20 #include <AIS_InteractiveContext.hxx>
21 #include <IntAna_IntConicQuad.hxx>
26 #include <qapplication.h>
27 #include <qgroupbox.h>
30 #include <qpushbutton.h>
31 #include <qcombobox.h>
32 #include <qcheckbox.h>
34 //=================================================================================
35 // class : OCCViewer_ClippingDlg()
37 //=================================================================================
38 OCCViewer_ClippingDlg::OCCViewer_ClippingDlg( OCCViewer_ViewWindow* view, QWidget* parent, const char* name, bool modal, WFlags fl )
39 : QDialog( parent, "OCCViewer_ClippingDlg", modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu ), myView( view )
41 setCaption( tr( "Clipping" ) );
43 QVBoxLayout* topLayout = new QVBoxLayout( this );
44 topLayout->setMargin( 11 ); topLayout->setSpacing( 6 );
46 /***************************************************************/
47 GroupPoint = new QGroupBox( this, "GroupPoint" );
48 GroupPoint->setTitle( tr("Base point") );
49 GroupPoint->setColumnLayout(0, Qt::Vertical );
50 GroupPoint->layout()->setSpacing( 0 );
51 GroupPoint->layout()->setMargin( 0 );
52 QGridLayout* GroupPointLayout = new QGridLayout( GroupPoint->layout() );
53 GroupPointLayout->setAlignment( Qt::AlignTop );
54 GroupPointLayout->setSpacing( 6 );
55 GroupPointLayout->setMargin( 11 );
58 const double min = -1e+06;
59 const double max = 1e+06;
60 const double step = 5;
62 TextLabelX = new QLabel( GroupPoint, "TextLabelX" );
63 TextLabelX->setText( tr("X:") );
64 GroupPointLayout->addWidget( TextLabelX, 0, 0 );
66 SpinBox_X = new QtxDblSpinBox( min, max, step, GroupPoint, "SpinBox_X" );
67 GroupPointLayout->addWidget( SpinBox_X, 0, 1 );
69 TextLabelY = new QLabel( GroupPoint, "TextLabelY" );
70 TextLabelY->setText( tr("Y:") );
71 GroupPointLayout->addWidget( TextLabelY, 0, 2 );
73 SpinBox_Y = new QtxDblSpinBox( min, max, step, GroupPoint, "SpinBox_Y" );
74 GroupPointLayout->addWidget( SpinBox_Y, 0, 3 );
76 TextLabelZ = new QLabel( GroupPoint, "TextLabelZ" );
77 TextLabelZ->setText( tr("Z:") );
78 GroupPointLayout->addWidget( TextLabelZ, 0, 4 );
80 SpinBox_Z = new QtxDblSpinBox( min, max, step, GroupPoint, "SpinBox_Z" );
81 GroupPointLayout->addWidget( SpinBox_Z, 0, 5 );
83 resetButton = new QPushButton( GroupPoint, "resetButton" );
84 resetButton->setText( tr( "Reset" ) );
85 GroupPointLayout->addWidget( resetButton, 0, 6 );
87 /***************************************************************/
88 GroupDirection = new QGroupBox( this, "GroupDirection" );
89 GroupDirection->setTitle( tr("Direction") );
90 GroupDirection->setColumnLayout(0, Qt::Vertical );
91 GroupDirection->layout()->setSpacing( 0 );
92 GroupDirection->layout()->setMargin( 0 );
93 QGridLayout* GroupDirectionLayout = new QGridLayout( GroupDirection->layout() );
94 GroupDirectionLayout->setAlignment( Qt::AlignTop );
95 GroupDirectionLayout->setSpacing( 6 );
96 GroupDirectionLayout->setMargin( 11 );
99 TextLabelDx = new QLabel( GroupDirection, "TextLabelDx" );
100 TextLabelDx->setText( tr("Dx:") );
101 GroupDirectionLayout->addWidget( TextLabelDx, 0, 0 );
103 SpinBox_Dx = new QtxDblSpinBox( min, max, step, GroupDirection, "SpinBox_Dx" );
104 GroupDirectionLayout->addWidget( SpinBox_Dx, 0, 1 );
106 TextLabelDy = new QLabel( GroupDirection, "TextLabelDy" );
107 TextLabelDy->setText( tr("Dy:") );
108 GroupDirectionLayout->addWidget( TextLabelDy, 0, 2 );
110 SpinBox_Dy = new QtxDblSpinBox( min, max, step, GroupDirection, "SpinBox_Dy" );
111 GroupDirectionLayout->addWidget( SpinBox_Dy, 0, 3 );
113 TextLabelDz = new QLabel( GroupDirection, "TextLabelDz" );
114 TextLabelDz->setText( tr("Dz:") );
115 GroupDirectionLayout->addWidget( TextLabelDz, 0, 4 );
117 SpinBox_Dz = new QtxDblSpinBox( min, max, step, GroupDirection, "SpinBox_Dz" );
118 GroupDirectionLayout->addWidget( SpinBox_Dz, 0, 5 );
120 invertButton = new QPushButton( GroupDirection, "invertButton" );
121 invertButton->setText( tr( "Invert" ) );
122 GroupDirectionLayout->addWidget( invertButton, 0, 6 );
124 DirectionCB = new QComboBox( GroupDirection, "DirectionCB" );
125 DirectionCB->insertItem(tr("CUSTOM"));
126 DirectionCB->insertItem(tr("||X-Y"));
127 DirectionCB->insertItem(tr("||Y-Z"));
128 DirectionCB->insertItem(tr("||Z-X"));
129 GroupDirectionLayout->addMultiCellWidget( DirectionCB, 1, 1, 0, 5 );
131 /***************************************************************/
133 PreviewChB = new QCheckBox( tr("Preview") ,this, "PreviewChB" );
134 PreviewChB->setChecked( true );
136 /***************************************************************/
137 QGroupBox* GroupButtons = new QGroupBox( this, "GroupButtons" );
138 GroupButtons->setColumnLayout(0, Qt::Vertical );
139 GroupButtons->layout()->setMargin( 0 ); GroupButtons->layout()->setSpacing( 0 );
140 QHBoxLayout* GroupButtonsLayout = new QHBoxLayout( GroupButtons->layout() );
141 GroupButtonsLayout->setAlignment( Qt::AlignTop );
142 GroupButtonsLayout->setMargin( 11 ); GroupButtonsLayout->setSpacing( 6 );
144 buttonApply = new QPushButton( GroupButtons, "buttonApply" );
145 buttonApply->setText( tr( "BUT_APPLY" ) );
146 buttonApply->setAutoDefault( TRUE );
147 buttonApply->setDefault( TRUE );
148 GroupButtonsLayout->addWidget( buttonApply );
150 GroupButtonsLayout->addStretch();
152 buttonClose = new QPushButton( GroupButtons, "buttonClose" );
153 buttonClose->setText( tr( "BUT_CLOSE" ) );
154 buttonClose->setAutoDefault( TRUE );
155 GroupButtonsLayout->addWidget( buttonClose );
156 /***************************************************************/
158 topLayout->addWidget( GroupPoint );
159 topLayout->addWidget( GroupDirection );
161 topLayout->addWidget( PreviewChB );
163 topLayout->addWidget( GroupButtons );
165 /* initializations */
167 SpinBox_X->setValue( 0.0 );
168 SpinBox_Y->setValue( 0.0 );
169 SpinBox_Z->setValue( 0.0 );
171 SpinBox_Dx->setValue( 1.0 );
172 SpinBox_Dy->setValue( 1.0 );
173 SpinBox_Dz->setValue( 1.0 );
175 /* signals and slots connections */
176 connect( resetButton, SIGNAL (clicked() ), this, SLOT( onReset() ) );
177 connect( invertButton, SIGNAL (clicked() ), this, SLOT( onInvert() ) ) ;
179 connect( SpinBox_X, SIGNAL ( valueChanged( double ) ), this, SLOT( onValueChanged() ) );
180 connect( SpinBox_Y, SIGNAL ( valueChanged( double ) ), this, SLOT( onValueChanged() ) );
181 connect( SpinBox_Z, SIGNAL ( valueChanged( double ) ), this, SLOT( onValueChanged() ) );
182 connect( SpinBox_Dx, SIGNAL ( valueChanged( double ) ), this, SLOT( onValueChanged() ) );
183 connect( SpinBox_Dy, SIGNAL ( valueChanged( double ) ), this, SLOT( onValueChanged() ) );
184 connect( SpinBox_Dz, SIGNAL ( valueChanged( double ) ), this, SLOT( onValueChanged() ) );
186 connect( DirectionCB, SIGNAL ( activated ( int ) ), this, SLOT( onModeChanged( int ) ) ) ;
188 connect( PreviewChB, SIGNAL ( toggled ( bool ) ), this, SLOT( onPreview( bool ) ) ) ;
190 connect( buttonClose, SIGNAL( clicked() ), this, SLOT( ClickOnClose() ) ) ;
191 connect( buttonApply, SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
196 //=================================================================================
197 // function : ~ OCCViewer_ClippingDlg()
198 // purpose : Destroys the object and frees any allocated resources
199 //=================================================================================
200 OCCViewer_ClippingDlg::~ OCCViewer_ClippingDlg()
202 // no need to delete child widgets, Qt does it all for us
206 //=================================================================================
207 // function : closeEvent
209 //=================================================================================
210 void OCCViewer_ClippingDlg::closeEvent( QCloseEvent* e )
213 QDialog::closeEvent( e );
217 //=================================================================================
218 // function : showEvent
220 //=================================================================================
221 void OCCViewer_ClippingDlg::showEvent( QShowEvent* e )
223 QDialog::showEvent( e );
224 onPreview( PreviewChB->isChecked() );
228 //=================================================================================
229 // function : hideEvent
231 //=================================================================================
232 void OCCViewer_ClippingDlg::hideEvent( QHideEvent* e )
235 QDialog::hideEvent( e );
239 //=================================================================================
240 // function : ClickOnClose()
242 //=================================================================================
243 void OCCViewer_ClippingDlg::ClickOnClose()
250 //=================================================================================
251 // function : ClickOnApply()
253 //=================================================================================
254 void OCCViewer_ClippingDlg::ClickOnApply()
256 qApp->processEvents();
257 QApplication::setOverrideCursor( Qt::WaitCursor );
258 qApp->processEvents();
260 myView->setCuttingPlane( true, SpinBox_X->value() , SpinBox_Y->value() , SpinBox_Z->value(),
261 SpinBox_Dx->value(), SpinBox_Dy->value(), SpinBox_Dz->value() );
263 QApplication::restoreOverrideCursor();
269 //=================================================================================
270 // function : onReset()
272 //=================================================================================
273 void OCCViewer_ClippingDlg::onReset()
276 SpinBox_X->setValue(0);
277 SpinBox_Y->setValue(0);
278 SpinBox_Z->setValue(0);
281 if ( PreviewChB->isChecked() )
289 //=================================================================================
290 // function : onInvert()
292 //=================================================================================
293 void OCCViewer_ClippingDlg::onInvert()
295 double Dx = SpinBox_Dx->value();
296 double Dy = SpinBox_Dy->value();
297 double Dz = SpinBox_Dz->value();
300 SpinBox_Dx->setValue( -Dx );
301 SpinBox_Dy->setValue( -Dy );
302 SpinBox_Dz->setValue( -Dz );
305 if ( PreviewChB->isChecked() )
313 //=================================================================================
314 // function : onModeChanged()
316 //=================================================================================
317 void OCCViewer_ClippingDlg::onModeChanged( int mode )
319 bool isUserMode = (mode==0);
321 TextLabelX->setEnabled( isUserMode );
322 TextLabelY->setEnabled( isUserMode );
323 TextLabelZ->setEnabled( isUserMode );
325 SpinBox_X->setEnabled( isUserMode );
326 SpinBox_Y->setEnabled( isUserMode );
327 SpinBox_Z->setEnabled( isUserMode );
329 TextLabelDx->setEnabled( isUserMode );
330 TextLabelDy->setEnabled( isUserMode );
331 TextLabelDz->setEnabled( isUserMode );
333 SpinBox_Dx->setEnabled( isUserMode );
334 SpinBox_Dy->setEnabled( isUserMode );
335 SpinBox_Dz->setEnabled( isUserMode );
340 double aDx = 0, aDy = 0, aDz = 0;
345 TextLabelZ->setEnabled( true );
346 SpinBox_Z->setEnabled( true );
347 SpinBox_Z->setFocus();
349 else if ( mode == 2 )
352 TextLabelX->setEnabled( true );
353 SpinBox_X->setEnabled( true );
354 SpinBox_X->setFocus();
356 else if ( mode == 3 )
359 TextLabelY->setEnabled( true );
360 SpinBox_Y->setEnabled( true );
361 SpinBox_Y->setFocus();
365 SpinBox_Dx->setValue( aDx );
366 SpinBox_Dy->setValue( aDy );
367 SpinBox_Dz->setValue( aDz );
370 if ( PreviewChB->isChecked() )
378 //================================================================
379 // Function : displayPreview
381 //================================================================
382 void OCCViewer_ClippingDlg::displayPreview()
384 if ( myBusy || !isValid() )
387 OCCViewer_Viewer* anOCCViewer = (OCCViewer_Viewer*)myView->getViewManager()->getViewModel();
391 Handle(AIS_InteractiveContext) ic = anOCCViewer->getAISContext();
393 double aXMin, aYMin, aZMin, aXMax, aYMax, aZMax;
394 aXMin = aYMin = aZMin = DBL_MAX;
395 aXMax = aYMax = aZMax = -DBL_MAX;
397 bool isFound = false;
398 AIS_ListOfInteractive aList;
399 ic->DisplayedObjects( aList );
400 for ( AIS_ListIteratorOfListOfInteractive it( aList ); it.More(); it.Next() )
402 Handle(AIS_InteractiveObject) anObj = it.Value();
403 if ( !anObj.IsNull() && anObj->HasPresentation() &&
404 !anObj->IsKind( STANDARD_TYPE(AIS_Plane) ) )
406 Handle(Prs3d_Presentation) aPrs = anObj->Presentation();
407 if ( !aPrs->IsEmpty() && !aPrs->IsInfinite() )
410 double xmin, ymin, zmin, xmax, ymax, zmax;
411 aPrs->MinMaxValues( xmin, ymin, zmin, xmax, ymax, zmax );
412 aXMin = QMIN( aXMin, xmin ); aXMax = QMAX( aXMax, xmax );
413 aYMin = QMIN( aYMin, ymin ); aYMax = QMAX( aYMax, ymax );
414 aZMin = QMIN( aZMin, zmin ); aZMax = QMAX( aZMax, zmax );
421 gp_Pnt aBasePnt( SpinBox_X->value(), SpinBox_Y->value(), SpinBox_Z->value() );
422 gp_Dir aNormal( SpinBox_Dx->value(), SpinBox_Dy->value(), SpinBox_Dz->value() );
423 gp_Pnt aCenter = aBasePnt;
427 // compute clipping plane size
428 aCenter = gp_Pnt( ( aXMin + aXMax ) / 2, ( aYMin + aYMax ) / 2, ( aZMin + aZMax ) / 2 );
429 double aDiag = aCenter.Distance(gp_Pnt(aXMax, aYMax, aZMax ))*2;
432 // compute clipping plane center ( redefine the base point )
433 IntAna_IntConicQuad intersector = IntAna_IntConicQuad();
435 intersector.Perform( gp_Lin( aCenter, aNormal), gp_Pln( aBasePnt, aNormal), Precision::Confusion() );
436 if ( intersector.IsDone() && intersector.NbPoints() == 1 )
437 aBasePnt = intersector.Point( 1 );
440 myPreviewPlane = new AIS_Plane( new Geom_Plane( aBasePnt, aNormal ) );
441 myPreviewPlane->SetSize( aSize, aSize );
443 ic->Display( myPreviewPlane, 1, -1, false );
444 ic->SetWidth( myPreviewPlane, 10, false );
445 ic->SetMaterial( myPreviewPlane, Graphic3d_NOM_PLASTIC, false );
446 ic->SetTransparency( myPreviewPlane, 0.5, false );
447 ic->SetColor( myPreviewPlane, Quantity_Color( 85 / 255., 85 / 255., 255 / 255., Quantity_TOC_RGB ), false );
449 anOCCViewer->update();
453 //================================================================
454 // Function : erasePreview
456 //================================================================
457 void OCCViewer_ClippingDlg::erasePreview ()
459 OCCViewer_Viewer* anOCCViewer = (OCCViewer_Viewer*)myView->getViewManager()->getViewModel();
463 Handle(AIS_InteractiveContext) ic = anOCCViewer->getAISContext();
465 if ( !myPreviewPlane.IsNull() && ic->IsDisplayed( myPreviewPlane ) )
467 ic->Erase( myPreviewPlane, false, false );
468 ic->Remove( myPreviewPlane, false );
469 myPreviewPlane.Nullify();
472 anOCCViewer->update();
476 //================================================================
477 // Function : onValueChanged
479 //================================================================
480 void OCCViewer_ClippingDlg::onValueChanged()
482 if ( PreviewChB->isChecked() )
490 //================================================================
491 // Function : onPreview
493 //================================================================
494 void OCCViewer_ClippingDlg::onPreview( bool on )
502 //================================================================
503 // Function : onPreview
505 //================================================================
506 bool OCCViewer_ClippingDlg::isValid()
508 return ( SpinBox_Dx->value()!=0 || SpinBox_Dy->value()!=0 || SpinBox_Dz->value()!=0 );