aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/result/stringresult.cpp
blob: 113c22cbcce9bcf86d041b555d0fe952523e21ad (plain) (blame)
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
#include "stringresult.h"
#include <QDebug>

StringResult::StringResult()
{
    m_highlight= true;
    m_resultTypes= static_cast<int>(Dice::RESULT_TYPE::STRING);
}
void StringResult::addText(QString text)
{
    m_value.append(text);
}
StringResult::~StringResult() {}
bool StringResult::hasResultOfType(Dice::RESULT_TYPE resultType) const
{
    bool val= false;

    switch(resultType)
    {
    case Dice::RESULT_TYPE::STRING:
        val= !isDigitOnly();
        break;
    case Dice::RESULT_TYPE::SCALAR:
        val= isDigitOnly();
        break;
    case Dice::RESULT_TYPE::DICE_LIST:
        val= (isDigitOnly() && m_value.size() > 1);
        break;
    default:
        break;
    }
    return val;
}

void StringResult::setNoComma(bool b)
{
    m_commaSeparator= !b;
}

QString StringResult::getText() const
{
    return m_commaSeparator ? m_value.join(",") : m_value.join(QString());
}

QVariant StringResult::getResult(Dice::RESULT_TYPE type)
{
    switch(type)
    {
    case Dice::RESULT_TYPE::STRING:
        return getText();
    case Dice::RESULT_TYPE::SCALAR:
        return getScalarResult();
    default:
        return QVariant();
    }
}
QString StringResult::toString(bool wl)
{
    if(wl)
    {
        return QString("%2 [label=\"StringResult_value_%1\"]").arg(getText().replace("%", "_"), m_id);
    }
    else
    {
        return m_id;
    }
}
void StringResult::setHighLight(bool b)
{
    m_highlight= b;
}

bool StringResult::hasHighLight() const
{
    return m_highlight;
}

void StringResult::finished()
{
    if(isDigitOnly())
    {
        std::for_each(m_value.begin(), m_value.end(), [this](const QString& str) {
            auto die= new Die();
            die->setMaxValue(m_stringCount);
            die->setValue(str.toInt());
            insertResult(die);
        });
    }
}

void StringResult::setStringCount(int count)
{
    m_stringCount= count;
}

bool StringResult::isDigitOnly() const
{
    return std::all_of(m_value.begin(), m_value.end(), [](const QString& str) {
        bool ok= false;
        str.toInt(&ok);
        return ok;
    });
}

Result* StringResult::getCopy() const
{
    auto copy= new StringResult();
    copy->setPrevious(getPrevious());
    copy->setHighLight(m_highlight);
    std::for_each(m_value.begin(), m_value.end(), [copy](const QString& str) { copy->addText(str); });
    return copy;
}

QString StringResult::getStringResult() const
{
    return getText();
}