<plugin>
<workbench id="Construction">
<group id="Basic">
- <feature id="Point" title="Point" tooltip="Create a new point" icon=":icons/point.png">
- <!-- validator id="test" parameters="x,y,z"/ -->
- <source path="point_widget.xml"/>
+ <feature
+ id="Point"
+ title="Point"
+ tooltip="Create a new point"
+ icon=":icons/point.png">
+ <source path="point_widget.xml" />
</feature>
- <feature id="Axis" title="Axis" tooltip="Create a new axis" icon=":icons/axis.png" keysequence="" internal="true">
- <!-- validator id="inside" parameters="a,b"/ -->
- </feature>
- <feature id="Plane" title="Plane" tooltip="Create a new plane" icon=":icons/plane.png" keysequence="" internal="true"/>
+ <feature
+ id="Axis"
+ title="Axis"
+ tooltip="Create a new axis"
+ icon=":icons/axis.png"
+ keysequence=""
+ internal="true" />
+ <feature
+ id="Plane"
+ title="Plane"
+ tooltip="Create a new plane"
+ icon=":icons/plane.png"
+ keysequence=""
+ internal="true" />
</group>
-</workbench>
-</plugin>
+ </workbench>
+</plugin>
\ No newline at end of file
#include <ExchangePlugin_Plugin.h>
#include <ExchangePlugin_ImportFeature.h>
+#include <ExchangePlugin_Validators.h>
#include <ModelAPI_Session.h>
+#include <ModelAPI_Validator.h>
#include <boost/smart_ptr/shared_ptr.hpp>
ExchangePlugin_Plugin::ExchangePlugin_Plugin()
{
// register this plugin
- ModelAPI_Session::get()->registerPlugin(this);
+ SessionPtr aSession = ModelAPI_Session::get();
+ aSession->registerPlugin(this);
+ ModelAPI_ValidatorsFactory* aFactory = aSession->validators();
+ aFactory->registerValidator("ExchangePlugin_ImportFormat",
+ new ExchangePlugin_ImportFormatValidator);
}
FeaturePtr ExchangePlugin_Plugin::createFeature(string theFeatureID)
#include <ModelAPI_Feature.h>
#include <ModelAPI_Object.h>
#include <ModelAPI_Session.h>
+#include <ModelAPI_AttributeString.h>
#include <list>
#include <string>
+#include <algorithm>
+bool ExchangePlugin_ImportFormatValidator::parseFormats(const std::list<std::string>& theArguments,
+ std::list<std::string>& outFormats)
+{
+ std::list<std::string>::const_iterator it = theArguments.begin();
+ bool result = true;
+ for (; it != theArguments.end(); ++it) {
+ std::string anArg = *it;
+ int aSepPos = anArg.find(":");
+ if (aSepPos == std::string::npos) {
+ result = false;
+ continue;
+ }
+ std::string aFormat = anArg.substr(0, aSepPos);
+ std::transform(aFormat.begin(), aFormat.end(), aFormat.begin(), toupper);
+ outFormats.push_back(aFormat);
+ }
+ return result;
+}
+
+bool ExchangePlugin_ImportFormatValidator::parsePlugins(const std::list<std::string>& theArguments,
+ std::list<std::string>& outPlugins)
+{
+ std::list<std::string>::const_iterator it = theArguments.begin();
+ bool result = true;
+ for (; it != theArguments.end(); ++it) {
+ std::string anArg = *it;
+ int aSepPos = anArg.find(":");
+ if (aSepPos == std::string::npos) {
+ result = false;
+ continue;
+ }
+ outPlugins.push_back(anArg.substr(aSepPos + 1));
+ }
+ return result;
+}
-bool ExchangePlugin_ImportFormatValidator::isValid(
- const AttributePtr& theAttribute, const std::list<std::string>& theArguments) const
+bool ExchangePlugin_ImportFormatValidator::isValid(const AttributePtr& theAttribute,
+ const std::list<std::string>& theArguments) const
{
SessionPtr aMgr = ModelAPI_Session::get();
ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
+ if (theAttribute->isInitialized()) {
+ const AttributeStringPtr aStrAttr =
+ boost::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttribute);
+ if(!aStrAttr)
+ return false;
+ std::string aFileName = aStrAttr->value();
+ if (!aFileName.empty()) {
+ std::list<std::string> aFormats;
+ ExchangePlugin_ImportFormatValidator::parseFormats(theArguments, aFormats);
+ std::list<std::string>::const_iterator itFormats = aFormats.begin();
+ size_t aFileNameLen = aFileName.length();
+ std::transform(aFileName.begin(), aFileName.end(), aFileName.begin(), toupper);
+ // Is file name ends with the format
+ for (; itFormats != aFormats.end(); ++itFormats) {
+ size_t aFormatBeginPos = aFileNameLen - (*itFormats).length();
+ if (aFileName.compare(aFormatBeginPos, std::string::npos, *itFormats) == 0) {
+ return true;
+ }
+ }
+ }
+ }
return false;
}
class ExchangePlugin_ImportFormatValidator : public ModelAPI_AttributeValidator
{
+ /*
+ * Parses input arguments "BREP:BREPImport", "STEP:STEPImport"
+ * into list of file formats "BREP","STEP"
+ * and list of corresponding plugins: "BREPImport", "STEPImport"
+ */
+ static bool parseFormats(const std::list<std::string>& theArguments,
+ std::list<std::string>& outFormats);
+ static bool parsePlugins(const std::list<std::string>& theArguments,
+ std::list<std::string>& outPlugins);
public:
- virtual bool isValid(
- const AttributePtr& theAttribute, const std::list<std::string>& theArguments) const;
+ virtual bool isValid(const AttributePtr& theAttribute,
+ const std::list<std::string>& theArguments) const;
+
+
};
<plugin>
<workbench id="Features" document="Part">
<group id="Exchange">
- <feature
- id="Import"
- title="Import"
- tooltip="Import shape"
- icon=":icons/import.png">
+ <feature id="Import" title="Import" tooltip="Import shape" icon=":icons/import.png">
<file_selector
id="import_file_selector"
title="Import file"
- path=""
- formats="BREP,STEP" />
- <!-- TODO: pass formats as validator
- <validator id="Format" parameters="BREP:BREPImport,STEP:STEPImport" />
- -->
+ path="">
+ <validator id="ExchangePlugin_ImportFormat" parameters="BREP:BREPImport,STEP:STEPImport" />
+ </file_selector>
</feature>
</group>
</workbench>
-</plugin>
+</plugin>
\ No newline at end of file
//! returns true if attribute is valid
//! \param theAttribute the checked attribute
//! \param theArguments arguments of the attribute
- GEOMVALIDATORS_EXPORT virtual bool isValid(
- const AttributePtr& theAttribute, const std::list<std::string>& theArguments) const;
+ GEOMVALIDATORS_EXPORT virtual bool isValid(const AttributePtr& theAttribute,
+ const std::list<std::string>& theArguments) const;
};
std::list<std::string>::iterator anAttrIter = aLtAttributes.begin();
for (; anAttrIter != aLtAttributes.end(); anAttrIter++) {
std::map<std::string, AttrValidators>::const_iterator anAttr =
- aFeatureIter->second.find(*anAttrIter);
+ aFeatureIter->second.find(*anAttrIter);
if (anAttr != aFeatureIter->second.end()) {
AttrValidators::const_iterator aValIter = anAttr->second.cbegin();
for (; aValIter != anAttr->second.cend(); aValIter++) {
const ModelAPI_AttributeValidator* anAttrValidator =
dynamic_cast<const ModelAPI_AttributeValidator*>(aFound->second);
if (anAttrValidator) {
- if (!anAttrValidator->isValid(theFeature->data()->attribute(*anAttrIter),
- aValIter->second)) {
+ AttributePtr anAttribute = theFeature->data()->attribute(*anAttrIter);
+ if (!anAttrValidator->isValid(anAttribute, aValIter->second)) {
return false;
}
}
aValidator->registerNotObligatory(theFeature, theAttribute);
}
}
-}
\ No newline at end of file
+}
//! returns true if attribute is valid
//! \param theAttribute the checked attribute
//! \param theArguments arguments of the attribute
- virtual bool isValid(
- const AttributePtr& theAttribute, const std::list<std::string>& theArguments) const = 0;
+ virtual bool isValid(const AttributePtr& theAttribute,
+ const std::list<std::string>& theArguments) const = 0;
};
#endif
#include <ModelAPI_AttributeString.h>
#include <ModelAPI_Data.h>
#include <ModelAPI_Object.h>
+#include <ModelAPI_Validator.h>
#include <ModuleBase_WidgetFileSelector.h>
#include <ModuleBase_Tools.h>
: ModuleBase_ModelWidget(theParent, theData, theParentId)
{
myTitle = QString::fromStdString(theData->getProperty("title"));
- //TODO(sbh): Get them from the feature
- myFormats = getSupportedFormats(theData);
myDefaultPath = QString::fromStdString(theData->getProperty("path"));
myMainWidget = new QWidget(theParent);
bool ModuleBase_WidgetFileSelector::storeValue() const
{
+ // A rare case when plugin was not loaded.
+ if(!myFeature)
+ return false;
DataPtr aData = myFeature->data();
AttributeStringPtr aStringAttr = aData->string(attributeID());
QString aWidgetValue = myPathField->text();
bool ModuleBase_WidgetFileSelector::restoreValue()
{
+ // A rare case when plugin was not loaded.
+ if(!myFeature)
+ return false;
DataPtr aData = myFeature->data();
AttributeStringPtr aStringAttr = aData->string(attributeID());
bool ModuleBase_WidgetFileSelector::isCurrentPathValid()
{
QFileInfo aFile (myPathField->text());
- return aFile.exists() && myFormats.contains(aFile.suffix(), Qt::CaseInsensitive);
+ return aFile.exists();
}
void ModuleBase_WidgetFileSelector::onPathSelectionBtn()
{
- QString aFilter = formatsString(myFormats);
+ QString aFilter = formatsString();
QString aFileName = QFileDialog::getOpenFileName(myMainWidget, myTitle, myDefaultPath, aFilter);
if (!aFileName.isEmpty()) {
myPathField->setText(aFileName);
emit valuesChanged();
}
-QStringList ModuleBase_WidgetFileSelector::getSupportedFormats(const Config_WidgetAPI* theData) const
-{
- QString aXMLFormat = QString::fromStdString(theData->getProperty("formats"));
- aXMLFormat = aXMLFormat.toUpper();
- const QChar kSep = ',';
- return aXMLFormat.split(kSep, QString::SkipEmptyParts);
-}
-
-QString ModuleBase_WidgetFileSelector::formatsString(const QStringList theFormats) const
+QString ModuleBase_WidgetFileSelector::formatsString() const
{
QStringList aResult;
- foreach(QString eachFormat, theFormats) {
+ QStringList aValidatorFormats = getValidatorFormats();
+
+ foreach(QString eachFormat, aValidatorFormats) {
aResult << QString("%1 files (*.%1)").arg(eachFormat);
}
+ aResult << QString("All files (*.*)");
return aResult.join(";;");
}
+QStringList ModuleBase_WidgetFileSelector::getValidatorFormats() const
+{
+ SessionPtr aMgr = ModelAPI_Session::get();
+ ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
+ std::list<ModelAPI_Validator*> allValidators;
+ std::list<std::list<std::string> > allArguments;
+ aFactory->validators(myFeature->getKind(), myAttributeID, allValidators, allArguments);
+ //TODO(sbh): extract as separate method
+ if(allArguments.empty())
+ return QStringList();
+ std::list<std::string> anArgumentList = allArguments.front();
+ std::list<std::string>::const_iterator it = anArgumentList.begin();
+ QStringList aResult;
+ for (; it != anArgumentList.end(); ++it) {
+ std::string anArg = *it;
+ int aSepPos = anArg.find(":");
+ if (aSepPos == std::string::npos) {
+ continue;
+ }
+ QString aFormat = QString::fromStdString(anArg.substr(0, aSepPos));
+ aFormat = aFormat.toUpper();
+ aResult.append(aFormat);
+ }
+ return aResult;
+}
void onPathChanged();
protected:
- QStringList getSupportedFormats(const Config_WidgetAPI* theData) const;
- QString formatsString(const QStringList theFormats) const;
+ QString formatsString() const;
+ QStringList getValidatorFormats() const;
private:
QLineEdit* myPathField;
QWidget* myMainWidget;
- QStringList myFormats;
QString myTitle;
QString myDefaultPath;