Salome HOME
Update copyrights 2014.
[modules/geom.git] / src / MeasureGUI / MeasureGUI_ManageDimensionsDlg.cxx
1 // Copyright (C) 2007-2014  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   redisplay( myEditObject.get() );
810 }
811
812 //=================================================================================
813 // function : enterEvent()
814 // purpose  :
815 //=================================================================================
816 void MeasureGUI_ManageDimensionsDlg::enterEvent( QEvent* )
817 {
818   if ( !mainFrame()->GroupConstructors->isEnabled() )
819   {
820     OnActivateThisDialog();
821   }
822 }
823
824 //=================================================================================
825 // function : OnActivateThisDialog
826 // purpose  :
827 //=================================================================================
828 void MeasureGUI_ManageDimensionsDlg::OnActivateThisDialog()
829 {
830   disconnect( myGeomGUI, SIGNAL( SignalDeactivateActiveDialog() ), this, SLOT( OnDeactivateThisDialog() ) );
831   GEOMBase_Skeleton::ActivateThisDialog();
832   connect( myGeomGUI, SIGNAL( SignalDeactivateActiveDialog() ), this, SLOT( OnDeactivateThisDialog() ) );
833
834   SelectionIntoArgument( Selection_Object );
835
836   if ( myEditObject.isNull() )
837   {
838     myObjectSelector->PushButton1->click();
839   }
840 }
841
842 //=================================================================================
843 // function : OnDeactivateThisDialog
844 // purpose  :
845 //=================================================================================
846 void MeasureGUI_ManageDimensionsDlg::OnDeactivateThisDialog()
847 {
848   if ( AllowedToSaveChanges() )
849   {
850     ClickOnApply();
851   }
852
853   if ( !myEditObject.isNull() )
854   {
855     SalomeApp_Study* aStudy = NULL;
856     SalomeApp_Application* anApp = myGeomGUI->getApp();
857     if ( anApp )
858     {
859       aStudy = dynamic_cast<SalomeApp_Study*>( anApp->activeStudy() );
860     }
861
862     if ( aStudy )
863     {
864       aStudy->setObjectProperty( GEOM::sharedPropertiesId(),
865                                  myEditObject->GetStudyEntry(),
866                                  GEOM::propertyName( GEOM::Dimensions ),
867                                  QVariant() );
868     }
869
870     redisplay( myEditObject.get() );
871   }
872
873   GEOMBase_Skeleton::DeactivateActiveDialog();
874 }
875
876 //=================================================================================
877 // function : SetEditObject
878 // purpose  :
879 //=================================================================================
880 void MeasureGUI_ManageDimensionsDlg::SetEditObject( const GEOM::GeomObjPtr& theObj )
881 {
882   RestoreState();
883
884   QString aName = GEOMBase::GetName( theObj.get() );
885
886   myObjectSelector->LineEdit1->setText( aName );
887
888   myEditObject = theObj;
889
890   if ( myEditObject.isNull() )
891   {
892     myDimensionView->setEnabled( false );
893
894     return;
895   }
896
897   SalomeApp_Application* anApp = myGeomGUI->getApp();
898   if ( !anApp )
899   {
900     return;
901   }
902
903   SUIT_ViewManager* aViewMgr = anApp->activeViewManager();
904   if ( aViewMgr->getType() != SOCC_Viewer::Type() )
905   {
906     return;
907   }
908
909   SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( anApp->activeStudy() );
910   if ( !aStudy )
911   {
912     return;
913   }
914
915   mySavedPropertyState.LoadFromAttribute( getStudy(), myEditObject->GetStudyEntry() );
916
917   // set property state for preview
918   aStudy->setObjectProperty( GEOM::sharedPropertiesId(),
919                              myEditObject->GetStudyEntry(),
920                              GEOM::propertyName( GEOM::Dimensions ),
921                              mySavedPropertyState );
922
923   PopulateList();
924
925   myDimensionView->setEnabled( true );
926 }
927
928 //=================================================================================
929 // function : RestoreState
930 // purpose  :
931 //=================================================================================
932 void MeasureGUI_ManageDimensionsDlg::RestoreState()
933 {
934   if ( myEditObject.isNull() )
935   {
936     return;
937   }
938
939   SalomeApp_Application* anApp = myGeomGUI->getApp();
940   if ( !anApp )
941   {
942     return;
943   }
944
945   SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( anApp->activeStudy() );
946   if ( !aStudy )
947   {
948     return;
949   }
950
951   QString aEntry = myEditObject->GetStudyEntry();
952
953   // reset preview property state
954   aStudy->setObjectProperty( GEOM::sharedPropertiesId(),
955                              myEditObject->GetStudyEntry(),
956                              GEOM::propertyName( GEOM::Dimensions ),
957                              QVariant() );
958
959   RedisplayObject();
960 }
961
962 //=================================================================================
963 // function : PopulateList
964 // purpose  :
965 //=================================================================================
966 void MeasureGUI_ManageDimensionsDlg::PopulateList()
967 {
968   QTreeWidget* aListView = myDimensionView->TreeWidget;
969
970   // clear list completely if object is null
971   if ( myEditObject.isNull() )
972   {
973     aListView->clear();
974     return;
975   }
976
977   // clear only groups data if object is ok
978   QTreeWidgetItem* aDistanceGroupItem = aListView->topLevelItem( GroupItem_Distance );
979   QTreeWidgetItem* aDiameterGroupItem = aListView->topLevelItem( GroupItem_Diameter );
980   QTreeWidgetItem* anAngleGroupItem   = aListView->topLevelItem( GroupItem_Angle );
981
982   // create group items if not exist yet
983   if ( !aDistanceGroupItem || !aDiameterGroupItem || !anAngleGroupItem )
984   {
985     aDistanceGroupItem = new QTreeWidgetItem( (QTreeWidget*)NULL, QStringList( tr( "DISTANCE_ITEM" ) ) );
986     aDiameterGroupItem = new QTreeWidgetItem( (QTreeWidget*)NULL, QStringList( tr( "DIAMETER_ITEM" ) ) );
987     anAngleGroupItem   = new QTreeWidgetItem( (QTreeWidget*)NULL, QStringList( tr( "ANGLE_ITEM" ) ) );
988
989     aListView->clear();
990
991     aListView->addTopLevelItem( aDistanceGroupItem );
992     aListView->addTopLevelItem( aDiameterGroupItem );
993     aListView->addTopLevelItem( anAngleGroupItem );
994     aListView->expandAll();
995   }
996
997   qDeleteAll( aDistanceGroupItem->takeChildren() );
998   qDeleteAll( aDiameterGroupItem->takeChildren() );
999   qDeleteAll( anAngleGroupItem->takeChildren() );
1000
1001   // collect all groupped items
1002   QList<QTreeWidgetItem*> aDistItems;
1003   QList<QTreeWidgetItem*> aDiamItems;
1004   QList<QTreeWidgetItem*> anAngItems;
1005
1006   SalomeApp_Application* anApp = myGeomGUI->getApp();
1007   if ( !anApp )
1008   {
1009     return;
1010   }
1011
1012   SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( anApp->activeStudy() );
1013   if ( !aStudy )
1014   {
1015     return;
1016   }
1017
1018   QString aEntry = myEditObject->GetStudyEntry();
1019
1020   // get property data to change
1021   GEOMGUI_DimensionProperty aProp =
1022     aStudy->getObjectProperty( GEOM::sharedPropertiesId(),
1023                                myEditObject->GetStudyEntry(),
1024                                GEOM::propertyName( GEOM::Dimensions ),
1025                                QVariant() )
1026                                .value<GEOMGUI_DimensionProperty>();
1027
1028   // read dimension records from property
1029   for ( int anIt = 0; anIt < aProp.GetNumber(); ++anIt )
1030   {
1031     QString aName  = aProp.GetName( anIt );
1032     bool isVisible = aProp.IsVisible( anIt );
1033     int aType      = aProp.GetType( anIt );
1034
1035     QTreeWidgetItem* anItem = new QTreeWidgetItem;
1036     anItem->setText( 0, aName );
1037     anItem->setCheckState( 0, isVisible ? Qt::Checked : Qt::Unchecked );
1038     anItem->setData( 0, Qt::UserRole, anIt );
1039     anItem->setFlags( Qt::ItemIsUserCheckable
1040                     | Qt::ItemIsSelectable
1041                     | Qt::ItemIsEditable
1042                     | Qt::ItemIsEnabled );
1043
1044     switch ( aType )
1045     {
1046       case GEOMGUI_DimensionProperty::DimensionType_Length   : aDistItems << anItem; break;
1047       case GEOMGUI_DimensionProperty::DimensionType_Diameter : aDiamItems << anItem; break;
1048       case GEOMGUI_DimensionProperty::DimensionType_Angle    : anAngItems << anItem; break;
1049     }
1050   }
1051
1052   aDistanceGroupItem->addChildren( aDistItems );
1053   aDiameterGroupItem->addChildren( aDiamItems );
1054   anAngleGroupItem->addChildren( anAngItems );
1055
1056   aListView->setItemHidden( aDistanceGroupItem, aDistItems.isEmpty() );
1057   aListView->setItemHidden( aDiameterGroupItem, aDiamItems.isEmpty() );
1058   aListView->setItemHidden( anAngleGroupItem,   anAngItems.isEmpty() );
1059 }
1060
1061 //=================================================================================
1062 // function : HasUnsavedChanges
1063 // purpose  :
1064 //=================================================================================
1065 bool MeasureGUI_ManageDimensionsDlg::HasUnsavedChanges()
1066 {
1067   if ( myEditObject.isNull() )
1068   {
1069     return false;
1070   }
1071
1072   SalomeApp_Application* anApp = myGeomGUI->getApp();
1073   if ( !anApp )
1074   {
1075     return false;
1076   }
1077
1078   SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( anApp->activeStudy() );
1079   if ( !aStudy )
1080   {
1081     return false;
1082   }
1083
1084   GEOMGUI_DimensionProperty aCurrentState =
1085     aStudy->getObjectProperty( GEOM::sharedPropertiesId(),
1086                                myEditObject->GetStudyEntry(),
1087                                GEOM::propertyName( GEOM::Dimensions ),
1088                                QVariant() )
1089                                .value<GEOMGUI_DimensionProperty>();
1090
1091   return aCurrentState != mySavedPropertyState;
1092 }
1093
1094 //=================================================================================
1095 // function : AllowedToCancelChanges
1096 // purpose  :
1097 //=================================================================================
1098 bool MeasureGUI_ManageDimensionsDlg::AllowedToCancelChanges()
1099 {
1100   if ( !HasUnsavedChanges() )
1101   {
1102     return true;
1103   }
1104
1105   int aResponse = SUIT_MessageBox::warning( this,
1106                                             tr( "WRN_TITLE_UNSAVED" ),
1107                                             tr( "WRN_MSG_CHANGES_LOST" ),
1108                                             QMessageBox::Ok,
1109                                             QMessageBox::Cancel );
1110
1111   return aResponse == QMessageBox::Ok;
1112 }
1113
1114 //=================================================================================
1115 // function : AllowedToSaveChanges
1116 // purpose  :
1117 //=================================================================================
1118 bool MeasureGUI_ManageDimensionsDlg::AllowedToSaveChanges()
1119 {
1120   if ( !HasUnsavedChanges() )
1121   {
1122     return false;
1123   }
1124
1125   int aResponse = SUIT_MessageBox::warning( this,
1126                                             tr( "WRN_TITLE_UNSAVED" ),
1127                                             tr( "WRN_MSG_CHANGES_SAVE" ),
1128                                             QMessageBox::Ok,
1129                                             QMessageBox::Cancel );
1130
1131   return aResponse == QMessageBox::Ok;
1132 }
1133
1134 //=================================================================================
1135 // function : IdFromItem
1136 // purpose  :
1137 //=================================================================================
1138 int MeasureGUI_ManageDimensionsDlg::IdFromItem( QTreeWidgetItem* theItem )
1139 {
1140   if ( !theItem )
1141   {
1142     return -1;
1143   }
1144
1145   bool isIdOK = false;
1146   const int anId = theItem->data( 0, Qt::UserRole ).toInt( &isIdOK );
1147
1148   return isIdOK ? anId : -1;
1149 }
1150
1151 //=================================================================================
1152 // function : IdFromPrs
1153 // purpose  :
1154 //=================================================================================
1155 int MeasureGUI_ManageDimensionsDlg::IdFromPrs( const Handle(AIS_InteractiveObject)& theAIS )
1156 {
1157   Handle(SALOME_InteractiveObject) anIO = Handle(SALOME_InteractiveObject)::DownCast( theAIS->GetOwner() );
1158
1159   if ( anIO.IsNull() )
1160   {
1161     return -1;
1162   }
1163
1164   QString anIOEntry   = anIO->getEntry();
1165   QString anEditEntry = myEditObject->GetStudyEntry();
1166   if ( anIOEntry != anEditEntry )
1167   {
1168     return -1;
1169   }
1170
1171   Handle(GEOM_AISLength) aLength = Handle(GEOM_AISLength)::DownCast( theAIS );
1172   if ( !aLength.IsNull() )
1173   {
1174     return aLength->GetId();
1175   }
1176
1177   Handle(GEOM_AISDiameter) aDiameter = Handle(GEOM_AISDiameter)::DownCast( theAIS );
1178   if ( !aDiameter.IsNull() )
1179   {
1180     return aDiameter->GetId();
1181   }
1182
1183   Handle(GEOM_AISAngle) anAngle = Handle(GEOM_AISAngle)::DownCast( theAIS );
1184   if ( !anAngle.IsNull() )
1185   {
1186     return anAngle->GetId();
1187   }
1188
1189   return -1;
1190 }
1191
1192 //=================================================================================
1193 // function : SelectInList
1194 // purpose  :
1195 //=================================================================================
1196 void MeasureGUI_ManageDimensionsDlg::SelectInList( const Handle(AIS_InteractiveObject)& theIO )
1197 {
1198   SelectInList( IdFromPrs( theIO ) );
1199 }
1200
1201 //=================================================================================
1202 // function : SelectInList
1203 // purpose  :
1204 //=================================================================================
1205 void MeasureGUI_ManageDimensionsDlg::SelectInList( const int theId )
1206 {
1207   QTreeWidget* aListOfDimension = myDimensionView->TreeWidget;
1208
1209   QAbstractItemModel* aModel = aListOfDimension->model();
1210   QModelIndexList aIndexes = aModel->match( aModel->index(0, 0),
1211                                             Qt::UserRole,
1212                                             QVariant( theId ),
1213                                             -1, Qt::MatchRecursive );
1214
1215   if ( aIndexes.isEmpty() )
1216   {
1217     aListOfDimension->setCurrentIndex( QModelIndex() );
1218     return;
1219   }
1220
1221   bool isBlocked = aListOfDimension->blockSignals( true );
1222   aListOfDimension->setCurrentIndex( aIndexes.first() );
1223   aListOfDimension->blockSignals( isBlocked );
1224 }
1225
1226 //=================================================================================
1227 // function : SelectInViewer
1228 // purpose  :
1229 //=================================================================================
1230 void MeasureGUI_ManageDimensionsDlg::SelectInViewer( SOCC_Viewer* theViewer, const int theId )
1231 {
1232   Handle(AIS_InteractiveContext) anAISContext = theViewer->getAISContext();
1233   if ( anAISContext.IsNull() )
1234   {
1235     return;
1236   }
1237
1238   Standard_Boolean isLocal = anAISContext->HasOpenedContext();
1239   if ( isLocal )
1240   {
1241     anAISContext->ClearSelected( Standard_False );
1242   }
1243   else
1244   {
1245     anAISContext->ClearCurrents( Standard_False );
1246   }
1247
1248   SOCC_Prs* aPrs = dynamic_cast<SOCC_Prs*>( theViewer->CreatePrs( myEditObject->GetStudyEntry() ) );
1249
1250   AIS_ListOfInteractive aListOfIO;
1251   aPrs->GetObjects( aListOfIO );
1252
1253   AIS_ListIteratorOfListOfInteractive anIt( aListOfIO );
1254   for ( ; anIt.More(); anIt.Next() )
1255   {
1256     const Handle(AIS_InteractiveObject)& anIO = anIt.Value();
1257     if ( IdFromPrs( anIO ) != theId )
1258     {
1259       if ( isLocal )
1260       {
1261         anAISContext->Deactivate( anIO, AIS_DSM_Line );
1262         anAISContext->Deactivate( anIO, AIS_DSM_Text );
1263       }
1264       continue;
1265     }
1266
1267     if ( isLocal )
1268     {
1269       anAISContext->AddOrRemoveSelected( anIO, Standard_False );
1270       anAISContext->Activate( anIO, AIS_DSM_Line );
1271       anAISContext->Activate( anIO, AIS_DSM_Text );
1272     }
1273     else
1274     {
1275       anAISContext->AddOrRemoveCurrentObject( anIO, Standard_False );
1276     }
1277
1278     anAISContext->UpdateCurrentViewer();
1279   }
1280 }
1281
1282 //=================================================================================
1283 // function : RedisplayObject
1284 // purpose  :
1285 //=================================================================================
1286 void MeasureGUI_ManageDimensionsDlg::RedisplayObject()
1287 {
1288   redisplay( myEditObject.get() );
1289
1290   if ( !myOperatedViewer )
1291   {
1292     return;
1293   }
1294
1295   QTreeWidgetItem* aSelectedItem = myDimensionView->TreeWidget->currentItem();
1296   if ( !aSelectedItem )
1297   {
1298     return;
1299   }
1300
1301   StartSelection( myCurrentSelection );
1302
1303   SelectInViewer( myOperatedViewer, IdFromItem( aSelectedItem ) );
1304 }