PageRenderTime 29ms CodeModel.GetById 18ms app.highlight 8ms RepoModel.GetById 0ms app.codeStats 1ms

/inc/militarypreference.h

https://code.google.com/p/dwarftherapist/
C++ Header | 107 lines | 74 code | 10 blank | 23 comment | 13 complexity | d8ab69701e83e71f7e213cfa34212d38 MD5 | raw file
  1/*
  2Dwarf Therapist
  3Copyright (c) 2009 Trey Stout (chmod)
  4
  5Permission is hereby granted, free of charge, to any person obtaining a copy
  6of this software and associated documentation files (the "Software"), to deal
  7in the Software without restriction, including without limitation the rights
  8to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9copies of the Software, and to permit persons to whom the Software is
 10furnished to do so, subject to the following conditions:
 11
 12The above copyright notice and this permission notice shall be included in
 13all copies or substantial portions of the Software.
 14
 15THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 20OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 21THE SOFTWARE.
 22*/
 23#ifndef MILITARY_PREFERENCE_H
 24#define MILITARY_PREFERENCE_H
 25
 26#include <QtGui>
 27#include "truncatingfilelogger.h"
 28
 29struct PreferenceValue {
 30    short id;
 31    QString name;
 32    QString symbol;
 33};
 34
 35class MilitaryPreference : public QObject {
 36    Q_OBJECT
 37public:
 38    MilitaryPreference(QSettings &s, QObject *parent = 0)
 39        : QObject(parent)
 40        , name(s.value("name", "UNKNOWN PREFERENCE").toString())
 41        , labor_id(s.value("id", -1).toInt())
 42        , skill_id(s.value("skill", -1).toInt())
 43    {
 44        int values = s.beginReadArray("values");
 45        for (int i = 0; i < values; ++i) {
 46            s.setArrayIndex(i);
 47            PreferenceValue *pv = new PreferenceValue;
 48            pv->id = s.value("id", -1).toInt();
 49            pv->name = s.value("name").toString();
 50            pv->symbol = s.value("symbol", pv->name).toString();
 51            if (pv->id != -1 || !pv->name.isEmpty()) {
 52                m_values << pv;
 53            } else {
 54                delete pv;
 55            }
 56        }
 57        s.endArray();
 58    }
 59
 60    virtual ~MilitaryPreference() {
 61        foreach(PreferenceValue *pv, m_values) {
 62            delete pv;
 63        }
 64        m_values.clear();
 65    }
 66
 67    QString value_symbol(const short &val_id) {
 68        foreach(PreferenceValue *pv, m_values) {
 69            if (pv->id == val_id)
 70                return pv->symbol;
 71        }
 72        return QString("%1: Unknown Symbol").arg(name);
 73    }
 74
 75    QString value_name(const short &val_id) {
 76        foreach(PreferenceValue *pv, m_values) {
 77            if (pv->id == val_id)
 78                return pv->name;
 79        }
 80        return QString("%1: Unknown Value").arg(name);
 81    }
 82
 83    // return the next value in the list, should wrap around
 84    short next_val(const short &old_val) {
 85        int count = 0;
 86        foreach (PreferenceValue *pv, m_values) {
 87            if (pv->id == old_val) {
 88                if (count + 1 >= m_values.size()) {
 89                    return m_values.at(0)->id;
 90                } else {
 91                    return m_values.at(count + 1)->id;
 92                }
 93            }
 94            count++;
 95        }
 96        LOGW << "Military Preference:" << name << "was unable to find a suitable next value after" << old_val;
 97        return m_values.at(0)->id; // that sucks...
 98    }
 99
100    QString name;
101    int labor_id;
102    int skill_id;
103    QList<PreferenceValue*> m_values;
104};
105
106#endif
107