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