Salome HOME
Fix compilation errors
[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 #include <ModuleBase_WidgetFactory.h>
19 #include <ModuleBase_OperationDescription.h>
20
21 #include <ModelAPI_Session.h>
22 #include <ModelAPI_Validator.h>
23
24 #include <QEvent>
25 #include <QFrame>
26 #include <QIcon>
27 #include <QKeyEvent>
28 #include <QLayoutItem>
29 #include <QToolButton>
30 #include <QVBoxLayout>
31 #include <QGridLayout>
32 #include <QWidget>
33 #include <QToolButton>
34 #include <QAction>
35
36 #ifdef _DEBUG
37 #include <iostream>
38 #endif
39
40 //#define DEBUG_TAB_WIDGETS
41
42 XGUI_PropertyPanel::XGUI_PropertyPanel(QWidget* theParent, XGUI_OperationMgr* theMgr)
43     : ModuleBase_IPropertyPanel(theParent), 
44     myActiveWidget(NULL),
45     myPreselectionWidget(NULL),
46     myPanelPage(NULL),
47     myOperationMgr(theMgr)
48 {
49   this->setWindowTitle(tr("Property Panel"));
50   QAction* aViewAct = this->toggleViewAction();
51   this->setObjectName(PROP_PANEL);
52   setStyleSheet("::title { position: relative; padding-left: 5px; text-align: left center }");
53
54   QWidget* aContent = new QWidget(this);
55   QGridLayout* aMainLayout = new QGridLayout(aContent);
56   const int kPanelColumn = 0;
57   int aPanelRow = 0;
58   aMainLayout->setContentsMargins(3, 3, 3, 3);
59   this->setWidget(aContent);
60
61   QFrame* aFrm = new QFrame(aContent);
62   aFrm->setFrameStyle(QFrame::Raised);
63   aFrm->setFrameShape(QFrame::Panel);
64   QHBoxLayout* aBtnLay = new QHBoxLayout(aFrm);
65   ModuleBase_Tools::zeroMargins(aBtnLay);
66   aMainLayout->addWidget(aFrm, aPanelRow++, kPanelColumn);
67
68   myHeaderWidget = aFrm;
69
70   QStringList aBtnNames;
71   aBtnNames << QString(PROP_PANEL_HELP)
72             << QString(PROP_PANEL_OK)
73             << QString(PROP_PANEL_CANCEL);
74   foreach(QString eachBtnName, aBtnNames) {
75     QToolButton* aBtn = new QToolButton(aFrm);
76     aBtn->setObjectName(eachBtnName);
77     aBtn->setAutoRaise(true);
78     aBtnLay->addWidget(aBtn);
79   }
80   aBtnLay->insertStretch(1, 1);
81
82   myPanelPage = new ModuleBase_PageWidget(aContent);
83   myPanelPage->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::MinimumExpanding);
84   aMainLayout->addWidget(myPanelPage, aPanelRow, kPanelColumn);
85 }
86
87 XGUI_PropertyPanel::~XGUI_PropertyPanel()
88 {
89 }
90
91 void XGUI_PropertyPanel::cleanContent()
92 {
93   if (myActiveWidget)
94     myActiveWidget->deactivate();
95
96   /// as the widgets are deleted later, it is important that the signals
97   /// of these widgets are not processed. An example of the error is issue 986.
98   /// In the given case, the property panel is firstly filled by new widgets
99   /// of restarted operation and after that the mouse release signal come from
100   /// the widget of the previous operation (Point2d widget about mouse is released
101   /// and focus is out of this widget)
102   QList<ModuleBase_ModelWidget*>::const_iterator anIt = myWidgets.begin(),
103                                                  aLast = myWidgets.end();
104   for (; anIt != aLast; anIt++) {
105     QWidget* aWidget = *anIt;
106     if (aWidget) {
107       aWidget->blockSignals(true);
108     }
109   }
110
111   myWidgets.clear();
112   myPanelPage->clearPage();
113   myActiveWidget = NULL;
114   setWindowTitle(tr("Property Panel"));
115 }
116
117 void XGUI_PropertyPanel::setModelWidgets(const QList<ModuleBase_ModelWidget*>& theWidgets)
118 {
119   myWidgets = theWidgets;
120   if (theWidgets.empty()) return;
121   foreach (ModuleBase_ModelWidget* aWidget, theWidgets) {
122     connect(aWidget, SIGNAL(focusInWidget(ModuleBase_ModelWidget*)),
123             this,    SLOT(onFocusInWidget(ModuleBase_ModelWidget*)));
124     connect(aWidget, SIGNAL(focusOutWidget(ModuleBase_ModelWidget*)),
125             this,    SLOT(onActivateNextWidget(ModuleBase_ModelWidget*)));
126     connect(aWidget, SIGNAL(keyReleased(QObject*, QKeyEvent*)),
127             this,    SIGNAL(keyReleased(QObject*, QKeyEvent*)));
128     connect(aWidget, SIGNAL(enterClicked(QObject*)),
129             this,    SIGNAL(enterClicked(QObject*)));
130
131   }
132 }
133
134 const QList<ModuleBase_ModelWidget*>& XGUI_PropertyPanel::modelWidgets() const
135 {
136   return myWidgets;
137 }
138
139 ModuleBase_PageBase* XGUI_PropertyPanel::contentWidget()
140 {
141   return static_cast<ModuleBase_PageBase*>(myPanelPage);
142 }
143
144 void XGUI_PropertyPanel::updateContentWidget(FeaturePtr theFeature)
145 {
146   // Invalid feature case on abort of the operation
147   if (theFeature.get() == NULL)
148     return;
149   if (theFeature->isAction() || !theFeature->data())
150     return;
151   foreach(ModuleBase_ModelWidget* eachWidget, myWidgets) {
152     if (!eachWidget->feature().get())
153       eachWidget->setFeature(theFeature);
154     eachWidget->restoreValue();
155   }
156   // the repaint is used here to immediately react in GUI to the values change.
157   repaint();
158 }
159
160 void XGUI_PropertyPanel::createContentPanel(FeaturePtr theFeature)
161 {
162   // Invalid feature case on abort of the operation
163   if (theFeature.get() == NULL)
164     return;
165   if (theFeature->isAction() || !theFeature->data())
166     return;
167
168   if (myWidgets.empty()) {
169     ModuleBase_Operation* anOperation = myOperationMgr->currentOperation();
170     QString aXmlRepr = anOperation->getDescription()->xmlRepresentation();
171
172     ModuleBase_WidgetFactory aFactory(aXmlRepr.toStdString(), myOperationMgr->workshop());
173     aFactory.createPanel(contentWidget(), theFeature);
174     /// Apply button should be update if the feature was modified by the panel
175     myOperationMgr->onValidateOperation();
176   }
177 }
178
179 void XGUI_PropertyPanel::activateNextWidget(ModuleBase_ModelWidget* theWidget)
180 {
181   // it is possible that the property panel widgets have not been visualized
182   // (e.g. on start operation), so it is strictly important to do not check visualized state
183   activateNextWidget(theWidget, false);
184 }
185
186 void XGUI_PropertyPanel::onFocusInWidget(ModuleBase_ModelWidget* theWidget)
187 {
188   if (theWidget->canAcceptFocus())
189     activateWidget(theWidget);
190 }
191
192 void XGUI_PropertyPanel::onActivateNextWidget(ModuleBase_ModelWidget* theWidget)
193 {
194   // this slot happens when some widget lost focus, the next widget which accepts the focus
195   // should be shown, so the second parameter is true
196   // it is important for features where in cases the same attributes are used, isCase for this
197   // attribute returns true, however it can be placed in hidden stack widget(extrusion: elements,
198   // sketch multi rotation -> single/full point)
199   // it is important to check the widget visibility to do not check of the next widget visible
200   // state if the current is not shown. (example: sketch circle re-entrant operation after mouse
201   // release in the viewer, next, radius, widget should be activated but the first is not visualized)
202   bool isVisible = theWidget->isVisible();
203   activateNextWidget(theWidget, isVisible);
204 }
205
206
207 void XGUI_PropertyPanel::activateNextWidget(ModuleBase_ModelWidget* theWidget,
208                                             const bool isCheckVisibility)
209 {
210   // TO CHECK: Editing operation does not have automatical activation of widgets
211   if (isEditingMode()) {
212     activateWidget(NULL);
213     return;
214   }
215   ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
216
217   QList<ModuleBase_ModelWidget*>::const_iterator anIt = myWidgets.begin(), aLast = myWidgets.end();
218   bool isFoundWidget = false;
219   ModuleBase_Tools::activateWindow(this, "XGUI_PropertyPanel::activateNextWidget()");
220   for (; anIt != aLast; anIt++) {
221     ModuleBase_ModelWidget* aCurrentWidget = *anIt;
222     if (isFoundWidget || !theWidget) {
223
224       if (!aValidators->isCase(aCurrentWidget->feature(), aCurrentWidget->attributeID()))
225         continue; // this attribute is not participated in the current case
226       if (isCheckVisibility && !aCurrentWidget->isInternal()) {
227         if (!aCurrentWidget->isVisible())
228           continue;
229       }
230       if (!aCurrentWidget->isObligatory())
231         continue; // not obligatory widgets are not activated automatically
232
233       if (aCurrentWidget->focusTo()) {
234         return;
235       }
236     }
237     isFoundWidget = isFoundWidget || (*anIt) == theWidget;
238   }
239   // set focus to Ok/Cancel button in Property panel if there are no more active widgets
240   // it should be performed before activateWidget(NULL) because it emits some signals which
241   // can be processed by moudule for example as to activate another widget with setting focus
242   QWidget* aNewFocusWidget = 0;
243   QToolButton* anOkBtn = findChild<QToolButton*>(PROP_PANEL_OK);
244   if (anOkBtn->isEnabled())
245     aNewFocusWidget = anOkBtn;
246   else {
247     QToolButton* aCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
248     if (aCancelBtn->isEnabled())
249       aNewFocusWidget = aCancelBtn;
250   }
251   if (aNewFocusWidget)
252     aNewFocusWidget->setFocus(Qt::TabFocusReason);
253
254   activateWidget(NULL);
255 }
256
257 void findDirectChildren(QWidget* theParent, QList<QWidget*>& theWidgets, const bool theDebug)
258 {
259   QList<QWidget*> aWidgets;
260
261   if (theParent) {
262     QLayout* aLayout = theParent->layout();
263     if (aLayout) {
264       for (int i = 0, aCount = aLayout->count(); i < aCount; i++) {
265         QLayoutItem* anItem = aLayout->itemAt(i);
266         QWidget* aWidget = anItem ? anItem->widget() : 0;
267         if (aWidget) {
268           if (aWidget->isVisible()) {
269             if (aWidget->focusPolicy() != Qt::NoFocus)
270               theWidgets.append(aWidget);
271             findDirectChildren(aWidget, theWidgets, false);
272           }
273         }
274         else if (anItem->layout()) {
275           QLayout* aLayout = anItem->layout();
276           for (int i = 0, aCount = aLayout->count(); i < aCount; i++) {
277             QLayoutItem* anItem = aLayout->itemAt(i);
278             QWidget* aWidget = anItem ? anItem->widget() : 0;
279             if (aWidget) {
280               if (aWidget->isVisible()) {
281                 if (aWidget->focusPolicy() != Qt::NoFocus)
282                   theWidgets.append(aWidget);
283                 findDirectChildren(aWidget, theWidgets, false);
284               }
285             }
286             else {
287               // TODO: improve recursive search for the case when here QLayout is used
288               // currently, the switch widget uses only 1 level of qlayout in qlayout using for
289               // example for construction plane feature. If there are more levels,
290               // it should be implemented here
291             }
292           }
293         }
294       }
295     }
296   }
297 #ifdef DEBUG_TAB_WIDGETS
298   if (theDebug) {
299     QStringList aWidgetTypes;
300     QList<QWidget*>::const_iterator anIt =  theWidgets.begin(), aLast = theWidgets.end();
301     for (; anIt != aLast; anIt++) {
302       QWidget* aWidget = *anIt;
303       if (aWidget)
304         aWidgetTypes.append(aWidget->objectName());
305     }
306     QString anInfo = QString("theWidgets[%1]: %2").arg(theWidgets.count()).arg(aWidgetTypes.join(","));
307     qDebug(anInfo.toStdString().c_str());
308   }
309 #endif
310 }
311
312 bool XGUI_PropertyPanel::focusNextPrevChild(bool theIsNext)
313 {
314   // it wraps the Tabs clicking to follow in the chain:
315   // controls, last control, Apply, Cancel, first control, controls
316   bool isChangedFocus = false;
317
318   QWidget* aFocusWidget = focusWidget();
319 #ifdef DEBUG_TAB_WIDGETS
320   if (aFocusWidget) {
321     QString anInfo = QString("focus Widget: %1").arg(aFocusWidget->objectName());
322     qDebug(anInfo.toStdString().c_str());
323   }
324 #endif
325
326   QWidget* aNewFocusWidget = 0;
327   if (aFocusWidget) {
328     QList<QWidget*> aChildren;
329     findDirectChildren(this, aChildren, true);
330     int aChildrenCount = aChildren.count();
331     int aFocusWidgetIndex = aChildren.indexOf(aFocusWidget);
332     if (aFocusWidgetIndex >= 0) {
333       if (theIsNext) {
334         if (aFocusWidgetIndex == aChildrenCount-1) {
335           // after the last widget focus should be set to "Apply"
336           QToolButton* anOkBtn = findChild<QToolButton*>(PROP_PANEL_OK);
337           if (anOkBtn->isEnabled())
338             aNewFocusWidget = anOkBtn;
339           else {
340             QToolButton* aCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
341             if (aCancelBtn->isEnabled())
342               aNewFocusWidget = aCancelBtn;
343           }
344         }
345         else {
346           aNewFocusWidget = aChildren[aFocusWidgetIndex+1];
347         }
348       }
349       else {
350         if (aFocusWidgetIndex == 0) {
351           // before the first widget, the last should accept focus
352           aNewFocusWidget = aChildren[aChildrenCount - 1];
353         }
354         else {
355           // before the "Apply" button, the last should accept focus for consistency with "Next"
356           QToolButton* anOkBtn = findChild<QToolButton*>(PROP_PANEL_OK);
357           if (aFocusWidget == anOkBtn) {
358             aNewFocusWidget = aChildren[aChildrenCount - 1];
359           }
360           else {
361             aNewFocusWidget = aChildren[aFocusWidgetIndex-1];
362           }
363         }
364       }
365     }
366   }
367   if (aNewFocusWidget) {
368     if (myActiveWidget) {
369       myActiveWidget->getControls();
370       bool isFirstControl = !theIsNext;
371       QWidget* aLastFocusControl = myActiveWidget->getControlAcceptingFocus(isFirstControl);
372       if (aFocusWidget == aLastFocusControl) {
373         setActiveWidget(NULL);
374       }
375     }
376
377     //ModuleBase_Tools::setFocus(aNewFocusWidget, "XGUI_PropertyPanel::focusNextPrevChild()");
378     aNewFocusWidget->setFocus(theIsNext ? Qt::TabFocusReason : Qt::BacktabFocusReason);
379     isChangedFocus = true;
380   }
381   return isChangedFocus;
382 }
383
384 void XGUI_PropertyPanel::activateNextWidget()
385 {
386   activateNextWidget(myActiveWidget);
387 }
388
389 void XGUI_PropertyPanel::activateWidget(ModuleBase_ModelWidget* theWidget, const bool theEmitSignal)
390 {
391   std::string aPreviosAttributeID;
392   if(myActiveWidget)
393     aPreviosAttributeID = myActiveWidget->attributeID();
394
395   // Avoid activation of already actve widget. It could happen on focusIn event many times
396   if (setActiveWidget(theWidget) && theEmitSignal) {
397     emit widgetActivated(myActiveWidget);
398     if (!myActiveWidget && !isEditingMode()) {
399       emit noMoreWidgets(aPreviosAttributeID);
400     }
401   }
402 }
403
404 bool XGUI_PropertyPanel::setActiveWidget(ModuleBase_ModelWidget* theWidget)
405 {
406   // Avoid activation of already actve widget. It could happen on focusIn event many times
407   if (theWidget == myActiveWidget) {
408     return false;
409   }
410   std::string aPreviosAttributeID;
411   if(myActiveWidget) {
412     aPreviosAttributeID = myActiveWidget->attributeID();
413     myActiveWidget->deactivate();
414     myActiveWidget->setHighlighted(false);
415   }
416   if(theWidget) {
417     emit beforeWidgetActivated(theWidget);
418     theWidget->setHighlighted(true);
419     theWidget->activate();
420   }
421   myActiveWidget = theWidget;
422   return true;
423 }
424
425 void XGUI_PropertyPanel::setFocusOnOkButton()
426 {
427   QToolButton* anOkBtn = findChild<QToolButton*>(PROP_PANEL_OK);
428   ModuleBase_Tools::setFocus(anOkBtn, "XGUI_PropertyPanel::setFocusOnOkButton()");
429 }
430
431 void XGUI_PropertyPanel::setCancelEnabled(bool theEnabled)
432 {
433   QToolButton* anCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
434   anCancelBtn->setEnabled(theEnabled);
435 }
436
437 bool XGUI_PropertyPanel::isCancelEnabled() const
438 {
439   QToolButton* anCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
440   return anCancelBtn->isEnabled();
441 }
442
443 void XGUI_PropertyPanel::setEditingMode(bool isEditing)
444 {
445   ModuleBase_IPropertyPanel::setEditingMode(isEditing);
446   foreach(ModuleBase_ModelWidget* aWgt, myWidgets) {
447     aWgt->setEditingMode(isEditing);
448   }
449 }
450
451 void XGUI_PropertyPanel::setupActions(XGUI_ActionsMgr* theMgr)
452 {
453   QStringList aButtonNames;
454   aButtonNames << PROP_PANEL_OK << PROP_PANEL_CANCEL << PROP_PANEL_HELP;
455   QList<XGUI_ActionsMgr::OperationStateActionId> aActionIds;
456   aActionIds << XGUI_ActionsMgr::Accept << XGUI_ActionsMgr::Abort << XGUI_ActionsMgr::Help;
457   for (int i = 0; i < aButtonNames.size(); ++i) {
458     QToolButton* aBtn = findChild<QToolButton*>(aButtonNames.at(i));
459     QAction* anAct = theMgr->operationStateAction(aActionIds.at(i));
460     aBtn->setDefaultAction(anAct);
461   }
462 }
463
464 ModuleBase_ModelWidget* XGUI_PropertyPanel::preselectionWidget() const
465 {
466   return myPreselectionWidget;
467 }
468
469 void XGUI_PropertyPanel::setPreselectionWidget(ModuleBase_ModelWidget* theWidget)
470 {
471   myPreselectionWidget = theWidget;
472 }
473
474
475 void XGUI_PropertyPanel::closeEvent(QCloseEvent* theEvent)
476 {
477   ModuleBase_Operation* aOp = myOperationMgr->currentOperation();
478   if (aOp) {
479     if (myOperationMgr->canStopOperation(aOp)) {
480       myOperationMgr->abortOperation(aOp);
481       if (myOperationMgr->hasOperation())
482         theEvent->ignore();
483       else
484         theEvent->accept();
485     } else 
486       theEvent->ignore();
487   } else
488     ModuleBase_IPropertyPanel::closeEvent(theEvent);
489 }