File: valuenode.cpp

package info (click to toggle)
openmw 0.49.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 33,992 kB
  • sloc: cpp: 372,479; xml: 2,149; sh: 1,403; python: 797; makefile: 26
file content (123 lines) | stat: -rw-r--r-- 2,981 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include "valuenode.hpp"

#include <sstream>
#include <stdexcept>
#include <utility>

#include "../world/idtablebase.hpp"

CSMFilter::ValueNode::ValueNode(int columnId, Type lowerType, Type upperType, double lower, double upper)
    : mColumnId(columnId)
    , mLower(lower)
    , mUpper(upper)
    , mLowerType(lowerType)
    , mUpperType(upperType)
{
}

bool CSMFilter::ValueNode::test(const CSMWorld::IdTableBase& table, int row, const std::map<int, int>& columns) const
{
    const std::map<int, int>::const_iterator iter = columns.find(mColumnId);

    if (iter == columns.end())
        throw std::logic_error("invalid column in value node test");

    if (iter->second == -1)
        return true;

    QModelIndex index = table.index(row, iter->second);

    QVariant data = table.data(index);

    if (data.type() != QVariant::Double && data.type() != QVariant::Bool && data.type() != QVariant::Int
        && data.type() != QVariant::UInt && data.type() != static_cast<QVariant::Type>(QMetaType::Float))
        return false;

    double value = data.toDouble();

    switch (mLowerType)
    {
        case Type_Closed:
            if (value < mLower)
                return false;
            break;
        case Type_Open:
            if (value <= mLower)
                return false;
            break;
        case Type_Infinite:
            break;
    }

    switch (mUpperType)
    {
        case Type_Closed:
            if (value > mUpper)
                return false;
            break;
        case Type_Open:
            if (value >= mUpper)
                return false;
            break;
        case Type_Infinite:
            break;
    }

    return true;
}

std::vector<int> CSMFilter::ValueNode::getReferencedColumns() const
{
    return std::vector<int>(1, mColumnId);
}

std::string CSMFilter::ValueNode::toString(bool numericColumns) const
{
    std::ostringstream stream;

    stream << "value (";

    if (numericColumns)
        stream << mColumnId;
    else
        stream << "\"" << CSMWorld::Columns::getName(static_cast<CSMWorld::Columns::ColumnId>(mColumnId)) << "\"";

    stream << ", ";

    if (mLower == mUpper && mLowerType != Type_Infinite && mUpperType != Type_Infinite)
        stream << mLower;
    else
    {
        switch (mLowerType)
        {
            case Type_Closed:
                stream << "[" << mLower;
                break;
            case Type_Open:
                stream << "(" << mLower;
                break;
            case Type_Infinite:
                stream << "(";
                break;
        }

        stream << ", ";

        switch (mUpperType)
        {
            case Type_Closed:
                stream << mUpper << "]";
                break;
            case Type_Open:
                stream << mUpper << ")";
                break;
            case Type_Infinite:
                stream << ")";
                break;
        }
    }

    stream << ")";

    return stream.str();
}