Salome HOME
Correct wrong addition
[modules/geom.git] / src / BuildGUI / BuildGUI_CompoundDlg.cxx
1 //  Copyright (C) 2007-2010  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 //  GEOM GEOMGUI : GUI for Geometry component
24 // File   : BuildGUI_CompoundDlg.cxx
25 // Author : Lucien PIGNOLONI, Open CASCADE S.A.S.
26 //
27 #include "BuildGUI_CompoundDlg.h"
28
29 #include <DlgRef.h>
30 #include <GeometryGUI.h>
31 #include <GEOMBase.h>
32
33 #include <GEOMImpl_Types.hxx>
34
35 #include <SUIT_ResourceMgr.h>
36 #include <SUIT_Session.h>
37 #include <SalomeApp_Application.h>
38 #include <LightApp_SelectionMgr.h>
39
40 //=================================================================================
41 // class    : BuildGUI_CompoundDlg()
42 // purpose  : Constructs a BuildGUI_CompoundDlg which is a child of 'parent', with the 
43 //            name 'name' and widget flags set to 'f'.
44 //            The dialog will by default be modeless, unless you set 'modal' to
45 //            TRUE to construct a modal dialog.
46 //=================================================================================
47 BuildGUI_CompoundDlg::BuildGUI_CompoundDlg( GeometryGUI* theGeometryGUI, QWidget* parent )
48   : GEOMBase_Skeleton( theGeometryGUI, parent )
49 {
50   QPixmap image0( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_BUILD_COMPOUND" ) ) );
51   QPixmap image1( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_SELECT" ) ) );
52
53   setWindowTitle( tr( "GEOM_COMPOUND_TITLE" ) );
54
55   /***************************************************************/
56   mainFrame()->GroupConstructors->setTitle( tr( "GEOM_COMPOUND" ) );
57   mainFrame()->RadioButton1->setIcon( image0 );
58   mainFrame()->RadioButton2->setAttribute( Qt::WA_DeleteOnClose );
59   mainFrame()->RadioButton2->close();
60   mainFrame()->RadioButton3->setAttribute( Qt::WA_DeleteOnClose );
61   mainFrame()->RadioButton3->close();
62
63   GroupShapes = new DlgRef_1Sel( centralWidget() );
64
65   GroupShapes->GroupBox1->setTitle( tr( "GEOM_ARGUMENTS" ) );
66   GroupShapes->TextLabel1->setText( tr( "GEOM_OBJECTS" ) );
67   GroupShapes->PushButton1->setIcon( image1 );
68   GroupShapes->LineEdit1->setReadOnly( true );
69
70   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
71   layout->setMargin( 0 ); layout->setSpacing( 6 );
72   layout->addWidget( GroupShapes );
73   /***************************************************************/
74
75   setHelpFileName("create_compound_page.html");
76
77   /* Initialisations */
78   Init();
79 }
80
81
82 //=================================================================================
83 // function : ~BuildGUI_CompoundDlg()
84 // purpose  : Destroys the object and frees any allocated resources
85 //=================================================================================
86 BuildGUI_CompoundDlg::~BuildGUI_CompoundDlg()
87 {
88   // no need to delete child widgets, Qt does it all for us
89 }
90
91
92 //=================================================================================
93 // function : Init()
94 // purpose  :
95 //=================================================================================
96 void BuildGUI_CompoundDlg::Init()
97 {
98   /* init variables */
99   myEditCurrentArgument = GroupShapes->LineEdit1;
100   GroupShapes->LineEdit1->setReadOnly( true );
101   
102   myShapes.clear();
103
104   mainFrame()->GroupBoxPublish->show();
105
106   /* signals and slots connections */
107   connect( buttonOk(),    SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
108   connect( buttonApply(), SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
109   connect( GroupShapes->PushButton1, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
110   connect( ( (SalomeApp_Application*)(SUIT_Session::session()->activeApplication() ) )->selectionMgr(), 
111            SIGNAL(currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
112
113   globalSelection( GEOM_ALLSHAPES );
114
115   initName( tr( "GEOM_COMPOUND" ) );
116   SelectionIntoArgument();
117 }
118
119
120 //=================================================================================
121 // function : ClickOnOk()
122 // purpose  :
123 //=================================================================================
124 void BuildGUI_CompoundDlg::ClickOnOk()
125 {
126   if ( ClickOnApply() )
127     ClickOnCancel();
128 }
129
130
131 //=================================================================================
132 // function : ClickOnApply()
133 // purpose  :
134 //=================================================================================
135 bool BuildGUI_CompoundDlg::ClickOnApply()
136 {
137   if ( !onAccept() )
138     return false;
139
140   initName();
141   return true;
142 }
143
144
145 //=================================================================================
146 // function : SelectionIntoArgument()
147 // purpose  : Called when selection as changed or other case
148 //=================================================================================
149 void BuildGUI_CompoundDlg::SelectionIntoArgument()
150 {
151   myEditCurrentArgument->setText( "" );
152
153   myShapes = getSelected( TopAbs_SHAPE, -1 );
154
155   if ( !myShapes.isEmpty() ) {
156     QString aName = myShapes.count() > 1 ? QString( "%1_objects").arg( myShapes.count() ) : GEOMBase::GetName( myShapes[0].get() );
157     myEditCurrentArgument->setText( aName );
158   }
159 }
160
161
162 //=================================================================================
163 // function : SetEditCurrentArgument()
164 // purpose  :
165 //=================================================================================
166 void BuildGUI_CompoundDlg::SetEditCurrentArgument()
167 {
168   QPushButton* send = (QPushButton*)sender();
169   if ( send != GroupShapes->PushButton1 )
170     return;
171   
172   myEditCurrentArgument = GroupShapes->LineEdit1;
173   
174   myEditCurrentArgument->setFocus();
175   SelectionIntoArgument();
176 }
177
178
179 //=================================================================================
180 // function : ActivateThisDialog()
181 // purpose  :
182 //=================================================================================
183 void BuildGUI_CompoundDlg::ActivateThisDialog()
184 {
185   GEOMBase_Skeleton::ActivateThisDialog();
186   globalSelection( GEOM_ALLSHAPES );
187   connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(),
188            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
189 }
190
191
192 //=================================================================================
193 // function : enterEvent()
194 // purpose  :
195 //=================================================================================
196 void BuildGUI_CompoundDlg::enterEvent(QEvent* e)
197 {
198   if ( !mainFrame()->GroupConstructors->isEnabled() )
199     ActivateThisDialog(); 
200 }
201
202 //=================================================================================
203 // function : createOperation
204 // purpose  :
205 //=================================================================================
206 GEOM::GEOM_IOperations_ptr BuildGUI_CompoundDlg::createOperation()
207 {
208   return getGeomEngine()->GetIShapesOperations( getStudyId() );
209 }
210
211 //=================================================================================
212 // function : isValid
213 // purpose  :
214 //=================================================================================
215 bool BuildGUI_CompoundDlg::isValid( QString& )
216 {
217   return !myShapes.isEmpty();
218 }
219
220 //=================================================================================
221 // function : execute
222 // purpose  :
223 //=================================================================================
224 bool BuildGUI_CompoundDlg::execute( ObjectList& objects )
225 {
226   GEOM::GEOM_IShapesOperations_var anOper = GEOM::GEOM_IShapesOperations::_narrow( getOperation() );
227
228   GEOM::ListOfGO_var objlist = new GEOM::ListOfGO();
229   objlist->length( myShapes.count() );
230   for ( int i = 0; i < myShapes.count(); i++ )
231     objlist[i] = myShapes[i].copy();
232
233   GEOM::GEOM_Object_var anObj = anOper->MakeCompound( objlist );
234
235   if ( !anObj->_is_nil() )
236     objects.push_back( anObj._retn() );
237
238   return true;
239 }
240
241 //=================================================================================
242 // function : restoreSubShapes
243 // purpose  :
244 //=================================================================================
245 void BuildGUI_CompoundDlg::restoreSubShapes( SALOMEDS::Study_ptr   theStudy,
246                                              SALOMEDS::SObject_ptr theSObject )
247 {
248   if ( mainFrame()->CheckBoxRestoreSS->isChecked() ) {
249     // empty list of arguments means that all arguments should be restored
250     getGeomEngine()->RestoreSubShapesSO( theStudy, theSObject, GEOM::ListOfGO(),
251                                          /*theFindMethod=*/GEOM::FSM_GetInPlace, // ? GEOM::FSM_GetSame
252                                          /*theInheritFirstArg=*/false,
253                                          mainFrame()->CheckBoxAddPrefix->isChecked() );
254   }
255 }