Salome HOME
Fix compilation error (conflict of OK name between OCCT Plate_Plate.hxx and GEOM...
[modules/geom.git] / MeasureGUI / MeasureGUI_CreateDimensionDlg.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 // GEOM GEOMGUI : GUI for Geometry component
24 // File   : MeasureGUI_CreateDimensionDlg.cxx
25 // Author : Anton POLETAEV, Open CASCADE S.A.S.
26
27 #include "MeasureGUI_CreateDimensionDlg.h"
28 #include "MeasureGUI_DimensionCreateTool.h"
29
30 #include <GEOM_Constants.h>
31 #include <GeometryGUI.h>
32 #include <GEOMBase.h>
33 #include <DlgRef.h>
34 #include <GEOMUtils.hxx>
35 #include <GEOMGUI_DimensionProperty.h>
36
37 #include <OCCViewer_ViewManager.h>
38 #include <OCCViewer_ViewWindow.h>
39 #include <OCCViewer_ViewPort3d.h>
40 #include <LightApp_SelectionMgr.h>
41 #include <SalomeApp_Application.h>
42 #include <SalomeApp_DataObject.h>
43 #include <SalomeApp_Study.h>
44 #include <SOCC_ViewModel.h>
45 #include <SOCC_Prs.h>
46 #include <SUIT_ViewWindow.h>
47 #include <SUIT_ViewManager.h>
48 #include <SUIT_MessageBox.h>
49 #include <SUIT_ResourceMgr.h>
50 #include <SUIT_Session.h>
51 #include <SUIT_Desktop.h>
52
53 #include <Aspect_PolygonOffsetMode.hxx>
54
55 //=================================================================================
56 // function : Constructor
57 // purpose  :
58 //=================================================================================
59 MeasureGUI_CreateDimensionDlg::MeasureGUI_CreateDimensionDlg( const GEOM::GeomObjPtr& theObj,
60                                                               GeometryGUI* theGUI,
61                                                               QWidget* theParent )
62 : GEOMBase_Skeleton( theGUI, theParent ),
63   myParentObj( theObj ),
64   myEditingViewer( NULL )
65 {
66   setWindowTitle( tr( "CREATE_DIMENSION_TITLE" ) );
67
68   // init "dimension type" radio buttons
69   QAbstractButton* aTypeButtonLength   = mainFrame()->RadioButton1;
70   QAbstractButton* aTypeButtonDiameter = mainFrame()->RadioButton2;
71   QAbstractButton* aTypeButtonAngle    = mainFrame()->RadioButton3;
72
73   mainFrame()->GroupConstructors->setTitle( tr( "DIMENSIONS" ) );
74   aTypeButtonDiameter->setText( tr( "DIAMETER" ) );
75   aTypeButtonLength->setText( tr( "LENGTH" ) );
76   aTypeButtonAngle->setText( tr( "ANGLE" ) );
77
78   // construct "arguments" pane
79   QGroupBox*   aGroupArgs       = new QGroupBox( tr( "ARGUMENTS" ), centralWidget() );
80   QVBoxLayout* aGroupArgsLayout = new QVBoxLayout( aGroupArgs );
81
82   myLengthArgs   = new LengthPane  ( centralWidget() );
83   myDiameterArgs = new DiameterPane( centralWidget() );
84   myAngleArgs    = new AnglePane   ( centralWidget() );
85
86   // connect selector pane signals
87   connect( myLengthArgs,   SIGNAL( StartSelection( const QList<TopAbs_ShapeEnum>& ) ),
88                            SLOT( OnStartSelection( const QList<TopAbs_ShapeEnum>& ) ) );
89   connect( myDiameterArgs, SIGNAL( StartSelection( const QList<TopAbs_ShapeEnum>& ) ),
90                            SLOT( OnStartSelection( const QList<TopAbs_ShapeEnum>& ) ) );
91   connect( myAngleArgs,    SIGNAL( StartSelection( const QList<TopAbs_ShapeEnum>& ) ), 
92                            SLOT( OnStartSelection( const QList<TopAbs_ShapeEnum>& ) ) );
93
94   connect( myLengthArgs,   SIGNAL( StopSelection() ), SLOT( OnStopSelection() ) );
95   connect( myDiameterArgs, SIGNAL( StopSelection() ), SLOT( OnStopSelection() ) );
96   connect( myAngleArgs,    SIGNAL( StopSelection() ), SLOT( OnStopSelection( ) ) );
97
98   connect( myLengthArgs,   SIGNAL( SelectionDone() ), SLOT( OnSelectionDone() ) );
99   connect( myDiameterArgs, SIGNAL( SelectionDone() ), SLOT( OnSelectionDone() ) );
100   connect( myAngleArgs,    SIGNAL( SelectionDone() ), SLOT( OnSelectionDone() ) );
101
102   aGroupArgsLayout->addWidget( myLengthArgs );
103   aGroupArgsLayout->addWidget( myDiameterArgs );
104   aGroupArgsLayout->addWidget( myAngleArgs );
105
106   // construct main layout
107   QVBoxLayout* aCustomWidLayout = new QVBoxLayout( centralWidget() );
108   aCustomWidLayout->setMargin( 0 ); 
109   aCustomWidLayout->setSpacing( 6 );
110   aCustomWidLayout->addWidget( aGroupArgs );
111
112   // add visibility rules for structural widgets
113   connect( aTypeButtonLength,   SIGNAL( toggled(bool) ), myLengthArgs,   SLOT( setVisible(bool) ) );
114   connect( aTypeButtonDiameter, SIGNAL( toggled(bool) ), myDiameterArgs, SLOT( setVisible(bool) ) );
115   connect( aTypeButtonAngle,    SIGNAL( toggled(bool) ), myAngleArgs,    SLOT( setVisible(bool) ) );
116
117   // signals and slots connections
118   connect( buttonOk(),    SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
119   connect( buttonApply(), SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
120
121   // map functional signals
122   myRBGroup->setId( aTypeButtonLength,   TypeButtonID_Length );
123   myRBGroup->setId( aTypeButtonDiameter, TypeButtonID_Diameter );
124   myRBGroup->setId( aTypeButtonAngle,    TypeButtonID_Angle );
125
126   connect( this, SIGNAL( constructorsClicked( int ) ), SLOT( ConstructTypeChanged( int ) ) );
127   connect( myLengthArgs,   SIGNAL( TabChanged() ), SLOT( OnArgumentTabChanged() ) );
128   connect( myDiameterArgs, SIGNAL( TabChanged() ), SLOT( OnArgumentTabChanged() ) );
129   connect( myAngleArgs,    SIGNAL( TabChanged() ), SLOT( OnArgumentTabChanged() ) );
130
131   myDimensionInteractor = new MeasureGUI_DimensionInteractor( theGUI, theParent );
132
133   myDiameterArgs->setVisible( false );
134   myAngleArgs   ->setVisible( false );
135   myLengthArgs  ->setVisible( true );
136
137   myRBGroup->button( TypeButtonID_Length )->click();
138
139   Init();
140
141   setHelpFileName("add_dimension_page.html");
142 }
143
144 //=================================================================================
145 // function : Destructor
146 // purpose  :
147 //=================================================================================
148 MeasureGUI_CreateDimensionDlg::~MeasureGUI_CreateDimensionDlg()
149 {
150 }
151
152 //=================================================================================
153 // function : ActiveArgs
154 // purpose  : 
155 //=================================================================================
156 MeasureGUI_CreateDimensionDlg::BaseSelectorPane* MeasureGUI_CreateDimensionDlg::ActiveArgs()
157 {
158   switch ( getConstructorId() )
159   {
160     case TypeButtonID_Length   : return myLengthArgs;
161     case TypeButtonID_Diameter : return myDiameterArgs;
162     case TypeButtonID_Angle    : return myAngleArgs;
163     default:
164       return NULL;
165   }
166 }
167
168 //=================================================================================
169 // function : GenerateName
170 // purpose  : 
171 //=================================================================================
172 QString MeasureGUI_CreateDimensionDlg::GenerateName( const QString& thePrefix )
173 {
174   QRegExp anExtractIds( "^" + thePrefix + "_([0-9]+)$" );
175
176   QSet<int> anOccupiedIds;
177
178   GEOMGUI_DimensionProperty aProp =
179     getStudy()->getObjectProperty( GEOM::sharedPropertiesId(),
180                                    myParentObj->GetStudyEntry(),
181                                    GEOM::propertyName( GEOM::Dimensions ),
182                                    QVariant() )
183                                    .value<GEOMGUI_DimensionProperty>();
184
185   // get names and convert to QStrings to perform index matching
186   for( int anIt = 0; anIt < aProp.GetNumber(); ++anIt )
187   {
188     if ( anExtractIds.indexIn( aProp.GetName( anIt ) ) == -1 )
189     {
190       continue;
191     }
192
193     anOccupiedIds.insert( anExtractIds.cap( 1 ).toInt() );
194   }
195
196   int aSearchId = 1;
197   while ( true )
198   {
199     if ( !anOccupiedIds.contains( aSearchId ) )
200     {
201       break;
202     }
203
204     aSearchId++;
205   }
206
207   return QString( thePrefix + "_%1" ).arg( aSearchId );
208 }
209
210 //=================================================================================
211 // function : ConstructTypeChanged
212 // purpose  : 
213 //=================================================================================
214 void MeasureGUI_CreateDimensionDlg::ConstructTypeChanged( int theType )
215 {
216   ActiveArgs()->Reset();
217
218   updateGeometry();
219
220   QString aName;
221   switch ( theType )
222   {
223     case TypeButtonID_Length   : aName = GenerateName( tr("NAME_LENGTH") );   break;
224     case TypeButtonID_Diameter : aName = GenerateName( tr("NAME_DIAMETER") ); break;
225     case TypeButtonID_Angle    : aName = GenerateName( tr("NAME_ANGLE") );    break;
226   }
227
228   myMainFrame->ResultName->setText( aName );
229
230   StopLocalEditing();
231   erasePreview();
232
233   updateGeometry();
234   resize( minimumSizeHint() );
235 }
236
237 //=================================================================================
238 // function : OnArgumentTabChanged
239 // purpose  : 
240 //=================================================================================
241 void MeasureGUI_CreateDimensionDlg::OnArgumentTabChanged()
242 {
243   StopLocalEditing();
244   erasePreview();
245 }
246
247 //=================================================================================
248 // function : OnStartSelection
249 // purpose  :
250 //=================================================================================
251 void MeasureGUI_CreateDimensionDlg::OnStartSelection( const QList<TopAbs_ShapeEnum>& theModes )
252 {
253   OnStopSelection();
254
255   StopLocalEditing();
256
257   QList<TopAbs_ShapeEnum>::const_iterator aModeIt = theModes.constBegin();
258   for ( ; aModeIt != theModes.constEnd(); ++aModeIt )
259   {
260     localSelection( myParentObj.get(), *aModeIt );
261   }
262
263   mySelectionModes = theModes;
264
265   connect( myGeomGUI->getApp()->selectionMgr(),
266            SIGNAL( currentSelectionChanged() ),
267            SLOT( SelectionIntoArgument() ) );
268 }
269
270 //=================================================================================
271 // function : OnStopSelection
272 // purpose  :
273 //=================================================================================
274 void MeasureGUI_CreateDimensionDlg::OnStopSelection()
275 {
276   globalSelection();
277
278   disconnect( myGeomGUI->getApp()->selectionMgr(),
279               SIGNAL( currentSelectionChanged() ),
280               this,
281               SLOT( SelectionIntoArgument() ) );
282 }
283
284 //=================================================================================
285 // function : SelectionIntoArgument
286 // purpose  : Handle model selection by active selector pane
287 //=================================================================================
288 void MeasureGUI_CreateDimensionDlg::SelectionIntoArgument()
289 {
290   LightApp_SelectionMgr* aSelectionMgr = myGeomGUI->getApp()->selectionMgr();
291   SALOME_ListIO aSelection;
292   aSelectionMgr->selectedObjects( aSelection );
293
294   if ( aSelection.Extent() != 1 )
295   {
296     ActiveArgs()->SelectionIntoArguments( GEOM::GeomObjPtr() );
297     return;
298   }
299
300   StopLocalEditing();
301
302   erasePreview();
303
304   ActiveArgs()->SelectionIntoArguments( getSelected( mySelectionModes ) );
305 }
306
307 //=================================================================================
308 // function : OnSelectionDone
309 // purpose  :
310 //=================================================================================
311 void MeasureGUI_CreateDimensionDlg::OnSelectionDone()
312 {
313   // create new dimension presentation
314   myDimension = CreateDimension();
315
316   if ( myDimension.IsNull() )
317   {
318     // can not create preview, repeat selection
319     SUIT_MessageBox::warning( this,
320                               tr( "WARNING_TITLE_CANNOT_CREATE_DIMENSION" ),
321                               tr( "WARNING_MSG_INVALID_ARGUMENTS" ) );
322
323     ActiveArgs()->Reset();
324
325     return;
326   }
327
328   // show preview of dimension object
329   SUIT_ViewWindow* aViewWindow = SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
330
331   SOCC_Prs* aSalomePrs = dynamic_cast<SOCC_Prs*>( ( (SOCC_Viewer*)( aViewWindow->getViewManager()->getViewModel() ) )->CreatePrs( 0 ) );
332
333   aSalomePrs->AddObject( myDimension );
334
335   displayPreview( aSalomePrs );
336
337   StartLocalEditing();
338 }
339
340 //=================================================================================
341 // function : ClickOnOk
342 // purpose  : 
343 //=================================================================================
344 void MeasureGUI_CreateDimensionDlg::ClickOnOk()
345 {
346   setIsApplyAndClose( true );
347   if ( ClickOnApply() )
348   {
349     ClickOnCancel();
350   }
351 }
352
353 //=================================================================================
354 // function : ClickOnApply
355 // purpose  : 
356 //=================================================================================
357 bool MeasureGUI_CreateDimensionDlg::ClickOnApply()
358 {
359   StopLocalEditing();
360
361   if ( myDimension.IsNull() )
362   {
363     return true;
364   }
365
366   if ( !AddDimensionToOwner() )
367   {
368     return false;
369   }
370
371   redisplay( myParentObj.get() );
372
373   if ( !isApplyAndClose() )
374   {
375     Init();
376   }
377
378   return true;
379 }
380
381 //=================================================================================
382 // function : StartLocalEditing
383 // purpose  : 
384 //=================================================================================
385 void MeasureGUI_CreateDimensionDlg::StartLocalEditing()
386 {
387   StopLocalEditing();
388
389   myDimensionInteractor->Enable();
390
391   if ( myDimension.IsNull() )
392   {
393     return;
394   }
395
396   SalomeApp_Application* anApp = myGeomGUI->getApp();
397
398   SUIT_ViewManager* aViewMgr = anApp->activeViewManager();
399
400   if ( aViewMgr->getType() != OCCViewer_Viewer::Type() )
401   {
402     return;
403   }
404
405   SOCC_Viewer* anOCCViewer = dynamic_cast<SOCC_Viewer*>( aViewMgr->getViewModel() );
406
407   myEditingViewer = anOCCViewer;
408   if ( !myEditingViewer )
409   {
410     return;
411   }
412
413   Handle(AIS_InteractiveContext) anAISContext = myEditingViewer->getAISContext();
414   Handle(V3d_Viewer)             aViewer3d    = myEditingViewer->getViewer3d();
415
416   aViewer3d->AddZLayer( myEditingLayer );
417
418   anAISContext->OpenLocalContext( Standard_False, Standard_False );
419   anAISContext->Load( myDimension, AIS_DSM_All );
420   anAISContext->SetZLayer( myDimension, myEditingLayer );
421   anAISContext->Activate( myDimension, AIS_DSM_Line );
422   anAISContext->Activate( myDimension, AIS_DSM_Text );
423   anAISContext->Redisplay( myDimension );
424 }
425
426 //=================================================================================
427 // function : StopLocalEditing
428 // purpose  : 
429 //=================================================================================
430 void MeasureGUI_CreateDimensionDlg::StopLocalEditing()
431 {
432   myDimensionInteractor->Disable();
433
434   if ( !myEditingViewer )
435   {
436     return;
437   }
438
439   Handle(AIS_InteractiveContext) anAISContext = myEditingViewer->getAISContext();
440   Handle(V3d_Viewer)             aViewer3d    = myEditingViewer->getViewer3d();
441
442   aViewer3d->RemoveZLayer( myEditingLayer );
443   anAISContext->CloseLocalContext();
444
445   myEditingViewer = NULL;
446 }
447
448 //=================================================================================
449 // function : Init
450 // purpose  : 
451 //=================================================================================
452 void MeasureGUI_CreateDimensionDlg::Init()
453 {
454   myDimension = NULL;
455
456   StopLocalEditing();
457
458   erasePreview();
459
460   myDiameterArgs->Reset();
461   myLengthArgs->Reset();
462   myAngleArgs->Reset();
463 }
464
465 //=================================================================================
466 // function : CreateDimensionPrs
467 // purpose  : 
468 //=================================================================================
469 Handle(AIS_Dimension) MeasureGUI_CreateDimensionDlg::CreateDimension()
470 {
471   Handle(AIS_Dimension) aDimensionIO;
472
473   // prepare dimension styling
474   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
475
476   QColor  aQColor       = aResMgr->colorValue  ( "Geometry", "dimensions_color", QColor( 0, 255, 0 ) );
477   int     aLineWidth    = aResMgr->integerValue( "Geometry", "dimensions_line_width", 1 );
478   double  aFontHeight   = aResMgr->doubleValue ( "Geometry", "dimensions_font_height", 10 );
479   double  anArrowLength = aResMgr->doubleValue ( "Geometry", "dimensions_arrow_length", 5 );
480   double  aDefFlyout    = aResMgr->doubleValue ( "Geometry", "dimensions_default_flyout", 20 );
481   bool    isUnitsShown  = aResMgr->booleanValue( "Geometry", "dimensions_show_units", false );
482   QString aUnitsLength  = aResMgr->stringValue ( "Geometry", "dimensions_length_units", "m" );
483   QString aUnitsAngle   = aResMgr->stringValue ( "Geometry", "dimensions_angle_units", "deg" );
484
485   OCCViewer_ViewWindow* anActiveView = NULL;
486
487   SalomeApp_Application* anApp = myGeomGUI->getApp();
488
489   if ( anApp )
490   {
491     OCCViewer_ViewManager* aViewMgr = (OCCViewer_ViewManager*) anApp->getViewManager( OCCViewer_Viewer::Type(), false );
492     if ( aViewMgr )
493     {
494       anActiveView = (OCCViewer_ViewWindow*) aViewMgr->getActiveView();
495     }
496   }
497
498   MeasureGUI_DimensionCreateTool aTool;
499
500   aTool.Settings.DefaultFlyout = aDefFlyout;
501   aTool.Settings.ActiveView    = anActiveView ? anActiveView->getViewPort()->getView() : NULL;
502
503   switch ( getConstructorId() )
504   {
505     // create length dimension
506     case TypeButtonID_Length :
507     {
508       LengthPane* aLengthPane = qobject_cast<LengthPane*>( ActiveArgs() );
509
510       switch (aLengthPane->ActiveTab())
511       {
512         case LengthPane::TabID_SingleEdge:
513         {
514           const GEOM::GeomObjPtr& anEdge = aLengthPane->GetSingleEdge();
515
516           aDimensionIO = aTool.LengthOnEdge( anEdge );
517           break;
518         }
519
520         case LengthPane::TabID_TwoPoints:
521         {
522           const GEOM::GeomObjPtr& aPoint1 = aLengthPane->GetPoint1();
523           const GEOM::GeomObjPtr& aPoint2 = aLengthPane->GetPoint2();
524
525           aDimensionIO = aTool.LengthByPoints( aPoint1, aPoint2 );
526           break;
527         }
528
529         case LengthPane::TabID_ParallelEdges:
530         {
531           const GEOM::GeomObjPtr& anEdge1 = aLengthPane->GetEdge1();
532           const GEOM::GeomObjPtr& anEdge2 = aLengthPane->GetEdge2();
533
534           aDimensionIO = aTool.LengthByParallelEdges( anEdge1, anEdge2 );
535           break;
536         }
537       }
538     }
539     break;
540
541     // create diameter dimension
542     case TypeButtonID_Diameter :
543     {
544       DiameterPane* aDiameterPane = qobject_cast<DiameterPane*>( ActiveArgs() );
545
546       const GEOM::GeomObjPtr& aShape = aDiameterPane->GetShape();
547
548       aDimensionIO = aTool.Diameter( aShape );
549       break;
550     }
551
552     // create angle dimension
553     case TypeButtonID_Angle :
554     {
555       AnglePane* anAnglePane = qobject_cast<AnglePane*>( ActiveArgs() );
556
557       switch (ActiveArgs()->ActiveTab())
558       {
559         case AnglePane::TabID_TwoEdges:
560         {
561           const GEOM::GeomObjPtr& anEdge1 = anAnglePane->GetEdge1();
562           const GEOM::GeomObjPtr& anEdge2 = anAnglePane->GetEdge2();
563
564           aDimensionIO = aTool.AngleByTwoEdges( anEdge1, anEdge2 );
565           break;
566         }
567
568         case AnglePane::TabID_ThreePoints:
569         {
570           const GEOM::GeomObjPtr& aPoint1 = anAnglePane->GetPoint1();
571           const GEOM::GeomObjPtr& aPoint2 = anAnglePane->GetPoint2();
572           const GEOM::GeomObjPtr& aPoint3 = anAnglePane->GetPoint3();
573
574           aDimensionIO = aTool.AngleByThreePoints( aPoint1, aPoint2, aPoint3 );
575           break;
576         }
577       }
578       break;
579     }
580   }
581
582   if ( aDimensionIO.IsNull() )
583   {
584     return NULL;
585   }
586
587   Quantity_Color aColor( aQColor.redF(), aQColor.greenF(), aQColor.blueF(), Quantity_TOC_RGB );
588
589   Handle(Prs3d_DimensionAspect) aStyle = new Prs3d_DimensionAspect();
590
591   aStyle->SetCommonColor( aColor );
592   aStyle->MakeUnitsDisplayed( (Standard_Boolean) isUnitsShown );
593   aStyle->MakeText3d( Standard_True );
594   aStyle->MakeTextShaded( Standard_True );
595   aStyle->SetExtensionSize( aFontHeight * 0.5 );
596   aStyle->TextAspect()->SetHeight( aFontHeight );
597   aStyle->ArrowAspect()->SetLength( anArrowLength );
598   aStyle->LineAspect()->SetWidth( aLineWidth );
599
600   if ( aDimensionIO->IsKind( STANDARD_TYPE(AIS_AngleDimension) ) )
601   {
602     // show degree symbol for dimension instead of label "deg"
603     if ( aUnitsAngle == "deg" )
604     {
605       aDimensionIO->SetSpecialSymbol(0xB0);
606       aDimensionIO->SetDisplaySpecialSymbol( isUnitsShown ? AIS_DSS_After : AIS_DSS_No );
607       aStyle->MakeUnitsDisplayed(Standard_False);
608     }
609     else
610     {
611       aDimensionIO->SetDisplaySpecialSymbol(AIS_DSS_No);
612       aStyle->MakeUnitsDisplayed( (Standard_Boolean) isUnitsShown );
613     }
614   }
615   else
616   {
617     aStyle->MakeUnitsDisplayed( (Standard_Boolean) isUnitsShown );
618   }
619
620   aDimensionIO->Attributes()->SetDimLengthDisplayUnits( aUnitsLength.toLatin1().data() );
621   aDimensionIO->Attributes()->SetDimAngleDisplayUnits( aUnitsAngle.toLatin1().data() );
622   aDimensionIO->SetDimensionAspect( aStyle );
623   aDimensionIO->SetPolygonOffsets( Aspect_POM_Fill, -1.0, -1.0 );
624
625   return aDimensionIO;
626 }
627
628 //=================================================================================
629 // class    : AddDimensionToOwner
630 // purpose  : 
631 //=================================================================================
632 bool MeasureGUI_CreateDimensionDlg::AddDimensionToOwner()
633 {
634   if ( myDimension.IsNull() )
635   {
636     return false;
637   }
638
639   gp_Ax3 aLCS;
640   TopoDS_Shape anParentSh;
641   if ( GEOMBase::GetShape( myParentObj.get(), anParentSh ) )
642   {
643     aLCS = gp_Ax3().Transformed( anParentSh.Location().Transformation() );
644   }
645
646   QString aName = getNewObjectName();
647
648   SalomeApp_Study* aStudy = getStudy();
649
650   GEOMGUI_DimensionProperty aProp =
651     aStudy->getObjectProperty( GEOM::sharedPropertiesId(),
652                                myParentObj->GetStudyEntry(),
653                                GEOM::propertyName( GEOM::Dimensions ),
654                                QVariant() )
655                                .value<GEOMGUI_DimensionProperty>();
656
657   // append new dimension record to data
658   aProp.AddRecord( myDimension, aLCS );
659   aProp.SetName( aProp.GetNumber() - 1, aName );
660   aProp.SetVisible( aProp.GetNumber() - 1, true );
661
662   // store modified property data
663   aStudy->setObjectProperty( GEOM::sharedPropertiesId(),
664                              myParentObj->GetStudyEntry(),
665                              GEOM::propertyName( GEOM::Dimensions ),
666                              aProp );
667
668   return true;
669 }
670
671 /* ------------------------------------------------------------------------------*
672  *                                                                               *
673  *                    Argument shape selector layout                             *
674  *                                                                               *
675  * ------------------------------------------------------------------------------*/
676
677 //=================================================================================
678 // class    : MeasureGUI_CreateDimensionDlg::LengthPane
679 // purpose  : Constructor
680 //=================================================================================
681 MeasureGUI_CreateDimensionDlg::LengthPane::LengthPane( QWidget* theParent )
682 : BaseSelectorPane( theParent )
683 {
684   myTabs                   = new QTabWidget( this );
685   mySingleEdgeSelectors    = new MeasureGUI_1Sel_Frame( this );
686   myTwoPointsSelectors     = new MeasureGUI_2Sel_Frame( this );
687   myParallelEdgesSelectors = new MeasureGUI_2Sel_Frame( this );
688
689   myTabs->addTab( mySingleEdgeSelectors,    MeasureGUI_CreateDimensionDlg::tr( "EDGE_LENGTH" ) );
690   myTabs->addTab( myTwoPointsSelectors,     MeasureGUI_CreateDimensionDlg::tr( "TWO_POINTS" ) );
691   myTabs->addTab( myParallelEdgesSelectors, MeasureGUI_CreateDimensionDlg::tr( "PARALLEL_EDGES" ) );
692
693   mySingleEdgeSelectors->PushButton1->setIcon( mySelectorIcon );
694   mySingleEdgeSelectors->TextLabel1->setText( MeasureGUI_CreateDimensionDlg::tr( "EDGE" ) );
695   mySingleEdgeSelectors->LineEdit1->setReadOnly( true );
696
697   myTwoPointsSelectors->PushButton1->setIcon( mySelectorIcon );
698   myTwoPointsSelectors->PushButton2->setIcon( mySelectorIcon );
699   myTwoPointsSelectors->TextLabel1->setText( MeasureGUI_CreateDimensionDlg::tr( "POINT_1" ) );
700   myTwoPointsSelectors->TextLabel2->setText( MeasureGUI_CreateDimensionDlg::tr( "POINT_2" ) );
701   myTwoPointsSelectors->LineEdit1->setReadOnly( true );
702   myTwoPointsSelectors->LineEdit2->setReadOnly( true );
703
704   myParallelEdgesSelectors->PushButton1->setIcon( mySelectorIcon );
705   myParallelEdgesSelectors->PushButton2->setIcon( mySelectorIcon );
706   myParallelEdgesSelectors->TextLabel1 ->setText( MeasureGUI_CreateDimensionDlg::tr( "EDGE_1" ) );
707   myParallelEdgesSelectors->TextLabel2 ->setText( MeasureGUI_CreateDimensionDlg::tr( "EDGE_2" ) );
708   myParallelEdgesSelectors->LineEdit1->setReadOnly( true );
709   myParallelEdgesSelectors->LineEdit2->setReadOnly( true );
710
711   QVBoxLayout* aLayout = new QVBoxLayout( this );
712
713   aLayout->setMargin( 0 );
714   aLayout->addWidget( myTabs );
715
716   SelectionModes anEdgeModes;
717   SelectionModes aPointModes;
718   anEdgeModes << TopAbs_EDGE;
719   aPointModes << TopAbs_VERTEX;
720
721   SetTabWidget( myTabs );
722   RegisterSelector( mySingleEdgeSelectors->LineEdit1,    mySingleEdgeSelectors->PushButton1,    anEdgeModes, TabID_SingleEdge );
723   RegisterSelector( myTwoPointsSelectors->LineEdit1,     myTwoPointsSelectors->PushButton1,     aPointModes, TabID_TwoPoints );
724   RegisterSelector( myTwoPointsSelectors->LineEdit2,     myTwoPointsSelectors->PushButton2,     aPointModes, TabID_TwoPoints );
725   RegisterSelector( myParallelEdgesSelectors->LineEdit1, myParallelEdgesSelectors->PushButton1, anEdgeModes, TabID_ParallelEdges );
726   RegisterSelector( myParallelEdgesSelectors->LineEdit2, myParallelEdgesSelectors->PushButton2, anEdgeModes, TabID_ParallelEdges );
727 }
728
729 //=================================================================================
730 // class    : MeasureGUI_CreateDimensionDlg::DiameterPane
731 // purpose  : Constructor
732 //=================================================================================
733 MeasureGUI_CreateDimensionDlg::DiameterPane::DiameterPane( QWidget* theParent )
734 : BaseSelectorPane( theParent )
735 {
736   myShapeSelector = new MeasureGUI_1Sel_Frame( this );
737   myShapeSelector->PushButton1->setIcon( mySelectorIcon );
738   myShapeSelector->TextLabel1 ->setText( MeasureGUI_CreateDimensionDlg::tr( "OBJECT" ) );
739
740   QVBoxLayout* aLayout = new QVBoxLayout( this );
741
742   aLayout->setMargin( 0 );
743   aLayout->addWidget( myShapeSelector );
744
745   SelectionModes aDiamModes;
746   aDiamModes << TopAbs_SHAPE
747              << TopAbs_FACE
748              << TopAbs_EDGE;
749
750   RegisterSelector( myShapeSelector->LineEdit1, myShapeSelector->PushButton1, aDiamModes );
751 }
752
753 //=================================================================================
754 // class    : MeasureGUI_CreateDimensionDlg::AnglePane
755 // purpose  : Constructor
756 //=================================================================================
757 MeasureGUI_CreateDimensionDlg::AnglePane::AnglePane( QWidget* theParent )
758 : BaseSelectorPane( theParent )
759 {
760   myTabs                 = new QTabWidget( this );
761   myTwoEdgesSelectors    = new MeasureGUI_2Sel_Frame( this );
762   myThreePointsSelectors = new MeasureGUI_3Sel_Frame( this );
763
764   myTabs->addTab( myTwoEdgesSelectors,    MeasureGUI_CreateDimensionDlg::tr( "TWO_EDGES" ) );
765   myTabs->addTab( myThreePointsSelectors, MeasureGUI_CreateDimensionDlg::tr( "THREE_POINTS" ) );
766
767   myTwoEdgesSelectors->PushButton1->setIcon( mySelectorIcon );
768   myTwoEdgesSelectors->PushButton2->setIcon( mySelectorIcon );
769   myTwoEdgesSelectors->TextLabel1 ->setText( MeasureGUI_CreateDimensionDlg::tr( "EDGE_1" ) );
770   myTwoEdgesSelectors->TextLabel2 ->setText( MeasureGUI_CreateDimensionDlg::tr( "EDGE_2" ) );
771
772   myThreePointsSelectors->PushButton1->setIcon( mySelectorIcon );
773   myThreePointsSelectors->PushButton2->setIcon( mySelectorIcon );
774   myThreePointsSelectors->PushButton3->setIcon( mySelectorIcon );
775   myThreePointsSelectors->TextLabel1 ->setText( MeasureGUI_CreateDimensionDlg::tr( "POINT_1" ) );
776   myThreePointsSelectors->TextLabel2 ->setText( MeasureGUI_CreateDimensionDlg::tr( "POINT_2" ) );
777   myThreePointsSelectors->TextLabel3 ->setText( MeasureGUI_CreateDimensionDlg::tr( "POINT_3" ) );
778
779   QVBoxLayout* aLayout = new QVBoxLayout( this );
780
781   aLayout->setMargin( 0 );
782   aLayout->addWidget( myTabs );
783
784   SelectionModes anEdgeModes;
785   SelectionModes aPointModes;
786   anEdgeModes << TopAbs_EDGE;
787   aPointModes << TopAbs_VERTEX;
788
789   SetTabWidget( myTabs );
790   RegisterSelector( myTwoEdgesSelectors->LineEdit1,    myTwoEdgesSelectors->PushButton1,    anEdgeModes, TabID_TwoEdges );
791   RegisterSelector( myTwoEdgesSelectors->LineEdit2,    myTwoEdgesSelectors->PushButton2,    anEdgeModes, TabID_TwoEdges );
792   RegisterSelector( myThreePointsSelectors->LineEdit1, myThreePointsSelectors->PushButton1, aPointModes, TabID_ThreePoints );
793   RegisterSelector( myThreePointsSelectors->LineEdit2, myThreePointsSelectors->PushButton2, aPointModes, TabID_ThreePoints );
794   RegisterSelector( myThreePointsSelectors->LineEdit3, myThreePointsSelectors->PushButton3, aPointModes, TabID_ThreePoints );
795 }
796
797 /* ------------------------------------------------------------------------------*
798  *                                                                               *
799  *                   Argument shape selection logics                             *
800  *                                                                               *
801  * ------------------------------------------------------------------------------*/
802
803 //=================================================================================
804 // class    : MeasureGUI_CreateDimensionDlg::BaseSelectorPane
805 // purpose  : Constructor
806 //=================================================================================
807 MeasureGUI_CreateDimensionDlg::BaseSelectorPane::BaseSelectorPane( QWidget* theParent )
808 : QWidget( theParent ),
809   myTabs( NULL )
810 {
811   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
812   mySelectorIcon = aResMgr->loadPixmap( "GEOM", tr( "ICON_SELECT" ) );
813 }
814
815 //=================================================================================
816 // function : BaseSelectorPane::Reset
817 // purpose  : Reset selector line edits and controls
818 //=================================================================================
819 void MeasureGUI_CreateDimensionDlg::BaseSelectorPane::Reset( bool theOpenDefaultTab )
820 {
821   if ( theOpenDefaultTab && myTabs )
822   {
823     myTabs->setCurrentIndex( 0 );
824   }
825
826   QMap< int, QList<QLineEdit*> >::iterator aTabsIt = mySelectors.begin();
827   for ( ; aTabsIt != mySelectors.end(); ++aTabsIt )
828   {
829     QList<QLineEdit*>& aSelectors = *aTabsIt;
830     QList<QLineEdit*>::iterator aSelectorIt = aSelectors.begin();
831     for ( ; aSelectorIt != aSelectors.end(); ++aSelectorIt )
832     {
833       QLineEdit* aSelector = *aSelectorIt;
834       aSelector->clear();
835       mySelectedShapes[aSelector] = GEOM::GeomObjPtr();
836     }
837   }
838
839   QLineEdit* aFirstSelector = mySelectors[ ActiveTab() ].first();
840
841   mySelectionButtons[aFirstSelector]->click();
842 }
843
844 //=================================================================================
845 // function : BaseSelectorPane::ActiveTab
846 // purpose  : Returns active tab
847 //=================================================================================
848 int MeasureGUI_CreateDimensionDlg::BaseSelectorPane::ActiveTab() const
849 {
850   return myTabs != NULL ? myTabs->currentIndex() : 0;
851 }
852
853 //=================================================================================
854 // function : BaseSelectorPane::SelectionIntoArgument
855 // purpose  : Populates current selector
856 //=================================================================================
857 void MeasureGUI_CreateDimensionDlg::BaseSelectorPane::SelectionIntoArguments( const GEOM::GeomObjPtr& theSelected )
858 {
859   if ( theSelected.isNull() )
860   {
861     myCurrentSelector->clear();
862     mySelectedShapes[myCurrentSelector] = GEOM::GeomObjPtr();
863     return;
864   }
865
866   QString aName = GEOMBase::GetName( theSelected.get() );
867
868   myCurrentSelector->setText( aName );
869
870   mySelectedShapes[myCurrentSelector] = theSelected;
871
872   // find next empty selector (iterate to the last)
873   QList<QLineEdit*>& aCurrentSelectors = mySelectors[ ActiveTab() ];
874   int aCurrentPos = aCurrentSelectors.indexOf( myCurrentSelector );
875   int aSelectorIt = aCurrentPos + 1;
876   for ( ; aSelectorIt != aCurrentSelectors.size(); ++aSelectorIt )
877   {
878     QLineEdit* aNextSelector = aCurrentSelectors[aSelectorIt];
879     if ( mySelectedShapes[aNextSelector].isNull() )
880     {
881       mySelectionButtons[aNextSelector]->click();
882       return;
883     }
884   }
885
886   // find next empty selector (itearte from the first)
887   aSelectorIt = 0;
888   for ( ; aSelectorIt != aCurrentPos; ++aSelectorIt )
889   {
890     QLineEdit* aNextSelector = aCurrentSelectors[aSelectorIt];
891     if ( mySelectedShapes[aNextSelector].isNull() )
892     {
893       mySelectionButtons[aNextSelector]->click();
894       return;
895     }
896   }
897
898   // stop selector switching
899   myCurrentSelector->setEnabled( false );
900   mySelectionButtons[myCurrentSelector]->setDown( false );
901
902   // every selector is populated - construct presentation
903   emit StopSelection();
904   emit SelectionDone();
905 }
906
907 //=================================================================================
908 // function : BaseSelectorPane::GetSelection
909 // purpose  : 
910 //=================================================================================
911 GEOM::GeomObjPtr MeasureGUI_CreateDimensionDlg::BaseSelectorPane::GetSelection( QLineEdit* theSelector ) const
912 {
913   return mySelectedShapes[theSelector];
914 }
915
916 //=================================================================================
917 // function : BaseSelectorPane::OnSelectorClicked
918 // purpose  : Handle activation of selector controls pair {edit, button}
919 //=================================================================================
920 void MeasureGUI_CreateDimensionDlg::BaseSelectorPane::OnSelectorClicked()
921 {
922   // clicked "selector" button
923   QAbstractButton* aSender = qobject_cast<QAbstractButton*>( sender() );
924   if ( !aSender )
925   {
926     return;
927   }
928
929   // get "selector" controls on the active tab
930   QList<QLineEdit*>& aCurrentSelectors = mySelectors[ ActiveTab() ];
931
932   // iterate over controls on the tab and process them
933   for ( int aSelectorIt = 0; aSelectorIt < aCurrentSelectors.size(); ++aSelectorIt )
934   {
935     QLineEdit* aSelector = aCurrentSelectors[aSelectorIt];
936     QPushButton* aButton = mySelectionButtons[aSelector];
937
938     bool isClickedOnes = (aButton == aSender);
939
940     aSelector->setEnabled( isClickedOnes );
941
942     if ( isClickedOnes )
943     {
944       myCurrentSelector = aSelector;
945       myCurrentSelector->setFocus();
946     }
947
948     aButton->setDown( isClickedOnes );
949   }
950
951   emit StartSelection( mySelectionModes[myCurrentSelector] );
952 }
953
954 //=================================================================================
955 // function : BaseSelectorPane::OnTabChanged
956 // purpose  :
957 //=================================================================================
958 void MeasureGUI_CreateDimensionDlg::BaseSelectorPane::OnTabChanged()
959 {
960   QList<QLineEdit*>& aSelectors = mySelectors[ ActiveTab() ];
961   QList<QLineEdit*>::iterator aSelectorIt = aSelectors.begin();
962   for ( ; aSelectorIt != aSelectors.end(); ++aSelectorIt )
963   {
964     QLineEdit* aSelector = *aSelectorIt;
965
966     aSelector->clear();
967
968     mySelectedShapes[aSelector] = GEOM::GeomObjPtr();
969   }
970
971   QLineEdit* aFirstSelector = mySelectors[ ActiveTab() ].first();
972
973   mySelectionButtons[aFirstSelector]->click();
974
975   emit TabChanged();
976 }
977
978 //=================================================================================
979 // function : BaseSelectorPane::SetTabWidget
980 // purpose  :
981 //=================================================================================
982 void MeasureGUI_CreateDimensionDlg::BaseSelectorPane::SetTabWidget( QTabWidget* theTabs )
983 {
984   myTabs = theTabs;
985   connect( myTabs, SIGNAL( currentChanged( int ) ), this, SLOT( OnTabChanged() ) );
986 }
987
988 //=================================================================================
989 // function : BaseSelectorPane::RegisterSelector
990 // purpose  :
991 //=================================================================================
992 void MeasureGUI_CreateDimensionDlg::BaseSelectorPane::RegisterSelector( QLineEdit* theSelectorEdit,
993                                                                         QPushButton* theSelectorButton,
994                                                                         const SelectionModes& theSelectorModes,
995                                                                         const int theTab )
996 {
997   if ( !mySelectors.contains( theTab ) )
998   {
999     mySelectors.insert( theTab, QList<QLineEdit*>() );
1000   }
1001   mySelectors[theTab].append( theSelectorEdit );
1002
1003   mySelectionButtons[theSelectorEdit] = theSelectorButton;
1004   mySelectionModes  [theSelectorEdit] = theSelectorModes;
1005
1006   connect( theSelectorButton, SIGNAL( clicked() ), this, SLOT( OnSelectorClicked() ) );
1007 }