]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_PropertyPanel.cpp
Salome HOME
1779431f5511a3dc204568139f8b334c7e9f72eb
[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 <XGUI_ActionsMgr.h>
12 //#include <AppElements_Constants.h>
13 #include <ModuleBase_WidgetMultiSelector.h>
14 #include <ModuleBase_Tools.h>
15
16 #include <QEvent>
17 #include <QFrame>
18 #include <QIcon>
19 #include <QKeyEvent>
20 #include <QLayoutItem>
21 #include <QToolButton>
22 #include <QVBoxLayout>
23 #include <QGridLayout>
24 #include <QWidget>
25 #include <QToolButton>
26 #include <QAction>
27
28 #ifdef _DEBUG
29 #include <iostream>
30 #endif
31
32 XGUI_PropertyPanel::XGUI_PropertyPanel(QWidget* theParent)
33     : ModuleBase_IPropertyPanel(theParent), 
34     myActiveWidget(NULL)
35 {
36   this->setWindowTitle(tr("Property Panel"));
37   QAction* aViewAct = this->toggleViewAction();
38   this->setObjectName(PROP_PANEL);
39   setStyleSheet("::title { position: relative; padding-left: 5px; text-align: left center }");
40
41   QWidget* aContent = new QWidget(this);
42   myMainLayout = new QGridLayout(aContent);
43   const int kPanelColumn = 0;
44   int aPanelRow = 0;
45   myMainLayout->setContentsMargins(3, 3, 3, 3);
46   this->setWidget(aContent);
47
48   QFrame* aFrm = new QFrame(aContent);
49   aFrm->setFrameStyle(QFrame::Raised);
50   aFrm->setFrameShape(QFrame::Panel);
51   QHBoxLayout* aBtnLay = new QHBoxLayout(aFrm);
52   ModuleBase_Tools::zeroMargins(aBtnLay);
53   myMainLayout->addWidget(aFrm, aPanelRow++, kPanelColumn);
54
55   QStringList aBtnNames;
56   aBtnNames << QString(PROP_PANEL_HELP)
57             << QString(PROP_PANEL_OK)
58             << QString(PROP_PANEL_CANCEL);
59   foreach(QString eachBtnName, aBtnNames) {
60     QToolButton* aBtn = new QToolButton(aFrm);
61     aBtn->setObjectName(eachBtnName);
62     aBtn->setAutoRaise(true);
63     aBtnLay->addWidget(aBtn);
64   }
65   aBtnLay->insertStretch(1, 1);
66
67   myCustomWidget = new QWidget(aContent);
68   myCustomWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::MinimumExpanding);
69   myMainLayout->addWidget(myCustomWidget, aPanelRow, kPanelColumn);
70   setStretchEnabled(true);
71 }
72
73 XGUI_PropertyPanel::~XGUI_PropertyPanel()
74 {
75 }
76
77 void XGUI_PropertyPanel::cleanContent()
78 {
79   if (myActiveWidget)
80     myActiveWidget->deactivate();
81   myWidgets.clear();
82   qDeleteAll(myCustomWidget->children());
83   myActiveWidget = NULL;
84   setWindowTitle(tr("Property Panel"));
85 }
86
87 void XGUI_PropertyPanel::setModelWidgets(const QList<ModuleBase_ModelWidget*>& theWidgets)
88 {
89   myWidgets = theWidgets;
90   if (theWidgets.empty()) return;
91   QList<ModuleBase_ModelWidget*>::const_iterator anIt = theWidgets.begin();
92   for (; anIt != theWidgets.end(); 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_ModelWidget* aLastWidget = theWidgets.last();
100   if (aLastWidget) {
101     QList<QWidget*> aControls = aLastWidget->getControls();
102     if (!aControls.empty()) {
103       QWidget* aLastControl = aControls.last();
104
105       QToolButton* anOkBtn = findChild<QToolButton*>(PROP_PANEL_OK);
106       QToolButton* aCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
107
108       setTabOrder(aLastControl, anOkBtn);
109       setTabOrder(anOkBtn, aCancelBtn);
110     }
111   }
112 }
113
114 const QList<ModuleBase_ModelWidget*>& XGUI_PropertyPanel::modelWidgets() const
115 {
116   return myWidgets;
117 }
118
119 QWidget* XGUI_PropertyPanel::contentWidget()
120 {
121   return myCustomWidget;
122 }
123
124 void XGUI_PropertyPanel::updateContentWidget(FeaturePtr theFeature)
125 {
126   // Invalid feature case on abort of the operation
127   if (theFeature.get() == NULL)
128     return;
129   if (theFeature->isAction() || !theFeature->data())
130     return;
131   foreach(ModuleBase_ModelWidget* eachWidget, myWidgets)
132   {
133     eachWidget->setFeature(theFeature);
134     eachWidget->restoreValue();
135   }
136   // the repaint is used here to immediately react in GUI to the values change.
137   repaint();
138 }
139
140 void XGUI_PropertyPanel::activateNextWidget(ModuleBase_ModelWidget* theWidget)
141 {
142   // TO CHECK: Editing operation does not have automatical activation of widgets
143   if (isEditingMode()) {
144     activateWidget(NULL);
145     return;
146   }
147   ModuleBase_ModelWidget* aNextWidget = 0;
148   QList<ModuleBase_ModelWidget*>::const_iterator anIt = myWidgets.begin(), aLast = myWidgets.end();
149   bool isFoundWidget = false;
150   for (; anIt != aLast && !aNextWidget; anIt++) {
151     if (isFoundWidget || !theWidget) {
152       if ((*anIt)->focusTo()) {
153         aNextWidget = *anIt;
154       }
155     }
156     isFoundWidget = (*anIt) == theWidget;
157   }
158   // Normaly focusTo is enough to activate widget
159   // here is a special case on mouse click in the viewer
160   //if(aNextWidget == NULL) {
161     activateWidget(aNextWidget);
162   //}
163 }
164
165 void XGUI_PropertyPanel::setStretchEnabled(bool isEnabled)
166 {
167   int aStretchIdx = myMainLayout->rowCount() - 1;
168   if (aStretchIdx < 0)
169     return;
170   myMainLayout->setRowStretch(aStretchIdx, isEnabled ? 1 : 0);
171 }
172
173 void XGUI_PropertyPanel::activateNextWidget()
174 {
175   activateNextWidget(myActiveWidget);
176 }
177
178 void XGUI_PropertyPanel::activateWidget(ModuleBase_ModelWidget* theWidget)
179 {
180   // Avoid activation of already actve widget. It could happen on focusIn event many times
181   if (theWidget == myActiveWidget)
182     return;
183   if(myActiveWidget) {
184     myActiveWidget->deactivate();
185     myActiveWidget->setHighlighted(false);
186   }
187   if(theWidget) {
188     if (theWidget)
189       emit beforeWidgetActivated(theWidget);
190     theWidget->activate();
191     theWidget->setHighlighted(true);
192   }
193   myActiveWidget = theWidget;
194   if (myActiveWidget)
195     emit widgetActivated(theWidget);
196   else if (!isEditingMode())
197     emit noMoreWidgets();
198 }
199
200 void XGUI_PropertyPanel::setCancelEnabled(bool theEnabled)
201 {
202   QToolButton* anCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
203   anCancelBtn->setEnabled(theEnabled);
204 }
205
206 bool XGUI_PropertyPanel::isCancelEnabled() const
207 {
208   QToolButton* anCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
209   return anCancelBtn->isEnabled();
210 }
211
212 void XGUI_PropertyPanel::setEditingMode(bool isEditing)
213 {
214   ModuleBase_IPropertyPanel::setEditingMode(isEditing);
215   foreach(ModuleBase_ModelWidget* aWgt, myWidgets) {
216     aWgt->setEditingMode(isEditing);
217   }
218 }
219
220 void XGUI_PropertyPanel::setupActions(XGUI_ActionsMgr* theMgr)
221 {
222   QStringList aButtonNames;
223   aButtonNames << PROP_PANEL_OK << PROP_PANEL_CANCEL << PROP_PANEL_HELP;
224   QList<XGUI_ActionsMgr::OperationStateActionId> aActionIds;
225   aActionIds << XGUI_ActionsMgr::Accept << XGUI_ActionsMgr::Abort << XGUI_ActionsMgr::Help;
226   for (int i = 0; i < aButtonNames.size(); ++i) {
227     QToolButton* aBtn = findChild<QToolButton*>(aButtonNames.at(i));
228     QAction* anAct = theMgr->operationStateAction(aActionIds.at(i));
229     aBtn->setDefaultAction(anAct);
230   }
231 }