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()) {
265 aCurrentWidget->emitFocusInWidget();
269 isFoundWidget = isFoundWidget || (*anIt) == theWidget;
271 // set focus to Ok/Cancel button in Property panel if there are no more active widgets
272 // it should be performed before activateWidget(NULL) because it emits some signals which
273 // can be processed by moudule for example as to activate another widget with setting focus
274 QWidget* aNewFocusWidget = 0;
275 QToolButton* anOkBtn = findButton(PROP_PANEL_OK);
276 if (anOkBtn->isEnabled())
277 aNewFocusWidget = anOkBtn;
279 QToolButton* aCancelBtn = findButton(PROP_PANEL_CANCEL);
280 if (aCancelBtn->isEnabled())
281 aNewFocusWidget = aCancelBtn;
284 aNewFocusWidget->setFocus(Qt::TabFocusReason);
286 activateWidget(NULL);
289 void findDirectChildren(QWidget* theParent, QList<QWidget*>& theWidgets, const bool theDebug)
291 QList<QWidget*> aWidgets;
294 QLayout* aLayout = theParent->layout();
296 for (int i = 0, aCount = aLayout->count(); i < aCount; i++) {
297 QLayoutItem* anItem = aLayout->itemAt(i);
298 QWidget* aWidget = anItem ? anItem->widget() : 0;
300 if (aWidget->isVisible()) {
301 if (aWidget->focusPolicy() != Qt::NoFocus)
302 theWidgets.append(aWidget);
303 findDirectChildren(aWidget, theWidgets, false);
306 else if (anItem->layout()) {
307 QLayout* aLayout = anItem->layout();
308 for (int i = 0, aCount = aLayout->count(); i < aCount; i++) {
309 QLayoutItem* anItem = aLayout->itemAt(i);
310 QWidget* aWidget = anItem ? anItem->widget() : 0;
312 if (aWidget->isVisible()) {
313 if (aWidget->focusPolicy() != Qt::NoFocus)
314 theWidgets.append(aWidget);
315 findDirectChildren(aWidget, theWidgets, false);
319 // TODO: improve recursive search for the case when here QLayout is used
320 // currently, the switch widget uses only 1 level of qlayout in qlayout using for
321 // example for construction plane feature. If there are more levels,
322 // it should be implemented here
329 #ifdef DEBUG_TAB_WIDGETS
331 QStringList aWidgetTypes;
332 QList<QWidget*>::const_iterator anIt = theWidgets.begin(), aLast = theWidgets.end();
333 for (; anIt != aLast; anIt++) {
334 QWidget* aWidget = *anIt;
336 aWidgetTypes.append(aWidget->objectName());
338 QString anInfo = QString("theWidgets[%1]: %2")
339 .arg(theWidgets.count()).arg(aWidgetTypes.join(","));
340 qDebug(anInfo.toStdString().c_str());
345 bool XGUI_PropertyPanel::focusNextPrevChild_(bool theIsNext)
347 // it wraps the Tabs clicking to follow in the chain:
348 // controls, last control, Apply, Cancel, first control, controls
349 bool isChangedFocus = false;
351 QWidget* aFocusWidget = focusWidget();
352 #ifdef DEBUG_TAB_WIDGETS
354 QString anInfo = QString("focus Widget: %1").arg(aFocusWidget->objectName());
355 qDebug(anInfo.toStdString().c_str());
358 ModuleBase_ModelWidget* aFocusMWidget = ModuleBase_ModelWidget::findModelWidget(this,
361 aFocusMWidget->setHighlighted(false);
363 QWidget* aNewFocusWidget = 0;
365 QList<QWidget*> aChildren;
366 findDirectChildren(this, aChildren, true);
367 int aChildrenCount = aChildren.count();
368 int aFocusWidgetIndex = aChildren.indexOf(aFocusWidget);
369 if (aFocusWidgetIndex >= 0) {
371 if (aFocusWidgetIndex == aChildrenCount-1) {
372 // after the last widget focus should be set to "Apply"
373 QToolButton* anOkBtn = findButton(PROP_PANEL_OK);
374 if (anOkBtn->isEnabled())
375 aNewFocusWidget = anOkBtn;
377 QToolButton* aCancelBtn = findButton(PROP_PANEL_CANCEL);
378 if (aCancelBtn->isEnabled())
379 aNewFocusWidget = aCancelBtn;
383 aNewFocusWidget = aChildren[aFocusWidgetIndex+1];
387 if (aFocusWidgetIndex == 0) {
388 // before the first widget, the last should accept focus
389 aNewFocusWidget = aChildren[aChildrenCount - 1];
392 // before the "Apply" button, the last should accept focus for consistency with "Next"
393 QToolButton* anOkBtn = findButton(PROP_PANEL_OK);
394 if (aFocusWidget == anOkBtn) {
395 aNewFocusWidget = aChildren[aChildrenCount - 1];
398 aNewFocusWidget = aChildren[aFocusWidgetIndex-1];
404 if (aNewFocusWidget) {
405 if (myActiveWidget) {
406 bool isFirstControl = !theIsNext;
407 QWidget* aLastFocusControl = myActiveWidget->getControlAcceptingFocus(isFirstControl);
408 if (aFocusWidget == aLastFocusControl) {
409 setActiveWidget(NULL);
413 ModuleBase_Tools::setFocus(aNewFocusWidget, "XGUI_PropertyPanel::focusNextPrevChild()");
414 //aNewFocusWidget->setFocus(theIsNext ? Qt::TabFocusReason : Qt::BacktabFocusReason);
416 ModuleBase_ModelWidget* aNewFocusMWidget = ModuleBase_ModelWidget::findModelWidget(this,
418 if (aNewFocusMWidget)
419 aNewFocusMWidget->emitFocusInWidget();
420 isChangedFocus = true;
422 return isChangedFocus;
425 void XGUI_PropertyPanel::activateNextWidget()
427 activateNextWidget(myActiveWidget);
430 void XGUI_PropertyPanel::activateWidget(ModuleBase_ModelWidget* theWidget, const bool theEmitSignal)
432 std::string aPreviosAttributeID;
434 aPreviosAttributeID = myActiveWidget->attributeID();
436 // Avoid activation of already actve widget. It could happen on focusIn event many times
437 if (setActiveWidget(theWidget) && theEmitSignal) {
438 emit widgetActivated(myActiveWidget);
439 if (!myActiveWidget && !isEditingMode()) {
440 emit noMoreWidgets(aPreviosAttributeID);
445 bool XGUI_PropertyPanel::setActiveWidget(ModuleBase_ModelWidget* theWidget)
447 // Avoid activation of already actve widget. It could happen on focusIn event many times
448 if (theWidget == myActiveWidget) {
451 std::string aPreviosAttributeID;
453 aPreviosAttributeID = myActiveWidget->attributeID();
454 myActiveWidget->processValueState();
455 myActiveWidget->deactivate();
456 myActiveWidget->setHighlighted(false);
459 emit beforeWidgetActivated(theWidget);
460 theWidget->setHighlighted(true);
461 theWidget->activate();
463 myActiveWidget = theWidget;
464 #ifdef DEBUG_ACTIVE_WIDGET
465 std::cout << "myActiveWidget = " << (theWidget ? theWidget->context().c_str() : "") << std::endl;
467 static Events_ID anEvent = Events_Loop::eventByName(EVENT_UPDATE_BY_WIDGET_SELECTION);
468 Events_Loop::loop()->flush(anEvent);
473 void XGUI_PropertyPanel::setFocusOnOkButton()
475 QToolButton* anOkBtn = findButton(PROP_PANEL_OK);
476 ModuleBase_Tools::setFocus(anOkBtn, "XGUI_PropertyPanel::setFocusOnOkButton()");
479 void XGUI_PropertyPanel::setCancelEnabled(bool theEnabled)
481 QToolButton* anCancelBtn = findButton(PROP_PANEL_CANCEL);
482 anCancelBtn->setEnabled(theEnabled);
485 bool XGUI_PropertyPanel::isCancelEnabled() const
487 QToolButton* anCancelBtn = findButton(PROP_PANEL_CANCEL);
488 return anCancelBtn->isEnabled();
491 void XGUI_PropertyPanel::setEditingMode(bool isEditing)
493 ModuleBase_IPropertyPanel::setEditingMode(isEditing);
494 foreach(ModuleBase_ModelWidget* aWgt, myWidgets) {
495 aWgt->setEditingMode(isEditing);
499 void XGUI_PropertyPanel::setupActions(XGUI_ActionsMgr* theMgr)
501 QStringList aButtonNames;
502 aButtonNames << PROP_PANEL_OK << PROP_PANEL_CANCEL << PROP_PANEL_HELP << PROP_PANEL_PREVIEW;
503 QList<XGUI_ActionsMgr::OperationStateActionId> aActionIds;
504 aActionIds << XGUI_ActionsMgr::Accept << XGUI_ActionsMgr::Abort << XGUI_ActionsMgr::Help
505 << XGUI_ActionsMgr::Preview;
506 for (int i = 0; i < aButtonNames.size(); ++i) {
507 QToolButton* aBtn = findButton(aButtonNames.at(i).toStdString().c_str());
508 QAction* anAct = theMgr->operationStateAction(aActionIds.at(i));
509 aBtn->setDefaultAction(anAct);
513 ModuleBase_ModelWidget* XGUI_PropertyPanel::preselectionWidget() const
515 return myPreselectionWidget;
518 void XGUI_PropertyPanel::setPreselectionWidget(ModuleBase_ModelWidget* theWidget)
520 myPreselectionWidget = theWidget;
524 void XGUI_PropertyPanel::closeEvent(QCloseEvent* theEvent)
526 ModuleBase_Operation* aOp = myOperationMgr->currentOperation();
528 if (myOperationMgr->canStopOperation(aOp)) {
529 myOperationMgr->abortOperation(aOp);
530 if (myOperationMgr->hasOperation())
537 ModuleBase_IPropertyPanel::closeEvent(theEvent);
540 QToolButton* XGUI_PropertyPanel::findButton(const char* theInternalName) const
542 return findChild<QToolButton*>(theInternalName);