Salome HOME
591d52b2810bbb8374c6cb8d917dca71ea9dd953
[modules/geom.git] / src / OperationGUI / OperationGUI_TransferDataDlg.cxx
1 // Copyright (C) 2007-2020  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "OperationGUI_TransferDataDlg.h"
21
22 #include <DlgRef.h>
23 #include <GeometryGUI.h>
24 #include <GEOMBase.h>
25
26 #include <SUIT_Desktop.h>
27 #include <SUIT_MessageBox.h>
28 #include <SUIT_Session.h>
29 #include <SUIT_ResourceMgr.h>
30 #include <SalomeApp_Application.h>
31 #include <LightApp_SelectionMgr.h>
32
33 //==============================================================================
34 // class    : OperationGUI_TransferDataDlg()
35 // purpose  : Constructs a OperationGUI_TransferDataDlg which is a child of
36 //            'parent'.
37 //=================================================================================
38 OperationGUI_TransferDataDlg::OperationGUI_TransferDataDlg (GeometryGUI* theGeometryGUI,
39                                                             QWidget* parent)
40   : GEOMBase_Skeleton(theGeometryGUI, parent, false),
41     myGroup(NULL)
42 {
43   SUIT_ResourceMgr* aResMgr = myGeomGUI->getApp()->resourceMgr();
44   QPixmap image0 (aResMgr->loadPixmap("GEOM", tr("ICON_DLG_TRANSFER_DATA")));
45   QPixmap image1 (aResMgr->loadPixmap("GEOM", tr("ICON_SELECT")));
46
47   setWindowTitle(tr("GEOM_TRANSFER_DATA_TITLE"));
48
49   /***************************************************************/
50   mainFrame()->GroupConstructors->setTitle(tr("GEOM_TRANSFER_DATA"));
51   mainFrame()->RadioButton1->setIcon(image0);
52   mainFrame()->RadioButton2->setAttribute(Qt::WA_DeleteOnClose);
53   mainFrame()->RadioButton2->close();
54   mainFrame()->RadioButton3->setAttribute(Qt::WA_DeleteOnClose);
55   mainFrame()->RadioButton3->close();
56
57   mainFrame()->GroupBoxName->hide();
58
59   myGroup = new DlgRef_2Sel1List(centralWidget());
60   myGroup->GroupBox1->setTitle(tr("GEOM_PARAMETERS"));
61   myGroup->TextLabel1->setText(tr("GEOM_TRANSFER_DATA_FROM"));
62   myGroup->TextLabel2->setText(tr("GEOM_TRANSFER_DATA_TO"));
63   myGroup->TextLabel3->setText(tr("GEOM_TRANSFER_DATA_METHOD"));
64   myGroup->PushButton1->setIcon(image1);
65   myGroup->PushButton2->setIcon(image1);
66   myGroup->LineEdit1->setReadOnly(true);
67   myGroup->LineEdit2->setReadOnly(true);
68
69   QVBoxLayout* layout = new QVBoxLayout(centralWidget());
70   layout->setMargin(0); layout->setSpacing(6);
71   layout->addWidget(myGroup);
72   /***************************************************************/
73
74   setHelpFileName("transfer_data_page.html");
75
76   Init();
77 }
78
79 //=================================================================================
80 // function : ~OperationGUI_TransferDataDlg
81 // purpose  : Destroys the object and frees any allocated resources
82 //=================================================================================
83 OperationGUI_TransferDataDlg::~OperationGUI_TransferDataDlg()
84 {
85 }
86
87 //=================================================================================
88 // function : Init()
89 // purpose  :
90 //=================================================================================
91 void OperationGUI_TransferDataDlg::Init()
92 {
93   // type for detection
94   myGroup->ComboBox1->addItem(tr("GEOM_TD_METHOD_GETINPLACE"));
95   myGroup->ComboBox1->addItem(tr("GEOM_TD_METHOD_GETINPLACE_OLD"));
96   myGroup->ComboBox1->addItem(tr("GEOM_TD_METHOD_GETINPLACE_HISTORY"));
97
98   myGroup->LineEdit1->clear();
99   myGroup->LineEdit2->clear();
100   myObject1.nullify();
101   myObject2.nullify();
102
103   // signals and slots connections
104   connect(buttonOk(),    SIGNAL(clicked()), this, SLOT(ClickOnOk()));
105   connect(buttonApply(), SIGNAL(clicked()), this, SLOT(ClickOnApply()));
106
107   connect(myGroup->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
108   connect(myGroup->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
109
110   connect(myGroup->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
111   connect(myGroup->LineEdit2, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
112
113   connect(myGroup->ComboBox1, SIGNAL(activated(int)), this, SLOT(ComboTextChanged()));
114
115   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
116           this, SLOT(SelectionIntoArgument()));
117
118   myGroup->TextLabel3->show();
119   myGroup->ComboBox1->show();
120   myGroup->ComboBox1->setCurrentIndex(0);
121
122   globalSelection(GEOM_ALLSHAPES);
123
124   myGroup->PushButton1->click();
125   SelectionIntoArgument();
126   resize(100,100);
127 }
128
129 //=================================================================================
130 // function : ClickOnOk()
131 // purpose  :
132 //=================================================================================
133 void OperationGUI_TransferDataDlg::ClickOnOk()
134 {
135   setIsApplyAndClose( true );
136   if (ClickOnApply())
137     ClickOnCancel();
138 }
139
140 //=================================================================================
141 // function : ClickOnApply()
142 // purpose  :
143 //=================================================================================
144 bool OperationGUI_TransferDataDlg::ClickOnApply()
145 {
146   if (!onAccept(false))
147     return false;
148   // activate first line edit
149   myGroup->PushButton1->click();
150   return true;
151 }
152
153 //=================================================================================
154 // function : SelectionIntoArgument()
155 // purpose  : Called when selection is changed or on dialog initialization or activation
156 //=================================================================================
157 void OperationGUI_TransferDataDlg::SelectionIntoArgument()
158 {
159   GEOM::GeomObjPtr aSelectedObject = getSelected(TopAbs_SHAPE);
160
161   if (aSelectedObject) {
162     myEditCurrentArgument->setText(GEOMBase::GetName(aSelectedObject.get()));
163
164     if (myEditCurrentArgument == myGroup->LineEdit1) {
165       myObject1 = aSelectedObject;
166
167       if (!myObject2) {
168         myGroup->PushButton2->click();
169       }
170     } else if (myEditCurrentArgument == myGroup->LineEdit2) {
171       myObject2 = aSelectedObject;
172
173       if (!myObject1) {
174         myGroup->PushButton1->click();
175       }
176     }
177   } else {
178     myEditCurrentArgument->setText("");
179
180     if (myEditCurrentArgument == myGroup->LineEdit1) {
181       myObject1.nullify();
182     } else if (myEditCurrentArgument == myGroup->LineEdit2) {
183       myObject2.nullify();
184     }
185   }
186 }
187
188 //=================================================================================
189 // function : SetEditCurrentArgument()
190 // purpose  :
191 //=================================================================================
192 void OperationGUI_TransferDataDlg::SetEditCurrentArgument()
193 {
194   QPushButton* send = (QPushButton*)sender();
195
196   if      (send == myGroup->PushButton1) {
197     myEditCurrentArgument = myGroup->LineEdit1;
198
199     myGroup->PushButton2->setDown(false);
200     myGroup->LineEdit2->setEnabled(false);
201   }
202   else if (send == myGroup->PushButton2) {
203     myEditCurrentArgument = myGroup->LineEdit2;
204
205     myGroup->PushButton1->setDown(false);
206     myGroup->LineEdit1->setEnabled(false);
207   }
208
209   // enable line edit
210   myEditCurrentArgument->setEnabled(true);
211   myEditCurrentArgument->setFocus();
212   // after setFocus(), because it will be setDown(false) when loses focus
213   send->setDown(true);
214 }
215
216 //=================================================================================
217 // function : LineEditReturnPressed()
218 // purpose  :
219 //=================================================================================
220 void OperationGUI_TransferDataDlg::LineEditReturnPressed()
221 {
222   QLineEdit* send = (QLineEdit*)sender();
223   if (send == myGroup->LineEdit1 || send == myGroup->LineEdit2)
224   {
225     myEditCurrentArgument = send;
226     GEOMBase_Skeleton::LineEditReturnPressed();
227   }
228 }
229
230 //=================================================================================
231 // function : ActivateThisDialog()
232 // purpose  :
233 //=================================================================================
234 void OperationGUI_TransferDataDlg::ActivateThisDialog()
235 {
236   GEOMBase_Skeleton::ActivateThisDialog();
237   globalSelection(GEOM_ALLSHAPES);
238   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
239            this, SLOT( SelectionIntoArgument() ) );
240 }
241
242 //=================================================================================
243 // function : enterEvent()
244 // purpose  : when mouse enter onto the QWidget
245 //=================================================================================
246 void OperationGUI_TransferDataDlg::enterEvent (QEvent*)
247 {
248   if (!mainFrame()->GroupConstructors->isEnabled())
249     ActivateThisDialog();
250 }
251
252 //=================================================================================
253 // function : createOperation
254 // purpose  :
255 //=================================================================================
256 GEOM::GEOM_IOperations_ptr OperationGUI_TransferDataDlg::createOperation()
257 {
258   return getGeomEngine()->GetIInsertOperations();
259 }
260
261 //=================================================================================
262 // function : isValid
263 // purpose  :
264 //=================================================================================
265 bool OperationGUI_TransferDataDlg::isValid(QString&)
266 {
267   return myObject1 && myObject2;
268 }
269
270 //=================================================================================
271 // function : execute
272 // purpose  :
273 //=================================================================================
274 bool OperationGUI_TransferDataDlg::execute (ObjectList& objects)
275 {
276   const int aLimit = myGroup->ComboBox1->currentIndex();
277   GEOM::find_shape_method aMethod;
278
279   switch (aLimit)
280   {
281   case 0:
282     aMethod = GEOM::FSM_GetInPlace;
283     break;
284   case 1:
285     aMethod = GEOM::FSM_GetInPlace_Old;
286     break;
287   case 2:
288   default:
289     aMethod = GEOM::FSM_GetInPlaceByHistory;
290     break;
291   }
292
293   GEOM::GEOM_IInsertOperations_var anOper =
294     GEOM::GEOM_IInsertOperations::_narrow(getOperation());
295   GEOM::GEOM_IInsertOperations::ListOfTransferDatum_var aResult;
296
297   bool isOK = anOper->TransferData
298     (myObject1.get(), myObject2.get(), aMethod, aResult);
299
300   if (isOK) {
301     // Construct info message.
302     QString aMsg;
303     int aNbTypes = aResult->length();
304
305     if (aNbTypes == 0) {
306       aMsg = tr("GEOM_TRANSFER_DATA_NOT_COPIED");
307     } else {
308       aMsg = tr("GEOM_TRANSFER_DATA_COPIED");
309       aMsg += "\n";
310       int i;
311
312       for (i = 0; i < aNbTypes; i++ ) {
313         if (i > 0) {
314           aMsg += "\n";
315         }
316
317         aMsg += tr(aResult[i].myName).
318                   arg(aResult[i].myNumber).arg(aResult[i].myMaxNumber);
319       }
320     }
321
322     // Show info message.
323     SUIT_MessageBox::information
324             (SUIT_Session::session()->activeApplication()->desktop(),
325              tr("GEOM_TRANSFER_DATA_INFO"), aMsg, tr("BUT_OK"));
326
327     objects.push_back(myObject2.copy());
328   }
329
330   return isOK;
331 }
332
333 //=================================================================================
334 // function : getSourceObjects
335 // purpose  : virtual method to get source objects
336 //=================================================================================
337 QList<GEOM::GeomObjPtr> OperationGUI_TransferDataDlg::getSourceObjects()
338 {
339   QList<GEOM::GeomObjPtr> res;
340   res << myObject1;
341   return res;
342 }