Beispiel #1
0
TEST(OptionsTest, test_option_writing)
{
    std::ostringstream ostr_i;
    const std::string ref_i = xml_header + xml_int_ref;
    std::ostringstream ostr_s;
    const std::string ref_s = xml_header + xml_str_ref;

    const Option option_i("my_int", (uint16_t)17,
        "This is my integral option.");
    EXPECT_TRUE(option_i.getName() == "my_int");
    EXPECT_TRUE(option_i.getDescription() == "This is my integral option.");
    EXPECT_TRUE(option_i.getValue<uint16_t>() == 17);
    EXPECT_TRUE(option_i.getValue<std::string>() == "17");

    const Option option_s("my_string", "Yow.", "This is my stringy option.");
    EXPECT_TRUE(option_s.getName() == "my_string");
    EXPECT_TRUE(option_s.getDescription() == "This is my stringy option.");
    EXPECT_TRUE(option_s.getValue<std::string>() == "Yow.");
    EXPECT_TRUE(option_s.getValue<std::string>() == "Yow.");

//     const pdalboost::property_tree::ptree tree_i = Utils::toPTree(option_i);
//     pdalboost::property_tree::xml_parser::write_xml(ostr_i, tree_i);
//     const std::string str_i = ostr_i.str();
//     EXPECT_TRUE(str_i == ref_i);
//
//     const pdalboost::property_tree::ptree tree_s = Utils::toPTree(option_s);
//     pdalboost::property_tree::xml_parser::write_xml(ostr_s, tree_s);
//     const std::string str_s = ostr_s.str();
//     EXPECT_TRUE(str_s == ref_s);
}
Beispiel #2
0
/// @name Equality
    /// Equality
    bool equals(const Option& rhs) const
    {
        return (m_name == rhs.getName() &&
            m_value == rhs.getValue<std::string>() &&
            m_description == rhs.getDescription() &&
            m_options == rhs.m_options);
    }
Beispiel #3
0
inline ptree toPTree(const Option& option)
{
    ptree t;
    t.put("Name", option.getName());
    t.put("Value", option.getValue<std::string>());
    if (option.getDescription() != "")
    {
        t.put("Description", option.getDescription());
    }
    boost::optional<Options const&> options = option.getOptions();

    if (options != boost::none)
    {
        t.add_child("Options", toPTree(*options));
    }
    return t;
}
Beispiel #4
0
//! The (overloaded) initializeControl routine is responsible for the following
//! tasks:
//!  - Ensuring the control displays the appropriate options based on the
//!    contents of the OptionDatabase.
//!  - Adding the ToolTip documentation found in the database.
//!  - Adding connections (signals & slots) between the Nodes in the
//!    OptionRegister and the control.  This allows for synchronization of the
//!    logic in InitializeQChemLogic and initializeQuiLogic.
//!  - Binding an Action to enable the control to be reset to the default value.
//!  - Binding an Update to enable the control to be reset to a string value.
void InputDialog::initializeControl(Option const& opt, QComboBox* combo) {

   QString name = opt.getName();
   QStringList opts = opt.getOptions();
   QStringList split;

   // This allows for ad hoc text replacements.  This is useful so that more
   // informative text can be presented to the user which is then obfiscated
   // before being passed to QChem.  The replacements should be set in the
   // option database and have the form text//replacement.
   for (int i = 0; i < opts.size(); ++i) {
       split = opts[i].split("//");

       if (split.size() == 1) {
          opts[i] = split[0];
       }else if (split.size() == 2) {
          opts[i] = split[0];
          QString key(name.toUpper() + "::" + split[0]);
          //Job::addAdHoc(key,split[1]);
          RemSection::addAdHoc(name, split[0], split[1]);
       }else {
          qDebug() << "InputDialog::initialiseComboBox:\n"
                   << " replacement for option" << name << "is invalid:" << opts[i];
       }
   }

   combo->clear();
   combo->addItems(opts);

#if QT_VERSION >= 0x040400
   // This just allows us to add some spacers to the lists
   bool keepLooking(true);
   while (keepLooking) {
      int i = combo->findText("---", Qt::MatchStartsWith);
      if (i > 0) {
         combo->removeItem(i);
         combo->insertSeparator(i);
      }else {
         keepLooking = false;
      }
   }
#endif

   connectControl(opt, combo);
   combo->setToolTip(opt.getDescription());

   Action* action = new Action(
      boost::bind(&QComboBox::setCurrentIndex, combo, opt.getDefaultIndex()) );
   m_resetActions.push_back(action);

   Update* update = new Update(
      boost::bind(
         static_cast<void(*)(QComboBox*, QString const&)>(SetControl), combo, _1));
   m_setUpdates[name] = update;
}
void
OptionsCont::writeConfiguration(std::ostream &os, bool filled,
                                bool complete, bool addComments) throw() {
    std::vector<std::string>::const_iterator i, j;
    os << "<configuration>" << std::endl << std::endl;
    for (i=mySubTopics.begin(); i!=mySubTopics.end(); ++i) {
        std::string subtopic = *i;
        if (subtopic=="Configuration") {
            continue;
        }
        for (size_t k=0; k<subtopic.length(); ++k) {
            if (subtopic[k]==' ') {
                subtopic[k] = '_';
            }
            if (subtopic[k]>='A'&&subtopic[k]<='Z') {
                subtopic[k] = subtopic[k] - 'A' + 'a';
            }
        }
        const std::vector<std::string> &entries = mySubTopicEntries[*i];
        bool hadOne = false;
        for (j=entries.begin(); j!=entries.end(); ++j) {
            Option *o = getSecure(*j);
            bool write = complete || (filled&&!o->isDefault());
            if (!write) {
                continue;
            }
            if (!hadOne) {
                os << "    <" << subtopic << ">" << std::endl;
            }
            // add the comment if wished
            if (addComments) {
                os << "        <!-- " << o->getDescription() << " -->" << std::endl;
            }
            // write the option and the value (if given)
            os << "        <" << *j << " value=\"";
            if (o->isSet()) {
                os << o->getValueString();
            }
            os << "\"/>" << std::endl;
            // append an endline if a comment was printed
            if (addComments) {
                os << std::endl;
            }
            hadOne = true;
        }
        if (hadOne) {
            os << "    </" << subtopic << ">" << std::endl << std::endl;
        }
    }
    os << "</configuration>" << std::endl;
}
Beispiel #6
0
void InputDialog::initializeControl(Option const& opt, QLineEdit* edit) {
   connectControl(opt, edit);
   edit->setToolTip(opt.getDescription());

   Action* action = new Action(
      boost::bind(&QLineEdit::setText, edit, opt.getOptionString()) );
   m_resetActions.push_back(action);

   Update* update = new Update(
      boost::bind(
         static_cast<void(*)(QLineEdit*, QString const&)>(SetControl), edit, _1));
   QString name = opt.getName();
   m_setUpdates[name] = update;
}
Beispiel #7
0
void InputDialog::initializeControl(Option const& opt, QRadioButton* radio) {
   connectControl(opt, radio);
   radio->setToolTip(opt.getDescription());

   Action* action = new Action(
      boost::bind(&QRadioButton::setChecked, radio, opt.getDefaultIndex()) );
   m_resetActions.push_back(action);

   Update* update = new Update(
      boost::bind(
         static_cast<void(*)(QRadioButton*, QString const&)>(SetControl), radio, _1));
   QString name = opt.getName();
   m_setUpdates[name] = update;
}
Beispiel #8
0
void InputDialog::initializeControl(Option const& opt, QCheckBox* check) 
{
   connectControl(opt, check);
   check->setToolTip(prependRemName(opt.getName(), opt.getDescription()));

   Action* action = new Action(
      boost::bind(&QCheckBox::setChecked, check, opt.getDefaultIndex()) );
   m_resetActions.push_back(action);

   Update* update = new Update(
      boost::bind( 
         static_cast<void(*)(QCheckBox*, QString const&)>(SetControl), check, _1));
   QString name = opt.getName();
   m_setUpdates[name] = update;
}
Beispiel #9
0
void InputDialog::initializeControl(Option const& opt, QDoubleSpinBox* dspin) {
   connectControl(opt, dspin);
   dspin->setToolTip(opt.getDescription());
   dspin->setRange(opt.doubleMin(), opt.doubleMax());
   dspin->setSingleStep(opt.doubleStep());

   Action* action = new Action(
      boost::bind(&QDoubleSpinBox::setValue, dspin, opt.doubleDefault()) );
   m_resetActions.push_back(action);

   Update* update = new Update(
      boost::bind(
         static_cast<void(*)(QDoubleSpinBox*, QString const&)>(SetControl), dspin, _1));
   QString name = opt.getName();
   m_setUpdates[name] = update;
}
Beispiel #10
0
void InputDialog::initializeControl(Option const& opt, QLineEdit* edit) 
{
   connectControl(opt, edit);
   edit->setToolTip(prependRemName(opt.getName(), opt.getDescription()));

   Action* action = new Action(
      boost::bind(&QLineEdit::setText, edit, opt.getOptionString()) );
   m_resetActions.push_back(action);

   Update* update = new Update(
      boost::bind( 
         static_cast<void(*)(QLineEdit*, QString const&)>(SetControl), edit, _1));
   QString name = opt.getName();
   m_setUpdates[name] = update;

   if (opt.getType() == Option::Type_Array) {
      QRegExp rx("^\\[(\\d+,?)+\\]$");
      QValidator* validator(new QRegExpValidator(rx, this));
      edit->setValidator(validator);
      //edit->setMask("");
   }
}
Beispiel #11
0
//! Inserts an Option into the database, overwriting the record if the
//! option name already exists.  The user is prompted if an overwrite will
//! occur and if \var promptOnOverwrite is set to true.
bool OptionDatabase::insert(Option const& opt, bool const promptOnOverwrite)
{
    Option tmp;
    QString name(opt.getName());

    if ( get(name, tmp) ) {
        if (promptOnOverwrite) {
            QString msg("Option name ");
            msg += name;
            msg += " already exists in database, overwrite?";
            int ret = QMsgBox::question(0, "Option Exists",msg,
                                        QMessageBox::Ok | QMessageBox::Cancel);
            if (ret == QMessageBox::Cancel) {
                return false;
            }
        }
        remove(name,false);
    }

    QString buf("insert into options( 'Name', 'Type', 'Default', 'Options', "
                "'Description', 'Implementation' ) values ( '");
    buf += opt.getName() + "', ";
    buf += QString::number(opt.getType()) + ", ";
    buf += QString::number(opt.getDefaultIndex()) + ", '";
    buf += opt.getOptionString() + "', ";   // No quote!

    QSqlField desc("Description", QVariant::String);
    desc.setValue(opt.getDescription());
    buf += QSqlDatabase::database("QChem").driver()->formatValue(desc);
    buf += ", ";
    buf += QString::number(opt.getImplementation()) + ");";

    qDebug() << "Database insert: " << buf;

    return execute(buf);
}
void
OptionsCont::printHelp(std::ostream &os) throw() {
    std::vector<std::string>::const_iterator i, j;
    // print application description
    os << ' ' << std::endl;
    splitLines(os, myAppDescription , 0, 0);
    os << std::endl;
    // print usage BNF
    os << "Usage: " << myAppName << " [OPTION]*" << std::endl;
    os << ' ' << std::endl;
    // print usage examples
    if (myCallExamples.size()>1) {
        os << " Examples:" << std::endl;
    } else if (myCallExamples.size()!=0) {
        os << " Example:" << std::endl;
    }
    if (myCallExamples.size()!=0) {
        for (i=myCallExamples.begin(); i!=myCallExamples.end(); ++i) {
            os << "  " << myAppName << ' ' << (*i) << std::endl;
        }
    }
    os << ' ' << std::endl;
    // print additional text if any
    if (myAdditionalMessage.length()>0) {
        os << myAdditionalMessage << std::endl << ' ' << std::endl;
    }
    // print the options
    // check their sizes first
    //  we want to know how large the largest not-too-large-entry will be
    size_t tooLarge = 40;
    size_t maxSize = 0;
    for (i=mySubTopics.begin(); i!=mySubTopics.end(); ++i) {
        const std::vector<std::string> &entries = mySubTopicEntries[*i];
        for (j=entries.begin(); j!=entries.end(); ++j) {
            Option *o = getSecure(*j);
            // name, two leading spaces and "--"
            size_t csize = (*j).length() + 2 + 4;
            // abbreviation length ("-X, "->4chars) if any
            std::vector<std::string> synonymes = getSynonymes(*j);
            if (find_if(synonymes.begin(), synonymes.end(), abbreviation_finder())!=synonymes.end()) {
                csize += 4;
            }
            // the type name
            if (!o->isBool()) {
                csize += 1 + o->getTypeName().length();
            }
            // divider
            csize += 2;
            if (csize<tooLarge&&maxSize<csize) {
                maxSize = csize;
            }
        }
    }

    for (i=mySubTopics.begin(); i!=mySubTopics.end(); ++i) {
        os << ' ' << *i << " Options:" << std::endl;
        const std::vector<std::string> &entries = mySubTopicEntries[*i];
        for (j=entries.begin(); j!=entries.end(); ++j) {
            // start length computation
            size_t csize = (*j).length() + 2;
            Option *o = getSecure(*j);
            os << "  ";
            // write abbreviation if given
            std::vector<std::string> synonymes = getSynonymes(*j);
            std::vector<std::string>::iterator a = find_if(synonymes.begin(), synonymes.end(), abbreviation_finder());
            if (a!=synonymes.end()) {
                os << '-' << (*a) << ", ";
                csize += 4;
            }
            // write leading '-'/"--"
            os << "--";
            csize += 2;
            // write the name
            os << *j;
            // write the type if not a bool option
            if (!o->isBool()) {
                os << ' ' << o->getTypeName();
                csize += 1 + o->getTypeName().length();
            }
            csize += 2;
            // write the description formatting it
            os << "  ";
            size_t r;
            for (r=maxSize; r>csize; --r) {
                os << ' ';
            }
            std::string desc = o->getDescription();
            size_t offset = csize > tooLarge ? csize : maxSize;
            splitLines(os, desc, offset, maxSize);
        }
        os << std::endl;
    }
}
Beispiel #13
0
void
OptionsCont::writeConfiguration(std::ostream& os, bool filled,
                                bool complete, bool addComments) const {
    os << "<?xml version=\"1.0\"" << SUMOSAXAttributes::ENCODING << "?>\n\n";
    os << "<configuration xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"http://sumo.dlr.de/xsd/" << myAppName << "Configuration.xsd\">" << std::endl << std::endl;
    for (std::vector<std::string>::const_iterator i = mySubTopics.begin(); i != mySubTopics.end(); ++i) {
        std::string subtopic = *i;
        if (subtopic == "Configuration" && !complete) {
            continue;
        }
        std::replace(subtopic.begin(), subtopic.end(), ' ', '_');
        std::transform(subtopic.begin(), subtopic.end(), subtopic.begin(), tolower);
        const std::vector<std::string>& entries = mySubTopicEntries.find(*i)->second;
        bool hadOne = false;
        for (std::vector<std::string>::const_iterator j = entries.begin(); j != entries.end(); ++j) {
            Option* o = getSecure(*j);
            bool write = complete || (filled && !o->isDefault());
            if (!write) {
                continue;
            }
            if (!hadOne) {
                os << "    <" << subtopic << ">" << std::endl;
            }
            // add the comment if wished
            if (addComments) {
                os << "        <!-- " << StringUtils::escapeXML(o->getDescription()) << " -->" << std::endl;
            }
            // write the option and the value (if given)
            os << "        <" << *j << " value=\"";
            if (o->isSet() && (filled || o->isDefault())) {
                os << o->getValueString();
            }
            if (complete) {
                std::vector<std::string> synonymes = getSynonymes(*j);
                if (!synonymes.empty()) {
                    os << "\" synonymes=\"";
                    for (std::vector<std::string>::const_iterator s = synonymes.begin(); s != synonymes.end(); ++s) {
                        if (s != synonymes.begin()) {
                            os << " ";
                        }
                        os << (*s);
                    }
                }
                os << "\" type=\"" << o->getTypeName();
                if (!addComments) {
                    os << "\" help=\"" << StringUtils::escapeXML(o->getDescription());
                }
            }
            os << "\"/>" << std::endl;
            // append an endline if a comment was printed
            if (addComments) {
                os << std::endl;
            }
            hadOne = true;
        }
        if (hadOne) {
            os << "    </" << subtopic << ">" << std::endl << std::endl;
        }
    }
    os << "</configuration>" << std::endl;
}
Beispiel #14
0
 /// Equality operator
 bool operator==(const Option& rhs) const
 {
     return (m_name == rhs.getName() &&
         m_value == rhs.getValue<std::string>() &&
         m_description == rhs.getDescription());
 }