stringer.cpp 6.74 KB
#include "stringer.h"

#include "config.h"

namespace {
QString heavySpan("<span class=\"heavy\">%1</span>");
QString lightSpan("<span class=\"light\">%1</span>");
QString lightestSpan("<span class=\"lightest\">%1</span>");

enum RemainingTimeFormat { RemainingTime, FinishTime };
RemainingTimeFormat remainingTime()
{
    Define::config_item item = Config::getInstance()->getConfigValue(Define::config_resttime_format);
    switch (item.d32)
    {
    case Define::rest_time_target:
        return FinishTime;
    case Define::rest_time_rest:
    default:
        return RemainingTime;
    }
}

enum TemperatureFormat { Celsius, Fahrenheit };
TemperatureFormat temperatureFormat()
{
    Define::config_item item = Config::getInstance()->getConfigValue(Define::config_temptype);
    switch (item.d32)
    {
    case Define::temp_type_f:
        return Fahrenheit;
    case Define::temp_type_c:
    default:
        return Celsius;
    }
}

int toFahrenheit(int celsius)
{
    return celsius * 1.8 + 32;
}

enum RealTimeFormat { Hour12, Hour24 };
RealTimeFormat realTimeFormat()
{
    Define::config_item item = Config::getInstance()->getConfigValue(Define::config_time_type);
    switch (item.d32)
    {
    case Define::time_type_12h:
        return Hour12;
    case Define::time_type_24h:
    default:
        return Hour24;
    }
}

}

QString Stringer::remainingTime(int msecs)
{
    switch (::remainingTime())
    {
    case RemainingTime:
        msecs /= 1000;
        if (msecs >= 3600)
            return QCoreApplication::tr("%1시간 %2분").arg(msecs / 3600).arg((msecs % 3600) / 60, 2, 10, QLatin1Char('0'));
        if (msecs >= 60)
            return QCoreApplication::tr("%1분 %2초").arg(msecs / 60).arg(msecs % 60, 2, 10, QLatin1Char('0'));

        return QCoreApplication::tr("%1초").arg(msecs);
    case FinishTime:
        QDateTime dateTime = QDateTime::currentDateTime().addMSecs(msecs);
        switch (realTimeFormat())
        {
        case Hour12:
            return dateTime.toString("A hh:mm:ss");
        case Hour24:
            return dateTime.toString("HH:mm:ss");
        }
    }

    return QString();
}

QString Stringer::remainingTime(int msecs, QString style)
{
    switch (::remainingTime())
    {
    case RemainingTime:
        msecs /= 1000;
        if (msecs >= 3600)
        {
            QString hour = heavySpan.arg(msecs / 3600) + lightSpan.arg(QCoreApplication::tr("시간"));
            QString min = heavySpan.arg((msecs % 3600) / 60, 2, 10, QLatin1Char('0')) + lightSpan.arg(QCoreApplication::tr("분"));

            return style + QString("%1 %2").arg(hour).arg(min);
        }
        if (msecs >= 60)
        {
            QString min = heavySpan.arg(msecs / 60) + lightSpan.arg(QCoreApplication::tr("분"));
            QString sec = heavySpan.arg(msecs % 60, 2, 10, QLatin1Char('0')) + lightSpan.arg(QCoreApplication::tr("초"));

            return style + QString("%1 %2").arg(min).arg(sec);
        }

        return style + heavySpan.arg(msecs) + lightSpan.arg(QCoreApplication::tr("초"));
    case FinishTime:
        QDateTime dateTime = QDateTime::currentDateTime().addMSecs(msecs);
        switch (realTimeFormat())
        {
        case Hour12:
            return heavySpan.arg(dateTime.toString("A hh:mm:ss"));
        case Hour24:
            return heavySpan.arg(dateTime.toString("HH:mm:ss"));
        }
    }

    return QString();
}

QString Stringer::remainingTime(qint64 msecs)
{
    switch (::remainingTime())
    {
    case RemainingTime:
        msecs /= 1000;
        if (msecs >= 3600)
            return QCoreApplication::tr("%1시간 %2분").arg(msecs / 3600).arg((msecs % 3600) / 60, 2, 10, QLatin1Char('0'));
        if (msecs >= 60)
            return QCoreApplication::tr("%1분 %2초").arg(msecs / 60).arg(msecs % 60, 2, 10, QLatin1Char('0'));

        return QCoreApplication::tr("%1초").arg(msecs);
    case FinishTime:
        QDateTime dateTime = QDateTime::currentDateTime().addMSecs(msecs);
        switch (realTimeFormat())
        {
        case Hour12:
            return dateTime.toString("A hh:mm:ss");
        case Hour24:
            return dateTime.toString("HH:mm:ss");
        }
    }

    return QString();
}

QString Stringer::temperature(int celsius)
{
    switch (temperatureFormat())
    {
    case Fahrenheit:
        return QString("%1℉").arg(toFahrenheit(celsius));
    case Celsius:
    default:
        return QString("%1℃").arg(celsius);
    }
}

QString Stringer::temperature(int celsius, QString style)
{
    switch (temperatureFormat())
    {
    case Fahrenheit:
        return style + heavySpan.arg(toFahrenheit(celsius)) + lightSpan.arg("℉");
    case Celsius:
    default:
        return style + heavySpan.arg(celsius) + lightSpan.arg("℃");
    }
}

QString Stringer::temperature(int current, int target)
{
    switch (temperatureFormat())
    {
    case Fahrenheit:
        return QString("%1℉ / %2℉").arg(toFahrenheit(current)).arg(toFahrenheit(target));
    case Celsius:
    default:
        return QString("%1℃ / %2℃").arg(current).arg(target);
    }
}

QString Stringer::temperature(int current, int target, QString style)
{
    switch (temperatureFormat())
    {
    case Fahrenheit:
        return style + heavySpan.arg(toFahrenheit(current)) + lightSpan.arg(QString("℉ / %1").arg(toFahrenheit(target))) + lightestSpan.arg("℉");
    case Celsius:
    default:
        return style + heavySpan.arg(current) + lightSpan.arg(QString("℃ / %1").arg(target)) + lightestSpan.arg("℃");
    }
}

QString Stringer::unusedTemperature()
{
    switch (temperatureFormat())
    {
    case Fahrenheit:
        return QString("℉");
    case Celsius:
    default:
        return QString("℃");
    }
}

QString Stringer::unusedTemperature(QString style)
{
    switch (temperatureFormat())
    {
    case Fahrenheit:
        return style + lightSpan.arg("℉");
    case Celsius:
    default:
        return style + lightSpan.arg("℃");
    }
}

QString Stringer::dateTime(const QDateTime &dateTime, DateTimeType type)
{
    Define::config_item item = Config::getInstance()->getConfigValue(Define::config_time_type);
    switch(item.d32)
    {
    case Define::time_type_12h:
        if (type == OneLine)
            return dateTime.toString("yy-MM-dd a hh:mm");
        else
            return dateTime.toString("yy-MM-dd\na hh:mm");
    case Define::time_type_24h:
    default:
        if (type == OneLine)
            return dateTime.toString("yy-MM-dd HH:mm");
        else
            return dateTime.toString("yy-MM-dd\nHH:mm");
    }
}

QString Stringer::time(int msecs)
{
    int t = qCeil(msecs / 1000.0);

    int h = t / (60*60);
    int m = (t/60) % 60;
    int s = t % 60;

    return QString("%1:%2:%3")
            .arg(h, 2, 10, QLatin1Char('0'))
            .arg(m, 2, 10, QLatin1Char('0'))
            .arg(s, 2, 10, QLatin1Char('0'));
}