1 // File: ModuleBase_SelectorWidget.h
2 // Created: 2 June 2014
3 // Author: Vitaly Smetannikov
6 #include "ModuleBase_SelectorWidget.h"
7 #include "ModuleBase_IWorkshop.h"
9 #include <Events_Loop.h>
10 #include <Model_Events.h>
12 #include <ModelAPI_Data.h>
13 #include <ModelAPI_Object.h>
14 #include <ModelAPI_AttributeReference.h>
15 #include <Config_WidgetAPI.h>
21 #include <QToolButton>
26 ModuleBase_SelectorWidget::ModuleBase_SelectorWidget(QWidget* theParent,
27 ModuleBase_IWorkshop* theWorkshop,
28 const Config_WidgetAPI* theData)
29 : ModuleBase_ModelWidget(theParent), myWorkshop(theWorkshop), myActivateOnStart(false)
31 myFeatureAttributeID = theData->widgetId();
33 myContainer = new QWidget(theParent);
34 QHBoxLayout* aLayout = new QHBoxLayout(myContainer);
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));
42 aLayout->addWidget(myLabel);
44 QString aToolTip = QString::fromStdString(theData->widgetTooltip());
45 myTextLine = new QLineEdit(myContainer);
46 myTextLine->setReadOnly(true);
47 myTextLine->setToolTip(aToolTip);
48 myTextLine->installEventFilter(this);
50 aLayout->addWidget(myTextLine);
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)));
58 aLayout->addWidget(myActivateBtn);
60 QString aActivateTxt = QString::fromStdString(theData->getProperty("activate"));
61 if (!aActivateTxt.isNull()) {
62 myActivateOnStart = (aActivateTxt == "true");
66 //********************************************************************
67 ModuleBase_SelectorWidget::~ModuleBase_SelectorWidget()
71 //********************************************************************
72 bool ModuleBase_SelectorWidget::storeValue(FeaturePtr theFeature)
74 DataPtr aData = theFeature->data();
75 boost::shared_ptr<ModelAPI_AttributeReference> aRef =
76 boost::dynamic_pointer_cast<ModelAPI_AttributeReference>(aData->attribute(myFeatureAttributeID));
78 bool isBlocked = this->blockSignals(true);
79 aRef->setValue(mySelectedFeature);
80 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_FEATURE_UPDATED));
82 this->blockSignals(isBlocked);
86 //********************************************************************
87 bool ModuleBase_SelectorWidget::restoreValue(FeaturePtr theFeature)
89 DataPtr aData = theFeature->data();
90 boost::shared_ptr<ModelAPI_AttributeReference> aRef =
91 boost::dynamic_pointer_cast<ModelAPI_AttributeReference>(aData->attribute(myFeatureAttributeID));
93 bool isBlocked = this->blockSignals(true);
94 mySelectedFeature = aRef->value();
95 updateSelectionName();
97 this->blockSignals(isBlocked);
101 //********************************************************************
102 bool ModuleBase_SelectorWidget::canFocusTo(const std::string& theAttributeName)
107 //********************************************************************
108 void ModuleBase_SelectorWidget::focusTo()
112 //********************************************************************
113 QList<QWidget*> ModuleBase_SelectorWidget::getControls() const
115 QList<QWidget*> aControls;
116 aControls.append(myLabel);
117 aControls.append(myTextLine);
118 aControls.append(myActivateBtn);
122 //********************************************************************
123 void ModuleBase_SelectorWidget::onSelectionChanged()
125 QList<FeaturePtr> aFeatures = myWorkshop->selectedFeatures();
126 if (aFeatures.size() > 0) {
127 FeaturePtr aFeature = aFeatures.first();
128 if ((!mySelectedFeature) && (!aFeature))
130 if (mySelectedFeature && aFeature && mySelectedFeature->isSame(aFeature))
133 // TODO: Check that the selection corresponds to selection type
134 if (aFeature->getKind().compare("Sketch") != 0)
137 mySelectedFeature = aFeature;
138 if (mySelectedFeature) {
139 updateSelectionName();
140 activateSelection(false);
142 myTextLine->setText("");
144 emit valuesChanged();
148 //********************************************************************
149 void ModuleBase_SelectorWidget::updateSelectionName()
151 if (mySelectedFeature) {
153 if (mySelectedFeature->data())
154 aName = mySelectedFeature->data()->getName();
156 aName = boost::dynamic_pointer_cast<ModelAPI_Object>(mySelectedFeature)->getName();
158 myTextLine->setText(QString::fromStdString(aName));
160 myTextLine->setText("");
163 //********************************************************************
164 bool ModuleBase_SelectorWidget::eventFilter(QObject* theObj, QEvent* theEvent)
166 if (theObj == myTextLine) {
167 if (theEvent->type() == QEvent::Polish) {
168 activateSelection(myActivateOnStart);
169 onSelectionChanged();
172 return ModuleBase_ModelWidget::eventFilter(theObj, theEvent);
175 //********************************************************************
176 void ModuleBase_SelectorWidget::enableOthersControls(bool toEnable)
178 QWidget* aParent = myContainer->parentWidget();
179 QList<QWidget*> aChldList = aParent->findChildren<QWidget*>();
180 foreach(QWidget* aWgt, aChldList) {
181 if ((aWgt != myLabel) && (aWgt != myActivateBtn) && (aWgt != myTextLine) && (aWgt != myContainer))
182 aWgt->setEnabled(toEnable);
186 //********************************************************************
187 void ModuleBase_SelectorWidget::activateSelection(bool toActivate)
189 enableOthersControls(!toActivate);
190 myTextLine->setEnabled(toActivate);
193 connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
195 disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
197 myActivateBtn->setDown(toActivate);