#include "ModuleBase_IWorkshop.h"
#include <ModelAPI_Data.h>
+#include <ModelAPI_Object.h>
#include <ModelAPI_AttributeReference.h>
#include <Config_WidgetAPI.h>
#include <QLabel>
#include <QLineEdit>
#include <QToolButton>
+#include <QString>
+#include <QEvent>
ModuleBase_SelectorWidget::ModuleBase_SelectorWidget(QWidget* theParent,
ModuleBase_IWorkshop* theWorkshop,
const Config_WidgetAPI* theData)
-: ModuleBase_ModelWidget(theParent), myWorkshop(theWorkshop)
+: ModuleBase_ModelWidget(theParent), myWorkshop(theWorkshop), myActivateOnStart(false)
{
myFeatureAttributeID = theData->widgetId();
myTextLine = new QLineEdit(myContainer);
myTextLine->setReadOnly(true);
myTextLine->setToolTip(aToolTip);
+ myTextLine->installEventFilter(this);
aLayout->addWidget(myTextLine);
myActivateBtn->setIcon(QIcon(":icons/hand_point.png"));
myActivateBtn->setCheckable(true);
myActivateBtn->setToolTip(tr("Activate/Deactivate selection"));
+ connect(myActivateBtn, SIGNAL(toggled(bool)), this, SLOT(activateSelection(bool)));
aLayout->addWidget(myActivateBtn);
- connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
+ QString aActivateTxt = QString::fromStdString(theData->getProperty("activate"));
+ if (!aActivateTxt.isNull()) {
+ myActivateOnStart = (aActivateTxt == "true");
+ }
}
//********************************************************************
boost::shared_ptr<ModelAPI_AttributeReference> aRef =
boost::dynamic_pointer_cast<ModelAPI_AttributeReference>(aData->attribute(myFeatureAttributeID));
+ aRef->setFeature(mySelectedFeature);
+
return true;
}
DataPtr aData = theFeature->data();
boost::shared_ptr<ModelAPI_AttributeReference> aRef =
boost::dynamic_pointer_cast<ModelAPI_AttributeReference>(aData->attribute(myFeatureAttributeID));
-
+
+ mySelectedFeature = aRef->value();
+ updateSelectionName();
return true;
}
void ModuleBase_SelectorWidget::onSelectionChanged()
{
QList<FeaturePtr> aFeatures = myWorkshop->selectedFeatures();
- int aCount = aFeatures.size();
+ if (aFeatures.size() > 0) {
+ FeaturePtr aFeature = aFeatures.first();
+ if ((!mySelectedFeature) && (!aFeature))
+ return;
+ if (mySelectedFeature && aFeature && mySelectedFeature->isSame(aFeature))
+ return;
+
+ // TODO: Check that the selection corresponds to selection type
+ mySelectedFeature = aFeature;
+ if (mySelectedFeature) {
+ updateSelectionName();
+ activateSelection(false);
+ } else {
+ myTextLine->setText("");
+ }
+ emit valuesChanged();
+ }
+}
+
+//********************************************************************
+void ModuleBase_SelectorWidget::updateSelectionName()
+{
+ if (mySelectedFeature) {
+ std::string aName;
+ if (mySelectedFeature->data())
+ aName = mySelectedFeature->data()->getName();
+ else
+ aName = boost::dynamic_pointer_cast<ModelAPI_Object>(mySelectedFeature)->getName();
+
+ myTextLine->setText(QString::fromStdString(aName));
+ } else
+ myTextLine->setText("");
+}
+
+//********************************************************************
+bool ModuleBase_SelectorWidget::eventFilter(QObject* theObj, QEvent* theEvent)
+{
+ if (theObj == myTextLine) {
+ if (theEvent->type() == QEvent::Polish) {
+ activateSelection(myActivateOnStart);
+ onSelectionChanged();
+ }
+ }
+ return ModuleBase_ModelWidget::eventFilter(theObj, theEvent);
+}
+
+//********************************************************************
+void ModuleBase_SelectorWidget::enableOthersControls(bool toEnable)
+{
+ QWidget* aParent = myContainer->parentWidget();
+ QList<QWidget*> aChldList = aParent->findChildren<QWidget*>();
+ foreach(QWidget* aWgt, aChldList) {
+ if ((aWgt != myLabel) && (aWgt != myActivateBtn) && (aWgt != myTextLine) && (aWgt != myContainer))
+ aWgt->setEnabled(toEnable);
+ }
+}
+
+//********************************************************************
+void ModuleBase_SelectorWidget::activateSelection(bool toActivate)
+{
+ enableOthersControls(!toActivate);
+ myTextLine->setEnabled(toActivate);
+
+ if (toActivate)
+ connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
+ else
+ disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
+
+ myActivateBtn->setDown(toActivate);
}
\ No newline at end of file
/// \return a control list
virtual QList<QWidget*> getControls() const;
+ void setActivationOnStart(bool toActivate) { myActivateOnStart = toActivate; }
+ bool activateOnStart() const { return myActivateOnStart; }
+
+ FeaturePtr selectedFeature() const { return mySelectedFeature; }
+
+public slots:
+
+ /// Activate or deactivate selection
+ void activateSelection(bool toActivate);
+
+protected:
+ bool eventFilter(QObject* theObj, QEvent* theEvent);
+
private slots:
void onSelectionChanged();
private:
+ void enableOthersControls(bool toEnable);
+ void updateSelectionName();
+
std::string myFeatureAttributeID;
QWidget* myContainer;
QToolButton* myActivateBtn;
ModuleBase_IWorkshop* myWorkshop;
+
+ bool myActivateOnStart;
+
+ FeaturePtr mySelectedFeature;
};
#endif
\ No newline at end of file