MythTV  master
mythuiclock.cpp
Go to the documentation of this file.
1 
2 #include "mythuiclock.h"
3 
4 #include <QCoreApplication>
5 #include <QDomDocument>
6 
7 #include "mythpainter.h"
8 #include "mythmainwindow.h"
9 #include "mythfontproperties.h"
10 
11 #include "mythcorecontext.h"
12 #include "mythlogging.h"
13 #include "mythdate.h"
14 #include "mythdb.h"
15 
16 MythUIClock::MythUIClock(MythUIType *parent, const QString &name)
17  : MythUIText(parent, name)
18 {
19  m_DateFormat = GetMythDB()->GetSetting("DateFormat", "ddd d MMM yyyy");
20  m_ShortDateFormat = GetMythDB()->GetSetting("ShortDateFormat", "ddd M/d");
21  m_TimeFormat = GetMythDB()->GetSetting("TimeFormat", "h:mm ap");
22 
23  m_Format = QString("%1, %2").arg(m_DateFormat).arg(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  m_nextUpdate = QDateTime(m_Time.date(),
59  m_Time.time().addMSecs(m_Time.time().msec()),
60  Qt::UTC);
61 
62  return newMsg;
63 }
64 
68 void MythUIClock::SetText(const QString &text)
69 {
70  QString txt = text;
71 
72  if (txt.isEmpty())
73  {
75  txt = GetTimeText();
76  }
77 
79 }
80 
85  const QString &filename, QDomElement &element, bool showWarnings)
86 {
87  if (element.tagName() == "format" ||
88  element.tagName() == "template")
89  {
90  QString format = parseText(element);
91  format = qApp->translate("ThemeUI", format.toUtf8());
92  format.replace("%TIME%", m_TimeFormat, Qt::CaseInsensitive);
93  format.replace("%DATE%", m_DateFormat, Qt::CaseInsensitive);
94  format.replace("%SHORTDATE%", m_ShortDateFormat, Qt::CaseInsensitive);
95  m_Format = format;
96  m_Message = gCoreContext->GetQLocale().toString(QDateTime::currentDateTime(), m_Format);
97  }
98  else
99  {
100  m_Message = gCoreContext->GetQLocale().toString(QDateTime::currentDateTime(), m_Format);
101  return MythUIText::ParseElement(filename, element, showWarnings);
102  }
103 
104  return true;
105 }
106 
111 {
112  auto *clock = dynamic_cast<MythUIClock *>(base);
113 
114  if (!clock)
115  {
116  LOG(VB_GENERAL, LOG_ERR, "ERROR, bad parsing");
117  return;
118  }
119 
120  m_Time = clock->m_Time;
121  m_nextUpdate = clock->m_nextUpdate;
122 
123  m_Format = clock->m_Format;
124  m_TimeFormat = clock->m_TimeFormat;
125  m_DateFormat = clock->m_DateFormat;
126  m_ShortDateFormat = clock->m_ShortDateFormat;
127 
128  m_Flash = clock->m_Flash;
129 
130  MythUIText::CopyFrom(base);
131 }
132 
137 {
138  auto *clock = new MythUIClock(parent, objectName());
139  clock->CopyFrom(this);
140 }
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.
All purpose text widget, displays a text string.
Definition: mythuitext.h:28
virtual void SetText(const QString &text)
Definition: mythuitext.cpp:135
QString m_TimeFormat
Definition: mythuiclock.h:46
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
QString m_Message
Definition: mythuitext.h:116
QString m_ShortDateFormat
Definition: mythuiclock.h:48
The base class on which all widgets and screens are based.
Definition: mythuitype.h:63
MythUIClock(MythUIType *parent, const QString &name)
Definition: mythuiclock.cpp:16
void Pulse(void) override
Pulse is called 70 times a second to trigger a single frame of an animation.
QString m_DateFormat
Definition: mythuiclock.h:47
QDateTime current(bool stripped)
Returns current Date and Time in UTC.
Definition: mythdate.cpp:10
QDateTime m_Time
Definition: mythuiclock.h:42
MythFontProperties * m_Font
Definition: mythuitext.h:138
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:84
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
QString GetSetting(const QString &_key, const QString &defaultval)
Definition: mythdb.cpp:362
static QString parseText(QDomElement &element)
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.
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: mythlogging.h:41
QLocale GetQLocale(void)
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:68
void CopyFrom(MythUIType *base) override
Copy this widgets state from another.
bool m_Flash
Definition: mythuiclock.h:50
MythDB * GetMythDB(void)
Definition: mythdb.cpp:46
QString m_Format
Definition: mythuiclock.h:45
QDateTime m_nextUpdate
Definition: mythuiclock.h:43