]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_PropertyPanel.cpp
Salome HOME
9f6a706cfb418d9c8b7d368440be2e1d8b3cc487
[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_WidgetPoint2D.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
21 #ifdef _DEBUG
22 #include <iostream>
23 #endif
24
25 XGUI_PropertyPanel::XGUI_PropertyPanel(QWidget* theParent)
26     : ModuleBase_IPropertyPanel(theParent), 
27     myActiveWidget(NULL)
28 {
29   this->setWindowTitle(tr("Property Panel"));
30   QAction* aViewAct = this->toggleViewAction();
31   this->setObjectName(XGUI::PROP_PANEL);
32   setStyleSheet("::title { position: relative; padding-left: 5px; text-align: left center }");
33
34   QWidget* aContent = new QWidget(this);
35   myMainLayout = new QVBoxLayout(aContent);
36   myMainLayout->setContentsMargins(3, 3, 3, 3);
37   this->setWidget(aContent);
38
39   QFrame* aFrm = new QFrame(aContent);
40   aFrm->setFrameStyle(QFrame::Sunken);
41   aFrm->setFrameShape(QFrame::Panel);
42   QHBoxLayout* aBtnLay = new QHBoxLayout(aFrm);
43   aBtnLay->setContentsMargins(0, 0, 0, 0);
44   myMainLayout->addWidget(aFrm);
45
46   QPushButton* aBtn = new QPushButton(QIcon(":pictures/button_help.png"), "", aFrm);
47   aBtn->setFlat(true);
48   aBtnLay->addWidget(aBtn);
49   aBtnLay->addStretch(1);
50   aBtn = new QPushButton(QIcon(":pictures/button_ok.png"), "", aFrm);
51   aBtn->setObjectName(XGUI::PROP_PANEL_OK);
52   aBtn->setToolTip(tr("Ok"));
53   aBtn->setFlat(true);
54   aBtnLay->addWidget(aBtn);
55
56   aBtn = new QPushButton(QIcon(":pictures/button_cancel.png"), "", aFrm);
57   aBtn->setToolTip(tr("Cancel"));
58   aBtn->setObjectName(XGUI::PROP_PANEL_CANCEL);
59   aBtn->setFlat(true);
60   aBtnLay->addWidget(aBtn);
61
62   myCustomWidget = new QWidget(aContent);
63   myMainLayout->addWidget(myCustomWidget);
64   myMainLayout->addStretch(1);
65 }
66
67 XGUI_PropertyPanel::~XGUI_PropertyPanel()
68 {
69 }
70
71 void XGUI_PropertyPanel::cleanContent()
72 {
73   myWidgets.clear();
74   qDeleteAll(myCustomWidget->children());
75   myActiveWidget = NULL;
76 }
77
78 void XGUI_PropertyPanel::setModelWidgets(const QList<ModuleBase_ModelWidget*>& theWidgets)
79 {
80   myWidgets = theWidgets;
81   int aS = myWidgets.size();
82   if (theWidgets.empty()) return;
83
84   QList<ModuleBase_ModelWidget*>::const_iterator anIt = theWidgets.begin(), aLast =
85       theWidgets.end();
86   for (; anIt != aLast; anIt++) {
87     connect(*anIt, SIGNAL(keyReleased(QKeyEvent*)), this, SIGNAL(keyReleased(QKeyEvent*)));
88
89     connect(*anIt, SIGNAL(focusOutWidget(ModuleBase_ModelWidget*)), this,
90             SLOT(activateNextWidget(ModuleBase_ModelWidget*)));
91     connect(*anIt, SIGNAL(focusInWidget(ModuleBase_ModelWidget*)),
92             this, SLOT(activateWidget(ModuleBase_ModelWidget*)));
93
94     ModuleBase_WidgetPoint2D* aPointWidget = dynamic_cast<ModuleBase_WidgetPoint2D*>(*anIt);
95     if (aPointWidget)
96       connect(aPointWidget, SIGNAL(storedPoint2D(ObjectPtr, const std::string&)), this,
97               SIGNAL(storedPoint2D(ObjectPtr, const std::string&)));
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       QPushButton* anOkBtn = findChild<QPushButton*>(XGUI::PROP_PANEL_OK);
106       QPushButton* aCancelBtn = findChild<QPushButton*>(XGUI::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   foreach(ModuleBase_ModelWidget* eachWidget, myWidgets)
127   {
128     eachWidget->setFeature(theFeature);
129     eachWidget->restoreValue();
130   }
131   // the repaint is used here to immediately react in GUI to the values change.
132   repaint();
133 }
134
135 void XGUI_PropertyPanel::activateNextWidget(ModuleBase_ModelWidget* theWidget)
136 {
137   ModuleBase_ModelWidget* aNextWidget = 0;
138   QList<ModuleBase_ModelWidget*>::const_iterator anIt = myWidgets.begin(), aLast = myWidgets.end();
139   bool isFoundWidget = false;
140   for (; anIt != aLast && !aNextWidget; anIt++) {
141     if (isFoundWidget || !theWidget) {
142       if ((*anIt)->focusTo()) {
143         aNextWidget = *anIt;
144       }
145     }
146     isFoundWidget = (*anIt) == theWidget;
147   }
148   // Normaly focusTo is enough to activate widget
149   // here is a special case on mouse click in the viewer
150   if(aNextWidget == NULL) {
151     activateWidget(NULL);
152   }
153 }
154
155 void XGUI_PropertyPanel::activateNextWidget()
156 {
157   activateNextWidget(myActiveWidget);
158 }
159
160 void XGUI_PropertyPanel::setAcceptEnabled(bool isEnabled)
161 {
162   QPushButton* anOkBtn = findChild<QPushButton*>(XGUI::PROP_PANEL_OK);
163   anOkBtn->setEnabled(isEnabled);
164 }
165
166 void XGUI_PropertyPanel::activateWidget(ModuleBase_ModelWidget* theWidget)
167 {
168   if(myActiveWidget) {
169     myActiveWidget->setHighlighted(false);
170   }
171   if(theWidget) {
172     theWidget->setHighlighted(true);
173   }
174   myActiveWidget = theWidget;
175   emit widgetActivated(theWidget);
176 }