/src/models/dwarfmodelproxy.cpp

https://code.google.com/p/dwarftherapist/ · C++ · 181 lines · 142 code · 16 blank · 23 comment · 33 complexity · 937d48283283aa25d8e112a4eca2202d 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. #include <QtScript>
  21. #include "dwarfmodelproxy.h"
  22. #include "dwarfmodel.h"
  23. #include "dwarf.h"
  24. #include "profession.h"
  25. #include "defines.h"
  26. #include "dwarftherapist.h"
  27. #include "mainwindow.h"
  28. DwarfModelProxy::DwarfModelProxy(QObject *parent)
  29. :QSortFilterProxyModel(parent)
  30. , m_engine(new QScriptEngine(this))
  31. {}
  32. DwarfModel* DwarfModelProxy::get_dwarf_model() const {
  33. return static_cast<DwarfModel*>(sourceModel());
  34. }
  35. void DwarfModelProxy::cell_activated(const QModelIndex &idx) {
  36. bool valid = idx.isValid();
  37. QModelIndex new_idx = mapToSource(idx);
  38. valid = new_idx.isValid();
  39. return get_dwarf_model()->cell_activated(new_idx);
  40. }
  41. void DwarfModelProxy::setFilterFixedString(const QString &pattern) {
  42. m_filter_text = pattern;
  43. QSortFilterProxyModel::setFilterFixedString(pattern);
  44. }
  45. void DwarfModelProxy::apply_script(const QString &script_body) {
  46. m_active_filter_script = script_body;
  47. invalidateFilter();
  48. }
  49. bool DwarfModelProxy::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const {
  50. bool matches = true;
  51. int dwarf_id = 0;
  52. QSettings *s = DT->user_settings();
  53. const DwarfModel *m = get_dwarf_model();
  54. if (m->current_grouping() == DwarfModel::GB_NOTHING) {
  55. QModelIndex idx = m->index(source_row, 0, source_parent);
  56. dwarf_id = m->data(idx, DwarfModel::DR_ID).toInt();
  57. QString data = m->data(idx, filterRole()).toString();
  58. if (!m_filter_text.isEmpty())
  59. matches = matches && data.contains(m_filter_text, Qt::CaseInsensitive);
  60. } else {
  61. QModelIndex tmp_idx = m->index(source_row, 0, source_parent);
  62. QStandardItem *item = m->itemFromIndex(tmp_idx);
  63. if (m->data(tmp_idx, DwarfModel::DR_IS_AGGREGATE).toBool()) {
  64. int matches = 0;
  65. for(int i = 0; i < item->rowCount(); ++i) {
  66. if (filterAcceptsRow(i, tmp_idx)) // a child matches
  67. matches++;
  68. }
  69. matches = matches && matches > 0;
  70. } else {
  71. QModelIndex idx = m->index(source_row, 0, source_parent);
  72. dwarf_id = m->data(idx, DwarfModel::DR_ID).toInt();
  73. QString data = m->data(idx, filterRole()).toString();
  74. if (!m_filter_text.isEmpty())
  75. matches = matches && data.contains(m_filter_text, Qt::CaseInsensitive);
  76. }
  77. }
  78. if (dwarf_id && !m_active_filter_script.isEmpty()) {
  79. Dwarf *d = m->get_dwarf_by_id(dwarf_id);
  80. if (d) {
  81. QScriptValue d_obj = m_engine->newQObject(d);
  82. m_engine->globalObject().setProperty("d", d_obj);
  83. matches = matches && m_engine->evaluate(m_active_filter_script).toBool();
  84. }
  85. }
  86. //filter children and babies if necessary
  87. bool hide_children = s->value("options/hide_children_and_babies",
  88. false).toBool();
  89. if(dwarf_id && hide_children) {
  90. Dwarf *d = m->get_dwarf_by_id(dwarf_id);
  91. short baby_id = -1;
  92. short child_id = -1;
  93. foreach(Profession *p, GameDataReader::ptr()->get_professions()) {
  94. if (p->name(true) == "Baby") {
  95. baby_id = p->id();
  96. }
  97. if (p->name(true) == "Child") {
  98. child_id = p->id();
  99. }
  100. if(baby_id > 0 && child_id > 0)
  101. break;
  102. }
  103. matches = (d->raw_profession() != baby_id &&
  104. d->raw_profession() != child_id);
  105. }
  106. return matches;
  107. }
  108. bool DwarfModelProxy::filterAcceptsColumn(int source_column, const QModelIndex &source_parent) const {
  109. Q_UNUSED(source_column);
  110. Q_UNUSED(source_parent);
  111. return true;
  112. }
  113. void DwarfModelProxy::sort(int column, Qt::SortOrder order) {
  114. if (order == Qt::AscendingOrder)
  115. sort(column, DSR_NAME_ASC);
  116. else
  117. sort(column, DSR_NAME_DESC);
  118. }
  119. void DwarfModelProxy::sort(int column, DWARF_SORT_ROLE role) {
  120. Qt::SortOrder order;
  121. if (column == 0) {
  122. switch(role) {
  123. default:
  124. case DSR_NAME_ASC:
  125. order = Qt::AscendingOrder;
  126. setSortRole(DwarfModel::DR_SORT_VALUE);
  127. break;
  128. case DSR_NAME_DESC:
  129. order = Qt::DescendingOrder;
  130. setSortRole(DwarfModel::DR_SORT_VALUE);
  131. break;
  132. case DSR_ID_ASC:
  133. order = Qt::AscendingOrder;
  134. setSortRole(DwarfModel::DR_ID);
  135. break;
  136. case DSR_ID_DESC:
  137. order = Qt::DescendingOrder;
  138. setSortRole(DwarfModel::DR_ID);
  139. break;
  140. case DSR_GAME_ORDER:
  141. order = Qt::AscendingOrder;
  142. setSortRole(DwarfModel::DR_SORT_VALUE);
  143. break;
  144. }
  145. } else {
  146. switch(role) {
  147. default:
  148. case DSR_NAME_ASC:
  149. case DSR_ID_ASC:
  150. order = Qt::AscendingOrder;
  151. break;
  152. case DSR_NAME_DESC:
  153. case DSR_ID_DESC:
  154. order = Qt::DescendingOrder;
  155. break;
  156. }
  157. setSortRole(DwarfModel::DR_SORT_VALUE);
  158. }
  159. setSortCaseSensitivity(Qt::CaseInsensitive);
  160. setSortLocaleAware(true);
  161. QSortFilterProxyModel::sort(column, order);
  162. }