1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
4 * XGUI_PropertyPanel.cpp
6 * Created on: Apr 29, 2014
10 #include <XGUI_PropertyPanel.h>
11 #include <XGUI_ActionsMgr.h>
12 #include <XGUI_OperationMgr.h>
13 //#include <AppElements_Constants.h>
14 #include <ModuleBase_WidgetMultiSelector.h>
15 #include <ModuleBase_Tools.h>
16 #include <ModuleBase_PageBase.h>
17 #include <ModuleBase_PageWidget.h>
18 #include <ModuleBase_WidgetFactory.h>
19 #include <ModuleBase_OperationDescription.h>
20 #include <ModuleBase_Events.h>
22 #include <Events_Loop.h>
24 #include <ModelAPI_Session.h>
25 #include <ModelAPI_Validator.h>
31 #include <QLayoutItem>
32 #include <QToolButton>
33 #include <QVBoxLayout>
34 #include <QGridLayout>
36 #include <QToolButton>
43 //#define DEBUG_TAB_WIDGETS
45 //#define DEBUG_ACTIVE_WIDGET
47 XGUI_PropertyPanel::XGUI_PropertyPanel(QWidget* theParent, XGUI_OperationMgr* theMgr)
48 : ModuleBase_IPropertyPanel(theParent),
50 myPreselectionWidget(NULL),
52 myOperationMgr(theMgr)
54 setWindowTitle(tr("Property Panel"));
55 QAction* aViewAct = toggleViewAction();
56 setObjectName(PROP_PANEL);
57 setStyleSheet("::title { position: relative; padding-left: 5px; text-align: left center }");
59 QWidget* aContent = new QWidget(this);
60 QGridLayout* aMainLayout = new QGridLayout(aContent);
61 const int kPanelColumn = 0;
63 aMainLayout->setContentsMargins(3, 3, 3, 3);
66 QFrame* aFrm = new QFrame(aContent);
67 aFrm->setFrameStyle(QFrame::Raised);
68 aFrm->setFrameShape(QFrame::Panel);
69 QHBoxLayout* aBtnLay = new QHBoxLayout(aFrm);
70 ModuleBase_Tools::zeroMargins(aBtnLay);
71 aMainLayout->addWidget(aFrm, aPanelRow++, kPanelColumn);
73 myHeaderWidget = aFrm;
75 QStringList aBtnNames;
76 aBtnNames << QString(PROP_PANEL_HELP)
77 << QString(PROP_PANEL_OK)
78 << QString(PROP_PANEL_CANCEL);
79 foreach(QString eachBtnName, aBtnNames) {
80 QToolButton* aBtn = new QToolButton(aFrm);
81 aBtn->setObjectName(eachBtnName);
82 aBtn->setAutoRaise(true);
83 aBtnLay->addWidget(aBtn);
85 aBtnLay->insertStretch(1, 1);
87 myPanelPage = new ModuleBase_PageWidget(aContent);
88 myPanelPage->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::MinimumExpanding);
89 aMainLayout->addWidget(myPanelPage, aPanelRow, kPanelColumn);
91 // spit to make the preview button on the bottom of the panel
92 aMainLayout->setRowStretch(aPanelRow++, 1);
94 // preview button on the bottom of panel
95 aFrm = new QFrame(aContent);
96 aBtnLay = new QHBoxLayout(aFrm);
97 aBtnLay->addStretch(1);
98 ModuleBase_Tools::zeroMargins(aBtnLay);
99 aMainLayout->addWidget(aFrm, aPanelRow++, kPanelColumn);
101 QToolButton* aBtn = new QToolButton(aFrm);
102 aBtn->setObjectName(PROP_PANEL_PREVIEW);
103 aBtnLay->addWidget(aBtn);
106 XGUI_PropertyPanel::~XGUI_PropertyPanel()
110 void XGUI_PropertyPanel::cleanContent()
113 myActiveWidget->deactivate();
115 /// as the widgets are deleted later, it is important that the signals
116 /// of these widgets are not processed. An example of the error is issue 986.
117 /// In the given case, the property panel is firstly filled by new widgets
118 /// of restarted operation and after that the mouse release signal come from
119 /// the widget of the previous operation (Point2d widget about mouse is released
120 /// and focus is out of this widget)
121 QList<ModuleBase_ModelWidget*>::const_iterator anIt = myWidgets.begin(),
122 aLast = myWidgets.end();
123 for (; anIt != aLast; anIt++) {
124 QWidget* aWidget = *anIt;
126 aWidget->blockSignals(true);
131 myPanelPage->clearPage();
132 myActiveWidget = NULL;
133 #ifdef DEBUG_ACTIVE_WIDGET
134 std::cout << "myActiveWidget = NULL" << std::endl;
137 findButton(PROP_PANEL_PREVIEW)->setVisible(false); /// by default it is hidden
138 setWindowTitle(tr("Property Panel"));
141 void XGUI_PropertyPanel::setModelWidgets(const QList<ModuleBase_ModelWidget*>& theWidgets)
143 myWidgets = theWidgets;
144 if (theWidgets.empty()) return;
145 foreach (ModuleBase_ModelWidget* aWidget, theWidgets) {
146 connect(aWidget, SIGNAL(focusInWidget(ModuleBase_ModelWidget*)),
147 this, SLOT(onFocusInWidget(ModuleBase_ModelWidget*)));
148 connect(aWidget, SIGNAL(focusOutWidget(ModuleBase_ModelWidget*)),
149 this, SLOT(onActivateNextWidget(ModuleBase_ModelWidget*)));
150 connect(aWidget, SIGNAL(keyReleased(QObject*, QKeyEvent*)),
151 this, SIGNAL(keyReleased(QObject*, QKeyEvent*)));
152 connect(aWidget, SIGNAL(enterClicked(QObject*)),
153 this, SIGNAL(enterClicked(QObject*)));
158 const QList<ModuleBase_ModelWidget*>& XGUI_PropertyPanel::modelWidgets() const
163 ModuleBase_PageBase* XGUI_PropertyPanel::contentWidget()
165 return static_cast<ModuleBase_PageBase*>(myPanelPage);
168 void XGUI_PropertyPanel::updateContentWidget(FeaturePtr theFeature)
170 // Invalid feature case on abort of the operation
171 if (theFeature.get() == NULL)
173 if (theFeature->isAction() || !theFeature->data())
175 foreach(ModuleBase_ModelWidget* eachWidget, myWidgets) {
176 if (!eachWidget->feature().get())
177 eachWidget->setFeature(theFeature);
178 eachWidget->restoreValue();
180 // the repaint is used here to immediately react in GUI to the values change.
184 void XGUI_PropertyPanel::createContentPanel(FeaturePtr theFeature)
186 // Invalid feature case on abort of the operation
187 if (theFeature.get() == NULL)
189 if (theFeature->isAction() || !theFeature->data())
192 if (myWidgets.empty()) {
193 ModuleBase_Operation* anOperation = myOperationMgr->currentOperation();
194 QString aXmlRepr = anOperation->getDescription()->xmlRepresentation();
196 ModuleBase_WidgetFactory aFactory(aXmlRepr.toStdString(), myOperationMgr->workshop());
197 aFactory.createPanel(contentWidget(), theFeature);
198 /// Apply button should be update if the feature was modified by the panel
199 myOperationMgr->onValidateOperation();
203 void XGUI_PropertyPanel::activateNextWidget(ModuleBase_ModelWidget* theWidget)
205 // it is possible that the property panel widgets have not been visualized
206 // (e.g. on start operation), so it is strictly important to do not check visualized state
207 activateNextWidget(theWidget, false);
210 void XGUI_PropertyPanel::onFocusInWidget(ModuleBase_ModelWidget* theWidget)
212 #ifdef DEBUG_ACTIVE_WIDGET
213 std::cout << "onFocusInWidget" << std::endl;
215 if (theWidget->canAcceptFocus())
216 activateWidget(theWidget);
219 void XGUI_PropertyPanel::onActivateNextWidget(ModuleBase_ModelWidget* theWidget)
221 // this slot happens when some widget lost focus, the next widget which accepts the focus
222 // should be shown, so the second parameter is true
223 // it is important for features where in cases the same attributes are used, isCase for this
224 // attribute returns true, however it can be placed in hidden stack widget(extrusion: elements,
225 // sketch multi rotation -> single/full point)
226 // it is important to check the widget visibility to do not check of the next widget visible
227 // state if the current is not shown. (example: sketch circle re-entrant operation after mouse
228 // release in the viewer, next, radius,
229 // widget should be activated but the first is not visualized)
230 bool isVisible = theWidget->isVisible();
231 activateNextWidget(theWidget, isVisible);
235 void XGUI_PropertyPanel::activateNextWidget(ModuleBase_ModelWidget* theWidget,
236 const bool isCheckVisibility)
238 // TO CHECK: Editing operation does not have automatical activation of widgets
239 if (isEditingMode()) {
240 activateWidget(NULL);
243 ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
245 QList<ModuleBase_ModelWidget*>::const_iterator anIt = myWidgets.begin(), aLast = myWidgets.end();
246 bool isFoundWidget = false;
247 ModuleBase_Tools::activateWindow(this, "XGUI_PropertyPanel::activateNextWidget()");
248 for (; anIt != aLast; anIt++) {
249 ModuleBase_ModelWidget* aCurrentWidget = *anIt;
250 if (isFoundWidget || !theWidget) {
252 if (!aValidators->isCase(aCurrentWidget->feature(), aCurrentWidget->attributeID()))
253 continue; // this attribute does not participate in the current case
254 if (isCheckVisibility && !aCurrentWidget->isInternal()) {
255 if (!aCurrentWidget->isVisible())
258 if (!aCurrentWidget->isObligatory())
259 continue; // not obligatory widgets are not activated automatically
261 if (!aCurrentWidget->canAcceptFocus())
262 continue; // do not set focus if it can not be accepted, case: optional choice
264 if (aCurrentWidget->focusTo()) {
268 isFoundWidget = isFoundWidget || (*anIt) == theWidget;
270 // set focus to Ok/Cancel button in Property panel if there are no more active widgets
271 // it should be performed before activateWidget(NULL) because it emits some signals which
272 // can be processed by moudule for example as to activate another widget with setting focus
273 QWidget* aNewFocusWidget = 0;
274 QToolButton* anOkBtn = findButton(PROP_PANEL_OK);
275 if (anOkBtn->isEnabled())
276 aNewFocusWidget = anOkBtn;
278 QToolButton* aCancelBtn = findButton(PROP_PANEL_CANCEL);
279 if (aCancelBtn->isEnabled())
280 aNewFocusWidget = aCancelBtn;
283 aNewFocusWidget->setFocus(Qt::TabFocusReason);
285 activateWidget(NULL);
288 void findDirectChildren(QWidget* theParent, QList<QWidget*>& theWidgets, const bool theDebug)
290 QList<QWidget*> aWidgets;
293 QLayout* aLayout = theParent->layout();
295 for (int i = 0, aCount = aLayout->count(); i < aCount; i++) {
296 QLayoutItem* anItem = aLayout->itemAt(i);
297 QWidget* aWidget = anItem ? anItem->widget() : 0;
299 if (aWidget->isVisible()) {
300 if (aWidget->focusPolicy() != Qt::NoFocus)
301 theWidgets.append(aWidget);
302 findDirectChildren(aWidget, theWidgets, false);
305 else if (anItem->layout()) {
306 QLayout* aLayout = anItem->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);
318 // TODO: improve recursive search for the case when here QLayout is used
319 // currently, the switch widget uses only 1 level of qlayout in qlayout using for
320 // example for construction plane feature. If there are more levels,
321 // it should be implemented here
328 #ifdef DEBUG_TAB_WIDGETS
330 QStringList aWidgetTypes;
331 QList<QWidget*>::const_iterator anIt = theWidgets.begin(), aLast = theWidgets.end();
332 for (; anIt != aLast; anIt++) {
333 QWidget* aWidget = *anIt;
335 aWidgetTypes.append(aWidget->objectName());
337 QString anInfo = QString("theWidgets[%1]: %2")
338 .arg(theWidgets.count()).arg(aWidgetTypes.join(","));
339 qDebug(anInfo.toStdString().c_str());
344 bool XGUI_PropertyPanel::focusNextPrevChild(bool theIsNext)
346 // it wraps the Tabs clicking to follow in the chain:
347 // controls, last control, Apply, Cancel, first control, controls
348 bool isChangedFocus = false;
350 QWidget* aFocusWidget = focusWidget();
351 #ifdef DEBUG_TAB_WIDGETS
353 QString anInfo = QString("focus Widget: %1").arg(aFocusWidget->objectName());
354 qDebug(anInfo.toStdString().c_str());
358 QWidget* aNewFocusWidget = 0;
360 QList<QWidget*> aChildren;
361 findDirectChildren(this, aChildren, true);
362 int aChildrenCount = aChildren.count();
363 int aFocusWidgetIndex = aChildren.indexOf(aFocusWidget);
364 if (aFocusWidgetIndex >= 0) {
366 if (aFocusWidgetIndex == aChildrenCount-1) {
367 // after the last widget focus should be set to "Apply"
368 QToolButton* anOkBtn = findButton(PROP_PANEL_OK);
369 if (anOkBtn->isEnabled())
370 aNewFocusWidget = anOkBtn;
372 QToolButton* aCancelBtn = findButton(PROP_PANEL_CANCEL);
373 if (aCancelBtn->isEnabled())
374 aNewFocusWidget = aCancelBtn;
378 aNewFocusWidget = aChildren[aFocusWidgetIndex+1];
382 if (aFocusWidgetIndex == 0) {
383 // before the first widget, the last should accept focus
384 aNewFocusWidget = aChildren[aChildrenCount - 1];
387 // before the "Apply" button, the last should accept focus for consistency with "Next"
388 QToolButton* anOkBtn = findButton(PROP_PANEL_OK);
389 if (aFocusWidget == anOkBtn) {
390 aNewFocusWidget = aChildren[aChildrenCount - 1];
393 aNewFocusWidget = aChildren[aFocusWidgetIndex-1];
399 if (aNewFocusWidget) {
400 if (myActiveWidget) {
401 myActiveWidget->getControls();
402 bool isFirstControl = !theIsNext;
403 QWidget* aLastFocusControl = myActiveWidget->getControlAcceptingFocus(isFirstControl);
404 if (aFocusWidget == aLastFocusControl) {
405 setActiveWidget(NULL);
409 //ModuleBase_Tools::setFocus(aNewFocusWidget, "XGUI_PropertyPanel::focusNextPrevChild()");
410 aNewFocusWidget->setFocus(theIsNext ? Qt::TabFocusReason : Qt::BacktabFocusReason);
411 isChangedFocus = true;
413 return isChangedFocus;
416 void XGUI_PropertyPanel::activateNextWidget()
418 activateNextWidget(myActiveWidget);
421 void XGUI_PropertyPanel::activateWidget(ModuleBase_ModelWidget* theWidget, const bool theEmitSignal)
423 std::string aPreviosAttributeID;
425 aPreviosAttributeID = myActiveWidget->attributeID();
427 // Avoid activation of already actve widget. It could happen on focusIn event many times
428 if (setActiveWidget(theWidget) && theEmitSignal) {
429 emit widgetActivated(myActiveWidget);
430 if (!myActiveWidget && !isEditingMode()) {
431 emit noMoreWidgets(aPreviosAttributeID);
436 bool XGUI_PropertyPanel::setActiveWidget(ModuleBase_ModelWidget* theWidget)
438 // Avoid activation of already actve widget. It could happen on focusIn event many times
439 if (theWidget == myActiveWidget) {
442 std::string aPreviosAttributeID;
444 aPreviosAttributeID = myActiveWidget->attributeID();
445 myActiveWidget->processValueState();
446 myActiveWidget->deactivate();
447 myActiveWidget->setHighlighted(false);
450 emit beforeWidgetActivated(theWidget);
451 theWidget->setHighlighted(true);
452 theWidget->activate();
454 myActiveWidget = theWidget;
455 #ifdef DEBUG_ACTIVE_WIDGET
456 std::cout << "myActiveWidget = " << (theWidget ? theWidget->context().c_str() : "") << std::endl;
458 static Events_ID anEvent = Events_Loop::eventByName(EVENT_UPDATE_BY_WIDGET_SELECTION);
459 Events_Loop::loop()->flush(anEvent);
464 void XGUI_PropertyPanel::setFocusOnOkButton()
466 QToolButton* anOkBtn = findButton(PROP_PANEL_OK);
467 ModuleBase_Tools::setFocus(anOkBtn, "XGUI_PropertyPanel::setFocusOnOkButton()");
470 void XGUI_PropertyPanel::setCancelEnabled(bool theEnabled)
472 QToolButton* anCancelBtn = findButton(PROP_PANEL_CANCEL);
473 anCancelBtn->setEnabled(theEnabled);
476 bool XGUI_PropertyPanel::isCancelEnabled() const
478 QToolButton* anCancelBtn = findButton(PROP_PANEL_CANCEL);
479 return anCancelBtn->isEnabled();
482 void XGUI_PropertyPanel::setEditingMode(bool isEditing)
484 ModuleBase_IPropertyPanel::setEditingMode(isEditing);
485 foreach(ModuleBase_ModelWidget* aWgt, myWidgets) {
486 aWgt->setEditingMode(isEditing);
490 void XGUI_PropertyPanel::setupActions(XGUI_ActionsMgr* theMgr)
492 QStringList aButtonNames;
493 aButtonNames << PROP_PANEL_OK << PROP_PANEL_CANCEL << PROP_PANEL_HELP << PROP_PANEL_PREVIEW;
494 QList<XGUI_ActionsMgr::OperationStateActionId> aActionIds;
495 aActionIds << XGUI_ActionsMgr::Accept << XGUI_ActionsMgr::Abort << XGUI_ActionsMgr::Help
496 << XGUI_ActionsMgr::Preview;
497 for (int i = 0; i < aButtonNames.size(); ++i) {
498 QToolButton* aBtn = findButton(aButtonNames.at(i).toStdString().c_str());
499 QAction* anAct = theMgr->operationStateAction(aActionIds.at(i));
500 aBtn->setDefaultAction(anAct);
504 ModuleBase_ModelWidget* XGUI_PropertyPanel::preselectionWidget() const
506 return myPreselectionWidget;
509 void XGUI_PropertyPanel::setPreselectionWidget(ModuleBase_ModelWidget* theWidget)
511 myPreselectionWidget = theWidget;
515 void XGUI_PropertyPanel::closeEvent(QCloseEvent* theEvent)
517 ModuleBase_Operation* aOp = myOperationMgr->currentOperation();
519 if (myOperationMgr->canStopOperation(aOp)) {
520 myOperationMgr->abortOperation(aOp);
521 if (myOperationMgr->hasOperation())
528 ModuleBase_IPropertyPanel::closeEvent(theEvent);
531 QToolButton* XGUI_PropertyPanel::findButton(const char* theInternalName) const
533 return findChild<QToolButton*>(theInternalName);