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 XGUI_PropertyPanel::XGUI_PropertyPanel(QWidget* theParent, XGUI_OperationMgr* theMgr)
46 : ModuleBase_IPropertyPanel(theParent),
48 myPreselectionWidget(NULL),
50 myOperationMgr(theMgr)
52 setWindowTitle(tr("Property Panel"));
53 QAction* aViewAct = toggleViewAction();
54 setObjectName(PROP_PANEL);
55 setStyleSheet("::title { position: relative; padding-left: 5px; text-align: left center }");
57 QWidget* aContent = new QWidget(this);
58 QGridLayout* aMainLayout = new QGridLayout(aContent);
59 const int kPanelColumn = 0;
61 aMainLayout->setContentsMargins(3, 3, 3, 3);
64 QFrame* aFrm = new QFrame(aContent);
65 aFrm->setFrameStyle(QFrame::Raised);
66 aFrm->setFrameShape(QFrame::Panel);
67 QHBoxLayout* aBtnLay = new QHBoxLayout(aFrm);
68 ModuleBase_Tools::zeroMargins(aBtnLay);
69 aMainLayout->addWidget(aFrm, aPanelRow++, kPanelColumn);
71 myHeaderWidget = aFrm;
73 QStringList aBtnNames;
74 aBtnNames << QString(PROP_PANEL_HELP)
75 << QString(PROP_PANEL_OK)
76 << QString(PROP_PANEL_CANCEL);
77 foreach(QString eachBtnName, aBtnNames) {
78 QToolButton* aBtn = new QToolButton(aFrm);
79 aBtn->setObjectName(eachBtnName);
80 aBtn->setAutoRaise(true);
81 aBtnLay->addWidget(aBtn);
83 aBtnLay->insertStretch(1, 1);
85 myPanelPage = new ModuleBase_PageWidget(aContent);
86 myPanelPage->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::MinimumExpanding);
87 aMainLayout->addWidget(myPanelPage, aPanelRow, kPanelColumn);
89 // spit to make the preview button on the bottom of the panel
90 aMainLayout->setRowStretch(aPanelRow++, 1);
92 // preview button on the bottom of panel
93 aFrm = new QFrame(aContent);
94 aBtnLay = new QHBoxLayout(aFrm);
95 aBtnLay->addStretch(1);
96 ModuleBase_Tools::zeroMargins(aBtnLay);
97 aMainLayout->addWidget(aFrm, aPanelRow++, kPanelColumn);
99 QToolButton* aBtn = new QToolButton(aFrm);
100 aBtn->setObjectName(PROP_PANEL_PREVIEW);
101 aBtnLay->addWidget(aBtn);
104 XGUI_PropertyPanel::~XGUI_PropertyPanel()
108 void XGUI_PropertyPanel::cleanContent()
111 myActiveWidget->deactivate();
113 /// as the widgets are deleted later, it is important that the signals
114 /// of these widgets are not processed. An example of the error is issue 986.
115 /// In the given case, the property panel is firstly filled by new widgets
116 /// of restarted operation and after that the mouse release signal come from
117 /// the widget of the previous operation (Point2d widget about mouse is released
118 /// and focus is out of this widget)
119 QList<ModuleBase_ModelWidget*>::const_iterator anIt = myWidgets.begin(),
120 aLast = myWidgets.end();
121 for (; anIt != aLast; anIt++) {
122 QWidget* aWidget = *anIt;
124 aWidget->blockSignals(true);
129 myPanelPage->clearPage();
130 myActiveWidget = NULL;
132 findButton(PROP_PANEL_PREVIEW)->setVisible(false); /// by default it is hidden
133 setWindowTitle(tr("Property Panel"));
136 void XGUI_PropertyPanel::setModelWidgets(const QList<ModuleBase_ModelWidget*>& theWidgets)
138 myWidgets = theWidgets;
139 if (theWidgets.empty()) return;
140 foreach (ModuleBase_ModelWidget* aWidget, theWidgets) {
141 connect(aWidget, SIGNAL(focusInWidget(ModuleBase_ModelWidget*)),
142 this, SLOT(onFocusInWidget(ModuleBase_ModelWidget*)));
143 connect(aWidget, SIGNAL(focusOutWidget(ModuleBase_ModelWidget*)),
144 this, SLOT(onActivateNextWidget(ModuleBase_ModelWidget*)));
145 connect(aWidget, SIGNAL(keyReleased(QObject*, QKeyEvent*)),
146 this, SIGNAL(keyReleased(QObject*, QKeyEvent*)));
147 connect(aWidget, SIGNAL(enterClicked(QObject*)),
148 this, SIGNAL(enterClicked(QObject*)));
153 const QList<ModuleBase_ModelWidget*>& XGUI_PropertyPanel::modelWidgets() const
158 ModuleBase_PageBase* XGUI_PropertyPanel::contentWidget()
160 return static_cast<ModuleBase_PageBase*>(myPanelPage);
163 void XGUI_PropertyPanel::updateContentWidget(FeaturePtr theFeature)
165 // Invalid feature case on abort of the operation
166 if (theFeature.get() == NULL)
168 if (theFeature->isAction() || !theFeature->data())
170 foreach(ModuleBase_ModelWidget* eachWidget, myWidgets) {
171 if (!eachWidget->feature().get())
172 eachWidget->setFeature(theFeature);
173 eachWidget->restoreValue();
175 // the repaint is used here to immediately react in GUI to the values change.
179 void XGUI_PropertyPanel::createContentPanel(FeaturePtr theFeature)
181 // Invalid feature case on abort of the operation
182 if (theFeature.get() == NULL)
184 if (theFeature->isAction() || !theFeature->data())
187 if (myWidgets.empty()) {
188 ModuleBase_Operation* anOperation = myOperationMgr->currentOperation();
189 QString aXmlRepr = anOperation->getDescription()->xmlRepresentation();
191 ModuleBase_WidgetFactory aFactory(aXmlRepr.toStdString(), myOperationMgr->workshop());
192 aFactory.createPanel(contentWidget(), theFeature);
193 /// Apply button should be update if the feature was modified by the panel
194 myOperationMgr->onValidateOperation();
198 void XGUI_PropertyPanel::activateNextWidget(ModuleBase_ModelWidget* theWidget)
200 // it is possible that the property panel widgets have not been visualized
201 // (e.g. on start operation), so it is strictly important to do not check visualized state
202 activateNextWidget(theWidget, false);
205 void XGUI_PropertyPanel::onFocusInWidget(ModuleBase_ModelWidget* theWidget)
207 if (theWidget->canAcceptFocus())
208 activateWidget(theWidget);
211 void XGUI_PropertyPanel::onActivateNextWidget(ModuleBase_ModelWidget* theWidget)
213 // this slot happens when some widget lost focus, the next widget which accepts the focus
214 // should be shown, so the second parameter is true
215 // it is important for features where in cases the same attributes are used, isCase for this
216 // attribute returns true, however it can be placed in hidden stack widget(extrusion: elements,
217 // sketch multi rotation -> single/full point)
218 // it is important to check the widget visibility to do not check of the next widget visible
219 // state if the current is not shown. (example: sketch circle re-entrant operation after mouse
220 // release in the viewer, next, radius,
221 // widget should be activated but the first is not visualized)
222 bool isVisible = theWidget->isVisible();
223 activateNextWidget(theWidget, isVisible);
227 void XGUI_PropertyPanel::activateNextWidget(ModuleBase_ModelWidget* theWidget,
228 const bool isCheckVisibility)
230 // TO CHECK: Editing operation does not have automatical activation of widgets
231 if (isEditingMode()) {
232 activateWidget(NULL);
235 ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
237 QList<ModuleBase_ModelWidget*>::const_iterator anIt = myWidgets.begin(), aLast = myWidgets.end();
238 bool isFoundWidget = false;
239 ModuleBase_Tools::activateWindow(this, "XGUI_PropertyPanel::activateNextWidget()");
240 for (; anIt != aLast; anIt++) {
241 ModuleBase_ModelWidget* aCurrentWidget = *anIt;
242 if (isFoundWidget || !theWidget) {
244 if (!aValidators->isCase(aCurrentWidget->feature(), aCurrentWidget->attributeID()))
245 continue; // this attribute does not participate in the current case
246 if (isCheckVisibility && !aCurrentWidget->isInternal()) {
247 if (!aCurrentWidget->isVisible())
250 if (!aCurrentWidget->isObligatory())
251 continue; // not obligatory widgets are not activated automatically
253 if (!aCurrentWidget->canAcceptFocus())
254 continue; // do not set focus if it can not be accepted, case: optional choice
256 if (aCurrentWidget->focusTo()) {
260 isFoundWidget = isFoundWidget || (*anIt) == theWidget;
262 // set focus to Ok/Cancel button in Property panel if there are no more active widgets
263 // it should be performed before activateWidget(NULL) because it emits some signals which
264 // can be processed by moudule for example as to activate another widget with setting focus
265 QWidget* aNewFocusWidget = 0;
266 QToolButton* anOkBtn = findButton(PROP_PANEL_OK);
267 if (anOkBtn->isEnabled())
268 aNewFocusWidget = anOkBtn;
270 QToolButton* aCancelBtn = findButton(PROP_PANEL_CANCEL);
271 if (aCancelBtn->isEnabled())
272 aNewFocusWidget = aCancelBtn;
275 aNewFocusWidget->setFocus(Qt::TabFocusReason);
277 activateWidget(NULL);
280 void findDirectChildren(QWidget* theParent, QList<QWidget*>& theWidgets, const bool theDebug)
282 QList<QWidget*> aWidgets;
285 QLayout* aLayout = theParent->layout();
287 for (int i = 0, aCount = aLayout->count(); i < aCount; i++) {
288 QLayoutItem* anItem = aLayout->itemAt(i);
289 QWidget* aWidget = anItem ? anItem->widget() : 0;
291 if (aWidget->isVisible()) {
292 if (aWidget->focusPolicy() != Qt::NoFocus)
293 theWidgets.append(aWidget);
294 findDirectChildren(aWidget, theWidgets, false);
297 else if (anItem->layout()) {
298 QLayout* aLayout = anItem->layout();
299 for (int i = 0, aCount = aLayout->count(); i < aCount; i++) {
300 QLayoutItem* anItem = aLayout->itemAt(i);
301 QWidget* aWidget = anItem ? anItem->widget() : 0;
303 if (aWidget->isVisible()) {
304 if (aWidget->focusPolicy() != Qt::NoFocus)
305 theWidgets.append(aWidget);
306 findDirectChildren(aWidget, theWidgets, false);
310 // TODO: improve recursive search for the case when here QLayout is used
311 // currently, the switch widget uses only 1 level of qlayout in qlayout using for
312 // example for construction plane feature. If there are more levels,
313 // it should be implemented here
320 #ifdef DEBUG_TAB_WIDGETS
322 QStringList aWidgetTypes;
323 QList<QWidget*>::const_iterator anIt = theWidgets.begin(), aLast = theWidgets.end();
324 for (; anIt != aLast; anIt++) {
325 QWidget* aWidget = *anIt;
327 aWidgetTypes.append(aWidget->objectName());
329 QString anInfo = QString("theWidgets[%1]: %2")
330 .arg(theWidgets.count()).arg(aWidgetTypes.join(","));
331 qDebug(anInfo.toStdString().c_str());
336 bool XGUI_PropertyPanel::focusNextPrevChild(bool theIsNext)
338 // it wraps the Tabs clicking to follow in the chain:
339 // controls, last control, Apply, Cancel, first control, controls
340 bool isChangedFocus = false;
342 QWidget* aFocusWidget = focusWidget();
343 #ifdef DEBUG_TAB_WIDGETS
345 QString anInfo = QString("focus Widget: %1").arg(aFocusWidget->objectName());
346 qDebug(anInfo.toStdString().c_str());
350 QWidget* aNewFocusWidget = 0;
352 QList<QWidget*> aChildren;
353 findDirectChildren(this, aChildren, true);
354 int aChildrenCount = aChildren.count();
355 int aFocusWidgetIndex = aChildren.indexOf(aFocusWidget);
356 if (aFocusWidgetIndex >= 0) {
358 if (aFocusWidgetIndex == aChildrenCount-1) {
359 // after the last widget focus should be set to "Apply"
360 QToolButton* anOkBtn = findButton(PROP_PANEL_OK);
361 if (anOkBtn->isEnabled())
362 aNewFocusWidget = anOkBtn;
364 QToolButton* aCancelBtn = findButton(PROP_PANEL_CANCEL);
365 if (aCancelBtn->isEnabled())
366 aNewFocusWidget = aCancelBtn;
370 aNewFocusWidget = aChildren[aFocusWidgetIndex+1];
374 if (aFocusWidgetIndex == 0) {
375 // before the first widget, the last should accept focus
376 aNewFocusWidget = aChildren[aChildrenCount - 1];
379 // before the "Apply" button, the last should accept focus for consistency with "Next"
380 QToolButton* anOkBtn = findButton(PROP_PANEL_OK);
381 if (aFocusWidget == anOkBtn) {
382 aNewFocusWidget = aChildren[aChildrenCount - 1];
385 aNewFocusWidget = aChildren[aFocusWidgetIndex-1];
391 if (aNewFocusWidget) {
392 if (myActiveWidget) {
393 myActiveWidget->getControls();
394 bool isFirstControl = !theIsNext;
395 QWidget* aLastFocusControl = myActiveWidget->getControlAcceptingFocus(isFirstControl);
396 if (aFocusWidget == aLastFocusControl) {
397 setActiveWidget(NULL);
401 //ModuleBase_Tools::setFocus(aNewFocusWidget, "XGUI_PropertyPanel::focusNextPrevChild()");
402 aNewFocusWidget->setFocus(theIsNext ? Qt::TabFocusReason : Qt::BacktabFocusReason);
403 isChangedFocus = true;
405 return isChangedFocus;
408 void XGUI_PropertyPanel::activateNextWidget()
410 activateNextWidget(myActiveWidget);
413 void XGUI_PropertyPanel::activateWidget(ModuleBase_ModelWidget* theWidget, const bool theEmitSignal)
415 std::string aPreviosAttributeID;
417 aPreviosAttributeID = myActiveWidget->attributeID();
419 // Avoid activation of already actve widget. It could happen on focusIn event many times
420 if (setActiveWidget(theWidget) && theEmitSignal) {
421 emit widgetActivated(myActiveWidget);
422 if (!myActiveWidget && !isEditingMode()) {
423 emit noMoreWidgets(aPreviosAttributeID);
428 bool XGUI_PropertyPanel::setActiveWidget(ModuleBase_ModelWidget* theWidget)
430 // Avoid activation of already actve widget. It could happen on focusIn event many times
431 if (theWidget == myActiveWidget) {
434 std::string aPreviosAttributeID;
436 aPreviosAttributeID = myActiveWidget->attributeID();
437 myActiveWidget->processValueState();
438 myActiveWidget->deactivate();
439 myActiveWidget->setHighlighted(false);
442 emit beforeWidgetActivated(theWidget);
443 theWidget->setHighlighted(true);
444 theWidget->activate();
446 myActiveWidget = theWidget;
447 static Events_ID anEvent = Events_Loop::eventByName(EVENT_UPDATE_BY_WIDGET_SELECTION);
448 Events_Loop::loop()->flush(anEvent);
453 void XGUI_PropertyPanel::setFocusOnOkButton()
455 QToolButton* anOkBtn = findButton(PROP_PANEL_OK);
456 ModuleBase_Tools::setFocus(anOkBtn, "XGUI_PropertyPanel::setFocusOnOkButton()");
459 void XGUI_PropertyPanel::setCancelEnabled(bool theEnabled)
461 QToolButton* anCancelBtn = findButton(PROP_PANEL_CANCEL);
462 anCancelBtn->setEnabled(theEnabled);
465 bool XGUI_PropertyPanel::isCancelEnabled() const
467 QToolButton* anCancelBtn = findButton(PROP_PANEL_CANCEL);
468 return anCancelBtn->isEnabled();
471 void XGUI_PropertyPanel::setEditingMode(bool isEditing)
473 ModuleBase_IPropertyPanel::setEditingMode(isEditing);
474 foreach(ModuleBase_ModelWidget* aWgt, myWidgets) {
475 aWgt->setEditingMode(isEditing);
479 void XGUI_PropertyPanel::setupActions(XGUI_ActionsMgr* theMgr)
481 QStringList aButtonNames;
482 aButtonNames << PROP_PANEL_OK << PROP_PANEL_CANCEL << PROP_PANEL_HELP << PROP_PANEL_PREVIEW;
483 QList<XGUI_ActionsMgr::OperationStateActionId> aActionIds;
484 aActionIds << XGUI_ActionsMgr::Accept << XGUI_ActionsMgr::Abort << XGUI_ActionsMgr::Help
485 << XGUI_ActionsMgr::Preview;
486 for (int i = 0; i < aButtonNames.size(); ++i) {
487 QToolButton* aBtn = findButton(aButtonNames.at(i).toStdString().c_str());
488 QAction* anAct = theMgr->operationStateAction(aActionIds.at(i));
489 aBtn->setDefaultAction(anAct);
493 ModuleBase_ModelWidget* XGUI_PropertyPanel::preselectionWidget() const
495 return myPreselectionWidget;
498 void XGUI_PropertyPanel::setPreselectionWidget(ModuleBase_ModelWidget* theWidget)
500 myPreselectionWidget = theWidget;
504 void XGUI_PropertyPanel::closeEvent(QCloseEvent* theEvent)
506 ModuleBase_Operation* aOp = myOperationMgr->currentOperation();
508 if (myOperationMgr->canStopOperation(aOp)) {
509 myOperationMgr->abortOperation(aOp);
510 if (myOperationMgr->hasOperation())
517 ModuleBase_IPropertyPanel::closeEvent(theEvent);
520 QToolButton* XGUI_PropertyPanel::findButton(const char* theInternalName) const
522 return findChild<QToolButton*>(theInternalName);