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_IModule.h>
40 #include <ModuleBase_IWorkshop.h>
42 #include <Events_Loop.h>
44 #include <ModelAPI_Session.h>
45 #include <ModelAPI_Validator.h>
51 #include <QLayoutItem>
52 #include <QToolButton>
53 #include <QVBoxLayout>
54 #include <QGridLayout>
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 QWidget* aContent = new QWidget(this);
80 QGridLayout* aMainLayout = new QGridLayout(aContent);
81 const int kPanelColumn = 0;
83 aMainLayout->setContentsMargins(3, 3, 3, 3);
86 QFrame* aFrm = new QFrame(aContent);
87 aFrm->setFrameStyle(QFrame::Raised);
88 aFrm->setFrameShape(QFrame::Panel);
89 QHBoxLayout* aBtnLay = new QHBoxLayout(aFrm);
90 ModuleBase_Tools::zeroMargins(aBtnLay);
91 aMainLayout->addWidget(aFrm, aPanelRow++, kPanelColumn);
93 myHeaderWidget = aFrm;
95 QStringList aBtnNames;
96 aBtnNames << QString(PROP_PANEL_HELP)
97 << QString(PROP_PANEL_OK)
98 << QString(PROP_PANEL_OK_PLUS)
99 << QString(PROP_PANEL_CANCEL);
100 foreach(QString eachBtnName, aBtnNames) {
101 QToolButton* aBtn = new QToolButton(aFrm);
102 aBtn->setObjectName(eachBtnName);
103 aBtn->setAutoRaise(true);
104 aBtnLay->addWidget(aBtn);
106 aBtnLay->insertStretch(1, 1);
108 myPanelPage = new ModuleBase_PageWidget(aContent);
109 myPanelPage->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::MinimumExpanding);
110 aMainLayout->addWidget(myPanelPage, aPanelRow, kPanelColumn);
112 // spit to make the preview button on the bottom of the panel
113 aMainLayout->setRowStretch(aPanelRow++, 1);
115 // preview button on the bottom of panel
116 aFrm = new QFrame(aContent);
117 aBtnLay = new QHBoxLayout(aFrm);
118 aBtnLay->addStretch(1);
119 ModuleBase_Tools::zeroMargins(aBtnLay);
120 aMainLayout->addWidget(aFrm, aPanelRow++, kPanelColumn);
122 QToolButton* aBtn = new QToolButton(aFrm);
123 aBtn->setObjectName(PROP_PANEL_PREVIEW);
124 aBtnLay->addWidget(aBtn);
127 XGUI_PropertyPanel::~XGUI_PropertyPanel()
131 void XGUI_PropertyPanel::cleanContent()
134 myActiveWidget->deactivate();
136 XGUI_ActiveControlSelector* aPPSelector = XGUI_Tools::workshop(myOperationMgr->workshop())->
137 activeControlMgr()->getSelector(XGUI_PropertyPanelSelector::Type());
138 aPPSelector->reset(); // it removes need to be updated widget link
140 /// as the widgets are deleted later, it is important that the signals
141 /// of these widgets are not processed. An example of the error is issue 986.
142 /// In the given case, the property panel is firstly filled by new widgets
143 /// of restarted operation and after that the mouse release signal come from
144 /// the widget of the previous operation (Point2d widget about mouse is released
145 /// and focus is out of this widget)
146 QList<ModuleBase_ModelWidget*>::const_iterator anIt = myWidgets.begin(),
147 aLast = myWidgets.end();
148 for (; anIt != aLast; anIt++) {
149 QWidget* aWidget = *anIt;
151 aWidget->blockSignals(true);
156 myPanelPage->clearPage();
157 myActiveWidget = NULL;
158 emit propertyPanelDeactivated();
159 myOperationMgr->workshop()->selectionActivate()->updateSelectionModes();
160 myOperationMgr->workshop()->selectionActivate()->updateSelectionFilters();
161 #ifdef DEBUG_ACTIVE_WIDGET
162 std::cout << "myActiveWidget = NULL" << std::endl;
165 findButton(PROP_PANEL_PREVIEW)->setVisible(false); /// by default it is hidden
166 setWindowTitle(tr("Property Panel"));
169 void XGUI_PropertyPanel::setModelWidgets(const QList<ModuleBase_ModelWidget*>& theWidgets)
171 myWidgets = theWidgets;
172 if (theWidgets.empty())
174 foreach (ModuleBase_ModelWidget* aWidget, theWidgets) {
175 connect(aWidget, SIGNAL(focusInWidget(ModuleBase_ModelWidget*)),
176 this, SLOT(onFocusInWidget(ModuleBase_ModelWidget*)));
177 connect(aWidget, SIGNAL(focusOutWidget(ModuleBase_ModelWidget*)),
178 this, SLOT(onActivateNextWidget(ModuleBase_ModelWidget*)));
179 connect(aWidget, SIGNAL(keyReleased(QObject*, QKeyEvent*)),
180 this, SIGNAL(keyReleased(QObject*, QKeyEvent*)));
181 connect(aWidget, SIGNAL(enterClicked(QObject*)),
182 this, SIGNAL(enterClicked(QObject*)));
186 const QList<ModuleBase_ModelWidget*>& XGUI_PropertyPanel::modelWidgets() const
191 ModuleBase_PageBase* XGUI_PropertyPanel::contentWidget()
193 return static_cast<ModuleBase_PageBase*>(myPanelPage);
196 void XGUI_PropertyPanel::updateContentWidget(FeaturePtr theFeature)
198 // Invalid feature case on abort of the operation
199 if (theFeature.get() == NULL)
201 if (theFeature->isAction() || !theFeature->data())
203 foreach(ModuleBase_ModelWidget* eachWidget, myWidgets) {
204 if (!eachWidget->feature().get())
205 eachWidget->setFeature(theFeature);
206 eachWidget->restoreValue();
208 // the repaint is used here to immediately react in GUI to the values change.
212 void XGUI_PropertyPanel::createContentPanel(FeaturePtr theFeature)
214 // Invalid feature case on abort of the operation
215 if (theFeature.get() == NULL)
217 if (theFeature->isAction() || !theFeature->data())
220 if (myWidgets.empty()) {
221 ModuleBase_Operation* anOperation = myOperationMgr->currentOperation();
222 QString aXmlRepr = anOperation->getDescription()->xmlRepresentation();
224 ModuleBase_WidgetFactory aFactory(aXmlRepr.toStdString(), myOperationMgr->workshop());
225 aFactory.createPanel(contentWidget(), theFeature);
226 /// Apply button should be update if the feature was modified by the panel
227 myOperationMgr->onValidateOperation();
229 std::shared_ptr<Config_FeatureMessage> aFeatureInfo =
230 myOperationMgr->workshop()->featureInfo(theFeature->getKind().c_str());
231 if (aFeatureInfo.get())
232 findButton(PROP_PANEL_OK_PLUS)->setVisible(aFeatureInfo->isApplyContinue());
235 ModuleBase_ModelWidget* XGUI_PropertyPanel::activeWidget(const bool isUseCustomWidget) const
237 if (isUseCustomWidget && myInternalActiveWidget)
238 return myInternalActiveWidget;
240 return myActiveWidget;
243 void XGUI_PropertyPanel::activateNextWidget(ModuleBase_ModelWidget* theWidget)
245 // it is possible that the property panel widgets have not been visualized
246 // (e.g. on start operation), so it is strictly important to do not check visualized state
247 activateNextWidget(theWidget, false);
250 void XGUI_PropertyPanel::onFocusInWidget(ModuleBase_ModelWidget* theWidget)
252 #ifdef DEBUG_ACTIVE_WIDGET
253 std::cout << "onFocusInWidget" << std::endl;
255 if (theWidget->canAcceptFocus())
256 activateWidget(theWidget);
259 void XGUI_PropertyPanel::onActivateNextWidget(ModuleBase_ModelWidget* theWidget)
261 // this slot happens when some widget lost focus, the next widget which accepts the focus
262 // should be shown, so the second parameter is true
263 // it is important for features where in cases the same attributes are used, isCase for this
264 // attribute returns true, however it can be placed in hidden stack widget(extrusion: elements,
265 // sketch multi rotation -> single/full point)
266 // it is important to check the widget visibility to do not check of the next widget visible
267 // state if the current is not shown. (example: sketch circle re-entrant operation after mouse
268 // release in the viewer, next, radius,
269 // widget should be activated but the first is not visualized)
270 bool isVisible = theWidget->isVisible();
271 activateNextWidget(theWidget, isVisible);
275 void XGUI_PropertyPanel::activateNextWidget(ModuleBase_ModelWidget* theWidget,
276 const bool isCheckVisibility)
278 // TO CHECK: Editing operation does not have automatical activation of widgets
279 if (isEditingMode()) {
280 activateWidget(NULL);
283 ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
285 QList<ModuleBase_ModelWidget*>::const_iterator anIt = myWidgets.begin(), aLast = myWidgets.end();
286 bool isFoundWidget = false;
287 ModuleBase_Tools::activateWindow(this, "XGUI_PropertyPanel::activateNextWidget()");
288 for (; anIt != aLast; anIt++) {
289 ModuleBase_ModelWidget* aCurrentWidget = *anIt;
290 if (isFoundWidget || !theWidget) {
292 if (!aValidators->isCase(aCurrentWidget->feature(), aCurrentWidget->attributeID()))
293 continue; // this attribute does not participate in the current case
294 if (isCheckVisibility && !aCurrentWidget->isInternal()) {
295 if (!aCurrentWidget->isVisible())
298 if (!aCurrentWidget->isObligatory())
299 continue; // not obligatory widgets are not activated automatically
301 if (!aCurrentWidget->canAcceptFocus())
302 continue; // do not set focus if it can not be accepted, case: optional choice
304 if (aCurrentWidget->focusTo()) {
305 aCurrentWidget->emitFocusInWidget();
309 isFoundWidget = isFoundWidget || (*anIt) == theWidget;
311 // set focus to Ok/Cancel button in Property panel if there are no more active widgets
312 // it should be performed before activateWidget(NULL) because it emits some signals which
313 // can be processed by moudule for example as to activate another widget with setting focus
314 QWidget* aNewFocusWidget = 0;
315 QToolButton* anOkBtn = findButton(PROP_PANEL_OK);
316 if (anOkBtn->isEnabled())
317 aNewFocusWidget = anOkBtn;
319 QToolButton* aCancelBtn = findButton(PROP_PANEL_CANCEL);
320 if (aCancelBtn->isEnabled())
321 aNewFocusWidget = aCancelBtn;
324 ModuleBase_Tools::setFocus(aNewFocusWidget, "XGUI_PropertyPanel::activateNextWidget");
326 activateWidget(NULL);
329 void findDirectChildren(QWidget* theParent, QList<QWidget*>& theWidgets, const bool theDebug)
331 QList<QWidget*> aWidgets;
334 QLayout* aLayout = theParent->layout();
336 for (int i = 0, aCount = aLayout->count(); i < aCount; i++) {
337 QLayoutItem* anItem = aLayout->itemAt(i);
338 QWidget* aWidget = anItem ? anItem->widget() : 0;
340 if (aWidget->isVisible()) {
341 if (aWidget->focusPolicy() != Qt::NoFocus)
342 theWidgets.append(aWidget);
343 findDirectChildren(aWidget, theWidgets, false);
346 else if (anItem->layout()) {
347 QLayout* aLayout = anItem->layout();
348 for (int i = 0, aCount = aLayout->count(); i < aCount; i++) {
349 QLayoutItem* anItem = aLayout->itemAt(i);
350 QWidget* aWidget = anItem ? anItem->widget() : 0;
352 if (aWidget->isVisible()) {
353 if (aWidget->focusPolicy() != Qt::NoFocus)
354 theWidgets.append(aWidget);
355 findDirectChildren(aWidget, theWidgets, false);
359 // TODO: improve recursive search for the case when here QLayout is used
360 // currently, the switch widget uses only 1 level of qlayout in qlayout using for
361 // example for construction plane feature. If there are more levels,
362 // it should be implemented here
369 #ifdef DEBUG_TAB_WIDGETS
371 QStringList aWidgetTypes;
372 QList<QWidget*>::const_iterator anIt = theWidgets.begin(), aLast = theWidgets.end();
373 for (; anIt != aLast; anIt++) {
374 QWidget* aWidget = *anIt;
376 aWidgetTypes.append(aWidget->objectName());
378 QString anInfo = QString("theWidgets[%1]: %2")
379 .arg(theWidgets.count()).arg(aWidgetTypes.join(","));
380 qDebug(anInfo.toStdString().c_str());
385 bool XGUI_PropertyPanel::setFocusNextPrevChild(bool theIsNext)
387 return focusNextPrevChild(theIsNext);
390 bool XGUI_PropertyPanel::focusNextPrevChild(bool theIsNext)
392 // it wraps the Tabs clicking to follow in the chain:
393 // controls, last control, Apply, Cancel, first control, controls
394 bool isChangedFocus = false;
396 QWidget* aFocusWidget = focusWidget();
397 #ifdef DEBUG_TAB_WIDGETS
399 QString anInfo = QString("focus Widget: %1").arg(aFocusWidget->objectName());
400 qDebug(anInfo.toStdString().c_str());
403 ModuleBase_ModelWidget* aFocusMWidget = ModuleBase_ModelWidget::findModelWidget(this,
406 // aFocusMWidget->setHighlighted(false);
408 QWidget* aNewFocusWidget = 0;
410 QList<QWidget*> aChildren;
411 findDirectChildren(this, aChildren, true);
412 int aChildrenCount = aChildren.count();
413 int aFocusWidgetIndex = aChildren.indexOf(aFocusWidget);
414 QToolButton* anOkBtn = findButton(PROP_PANEL_OK);
415 if (aFocusWidgetIndex >= 0) {
417 if (aFocusWidgetIndex == aChildrenCount-1) {
418 // after the last widget focus should be set to "Apply"
419 if (anOkBtn->isEnabled())
420 aNewFocusWidget = anOkBtn;
422 QToolButton* aCancelBtn = findButton(PROP_PANEL_CANCEL);
423 if (aCancelBtn->isEnabled())
424 aNewFocusWidget = aCancelBtn;
428 aNewFocusWidget = aChildren[aFocusWidgetIndex+1];
432 if (aFocusWidgetIndex == 0) {
433 // before the first widget, the last should accept focus
434 aNewFocusWidget = aChildren[aChildrenCount - 1];
437 // before the "Apply" button, the last should accept focus for consistency with "Next"
438 if (aFocusWidget == anOkBtn) {
439 aNewFocusWidget = aChildren[aChildrenCount - 1];
442 aNewFocusWidget = aChildren[aFocusWidgetIndex-1];
448 if (aNewFocusWidget) {
449 if (myActiveWidget) {
450 bool isFirstControl = !theIsNext;
451 QWidget* aLastFocusControl = myActiveWidget->getControlAcceptingFocus(isFirstControl);
452 if (aFocusWidget == aLastFocusControl) {
453 setActiveWidget(NULL, false);
457 // we want to have property panel as an active window to enter values in double control
458 ModuleBase_Tools::setFocus(aNewFocusWidget, "XGUI_PropertyPanel::focusNextPrevChild()");
460 ModuleBase_ModelWidget* aNewFocusMWidget = ModuleBase_ModelWidget::findModelWidget(this,
462 if (aNewFocusMWidget) {
464 aFocusMWidget->setHighlighted(false);
466 aNewFocusMWidget->emitFocusInWidget();
467 isChangedFocus = true;
470 return isChangedFocus;
473 void XGUI_PropertyPanel::activateNextWidget()
475 activateNextWidget(myActiveWidget);
478 void XGUI_PropertyPanel::activateWidget(ModuleBase_ModelWidget* theWidget, const bool theEmitSignal)
480 std::string aPreviosAttributeID;
482 aPreviosAttributeID = myActiveWidget->attributeID();
484 // Avoid activation of already actve widget. It could happen on focusIn event many times
485 setActiveWidget(theWidget, theEmitSignal);
488 bool XGUI_PropertyPanel::setActiveWidget(ModuleBase_ModelWidget* theWidget, const bool isEmitSignal)
490 // Avoid activation of already actve widget. It could happen on focusIn event many times
491 if (theWidget == myActiveWidget) {
494 std::string aPreviosAttributeID;
495 ModuleBase_ModelWidget* aDeactivatedWidget = NULL, *anActivatedWidget = NULL;
497 aPreviosAttributeID = myActiveWidget->attributeID();
498 myActiveWidget->processValueState();
499 myActiveWidget->deactivate();
500 myActiveWidget->setHighlighted(false);
501 aDeactivatedWidget = myActiveWidget;
504 emit beforeWidgetActivated(theWidget);
505 theWidget->setHighlighted(true);
506 theWidget->activate();
507 anActivatedWidget = theWidget;
509 myActiveWidget = theWidget;
511 #ifdef DEBUG_ACTIVE_WIDGET
512 std::cout << "myActiveWidget = " << (theWidget ? theWidget->context().c_str() : "") << std::endl;
514 bool aHasMoreWidgets = true;
516 //emit widgetActivated(myActiveWidget);
517 if (!myActiveWidget && !isEditingMode()) {
518 aHasMoreWidgets = false;
519 emit noMoreWidgets(aPreviosAttributeID);
523 emit propertyPanelActivated();
525 emit propertyPanelDeactivated();
526 myOperationMgr->workshop()->selectionActivate()->updateSelectionModes();
527 myOperationMgr->workshop()->selectionActivate()->updateSelectionFilters();
529 if (aHasMoreWidgets && aDeactivatedWidget)
530 aDeactivatedWidget->updateAfterDeactivation();
531 if (aHasMoreWidgets && anActivatedWidget)
532 anActivatedWidget->updateAfterActivation();
534 if (aHasMoreWidgets && myActiveWidget)
536 // restore widget selection should be done after selection modes of widget activating
537 static Events_ID anEvent = Events_Loop::eventByName(EVENT_UPDATE_BY_WIDGET_SELECTION);
538 Events_Loop::loop()->flush(anEvent);
543 void XGUI_PropertyPanel::setFocusOnOkButton()
545 QToolButton* anOkBtn = findButton(PROP_PANEL_OK);
546 ModuleBase_Tools::setFocus(anOkBtn, "XGUI_PropertyPanel::setFocusOnOkButton()");
549 void XGUI_PropertyPanel::setCancelEnabled(bool theEnabled)
551 QToolButton* anCancelBtn = findButton(PROP_PANEL_CANCEL);
552 anCancelBtn->setEnabled(theEnabled);
555 bool XGUI_PropertyPanel::isCancelEnabled() const
557 QToolButton* anCancelBtn = findButton(PROP_PANEL_CANCEL);
558 return anCancelBtn->isEnabled();
561 void XGUI_PropertyPanel::setEditingMode(bool isEditing)
563 ModuleBase_IPropertyPanel::setEditingMode(isEditing);
564 foreach(ModuleBase_ModelWidget* aWgt, myWidgets) {
565 aWgt->setEditingMode(isEditing);
569 void XGUI_PropertyPanel::setupActions(XGUI_ActionsMgr* theMgr)
571 QStringList aButtonNames;
572 aButtonNames << PROP_PANEL_OK<< PROP_PANEL_OK_PLUS << PROP_PANEL_CANCEL
573 << PROP_PANEL_HELP << PROP_PANEL_PREVIEW;
574 QList<XGUI_ActionsMgr::OperationStateActionId> aActionIds;
575 aActionIds << XGUI_ActionsMgr::Accept << XGUI_ActionsMgr::AcceptPlus << XGUI_ActionsMgr::Abort
576 << XGUI_ActionsMgr::Help << XGUI_ActionsMgr::Preview;
577 for (int i = 0; i < aButtonNames.size(); ++i) {
578 QToolButton* aBtn = findButton(aButtonNames.at(i).toStdString().c_str());
579 QAction* anAct = theMgr->operationStateAction(aActionIds.at(i));
580 aBtn->setDefaultAction(anAct);
584 void XGUI_PropertyPanel::onAcceptData()
586 foreach (ModuleBase_ModelWidget* aWidget, myWidgets) {
587 aWidget->onFeatureAccepted();
591 void XGUI_PropertyPanel::setInternalActiveWidget(ModuleBase_ModelWidget* theWidget)
595 myInternalActiveWidget = theWidget;
596 emit propertyPanelActivated();
600 if (myInternalActiveWidget)
602 delete myInternalActiveWidget;
603 myInternalActiveWidget = 0;
605 emit propertyPanelDeactivated();
607 myOperationMgr->workshop()->selectionActivate()->updateSelectionModes();
608 myOperationMgr->workshop()->selectionActivate()->updateSelectionFilters();
611 ModuleBase_ModelWidget* XGUI_PropertyPanel::preselectionWidget() const
613 return myPreselectionWidget;
616 void XGUI_PropertyPanel::setPreselectionWidget(ModuleBase_ModelWidget* theWidget)
618 myPreselectionWidget = theWidget;
622 void XGUI_PropertyPanel::closeEvent(QCloseEvent* theEvent)
624 ModuleBase_Operation* aOp = myOperationMgr->currentOperation();
626 if (myOperationMgr->canStopOperation(aOp)) {
627 myOperationMgr->abortOperation(aOp);
628 if (myOperationMgr->hasOperation())
635 ModuleBase_IPropertyPanel::closeEvent(theEvent);
638 QToolButton* XGUI_PropertyPanel::findButton(const char* theInternalName) const
640 return findChild<QToolButton*>(theInternalName);