Salome HOME
#816 In multi-rotation, be able to put the total angle or the step angle (Rotation...
[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
19 #include <ModelAPI_Session.h>
20 #include <ModelAPI_Validator.h>
21
22 #include <QEvent>
23 #include <QFrame>
24 #include <QIcon>
25 #include <QKeyEvent>
26 #include <QLayoutItem>
27 #include <QToolButton>
28 #include <QVBoxLayout>
29 #include <QGridLayout>
30 #include <QWidget>
31 #include <QToolButton>
32 #include <QAction>
33
34 #ifdef _DEBUG
35 #include <iostream>
36 #endif
37
38 XGUI_PropertyPanel::XGUI_PropertyPanel(QWidget* theParent, XGUI_OperationMgr* theMgr)
39     : ModuleBase_IPropertyPanel(theParent), 
40     myActiveWidget(NULL),
41     myPreselectionWidget(NULL),
42     myPanelPage(NULL),
43     myOperationMgr(theMgr)
44 {
45   this->setWindowTitle(tr("Property Panel"));
46   QAction* aViewAct = this->toggleViewAction();
47   this->setObjectName(PROP_PANEL);
48   setStyleSheet("::title { position: relative; padding-left: 5px; text-align: left center }");
49
50   QWidget* aContent = new QWidget(this);
51   QGridLayout* aMainLayout = new QGridLayout(aContent);
52   const int kPanelColumn = 0;
53   int aPanelRow = 0;
54   aMainLayout->setContentsMargins(3, 3, 3, 3);
55   this->setWidget(aContent);
56
57   QFrame* aFrm = new QFrame(aContent);
58   aFrm->setFrameStyle(QFrame::Raised);
59   aFrm->setFrameShape(QFrame::Panel);
60   QHBoxLayout* aBtnLay = new QHBoxLayout(aFrm);
61   ModuleBase_Tools::zeroMargins(aBtnLay);
62   aMainLayout->addWidget(aFrm, aPanelRow++, kPanelColumn);
63
64   myHeaderWidget = aFrm;
65
66   QStringList aBtnNames;
67   aBtnNames << QString(PROP_PANEL_HELP)
68             << QString(PROP_PANEL_OK)
69             << QString(PROP_PANEL_CANCEL);
70   foreach(QString eachBtnName, aBtnNames) {
71     QToolButton* aBtn = new QToolButton(aFrm);
72     aBtn->setObjectName(eachBtnName);
73     aBtn->setAutoRaise(true);
74     aBtnLay->addWidget(aBtn);
75   }
76   aBtnLay->insertStretch(1, 1);
77
78   myPanelPage = new ModuleBase_PageWidget(aContent);
79   myPanelPage->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::MinimumExpanding);
80   aMainLayout->addWidget(myPanelPage, aPanelRow, kPanelColumn);
81 }
82
83 XGUI_PropertyPanel::~XGUI_PropertyPanel()
84 {
85 }
86
87 void XGUI_PropertyPanel::cleanContent()
88 {
89   if (myActiveWidget)
90     myActiveWidget->deactivate();
91
92   /// as the widgets are deleted later, it is important that the signals
93   /// of these widgets are not processed. An example of the error is issue 986.
94   /// In the given case, the property panel is firstly filled by new widgets
95   /// of restarted operation and after that the mouse release signal come from
96   /// the widget of the previous operation (Point2d widget about mouse is released
97   /// and focus is out of this widget)
98   QList<ModuleBase_ModelWidget*>::const_iterator anIt = myWidgets.begin(),
99                                                  aLast = myWidgets.end();
100   for (; anIt != aLast; anIt++) {
101     QWidget* aWidget = *anIt;
102     if (aWidget) {
103       aWidget->blockSignals(true);
104     }
105   }
106
107   myWidgets.clear();
108   myPanelPage->clearPage();
109   myActiveWidget = NULL;
110   setWindowTitle(tr("Property Panel"));
111 }
112
113 void XGUI_PropertyPanel::setModelWidgets(const QList<ModuleBase_ModelWidget*>& theWidgets)
114 {
115   myWidgets = theWidgets;
116   if (theWidgets.empty()) return;
117   foreach (ModuleBase_ModelWidget* aWidget, theWidgets) {
118     connect(aWidget, SIGNAL(focusInWidget(ModuleBase_ModelWidget*)),
119             this,    SLOT(activateWidget(ModuleBase_ModelWidget*)));
120     connect(aWidget, SIGNAL(focusOutWidget(ModuleBase_ModelWidget*)),
121             this,    SLOT(activateNextWidget(ModuleBase_ModelWidget*)));
122     connect(aWidget, SIGNAL(keyReleased(QKeyEvent*)),
123             this,    SIGNAL(keyReleased(QKeyEvent*)));
124   }
125 }
126
127 const QList<ModuleBase_ModelWidget*>& XGUI_PropertyPanel::modelWidgets() const
128 {
129   return myWidgets;
130 }
131
132 ModuleBase_PageBase* XGUI_PropertyPanel::contentWidget()
133 {
134   return static_cast<ModuleBase_PageBase*>(myPanelPage);
135 }
136
137 void XGUI_PropertyPanel::updateContentWidget(FeaturePtr theFeature)
138 {
139   // Invalid feature case on abort of the operation
140   if (theFeature.get() == NULL)
141     return;
142   if (theFeature->isAction() || !theFeature->data())
143     return;
144   foreach(ModuleBase_ModelWidget* eachWidget, myWidgets) {
145     if (!eachWidget->feature().get())
146       eachWidget->setFeature(theFeature);
147     eachWidget->restoreValue();
148   }
149   // the repaint is used here to immediately react in GUI to the values change.
150   repaint();
151 }
152
153 void XGUI_PropertyPanel::activateNextWidget(ModuleBase_ModelWidget* theWidget)
154 {
155   // TO CHECK: Editing operation does not have automatical activation of widgets
156   if (isEditingMode()) {
157     activateWidget(NULL);
158     return;
159   }
160   ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
161
162   QList<ModuleBase_ModelWidget*>::const_iterator anIt = myWidgets.begin(), aLast = myWidgets.end();
163   bool isFoundWidget = false;
164   activateWindow();
165   for (; anIt != aLast; anIt++) {
166     ModuleBase_ModelWidget* aCurrentWidget = *anIt;
167     if (isFoundWidget || !theWidget) {
168
169       if (!aValidators->isCase(aCurrentWidget->feature(), aCurrentWidget->attributeID()))
170         continue; // this attribute is not participated in the current case
171
172       if (aCurrentWidget->focusTo()) {
173         return;
174       }
175     }
176     isFoundWidget = isFoundWidget || (*anIt) == theWidget;
177   }
178   activateWidget(NULL);
179 }
180
181 bool XGUI_PropertyPanel::focusNextPrevChild(bool theIsNext)
182 {
183   // it wraps the Tabs clicking to follow in the chain:
184   // controls, last control, Apply, Cancel, first control, controls
185
186   bool isChangedFocus = false;
187   if (theIsNext) { // forward by Tab
188     QToolButton* aCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
189     if (aCancelBtn->hasFocus()) {
190       // after cancel, the first control should be focused
191       QWidget* aFirstControl = 0;
192       for (int i = 0, aSize = myWidgets.size(); i < aSize && !aFirstControl; i++)
193         aFirstControl = myWidgets[i]->getControlAcceptingFocus(true);
194       if (aFirstControl)
195         aFirstControl->setFocus();
196         isChangedFocus = true;
197     }
198     else {
199       // after the last control, the Apply button should be focused
200       QWidget* aLastControl = 0;
201       for (int i = myWidgets.size()-1; i >= 0 && !aLastControl; i--)
202         aLastControl = myWidgets[i]->getControlAcceptingFocus(false);
203       if (aLastControl && aLastControl->hasFocus()) {
204         setFocusOnOkButton();
205         isChangedFocus = true;
206       }
207     }
208   }
209   else { // backward by SHIFT + Tab
210     QToolButton* anOkBtn = findChild<QToolButton*>(PROP_PANEL_OK);
211     if (anOkBtn->hasFocus()) {
212       // after Apply, the last control should be focused
213       QWidget* aLastControl = 0;
214       for (int i = myWidgets.size()-1; i >= 0 && !aLastControl; i--)
215         aLastControl = myWidgets[i]->getControlAcceptingFocus(false);
216       if (aLastControl)
217         aLastControl->setFocus();
218         isChangedFocus = true;
219     }
220     else {
221       // after the first control, the Cancel button should be focused
222       QWidget* aFirstControl = 0;
223       for (int i = 0, aSize = myWidgets.size(); i < aSize && !aFirstControl; i++)
224         aFirstControl = myWidgets[i]->getControlAcceptingFocus(true);
225       if (aFirstControl && aFirstControl->hasFocus()) {
226         QToolButton* aCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
227         aCancelBtn->setFocus();
228         isChangedFocus = true;
229       }
230     }
231   }
232
233   if (!isChangedFocus)
234     isChangedFocus = ModuleBase_IPropertyPanel::focusNextPrevChild(theIsNext);
235
236   return isChangedFocus;
237 }
238
239 void XGUI_PropertyPanel::activateNextWidget()
240 {
241   activateNextWidget(myActiveWidget);
242 }
243
244 void XGUI_PropertyPanel::activateWidget(ModuleBase_ModelWidget* theWidget)
245 {
246   // Avoid activation of already actve widget. It could happen on focusIn event many times
247   if (setActiveWidget(theWidget)) {
248     if (myActiveWidget) {
249       emit widgetActivated(myActiveWidget);
250     } else if (!isEditingMode()) {
251       emit noMoreWidgets();
252       //setFocusOnOkButton();
253     }
254   }
255 }
256
257 bool XGUI_PropertyPanel::setActiveWidget(ModuleBase_ModelWidget* theWidget)
258 {
259   // Avoid activation of already actve widget. It could happen on focusIn event many times
260   if (theWidget == myActiveWidget) {
261     return false;
262   }
263   if(myActiveWidget) {
264     myActiveWidget->deactivate();
265     myActiveWidget->setHighlighted(false);
266   }
267   if(theWidget) {
268     emit beforeWidgetActivated(theWidget);
269     theWidget->setHighlighted(true);
270     theWidget->activate();
271   }
272   myActiveWidget = theWidget;
273   return true;
274 }
275
276 void XGUI_PropertyPanel::setFocusOnOkButton()
277 {
278   QToolButton* anOkBtn = findChild<QToolButton*>(PROP_PANEL_OK);
279   anOkBtn->setFocus();
280 }
281
282 void XGUI_PropertyPanel::setCancelEnabled(bool theEnabled)
283 {
284   QToolButton* anCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
285   anCancelBtn->setEnabled(theEnabled);
286 }
287
288 bool XGUI_PropertyPanel::isCancelEnabled() const
289 {
290   QToolButton* anCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
291   return anCancelBtn->isEnabled();
292 }
293
294 void XGUI_PropertyPanel::setEditingMode(bool isEditing)
295 {
296   ModuleBase_IPropertyPanel::setEditingMode(isEditing);
297   foreach(ModuleBase_ModelWidget* aWgt, myWidgets) {
298     aWgt->setEditingMode(isEditing);
299   }
300 }
301
302 void XGUI_PropertyPanel::setupActions(XGUI_ActionsMgr* theMgr)
303 {
304   QStringList aButtonNames;
305   aButtonNames << PROP_PANEL_OK << PROP_PANEL_CANCEL << PROP_PANEL_HELP;
306   QList<XGUI_ActionsMgr::OperationStateActionId> aActionIds;
307   aActionIds << XGUI_ActionsMgr::Accept << XGUI_ActionsMgr::Abort << XGUI_ActionsMgr::Help;
308   for (int i = 0; i < aButtonNames.size(); ++i) {
309     QToolButton* aBtn = findChild<QToolButton*>(aButtonNames.at(i));
310     QAction* anAct = theMgr->operationStateAction(aActionIds.at(i));
311     aBtn->setDefaultAction(anAct);
312   }
313 }
314
315 ModuleBase_ModelWidget* XGUI_PropertyPanel::preselectionWidget() const
316 {
317   return myPreselectionWidget;
318 }
319
320 void XGUI_PropertyPanel::setPreselectionWidget(ModuleBase_ModelWidget* theWidget)
321 {
322   myPreselectionWidget = theWidget;
323 }
324
325
326 void XGUI_PropertyPanel::closeEvent(QCloseEvent* theEvent)
327 {
328   ModuleBase_Operation* aOp = myOperationMgr->currentOperation();
329   if (aOp) {
330     if (myOperationMgr->canStopOperation(aOp)) {
331       myOperationMgr->abortAllOperations();
332       theEvent->accept();
333     } else 
334       theEvent->ignore();
335   } else
336     ModuleBase_IPropertyPanel::closeEvent(theEvent);
337 }