Salome HOME
Issue #273: Add copyright string
[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 }
83
84 void XGUI_PropertyPanel::setModelWidgets(const QList<ModuleBase_ModelWidget*>& theWidgets)
85 {
86   myWidgets = theWidgets;
87   if (theWidgets.empty()) return;
88   bool isEnableStretch = true;
89   QList<ModuleBase_ModelWidget*>::const_iterator anIt = theWidgets.begin(), aLast =
90       theWidgets.end();
91   for (; anIt != aLast; anIt++) {
92     connect(*anIt, SIGNAL(keyReleased(QKeyEvent*)), this, SIGNAL(keyReleased(QKeyEvent*)));
93     connect(*anIt, SIGNAL(focusOutWidget(ModuleBase_ModelWidget*)),
94             this,  SLOT(activateNextWidget(ModuleBase_ModelWidget*)));
95     connect(*anIt, SIGNAL(focusInWidget(ModuleBase_ModelWidget*)),
96             this,  SLOT(activateWidget(ModuleBase_ModelWidget*)));
97
98     //ModuleBase_WidgetPoint2D* aPointWidget = dynamic_cast<ModuleBase_WidgetPoint2D*>(*anIt);
99     //if (aPointWidget)
100     //  connect(aPointWidget, SIGNAL(storedPoint2D(ObjectPtr, const std::string&)), this,
101     //          SIGNAL(storedPoint2D(ObjectPtr, const std::string&)))
102     //}
103
104     if (!isEnableStretch) continue;
105     foreach(QWidget* eachWidget, (*anIt)->getControls()) {
106       QSizePolicy::Policy aVPolicy = eachWidget->sizePolicy().verticalPolicy();
107       if(aVPolicy == QSizePolicy::Expanding ||
108          aVPolicy == QSizePolicy::MinimumExpanding) {
109         isEnableStretch = false;
110       }
111     }
112   }
113   setStretchEnabled(isEnableStretch);
114   ModuleBase_ModelWidget* aLastWidget = theWidgets.last();
115   if (aLastWidget) {
116     QList<QWidget*> aControls = aLastWidget->getControls();
117     if (!aControls.empty()) {
118       QWidget* aLastControl = aControls.last();
119
120       QPushButton* anOkBtn = findChild<QPushButton*>(PROP_PANEL_OK);
121       QPushButton* aCancelBtn = findChild<QPushButton*>(PROP_PANEL_CANCEL);
122
123       setTabOrder(aLastControl, anOkBtn);
124       setTabOrder(anOkBtn, aCancelBtn);
125     }
126   }
127 }
128
129 const QList<ModuleBase_ModelWidget*>& XGUI_PropertyPanel::modelWidgets() const
130 {
131   return myWidgets;
132 }
133
134 QWidget* XGUI_PropertyPanel::contentWidget()
135 {
136   return myCustomWidget;
137 }
138
139 void XGUI_PropertyPanel::updateContentWidget(FeaturePtr theFeature)
140 {
141   // Invalid feature case on abort of the operation
142   if(!theFeature->data())
143     return;
144   foreach(ModuleBase_ModelWidget* eachWidget, myWidgets)
145   {
146     eachWidget->setFeature(theFeature);
147     eachWidget->restoreValue();
148   }
149   // the repaint is used here to immediately react in GUI to the values change.
150   repaint();
151 }
152
153 void XGUI_PropertyPanel::activateNextWidget(ModuleBase_ModelWidget* theWidget)
154 {
155   // TO CHECK: Editing operation does not have automatical activation of widgets
156   if (isEditingMode()) {
157     activateWidget(NULL);
158     return;
159   }
160   ModuleBase_ModelWidget* aNextWidget = 0;
161   QList<ModuleBase_ModelWidget*>::const_iterator anIt = myWidgets.begin(), aLast = myWidgets.end();
162   bool isFoundWidget = false;
163   for (; anIt != aLast && !aNextWidget; anIt++) {
164     if (isFoundWidget || !theWidget) {
165       if ((*anIt)->focusTo()) {
166         aNextWidget = *anIt;
167       }
168     }
169     isFoundWidget = (*anIt) == theWidget;
170   }
171   // Normaly focusTo is enough to activate widget
172   // here is a special case on mouse click in the viewer
173   //if(aNextWidget == NULL) {
174     activateWidget(aNextWidget);
175   //}
176 }
177
178 void XGUI_PropertyPanel::setStretchEnabled(bool isEnabled)
179 {
180   if (myMainLayout->count() == 0)
181     return;
182   int aStretchIdx = myMainLayout->count() - 1;
183   bool hasStretch = myMainLayout->itemAt(aStretchIdx)->spacerItem() != NULL;
184   QLayoutItem* aChild;
185   if (isEnabled) {
186     if (!hasStretch) myMainLayout->addStretch(1);
187   } else if (hasStretch) {
188     aChild = myMainLayout->takeAt(aStretchIdx);
189     delete aChild;
190   }
191 }
192
193 void XGUI_PropertyPanel::activateNextWidget()
194 {
195   activateNextWidget(myActiveWidget);
196 }
197
198 void XGUI_PropertyPanel::setAcceptEnabled(bool isEnabled)
199 {
200   QPushButton* anOkBtn = findChild<QPushButton*>(PROP_PANEL_OK);
201   anOkBtn->setEnabled(isEnabled);
202 }
203
204 void XGUI_PropertyPanel::activateWidget(ModuleBase_ModelWidget* theWidget)
205 {
206   // Avoid activation of already actve widget. It could happen on focusIn event many times
207   if (theWidget == myActiveWidget)
208     return;
209   if(myActiveWidget) {
210     myActiveWidget->deactivate();
211     myActiveWidget->setHighlighted(false);
212   }
213   if(theWidget) {
214     theWidget->activate();
215     theWidget->setHighlighted(true);
216   }
217   myActiveWidget = theWidget;
218   if (myActiveWidget)
219     emit widgetActivated(theWidget);
220   else if (!isEditingMode())
221     emit noMoreWidgets();
222 }