]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_PropertyPanel.cpp
Salome HOME
Issue #924 Fatal error as result of ExtrusionCut call
[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 <AppElements_Constants.h>
13 #include <ModuleBase_WidgetMultiSelector.h>
14 #include <ModuleBase_Tools.h>
15 #include <ModuleBase_PageBase.h>
16 #include <ModuleBase_PageWidget.h>
17
18 #include <QEvent>
19 #include <QFrame>
20 #include <QIcon>
21 #include <QKeyEvent>
22 #include <QLayoutItem>
23 #include <QToolButton>
24 #include <QVBoxLayout>
25 #include <QGridLayout>
26 #include <QWidget>
27 #include <QToolButton>
28 #include <QAction>
29
30 #ifdef _DEBUG
31 #include <iostream>
32 #endif
33
34 XGUI_PropertyPanel::XGUI_PropertyPanel(QWidget* theParent)
35     : ModuleBase_IPropertyPanel(theParent), 
36     myActiveWidget(NULL),
37     myPreselectionWidget(NULL),
38     myPanelPage(NULL)
39 {
40   this->setWindowTitle(tr("Property Panel"));
41   QAction* aViewAct = this->toggleViewAction();
42   this->setObjectName(PROP_PANEL);
43   setStyleSheet("::title { position: relative; padding-left: 5px; text-align: left center }");
44
45   QWidget* aContent = new QWidget(this);
46   QGridLayout* aMainLayout = new QGridLayout(aContent);
47   const int kPanelColumn = 0;
48   int aPanelRow = 0;
49   aMainLayout->setContentsMargins(3, 3, 3, 3);
50   this->setWidget(aContent);
51
52   QFrame* aFrm = new QFrame(aContent);
53   aFrm->setFrameStyle(QFrame::Raised);
54   aFrm->setFrameShape(QFrame::Panel);
55   QHBoxLayout* aBtnLay = new QHBoxLayout(aFrm);
56   ModuleBase_Tools::zeroMargins(aBtnLay);
57   aMainLayout->addWidget(aFrm, aPanelRow++, kPanelColumn);
58
59   myHeaderWidget = aFrm;
60
61   QStringList aBtnNames;
62   aBtnNames << QString(PROP_PANEL_HELP)
63             << QString(PROP_PANEL_OK)
64             << QString(PROP_PANEL_CANCEL);
65   foreach(QString eachBtnName, aBtnNames) {
66     QToolButton* aBtn = new QToolButton(aFrm);
67     aBtn->setObjectName(eachBtnName);
68     aBtn->setAutoRaise(true);
69     aBtnLay->addWidget(aBtn);
70   }
71   aBtnLay->insertStretch(1, 1);
72
73   myPanelPage = new ModuleBase_PageWidget(aContent);
74   myPanelPage->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::MinimumExpanding);
75   aMainLayout->addWidget(myPanelPage, aPanelRow, kPanelColumn);
76 }
77
78 XGUI_PropertyPanel::~XGUI_PropertyPanel()
79 {
80 }
81
82 void XGUI_PropertyPanel::cleanContent()
83 {
84   if (myActiveWidget)
85     myActiveWidget->deactivate();
86
87   /// as the widgets are deleted later, it is important that the signals
88   /// of these widgets are not processed. An example of the error is issue 986.
89   /// In the given case, the property panel is firstly filled by new widgets
90   /// of restarted operation and after that the mouse release signal come from
91   /// the widget of the previous operation (Point2d widget about mouse is released
92   /// and focus is out of this widget)
93   QList<ModuleBase_ModelWidget*>::const_iterator anIt = myWidgets.begin(),
94                                                  aLast = myWidgets.end();
95   for (; anIt != aLast; anIt++) {
96     QWidget* aWidget = *anIt;
97     if (aWidget) {
98       aWidget->blockSignals(true);
99       aWidget->setParent(0);
100       aWidget->deleteLater();
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   ModuleBase_ModelWidget* aLastWidget = theWidgets.last();
123   if (aLastWidget) {
124     QList<QWidget*> aControls = aLastWidget->getControls();
125     if (!aControls.empty()) {
126       QWidget* aLastControl = aControls.last();
127
128       QToolButton* anOkBtn = findChild<QToolButton*>(PROP_PANEL_OK);
129       QToolButton* aCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
130
131       setTabOrder(aLastControl, anOkBtn);
132       setTabOrder(anOkBtn, aCancelBtn);
133     }
134   }
135 }
136
137 const QList<ModuleBase_ModelWidget*>& XGUI_PropertyPanel::modelWidgets() const
138 {
139   return myWidgets;
140 }
141
142 ModuleBase_PageBase* XGUI_PropertyPanel::contentWidget()
143 {
144
145   return static_cast<ModuleBase_PageBase*>(myPanelPage);
146 }
147
148 void XGUI_PropertyPanel::updateContentWidget(FeaturePtr theFeature)
149 {
150   // Invalid feature case on abort of the operation
151   if (theFeature.get() == NULL)
152     return;
153   if (theFeature->isAction() || !theFeature->data())
154     return;
155   foreach(ModuleBase_ModelWidget* eachWidget, myWidgets) {
156     if (!eachWidget->feature().get())
157       eachWidget->setFeature(theFeature);
158     eachWidget->restoreValue();
159   }
160   // the repaint is used here to immediately react in GUI to the values change.
161   repaint();
162 }
163
164 void XGUI_PropertyPanel::activateNextWidget(ModuleBase_ModelWidget* theWidget)
165 {
166   // TO CHECK: Editing operation does not have automatical activation of widgets
167   if (isEditingMode()) {
168     activateWidget(NULL);
169     return;
170   }
171   ModuleBase_ModelWidget* aNextWidget = 0;
172   QList<ModuleBase_ModelWidget*>::const_iterator anIt = myWidgets.begin(), aLast = myWidgets.end();
173   bool isFoundWidget = false;
174   activateWindow();
175   for (; anIt != aLast && !aNextWidget; anIt++) {
176     if (isFoundWidget || !theWidget) {
177       if ((*anIt)->focusTo()) {
178         aNextWidget = *anIt;
179       }
180     }
181     isFoundWidget = (*anIt) == theWidget;
182   }
183   // Normaly focusTo is enough to activate widget
184   // here is a special case on mouse click in the viewer
185   if(aNextWidget == NULL) {
186     activateWidget(aNextWidget);
187   }
188 }
189
190 void XGUI_PropertyPanel::activateNextWidget()
191 {
192   activateNextWidget(myActiveWidget);
193 }
194
195 void XGUI_PropertyPanel::activateWidget(ModuleBase_ModelWidget* theWidget)
196 {
197   // Avoid activation of already actve widget. It could happen on focusIn event many times
198   if (theWidget == myActiveWidget) {
199     return;
200   }
201   if(myActiveWidget) {
202     myActiveWidget->deactivate();
203     myActiveWidget->setHighlighted(false);
204   }
205   if(theWidget) {
206     emit beforeWidgetActivated(theWidget);
207     theWidget->setHighlighted(true);
208     theWidget->activate();
209   }
210   myActiveWidget = theWidget;
211   if (myActiveWidget) {
212     emit widgetActivated(theWidget);
213   } else if (!isEditingMode()) {
214     emit noMoreWidgets();
215   }
216 }
217
218 void XGUI_PropertyPanel::setFocusOnOkButton()
219 {
220   QToolButton* anOkBtn = findChild<QToolButton*>(PROP_PANEL_OK);
221   anOkBtn->setFocus();
222 }
223
224 void XGUI_PropertyPanel::setCancelEnabled(bool theEnabled)
225 {
226   QToolButton* anCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
227   anCancelBtn->setEnabled(theEnabled);
228 }
229
230 bool XGUI_PropertyPanel::isCancelEnabled() const
231 {
232   QToolButton* anCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
233   return anCancelBtn->isEnabled();
234 }
235
236 void XGUI_PropertyPanel::setEditingMode(bool isEditing)
237 {
238   ModuleBase_IPropertyPanel::setEditingMode(isEditing);
239   foreach(ModuleBase_ModelWidget* aWgt, myWidgets) {
240     aWgt->setEditingMode(isEditing);
241   }
242 }
243
244 void XGUI_PropertyPanel::setupActions(XGUI_ActionsMgr* theMgr)
245 {
246   QStringList aButtonNames;
247   aButtonNames << PROP_PANEL_OK << PROP_PANEL_CANCEL << PROP_PANEL_HELP;
248   QList<XGUI_ActionsMgr::OperationStateActionId> aActionIds;
249   aActionIds << XGUI_ActionsMgr::Accept << XGUI_ActionsMgr::Abort << XGUI_ActionsMgr::Help;
250   for (int i = 0; i < aButtonNames.size(); ++i) {
251     QToolButton* aBtn = findChild<QToolButton*>(aButtonNames.at(i));
252     QAction* anAct = theMgr->operationStateAction(aActionIds.at(i));
253     aBtn->setDefaultAction(anAct);
254   }
255 }
256
257 ModuleBase_ModelWidget* XGUI_PropertyPanel::preselectionWidget() const
258 {
259   return myPreselectionWidget;
260 }
261
262 void XGUI_PropertyPanel::setPreselectionWidget(ModuleBase_ModelWidget* theWidget)
263 {
264   myPreselectionWidget = theWidget;
265 }