Salome HOME
Issue #1278 In the sketch the creation line is not functional when I check the box...
[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(onFocusInWidget(ModuleBase_ModelWidget*)));
122     connect(aWidget, SIGNAL(focusOutWidget(ModuleBase_ModelWidget*)),
123             this,    SLOT(onActivateNextWidget(ModuleBase_ModelWidget*)));
124     connect(aWidget, SIGNAL(keyReleased(QObject*, QKeyEvent*)),
125             this,    SIGNAL(keyReleased(QObject*, QKeyEvent*)));
126     connect(aWidget, SIGNAL(enterClicked(QObject*)),
127             this,    SIGNAL(enterClicked(QObject*)));
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::onFocusInWidget(ModuleBase_ModelWidget* theWidget)
166 {
167   if (theWidget->canAcceptFocus())
168     activateWidget(theWidget);
169 }
170
171 void XGUI_PropertyPanel::onActivateNextWidget(ModuleBase_ModelWidget* theWidget)
172 {
173   // this slot happens when some widget lost focus, the next widget which accepts the focus
174   // should be shown, so the second parameter is true
175   // it is important for features where in cases the same attributes are used, isCase for this
176   // attribute returns true, however it can be placed in hidden stack widget(extrusion: elements,
177   // sketch multi rotation -> single/full point)
178   // it is important to check the widget visibility to do not check of the next widget visible
179   // state if the current is not shown. (example: sketch circle re-entrant operation after mouse
180   // release in the viewer, next, radius, widget should be activated but the first is not visualized)
181   bool isVisible = theWidget->isVisible();
182   activateNextWidget(theWidget, isVisible);
183 }
184
185
186 void XGUI_PropertyPanel::activateNextWidget(ModuleBase_ModelWidget* theWidget,
187                                             const bool isCheckVisibility)
188 {
189   // TO CHECK: Editing operation does not have automatical activation of widgets
190   if (isEditingMode()) {
191     activateWidget(NULL);
192     return;
193   }
194   ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
195
196   QList<ModuleBase_ModelWidget*>::const_iterator anIt = myWidgets.begin(), aLast = myWidgets.end();
197   bool isFoundWidget = false;
198   ModuleBase_Tools::activateWindow(this, "XGUI_PropertyPanel::activateNextWidget()");
199   for (; anIt != aLast; anIt++) {
200     ModuleBase_ModelWidget* aCurrentWidget = *anIt;
201     if (isFoundWidget || !theWidget) {
202
203       if (!aValidators->isCase(aCurrentWidget->feature(), aCurrentWidget->attributeID()))
204         continue; // this attribute is not participated in the current case
205       if (isCheckVisibility && !aCurrentWidget->isInternal()) {
206         if (!aCurrentWidget->isVisible())
207           continue;
208       }
209       if (!aCurrentWidget->isObligatory())
210         continue; // not obligatory widgets are not activated automatically
211
212       if (aCurrentWidget->focusTo()) {
213         return;
214       }
215     }
216     isFoundWidget = isFoundWidget || (*anIt) == theWidget;
217   }
218   // set focus to Ok/Cancel button in Property panel if there are no more active widgets
219   // it should be performed before activateWidget(NULL) because it emits some signals which
220   // can be processed by moudule for example as to activate another widget with setting focus
221   QWidget* aNewFocusWidget = 0;
222   QToolButton* anOkBtn = findChild<QToolButton*>(PROP_PANEL_OK);
223   if (anOkBtn->isEnabled())
224     aNewFocusWidget = anOkBtn;
225   else {
226     QToolButton* aCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
227     if (aCancelBtn->isEnabled())
228       aNewFocusWidget = aCancelBtn;
229   }
230   if (aNewFocusWidget)
231     aNewFocusWidget->setFocus(Qt::TabFocusReason);
232
233   activateWidget(NULL);
234 }
235
236 void findDirectChildren(QWidget* theParent, QList<QWidget*>& theWidgets, const bool theDebug)
237 {
238   QList<QWidget*> aWidgets;
239
240   if (theParent) {
241     QLayout* aLayout = theParent->layout();
242     if (aLayout) {
243       for (int i = 0, aCount = aLayout->count(); i < aCount; i++) {
244         QLayoutItem* anItem = aLayout->itemAt(i);
245         QWidget* aWidget = anItem ? anItem->widget() : 0;
246         if (aWidget) {
247           if (aWidget->isVisible()) {
248             if (aWidget->focusPolicy() != Qt::NoFocus)
249               theWidgets.append(aWidget);
250             findDirectChildren(aWidget, theWidgets, false);
251           }
252         }
253         else if (anItem->layout()) {
254           QLayout* aLayout = anItem->layout();
255           for (int i = 0, aCount = aLayout->count(); i < aCount; i++) {
256             QLayoutItem* anItem = aLayout->itemAt(i);
257             QWidget* aWidget = anItem ? anItem->widget() : 0;
258             if (aWidget) {
259               if (aWidget->isVisible()) {
260                 if (aWidget->focusPolicy() != Qt::NoFocus)
261                   theWidgets.append(aWidget);
262                 findDirectChildren(aWidget, theWidgets, false);
263               }
264             }
265             else {
266               // TODO: improve recursive search for the case when here QLayout is used
267               // currently, the switch widget uses only 1 level of qlayout in qlayout using for
268               // example for construction plane feature. If there are more levels,
269               // it should be implemented here
270             }
271           }
272         }
273       }
274     }
275   }
276 #ifdef DEBUG_TAB_WIDGETS
277   if (theDebug) {
278     QStringList aWidgetTypes;
279     QList<QWidget*>::const_iterator anIt =  theWidgets.begin(), aLast = theWidgets.end();
280     for (; anIt != aLast; anIt++) {
281       QWidget* aWidget = *anIt;
282       if (aWidget)
283         aWidgetTypes.append(aWidget->objectName());
284     }
285     QString anInfo = QString("theWidgets[%1]: %2").arg(theWidgets.count()).arg(aWidgetTypes.join(","));
286     qDebug(anInfo.toStdString().c_str());
287   }
288 #endif
289 }
290
291 bool XGUI_PropertyPanel::focusNextPrevChild(bool theIsNext)
292 {
293   // it wraps the Tabs clicking to follow in the chain:
294   // controls, last control, Apply, Cancel, first control, controls
295   bool isChangedFocus = false;
296
297   QWidget* aFocusWidget = focusWidget();
298 #ifdef DEBUG_TAB_WIDGETS
299   if (aFocusWidget) {
300     QString anInfo = QString("focus Widget: %1").arg(aFocusWidget->objectName());
301     qDebug(anInfo.toStdString().c_str());
302   }
303 #endif
304
305   QWidget* aNewFocusWidget = 0;
306   if (aFocusWidget) {
307     QList<QWidget*> aChildren;
308     findDirectChildren(this, aChildren, true);
309     int aChildrenCount = aChildren.count();
310     int aFocusWidgetIndex = aChildren.indexOf(aFocusWidget);
311     if (aFocusWidgetIndex >= 0) {
312       if (theIsNext) {
313         if (aFocusWidgetIndex == aChildrenCount-1) {
314           // after the last widget focus should be set to "Apply"
315           QToolButton* anOkBtn = findChild<QToolButton*>(PROP_PANEL_OK);
316           if (anOkBtn->isEnabled())
317             aNewFocusWidget = anOkBtn;
318           else {
319             QToolButton* aCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
320             if (aCancelBtn->isEnabled())
321               aNewFocusWidget = aCancelBtn;
322           }
323         }
324         else {
325           aNewFocusWidget = aChildren[aFocusWidgetIndex+1];
326         }
327       }
328       else {
329         if (aFocusWidgetIndex == 0) {
330           // before the first widget, the last should accept focus
331           aNewFocusWidget = aChildren[aChildrenCount - 1];
332         }
333         else {
334           // before the "Apply" button, the last should accept focus for consistency with "Next"
335           QToolButton* anOkBtn = findChild<QToolButton*>(PROP_PANEL_OK);
336           if (aFocusWidget == anOkBtn) {
337             aNewFocusWidget = aChildren[aChildrenCount - 1];
338           }
339           else {
340             aNewFocusWidget = aChildren[aFocusWidgetIndex-1];
341           }
342         }
343       }
344     }
345   }
346   if (aNewFocusWidget) {
347     if (myActiveWidget) {
348       myActiveWidget->getControls();
349       bool isFirstControl = !theIsNext;
350       QWidget* aLastFocusControl = myActiveWidget->getControlAcceptingFocus(isFirstControl);
351       if (aFocusWidget == aLastFocusControl) {
352         setActiveWidget(NULL);
353       }
354     }
355
356     //ModuleBase_Tools::setFocus(aNewFocusWidget, "XGUI_PropertyPanel::focusNextPrevChild()");
357     aNewFocusWidget->setFocus(theIsNext ? Qt::TabFocusReason : Qt::BacktabFocusReason);
358     isChangedFocus = true;
359   }
360   return isChangedFocus;
361 }
362
363 void XGUI_PropertyPanel::activateNextWidget()
364 {
365   activateNextWidget(myActiveWidget);
366 }
367
368 void XGUI_PropertyPanel::activateWidget(ModuleBase_ModelWidget* theWidget, const bool theEmitSignal)
369 {
370   std::string aPreviosAttributeID;
371   if(myActiveWidget)
372     aPreviosAttributeID = myActiveWidget->attributeID();
373
374   // Avoid activation of already actve widget. It could happen on focusIn event many times
375   if (setActiveWidget(theWidget) && theEmitSignal) {
376     emit widgetActivated(myActiveWidget);
377     if (!myActiveWidget && !isEditingMode()) {
378       emit noMoreWidgets(aPreviosAttributeID);
379     }
380   }
381 }
382
383 bool XGUI_PropertyPanel::setActiveWidget(ModuleBase_ModelWidget* theWidget)
384 {
385   // Avoid activation of already actve widget. It could happen on focusIn event many times
386   if (theWidget == myActiveWidget) {
387     return false;
388   }
389   std::string aPreviosAttributeID;
390   if(myActiveWidget) {
391     aPreviosAttributeID = myActiveWidget->attributeID();
392     myActiveWidget->deactivate();
393     myActiveWidget->setHighlighted(false);
394   }
395   if(theWidget) {
396     emit beforeWidgetActivated(theWidget);
397     theWidget->setHighlighted(true);
398     theWidget->activate();
399   }
400   myActiveWidget = theWidget;
401   return true;
402 }
403
404 void XGUI_PropertyPanel::setFocusOnOkButton()
405 {
406   QToolButton* anOkBtn = findChild<QToolButton*>(PROP_PANEL_OK);
407   ModuleBase_Tools::setFocus(anOkBtn, "XGUI_PropertyPanel::setFocusOnOkButton()");
408 }
409
410 void XGUI_PropertyPanel::setCancelEnabled(bool theEnabled)
411 {
412   QToolButton* anCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
413   anCancelBtn->setEnabled(theEnabled);
414 }
415
416 bool XGUI_PropertyPanel::isCancelEnabled() const
417 {
418   QToolButton* anCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
419   return anCancelBtn->isEnabled();
420 }
421
422 void XGUI_PropertyPanel::setEditingMode(bool isEditing)
423 {
424   ModuleBase_IPropertyPanel::setEditingMode(isEditing);
425   foreach(ModuleBase_ModelWidget* aWgt, myWidgets) {
426     aWgt->setEditingMode(isEditing);
427   }
428 }
429
430 void XGUI_PropertyPanel::setupActions(XGUI_ActionsMgr* theMgr)
431 {
432   QStringList aButtonNames;
433   aButtonNames << PROP_PANEL_OK << PROP_PANEL_CANCEL << PROP_PANEL_HELP;
434   QList<XGUI_ActionsMgr::OperationStateActionId> aActionIds;
435   aActionIds << XGUI_ActionsMgr::Accept << XGUI_ActionsMgr::Abort << XGUI_ActionsMgr::Help;
436   for (int i = 0; i < aButtonNames.size(); ++i) {
437     QToolButton* aBtn = findChild<QToolButton*>(aButtonNames.at(i));
438     QAction* anAct = theMgr->operationStateAction(aActionIds.at(i));
439     aBtn->setDefaultAction(anAct);
440   }
441 }
442
443 ModuleBase_ModelWidget* XGUI_PropertyPanel::preselectionWidget() const
444 {
445   return myPreselectionWidget;
446 }
447
448 void XGUI_PropertyPanel::setPreselectionWidget(ModuleBase_ModelWidget* theWidget)
449 {
450   myPreselectionWidget = theWidget;
451 }
452
453
454 void XGUI_PropertyPanel::closeEvent(QCloseEvent* theEvent)
455 {
456   ModuleBase_Operation* aOp = myOperationMgr->currentOperation();
457   if (aOp) {
458     if (myOperationMgr->canStopOperation(aOp)) {
459       myOperationMgr->abortOperation(aOp);
460       if (myOperationMgr->hasOperation())
461         theEvent->ignore();
462       else
463         theEvent->accept();
464     } else 
465       theEvent->ignore();
466   } else
467     ModuleBase_IPropertyPanel::closeEvent(theEvent);
468 }