File: textnode.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 (86 lines) | stat: -rw-r--r-- 2,350 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
#include "textnode.hpp"

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

#include <QRegularExpression>

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

CSMFilter::TextNode::TextNode(int columnId, const std::string& text)
    : mColumnId(columnId)
    , mText(text)
    , mRegExp(QRegularExpression::anchoredPattern(QString::fromUtf8(mText.c_str())),
          QRegularExpression::CaseInsensitiveOption)
{
}

bool CSMFilter::TextNode::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 text node test");

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

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

    QVariant data = table.data(index);

    QString string;

    if (data.type() == QVariant::String)
    {
        string = data.toString();
    }
    else if ((data.type() == QVariant::Int || data.type() == QVariant::UInt)
        && CSMWorld::Columns::hasEnums(static_cast<CSMWorld::Columns::ColumnId>(mColumnId)))
    {
        int value = data.toInt();

        std::vector<std::pair<int, std::string>> enums
            = CSMWorld::Columns::getEnums(static_cast<CSMWorld::Columns::ColumnId>(mColumnId));

        if (value >= 0 && value < static_cast<int>(enums.size()))
            string = QString::fromUtf8(enums[value].second.c_str());
    }
    else if (data.type() == QVariant::Bool)
    {
        string = data.toBool() ? "true" : "false";
    }
    else if (mText.empty() && !data.isValid())
        return true;
    else
        return false;

    /// \todo make pattern syntax configurable
    QRegularExpressionMatch match = mRegExp.match(string);

    return match.hasMatch();
}

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

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

    stream << "text (";

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

    stream << ", \"" << mText << "\")";

    return stream.str();
}