MythTV  master
proglist_helpers.cpp
Go to the documentation of this file.
1 // MythTV
2 #include "mythuibuttonlist.h"
3 #include "proglist_helpers.h"
4 #include "mythcorecontext.h"
5 #include "mythuitextedit.h"
6 #include "scheduleeditor.h"
7 #include "recordingrule.h"
8 #include "mythuibutton.h"
9 #include "channelinfo.h"
10 #include "channelutil.h"
11 #include "mythuitext.h"
12 #include "proglist.h"
13 #include "mythdate.h"
14 
16 {
17  if (!LoadWindowFromXML("schedule-ui.xml", "phrasepopup", this))
18  return false;
19 
20  bool err = false;
21  UIUtilE::Assign(this, m_titleText, "title_text", &err);
22  UIUtilE::Assign(this, m_phraseList, "phrase_list", &err);
23  UIUtilE::Assign(this, m_phraseEdit, "phrase_edit", &err);
24  UIUtilE::Assign(this, m_okButton, "ok_button", &err);
25  UIUtilE::Assign(this, m_deleteButton, "delete_button", &err);
26  UIUtilE::Assign(this, m_recordButton, "record_button", &err);
27 
28  if (err)
29  {
30  LOG(VB_GENERAL, LOG_ERR, "Cannot load screen 'phrasepopup'");
31  return false;
32  }
33 
35  {
36  m_titleText->SetText(tr("Select Search"));
37  new MythUIButtonListItem(m_phraseList, tr("<New Search>"), nullptr, false);
38  m_okButton->SetText(tr("Edit"));
39  }
40  else
41  {
42  m_titleText->SetText(tr("Phrase"));
43  new MythUIButtonListItem(m_phraseList, tr("<New Phrase>"), nullptr, false);
44  }
45 
46  for (int x = 0; x < m_list.size(); x++)
47  {
48  new MythUIButtonListItem(m_phraseList, m_list.at(x), nullptr, false);
49  }
50 
51  connect(m_phraseList, SIGNAL(itemClicked(MythUIButtonListItem*)),
52  this, SLOT(phraseClicked(MythUIButtonListItem*)));
53  connect(m_phraseList, SIGNAL(itemSelected(MythUIButtonListItem*)),
54  this, SLOT(phraseSelected(MythUIButtonListItem*)));
55 
56 
58 
59  m_deleteButton->SetText(tr("Delete"));
60  m_recordButton->SetText(tr("Record"));
61 
62  connect(m_okButton, SIGNAL(Clicked()), this, SLOT(okClicked()));
63  connect(m_deleteButton, SIGNAL(Clicked()), this, SLOT(deleteClicked()));
64  connect(m_recordButton, SIGNAL(Clicked()), this, SLOT(recordClicked()));
65 
66  connect(m_phraseEdit, SIGNAL(valueChanged()), this, SLOT(editChanged()));
67 
69 
71 
72  return true;
73 }
74 
76 {
77  m_okButton->SetEnabled(!m_phraseEdit->GetText().trimmed().isEmpty());
79  (m_list.indexOf(m_phraseEdit->GetText().trimmed()) != -1));
80  m_recordButton->SetEnabled(!m_phraseEdit->GetText().trimmed().isEmpty());
81 }
82 
84 {
85  if (!item)
86  return;
87 
88  int pos = m_phraseList->GetCurrentPos();
89 
90  if (pos == 0)
92  else
93  okClicked();
94 }
95 
97 {
98  if (!item)
99  return;
100 
101  if (m_phraseList->GetCurrentPos() == 0)
102  m_phraseEdit->Reset();
103  else
104  m_phraseEdit->SetText(item->GetText());
105 
109 }
110 
112 {
113  if (m_phraseEdit->GetText().trimmed().isEmpty())
114  return;
115 
116  // check to see if we need to save the phrase
119 
120 // emit haveResult(m_phraseList->GetCurrentPos());
122 
123  Close();
124 }
125 
127 {
128  int view = m_phraseList->GetCurrentPos() - 1;
129 
130  if (view < 0)
131  return;
132 
133  QString text = m_list[view];
134  const QString& qphrase = text;
135 
136  MSqlQuery query(MSqlQuery::InitCon());
137  query.prepare("DELETE FROM keyword "
138  "WHERE phrase = :PHRASE AND searchtype = :TYPE;");
139  query.bindValue(":PHRASE", qphrase);
140  query.bindValue(":TYPE", m_searchType);
141  if (!query.exec())
142  MythDB::DBError("PhrasePopup::deleteClicked", query);
143 
145 
146  m_parent->m_viewList.removeAll(text);
147  m_parent->m_viewTextList.removeAll(text);
148 
149  if (view < m_parent->m_curView)
150  m_parent->m_curView--;
151  else if (view == m_parent->m_curView)
152  m_parent->m_curView = -1;
153 
154  if (m_parent->m_viewList.count() < 1)
156  else
158 }
159 
161 {
162  QString text = m_phraseEdit->GetText();
163  QString what = text;
164  QString fromgenre;
165 
166  if (text.trimmed().isEmpty())
167  return;
168 
169  if (m_searchType == kNoSearch)
170  {
171  LOG(VB_GENERAL, LOG_ERR, "Unknown search in ProgLister");
172  return;
173  }
174 
175  if (m_searchType == kPowerSearch)
176  {
177  if (text == ":::::")
178  return;
179 
180  MSqlBindings bindings;
181  if (ProgLister::PowerStringToSQL(text, what, bindings))
182  fromgenre = QString("LEFT JOIN programgenres ON "
183  "program.chanid = programgenres.chanid AND "
184  "program.starttime = programgenres.starttime ");
185 
186  if (what.isEmpty())
187  return;
188 
189  MSqlEscapeAsAQuery(what, bindings);
190  }
191 
192  auto *record = new RecordingRule();
193 
194  record->LoadBySearch(m_searchType, text, what, fromgenre);
195 
197  auto *schededit = new ScheduleEditor(mainStack, record);
198  if (schededit->Create())
199  {
200  mainStack->AddScreen(schededit);
201  okClicked();
202  }
203  else
204  delete schededit;
205 }
206 
208 
210 {
211  if (!LoadWindowFromXML("schedule-ui.xml", "powersearchpopup", this))
212  return false;
213 
214  bool err = false;
215  UIUtilE::Assign(this, m_titleText, "title_text", &err);
216  UIUtilE::Assign(this, m_phraseList, "phrase_list", &err);
217  UIUtilE::Assign(this, m_editButton, "edit_button", &err);
218  UIUtilE::Assign(this, m_deleteButton, "delete_button", &err);
219  UIUtilE::Assign(this, m_recordButton, "record_button", &err);
220 
221  if (err)
222  {
223  LOG(VB_GENERAL, LOG_ERR, "Cannot load screen 'powersearchpopup'");
224  return false;
225  }
226 
227  m_titleText->SetText(tr("Select Search"));
228  new MythUIButtonListItem(m_phraseList, tr("<New Search>"), nullptr, false);
229 
230  for (int x = 0; x < m_list.size(); x++)
231  {
232  new MythUIButtonListItem(m_phraseList, m_list.at(x), nullptr, false);
233  }
234 
235  connect(m_phraseList, SIGNAL(itemClicked(MythUIButtonListItem*)),
236  this, SLOT(phraseClicked(MythUIButtonListItem*)));
237  connect(m_phraseList, SIGNAL(itemSelected(MythUIButtonListItem*)),
238  this, SLOT(phraseSelected(MythUIButtonListItem*)));
239 
240 
242 
243  m_editButton->SetText(tr("Edit"));
244  m_deleteButton->SetText(tr("Delete"));
245  m_recordButton->SetText(tr("Record"));
246 
247  connect(m_editButton, SIGNAL(Clicked()), this, SLOT(editClicked()));
248  connect(m_deleteButton, SIGNAL(Clicked()), this, SLOT(deleteClicked()));
249  connect(m_recordButton, SIGNAL(Clicked()), this, SLOT(recordClicked()));
250 
251  BuildFocusList();
252 
254 
255  return true;
256 }
257 
259 {
260  if (!item)
261  return;
262 
263  int pos = m_phraseList->GetCurrentPos();
264 
265  if (pos == 0)
266  editClicked();
267  else
268  {
270  Close();
271  }
272 }
273 
275 {
276  if (!item)
277  return;
278 
282 }
283 
285 {
286  MythScreenStack *popupStack = GetMythMainWindow()->GetStack("popup stack");
287 
288  QString currentItem = ":::::";
289 
290  if (m_phraseList->GetCurrentPos() != 0)
291  currentItem = m_phraseList->GetValue();
292 
293  auto *popup = new EditPowerSearchPopup(popupStack, m_parent, currentItem);
294 
295  if (!popup->Create())
296  {
297  delete popup;
298  return;
299  }
300 
301  popupStack->AddScreen(popup);
302 
303  Close();
304 }
305 
307 {
308  int view = m_phraseList->GetCurrentPos() - 1;
309 
310  if (view < 0)
311  return;
312 
313  QString text = m_list[view];
314  const QString& qphrase = text;
315 
316  MSqlQuery query(MSqlQuery::InitCon());
317  query.prepare("DELETE FROM keyword "
318  "WHERE phrase = :PHRASE AND searchtype = :TYPE;");
319  query.bindValue(":PHRASE", qphrase);
320  query.bindValue(":TYPE", m_searchType);
321  if (!query.exec())
322  MythDB::DBError("PowerSearchPopup::deleteClicked", query);
323 
325 
326  m_parent->m_viewList.removeAll(text);
327  m_parent->m_viewTextList.removeAll(text);
328 
329  if (view < m_parent->m_curView)
330  m_parent->m_curView--;
331  else if (view == m_parent->m_curView)
332  m_parent->m_curView = -1;
333 
334  if (m_parent->m_viewList.count() < 1)
336  else
338 }
339 
341 {
342  QString text = m_phraseList->GetValue();
343  QString what = text;
344  QString fromgenre;
345 
346  if (text.trimmed().isEmpty())
347  return;
348 
349  if (m_searchType == kNoSearch)
350  {
351  LOG(VB_GENERAL, LOG_ERR, "Unknown search in ProgLister");
352  return;
353  }
354 
355  if (m_searchType == kPowerSearch)
356  {
357  if (text == ":::::")
358  return;
359 
360  MSqlBindings bindings;
361  if (ProgLister::PowerStringToSQL(text, what, bindings))
362  fromgenre = QString(
363  "LEFT JOIN programgenres ON "
364  "program.chanid = programgenres.chanid AND "
365  "program.starttime = programgenres.starttime ");
366 
367  if (what.isEmpty())
368  return;
369 
370  MSqlEscapeAsAQuery(what, bindings);
371  }
372 
373  auto *record = new RecordingRule();
374 
375  record->LoadBySearch(m_searchType, text, what, fromgenre);
376 
378  auto *schededit = new ScheduleEditor(mainStack, record);
379  if (schededit->Create())
380  {
381  mainStack->AddScreen(schededit);
383  Close();
384  }
385  else
386  delete schededit;
387 }
388 
390 
392  ProgLister *parent,
393  const QString &currentValue)
394  : MythScreenType(parentStack, "phrasepopup")
395 {
396  m_parent = parent;
397 
398  //sanity check currentvalue
399  m_currentValue = currentValue;
400  QStringList field = m_currentValue.split(':');
401  if (field.count() != 6)
402  {
403  LOG(VB_GENERAL, LOG_ERR, QString("Error. PowerSearch %1 has %2 fields")
404  .arg(m_currentValue).arg(field.count()));
405  m_currentValue = ":::::";
406  }
407 }
408 
410 {
411  if (!LoadWindowFromXML("schedule-ui.xml", "editpowersearchpopup", this))
412  return false;
413 
414  bool err = false;
415  UIUtilE::Assign(this, m_titleEdit, "title_edit", &err);
416  UIUtilE::Assign(this, m_subtitleEdit, "subtitle_edit", &err);
417  UIUtilE::Assign(this, m_descEdit, "desc_edit", &err);
418  UIUtilE::Assign(this, m_categoryList, "category_list", &err);
419  UIUtilE::Assign(this, m_genreList, "genre_list", &err);
420  UIUtilE::Assign(this, m_channelList, "channel_list", &err);
421  UIUtilE::Assign(this, m_okButton, "ok_button", &err);
422 
423  if (err)
424  {
425  LOG(VB_GENERAL, LOG_ERR, "Cannot load screen 'editpowersearchpopup'");
426  return false;
427  }
428 
429  QStringList field = m_currentValue.split(':');
430 
431  m_titleEdit->SetText(field[0]);
432  m_subtitleEdit->SetText(field[1]);
433  m_descEdit->SetText(field[2]);
434 
435  initLists();
436 
437  connect(m_okButton, SIGNAL(Clicked()), this, SLOT(okClicked()));
438 
439  BuildFocusList();
440 
442 
443  return true;
444 }
445 
447 {
448  QString text;
449  text = m_titleEdit->GetText().replace(':','%').replace('*','%') + ':';
450  text += m_subtitleEdit->GetText().replace(':','%').replace('*','%') + ':';
451  text += m_descEdit->GetText().replace(':','%').replace('*','%') + ':';
452 
453  if (m_categoryList->GetCurrentPos() > 0)
455  text += ':';
456  if (m_genreList->GetCurrentPos() > 0)
457  text += m_genres[m_genreList->GetCurrentPos()];
458  text += ':';
459  if (m_channelList->GetCurrentPos() > 0)
461 
462  if (text == ":::::")
463  return;
464 
466  m_parent->FillViewList(text);
467  m_parent->SetViewFromList(text);
468 
469  Close();
470 }
471 
473 {
474  QStringList field = m_currentValue.split(':');
475 
476  // category type
477  m_categories.clear();
479  m_categoryList, tr("(Any Program Type)"), nullptr, false);
480  m_categories << "";
481  new MythUIButtonListItem(m_categoryList, tr("Movies"), nullptr, false);
482  m_categories << "movie";
483  new MythUIButtonListItem(m_categoryList, tr("Series"), nullptr, false);
484  m_categories << "series";
485  new MythUIButtonListItem(m_categoryList, tr("Show"), nullptr, false);
486  m_categories << "tvshow";
487  new MythUIButtonListItem(m_categoryList, tr("Sports"), nullptr, false);
488  m_categories << "sports";
489  m_categoryList->SetItemCurrent(m_categories.indexOf(field[3]));
490 
491  // genre
492  m_genres.clear();
493  new MythUIButtonListItem(m_genreList, tr("(Any Genre)"), nullptr, false);
494  m_genres << "";
495 
496  MSqlQuery query(MSqlQuery::InitCon());
497 
498  query.prepare("SELECT genre FROM programgenres GROUP BY genre;");
499 
500  if (query.exec())
501  {
502  while (query.next())
503  {
504  QString category = query.value(0).toString();
505  if (category.isEmpty() || category.trimmed().isEmpty())
506  continue;
507  category = query.value(0).toString();
508  new MythUIButtonListItem(m_genreList, category, nullptr, false);
509  m_genres << category;
510  if (category == field[4])
512  }
513  }
514 
515  // channel
516  QString channelOrdering = gCoreContext->GetSetting(
517  "ChannelOrdering", "channum");
518 
519  m_channels.clear();
520  new MythUIButtonListItem(m_channelList, tr("(Any Channel)"), nullptr, false);
521  m_channels << "";
522 
523  ChannelInfoList channels = ChannelUtil::GetChannels(0, true, "callsign");
524  ChannelUtil::SortChannels(channels, channelOrdering, true);
525 
526  for (size_t i = 0; i < channels.size(); ++i)
527  {
528  QString chantext = channels[i].GetFormatted(ChannelInfo::kChannelShort);
529 
530  m_parent->m_viewList << QString::number(channels[i].m_chanid);
531  m_parent->m_viewTextList << chantext;
532 
533  auto *item = new MythUIButtonListItem(m_channelList, chantext,
534  nullptr, false);
535 
536  InfoMap chanmap;
537  channels[i].ToMap(chanmap);
538  item->SetTextFromMap(chanmap);
539 
540  m_channels << channels[i].m_callsign;
541  if (channels[i].m_callsign == field[5])
543  }
544 }
int m_curView
Definition: proglist.h:108
bool next(void)
Wrap QSqlQuery::next() so we can display the query results.
Definition: mythdbcon.cpp:782
void phraseSelected(MythUIButtonListItem *item)
void bindValue(const QString &placeholder, const QVariant &val)
Add a single binding.
Definition: mythdbcon.cpp:863
void haveResult(QString item)
ProgLister * m_parent
QStringList m_list
MythUITextEdit * m_subtitleEdit
RecSearchType m_searchType
static bool PowerStringToSQL(const QString &qphrase, QString &output, MSqlBindings &bindings)
Definition: proglist.cpp:523
MythUIButton * m_recordButton
void SetEnabled(bool enable)
void FillViewList(const QString &view)
Definition: proglist.cpp:721
QHash< QString, QString > InfoMap
Definition: mythtypes.h:15
MythUIButton * m_okButton
QSqlQuery wrapper that fetches a DB connection from the connection pool.
Definition: mythdbcon.h:125
MythUITextEdit * m_descEdit
void SetViewFromList(const QString &item)
Definition: proglist.cpp:516
void phraseClicked(MythUIButtonListItem *item)
virtual void SetText(const QString &text)
Definition: mythuitext.cpp:135
void phraseSelected(MythUIButtonListItem *item)
MythUIText * m_titleText
void haveResult(QString item)
MythScreenStack * GetStack(const QString &stackname)
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
MythScreenStack * GetMainStack()
MythUITextEdit * m_phraseEdit
RecSearchType m_searchType
static void SortChannels(ChannelInfoList &list, const QString &order, bool eliminate_duplicates=false)
bool Create() override
QString m_currentValue
void RemoveItem(MythUIButtonListItem *item)
void BuildFocusList(void)
bool MoveToNamedPosition(const QString &position_name)
void recordClicked(void)
MythUIButtonList * m_channelList
QVariant value(int i) const
Definition: mythdbcon.h:198
virtual void Close()
MythUIText * m_titleText
virtual void AddScreen(MythScreenType *screen, bool allowFade=true)
QMap< QString, QVariant > MSqlBindings
typedef for a map of string -> string bindings for generic queries.
Definition: mythdbcon.h:98
MythUIButtonList * m_genreList
void MSqlEscapeAsAQuery(QString &query, MSqlBindings &bindings)
Given a partial query string and a bindings object, escape the string.
Definition: mythdbcon.cpp:927
bool Create() override
Construct a recording schedule.
QString GetSetting(const QString &key, const QString &defaultval="")
void Reset(void) override
Reset the widget to it's original state, should not reset changes made by the theme.
static ChannelInfoList GetChannels(uint sourceid, bool visible_only, const QString &group_by=QString(), uint channel_groupid=0)
Definition: channelutil.h:237
QStringList m_list
MythUITextEdit * m_titleEdit
static bool Assign(ContainerType *container, UIType *&item, const QString &name, bool *err=nullptr)
Definition: mythuiutils.h:27
MythUITextEdit * m_phraseEdit
static MSqlQueryInfo InitCon(ConnectionReuse=kNormalConnection)
Only use this in combination with MSqlQuery constructor.
Definition: mythdbcon.cpp:535
Internal representation of a recording rule, mirrors the record table.
Definition: recordingrule.h:32
MythUIButton * m_okButton
MythUIButton * m_recordButton
MythUIButtonList * m_phraseList
static bool LoadWindowFromXML(const QString &xmlfile, const QString &windowname, MythUIType *parent)
void SetText(const QString &text, bool moveCursor=true)
QStringList m_viewList
Definition: proglist.h:109
MythMainWindow * GetMythMainWindow(void)
QString GetText(const QString &name="") const
virtual QString GetValue() const
void editChanged(void)
bool prepare(const QString &query)
QSqlQuery::prepare() is not thread safe in Qt <= 3.3.2.
Definition: mythdbcon.cpp:807
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: mythlogging.h:41
QStringList m_viewTextList
Definition: proglist.h:110
void deleteClicked(void)
EditPowerSearchPopup(MythScreenStack *parentStack, ProgLister *parent, const QString &currentValue)
ProgLister * m_parent
void phraseClicked(MythUIButtonListItem *item)
void SetItemCurrent(MythUIButtonListItem *item)
MythUIButtonList * m_categoryList
bool SetFocusWidget(MythUIType *widget=nullptr)
bool exec(void)
Wrap QSqlQuery::exec() so we can display SQL.
Definition: mythdbcon.cpp:603
MythUIButton * m_deleteButton
static void DBError(const QString &where, const MSqlQuery &query)
Definition: mythdb.cpp:179
Screen in which all other widgets are contained and rendered.
int GetCurrentPos() const
void UpdateKeywordInDB(const QString &text, const QString &oldValue)
Definition: proglist.cpp:367
MythUIButton * m_editButton
void SetText(const QString &msg)
vector< ChannelInfo > ChannelInfoList
Definition: channelinfo.h:121
MythUIButton * m_deleteButton
QString GetText(void) const
void okClicked(void)
MythUIButtonList * m_phraseList
MythUIButtonListItem * GetItemCurrent() const