oopapidocs  2.0
stdinreader.cpp
00001 #include <QMetaObject>
00002 #include <QMetaProperty>
00003 #include <QVariant>
00004 #include <QDebug>
00005 
00006 #include "stdinreader.h"
00007 
00008 static QTextStream cout(stdout);
00009 static QTextStream cin(stdin);
00010 static QTextStream cerr(stderr);
00011 
00012 bool StdinReader::readProperties(DataObject* doptr) {
00013     const QMetaObject* meta = doptr->metaObject();
00014 
00015     cout << QString(" readFromStandardInput<%1>(): ").arg(doptr->className()) << endl;
00016     for (int i=0; i < meta->propertyCount(); ++i) {
00017         QMetaProperty mp = meta->property(i);
00018 
00019         // Special case for the property "name" of QObject, which we wish to skip
00020         if (QString("name") == mp.name())
00021             continue;
00022 
00023         QString prompt = QString( "[%1] %2: ").arg( mp.type() ).arg( mp.name());
00024         bool done=false;
00025         do {
00026             // qDebug() << QString("ValidInputs: %1").arg(doptr->validInputs(mp.name()));
00027             cout << prompt;
00028             QString inpline = cin.readLine();
00029             QVariant qv(inpline);
00030 
00031             // it seems that QObject::setProperty() will sometimes return true even if the
00032             // value is invalid. Therefore, we must also check the QVariant's isValid() method.
00033             if (doptr->setProperty(mp.name(), qv) &&  doptr->property(mp.name()).isValid()) {
00034                 done=true;
00035             } else {
00036                                 cout << "Invalid input - please try again." << endl;
00037 //                cout() << doptr->validInputs(mp.name()) << endl;
00038             }
00039         } while (!done);
00040     }
00041     return true;
00042 }
 All Classes Namespaces Functions Enumerations