Salome HOME
Highload tests added.
[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   aBtn->setShortcut(QKeySequence(Qt::Key_Escape));
61   aBtnLay->addWidget(aBtn);
62
63   myCustomWidget = new QWidget(aContent);
64   myMainLayout->addWidget(myCustomWidget);
65   myMainLayout->addStretch(1);
66 }
67
68 XGUI_PropertyPanel::~XGUI_PropertyPanel()
69 {
70 }
71
72 void XGUI_PropertyPanel::cleanContent()
73 {
74   myWidgets.clear();
75   qDeleteAll(myCustomWidget->children());
76   myActiveWidget = NULL;
77 }
78
79 void XGUI_PropertyPanel::setModelWidgets(const QList<ModuleBase_ModelWidget*>& theWidgets)
80 {
81   myWidgets = theWidgets;
82   int aS = myWidgets.size();
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(NULL);
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   if(myActiveWidget) {
173     myActiveWidget->setHighlighted(false);
174   }
175   if(theWidget) {
176     theWidget->setHighlighted(true);
177   }
178   myActiveWidget = theWidget;
179   emit widgetActivated(theWidget);
180 }