Salome HOME
Issues #2173 Can't set length on several edges when property panel is undocked,
[modules/shaper.git] / src / XGUI / XGUI_PropertyPanel.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 /*
4  * XGUI_PropertyPanel.cpp
5  *
6  *  Created on: Apr 29, 2014
7  *      Author: sbh
8  */
9
10 #include <XGUI_PropertyPanel.h>
11 #include <XGUI_ActionsMgr.h>
12 #include <XGUI_OperationMgr.h>
13 //#include <AppElements_Constants.h>
14 #include <ModuleBase_WidgetMultiSelector.h>
15 #include <ModuleBase_Tools.h>
16 #include <ModuleBase_PageBase.h>
17 #include <ModuleBase_PageWidget.h>
18 #include <ModuleBase_WidgetFactory.h>
19 #include <ModuleBase_OperationDescription.h>
20 #include <ModuleBase_Events.h>
21
22 #include <Events_Loop.h>
23
24 #include <ModelAPI_Session.h>
25 #include <ModelAPI_Validator.h>
26
27 #include <QEvent>
28 #include <QFrame>
29 #include <QIcon>
30 #include <QKeyEvent>
31 #include <QLayoutItem>
32 #include <QToolButton>
33 #include <QVBoxLayout>
34 #include <QGridLayout>
35 #include <QWidget>
36 #include <QToolButton>
37 #include <QAction>
38
39 #ifdef _DEBUG
40 #include <iostream>
41 #endif
42
43 //#define DEBUG_TAB_WIDGETS
44
45 //#define DEBUG_ACTIVE_WIDGET
46
47 XGUI_PropertyPanel::XGUI_PropertyPanel(QWidget* theParent, XGUI_OperationMgr* theMgr)
48     : ModuleBase_IPropertyPanel(theParent),
49     myActiveWidget(NULL),
50     myPreselectionWidget(NULL),
51     myPanelPage(NULL),
52     myOperationMgr(theMgr)
53 {
54   setWindowTitle(tr("Property Panel"));
55   QAction* aViewAct = toggleViewAction();
56   setObjectName(PROP_PANEL);
57   setStyleSheet("::title { position: relative; padding-left: 5px; text-align: left center }");
58
59   QWidget* aContent = new QWidget(this);
60   QGridLayout* aMainLayout = new QGridLayout(aContent);
61   const int kPanelColumn = 0;
62   int aPanelRow = 0;
63   aMainLayout->setContentsMargins(3, 3, 3, 3);
64   setWidget(aContent);
65
66   QFrame* aFrm = new QFrame(aContent);
67   aFrm->setFrameStyle(QFrame::Raised);
68   aFrm->setFrameShape(QFrame::Panel);
69   QHBoxLayout* aBtnLay = new QHBoxLayout(aFrm);
70   ModuleBase_Tools::zeroMargins(aBtnLay);
71   aMainLayout->addWidget(aFrm, aPanelRow++, kPanelColumn);
72
73   myHeaderWidget = aFrm;
74
75   QStringList aBtnNames;
76   aBtnNames << QString(PROP_PANEL_HELP)
77             << QString(PROP_PANEL_OK)
78             << QString(PROP_PANEL_CANCEL);
79   foreach(QString eachBtnName, aBtnNames) {
80     QToolButton* aBtn = new QToolButton(aFrm);
81     aBtn->setObjectName(eachBtnName);
82     aBtn->setAutoRaise(true);
83     aBtnLay->addWidget(aBtn);
84   }
85   aBtnLay->insertStretch(1, 1);
86
87   myPanelPage = new ModuleBase_PageWidget(aContent);
88   myPanelPage->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::MinimumExpanding);
89   aMainLayout->addWidget(myPanelPage, aPanelRow, kPanelColumn);
90
91   // spit to make the preview button on the bottom of the panel
92   aMainLayout->setRowStretch(aPanelRow++, 1);
93
94   // preview button on the bottom of panel
95   aFrm = new QFrame(aContent);
96   aBtnLay = new QHBoxLayout(aFrm);
97   aBtnLay->addStretch(1);
98   ModuleBase_Tools::zeroMargins(aBtnLay);
99   aMainLayout->addWidget(aFrm, aPanelRow++, kPanelColumn);
100
101   QToolButton* aBtn = new QToolButton(aFrm);
102   aBtn->setObjectName(PROP_PANEL_PREVIEW);
103   aBtnLay->addWidget(aBtn);
104 }
105
106 XGUI_PropertyPanel::~XGUI_PropertyPanel()
107 {
108 }
109
110 void XGUI_PropertyPanel::cleanContent()
111 {
112   if (myActiveWidget)
113     myActiveWidget->deactivate();
114
115   /// as the widgets are deleted later, it is important that the signals
116   /// of these widgets are not processed. An example of the error is issue 986.
117   /// In the given case, the property panel is firstly filled by new widgets
118   /// of restarted operation and after that the mouse release signal come from
119   /// the widget of the previous operation (Point2d widget about mouse is released
120   /// and focus is out of this widget)
121   QList<ModuleBase_ModelWidget*>::const_iterator anIt = myWidgets.begin(),
122                                                  aLast = myWidgets.end();
123   for (; anIt != aLast; anIt++) {
124     QWidget* aWidget = *anIt;
125     if (aWidget) {
126       aWidget->blockSignals(true);
127     }
128   }
129
130   myWidgets.clear();
131   myPanelPage->clearPage();
132   myActiveWidget = NULL;
133 #ifdef DEBUG_ACTIVE_WIDGET
134   std::cout << "myActiveWidget = NULL" << std::endl;
135 #endif
136
137   findButton(PROP_PANEL_PREVIEW)->setVisible(false); /// by default it is hidden
138   setWindowTitle(tr("Property Panel"));
139 }
140
141 void XGUI_PropertyPanel::setModelWidgets(const QList<ModuleBase_ModelWidget*>& theWidgets)
142 {
143   myWidgets = theWidgets;
144   if (theWidgets.empty()) return;
145   foreach (ModuleBase_ModelWidget* aWidget, theWidgets) {
146     connect(aWidget, SIGNAL(focusInWidget(ModuleBase_ModelWidget*)),
147             this,    SLOT(onFocusInWidget(ModuleBase_ModelWidget*)));
148     connect(aWidget, SIGNAL(focusOutWidget(ModuleBase_ModelWidget*)),
149             this,    SLOT(onActivateNextWidget(ModuleBase_ModelWidget*)));
150     connect(aWidget, SIGNAL(keyReleased(QObject*, QKeyEvent*)),
151             this,    SIGNAL(keyReleased(QObject*, QKeyEvent*)));
152     connect(aWidget, SIGNAL(enterClicked(QObject*)),
153             this,    SIGNAL(enterClicked(QObject*)));
154
155   }
156 }
157
158 const QList<ModuleBase_ModelWidget*>& XGUI_PropertyPanel::modelWidgets() const
159 {
160   return myWidgets;
161 }
162
163 ModuleBase_PageBase* XGUI_PropertyPanel::contentWidget()
164 {
165   return static_cast<ModuleBase_PageBase*>(myPanelPage);
166 }
167
168 void XGUI_PropertyPanel::updateContentWidget(FeaturePtr theFeature)
169 {
170   // Invalid feature case on abort of the operation
171   if (theFeature.get() == NULL)
172     return;
173   if (theFeature->isAction() || !theFeature->data())
174     return;
175   foreach(ModuleBase_ModelWidget* eachWidget, myWidgets) {
176     if (!eachWidget->feature().get())
177       eachWidget->setFeature(theFeature);
178     eachWidget->restoreValue();
179   }
180   // the repaint is used here to immediately react in GUI to the values change.
181   repaint();
182 }
183
184 void XGUI_PropertyPanel::createContentPanel(FeaturePtr theFeature)
185 {
186   // Invalid feature case on abort of the operation
187   if (theFeature.get() == NULL)
188     return;
189   if (theFeature->isAction() || !theFeature->data())
190     return;
191
192   if (myWidgets.empty()) {
193     ModuleBase_Operation* anOperation = myOperationMgr->currentOperation();
194     QString aXmlRepr = anOperation->getDescription()->xmlRepresentation();
195
196     ModuleBase_WidgetFactory aFactory(aXmlRepr.toStdString(), myOperationMgr->workshop());
197     aFactory.createPanel(contentWidget(), theFeature);
198     /// Apply button should be update if the feature was modified by the panel
199     myOperationMgr->onValidateOperation();
200   }
201 }
202
203 void XGUI_PropertyPanel::activateNextWidget(ModuleBase_ModelWidget* theWidget)
204 {
205   // it is possible that the property panel widgets have not been visualized
206   // (e.g. on start operation), so it is strictly important to do not check visualized state
207   activateNextWidget(theWidget, false);
208 }
209
210 void XGUI_PropertyPanel::onFocusInWidget(ModuleBase_ModelWidget* theWidget)
211 {
212 #ifdef DEBUG_ACTIVE_WIDGET
213   std::cout << "onFocusInWidget" << std::endl;
214 #endif
215   if (theWidget->canAcceptFocus())
216     activateWidget(theWidget);
217 }
218
219 void XGUI_PropertyPanel::onActivateNextWidget(ModuleBase_ModelWidget* theWidget)
220 {
221   // this slot happens when some widget lost focus, the next widget which accepts the focus
222   // should be shown, so the second parameter is true
223   // it is important for features where in cases the same attributes are used, isCase for this
224   // attribute returns true, however it can be placed in hidden stack widget(extrusion: elements,
225   // sketch multi rotation -> single/full point)
226   // it is important to check the widget visibility to do not check of the next widget visible
227   // state if the current is not shown. (example: sketch circle re-entrant operation after mouse
228   // release in the viewer, next, radius,
229   // widget should be activated but the first is not visualized)
230   bool isVisible = theWidget->isVisible();
231   activateNextWidget(theWidget, isVisible);
232 }
233
234
235 void XGUI_PropertyPanel::activateNextWidget(ModuleBase_ModelWidget* theWidget,
236                                             const bool isCheckVisibility)
237 {
238   // TO CHECK: Editing operation does not have automatical activation of widgets
239   if (isEditingMode()) {
240     activateWidget(NULL);
241     return;
242   }
243   ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
244
245   QList<ModuleBase_ModelWidget*>::const_iterator anIt = myWidgets.begin(), aLast = myWidgets.end();
246   bool isFoundWidget = false;
247   ModuleBase_Tools::activateWindow(this, "XGUI_PropertyPanel::activateNextWidget()");
248   for (; anIt != aLast; anIt++) {
249     ModuleBase_ModelWidget* aCurrentWidget = *anIt;
250     if (isFoundWidget || !theWidget) {
251
252       if (!aValidators->isCase(aCurrentWidget->feature(), aCurrentWidget->attributeID()))
253         continue; // this attribute does not participate in the current case
254       if (isCheckVisibility && !aCurrentWidget->isInternal()) {
255         if (!aCurrentWidget->isVisible())
256           continue;
257       }
258       if (!aCurrentWidget->isObligatory())
259         continue; // not obligatory widgets are not activated automatically
260
261       if (!aCurrentWidget->canAcceptFocus())
262         continue; // do not set focus if it can not be accepted, case: optional choice
263
264       if (aCurrentWidget->focusTo()) {
265         aCurrentWidget->emitFocusInWidget();
266         return;
267       }
268     }
269     isFoundWidget = isFoundWidget || (*anIt) == theWidget;
270   }
271   // set focus to Ok/Cancel button in Property panel if there are no more active widgets
272   // it should be performed before activateWidget(NULL) because it emits some signals which
273   // can be processed by moudule for example as to activate another widget with setting focus
274   QWidget* aNewFocusWidget = 0;
275   QToolButton* anOkBtn = findButton(PROP_PANEL_OK);
276   if (anOkBtn->isEnabled())
277     aNewFocusWidget = anOkBtn;
278   else {
279     QToolButton* aCancelBtn = findButton(PROP_PANEL_CANCEL);
280     if (aCancelBtn->isEnabled())
281       aNewFocusWidget = aCancelBtn;
282   }
283   if (aNewFocusWidget)
284     ModuleBase_Tools::setFocus(aNewFocusWidget, "XGUI_PropertyPanel::activateNextWidget");
285
286   activateWidget(NULL);
287 }
288
289 void findDirectChildren(QWidget* theParent, QList<QWidget*>& theWidgets, const bool theDebug)
290 {
291   QList<QWidget*> aWidgets;
292
293   if (theParent) {
294     QLayout* aLayout = theParent->layout();
295     if (aLayout) {
296       for (int i = 0, aCount = aLayout->count(); i < aCount; i++) {
297         QLayoutItem* anItem = aLayout->itemAt(i);
298         QWidget* aWidget = anItem ? anItem->widget() : 0;
299         if (aWidget) {
300           if (aWidget->isVisible()) {
301             if (aWidget->focusPolicy() != Qt::NoFocus)
302               theWidgets.append(aWidget);
303             findDirectChildren(aWidget, theWidgets, false);
304           }
305         }
306         else if (anItem->layout()) {
307           QLayout* aLayout = anItem->layout();
308           for (int i = 0, aCount = aLayout->count(); i < aCount; i++) {
309             QLayoutItem* anItem = aLayout->itemAt(i);
310             QWidget* aWidget = anItem ? anItem->widget() : 0;
311             if (aWidget) {
312               if (aWidget->isVisible()) {
313                 if (aWidget->focusPolicy() != Qt::NoFocus)
314                   theWidgets.append(aWidget);
315                 findDirectChildren(aWidget, theWidgets, false);
316               }
317             }
318             else {
319               // TODO: improve recursive search for the case when here QLayout is used
320               // currently, the switch widget uses only 1 level of qlayout in qlayout using for
321               // example for construction plane feature. If there are more levels,
322               // it should be implemented here
323             }
324           }
325         }
326       }
327     }
328   }
329 #ifdef DEBUG_TAB_WIDGETS
330   if (theDebug) {
331     QStringList aWidgetTypes;
332     QList<QWidget*>::const_iterator anIt =  theWidgets.begin(), aLast = theWidgets.end();
333     for (; anIt != aLast; anIt++) {
334       QWidget* aWidget = *anIt;
335       if (aWidget)
336         aWidgetTypes.append(aWidget->objectName());
337     }
338     QString anInfo = QString("theWidgets[%1]: %2")
339       .arg(theWidgets.count()).arg(aWidgetTypes.join(","));
340     qDebug(anInfo.toStdString().c_str());
341   }
342 #endif
343 }
344
345 bool XGUI_PropertyPanel::setFocusNextPrevChild(bool theIsNext)
346 {
347   return focusNextPrevChild(theIsNext);
348 }
349
350 bool XGUI_PropertyPanel::focusNextPrevChild(bool theIsNext)
351 {
352   // it wraps the Tabs clicking to follow in the chain:
353   // controls, last control, Apply, Cancel, first control, controls
354   bool isChangedFocus = false;
355
356   QWidget* aFocusWidget = focusWidget();
357 #ifdef DEBUG_TAB_WIDGETS
358   if (aFocusWidget) {
359     QString anInfo = QString("focus Widget: %1").arg(aFocusWidget->objectName());
360     qDebug(anInfo.toStdString().c_str());
361   }
362 #endif
363   ModuleBase_ModelWidget* aFocusMWidget = ModuleBase_ModelWidget::findModelWidget(this,
364                                                                          aFocusWidget);
365   if (aFocusMWidget)
366     aFocusMWidget->setHighlighted(false);
367
368   QWidget* aNewFocusWidget = 0;
369   if (aFocusWidget) {
370     QList<QWidget*> aChildren;
371     findDirectChildren(this, aChildren, true);
372     int aChildrenCount = aChildren.count();
373     int aFocusWidgetIndex = aChildren.indexOf(aFocusWidget);
374     if (aFocusWidgetIndex >= 0) {
375       if (theIsNext) {
376         if (aFocusWidgetIndex == aChildrenCount-1) {
377           // after the last widget focus should be set to "Apply"
378           QToolButton* anOkBtn = findButton(PROP_PANEL_OK);
379           if (anOkBtn->isEnabled())
380             aNewFocusWidget = anOkBtn;
381           else {
382             QToolButton* aCancelBtn = findButton(PROP_PANEL_CANCEL);
383             if (aCancelBtn->isEnabled())
384               aNewFocusWidget = aCancelBtn;
385           }
386         }
387         else {
388           aNewFocusWidget = aChildren[aFocusWidgetIndex+1];
389         }
390       }
391       else {
392         if (aFocusWidgetIndex == 0) {
393           // before the first widget, the last should accept focus
394           aNewFocusWidget = aChildren[aChildrenCount - 1];
395         }
396         else {
397           // before the "Apply" button, the last should accept focus for consistency with "Next"
398           QToolButton* anOkBtn = findButton(PROP_PANEL_OK);
399           if (aFocusWidget == anOkBtn) {
400             aNewFocusWidget = aChildren[aChildrenCount - 1];
401           }
402           else {
403             aNewFocusWidget = aChildren[aFocusWidgetIndex-1];
404           }
405         }
406       }
407     }
408   }
409   if (aNewFocusWidget) {
410     if (myActiveWidget) {
411       bool isFirstControl = !theIsNext;
412       QWidget* aLastFocusControl = myActiveWidget->getControlAcceptingFocus(isFirstControl);
413       if (aFocusWidget == aLastFocusControl) {
414         setActiveWidget(NULL);
415       }
416     }
417
418     // we want to have property panel as an active window to enter values in double control
419     ModuleBase_Tools::setFocus(aNewFocusWidget, "XGUI_PropertyPanel::focusNextPrevChild()");
420
421     ModuleBase_ModelWidget* aNewFocusMWidget = ModuleBase_ModelWidget::findModelWidget(this,
422                                                                               aNewFocusWidget);
423     if (aNewFocusMWidget)
424       aNewFocusMWidget->emitFocusInWidget();
425     isChangedFocus = true;
426   }
427   return isChangedFocus;
428 }
429
430 void XGUI_PropertyPanel::activateNextWidget()
431 {
432   activateNextWidget(myActiveWidget);
433 }
434
435 void XGUI_PropertyPanel::activateWidget(ModuleBase_ModelWidget* theWidget, const bool theEmitSignal)
436 {
437   std::string aPreviosAttributeID;
438   if(myActiveWidget)
439     aPreviosAttributeID = myActiveWidget->attributeID();
440
441   // Avoid activation of already actve widget. It could happen on focusIn event many times
442   if (setActiveWidget(theWidget) && theEmitSignal) {
443     emit widgetActivated(myActiveWidget);
444     if (!myActiveWidget && !isEditingMode()) {
445       emit noMoreWidgets(aPreviosAttributeID);
446     }
447   }
448 }
449
450 bool XGUI_PropertyPanel::setActiveWidget(ModuleBase_ModelWidget* theWidget)
451 {
452   // Avoid activation of already actve widget. It could happen on focusIn event many times
453   if (theWidget == myActiveWidget) {
454     return false;
455   }
456   std::string aPreviosAttributeID;
457   if(myActiveWidget) {
458     aPreviosAttributeID = myActiveWidget->attributeID();
459     myActiveWidget->processValueState();
460     myActiveWidget->deactivate();
461     myActiveWidget->setHighlighted(false);
462   }
463   if(theWidget) {
464     emit beforeWidgetActivated(theWidget);
465     theWidget->setHighlighted(true);
466     theWidget->activate();
467   }
468   myActiveWidget = theWidget;
469 #ifdef DEBUG_ACTIVE_WIDGET
470   std::cout << "myActiveWidget = " << (theWidget ? theWidget->context().c_str() : "") << std::endl;
471 #endif
472   static Events_ID anEvent = Events_Loop::eventByName(EVENT_UPDATE_BY_WIDGET_SELECTION);
473   Events_Loop::loop()->flush(anEvent);
474
475   return true;
476 }
477
478 void XGUI_PropertyPanel::setFocusOnOkButton()
479 {
480   QToolButton* anOkBtn = findButton(PROP_PANEL_OK);
481   ModuleBase_Tools::setFocus(anOkBtn, "XGUI_PropertyPanel::setFocusOnOkButton()");
482 }
483
484 void XGUI_PropertyPanel::setCancelEnabled(bool theEnabled)
485 {
486   QToolButton* anCancelBtn = findButton(PROP_PANEL_CANCEL);
487   anCancelBtn->setEnabled(theEnabled);
488 }
489
490 bool XGUI_PropertyPanel::isCancelEnabled() const
491 {
492   QToolButton* anCancelBtn = findButton(PROP_PANEL_CANCEL);
493   return anCancelBtn->isEnabled();
494 }
495
496 void XGUI_PropertyPanel::setEditingMode(bool isEditing)
497 {
498   ModuleBase_IPropertyPanel::setEditingMode(isEditing);
499   foreach(ModuleBase_ModelWidget* aWgt, myWidgets) {
500     aWgt->setEditingMode(isEditing);
501   }
502 }
503
504 void XGUI_PropertyPanel::setupActions(XGUI_ActionsMgr* theMgr)
505 {
506   QStringList aButtonNames;
507   aButtonNames << PROP_PANEL_OK << PROP_PANEL_CANCEL << PROP_PANEL_HELP << PROP_PANEL_PREVIEW;
508   QList<XGUI_ActionsMgr::OperationStateActionId> aActionIds;
509   aActionIds << XGUI_ActionsMgr::Accept << XGUI_ActionsMgr::Abort << XGUI_ActionsMgr::Help
510              << XGUI_ActionsMgr::Preview;
511   for (int i = 0; i < aButtonNames.size(); ++i) {
512     QToolButton* aBtn = findButton(aButtonNames.at(i).toStdString().c_str());
513     QAction* anAct = theMgr->operationStateAction(aActionIds.at(i));
514     aBtn->setDefaultAction(anAct);
515   }
516 }
517
518 ModuleBase_ModelWidget* XGUI_PropertyPanel::preselectionWidget() const
519 {
520   return myPreselectionWidget;
521 }
522
523 void XGUI_PropertyPanel::setPreselectionWidget(ModuleBase_ModelWidget* theWidget)
524 {
525   myPreselectionWidget = theWidget;
526 }
527
528
529 void XGUI_PropertyPanel::closeEvent(QCloseEvent* theEvent)
530 {
531   ModuleBase_Operation* aOp = myOperationMgr->currentOperation();
532   if (aOp) {
533     if (myOperationMgr->canStopOperation(aOp)) {
534       myOperationMgr->abortOperation(aOp);
535       if (myOperationMgr->hasOperation())
536         theEvent->ignore();
537       else
538         theEvent->accept();
539     } else
540       theEvent->ignore();
541   } else
542     ModuleBase_IPropertyPanel::closeEvent(theEvent);
543 }
544
545 QToolButton* XGUI_PropertyPanel::findButton(const char* theInternalName) const
546 {
547   return findChild<QToolButton*>(theInternalName);
548 }