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 if (aFeatureInfo.get())
230 findButton(PROP_PANEL_OK_PLUS)->setVisible(aFeatureInfo->isApplyContinue());
233 void XGUI_PropertyPanel::activateNextWidget(ModuleBase_ModelWidget* theWidget)
235 // it is possible that the property panel widgets have not been visualized
236 // (e.g. on start operation), so it is strictly important to do not check visualized state
237 activateNextWidget(theWidget, false);
240 void XGUI_PropertyPanel::onFocusInWidget(ModuleBase_ModelWidget* theWidget)
242 #ifdef DEBUG_ACTIVE_WIDGET
243 std::cout << "onFocusInWidget" << std::endl;
245 if (theWidget->canAcceptFocus())
246 activateWidget(theWidget);
249 void XGUI_PropertyPanel::onActivateNextWidget(ModuleBase_ModelWidget* theWidget)
251 // this slot happens when some widget lost focus, the next widget which accepts the focus
252 // should be shown, so the second parameter is true
253 // it is important for features where in cases the same attributes are used, isCase for this
254 // attribute returns true, however it can be placed in hidden stack widget(extrusion: elements,
255 // sketch multi rotation -> single/full point)
256 // it is important to check the widget visibility to do not check of the next widget visible
257 // state if the current is not shown. (example: sketch circle re-entrant operation after mouse
258 // release in the viewer, next, radius,
259 // widget should be activated but the first is not visualized)
260 bool isVisible = theWidget->isVisible();
261 activateNextWidget(theWidget, isVisible);
265 void XGUI_PropertyPanel::activateNextWidget(ModuleBase_ModelWidget* theWidget,
266 const bool isCheckVisibility)
268 // TO CHECK: Editing operation does not have automatical activation of widgets
269 if (isEditingMode()) {
270 activateWidget(NULL);
273 ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
275 QList<ModuleBase_ModelWidget*>::const_iterator anIt = myWidgets.begin(), aLast = myWidgets.end();
276 bool isFoundWidget = false;
277 ModuleBase_Tools::activateWindow(this, "XGUI_PropertyPanel::activateNextWidget()");
278 for (; anIt != aLast; anIt++) {
279 ModuleBase_ModelWidget* aCurrentWidget = *anIt;
280 if (isFoundWidget || !theWidget) {
282 if (!aValidators->isCase(aCurrentWidget->feature(), aCurrentWidget->attributeID()))
283 continue; // this attribute does not participate in the current case
284 if (isCheckVisibility && !aCurrentWidget->isInternal()) {
285 if (!aCurrentWidget->isVisible())
288 if (!aCurrentWidget->isObligatory())
289 continue; // not obligatory widgets are not activated automatically
291 if (!aCurrentWidget->canAcceptFocus())
292 continue; // do not set focus if it can not be accepted, case: optional choice
294 if (aCurrentWidget->focusTo()) {
295 aCurrentWidget->emitFocusInWidget();
299 isFoundWidget = isFoundWidget || (*anIt) == theWidget;
301 // set focus to Ok/Cancel button in Property panel if there are no more active widgets
302 // it should be performed before activateWidget(NULL) because it emits some signals which
303 // can be processed by moudule for example as to activate another widget with setting focus
304 QWidget* aNewFocusWidget = 0;
305 QToolButton* anOkBtn = findButton(PROP_PANEL_OK);
306 if (anOkBtn->isEnabled())
307 aNewFocusWidget = anOkBtn;
309 QToolButton* aCancelBtn = findButton(PROP_PANEL_CANCEL);
310 if (aCancelBtn->isEnabled())
311 aNewFocusWidget = aCancelBtn;
314 ModuleBase_Tools::setFocus(aNewFocusWidget, "XGUI_PropertyPanel::activateNextWidget");
316 activateWidget(NULL);
319 void findDirectChildren(QWidget* theParent, QList<QWidget*>& theWidgets, const bool theDebug)
321 QList<QWidget*> aWidgets;
324 QLayout* aLayout = theParent->layout();
326 for (int i = 0, aCount = aLayout->count(); i < aCount; i++) {
327 QLayoutItem* anItem = aLayout->itemAt(i);
328 QWidget* aWidget = anItem ? anItem->widget() : 0;
330 if (aWidget->isVisible()) {
331 if (aWidget->focusPolicy() != Qt::NoFocus)
332 theWidgets.append(aWidget);
333 findDirectChildren(aWidget, theWidgets, false);
336 else if (anItem->layout()) {
337 QLayout* aLayout = anItem->layout();
338 for (int i = 0, aCount = aLayout->count(); i < aCount; i++) {
339 QLayoutItem* anItem = aLayout->itemAt(i);
340 QWidget* aWidget = anItem ? anItem->widget() : 0;
342 if (aWidget->isVisible()) {
343 if (aWidget->focusPolicy() != Qt::NoFocus)
344 theWidgets.append(aWidget);
345 findDirectChildren(aWidget, theWidgets, false);
349 // TODO: improve recursive search for the case when here QLayout is used
350 // currently, the switch widget uses only 1 level of qlayout in qlayout using for
351 // example for construction plane feature. If there are more levels,
352 // it should be implemented here
359 #ifdef DEBUG_TAB_WIDGETS
361 QStringList aWidgetTypes;
362 QList<QWidget*>::const_iterator anIt = theWidgets.begin(), aLast = theWidgets.end();
363 for (; anIt != aLast; anIt++) {
364 QWidget* aWidget = *anIt;
366 aWidgetTypes.append(aWidget->objectName());
368 QString anInfo = QString("theWidgets[%1]: %2")
369 .arg(theWidgets.count()).arg(aWidgetTypes.join(","));
370 qDebug(anInfo.toStdString().c_str());
375 bool XGUI_PropertyPanel::setFocusNextPrevChild(bool theIsNext)
377 return focusNextPrevChild(theIsNext);
380 bool XGUI_PropertyPanel::focusNextPrevChild(bool theIsNext)
382 // it wraps the Tabs clicking to follow in the chain:
383 // controls, last control, Apply, Cancel, first control, controls
384 bool isChangedFocus = false;
386 QWidget* aFocusWidget = focusWidget();
387 #ifdef DEBUG_TAB_WIDGETS
389 QString anInfo = QString("focus Widget: %1").arg(aFocusWidget->objectName());
390 qDebug(anInfo.toStdString().c_str());
393 ModuleBase_ModelWidget* aFocusMWidget = ModuleBase_ModelWidget::findModelWidget(this,
396 aFocusMWidget->setHighlighted(false);
398 QWidget* aNewFocusWidget = 0;
400 QList<QWidget*> aChildren;
401 findDirectChildren(this, aChildren, true);
402 int aChildrenCount = aChildren.count();
403 int aFocusWidgetIndex = aChildren.indexOf(aFocusWidget);
404 QToolButton* anOkBtn = findButton(PROP_PANEL_OK);
405 if (aFocusWidgetIndex >= 0) {
407 if (aFocusWidgetIndex == aChildrenCount-1) {
408 // after the last widget focus should be set to "Apply"
409 if (anOkBtn->isEnabled())
410 aNewFocusWidget = anOkBtn;
412 QToolButton* aCancelBtn = findButton(PROP_PANEL_CANCEL);
413 if (aCancelBtn->isEnabled())
414 aNewFocusWidget = aCancelBtn;
418 aNewFocusWidget = aChildren[aFocusWidgetIndex+1];
422 if (aFocusWidgetIndex == 0) {
423 // before the first widget, the last should accept focus
424 aNewFocusWidget = aChildren[aChildrenCount - 1];
427 // before the "Apply" button, the last should accept focus for consistency with "Next"
428 if (aFocusWidget == anOkBtn) {
429 aNewFocusWidget = aChildren[aChildrenCount - 1];
432 aNewFocusWidget = aChildren[aFocusWidgetIndex-1];
438 if (aNewFocusWidget) {
439 if (myActiveWidget) {
440 bool isFirstControl = !theIsNext;
441 QWidget* aLastFocusControl = myActiveWidget->getControlAcceptingFocus(isFirstControl);
442 if (aFocusWidget == aLastFocusControl) {
443 setActiveWidget(NULL, false);
447 // we want to have property panel as an active window to enter values in double control
448 ModuleBase_Tools::setFocus(aNewFocusWidget, "XGUI_PropertyPanel::focusNextPrevChild()");
450 ModuleBase_ModelWidget* aNewFocusMWidget = ModuleBase_ModelWidget::findModelWidget(this,
452 if (aNewFocusMWidget)
453 aNewFocusMWidget->emitFocusInWidget();
454 isChangedFocus = true;
456 return isChangedFocus;
459 void XGUI_PropertyPanel::activateNextWidget()
461 activateNextWidget(myActiveWidget);
464 void XGUI_PropertyPanel::activateWidget(ModuleBase_ModelWidget* theWidget, const bool theEmitSignal)
466 std::string aPreviosAttributeID;
468 aPreviosAttributeID = myActiveWidget->attributeID();
470 // Avoid activation of already actve widget. It could happen on focusIn event many times
471 setActiveWidget(theWidget, theEmitSignal);
474 bool XGUI_PropertyPanel::setActiveWidget(ModuleBase_ModelWidget* theWidget, const bool isEmitSignal)
476 // Avoid activation of already actve widget. It could happen on focusIn event many times
477 if (theWidget == myActiveWidget) {
480 std::string aPreviosAttributeID;
481 ModuleBase_ModelWidget* aDeactivatedWidget = NULL, *anActivatedWidget = NULL;
483 aPreviosAttributeID = myActiveWidget->attributeID();
484 myActiveWidget->processValueState();
485 myActiveWidget->deactivate();
486 myActiveWidget->setHighlighted(false);
487 aDeactivatedWidget = myActiveWidget;
490 emit beforeWidgetActivated(theWidget);
491 theWidget->setHighlighted(true);
492 theWidget->activate();
493 anActivatedWidget = theWidget;
495 myActiveWidget = theWidget;
497 #ifdef DEBUG_ACTIVE_WIDGET
498 std::cout << "myActiveWidget = " << (theWidget ? theWidget->context().c_str() : "") << std::endl;
500 bool aHasMoreWidgets = true;
502 //emit widgetActivated(myActiveWidget);
503 if (!myActiveWidget && !isEditingMode()) {
504 aHasMoreWidgets = false;
505 emit noMoreWidgets(aPreviosAttributeID);
509 emit propertyPanelActivated();
511 emit propertyPanelDeactivated();
512 myOperationMgr->workshop()->selectionActivate()->updateSelectionModes();
513 myOperationMgr->workshop()->selectionActivate()->updateSelectionFilters();
515 if (aHasMoreWidgets && aDeactivatedWidget)
516 aDeactivatedWidget->updateAfterDeactivation();
517 if (aHasMoreWidgets && anActivatedWidget)
518 anActivatedWidget->updateAfterActivation();
520 if (aHasMoreWidgets && myActiveWidget)
522 // restore widget selection should be done after selection modes of widget activating
523 static Events_ID anEvent = Events_Loop::eventByName(EVENT_UPDATE_BY_WIDGET_SELECTION);
524 Events_Loop::loop()->flush(anEvent);
529 void XGUI_PropertyPanel::setFocusOnOkButton()
531 QToolButton* anOkBtn = findButton(PROP_PANEL_OK);
532 ModuleBase_Tools::setFocus(anOkBtn, "XGUI_PropertyPanel::setFocusOnOkButton()");
535 void XGUI_PropertyPanel::setCancelEnabled(bool theEnabled)
537 QToolButton* anCancelBtn = findButton(PROP_PANEL_CANCEL);
538 anCancelBtn->setEnabled(theEnabled);
541 bool XGUI_PropertyPanel::isCancelEnabled() const
543 QToolButton* anCancelBtn = findButton(PROP_PANEL_CANCEL);
544 return anCancelBtn->isEnabled();
547 void XGUI_PropertyPanel::setEditingMode(bool isEditing)
549 ModuleBase_IPropertyPanel::setEditingMode(isEditing);
550 foreach(ModuleBase_ModelWidget* aWgt, myWidgets) {
551 aWgt->setEditingMode(isEditing);
555 void XGUI_PropertyPanel::setupActions(XGUI_ActionsMgr* theMgr)
557 QStringList aButtonNames;
558 aButtonNames << PROP_PANEL_OK<< PROP_PANEL_OK_PLUS << PROP_PANEL_CANCEL
559 << PROP_PANEL_HELP << PROP_PANEL_PREVIEW;
560 QList<XGUI_ActionsMgr::OperationStateActionId> aActionIds;
561 aActionIds << XGUI_ActionsMgr::Accept << XGUI_ActionsMgr::AcceptPlus << XGUI_ActionsMgr::Abort
562 << XGUI_ActionsMgr::Help << XGUI_ActionsMgr::Preview;
563 for (int i = 0; i < aButtonNames.size(); ++i) {
564 QToolButton* aBtn = findButton(aButtonNames.at(i).toStdString().c_str());
565 QAction* anAct = theMgr->operationStateAction(aActionIds.at(i));
566 aBtn->setDefaultAction(anAct);
568 // This code is necessary for remembering of user choice in property panel
569 QToolButton* aBtn = findButton(PROP_PANEL_OK);
571 connect(aBtn->defaultAction(), SIGNAL(triggered(bool)), this, SLOT(onAcceptData()));
572 aBtn = findButton(PROP_PANEL_OK_PLUS);
574 connect(aBtn->defaultAction(), SIGNAL(triggered(bool)), this, SLOT(onAcceptData()));
577 void XGUI_PropertyPanel::onAcceptData()
579 foreach (ModuleBase_ModelWidget* aWidget, myWidgets) {
580 aWidget->onFeatureAccepted();
585 ModuleBase_ModelWidget* XGUI_PropertyPanel::preselectionWidget() const
587 return myPreselectionWidget;
590 void XGUI_PropertyPanel::setPreselectionWidget(ModuleBase_ModelWidget* theWidget)
592 myPreselectionWidget = theWidget;
596 void XGUI_PropertyPanel::closeEvent(QCloseEvent* theEvent)
598 ModuleBase_Operation* aOp = myOperationMgr->currentOperation();
600 if (myOperationMgr->canStopOperation(aOp)) {
601 myOperationMgr->abortOperation(aOp);
602 if (myOperationMgr->hasOperation())
609 ModuleBase_IPropertyPanel::closeEvent(theEvent);
612 QToolButton* XGUI_PropertyPanel::findButton(const char* theInternalName) const
614 return findChild<QToolButton*>(theInternalName);