]> SALOME platform Git repositories - modules/geom.git/blob - src/MeasureGUI/MeasureGUI_ManageDimensionsDlg.cxx
Salome HOME
f238a6152ec6fc1272c5bd005c4884703b2da817
[modules/geom.git] / src / MeasureGUI / MeasureGUI_ManageDimensionsDlg.cxx
1 // Copyright (C) 2007-2015  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, or (at your option) any later version.
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_ManageDimensionsDlg.cxx
25 // Author : Anton POLETAEV, Open CASCADE S.A.S.
26
27 #include "MeasureGUI_ManageDimensionsDlg.h"
28 #include "MeasureGUI_CreateDimensionDlg.h"
29 #include "MeasureGUI_DimensionFilter.h"
30
31 #include <GEOMGUI_DimensionProperty.h>
32 #include <GEOMUtils.hxx>
33 #include <GEOMGUI_OCCSelector.h>
34 #include <GEOM_AISDimension.hxx>
35 #include <GEOM_Constants.h>
36 #include <GEOMBase.h>
37 #include <DlgRef.h>
38
39 #include <SOCC_ViewModel.h>
40 #include <SOCC_Prs.h>
41 #include <SalomeApp_Application.h>
42 #include <SalomeApp_Study.h>
43 #include <LightApp_SelectionMgr.h>
44 #include <SUIT_ViewManager.h>
45 #include <SUIT_ResourceMgr.h>
46 #include <SUIT_Session.h>
47 #include <SUIT_MessageBox.h>
48
49 #include <AIS_ListOfInteractive.hxx>
50 #include <AIS_ListIteratorOfListOfInteractive.hxx>
51 #include <AIS_InteractiveContext.hxx>
52 #include <SelectMgr_Filter.hxx>
53 #include <SelectMgr_ListOfFilter.hxx>
54 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
55
56 #include <QTreeWidget>
57 #include <QPushButton>
58 #include <QGroupBox>
59 #include <QVBoxLayout>
60 #include <QHBoxLayout>
61 #include <QPixmap>
62
63 //=================================================================================
64 // function : Constructor
65 // purpose  :
66 //=================================================================================
67 MeasureGUI_ManageDimensionsDlg::MeasureGUI_ManageDimensionsDlg( GeometryGUI* theGUI, QWidget* theParent )
68 : GEOMBase_Skeleton( theGUI, theParent ),
69   myOperatedViewer( NULL ),
70   myCurrentSelection( Selection_None ),
71   myCreateDialog(0)
72 {
73   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
74
75   QPixmap aSelectorIcon = aResMgr->loadPixmap( "GEOM", tr( "ICON_SELECT" ) );
76
77   setWindowTitle( tr( "MANAGE_DIMENSIONS_TITLE" ) );
78
79   mainFrame()->GroupConstructors->setVisible( false );
80   mainFrame()->GroupBoxName->setVisible( false );
81
82   // construct object selector pane
83   myObjectSelector = new DlgRef_1Sel( centralWidget() );
84   myObjectSelector->TextLabel1->setText( tr( "OBJECT_LABEL" ) );
85   myObjectSelector->LineEdit1->setReadOnly( true );
86   myObjectSelector->PushButton1->setIcon( aSelectorIcon );
87   myObjectSelector->PushButton1->setCheckable( true );
88
89   // construct dimension list view pane
90   myDimensionView = new MeasureGUI_1TreeWidget_4Button( centralWidget() );
91   myDimensionView->GroupBox->setTitle( tr( "DIMENSIONS_GROUP" ) );
92   myDimensionView->PushButton1->setText( tr( "ADD_BTN" ) );
93   myDimensionView->PushButton2->setText( tr( "REMOVE_BTN" ) );
94   myDimensionView->PushButton3->setText( tr( "SHOW_ALL_BTN" ) );
95   myDimensionView->PushButton4->setText( tr( "HIDE_ALL_BTN" ) );
96   myDimensionView->TreeWidget->setMinimumHeight( 250 );
97   myDimensionView->setEnabled( false );
98
99   connect( myDimensionView->PushButton1, SIGNAL( clicked() ), SLOT( OnAdd() ) );
100   connect( myDimensionView->PushButton2, SIGNAL( clicked() ), SLOT( OnRemove() ) );
101   connect( myDimensionView->PushButton3, SIGNAL( clicked() ), SLOT( OnShowAll() ) );
102   connect( myDimensionView->PushButton4, SIGNAL( clicked() ), SLOT( OnHideAll() ) );
103   connect( myDimensionView->TreeWidget, 
104            SIGNAL( itemChanged( QTreeWidgetItem*, int ) ),
105            SLOT( OnChangeItem( QTreeWidgetItem* ) ) );
106   connect( myDimensionView->TreeWidget,
107            SIGNAL( currentItemChanged( QTreeWidgetItem*, QTreeWidgetItem* ) ),
108            SLOT( OnSelectItem() ) );
109
110   // populate tree with top-level items
111   myDimensionView->TreeWidget->setHeaderItem( new QTreeWidgetItem( (QTreeWidget*)NULL, QStringList( QString() ) ) );
112
113   // construct main frame layout
114   QVBoxLayout* aMainLayout = new QVBoxLayout( centralWidget() );
115   aMainLayout->setMargin( 0 );
116   aMainLayout->addWidget( myObjectSelector, 0 );
117   aMainLayout->addWidget( myDimensionView, 1 );
118
119   // signals and slots connections
120   connect( myObjectSelector->PushButton1, SIGNAL( clicked() ), SLOT( StartObjectSelection() ) );
121   connect( buttonOk(),    SIGNAL( clicked() ), SLOT( ClickOnOk() ) );
122   connect( buttonApply(), SIGNAL( clicked() ), SLOT( ClickOnApply() ) );
123
124   connect( this, SIGNAL( finished( int ) ), SLOT( OnFinish() ) );
125   
126   connect( myGeomGUI, SIGNAL( SignalDeactivateActiveDialog() ), this, SLOT( OnDeactivateThisDialog() ) );
127   connect( myGeomGUI, SIGNAL( SignalCloseAllDialogs() ),        this, SLOT( ClickOnCancel() ) );
128
129   myDimensionInteractor = new MeasureGUI_DimensionInteractor( theGUI, theParent ),
130
131   SelectionIntoArgument( Selection_Object );
132
133   if ( myEditObject.isNull() )
134   {
135     myObjectSelector->PushButton1->click();
136   }
137
138   setHelpFileName("managing_dimensions_page.html");
139 }
140
141 //=================================================================================
142 // function : Destructor
143 // purpose  :
144 //=================================================================================
145 MeasureGUI_ManageDimensionsDlg::~MeasureGUI_ManageDimensionsDlg()
146 {
147   StopSelection();
148 }
149
150 //=================================================================================
151 // function : StartSelection
152 // purpose  : Starts specific selection
153 //=================================================================================
154 void MeasureGUI_ManageDimensionsDlg::StartSelection( const Selection theSelection )
155 {
156   StopSelection();
157
158   myCurrentSelection = theSelection;
159
160   if ( theSelection == Selection_Object && AllowedToCancelChanges() )
161   {
162     /* -----------------------------------------------  *
163      *               selection of object                *
164      * ------------------------------------------------ */
165
166     globalSelection( GEOM_ALLSHAPES );
167     connect( myGeomGUI->getApp()->selectionMgr(),
168              SIGNAL( currentSelectionChanged() ),
169              this,
170              SLOT( OnSelection() ) );
171   }
172   else if ( theSelection == Selection_Dimension && !myEditObject.isNull() )
173   {
174     /* -----------------------------------------------  *
175      *               selection of dimension             *
176      * ------------------------------------------------ */
177
178     SalomeApp_Application* anApp = myGeomGUI->getApp();
179     if ( !anApp )
180     {
181       return;
182     }
183
184     SUIT_ViewManager* aViewMgr = anApp->activeViewManager();
185     if ( aViewMgr->getType() != SOCC_Viewer::Type() )
186     {
187       return;
188     }
189
190     myOperatedViewer = dynamic_cast<SOCC_Viewer*>( aViewMgr->getViewModel() );
191
192     globalSelection();
193
194     Handle(AIS_InteractiveContext) anAISContext = myOperatedViewer->getAISContext();
195
196     anAISContext->ClearCurrents( Standard_False );
197     anAISContext->ClearSelected( Standard_False );
198     anAISContext->OpenLocalContext( Standard_True, Standard_False );
199
200     Handle(MeasureGUI_DimensionFilter) aFilter = new MeasureGUI_DimensionFilter( myEditObject->GetStudyEntry() );
201
202     anAISContext->AddFilter( aFilter );
203
204     LightApp_SelectionMgr* aSelectionMgr = myGeomGUI->getApp()->selectionMgr();
205
206     QList<SUIT_Selector*> aSelectors;
207     aSelectionMgr->selectors( aSelectors );
208     QList<SUIT_Selector*>::iterator aSelectorIt = aSelectors.begin();
209     for ( ; aSelectorIt != aSelectors.end(); ++aSelectorIt )
210     {
211       SUIT_Selector* aSelector = *aSelectorIt;
212
213       GEOMGUI_OCCSelector* aGeomSelector = dynamic_cast<GEOMGUI_OCCSelector*>( aSelector );
214       if ( !aGeomSelector )
215       {
216         continue;
217       }
218
219       aGeomSelector->setEnabled( false );
220     }
221
222     connect( myOperatedViewer,
223              SIGNAL( selectionChanged() ),
224              this,
225              SLOT( OnSelection() ) );
226
227     myDimensionInteractor->Enable();
228
229     connect( myDimensionInteractor,
230              SIGNAL( InteractionFinished( Handle_AIS_InteractiveObject ) ),
231              this,
232              SLOT( OnInteractionFinished( Handle_AIS_InteractiveObject ) ) );
233
234     anAISContext->UpdateCurrentViewer();
235   }
236 }
237
238 //=================================================================================
239 // function : StopObjectSelection
240 // purpose  : Stops specific selection
241 //=================================================================================
242 void MeasureGUI_ManageDimensionsDlg::StopSelection()
243 {
244   if ( myCurrentSelection == Selection_Object )
245   {
246     /* -----------------------------------------------  *
247      *               selection of object                *
248      * ------------------------------------------------ */
249
250     myObjectSelector->PushButton1->setChecked( false );
251     disconnect( myGeomGUI->getApp()->selectionMgr(),
252                 SIGNAL( currentSelectionChanged() ),
253                 this,
254                 SLOT( OnSelection() ) );
255   }
256   else if ( myCurrentSelection == Selection_Dimension && myOperatedViewer )
257   {
258     /* -----------------------------------------------  *
259      *               selection of dimension             *
260      * ------------------------------------------------ */
261
262     Handle(AIS_InteractiveContext) anAISContext = myOperatedViewer->getAISContext();
263
264     anAISContext->CloseLocalContext();
265
266     LightApp_SelectionMgr* aSelectionMgr = myGeomGUI->getApp()->selectionMgr();
267
268     QList<SUIT_Selector*> aSelectors;
269     aSelectionMgr->selectors( aSelectors );
270     QList<SUIT_Selector*>::iterator aSelectorIt = aSelectors.begin();
271     for ( ; aSelectorIt != aSelectors.end(); ++aSelectorIt )
272     {
273       SUIT_Selector* aSelector = *aSelectorIt;
274
275       GEOMGUI_OCCSelector* aGeomSelector = dynamic_cast<GEOMGUI_OCCSelector*>( aSelector );
276       if ( !aGeomSelector )
277       {
278         continue;
279       }
280
281       aGeomSelector->setEnabled( true );
282     }
283
284     disconnect( myOperatedViewer,
285                 SIGNAL( selectionChanged() ),
286                 this,
287                 SLOT( OnSelection() ) );
288
289     myDimensionInteractor->Disable();
290
291     disconnect( myDimensionInteractor,
292                 SIGNAL( InteractionFinished( Handle_AIS_InteractiveObject ) ),
293                 this,
294                 SLOT( OnInteractionFinished( Handle_AIS_InteractiveObject ) ) );
295   }
296
297   myCurrentSelection = Selection_None;
298
299   globalSelection();
300 }
301
302 //=================================================================================
303 // function : OnSelection
304 // purpose  : 
305 //=================================================================================
306 void MeasureGUI_ManageDimensionsDlg::OnSelection()
307 {
308   SelectionIntoArgument( myCurrentSelection );
309 }
310
311 //=================================================================================
312 // function : SelectionIntoArgument
313 // purpose  : 
314 //=================================================================================
315 void MeasureGUI_ManageDimensionsDlg::SelectionIntoArgument( const Selection theSelection  )
316 {
317   if ( theSelection == Selection_Object )
318   {
319     /* -----------------------------------------------  *
320      *               selection of object                *
321      * ------------------------------------------------ */
322
323     LightApp_SelectionMgr* aSelectionMgr = myGeomGUI->getApp()->selectionMgr();
324     SALOME_ListIO aSelection;
325     aSelectionMgr->selectedObjects( aSelection );
326
327     GEOM::GeomObjPtr aSelected;
328
329     if ( aSelection.Extent() == 1 )
330     {
331       aSelected = GEOMBase::ConvertIOinGEOMObject( aSelection.First() );
332     }
333
334     SetEditObject( aSelected );
335     StartSelection( Selection_Dimension );
336   }
337   else if ( theSelection == Selection_Dimension && myOperatedViewer )
338   {
339     /* -----------------------------------------------  *
340      *               selection of dimension             *
341      * ------------------------------------------------ */
342
343     QTreeWidget* aDimensionList = myDimensionView->TreeWidget;
344
345     Handle(AIS_InteractiveContext) anAISContext = myOperatedViewer->getAISContext();
346
347     // non-single selection is prohibited
348     if ( anAISContext->NbSelected() != 1 )
349     {
350       aDimensionList->setCurrentIndex( QModelIndex() );
351       return;
352     }
353
354     anAISContext->InitSelected();
355
356     Handle(AIS_InteractiveObject) anAIS;
357
358     if ( anAISContext->HasOpenedContext() )
359     {
360       Handle(SelectMgr_EntityOwner) anAISOwner = anAISContext->SelectedOwner();
361       anAIS = Handle(AIS_InteractiveObject)::DownCast( anAISOwner->Selectable() );
362     }
363     else
364     {
365       anAIS = anAISContext->Current();
366     }
367
368     int aDimensionId = IdFromPrs( anAIS );
369
370     SelectInList( aDimensionId );
371
372     if ( aDimensionId >= 0 )
373     {
374       SelectInViewer( myOperatedViewer, aDimensionId );
375     }
376   }
377 }
378
379 //=================================================================================
380 // function : OnAdd
381 // purpose  :
382 //=================================================================================
383 void MeasureGUI_ManageDimensionsDlg::OnAdd()
384 {
385   if(!myCreateDialog) {
386     QWidget* aParent = qobject_cast<QWidget*>( this->parent() );
387     this->Suspend();
388     myCreateDialog = new MeasureGUI_CreateDimensionDlg( myEditObject, myGeomGUI, aParent );
389     connect( myCreateDialog, SIGNAL( finished( int ) ), this, SLOT( Resume() ) );
390     // this is necessary as the GEOMBase_Helper switches selection mode on destruction
391     connect( myCreateDialog, SIGNAL( destroyed( QObject* ) ), this, SLOT( Resume() ) );
392     connect( myCreateDialog, SIGNAL( applyClicked() ), this, SLOT( OnDimensionAdded() ) );
393     myCreateDialog->updateGeometry();
394     myCreateDialog->resize( myCreateDialog->minimumSizeHint() );
395     myCreateDialog->show();
396   } else {
397     myCreateDialog->activateWindow();
398   }
399   myObjectSelector->setEnabled(false);
400   myDimensionView->setEnabled(false);
401 }
402
403 //=================================================================================
404 // function : OnRemove
405 // purpose  :
406 //=================================================================================
407 void MeasureGUI_ManageDimensionsDlg::OnRemove()
408 {
409   int aDimensionId = IdFromItem( myDimensionView->TreeWidget->currentItem() );
410   if ( aDimensionId < 0 )
411   {
412     return;
413   }
414
415   SalomeApp_Application* anApp = myGeomGUI->getApp();
416   if ( !anApp )
417   {
418     return;
419   }
420
421   SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( anApp->activeStudy() );
422   if ( !aStudy )
423   {
424     return;
425   }
426
427   // get property data to change
428   GEOMGUI_DimensionProperty aProp =
429     aStudy->getObjectProperty( GEOM::sharedPropertiesId(),
430                                myEditObject->GetStudyEntry(),
431                                GEOM::propertyName( GEOM::Dimensions ),
432                                QVariant() )
433                                .value<GEOMGUI_DimensionProperty>();
434
435   aProp.RemoveRecord( aDimensionId );
436
437   // store modified property data
438   aStudy->setObjectProperty( GEOM::sharedPropertiesId(),
439                              myEditObject->GetStudyEntry(),
440                              GEOM::propertyName( GEOM::Dimensions ),
441                              aProp );
442
443   PopulateList();
444
445   RedisplayObject();
446 }
447
448 //=================================================================================
449 // function : OnChangeItem
450 // purpose  :
451 //=================================================================================
452 void MeasureGUI_ManageDimensionsDlg::OnChangeItem( QTreeWidgetItem* theItem )
453 {
454   int aDimensionId = IdFromItem( theItem );
455   if ( aDimensionId < 0 )
456   {
457     return;
458   }
459
460   SalomeApp_Application* anApp = myGeomGUI->getApp();
461   if ( !anApp )
462   {
463     return;
464   }
465
466   SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( anApp->activeStudy() );
467   if ( !aStudy )
468   {
469     return;
470   }
471
472   // get property data to change
473   GEOMGUI_DimensionProperty aProp =
474     aStudy->getObjectProperty( GEOM::sharedPropertiesId(),
475                                myEditObject->GetStudyEntry(),
476                                GEOM::propertyName( GEOM::Dimensions ),
477                                QVariant() )
478                                .value<GEOMGUI_DimensionProperty>();
479
480   // change property data
481   bool isAnythingChanged = false;
482
483   QString aName = theItem->text( 0 );
484   if ( aProp.GetName( aDimensionId ) != aName )
485   {
486     isAnythingChanged = true;
487     aProp.SetName( aDimensionId, aName );
488   }
489
490   bool isVisible = theItem->checkState( 0 ) == Qt::Checked;
491   if ( aProp.IsVisible( aDimensionId ) != isVisible )
492   {
493     isAnythingChanged = true;
494     aProp.SetVisible( aDimensionId, isVisible );
495   }
496
497   // store property data
498   if ( !isAnythingChanged )
499   {
500     return;
501   }
502
503   // store modified property data
504   aStudy->setObjectProperty( GEOM::sharedPropertiesId(),
505                              myEditObject->GetStudyEntry(),
506                              GEOM::propertyName( GEOM::Dimensions ),
507                              aProp );
508
509   RedisplayObject();
510 }
511
512 //=================================================================================
513 // function : OnSelectItem
514 // purpose  :
515 //=================================================================================
516 void MeasureGUI_ManageDimensionsDlg::OnSelectItem()
517 {
518   if ( !myOperatedViewer )
519   {
520     return;
521   }
522
523   QTreeWidgetItem* aSelectedItem = myDimensionView->TreeWidget->currentItem();
524
525   SelectInViewer( myOperatedViewer, IdFromItem( aSelectedItem ) );
526 }
527
528 //=================================================================================
529 // function : OnShowAll
530 // purpose  :
531 //=================================================================================
532 void MeasureGUI_ManageDimensionsDlg::OnShowAll()
533 {
534   // read propety data
535   SalomeApp_Application* anApp = myGeomGUI->getApp();
536   if ( !anApp )
537   {
538     return;
539   }
540
541   SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( anApp->activeStudy() );
542   if ( !aStudy )
543   {
544     return;
545   }
546
547   // get property data to change
548   GEOMGUI_DimensionProperty aProp =
549     aStudy->getObjectProperty( GEOM::sharedPropertiesId(),
550                                myEditObject->GetStudyEntry(),
551                                GEOM::propertyName( GEOM::Dimensions ),
552                                QVariant() )
553                                .value<GEOMGUI_DimensionProperty>();
554
555   QTreeWidget* aDimensionList = myDimensionView->TreeWidget;
556
557   bool isBlocked = aDimensionList->blockSignals( true );
558
559   for ( int anIt = 0; anIt < aProp.GetNumber(); ++anIt )
560   {
561     aProp.SetVisible( anIt, true );
562   }
563
564   // store modified property data
565   aStudy->setObjectProperty( GEOM::sharedPropertiesId(),
566                              myEditObject->GetStudyEntry(),
567                              GEOM::propertyName( GEOM::Dimensions ),
568                              aProp );
569
570   PopulateList();
571
572   RedisplayObject();
573
574   aDimensionList->blockSignals( isBlocked );
575 }
576
577 //=================================================================================
578 // function : OnHideAll
579 // purpose  :
580 //=================================================================================
581 void MeasureGUI_ManageDimensionsDlg::OnHideAll()
582 {
583   // read propety data
584   SalomeApp_Application* anApp = myGeomGUI->getApp();
585   if ( !anApp )
586   {
587     return;
588   }
589
590   SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( anApp->activeStudy() );
591   if ( !aStudy )
592   {
593     return;
594   }
595
596   // get property data to change
597   GEOMGUI_DimensionProperty aProp =
598     aStudy->getObjectProperty( GEOM::sharedPropertiesId(),
599                                myEditObject->GetStudyEntry(),
600                                GEOM::propertyName( GEOM::Dimensions ),
601                                QVariant() )
602                                .value<GEOMGUI_DimensionProperty>();
603
604   QTreeWidget* aDimensionList = myDimensionView->TreeWidget;
605
606   bool isBlocked = aDimensionList->blockSignals( true );
607
608   for ( int anIt = 0; anIt < aProp.GetNumber(); ++anIt )
609   {
610     aProp.SetVisible( anIt, false );
611   }
612
613   // store modified property data
614   aStudy->setObjectProperty( GEOM::sharedPropertiesId(),
615                              myEditObject->GetStudyEntry(),
616                              GEOM::propertyName( GEOM::Dimensions ),
617                              aProp );
618
619   PopulateList();
620
621   RedisplayObject();
622
623   aDimensionList->blockSignals( isBlocked );
624 }
625
626 //=================================================================================
627 // function : OnInteractionFinished
628 // purpose  :
629 //=================================================================================
630 void MeasureGUI_ManageDimensionsDlg::OnInteractionFinished( Handle(AIS_InteractiveObject) theIO )
631 {
632   // update property data
633   SalomeApp_Application* anApp = myGeomGUI->getApp();
634   if ( !anApp )
635   {
636     return;
637   }
638
639   SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( anApp->activeStudy() );
640   if ( !aStudy )
641   {
642     return;
643   }
644
645   gp_Ax3 aLCS;
646   TopoDS_Shape anParentSh;
647   if ( GEOMBase::GetShape( myEditObject.get(), anParentSh ) )
648   {
649     aLCS = gp_Ax3().Transformed( anParentSh.Location().Transformation() );
650   }
651
652   int aDimensionId = IdFromPrs( theIO );
653
654   // get property data to change
655   GEOMGUI_DimensionProperty aProp =
656     aStudy->getObjectProperty( GEOM::sharedPropertiesId(),
657                                myEditObject->GetStudyEntry(),
658                                GEOM::propertyName( GEOM::Dimensions ),
659                                QVariant() )
660                                .value<GEOMGUI_DimensionProperty>();
661
662   aProp.SetRecord( aDimensionId, Handle(AIS_Dimension)::DownCast( theIO ), aLCS );
663
664   // store modified property data
665   aStudy->setObjectProperty( GEOM::sharedPropertiesId(),
666                              myEditObject->GetStudyEntry(),
667                              GEOM::propertyName( GEOM::Dimensions ),
668                              aProp );
669 }
670
671 //=================================================================================
672 // function : Suspend
673 // purpose  :
674 //=================================================================================
675 void MeasureGUI_ManageDimensionsDlg::Suspend()
676 {
677   StopSelection();
678 }
679
680 //=================================================================================
681 // function : Suspend
682 // purpose  :
683 //=================================================================================
684 void MeasureGUI_ManageDimensionsDlg::Resume()
685 {
686   if(myCreateDialog){
687     myCreateDialog = 0;
688     myObjectSelector->setEnabled(true);
689     myDimensionView->setEnabled(true);
690   }
691
692   if ( !myEditObject.isNull() )
693   {
694     PopulateList();
695     StartSelection( Selection_Dimension );
696   }
697 }
698
699 //=================================================================================
700 // function : Suspend
701 // purpose  :
702 //=================================================================================
703 void MeasureGUI_ManageDimensionsDlg::OnDimensionAdded() {
704   if ( !myEditObject.isNull() )
705   {
706     PopulateList();
707   }
708 }
709
710 //=================================================================================
711 // function : ClickOnOk
712 // purpose  : 
713 //=================================================================================
714 void MeasureGUI_ManageDimensionsDlg::ClickOnOk()
715 {
716   setIsApplyAndClose( true );
717   if ( ClickOnApply() )
718   {
719     ClickOnCancel();
720   }
721 }
722
723 //=================================================================================
724 // function : ClickOnCancel
725 // purpose  : 
726 //=================================================================================
727 void MeasureGUI_ManageDimensionsDlg::ClickOnCancel()
728 {
729   if ( !AllowedToCancelChanges() )
730   {
731     return;
732   }
733   
734   if( myCreateDialog ) {
735     myCreateDialog->close();
736     myCreateDialog = 0;
737   }
738   
739   GEOMBase_Skeleton::ClickOnCancel();
740 }
741
742 //=================================================================================
743 // function : ClickOnApply
744 // purpose  : 
745 //=================================================================================
746 bool MeasureGUI_ManageDimensionsDlg::ClickOnApply()
747 {
748   if ( myEditObject.isNull() )
749   {
750     return true;
751   }
752
753   SalomeApp_Application* anApp = myGeomGUI->getApp();
754   if ( !anApp )
755   {
756     return true;
757   }
758
759   SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( anApp->activeStudy() );
760   if ( !aStudy )
761   {
762     return true;
763   }
764
765   mySavedPropertyState = 
766     aStudy->getObjectProperty( GEOM::sharedPropertiesId(),
767                                myEditObject->GetStudyEntry(),
768                                GEOM::propertyName( GEOM::Dimensions ),
769                                QVariant() )
770                                .value<GEOMGUI_DimensionProperty>();
771
772   mySavedPropertyState.SaveToAttribute( aStudy, myEditObject->GetStudyEntry() );
773
774   return true;
775 }
776
777 //=================================================================================
778 // function : OnFinish
779 // purpose  : 
780 //=================================================================================
781 void MeasureGUI_ManageDimensionsDlg::OnFinish()
782 {
783   if ( myEditObject.isNull() )
784   {
785     GEOMBase_Skeleton::ClickOnCancel();
786     return;
787   }
788
789   SalomeApp_Application* anApp = myGeomGUI->getApp();
790   if ( !anApp )
791   {
792     GEOMBase_Skeleton::ClickOnCancel();
793     return;
794   }
795
796   SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( anApp->activeStudy() );
797   if ( !aStudy )
798   {
799     GEOMBase_Skeleton::ClickOnCancel();
800     return;
801   }
802
803   // reset preview property state
804   aStudy->setObjectProperty( GEOM::sharedPropertiesId(),
805                              myEditObject->GetStudyEntry(),
806                              GEOM::propertyName( GEOM::Dimensions ),
807                              QVariant() );
808 }
809
810 //=================================================================================
811 // function : enterEvent()
812 // purpose  :
813 //=================================================================================
814 void MeasureGUI_ManageDimensionsDlg::enterEvent( QEvent* )
815 {
816   if ( !mainFrame()->GroupConstructors->isEnabled() )
817   {
818     OnActivateThisDialog();
819   }
820 }
821
822 //=================================================================================
823 // function : OnActivateThisDialog
824 // purpose  :
825 //=================================================================================
826 void MeasureGUI_ManageDimensionsDlg::OnActivateThisDialog()
827 {
828   disconnect( myGeomGUI, SIGNAL( SignalDeactivateActiveDialog() ), this, SLOT( OnDeactivateThisDialog() ) );
829   GEOMBase_Skeleton::ActivateThisDialog();
830   connect( myGeomGUI, SIGNAL( SignalDeactivateActiveDialog() ), this, SLOT( OnDeactivateThisDialog() ) );
831
832   SelectionIntoArgument( Selection_Object );
833
834   if ( myEditObject.isNull() )
835   {
836     myObjectSelector->PushButton1->click();
837   }
838 }
839
840 //=================================================================================
841 // function : OnDeactivateThisDialog
842 // purpose  :
843 //=================================================================================
844 void MeasureGUI_ManageDimensionsDlg::OnDeactivateThisDialog()
845 {
846   if ( AllowedToSaveChanges() )
847   {
848     ClickOnApply();
849   }
850
851   if ( !myEditObject.isNull() )
852   {
853     SalomeApp_Study* aStudy = NULL;
854     SalomeApp_Application* anApp = myGeomGUI->getApp();
855     if ( anApp )
856     {
857       aStudy = dynamic_cast<SalomeApp_Study*>( anApp->activeStudy() );
858     }
859
860     if ( aStudy )
861     {
862       aStudy->setObjectProperty( GEOM::sharedPropertiesId(),
863                                  myEditObject->GetStudyEntry(),
864                                  GEOM::propertyName( GEOM::Dimensions ),
865                                  QVariant() );
866     }
867
868     redisplay( myEditObject.get() );
869   }
870
871   GEOMBase_Skeleton::DeactivateActiveDialog();
872 }
873
874 //=================================================================================
875 // function : SetEditObject
876 // purpose  :
877 //=================================================================================
878 void MeasureGUI_ManageDimensionsDlg::SetEditObject( const GEOM::GeomObjPtr& theObj )
879 {
880   RestoreState();
881
882   QString aName = GEOMBase::GetName( theObj.get() );
883
884   myObjectSelector->LineEdit1->setText( aName );
885
886   myEditObject = theObj;
887
888   if ( myEditObject.isNull() )
889   {
890     myDimensionView->TreeWidget->clear();
891     myDimensionView->setEnabled( false );
892     return;
893   }
894
895   SalomeApp_Application* anApp = myGeomGUI->getApp();
896   if ( !anApp )
897   {
898     return;
899   }
900
901   SUIT_ViewManager* aViewMgr = anApp->activeViewManager();
902   if ( aViewMgr->getType() != SOCC_Viewer::Type() )
903   {
904     return;
905   }
906
907   SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( anApp->activeStudy() );
908   if ( !aStudy )
909   {
910     return;
911   }
912
913   mySavedPropertyState.LoadFromAttribute( getStudy(), myEditObject->GetStudyEntry() );
914
915   // set property state for preview
916   aStudy->setObjectProperty( GEOM::sharedPropertiesId(),
917                              myEditObject->GetStudyEntry(),
918                              GEOM::propertyName( GEOM::Dimensions ),
919                              mySavedPropertyState );
920
921   PopulateList();
922
923   myDimensionView->setEnabled( true );
924 }
925
926 //=================================================================================
927 // function : RestoreState
928 // purpose  :
929 //=================================================================================
930 void MeasureGUI_ManageDimensionsDlg::RestoreState()
931 {
932   if ( myEditObject.isNull() )
933   {
934     return;
935   }
936
937   SalomeApp_Application* anApp = myGeomGUI->getApp();
938   if ( !anApp )
939   {
940     return;
941   }
942
943   SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( anApp->activeStudy() );
944   if ( !aStudy )
945   {
946     return;
947   }
948
949   QString aEntry = myEditObject->GetStudyEntry();
950
951   // reset preview property state
952   aStudy->setObjectProperty( GEOM::sharedPropertiesId(),
953                              myEditObject->GetStudyEntry(),
954                              GEOM::propertyName( GEOM::Dimensions ),
955                              QVariant() );
956 }
957
958 //=================================================================================
959 // function : PopulateList
960 // purpose  :
961 //=================================================================================
962 void MeasureGUI_ManageDimensionsDlg::PopulateList()
963 {
964   QTreeWidget* aListView = myDimensionView->TreeWidget;
965
966   // clear list completely if object is null
967   if ( myEditObject.isNull() )
968   {
969     aListView->clear();
970     return;
971   }
972
973   // clear only groups data if object is ok
974   QTreeWidgetItem* aDistanceGroupItem = aListView->topLevelItem( GroupItem_Distance );
975   QTreeWidgetItem* aDiameterGroupItem = aListView->topLevelItem( GroupItem_Diameter );
976   QTreeWidgetItem* anAngleGroupItem   = aListView->topLevelItem( GroupItem_Angle );
977
978   // create group items if not exist yet
979   if ( !aDistanceGroupItem || !aDiameterGroupItem || !anAngleGroupItem )
980   {
981     aDistanceGroupItem = new QTreeWidgetItem( (QTreeWidget*)NULL, QStringList( tr( "DISTANCE_ITEM" ) ) );
982     aDiameterGroupItem = new QTreeWidgetItem( (QTreeWidget*)NULL, QStringList( tr( "DIAMETER_ITEM" ) ) );
983     anAngleGroupItem   = new QTreeWidgetItem( (QTreeWidget*)NULL, QStringList( tr( "ANGLE_ITEM" ) ) );
984
985     aListView->clear();
986
987     aListView->addTopLevelItem( aDistanceGroupItem );
988     aListView->addTopLevelItem( aDiameterGroupItem );
989     aListView->addTopLevelItem( anAngleGroupItem );
990     aListView->expandAll();
991   }
992
993   qDeleteAll( aDistanceGroupItem->takeChildren() );
994   qDeleteAll( aDiameterGroupItem->takeChildren() );
995   qDeleteAll( anAngleGroupItem->takeChildren() );
996
997   // collect all groupped items
998   QList<QTreeWidgetItem*> aDistItems;
999   QList<QTreeWidgetItem*> aDiamItems;
1000   QList<QTreeWidgetItem*> anAngItems;
1001
1002   SalomeApp_Application* anApp = myGeomGUI->getApp();
1003   if ( !anApp )
1004   {
1005     return;
1006   }
1007
1008   SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( anApp->activeStudy() );
1009   if ( !aStudy )
1010   {
1011     return;
1012   }
1013
1014   QString aEntry = myEditObject->GetStudyEntry();
1015
1016   // get property data to change
1017   GEOMGUI_DimensionProperty aProp =
1018     aStudy->getObjectProperty( GEOM::sharedPropertiesId(),
1019                                myEditObject->GetStudyEntry(),
1020                                GEOM::propertyName( GEOM::Dimensions ),
1021                                QVariant() )
1022                                .value<GEOMGUI_DimensionProperty>();
1023
1024   // read dimension records from property
1025   for ( int anIt = 0; anIt < aProp.GetNumber(); ++anIt )
1026   {
1027     QString aName  = aProp.GetName( anIt );
1028     bool isVisible = aProp.IsVisible( anIt );
1029     int aType      = aProp.GetType( anIt );
1030
1031     QTreeWidgetItem* anItem = new QTreeWidgetItem;
1032     anItem->setText( 0, aName );
1033     anItem->setCheckState( 0, isVisible ? Qt::Checked : Qt::Unchecked );
1034     anItem->setData( 0, Qt::UserRole, anIt );
1035     anItem->setFlags( Qt::ItemIsUserCheckable
1036                     | Qt::ItemIsSelectable
1037                     | Qt::ItemIsEditable
1038                     | Qt::ItemIsEnabled );
1039
1040     switch ( aType )
1041     {
1042       case GEOMGUI_DimensionProperty::DimensionType_Length   : aDistItems << anItem; break;
1043       case GEOMGUI_DimensionProperty::DimensionType_Diameter : aDiamItems << anItem; break;
1044       case GEOMGUI_DimensionProperty::DimensionType_Angle    : anAngItems << anItem; break;
1045     }
1046   }
1047
1048   aDistanceGroupItem->addChildren( aDistItems );
1049   aDiameterGroupItem->addChildren( aDiamItems );
1050   anAngleGroupItem->addChildren( anAngItems );
1051
1052   aListView->setItemHidden( aDistanceGroupItem, aDistItems.isEmpty() );
1053   aListView->setItemHidden( aDiameterGroupItem, aDiamItems.isEmpty() );
1054   aListView->setItemHidden( anAngleGroupItem,   anAngItems.isEmpty() );
1055 }
1056
1057 //=================================================================================
1058 // function : HasUnsavedChanges
1059 // purpose  :
1060 //=================================================================================
1061 bool MeasureGUI_ManageDimensionsDlg::HasUnsavedChanges()
1062 {
1063   if ( myEditObject.isNull() )
1064   {
1065     return false;
1066   }
1067
1068   SalomeApp_Application* anApp = myGeomGUI->getApp();
1069   if ( !anApp )
1070   {
1071     return false;
1072   }
1073
1074   SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( anApp->activeStudy() );
1075   if ( !aStudy )
1076   {
1077     return false;
1078   }
1079
1080   GEOMGUI_DimensionProperty aCurrentState =
1081     aStudy->getObjectProperty( GEOM::sharedPropertiesId(),
1082                                myEditObject->GetStudyEntry(),
1083                                GEOM::propertyName( GEOM::Dimensions ),
1084                                QVariant() )
1085                                .value<GEOMGUI_DimensionProperty>();
1086
1087   return aCurrentState != mySavedPropertyState;
1088 }
1089
1090 //=================================================================================
1091 // function : AllowedToCancelChanges
1092 // purpose  :
1093 //=================================================================================
1094 bool MeasureGUI_ManageDimensionsDlg::AllowedToCancelChanges()
1095 {
1096   if ( !HasUnsavedChanges() )
1097   {
1098     return true;
1099   }
1100
1101   int aResponse = SUIT_MessageBox::warning( this,
1102                                             tr( "WRN_TITLE_UNSAVED" ),
1103                                             tr( "WRN_MSG_CHANGES_LOST" ),
1104                                             QMessageBox::Ok,
1105                                             QMessageBox::Cancel );
1106
1107   return aResponse == QMessageBox::Ok;
1108 }
1109
1110 //=================================================================================
1111 // function : AllowedToSaveChanges
1112 // purpose  :
1113 //=================================================================================
1114 bool MeasureGUI_ManageDimensionsDlg::AllowedToSaveChanges()
1115 {
1116   if ( !HasUnsavedChanges() )
1117   {
1118     return false;
1119   }
1120
1121   int aResponse = SUIT_MessageBox::warning( this,
1122                                             tr( "WRN_TITLE_UNSAVED" ),
1123                                             tr( "WRN_MSG_CHANGES_SAVE" ),
1124                                             QMessageBox::Ok,
1125                                             QMessageBox::Cancel );
1126
1127   return aResponse == QMessageBox::Ok;
1128 }
1129
1130 //=================================================================================
1131 // function : IdFromItem
1132 // purpose  :
1133 //=================================================================================
1134 int MeasureGUI_ManageDimensionsDlg::IdFromItem( QTreeWidgetItem* theItem )
1135 {
1136   if ( !theItem )
1137   {
1138     return -1;
1139   }
1140
1141   bool isIdOK = false;
1142   const int anId = theItem->data( 0, Qt::UserRole ).toInt( &isIdOK );
1143
1144   return isIdOK ? anId : -1;
1145 }
1146
1147 //=================================================================================
1148 // function : IdFromPrs
1149 // purpose  :
1150 //=================================================================================
1151 int MeasureGUI_ManageDimensionsDlg::IdFromPrs( const Handle(AIS_InteractiveObject)& theAIS )
1152 {
1153   Handle(SALOME_InteractiveObject) anIO = Handle(SALOME_InteractiveObject)::DownCast( theAIS->GetOwner() );
1154
1155   if ( anIO.IsNull() )
1156   {
1157     return -1;
1158   }
1159
1160   QString anIOEntry   = anIO->getEntry();
1161   QString anEditEntry = myEditObject->GetStudyEntry();
1162   if ( anIOEntry != anEditEntry )
1163   {
1164     return -1;
1165   }
1166
1167   Handle(GEOM_AISLength) aLength = Handle(GEOM_AISLength)::DownCast( theAIS );
1168   if ( !aLength.IsNull() )
1169   {
1170     return aLength->GetId();
1171   }
1172
1173   Handle(GEOM_AISDiameter) aDiameter = Handle(GEOM_AISDiameter)::DownCast( theAIS );
1174   if ( !aDiameter.IsNull() )
1175   {
1176     return aDiameter->GetId();
1177   }
1178
1179   Handle(GEOM_AISAngle) anAngle = Handle(GEOM_AISAngle)::DownCast( theAIS );
1180   if ( !anAngle.IsNull() )
1181   {
1182     return anAngle->GetId();
1183   }
1184
1185   return -1;
1186 }
1187
1188 //=================================================================================
1189 // function : SelectInList
1190 // purpose  :
1191 //=================================================================================
1192 void MeasureGUI_ManageDimensionsDlg::SelectInList( const Handle(AIS_InteractiveObject)& theIO )
1193 {
1194   SelectInList( IdFromPrs( theIO ) );
1195 }
1196
1197 //=================================================================================
1198 // function : SelectInList
1199 // purpose  :
1200 //=================================================================================
1201 void MeasureGUI_ManageDimensionsDlg::SelectInList( const int theId )
1202 {
1203   QTreeWidget* aListOfDimension = myDimensionView->TreeWidget;
1204
1205   QAbstractItemModel* aModel = aListOfDimension->model();
1206   QModelIndexList aIndexes = aModel->match( aModel->index(0, 0),
1207                                             Qt::UserRole,
1208                                             QVariant( theId ),
1209                                             -1, Qt::MatchRecursive );
1210
1211   if ( aIndexes.isEmpty() )
1212   {
1213     aListOfDimension->setCurrentIndex( QModelIndex() );
1214     return;
1215   }
1216
1217   bool isBlocked = aListOfDimension->blockSignals( true );
1218   aListOfDimension->setCurrentIndex( aIndexes.first() );
1219   aListOfDimension->blockSignals( isBlocked );
1220 }
1221
1222 //=================================================================================
1223 // function : SelectInViewer
1224 // purpose  :
1225 //=================================================================================
1226 void MeasureGUI_ManageDimensionsDlg::SelectInViewer( SOCC_Viewer* theViewer, const int theId )
1227 {
1228   Handle(AIS_InteractiveContext) anAISContext = theViewer->getAISContext();
1229   if ( anAISContext.IsNull() )
1230   {
1231     return;
1232   }
1233
1234   Standard_Boolean isLocal = anAISContext->HasOpenedContext();
1235   if ( isLocal )
1236   {
1237     anAISContext->ClearSelected( Standard_False );
1238   }
1239   else
1240   {
1241     anAISContext->ClearCurrents( Standard_False );
1242   }
1243
1244   SOCC_Prs* aPrs = dynamic_cast<SOCC_Prs*>( theViewer->CreatePrs( myEditObject->GetStudyEntry() ) );
1245
1246   AIS_ListOfInteractive aListOfIO;
1247   aPrs->GetObjects( aListOfIO );
1248
1249   AIS_ListIteratorOfListOfInteractive anIt( aListOfIO );
1250   for ( ; anIt.More(); anIt.Next() )
1251   {
1252     const Handle(AIS_InteractiveObject)& anIO = anIt.Value();
1253     if ( IdFromPrs( anIO ) != theId )
1254     {
1255       if ( isLocal )
1256       {
1257         anAISContext->Deactivate( anIO, AIS_DSM_Line );
1258         anAISContext->Deactivate( anIO, AIS_DSM_Text );
1259       }
1260       continue;
1261     }
1262
1263     if ( isLocal )
1264     {
1265       anAISContext->AddOrRemoveSelected( anIO, Standard_False );
1266       anAISContext->Activate( anIO, AIS_DSM_Line );
1267       anAISContext->Activate( anIO, AIS_DSM_Text );
1268     }
1269     else
1270     {
1271       anAISContext->AddOrRemoveCurrentObject( anIO, Standard_False );
1272     }
1273
1274     anAISContext->UpdateCurrentViewer();
1275   }
1276 }
1277
1278 //=================================================================================
1279 // function : RedisplayObject
1280 // purpose  :
1281 //=================================================================================
1282 void MeasureGUI_ManageDimensionsDlg::RedisplayObject()
1283 {
1284   redisplay( myEditObject.get() );
1285
1286   if ( !myOperatedViewer )
1287   {
1288     return;
1289   }
1290
1291   QTreeWidgetItem* aSelectedItem = myDimensionView->TreeWidget->currentItem();
1292   if ( !aSelectedItem )
1293   {
1294     return;
1295   }
1296
1297   StartSelection( myCurrentSelection );
1298
1299   SelectInViewer( myOperatedViewer, IdFromItem( aSelectedItem ) );
1300 }