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