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