]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_PropertyPanel.cpp
Salome HOME
Issues #2173, #2169: Activate PropertyPanel window to accept focus always in double...
[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     aNewFocusWidget->setFocus(Qt::TabFocusReason);
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::focusNextPrevChild_(bool theIsNext)
346 {
347   // it wraps the Tabs clicking to follow in the chain:
348   // controls, last control, Apply, Cancel, first control, controls
349   bool isChangedFocus = false;
350
351   QWidget* aFocusWidget = focusWidget();
352 #ifdef DEBUG_TAB_WIDGETS
353   if (aFocusWidget) {
354     QString anInfo = QString("focus Widget: %1").arg(aFocusWidget->objectName());
355     qDebug(anInfo.toStdString().c_str());
356   }
357 #endif
358   ModuleBase_ModelWidget* aFocusMWidget = ModuleBase_ModelWidget::findModelWidget(this,
359                                                                          aFocusWidget);
360   if (aFocusMWidget)
361     aFocusMWidget->setHighlighted(false);
362
363   QWidget* aNewFocusWidget = 0;
364   if (aFocusWidget) {
365     QList<QWidget*> aChildren;
366     findDirectChildren(this, aChildren, true);
367     int aChildrenCount = aChildren.count();
368     int aFocusWidgetIndex = aChildren.indexOf(aFocusWidget);
369     if (aFocusWidgetIndex >= 0) {
370       if (theIsNext) {
371         if (aFocusWidgetIndex == aChildrenCount-1) {
372           // after the last widget focus should be set to "Apply"
373           QToolButton* anOkBtn = findButton(PROP_PANEL_OK);
374           if (anOkBtn->isEnabled())
375             aNewFocusWidget = anOkBtn;
376           else {
377             QToolButton* aCancelBtn = findButton(PROP_PANEL_CANCEL);
378             if (aCancelBtn->isEnabled())
379               aNewFocusWidget = aCancelBtn;
380           }
381         }
382         else {
383           aNewFocusWidget = aChildren[aFocusWidgetIndex+1];
384         }
385       }
386       else {
387         if (aFocusWidgetIndex == 0) {
388           // before the first widget, the last should accept focus
389           aNewFocusWidget = aChildren[aChildrenCount - 1];
390         }
391         else {
392           // before the "Apply" button, the last should accept focus for consistency with "Next"
393           QToolButton* anOkBtn = findButton(PROP_PANEL_OK);
394           if (aFocusWidget == anOkBtn) {
395             aNewFocusWidget = aChildren[aChildrenCount - 1];
396           }
397           else {
398             aNewFocusWidget = aChildren[aFocusWidgetIndex-1];
399           }
400         }
401       }
402     }
403   }
404   if (aNewFocusWidget) {
405     if (myActiveWidget) {
406       bool isFirstControl = !theIsNext;
407       QWidget* aLastFocusControl = myActiveWidget->getControlAcceptingFocus(isFirstControl);
408       if (aFocusWidget == aLastFocusControl) {
409         setActiveWidget(NULL);
410       }
411     }
412
413     // we want to have property panel as an active window to enter values in double control
414     ModuleBase_Tools::activateWindow(this, "XGUI_PropertyPanel::activateNextWidget()");
415     ModuleBase_Tools::setFocus(aNewFocusWidget, "XGUI_PropertyPanel::focusNextPrevChild()");
416
417     ModuleBase_ModelWidget* aNewFocusMWidget = ModuleBase_ModelWidget::findModelWidget(this,
418                                                                               aNewFocusWidget);
419     if (aNewFocusMWidget)
420       aNewFocusMWidget->emitFocusInWidget();
421     isChangedFocus = true;
422   }
423   return isChangedFocus;
424 }
425
426 void XGUI_PropertyPanel::activateNextWidget()
427 {
428   activateNextWidget(myActiveWidget);
429 }
430
431 void XGUI_PropertyPanel::activateWidget(ModuleBase_ModelWidget* theWidget, const bool theEmitSignal)
432 {
433   std::string aPreviosAttributeID;
434   if(myActiveWidget)
435     aPreviosAttributeID = myActiveWidget->attributeID();
436
437   // Avoid activation of already actve widget. It could happen on focusIn event many times
438   if (setActiveWidget(theWidget) && theEmitSignal) {
439     emit widgetActivated(myActiveWidget);
440     if (!myActiveWidget && !isEditingMode()) {
441       emit noMoreWidgets(aPreviosAttributeID);
442     }
443   }
444 }
445
446 bool XGUI_PropertyPanel::setActiveWidget(ModuleBase_ModelWidget* theWidget)
447 {
448   // Avoid activation of already actve widget. It could happen on focusIn event many times
449   if (theWidget == myActiveWidget) {
450     return false;
451   }
452   std::string aPreviosAttributeID;
453   if(myActiveWidget) {
454     aPreviosAttributeID = myActiveWidget->attributeID();
455     myActiveWidget->processValueState();
456     myActiveWidget->deactivate();
457     myActiveWidget->setHighlighted(false);
458   }
459   if(theWidget) {
460     emit beforeWidgetActivated(theWidget);
461     theWidget->setHighlighted(true);
462     theWidget->activate();
463   }
464   myActiveWidget = theWidget;
465 #ifdef DEBUG_ACTIVE_WIDGET
466   std::cout << "myActiveWidget = " << (theWidget ? theWidget->context().c_str() : "") << std::endl;
467 #endif
468   static Events_ID anEvent = Events_Loop::eventByName(EVENT_UPDATE_BY_WIDGET_SELECTION);
469   Events_Loop::loop()->flush(anEvent);
470
471   return true;
472 }
473
474 void XGUI_PropertyPanel::setFocusOnOkButton()
475 {
476   QToolButton* anOkBtn = findButton(PROP_PANEL_OK);
477   ModuleBase_Tools::setFocus(anOkBtn, "XGUI_PropertyPanel::setFocusOnOkButton()");
478 }
479
480 void XGUI_PropertyPanel::setCancelEnabled(bool theEnabled)
481 {
482   QToolButton* anCancelBtn = findButton(PROP_PANEL_CANCEL);
483   anCancelBtn->setEnabled(theEnabled);
484 }
485
486 bool XGUI_PropertyPanel::isCancelEnabled() const
487 {
488   QToolButton* anCancelBtn = findButton(PROP_PANEL_CANCEL);
489   return anCancelBtn->isEnabled();
490 }
491
492 void XGUI_PropertyPanel::setEditingMode(bool isEditing)
493 {
494   ModuleBase_IPropertyPanel::setEditingMode(isEditing);
495   foreach(ModuleBase_ModelWidget* aWgt, myWidgets) {
496     aWgt->setEditingMode(isEditing);
497   }
498 }
499
500 void XGUI_PropertyPanel::setupActions(XGUI_ActionsMgr* theMgr)
501 {
502   QStringList aButtonNames;
503   aButtonNames << PROP_PANEL_OK << PROP_PANEL_CANCEL << PROP_PANEL_HELP << PROP_PANEL_PREVIEW;
504   QList<XGUI_ActionsMgr::OperationStateActionId> aActionIds;
505   aActionIds << XGUI_ActionsMgr::Accept << XGUI_ActionsMgr::Abort << XGUI_ActionsMgr::Help
506              << XGUI_ActionsMgr::Preview;
507   for (int i = 0; i < aButtonNames.size(); ++i) {
508     QToolButton* aBtn = findButton(aButtonNames.at(i).toStdString().c_str());
509     QAction* anAct = theMgr->operationStateAction(aActionIds.at(i));
510     aBtn->setDefaultAction(anAct);
511   }
512 }
513
514 ModuleBase_ModelWidget* XGUI_PropertyPanel::preselectionWidget() const
515 {
516   return myPreselectionWidget;
517 }
518
519 void XGUI_PropertyPanel::setPreselectionWidget(ModuleBase_ModelWidget* theWidget)
520 {
521   myPreselectionWidget = theWidget;
522 }
523
524
525 void XGUI_PropertyPanel::closeEvent(QCloseEvent* theEvent)
526 {
527   ModuleBase_Operation* aOp = myOperationMgr->currentOperation();
528   if (aOp) {
529     if (myOperationMgr->canStopOperation(aOp)) {
530       myOperationMgr->abortOperation(aOp);
531       if (myOperationMgr->hasOperation())
532         theEvent->ignore();
533       else
534         theEvent->accept();
535     } else
536       theEvent->ignore();
537   } else
538     ModuleBase_IPropertyPanel::closeEvent(theEvent);
539 }
540
541 QToolButton* XGUI_PropertyPanel::findButton(const char* theInternalName) const
542 {
543   return findChild<QToolButton*>(theInternalName);
544 }