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   activateNextWidget(theWidget, false); // true
171 }
172
173
174 void XGUI_PropertyPanel::activateNextWidget(ModuleBase_ModelWidget* theWidget,
175                                             const bool isCheckVisibility)
176 {
177   // TO CHECK: Editing operation does not have automatical activation of widgets
178   if (isEditingMode()) {
179     activateWidget(NULL);
180     return;
181   }
182   ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
183
184   QList<ModuleBase_ModelWidget*>::const_iterator anIt = myWidgets.begin(), aLast = myWidgets.end();
185   bool isFoundWidget = false;
186   ModuleBase_Tools::activateWindow(this, "XGUI_PropertyPanel::activateNextWidget()");
187   for (; anIt != aLast; anIt++) {
188     ModuleBase_ModelWidget* aCurrentWidget = *anIt;
189     if (isFoundWidget || !theWidget) {
190
191       if (!aValidators->isCase(aCurrentWidget->feature(), aCurrentWidget->attributeID()))
192         continue; // this attribute is not participated in the current case
193       if (isCheckVisibility) {
194         if (!aCurrentWidget->isVisible())
195           continue;
196       }
197       if (!aCurrentWidget->isObligatory())
198         continue; // not obligatory widgets are not activated automatically
199
200       if (aCurrentWidget->focusTo()) {
201         return;
202       }
203     }
204     isFoundWidget = isFoundWidget || (*anIt) == theWidget;
205   }
206   activateWidget(NULL);
207   //focusNextPrevChild(true);
208 }
209
210 //#define DEBUG_TAB_WIDGETS
211
212 #define DEBUG_TAB
213 #ifdef DEBUG_TAB
214 void findDirectChildren(QWidget* theParent, QList<QWidget*>& theWidgets, const bool theDebug)
215 {
216   QList<QWidget*> aWidgets;
217
218   if (theParent) {
219     QLayout* aLayout = theParent->layout();
220     if (aLayout) {
221       for (int i = 0, aCount = aLayout->count(); i < aCount; i++) {
222         QLayoutItem* anItem = aLayout->itemAt(i);
223         QWidget* aWidget = anItem ? anItem->widget() : 0;
224         if (aWidget && aWidget->isVisible()) {
225           if (aWidget->focusPolicy() != Qt::NoFocus)
226             theWidgets.append(aWidget);
227           findDirectChildren(aWidget, theWidgets, false);
228         }
229       }
230     }
231   }
232 #ifdef DEBUG_TAB_WIDGETS
233   if (theDebug) {
234     QStringList aWidgetTypes;
235     QList<QWidget*>::const_iterator anIt =  theWidgets.begin(), aLast = theWidgets.end();
236     for (; anIt != aLast; anIt++) {
237       QWidget* aWidget = *anIt;
238       if (aWidget)
239         aWidgetTypes.append(aWidget->objectName());
240     }
241     QString anInfo = QString("theWidgets[%1]: %2").arg(theWidgets.count()).arg(aWidgetTypes.join(","));
242     qDebug(anInfo.toStdString().c_str());
243   }
244 #endif
245 }
246
247 bool XGUI_PropertyPanel::focusNextPrevChild(bool theIsNext)
248 {
249   //bool isChangedFocus = ModuleBase_IPropertyPanel::focusNextPrevChild(theIsNext);
250   //return true;//isChangedFocus;
251
252   // it wraps the Tabs clicking to follow in the chain:
253   // controls, last control, Apply, Cancel, first control, controls
254   bool isChangedFocus = false;
255
256   QWidget* aFocusWidget = focusWidget();
257 #ifdef DEBUG_TAB_WIDGETS
258   if (aFocusWidget) {
259     QString anInfo = QString("focus Widget: %1").arg(aFocusWidget->objectName());
260     qDebug(anInfo.toStdString().c_str());
261   }
262 #endif
263
264   QWidget* aNewFocusWidget = 0;
265   if (aFocusWidget) {
266     QList<QWidget*> aChildren;
267     findDirectChildren(this, aChildren, true);
268     int aChildrenCount = aChildren.count();
269     int aFocusWidgetIndex = aChildren.indexOf(aFocusWidget);
270     if (aFocusWidgetIndex >= 0) {
271       if (theIsNext) {
272         if (aFocusWidgetIndex == aChildrenCount-1) {
273           // after the last widget focus should be set to "Apply"
274           QToolButton* anOkBtn = findChild<QToolButton*>(PROP_PANEL_OK);
275           aNewFocusWidget = anOkBtn;
276         }
277         else {
278           aNewFocusWidget = aChildren[aFocusWidgetIndex+1];
279         }
280       }
281       else {
282         if (aFocusWidgetIndex == 0) {
283           // before the first widget, the last should accept focus
284           aNewFocusWidget = aChildren[aChildrenCount - 1];
285         }
286         else {
287           // before the "Apply" button, the last should accept focus for consistency with "Next"
288           QToolButton* anOkBtn = findChild<QToolButton*>(PROP_PANEL_OK);
289           if (aFocusWidget == anOkBtn) {
290             aNewFocusWidget = aChildren[aChildrenCount - 1];
291           }
292           else {
293             aNewFocusWidget = aChildren[aFocusWidgetIndex-1];
294           }
295         }
296       }
297     }
298   }
299   if (aNewFocusWidget) {
300     if (myActiveWidget) {
301       myActiveWidget->getControls();
302       bool isFirstControl = !theIsNext;
303       QWidget* aLastFocusControl = myActiveWidget->getControlAcceptingFocus(isFirstControl);
304       if (aFocusWidget == aLastFocusControl) {
305         setActiveWidget(NULL);
306       }
307     }
308
309     //ModuleBase_Tools::setFocus(aNewFocusWidget, "XGUI_PropertyPanel::focusNextPrevChild()");
310     aNewFocusWidget->setFocus(theIsNext ? Qt::TabFocusReason : Qt::BacktabFocusReason);
311     isChangedFocus = true;
312   }
313   return isChangedFocus;
314 }
315 #else
316 bool XGUI_PropertyPanel::focusNextPrevChild(bool theIsNext)
317 {
318   // it wraps the Tabs clicking to follow in the chain:
319   // controls, last control, Apply, Cancel, first control, controls
320   bool isChangedFocus = false;
321
322   if (theIsNext) { // forward by Tab
323     QToolButton* aCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
324     if (aCancelBtn->hasFocus()) {
325       // after cancel, the first control should be focused
326       QWidget* aFirstControl = 0;
327       for (int i = 0, aSize = myWidgets.size(); i < aSize && !aFirstControl; i++)
328         aFirstControl = myWidgets[i]->getControlAcceptingFocus(true);
329       if (aFirstControl)
330         ModuleBase_Tools::setFocus(aFirstControl, "XGUI_PropertyPanel::focusNextPrevChild()");
331         isChangedFocus = true;
332     }
333     else {
334       // after the last control, the Apply button should be focused
335       QWidget* aLastControl = 0;
336       for (int i = myWidgets.size()-1; i >= 0 && !aLastControl; i--)
337         aLastControl = myWidgets[i]->getControlAcceptingFocus(false);
338       if (aLastControl && aLastControl->hasFocus()) {
339         setFocusOnOkButton();
340         isChangedFocus = true;
341       }
342     }
343   }
344   else { // backward by SHIFT + Tab
345     QToolButton* anOkBtn = findChild<QToolButton*>(PROP_PANEL_OK);
346     if (anOkBtn->hasFocus()) {
347       // after Apply, the last control should be focused
348       QWidget* aLastControl = 0;
349       for (int i = myWidgets.size()-1; i >= 0 && !aLastControl; i--)
350         aLastControl = myWidgets[i]->getControlAcceptingFocus(false);
351       if (aLastControl)
352         ModuleBase_Tools::setFocus(aLastControl, "XGUI_PropertyPanel::focusNextPrevChild()");
353         isChangedFocus = true;
354     }
355     else {
356       // after the first control, the Cancel button should be focused
357       QWidget* aFirstControl = 0;
358       for (int i = 0, aSize = myWidgets.size(); i < aSize && !aFirstControl; i++)
359         aFirstControl = myWidgets[i]->getControlAcceptingFocus(true);
360       if (aFirstControl && aFirstControl->hasFocus()) {
361         QToolButton* aCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
362         ModuleBase_Tools::setFocus(aCancelBtn, "XGUI_PropertyPanel::focusNextPrevChild()");
363         isChangedFocus = true;
364       }
365     }
366   }
367
368   if (!isChangedFocus)
369     isChangedFocus = ModuleBase_IPropertyPanel::focusNextPrevChild(theIsNext);
370   return isChangedFocus;
371 }
372
373 #endif
374 void XGUI_PropertyPanel::activateNextWidget()
375 {
376   activateNextWidget(myActiveWidget);
377 }
378
379 void XGUI_PropertyPanel::activateWidget(ModuleBase_ModelWidget* theWidget)
380 {
381   std::string aPreviosAttributeID;
382   if(myActiveWidget)
383     aPreviosAttributeID = myActiveWidget->attributeID();
384
385   // Avoid activation of already actve widget. It could happen on focusIn event many times
386   if (setActiveWidget(theWidget)) {
387     emit widgetActivated(myActiveWidget);
388     if (!myActiveWidget && !isEditingMode()) {
389       emit noMoreWidgets(aPreviosAttributeID);
390     }
391   }
392 }
393
394 bool XGUI_PropertyPanel::setActiveWidget(ModuleBase_ModelWidget* theWidget)
395 {
396   // Avoid activation of already actve widget. It could happen on focusIn event many times
397   if (theWidget == myActiveWidget) {
398     return false;
399   }
400   std::string aPreviosAttributeID;
401   if(myActiveWidget) {
402     aPreviosAttributeID = myActiveWidget->attributeID();
403     myActiveWidget->deactivate();
404     myActiveWidget->setHighlighted(false);
405   }
406   if(theWidget) {
407     emit beforeWidgetActivated(theWidget);
408     theWidget->setHighlighted(true);
409     theWidget->activate();
410   }
411   myActiveWidget = theWidget;
412   return true;
413 }
414
415 void XGUI_PropertyPanel::setFocusOnOkButton()
416 {
417   QToolButton* anOkBtn = findChild<QToolButton*>(PROP_PANEL_OK);
418   ModuleBase_Tools::setFocus(anOkBtn, "XGUI_PropertyPanel::setFocusOnOkButton()");
419 }
420
421 void XGUI_PropertyPanel::setCancelEnabled(bool theEnabled)
422 {
423   QToolButton* anCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
424   anCancelBtn->setEnabled(theEnabled);
425 }
426
427 bool XGUI_PropertyPanel::isCancelEnabled() const
428 {
429   QToolButton* anCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
430   return anCancelBtn->isEnabled();
431 }
432
433 void XGUI_PropertyPanel::setEditingMode(bool isEditing)
434 {
435   ModuleBase_IPropertyPanel::setEditingMode(isEditing);
436   foreach(ModuleBase_ModelWidget* aWgt, myWidgets) {
437     aWgt->setEditingMode(isEditing);
438   }
439 }
440
441 void XGUI_PropertyPanel::setupActions(XGUI_ActionsMgr* theMgr)
442 {
443   QStringList aButtonNames;
444   aButtonNames << PROP_PANEL_OK << PROP_PANEL_CANCEL << PROP_PANEL_HELP;
445   QList<XGUI_ActionsMgr::OperationStateActionId> aActionIds;
446   aActionIds << XGUI_ActionsMgr::Accept << XGUI_ActionsMgr::Abort << XGUI_ActionsMgr::Help;
447   for (int i = 0; i < aButtonNames.size(); ++i) {
448     QToolButton* aBtn = findChild<QToolButton*>(aButtonNames.at(i));
449     QAction* anAct = theMgr->operationStateAction(aActionIds.at(i));
450     aBtn->setDefaultAction(anAct);
451   }
452 }
453
454 ModuleBase_ModelWidget* XGUI_PropertyPanel::preselectionWidget() const
455 {
456   return myPreselectionWidget;
457 }
458
459 void XGUI_PropertyPanel::setPreselectionWidget(ModuleBase_ModelWidget* theWidget)
460 {
461   myPreselectionWidget = theWidget;
462 }
463
464
465 void XGUI_PropertyPanel::closeEvent(QCloseEvent* theEvent)
466 {
467   ModuleBase_Operation* aOp = myOperationMgr->currentOperation();
468   if (aOp) {
469     if (myOperationMgr->abortAllOperations()) {
470       theEvent->accept();
471     } else 
472       theEvent->ignore();
473   } else
474     ModuleBase_IPropertyPanel::closeEvent(theEvent);
475 }