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