1 // Copyright (C) 2014-2017 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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include <XGUI_ActionsMgr.h>
22 #include <XGUI_ActiveControlMgr.h>
23 #include <XGUI_SelectionActivate.h>
24 #include <XGUI_ActiveControlSelector.h>
25 #include <XGUI_PropertyPanel.h>
26 #include <XGUI_PropertyPanelSelector.h>
27 #include <XGUI_OperationMgr.h>
28 #include <XGUI_Tools.h>
29 #include <XGUI_Workshop.h>
31 //#include <AppElements_Constants.h>
32 #include <ModuleBase_WidgetMultiSelector.h>
33 #include <ModuleBase_Tools.h>
34 #include <ModuleBase_PageBase.h>
35 #include <ModuleBase_PageWidget.h>
36 #include <ModuleBase_WidgetFactory.h>
37 #include <ModuleBase_OperationDescription.h>
38 #include <ModuleBase_Events.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),
70 myOperationMgr(theMgr)
72 setWindowTitle(tr("Property Panel"));
73 QAction* aViewAct = toggleViewAction();
74 setObjectName(PROP_PANEL);
75 setStyleSheet("::title { position: relative; padding-left: 5px; text-align: left center }");
77 QWidget* aContent = new QWidget(this);
78 QGridLayout* aMainLayout = new QGridLayout(aContent);
79 const int kPanelColumn = 0;
81 aMainLayout->setContentsMargins(3, 3, 3, 3);
84 QFrame* aFrm = new QFrame(aContent);
85 aFrm->setFrameStyle(QFrame::Raised);
86 aFrm->setFrameShape(QFrame::Panel);
87 QHBoxLayout* aBtnLay = new QHBoxLayout(aFrm);
88 ModuleBase_Tools::zeroMargins(aBtnLay);
89 aMainLayout->addWidget(aFrm, aPanelRow++, kPanelColumn);
91 myHeaderWidget = aFrm;
93 QStringList aBtnNames;
94 aBtnNames << QString(PROP_PANEL_HELP)
95 << QString(PROP_PANEL_OK)
96 << QString(PROP_PANEL_OK_PLUS)
97 << QString(PROP_PANEL_CANCEL);
98 foreach(QString eachBtnName, aBtnNames) {
99 QToolButton* aBtn = new QToolButton(aFrm);
100 aBtn->setObjectName(eachBtnName);
101 aBtn->setAutoRaise(true);
102 aBtnLay->addWidget(aBtn);
104 aBtnLay->insertStretch(1, 1);
106 myPanelPage = new ModuleBase_PageWidget(aContent);
107 myPanelPage->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::MinimumExpanding);
108 aMainLayout->addWidget(myPanelPage, aPanelRow, kPanelColumn);
110 // spit to make the preview button on the bottom of the panel
111 aMainLayout->setRowStretch(aPanelRow++, 1);
113 // preview button on the bottom of panel
114 aFrm = new QFrame(aContent);
115 aBtnLay = new QHBoxLayout(aFrm);
116 aBtnLay->addStretch(1);
117 ModuleBase_Tools::zeroMargins(aBtnLay);
118 aMainLayout->addWidget(aFrm, aPanelRow++, kPanelColumn);
120 QToolButton* aBtn = new QToolButton(aFrm);
121 aBtn->setObjectName(PROP_PANEL_PREVIEW);
122 aBtnLay->addWidget(aBtn);
125 XGUI_PropertyPanel::~XGUI_PropertyPanel()
129 void XGUI_PropertyPanel::cleanContent()
132 myActiveWidget->deactivate();
134 XGUI_ActiveControlSelector* aPPSelector = XGUI_Tools::workshop(myOperationMgr->workshop())->
135 activeControlMgr()->getSelector(XGUI_PropertyPanelSelector::Type());
136 aPPSelector->reset(); // it removes need to be updated widget link
138 /// as the widgets are deleted later, it is important that the signals
139 /// of these widgets are not processed. An example of the error is issue 986.
140 /// In the given case, the property panel is firstly filled by new widgets
141 /// of restarted operation and after that the mouse release signal come from
142 /// the widget of the previous operation (Point2d widget about mouse is released
143 /// and focus is out of this widget)
144 QList<ModuleBase_ModelWidget*>::const_iterator anIt = myWidgets.begin(),
145 aLast = myWidgets.end();
146 for (; anIt != aLast; anIt++) {
147 QWidget* aWidget = *anIt;
149 aWidget->blockSignals(true);
154 myPanelPage->clearPage();
155 myActiveWidget = NULL;
156 emit propertyPanelDeactivated();
157 myOperationMgr->workshop()->selectionActivate()->updateSelectionModes();
158 myOperationMgr->workshop()->selectionActivate()->updateSelectionFilters();
159 #ifdef DEBUG_ACTIVE_WIDGET
160 std::cout << "myActiveWidget = NULL" << std::endl;
163 findButton(PROP_PANEL_PREVIEW)->setVisible(false); /// by default it is hidden
164 setWindowTitle(tr("Property Panel"));
167 void XGUI_PropertyPanel::setModelWidgets(const QList<ModuleBase_ModelWidget*>& theWidgets)
169 myWidgets = theWidgets;
170 if (theWidgets.empty())
172 foreach (ModuleBase_ModelWidget* aWidget, theWidgets) {
173 connect(aWidget, SIGNAL(focusInWidget(ModuleBase_ModelWidget*)),
174 this, SLOT(onFocusInWidget(ModuleBase_ModelWidget*)));
175 connect(aWidget, SIGNAL(focusOutWidget(ModuleBase_ModelWidget*)),
176 this, SLOT(onActivateNextWidget(ModuleBase_ModelWidget*)));
177 connect(aWidget, SIGNAL(keyReleased(QObject*, QKeyEvent*)),
178 this, SIGNAL(keyReleased(QObject*, QKeyEvent*)));
179 connect(aWidget, SIGNAL(enterClicked(QObject*)),
180 this, SIGNAL(enterClicked(QObject*)));
184 const QList<ModuleBase_ModelWidget*>& XGUI_PropertyPanel::modelWidgets() const
189 ModuleBase_PageBase* XGUI_PropertyPanel::contentWidget()
191 return static_cast<ModuleBase_PageBase*>(myPanelPage);
194 void XGUI_PropertyPanel::updateContentWidget(FeaturePtr theFeature)
196 // Invalid feature case on abort of the operation
197 if (theFeature.get() == NULL)
199 if (theFeature->isAction() || !theFeature->data())
201 foreach(ModuleBase_ModelWidget* eachWidget, myWidgets) {
202 if (!eachWidget->feature().get())
203 eachWidget->setFeature(theFeature);
204 eachWidget->restoreValue();
206 // the repaint is used here to immediately react in GUI to the values change.
210 void XGUI_PropertyPanel::createContentPanel(FeaturePtr theFeature)
212 // Invalid feature case on abort of the operation
213 if (theFeature.get() == NULL)
215 if (theFeature->isAction() || !theFeature->data())
218 if (myWidgets.empty()) {
219 ModuleBase_Operation* anOperation = myOperationMgr->currentOperation();
220 QString aXmlRepr = anOperation->getDescription()->xmlRepresentation();
222 ModuleBase_WidgetFactory aFactory(aXmlRepr.toStdString(), myOperationMgr->workshop());
223 aFactory.createPanel(contentWidget(), theFeature);
224 /// Apply button should be update if the feature was modified by the panel
225 myOperationMgr->onValidateOperation();
227 std::shared_ptr<Config_FeatureMessage> aFeatureInfo =
228 myOperationMgr->workshop()->featureInfo(theFeature->getKind().c_str());
229 findButton(PROP_PANEL_OK_PLUS)->setVisible(aFeatureInfo->isApplyContinue());
232 void XGUI_PropertyPanel::activateNextWidget(ModuleBase_ModelWidget* theWidget)
234 // it is possible that the property panel widgets have not been visualized
235 // (e.g. on start operation), so it is strictly important to do not check visualized state
236 activateNextWidget(theWidget, false);
239 void XGUI_PropertyPanel::onFocusInWidget(ModuleBase_ModelWidget* theWidget)
241 #ifdef DEBUG_ACTIVE_WIDGET
242 std::cout << "onFocusInWidget" << std::endl;
244 if (theWidget->canAcceptFocus())
245 activateWidget(theWidget);
248 void XGUI_PropertyPanel::onActivateNextWidget(ModuleBase_ModelWidget* theWidget)
250 // this slot happens when some widget lost focus, the next widget which accepts the focus
251 // should be shown, so the second parameter is true
252 // it is important for features where in cases the same attributes are used, isCase for this
253 // attribute returns true, however it can be placed in hidden stack widget(extrusion: elements,
254 // sketch multi rotation -> single/full point)
255 // it is important to check the widget visibility to do not check of the next widget visible
256 // state if the current is not shown. (example: sketch circle re-entrant operation after mouse
257 // release in the viewer, next, radius,
258 // widget should be activated but the first is not visualized)
259 bool isVisible = theWidget->isVisible();
260 activateNextWidget(theWidget, isVisible);
264 void XGUI_PropertyPanel::activateNextWidget(ModuleBase_ModelWidget* theWidget,
265 const bool isCheckVisibility)
267 // TO CHECK: Editing operation does not have automatical activation of widgets
268 if (isEditingMode()) {
269 activateWidget(NULL);
272 ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
274 QList<ModuleBase_ModelWidget*>::const_iterator anIt = myWidgets.begin(), aLast = myWidgets.end();
275 bool isFoundWidget = false;
276 ModuleBase_Tools::activateWindow(this, "XGUI_PropertyPanel::activateNextWidget()");
277 for (; anIt != aLast; anIt++) {
278 ModuleBase_ModelWidget* aCurrentWidget = *anIt;
279 if (isFoundWidget || !theWidget) {
281 if (!aValidators->isCase(aCurrentWidget->feature(), aCurrentWidget->attributeID()))
282 continue; // this attribute does not participate in the current case
283 if (isCheckVisibility && !aCurrentWidget->isInternal()) {
284 if (!aCurrentWidget->isVisible())
287 if (!aCurrentWidget->isObligatory())
288 continue; // not obligatory widgets are not activated automatically
290 if (!aCurrentWidget->canAcceptFocus())
291 continue; // do not set focus if it can not be accepted, case: optional choice
293 if (aCurrentWidget->focusTo()) {
294 aCurrentWidget->emitFocusInWidget();
298 isFoundWidget = isFoundWidget || (*anIt) == theWidget;
300 // set focus to Ok/Cancel button in Property panel if there are no more active widgets
301 // it should be performed before activateWidget(NULL) because it emits some signals which
302 // can be processed by moudule for example as to activate another widget with setting focus
303 QWidget* aNewFocusWidget = 0;
304 QToolButton* anOkBtn = findButton(PROP_PANEL_OK);
305 if (anOkBtn->isEnabled())
306 aNewFocusWidget = anOkBtn;
308 QToolButton* aCancelBtn = findButton(PROP_PANEL_CANCEL);
309 if (aCancelBtn->isEnabled())
310 aNewFocusWidget = aCancelBtn;
313 ModuleBase_Tools::setFocus(aNewFocusWidget, "XGUI_PropertyPanel::activateNextWidget");
315 activateWidget(NULL);
318 void findDirectChildren(QWidget* theParent, QList<QWidget*>& theWidgets, const bool theDebug)
320 QList<QWidget*> aWidgets;
323 QLayout* aLayout = theParent->layout();
325 for (int i = 0, aCount = aLayout->count(); i < aCount; i++) {
326 QLayoutItem* anItem = aLayout->itemAt(i);
327 QWidget* aWidget = anItem ? anItem->widget() : 0;
329 if (aWidget->isVisible()) {
330 if (aWidget->focusPolicy() != Qt::NoFocus)
331 theWidgets.append(aWidget);
332 findDirectChildren(aWidget, theWidgets, false);
335 else if (anItem->layout()) {
336 QLayout* aLayout = anItem->layout();
337 for (int i = 0, aCount = aLayout->count(); i < aCount; i++) {
338 QLayoutItem* anItem = aLayout->itemAt(i);
339 QWidget* aWidget = anItem ? anItem->widget() : 0;
341 if (aWidget->isVisible()) {
342 if (aWidget->focusPolicy() != Qt::NoFocus)
343 theWidgets.append(aWidget);
344 findDirectChildren(aWidget, theWidgets, false);
348 // TODO: improve recursive search for the case when here QLayout is used
349 // currently, the switch widget uses only 1 level of qlayout in qlayout using for
350 // example for construction plane feature. If there are more levels,
351 // it should be implemented here
358 #ifdef DEBUG_TAB_WIDGETS
360 QStringList aWidgetTypes;
361 QList<QWidget*>::const_iterator anIt = theWidgets.begin(), aLast = theWidgets.end();
362 for (; anIt != aLast; anIt++) {
363 QWidget* aWidget = *anIt;
365 aWidgetTypes.append(aWidget->objectName());
367 QString anInfo = QString("theWidgets[%1]: %2")
368 .arg(theWidgets.count()).arg(aWidgetTypes.join(","));
369 qDebug(anInfo.toStdString().c_str());
374 bool XGUI_PropertyPanel::setFocusNextPrevChild(bool theIsNext)
376 return focusNextPrevChild(theIsNext);
379 bool XGUI_PropertyPanel::focusNextPrevChild(bool theIsNext)
381 // it wraps the Tabs clicking to follow in the chain:
382 // controls, last control, Apply, Cancel, first control, controls
383 bool isChangedFocus = false;
385 QWidget* aFocusWidget = focusWidget();
386 #ifdef DEBUG_TAB_WIDGETS
388 QString anInfo = QString("focus Widget: %1").arg(aFocusWidget->objectName());
389 qDebug(anInfo.toStdString().c_str());
392 ModuleBase_ModelWidget* aFocusMWidget = ModuleBase_ModelWidget::findModelWidget(this,
395 aFocusMWidget->setHighlighted(false);
397 QWidget* aNewFocusWidget = 0;
399 QList<QWidget*> aChildren;
400 findDirectChildren(this, aChildren, true);
401 int aChildrenCount = aChildren.count();
402 int aFocusWidgetIndex = aChildren.indexOf(aFocusWidget);
403 QToolButton* anOkBtn = findButton(PROP_PANEL_OK);
404 if (aFocusWidgetIndex >= 0) {
406 if (aFocusWidgetIndex == aChildrenCount-1) {
407 // after the last widget focus should be set to "Apply"
408 if (anOkBtn->isEnabled())
409 aNewFocusWidget = anOkBtn;
411 QToolButton* aCancelBtn = findButton(PROP_PANEL_CANCEL);
412 if (aCancelBtn->isEnabled())
413 aNewFocusWidget = aCancelBtn;
417 aNewFocusWidget = aChildren[aFocusWidgetIndex+1];
421 if (aFocusWidgetIndex == 0) {
422 // before the first widget, the last should accept focus
423 aNewFocusWidget = aChildren[aChildrenCount - 1];
426 // before the "Apply" button, the last should accept focus for consistency with "Next"
427 if (aFocusWidget == anOkBtn) {
428 aNewFocusWidget = aChildren[aChildrenCount - 1];
431 aNewFocusWidget = aChildren[aFocusWidgetIndex-1];
437 if (aNewFocusWidget) {
438 if (myActiveWidget) {
439 bool isFirstControl = !theIsNext;
440 QWidget* aLastFocusControl = myActiveWidget->getControlAcceptingFocus(isFirstControl);
441 if (aFocusWidget == aLastFocusControl) {
442 setActiveWidget(NULL, false);
446 // we want to have property panel as an active window to enter values in double control
447 ModuleBase_Tools::setFocus(aNewFocusWidget, "XGUI_PropertyPanel::focusNextPrevChild()");
449 ModuleBase_ModelWidget* aNewFocusMWidget = ModuleBase_ModelWidget::findModelWidget(this,
451 if (aNewFocusMWidget)
452 aNewFocusMWidget->emitFocusInWidget();
453 isChangedFocus = true;
455 return isChangedFocus;
458 void XGUI_PropertyPanel::activateNextWidget()
460 activateNextWidget(myActiveWidget);
463 void XGUI_PropertyPanel::activateWidget(ModuleBase_ModelWidget* theWidget, const bool theEmitSignal)
465 std::string aPreviosAttributeID;
467 aPreviosAttributeID = myActiveWidget->attributeID();
469 // Avoid activation of already actve widget. It could happen on focusIn event many times
470 setActiveWidget(theWidget, theEmitSignal);
473 bool XGUI_PropertyPanel::setActiveWidget(ModuleBase_ModelWidget* theWidget, const bool isEmitSignal)
475 // Avoid activation of already actve widget. It could happen on focusIn event many times
476 if (theWidget == myActiveWidget) {
479 std::string aPreviosAttributeID;
480 ModuleBase_ModelWidget* aDeactivatedWidget = NULL, *anActivatedWidget = NULL;
482 aPreviosAttributeID = myActiveWidget->attributeID();
483 myActiveWidget->processValueState();
484 myActiveWidget->deactivate();
485 myActiveWidget->setHighlighted(false);
486 aDeactivatedWidget = myActiveWidget;
489 emit beforeWidgetActivated(theWidget);
490 theWidget->setHighlighted(true);
491 theWidget->activate();
492 anActivatedWidget = theWidget;
494 myActiveWidget = theWidget;
496 #ifdef DEBUG_ACTIVE_WIDGET
497 std::cout << "myActiveWidget = " << (theWidget ? theWidget->context().c_str() : "") << std::endl;
499 bool aHasMoreWidgets = true;
501 //emit widgetActivated(myActiveWidget);
502 if (!myActiveWidget && !isEditingMode()) {
503 aHasMoreWidgets = false;
504 emit noMoreWidgets(aPreviosAttributeID);
508 emit propertyPanelActivated();
510 emit propertyPanelDeactivated();
511 myOperationMgr->workshop()->selectionActivate()->updateSelectionModes();
512 myOperationMgr->workshop()->selectionActivate()->updateSelectionFilters();
514 if (aHasMoreWidgets && aDeactivatedWidget)
515 aDeactivatedWidget->updateAfterDeactivation();
516 if (aHasMoreWidgets && anActivatedWidget)
517 anActivatedWidget->updateAfterActivation();
519 if (aHasMoreWidgets && myActiveWidget)
521 // restore widget selection should be done after selection modes of widget activating
522 static Events_ID anEvent = Events_Loop::eventByName(EVENT_UPDATE_BY_WIDGET_SELECTION);
523 Events_Loop::loop()->flush(anEvent);
528 void XGUI_PropertyPanel::setFocusOnOkButton()
530 QToolButton* anOkBtn = findButton(PROP_PANEL_OK);
531 ModuleBase_Tools::setFocus(anOkBtn, "XGUI_PropertyPanel::setFocusOnOkButton()");
534 void XGUI_PropertyPanel::setCancelEnabled(bool theEnabled)
536 QToolButton* anCancelBtn = findButton(PROP_PANEL_CANCEL);
537 anCancelBtn->setEnabled(theEnabled);
540 bool XGUI_PropertyPanel::isCancelEnabled() const
542 QToolButton* anCancelBtn = findButton(PROP_PANEL_CANCEL);
543 return anCancelBtn->isEnabled();
546 void XGUI_PropertyPanel::setEditingMode(bool isEditing)
548 ModuleBase_IPropertyPanel::setEditingMode(isEditing);
549 foreach(ModuleBase_ModelWidget* aWgt, myWidgets) {
550 aWgt->setEditingMode(isEditing);
554 void XGUI_PropertyPanel::setupActions(XGUI_ActionsMgr* theMgr)
556 QStringList aButtonNames;
557 aButtonNames << PROP_PANEL_OK<< PROP_PANEL_OK_PLUS << PROP_PANEL_CANCEL
558 << PROP_PANEL_HELP << PROP_PANEL_PREVIEW;
559 QList<XGUI_ActionsMgr::OperationStateActionId> aActionIds;
560 aActionIds << XGUI_ActionsMgr::Accept << XGUI_ActionsMgr::AcceptPlus << XGUI_ActionsMgr::Abort
561 << XGUI_ActionsMgr::Help << XGUI_ActionsMgr::Preview;
562 for (int i = 0; i < aButtonNames.size(); ++i) {
563 QToolButton* aBtn = findButton(aButtonNames.at(i).toStdString().c_str());
564 QAction* anAct = theMgr->operationStateAction(aActionIds.at(i));
565 aBtn->setDefaultAction(anAct);
567 QToolButton* aBtn = findButton(PROP_PANEL_OK);
568 connect(aBtn->defaultAction(), SIGNAL(triggered(bool)), this, SLOT(onAcceptData()));
569 aBtn = findButton(PROP_PANEL_OK_PLUS);
570 connect(aBtn->defaultAction(), SIGNAL(triggered(bool)), this, SLOT(onAcceptData()));
573 void XGUI_PropertyPanel::onAcceptData()
575 foreach (ModuleBase_ModelWidget* aWidget, myWidgets) {
576 aWidget->onFeatureAccepted();
581 ModuleBase_ModelWidget* XGUI_PropertyPanel::preselectionWidget() const
583 return myPreselectionWidget;
586 void XGUI_PropertyPanel::setPreselectionWidget(ModuleBase_ModelWidget* theWidget)
588 myPreselectionWidget = theWidget;
592 void XGUI_PropertyPanel::closeEvent(QCloseEvent* theEvent)
594 ModuleBase_Operation* aOp = myOperationMgr->currentOperation();
596 if (myOperationMgr->canStopOperation(aOp)) {
597 myOperationMgr->abortOperation(aOp);
598 if (myOperationMgr->hasOperation())
605 ModuleBase_IPropertyPanel::closeEvent(theEvent);
608 QToolButton* XGUI_PropertyPanel::findButton(const char* theInternalName) const
610 return findChild<QToolButton*>(theInternalName);