MythTV master
mythuiclock.cpp
Go to the documentation of this file.
1
2#include "mythuiclock.h"
3
4#include <QCoreApplication>
5#include <QDomDocument>
6#include <QTimeZone>
7
8#include "mythpainter.h"
9#include "mythmainwindow.h"
10#include "mythfontproperties.h"
11
14#include "libmythbase/mythdb.h"
16
17MythUIClock::MythUIClock(MythUIType *parent, const QString &name)
18 : MythUIText(parent, name),
19 m_timeFormat(GetMythDB()->GetSetting("TimeFormat", "h:mm ap")),
20 m_dateFormat(GetMythDB()->GetSetting("DateFormat", "ddd d MMM yyyy")),
21 m_shortDateFormat(GetMythDB()->GetSetting("ShortDateFormat", "ddd M/d"))
22{
23 m_format = QString("%1, %2").arg(m_dateFormat, m_timeFormat);
24}
25
27{
28 delete m_font;
29 m_font = nullptr;
30}
31
36{
38
39 if (m_nextUpdate.isNull() || (m_time >= m_nextUpdate))
41
43}
44
53{
54 QDateTime dt = m_time.toLocalTime();
55 QString newMsg = gCoreContext->GetQLocale().toString(dt, m_format);
56
57 m_nextUpdate = m_time.addSecs(1);
58#if QT_VERSION < QT_VERSION_CHECK(6,5,0)
59 m_nextUpdate = QDateTime(m_time.date(),
60 m_time.time().addMSecs(m_time.time().msec()),
61 Qt::UTC);
62#else
63 m_nextUpdate = QDateTime(m_time.date(),
64 m_time.time().addMSecs(m_time.time().msec()),
65 QTimeZone(QTimeZone::UTC));
66#endif
67
68 return newMsg;
69}
70
74void MythUIClock::SetText(const QString &text)
75{
76 QString txt = text;
77
78 if (txt.isEmpty())
79 {
81 txt = GetTimeText();
82 }
83
85}
86
91 const QString &filename, QDomElement &element, bool showWarnings)
92{
93 if (element.tagName() == "format" ||
94 element.tagName() == "template")
95 {
96 QString format = parseText(element);
97 format = QCoreApplication::translate("ThemeUI", format.toUtf8());
98 format.replace("%TIME%", m_timeFormat, Qt::CaseInsensitive);
99 format.replace("%DATE%", m_dateFormat, Qt::CaseInsensitive);
100 format.replace("%SHORTDATE%", m_shortDateFormat, Qt::CaseInsensitive);
101 m_format = format;
102 m_message = gCoreContext->GetQLocale().toString(QDateTime::currentDateTime(), m_format);
103 }
104 else
105 {
106 m_message = gCoreContext->GetQLocale().toString(QDateTime::currentDateTime(), m_format);
107 return MythUIText::ParseElement(filename, element, showWarnings);
108 }
109
110 return true;
111}
112
117{
118 auto *clock = dynamic_cast<MythUIClock *>(base);
119
120 if (!clock)
121 {
122 LOG(VB_GENERAL, LOG_ERR, "ERROR, bad parsing");
123 return;
124 }
125
126 m_time = clock->m_time;
127 m_nextUpdate = clock->m_nextUpdate;
128
129 m_format = clock->m_format;
130 m_timeFormat = clock->m_timeFormat;
131 m_dateFormat = clock->m_dateFormat;
132 m_shortDateFormat = clock->m_shortDateFormat;
133
134 m_flash = clock->m_flash;
135
137}
138
143{
144 auto *clock = new MythUIClock(parent, objectName());
145 clock->CopyFrom(this);
146}
QLocale GetQLocale(void)
A simple text clock widget.
Definition: mythuiclock.h:26
QString m_format
Definition: mythuiclock.h:45
void SetText(const QString &text) override
This sets the text, unless the string is blank, in that case the time is looked up and set as the tex...
Definition: mythuiclock.cpp:74
~MythUIClock() override
Definition: mythuiclock.cpp:26
QString GetTimeText(void)
This creates a string based on m_time, and sets m_nextUpdate to the second following m_time.
Definition: mythuiclock.cpp:52
void Pulse(void) override
Looks up the time and sets the clock if the current time is greater than or equal to m_nextUpdate.
Definition: mythuiclock.cpp:35
void CopyFrom(MythUIType *base) override
Copy this widgets state from another.
QDateTime m_time
Definition: mythuiclock.h:42
QString m_shortDateFormat
Definition: mythuiclock.h:48
void CreateCopy(MythUIType *parent) override
Copy the state of this widget to the one given, it must be of the same type.
bool ParseElement(const QString &filename, QDomElement &element, bool showWarnings) override
Parse the xml definition of this widget setting the state of the object accordingly.
Definition: mythuiclock.cpp:90
QDateTime m_nextUpdate
Definition: mythuiclock.h:43
bool m_flash
Definition: mythuiclock.h:50
QString m_dateFormat
Definition: mythuiclock.h:47
QString m_timeFormat
Definition: mythuiclock.h:46
MythUIClock(MythUIType *parent, const QString &name)
Definition: mythuiclock.cpp:17
All purpose text widget, displays a text string.
Definition: mythuitext.h:29
bool ParseElement(const QString &filename, QDomElement &element, bool showWarnings) override
Parse the xml definition of this widget setting the state of the object accordingly.
MythFontProperties * m_font
Definition: mythuitext.h:138
void CopyFrom(MythUIType *base) override
Copy this widgets state from another.
QString m_message
Definition: mythuitext.h:116
void Pulse(void) override
Pulse is called 70 times a second to trigger a single frame of an animation.
virtual void SetText(const QString &text)
Definition: mythuitext.cpp:115
The base class on which all widgets and screens are based.
Definition: mythuitype.h:86
static QString parseText(QDomElement &element)
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
MythDB * GetMythDB(void)
Definition: mythdb.cpp:51
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
QDateTime current(bool stripped)
Returns current Date and Time in UTC.
Definition: mythdate.cpp:15