]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_PropertyPanel.cpp
Salome HOME
#1189 Tab - Cancel - Enter validate not cancel: In case of disabled Apply button...
[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 //#define DEBUG_TAB_WIDGETS
39
40 XGUI_PropertyPanel::XGUI_PropertyPanel(QWidget* theParent, XGUI_OperationMgr* theMgr)
41     : ModuleBase_IPropertyPanel(theParent), 
42     myActiveWidget(NULL),
43     myPreselectionWidget(NULL),
44     myPanelPage(NULL),
45     myOperationMgr(theMgr)
46 {
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 }");
51
52   QWidget* aContent = new QWidget(this);
53   QGridLayout* aMainLayout = new QGridLayout(aContent);
54   const int kPanelColumn = 0;
55   int aPanelRow = 0;
56   aMainLayout->setContentsMargins(3, 3, 3, 3);
57   this->setWidget(aContent);
58
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);
65
66   myHeaderWidget = aFrm;
67
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);
77   }
78   aBtnLay->insertStretch(1, 1);
79
80   myPanelPage = new ModuleBase_PageWidget(aContent);
81   myPanelPage->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::MinimumExpanding);
82   aMainLayout->addWidget(myPanelPage, aPanelRow, kPanelColumn);
83 }
84
85 XGUI_PropertyPanel::~XGUI_PropertyPanel()
86 {
87 }
88
89 void XGUI_PropertyPanel::cleanContent()
90 {
91   if (myActiveWidget)
92     myActiveWidget->deactivate();
93
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;
104     if (aWidget) {
105       aWidget->blockSignals(true);
106     }
107   }
108
109   myWidgets.clear();
110   myPanelPage->clearPage();
111   myActiveWidget = NULL;
112   setWindowTitle(tr("Property Panel"));
113 }
114
115 void XGUI_PropertyPanel::setModelWidgets(const QList<ModuleBase_ModelWidget*>& theWidgets)
116 {
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()));
128
129   }
130 }
131
132 const QList<ModuleBase_ModelWidget*>& XGUI_PropertyPanel::modelWidgets() const
133 {
134   return myWidgets;
135 }
136
137 ModuleBase_PageBase* XGUI_PropertyPanel::contentWidget()
138 {
139   return static_cast<ModuleBase_PageBase*>(myPanelPage);
140 }
141
142 void XGUI_PropertyPanel::updateContentWidget(FeaturePtr theFeature)
143 {
144   // Invalid feature case on abort of the operation
145   if (theFeature.get() == NULL)
146     return;
147   if (theFeature->isAction() || !theFeature->data())
148     return;
149   foreach(ModuleBase_ModelWidget* eachWidget, myWidgets) {
150     if (!eachWidget->feature().get())
151       eachWidget->setFeature(theFeature);
152     eachWidget->restoreValue();
153   }
154   // the repaint is used here to immediately react in GUI to the values change.
155   repaint();
156 }
157
158 void XGUI_PropertyPanel::activateNextWidget(ModuleBase_ModelWidget* theWidget)
159 {
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);
163 }
164
165 void XGUI_PropertyPanel::onActivateNextWidget(ModuleBase_ModelWidget* theWidget)
166 {
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);
177 }
178
179
180 void XGUI_PropertyPanel::activateNextWidget(ModuleBase_ModelWidget* theWidget,
181                                             const bool isCheckVisibility)
182 {
183   // TO CHECK: Editing operation does not have automatical activation of widgets
184   if (isEditingMode()) {
185     activateWidget(NULL);
186     return;
187   }
188   ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
189
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) {
196
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())
201           continue;
202       }
203       if (!aCurrentWidget->isObligatory())
204         continue; // not obligatory widgets are not activated automatically
205
206       if (aCurrentWidget->focusTo()) {
207         return;
208       }
209     }
210     isFoundWidget = isFoundWidget || (*anIt) == theWidget;
211   }
212   activateWidget(NULL);
213 }
214
215 void findDirectChildren(QWidget* theParent, QList<QWidget*>& theWidgets, const bool theDebug)
216 {
217   QList<QWidget*> aWidgets;
218
219   if (theParent) {
220     QLayout* aLayout = theParent->layout();
221     if (aLayout) {
222       for (int i = 0, aCount = aLayout->count(); i < aCount; i++) {
223         QLayoutItem* anItem = aLayout->itemAt(i);
224         QWidget* aWidget = anItem ? anItem->widget() : 0;
225         if (aWidget && aWidget->isVisible()) {
226           if (aWidget->focusPolicy() != Qt::NoFocus)
227             theWidgets.append(aWidget);
228           findDirectChildren(aWidget, theWidgets, false);
229         }
230       }
231     }
232   }
233 #ifdef DEBUG_TAB_WIDGETS
234   if (theDebug) {
235     QStringList aWidgetTypes;
236     QList<QWidget*>::const_iterator anIt =  theWidgets.begin(), aLast = theWidgets.end();
237     for (; anIt != aLast; anIt++) {
238       QWidget* aWidget = *anIt;
239       if (aWidget)
240         aWidgetTypes.append(aWidget->objectName());
241     }
242     QString anInfo = QString("theWidgets[%1]: %2").arg(theWidgets.count()).arg(aWidgetTypes.join(","));
243     qDebug(anInfo.toStdString().c_str());
244   }
245 #endif
246 }
247
248 bool XGUI_PropertyPanel::focusNextPrevChild(bool theIsNext)
249 {
250   // it wraps the Tabs clicking to follow in the chain:
251   // controls, last control, Apply, Cancel, first control, controls
252   bool isChangedFocus = false;
253
254   QWidget* aFocusWidget = focusWidget();
255 #ifdef DEBUG_TAB_WIDGETS
256   if (aFocusWidget) {
257     QString anInfo = QString("focus Widget: %1").arg(aFocusWidget->objectName());
258     qDebug(anInfo.toStdString().c_str());
259   }
260 #endif
261
262   QWidget* aNewFocusWidget = 0;
263   if (aFocusWidget) {
264     QList<QWidget*> aChildren;
265     findDirectChildren(this, aChildren, true);
266     int aChildrenCount = aChildren.count();
267     int aFocusWidgetIndex = aChildren.indexOf(aFocusWidget);
268     if (aFocusWidgetIndex >= 0) {
269       if (theIsNext) {
270         if (aFocusWidgetIndex == aChildrenCount-1) {
271           // after the last widget focus should be set to "Apply"
272           QToolButton* anOkBtn = findChild<QToolButton*>(PROP_PANEL_OK);
273           if (anOkBtn->isEnabled())
274             aNewFocusWidget = anOkBtn;
275           else {
276             QToolButton* aCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
277             if (aCancelBtn->isEnabled())
278               aNewFocusWidget = aCancelBtn;
279           }
280         }
281         else {
282           aNewFocusWidget = aChildren[aFocusWidgetIndex+1];
283         }
284       }
285       else {
286         if (aFocusWidgetIndex == 0) {
287           // before the first widget, the last should accept focus
288           aNewFocusWidget = aChildren[aChildrenCount - 1];
289         }
290         else {
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];
295           }
296           else {
297             aNewFocusWidget = aChildren[aFocusWidgetIndex-1];
298           }
299         }
300       }
301     }
302   }
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);
310       }
311     }
312
313     //ModuleBase_Tools::setFocus(aNewFocusWidget, "XGUI_PropertyPanel::focusNextPrevChild()");
314     aNewFocusWidget->setFocus(theIsNext ? Qt::TabFocusReason : Qt::BacktabFocusReason);
315     isChangedFocus = true;
316   }
317   return isChangedFocus;
318 }
319
320 void XGUI_PropertyPanel::activateNextWidget()
321 {
322   activateNextWidget(myActiveWidget);
323 }
324
325 void XGUI_PropertyPanel::activateWidget(ModuleBase_ModelWidget* theWidget)
326 {
327   std::string aPreviosAttributeID;
328   if(myActiveWidget)
329     aPreviosAttributeID = myActiveWidget->attributeID();
330
331   // Avoid activation of already actve widget. It could happen on focusIn event many times
332   if (setActiveWidget(theWidget)) {
333     emit widgetActivated(myActiveWidget);
334     if (!myActiveWidget && !isEditingMode()) {
335       emit noMoreWidgets(aPreviosAttributeID);
336     }
337   }
338 }
339
340 bool XGUI_PropertyPanel::setActiveWidget(ModuleBase_ModelWidget* theWidget)
341 {
342   // Avoid activation of already actve widget. It could happen on focusIn event many times
343   if (theWidget == myActiveWidget) {
344     return false;
345   }
346   std::string aPreviosAttributeID;
347   if(myActiveWidget) {
348     aPreviosAttributeID = myActiveWidget->attributeID();
349     myActiveWidget->deactivate();
350     myActiveWidget->setHighlighted(false);
351   }
352   if(theWidget) {
353     emit beforeWidgetActivated(theWidget);
354     theWidget->setHighlighted(true);
355     theWidget->activate();
356   }
357   myActiveWidget = theWidget;
358   return true;
359 }
360
361 void XGUI_PropertyPanel::setFocusOnOkButton()
362 {
363   QToolButton* anOkBtn = findChild<QToolButton*>(PROP_PANEL_OK);
364   ModuleBase_Tools::setFocus(anOkBtn, "XGUI_PropertyPanel::setFocusOnOkButton()");
365 }
366
367 void XGUI_PropertyPanel::setCancelEnabled(bool theEnabled)
368 {
369   QToolButton* anCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
370   anCancelBtn->setEnabled(theEnabled);
371 }
372
373 bool XGUI_PropertyPanel::isCancelEnabled() const
374 {
375   QToolButton* anCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
376   return anCancelBtn->isEnabled();
377 }
378
379 void XGUI_PropertyPanel::setEditingMode(bool isEditing)
380 {
381   ModuleBase_IPropertyPanel::setEditingMode(isEditing);
382   foreach(ModuleBase_ModelWidget* aWgt, myWidgets) {
383     aWgt->setEditingMode(isEditing);
384   }
385 }
386
387 void XGUI_PropertyPanel::setupActions(XGUI_ActionsMgr* theMgr)
388 {
389   QStringList aButtonNames;
390   aButtonNames << PROP_PANEL_OK << PROP_PANEL_CANCEL << PROP_PANEL_HELP;
391   QList<XGUI_ActionsMgr::OperationStateActionId> aActionIds;
392   aActionIds << XGUI_ActionsMgr::Accept << XGUI_ActionsMgr::Abort << XGUI_ActionsMgr::Help;
393   for (int i = 0; i < aButtonNames.size(); ++i) {
394     QToolButton* aBtn = findChild<QToolButton*>(aButtonNames.at(i));
395     QAction* anAct = theMgr->operationStateAction(aActionIds.at(i));
396     aBtn->setDefaultAction(anAct);
397   }
398 }
399
400 ModuleBase_ModelWidget* XGUI_PropertyPanel::preselectionWidget() const
401 {
402   return myPreselectionWidget;
403 }
404
405 void XGUI_PropertyPanel::setPreselectionWidget(ModuleBase_ModelWidget* theWidget)
406 {
407   myPreselectionWidget = theWidget;
408 }
409
410
411 void XGUI_PropertyPanel::closeEvent(QCloseEvent* theEvent)
412 {
413   ModuleBase_Operation* aOp = myOperationMgr->currentOperation();
414   if (aOp) {
415     if (myOperationMgr->canStopOperation(aOp)) {
416       myOperationMgr->abortOperation(aOp);
417       if (myOperationMgr->hasOperation())
418         theEvent->ignore();
419       else
420         theEvent->accept();
421     } else 
422       theEvent->ignore();
423   } else
424     ModuleBase_IPropertyPanel::closeEvent(theEvent);
425 }