Salome HOME
a189af9bd281424e3c596cba24040fd46f2cf035
[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     connect(aWidget, SIGNAL(enterClicked()),
125             this,    SIGNAL(enterClicked()));
126
127   }
128 }
129
130 const QList<ModuleBase_ModelWidget*>& XGUI_PropertyPanel::modelWidgets() const
131 {
132   return myWidgets;
133 }
134
135 ModuleBase_PageBase* XGUI_PropertyPanel::contentWidget()
136 {
137   return static_cast<ModuleBase_PageBase*>(myPanelPage);
138 }
139
140 void XGUI_PropertyPanel::updateContentWidget(FeaturePtr theFeature)
141 {
142   // Invalid feature case on abort of the operation
143   if (theFeature.get() == NULL)
144     return;
145   if (theFeature->isAction() || !theFeature->data())
146     return;
147   foreach(ModuleBase_ModelWidget* eachWidget, myWidgets) {
148     if (!eachWidget->feature().get())
149       eachWidget->setFeature(theFeature);
150     eachWidget->restoreValue();
151   }
152   // the repaint is used here to immediately react in GUI to the values change.
153   repaint();
154 }
155
156 void XGUI_PropertyPanel::activateNextWidget(ModuleBase_ModelWidget* theWidget)
157 {
158   // TO CHECK: Editing operation does not have automatical activation of widgets
159   if (isEditingMode()) {
160     activateWidget(NULL);
161     return;
162   }
163   ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
164
165   QList<ModuleBase_ModelWidget*>::const_iterator anIt = myWidgets.begin(), aLast = myWidgets.end();
166   bool isFoundWidget = false;
167   ModuleBase_Tools::activateWindow(this, "XGUI_PropertyPanel::activateNextWidget()");
168   for (; anIt != aLast; anIt++) {
169     ModuleBase_ModelWidget* aCurrentWidget = *anIt;
170     if (isFoundWidget || !theWidget) {
171
172       if (!aValidators->isCase(aCurrentWidget->feature(), aCurrentWidget->attributeID()))
173         continue; // this attribute is not participated in the current case
174       if (!aCurrentWidget->isObligatory())
175         continue; // not obligatory widgets are not activated automatically
176
177       if (aCurrentWidget->focusTo()) {
178         return;
179       }
180     }
181     isFoundWidget = isFoundWidget || (*anIt) == theWidget;
182   }
183   activateWidget(NULL);
184 }
185 //#define DEBUG_TAB
186 #ifdef DEBUG_TAB
187 void findDirectChildren(QWidget* theParent, QList<QWidget*>& theWidgets, const bool theDebug)
188 {
189   QList<QWidget*> aWidgets;
190
191   if (theParent) {
192     QLayout* aLayout = theParent->layout();
193     if (aLayout) {
194       for (int i = 0, aCount = aLayout->count(); i < aCount; i++) {
195         QLayoutItem* anItem = aLayout->itemAt(i);
196         QWidget* aWidget = anItem ? anItem->widget() : 0;
197         if (aWidget && aWidget->isVisible()) {
198           if (aWidget->focusPolicy() != Qt::NoFocus)
199             theWidgets.append(aWidget);
200           findDirectChildren(aWidget, theWidgets, false);
201         }
202         else {
203           if (aWidget) {
204             QString anInfo = QString("widget [%1] is visible = %2, nofocus policy = %3").arg(aWidget->objectName()).
205               arg(aWidget->isVisible()).arg(aWidget->focusPolicy() == Qt::NoFocus);
206             qDebug(anInfo.toStdString().c_str());
207           }
208         }
209       }
210     }
211   }
212   if (theDebug) {
213     QStringList aWidgetTypes;
214     QList<QWidget*>::const_iterator anIt =  theWidgets.begin(), aLast = theWidgets.end();
215     for (; anIt != aLast; anIt++) {
216       QWidget* aWidget = *anIt;
217       if (aWidget)
218         aWidgetTypes.append(aWidget->objectName());
219     }
220     QString anInfo = QString("theWidgets[%1]: %2").arg(theWidgets.count()).arg(aWidgetTypes.join(","));
221     qDebug(anInfo.toStdString().c_str());
222   }
223 }
224
225 bool XGUI_PropertyPanel::focusNextPrevChild(bool theIsNext)
226 {
227   // it wraps the Tabs clicking to follow in the chain:
228   // controls, last control, Apply, Cancel, first control, controls
229   bool isChangedFocus = false;
230
231   QWidget* aFocusWidget = focusWidget();
232   if (aFocusWidget) {
233     QString anInfo = QString("focus Widget: %1").arg(aFocusWidget->objectName());
234     qDebug(anInfo.toStdString().c_str());
235   }
236
237   QWidget* aNewFocusWidget = 0;
238   if (theIsNext) {
239     if (aFocusWidget) {
240       QList<QWidget*> aChildren;
241       findDirectChildren(this, aChildren, true);
242
243       int aChildrenCount = aChildren.count();
244       int aFocusWidgetIndex = aChildren.indexOf(aFocusWidget);
245       if (aFocusWidgetIndex >= 0) {
246         if (aFocusWidgetIndex == aChildrenCount-1) {
247           QToolButton* anOkBtn = findChild<QToolButton*>(PROP_PANEL_OK);
248           aNewFocusWidget = anOkBtn;
249         }
250         else {
251           aNewFocusWidget = aChildren[aFocusWidgetIndex+1];
252         }
253       }
254     }
255   }
256   else {
257   }
258   if (aNewFocusWidget) {
259     ModuleBase_Tools::setFocus(aNewFocusWidget, "XGUI_PropertyPanel::focusNextPrevChild()");
260     isChangedFocus = true;
261   }
262   return isChangedFocus;
263 }
264 #else
265 bool XGUI_PropertyPanel::focusNextPrevChild(bool theIsNext)
266 {
267   // it wraps the Tabs clicking to follow in the chain:
268   // controls, last control, Apply, Cancel, first control, controls
269   bool isChangedFocus = false;
270
271   if (theIsNext) { // forward by Tab
272     QToolButton* aCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
273     if (aCancelBtn->hasFocus()) {
274       // after cancel, the first control should be focused
275       QWidget* aFirstControl = 0;
276       for (int i = 0, aSize = myWidgets.size(); i < aSize && !aFirstControl; i++)
277         aFirstControl = myWidgets[i]->getControlAcceptingFocus(true);
278       if (aFirstControl)
279         ModuleBase_Tools::setFocus(aFirstControl, "XGUI_PropertyPanel::focusNextPrevChild()");
280         isChangedFocus = true;
281     }
282     else {
283       // after the last control, the Apply button should be focused
284       QWidget* aLastControl = 0;
285       for (int i = myWidgets.size()-1; i >= 0 && !aLastControl; i--)
286         aLastControl = myWidgets[i]->getControlAcceptingFocus(false);
287       if (aLastControl && aLastControl->hasFocus()) {
288         setFocusOnOkButton();
289         isChangedFocus = true;
290       }
291     }
292   }
293   else { // backward by SHIFT + Tab
294     QToolButton* anOkBtn = findChild<QToolButton*>(PROP_PANEL_OK);
295     if (anOkBtn->hasFocus()) {
296       // after Apply, the last control should be focused
297       QWidget* aLastControl = 0;
298       for (int i = myWidgets.size()-1; i >= 0 && !aLastControl; i--)
299         aLastControl = myWidgets[i]->getControlAcceptingFocus(false);
300       if (aLastControl)
301         ModuleBase_Tools::setFocus(aLastControl, "XGUI_PropertyPanel::focusNextPrevChild()");
302         isChangedFocus = true;
303     }
304     else {
305       // after the first control, the Cancel button should be focused
306       QWidget* aFirstControl = 0;
307       for (int i = 0, aSize = myWidgets.size(); i < aSize && !aFirstControl; i++)
308         aFirstControl = myWidgets[i]->getControlAcceptingFocus(true);
309       if (aFirstControl && aFirstControl->hasFocus()) {
310         QToolButton* aCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
311         ModuleBase_Tools::setFocus(aCancelBtn, "XGUI_PropertyPanel::focusNextPrevChild()");
312         isChangedFocus = true;
313       }
314     }
315   }
316
317   if (!isChangedFocus)
318     isChangedFocus = ModuleBase_IPropertyPanel::focusNextPrevChild(theIsNext);
319   return isChangedFocus;
320 }
321
322 #endif
323 void XGUI_PropertyPanel::activateNextWidget()
324 {
325   activateNextWidget(myActiveWidget);
326 }
327
328 void XGUI_PropertyPanel::activateWidget(ModuleBase_ModelWidget* theWidget)
329 {
330   std::string aPreviosAttributeID;
331   if(myActiveWidget)
332     aPreviosAttributeID = myActiveWidget->attributeID();
333
334   // Avoid activation of already actve widget. It could happen on focusIn event many times
335   if (setActiveWidget(theWidget)) {
336     emit widgetActivated(myActiveWidget);
337     if (!myActiveWidget && !isEditingMode()) {
338       emit noMoreWidgets(aPreviosAttributeID);
339     }
340   }
341 }
342
343 bool XGUI_PropertyPanel::setActiveWidget(ModuleBase_ModelWidget* theWidget)
344 {
345   // Avoid activation of already actve widget. It could happen on focusIn event many times
346   if (theWidget == myActiveWidget) {
347     return false;
348   }
349   std::string aPreviosAttributeID;
350   if(myActiveWidget) {
351     aPreviosAttributeID = myActiveWidget->attributeID();
352     myActiveWidget->deactivate();
353     myActiveWidget->setHighlighted(false);
354   }
355   if(theWidget) {
356     emit beforeWidgetActivated(theWidget);
357     theWidget->setHighlighted(true);
358     theWidget->activate();
359   }
360   myActiveWidget = theWidget;
361   return true;
362 }
363
364 void XGUI_PropertyPanel::setFocusOnOkButton()
365 {
366   QToolButton* anOkBtn = findChild<QToolButton*>(PROP_PANEL_OK);
367   ModuleBase_Tools::setFocus(anOkBtn, "XGUI_PropertyPanel::setFocusOnOkButton()");
368 }
369
370 void XGUI_PropertyPanel::setCancelEnabled(bool theEnabled)
371 {
372   QToolButton* anCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
373   anCancelBtn->setEnabled(theEnabled);
374 }
375
376 bool XGUI_PropertyPanel::isCancelEnabled() const
377 {
378   QToolButton* anCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
379   return anCancelBtn->isEnabled();
380 }
381
382 void XGUI_PropertyPanel::setEditingMode(bool isEditing)
383 {
384   ModuleBase_IPropertyPanel::setEditingMode(isEditing);
385   foreach(ModuleBase_ModelWidget* aWgt, myWidgets) {
386     aWgt->setEditingMode(isEditing);
387   }
388 }
389
390 void XGUI_PropertyPanel::setupActions(XGUI_ActionsMgr* theMgr)
391 {
392   QStringList aButtonNames;
393   aButtonNames << PROP_PANEL_OK << PROP_PANEL_CANCEL << PROP_PANEL_HELP;
394   QList<XGUI_ActionsMgr::OperationStateActionId> aActionIds;
395   aActionIds << XGUI_ActionsMgr::Accept << XGUI_ActionsMgr::Abort << XGUI_ActionsMgr::Help;
396   for (int i = 0; i < aButtonNames.size(); ++i) {
397     QToolButton* aBtn = findChild<QToolButton*>(aButtonNames.at(i));
398     QAction* anAct = theMgr->operationStateAction(aActionIds.at(i));
399     aBtn->setDefaultAction(anAct);
400   }
401 }
402
403 ModuleBase_ModelWidget* XGUI_PropertyPanel::preselectionWidget() const
404 {
405   return myPreselectionWidget;
406 }
407
408 void XGUI_PropertyPanel::setPreselectionWidget(ModuleBase_ModelWidget* theWidget)
409 {
410   myPreselectionWidget = theWidget;
411 }
412
413
414 void XGUI_PropertyPanel::closeEvent(QCloseEvent* theEvent)
415 {
416   ModuleBase_Operation* aOp = myOperationMgr->currentOperation();
417   if (aOp) {
418     if (myOperationMgr->abortAllOperations()) {
419       theEvent->accept();
420     } else 
421       theEvent->ignore();
422   } else
423     ModuleBase_IPropertyPanel::closeEvent(theEvent);
424 }