Subversion Repositories public iLand

Rev

Rev 90 | Rev 94 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1
 
91 Werner 2
#include <QtCore>
3
#include <QtSql>
4
#include "global.h"
90 Werner 5
#include "speciesset.h"
91 Werner 6
#include "species.h"
90 Werner 7
 
8
SpeciesSet::SpeciesSet()
9
{
91 Werner 10
    mSetupQuery = 0;
90 Werner 11
}
91 Werner 12
 
13
SpeciesSet::~SpeciesSet()
14
{
15
   clear();
16
}
17
 
18
void SpeciesSet::clear()
19
{
20
    qDeleteAll(mSpecies.values());
21
    mSpecies.clear();
22
}
23
 
24
const Species *SpeciesSet::species(const QString &speciesId)
25
{
26
    return mSpecies.value(speciesId);
27
}
28
 
29
/** loads active species from a database table and creates/setups the species.
30
    The function uses the global database-connection.
31
  */
32
int SpeciesSet::loadFromDatabase(const QString &tableName)
33
{
34
    QSqlQuery query(GlobalSettings::instance()->dbin());
35
    mSetupQuery = &query;
36
    QString sql = QString("select * from %1").arg(tableName);
37
    query.exec(sql);
38
    clear();
39
    qDebug() << "attempting to load a species set from" << tableName;
40
    while (query.next()) {
41
        if (var("active").toInt()==0)
42
            continue;
43
 
44
        Species *s = new Species(this); // create
45
        // call setup routine (which call SpeciesSet::var() to retrieve values
46
        s->setup();
47
 
48
        mSpecies.insert(s->id(), s); // store
49
    } // while query.next()
50
    qDebug() << "loaded" << mSpecies.count() << "active species:";
51
    qDebug() << mSpecies.keys();
52
 
53
    mSetupQuery = 0;
54
    return mSpecies.count();
55
 
56
}
57
/** retrieves variables from the datasource available during the setup of species.
58
  */
59
QVariant SpeciesSet::var(const QString& varName)
60
{
61
    Q_ASSERT(mSetupQuery==0);
62
 
63
    int idx = mSetupQuery->record().indexOf(varName);
64
    if (idx>=0)
65
        return mSetupQuery->value(idx);
66
    // lookup in defaults
67
    qDebug() << "variable" << varName << "not found - using default.";
68
    return GlobalSettings::instance()->settingDefaultValue(varName);
69
}