Salome HOME
Create Extrusion property panel.
[modules/shaper.git] / src / ModuleBase / ModuleBase_SelectorWidget.cpp
1 // File:        ModuleBase_SelectorWidget.h
2 // Created:     2 June 2014
3 // Author:      Vitaly Smetannikov
4
5
6 #include "ModuleBase_SelectorWidget.h"
7 #include "ModuleBase_IWorkshop.h"
8
9 #include <ModelAPI_Data.h>
10 #include <ModelAPI_Object.h>
11 #include <ModelAPI_AttributeReference.h>
12 #include <Config_WidgetAPI.h>
13
14 #include <QWidget>
15 #include <QLayout>
16 #include <QLabel>
17 #include <QLineEdit>
18 #include <QToolButton>
19 #include <QString>
20 #include <QEvent>
21
22
23 ModuleBase_SelectorWidget::ModuleBase_SelectorWidget(QWidget* theParent, 
24                                                      ModuleBase_IWorkshop* theWorkshop, 
25                                                      const Config_WidgetAPI* theData)
26 : ModuleBase_ModelWidget(theParent), myWorkshop(theWorkshop), myActivateOnStart(false)
27 {
28   myFeatureAttributeID = theData->widgetId();
29
30   myContainer = new QWidget(theParent);
31   QHBoxLayout* aLayout = new QHBoxLayout(myContainer);
32
33   aLayout->setContentsMargins(0, 0, 0, 0);
34   QString aLabelText = QString::fromStdString(theData->widgetLabel());
35   QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
36   myLabel = new QLabel(aLabelText, myContainer);
37   myLabel->setPixmap(QPixmap(aLabelIcon));
38
39   aLayout->addWidget(myLabel);
40
41   QString aToolTip = QString::fromStdString(theData->widgetTooltip());
42   myTextLine = new QLineEdit(myContainer);
43   myTextLine->setReadOnly(true);
44   myTextLine->setToolTip(aToolTip);
45   myTextLine->installEventFilter(this);
46
47   aLayout->addWidget(myTextLine);
48
49   myActivateBtn = new QToolButton(myContainer);
50   myActivateBtn->setIcon(QIcon(":icons/hand_point.png"));
51   myActivateBtn->setCheckable(true);
52   myActivateBtn->setToolTip(tr("Activate/Deactivate selection"));
53   connect(myActivateBtn, SIGNAL(toggled(bool)), this, SLOT(activateSelection(bool)));
54
55   aLayout->addWidget(myActivateBtn);
56
57   QString aActivateTxt = QString::fromStdString(theData->getProperty("activate"));
58   if (!aActivateTxt.isNull()) {
59     myActivateOnStart = (aActivateTxt == "true");
60   }
61 }
62
63 //********************************************************************
64 ModuleBase_SelectorWidget::~ModuleBase_SelectorWidget()
65 {
66 }
67
68 //********************************************************************
69 bool ModuleBase_SelectorWidget::storeValue(FeaturePtr theFeature)
70 {
71   DataPtr aData = theFeature->data();
72   boost::shared_ptr<ModelAPI_AttributeReference> aRef = 
73     boost::dynamic_pointer_cast<ModelAPI_AttributeReference>(aData->attribute(myFeatureAttributeID));
74
75   aRef->setFeature(mySelectedFeature);
76
77   return true;
78 }
79
80 //********************************************************************
81 bool ModuleBase_SelectorWidget::restoreValue(FeaturePtr theFeature)
82 {
83   DataPtr aData = theFeature->data();
84   boost::shared_ptr<ModelAPI_AttributeReference> aRef = 
85     boost::dynamic_pointer_cast<ModelAPI_AttributeReference>(aData->attribute(myFeatureAttributeID));
86
87   mySelectedFeature = aRef->value();
88   updateSelectionName(); 
89   return true;
90 }
91
92 //********************************************************************
93 bool ModuleBase_SelectorWidget::canFocusTo(const std::string& theAttributeName)
94 {
95   return false;
96 }
97
98 //********************************************************************
99 void ModuleBase_SelectorWidget::focusTo()
100 {
101 }
102
103 //********************************************************************
104 QList<QWidget*> ModuleBase_SelectorWidget::getControls() const
105 {
106   QList<QWidget*> aControls;
107   aControls.append(myLabel);
108   aControls.append(myTextLine);
109   aControls.append(myActivateBtn);
110   return aControls;
111 }
112
113 //********************************************************************
114 void ModuleBase_SelectorWidget::onSelectionChanged()
115 {
116   QList<FeaturePtr> aFeatures = myWorkshop->selectedFeatures();
117   if (aFeatures.size() > 0) {
118     FeaturePtr aFeature = aFeatures.first();
119     if ((!mySelectedFeature) && (!aFeature))
120       return;
121     if (mySelectedFeature && aFeature && mySelectedFeature->isSame(aFeature))
122       return;
123
124     // TODO: Check that the selection corresponds to selection type
125     mySelectedFeature = aFeature;
126     if (mySelectedFeature) {
127       updateSelectionName();
128       activateSelection(false);
129     } else {
130       myTextLine->setText("");
131     }
132     emit valuesChanged();
133   }
134 }
135
136 //********************************************************************
137 void ModuleBase_SelectorWidget::updateSelectionName()
138 {
139   if (mySelectedFeature) {
140     std::string aName;
141     if (mySelectedFeature->data())
142       aName = mySelectedFeature->data()->getName();
143     else 
144       aName = boost::dynamic_pointer_cast<ModelAPI_Object>(mySelectedFeature)->getName();
145  
146     myTextLine->setText(QString::fromStdString(aName));
147   } else 
148     myTextLine->setText("");
149 }
150
151 //********************************************************************
152 bool ModuleBase_SelectorWidget::eventFilter(QObject* theObj, QEvent* theEvent)
153 {
154   if (theObj == myTextLine) {
155     if (theEvent->type() == QEvent::Polish) {
156       activateSelection(myActivateOnStart);
157       onSelectionChanged();
158     }
159   }
160   return ModuleBase_ModelWidget::eventFilter(theObj, theEvent);
161 }
162
163 //********************************************************************
164 void ModuleBase_SelectorWidget::enableOthersControls(bool toEnable)
165 {
166   QWidget* aParent = myContainer->parentWidget();
167   QList<QWidget*> aChldList = aParent->findChildren<QWidget*>();
168   foreach(QWidget* aWgt, aChldList) {
169     if ((aWgt != myLabel) && (aWgt != myActivateBtn) && (aWgt != myTextLine) && (aWgt != myContainer))
170       aWgt->setEnabled(toEnable);
171   }
172 }
173
174 //********************************************************************
175 void ModuleBase_SelectorWidget::activateSelection(bool toActivate)
176 {
177   enableOthersControls(!toActivate);
178   myTextLine->setEnabled(toActivate);
179
180   if (toActivate)
181     connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
182   else
183     disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
184
185   myActivateBtn->setDown(toActivate);
186 }