Salome HOME
Icons size correction to be 16x16
[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->data())
144     return;
145   foreach(ModuleBase_ModelWidget* eachWidget, myWidgets)
146   {
147     eachWidget->setFeature(theFeature);
148     eachWidget->restoreValue();
149   }
150   // the repaint is used here to immediately react in GUI to the values change.
151   repaint();
152 }
153
154 void XGUI_PropertyPanel::activateNextWidget(ModuleBase_ModelWidget* theWidget)
155 {
156   // TO CHECK: Editing operation does not have automatical activation of widgets
157   if (isEditingMode()) {
158     activateWidget(NULL);
159     return;
160   }
161   ModuleBase_ModelWidget* aNextWidget = 0;
162   QList<ModuleBase_ModelWidget*>::const_iterator anIt = myWidgets.begin(), aLast = myWidgets.end();
163   bool isFoundWidget = false;
164   for (; anIt != aLast && !aNextWidget; anIt++) {
165     if (isFoundWidget || !theWidget) {
166       if ((*anIt)->focusTo()) {
167         aNextWidget = *anIt;
168       }
169     }
170     isFoundWidget = (*anIt) == theWidget;
171   }
172   // Normaly focusTo is enough to activate widget
173   // here is a special case on mouse click in the viewer
174   //if(aNextWidget == NULL) {
175     activateWidget(aNextWidget);
176   //}
177 }
178
179 void XGUI_PropertyPanel::setStretchEnabled(bool isEnabled)
180 {
181   if (myMainLayout->count() == 0)
182     return;
183   int aStretchIdx = myMainLayout->count() - 1;
184   bool hasStretch = myMainLayout->itemAt(aStretchIdx)->spacerItem() != NULL;
185   QLayoutItem* aChild;
186   if (isEnabled) {
187     if (!hasStretch) myMainLayout->addStretch(1);
188   } else if (hasStretch) {
189     aChild = myMainLayout->takeAt(aStretchIdx);
190     delete aChild;
191   }
192 }
193
194 void XGUI_PropertyPanel::activateNextWidget()
195 {
196   activateNextWidget(myActiveWidget);
197 }
198
199 void XGUI_PropertyPanel::setAcceptEnabled(bool isEnabled)
200 {
201   QPushButton* anOkBtn = findChild<QPushButton*>(PROP_PANEL_OK);
202   anOkBtn->setEnabled(isEnabled);
203 }
204
205 void XGUI_PropertyPanel::activateWidget(ModuleBase_ModelWidget* theWidget)
206 {
207   // Avoid activation of already actve widget. It could happen on focusIn event many times
208   if (theWidget == myActiveWidget)
209     return;
210   if(myActiveWidget) {
211     myActiveWidget->deactivate();
212     myActiveWidget->setHighlighted(false);
213   }
214   if(theWidget) {
215     theWidget->activate();
216     theWidget->setHighlighted(true);
217   }
218   myActiveWidget = theWidget;
219   if (myActiveWidget)
220     emit widgetActivated(theWidget);
221   else if (!isEditingMode())
222     emit noMoreWidgets();
223 }
224
225 void XGUI_PropertyPanel::setOkEnabled(bool theEnabled)
226 {
227   QPushButton* anOkBtn = findChild<QPushButton*>(PROP_PANEL_OK);
228   anOkBtn->setEnabled(theEnabled);
229 }
230
231 bool XGUI_PropertyPanel::isOkEnabled() const
232 {
233   QPushButton* anOkBtn = findChild<QPushButton*>(PROP_PANEL_OK);
234   return anOkBtn->isEnabled();
235 }
236
237 void XGUI_PropertyPanel::setCancelEnabled(bool theEnabled)
238 {
239   QPushButton* anCancelBtn = findChild<QPushButton*>(PROP_PANEL_CANCEL);
240   anCancelBtn->setEnabled(theEnabled);
241 }
242
243 bool XGUI_PropertyPanel::isCancelEnabled() const
244 {
245   QPushButton* anCancelBtn = findChild<QPushButton*>(PROP_PANEL_CANCEL);
246   return anCancelBtn->isEnabled();
247 }
248
249