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_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>
39 #include <Events_Loop.h>
41 #include <ModelAPI_Session.h>
42 #include <ModelAPI_Validator.h>
48 #include <QLayoutItem>
49 #include <QToolButton>
50 #include <QVBoxLayout>
51 #include <QGridLayout>
53 #include <QToolButton>
60 //#define DEBUG_TAB_WIDGETS
62 //#define DEBUG_ACTIVE_WIDGET
64 XGUI_PropertyPanel::XGUI_PropertyPanel(QWidget* theParent, XGUI_OperationMgr* theMgr)
65 : ModuleBase_IPropertyPanel(theParent),
67 myPreselectionWidget(NULL),
69 myOperationMgr(theMgr)
71 setWindowTitle(tr("Property Panel"));
72 QAction* aViewAct = toggleViewAction();
73 setObjectName(PROP_PANEL);
74 setStyleSheet("::title { position: relative; padding-left: 5px; text-align: left center }");
76 QWidget* aContent = new QWidget(this);
77 QGridLayout* aMainLayout = new QGridLayout(aContent);
78 const int kPanelColumn = 0;
80 aMainLayout->setContentsMargins(3, 3, 3, 3);
83 QFrame* aFrm = new QFrame(aContent);
84 aFrm->setFrameStyle(QFrame::Raised);
85 aFrm->setFrameShape(QFrame::Panel);
86 QHBoxLayout* aBtnLay = new QHBoxLayout(aFrm);
87 ModuleBase_Tools::zeroMargins(aBtnLay);
88 aMainLayout->addWidget(aFrm, aPanelRow++, kPanelColumn);
90 myHeaderWidget = aFrm;
92 QStringList aBtnNames;
93 aBtnNames << QString(PROP_PANEL_HELP)
94 << QString(PROP_PANEL_OK)
95 << QString(PROP_PANEL_CANCEL);
96 foreach(QString eachBtnName, aBtnNames) {
97 QToolButton* aBtn = new QToolButton(aFrm);
98 aBtn->setObjectName(eachBtnName);
99 aBtn->setAutoRaise(true);
100 aBtnLay->addWidget(aBtn);
102 aBtnLay->insertStretch(1, 1);
104 myPanelPage = new ModuleBase_PageWidget(aContent);
105 myPanelPage->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::MinimumExpanding);
106 aMainLayout->addWidget(myPanelPage, aPanelRow, kPanelColumn);
108 // spit to make the preview button on the bottom of the panel
109 aMainLayout->setRowStretch(aPanelRow++, 1);
111 // preview button on the bottom of panel
112 aFrm = new QFrame(aContent);
113 aBtnLay = new QHBoxLayout(aFrm);
114 aBtnLay->addStretch(1);
115 ModuleBase_Tools::zeroMargins(aBtnLay);
116 aMainLayout->addWidget(aFrm, aPanelRow++, kPanelColumn);
118 QToolButton* aBtn = new QToolButton(aFrm);
119 aBtn->setObjectName(PROP_PANEL_PREVIEW);
120 aBtnLay->addWidget(aBtn);
123 XGUI_PropertyPanel::~XGUI_PropertyPanel()
127 void XGUI_PropertyPanel::cleanContent()
130 myActiveWidget->deactivate();
132 XGUI_ActiveControlSelector* aPPSelector = XGUI_Tools::workshop(myOperationMgr->workshop())->
133 activeControlMgr()->getSelector(XGUI_PropertyPanelSelector::Type());
134 aPPSelector->reset(); // it removes need to be updated widget link
136 /// as the widgets are deleted later, it is important that the signals
137 /// of these widgets are not processed. An example of the error is issue 986.
138 /// In the given case, the property panel is firstly filled by new widgets
139 /// of restarted operation and after that the mouse release signal come from
140 /// the widget of the previous operation (Point2d widget about mouse is released
141 /// and focus is out of this widget)
142 QList<ModuleBase_ModelWidget*>::const_iterator anIt = myWidgets.begin(),
143 aLast = myWidgets.end();
144 for (; anIt != aLast; anIt++) {
145 QWidget* aWidget = *anIt;
147 aWidget->blockSignals(true);
152 myPanelPage->clearPage();
153 myActiveWidget = NULL;
154 #ifdef DEBUG_ACTIVE_WIDGET
155 std::cout << "myActiveWidget = NULL" << std::endl;
158 findButton(PROP_PANEL_PREVIEW)->setVisible(false); /// by default it is hidden
159 setWindowTitle(tr("Property Panel"));
162 void XGUI_PropertyPanel::setModelWidgets(const QList<ModuleBase_ModelWidget*>& theWidgets)
164 myWidgets = theWidgets;
165 if (theWidgets.empty()) return;
166 foreach (ModuleBase_ModelWidget* aWidget, theWidgets) {
167 connect(aWidget, SIGNAL(focusInWidget(ModuleBase_ModelWidget*)),
168 this, SLOT(onFocusInWidget(ModuleBase_ModelWidget*)));
169 connect(aWidget, SIGNAL(focusOutWidget(ModuleBase_ModelWidget*)),
170 this, SLOT(onActivateNextWidget(ModuleBase_ModelWidget*)));
171 connect(aWidget, SIGNAL(keyReleased(QObject*, QKeyEvent*)),
172 this, SIGNAL(keyReleased(QObject*, QKeyEvent*)));
173 connect(aWidget, SIGNAL(enterClicked(QObject*)),
174 this, SIGNAL(enterClicked(QObject*)));
179 const QList<ModuleBase_ModelWidget*>& XGUI_PropertyPanel::modelWidgets() const
184 ModuleBase_PageBase* XGUI_PropertyPanel::contentWidget()
186 return static_cast<ModuleBase_PageBase*>(myPanelPage);
189 void XGUI_PropertyPanel::updateContentWidget(FeaturePtr theFeature)
191 // Invalid feature case on abort of the operation
192 if (theFeature.get() == NULL)
194 if (theFeature->isAction() || !theFeature->data())
196 foreach(ModuleBase_ModelWidget* eachWidget, myWidgets) {
197 if (!eachWidget->feature().get())
198 eachWidget->setFeature(theFeature);
199 eachWidget->restoreValue();
201 // the repaint is used here to immediately react in GUI to the values change.
205 void XGUI_PropertyPanel::createContentPanel(FeaturePtr theFeature)
207 // Invalid feature case on abort of the operation
208 if (theFeature.get() == NULL)
210 if (theFeature->isAction() || !theFeature->data())
213 if (myWidgets.empty()) {
214 ModuleBase_Operation* anOperation = myOperationMgr->currentOperation();
215 QString aXmlRepr = anOperation->getDescription()->xmlRepresentation();
217 ModuleBase_WidgetFactory aFactory(aXmlRepr.toStdString(), myOperationMgr->workshop());
218 aFactory.createPanel(contentWidget(), theFeature);
219 /// Apply button should be update if the feature was modified by the panel
220 myOperationMgr->onValidateOperation();
224 void XGUI_PropertyPanel::activateNextWidget(ModuleBase_ModelWidget* theWidget)
226 // it is possible that the property panel widgets have not been visualized
227 // (e.g. on start operation), so it is strictly important to do not check visualized state
228 activateNextWidget(theWidget, false);
231 void XGUI_PropertyPanel::onFocusInWidget(ModuleBase_ModelWidget* theWidget)
233 #ifdef DEBUG_ACTIVE_WIDGET
234 std::cout << "onFocusInWidget" << std::endl;
236 if (theWidget->canAcceptFocus())
237 activateWidget(theWidget);
240 void XGUI_PropertyPanel::onActivateNextWidget(ModuleBase_ModelWidget* theWidget)
242 // this slot happens when some widget lost focus, the next widget which accepts the focus
243 // should be shown, so the second parameter is true
244 // it is important for features where in cases the same attributes are used, isCase for this
245 // attribute returns true, however it can be placed in hidden stack widget(extrusion: elements,
246 // sketch multi rotation -> single/full point)
247 // it is important to check the widget visibility to do not check of the next widget visible
248 // state if the current is not shown. (example: sketch circle re-entrant operation after mouse
249 // release in the viewer, next, radius,
250 // widget should be activated but the first is not visualized)
251 bool isVisible = theWidget->isVisible();
252 activateNextWidget(theWidget, isVisible);
256 void XGUI_PropertyPanel::activateNextWidget(ModuleBase_ModelWidget* theWidget,
257 const bool isCheckVisibility)
259 // TO CHECK: Editing operation does not have automatical activation of widgets
260 if (isEditingMode()) {
261 activateWidget(NULL);
264 ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
266 QList<ModuleBase_ModelWidget*>::const_iterator anIt = myWidgets.begin(), aLast = myWidgets.end();
267 bool isFoundWidget = false;
268 ModuleBase_Tools::activateWindow(this, "XGUI_PropertyPanel::activateNextWidget()");
269 for (; anIt != aLast; anIt++) {
270 ModuleBase_ModelWidget* aCurrentWidget = *anIt;
271 if (isFoundWidget || !theWidget) {
273 if (!aValidators->isCase(aCurrentWidget->feature(), aCurrentWidget->attributeID()))
274 continue; // this attribute does not participate in the current case
275 if (isCheckVisibility && !aCurrentWidget->isInternal()) {
276 if (!aCurrentWidget->isVisible())
279 if (!aCurrentWidget->isObligatory())
280 continue; // not obligatory widgets are not activated automatically
282 if (!aCurrentWidget->canAcceptFocus())
283 continue; // do not set focus if it can not be accepted, case: optional choice
285 if (aCurrentWidget->focusTo()) {
286 aCurrentWidget->emitFocusInWidget();
290 isFoundWidget = isFoundWidget || (*anIt) == theWidget;
292 // set focus to Ok/Cancel button in Property panel if there are no more active widgets
293 // it should be performed before activateWidget(NULL) because it emits some signals which
294 // can be processed by moudule for example as to activate another widget with setting focus
295 QWidget* aNewFocusWidget = 0;
296 QToolButton* anOkBtn = findButton(PROP_PANEL_OK);
297 if (anOkBtn->isEnabled())
298 aNewFocusWidget = anOkBtn;
300 QToolButton* aCancelBtn = findButton(PROP_PANEL_CANCEL);
301 if (aCancelBtn->isEnabled())
302 aNewFocusWidget = aCancelBtn;
305 ModuleBase_Tools::setFocus(aNewFocusWidget, "XGUI_PropertyPanel::activateNextWidget");
307 activateWidget(NULL);
310 void findDirectChildren(QWidget* theParent, QList<QWidget*>& theWidgets, const bool theDebug)
312 QList<QWidget*> aWidgets;
315 QLayout* aLayout = theParent->layout();
317 for (int i = 0, aCount = aLayout->count(); i < aCount; i++) {
318 QLayoutItem* anItem = aLayout->itemAt(i);
319 QWidget* aWidget = anItem ? anItem->widget() : 0;
321 if (aWidget->isVisible()) {
322 if (aWidget->focusPolicy() != Qt::NoFocus)
323 theWidgets.append(aWidget);
324 findDirectChildren(aWidget, theWidgets, false);
327 else if (anItem->layout()) {
328 QLayout* aLayout = anItem->layout();
329 for (int i = 0, aCount = aLayout->count(); i < aCount; i++) {
330 QLayoutItem* anItem = aLayout->itemAt(i);
331 QWidget* aWidget = anItem ? anItem->widget() : 0;
333 if (aWidget->isVisible()) {
334 if (aWidget->focusPolicy() != Qt::NoFocus)
335 theWidgets.append(aWidget);
336 findDirectChildren(aWidget, theWidgets, false);
340 // TODO: improve recursive search for the case when here QLayout is used
341 // currently, the switch widget uses only 1 level of qlayout in qlayout using for
342 // example for construction plane feature. If there are more levels,
343 // it should be implemented here
350 #ifdef DEBUG_TAB_WIDGETS
352 QStringList aWidgetTypes;
353 QList<QWidget*>::const_iterator anIt = theWidgets.begin(), aLast = theWidgets.end();
354 for (; anIt != aLast; anIt++) {
355 QWidget* aWidget = *anIt;
357 aWidgetTypes.append(aWidget->objectName());
359 QString anInfo = QString("theWidgets[%1]: %2")
360 .arg(theWidgets.count()).arg(aWidgetTypes.join(","));
361 qDebug(anInfo.toStdString().c_str());
366 bool XGUI_PropertyPanel::setFocusNextPrevChild(bool theIsNext)
368 return focusNextPrevChild(theIsNext);
371 bool XGUI_PropertyPanel::focusNextPrevChild(bool theIsNext)
373 // it wraps the Tabs clicking to follow in the chain:
374 // controls, last control, Apply, Cancel, first control, controls
375 bool isChangedFocus = false;
377 QWidget* aFocusWidget = focusWidget();
378 #ifdef DEBUG_TAB_WIDGETS
380 QString anInfo = QString("focus Widget: %1").arg(aFocusWidget->objectName());
381 qDebug(anInfo.toStdString().c_str());
384 ModuleBase_ModelWidget* aFocusMWidget = ModuleBase_ModelWidget::findModelWidget(this,
387 aFocusMWidget->setHighlighted(false);
389 QWidget* aNewFocusWidget = 0;
391 QList<QWidget*> aChildren;
392 findDirectChildren(this, aChildren, true);
393 int aChildrenCount = aChildren.count();
394 int aFocusWidgetIndex = aChildren.indexOf(aFocusWidget);
395 if (aFocusWidgetIndex >= 0) {
397 if (aFocusWidgetIndex == aChildrenCount-1) {
398 // after the last widget focus should be set to "Apply"
399 QToolButton* anOkBtn = findButton(PROP_PANEL_OK);
400 if (anOkBtn->isEnabled())
401 aNewFocusWidget = anOkBtn;
403 QToolButton* aCancelBtn = findButton(PROP_PANEL_CANCEL);
404 if (aCancelBtn->isEnabled())
405 aNewFocusWidget = aCancelBtn;
409 aNewFocusWidget = aChildren[aFocusWidgetIndex+1];
413 if (aFocusWidgetIndex == 0) {
414 // before the first widget, the last should accept focus
415 aNewFocusWidget = aChildren[aChildrenCount - 1];
418 // before the "Apply" button, the last should accept focus for consistency with "Next"
419 QToolButton* anOkBtn = findButton(PROP_PANEL_OK);
420 if (aFocusWidget == anOkBtn) {
421 aNewFocusWidget = aChildren[aChildrenCount - 1];
424 aNewFocusWidget = aChildren[aFocusWidgetIndex-1];
430 if (aNewFocusWidget) {
431 if (myActiveWidget) {
432 bool isFirstControl = !theIsNext;
433 QWidget* aLastFocusControl = myActiveWidget->getControlAcceptingFocus(isFirstControl);
434 if (aFocusWidget == aLastFocusControl) {
435 setActiveWidget(NULL);
439 // we want to have property panel as an active window to enter values in double control
440 ModuleBase_Tools::setFocus(aNewFocusWidget, "XGUI_PropertyPanel::focusNextPrevChild()");
442 ModuleBase_ModelWidget* aNewFocusMWidget = ModuleBase_ModelWidget::findModelWidget(this,
444 if (aNewFocusMWidget)
445 aNewFocusMWidget->emitFocusInWidget();
446 isChangedFocus = true;
448 return isChangedFocus;
451 void XGUI_PropertyPanel::activateNextWidget()
453 activateNextWidget(myActiveWidget);
456 void XGUI_PropertyPanel::activateWidget(ModuleBase_ModelWidget* theWidget, const bool theEmitSignal)
458 std::string aPreviosAttributeID;
460 aPreviosAttributeID = myActiveWidget->attributeID();
462 // Avoid activation of already actve widget. It could happen on focusIn event many times
463 if (setActiveWidget(theWidget) && theEmitSignal) {
464 emit widgetActivated(myActiveWidget);
465 if (!myActiveWidget && !isEditingMode()) {
466 emit noMoreWidgets(aPreviosAttributeID);
471 bool XGUI_PropertyPanel::setActiveWidget(ModuleBase_ModelWidget* theWidget)
473 // Avoid activation of already actve widget. It could happen on focusIn event many times
474 if (theWidget == myActiveWidget) {
477 std::string aPreviosAttributeID;
479 aPreviosAttributeID = myActiveWidget->attributeID();
480 myActiveWidget->processValueState();
481 myActiveWidget->deactivate();
482 myActiveWidget->setHighlighted(false);
485 emit beforeWidgetActivated(theWidget);
486 theWidget->setHighlighted(true);
487 theWidget->activate();
489 myActiveWidget = theWidget;
490 #ifdef DEBUG_ACTIVE_WIDGET
491 std::cout << "myActiveWidget = " << (theWidget ? theWidget->context().c_str() : "") << std::endl;
493 static Events_ID anEvent = Events_Loop::eventByName(EVENT_UPDATE_BY_WIDGET_SELECTION);
494 Events_Loop::loop()->flush(anEvent);
499 void XGUI_PropertyPanel::setFocusOnOkButton()
501 QToolButton* anOkBtn = findButton(PROP_PANEL_OK);
502 ModuleBase_Tools::setFocus(anOkBtn, "XGUI_PropertyPanel::setFocusOnOkButton()");
505 void XGUI_PropertyPanel::setCancelEnabled(bool theEnabled)
507 QToolButton* anCancelBtn = findButton(PROP_PANEL_CANCEL);
508 anCancelBtn->setEnabled(theEnabled);
511 bool XGUI_PropertyPanel::isCancelEnabled() const
513 QToolButton* anCancelBtn = findButton(PROP_PANEL_CANCEL);
514 return anCancelBtn->isEnabled();
517 void XGUI_PropertyPanel::setEditingMode(bool isEditing)
519 ModuleBase_IPropertyPanel::setEditingMode(isEditing);
520 foreach(ModuleBase_ModelWidget* aWgt, myWidgets) {
521 aWgt->setEditingMode(isEditing);
525 void XGUI_PropertyPanel::setupActions(XGUI_ActionsMgr* theMgr)
527 QStringList aButtonNames;
528 aButtonNames << PROP_PANEL_OK << PROP_PANEL_CANCEL << PROP_PANEL_HELP << PROP_PANEL_PREVIEW;
529 QList<XGUI_ActionsMgr::OperationStateActionId> aActionIds;
530 aActionIds << XGUI_ActionsMgr::Accept << XGUI_ActionsMgr::Abort << XGUI_ActionsMgr::Help
531 << XGUI_ActionsMgr::Preview;
532 for (int i = 0; i < aButtonNames.size(); ++i) {
533 QToolButton* aBtn = findButton(aButtonNames.at(i).toStdString().c_str());
534 QAction* anAct = theMgr->operationStateAction(aActionIds.at(i));
535 aBtn->setDefaultAction(anAct);
539 ModuleBase_ModelWidget* XGUI_PropertyPanel::preselectionWidget() const
541 return myPreselectionWidget;
544 void XGUI_PropertyPanel::setPreselectionWidget(ModuleBase_ModelWidget* theWidget)
546 myPreselectionWidget = theWidget;
550 void XGUI_PropertyPanel::closeEvent(QCloseEvent* theEvent)
552 ModuleBase_Operation* aOp = myOperationMgr->currentOperation();
554 if (myOperationMgr->canStopOperation(aOp)) {
555 myOperationMgr->abortOperation(aOp);
556 if (myOperationMgr->hasOperation())
563 ModuleBase_IPropertyPanel::closeEvent(theEvent);
566 QToolButton* XGUI_PropertyPanel::findButton(const char* theInternalName) const
568 return findChild<QToolButton*>(theInternalName);