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(activateNextWidget(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 // TO CHECK: Editing operation does not have automatical activation of widgets
159 if (isEditingMode()) {
160 activateWidget(NULL);
163 ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
165 QList<ModuleBase_ModelWidget*>::const_iterator anIt = myWidgets.begin(), aLast = myWidgets.end();
166 bool isFoundWidget = false;
167 ModuleBase_Tools::activateWindow(this, "XGUI_PropertyPanel::activateNextWidget()");
168 for (; anIt != aLast; anIt++) {
169 ModuleBase_ModelWidget* aCurrentWidget = *anIt;
170 if (isFoundWidget || !theWidget) {
172 if (!aValidators->isCase(aCurrentWidget->feature(), aCurrentWidget->attributeID()))
173 continue; // this attribute is not participated in the current case
174 if (!aCurrentWidget->isObligatory())
175 continue; // not obligatory widgets are not activated automatically
177 if (aCurrentWidget->focusTo()) {
181 isFoundWidget = isFoundWidget || (*anIt) == theWidget;
183 activateWidget(NULL);
186 #define DEBUG_TAB_WIDGETS
190 void findDirectChildren(QWidget* theParent, QList<QWidget*>& theWidgets, const bool theDebug)
192 QList<QWidget*> aWidgets;
195 QLayout* aLayout = theParent->layout();
197 for (int i = 0, aCount = aLayout->count(); i < aCount; i++) {
198 QLayoutItem* anItem = aLayout->itemAt(i);
199 QWidget* aWidget = anItem ? anItem->widget() : 0;
200 if (aWidget && aWidget->isVisible()) {
201 if (aWidget->focusPolicy() != Qt::NoFocus)
202 theWidgets.append(aWidget);
203 findDirectChildren(aWidget, theWidgets, false);
208 #ifdef DEBUG_TAB_WIDGETS
210 QStringList aWidgetTypes;
211 QList<QWidget*>::const_iterator anIt = theWidgets.begin(), aLast = theWidgets.end();
212 for (; anIt != aLast; anIt++) {
213 QWidget* aWidget = *anIt;
215 aWidgetTypes.append(aWidget->objectName());
217 QString anInfo = QString("theWidgets[%1]: %2").arg(theWidgets.count()).arg(aWidgetTypes.join(","));
218 qDebug(anInfo.toStdString().c_str());
223 bool XGUI_PropertyPanel::focusNextPrevChild(bool theIsNext)
225 //bool isChangedFocus = ModuleBase_IPropertyPanel::focusNextPrevChild(theIsNext);
226 //return true;//isChangedFocus;
228 // it wraps the Tabs clicking to follow in the chain:
229 // controls, last control, Apply, Cancel, first control, controls
230 bool isChangedFocus = false;
232 #ifdef DEBUG_TAB_WIDGETS
233 QWidget* aFocusWidget = focusWidget();
235 QString anInfo = QString("focus Widget: %1").arg(aFocusWidget->objectName());
236 qDebug(anInfo.toStdString().c_str());
240 QWidget* aNewFocusWidget = 0;
242 QList<QWidget*> aChildren;
243 findDirectChildren(this, aChildren, true);
244 int aChildrenCount = aChildren.count();
245 int aFocusWidgetIndex = aChildren.indexOf(aFocusWidget);
246 if (aFocusWidgetIndex >= 0) {
248 if (aFocusWidgetIndex == aChildrenCount-1) {
249 // after the last widget focus should be set to "Apply"
250 QToolButton* anOkBtn = findChild<QToolButton*>(PROP_PANEL_OK);
251 aNewFocusWidget = anOkBtn;
254 aNewFocusWidget = aChildren[aFocusWidgetIndex+1];
258 if (aFocusWidgetIndex == 0) {
259 // before the first widget, the last should accept focus
260 aNewFocusWidget = aChildren[aChildrenCount - 1];
263 // before the "Apply" button, the last should accept focus for consistency with "Next"
264 QToolButton* anOkBtn = findChild<QToolButton*>(PROP_PANEL_OK);
265 if (aFocusWidget == anOkBtn) {
266 aNewFocusWidget = aChildren[aChildrenCount - 1];
269 aNewFocusWidget = aChildren[aFocusWidgetIndex-1];
275 if (aNewFocusWidget) {
276 //ModuleBase_Tools::setFocus(aNewFocusWidget, "XGUI_PropertyPanel::focusNextPrevChild()");
277 aNewFocusWidget->setFocus(theIsNext ? Qt::TabFocusReason : Qt::BacktabFocusReason);
278 isChangedFocus = true;
280 return isChangedFocus;
283 bool XGUI_PropertyPanel::focusNextPrevChild(bool theIsNext)
285 // it wraps the Tabs clicking to follow in the chain:
286 // controls, last control, Apply, Cancel, first control, controls
287 bool isChangedFocus = false;
289 if (theIsNext) { // forward by Tab
290 QToolButton* aCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
291 if (aCancelBtn->hasFocus()) {
292 // after cancel, the first control should be focused
293 QWidget* aFirstControl = 0;
294 for (int i = 0, aSize = myWidgets.size(); i < aSize && !aFirstControl; i++)
295 aFirstControl = myWidgets[i]->getControlAcceptingFocus(true);
297 ModuleBase_Tools::setFocus(aFirstControl, "XGUI_PropertyPanel::focusNextPrevChild()");
298 isChangedFocus = true;
301 // after the last control, the Apply button should be focused
302 QWidget* aLastControl = 0;
303 for (int i = myWidgets.size()-1; i >= 0 && !aLastControl; i--)
304 aLastControl = myWidgets[i]->getControlAcceptingFocus(false);
305 if (aLastControl && aLastControl->hasFocus()) {
306 setFocusOnOkButton();
307 isChangedFocus = true;
311 else { // backward by SHIFT + Tab
312 QToolButton* anOkBtn = findChild<QToolButton*>(PROP_PANEL_OK);
313 if (anOkBtn->hasFocus()) {
314 // after Apply, the last control should be focused
315 QWidget* aLastControl = 0;
316 for (int i = myWidgets.size()-1; i >= 0 && !aLastControl; i--)
317 aLastControl = myWidgets[i]->getControlAcceptingFocus(false);
319 ModuleBase_Tools::setFocus(aLastControl, "XGUI_PropertyPanel::focusNextPrevChild()");
320 isChangedFocus = true;
323 // after the first control, the Cancel button should be focused
324 QWidget* aFirstControl = 0;
325 for (int i = 0, aSize = myWidgets.size(); i < aSize && !aFirstControl; i++)
326 aFirstControl = myWidgets[i]->getControlAcceptingFocus(true);
327 if (aFirstControl && aFirstControl->hasFocus()) {
328 QToolButton* aCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
329 ModuleBase_Tools::setFocus(aCancelBtn, "XGUI_PropertyPanel::focusNextPrevChild()");
330 isChangedFocus = true;
336 isChangedFocus = ModuleBase_IPropertyPanel::focusNextPrevChild(theIsNext);
337 return isChangedFocus;
341 void XGUI_PropertyPanel::activateNextWidget()
343 activateNextWidget(myActiveWidget);
346 void XGUI_PropertyPanel::activateWidget(ModuleBase_ModelWidget* theWidget)
348 std::string aPreviosAttributeID;
350 aPreviosAttributeID = myActiveWidget->attributeID();
352 // Avoid activation of already actve widget. It could happen on focusIn event many times
353 if (setActiveWidget(theWidget)) {
354 emit widgetActivated(myActiveWidget);
355 if (!myActiveWidget && !isEditingMode()) {
356 emit noMoreWidgets(aPreviosAttributeID);
361 bool XGUI_PropertyPanel::setActiveWidget(ModuleBase_ModelWidget* theWidget)
363 // Avoid activation of already actve widget. It could happen on focusIn event many times
364 if (theWidget == myActiveWidget) {
367 std::string aPreviosAttributeID;
369 aPreviosAttributeID = myActiveWidget->attributeID();
370 myActiveWidget->deactivate();
371 myActiveWidget->setHighlighted(false);
374 emit beforeWidgetActivated(theWidget);
375 theWidget->setHighlighted(true);
376 theWidget->activate();
378 myActiveWidget = theWidget;
382 void XGUI_PropertyPanel::setFocusOnOkButton()
384 QToolButton* anOkBtn = findChild<QToolButton*>(PROP_PANEL_OK);
385 ModuleBase_Tools::setFocus(anOkBtn, "XGUI_PropertyPanel::setFocusOnOkButton()");
388 void XGUI_PropertyPanel::setCancelEnabled(bool theEnabled)
390 QToolButton* anCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
391 anCancelBtn->setEnabled(theEnabled);
394 bool XGUI_PropertyPanel::isCancelEnabled() const
396 QToolButton* anCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
397 return anCancelBtn->isEnabled();
400 void XGUI_PropertyPanel::setEditingMode(bool isEditing)
402 ModuleBase_IPropertyPanel::setEditingMode(isEditing);
403 foreach(ModuleBase_ModelWidget* aWgt, myWidgets) {
404 aWgt->setEditingMode(isEditing);
408 void XGUI_PropertyPanel::setupActions(XGUI_ActionsMgr* theMgr)
410 QStringList aButtonNames;
411 aButtonNames << PROP_PANEL_OK << PROP_PANEL_CANCEL << PROP_PANEL_HELP;
412 QList<XGUI_ActionsMgr::OperationStateActionId> aActionIds;
413 aActionIds << XGUI_ActionsMgr::Accept << XGUI_ActionsMgr::Abort << XGUI_ActionsMgr::Help;
414 for (int i = 0; i < aButtonNames.size(); ++i) {
415 QToolButton* aBtn = findChild<QToolButton*>(aButtonNames.at(i));
416 QAction* anAct = theMgr->operationStateAction(aActionIds.at(i));
417 aBtn->setDefaultAction(anAct);
421 ModuleBase_ModelWidget* XGUI_PropertyPanel::preselectionWidget() const
423 return myPreselectionWidget;
426 void XGUI_PropertyPanel::setPreselectionWidget(ModuleBase_ModelWidget* theWidget)
428 myPreselectionWidget = theWidget;
432 void XGUI_PropertyPanel::closeEvent(QCloseEvent* theEvent)
434 ModuleBase_Operation* aOp = myOperationMgr->currentOperation();
436 if (myOperationMgr->abortAllOperations()) {
441 ModuleBase_IPropertyPanel::closeEvent(theEvent);