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  {
183  fromgenre = QString("LEFT JOIN programgenres ON "
184  "program.chanid = programgenres.chanid AND "
185  "program.starttime = programgenres.starttime ");
186  }
187 
188  if (what.isEmpty())
189  return;
190 
191  MSqlEscapeAsAQuery(what, bindings);
192  }
193 
194  auto *record = new RecordingRule();
195 
196  record->LoadBySearch(m_searchType, text, what, fromgenre);
197 
199  auto *schededit = new ScheduleEditor(mainStack, record);
200  if (schededit->Create())
201  {
202  mainStack->AddScreen(schededit);
203  okClicked();
204  }
205  else
206  delete schededit;
207 }
208 
210 
212 {
213  if (!LoadWindowFromXML("schedule-ui.xml", "powersearchpopup", this))
214  return false;
215 
216  bool err = false;
217  UIUtilE::Assign(this, m_titleText, "title_text", &err);
218  UIUtilE::Assign(this, m_phraseList, "phrase_list", &err);
219  UIUtilE::Assign(this, m_editButton, "edit_button", &err);
220  UIUtilE::Assign(this, m_deleteButton, "delete_button", &err);
221  UIUtilE::Assign(this, m_recordButton, "record_button", &err);
222 
223  if (err)
224  {
225  LOG(VB_GENERAL, LOG_ERR, "Cannot load screen 'powersearchpopup'");
226  return false;
227  }
228 
229  m_titleText->SetText(tr("Select Search"));
230  new MythUIButtonListItem(m_phraseList, tr("<New Search>"), nullptr, false);
231 
232  for (int x = 0; x < m_list.size(); x++)
233  {
234  new MythUIButtonListItem(m_phraseList, m_list.at(x), nullptr, false);
235  }
236 
237  connect(m_phraseList, SIGNAL(itemClicked(MythUIButtonListItem*)),
238  this, SLOT(phraseClicked(MythUIButtonListItem*)));
239  connect(m_phraseList, SIGNAL(itemSelected(MythUIButtonListItem*)),
240  this, SLOT(phraseSelected(MythUIButtonListItem*)));
241 
242 
244 
245  m_editButton->SetText(tr("Edit"));
246  m_deleteButton->SetText(tr("Delete"));
247  m_recordButton->SetText(tr("Record"));
248 
249  connect(m_editButton, SIGNAL(Clicked()), this, SLOT(editClicked()));
250  connect(m_deleteButton, SIGNAL(Clicked()), this, SLOT(deleteClicked()));
251  connect(m_recordButton, SIGNAL(Clicked()), this, SLOT(recordClicked()));
252 
253  BuildFocusList();
254 
256 
257  return true;
258 }
259 
261 {
262  if (!item)
263  return;
264 
265  int pos = m_phraseList->GetCurrentPos();
266 
267  if (pos == 0)
268  editClicked();
269  else
270  {
272  Close();
273  }
274 }
275 
277 {
278  if (!item)
279  return;
280 
284 }
285 
287 {
288  MythScreenStack *popupStack = GetMythMainWindow()->GetStack("popup stack");
289 
290  QString currentItem = ":::::";
291 
292  if (m_phraseList->GetCurrentPos() != 0)
293  currentItem = m_phraseList->GetValue();
294 
295  auto *popup = new EditPowerSearchPopup(popupStack, m_parent, currentItem);
296 
297  if (!popup->Create())
298  {
299  delete popup;
300  return;
301  }
302 
303  popupStack->AddScreen(popup);
304 
305  Close();
306 }
307 
309 {
310  int view = m_phraseList->GetCurrentPos() - 1;
311 
312  if (view < 0)
313  return;
314 
315  QString text = m_list[view];
316  const QString& qphrase = text;
317 
318  MSqlQuery query(MSqlQuery::InitCon());
319  query.prepare("DELETE FROM keyword "
320  "WHERE phrase = :PHRASE AND searchtype = :TYPE;");
321  query.bindValue(":PHRASE", qphrase);
322  query.bindValue(":TYPE", m_searchType);
323  if (!query.exec())
324  MythDB::DBError("PowerSearchPopup::deleteClicked", query);
325 
327 
328  m_parent->m_viewList.removeAll(text);
329  m_parent->m_viewTextList.removeAll(text);
330 
331  if (view < m_parent->m_curView)
332  m_parent->m_curView--;
333  else if (view == m_parent->m_curView)
334  m_parent->m_curView = -1;
335 
336  if (m_parent->m_viewList.count() < 1)
338  else
340 }
341 
343 {
344  QString text = m_phraseList->GetValue();
345  QString what = text;
346  QString fromgenre;
347 
348  if (text.trimmed().isEmpty())
349  return;
350 
351  if (m_searchType == kNoSearch)
352  {
353  LOG(VB_GENERAL, LOG_ERR, "Unknown search in ProgLister");
354  return;
355  }
356 
357  if (m_searchType == kPowerSearch)
358  {
359  if (text == ":::::")
360  return;
361 
362  MSqlBindings bindings;
363  if (ProgLister::PowerStringToSQL(text, what, bindings))
364  {
365  fromgenre = QString(
366  "LEFT JOIN programgenres ON "
367  "program.chanid = programgenres.chanid AND "
368  "program.starttime = programgenres.starttime ");
369  }
370 
371  if (what.isEmpty())
372  return;
373 
374  MSqlEscapeAsAQuery(what, bindings);
375  }
376 
377  auto *record = new RecordingRule();
378 
379  record->LoadBySearch(m_searchType, text, what, fromgenre);
380 
382  auto *schededit = new ScheduleEditor(mainStack, record);
383  if (schededit->Create())
384  {
385  mainStack->AddScreen(schededit);
387  Close();
388  }
389  else
390  delete schededit;
391 }
392 
394 
396  ProgLister *parent,
397  const QString &currentValue)
398  : MythScreenType(parentStack, "phrasepopup")
399 {
400  m_parent = parent;
401 
402  //sanity check currentvalue
403  m_currentValue = currentValue;
404  QStringList field = m_currentValue.split(':');
405  if (field.count() != 6)
406  {
407  LOG(VB_GENERAL, LOG_ERR, QString("Error. PowerSearch %1 has %2 fields")
408  .arg(m_currentValue).arg(field.count()));
409  m_currentValue = ":::::";
410  }
411 }
412 
414 {
415  if (!LoadWindowFromXML("schedule-ui.xml", "editpowersearchpopup", this))
416  return false;
417 
418  bool err = false;
419  UIUtilE::Assign(this, m_titleEdit, "title_edit", &err);
420  UIUtilE::Assign(this, m_subtitleEdit, "subtitle_edit", &err);
421  UIUtilE::Assign(this, m_descEdit, "desc_edit", &err);
422  UIUtilE::Assign(this, m_categoryList, "category_list", &err);
423  UIUtilE::Assign(this, m_genreList, "genre_list", &err);
424  UIUtilE::Assign(this, m_channelList, "channel_list", &err);
425  UIUtilE::Assign(this, m_okButton, "ok_button", &err);
426 
427  if (err)
428  {
429  LOG(VB_GENERAL, LOG_ERR, "Cannot load screen 'editpowersearchpopup'");
430  return false;
431  }
432 
433  QStringList field = m_currentValue.split(':');
434 
435  m_titleEdit->SetText(field[0]);
436  m_subtitleEdit->SetText(field[1]);
437  m_descEdit->SetText(field[2]);
438 
439  initLists();
440 
441  connect(m_okButton, SIGNAL(Clicked()), this, SLOT(okClicked()));
442 
443  BuildFocusList();
444 
446 
447  return true;
448 }
449 
451 {
452  QString text;
453  text = m_titleEdit->GetText().replace(':','%').replace('*','%') + ':';
454  text += m_subtitleEdit->GetText().replace(':','%').replace('*','%') + ':';
455  text += m_descEdit->GetText().replace(':','%').replace('*','%') + ':';
456 
457  if (m_categoryList->GetCurrentPos() > 0)
459  text += ':';
460  if (m_genreList->GetCurrentPos() > 0)
461  text += m_genres[m_genreList->GetCurrentPos()];
462  text += ':';
463  if (m_channelList->GetCurrentPos() > 0)
465 
466  if (text == ":::::")
467  return;
468 
470  m_parent->FillViewList(text);
471  m_parent->SetViewFromList(text);
472 
473  Close();
474 }
475 
477 {
478  QStringList field = m_currentValue.split(':');
479 
480  // category type
481  m_categories.clear();
483  m_categoryList, tr("(Any Program Type)"), nullptr, false);
484  m_categories << "";
485  new MythUIButtonListItem(m_categoryList, tr("Movies"), nullptr, false);
486  m_categories << "movie";
487  new MythUIButtonListItem(m_categoryList, tr("Series"), nullptr, false);
488  m_categories << "series";
489  new MythUIButtonListItem(m_categoryList, tr("Show"), nullptr, false);
490  m_categories << "tvshow";
491  new MythUIButtonListItem(m_categoryList, tr("Sports"), nullptr, false);
492  m_categories << "sports";
493  m_categoryList->SetItemCurrent(m_categories.indexOf(field[3]));
494 
495  // genre
496  m_genres.clear();
497  new MythUIButtonListItem(m_genreList, tr("(Any Genre)"), nullptr, false);
498  m_genres << "";
499 
500  MSqlQuery query(MSqlQuery::InitCon());
501 
502  query.prepare("SELECT genre FROM programgenres GROUP BY genre;");
503 
504  if (query.exec())
505  {
506  while (query.next())
507  {
508  QString category = query.value(0).toString();
509  if (category.isEmpty() || category.trimmed().isEmpty())
510  continue;
511  category = query.value(0).toString();
512  new MythUIButtonListItem(m_genreList, category, nullptr, false);
513  m_genres << category;
514  if (category == field[4])
516  }
517  }
518 
519  // channel
520  QString channelOrdering = gCoreContext->GetSetting(
521  "ChannelOrdering", "channum");
522 
523  m_channels.clear();
524  new MythUIButtonListItem(m_channelList, tr("(Any Channel)"), nullptr, false);
525  m_channels << "";
526 
527  ChannelInfoList channels = ChannelUtil::GetChannels(0, true, "callsign");
528  ChannelUtil::SortChannels(channels, channelOrdering, true);
529 
530  for (auto & channel : channels)
531  {
532  QString chantext = channel.GetFormatted(ChannelInfo::kChannelShort);
533 
534  m_parent->m_viewList << QString::number(channel.m_chanId);
535  m_parent->m_viewTextList << chantext;
536 
537  auto *item = new MythUIButtonListItem(m_channelList, chantext,
538  nullptr, false);
539 
540  InfoMap chanmap;
541  channel.ToMap(chanmap);
542  item->SetTextFromMap(chanmap);
543 
544  m_channels << channel.m_callSign;
545  if (channel.m_callSign == field[5])
547  }
548 }
int m_curView
Definition: proglist.h:108
bool next(void)
Wrap QSqlQuery::next() so we can display the query results.
Definition: mythdbcon.cpp:783
void phraseSelected(MythUIButtonListItem *item)
void bindValue(const QString &placeholder, const QVariant &val)
Add a single binding.
Definition: mythdbcon.cpp:864
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:527
MythUIButton * m_recordButton
void SetEnabled(bool enable)
void FillViewList(const QString &view)
Definition: proglist.cpp:725
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:520
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:928
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:239
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
Internal representation of a recording rule, mirrors the record table.
Definition: recordingrule.h:32
static MSqlQueryInfo InitCon(ConnectionReuse _reuse=kNormalConnection)
Only use this in combination with MSqlQuery constructor.
Definition: mythdbcon.cpp:535
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:808
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:371
MythUIButton * m_editButton
void SetText(const QString &msg)
vector< ChannelInfo > ChannelInfoList
Definition: channelinfo.h:133
MythUIButton * m_deleteButton
QString GetText(void) const
void okClicked(void)
MythUIButtonList * m_phraseList
MythUIButtonListItem * GetItemCurrent() const
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:41