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>
56 #include <QScrollArea>
62 //#define DEBUG_TAB_WIDGETS
64 //#define DEBUG_ACTIVE_WIDGET
66 XGUI_PropertyPanel::XGUI_PropertyPanel(QWidget* theParent, XGUI_OperationMgr* theMgr)
67 : ModuleBase_IPropertyPanel(theParent),
69 myPreselectionWidget(NULL),
70 myInternalActiveWidget(NULL),
72 myOperationMgr(theMgr)
74 setWindowTitle(tr("Property Panel"));
75 QAction* aViewAct = toggleViewAction();
76 setObjectName(PROP_PANEL);
77 setStyleSheet("::title { position: relative; padding-left: 5px; text-align: left center }");
79 QScrollArea* aScroll = new QScrollArea(this);
80 aScroll->setWidgetResizable(true);
81 aScroll->setFrameStyle(QFrame::NoFrame);
83 QWidget* aContent = new QWidget(aScroll);
84 QGridLayout* aMainLayout = new QGridLayout(aContent);
85 const int kPanelColumn = 0;
87 aMainLayout->setContentsMargins(3, 3, 3, 3);
88 aScroll->setWidget(aContent);
92 QFrame* aFrm = new QFrame(aContent);
93 aFrm->setFrameStyle(QFrame::Raised);
94 aFrm->setFrameShape(QFrame::Panel);
95 QHBoxLayout* aBtnLay = new QHBoxLayout(aFrm);
96 ModuleBase_Tools::zeroMargins(aBtnLay);
97 aMainLayout->addWidget(aFrm, aPanelRow++, kPanelColumn);
99 myHeaderWidget = aFrm;
101 QStringList aBtnNames;
102 aBtnNames << QString(PROP_PANEL_HELP)
103 << QString(PROP_PANEL_OK)
104 << QString(PROP_PANEL_OK_PLUS)
105 << QString(PROP_PANEL_CANCEL);
106 foreach(QString eachBtnName, aBtnNames) {
107 QToolButton* aBtn = new QToolButton(aFrm);
108 aBtn->setObjectName(eachBtnName);
109 aBtn->setAutoRaise(true);
110 aBtnLay->addWidget(aBtn);
112 aBtnLay->insertStretch(1, 1);
114 myPanelPage = new ModuleBase_PageWidget(aContent);
115 myPanelPage->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::MinimumExpanding);
116 aMainLayout->addWidget(myPanelPage, aPanelRow, kPanelColumn);
118 // spit to make the preview button on the bottom of the panel
119 aMainLayout->setRowStretch(aPanelRow++, 1);
121 // preview button on the bottom of panel
122 aFrm = new QFrame(aContent);
123 aBtnLay = new QHBoxLayout(aFrm);
124 aBtnLay->addStretch(1);
125 ModuleBase_Tools::zeroMargins(aBtnLay);
126 aMainLayout->addWidget(aFrm, aPanelRow++, kPanelColumn);
128 QToolButton* aBtn = new QToolButton(aFrm);
129 aBtn->setObjectName(PROP_PANEL_PREVIEW);
130 aBtnLay->addWidget(aBtn);
133 XGUI_PropertyPanel::~XGUI_PropertyPanel()
137 void XGUI_PropertyPanel::cleanContent()
140 myActiveWidget->deactivate();
142 XGUI_ActiveControlSelector* aPPSelector = XGUI_Tools::workshop(myOperationMgr->workshop())->
143 activeControlMgr()->getSelector(XGUI_PropertyPanelSelector::Type());
144 aPPSelector->reset(); // it removes need to be updated widget link
146 /// as the widgets are deleted later, it is important that the signals
147 /// of these widgets are not processed. An example of the error is issue 986.
148 /// In the given case, the property panel is firstly filled by new widgets
149 /// of restarted operation and after that the mouse release signal come from
150 /// the widget of the previous operation (Point2d widget about mouse is released
151 /// and focus is out of this widget)
152 QList<ModuleBase_ModelWidget*>::const_iterator anIt = myWidgets.begin(),
153 aLast = myWidgets.end();
154 for (; anIt != aLast; anIt++) {
155 QWidget* aWidget = *anIt;
157 aWidget->blockSignals(true);
162 myPanelPage->clearPage();
163 myActiveWidget = NULL;
164 emit propertyPanelDeactivated();
165 // VSV: It seems that this code is not necessary:
166 // it is called on propertyPanelDeactivated() event
167 //myOperationMgr->workshop()->selectionActivate()->updateSelectionModes();
168 //myOperationMgr->workshop()->selectionActivate()->updateSelectionFilters();
169 #ifdef DEBUG_ACTIVE_WIDGET
170 std::cout << "myActiveWidget = NULL" << std::endl;
173 findButton(PROP_PANEL_PREVIEW)->setVisible(false); /// by default it is hidden
174 setWindowTitle(tr("Property Panel"));
177 void XGUI_PropertyPanel::setModelWidgets(const QList<ModuleBase_ModelWidget*>& theWidgets)
179 myWidgets = theWidgets;
180 if (theWidgets.empty())
182 foreach (ModuleBase_ModelWidget* aWidget, theWidgets) {
183 connect(aWidget, SIGNAL(focusInWidget(ModuleBase_ModelWidget*)),
184 this, SLOT(onFocusInWidget(ModuleBase_ModelWidget*)));
185 connect(aWidget, SIGNAL(focusOutWidget(ModuleBase_ModelWidget*)),
186 this, SLOT(onActivateNextWidget(ModuleBase_ModelWidget*)));
187 connect(aWidget, SIGNAL(keyReleased(QObject*, QKeyEvent*)),
188 this, SIGNAL(keyReleased(QObject*, QKeyEvent*)));
189 connect(aWidget, SIGNAL(enterClicked(QObject*)),
190 this, SIGNAL(enterClicked(QObject*)));
195 const QList<ModuleBase_ModelWidget*>& XGUI_PropertyPanel::modelWidgets() const
200 ModuleBase_PageBase* XGUI_PropertyPanel::contentWidget()
202 return static_cast<ModuleBase_PageBase*>(myPanelPage);
205 void XGUI_PropertyPanel::updateContentWidget(FeaturePtr theFeature)
207 // Invalid feature case on abort of the operation
208 if (theFeature.get() == NULL)
210 if (theFeature->isAction() || !theFeature->data())
212 foreach(ModuleBase_ModelWidget* eachWidget, myWidgets) {
213 if (!eachWidget->feature().get())
214 eachWidget->setFeature(theFeature);
215 eachWidget->restoreValue();
217 // the repaint is used here to immediately react in GUI to the values change.
221 void XGUI_PropertyPanel::createContentPanel(FeaturePtr theFeature)
223 // Invalid feature case on abort of the operation
224 if (theFeature.get() == NULL)
226 if (theFeature->isAction() || !theFeature->data())
229 if (myWidgets.empty()) {
230 ModuleBase_Operation* anOperation = myOperationMgr->currentOperation();
231 QString aXmlRepr = anOperation->getDescription()->xmlRepresentation();
233 ModuleBase_WidgetFactory aFactory(aXmlRepr.toStdString(), myOperationMgr->workshop());
234 aFactory.createPanel(contentWidget(), theFeature);
235 /// Apply button should be update if the feature was modified by the panel
236 myOperationMgr->onValidateOperation();
238 updateApplyPlusButton(theFeature);
241 void XGUI_PropertyPanel::updateApplyPlusButton(FeaturePtr theFeature)
243 if (theFeature.get()) {
244 std::shared_ptr<Config_FeatureMessage> aFeatureInfo =
245 myOperationMgr->workshop()->featureInfo(theFeature->getKind().c_str());
246 if (aFeatureInfo.get()) {
247 findButton(PROP_PANEL_OK_PLUS)->setVisible(aFeatureInfo->isApplyContinue());
251 findButton(PROP_PANEL_OK_PLUS)->setVisible(false);
254 ModuleBase_ModelWidget* XGUI_PropertyPanel::activeWidget(const bool isUseCustomWidget) const
256 if (isUseCustomWidget && myInternalActiveWidget)
257 return myInternalActiveWidget;
259 return myActiveWidget;
262 void XGUI_PropertyPanel::activateNextWidget(ModuleBase_ModelWidget* theWidget)
264 // it is possible that the property panel widgets have not been visualized
265 // (e.g. on start operation), so it is strictly important to do not check visualized state
266 activateNextWidget(theWidget, false);
269 void XGUI_PropertyPanel::onFocusInWidget(ModuleBase_ModelWidget* theWidget)
271 #ifdef DEBUG_ACTIVE_WIDGET
272 std::cout << "onFocusInWidget" << std::endl;
274 if (theWidget->canAcceptFocus())
275 activateWidget(theWidget);
278 void XGUI_PropertyPanel::onActivateNextWidget(ModuleBase_ModelWidget* theWidget)
280 // this slot happens when some widget lost focus, the next widget which accepts the focus
281 // should be shown, so the second parameter is true
282 // it is important for features where in cases the same attributes are used, isCase for this
283 // attribute returns true, however it can be placed in hidden stack widget(extrusion: elements,
284 // sketch multi rotation -> single/full point)
285 // it is important to check the widget visibility to do not check of the next widget visible
286 // state if the current is not shown. (example: sketch circle re-entrant operation after mouse
287 // release in the viewer, next, radius,
288 // widget should be activated but the first is not visualized)
289 bool isVisible = theWidget->isVisible();
290 activateNextWidget(theWidget, isVisible);
294 void XGUI_PropertyPanel::activateNextWidget(ModuleBase_ModelWidget* theWidget,
295 const bool isCheckVisibility)
297 // TO CHECK: Editing operation does not have automatical activation of widgets
298 if (isEditingMode()) {
299 activateWidget(NULL);
302 ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
304 QList<ModuleBase_ModelWidget*>::const_iterator anIt = myWidgets.begin(), aLast = myWidgets.end();
305 bool isFoundWidget = false;
306 for (; anIt != aLast; anIt++) {
307 ModuleBase_ModelWidget* aCurrentWidget = *anIt;
308 if (isFoundWidget || !theWidget) {
310 if (!aValidators->isCase(aCurrentWidget->feature(), aCurrentWidget->attributeID()))
311 continue; // this attribute does not participate in the current case
312 if (isCheckVisibility && !aCurrentWidget->isInternal()) {
313 if (!aCurrentWidget->isVisible())
316 if (!aCurrentWidget->isObligatory())
317 continue; // not obligatory widgets are not activated automatically
319 if (!aCurrentWidget->canAcceptFocus())
320 continue; // do not set focus if it can not be accepted, case: optional choice
322 if (aCurrentWidget->focusTo()) {
323 aCurrentWidget->emitFocusInWidget();
327 isFoundWidget = isFoundWidget || (*anIt) == theWidget;
329 // set focus to Ok/Cancel button in Property panel if there are no more active widgets
330 // it should be performed before activateWidget(NULL) because it emits some signals which
331 // can be processed by moudule for example as to activate another widget with setting focus
332 QWidget* aNewFocusWidget = 0;
333 QToolButton* anOkBtn = findButton(PROP_PANEL_OK);
334 if (anOkBtn->isEnabled())
335 aNewFocusWidget = anOkBtn;
337 QToolButton* aCancelBtn = findButton(PROP_PANEL_CANCEL);
338 if (aCancelBtn->isEnabled())
339 aNewFocusWidget = aCancelBtn;
342 ModuleBase_Tools::setFocus(aNewFocusWidget, "XGUI_PropertyPanel::activateNextWidget");
344 activateWidget(NULL);
347 void findDirectChildren(QWidget* theParent, QList<QWidget*>& theWidgets, const bool theDebug)
349 QList<QWidget*> aWidgets;
352 QLayout* aLayout = theParent->layout();
354 for (int i = 0, aCount = aLayout->count(); i < aCount; i++) {
355 QLayoutItem* anItem = aLayout->itemAt(i);
356 QWidget* aWidget = anItem ? anItem->widget() : 0;
358 if (aWidget->isVisible()) {
359 if (aWidget->focusPolicy() != Qt::NoFocus)
360 theWidgets.append(aWidget);
361 findDirectChildren(aWidget, theWidgets, false);
364 else if (anItem->layout()) {
365 QLayout* aLayout = anItem->layout();
366 for (int i = 0, aCount = aLayout->count(); i < aCount; i++) {
367 QLayoutItem* anItem = aLayout->itemAt(i);
368 QWidget* aWidget = anItem ? anItem->widget() : 0;
370 if (aWidget->isVisible()) {
371 if (aWidget->focusPolicy() != Qt::NoFocus)
372 theWidgets.append(aWidget);
373 findDirectChildren(aWidget, theWidgets, false);
377 // TODO: improve recursive search for the case when here QLayout is used
378 // currently, the switch widget uses only 1 level of qlayout in qlayout using for
379 // example for construction plane feature. If there are more levels,
380 // it should be implemented here
387 #ifdef DEBUG_TAB_WIDGETS
389 QStringList aWidgetTypes;
390 QList<QWidget*>::const_iterator anIt = theWidgets.begin(), aLast = theWidgets.end();
391 for (; anIt != aLast; anIt++) {
392 QWidget* aWidget = *anIt;
394 aWidgetTypes.append(aWidget->objectName());
396 QString anInfo = QString("theWidgets[%1]: %2")
397 .arg(theWidgets.count()).arg(aWidgetTypes.join(","));
398 qDebug(anInfo.toStdString().c_str());
403 bool XGUI_PropertyPanel::setFocusNextPrevChild(bool theIsNext)
405 return focusNextPrevChild(theIsNext);
408 bool XGUI_PropertyPanel::focusNextPrevChild(bool theIsNext)
410 // it wraps the Tabs clicking to follow in the chain:
411 // controls, last control, Apply, Cancel, first control, controls
412 bool isChangedFocus = false;
414 QWidget* aFocusWidget = focusWidget();
415 #ifdef DEBUG_TAB_WIDGETS
417 QString anInfo = QString("focus Widget: %1").arg(aFocusWidget->objectName());
418 qDebug(anInfo.toStdString().c_str());
421 ModuleBase_ModelWidget* aFocusMWidget = ModuleBase_ModelWidget::findModelWidget(this,
424 // aFocusMWidget->setHighlighted(false);
426 QWidget* aNewFocusWidget = 0;
428 QList<QWidget*> aChildren;
429 findDirectChildren(this, aChildren, true);
430 int aChildrenCount = aChildren.count();
431 int aFocusWidgetIndex = aChildren.indexOf(aFocusWidget);
432 QToolButton* anOkBtn = findButton(PROP_PANEL_OK);
433 if (aFocusWidgetIndex >= 0) {
435 if (aFocusWidgetIndex == aChildrenCount-1) {
436 // after the last widget focus should be set to "Apply"
437 if (anOkBtn->isEnabled())
438 aNewFocusWidget = anOkBtn;
440 QToolButton* aCancelBtn = findButton(PROP_PANEL_CANCEL);
441 if (aCancelBtn->isEnabled())
442 aNewFocusWidget = aCancelBtn;
446 aNewFocusWidget = aChildren[aFocusWidgetIndex+1];
450 if (aFocusWidgetIndex == 0) {
451 // before the first widget, the last should accept focus
452 aNewFocusWidget = aChildren[aChildrenCount - 1];
455 // before the "Apply" button, the last should accept focus for consistency with "Next"
456 if (aFocusWidget == anOkBtn) {
457 aNewFocusWidget = aChildren[aChildrenCount - 1];
460 aNewFocusWidget = aChildren[aFocusWidgetIndex-1];
466 if (aNewFocusWidget) {
467 if (myActiveWidget) {
468 bool isFirstControl = !theIsNext;
469 QWidget* aLastFocusControl = myActiveWidget->getControlAcceptingFocus(isFirstControl);
470 if (aFocusWidget == aLastFocusControl) {
471 setActiveWidget(NULL, false);
475 // we want to have property panel as an active window to enter values in double control
476 ModuleBase_Tools::setFocus(aNewFocusWidget, "XGUI_PropertyPanel::focusNextPrevChild()");
478 ModuleBase_ModelWidget* aNewFocusMWidget = ModuleBase_ModelWidget::findModelWidget(this,
480 if (aNewFocusMWidget) {
482 aFocusMWidget->setHighlighted(false);
484 aNewFocusMWidget->emitFocusInWidget();
485 isChangedFocus = true;
488 return isChangedFocus;
491 void XGUI_PropertyPanel::activateNextWidget()
493 activateNextWidget(myActiveWidget);
496 void XGUI_PropertyPanel::activateWidget(ModuleBase_ModelWidget* theWidget, const bool theEmitSignal)
498 std::string aPreviosAttributeID;
500 aPreviosAttributeID = myActiveWidget->attributeID();
502 // Avoid activation of already actve widget. It could happen on focusIn event many times
503 setActiveWidget(theWidget, theEmitSignal);
506 bool XGUI_PropertyPanel::setActiveWidget(ModuleBase_ModelWidget* theWidget, const bool isEmitSignal)
508 // Avoid activation of already actve widget. It could happen on focusIn event many times
509 if (theWidget == myActiveWidget) {
512 std::string aPreviosAttributeID;
513 ModuleBase_ModelWidget* aDeactivatedWidget = NULL, *anActivatedWidget = NULL;
515 aPreviosAttributeID = myActiveWidget->attributeID();
516 myActiveWidget->processValueState();
517 myActiveWidget->deactivate();
518 myActiveWidget->setHighlighted(false);
519 aDeactivatedWidget = myActiveWidget;
522 emit beforeWidgetActivated(theWidget);
523 theWidget->setHighlighted(true);
524 theWidget->activate();
525 anActivatedWidget = theWidget;
527 myActiveWidget = theWidget;
529 #ifdef DEBUG_ACTIVE_WIDGET
530 std::cout << "myActiveWidget = " << (theWidget ? theWidget->context().c_str() : "") << std::endl;
532 bool aHasMoreWidgets = true;
534 //emit widgetActivated(myActiveWidget);
535 if (!myActiveWidget && !isEditingMode()) {
536 aHasMoreWidgets = false;
537 emit noMoreWidgets(aPreviosAttributeID);
541 emit propertyPanelActivated();
543 emit propertyPanelDeactivated();
544 myOperationMgr->workshop()->selectionActivate()->updateSelectionModes();
545 myOperationMgr->workshop()->selectionActivate()->updateSelectionFilters();
547 if (aHasMoreWidgets && aDeactivatedWidget)
548 aDeactivatedWidget->updateAfterDeactivation();
549 if (aHasMoreWidgets && anActivatedWidget)
550 anActivatedWidget->updateAfterActivation();
552 if (aHasMoreWidgets && myActiveWidget)
554 // restore widget selection should be done after selection modes of widget activating
555 static Events_ID anEvent = Events_Loop::eventByName(EVENT_UPDATE_BY_WIDGET_SELECTION);
556 Events_Loop::loop()->flush(anEvent);
561 void XGUI_PropertyPanel::setFocusOnOkButton()
563 QToolButton* anOkBtn = findButton(PROP_PANEL_OK);
564 ModuleBase_Tools::setFocus(anOkBtn, "XGUI_PropertyPanel::setFocusOnOkButton()");
567 void XGUI_PropertyPanel::setCancelEnabled(bool theEnabled)
569 QToolButton* anCancelBtn = findButton(PROP_PANEL_CANCEL);
570 anCancelBtn->setEnabled(theEnabled);
573 bool XGUI_PropertyPanel::isCancelEnabled() const
575 QToolButton* anCancelBtn = findButton(PROP_PANEL_CANCEL);
576 return anCancelBtn->isEnabled();
579 void XGUI_PropertyPanel::setEditingMode(bool isEditing)
581 ModuleBase_IPropertyPanel::setEditingMode(isEditing);
582 foreach(ModuleBase_ModelWidget* aWgt, myWidgets) {
583 aWgt->setEditingMode(isEditing);
587 void XGUI_PropertyPanel::setupActions(XGUI_ActionsMgr* theMgr)
589 QStringList aButtonNames;
590 aButtonNames << PROP_PANEL_OK<< PROP_PANEL_OK_PLUS << PROP_PANEL_CANCEL
591 << PROP_PANEL_HELP << PROP_PANEL_PREVIEW;
592 QList<XGUI_ActionsMgr::OperationStateActionId> aActionIds;
593 aActionIds << XGUI_ActionsMgr::Accept << XGUI_ActionsMgr::AcceptPlus << XGUI_ActionsMgr::Abort
594 << XGUI_ActionsMgr::Help << XGUI_ActionsMgr::Preview;
595 for (int i = 0; i < aButtonNames.size(); ++i) {
596 QToolButton* aBtn = findButton(aButtonNames.at(i).toStdString().c_str());
597 QAction* anAct = theMgr->operationStateAction(aActionIds.at(i));
598 aBtn->setDefaultAction(anAct);
602 void XGUI_PropertyPanel::onAcceptData()
604 foreach (ModuleBase_ModelWidget* aWidget, myWidgets) {
605 aWidget->onFeatureAccepted();
609 void XGUI_PropertyPanel::setInternalActiveWidget(ModuleBase_ModelWidget* theWidget)
613 myInternalActiveWidget = theWidget;
614 emit propertyPanelActivated();
618 if (myInternalActiveWidget)
620 delete myInternalActiveWidget;
621 myInternalActiveWidget = 0;
623 emit propertyPanelDeactivated();
625 myOperationMgr->workshop()->selectionActivate()->updateSelectionModes();
626 myOperationMgr->workshop()->selectionActivate()->updateSelectionFilters();
629 ModuleBase_ModelWidget* XGUI_PropertyPanel::preselectionWidget() const
631 return myPreselectionWidget;
634 void XGUI_PropertyPanel::setPreselectionWidget(ModuleBase_ModelWidget* theWidget)
636 myPreselectionWidget = theWidget;
640 void XGUI_PropertyPanel::closeEvent(QCloseEvent* theEvent)
642 ModuleBase_Operation* aOp = myOperationMgr->currentOperation();
644 if (myOperationMgr->canStopOperation(aOp)) {
645 myOperationMgr->abortOperation(aOp);
646 if (myOperationMgr->hasOperation())
653 ModuleBase_IPropertyPanel::closeEvent(theEvent);
656 QToolButton* XGUI_PropertyPanel::findButton(const char* theInternalName) const
658 return findChild<QToolButton*>(theInternalName);