]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_PropertyPanel.cpp
Salome HOME
Merge branch 'Dev_0.7.1' of newgeom:newgeom.git into Dev_0.7.1
[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 <AppElements_Constants.h>
12 #include <ModuleBase_WidgetMultiSelector.h>
13
14 #include <QWidget>
15 #include <QVBoxLayout>
16 #include <QFrame>
17 #include <QPushButton>
18 #include <QIcon>
19 #include <QVBoxLayout>
20 #include <QEvent>
21 #include <QKeyEvent>
22 #include <QLayoutItem>
23
24 #ifdef _DEBUG
25 #include <iostream>
26 #endif
27
28 XGUI_PropertyPanel::XGUI_PropertyPanel(QWidget* theParent)
29     : ModuleBase_IPropertyPanel(theParent), 
30     myActiveWidget(NULL)
31 {
32   this->setWindowTitle(tr("Property Panel"));
33   QAction* aViewAct = this->toggleViewAction();
34   this->setObjectName(PROP_PANEL);
35   setStyleSheet("::title { position: relative; padding-left: 5px; text-align: left center }");
36
37   QWidget* aContent = new QWidget(this);
38   myMainLayout = new QVBoxLayout(aContent);
39   myMainLayout->setContentsMargins(3, 3, 3, 3);
40   this->setWidget(aContent);
41
42   QFrame* aFrm = new QFrame(aContent);
43   aFrm->setFrameStyle(QFrame::Sunken);
44   aFrm->setFrameShape(QFrame::Panel);
45   QHBoxLayout* aBtnLay = new QHBoxLayout(aFrm);
46   aBtnLay->setContentsMargins(0, 0, 0, 0);
47   myMainLayout->addWidget(aFrm);
48
49   QPushButton* aBtn = new QPushButton(QIcon(":pictures/button_help.png"), "", aFrm);
50   aBtn->setFlat(true);
51   aBtnLay->addWidget(aBtn);
52   aBtnLay->addStretch(1);
53   aBtn = new QPushButton(QIcon(":pictures/button_ok.png"), "", aFrm);
54   aBtn->setObjectName(PROP_PANEL_OK);
55   aBtn->setToolTip(tr("Ok"));
56   aBtn->setFlat(true);
57   aBtnLay->addWidget(aBtn);
58
59   aBtn = new QPushButton(QIcon(":pictures/button_cancel.png"), "", aFrm);
60   aBtn->setToolTip(tr("Cancel"));
61   aBtn->setObjectName(PROP_PANEL_CANCEL);
62   aBtn->setFlat(true);
63   aBtn->setShortcut(QKeySequence(Qt::Key_Escape));
64   aBtnLay->addWidget(aBtn);
65
66   myCustomWidget = new QWidget(aContent);
67   myMainLayout->addWidget(myCustomWidget);
68   setStretchEnabled(true);
69 }
70
71 XGUI_PropertyPanel::~XGUI_PropertyPanel()
72 {
73 }
74
75 void XGUI_PropertyPanel::cleanContent()
76 {
77   if (myActiveWidget)
78     myActiveWidget->deactivate();
79   myWidgets.clear();
80   qDeleteAll(myCustomWidget->children());
81   myActiveWidget = NULL;
82   setWindowTitle(tr("Property Panel"));
83 }
84
85 void XGUI_PropertyPanel::setModelWidgets(const QList<ModuleBase_ModelWidget*>& theWidgets)
86 {
87   myWidgets = theWidgets;
88   if (theWidgets.empty()) return;
89   bool isEnableStretch = true;
90   QList<ModuleBase_ModelWidget*>::const_iterator anIt = theWidgets.begin(), aLast =
91       theWidgets.end();
92   for (; anIt != aLast; anIt++) {
93     connect(*anIt, SIGNAL(keyReleased(QKeyEvent*)), this, SIGNAL(keyReleased(QKeyEvent*)));
94     connect(*anIt, SIGNAL(focusOutWidget(ModuleBase_ModelWidget*)),
95             this,  SLOT(activateNextWidget(ModuleBase_ModelWidget*)));
96     connect(*anIt, SIGNAL(focusInWidget(ModuleBase_ModelWidget*)),
97             this,  SLOT(activateWidget(ModuleBase_ModelWidget*)));
98
99     //ModuleBase_WidgetPoint2D* aPointWidget = dynamic_cast<ModuleBase_WidgetPoint2D*>(*anIt);
100     //if (aPointWidget)
101     //  connect(aPointWidget, SIGNAL(storedPoint2D(ObjectPtr, const std::string&)), this,
102     //          SIGNAL(storedPoint2D(ObjectPtr, const std::string&)))
103     //}
104
105     if (!isEnableStretch) continue;
106     foreach(QWidget* eachWidget, (*anIt)->getControls()) {
107       QSizePolicy::Policy aVPolicy = eachWidget->sizePolicy().verticalPolicy();
108       if(aVPolicy == QSizePolicy::Expanding ||
109          aVPolicy == QSizePolicy::MinimumExpanding) {
110         isEnableStretch = false;
111       }
112     }
113   }
114   setStretchEnabled(isEnableStretch);
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       QPushButton* anOkBtn = findChild<QPushButton*>(PROP_PANEL_OK);
122       QPushButton* aCancelBtn = findChild<QPushButton*>(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 QWidget* XGUI_PropertyPanel::contentWidget()
136 {
137   return myCustomWidget;
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   {
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   ModuleBase_ModelWidget* aNextWidget = 0;
164   QList<ModuleBase_ModelWidget*>::const_iterator anIt = myWidgets.begin(), aLast = myWidgets.end();
165   bool isFoundWidget = false;
166   for (; anIt != aLast && !aNextWidget; anIt++) {
167     if (isFoundWidget || !theWidget) {
168       if ((*anIt)->focusTo()) {
169         aNextWidget = *anIt;
170       }
171     }
172     isFoundWidget = (*anIt) == theWidget;
173   }
174   // Normaly focusTo is enough to activate widget
175   // here is a special case on mouse click in the viewer
176   //if(aNextWidget == NULL) {
177     activateWidget(aNextWidget);
178   //}
179 }
180
181 void XGUI_PropertyPanel::setStretchEnabled(bool isEnabled)
182 {
183   if (myMainLayout->count() == 0)
184     return;
185   int aStretchIdx = myMainLayout->count() - 1;
186   bool hasStretch = myMainLayout->itemAt(aStretchIdx)->spacerItem() != NULL;
187   QLayoutItem* aChild;
188   if (isEnabled) {
189     if (!hasStretch) myMainLayout->addStretch(1);
190   } else if (hasStretch) {
191     aChild = myMainLayout->takeAt(aStretchIdx);
192     delete aChild;
193   }
194 }
195
196 void XGUI_PropertyPanel::activateNextWidget()
197 {
198   activateNextWidget(myActiveWidget);
199 }
200
201 void XGUI_PropertyPanel::setAcceptEnabled(bool isEnabled)
202 {
203   QPushButton* anOkBtn = findChild<QPushButton*>(PROP_PANEL_OK);
204   anOkBtn->setEnabled(isEnabled);
205 }
206
207 void XGUI_PropertyPanel::activateWidget(ModuleBase_ModelWidget* theWidget)
208 {
209   // Avoid activation of already actve widget. It could happen on focusIn event many times
210   if (theWidget == myActiveWidget)
211     return;
212   if(myActiveWidget) {
213     myActiveWidget->deactivate();
214     myActiveWidget->setHighlighted(false);
215   }
216   if(theWidget) {
217     if (theWidget)
218       emit beforeWidgetActivated(theWidget);
219     theWidget->activate();
220     theWidget->setHighlighted(true);
221   }
222   myActiveWidget = theWidget;
223   if (myActiveWidget)
224     emit widgetActivated(theWidget);
225   else if (!isEditingMode())
226     emit noMoreWidgets();
227 }
228
229 void XGUI_PropertyPanel::setCancelEnabled(bool theEnabled)
230 {
231   QPushButton* anCancelBtn = findChild<QPushButton*>(PROP_PANEL_CANCEL);
232   anCancelBtn->setEnabled(theEnabled);
233 }
234
235 bool XGUI_PropertyPanel::isCancelEnabled() const
236 {
237   QPushButton* anCancelBtn = findChild<QPushButton*>(PROP_PANEL_CANCEL);
238   return anCancelBtn->isEnabled();
239 }
240
241 void XGUI_PropertyPanel::setEditingMode(bool isEditing)
242 {
243   ModuleBase_IPropertyPanel::setEditingMode(isEditing);
244   foreach(ModuleBase_ModelWidget* aWgt, myWidgets) {
245     aWgt->setEditingMode(isEditing);
246   }
247 }