Salome HOME
#1150 Tab buttons problems
[modules/shaper.git] / src / XGUI / XGUI_PropertyPanel.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 /*
4  * XGUI_PropertyPanel.cpp
5  *
6  *  Created on: Apr 29, 2014
7  *      Author: sbh
8  */
9
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
19 #include <ModelAPI_Session.h>
20 #include <ModelAPI_Validator.h>
21
22 #include <QEvent>
23 #include <QFrame>
24 #include <QIcon>
25 #include <QKeyEvent>
26 #include <QLayoutItem>
27 #include <QToolButton>
28 #include <QVBoxLayout>
29 #include <QGridLayout>
30 #include <QWidget>
31 #include <QToolButton>
32 #include <QAction>
33
34 #ifdef _DEBUG
35 #include <iostream>
36 #endif
37
38 XGUI_PropertyPanel::XGUI_PropertyPanel(QWidget* theParent, XGUI_OperationMgr* theMgr)
39     : ModuleBase_IPropertyPanel(theParent), 
40     myActiveWidget(NULL),
41     myPreselectionWidget(NULL),
42     myPanelPage(NULL),
43     myOperationMgr(theMgr)
44 {
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 }");
49
50   QWidget* aContent = new QWidget(this);
51   QGridLayout* aMainLayout = new QGridLayout(aContent);
52   const int kPanelColumn = 0;
53   int aPanelRow = 0;
54   aMainLayout->setContentsMargins(3, 3, 3, 3);
55   this->setWidget(aContent);
56
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);
63
64   myHeaderWidget = aFrm;
65
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);
75   }
76   aBtnLay->insertStretch(1, 1);
77
78   myPanelPage = new ModuleBase_PageWidget(aContent);
79   myPanelPage->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::MinimumExpanding);
80   aMainLayout->addWidget(myPanelPage, aPanelRow, kPanelColumn);
81 }
82
83 XGUI_PropertyPanel::~XGUI_PropertyPanel()
84 {
85 }
86
87 void XGUI_PropertyPanel::cleanContent()
88 {
89   if (myActiveWidget)
90     myActiveWidget->deactivate();
91
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;
102     if (aWidget) {
103       aWidget->blockSignals(true);
104     }
105   }
106
107   myWidgets.clear();
108   myPanelPage->clearPage();
109   myActiveWidget = NULL;
110   setWindowTitle(tr("Property Panel"));
111 }
112
113 void XGUI_PropertyPanel::setModelWidgets(const QList<ModuleBase_ModelWidget*>& theWidgets)
114 {
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()));
126
127   }
128 }
129
130 const QList<ModuleBase_ModelWidget*>& XGUI_PropertyPanel::modelWidgets() const
131 {
132   return myWidgets;
133 }
134
135 ModuleBase_PageBase* XGUI_PropertyPanel::contentWidget()
136 {
137   return static_cast<ModuleBase_PageBase*>(myPanelPage);
138 }
139
140 void XGUI_PropertyPanel::updateContentWidget(FeaturePtr theFeature)
141 {
142   // Invalid feature case on abort of the operation
143   if (theFeature.get() == NULL)
144     return;
145   if (theFeature->isAction() || !theFeature->data())
146     return;
147   foreach(ModuleBase_ModelWidget* eachWidget, myWidgets) {
148     if (!eachWidget->feature().get())
149       eachWidget->setFeature(theFeature);
150     eachWidget->restoreValue();
151   }
152   // the repaint is used here to immediately react in GUI to the values change.
153   repaint();
154 }
155
156 void XGUI_PropertyPanel::activateNextWidget(ModuleBase_ModelWidget* theWidget)
157 {
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);
161 }
162
163 void XGUI_PropertyPanel::onActivateNextWidget(ModuleBase_ModelWidget* theWidget)
164 {
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);
175 }
176
177
178 void XGUI_PropertyPanel::activateNextWidget(ModuleBase_ModelWidget* theWidget,
179                                             const bool isCheckVisibility)
180 {
181   // TO CHECK: Editing operation does not have automatical activation of widgets
182   if (isEditingMode()) {
183     activateWidget(NULL);
184     return;
185   }
186   ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
187
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) {
194
195       if (!aValidators->isCase(aCurrentWidget->feature(), aCurrentWidget->attributeID()))
196         continue; // this attribute is not participated in the current case
197       if (isCheckVisibility && !aCurrentWidget->isInternal()) {
198         if (!aCurrentWidget->isVisible())
199           continue;
200       }
201       if (!aCurrentWidget->isObligatory())
202         continue; // not obligatory widgets are not activated automatically
203
204       if (aCurrentWidget->focusTo()) {
205         return;
206       }
207     }
208     isFoundWidget = isFoundWidget || (*anIt) == theWidget;
209   }
210   activateWidget(NULL);
211   // if there is no the next widget to be automatically activated, the Ok button in property
212   // panel should accept the focus(example is parallel constraint on sketch lines)
213   QToolButton* anOkBtn = findChild<QToolButton*>(PROP_PANEL_OK);
214   if (anOkBtn)
215     anOkBtn->setFocus(Qt::TabFocusReason);
216 }
217
218 //#define DEBUG_TAB_WIDGETS
219
220 #define DEBUG_TAB
221 #ifdef DEBUG_TAB
222 void findDirectChildren(QWidget* theParent, QList<QWidget*>& theWidgets, const bool theDebug)
223 {
224   QList<QWidget*> aWidgets;
225
226   if (theParent) {
227     QLayout* aLayout = theParent->layout();
228     if (aLayout) {
229       for (int i = 0, aCount = aLayout->count(); i < aCount; i++) {
230         QLayoutItem* anItem = aLayout->itemAt(i);
231         QWidget* aWidget = anItem ? anItem->widget() : 0;
232         if (aWidget && aWidget->isVisible()) {
233           if (aWidget->focusPolicy() != Qt::NoFocus)
234             theWidgets.append(aWidget);
235           findDirectChildren(aWidget, theWidgets, false);
236         }
237       }
238     }
239   }
240 #ifdef DEBUG_TAB_WIDGETS
241   if (theDebug) {
242     QStringList aWidgetTypes;
243     QList<QWidget*>::const_iterator anIt =  theWidgets.begin(), aLast = theWidgets.end();
244     for (; anIt != aLast; anIt++) {
245       QWidget* aWidget = *anIt;
246       if (aWidget)
247         aWidgetTypes.append(aWidget->objectName());
248     }
249     QString anInfo = QString("theWidgets[%1]: %2").arg(theWidgets.count()).arg(aWidgetTypes.join(","));
250     qDebug(anInfo.toStdString().c_str());
251   }
252 #endif
253 }
254
255 bool XGUI_PropertyPanel::focusNextPrevChild(bool theIsNext)
256 {
257   //bool isChangedFocus = ModuleBase_IPropertyPanel::focusNextPrevChild(theIsNext);
258   //return true;//isChangedFocus;
259
260   // it wraps the Tabs clicking to follow in the chain:
261   // controls, last control, Apply, Cancel, first control, controls
262   bool isChangedFocus = false;
263
264   QWidget* aFocusWidget = focusWidget();
265 #ifdef DEBUG_TAB_WIDGETS
266   if (aFocusWidget) {
267     QString anInfo = QString("focus Widget: %1").arg(aFocusWidget->objectName());
268     qDebug(anInfo.toStdString().c_str());
269   }
270 #endif
271
272   QWidget* aNewFocusWidget = 0;
273   if (aFocusWidget) {
274     QList<QWidget*> aChildren;
275     findDirectChildren(this, aChildren, true);
276     int aChildrenCount = aChildren.count();
277     int aFocusWidgetIndex = aChildren.indexOf(aFocusWidget);
278     if (aFocusWidgetIndex >= 0) {
279       if (theIsNext) {
280         if (aFocusWidgetIndex == aChildrenCount-1) {
281           // after the last widget focus should be set to "Apply"
282           QToolButton* anOkBtn = findChild<QToolButton*>(PROP_PANEL_OK);
283           aNewFocusWidget = anOkBtn;
284         }
285         else {
286           aNewFocusWidget = aChildren[aFocusWidgetIndex+1];
287         }
288       }
289       else {
290         if (aFocusWidgetIndex == 0) {
291           // before the first widget, the last should accept focus
292           aNewFocusWidget = aChildren[aChildrenCount - 1];
293         }
294         else {
295           // before the "Apply" button, the last should accept focus for consistency with "Next"
296           QToolButton* anOkBtn = findChild<QToolButton*>(PROP_PANEL_OK);
297           if (aFocusWidget == anOkBtn) {
298             aNewFocusWidget = aChildren[aChildrenCount - 1];
299           }
300           else {
301             aNewFocusWidget = aChildren[aFocusWidgetIndex-1];
302           }
303         }
304       }
305     }
306   }
307   if (aNewFocusWidget) {
308     if (myActiveWidget) {
309       myActiveWidget->getControls();
310       bool isFirstControl = !theIsNext;
311       QWidget* aLastFocusControl = myActiveWidget->getControlAcceptingFocus(isFirstControl);
312       if (aFocusWidget == aLastFocusControl) {
313         setActiveWidget(NULL);
314       }
315     }
316
317     //ModuleBase_Tools::setFocus(aNewFocusWidget, "XGUI_PropertyPanel::focusNextPrevChild()");
318     aNewFocusWidget->setFocus(theIsNext ? Qt::TabFocusReason : Qt::BacktabFocusReason);
319     isChangedFocus = true;
320   }
321   return isChangedFocus;
322 }
323 #else
324 bool XGUI_PropertyPanel::focusNextPrevChild(bool theIsNext)
325 {
326   // it wraps the Tabs clicking to follow in the chain:
327   // controls, last control, Apply, Cancel, first control, controls
328   bool isChangedFocus = false;
329
330   if (theIsNext) { // forward by Tab
331     QToolButton* aCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
332     if (aCancelBtn->hasFocus()) {
333       // after cancel, the first control should be focused
334       QWidget* aFirstControl = 0;
335       for (int i = 0, aSize = myWidgets.size(); i < aSize && !aFirstControl; i++)
336         aFirstControl = myWidgets[i]->getControlAcceptingFocus(true);
337       if (aFirstControl)
338         ModuleBase_Tools::setFocus(aFirstControl, "XGUI_PropertyPanel::focusNextPrevChild()");
339         isChangedFocus = true;
340     }
341     else {
342       // after the last control, the Apply button should be focused
343       QWidget* aLastControl = 0;
344       for (int i = myWidgets.size()-1; i >= 0 && !aLastControl; i--)
345         aLastControl = myWidgets[i]->getControlAcceptingFocus(false);
346       if (aLastControl && aLastControl->hasFocus()) {
347         setFocusOnOkButton();
348         isChangedFocus = true;
349       }
350     }
351   }
352   else { // backward by SHIFT + Tab
353     QToolButton* anOkBtn = findChild<QToolButton*>(PROP_PANEL_OK);
354     if (anOkBtn->hasFocus()) {
355       // after Apply, the last control should be focused
356       QWidget* aLastControl = 0;
357       for (int i = myWidgets.size()-1; i >= 0 && !aLastControl; i--)
358         aLastControl = myWidgets[i]->getControlAcceptingFocus(false);
359       if (aLastControl)
360         ModuleBase_Tools::setFocus(aLastControl, "XGUI_PropertyPanel::focusNextPrevChild()");
361         isChangedFocus = true;
362     }
363     else {
364       // after the first control, the Cancel button should be focused
365       QWidget* aFirstControl = 0;
366       for (int i = 0, aSize = myWidgets.size(); i < aSize && !aFirstControl; i++)
367         aFirstControl = myWidgets[i]->getControlAcceptingFocus(true);
368       if (aFirstControl && aFirstControl->hasFocus()) {
369         QToolButton* aCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
370         ModuleBase_Tools::setFocus(aCancelBtn, "XGUI_PropertyPanel::focusNextPrevChild()");
371         isChangedFocus = true;
372       }
373     }
374   }
375
376   if (!isChangedFocus)
377     isChangedFocus = ModuleBase_IPropertyPanel::focusNextPrevChild(theIsNext);
378   return isChangedFocus;
379 }
380
381 #endif
382 void XGUI_PropertyPanel::activateNextWidget()
383 {
384   activateNextWidget(myActiveWidget);
385 }
386
387 void XGUI_PropertyPanel::activateWidget(ModuleBase_ModelWidget* theWidget)
388 {
389   std::string aPreviosAttributeID;
390   if(myActiveWidget)
391     aPreviosAttributeID = myActiveWidget->attributeID();
392
393   // Avoid activation of already actve widget. It could happen on focusIn event many times
394   if (setActiveWidget(theWidget)) {
395     emit widgetActivated(myActiveWidget);
396     if (!myActiveWidget && !isEditingMode()) {
397       emit noMoreWidgets(aPreviosAttributeID);
398     }
399   }
400 }
401
402 bool XGUI_PropertyPanel::setActiveWidget(ModuleBase_ModelWidget* theWidget)
403 {
404   // Avoid activation of already actve widget. It could happen on focusIn event many times
405   if (theWidget == myActiveWidget) {
406     return false;
407   }
408   std::string aPreviosAttributeID;
409   if(myActiveWidget) {
410     aPreviosAttributeID = myActiveWidget->attributeID();
411     myActiveWidget->deactivate();
412     myActiveWidget->setHighlighted(false);
413   }
414   if(theWidget) {
415     emit beforeWidgetActivated(theWidget);
416     theWidget->setHighlighted(true);
417     theWidget->activate();
418   }
419   myActiveWidget = theWidget;
420   return true;
421 }
422
423 void XGUI_PropertyPanel::setFocusOnOkButton()
424 {
425   QToolButton* anOkBtn = findChild<QToolButton*>(PROP_PANEL_OK);
426   ModuleBase_Tools::setFocus(anOkBtn, "XGUI_PropertyPanel::setFocusOnOkButton()");
427 }
428
429 void XGUI_PropertyPanel::setCancelEnabled(bool theEnabled)
430 {
431   QToolButton* anCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
432   anCancelBtn->setEnabled(theEnabled);
433 }
434
435 bool XGUI_PropertyPanel::isCancelEnabled() const
436 {
437   QToolButton* anCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
438   return anCancelBtn->isEnabled();
439 }
440
441 void XGUI_PropertyPanel::setEditingMode(bool isEditing)
442 {
443   ModuleBase_IPropertyPanel::setEditingMode(isEditing);
444   foreach(ModuleBase_ModelWidget* aWgt, myWidgets) {
445     aWgt->setEditingMode(isEditing);
446   }
447 }
448
449 void XGUI_PropertyPanel::setupActions(XGUI_ActionsMgr* theMgr)
450 {
451   QStringList aButtonNames;
452   aButtonNames << PROP_PANEL_OK << PROP_PANEL_CANCEL << PROP_PANEL_HELP;
453   QList<XGUI_ActionsMgr::OperationStateActionId> aActionIds;
454   aActionIds << XGUI_ActionsMgr::Accept << XGUI_ActionsMgr::Abort << XGUI_ActionsMgr::Help;
455   for (int i = 0; i < aButtonNames.size(); ++i) {
456     QToolButton* aBtn = findChild<QToolButton*>(aButtonNames.at(i));
457     QAction* anAct = theMgr->operationStateAction(aActionIds.at(i));
458     aBtn->setDefaultAction(anAct);
459   }
460 }
461
462 ModuleBase_ModelWidget* XGUI_PropertyPanel::preselectionWidget() const
463 {
464   return myPreselectionWidget;
465 }
466
467 void XGUI_PropertyPanel::setPreselectionWidget(ModuleBase_ModelWidget* theWidget)
468 {
469   myPreselectionWidget = theWidget;
470 }
471
472
473 void XGUI_PropertyPanel::closeEvent(QCloseEvent* theEvent)
474 {
475   ModuleBase_Operation* aOp = myOperationMgr->currentOperation();
476   if (aOp) {
477     if (myOperationMgr->abortAllOperations()) {
478       theEvent->accept();
479     } else 
480       theEvent->ignore();
481   } else
482     ModuleBase_IPropertyPanel::closeEvent(theEvent);
483 }