Salome HOME
Make finishing operation in python console produce no warnings: isOperation of root...
[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     : QDockWidget(theParent)
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   aBtnLay->addWidget(aBtn);
60
61   myCustomWidget = new QWidget(aContent);
62   myMainLayout->addWidget(myCustomWidget);
63   myMainLayout->addStretch(1);
64 }
65
66 XGUI_PropertyPanel::~XGUI_PropertyPanel()
67 {
68 }
69
70 void XGUI_PropertyPanel::cleanContent()
71 {
72   myWidgets.clear();
73   qDeleteAll(myCustomWidget->children());
74 }
75
76 void XGUI_PropertyPanel::setModelWidgets(const QList<ModuleBase_ModelWidget*>& theWidgets)
77 {
78   myWidgets = theWidgets;
79   if (theWidgets.empty()) return;
80
81   QList<ModuleBase_ModelWidget*>::const_iterator anIt = theWidgets.begin(), aLast =
82       theWidgets.end();
83   for (; anIt != aLast; anIt++) {
84     connect(*anIt, SIGNAL(keyReleased(QKeyEvent*)), this, SIGNAL(keyReleased(QKeyEvent*)));
85
86     connect(*anIt, SIGNAL(focusOutWidget(ModuleBase_ModelWidget*)), this,
87             SLOT(onActivateNextWidget(ModuleBase_ModelWidget*)));
88     connect(*anIt, SIGNAL(focusInWidget(ModuleBase_ModelWidget*)),
89             this, SIGNAL(widgetActivated(ModuleBase_ModelWidget*)));
90
91     ModuleBase_WidgetPoint2D* aPointWidget = dynamic_cast<ModuleBase_WidgetPoint2D*>(*anIt);
92     if (aPointWidget)
93       connect(aPointWidget, SIGNAL(storedPoint2D(ObjectPtr, const std::string&)), this,
94               SIGNAL(storedPoint2D(ObjectPtr, const std::string&)));
95   }
96   ModuleBase_ModelWidget* aLastWidget = theWidgets.last();
97   if (aLastWidget) {
98     QList<QWidget*> aControls = aLastWidget->getControls();
99     if (!aControls.empty()) {
100       QWidget* aLastControl = aControls.last();
101
102       QPushButton* anOkBtn = findChild<QPushButton*>(XGUI::PROP_PANEL_OK);
103       QPushButton* aCancelBtn = findChild<QPushButton*>(XGUI::PROP_PANEL_CANCEL);
104
105       setTabOrder(aLastControl, anOkBtn);
106       setTabOrder(anOkBtn, aCancelBtn);
107     }
108   }
109 }
110
111 const QList<ModuleBase_ModelWidget*>& XGUI_PropertyPanel::modelWidgets() const
112 {
113   return myWidgets;
114 }
115
116 QWidget* XGUI_PropertyPanel::contentWidget()
117 {
118   return myCustomWidget;
119 }
120
121 void XGUI_PropertyPanel::updateContentWidget(FeaturePtr theFeature)
122 {
123   foreach(ModuleBase_ModelWidget* eachWidget, myWidgets)
124   {
125     eachWidget->setFeature(theFeature);
126     eachWidget->restoreValue();
127   }
128   // the repaint is used here to immediatelly react in GUI to the values change.
129   repaint();
130 }
131
132 void XGUI_PropertyPanel::onActivateNextWidget(ModuleBase_ModelWidget* theWidget)
133 {
134   ModuleBase_ModelWidget* aNextWidget = 0;
135   QList<ModuleBase_ModelWidget*>::const_iterator anIt = myWidgets.begin(), aLast = myWidgets.end();
136   bool isFoundWidget = false;
137   for (; anIt != aLast && !aNextWidget; anIt++) {
138     if (isFoundWidget || !theWidget) {
139       if ((*anIt)->focusTo()) {
140         aNextWidget = *anIt;
141       }
142     }
143     isFoundWidget = (*anIt) == theWidget;
144   }
145   emit widgetActivated(aNextWidget);
146 }
147
148 void XGUI_PropertyPanel::setAcceptEnabled(bool isEnabled)
149 {
150   QPushButton* anOkBtn = findChild<QPushButton*>(XGUI::PROP_PANEL_OK);
151   anOkBtn->setEnabled(isEnabled);
152 }