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