2020-06-06 14:48:05 +00:00
|
|
|
//
|
|
|
|
// DateTimeComponent.cpp
|
|
|
|
//
|
|
|
|
// Date and time component.
|
|
|
|
//
|
|
|
|
|
2014-06-20 01:30:09 +00:00
|
|
|
#include "components/DateTimeComponent.h"
|
2017-11-01 22:21:10 +00:00
|
|
|
|
2018-01-27 17:04:28 +00:00
|
|
|
#include "utils/StringUtil.h"
|
2018-10-13 01:08:15 +00:00
|
|
|
#include "Log.h"
|
|
|
|
#include "Settings.h"
|
2013-09-28 22:35:38 +00:00
|
|
|
|
2020-06-06 14:48:05 +00:00
|
|
|
DateTimeComponent::DateTimeComponent(Window* window)
|
|
|
|
: TextComponent(window), mDisplayRelative(false)
|
2013-09-28 22:35:38 +00:00
|
|
|
{
|
2018-10-13 01:08:15 +00:00
|
|
|
setFormat("%m/%d/%Y");
|
2013-09-28 22:35:38 +00:00
|
|
|
}
|
|
|
|
|
2020-06-06 14:48:05 +00:00
|
|
|
DateTimeComponent::DateTimeComponent(
|
|
|
|
Window* window,
|
|
|
|
const std::string& text,
|
|
|
|
const std::shared_ptr<Font>& font,
|
|
|
|
unsigned int color,
|
|
|
|
Alignment align,
|
|
|
|
Vector3f pos,
|
|
|
|
Vector2f size,
|
|
|
|
unsigned int bgcolor)
|
|
|
|
: TextComponent(window, text, font, color, align, pos, size, bgcolor),
|
|
|
|
mDisplayRelative(false)
|
2013-09-28 22:35:38 +00:00
|
|
|
{
|
2018-10-13 01:08:15 +00:00
|
|
|
setFormat("%m/%d/%Y");
|
2013-09-28 22:35:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DateTimeComponent::setValue(const std::string& val)
|
|
|
|
{
|
2017-11-22 21:01:12 +00:00
|
|
|
mTime = val;
|
2018-10-13 01:08:15 +00:00
|
|
|
onTextChanged();
|
2013-09-28 22:35:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string DateTimeComponent::getValue() const
|
|
|
|
{
|
2017-11-22 21:01:12 +00:00
|
|
|
return mTime;
|
2013-09-28 22:35:38 +00:00
|
|
|
}
|
|
|
|
|
2018-10-13 01:08:15 +00:00
|
|
|
void DateTimeComponent::setFormat(const std::string& format)
|
2013-09-28 22:35:38 +00:00
|
|
|
{
|
2018-10-13 01:08:15 +00:00
|
|
|
mFormat = format;
|
|
|
|
onTextChanged();
|
2013-09-28 22:35:38 +00:00
|
|
|
}
|
|
|
|
|
2018-10-13 01:08:15 +00:00
|
|
|
void DateTimeComponent::setDisplayRelative(bool displayRelative)
|
2013-09-28 22:35:38 +00:00
|
|
|
{
|
2018-10-13 01:08:15 +00:00
|
|
|
mDisplayRelative = displayRelative;
|
|
|
|
onTextChanged();
|
2013-09-28 22:35:38 +00:00
|
|
|
}
|
|
|
|
|
2018-10-13 01:08:15 +00:00
|
|
|
void DateTimeComponent::onTextChanged()
|
2013-09-28 22:35:38 +00:00
|
|
|
{
|
2018-10-13 01:08:15 +00:00
|
|
|
mText = getDisplayString();
|
|
|
|
TextComponent::onTextChanged();
|
2013-09-28 22:35:38 +00:00
|
|
|
}
|
|
|
|
|
2018-10-13 01:08:15 +00:00
|
|
|
std::string DateTimeComponent::getDisplayString() const
|
2013-09-28 22:35:38 +00:00
|
|
|
{
|
2018-10-13 01:08:15 +00:00
|
|
|
if (mDisplayRelative) {
|
2020-06-06 14:48:05 +00:00
|
|
|
// Relative time.
|
2018-10-13 01:08:15 +00:00
|
|
|
if(mTime.getTime() == 0)
|
|
|
|
return "never";
|
2013-09-28 22:35:38 +00:00
|
|
|
|
2018-10-13 01:08:15 +00:00
|
|
|
Utils::Time::DateTime now(Utils::Time::now());
|
|
|
|
Utils::Time::Duration dur(now.getTime() - mTime.getTime());
|
2013-09-28 22:35:38 +00:00
|
|
|
|
2018-10-13 01:08:15 +00:00
|
|
|
char buf[64];
|
2013-09-28 22:35:38 +00:00
|
|
|
|
2018-10-13 01:08:15 +00:00
|
|
|
if(dur.getDays() > 0)
|
|
|
|
sprintf(buf, "%d day%s ago", dur.getDays(), (dur.getDays() > 1) ? "s" : "");
|
|
|
|
else if(dur.getHours() > 0)
|
|
|
|
sprintf(buf, "%d hour%s ago", dur.getHours(), (dur.getHours() > 1) ? "s" : "");
|
|
|
|
else if(dur.getMinutes() > 0)
|
|
|
|
sprintf(buf, "%d minute%s ago", dur.getMinutes(), (dur.getMinutes() > 1) ? "s" : "");
|
|
|
|
else
|
|
|
|
sprintf(buf, "%d second%s ago", dur.getSeconds(), (dur.getSeconds() > 1) ? "s" : "");
|
2013-09-28 22:35:38 +00:00
|
|
|
|
2018-10-13 01:08:15 +00:00
|
|
|
return std::string(buf);
|
|
|
|
}
|
2013-09-28 22:35:38 +00:00
|
|
|
|
2018-10-13 01:08:15 +00:00
|
|
|
if(mTime.getTime() == 0)
|
|
|
|
return "unknown";
|
2013-09-28 22:35:38 +00:00
|
|
|
|
2018-10-13 01:08:15 +00:00
|
|
|
return Utils::Time::timeToString(mTime.getTime(), mFormat);
|
2013-09-28 22:35:38 +00:00
|
|
|
}
|
2013-10-16 23:11:43 +00:00
|
|
|
|
2018-10-13 01:08:15 +00:00
|
|
|
void DateTimeComponent::render(const Transform4x4f& parentTrans)
|
2013-10-16 23:11:43 +00:00
|
|
|
{
|
2018-10-13 01:08:15 +00:00
|
|
|
TextComponent::render(parentTrans);
|
2013-10-16 23:11:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-06 14:48:05 +00:00
|
|
|
void DateTimeComponent::applyTheme(const std::shared_ptr<ThemeData>& theme,
|
|
|
|
const std::string& view, const std::string& element, unsigned int properties)
|
2014-03-25 19:14:09 +00:00
|
|
|
{
|
2018-10-13 01:08:15 +00:00
|
|
|
GuiComponent::applyTheme(theme, view, element, properties);
|
2014-01-19 22:06:13 +00:00
|
|
|
|
2018-10-13 01:08:15 +00:00
|
|
|
using namespace ThemeFlags;
|
2014-05-03 19:51:50 +00:00
|
|
|
|
2014-01-19 22:06:13 +00:00
|
|
|
const ThemeData::ThemeElement* elem = theme->getElement(view, element, "datetime");
|
|
|
|
if(!elem)
|
|
|
|
return;
|
|
|
|
|
2018-10-13 01:08:15 +00:00
|
|
|
if(elem->has("displayRelative"))
|
|
|
|
setDisplayRelative(elem->get<bool>("displayRelative"));
|
2015-03-05 23:43:38 +00:00
|
|
|
|
2018-10-13 01:08:15 +00:00
|
|
|
if(elem->has("format"))
|
|
|
|
setFormat(elem->get<std::string>("format"));
|
2015-03-05 23:43:38 +00:00
|
|
|
|
2018-10-13 01:08:15 +00:00
|
|
|
if (properties & COLOR && elem->has("color"))
|
2014-01-19 22:06:13 +00:00
|
|
|
setColor(elem->get<unsigned int>("color"));
|
|
|
|
|
2018-10-13 01:08:15 +00:00
|
|
|
setRenderBackground(false);
|
|
|
|
if (properties & COLOR && elem->has("backgroundColor")) {
|
|
|
|
setBackgroundColor(elem->get<unsigned int>("backgroundColor"));
|
|
|
|
setRenderBackground(true);
|
|
|
|
}
|
|
|
|
|
2020-06-06 14:48:05 +00:00
|
|
|
if(properties & ALIGNMENT && elem->has("alignment")) {
|
2018-10-13 01:08:15 +00:00
|
|
|
std::string str = elem->get<std::string>("alignment");
|
|
|
|
if(str == "left")
|
|
|
|
setHorizontalAlignment(ALIGN_LEFT);
|
|
|
|
else if(str == "center")
|
|
|
|
setHorizontalAlignment(ALIGN_CENTER);
|
|
|
|
else if(str == "right")
|
|
|
|
setHorizontalAlignment(ALIGN_RIGHT);
|
|
|
|
else
|
|
|
|
LOG(LogError) << "Unknown text alignment string: " << str;
|
|
|
|
}
|
|
|
|
|
2014-05-03 19:51:50 +00:00
|
|
|
if(properties & FORCE_UPPERCASE && elem->has("forceUppercase"))
|
|
|
|
setUppercase(elem->get<bool>("forceUppercase"));
|
|
|
|
|
2018-10-13 01:08:15 +00:00
|
|
|
if(properties & LINE_SPACING && elem->has("lineSpacing"))
|
|
|
|
setLineSpacing(elem->get<float>("lineSpacing"));
|
|
|
|
|
2014-01-19 22:06:13 +00:00
|
|
|
setFont(Font::getFromTheme(elem, properties, mFont));
|
|
|
|
}
|