1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include <XGUI_ActionsMgr.h>
21 #include <XGUI_ActiveControlMgr.h>
22 #include <XGUI_SelectionActivate.h>
23 #include <XGUI_ActiveControlSelector.h>
24 #include <XGUI_PropertyPanel.h>
25 #include <XGUI_PropertyPanelSelector.h>
26 #include <XGUI_OperationMgr.h>
27 #include <XGUI_Tools.h>
28 #include <XGUI_Workshop.h>
30 //#include <AppElements_Constants.h>
31 #include <ModuleBase_WidgetMultiSelector.h>
32 #include <ModuleBase_Tools.h>
33 #include <ModuleBase_PageBase.h>
34 #include <ModuleBase_PageWidget.h>
35 #include <ModuleBase_WidgetFactory.h>
36 #include <ModuleBase_OperationDescription.h>
37 #include <ModuleBase_Events.h>
38 #include <ModuleBase_IModule.h>
39 #include <ModuleBase_IWorkshop.h>
41 #include <Events_Loop.h>
43 #include <ModelAPI_Session.h>
44 #include <ModelAPI_Validator.h>
50 #include <QLayoutItem>
51 #include <QToolButton>
52 #include <QVBoxLayout>
53 #include <QGridLayout>
61 //#define DEBUG_TAB_WIDGETS
63 //#define DEBUG_ACTIVE_WIDGET
65 XGUI_PropertyPanel::XGUI_PropertyPanel(QWidget* theParent, XGUI_OperationMgr* theMgr)
66 : ModuleBase_IPropertyPanel(theParent),
68 myPreselectionWidget(NULL),
69 myInternalActiveWidget(NULL),
71 myOperationMgr(theMgr)
73 setWindowTitle(tr("Property Panel"));
74 QAction* aViewAct = toggleViewAction();
75 setObjectName(PROP_PANEL);
76 setStyleSheet("::title { position: relative; padding-left: 5px; text-align: left center }");
78 QWidget* aContent = new QWidget(this);
79 QGridLayout* aMainLayout = new QGridLayout(aContent);
80 const int kPanelColumn = 0;
82 aMainLayout->setContentsMargins(3, 3, 3, 3);
85 QFrame* aFrm = new QFrame(aContent);
86 aFrm->setFrameStyle(QFrame::Raised);
87 aFrm->setFrameShape(QFrame::Panel);
88 QHBoxLayout* aBtnLay = new QHBoxLayout(aFrm);
89 ModuleBase_Tools::zeroMargins(aBtnLay);
90 aMainLayout->addWidget(aFrm, aPanelRow++, kPanelColumn);
92 myHeaderWidget = aFrm;
94 QStringList aBtnNames;
95 aBtnNames << QString(PROP_PANEL_HELP)
96 << QString(PROP_PANEL_OK)
97 << QString(PROP_PANEL_OK_PLUS)
98 << QString(PROP_PANEL_CANCEL);
99 foreach(QString eachBtnName, aBtnNames) {
100 QToolButton* aBtn = new QToolButton(aFrm);
101 aBtn->setObjectName(eachBtnName);
102 aBtn->setAutoRaise(true);
103 aBtnLay->addWidget(aBtn);
105 aBtnLay->insertStretch(1, 1);
107 myPanelPage = new ModuleBase_PageWidget(aContent);
108 myPanelPage->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::MinimumExpanding);
109 aMainLayout->addWidget(myPanelPage, aPanelRow, kPanelColumn);
111 // spit to make the preview button on the bottom of the panel
112 aMainLayout->setRowStretch(aPanelRow++, 1);
114 // preview button on the bottom of panel
115 aFrm = new QFrame(aContent);
116 aBtnLay = new QHBoxLayout(aFrm);
117 aBtnLay->addStretch(1);
118 ModuleBase_Tools::zeroMargins(aBtnLay);
119 aMainLayout->addWidget(aFrm, aPanelRow++, kPanelColumn);
121 QToolButton* aBtn = new QToolButton(aFrm);
122 aBtn->setObjectName(PROP_PANEL_PREVIEW);
123 aBtnLay->addWidget(aBtn);
126 XGUI_PropertyPanel::~XGUI_PropertyPanel()
130 void XGUI_PropertyPanel::cleanContent()
133 myActiveWidget->deactivate();
135 XGUI_ActiveControlSelector* aPPSelector = XGUI_Tools::workshop(myOperationMgr->workshop())->
136 activeControlMgr()->getSelector(XGUI_PropertyPanelSelector::Type());
137 aPPSelector->reset(); // it removes need to be updated widget link
139 /// as the widgets are deleted later, it is important that the signals
140 /// of these widgets are not processed. An example of the error is issue 986.
141 /// In the given case, the property panel is firstly filled by new widgets
142 /// of restarted operation and after that the mouse release signal come from
143 /// the widget of the previous operation (Point2d widget about mouse is released
144 /// and focus is out of this widget)
145 QList<ModuleBase_ModelWidget*>::const_iterator anIt = myWidgets.begin(),
146 aLast = myWidgets.end();
147 for (; anIt != aLast; anIt++) {
148 QWidget* aWidget = *anIt;
150 aWidget->blockSignals(true);
155 myPanelPage->clearPage();
156 myActiveWidget = NULL;
157 emit propertyPanelDeactivated();
158 myOperationMgr->workshop()->selectionActivate()->updateSelectionModes();
159 myOperationMgr->workshop()->selectionActivate()->updateSelectionFilters();
160 #ifdef DEBUG_ACTIVE_WIDGET
161 std::cout << "myActiveWidget = NULL" << std::endl;
164 findButton(PROP_PANEL_PREVIEW)->setVisible(false); /// by default it is hidden
165 setWindowTitle(tr("Property Panel"));
168 void XGUI_PropertyPanel::setModelWidgets(const QList<ModuleBase_ModelWidget*>& theWidgets)
170 myWidgets = theWidgets;
171 if (theWidgets.empty())
173 foreach (ModuleBase_ModelWidget* aWidget, theWidgets) {
174 connect(aWidget, SIGNAL(focusInWidget(ModuleBase_ModelWidget*)),
175 this, SLOT(onFocusInWidget(ModuleBase_ModelWidget*)));
176 connect(aWidget, SIGNAL(focusOutWidget(ModuleBase_ModelWidget*)),
177 this, SLOT(onActivateNextWidget(ModuleBase_ModelWidget*)));
178 connect(aWidget, SIGNAL(keyReleased(QObject*, QKeyEvent*)),
179 this, SIGNAL(keyReleased(QObject*, QKeyEvent*)));
180 connect(aWidget, SIGNAL(enterClicked(QObject*)),
181 this, SIGNAL(enterClicked(QObject*)));
185 const QList<ModuleBase_ModelWidget*>& XGUI_PropertyPanel::modelWidgets() const
190 ModuleBase_PageBase* XGUI_PropertyPanel::contentWidget()
192 return static_cast<ModuleBase_PageBase*>(myPanelPage);
195 void XGUI_PropertyPanel::updateContentWidget(FeaturePtr theFeature)
197 // Invalid feature case on abort of the operation
198 if (theFeature.get() == NULL)
200 if (theFeature->isAction() || !theFeature->data())
202 foreach(ModuleBase_ModelWidget* eachWidget, myWidgets) {
203 if (!eachWidget->feature().get())
204 eachWidget->setFeature(theFeature);
205 eachWidget->restoreValue();
207 // the repaint is used here to immediately react in GUI to the values change.
211 void XGUI_PropertyPanel::createContentPanel(FeaturePtr theFeature)
213 // Invalid feature case on abort of the operation
214 if (theFeature.get() == NULL)
216 if (theFeature->isAction() || !theFeature->data())
219 if (myWidgets.empty()) {
220 ModuleBase_Operation* anOperation = myOperationMgr->currentOperation();
221 QString aXmlRepr = anOperation->getDescription()->xmlRepresentation();
223 ModuleBase_WidgetFactory aFactory(aXmlRepr.toStdString(), myOperationMgr->workshop());
224 aFactory.createPanel(contentWidget(), theFeature);
225 /// Apply button should be update if the feature was modified by the panel
226 myOperationMgr->onValidateOperation();
228 std::shared_ptr<Config_FeatureMessage> aFeatureInfo =
229 myOperationMgr->workshop()->featureInfo(theFeature->getKind().c_str());
230 if (aFeatureInfo.get())
231 findButton(PROP_PANEL_OK_PLUS)->setVisible(aFeatureInfo->isApplyContinue());
234 ModuleBase_ModelWidget* XGUI_PropertyPanel::activeWidget(const bool isUseCustomWidget) const
236 if (isUseCustomWidget && myInternalActiveWidget)
237 return myInternalActiveWidget;
239 return myActiveWidget;
242 void XGUI_PropertyPanel::activateNextWidget(ModuleBase_ModelWidget* theWidget)
244 // it is possible that the property panel widgets have not been visualized
245 // (e.g. on start operation), so it is strictly important to do not check visualized state
246 activateNextWidget(theWidget, false);
249 void XGUI_PropertyPanel::onFocusInWidget(ModuleBase_ModelWidget* theWidget)
251 #ifdef DEBUG_ACTIVE_WIDGET
252 std::cout << "onFocusInWidget" << std::endl;
254 if (theWidget->canAcceptFocus())
255 activateWidget(theWidget);
258 void XGUI_PropertyPanel::onActivateNextWidget(ModuleBase_ModelWidget* theWidget)
260 // this slot happens when some widget lost focus, the next widget which accepts the focus
261 // should be shown, so the second parameter is true
262 // it is important for features where in cases the same attributes are used, isCase for this
263 // attribute returns true, however it can be placed in hidden stack widget(extrusion: elements,
264 // sketch multi rotation -> single/full point)
265 // it is important to check the widget visibility to do not check of the next widget visible
266 // state if the current is not shown. (example: sketch circle re-entrant operation after mouse
267 // release in the viewer, next, radius,
268 // widget should be activated but the first is not visualized)
269 bool isVisible = theWidget->isVisible();
270 activateNextWidget(theWidget, isVisible);
274 void XGUI_PropertyPanel::activateNextWidget(ModuleBase_ModelWidget* theWidget,
275 const bool isCheckVisibility)
277 // TO CHECK: Editing operation does not have automatical activation of widgets
278 if (isEditingMode()) {
279 activateWidget(NULL);
282 ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
284 QList<ModuleBase_ModelWidget*>::const_iterator anIt = myWidgets.begin(), aLast = myWidgets.end();
285 bool isFoundWidget = false;
286 ModuleBase_Tools::activateWindow(this, "XGUI_PropertyPanel::activateNextWidget()");
287 for (; anIt != aLast; anIt++) {
288 ModuleBase_ModelWidget* aCurrentWidget = *anIt;
289 if (isFoundWidget || !theWidget) {
291 if (!aValidators->isCase(aCurrentWidget->feature(), aCurrentWidget->attributeID()))
292 continue; // this attribute does not participate in the current case
293 if (isCheckVisibility && !aCurrentWidget->isInternal()) {
294 if (!aCurrentWidget->isVisible())
297 if (!aCurrentWidget->isObligatory())
298 continue; // not obligatory widgets are not activated automatically
300 if (!aCurrentWidget->canAcceptFocus())
301 continue; // do not set focus if it can not be accepted, case: optional choice
303 if (aCurrentWidget->focusTo()) {
304 aCurrentWidget->emitFocusInWidget();
308 isFoundWidget = isFoundWidget || (*anIt) == theWidget;
310 // set focus to Ok/Cancel button in Property panel if there are no more active widgets
311 // it should be performed before activateWidget(NULL) because it emits some signals which
312 // can be processed by moudule for example as to activate another widget with setting focus
313 QWidget* aNewFocusWidget = 0;
314 QToolButton* anOkBtn = findButton(PROP_PANEL_OK);
315 if (anOkBtn->isEnabled())
316 aNewFocusWidget = anOkBtn;
318 QToolButton* aCancelBtn = findButton(PROP_PANEL_CANCEL);
319 if (aCancelBtn->isEnabled())
320 aNewFocusWidget = aCancelBtn;
323 ModuleBase_Tools::setFocus(aNewFocusWidget, "XGUI_PropertyPanel::activateNextWidget");
325 activateWidget(NULL);
328 void findDirectChildren(QWidget* theParent, QList<QWidget*>& theWidgets, const bool theDebug)
330 QList<QWidget*> aWidgets;
333 QLayout* aLayout = theParent->layout();
335 for (int i = 0, aCount = aLayout->count(); i < aCount; i++) {
336 QLayoutItem* anItem = aLayout->itemAt(i);
337 QWidget* aWidget = anItem ? anItem->widget() : 0;
339 if (aWidget->isVisible()) {
340 if (aWidget->focusPolicy() != Qt::NoFocus)
341 theWidgets.append(aWidget);
342 findDirectChildren(aWidget, theWidgets, false);
345 else if (anItem->layout()) {
346 QLayout* aLayout = anItem->layout();
347 for (int i = 0, aCount = aLayout->count(); i < aCount; i++) {
348 QLayoutItem* anItem = aLayout->itemAt(i);
349 QWidget* aWidget = anItem ? anItem->widget() : 0;
351 if (aWidget->isVisible()) {
352 if (aWidget->focusPolicy() != Qt::NoFocus)
353 theWidgets.append(aWidget);
354 findDirectChildren(aWidget, theWidgets, false);
358 // TODO: improve recursive search for the case when here QLayout is used
359 // currently, the switch widget uses only 1 level of qlayout in qlayout using for
360 // example for construction plane feature. If there are more levels,
361 // it should be implemented here
368 #ifdef DEBUG_TAB_WIDGETS
370 QStringList aWidgetTypes;
371 QList<QWidget*>::const_iterator anIt = theWidgets.begin(), aLast = theWidgets.end();
372 for (; anIt != aLast; anIt++) {
373 QWidget* aWidget = *anIt;
375 aWidgetTypes.append(aWidget->objectName());
377 QString anInfo = QString("theWidgets[%1]: %2")
378 .arg(theWidgets.count()).arg(aWidgetTypes.join(","));
379 qDebug(anInfo.toStdString().c_str());
384 bool XGUI_PropertyPanel::setFocusNextPrevChild(bool theIsNext)
386 return focusNextPrevChild(theIsNext);
389 bool XGUI_PropertyPanel::focusNextPrevChild(bool theIsNext)
391 // it wraps the Tabs clicking to follow in the chain:
392 // controls, last control, Apply, Cancel, first control, controls
393 bool isChangedFocus = false;
395 QWidget* aFocusWidget = focusWidget();
396 #ifdef DEBUG_TAB_WIDGETS
398 QString anInfo = QString("focus Widget: %1").arg(aFocusWidget->objectName());
399 qDebug(anInfo.toStdString().c_str());
402 ModuleBase_ModelWidget* aFocusMWidget = ModuleBase_ModelWidget::findModelWidget(this,
405 // aFocusMWidget->setHighlighted(false);
407 QWidget* aNewFocusWidget = 0;
409 QList<QWidget*> aChildren;
410 findDirectChildren(this, aChildren, true);
411 int aChildrenCount = aChildren.count();
412 int aFocusWidgetIndex = aChildren.indexOf(aFocusWidget);
413 QToolButton* anOkBtn = findButton(PROP_PANEL_OK);
414 if (aFocusWidgetIndex >= 0) {
416 if (aFocusWidgetIndex == aChildrenCount-1) {
417 // after the last widget focus should be set to "Apply"
418 if (anOkBtn->isEnabled())
419 aNewFocusWidget = anOkBtn;
421 QToolButton* aCancelBtn = findButton(PROP_PANEL_CANCEL);
422 if (aCancelBtn->isEnabled())
423 aNewFocusWidget = aCancelBtn;
427 aNewFocusWidget = aChildren[aFocusWidgetIndex+1];
431 if (aFocusWidgetIndex == 0) {
432 // before the first widget, the last should accept focus
433 aNewFocusWidget = aChildren[aChildrenCount - 1];
436 // before the "Apply" button, the last should accept focus for consistency with "Next"
437 if (aFocusWidget == anOkBtn) {
438 aNewFocusWidget = aChildren[aChildrenCount - 1];
441 aNewFocusWidget = aChildren[aFocusWidgetIndex-1];
447 if (aNewFocusWidget) {
448 if (myActiveWidget) {
449 bool isFirstControl = !theIsNext;
450 QWidget* aLastFocusControl = myActiveWidget->getControlAcceptingFocus(isFirstControl);
451 if (aFocusWidget == aLastFocusControl) {
452 setActiveWidget(NULL, false);
456 // we want to have property panel as an active window to enter values in double control
457 ModuleBase_Tools::setFocus(aNewFocusWidget, "XGUI_PropertyPanel::focusNextPrevChild()");
459 ModuleBase_ModelWidget* aNewFocusMWidget = ModuleBase_ModelWidget::findModelWidget(this,
461 if (aNewFocusMWidget) {
463 aFocusMWidget->setHighlighted(false);
465 aNewFocusMWidget->emitFocusInWidget();
466 isChangedFocus = true;
469 return isChangedFocus;
472 void XGUI_PropertyPanel::activateNextWidget()
474 activateNextWidget(myActiveWidget);
477 void XGUI_PropertyPanel::activateWidget(ModuleBase_ModelWidget* theWidget, const bool theEmitSignal)
479 std::string aPreviosAttributeID;
481 aPreviosAttributeID = myActiveWidget->attributeID();
483 // Avoid activation of already actve widget. It could happen on focusIn event many times
484 setActiveWidget(theWidget, theEmitSignal);
487 bool XGUI_PropertyPanel::setActiveWidget(ModuleBase_ModelWidget* theWidget, const bool isEmitSignal)
489 // Avoid activation of already actve widget. It could happen on focusIn event many times
490 if (theWidget == myActiveWidget) {
493 std::string aPreviosAttributeID;
494 ModuleBase_ModelWidget* aDeactivatedWidget = NULL, *anActivatedWidget = NULL;
496 aPreviosAttributeID = myActiveWidget->attributeID();
497 myActiveWidget->processValueState();
498 myActiveWidget->deactivate();
499 myActiveWidget->setHighlighted(false);
500 aDeactivatedWidget = myActiveWidget;
503 emit beforeWidgetActivated(theWidget);
504 theWidget->setHighlighted(true);
505 theWidget->activate();
506 anActivatedWidget = theWidget;
508 myActiveWidget = theWidget;
510 #ifdef DEBUG_ACTIVE_WIDGET
511 std::cout << "myActiveWidget = " << (theWidget ? theWidget->context().c_str() : "") << std::endl;
513 bool aHasMoreWidgets = true;
515 //emit widgetActivated(myActiveWidget);
516 if (!myActiveWidget && !isEditingMode()) {
517 aHasMoreWidgets = false;
518 emit noMoreWidgets(aPreviosAttributeID);
522 emit propertyPanelActivated();
524 emit propertyPanelDeactivated();
525 myOperationMgr->workshop()->selectionActivate()->updateSelectionModes();
526 myOperationMgr->workshop()->selectionActivate()->updateSelectionFilters();
528 if (aHasMoreWidgets && aDeactivatedWidget)
529 aDeactivatedWidget->updateAfterDeactivation();
530 if (aHasMoreWidgets && anActivatedWidget)
531 anActivatedWidget->updateAfterActivation();
533 if (aHasMoreWidgets && myActiveWidget)
535 // restore widget selection should be done after selection modes of widget activating
536 static Events_ID anEvent = Events_Loop::eventByName(EVENT_UPDATE_BY_WIDGET_SELECTION);
537 Events_Loop::loop()->flush(anEvent);
542 void XGUI_PropertyPanel::setFocusOnOkButton()
544 QToolButton* anOkBtn = findButton(PROP_PANEL_OK);
545 ModuleBase_Tools::setFocus(anOkBtn, "XGUI_PropertyPanel::setFocusOnOkButton()");
548 void XGUI_PropertyPanel::setCancelEnabled(bool theEnabled)
550 QToolButton* anCancelBtn = findButton(PROP_PANEL_CANCEL);
551 anCancelBtn->setEnabled(theEnabled);
554 bool XGUI_PropertyPanel::isCancelEnabled() const
556 QToolButton* anCancelBtn = findButton(PROP_PANEL_CANCEL);
557 return anCancelBtn->isEnabled();
560 void XGUI_PropertyPanel::setEditingMode(bool isEditing)
562 ModuleBase_IPropertyPanel::setEditingMode(isEditing);
563 foreach(ModuleBase_ModelWidget* aWgt, myWidgets) {
564 aWgt->setEditingMode(isEditing);
568 void XGUI_PropertyPanel::setupActions(XGUI_ActionsMgr* theMgr)
570 QStringList aButtonNames;
571 aButtonNames << PROP_PANEL_OK<< PROP_PANEL_OK_PLUS << PROP_PANEL_CANCEL
572 << PROP_PANEL_HELP << PROP_PANEL_PREVIEW;
573 QList<XGUI_ActionsMgr::OperationStateActionId> aActionIds;
574 aActionIds << XGUI_ActionsMgr::Accept << XGUI_ActionsMgr::AcceptPlus << XGUI_ActionsMgr::Abort
575 << XGUI_ActionsMgr::Help << XGUI_ActionsMgr::Preview;
576 for (int i = 0; i < aButtonNames.size(); ++i) {
577 QToolButton* aBtn = findButton(aButtonNames.at(i).toStdString().c_str());
578 QAction* anAct = theMgr->operationStateAction(aActionIds.at(i));
579 aBtn->setDefaultAction(anAct);
583 void XGUI_PropertyPanel::onAcceptData()
585 foreach (ModuleBase_ModelWidget* aWidget, myWidgets) {
586 aWidget->onFeatureAccepted();
590 void XGUI_PropertyPanel::setInternalActiveWidget(ModuleBase_ModelWidget* theWidget)
594 myInternalActiveWidget = theWidget;
595 emit propertyPanelActivated();
599 if (myInternalActiveWidget)
601 delete myInternalActiveWidget;
602 myInternalActiveWidget = 0;
604 emit propertyPanelDeactivated();
606 myOperationMgr->workshop()->selectionActivate()->updateSelectionModes();
607 myOperationMgr->workshop()->selectionActivate()->updateSelectionFilters();
610 ModuleBase_ModelWidget* XGUI_PropertyPanel::preselectionWidget() const
612 return myPreselectionWidget;
615 void XGUI_PropertyPanel::setPreselectionWidget(ModuleBase_ModelWidget* theWidget)
617 myPreselectionWidget = theWidget;
621 void XGUI_PropertyPanel::closeEvent(QCloseEvent* theEvent)
623 ModuleBase_Operation* aOp = myOperationMgr->currentOperation();
625 if (myOperationMgr->canStopOperation(aOp)) {
626 myOperationMgr->abortOperation(aOp);
627 if (myOperationMgr->hasOperation())
634 ModuleBase_IPropertyPanel::closeEvent(theEvent);
637 QToolButton* XGUI_PropertyPanel::findButton(const char* theInternalName) const
639 return findChild<QToolButton*>(theInternalName);