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 this->setWindowTitle(tr("Property Panel"));
50 QAction* aViewAct = this->toggleViewAction();
51 this->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);
59 this->setWidget(aContent);
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);
87 XGUI_PropertyPanel::~XGUI_PropertyPanel()
91 void XGUI_PropertyPanel::cleanContent()
94 myActiveWidget->deactivate();
96 /// as the widgets are deleted later, it is important that the signals
97 /// of these widgets are not processed. An example of the error is issue 986.
98 /// In the given case, the property panel is firstly filled by new widgets
99 /// of restarted operation and after that the mouse release signal come from
100 /// the widget of the previous operation (Point2d widget about mouse is released
101 /// and focus is out of this widget)
102 QList<ModuleBase_ModelWidget*>::const_iterator anIt = myWidgets.begin(),
103 aLast = myWidgets.end();
104 for (; anIt != aLast; anIt++) {
105 QWidget* aWidget = *anIt;
107 aWidget->blockSignals(true);
112 myPanelPage->clearPage();
113 myActiveWidget = NULL;
114 setWindowTitle(tr("Property Panel"));
117 void XGUI_PropertyPanel::setModelWidgets(const QList<ModuleBase_ModelWidget*>& theWidgets)
119 myWidgets = theWidgets;
120 if (theWidgets.empty()) return;
121 foreach (ModuleBase_ModelWidget* aWidget, theWidgets) {
122 connect(aWidget, SIGNAL(focusInWidget(ModuleBase_ModelWidget*)),
123 this, SLOT(onFocusInWidget(ModuleBase_ModelWidget*)));
124 connect(aWidget, SIGNAL(focusOutWidget(ModuleBase_ModelWidget*)),
125 this, SLOT(onActivateNextWidget(ModuleBase_ModelWidget*)));
126 connect(aWidget, SIGNAL(keyReleased(QObject*, QKeyEvent*)),
127 this, SIGNAL(keyReleased(QObject*, QKeyEvent*)));
128 connect(aWidget, SIGNAL(enterClicked(QObject*)),
129 this, SIGNAL(enterClicked(QObject*)));
134 const QList<ModuleBase_ModelWidget*>& XGUI_PropertyPanel::modelWidgets() const
139 ModuleBase_PageBase* XGUI_PropertyPanel::contentWidget()
141 return static_cast<ModuleBase_PageBase*>(myPanelPage);
144 void XGUI_PropertyPanel::updateContentWidget(FeaturePtr theFeature)
146 // Invalid feature case on abort of the operation
147 if (theFeature.get() == NULL)
149 if (theFeature->isAction() || !theFeature->data())
151 foreach(ModuleBase_ModelWidget* eachWidget, myWidgets) {
152 if (!eachWidget->feature().get())
153 eachWidget->setFeature(theFeature);
154 eachWidget->restoreValue();
156 // the repaint is used here to immediately react in GUI to the values change.
160 void XGUI_PropertyPanel::createContentPanel(FeaturePtr theFeature)
162 // Invalid feature case on abort of the operation
163 if (theFeature.get() == NULL)
165 if (theFeature->isAction() || !theFeature->data())
168 if (myWidgets.empty()) {
169 ModuleBase_Operation* anOperation = myOperationMgr->currentOperation();
170 QString aXmlRepr = anOperation->getDescription()->xmlRepresentation();
172 ModuleBase_WidgetFactory aFactory(aXmlRepr.toStdString(), myOperationMgr->workshop());
173 aFactory.createPanel(contentWidget(), theFeature);
174 /// Apply button should be update if the feature was modified by the panel
175 myOperationMgr->onValidateOperation();
179 void XGUI_PropertyPanel::activateNextWidget(ModuleBase_ModelWidget* theWidget)
181 // it is possible that the property panel widgets have not been visualized
182 // (e.g. on start operation), so it is strictly important to do not check visualized state
183 activateNextWidget(theWidget, false);
186 void XGUI_PropertyPanel::onFocusInWidget(ModuleBase_ModelWidget* theWidget)
188 if (theWidget->canAcceptFocus())
189 activateWidget(theWidget);
192 void XGUI_PropertyPanel::onActivateNextWidget(ModuleBase_ModelWidget* theWidget)
194 // this slot happens when some widget lost focus, the next widget which accepts the focus
195 // should be shown, so the second parameter is true
196 // it is important for features where in cases the same attributes are used, isCase for this
197 // attribute returns true, however it can be placed in hidden stack widget(extrusion: elements,
198 // sketch multi rotation -> single/full point)
199 // it is important to check the widget visibility to do not check of the next widget visible
200 // state if the current is not shown. (example: sketch circle re-entrant operation after mouse
201 // release in the viewer, next, radius, widget should be activated but the first is not visualized)
202 bool isVisible = theWidget->isVisible();
203 activateNextWidget(theWidget, isVisible);
207 void XGUI_PropertyPanel::activateNextWidget(ModuleBase_ModelWidget* theWidget,
208 const bool isCheckVisibility)
210 // TO CHECK: Editing operation does not have automatical activation of widgets
211 if (isEditingMode()) {
212 activateWidget(NULL);
215 ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
217 QList<ModuleBase_ModelWidget*>::const_iterator anIt = myWidgets.begin(), aLast = myWidgets.end();
218 bool isFoundWidget = false;
219 ModuleBase_Tools::activateWindow(this, "XGUI_PropertyPanel::activateNextWidget()");
220 for (; anIt != aLast; anIt++) {
221 ModuleBase_ModelWidget* aCurrentWidget = *anIt;
222 if (isFoundWidget || !theWidget) {
224 if (!aValidators->isCase(aCurrentWidget->feature(), aCurrentWidget->attributeID()))
225 continue; // this attribute is not participated in the current case
226 if (isCheckVisibility && !aCurrentWidget->isInternal()) {
227 if (!aCurrentWidget->isVisible())
230 if (!aCurrentWidget->isObligatory())
231 continue; // not obligatory widgets are not activated automatically
233 if (aCurrentWidget->focusTo()) {
237 isFoundWidget = isFoundWidget || (*anIt) == theWidget;
239 // set focus to Ok/Cancel button in Property panel if there are no more active widgets
240 // it should be performed before activateWidget(NULL) because it emits some signals which
241 // can be processed by moudule for example as to activate another widget with setting focus
242 QWidget* aNewFocusWidget = 0;
243 QToolButton* anOkBtn = findChild<QToolButton*>(PROP_PANEL_OK);
244 if (anOkBtn->isEnabled())
245 aNewFocusWidget = anOkBtn;
247 QToolButton* aCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
248 if (aCancelBtn->isEnabled())
249 aNewFocusWidget = aCancelBtn;
252 aNewFocusWidget->setFocus(Qt::TabFocusReason);
254 activateWidget(NULL);
257 void findDirectChildren(QWidget* theParent, QList<QWidget*>& theWidgets, const bool theDebug)
259 QList<QWidget*> aWidgets;
262 QLayout* aLayout = theParent->layout();
264 for (int i = 0, aCount = aLayout->count(); i < aCount; i++) {
265 QLayoutItem* anItem = aLayout->itemAt(i);
266 QWidget* aWidget = anItem ? anItem->widget() : 0;
268 if (aWidget->isVisible()) {
269 if (aWidget->focusPolicy() != Qt::NoFocus)
270 theWidgets.append(aWidget);
271 findDirectChildren(aWidget, theWidgets, false);
274 else if (anItem->layout()) {
275 QLayout* aLayout = anItem->layout();
276 for (int i = 0, aCount = aLayout->count(); i < aCount; i++) {
277 QLayoutItem* anItem = aLayout->itemAt(i);
278 QWidget* aWidget = anItem ? anItem->widget() : 0;
280 if (aWidget->isVisible()) {
281 if (aWidget->focusPolicy() != Qt::NoFocus)
282 theWidgets.append(aWidget);
283 findDirectChildren(aWidget, theWidgets, false);
287 // TODO: improve recursive search for the case when here QLayout is used
288 // currently, the switch widget uses only 1 level of qlayout in qlayout using for
289 // example for construction plane feature. If there are more levels,
290 // it should be implemented here
297 #ifdef DEBUG_TAB_WIDGETS
299 QStringList aWidgetTypes;
300 QList<QWidget*>::const_iterator anIt = theWidgets.begin(), aLast = theWidgets.end();
301 for (; anIt != aLast; anIt++) {
302 QWidget* aWidget = *anIt;
304 aWidgetTypes.append(aWidget->objectName());
306 QString anInfo = QString("theWidgets[%1]: %2").arg(theWidgets.count()).arg(aWidgetTypes.join(","));
307 qDebug(anInfo.toStdString().c_str());
312 bool XGUI_PropertyPanel::focusNextPrevChild(bool theIsNext)
314 // it wraps the Tabs clicking to follow in the chain:
315 // controls, last control, Apply, Cancel, first control, controls
316 bool isChangedFocus = false;
318 QWidget* aFocusWidget = focusWidget();
319 #ifdef DEBUG_TAB_WIDGETS
321 QString anInfo = QString("focus Widget: %1").arg(aFocusWidget->objectName());
322 qDebug(anInfo.toStdString().c_str());
326 QWidget* aNewFocusWidget = 0;
328 QList<QWidget*> aChildren;
329 findDirectChildren(this, aChildren, true);
330 int aChildrenCount = aChildren.count();
331 int aFocusWidgetIndex = aChildren.indexOf(aFocusWidget);
332 if (aFocusWidgetIndex >= 0) {
334 if (aFocusWidgetIndex == aChildrenCount-1) {
335 // after the last widget focus should be set to "Apply"
336 QToolButton* anOkBtn = findChild<QToolButton*>(PROP_PANEL_OK);
337 if (anOkBtn->isEnabled())
338 aNewFocusWidget = anOkBtn;
340 QToolButton* aCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
341 if (aCancelBtn->isEnabled())
342 aNewFocusWidget = aCancelBtn;
346 aNewFocusWidget = aChildren[aFocusWidgetIndex+1];
350 if (aFocusWidgetIndex == 0) {
351 // before the first widget, the last should accept focus
352 aNewFocusWidget = aChildren[aChildrenCount - 1];
355 // before the "Apply" button, the last should accept focus for consistency with "Next"
356 QToolButton* anOkBtn = findChild<QToolButton*>(PROP_PANEL_OK);
357 if (aFocusWidget == anOkBtn) {
358 aNewFocusWidget = aChildren[aChildrenCount - 1];
361 aNewFocusWidget = aChildren[aFocusWidgetIndex-1];
367 if (aNewFocusWidget) {
368 if (myActiveWidget) {
369 myActiveWidget->getControls();
370 bool isFirstControl = !theIsNext;
371 QWidget* aLastFocusControl = myActiveWidget->getControlAcceptingFocus(isFirstControl);
372 if (aFocusWidget == aLastFocusControl) {
373 setActiveWidget(NULL);
377 //ModuleBase_Tools::setFocus(aNewFocusWidget, "XGUI_PropertyPanel::focusNextPrevChild()");
378 aNewFocusWidget->setFocus(theIsNext ? Qt::TabFocusReason : Qt::BacktabFocusReason);
379 isChangedFocus = true;
381 return isChangedFocus;
384 void XGUI_PropertyPanel::activateNextWidget()
386 activateNextWidget(myActiveWidget);
389 void XGUI_PropertyPanel::activateWidget(ModuleBase_ModelWidget* theWidget, const bool theEmitSignal)
391 std::string aPreviosAttributeID;
393 aPreviosAttributeID = myActiveWidget->attributeID();
395 // Avoid activation of already actve widget. It could happen on focusIn event many times
396 if (setActiveWidget(theWidget) && theEmitSignal) {
397 emit widgetActivated(myActiveWidget);
398 if (!myActiveWidget && !isEditingMode()) {
399 emit noMoreWidgets(aPreviosAttributeID);
404 bool XGUI_PropertyPanel::setActiveWidget(ModuleBase_ModelWidget* theWidget)
406 // Avoid activation of already actve widget. It could happen on focusIn event many times
407 if (theWidget == myActiveWidget) {
410 std::string aPreviosAttributeID;
412 aPreviosAttributeID = myActiveWidget->attributeID();
413 myActiveWidget->deactivate();
414 myActiveWidget->setHighlighted(false);
417 emit beforeWidgetActivated(theWidget);
418 theWidget->setHighlighted(true);
419 theWidget->activate();
421 myActiveWidget = theWidget;
425 void XGUI_PropertyPanel::setFocusOnOkButton()
427 QToolButton* anOkBtn = findChild<QToolButton*>(PROP_PANEL_OK);
428 ModuleBase_Tools::setFocus(anOkBtn, "XGUI_PropertyPanel::setFocusOnOkButton()");
431 void XGUI_PropertyPanel::setCancelEnabled(bool theEnabled)
433 QToolButton* anCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
434 anCancelBtn->setEnabled(theEnabled);
437 bool XGUI_PropertyPanel::isCancelEnabled() const
439 QToolButton* anCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
440 return anCancelBtn->isEnabled();
443 void XGUI_PropertyPanel::setEditingMode(bool isEditing)
445 ModuleBase_IPropertyPanel::setEditingMode(isEditing);
446 foreach(ModuleBase_ModelWidget* aWgt, myWidgets) {
447 aWgt->setEditingMode(isEditing);
451 void XGUI_PropertyPanel::setupActions(XGUI_ActionsMgr* theMgr)
453 QStringList aButtonNames;
454 aButtonNames << PROP_PANEL_OK << PROP_PANEL_CANCEL << PROP_PANEL_HELP;
455 QList<XGUI_ActionsMgr::OperationStateActionId> aActionIds;
456 aActionIds << XGUI_ActionsMgr::Accept << XGUI_ActionsMgr::Abort << XGUI_ActionsMgr::Help;
457 for (int i = 0; i < aButtonNames.size(); ++i) {
458 QToolButton* aBtn = findChild<QToolButton*>(aButtonNames.at(i));
459 QAction* anAct = theMgr->operationStateAction(aActionIds.at(i));
460 aBtn->setDefaultAction(anAct);
464 ModuleBase_ModelWidget* XGUI_PropertyPanel::preselectionWidget() const
466 return myPreselectionWidget;
469 void XGUI_PropertyPanel::setPreselectionWidget(ModuleBase_ModelWidget* theWidget)
471 myPreselectionWidget = theWidget;
475 void XGUI_PropertyPanel::closeEvent(QCloseEvent* theEvent)
477 ModuleBase_Operation* aOp = myOperationMgr->currentOperation();
479 if (myOperationMgr->canStopOperation(aOp)) {
480 myOperationMgr->abortOperation(aOp);
481 if (myOperationMgr->hasOperation())
488 ModuleBase_IPropertyPanel::closeEvent(theEvent);