Salome HOME
Simplification of the code: it removes focusNextPrevChild processing.
[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   }
122 }
123
124 const QList<ModuleBase_ModelWidget*>& XGUI_PropertyPanel::modelWidgets() const
125 {
126   return myWidgets;
127 }
128
129 ModuleBase_PageBase* XGUI_PropertyPanel::contentWidget()
130 {
131   return static_cast<ModuleBase_PageBase*>(myPanelPage);
132 }
133
134 void XGUI_PropertyPanel::updateContentWidget(FeaturePtr theFeature)
135 {
136   // Invalid feature case on abort of the operation
137   if (theFeature.get() == NULL)
138     return;
139   if (theFeature->isAction() || !theFeature->data())
140     return;
141   foreach(ModuleBase_ModelWidget* eachWidget, myWidgets) {
142     if (!eachWidget->feature().get())
143       eachWidget->setFeature(theFeature);
144     eachWidget->restoreValue();
145   }
146   // the repaint is used here to immediately react in GUI to the values change.
147   repaint();
148 }
149
150 void XGUI_PropertyPanel::activateNextWidget(ModuleBase_ModelWidget* theWidget)
151 {
152   // TO CHECK: Editing operation does not have automatical activation of widgets
153   if (isEditingMode()) {
154     activateWidget(NULL);
155     return;
156   }
157   QList<ModuleBase_ModelWidget*>::const_iterator anIt = myWidgets.begin(), aLast = myWidgets.end();
158   bool isFoundWidget = false;
159   activateWindow();
160   for (; anIt != aLast; anIt++) {
161     if (isFoundWidget || !theWidget) {
162       if ((*anIt)->focusTo()) {
163         return;
164       }
165     }
166     isFoundWidget = isFoundWidget || (*anIt) == theWidget;
167   }
168   activateWidget(NULL);
169 }
170
171 bool XGUI_PropertyPanel::focusNextPrevChild(bool theIsNext)
172 {
173   // it wraps the Tabs clicking to follow in the chain:
174   // controls, last control, Apply, Cancel, first control, controls
175
176   bool isChangedFocus = false;
177   if (theIsNext) { // forward by Tab
178     QToolButton* aCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
179     if (aCancelBtn->hasFocus()) {
180       // after cancel, the first control should be focused
181       QWidget* aFirstControl = 0;
182       for (int i = 0, aSize = myWidgets.size(); i < aSize && !aFirstControl; i++)
183         aFirstControl = myWidgets[i]->getControlAcceptingFocus(true);
184       if (aFirstControl)
185         aFirstControl->setFocus();
186         isChangedFocus = true;
187     }
188     else {
189       // after the last control, the Apply button should be focused
190       QWidget* aLastControl = 0;
191       for (int i = myWidgets.size()-1; i >= 0 && !aLastControl; i--)
192         aLastControl = myWidgets[i]->getControlAcceptingFocus(false);
193       if (aLastControl && aLastControl->hasFocus()) {
194         setFocusOnOkButton();
195         isChangedFocus = true;
196       }
197     }
198   }
199   else { // backward by SHIFT + Tab
200     QToolButton* anOkBtn = findChild<QToolButton*>(PROP_PANEL_OK);
201     if (anOkBtn->hasFocus()) {
202       // after Apply, the last control should be focused
203       QWidget* aLastControl = 0;
204       for (int i = myWidgets.size()-1; i >= 0 && !aLastControl; i--)
205         aLastControl = myWidgets[i]->getControlAcceptingFocus(false);
206       if (aLastControl)
207         aLastControl->setFocus();
208         isChangedFocus = true;
209     }
210     else {
211       // after the first control, the Cancel button should be focused
212       QWidget* aFirstControl = 0;
213       for (int i = 0, aSize = myWidgets.size(); i < aSize && !aFirstControl; i++)
214         aFirstControl = myWidgets[i]->getControlAcceptingFocus(true);
215       if (aFirstControl && aFirstControl->hasFocus()) {
216         QToolButton* aCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
217         aCancelBtn->setFocus();
218         isChangedFocus = true;
219       }
220     }
221   }
222
223   if (!isChangedFocus)
224     isChangedFocus = ModuleBase_IPropertyPanel::focusNextPrevChild(theIsNext);
225
226   return isChangedFocus;
227 }
228
229 void XGUI_PropertyPanel::activateNextWidget()
230 {
231   activateNextWidget(myActiveWidget);
232 }
233
234 void XGUI_PropertyPanel::activateWidget(ModuleBase_ModelWidget* theWidget)
235 {
236   // Avoid activation of already actve widget. It could happen on focusIn event many times
237   if (setActiveWidget(theWidget)) {
238     if (myActiveWidget) {
239       emit widgetActivated(myActiveWidget);
240     } else if (!isEditingMode()) {
241       emit noMoreWidgets();
242       //setFocusOnOkButton();
243     }
244   }
245 }
246
247 bool XGUI_PropertyPanel::setActiveWidget(ModuleBase_ModelWidget* theWidget)
248 {
249   // Avoid activation of already actve widget. It could happen on focusIn event many times
250   if (theWidget == myActiveWidget) {
251     return false;
252   }
253   if(myActiveWidget) {
254     myActiveWidget->deactivate();
255     myActiveWidget->setHighlighted(false);
256   }
257   if(theWidget) {
258     emit beforeWidgetActivated(theWidget);
259     theWidget->setHighlighted(true);
260     theWidget->activate();
261   }
262   myActiveWidget = theWidget;
263   return true;
264 }
265
266 void XGUI_PropertyPanel::setFocusOnOkButton()
267 {
268   QToolButton* anOkBtn = findChild<QToolButton*>(PROP_PANEL_OK);
269   anOkBtn->setFocus();
270 }
271
272 void XGUI_PropertyPanel::setCancelEnabled(bool theEnabled)
273 {
274   QToolButton* anCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
275   anCancelBtn->setEnabled(theEnabled);
276 }
277
278 bool XGUI_PropertyPanel::isCancelEnabled() const
279 {
280   QToolButton* anCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
281   return anCancelBtn->isEnabled();
282 }
283
284 void XGUI_PropertyPanel::setEditingMode(bool isEditing)
285 {
286   ModuleBase_IPropertyPanel::setEditingMode(isEditing);
287   foreach(ModuleBase_ModelWidget* aWgt, myWidgets) {
288     aWgt->setEditingMode(isEditing);
289   }
290 }
291
292 void XGUI_PropertyPanel::setupActions(XGUI_ActionsMgr* theMgr)
293 {
294   QStringList aButtonNames;
295   aButtonNames << PROP_PANEL_OK << PROP_PANEL_CANCEL << PROP_PANEL_HELP;
296   QList<XGUI_ActionsMgr::OperationStateActionId> aActionIds;
297   aActionIds << XGUI_ActionsMgr::Accept << XGUI_ActionsMgr::Abort << XGUI_ActionsMgr::Help;
298   for (int i = 0; i < aButtonNames.size(); ++i) {
299     QToolButton* aBtn = findChild<QToolButton*>(aButtonNames.at(i));
300     QAction* anAct = theMgr->operationStateAction(aActionIds.at(i));
301     aBtn->setDefaultAction(anAct);
302   }
303 }
304
305 ModuleBase_ModelWidget* XGUI_PropertyPanel::preselectionWidget() const
306 {
307   return myPreselectionWidget;
308 }
309
310 void XGUI_PropertyPanel::setPreselectionWidget(ModuleBase_ModelWidget* theWidget)
311 {
312   myPreselectionWidget = theWidget;
313 }
314
315
316 void XGUI_PropertyPanel::closeEvent(QCloseEvent* theEvent)
317 {
318   ModuleBase_Operation* aOp = myOperationMgr->currentOperation();
319   if (aOp) {
320     if (myOperationMgr->canStopOperation(aOp)) {
321       myOperationMgr->abortAllOperations();
322       theEvent->accept();
323     } else 
324       theEvent->ignore();
325   } else
326     ModuleBase_IPropertyPanel::closeEvent(theEvent);
327 }