1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
4 * XGUI_PropertyPanel.cpp
6 * Created on: Apr 29, 2014
10 #include <XGUI_PropertyPanel.h>
11 #include <XGUI_ActionsMgr.h>
12 //#include <AppElements_Constants.h>
13 #include <ModuleBase_WidgetMultiSelector.h>
19 #include <QLayoutItem>
20 #include <QToolButton>
21 #include <QVBoxLayout>
22 #include <QVBoxLayout>
24 #include <QToolButton>
31 XGUI_PropertyPanel::XGUI_PropertyPanel(QWidget* theParent)
32 : ModuleBase_IPropertyPanel(theParent),
35 this->setWindowTitle(tr("Property Panel"));
36 QAction* aViewAct = this->toggleViewAction();
37 this->setObjectName(PROP_PANEL);
38 setStyleSheet("::title { position: relative; padding-left: 5px; text-align: left center }");
40 QWidget* aContent = new QWidget(this);
41 myMainLayout = new QVBoxLayout(aContent);
42 myMainLayout->setContentsMargins(3, 3, 3, 3);
43 this->setWidget(aContent);
45 QFrame* aFrm = new QFrame(aContent);
46 aFrm->setFrameStyle(QFrame::Sunken);
47 aFrm->setFrameShape(QFrame::Panel);
48 QHBoxLayout* aBtnLay = new QHBoxLayout(aFrm);
49 aBtnLay->setContentsMargins(0, 0, 0, 0);
50 myMainLayout->addWidget(aFrm);
52 QStringList aBtnNames;
53 aBtnNames << QString(PROP_PANEL_HELP)
54 << QString(PROP_PANEL_OK)
55 << QString(PROP_PANEL_CANCEL);
56 foreach(QString eachBtnName, aBtnNames) {
57 QToolButton* aBtn = new QToolButton(aFrm);
58 aBtn->setObjectName(eachBtnName);
59 aBtn->setAutoRaise(true);
60 aBtnLay->addWidget(aBtn);
62 aBtnLay->insertStretch(1, 1);
63 // aBtn->setShortcut(QKeySequence(Qt::Key_Escape));
65 myCustomWidget = new QWidget(aContent);
66 myMainLayout->addWidget(myCustomWidget);
67 setStretchEnabled(true);
70 XGUI_PropertyPanel::~XGUI_PropertyPanel()
74 void XGUI_PropertyPanel::cleanContent()
77 myActiveWidget->deactivate();
79 qDeleteAll(myCustomWidget->children());
80 myActiveWidget = NULL;
81 setWindowTitle(tr("Property Panel"));
84 void XGUI_PropertyPanel::setModelWidgets(const QList<ModuleBase_ModelWidget*>& theWidgets)
86 myWidgets = theWidgets;
87 if (theWidgets.empty()) return;
88 bool isEnableStretch = true;
89 QList<ModuleBase_ModelWidget*>::const_iterator anIt = theWidgets.begin(), aLast =
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*)));
98 //ModuleBase_WidgetPoint2D* aPointWidget = dynamic_cast<ModuleBase_WidgetPoint2D*>(*anIt);
100 // connect(aPointWidget, SIGNAL(storedPoint2D(ObjectPtr, const std::string&)), this,
101 // SIGNAL(storedPoint2D(ObjectPtr, const std::string&)))
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;
113 setStretchEnabled(isEnableStretch);
114 ModuleBase_ModelWidget* aLastWidget = theWidgets.last();
116 QList<QWidget*> aControls = aLastWidget->getControls();
117 if (!aControls.empty()) {
118 QWidget* aLastControl = aControls.last();
120 QToolButton* anOkBtn = findChild<QToolButton*>(PROP_PANEL_OK);
121 QToolButton* aCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
123 setTabOrder(aLastControl, anOkBtn);
124 setTabOrder(anOkBtn, aCancelBtn);
129 const QList<ModuleBase_ModelWidget*>& XGUI_PropertyPanel::modelWidgets() const
134 QWidget* XGUI_PropertyPanel::contentWidget()
136 return myCustomWidget;
139 void XGUI_PropertyPanel::updateContentWidget(FeaturePtr theFeature)
141 // Invalid feature case on abort of the operation
142 if (theFeature.get() == NULL)
144 if (theFeature->isAction() || !theFeature->data())
146 foreach(ModuleBase_ModelWidget* eachWidget, myWidgets)
148 eachWidget->setFeature(theFeature);
149 eachWidget->restoreValue();
151 // the repaint is used here to immediately react in GUI to the values change.
155 void XGUI_PropertyPanel::activateNextWidget(ModuleBase_ModelWidget* theWidget)
157 // TO CHECK: Editing operation does not have automatical activation of widgets
158 if (isEditingMode()) {
159 activateWidget(NULL);
162 ModuleBase_ModelWidget* aNextWidget = 0;
163 QList<ModuleBase_ModelWidget*>::const_iterator anIt = myWidgets.begin(), aLast = myWidgets.end();
164 bool isFoundWidget = false;
165 for (; anIt != aLast && !aNextWidget; anIt++) {
166 if (isFoundWidget || !theWidget) {
167 if ((*anIt)->focusTo()) {
171 isFoundWidget = (*anIt) == theWidget;
173 // Normaly focusTo is enough to activate widget
174 // here is a special case on mouse click in the viewer
175 //if(aNextWidget == NULL) {
176 activateWidget(aNextWidget);
180 void XGUI_PropertyPanel::setStretchEnabled(bool isEnabled)
182 if (myMainLayout->count() == 0)
184 int aStretchIdx = myMainLayout->count() - 1;
185 bool hasStretch = myMainLayout->itemAt(aStretchIdx)->spacerItem() != NULL;
188 if (!hasStretch) myMainLayout->addStretch(1);
189 } else if (hasStretch) {
190 aChild = myMainLayout->takeAt(aStretchIdx);
195 void XGUI_PropertyPanel::activateNextWidget()
197 activateNextWidget(myActiveWidget);
200 void XGUI_PropertyPanel::activateWidget(ModuleBase_ModelWidget* theWidget)
202 // Avoid activation of already actve widget. It could happen on focusIn event many times
203 if (theWidget == myActiveWidget)
206 myActiveWidget->deactivate();
207 myActiveWidget->setHighlighted(false);
211 emit beforeWidgetActivated(theWidget);
212 theWidget->activate();
213 theWidget->setHighlighted(true);
215 myActiveWidget = theWidget;
217 emit widgetActivated(theWidget);
218 else if (!isEditingMode())
219 emit noMoreWidgets();
222 void XGUI_PropertyPanel::setCancelEnabled(bool theEnabled)
224 QToolButton* anCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
225 anCancelBtn->setEnabled(theEnabled);
228 bool XGUI_PropertyPanel::isCancelEnabled() const
230 QToolButton* anCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
231 return anCancelBtn->isEnabled();
234 void XGUI_PropertyPanel::setEditingMode(bool isEditing)
236 ModuleBase_IPropertyPanel::setEditingMode(isEditing);
237 foreach(ModuleBase_ModelWidget* aWgt, myWidgets) {
238 aWgt->setEditingMode(isEditing);
242 void XGUI_PropertyPanel::setupActions(XGUI_ActionsMgr* theMgr)
244 QStringList aButtonNames;
245 aButtonNames << PROP_PANEL_OK << PROP_PANEL_CANCEL << PROP_PANEL_HELP;
246 QList<XGUI_ActionsMgr::OperationStateActionId> aActionIds;
247 aActionIds << XGUI_ActionsMgr::Accept << XGUI_ActionsMgr::Abort << XGUI_ActionsMgr::Help;
248 for (int i = 0; i < aButtonNames.size(); ++i) {
249 QToolButton* aBtn = findChild<QToolButton*>(aButtonNames.at(i));
250 QAction* anAct = theMgr->operationStateAction(aActionIds.at(i));
251 aBtn->setDefaultAction(anAct);