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_PropertyPanel.h>
22 #include <XGUI_ActionsMgr.h>
23 #include <XGUI_OperationMgr.h>
24 //#include <AppElements_Constants.h>
25 #include <ModuleBase_WidgetMultiSelector.h>
26 #include <ModuleBase_Tools.h>
27 #include <ModuleBase_PageBase.h>
28 #include <ModuleBase_PageWidget.h>
29 #include <ModuleBase_WidgetFactory.h>
30 #include <ModuleBase_OperationDescription.h>
31 #include <ModuleBase_Events.h>
33 #include <Events_Loop.h>
35 #include <ModelAPI_Session.h>
36 #include <ModelAPI_Validator.h>
42 #include <QLayoutItem>
43 #include <QToolButton>
44 #include <QVBoxLayout>
45 #include <QGridLayout>
47 #include <QToolButton>
54 //#define DEBUG_TAB_WIDGETS
56 //#define DEBUG_ACTIVE_WIDGET
58 XGUI_PropertyPanel::XGUI_PropertyPanel(QWidget* theParent, XGUI_OperationMgr* theMgr)
59 : ModuleBase_IPropertyPanel(theParent),
61 myPreselectionWidget(NULL),
63 myOperationMgr(theMgr)
65 setWindowTitle(tr("Property Panel"));
66 QAction* aViewAct = toggleViewAction();
67 setObjectName(PROP_PANEL);
68 setStyleSheet("::title { position: relative; padding-left: 5px; text-align: left center }");
70 QWidget* aContent = new QWidget(this);
71 QGridLayout* aMainLayout = new QGridLayout(aContent);
72 const int kPanelColumn = 0;
74 aMainLayout->setContentsMargins(3, 3, 3, 3);
77 QFrame* aFrm = new QFrame(aContent);
78 aFrm->setFrameStyle(QFrame::Raised);
79 aFrm->setFrameShape(QFrame::Panel);
80 QHBoxLayout* aBtnLay = new QHBoxLayout(aFrm);
81 ModuleBase_Tools::zeroMargins(aBtnLay);
82 aMainLayout->addWidget(aFrm, aPanelRow++, kPanelColumn);
84 myHeaderWidget = aFrm;
86 QStringList aBtnNames;
87 aBtnNames << QString(PROP_PANEL_HELP)
88 << QString(PROP_PANEL_OK)
89 << QString(PROP_PANEL_CANCEL);
90 foreach(QString eachBtnName, aBtnNames) {
91 QToolButton* aBtn = new QToolButton(aFrm);
92 aBtn->setObjectName(eachBtnName);
93 aBtn->setAutoRaise(true);
94 aBtnLay->addWidget(aBtn);
96 aBtnLay->insertStretch(1, 1);
98 myPanelPage = new ModuleBase_PageWidget(aContent);
99 myPanelPage->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::MinimumExpanding);
100 aMainLayout->addWidget(myPanelPage, aPanelRow, kPanelColumn);
102 // spit to make the preview button on the bottom of the panel
103 aMainLayout->setRowStretch(aPanelRow++, 1);
105 // preview button on the bottom of panel
106 aFrm = new QFrame(aContent);
107 aBtnLay = new QHBoxLayout(aFrm);
108 aBtnLay->addStretch(1);
109 ModuleBase_Tools::zeroMargins(aBtnLay);
110 aMainLayout->addWidget(aFrm, aPanelRow++, kPanelColumn);
112 QToolButton* aBtn = new QToolButton(aFrm);
113 aBtn->setObjectName(PROP_PANEL_PREVIEW);
114 aBtnLay->addWidget(aBtn);
117 XGUI_PropertyPanel::~XGUI_PropertyPanel()
121 void XGUI_PropertyPanel::cleanContent()
124 myActiveWidget->deactivate();
126 /// as the widgets are deleted later, it is important that the signals
127 /// of these widgets are not processed. An example of the error is issue 986.
128 /// In the given case, the property panel is firstly filled by new widgets
129 /// of restarted operation and after that the mouse release signal come from
130 /// the widget of the previous operation (Point2d widget about mouse is released
131 /// and focus is out of this widget)
132 QList<ModuleBase_ModelWidget*>::const_iterator anIt = myWidgets.begin(),
133 aLast = myWidgets.end();
134 for (; anIt != aLast; anIt++) {
135 QWidget* aWidget = *anIt;
137 aWidget->blockSignals(true);
142 myPanelPage->clearPage();
143 myActiveWidget = NULL;
144 #ifdef DEBUG_ACTIVE_WIDGET
145 std::cout << "myActiveWidget = NULL" << std::endl;
148 findButton(PROP_PANEL_PREVIEW)->setVisible(false); /// by default it is hidden
149 setWindowTitle(tr("Property Panel"));
152 void XGUI_PropertyPanel::setModelWidgets(const QList<ModuleBase_ModelWidget*>& theWidgets)
154 myWidgets = theWidgets;
155 if (theWidgets.empty()) return;
156 foreach (ModuleBase_ModelWidget* aWidget, theWidgets) {
157 connect(aWidget, SIGNAL(focusInWidget(ModuleBase_ModelWidget*)),
158 this, SLOT(onFocusInWidget(ModuleBase_ModelWidget*)));
159 connect(aWidget, SIGNAL(focusOutWidget(ModuleBase_ModelWidget*)),
160 this, SLOT(onActivateNextWidget(ModuleBase_ModelWidget*)));
161 connect(aWidget, SIGNAL(keyReleased(QObject*, QKeyEvent*)),
162 this, SIGNAL(keyReleased(QObject*, QKeyEvent*)));
163 connect(aWidget, SIGNAL(enterClicked(QObject*)),
164 this, SIGNAL(enterClicked(QObject*)));
169 const QList<ModuleBase_ModelWidget*>& XGUI_PropertyPanel::modelWidgets() const
174 ModuleBase_PageBase* XGUI_PropertyPanel::contentWidget()
176 return static_cast<ModuleBase_PageBase*>(myPanelPage);
179 void XGUI_PropertyPanel::updateContentWidget(FeaturePtr theFeature)
181 // Invalid feature case on abort of the operation
182 if (theFeature.get() == NULL)
184 if (theFeature->isAction() || !theFeature->data())
186 foreach(ModuleBase_ModelWidget* eachWidget, myWidgets) {
187 if (!eachWidget->feature().get())
188 eachWidget->setFeature(theFeature);
189 eachWidget->restoreValue();
191 // the repaint is used here to immediately react in GUI to the values change.
195 void XGUI_PropertyPanel::createContentPanel(FeaturePtr theFeature)
197 // Invalid feature case on abort of the operation
198 if (theFeature.get() == NULL)
200 if (theFeature->isAction() || !theFeature->data())
203 if (myWidgets.empty()) {
204 ModuleBase_Operation* anOperation = myOperationMgr->currentOperation();
205 QString aXmlRepr = anOperation->getDescription()->xmlRepresentation();
207 ModuleBase_WidgetFactory aFactory(aXmlRepr.toStdString(), myOperationMgr->workshop());
208 aFactory.createPanel(contentWidget(), theFeature);
209 /// Apply button should be update if the feature was modified by the panel
210 myOperationMgr->onValidateOperation();
214 void XGUI_PropertyPanel::activateNextWidget(ModuleBase_ModelWidget* theWidget)
216 // it is possible that the property panel widgets have not been visualized
217 // (e.g. on start operation), so it is strictly important to do not check visualized state
218 activateNextWidget(theWidget, false);
221 void XGUI_PropertyPanel::onFocusInWidget(ModuleBase_ModelWidget* theWidget)
223 #ifdef DEBUG_ACTIVE_WIDGET
224 std::cout << "onFocusInWidget" << std::endl;
226 if (theWidget->canAcceptFocus())
227 activateWidget(theWidget);
230 void XGUI_PropertyPanel::onActivateNextWidget(ModuleBase_ModelWidget* theWidget)
232 // this slot happens when some widget lost focus, the next widget which accepts the focus
233 // should be shown, so the second parameter is true
234 // it is important for features where in cases the same attributes are used, isCase for this
235 // attribute returns true, however it can be placed in hidden stack widget(extrusion: elements,
236 // sketch multi rotation -> single/full point)
237 // it is important to check the widget visibility to do not check of the next widget visible
238 // state if the current is not shown. (example: sketch circle re-entrant operation after mouse
239 // release in the viewer, next, radius,
240 // widget should be activated but the first is not visualized)
241 bool isVisible = theWidget->isVisible();
242 activateNextWidget(theWidget, isVisible);
246 void XGUI_PropertyPanel::activateNextWidget(ModuleBase_ModelWidget* theWidget,
247 const bool isCheckVisibility)
249 // TO CHECK: Editing operation does not have automatical activation of widgets
250 if (isEditingMode()) {
251 activateWidget(NULL);
254 ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
256 QList<ModuleBase_ModelWidget*>::const_iterator anIt = myWidgets.begin(), aLast = myWidgets.end();
257 bool isFoundWidget = false;
258 ModuleBase_Tools::activateWindow(this, "XGUI_PropertyPanel::activateNextWidget()");
259 for (; anIt != aLast; anIt++) {
260 ModuleBase_ModelWidget* aCurrentWidget = *anIt;
261 if (isFoundWidget || !theWidget) {
263 if (!aValidators->isCase(aCurrentWidget->feature(), aCurrentWidget->attributeID()))
264 continue; // this attribute does not participate in the current case
265 if (isCheckVisibility && !aCurrentWidget->isInternal()) {
266 if (!aCurrentWidget->isVisible())
269 if (!aCurrentWidget->isObligatory())
270 continue; // not obligatory widgets are not activated automatically
272 if (!aCurrentWidget->canAcceptFocus())
273 continue; // do not set focus if it can not be accepted, case: optional choice
275 if (aCurrentWidget->focusTo()) {
276 aCurrentWidget->emitFocusInWidget();
280 isFoundWidget = isFoundWidget || (*anIt) == theWidget;
282 // set focus to Ok/Cancel button in Property panel if there are no more active widgets
283 // it should be performed before activateWidget(NULL) because it emits some signals which
284 // can be processed by moudule for example as to activate another widget with setting focus
285 QWidget* aNewFocusWidget = 0;
286 QToolButton* anOkBtn = findButton(PROP_PANEL_OK);
287 if (anOkBtn->isEnabled())
288 aNewFocusWidget = anOkBtn;
290 QToolButton* aCancelBtn = findButton(PROP_PANEL_CANCEL);
291 if (aCancelBtn->isEnabled())
292 aNewFocusWidget = aCancelBtn;
295 ModuleBase_Tools::setFocus(aNewFocusWidget, "XGUI_PropertyPanel::activateNextWidget");
297 activateWidget(NULL);
300 void findDirectChildren(QWidget* theParent, QList<QWidget*>& theWidgets, const bool theDebug)
302 QList<QWidget*> aWidgets;
305 QLayout* aLayout = theParent->layout();
307 for (int i = 0, aCount = aLayout->count(); i < aCount; i++) {
308 QLayoutItem* anItem = aLayout->itemAt(i);
309 QWidget* aWidget = anItem ? anItem->widget() : 0;
311 if (aWidget->isVisible()) {
312 if (aWidget->focusPolicy() != Qt::NoFocus)
313 theWidgets.append(aWidget);
314 findDirectChildren(aWidget, theWidgets, false);
317 else if (anItem->layout()) {
318 QLayout* aLayout = anItem->layout();
319 for (int i = 0, aCount = aLayout->count(); i < aCount; i++) {
320 QLayoutItem* anItem = aLayout->itemAt(i);
321 QWidget* aWidget = anItem ? anItem->widget() : 0;
323 if (aWidget->isVisible()) {
324 if (aWidget->focusPolicy() != Qt::NoFocus)
325 theWidgets.append(aWidget);
326 findDirectChildren(aWidget, theWidgets, false);
330 // TODO: improve recursive search for the case when here QLayout is used
331 // currently, the switch widget uses only 1 level of qlayout in qlayout using for
332 // example for construction plane feature. If there are more levels,
333 // it should be implemented here
340 #ifdef DEBUG_TAB_WIDGETS
342 QStringList aWidgetTypes;
343 QList<QWidget*>::const_iterator anIt = theWidgets.begin(), aLast = theWidgets.end();
344 for (; anIt != aLast; anIt++) {
345 QWidget* aWidget = *anIt;
347 aWidgetTypes.append(aWidget->objectName());
349 QString anInfo = QString("theWidgets[%1]: %2")
350 .arg(theWidgets.count()).arg(aWidgetTypes.join(","));
351 qDebug(anInfo.toStdString().c_str());
356 bool XGUI_PropertyPanel::setFocusNextPrevChild(bool theIsNext)
358 return focusNextPrevChild(theIsNext);
361 bool XGUI_PropertyPanel::focusNextPrevChild(bool theIsNext)
363 // it wraps the Tabs clicking to follow in the chain:
364 // controls, last control, Apply, Cancel, first control, controls
365 bool isChangedFocus = false;
367 QWidget* aFocusWidget = focusWidget();
368 #ifdef DEBUG_TAB_WIDGETS
370 QString anInfo = QString("focus Widget: %1").arg(aFocusWidget->objectName());
371 qDebug(anInfo.toStdString().c_str());
374 ModuleBase_ModelWidget* aFocusMWidget = ModuleBase_ModelWidget::findModelWidget(this,
377 aFocusMWidget->setHighlighted(false);
379 QWidget* aNewFocusWidget = 0;
381 QList<QWidget*> aChildren;
382 findDirectChildren(this, aChildren, true);
383 int aChildrenCount = aChildren.count();
384 int aFocusWidgetIndex = aChildren.indexOf(aFocusWidget);
385 if (aFocusWidgetIndex >= 0) {
387 if (aFocusWidgetIndex == aChildrenCount-1) {
388 // after the last widget focus should be set to "Apply"
389 QToolButton* anOkBtn = findButton(PROP_PANEL_OK);
390 if (anOkBtn->isEnabled())
391 aNewFocusWidget = anOkBtn;
393 QToolButton* aCancelBtn = findButton(PROP_PANEL_CANCEL);
394 if (aCancelBtn->isEnabled())
395 aNewFocusWidget = aCancelBtn;
399 aNewFocusWidget = aChildren[aFocusWidgetIndex+1];
403 if (aFocusWidgetIndex == 0) {
404 // before the first widget, the last should accept focus
405 aNewFocusWidget = aChildren[aChildrenCount - 1];
408 // before the "Apply" button, the last should accept focus for consistency with "Next"
409 QToolButton* anOkBtn = findButton(PROP_PANEL_OK);
410 if (aFocusWidget == anOkBtn) {
411 aNewFocusWidget = aChildren[aChildrenCount - 1];
414 aNewFocusWidget = aChildren[aFocusWidgetIndex-1];
420 if (aNewFocusWidget) {
421 if (myActiveWidget) {
422 bool isFirstControl = !theIsNext;
423 QWidget* aLastFocusControl = myActiveWidget->getControlAcceptingFocus(isFirstControl);
424 if (aFocusWidget == aLastFocusControl) {
425 setActiveWidget(NULL);
429 // we want to have property panel as an active window to enter values in double control
430 ModuleBase_Tools::setFocus(aNewFocusWidget, "XGUI_PropertyPanel::focusNextPrevChild()");
432 ModuleBase_ModelWidget* aNewFocusMWidget = ModuleBase_ModelWidget::findModelWidget(this,
434 if (aNewFocusMWidget)
435 aNewFocusMWidget->emitFocusInWidget();
436 isChangedFocus = true;
438 return isChangedFocus;
441 void XGUI_PropertyPanel::activateNextWidget()
443 activateNextWidget(myActiveWidget);
446 void XGUI_PropertyPanel::activateWidget(ModuleBase_ModelWidget* theWidget, const bool theEmitSignal)
448 std::string aPreviosAttributeID;
450 aPreviosAttributeID = myActiveWidget->attributeID();
452 // Avoid activation of already actve widget. It could happen on focusIn event many times
453 if (setActiveWidget(theWidget) && theEmitSignal) {
454 emit widgetActivated(myActiveWidget);
455 if (!myActiveWidget && !isEditingMode()) {
456 emit noMoreWidgets(aPreviosAttributeID);
461 bool XGUI_PropertyPanel::setActiveWidget(ModuleBase_ModelWidget* theWidget)
463 // Avoid activation of already actve widget. It could happen on focusIn event many times
464 if (theWidget == myActiveWidget) {
467 std::string aPreviosAttributeID;
469 aPreviosAttributeID = myActiveWidget->attributeID();
470 myActiveWidget->processValueState();
471 myActiveWidget->deactivate();
472 myActiveWidget->setHighlighted(false);
475 emit beforeWidgetActivated(theWidget);
476 theWidget->setHighlighted(true);
477 theWidget->activate();
479 myActiveWidget = theWidget;
480 #ifdef DEBUG_ACTIVE_WIDGET
481 std::cout << "myActiveWidget = " << (theWidget ? theWidget->context().c_str() : "") << std::endl;
483 static Events_ID anEvent = Events_Loop::eventByName(EVENT_UPDATE_BY_WIDGET_SELECTION);
484 Events_Loop::loop()->flush(anEvent);
489 void XGUI_PropertyPanel::setFocusOnOkButton()
491 QToolButton* anOkBtn = findButton(PROP_PANEL_OK);
492 ModuleBase_Tools::setFocus(anOkBtn, "XGUI_PropertyPanel::setFocusOnOkButton()");
495 void XGUI_PropertyPanel::setCancelEnabled(bool theEnabled)
497 QToolButton* anCancelBtn = findButton(PROP_PANEL_CANCEL);
498 anCancelBtn->setEnabled(theEnabled);
501 bool XGUI_PropertyPanel::isCancelEnabled() const
503 QToolButton* anCancelBtn = findButton(PROP_PANEL_CANCEL);
504 return anCancelBtn->isEnabled();
507 void XGUI_PropertyPanel::setEditingMode(bool isEditing)
509 ModuleBase_IPropertyPanel::setEditingMode(isEditing);
510 foreach(ModuleBase_ModelWidget* aWgt, myWidgets) {
511 aWgt->setEditingMode(isEditing);
515 void XGUI_PropertyPanel::setupActions(XGUI_ActionsMgr* theMgr)
517 QStringList aButtonNames;
518 aButtonNames << PROP_PANEL_OK << PROP_PANEL_CANCEL << PROP_PANEL_HELP << PROP_PANEL_PREVIEW;
519 QList<XGUI_ActionsMgr::OperationStateActionId> aActionIds;
520 aActionIds << XGUI_ActionsMgr::Accept << XGUI_ActionsMgr::Abort << XGUI_ActionsMgr::Help
521 << XGUI_ActionsMgr::Preview;
522 for (int i = 0; i < aButtonNames.size(); ++i) {
523 QToolButton* aBtn = findButton(aButtonNames.at(i).toStdString().c_str());
524 QAction* anAct = theMgr->operationStateAction(aActionIds.at(i));
525 aBtn->setDefaultAction(anAct);
529 ModuleBase_ModelWidget* XGUI_PropertyPanel::preselectionWidget() const
531 return myPreselectionWidget;
534 void XGUI_PropertyPanel::setPreselectionWidget(ModuleBase_ModelWidget* theWidget)
536 myPreselectionWidget = theWidget;
540 void XGUI_PropertyPanel::closeEvent(QCloseEvent* theEvent)
542 ModuleBase_Operation* aOp = myOperationMgr->currentOperation();
544 if (myOperationMgr->canStopOperation(aOp)) {
545 myOperationMgr->abortOperation(aOp);
546 if (myOperationMgr->hasOperation())
553 ModuleBase_IPropertyPanel::closeEvent(theEvent);
556 QToolButton* XGUI_PropertyPanel::findButton(const char* theInternalName) const
558 return findChild<QToolButton*>(theInternalName);