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>
19 #include <ModelAPI_Session.h>
20 #include <ModelAPI_Validator.h>
26 #include <QLayoutItem>
27 #include <QToolButton>
28 #include <QVBoxLayout>
29 #include <QGridLayout>
31 #include <QToolButton>
38 //#define DEBUG_TAB_WIDGETS
40 XGUI_PropertyPanel::XGUI_PropertyPanel(QWidget* theParent, XGUI_OperationMgr* theMgr)
41 : ModuleBase_IPropertyPanel(theParent),
43 myPreselectionWidget(NULL),
45 myOperationMgr(theMgr)
47 this->setWindowTitle(tr("Property Panel"));
48 QAction* aViewAct = this->toggleViewAction();
49 this->setObjectName(PROP_PANEL);
50 setStyleSheet("::title { position: relative; padding-left: 5px; text-align: left center }");
52 QWidget* aContent = new QWidget(this);
53 QGridLayout* aMainLayout = new QGridLayout(aContent);
54 const int kPanelColumn = 0;
56 aMainLayout->setContentsMargins(3, 3, 3, 3);
57 this->setWidget(aContent);
59 QFrame* aFrm = new QFrame(aContent);
60 aFrm->setFrameStyle(QFrame::Raised);
61 aFrm->setFrameShape(QFrame::Panel);
62 QHBoxLayout* aBtnLay = new QHBoxLayout(aFrm);
63 ModuleBase_Tools::zeroMargins(aBtnLay);
64 aMainLayout->addWidget(aFrm, aPanelRow++, kPanelColumn);
66 myHeaderWidget = aFrm;
68 QStringList aBtnNames;
69 aBtnNames << QString(PROP_PANEL_HELP)
70 << QString(PROP_PANEL_OK)
71 << QString(PROP_PANEL_CANCEL);
72 foreach(QString eachBtnName, aBtnNames) {
73 QToolButton* aBtn = new QToolButton(aFrm);
74 aBtn->setObjectName(eachBtnName);
75 aBtn->setAutoRaise(true);
76 aBtnLay->addWidget(aBtn);
78 aBtnLay->insertStretch(1, 1);
80 myPanelPage = new ModuleBase_PageWidget(aContent);
81 myPanelPage->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::MinimumExpanding);
82 aMainLayout->addWidget(myPanelPage, aPanelRow, kPanelColumn);
85 XGUI_PropertyPanel::~XGUI_PropertyPanel()
89 void XGUI_PropertyPanel::cleanContent()
92 myActiveWidget->deactivate();
94 /// as the widgets are deleted later, it is important that the signals
95 /// of these widgets are not processed. An example of the error is issue 986.
96 /// In the given case, the property panel is firstly filled by new widgets
97 /// of restarted operation and after that the mouse release signal come from
98 /// the widget of the previous operation (Point2d widget about mouse is released
99 /// and focus is out of this widget)
100 QList<ModuleBase_ModelWidget*>::const_iterator anIt = myWidgets.begin(),
101 aLast = myWidgets.end();
102 for (; anIt != aLast; anIt++) {
103 QWidget* aWidget = *anIt;
105 aWidget->blockSignals(true);
110 myPanelPage->clearPage();
111 myActiveWidget = NULL;
112 setWindowTitle(tr("Property Panel"));
115 void XGUI_PropertyPanel::setModelWidgets(const QList<ModuleBase_ModelWidget*>& theWidgets)
117 myWidgets = theWidgets;
118 if (theWidgets.empty()) return;
119 foreach (ModuleBase_ModelWidget* aWidget, theWidgets) {
120 connect(aWidget, SIGNAL(focusInWidget(ModuleBase_ModelWidget*)),
121 this, SLOT(activateWidget(ModuleBase_ModelWidget*)));
122 connect(aWidget, SIGNAL(focusOutWidget(ModuleBase_ModelWidget*)),
123 this, SLOT(onActivateNextWidget(ModuleBase_ModelWidget*)));
124 connect(aWidget, SIGNAL(keyReleased(QKeyEvent*)),
125 this, SIGNAL(keyReleased(QKeyEvent*)));
126 connect(aWidget, SIGNAL(enterClicked()),
127 this, SIGNAL(enterClicked()));
132 const QList<ModuleBase_ModelWidget*>& XGUI_PropertyPanel::modelWidgets() const
137 ModuleBase_PageBase* XGUI_PropertyPanel::contentWidget()
139 return static_cast<ModuleBase_PageBase*>(myPanelPage);
142 void XGUI_PropertyPanel::updateContentWidget(FeaturePtr theFeature)
144 // Invalid feature case on abort of the operation
145 if (theFeature.get() == NULL)
147 if (theFeature->isAction() || !theFeature->data())
149 foreach(ModuleBase_ModelWidget* eachWidget, myWidgets) {
150 if (!eachWidget->feature().get())
151 eachWidget->setFeature(theFeature);
152 eachWidget->restoreValue();
154 // the repaint is used here to immediately react in GUI to the values change.
158 void XGUI_PropertyPanel::activateNextWidget(ModuleBase_ModelWidget* theWidget)
160 // it is possible that the property panel widgets have not been visualized
161 // (e.g. on start operation), so it is strictly important to do not check visualized state
162 activateNextWidget(theWidget, false);
165 void XGUI_PropertyPanel::onActivateNextWidget(ModuleBase_ModelWidget* theWidget)
167 // this slot happens when some widget lost focus, the next widget which accepts the focus
168 // should be shown, so the second parameter is true
169 // it is important for features where in cases the same attributes are used, isCase for this
170 // attribute returns true, however it can be placed in hidden stack widget(extrusion: elements,
171 // sketch multi rotation -> single/full point)
172 // it is important to check the widget visibility to do not check of the next widget visible
173 // state if the current is not shown. (example: sketch circle re-entrant operation after mouse
174 // release in the viewer, next, radius, widget should be activated but the first is not visualized)
175 bool isVisible = theWidget->isVisible();
176 activateNextWidget(theWidget, isVisible);
180 void XGUI_PropertyPanel::activateNextWidget(ModuleBase_ModelWidget* theWidget,
181 const bool isCheckVisibility)
183 // TO CHECK: Editing operation does not have automatical activation of widgets
184 if (isEditingMode()) {
185 activateWidget(NULL);
188 ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
190 QList<ModuleBase_ModelWidget*>::const_iterator anIt = myWidgets.begin(), aLast = myWidgets.end();
191 bool isFoundWidget = false;
192 ModuleBase_Tools::activateWindow(this, "XGUI_PropertyPanel::activateNextWidget()");
193 for (; anIt != aLast; anIt++) {
194 ModuleBase_ModelWidget* aCurrentWidget = *anIt;
195 if (isFoundWidget || !theWidget) {
197 if (!aValidators->isCase(aCurrentWidget->feature(), aCurrentWidget->attributeID()))
198 continue; // this attribute is not participated in the current case
199 if (isCheckVisibility && !aCurrentWidget->isInternal()) {
200 if (!aCurrentWidget->isVisible())
203 if (!aCurrentWidget->isObligatory())
204 continue; // not obligatory widgets are not activated automatically
206 if (aCurrentWidget->focusTo()) {
210 isFoundWidget = isFoundWidget || (*anIt) == theWidget;
212 // set focus to Ok/Cancel button in Property panel if there are no more active widgets
213 // it should be performed before activateWidget(NULL) because it emits some signals which
214 // can be processed by moudule for example as to activate another widget with setting focus
215 QWidget* aNewFocusWidget = 0;
216 QToolButton* anOkBtn = findChild<QToolButton*>(PROP_PANEL_OK);
217 if (anOkBtn->isEnabled())
218 aNewFocusWidget = anOkBtn;
220 QToolButton* aCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
221 if (aCancelBtn->isEnabled())
222 aNewFocusWidget = aCancelBtn;
225 aNewFocusWidget->setFocus(Qt::TabFocusReason);
227 activateWidget(NULL);
230 void findDirectChildren(QWidget* theParent, QList<QWidget*>& theWidgets, const bool theDebug)
232 QList<QWidget*> aWidgets;
235 QLayout* aLayout = theParent->layout();
237 for (int i = 0, aCount = aLayout->count(); i < aCount; i++) {
238 QLayoutItem* anItem = aLayout->itemAt(i);
239 QWidget* aWidget = anItem ? anItem->widget() : 0;
241 if (aWidget->isVisible()) {
242 if (aWidget->focusPolicy() != Qt::NoFocus)
243 theWidgets.append(aWidget);
244 findDirectChildren(aWidget, theWidgets, false);
247 else if (anItem->layout()) {
248 QLayout* aLayout = anItem->layout();
249 for (int i = 0, aCount = aLayout->count(); i < aCount; i++) {
250 QLayoutItem* anItem = aLayout->itemAt(i);
251 QWidget* aWidget = anItem ? anItem->widget() : 0;
253 if (aWidget->isVisible()) {
254 if (aWidget->focusPolicy() != Qt::NoFocus)
255 theWidgets.append(aWidget);
256 findDirectChildren(aWidget, theWidgets, false);
260 // TODO: improve recursive search for the case when here QLayout is used
261 // currently, the switch widget uses only 1 level of qlayout in qlayout using for
262 // example for construction plane feature. If there are more levels,
263 // it should be implemented here
270 #ifdef DEBUG_TAB_WIDGETS
272 QStringList aWidgetTypes;
273 QList<QWidget*>::const_iterator anIt = theWidgets.begin(), aLast = theWidgets.end();
274 for (; anIt != aLast; anIt++) {
275 QWidget* aWidget = *anIt;
277 aWidgetTypes.append(aWidget->objectName());
279 QString anInfo = QString("theWidgets[%1]: %2").arg(theWidgets.count()).arg(aWidgetTypes.join(","));
280 qDebug(anInfo.toStdString().c_str());
285 bool XGUI_PropertyPanel::focusNextPrevChild(bool theIsNext)
287 // it wraps the Tabs clicking to follow in the chain:
288 // controls, last control, Apply, Cancel, first control, controls
289 bool isChangedFocus = false;
291 QWidget* aFocusWidget = focusWidget();
292 #ifdef DEBUG_TAB_WIDGETS
294 QString anInfo = QString("focus Widget: %1").arg(aFocusWidget->objectName());
295 qDebug(anInfo.toStdString().c_str());
299 QWidget* aNewFocusWidget = 0;
301 QList<QWidget*> aChildren;
302 findDirectChildren(this, aChildren, true);
303 int aChildrenCount = aChildren.count();
304 int aFocusWidgetIndex = aChildren.indexOf(aFocusWidget);
305 if (aFocusWidgetIndex >= 0) {
307 if (aFocusWidgetIndex == aChildrenCount-1) {
308 // after the last widget focus should be set to "Apply"
309 QToolButton* anOkBtn = findChild<QToolButton*>(PROP_PANEL_OK);
310 if (anOkBtn->isEnabled())
311 aNewFocusWidget = anOkBtn;
313 QToolButton* aCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
314 if (aCancelBtn->isEnabled())
315 aNewFocusWidget = aCancelBtn;
319 aNewFocusWidget = aChildren[aFocusWidgetIndex+1];
323 if (aFocusWidgetIndex == 0) {
324 // before the first widget, the last should accept focus
325 aNewFocusWidget = aChildren[aChildrenCount - 1];
328 // before the "Apply" button, the last should accept focus for consistency with "Next"
329 QToolButton* anOkBtn = findChild<QToolButton*>(PROP_PANEL_OK);
330 if (aFocusWidget == anOkBtn) {
331 aNewFocusWidget = aChildren[aChildrenCount - 1];
334 aNewFocusWidget = aChildren[aFocusWidgetIndex-1];
340 if (aNewFocusWidget) {
341 if (myActiveWidget) {
342 myActiveWidget->getControls();
343 bool isFirstControl = !theIsNext;
344 QWidget* aLastFocusControl = myActiveWidget->getControlAcceptingFocus(isFirstControl);
345 if (aFocusWidget == aLastFocusControl) {
346 setActiveWidget(NULL);
350 //ModuleBase_Tools::setFocus(aNewFocusWidget, "XGUI_PropertyPanel::focusNextPrevChild()");
351 aNewFocusWidget->setFocus(theIsNext ? Qt::TabFocusReason : Qt::BacktabFocusReason);
352 isChangedFocus = true;
354 return isChangedFocus;
357 void XGUI_PropertyPanel::activateNextWidget()
359 activateNextWidget(myActiveWidget);
362 void XGUI_PropertyPanel::activateWidget(ModuleBase_ModelWidget* theWidget, const bool theEmitSignal)
364 std::string aPreviosAttributeID;
366 aPreviosAttributeID = myActiveWidget->attributeID();
368 // Avoid activation of already actve widget. It could happen on focusIn event many times
369 if (setActiveWidget(theWidget) && theEmitSignal) {
370 emit widgetActivated(myActiveWidget);
371 if (!myActiveWidget && !isEditingMode()) {
372 emit noMoreWidgets(aPreviosAttributeID);
377 bool XGUI_PropertyPanel::setActiveWidget(ModuleBase_ModelWidget* theWidget)
379 // Avoid activation of already actve widget. It could happen on focusIn event many times
380 if (theWidget == myActiveWidget) {
383 std::string aPreviosAttributeID;
385 aPreviosAttributeID = myActiveWidget->attributeID();
386 myActiveWidget->deactivate();
387 myActiveWidget->setHighlighted(false);
390 emit beforeWidgetActivated(theWidget);
391 theWidget->setHighlighted(true);
392 theWidget->activate();
394 myActiveWidget = theWidget;
398 void XGUI_PropertyPanel::setFocusOnOkButton()
400 QToolButton* anOkBtn = findChild<QToolButton*>(PROP_PANEL_OK);
401 ModuleBase_Tools::setFocus(anOkBtn, "XGUI_PropertyPanel::setFocusOnOkButton()");
404 void XGUI_PropertyPanel::setCancelEnabled(bool theEnabled)
406 QToolButton* anCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
407 anCancelBtn->setEnabled(theEnabled);
410 bool XGUI_PropertyPanel::isCancelEnabled() const
412 QToolButton* anCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
413 return anCancelBtn->isEnabled();
416 void XGUI_PropertyPanel::setEditingMode(bool isEditing)
418 ModuleBase_IPropertyPanel::setEditingMode(isEditing);
419 foreach(ModuleBase_ModelWidget* aWgt, myWidgets) {
420 aWgt->setEditingMode(isEditing);
424 void XGUI_PropertyPanel::setupActions(XGUI_ActionsMgr* theMgr)
426 QStringList aButtonNames;
427 aButtonNames << PROP_PANEL_OK << PROP_PANEL_CANCEL << PROP_PANEL_HELP;
428 QList<XGUI_ActionsMgr::OperationStateActionId> aActionIds;
429 aActionIds << XGUI_ActionsMgr::Accept << XGUI_ActionsMgr::Abort << XGUI_ActionsMgr::Help;
430 for (int i = 0; i < aButtonNames.size(); ++i) {
431 QToolButton* aBtn = findChild<QToolButton*>(aButtonNames.at(i));
432 QAction* anAct = theMgr->operationStateAction(aActionIds.at(i));
433 aBtn->setDefaultAction(anAct);
437 ModuleBase_ModelWidget* XGUI_PropertyPanel::preselectionWidget() const
439 return myPreselectionWidget;
442 void XGUI_PropertyPanel::setPreselectionWidget(ModuleBase_ModelWidget* theWidget)
444 myPreselectionWidget = theWidget;
448 void XGUI_PropertyPanel::closeEvent(QCloseEvent* theEvent)
450 ModuleBase_Operation* aOp = myOperationMgr->currentOperation();
452 if (myOperationMgr->canStopOperation(aOp)) {
453 myOperationMgr->abortOperation(aOp);
454 if (myOperationMgr->hasOperation())
461 ModuleBase_IPropertyPanel::closeEvent(theEvent);