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