PageRenderTime 36ms CodeModel.GetById 13ms app.highlight 20ms RepoModel.GetById 1ms app.codeStats 0ms

/src/models/dwarfmodelproxy.cpp

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