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>
21 #include <ModelAPI_Session.h>
22 #include <ModelAPI_Validator.h>
28 #include <QLayoutItem>
29 #include <QToolButton>
30 #include <QVBoxLayout>
31 #include <QGridLayout>
33 #include <QToolButton>
40 //#define DEBUG_TAB_WIDGETS
42 XGUI_PropertyPanel::XGUI_PropertyPanel(QWidget* theParent, XGUI_OperationMgr* theMgr)
43 : ModuleBase_IPropertyPanel(theParent),
45 myPreselectionWidget(NULL),
47 myOperationMgr(theMgr)
49 setWindowTitle(tr("Property Panel"));
50 QAction* aViewAct = toggleViewAction();
51 setObjectName(PROP_PANEL);
52 setStyleSheet("::title { position: relative; padding-left: 5px; text-align: left center }");
54 QWidget* aContent = new QWidget(this);
55 QGridLayout* aMainLayout = new QGridLayout(aContent);
56 const int kPanelColumn = 0;
58 aMainLayout->setContentsMargins(3, 3, 3, 3);
61 QFrame* aFrm = new QFrame(aContent);
62 aFrm->setFrameStyle(QFrame::Raised);
63 aFrm->setFrameShape(QFrame::Panel);
64 QHBoxLayout* aBtnLay = new QHBoxLayout(aFrm);
65 ModuleBase_Tools::zeroMargins(aBtnLay);
66 aMainLayout->addWidget(aFrm, aPanelRow++, kPanelColumn);
68 myHeaderWidget = aFrm;
70 QStringList aBtnNames;
71 aBtnNames << QString(PROP_PANEL_HELP)
72 << QString(PROP_PANEL_OK)
73 << QString(PROP_PANEL_CANCEL);
74 foreach(QString eachBtnName, aBtnNames) {
75 QToolButton* aBtn = new QToolButton(aFrm);
76 aBtn->setObjectName(eachBtnName);
77 aBtn->setAutoRaise(true);
78 aBtnLay->addWidget(aBtn);
80 aBtnLay->insertStretch(1, 1);
82 myPanelPage = new ModuleBase_PageWidget(aContent);
83 myPanelPage->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::MinimumExpanding);
84 aMainLayout->addWidget(myPanelPage, aPanelRow, kPanelColumn);
86 // spit to make the preview button on the bottom of the panel
87 aMainLayout->setRowStretch(aPanelRow++, 1);
89 // preview button on the bottom of panel
90 aFrm = new QFrame(aContent);
91 aBtnLay = new QHBoxLayout(aFrm);
92 aBtnLay->addStretch(1);
93 ModuleBase_Tools::zeroMargins(aBtnLay);
94 aMainLayout->addWidget(aFrm, aPanelRow++, kPanelColumn);
96 QToolButton* aBtn = new QToolButton(aFrm);
97 aBtn->setObjectName(PROP_PANEL_PREVIEW);
98 aBtnLay->addWidget(aBtn);
101 XGUI_PropertyPanel::~XGUI_PropertyPanel()
105 void XGUI_PropertyPanel::cleanContent()
108 myActiveWidget->deactivate();
110 /// as the widgets are deleted later, it is important that the signals
111 /// of these widgets are not processed. An example of the error is issue 986.
112 /// In the given case, the property panel is firstly filled by new widgets
113 /// of restarted operation and after that the mouse release signal come from
114 /// the widget of the previous operation (Point2d widget about mouse is released
115 /// and focus is out of this widget)
116 QList<ModuleBase_ModelWidget*>::const_iterator anIt = myWidgets.begin(),
117 aLast = myWidgets.end();
118 for (; anIt != aLast; anIt++) {
119 QWidget* aWidget = *anIt;
121 aWidget->blockSignals(true);
126 myPanelPage->clearPage();
127 myActiveWidget = NULL;
129 findButton(PROP_PANEL_PREVIEW)->setVisible(false); /// by default it is hidden
130 setWindowTitle(tr("Property Panel"));
133 void XGUI_PropertyPanel::setModelWidgets(const QList<ModuleBase_ModelWidget*>& theWidgets)
135 myWidgets = theWidgets;
136 if (theWidgets.empty()) return;
137 foreach (ModuleBase_ModelWidget* aWidget, theWidgets) {
138 connect(aWidget, SIGNAL(focusInWidget(ModuleBase_ModelWidget*)),
139 this, SLOT(onFocusInWidget(ModuleBase_ModelWidget*)));
140 connect(aWidget, SIGNAL(focusOutWidget(ModuleBase_ModelWidget*)),
141 this, SLOT(onActivateNextWidget(ModuleBase_ModelWidget*)));
142 connect(aWidget, SIGNAL(keyReleased(QObject*, QKeyEvent*)),
143 this, SIGNAL(keyReleased(QObject*, QKeyEvent*)));
144 connect(aWidget, SIGNAL(enterClicked(QObject*)),
145 this, SIGNAL(enterClicked(QObject*)));
150 const QList<ModuleBase_ModelWidget*>& XGUI_PropertyPanel::modelWidgets() const
155 ModuleBase_PageBase* XGUI_PropertyPanel::contentWidget()
157 return static_cast<ModuleBase_PageBase*>(myPanelPage);
160 void XGUI_PropertyPanel::updateContentWidget(FeaturePtr theFeature)
162 // Invalid feature case on abort of the operation
163 if (theFeature.get() == NULL)
165 if (theFeature->isAction() || !theFeature->data())
167 foreach(ModuleBase_ModelWidget* eachWidget, myWidgets) {
168 if (!eachWidget->feature().get())
169 eachWidget->setFeature(theFeature);
170 eachWidget->restoreValue();
172 // the repaint is used here to immediately react in GUI to the values change.
176 void XGUI_PropertyPanel::createContentPanel(FeaturePtr theFeature)
178 // Invalid feature case on abort of the operation
179 if (theFeature.get() == NULL)
181 if (theFeature->isAction() || !theFeature->data())
184 if (myWidgets.empty()) {
185 ModuleBase_Operation* anOperation = myOperationMgr->currentOperation();
186 QString aXmlRepr = anOperation->getDescription()->xmlRepresentation();
188 ModuleBase_WidgetFactory aFactory(aXmlRepr.toStdString(), myOperationMgr->workshop());
189 aFactory.createPanel(contentWidget(), theFeature);
190 /// Apply button should be update if the feature was modified by the panel
191 myOperationMgr->onValidateOperation();
195 void XGUI_PropertyPanel::activateNextWidget(ModuleBase_ModelWidget* theWidget)
197 // it is possible that the property panel widgets have not been visualized
198 // (e.g. on start operation), so it is strictly important to do not check visualized state
199 activateNextWidget(theWidget, false);
202 void XGUI_PropertyPanel::onFocusInWidget(ModuleBase_ModelWidget* theWidget)
204 if (theWidget->canAcceptFocus())
205 activateWidget(theWidget);
208 void XGUI_PropertyPanel::onActivateNextWidget(ModuleBase_ModelWidget* theWidget)
210 // this slot happens when some widget lost focus, the next widget which accepts the focus
211 // should be shown, so the second parameter is true
212 // it is important for features where in cases the same attributes are used, isCase for this
213 // attribute returns true, however it can be placed in hidden stack widget(extrusion: elements,
214 // sketch multi rotation -> single/full point)
215 // it is important to check the widget visibility to do not check of the next widget visible
216 // state if the current is not shown. (example: sketch circle re-entrant operation after mouse
217 // release in the viewer, next, radius, widget should be activated but the first is not visualized)
218 bool isVisible = theWidget->isVisible();
219 activateNextWidget(theWidget, isVisible);
223 void XGUI_PropertyPanel::activateNextWidget(ModuleBase_ModelWidget* theWidget,
224 const bool isCheckVisibility)
226 // TO CHECK: Editing operation does not have automatical activation of widgets
227 if (isEditingMode()) {
228 activateWidget(NULL);
231 ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
233 QList<ModuleBase_ModelWidget*>::const_iterator anIt = myWidgets.begin(), aLast = myWidgets.end();
234 bool isFoundWidget = false;
235 ModuleBase_Tools::activateWindow(this, "XGUI_PropertyPanel::activateNextWidget()");
236 for (; anIt != aLast; anIt++) {
237 ModuleBase_ModelWidget* aCurrentWidget = *anIt;
238 if (isFoundWidget || !theWidget) {
240 if (!aValidators->isCase(aCurrentWidget->feature(), aCurrentWidget->attributeID()))
241 continue; // this attribute is not participated in the current case
242 if (isCheckVisibility && !aCurrentWidget->isInternal()) {
243 if (!aCurrentWidget->isVisible())
246 if (!aCurrentWidget->isObligatory())
247 continue; // not obligatory widgets are not activated automatically
249 if (aCurrentWidget->focusTo()) {
253 isFoundWidget = isFoundWidget || (*anIt) == theWidget;
255 // set focus to Ok/Cancel button in Property panel if there are no more active widgets
256 // it should be performed before activateWidget(NULL) because it emits some signals which
257 // can be processed by moudule for example as to activate another widget with setting focus
258 QWidget* aNewFocusWidget = 0;
259 QToolButton* anOkBtn = findButton(PROP_PANEL_OK);
260 if (anOkBtn->isEnabled())
261 aNewFocusWidget = anOkBtn;
263 QToolButton* aCancelBtn = findButton(PROP_PANEL_CANCEL);
264 if (aCancelBtn->isEnabled())
265 aNewFocusWidget = aCancelBtn;
268 aNewFocusWidget->setFocus(Qt::TabFocusReason);
270 activateWidget(NULL);
273 void findDirectChildren(QWidget* theParent, QList<QWidget*>& theWidgets, const bool theDebug)
275 QList<QWidget*> aWidgets;
278 QLayout* aLayout = theParent->layout();
280 for (int i = 0, aCount = aLayout->count(); i < aCount; i++) {
281 QLayoutItem* anItem = aLayout->itemAt(i);
282 QWidget* aWidget = anItem ? anItem->widget() : 0;
284 if (aWidget->isVisible()) {
285 if (aWidget->focusPolicy() != Qt::NoFocus)
286 theWidgets.append(aWidget);
287 findDirectChildren(aWidget, theWidgets, false);
290 else if (anItem->layout()) {
291 QLayout* aLayout = anItem->layout();
292 for (int i = 0, aCount = aLayout->count(); i < aCount; i++) {
293 QLayoutItem* anItem = aLayout->itemAt(i);
294 QWidget* aWidget = anItem ? anItem->widget() : 0;
296 if (aWidget->isVisible()) {
297 if (aWidget->focusPolicy() != Qt::NoFocus)
298 theWidgets.append(aWidget);
299 findDirectChildren(aWidget, theWidgets, false);
303 // TODO: improve recursive search for the case when here QLayout is used
304 // currently, the switch widget uses only 1 level of qlayout in qlayout using for
305 // example for construction plane feature. If there are more levels,
306 // it should be implemented here
313 #ifdef DEBUG_TAB_WIDGETS
315 QStringList aWidgetTypes;
316 QList<QWidget*>::const_iterator anIt = theWidgets.begin(), aLast = theWidgets.end();
317 for (; anIt != aLast; anIt++) {
318 QWidget* aWidget = *anIt;
320 aWidgetTypes.append(aWidget->objectName());
322 QString anInfo = QString("theWidgets[%1]: %2").arg(theWidgets.count()).arg(aWidgetTypes.join(","));
323 qDebug(anInfo.toStdString().c_str());
328 bool XGUI_PropertyPanel::focusNextPrevChild(bool theIsNext)
330 // it wraps the Tabs clicking to follow in the chain:
331 // controls, last control, Apply, Cancel, first control, controls
332 bool isChangedFocus = false;
334 QWidget* aFocusWidget = focusWidget();
335 #ifdef DEBUG_TAB_WIDGETS
337 QString anInfo = QString("focus Widget: %1").arg(aFocusWidget->objectName());
338 qDebug(anInfo.toStdString().c_str());
342 QWidget* aNewFocusWidget = 0;
344 QList<QWidget*> aChildren;
345 findDirectChildren(this, aChildren, true);
346 int aChildrenCount = aChildren.count();
347 int aFocusWidgetIndex = aChildren.indexOf(aFocusWidget);
348 if (aFocusWidgetIndex >= 0) {
350 if (aFocusWidgetIndex == aChildrenCount-1) {
351 // after the last widget focus should be set to "Apply"
352 QToolButton* anOkBtn = findButton(PROP_PANEL_OK);
353 if (anOkBtn->isEnabled())
354 aNewFocusWidget = anOkBtn;
356 QToolButton* aCancelBtn = findButton(PROP_PANEL_CANCEL);
357 if (aCancelBtn->isEnabled())
358 aNewFocusWidget = aCancelBtn;
362 aNewFocusWidget = aChildren[aFocusWidgetIndex+1];
366 if (aFocusWidgetIndex == 0) {
367 // before the first widget, the last should accept focus
368 aNewFocusWidget = aChildren[aChildrenCount - 1];
371 // before the "Apply" button, the last should accept focus for consistency with "Next"
372 QToolButton* anOkBtn = findButton(PROP_PANEL_OK);
373 if (aFocusWidget == anOkBtn) {
374 aNewFocusWidget = aChildren[aChildrenCount - 1];
377 aNewFocusWidget = aChildren[aFocusWidgetIndex-1];
383 if (aNewFocusWidget) {
384 if (myActiveWidget) {
385 myActiveWidget->getControls();
386 bool isFirstControl = !theIsNext;
387 QWidget* aLastFocusControl = myActiveWidget->getControlAcceptingFocus(isFirstControl);
388 if (aFocusWidget == aLastFocusControl) {
389 setActiveWidget(NULL);
393 //ModuleBase_Tools::setFocus(aNewFocusWidget, "XGUI_PropertyPanel::focusNextPrevChild()");
394 aNewFocusWidget->setFocus(theIsNext ? Qt::TabFocusReason : Qt::BacktabFocusReason);
395 isChangedFocus = true;
397 return isChangedFocus;
400 void XGUI_PropertyPanel::activateNextWidget()
402 activateNextWidget(myActiveWidget);
405 void XGUI_PropertyPanel::activateWidget(ModuleBase_ModelWidget* theWidget, const bool theEmitSignal)
407 std::string aPreviosAttributeID;
409 aPreviosAttributeID = myActiveWidget->attributeID();
411 // Avoid activation of already actve widget. It could happen on focusIn event many times
412 if (setActiveWidget(theWidget) && theEmitSignal) {
413 emit widgetActivated(myActiveWidget);
414 if (!myActiveWidget && !isEditingMode()) {
415 emit noMoreWidgets(aPreviosAttributeID);
420 bool XGUI_PropertyPanel::setActiveWidget(ModuleBase_ModelWidget* theWidget)
422 // Avoid activation of already actve widget. It could happen on focusIn event many times
423 if (theWidget == myActiveWidget) {
426 std::string aPreviosAttributeID;
428 aPreviosAttributeID = myActiveWidget->attributeID();
429 myActiveWidget->deactivate();
430 myActiveWidget->setHighlighted(false);
433 emit beforeWidgetActivated(theWidget);
434 theWidget->setHighlighted(true);
435 theWidget->activate();
437 myActiveWidget = theWidget;
441 void XGUI_PropertyPanel::setFocusOnOkButton()
443 QToolButton* anOkBtn = findButton(PROP_PANEL_OK);
444 ModuleBase_Tools::setFocus(anOkBtn, "XGUI_PropertyPanel::setFocusOnOkButton()");
447 void XGUI_PropertyPanel::setCancelEnabled(bool theEnabled)
449 QToolButton* anCancelBtn = findButton(PROP_PANEL_CANCEL);
450 anCancelBtn->setEnabled(theEnabled);
453 bool XGUI_PropertyPanel::isCancelEnabled() const
455 QToolButton* anCancelBtn = findButton(PROP_PANEL_CANCEL);
456 return anCancelBtn->isEnabled();
459 void XGUI_PropertyPanel::setEditingMode(bool isEditing)
461 ModuleBase_IPropertyPanel::setEditingMode(isEditing);
462 foreach(ModuleBase_ModelWidget* aWgt, myWidgets) {
463 aWgt->setEditingMode(isEditing);
467 void XGUI_PropertyPanel::setupActions(XGUI_ActionsMgr* theMgr)
469 QStringList aButtonNames;
470 aButtonNames << PROP_PANEL_OK << PROP_PANEL_CANCEL << PROP_PANEL_HELP << PROP_PANEL_PREVIEW;
471 QList<XGUI_ActionsMgr::OperationStateActionId> aActionIds;
472 aActionIds << XGUI_ActionsMgr::Accept << XGUI_ActionsMgr::Abort << XGUI_ActionsMgr::Help
473 << XGUI_ActionsMgr::Preview;
474 for (int i = 0; i < aButtonNames.size(); ++i) {
475 QToolButton* aBtn = findButton(aButtonNames.at(i).toStdString().c_str());
476 QAction* anAct = theMgr->operationStateAction(aActionIds.at(i));
477 aBtn->setDefaultAction(anAct);
481 ModuleBase_ModelWidget* XGUI_PropertyPanel::preselectionWidget() const
483 return myPreselectionWidget;
486 void XGUI_PropertyPanel::setPreselectionWidget(ModuleBase_ModelWidget* theWidget)
488 myPreselectionWidget = theWidget;
492 void XGUI_PropertyPanel::closeEvent(QCloseEvent* theEvent)
494 ModuleBase_Operation* aOp = myOperationMgr->currentOperation();
496 if (myOperationMgr->canStopOperation(aOp)) {
497 myOperationMgr->abortOperation(aOp);
498 if (myOperationMgr->hasOperation())
505 ModuleBase_IPropertyPanel::closeEvent(theEvent);
508 QToolButton* XGUI_PropertyPanel::findButton(const char* theInternalName) const
510 return findChild<QToolButton*>(theInternalName);