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 XGUI_PropertyPanel::XGUI_PropertyPanel(QWidget* theParent, XGUI_OperationMgr* theMgr)
39 : ModuleBase_IPropertyPanel(theParent),
41 myPreselectionWidget(NULL),
43 myOperationMgr(theMgr)
45 this->setWindowTitle(tr("Property Panel"));
46 QAction* aViewAct = this->toggleViewAction();
47 this->setObjectName(PROP_PANEL);
48 setStyleSheet("::title { position: relative; padding-left: 5px; text-align: left center }");
50 QWidget* aContent = new QWidget(this);
51 QGridLayout* aMainLayout = new QGridLayout(aContent);
52 const int kPanelColumn = 0;
54 aMainLayout->setContentsMargins(3, 3, 3, 3);
55 this->setWidget(aContent);
57 QFrame* aFrm = new QFrame(aContent);
58 aFrm->setFrameStyle(QFrame::Raised);
59 aFrm->setFrameShape(QFrame::Panel);
60 QHBoxLayout* aBtnLay = new QHBoxLayout(aFrm);
61 ModuleBase_Tools::zeroMargins(aBtnLay);
62 aMainLayout->addWidget(aFrm, aPanelRow++, kPanelColumn);
64 myHeaderWidget = aFrm;
66 QStringList aBtnNames;
67 aBtnNames << QString(PROP_PANEL_HELP)
68 << QString(PROP_PANEL_OK)
69 << QString(PROP_PANEL_CANCEL);
70 foreach(QString eachBtnName, aBtnNames) {
71 QToolButton* aBtn = new QToolButton(aFrm);
72 aBtn->setObjectName(eachBtnName);
73 aBtn->setAutoRaise(true);
74 aBtnLay->addWidget(aBtn);
76 aBtnLay->insertStretch(1, 1);
78 myPanelPage = new ModuleBase_PageWidget(aContent);
79 myPanelPage->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::MinimumExpanding);
80 aMainLayout->addWidget(myPanelPage, aPanelRow, kPanelColumn);
83 XGUI_PropertyPanel::~XGUI_PropertyPanel()
87 void XGUI_PropertyPanel::cleanContent()
90 myActiveWidget->deactivate();
92 /// as the widgets are deleted later, it is important that the signals
93 /// of these widgets are not processed. An example of the error is issue 986.
94 /// In the given case, the property panel is firstly filled by new widgets
95 /// of restarted operation and after that the mouse release signal come from
96 /// the widget of the previous operation (Point2d widget about mouse is released
97 /// and focus is out of this widget)
98 QList<ModuleBase_ModelWidget*>::const_iterator anIt = myWidgets.begin(),
99 aLast = myWidgets.end();
100 for (; anIt != aLast; anIt++) {
101 QWidget* aWidget = *anIt;
103 aWidget->blockSignals(true);
108 myPanelPage->clearPage();
109 myActiveWidget = NULL;
110 setWindowTitle(tr("Property Panel"));
113 void XGUI_PropertyPanel::setModelWidgets(const QList<ModuleBase_ModelWidget*>& theWidgets)
115 myWidgets = theWidgets;
116 if (theWidgets.empty()) return;
117 foreach (ModuleBase_ModelWidget* aWidget, theWidgets) {
118 connect(aWidget, SIGNAL(focusInWidget(ModuleBase_ModelWidget*)),
119 this, SLOT(activateWidget(ModuleBase_ModelWidget*)));
120 connect(aWidget, SIGNAL(focusOutWidget(ModuleBase_ModelWidget*)),
121 this, SLOT(onActivateNextWidget(ModuleBase_ModelWidget*)));
122 connect(aWidget, SIGNAL(keyReleased(QKeyEvent*)),
123 this, SIGNAL(keyReleased(QKeyEvent*)));
124 connect(aWidget, SIGNAL(enterClicked()),
125 this, SIGNAL(enterClicked()));
130 const QList<ModuleBase_ModelWidget*>& XGUI_PropertyPanel::modelWidgets() const
135 ModuleBase_PageBase* XGUI_PropertyPanel::contentWidget()
137 return static_cast<ModuleBase_PageBase*>(myPanelPage);
140 void XGUI_PropertyPanel::updateContentWidget(FeaturePtr theFeature)
142 // Invalid feature case on abort of the operation
143 if (theFeature.get() == NULL)
145 if (theFeature->isAction() || !theFeature->data())
147 foreach(ModuleBase_ModelWidget* eachWidget, myWidgets) {
148 if (!eachWidget->feature().get())
149 eachWidget->setFeature(theFeature);
150 eachWidget->restoreValue();
152 // the repaint is used here to immediately react in GUI to the values change.
156 void XGUI_PropertyPanel::activateNextWidget(ModuleBase_ModelWidget* theWidget)
158 // it is possible that the property panel widgets have not been visualized
159 // (e.g. on start operation), so it is strictly important to do not check visualized state
160 activateNextWidget(theWidget, false);
163 void XGUI_PropertyPanel::onActivateNextWidget(ModuleBase_ModelWidget* theWidget)
165 // this slot happens when some widget lost focus, the next widget which accepts the focus
166 // should be shown, so the second parameter is true
167 // it is important for features where in cases the same attributes are used, isCase for this
168 // attribute returns true, however it can be placed in hidden stack widget(extrusion: elements,
169 // sketch multi rotation -> single/full point)
170 // it is important to check the widget visibility to do not check of the next widget visible
171 // state if the current is not shown. (example: sketch circle re-entrant operation after mouse
172 // release in the viewer, next, radius, widget should be activated but the first is not visualized)
173 bool isVisible = theWidget->isVisible();
174 activateNextWidget(theWidget, isVisible);
178 void XGUI_PropertyPanel::activateNextWidget(ModuleBase_ModelWidget* theWidget,
179 const bool isCheckVisibility)
181 // TO CHECK: Editing operation does not have automatical activation of widgets
182 if (isEditingMode()) {
183 activateWidget(NULL);
186 ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
188 QList<ModuleBase_ModelWidget*>::const_iterator anIt = myWidgets.begin(), aLast = myWidgets.end();
189 bool isFoundWidget = false;
190 ModuleBase_Tools::activateWindow(this, "XGUI_PropertyPanel::activateNextWidget()");
191 for (; anIt != aLast; anIt++) {
192 ModuleBase_ModelWidget* aCurrentWidget = *anIt;
193 if (isFoundWidget || !theWidget) {
195 if (!aValidators->isCase(aCurrentWidget->feature(), aCurrentWidget->attributeID()))
196 continue; // this attribute is not participated in the current case
197 if (isCheckVisibility) {
198 if (!aCurrentWidget->isVisible())
201 if (!aCurrentWidget->isObligatory())
202 continue; // not obligatory widgets are not activated automatically
204 if (aCurrentWidget->focusTo()) {
208 isFoundWidget = isFoundWidget || (*anIt) == theWidget;
210 activateWidget(NULL);
211 //focusNextPrevChild(true);
214 //#define DEBUG_TAB_WIDGETS
218 void findDirectChildren(QWidget* theParent, QList<QWidget*>& theWidgets, const bool theDebug)
220 QList<QWidget*> aWidgets;
223 QLayout* aLayout = theParent->layout();
225 for (int i = 0, aCount = aLayout->count(); i < aCount; i++) {
226 QLayoutItem* anItem = aLayout->itemAt(i);
227 QWidget* aWidget = anItem ? anItem->widget() : 0;
228 if (aWidget && aWidget->isVisible()) {
229 if (aWidget->focusPolicy() != Qt::NoFocus)
230 theWidgets.append(aWidget);
231 findDirectChildren(aWidget, theWidgets, false);
236 #ifdef DEBUG_TAB_WIDGETS
238 QStringList aWidgetTypes;
239 QList<QWidget*>::const_iterator anIt = theWidgets.begin(), aLast = theWidgets.end();
240 for (; anIt != aLast; anIt++) {
241 QWidget* aWidget = *anIt;
243 aWidgetTypes.append(aWidget->objectName());
245 QString anInfo = QString("theWidgets[%1]: %2").arg(theWidgets.count()).arg(aWidgetTypes.join(","));
246 qDebug(anInfo.toStdString().c_str());
251 bool XGUI_PropertyPanel::focusNextPrevChild(bool theIsNext)
253 //bool isChangedFocus = ModuleBase_IPropertyPanel::focusNextPrevChild(theIsNext);
254 //return true;//isChangedFocus;
256 // it wraps the Tabs clicking to follow in the chain:
257 // controls, last control, Apply, Cancel, first control, controls
258 bool isChangedFocus = false;
260 QWidget* aFocusWidget = focusWidget();
261 #ifdef DEBUG_TAB_WIDGETS
263 QString anInfo = QString("focus Widget: %1").arg(aFocusWidget->objectName());
264 qDebug(anInfo.toStdString().c_str());
268 QWidget* aNewFocusWidget = 0;
270 QList<QWidget*> aChildren;
271 findDirectChildren(this, aChildren, true);
272 int aChildrenCount = aChildren.count();
273 int aFocusWidgetIndex = aChildren.indexOf(aFocusWidget);
274 if (aFocusWidgetIndex >= 0) {
276 if (aFocusWidgetIndex == aChildrenCount-1) {
277 // after the last widget focus should be set to "Apply"
278 QToolButton* anOkBtn = findChild<QToolButton*>(PROP_PANEL_OK);
279 aNewFocusWidget = anOkBtn;
282 aNewFocusWidget = aChildren[aFocusWidgetIndex+1];
286 if (aFocusWidgetIndex == 0) {
287 // before the first widget, the last should accept focus
288 aNewFocusWidget = aChildren[aChildrenCount - 1];
291 // before the "Apply" button, the last should accept focus for consistency with "Next"
292 QToolButton* anOkBtn = findChild<QToolButton*>(PROP_PANEL_OK);
293 if (aFocusWidget == anOkBtn) {
294 aNewFocusWidget = aChildren[aChildrenCount - 1];
297 aNewFocusWidget = aChildren[aFocusWidgetIndex-1];
303 if (aNewFocusWidget) {
304 if (myActiveWidget) {
305 myActiveWidget->getControls();
306 bool isFirstControl = !theIsNext;
307 QWidget* aLastFocusControl = myActiveWidget->getControlAcceptingFocus(isFirstControl);
308 if (aFocusWidget == aLastFocusControl) {
309 setActiveWidget(NULL);
313 //ModuleBase_Tools::setFocus(aNewFocusWidget, "XGUI_PropertyPanel::focusNextPrevChild()");
314 aNewFocusWidget->setFocus(theIsNext ? Qt::TabFocusReason : Qt::BacktabFocusReason);
315 isChangedFocus = true;
317 return isChangedFocus;
320 bool XGUI_PropertyPanel::focusNextPrevChild(bool theIsNext)
322 // it wraps the Tabs clicking to follow in the chain:
323 // controls, last control, Apply, Cancel, first control, controls
324 bool isChangedFocus = false;
326 if (theIsNext) { // forward by Tab
327 QToolButton* aCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
328 if (aCancelBtn->hasFocus()) {
329 // after cancel, the first control should be focused
330 QWidget* aFirstControl = 0;
331 for (int i = 0, aSize = myWidgets.size(); i < aSize && !aFirstControl; i++)
332 aFirstControl = myWidgets[i]->getControlAcceptingFocus(true);
334 ModuleBase_Tools::setFocus(aFirstControl, "XGUI_PropertyPanel::focusNextPrevChild()");
335 isChangedFocus = true;
338 // after the last control, the Apply button should be focused
339 QWidget* aLastControl = 0;
340 for (int i = myWidgets.size()-1; i >= 0 && !aLastControl; i--)
341 aLastControl = myWidgets[i]->getControlAcceptingFocus(false);
342 if (aLastControl && aLastControl->hasFocus()) {
343 setFocusOnOkButton();
344 isChangedFocus = true;
348 else { // backward by SHIFT + Tab
349 QToolButton* anOkBtn = findChild<QToolButton*>(PROP_PANEL_OK);
350 if (anOkBtn->hasFocus()) {
351 // after Apply, the last control should be focused
352 QWidget* aLastControl = 0;
353 for (int i = myWidgets.size()-1; i >= 0 && !aLastControl; i--)
354 aLastControl = myWidgets[i]->getControlAcceptingFocus(false);
356 ModuleBase_Tools::setFocus(aLastControl, "XGUI_PropertyPanel::focusNextPrevChild()");
357 isChangedFocus = true;
360 // after the first control, the Cancel button should be focused
361 QWidget* aFirstControl = 0;
362 for (int i = 0, aSize = myWidgets.size(); i < aSize && !aFirstControl; i++)
363 aFirstControl = myWidgets[i]->getControlAcceptingFocus(true);
364 if (aFirstControl && aFirstControl->hasFocus()) {
365 QToolButton* aCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
366 ModuleBase_Tools::setFocus(aCancelBtn, "XGUI_PropertyPanel::focusNextPrevChild()");
367 isChangedFocus = true;
373 isChangedFocus = ModuleBase_IPropertyPanel::focusNextPrevChild(theIsNext);
374 return isChangedFocus;
378 void XGUI_PropertyPanel::activateNextWidget()
380 activateNextWidget(myActiveWidget);
383 void XGUI_PropertyPanel::activateWidget(ModuleBase_ModelWidget* theWidget)
385 std::string aPreviosAttributeID;
387 aPreviosAttributeID = myActiveWidget->attributeID();
389 // Avoid activation of already actve widget. It could happen on focusIn event many times
390 if (setActiveWidget(theWidget)) {
391 emit widgetActivated(myActiveWidget);
392 if (!myActiveWidget && !isEditingMode()) {
393 emit noMoreWidgets(aPreviosAttributeID);
398 bool XGUI_PropertyPanel::setActiveWidget(ModuleBase_ModelWidget* theWidget)
400 // Avoid activation of already actve widget. It could happen on focusIn event many times
401 if (theWidget == myActiveWidget) {
404 std::string aPreviosAttributeID;
406 aPreviosAttributeID = myActiveWidget->attributeID();
407 myActiveWidget->deactivate();
408 myActiveWidget->setHighlighted(false);
411 emit beforeWidgetActivated(theWidget);
412 theWidget->setHighlighted(true);
413 theWidget->activate();
415 myActiveWidget = theWidget;
419 void XGUI_PropertyPanel::setFocusOnOkButton()
421 QToolButton* anOkBtn = findChild<QToolButton*>(PROP_PANEL_OK);
422 ModuleBase_Tools::setFocus(anOkBtn, "XGUI_PropertyPanel::setFocusOnOkButton()");
425 void XGUI_PropertyPanel::setCancelEnabled(bool theEnabled)
427 QToolButton* anCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
428 anCancelBtn->setEnabled(theEnabled);
431 bool XGUI_PropertyPanel::isCancelEnabled() const
433 QToolButton* anCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
434 return anCancelBtn->isEnabled();
437 void XGUI_PropertyPanel::setEditingMode(bool isEditing)
439 ModuleBase_IPropertyPanel::setEditingMode(isEditing);
440 foreach(ModuleBase_ModelWidget* aWgt, myWidgets) {
441 aWgt->setEditingMode(isEditing);
445 void XGUI_PropertyPanel::setupActions(XGUI_ActionsMgr* theMgr)
447 QStringList aButtonNames;
448 aButtonNames << PROP_PANEL_OK << PROP_PANEL_CANCEL << PROP_PANEL_HELP;
449 QList<XGUI_ActionsMgr::OperationStateActionId> aActionIds;
450 aActionIds << XGUI_ActionsMgr::Accept << XGUI_ActionsMgr::Abort << XGUI_ActionsMgr::Help;
451 for (int i = 0; i < aButtonNames.size(); ++i) {
452 QToolButton* aBtn = findChild<QToolButton*>(aButtonNames.at(i));
453 QAction* anAct = theMgr->operationStateAction(aActionIds.at(i));
454 aBtn->setDefaultAction(anAct);
458 ModuleBase_ModelWidget* XGUI_PropertyPanel::preselectionWidget() const
460 return myPreselectionWidget;
463 void XGUI_PropertyPanel::setPreselectionWidget(ModuleBase_ModelWidget* theWidget)
465 myPreselectionWidget = theWidget;
469 void XGUI_PropertyPanel::closeEvent(QCloseEvent* theEvent)
471 ModuleBase_Operation* aOp = myOperationMgr->currentOperation();
473 if (myOperationMgr->abortAllOperations()) {
478 ModuleBase_IPropertyPanel::closeEvent(theEvent);