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