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