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