Rev 90 | Rev 94 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 90 | Rev 91 | ||
---|---|---|---|
Line 1... | Line 1... | ||
1 | Redirecting to URL 'https://iland.boku.ac.at/svn/iland/tags/release_1.0/src/core/speciesset.cpp': |
1 | Redirecting to URL 'https://iland.boku.ac.at/svn/iland/tags/release_1.0/src/core/speciesset.cpp': |
- | 2 | #include <QtCore>
|
|
- | 3 | #include <QtSql>
|
|
- | 4 | #include "global.h"
|
|
2 | #include "speciesset.h"
|
5 | #include "speciesset.h"
|
- | 6 | #include "species.h"
|
|
3 | 7 | ||
4 | SpeciesSet::SpeciesSet() |
8 | SpeciesSet::SpeciesSet() |
5 | {
|
9 | {
|
- | 10 | mSetupQuery = 0; |
|
- | 11 | }
|
|
- | 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); |
|
6 | }
|
69 | }
|