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 // VSV: It seems that this code is not necessary:
159 // it is called on propertyPanelDeactivated() event
160 //myOperationMgr->workshop()->selectionActivate()->updateSelectionModes();
161 //myOperationMgr->workshop()->selectionActivate()->updateSelectionFilters();
162 #ifdef DEBUG_ACTIVE_WIDGET
163 std::cout << "myActiveWidget = NULL" << std::endl;
166 findButton(PROP_PANEL_PREVIEW)->setVisible(false); /// by default it is hidden
167 setWindowTitle(tr("Property Panel"));
170 void XGUI_PropertyPanel::setModelWidgets(const QList<ModuleBase_ModelWidget*>& theWidgets)
172 myWidgets = theWidgets;
173 if (theWidgets.empty())
175 foreach (ModuleBase_ModelWidget* aWidget, theWidgets) {
176 connect(aWidget, SIGNAL(focusInWidget(ModuleBase_ModelWidget*)),
177 this, SLOT(onFocusInWidget(ModuleBase_ModelWidget*)));
178 connect(aWidget, SIGNAL(focusOutWidget(ModuleBase_ModelWidget*)),
179 this, SLOT(onActivateNextWidget(ModuleBase_ModelWidget*)));
180 connect(aWidget, SIGNAL(keyReleased(QObject*, QKeyEvent*)),
181 this, SIGNAL(keyReleased(QObject*, QKeyEvent*)));
182 connect(aWidget, SIGNAL(enterClicked(QObject*)),
183 this, SIGNAL(enterClicked(QObject*)));
187 const QList<ModuleBase_ModelWidget*>& XGUI_PropertyPanel::modelWidgets() const
192 ModuleBase_PageBase* XGUI_PropertyPanel::contentWidget()
194 return static_cast<ModuleBase_PageBase*>(myPanelPage);
197 void XGUI_PropertyPanel::updateContentWidget(FeaturePtr theFeature)
199 // Invalid feature case on abort of the operation
200 if (theFeature.get() == NULL)
202 if (theFeature->isAction() || !theFeature->data())
204 foreach(ModuleBase_ModelWidget* eachWidget, myWidgets) {
205 if (!eachWidget->feature().get())
206 eachWidget->setFeature(theFeature);
207 eachWidget->restoreValue();
209 // the repaint is used here to immediately react in GUI to the values change.
213 void XGUI_PropertyPanel::createContentPanel(FeaturePtr theFeature)
215 // Invalid feature case on abort of the operation
216 if (theFeature.get() == NULL)
218 if (theFeature->isAction() || !theFeature->data())
221 if (myWidgets.empty()) {
222 ModuleBase_Operation* anOperation = myOperationMgr->currentOperation();
223 QString aXmlRepr = anOperation->getDescription()->xmlRepresentation();
225 ModuleBase_WidgetFactory aFactory(aXmlRepr.toStdString(), myOperationMgr->workshop());
226 aFactory.createPanel(contentWidget(), theFeature);
227 /// Apply button should be update if the feature was modified by the panel
228 myOperationMgr->onValidateOperation();
230 updateApplyPlusButton(theFeature);
233 void XGUI_PropertyPanel::updateApplyPlusButton(FeaturePtr theFeature)
235 if (theFeature.get()) {
236 std::shared_ptr<Config_FeatureMessage> aFeatureInfo =
237 myOperationMgr->workshop()->featureInfo(theFeature->getKind().c_str());
238 if (aFeatureInfo.get()) {
239 findButton(PROP_PANEL_OK_PLUS)->setVisible(aFeatureInfo->isApplyContinue());
243 findButton(PROP_PANEL_OK_PLUS)->setVisible(false);
246 ModuleBase_ModelWidget* XGUI_PropertyPanel::activeWidget(const bool isUseCustomWidget) const
248 if (isUseCustomWidget && myInternalActiveWidget)
249 return myInternalActiveWidget;
251 return myActiveWidget;
254 void XGUI_PropertyPanel::activateNextWidget(ModuleBase_ModelWidget* theWidget)
256 // it is possible that the property panel widgets have not been visualized
257 // (e.g. on start operation), so it is strictly important to do not check visualized state
258 activateNextWidget(theWidget, false);
261 void XGUI_PropertyPanel::onFocusInWidget(ModuleBase_ModelWidget* theWidget)
263 #ifdef DEBUG_ACTIVE_WIDGET
264 std::cout << "onFocusInWidget" << std::endl;
266 if (theWidget->canAcceptFocus())
267 activateWidget(theWidget);
270 void XGUI_PropertyPanel::onActivateNextWidget(ModuleBase_ModelWidget* theWidget)
272 // this slot happens when some widget lost focus, the next widget which accepts the focus
273 // should be shown, so the second parameter is true
274 // it is important for features where in cases the same attributes are used, isCase for this
275 // attribute returns true, however it can be placed in hidden stack widget(extrusion: elements,
276 // sketch multi rotation -> single/full point)
277 // it is important to check the widget visibility to do not check of the next widget visible
278 // state if the current is not shown. (example: sketch circle re-entrant operation after mouse
279 // release in the viewer, next, radius,
280 // widget should be activated but the first is not visualized)
281 bool isVisible = theWidget->isVisible();
282 activateNextWidget(theWidget, isVisible);
286 void XGUI_PropertyPanel::activateNextWidget(ModuleBase_ModelWidget* theWidget,
287 const bool isCheckVisibility)
289 // TO CHECK: Editing operation does not have automatical activation of widgets
290 if (isEditingMode()) {
291 activateWidget(NULL);
294 ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
296 QList<ModuleBase_ModelWidget*>::const_iterator anIt = myWidgets.begin(), aLast = myWidgets.end();
297 bool isFoundWidget = false;
298 for (; anIt != aLast; anIt++) {
299 ModuleBase_ModelWidget* aCurrentWidget = *anIt;
300 if (isFoundWidget || !theWidget) {
302 if (!aValidators->isCase(aCurrentWidget->feature(), aCurrentWidget->attributeID()))
303 continue; // this attribute does not participate in the current case
304 if (isCheckVisibility && !aCurrentWidget->isInternal()) {
305 if (!aCurrentWidget->isVisible())
308 if (!aCurrentWidget->isObligatory())
309 continue; // not obligatory widgets are not activated automatically
311 if (!aCurrentWidget->canAcceptFocus())
312 continue; // do not set focus if it can not be accepted, case: optional choice
314 if (aCurrentWidget->focusTo()) {
315 aCurrentWidget->emitFocusInWidget();
319 isFoundWidget = isFoundWidget || (*anIt) == theWidget;
321 // set focus to Ok/Cancel button in Property panel if there are no more active widgets
322 // it should be performed before activateWidget(NULL) because it emits some signals which
323 // can be processed by moudule for example as to activate another widget with setting focus
324 QWidget* aNewFocusWidget = 0;
325 QToolButton* anOkBtn = findButton(PROP_PANEL_OK);
326 if (anOkBtn->isEnabled())
327 aNewFocusWidget = anOkBtn;
329 QToolButton* aCancelBtn = findButton(PROP_PANEL_CANCEL);
330 if (aCancelBtn->isEnabled())
331 aNewFocusWidget = aCancelBtn;
334 ModuleBase_Tools::setFocus(aNewFocusWidget, "XGUI_PropertyPanel::activateNextWidget");
336 activateWidget(NULL);
339 void findDirectChildren(QWidget* theParent, QList<QWidget*>& theWidgets, const bool theDebug)
341 QList<QWidget*> aWidgets;
344 QLayout* aLayout = theParent->layout();
346 for (int i = 0, aCount = aLayout->count(); i < aCount; i++) {
347 QLayoutItem* anItem = aLayout->itemAt(i);
348 QWidget* aWidget = anItem ? anItem->widget() : 0;
350 if (aWidget->isVisible()) {
351 if (aWidget->focusPolicy() != Qt::NoFocus)
352 theWidgets.append(aWidget);
353 findDirectChildren(aWidget, theWidgets, false);
356 else if (anItem->layout()) {
357 QLayout* aLayout = anItem->layout();
358 for (int i = 0, aCount = aLayout->count(); i < aCount; i++) {
359 QLayoutItem* anItem = aLayout->itemAt(i);
360 QWidget* aWidget = anItem ? anItem->widget() : 0;
362 if (aWidget->isVisible()) {
363 if (aWidget->focusPolicy() != Qt::NoFocus)
364 theWidgets.append(aWidget);
365 findDirectChildren(aWidget, theWidgets, false);
369 // TODO: improve recursive search for the case when here QLayout is used
370 // currently, the switch widget uses only 1 level of qlayout in qlayout using for
371 // example for construction plane feature. If there are more levels,
372 // it should be implemented here
379 #ifdef DEBUG_TAB_WIDGETS
381 QStringList aWidgetTypes;
382 QList<QWidget*>::const_iterator anIt = theWidgets.begin(), aLast = theWidgets.end();
383 for (; anIt != aLast; anIt++) {
384 QWidget* aWidget = *anIt;
386 aWidgetTypes.append(aWidget->objectName());
388 QString anInfo = QString("theWidgets[%1]: %2")
389 .arg(theWidgets.count()).arg(aWidgetTypes.join(","));
390 qDebug(anInfo.toStdString().c_str());
395 bool XGUI_PropertyPanel::setFocusNextPrevChild(bool theIsNext)
397 return focusNextPrevChild(theIsNext);
400 bool XGUI_PropertyPanel::focusNextPrevChild(bool theIsNext)
402 // it wraps the Tabs clicking to follow in the chain:
403 // controls, last control, Apply, Cancel, first control, controls
404 bool isChangedFocus = false;
406 QWidget* aFocusWidget = focusWidget();
407 #ifdef DEBUG_TAB_WIDGETS
409 QString anInfo = QString("focus Widget: %1").arg(aFocusWidget->objectName());
410 qDebug(anInfo.toStdString().c_str());
413 ModuleBase_ModelWidget* aFocusMWidget = ModuleBase_ModelWidget::findModelWidget(this,
416 // aFocusMWidget->setHighlighted(false);
418 QWidget* aNewFocusWidget = 0;
420 QList<QWidget*> aChildren;
421 findDirectChildren(this, aChildren, true);
422 int aChildrenCount = aChildren.count();
423 int aFocusWidgetIndex = aChildren.indexOf(aFocusWidget);
424 QToolButton* anOkBtn = findButton(PROP_PANEL_OK);
425 if (aFocusWidgetIndex >= 0) {
427 if (aFocusWidgetIndex == aChildrenCount-1) {
428 // after the last widget focus should be set to "Apply"
429 if (anOkBtn->isEnabled())
430 aNewFocusWidget = anOkBtn;
432 QToolButton* aCancelBtn = findButton(PROP_PANEL_CANCEL);
433 if (aCancelBtn->isEnabled())
434 aNewFocusWidget = aCancelBtn;
438 aNewFocusWidget = aChildren[aFocusWidgetIndex+1];
442 if (aFocusWidgetIndex == 0) {
443 // before the first widget, the last should accept focus
444 aNewFocusWidget = aChildren[aChildrenCount - 1];
447 // before the "Apply" button, the last should accept focus for consistency with "Next"
448 if (aFocusWidget == anOkBtn) {
449 aNewFocusWidget = aChildren[aChildrenCount - 1];
452 aNewFocusWidget = aChildren[aFocusWidgetIndex-1];
458 if (aNewFocusWidget) {
459 if (myActiveWidget) {
460 bool isFirstControl = !theIsNext;
461 QWidget* aLastFocusControl = myActiveWidget->getControlAcceptingFocus(isFirstControl);
462 if (aFocusWidget == aLastFocusControl) {
463 setActiveWidget(NULL, false);
467 // we want to have property panel as an active window to enter values in double control
468 ModuleBase_Tools::setFocus(aNewFocusWidget, "XGUI_PropertyPanel::focusNextPrevChild()");
470 ModuleBase_ModelWidget* aNewFocusMWidget = ModuleBase_ModelWidget::findModelWidget(this,
472 if (aNewFocusMWidget) {
474 aFocusMWidget->setHighlighted(false);
476 aNewFocusMWidget->emitFocusInWidget();
477 isChangedFocus = true;
480 return isChangedFocus;
483 void XGUI_PropertyPanel::activateNextWidget()
485 activateNextWidget(myActiveWidget);
488 void XGUI_PropertyPanel::activateWidget(ModuleBase_ModelWidget* theWidget, const bool theEmitSignal)
490 std::string aPreviosAttributeID;
492 aPreviosAttributeID = myActiveWidget->attributeID();
494 // Avoid activation of already actve widget. It could happen on focusIn event many times
495 setActiveWidget(theWidget, theEmitSignal);
498 bool XGUI_PropertyPanel::setActiveWidget(ModuleBase_ModelWidget* theWidget, const bool isEmitSignal)
500 // Avoid activation of already actve widget. It could happen on focusIn event many times
501 if (theWidget == myActiveWidget) {
504 std::string aPreviosAttributeID;
505 ModuleBase_ModelWidget* aDeactivatedWidget = NULL, *anActivatedWidget = NULL;
507 aPreviosAttributeID = myActiveWidget->attributeID();
508 myActiveWidget->processValueState();
509 myActiveWidget->deactivate();
510 myActiveWidget->setHighlighted(false);
511 aDeactivatedWidget = myActiveWidget;
514 emit beforeWidgetActivated(theWidget);
515 theWidget->setHighlighted(true);
516 theWidget->activate();
517 anActivatedWidget = theWidget;
519 myActiveWidget = theWidget;
521 #ifdef DEBUG_ACTIVE_WIDGET
522 std::cout << "myActiveWidget = " << (theWidget ? theWidget->context().c_str() : "") << std::endl;
524 bool aHasMoreWidgets = true;
526 //emit widgetActivated(myActiveWidget);
527 if (!myActiveWidget && !isEditingMode()) {
528 aHasMoreWidgets = false;
529 emit noMoreWidgets(aPreviosAttributeID);
533 emit propertyPanelActivated();
535 emit propertyPanelDeactivated();
536 myOperationMgr->workshop()->selectionActivate()->updateSelectionModes();
537 myOperationMgr->workshop()->selectionActivate()->updateSelectionFilters();
539 if (aHasMoreWidgets && aDeactivatedWidget)
540 aDeactivatedWidget->updateAfterDeactivation();
541 if (aHasMoreWidgets && anActivatedWidget)
542 anActivatedWidget->updateAfterActivation();
544 if (aHasMoreWidgets && myActiveWidget)
546 // restore widget selection should be done after selection modes of widget activating
547 static Events_ID anEvent = Events_Loop::eventByName(EVENT_UPDATE_BY_WIDGET_SELECTION);
548 Events_Loop::loop()->flush(anEvent);
553 void XGUI_PropertyPanel::setFocusOnOkButton()
555 QToolButton* anOkBtn = findButton(PROP_PANEL_OK);
556 ModuleBase_Tools::setFocus(anOkBtn, "XGUI_PropertyPanel::setFocusOnOkButton()");
559 void XGUI_PropertyPanel::setCancelEnabled(bool theEnabled)
561 QToolButton* anCancelBtn = findButton(PROP_PANEL_CANCEL);
562 anCancelBtn->setEnabled(theEnabled);
565 bool XGUI_PropertyPanel::isCancelEnabled() const
567 QToolButton* anCancelBtn = findButton(PROP_PANEL_CANCEL);
568 return anCancelBtn->isEnabled();
571 void XGUI_PropertyPanel::setEditingMode(bool isEditing)
573 ModuleBase_IPropertyPanel::setEditingMode(isEditing);
574 foreach(ModuleBase_ModelWidget* aWgt, myWidgets) {
575 aWgt->setEditingMode(isEditing);
579 void XGUI_PropertyPanel::setupActions(XGUI_ActionsMgr* theMgr)
581 QStringList aButtonNames;
582 aButtonNames << PROP_PANEL_OK<< PROP_PANEL_OK_PLUS << PROP_PANEL_CANCEL
583 << PROP_PANEL_HELP << PROP_PANEL_PREVIEW;
584 QList<XGUI_ActionsMgr::OperationStateActionId> aActionIds;
585 aActionIds << XGUI_ActionsMgr::Accept << XGUI_ActionsMgr::AcceptPlus << XGUI_ActionsMgr::Abort
586 << XGUI_ActionsMgr::Help << XGUI_ActionsMgr::Preview;
587 for (int i = 0; i < aButtonNames.size(); ++i) {
588 QToolButton* aBtn = findButton(aButtonNames.at(i).toStdString().c_str());
589 QAction* anAct = theMgr->operationStateAction(aActionIds.at(i));
590 aBtn->setDefaultAction(anAct);
594 void XGUI_PropertyPanel::onAcceptData()
596 foreach (ModuleBase_ModelWidget* aWidget, myWidgets) {
597 aWidget->onFeatureAccepted();
601 void XGUI_PropertyPanel::setInternalActiveWidget(ModuleBase_ModelWidget* theWidget)
605 myInternalActiveWidget = theWidget;
606 emit propertyPanelActivated();
610 if (myInternalActiveWidget)
612 delete myInternalActiveWidget;
613 myInternalActiveWidget = 0;
615 emit propertyPanelDeactivated();
617 myOperationMgr->workshop()->selectionActivate()->updateSelectionModes();
618 myOperationMgr->workshop()->selectionActivate()->updateSelectionFilters();
621 ModuleBase_ModelWidget* XGUI_PropertyPanel::preselectionWidget() const
623 return myPreselectionWidget;
626 void XGUI_PropertyPanel::setPreselectionWidget(ModuleBase_ModelWidget* theWidget)
628 myPreselectionWidget = theWidget;
632 void XGUI_PropertyPanel::closeEvent(QCloseEvent* theEvent)
634 ModuleBase_Operation* aOp = myOperationMgr->currentOperation();
636 if (myOperationMgr->canStopOperation(aOp)) {
637 myOperationMgr->abortOperation(aOp);
638 if (myOperationMgr->hasOperation())
645 ModuleBase_IPropertyPanel::closeEvent(theEvent);
648 QToolButton* XGUI_PropertyPanel::findButton(const char* theInternalName) const
650 return findChild<QToolButton*>(theInternalName);