Commit db5843b8 authored by cfouard's avatar cfouard
Browse files

FIXED Wizard now uses all possible QVariants as Properties

git-svn-id: svn+ssh://scm.forge.imag.fr/var/lib/gforge/chroot/scmrepos/svn/camitk/trunk/camitk@94 ec899d31-69d1-42ba-9299-647d76f65fb3
parent 1be37dcb
......@@ -26,10 +26,16 @@ void ActionGenerator::createElement() throw (QString) {
this->additionalIncludes.push_back(QString("QString"));
this->additionalIncludes.push_back(QString("QTextStream"));
this->additionalIncludes.push_back(QString("QMessageBox"));
//QString compClass = "";
//compClass += this->theAction->classNames().componentClass().c_str();
//compClass += ".h";
//this->additionalIncludes.push_back(compClass);
if (hasParameters) {
for (Parameters::parameter_const_iterator it = theParams.begin(); it < theParams.end(); it++) {
QString parameterType = QString((*it).type().c_str());
if ((parameterType != "int") && (parameterType != "bool") && (parameterType != "double")) {
this->additionalIncludes.push_back(parameterType);
}
}
}
}
void ActionGenerator::specialHDeclare() {
......@@ -151,22 +157,22 @@ void ActionGenerator::writeApply() {
(*out) << "void " << elementClassName << "::apply(" << theComponent << " * comp) {" << endl;
if (hasParameters) {
(*out) << " // Get the parameters" << endl;
for (Parameters::parameter_const_iterator it = theParams.begin(); it < theParams.end(); it++) {
QString parameterName = QString((*it).name().c_str());
QString parameterType = QString((*it).type().c_str());
QString parameterTo;
if(parameterType == "int")
parameterTo = "toInt";
else if (parameterType == "bool")
parameterTo = "toBool";
else if (parameterType == "double")
parameterTo = "toDouble";
else if (parameterType == "String")
parameterTo = "toString";
else
parameterTo = "value<" + parameterType + ">";
(*out) << " " << parameterType << " " << parameterName << " = property(\"" << parameterName << "\")." << parameterTo << "();" << endl;
}
for (Parameters::parameter_const_iterator it = theParams.begin(); it < theParams.end(); it++) {
QString parameterName = QString((*it).name().c_str());
QString parameterType = QString((*it).type().c_str());
QString parameterTo;
if(parameterType == "int")
parameterTo = "toInt";
else if (parameterType == "bool")
parameterTo = "toBool";
else if (parameterType == "double")
parameterTo = "toDouble";
else if (parameterType == "String")
parameterTo = "toString";
else
parameterTo = "value<" + parameterType + ">";
(*out) << " " << parameterType << " " << parameterName << " = property(\"" << parameterName << "\")." << parameterTo << "();" << endl;
}
}
(*out) << " // For this example, the action just pops up a message ";
......@@ -187,7 +193,17 @@ void ActionGenerator::writeApply() {
if ((parameterType == "int") || (parameterType == "double") || (parameterType == "bool") || (parameterType == "QString")) {
(*out) << parameterName;
}
else
else if (parameterType == "QDate")
(*out) << parameterName << ".year() << \"/\" << " << parameterName << ".month() << \"/\" << " << parameterName << ".day() ";
else if (parameterType == "QTime")
(*out) << parameterName << ".hour() << \":\" << "<< parameterName << ".minute()";
else if (parameterType == "QColor")
(*out) << parameterName << ".red() << \", \" << " << parameterName << ".green() << \", \" << " << parameterName << ".blue()";
else if ((parameterType == "QPoint") || (parameterType == "QPointF"))
(*out) << "\"(\" << " << parameterName << ".x() << \", \" << " << parameterName << ".y() << \")\"";
else if (parameterType == "QVector3D")
(*out) << "\"(\" << " << parameterName << ".x() << \", \" << " << parameterName << ".y() << \", \" << " << parameterName << ".z() << \")\" ";
else
(*out) << "\"???\"";
(*out) << " << endl;" << endl;
}
......
......@@ -20,6 +20,16 @@ void ComponentGenerator::createElement() throw (QString) {
this->hasParameters = theComponent->properties().present();
if (this->hasParameters)
this->theParams = theComponent->properties().get().parameter();
if (hasParameters) {
for (Parameters::parameter_const_iterator it = theParams.begin(); it < theParams.end(); it++) {
QString parameterType = QString((*it).type().c_str());
if ((parameterType != "int") && (parameterType != "bool") && (parameterType != "double")) {
this->additionalIncludes.push_back(parameterType);
}
}
}
QString tmp(theComponent->representation().c_str());
if (tmp == QString("Image"))
......
#include "ElementGenerator.h"
// Includes from Qt
#include <QTextStream>
// includes from std
#include <iostream>
#include <Parameter.hxx>
#include <ParameterType.hxx>
using namespace coreschema;
ElementGenerator::ElementGenerator(QString xmlFilename, QString devDirectoryName) {
out = NULL;
name = QString("element");
elementClassName = QString("");
parentClass = QString("");
// hasImplementationFile = false;
hasParameters = false;
hasSpecialHDeclare = false;
theParams = Parameters::parameter_sequence();
setXmlFileName(xmlFilename);
setDevDirectoryName(devDirectoryName);
}
void ElementGenerator::setXmlFileName(QString xmlFileName) throw (QString) {
QFileInfo xmlFile(xmlFileName);
if ((! xmlFile.exists()) || (! xmlFile.isFile())) {
QString msg = "Exception from " + name + " generation: \n The file " + xmlFileName + " does not exist or is not a file...\n";
throw (msg);
}
this->xmlFileName = xmlFile;
// TODO manage exception (despite Qt...).
// TODO find how to read an xml file with an absolute path !!!
std::string xmlFileStr = this->xmlFileName.canonicalFilePath().toStdString();
}
void ElementGenerator::setDevDirectoryName(QString devDirectoryName) throw (QString) {
QFileInfo devDir(devDirectoryName);
if ( ! devDir.isDir()) {
QString msg = "Exception from action generation: \n The path " + devDirectoryName + " is not a directory\n";
throw (msg);
}
this->devDirectoryName.cd(devDirectoryName);
}
void ElementGenerator::generateElementFiles() throw (QString) {
// Generating .h
QFileInfo extFileHPath;
extFileHPath.setFile(this->devDirectoryName, elementClassName + ".h");
QFile extFileH(extFileHPath.absoluteFilePath());
if (! extFileH.open(QIODevice::WriteOnly | QIODevice::Text)) {
QString msg = "Exception from " + name + " generation: \n Cannot write on file " + extFileHPath.fileName() + "\n";
throw (msg);
}
out = new QTextStream(&extFileH);
writeElementHFile();
extFileH.close();
delete out;
out = NULL;
/*
// Generating _implementation.cpp
if (hasImplementationFile) {
QFileInfo extFileIPath;
extFileIPath.setFile(this->devDirectoryName, elementClassName + "_implementation.cpp");
QFile extFileI(extFileIPath.absoluteFilePath());
if (! extFileI.open(QIODevice::WriteOnly | QIODevice::Text)) {
QString msg = "Exception from " + name + " generation: \n Cannot write on file " + extFileIPath.fileName() + "\n";
throw (msg);
}
out = new QTextStream(&extFileI);
writeImplementationCFile();
extFileI.close();
delete out;
out = NULL;
}
*/
// Generating .cpp
QFileInfo extFileCPath;
extFileCPath.setFile(this->devDirectoryName, elementClassName + ".cpp");
QFile extFileC(extFileCPath.absoluteFilePath());
if (! extFileC.open(QIODevice::WriteOnly | QIODevice::Text)) {
QString msg = "Exception from " + name + " generation: \n Cannot write on file " + extFileCPath.fileName() + "\n";
throw (msg);
}
out = new QTextStream(&extFileC);
writeElementCFile();
extFileC.close();
delete out;
out = NULL;
}
void ElementGenerator::writeDeclareDestructor() {
(*out) << " /// Default Destructor" << endl;
(*out) << " virtual ~" << elementClassName << "();" << endl;
(*out) << endl;
}
void ElementGenerator::writeElementHFile() {
QString headdef = elementClassName.toUpper() + "_H";
(*out) << "#ifndef " << headdef << endl;
(*out) << "#define " << headdef << endl;
(*out) << endl;
(*out) << "#include <QObject>" << endl;
(*out) << "#include <" << parentClass << ".h>" << endl;
if (hasParameters)
(*out) << "#include <ObjectController.h>" << endl;
if (hasSpecialHDeclare) {
specialHDeclare();
}
(*out) << endl;
(*out) << "using namespace camitk;" << endl;
(*out) << endl;
(*out) << "class " << elementClassName << " : public " << parentClass <<" {" << endl;
(*out) << endl;
/*
if (hasParameters)
writeDeclareQProperties();
*/
(*out) << endl;
(*out) << "public: " << endl;
writeDeclareConstructor();
writeDeclareDestructor();
writeDeclareSpecificMethods();
/*
if (hasParameters) {
writeDeclareParametersAccessors();
(*out) << "protected: " << endl;
writeDeclareParametersAttributes();
}
*/
(*out) << "}; " << endl;
(*out) << "#endif // " << headdef << endl;
}
void ElementGenerator::writeElementCFile() {
(*out) << "#include \"" << elementClassName <<".h\"" << endl;
(*out) << endl;
for (std::vector<QString>::const_iterator it = this->additionalIncludes.begin();
it < additionalIncludes.end(); it++)
(*out) << "#include <" << (*it) << "> " << endl;
(*out) << endl;
(*out) << "using namespace camitk;" << endl;
(*out) << endl;
(*out) << endl;
writeConstructor();
writeDestructor();
writeSpecificCMethods();
/*
if(hasParameters)
writeParametersAccessors();
*/
}
/*
void ElementGenerator::writeDeclareQProperties() {
(*out) << " Q_OBJECT" << endl;
(*out) << endl << endl;
(*out) <<" // declaration of parameters as QProperties" << endl;
for (Parameters::parameter_const_iterator it = theParams.begin(); it < theParams.end(); it++)
{
QString parameterName = QString((*it).name().c_str());
(*out) << " Q_PROPERTY( " ;
(*out) << (*it).type().c_str() << " " << parameterName << " ";
// Set the first letter of the parameter name in Upper case
parameterName[0] = parameterName[0].toUpper();
(*out) << "READ " << "get" << parameterName ;
if ( (*it).editable() == true)
(*out) << " WRITE set" << parameterName;
(*out) << ")" << endl;
}
(*out) << endl;
}
*/
/*
void ElementGenerator::writeDeclareParametersAttributes() {
for (Parameters::parameter_const_iterator it = theParams.begin(); it < theParams.end(); it++)
{
QString parameterName = QString((*it).name().c_str());
QString parameterType = QString((*it).type().c_str());
(*out) << " " << parameterType << " " << parameterName << ";" << endl;
}
}
*/
void ElementGenerator::writeParametersInitialization() {
if (hasParameters) {
(*out) << " // Setting parameters default values" << endl;
for (Parameters::parameter_const_iterator it = theParams.begin(); it < theParams.end(); it++) {
QString parameterName = QString((*it).name().c_str());
QString parameterType = QString((*it).type().c_str());
QString parameterVal;
if ((*it).defaultValue().present()) {
QString parameterVal = QString((*it).defaultValue()->c_str());
}
else {
if(parameterType == "int")
parameterVal = "0";
else if (parameterType == "bool")
parameterVal = "false";
else if (parameterType == "double")
parameterVal="0.0";
else if (parameterType == "String")
parameterVal="\"\"";
else if (parameterType == "QVector3D")
parameterVal="QVector3D(0.0, 0.0,0.0)";
else
parameterVal="";
}
(*out) << " " << "setProperty(\"" << parameterName << "\", QVariant(" << parameterVal << "));" << endl;
}
}
}
/*
void ElementGenerator::writeDeclareParametersAccessors() {
(*out) << " // --------------- parameters accessors ----" << endl;
for (Parameters::parameter_const_iterator it = theParams.begin(); it < theParams.end(); it++)
{
QString parameterName = QString((*it).name().c_str());
QString parameterType = QString((*it).type().c_str());
QString nameUpper = parameterName; nameUpper[0] = nameUpper[0].toUpper();
(*out) << " " << parameterType << " get" << nameUpper << "() const ;" << endl;
if ( (*it).editable() == true)
(*out) << " " << "void" << " set" << nameUpper <<"(const " << parameterType << " " << parameterName << ");" << endl;
(*out) << endl;
}
}
void ElementGenerator::writeParametersAccessors() {
(*out) << " // Setters and Getters for parameters" << endl;
for (Parameters::parameter_const_iterator it = theParams.begin(); it < theParams.end(); it++)
{
QString parameterName = QString((*it).name().c_str());
QString parameterType = QString((*it).type().c_str());
QString nameUpper = parameterName; nameUpper[0] = nameUpper[0].toUpper();
(*out) << parameterType << " " << elementClassName << "::get" << nameUpper << "() const {" << endl;
(*out) << " return " << parameterName << ";" << endl;
(*out) << "}" << endl;
(*out) << endl;
if ( (*it).editable() == true) {
(*out) << "void" << " " << elementClassName << "::set" << nameUpper <<"(const " << parameterType << " " << parameterName << ") {" << endl;
(*out) << " this->" << parameterName << " = " << parameterName << ";" << endl;
(*out) << "}" << endl;
}
(*out) << endl;
}
}
*/
#include "ElementGenerator.h"
// Includes from Qt
#include <QTextStream>
#include <QDate>
#include <QTime>
// includes from std
#include <iostream>
#include <Parameter.hxx>
#include <ParameterType.hxx>
using namespace coreschema;
ElementGenerator::ElementGenerator(QString xmlFilename, QString devDirectoryName) {
out = NULL;
name = QString("element");
elementClassName = QString("");
parentClass = QString("");
hasParameters = false;
hasSpecialHDeclare = false;
theParams = Parameters::parameter_sequence();
setXmlFileName(xmlFilename);
setDevDirectoryName(devDirectoryName);
}
void ElementGenerator::setXmlFileName(QString xmlFileName) throw (QString) {
QFileInfo xmlFile(xmlFileName);
if ((! xmlFile.exists()) || (! xmlFile.isFile())) {
QString msg = "Exception from " + name + " generation: \n The file " + xmlFileName + " does not exist or is not a file...\n";
throw (msg);
}
this->xmlFileName = xmlFile;
// TODO manage exception (despite Qt...).
// TODO find how to read an xml file with an absolute path !!!
std::string xmlFileStr = this->xmlFileName.canonicalFilePath().toStdString();
}
void ElementGenerator::setDevDirectoryName(QString devDirectoryName) throw (QString) {
QFileInfo devDir(devDirectoryName);
if ( ! devDir.isDir()) {
QString msg = "Exception from action generation: \n The path " + devDirectoryName + " is not a directory\n";
throw (msg);
}
this->devDirectoryName.cd(devDirectoryName);
}
void ElementGenerator::generateElementFiles() throw (QString) {
// Generating .h
QFileInfo extFileHPath;
extFileHPath.setFile(this->devDirectoryName, elementClassName + ".h");
QFile extFileH(extFileHPath.absoluteFilePath());
if (! extFileH.open(QIODevice::WriteOnly | QIODevice::Text)) {
QString msg = "Exception from " + name + " generation: \n Cannot write on file " + extFileHPath.fileName() + "\n";
throw (msg);
}
out = new QTextStream(&extFileH);
writeElementHFile();
extFileH.close();
delete out;
out = NULL;
// Generating .cpp
QFileInfo extFileCPath;
extFileCPath.setFile(this->devDirectoryName, elementClassName + ".cpp");
QFile extFileC(extFileCPath.absoluteFilePath());
if (! extFileC.open(QIODevice::WriteOnly | QIODevice::Text)) {
QString msg = "Exception from " + name + " generation: \n Cannot write on file " + extFileCPath.fileName() + "\n";
throw (msg);
}
out = new QTextStream(&extFileC);
writeElementCFile();
extFileC.close();
delete out;
out = NULL;
}
void ElementGenerator::writeDeclareDestructor() {
(*out) << " /// Default Destructor" << endl;
(*out) << " virtual ~" << elementClassName << "();" << endl;
(*out) << endl;
}
void ElementGenerator::writeElementHFile() {
QString headdef = elementClassName.toUpper() + "_H";
(*out) << "#ifndef " << headdef << endl;
(*out) << "#define " << headdef << endl;
(*out) << endl;
(*out) << "#include <QObject>" << endl;
(*out) << "#include <" << parentClass << ".h>" << endl;
if (hasParameters)
(*out) << "#include <ObjectController.h>" << endl;
if (hasSpecialHDeclare) {
specialHDeclare();
}
(*out) << endl;
(*out) << "using namespace camitk;" << endl;
(*out) << endl;
(*out) << "class " << elementClassName << " : public " << parentClass <<" {" << endl;
(*out) << endl;
(*out) << endl;
(*out) << "public: " << endl;
writeDeclareConstructor();
writeDeclareDestructor();
writeDeclareSpecificMethods();
(*out) << "}; " << endl;
(*out) << "#endif // " << headdef << endl;
}
void ElementGenerator::writeElementCFile() {
(*out) << "#include \"" << elementClassName <<".h\"" << endl;
(*out) << endl;
for (std::vector<QString>::const_iterator it = this->additionalIncludes.begin();
it < additionalIncludes.end(); it++)
(*out) << "#include <" << (*it) << "> " << endl;
(*out) << endl;
(*out) << "using namespace camitk;" << endl;
(*out) << endl;
(*out) << endl;
writeConstructor();
writeDestructor();
writeSpecificCMethods();
}
void ElementGenerator::writeParametersInitialization() {
if (hasParameters) {
(*out) << " // Setting parameters default values" << endl;
for (Parameters::parameter_const_iterator it = theParams.begin(); it < theParams.end(); it++) {
QString parameterName = QString((*it).name().c_str());
QString parameterType = QString((*it).type().c_str());
QString parameterVal;
if ((*it).defaultValue().present()) {
parameterVal = QString((*it).defaultValue()->c_str());
}
else {
if (parameterType == "int")
parameterVal = "0";
else if (parameterType == "bool")
parameterVal = "false";
else if (parameterType == "double")
parameterVal = "0.0";
else if (parameterType == "QString")
parameterVal = "\"Hello World\"";
else if (parameterType == "QDate") {
QDate today = QDate::currentDate();
QString defValue;
QTextStream in(&defValue);
in << today.year() << ", " << today.month() << ", " << today.day();
parameterVal = defValue;
}
else if (parameterType == "QTime") {
QTime now = QTime::currentTime();
QString defValue;
QTextStream in(&defValue);
in << now.hour() << ", " << now.minute() << ", " << now.second();
parameterVal = defValue;
}
else if (parameterType == "QColor")
parameterVal = "255, 255, 255, 255";
else if (parameterType == "QPoint")
parameterVal = "0, 0";
else if (parameterType == "QPointF")
parameterVal = "0.0, 0.0";
else if (parameterType == "QVector3D")
parameterVal = "0.0, 0.0, 0.0";
}
(*out) << " " << "setProperty(\"" << parameterName << "\", QVariant(";
if ((parameterType == "int") || (parameterType == "double") || (parameterType == "bool") || (parameterType == "QString"))
(*out) << parameterVal << "));" << endl;
else
(*out) << parameterType << "(" << parameterVal << ")));" << endl;
}
}
}
......@@ -864,11 +864,6 @@ p, li { white-space: pre-wrap; }
<string>Mesh</string>
</property>
</item>
<item>
<property name="text">
<string>Other</string>
</property>
</item>
<item>
<property name="text">
<string>None</string>
......