Salome HOME
d1eea76877338175fb693231fc0153611709a193
[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   QWidget* aLastControl = 0;
124   QList<QWidget*> aControls;
125   for (int i = myWidgets.size()-1; i >= 0 && !aLastControl; i--)  {
126     aControls = myWidgets[i]->getControls();
127     for (int j = aControls.size()-1; j >= 0 && !aLastControl; j--)  {
128       if (aControls[j]->focusPolicy() != Qt::NoFocus)
129         aLastControl = aControls[j];
130     }
131   }
132   if (aLastControl) {
133     QToolButton* anOkBtn = findChild<QToolButton*>(PROP_PANEL_OK);
134     QToolButton* aCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
135
136     setTabOrder(aLastControl, anOkBtn);
137     setTabOrder(anOkBtn, aCancelBtn);
138   }
139 }
140
141 const QList<ModuleBase_ModelWidget*>& XGUI_PropertyPanel::modelWidgets() const
142 {
143   return myWidgets;
144 }
145
146 ModuleBase_PageBase* XGUI_PropertyPanel::contentWidget()
147 {
148
149   return static_cast<ModuleBase_PageBase*>(myPanelPage);
150 }
151
152 void XGUI_PropertyPanel::updateContentWidget(FeaturePtr theFeature)
153 {
154   // Invalid feature case on abort of the operation
155   if (theFeature.get() == NULL)
156     return;
157   if (theFeature->isAction() || !theFeature->data())
158     return;
159   foreach(ModuleBase_ModelWidget* eachWidget, myWidgets) {
160     if (!eachWidget->feature().get())
161       eachWidget->setFeature(theFeature);
162     eachWidget->restoreValue();
163   }
164   // the repaint is used here to immediately react in GUI to the values change.
165   repaint();
166 }
167
168 void XGUI_PropertyPanel::activateNextWidget(ModuleBase_ModelWidget* theWidget)
169 {
170   // TO CHECK: Editing operation does not have automatical activation of widgets
171   if (isEditingMode()) {
172     activateWidget(NULL);
173     return;
174   }
175   QList<ModuleBase_ModelWidget*>::const_iterator anIt = myWidgets.begin(), aLast = myWidgets.end();
176   bool isFoundWidget = false;
177   activateWindow();
178   for (; anIt != aLast; anIt++) {
179     if (isFoundWidget || !theWidget) {
180       if ((*anIt)->focusTo()) {
181         return;
182       }
183     }
184     isFoundWidget = isFoundWidget || (*anIt) == theWidget;
185   }
186   activateWidget(NULL);
187 }
188
189 void XGUI_PropertyPanel::activateNextWidget()
190 {
191   activateNextWidget(myActiveWidget);
192 }
193
194 void XGUI_PropertyPanel::activateWidget(ModuleBase_ModelWidget* theWidget)
195 {
196   // Avoid activation of already actve widget. It could happen on focusIn event many times
197   if (theWidget == myActiveWidget) {
198     return;
199   }
200   std::string aPreviosAttributeID;
201   if(myActiveWidget) {
202     aPreviosAttributeID = myActiveWidget->attributeID();
203     myActiveWidget->deactivate();
204     myActiveWidget->setHighlighted(false);
205   }
206   if(theWidget) {
207     emit beforeWidgetActivated(theWidget);
208     theWidget->setHighlighted(true);
209     theWidget->activate();
210   }
211   myActiveWidget = theWidget;
212   if (myActiveWidget) {
213     emit widgetActivated(theWidget);
214   } else if (!isEditingMode()) {
215     emit noMoreWidgets(aPreviosAttributeID);
216     //setFocusOnOkButton();
217   }
218 }
219
220 void XGUI_PropertyPanel::setFocusOnOkButton()
221 {
222   QToolButton* anOkBtn = findChild<QToolButton*>(PROP_PANEL_OK);
223   anOkBtn->setFocus();
224 }
225
226 void XGUI_PropertyPanel::setCancelEnabled(bool theEnabled)
227 {
228   QToolButton* anCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
229   anCancelBtn->setEnabled(theEnabled);
230 }
231
232 bool XGUI_PropertyPanel::isCancelEnabled() const
233 {
234   QToolButton* anCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
235   return anCancelBtn->isEnabled();
236 }
237
238 void XGUI_PropertyPanel::setEditingMode(bool isEditing)
239 {
240   ModuleBase_IPropertyPanel::setEditingMode(isEditing);
241   foreach(ModuleBase_ModelWidget* aWgt, myWidgets) {
242     aWgt->setEditingMode(isEditing);
243   }
244 }
245
246 void XGUI_PropertyPanel::setupActions(XGUI_ActionsMgr* theMgr)
247 {
248   QStringList aButtonNames;
249   aButtonNames << PROP_PANEL_OK << PROP_PANEL_CANCEL << PROP_PANEL_HELP;
250   QList<XGUI_ActionsMgr::OperationStateActionId> aActionIds;
251   aActionIds << XGUI_ActionsMgr::Accept << XGUI_ActionsMgr::Abort << XGUI_ActionsMgr::Help;
252   for (int i = 0; i < aButtonNames.size(); ++i) {
253     QToolButton* aBtn = findChild<QToolButton*>(aButtonNames.at(i));
254     QAction* anAct = theMgr->operationStateAction(aActionIds.at(i));
255     aBtn->setDefaultAction(anAct);
256   }
257 }
258
259 ModuleBase_ModelWidget* XGUI_PropertyPanel::preselectionWidget() const
260 {
261   return myPreselectionWidget;
262 }
263
264 void XGUI_PropertyPanel::setPreselectionWidget(ModuleBase_ModelWidget* theWidget)
265 {
266   myPreselectionWidget = theWidget;
267 }
268
269
270 void XGUI_PropertyPanel::closeEvent(QCloseEvent* theEvent)
271 {
272   ModuleBase_Operation* aOp = myOperationMgr->currentOperation();
273   if (aOp) {
274     if (myOperationMgr->canStopOperation(aOp)) {
275       myOperationMgr->abortAllOperations();
276       theEvent->accept();
277     } else 
278       theEvent->ignore();
279   } else
280     ModuleBase_IPropertyPanel::closeEvent(theEvent);
281 }