MythTV  master
mythprogressdialog.cpp
Go to the documentation of this file.
1 
2 
3 #include "mythprogressdialog.h"
4 
5 // libmythbase headers
6 #include "mythlogging.h"
7 
8 // libmythui headers
9 #include "mythuitext.h"
10 #include "mythuiprogressbar.h"
11 
13  (QEvent::Type) QEvent::registerEventType();
14 
15 // Force this class to have a vtable so that dynamic_cast works.
16 // NOLINTNEXTLINE(modernize-use-equals-default)
18 {
19 }
20 
21 MythUIBusyDialog::MythUIBusyDialog(const QString &message,
22  MythScreenStack *parent, const char *name)
23  : MythScreenType(parent, name, false)
24 {
25  if (!message.isEmpty())
26  m_message = message;
27  else
28  m_message = tr("Please Wait...");
30 }
31 
33 {
34  if (!CopyWindowFromBase("MythBusyDialog", this))
35  return false;
36 
37  m_messageText = dynamic_cast<MythUIText *>(GetChild("message"));
38 
39  if (m_messageText)
41 
42  return true;
43 }
44 
45 void MythUIBusyDialog::SetMessage(const QString &message)
46 {
47  m_newMessageLock.lock();
48  m_newMessage = message;
49  m_haveNewMessage = true;
50  m_newMessageLock.unlock();
51 }
52 
54 {
56 }
57 
59 {
61  {
62  m_newMessageLock.lock();
65  m_newMessageLock.unlock();
66  }
67 
69 }
70 
71 bool MythUIBusyDialog::keyPressEvent(QKeyEvent *event)
72 {
73  QStringList actions;
74  bool handled = GetMythMainWindow()->TranslateKeyPress("qt", event, actions, false);
75 
76  for (int i = 0; i < actions.size() && !handled; i++)
77  {
78  QString action = actions[i];
79  handled = true;
80 
81  if (action == "ESCAPE")
82  {
83  // eat the escape keypress
84  }
85  else
86  handled = false;
87  }
88 
89  if (!handled && MythScreenType::keyPressEvent(event))
90  handled = true;
91 
92  return handled;
93 }
94 
95 MythUIBusyDialog *ShowBusyPopup(const QString &message)
96 {
97  QString LOC = "ShowBusyPopup('" + message + "') - ";
98  MythUIBusyDialog *pop = nullptr;
99  static MythScreenStack *s_stk = nullptr;
100 
101 
102  if (!s_stk)
103  {
105 
106  if (win)
107  s_stk = win->GetStack("popup stack");
108  else
109  {
110  LOG(VB_GENERAL, LOG_ERR, LOC + "no main window?");
111  return nullptr;
112  }
113 
114  if (!s_stk)
115  {
116  LOG(VB_GENERAL, LOG_ERR, LOC + "no popup stack? "
117  "Is there a MythThemeBase?");
118  return nullptr;
119  }
120  }
121 
122  pop = new MythUIBusyDialog(message, s_stk, "showBusyPopup");
123  if (pop->Create())
124  s_stk->AddScreen(pop);
125 
126  return pop;
127 }
128 //---------------------------------------------------------
129 
131 {
132  if (!CopyWindowFromBase("MythProgressDialog", this))
133  return false;
134 
135  m_messageText = dynamic_cast<MythUIText *>(GetChild("message"));
136  m_progressText = dynamic_cast<MythUIText *>(GetChild("progresstext"));
137  m_progressBar = dynamic_cast<MythUIProgressBar *>(GetChild("progressbar"));
138 
139  if (m_messageText)
141 
142  return true;
143 }
144 
146 {
147  QStringList actions;
148  bool handled = GetMythMainWindow()->TranslateKeyPress("qt", event, actions, false);
149 
150  for (int i = 0; i < actions.size() && !handled; i++)
151  {
152  QString action = actions[i];
153  handled = true;
154 
155  if (action == "ESCAPE")
156  {
157  // eat the escape keypress
158  }
159  else
160  handled = false;
161  }
162 
163  if (!handled && MythScreenType::keyPressEvent(event))
164  handled = true;
165 
166  return handled;
167 }
168 
170 {
171  if (event->type() == ProgressUpdateEvent::kEventType)
172  {
173  auto *pue = dynamic_cast<ProgressUpdateEvent*>(event);
174  if (!pue)
175  {
176  LOG(VB_GENERAL, LOG_ERR,
177  "Error, event claims to be a progress update but fails "
178  "to cast");
179  return;
180  }
181 
182  QString message = pue->GetMessage();
183  if (!message.isEmpty())
184  m_message = message;
185  uint total = pue->GetTotal();
186  if (total > 0)
187  m_total = total;
188  m_count = pue->GetCount();
189  UpdateProgress();
190  }
191 }
192 
194 {
195  m_total = total;
196  UpdateProgress();
197 }
198 
200 {
201  m_count = count;
202  UpdateProgress();
203 }
204 
205 void MythUIProgressDialog::SetMessage(const QString &message)
206 {
207  m_message = message;
208  UpdateProgress();
209 }
210 
212 {
213  if (m_messageText)
215 
216  if (m_total == 0)
217  return;
218 
219  if (m_count > m_total)
220  {
221  LOG(VB_GENERAL, LOG_ERR, QString("Progress count (%1) is higher "
222  "than total (%2)")
223  .arg(m_count) .arg(m_total));
224  return;
225  }
226 
227  if (m_progressBar)
228  {
231  }
232 
233  uint percentage = (uint)(((float)m_count/(float)m_total) * 100.0F);
234 
235  if (m_progressText)
236  m_progressText->SetText(QString("%1%").arg(percentage));
237 }
void SetMessage(const QString &message)
void SetUsed(int value)
void SetMessage(const QString &message)
bool TranslateKeyPress(const QString &context, QKeyEvent *e, QStringList &actions, bool allowJumps=true)
Get a list of actions for a keypress in the given context.
void SetProgress(uint count)
bool Create(void) override
bool keyPressEvent(QKeyEvent *event) override
Key event handler.
#define LOC
void Reset(void) override
Reset the widget to it's original state, should not reset changes made by the theme.
virtual void SetText(const QString &text)
Definition: mythuitext.cpp:135
MythScreenStack * GetStack(const QString &stackname)
void Pulse(void) override
Pulse is called 70 times a second to trigger a single frame of an animation.
MythUIText * m_messageText
virtual void AddScreen(MythScreenType *screen, bool allowFade=true)
VERBOSE_PREAMBLE false
Definition: verbosedefs.h:85
bool Create(void) override
MythUIProgressBar * m_progressBar
static bool CopyWindowFromBase(const QString &windowname, MythScreenType *win)
virtual void Pulse(void)
Pulse is called 70 times a second to trigger a single frame of an animation.
Definition: mythuitype.cpp:440
void SetTotal(int value)
unsigned int uint
Definition: compat.h:140
MythUIBusyDialog(const QString &message, MythScreenStack *parent, const char *name)
MythMainWindow * GetMythMainWindow(void)
MythUIBusyDialog * ShowBusyPopup(const QString &message)
bool keyPressEvent(QKeyEvent *event) override
Key event handler.
bool keyPressEvent(QKeyEvent *event) override
Key event handler.
void customEvent(QEvent *event) override
Screen in which all other widgets are contained and rendered.
MythUIType * GetChild(const QString &name) const
Get a named child of this UIType.
Definition: mythuitype.cpp:130
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:23