File indexing completed on 2024-05-12 05:39:29

0001 #include "stringresult.h"
0002 #include <QDebug>
0003 
0004 StringResult::StringResult()
0005 {
0006     m_highlight= true;
0007     m_resultTypes= static_cast<int>(Dice::RESULT_TYPE::STRING);
0008 }
0009 void StringResult::addText(QString text)
0010 {
0011     m_value.append(text);
0012 }
0013 StringResult::~StringResult() {}
0014 bool StringResult::hasResultOfType(Dice::RESULT_TYPE resultType) const
0015 {
0016     bool val= false;
0017 
0018     switch(resultType)
0019     {
0020     case Dice::RESULT_TYPE::STRING:
0021         val= !isDigitOnly();
0022         break;
0023     case Dice::RESULT_TYPE::SCALAR:
0024         val= isDigitOnly();
0025         break;
0026     case Dice::RESULT_TYPE::DICE_LIST:
0027         val= (isDigitOnly() && m_value.size() > 1);
0028         break;
0029     default:
0030         break;
0031     }
0032     return val;
0033 }
0034 
0035 void StringResult::setNoComma(bool b)
0036 {
0037     m_commaSeparator= !b;
0038 }
0039 
0040 QString StringResult::getText() const
0041 {
0042     return m_commaSeparator ? m_value.join(",") : m_value.join(QString());
0043 }
0044 
0045 QVariant StringResult::getResult(Dice::RESULT_TYPE type)
0046 {
0047     switch(type)
0048     {
0049     case Dice::RESULT_TYPE::STRING:
0050         return getText();
0051     case Dice::RESULT_TYPE::SCALAR:
0052         return getScalarResult();
0053     default:
0054         return QVariant();
0055     }
0056 }
0057 QString StringResult::toString(bool wl)
0058 {
0059     if(wl)
0060     {
0061         return QString("%2 [label=\"StringResult_value_%1\"]").arg(getText().replace("%", "_"), m_id);
0062     }
0063     else
0064     {
0065         return m_id;
0066     }
0067 }
0068 void StringResult::setHighLight(bool b)
0069 {
0070     m_highlight= b;
0071 }
0072 
0073 bool StringResult::hasHighLight() const
0074 {
0075     return m_highlight;
0076 }
0077 
0078 void StringResult::finished()
0079 {
0080     if(isDigitOnly())
0081     {
0082         std::for_each(m_value.begin(), m_value.end(), [this](const QString& str) {
0083             auto die= new Die();
0084             die->setMaxValue(m_stringCount);
0085             die->setValue(str.toInt());
0086             insertResult(die);
0087         });
0088     }
0089 }
0090 
0091 void StringResult::setStringCount(int count)
0092 {
0093     m_stringCount= count;
0094 }
0095 
0096 bool StringResult::isDigitOnly() const
0097 {
0098     return std::all_of(m_value.begin(), m_value.end(), [](const QString& str) {
0099         bool ok= false;
0100         str.toInt(&ok);
0101         return ok;
0102     });
0103 }
0104 
0105 Result* StringResult::getCopy() const
0106 {
0107     auto copy= new StringResult();
0108     copy->setPrevious(getPrevious());
0109     copy->setHighLight(m_highlight);
0110     std::for_each(m_value.begin(), m_value.end(), [copy](const QString& str) { copy->addText(str); });
0111     return copy;
0112 }
0113 
0114 QString StringResult::getStringResult() const
0115 {
0116     return getText();
0117 }