Salome HOME
c5b1a50d7ad70b6e6133f8c7e1494166c43a296b
[modules/shaper.git] / src / XGUI / XGUI_PropertyPanel.cpp
1 /*
2  * XGUI_PropertyPanel.cpp
3  *
4  *  Created on: Apr 29, 2014
5  *      Author: sbh
6  */
7
8 #include <XGUI_PropertyPanel.h>
9 #include <XGUI_Constants.h>
10 #include <ModuleBase_WidgetMultiSelector.h>
11
12 #include <QWidget>
13 #include <QVBoxLayout>
14 #include <QFrame>
15 #include <QPushButton>
16 #include <QIcon>
17 #include <QVBoxLayout>
18 #include <QEvent>
19 #include <QKeyEvent>
20 #include <QLayoutItem>
21
22 #ifdef _DEBUG
23 #include <iostream>
24 #endif
25
26 XGUI_PropertyPanel::XGUI_PropertyPanel(QWidget* theParent)
27     : ModuleBase_IPropertyPanel(theParent), 
28     myActiveWidget(NULL)
29 {
30   this->setWindowTitle(tr("Property Panel"));
31   QAction* aViewAct = this->toggleViewAction();
32   this->setObjectName(XGUI::PROP_PANEL);
33   setStyleSheet("::title { position: relative; padding-left: 5px; text-align: left center }");
34
35   QWidget* aContent = new QWidget(this);
36   myMainLayout = new QVBoxLayout(aContent);
37   myMainLayout->setContentsMargins(3, 3, 3, 3);
38   this->setWidget(aContent);
39
40   QFrame* aFrm = new QFrame(aContent);
41   aFrm->setFrameStyle(QFrame::Sunken);
42   aFrm->setFrameShape(QFrame::Panel);
43   QHBoxLayout* aBtnLay = new QHBoxLayout(aFrm);
44   aBtnLay->setContentsMargins(0, 0, 0, 0);
45   myMainLayout->addWidget(aFrm);
46
47   QPushButton* aBtn = new QPushButton(QIcon(":pictures/button_help.png"), "", aFrm);
48   aBtn->setFlat(true);
49   aBtnLay->addWidget(aBtn);
50   aBtnLay->addStretch(1);
51   aBtn = new QPushButton(QIcon(":pictures/button_ok.png"), "", aFrm);
52   aBtn->setObjectName(XGUI::PROP_PANEL_OK);
53   aBtn->setToolTip(tr("Ok"));
54   aBtn->setFlat(true);
55   aBtnLay->addWidget(aBtn);
56
57   aBtn = new QPushButton(QIcon(":pictures/button_cancel.png"), "", aFrm);
58   aBtn->setToolTip(tr("Cancel"));
59   aBtn->setObjectName(XGUI::PROP_PANEL_CANCEL);
60   aBtn->setFlat(true);
61   aBtn->setShortcut(QKeySequence(Qt::Key_Escape));
62   aBtnLay->addWidget(aBtn);
63
64   myCustomWidget = new QWidget(aContent);
65   myMainLayout->addWidget(myCustomWidget);
66   setStretchEnabled(true);
67 }
68
69 XGUI_PropertyPanel::~XGUI_PropertyPanel()
70 {
71 }
72
73 void XGUI_PropertyPanel::cleanContent()
74 {
75   if (myActiveWidget)
76     myActiveWidget->deactivate();
77   myWidgets.clear();
78   qDeleteAll(myCustomWidget->children());
79   myActiveWidget = NULL;
80 }
81
82 void XGUI_PropertyPanel::setModelWidgets(const QList<ModuleBase_ModelWidget*>& theWidgets)
83 {
84   myWidgets = theWidgets;
85   if (theWidgets.empty()) return;
86   bool isEnableStretch = true;
87   QList<ModuleBase_ModelWidget*>::const_iterator anIt = theWidgets.begin(), aLast =
88       theWidgets.end();
89   for (; anIt != aLast; anIt++) {
90     connect(*anIt, SIGNAL(keyReleased(QKeyEvent*)), this, SIGNAL(keyReleased(QKeyEvent*)));
91     connect(*anIt, SIGNAL(focusOutWidget(ModuleBase_ModelWidget*)),
92             this,  SLOT(activateNextWidget(ModuleBase_ModelWidget*)));
93     connect(*anIt, SIGNAL(focusInWidget(ModuleBase_ModelWidget*)),
94             this,  SLOT(activateWidget(ModuleBase_ModelWidget*)));
95
96     //ModuleBase_WidgetPoint2D* aPointWidget = dynamic_cast<ModuleBase_WidgetPoint2D*>(*anIt);
97     //if (aPointWidget)
98     //  connect(aPointWidget, SIGNAL(storedPoint2D(ObjectPtr, const std::string&)), this,
99     //          SIGNAL(storedPoint2D(ObjectPtr, const std::string&)))
100
101     if (!isEnableStretch) continue;
102     foreach(QWidget* eachWidget, (*anIt)->getControls()) {
103       QSizePolicy::Policy aVPolicy = eachWidget->sizePolicy().verticalPolicy();
104       if(aVPolicy == QSizePolicy::Expanding ||
105          aVPolicy == QSizePolicy::MinimumExpanding) {
106         isEnableStretch = false;
107       }
108     }
109   }
110   setStretchEnabled(isEnableStretch);
111   ModuleBase_ModelWidget* aLastWidget = theWidgets.last();
112   if (aLastWidget) {
113     QList<QWidget*> aControls = aLastWidget->getControls();
114     if (!aControls.empty()) {
115       QWidget* aLastControl = aControls.last();
116
117       QPushButton* anOkBtn = findChild<QPushButton*>(XGUI::PROP_PANEL_OK);
118       QPushButton* aCancelBtn = findChild<QPushButton*>(XGUI::PROP_PANEL_CANCEL);
119
120       setTabOrder(aLastControl, anOkBtn);
121       setTabOrder(anOkBtn, aCancelBtn);
122     }
123   }
124 }
125
126 const QList<ModuleBase_ModelWidget*>& XGUI_PropertyPanel::modelWidgets() const
127 {
128   return myWidgets;
129 }
130
131 QWidget* XGUI_PropertyPanel::contentWidget()
132 {
133   return myCustomWidget;
134 }
135
136 void XGUI_PropertyPanel::updateContentWidget(FeaturePtr theFeature)
137 {
138   // Invalid feature case on abort of the operation
139   if(!theFeature->data())
140     return;
141   foreach(ModuleBase_ModelWidget* eachWidget, myWidgets)
142   {
143     eachWidget->setFeature(theFeature);
144     eachWidget->restoreValue();
145   }
146   // the repaint is used here to immediately react in GUI to the values change.
147   repaint();
148 }
149
150 void XGUI_PropertyPanel::activateNextWidget(ModuleBase_ModelWidget* theWidget)
151 {
152   ModuleBase_ModelWidget* aNextWidget = 0;
153   QList<ModuleBase_ModelWidget*>::const_iterator anIt = myWidgets.begin(), aLast = myWidgets.end();
154   bool isFoundWidget = false;
155   for (; anIt != aLast && !aNextWidget; anIt++) {
156     if (isFoundWidget || !theWidget) {
157       if ((*anIt)->focusTo()) {
158         aNextWidget = *anIt;
159       }
160     }
161     isFoundWidget = (*anIt) == theWidget;
162   }
163   // Normaly focusTo is enough to activate widget
164   // here is a special case on mouse click in the viewer
165   //if(aNextWidget == NULL) {
166     activateWidget(aNextWidget);
167   //}
168 }
169
170 void XGUI_PropertyPanel::setStretchEnabled(bool isEnabled)
171 {
172   if (myMainLayout->count() == 0)
173     return;
174   int aStretchIdx = myMainLayout->count() - 1;
175   bool hasStretch = myMainLayout->itemAt(aStretchIdx)->spacerItem() != NULL;
176   QLayoutItem* aChild;
177   if (isEnabled) {
178     if (!hasStretch) myMainLayout->addStretch(1);
179   } else if (hasStretch) {
180     aChild = myMainLayout->takeAt(aStretchIdx);
181     delete aChild;
182   }
183 }
184
185 void XGUI_PropertyPanel::activateNextWidget()
186 {
187   activateNextWidget(myActiveWidget);
188 }
189
190 void XGUI_PropertyPanel::setAcceptEnabled(bool isEnabled)
191 {
192   QPushButton* anOkBtn = findChild<QPushButton*>(XGUI::PROP_PANEL_OK);
193   anOkBtn->setEnabled(isEnabled);
194 }
195
196 void XGUI_PropertyPanel::activateWidget(ModuleBase_ModelWidget* theWidget)
197 {
198   // Avoid activation of already actve widget. It could happen on focusIn event many times
199   if (theWidget == myActiveWidget)
200     return;
201   if(myActiveWidget) {
202     myActiveWidget->deactivate();
203     myActiveWidget->setHighlighted(false);
204   }
205   if(theWidget) {
206     theWidget->activate();
207     theWidget->setHighlighted(true);
208   }
209   myActiveWidget = theWidget;
210   if (myActiveWidget)
211     emit widgetActivated(theWidget);
212   else
213     emit noMoreWidgets();
214 }