Salome HOME
Issue #971: Update OB on duplicate part: send event on creation of a new document...
[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   std::string aPreviosAttributeID;
247   if(myActiveWidget)
248     aPreviosAttributeID = myActiveWidget->attributeID();
249
250   // Avoid activation of already actve widget. It could happen on focusIn event many times
251   if (setActiveWidget(theWidget)) {
252     emit widgetActivated(myActiveWidget);
253     if (!myActiveWidget && !isEditingMode()) {
254       emit noMoreWidgets(aPreviosAttributeID);
255     }
256   }
257 }
258
259 bool XGUI_PropertyPanel::setActiveWidget(ModuleBase_ModelWidget* theWidget)
260 {
261   // Avoid activation of already actve widget. It could happen on focusIn event many times
262   if (theWidget == myActiveWidget) {
263     return false;
264   }
265   std::string aPreviosAttributeID;
266   if(myActiveWidget) {
267     aPreviosAttributeID = myActiveWidget->attributeID();
268     myActiveWidget->deactivate();
269     myActiveWidget->setHighlighted(false);
270   }
271   if(theWidget) {
272     emit beforeWidgetActivated(theWidget);
273     theWidget->setHighlighted(true);
274     theWidget->activate();
275   }
276   myActiveWidget = theWidget;
277   return true;
278 }
279
280 void XGUI_PropertyPanel::setFocusOnOkButton()
281 {
282   QToolButton* anOkBtn = findChild<QToolButton*>(PROP_PANEL_OK);
283   anOkBtn->setFocus();
284 }
285
286 void XGUI_PropertyPanel::setCancelEnabled(bool theEnabled)
287 {
288   QToolButton* anCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
289   anCancelBtn->setEnabled(theEnabled);
290 }
291
292 bool XGUI_PropertyPanel::isCancelEnabled() const
293 {
294   QToolButton* anCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
295   return anCancelBtn->isEnabled();
296 }
297
298 void XGUI_PropertyPanel::setEditingMode(bool isEditing)
299 {
300   ModuleBase_IPropertyPanel::setEditingMode(isEditing);
301   foreach(ModuleBase_ModelWidget* aWgt, myWidgets) {
302     aWgt->setEditingMode(isEditing);
303   }
304 }
305
306 void XGUI_PropertyPanel::setupActions(XGUI_ActionsMgr* theMgr)
307 {
308   QStringList aButtonNames;
309   aButtonNames << PROP_PANEL_OK << PROP_PANEL_CANCEL << PROP_PANEL_HELP;
310   QList<XGUI_ActionsMgr::OperationStateActionId> aActionIds;
311   aActionIds << XGUI_ActionsMgr::Accept << XGUI_ActionsMgr::Abort << XGUI_ActionsMgr::Help;
312   for (int i = 0; i < aButtonNames.size(); ++i) {
313     QToolButton* aBtn = findChild<QToolButton*>(aButtonNames.at(i));
314     QAction* anAct = theMgr->operationStateAction(aActionIds.at(i));
315     aBtn->setDefaultAction(anAct);
316   }
317 }
318
319 ModuleBase_ModelWidget* XGUI_PropertyPanel::preselectionWidget() const
320 {
321   return myPreselectionWidget;
322 }
323
324 void XGUI_PropertyPanel::setPreselectionWidget(ModuleBase_ModelWidget* theWidget)
325 {
326   myPreselectionWidget = theWidget;
327 }
328
329
330 void XGUI_PropertyPanel::closeEvent(QCloseEvent* theEvent)
331 {
332   ModuleBase_Operation* aOp = myOperationMgr->currentOperation();
333   if (aOp) {
334     if (myOperationMgr->canStopOperation(aOp)) {
335       myOperationMgr->abortAllOperations();
336       theEvent->accept();
337     } else 
338       theEvent->ignore();
339   } else
340     ModuleBase_IPropertyPanel::closeEvent(theEvent);
341 }