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 updateApplyPlusButton(theFeature);
231 void XGUI_PropertyPanel::updateApplyPlusButton(FeaturePtr theFeature)
233 if (theFeature.get()) {
234 std::shared_ptr<Config_FeatureMessage> aFeatureInfo =
235 myOperationMgr->workshop()->featureInfo(theFeature->getKind().c_str());
236 if (aFeatureInfo.get()) {
237 findButton(PROP_PANEL_OK_PLUS)->setVisible(aFeatureInfo->isApplyContinue());
241 findButton(PROP_PANEL_OK_PLUS)->setVisible(false);
244 ModuleBase_ModelWidget* XGUI_PropertyPanel::activeWidget(const bool isUseCustomWidget) const
246 if (isUseCustomWidget && myInternalActiveWidget)
247 return myInternalActiveWidget;
249 return myActiveWidget;
252 void XGUI_PropertyPanel::activateNextWidget(ModuleBase_ModelWidget* theWidget)
254 // it is possible that the property panel widgets have not been visualized
255 // (e.g. on start operation), so it is strictly important to do not check visualized state
256 activateNextWidget(theWidget, false);
259 void XGUI_PropertyPanel::onFocusInWidget(ModuleBase_ModelWidget* theWidget)
261 #ifdef DEBUG_ACTIVE_WIDGET
262 std::cout << "onFocusInWidget" << std::endl;
264 if (theWidget->canAcceptFocus())
265 activateWidget(theWidget);
268 void XGUI_PropertyPanel::onActivateNextWidget(ModuleBase_ModelWidget* theWidget)
270 // this slot happens when some widget lost focus, the next widget which accepts the focus
271 // should be shown, so the second parameter is true
272 // it is important for features where in cases the same attributes are used, isCase for this
273 // attribute returns true, however it can be placed in hidden stack widget(extrusion: elements,
274 // sketch multi rotation -> single/full point)
275 // it is important to check the widget visibility to do not check of the next widget visible
276 // state if the current is not shown. (example: sketch circle re-entrant operation after mouse
277 // release in the viewer, next, radius,
278 // widget should be activated but the first is not visualized)
279 bool isVisible = theWidget->isVisible();
280 activateNextWidget(theWidget, isVisible);
284 void XGUI_PropertyPanel::activateNextWidget(ModuleBase_ModelWidget* theWidget,
285 const bool isCheckVisibility)
287 // TO CHECK: Editing operation does not have automatical activation of widgets
288 if (isEditingMode()) {
289 activateWidget(NULL);
292 ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
294 QList<ModuleBase_ModelWidget*>::const_iterator anIt = myWidgets.begin(), aLast = myWidgets.end();
295 bool isFoundWidget = false;
296 ModuleBase_Tools::activateWindow(this, "XGUI_PropertyPanel::activateNextWidget()");
297 for (; anIt != aLast; anIt++) {
298 ModuleBase_ModelWidget* aCurrentWidget = *anIt;
299 if (isFoundWidget || !theWidget) {
301 if (!aValidators->isCase(aCurrentWidget->feature(), aCurrentWidget->attributeID()))
302 continue; // this attribute does not participate in the current case
303 if (isCheckVisibility && !aCurrentWidget->isInternal()) {
304 if (!aCurrentWidget->isVisible())
307 if (!aCurrentWidget->isObligatory())
308 continue; // not obligatory widgets are not activated automatically
310 if (!aCurrentWidget->canAcceptFocus())
311 continue; // do not set focus if it can not be accepted, case: optional choice
313 if (aCurrentWidget->focusTo()) {
314 aCurrentWidget->emitFocusInWidget();
318 isFoundWidget = isFoundWidget || (*anIt) == theWidget;
320 // set focus to Ok/Cancel button in Property panel if there are no more active widgets
321 // it should be performed before activateWidget(NULL) because it emits some signals which
322 // can be processed by moudule for example as to activate another widget with setting focus
323 QWidget* aNewFocusWidget = 0;
324 QToolButton* anOkBtn = findButton(PROP_PANEL_OK);
325 if (anOkBtn->isEnabled())
326 aNewFocusWidget = anOkBtn;
328 QToolButton* aCancelBtn = findButton(PROP_PANEL_CANCEL);
329 if (aCancelBtn->isEnabled())
330 aNewFocusWidget = aCancelBtn;
333 ModuleBase_Tools::setFocus(aNewFocusWidget, "XGUI_PropertyPanel::activateNextWidget");
335 activateWidget(NULL);
338 void findDirectChildren(QWidget* theParent, QList<QWidget*>& theWidgets, const bool theDebug)
340 QList<QWidget*> aWidgets;
343 QLayout* aLayout = theParent->layout();
345 for (int i = 0, aCount = aLayout->count(); i < aCount; i++) {
346 QLayoutItem* anItem = aLayout->itemAt(i);
347 QWidget* aWidget = anItem ? anItem->widget() : 0;
349 if (aWidget->isVisible()) {
350 if (aWidget->focusPolicy() != Qt::NoFocus)
351 theWidgets.append(aWidget);
352 findDirectChildren(aWidget, theWidgets, false);
355 else if (anItem->layout()) {
356 QLayout* aLayout = anItem->layout();
357 for (int i = 0, aCount = aLayout->count(); i < aCount; i++) {
358 QLayoutItem* anItem = aLayout->itemAt(i);
359 QWidget* aWidget = anItem ? anItem->widget() : 0;
361 if (aWidget->isVisible()) {
362 if (aWidget->focusPolicy() != Qt::NoFocus)
363 theWidgets.append(aWidget);
364 findDirectChildren(aWidget, theWidgets, false);
368 // TODO: improve recursive search for the case when here QLayout is used
369 // currently, the switch widget uses only 1 level of qlayout in qlayout using for
370 // example for construction plane feature. If there are more levels,
371 // it should be implemented here
378 #ifdef DEBUG_TAB_WIDGETS
380 QStringList aWidgetTypes;
381 QList<QWidget*>::const_iterator anIt = theWidgets.begin(), aLast = theWidgets.end();
382 for (; anIt != aLast; anIt++) {
383 QWidget* aWidget = *anIt;
385 aWidgetTypes.append(aWidget->objectName());
387 QString anInfo = QString("theWidgets[%1]: %2")
388 .arg(theWidgets.count()).arg(aWidgetTypes.join(","));
389 qDebug(anInfo.toStdString().c_str());
394 bool XGUI_PropertyPanel::setFocusNextPrevChild(bool theIsNext)
396 return focusNextPrevChild(theIsNext);
399 bool XGUI_PropertyPanel::focusNextPrevChild(bool theIsNext)
401 // it wraps the Tabs clicking to follow in the chain:
402 // controls, last control, Apply, Cancel, first control, controls
403 bool isChangedFocus = false;
405 QWidget* aFocusWidget = focusWidget();
406 #ifdef DEBUG_TAB_WIDGETS
408 QString anInfo = QString("focus Widget: %1").arg(aFocusWidget->objectName());
409 qDebug(anInfo.toStdString().c_str());
412 ModuleBase_ModelWidget* aFocusMWidget = ModuleBase_ModelWidget::findModelWidget(this,
415 // aFocusMWidget->setHighlighted(false);
417 QWidget* aNewFocusWidget = 0;
419 QList<QWidget*> aChildren;
420 findDirectChildren(this, aChildren, true);
421 int aChildrenCount = aChildren.count();
422 int aFocusWidgetIndex = aChildren.indexOf(aFocusWidget);
423 QToolButton* anOkBtn = findButton(PROP_PANEL_OK);
424 if (aFocusWidgetIndex >= 0) {
426 if (aFocusWidgetIndex == aChildrenCount-1) {
427 // after the last widget focus should be set to "Apply"
428 if (anOkBtn->isEnabled())
429 aNewFocusWidget = anOkBtn;
431 QToolButton* aCancelBtn = findButton(PROP_PANEL_CANCEL);
432 if (aCancelBtn->isEnabled())
433 aNewFocusWidget = aCancelBtn;
437 aNewFocusWidget = aChildren[aFocusWidgetIndex+1];
441 if (aFocusWidgetIndex == 0) {
442 // before the first widget, the last should accept focus
443 aNewFocusWidget = aChildren[aChildrenCount - 1];
446 // before the "Apply" button, the last should accept focus for consistency with "Next"
447 if (aFocusWidget == anOkBtn) {
448 aNewFocusWidget = aChildren[aChildrenCount - 1];
451 aNewFocusWidget = aChildren[aFocusWidgetIndex-1];
457 if (aNewFocusWidget) {
458 if (myActiveWidget) {
459 bool isFirstControl = !theIsNext;
460 QWidget* aLastFocusControl = myActiveWidget->getControlAcceptingFocus(isFirstControl);
461 if (aFocusWidget == aLastFocusControl) {
462 setActiveWidget(NULL, false);
466 // we want to have property panel as an active window to enter values in double control
467 ModuleBase_Tools::setFocus(aNewFocusWidget, "XGUI_PropertyPanel::focusNextPrevChild()");
469 ModuleBase_ModelWidget* aNewFocusMWidget = ModuleBase_ModelWidget::findModelWidget(this,
471 if (aNewFocusMWidget) {
473 aFocusMWidget->setHighlighted(false);
475 aNewFocusMWidget->emitFocusInWidget();
476 isChangedFocus = true;
479 return isChangedFocus;
482 void XGUI_PropertyPanel::activateNextWidget()
484 activateNextWidget(myActiveWidget);
487 void XGUI_PropertyPanel::activateWidget(ModuleBase_ModelWidget* theWidget, const bool theEmitSignal)
489 std::string aPreviosAttributeID;
491 aPreviosAttributeID = myActiveWidget->attributeID();
493 // Avoid activation of already actve widget. It could happen on focusIn event many times
494 setActiveWidget(theWidget, theEmitSignal);
497 bool XGUI_PropertyPanel::setActiveWidget(ModuleBase_ModelWidget* theWidget, const bool isEmitSignal)
499 // Avoid activation of already actve widget. It could happen on focusIn event many times
500 if (theWidget == myActiveWidget) {
503 std::string aPreviosAttributeID;
504 ModuleBase_ModelWidget* aDeactivatedWidget = NULL, *anActivatedWidget = NULL;
506 aPreviosAttributeID = myActiveWidget->attributeID();
507 myActiveWidget->processValueState();
508 myActiveWidget->deactivate();
509 myActiveWidget->setHighlighted(false);
510 aDeactivatedWidget = myActiveWidget;
513 emit beforeWidgetActivated(theWidget);
514 theWidget->setHighlighted(true);
515 theWidget->activate();
516 anActivatedWidget = theWidget;
518 myActiveWidget = theWidget;
520 #ifdef DEBUG_ACTIVE_WIDGET
521 std::cout << "myActiveWidget = " << (theWidget ? theWidget->context().c_str() : "") << std::endl;
523 bool aHasMoreWidgets = true;
525 //emit widgetActivated(myActiveWidget);
526 if (!myActiveWidget && !isEditingMode()) {
527 aHasMoreWidgets = false;
528 emit noMoreWidgets(aPreviosAttributeID);
532 emit propertyPanelActivated();
534 emit propertyPanelDeactivated();
535 myOperationMgr->workshop()->selectionActivate()->updateSelectionModes();
536 myOperationMgr->workshop()->selectionActivate()->updateSelectionFilters();
538 if (aHasMoreWidgets && aDeactivatedWidget)
539 aDeactivatedWidget->updateAfterDeactivation();
540 if (aHasMoreWidgets && anActivatedWidget)
541 anActivatedWidget->updateAfterActivation();
543 if (aHasMoreWidgets && myActiveWidget)
545 // restore widget selection should be done after selection modes of widget activating
546 static Events_ID anEvent = Events_Loop::eventByName(EVENT_UPDATE_BY_WIDGET_SELECTION);
547 Events_Loop::loop()->flush(anEvent);
552 void XGUI_PropertyPanel::setFocusOnOkButton()
554 QToolButton* anOkBtn = findButton(PROP_PANEL_OK);
555 ModuleBase_Tools::setFocus(anOkBtn, "XGUI_PropertyPanel::setFocusOnOkButton()");
558 void XGUI_PropertyPanel::setCancelEnabled(bool theEnabled)
560 QToolButton* anCancelBtn = findButton(PROP_PANEL_CANCEL);
561 anCancelBtn->setEnabled(theEnabled);
564 bool XGUI_PropertyPanel::isCancelEnabled() const
566 QToolButton* anCancelBtn = findButton(PROP_PANEL_CANCEL);
567 return anCancelBtn->isEnabled();
570 void XGUI_PropertyPanel::setEditingMode(bool isEditing)
572 ModuleBase_IPropertyPanel::setEditingMode(isEditing);
573 foreach(ModuleBase_ModelWidget* aWgt, myWidgets) {
574 aWgt->setEditingMode(isEditing);
578 void XGUI_PropertyPanel::setupActions(XGUI_ActionsMgr* theMgr)
580 QStringList aButtonNames;
581 aButtonNames << PROP_PANEL_OK<< PROP_PANEL_OK_PLUS << PROP_PANEL_CANCEL
582 << PROP_PANEL_HELP << PROP_PANEL_PREVIEW;
583 QList<XGUI_ActionsMgr::OperationStateActionId> aActionIds;
584 aActionIds << XGUI_ActionsMgr::Accept << XGUI_ActionsMgr::AcceptPlus << XGUI_ActionsMgr::Abort
585 << XGUI_ActionsMgr::Help << XGUI_ActionsMgr::Preview;
586 for (int i = 0; i < aButtonNames.size(); ++i) {
587 QToolButton* aBtn = findButton(aButtonNames.at(i).toStdString().c_str());
588 QAction* anAct = theMgr->operationStateAction(aActionIds.at(i));
589 aBtn->setDefaultAction(anAct);
593 void XGUI_PropertyPanel::onAcceptData()
595 foreach (ModuleBase_ModelWidget* aWidget, myWidgets) {
596 aWidget->onFeatureAccepted();
600 void XGUI_PropertyPanel::setInternalActiveWidget(ModuleBase_ModelWidget* theWidget)
604 myInternalActiveWidget = theWidget;
605 emit propertyPanelActivated();
609 if (myInternalActiveWidget)
611 delete myInternalActiveWidget;
612 myInternalActiveWidget = 0;
614 emit propertyPanelDeactivated();
616 myOperationMgr->workshop()->selectionActivate()->updateSelectionModes();
617 myOperationMgr->workshop()->selectionActivate()->updateSelectionFilters();
620 ModuleBase_ModelWidget* XGUI_PropertyPanel::preselectionWidget() const
622 return myPreselectionWidget;
625 void XGUI_PropertyPanel::setPreselectionWidget(ModuleBase_ModelWidget* theWidget)
627 myPreselectionWidget = theWidget;
631 void XGUI_PropertyPanel::closeEvent(QCloseEvent* theEvent)
633 ModuleBase_Operation* aOp = myOperationMgr->currentOperation();
635 if (myOperationMgr->canStopOperation(aOp)) {
636 myOperationMgr->abortOperation(aOp);
637 if (myOperationMgr->hasOperation())
644 ModuleBase_IPropertyPanel::closeEvent(theEvent);
647 QToolButton* XGUI_PropertyPanel::findButton(const char* theInternalName) const
649 return findChild<QToolButton*>(theInternalName);