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 <ModelAPI_Data.h>
10 #include <ModelAPI_Object.h>
11 #include <ModelAPI_AttributeReference.h>
12 #include <Config_WidgetAPI.h>
18 #include <QToolButton>
23 ModuleBase_SelectorWidget::ModuleBase_SelectorWidget(QWidget* theParent,
24 ModuleBase_IWorkshop* theWorkshop,
25 const Config_WidgetAPI* theData)
26 : ModuleBase_ModelWidget(theParent), myWorkshop(theWorkshop), myActivateOnStart(false)
28 myFeatureAttributeID = theData->widgetId();
30 myContainer = new QWidget(theParent);
31 QHBoxLayout* aLayout = new QHBoxLayout(myContainer);
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));
39 aLayout->addWidget(myLabel);
41 QString aToolTip = QString::fromStdString(theData->widgetTooltip());
42 myTextLine = new QLineEdit(myContainer);
43 myTextLine->setReadOnly(true);
44 myTextLine->setToolTip(aToolTip);
45 myTextLine->installEventFilter(this);
47 aLayout->addWidget(myTextLine);
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)));
55 aLayout->addWidget(myActivateBtn);
57 QString aActivateTxt = QString::fromStdString(theData->getProperty("activate"));
58 if (!aActivateTxt.isNull()) {
59 myActivateOnStart = (aActivateTxt == "true");
63 //********************************************************************
64 ModuleBase_SelectorWidget::~ModuleBase_SelectorWidget()
68 //********************************************************************
69 bool ModuleBase_SelectorWidget::storeValue(FeaturePtr theFeature)
71 DataPtr aData = theFeature->data();
72 boost::shared_ptr<ModelAPI_AttributeReference> aRef =
73 boost::dynamic_pointer_cast<ModelAPI_AttributeReference>(aData->attribute(myFeatureAttributeID));
75 aRef->setFeature(mySelectedFeature);
80 //********************************************************************
81 bool ModuleBase_SelectorWidget::restoreValue(FeaturePtr theFeature)
83 DataPtr aData = theFeature->data();
84 boost::shared_ptr<ModelAPI_AttributeReference> aRef =
85 boost::dynamic_pointer_cast<ModelAPI_AttributeReference>(aData->attribute(myFeatureAttributeID));
87 mySelectedFeature = aRef->value();
88 updateSelectionName();
92 //********************************************************************
93 bool ModuleBase_SelectorWidget::canFocusTo(const std::string& theAttributeName)
98 //********************************************************************
99 void ModuleBase_SelectorWidget::focusTo()
103 //********************************************************************
104 QList<QWidget*> ModuleBase_SelectorWidget::getControls() const
106 QList<QWidget*> aControls;
107 aControls.append(myLabel);
108 aControls.append(myTextLine);
109 aControls.append(myActivateBtn);
113 //********************************************************************
114 void ModuleBase_SelectorWidget::onSelectionChanged()
116 QList<FeaturePtr> aFeatures = myWorkshop->selectedFeatures();
117 if (aFeatures.size() > 0) {
118 FeaturePtr aFeature = aFeatures.first();
119 if ((!mySelectedFeature) && (!aFeature))
121 if (mySelectedFeature && aFeature && mySelectedFeature->isSame(aFeature))
124 // TODO: Check that the selection corresponds to selection type
125 mySelectedFeature = aFeature;
126 if (mySelectedFeature) {
127 updateSelectionName();
128 activateSelection(false);
130 myTextLine->setText("");
132 emit valuesChanged();
136 //********************************************************************
137 void ModuleBase_SelectorWidget::updateSelectionName()
139 if (mySelectedFeature) {
141 if (mySelectedFeature->data())
142 aName = mySelectedFeature->data()->getName();
144 aName = boost::dynamic_pointer_cast<ModelAPI_Object>(mySelectedFeature)->getName();
146 myTextLine->setText(QString::fromStdString(aName));
148 myTextLine->setText("");
151 //********************************************************************
152 bool ModuleBase_SelectorWidget::eventFilter(QObject* theObj, QEvent* theEvent)
154 if (theObj == myTextLine) {
155 if (theEvent->type() == QEvent::Polish) {
156 activateSelection(myActivateOnStart);
157 onSelectionChanged();
160 return ModuleBase_ModelWidget::eventFilter(theObj, theEvent);
163 //********************************************************************
164 void ModuleBase_SelectorWidget::enableOthersControls(bool toEnable)
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);
174 //********************************************************************
175 void ModuleBase_SelectorWidget::activateSelection(bool toActivate)
177 enableOthersControls(!toActivate);
178 myTextLine->setEnabled(toActivate);
181 connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
183 disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
185 myActivateBtn->setDown(toActivate);