]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_PropertyPanel.cpp
Salome HOME
Issue #986 activate field after apply constarint on one object
[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   }
100
101   myWidgets.clear();
102   myPanelPage->clearPage();
103   myActiveWidget = NULL;
104   setWindowTitle(tr("Property Panel"));
105 }
106
107 void XGUI_PropertyPanel::setModelWidgets(const QList<ModuleBase_ModelWidget*>& theWidgets)
108 {
109   myWidgets = theWidgets;
110   if (theWidgets.empty()) return;
111   foreach (ModuleBase_ModelWidget* aWidget, theWidgets) {
112     connect(aWidget, SIGNAL(focusInWidget(ModuleBase_ModelWidget*)),
113             this,    SLOT(activateWidget(ModuleBase_ModelWidget*)));
114     connect(aWidget, SIGNAL(focusOutWidget(ModuleBase_ModelWidget*)),
115             this,    SLOT(activateNextWidget(ModuleBase_ModelWidget*)));
116     connect(aWidget, SIGNAL(keyReleased(QKeyEvent*)),
117             this,    SIGNAL(keyReleased(QKeyEvent*)));
118   }
119   ModuleBase_ModelWidget* aLastWidget = theWidgets.last();
120   if (aLastWidget) {
121     QList<QWidget*> aControls = aLastWidget->getControls();
122     if (!aControls.empty()) {
123       QWidget* aLastControl = aControls.last();
124
125       QToolButton* anOkBtn = findChild<QToolButton*>(PROP_PANEL_OK);
126       QToolButton* aCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
127
128       setTabOrder(aLastControl, anOkBtn);
129       setTabOrder(anOkBtn, aCancelBtn);
130     }
131   }
132 }
133
134 const QList<ModuleBase_ModelWidget*>& XGUI_PropertyPanel::modelWidgets() const
135 {
136   return myWidgets;
137 }
138
139 ModuleBase_PageBase* XGUI_PropertyPanel::contentWidget()
140 {
141
142   return static_cast<ModuleBase_PageBase*>(myPanelPage);
143 }
144
145 void XGUI_PropertyPanel::updateContentWidget(FeaturePtr theFeature)
146 {
147   // Invalid feature case on abort of the operation
148   if (theFeature.get() == NULL)
149     return;
150   if (theFeature->isAction() || !theFeature->data())
151     return;
152   foreach(ModuleBase_ModelWidget* eachWidget, myWidgets) {
153     if (!eachWidget->feature().get())
154       eachWidget->setFeature(theFeature);
155     eachWidget->restoreValue();
156   }
157   // the repaint is used here to immediately react in GUI to the values change.
158   repaint();
159 }
160
161 void XGUI_PropertyPanel::activateNextWidget(ModuleBase_ModelWidget* theWidget)
162 {
163   // TO CHECK: Editing operation does not have automatical activation of widgets
164   if (isEditingMode()) {
165     activateWidget(NULL);
166     return;
167   }
168   ModuleBase_ModelWidget* aNextWidget = 0;
169   QList<ModuleBase_ModelWidget*>::const_iterator anIt = myWidgets.begin(), aLast = myWidgets.end();
170   bool isFoundWidget = false;
171   activateWindow();
172   for (; anIt != aLast && !aNextWidget; anIt++) {
173     if (isFoundWidget || !theWidget) {
174       if ((*anIt)->focusTo()) {
175         aNextWidget = *anIt;
176       }
177     }
178     isFoundWidget = (*anIt) == theWidget;
179   }
180   // Normaly focusTo is enough to activate widget
181   // here is a special case on mouse click in the viewer
182   if(aNextWidget == NULL) {
183     activateWidget(aNextWidget);
184   }
185 }
186
187 void XGUI_PropertyPanel::activateNextWidget()
188 {
189   activateNextWidget(myActiveWidget);
190 }
191
192 void XGUI_PropertyPanel::activateWidget(ModuleBase_ModelWidget* theWidget)
193 {
194   // Avoid activation of already actve widget. It could happen on focusIn event many times
195   if (theWidget == myActiveWidget) {
196     return;
197   }
198   if(myActiveWidget) {
199     myActiveWidget->deactivate();
200     myActiveWidget->setHighlighted(false);
201   }
202   if(theWidget) {
203     emit beforeWidgetActivated(theWidget);
204     theWidget->setHighlighted(true);
205     theWidget->activate();
206   }
207   myActiveWidget = theWidget;
208   if (myActiveWidget) {
209     emit widgetActivated(theWidget);
210   } else if (!isEditingMode()) {
211     emit noMoreWidgets();
212   }
213 }
214
215 void XGUI_PropertyPanel::setFocusOnOkButton()
216 {
217   QToolButton* anOkBtn = findChild<QToolButton*>(PROP_PANEL_OK);
218   anOkBtn->setFocus();
219 }
220
221 void XGUI_PropertyPanel::setCancelEnabled(bool theEnabled)
222 {
223   QToolButton* anCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
224   anCancelBtn->setEnabled(theEnabled);
225 }
226
227 bool XGUI_PropertyPanel::isCancelEnabled() const
228 {
229   QToolButton* anCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
230   return anCancelBtn->isEnabled();
231 }
232
233 void XGUI_PropertyPanel::setEditingMode(bool isEditing)
234 {
235   ModuleBase_IPropertyPanel::setEditingMode(isEditing);
236   foreach(ModuleBase_ModelWidget* aWgt, myWidgets) {
237     aWgt->setEditingMode(isEditing);
238   }
239 }
240
241 void XGUI_PropertyPanel::setupActions(XGUI_ActionsMgr* theMgr)
242 {
243   QStringList aButtonNames;
244   aButtonNames << PROP_PANEL_OK << PROP_PANEL_CANCEL << PROP_PANEL_HELP;
245   QList<XGUI_ActionsMgr::OperationStateActionId> aActionIds;
246   aActionIds << XGUI_ActionsMgr::Accept << XGUI_ActionsMgr::Abort << XGUI_ActionsMgr::Help;
247   for (int i = 0; i < aButtonNames.size(); ++i) {
248     QToolButton* aBtn = findChild<QToolButton*>(aButtonNames.at(i));
249     QAction* anAct = theMgr->operationStateAction(aActionIds.at(i));
250     aBtn->setDefaultAction(anAct);
251   }
252 }
253
254 ModuleBase_ModelWidget* XGUI_PropertyPanel::preselectionWidget() const
255 {
256   return myPreselectionWidget;
257 }
258
259 void XGUI_PropertyPanel::setPreselectionWidget(ModuleBase_ModelWidget* theWidget)
260 {
261   myPreselectionWidget = theWidget;
262 }