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