]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_PropertyPanel.cpp
Salome HOME
Issue #2169 Segmentation fault when drawing sketch line with undocked property panel
[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         return;
266       }
267     }
268     isFoundWidget = isFoundWidget || (*anIt) == theWidget;
269   }
270   // set focus to Ok/Cancel button in Property panel if there are no more active widgets
271   // it should be performed before activateWidget(NULL) because it emits some signals which
272   // can be processed by moudule for example as to activate another widget with setting focus
273   QWidget* aNewFocusWidget = 0;
274   QToolButton* anOkBtn = findButton(PROP_PANEL_OK);
275   if (anOkBtn->isEnabled())
276     aNewFocusWidget = anOkBtn;
277   else {
278     QToolButton* aCancelBtn = findButton(PROP_PANEL_CANCEL);
279     if (aCancelBtn->isEnabled())
280       aNewFocusWidget = aCancelBtn;
281   }
282   if (aNewFocusWidget)
283     aNewFocusWidget->setFocus(Qt::TabFocusReason);
284
285   activateWidget(NULL);
286 }
287
288 void findDirectChildren(QWidget* theParent, QList<QWidget*>& theWidgets, const bool theDebug)
289 {
290   QList<QWidget*> aWidgets;
291
292   if (theParent) {
293     QLayout* aLayout = theParent->layout();
294     if (aLayout) {
295       for (int i = 0, aCount = aLayout->count(); i < aCount; i++) {
296         QLayoutItem* anItem = aLayout->itemAt(i);
297         QWidget* aWidget = anItem ? anItem->widget() : 0;
298         if (aWidget) {
299           if (aWidget->isVisible()) {
300             if (aWidget->focusPolicy() != Qt::NoFocus)
301               theWidgets.append(aWidget);
302             findDirectChildren(aWidget, theWidgets, false);
303           }
304         }
305         else if (anItem->layout()) {
306           QLayout* aLayout = anItem->layout();
307           for (int i = 0, aCount = aLayout->count(); i < aCount; i++) {
308             QLayoutItem* anItem = aLayout->itemAt(i);
309             QWidget* aWidget = anItem ? anItem->widget() : 0;
310             if (aWidget) {
311               if (aWidget->isVisible()) {
312                 if (aWidget->focusPolicy() != Qt::NoFocus)
313                   theWidgets.append(aWidget);
314                 findDirectChildren(aWidget, theWidgets, false);
315               }
316             }
317             else {
318               // TODO: improve recursive search for the case when here QLayout is used
319               // currently, the switch widget uses only 1 level of qlayout in qlayout using for
320               // example for construction plane feature. If there are more levels,
321               // it should be implemented here
322             }
323           }
324         }
325       }
326     }
327   }
328 #ifdef DEBUG_TAB_WIDGETS
329   if (theDebug) {
330     QStringList aWidgetTypes;
331     QList<QWidget*>::const_iterator anIt =  theWidgets.begin(), aLast = theWidgets.end();
332     for (; anIt != aLast; anIt++) {
333       QWidget* aWidget = *anIt;
334       if (aWidget)
335         aWidgetTypes.append(aWidget->objectName());
336     }
337     QString anInfo = QString("theWidgets[%1]: %2")
338       .arg(theWidgets.count()).arg(aWidgetTypes.join(","));
339     qDebug(anInfo.toStdString().c_str());
340   }
341 #endif
342 }
343
344 bool XGUI_PropertyPanel::focusNextPrevChild(bool theIsNext)
345 {
346   // it wraps the Tabs clicking to follow in the chain:
347   // controls, last control, Apply, Cancel, first control, controls
348   bool isChangedFocus = false;
349
350   QWidget* aFocusWidget = focusWidget();
351 #ifdef DEBUG_TAB_WIDGETS
352   if (aFocusWidget) {
353     QString anInfo = QString("focus Widget: %1").arg(aFocusWidget->objectName());
354     qDebug(anInfo.toStdString().c_str());
355   }
356 #endif
357
358   QWidget* aNewFocusWidget = 0;
359   if (aFocusWidget) {
360     QList<QWidget*> aChildren;
361     findDirectChildren(this, aChildren, true);
362     int aChildrenCount = aChildren.count();
363     int aFocusWidgetIndex = aChildren.indexOf(aFocusWidget);
364     if (aFocusWidgetIndex >= 0) {
365       if (theIsNext) {
366         if (aFocusWidgetIndex == aChildrenCount-1) {
367           // after the last widget focus should be set to "Apply"
368           QToolButton* anOkBtn = findButton(PROP_PANEL_OK);
369           if (anOkBtn->isEnabled())
370             aNewFocusWidget = anOkBtn;
371           else {
372             QToolButton* aCancelBtn = findButton(PROP_PANEL_CANCEL);
373             if (aCancelBtn->isEnabled())
374               aNewFocusWidget = aCancelBtn;
375           }
376         }
377         else {
378           aNewFocusWidget = aChildren[aFocusWidgetIndex+1];
379         }
380       }
381       else {
382         if (aFocusWidgetIndex == 0) {
383           // before the first widget, the last should accept focus
384           aNewFocusWidget = aChildren[aChildrenCount - 1];
385         }
386         else {
387           // before the "Apply" button, the last should accept focus for consistency with "Next"
388           QToolButton* anOkBtn = findButton(PROP_PANEL_OK);
389           if (aFocusWidget == anOkBtn) {
390             aNewFocusWidget = aChildren[aChildrenCount - 1];
391           }
392           else {
393             aNewFocusWidget = aChildren[aFocusWidgetIndex-1];
394           }
395         }
396       }
397     }
398   }
399   if (aNewFocusWidget) {
400     if (myActiveWidget) {
401       myActiveWidget->getControls();
402       bool isFirstControl = !theIsNext;
403       QWidget* aLastFocusControl = myActiveWidget->getControlAcceptingFocus(isFirstControl);
404       if (aFocusWidget == aLastFocusControl) {
405         setActiveWidget(NULL);
406       }
407     }
408
409     //ModuleBase_Tools::setFocus(aNewFocusWidget, "XGUI_PropertyPanel::focusNextPrevChild()");
410     aNewFocusWidget->setFocus(theIsNext ? Qt::TabFocusReason : Qt::BacktabFocusReason);
411     isChangedFocus = true;
412   }
413   return isChangedFocus;
414 }
415
416 void XGUI_PropertyPanel::activateNextWidget()
417 {
418   activateNextWidget(myActiveWidget);
419 }
420
421 void XGUI_PropertyPanel::activateWidget(ModuleBase_ModelWidget* theWidget, const bool theEmitSignal)
422 {
423   std::string aPreviosAttributeID;
424   if(myActiveWidget)
425     aPreviosAttributeID = myActiveWidget->attributeID();
426
427   // Avoid activation of already actve widget. It could happen on focusIn event many times
428   if (setActiveWidget(theWidget) && theEmitSignal) {
429     emit widgetActivated(myActiveWidget);
430     if (!myActiveWidget && !isEditingMode()) {
431       emit noMoreWidgets(aPreviosAttributeID);
432     }
433   }
434 }
435
436 bool XGUI_PropertyPanel::setActiveWidget(ModuleBase_ModelWidget* theWidget)
437 {
438   // Avoid activation of already actve widget. It could happen on focusIn event many times
439   if (theWidget == myActiveWidget) {
440     return false;
441   }
442   std::string aPreviosAttributeID;
443   if(myActiveWidget) {
444     aPreviosAttributeID = myActiveWidget->attributeID();
445     myActiveWidget->processValueState();
446     myActiveWidget->deactivate();
447     myActiveWidget->setHighlighted(false);
448   }
449   if(theWidget) {
450     emit beforeWidgetActivated(theWidget);
451     theWidget->setHighlighted(true);
452     theWidget->activate();
453   }
454   myActiveWidget = theWidget;
455 #ifdef DEBUG_ACTIVE_WIDGET
456   std::cout << "myActiveWidget = " << (theWidget ? theWidget->context().c_str() : "") << std::endl;
457 #endif
458   static Events_ID anEvent = Events_Loop::eventByName(EVENT_UPDATE_BY_WIDGET_SELECTION);
459   Events_Loop::loop()->flush(anEvent);
460
461   return true;
462 }
463
464 void XGUI_PropertyPanel::setFocusOnOkButton()
465 {
466   QToolButton* anOkBtn = findButton(PROP_PANEL_OK);
467   ModuleBase_Tools::setFocus(anOkBtn, "XGUI_PropertyPanel::setFocusOnOkButton()");
468 }
469
470 void XGUI_PropertyPanel::setCancelEnabled(bool theEnabled)
471 {
472   QToolButton* anCancelBtn = findButton(PROP_PANEL_CANCEL);
473   anCancelBtn->setEnabled(theEnabled);
474 }
475
476 bool XGUI_PropertyPanel::isCancelEnabled() const
477 {
478   QToolButton* anCancelBtn = findButton(PROP_PANEL_CANCEL);
479   return anCancelBtn->isEnabled();
480 }
481
482 void XGUI_PropertyPanel::setEditingMode(bool isEditing)
483 {
484   ModuleBase_IPropertyPanel::setEditingMode(isEditing);
485   foreach(ModuleBase_ModelWidget* aWgt, myWidgets) {
486     aWgt->setEditingMode(isEditing);
487   }
488 }
489
490 void XGUI_PropertyPanel::setupActions(XGUI_ActionsMgr* theMgr)
491 {
492   QStringList aButtonNames;
493   aButtonNames << PROP_PANEL_OK << PROP_PANEL_CANCEL << PROP_PANEL_HELP << PROP_PANEL_PREVIEW;
494   QList<XGUI_ActionsMgr::OperationStateActionId> aActionIds;
495   aActionIds << XGUI_ActionsMgr::Accept << XGUI_ActionsMgr::Abort << XGUI_ActionsMgr::Help
496              << XGUI_ActionsMgr::Preview;
497   for (int i = 0; i < aButtonNames.size(); ++i) {
498     QToolButton* aBtn = findButton(aButtonNames.at(i).toStdString().c_str());
499     QAction* anAct = theMgr->operationStateAction(aActionIds.at(i));
500     aBtn->setDefaultAction(anAct);
501   }
502 }
503
504 ModuleBase_ModelWidget* XGUI_PropertyPanel::preselectionWidget() const
505 {
506   return myPreselectionWidget;
507 }
508
509 void XGUI_PropertyPanel::setPreselectionWidget(ModuleBase_ModelWidget* theWidget)
510 {
511   myPreselectionWidget = theWidget;
512 }
513
514
515 void XGUI_PropertyPanel::closeEvent(QCloseEvent* theEvent)
516 {
517   ModuleBase_Operation* aOp = myOperationMgr->currentOperation();
518   if (aOp) {
519     if (myOperationMgr->canStopOperation(aOp)) {
520       myOperationMgr->abortOperation(aOp);
521       if (myOperationMgr->hasOperation())
522         theEvent->ignore();
523       else
524         theEvent->accept();
525     } else
526       theEvent->ignore();
527   } else
528     ModuleBase_IPropertyPanel::closeEvent(theEvent);
529 }
530
531 QToolButton* XGUI_PropertyPanel::findButton(const char* theInternalName) const
532 {
533   return findChild<QToolButton*>(theInternalName);
534 }