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