MythTV  master
mythcontrols.cpp
Go to the documentation of this file.
1 // -*- Mode: c++ -*-
28 #include "mythcontrols.h"
29 
30 // Qt headers
31 #include <QStringList>
32 #include <QCoreApplication>
33 
34 // MythTV headers
35 #include "mythcorecontext.h"
36 #include "mythmainwindow.h"
37 
38 // MythUI headers
39 #include "mythuitext.h"
40 #include "mythuibutton.h"
41 #include "mythuibuttonlist.h"
42 #include "mythdialogbox.h"
43 
44 // MythControls headers
45 #include "keygrabber.h"
46 
47 #define LOC QString("MythControls: ")
48 #define LOC_ERR QString("MythControls, Error: ")
49 
51 {
52  Teardown();
53 }
54 
56 {
57  if (m_bindings)
58  {
59  delete m_bindings;
60  m_bindings = nullptr;
61  }
62 
63  m_contexts.clear();
64 }
65 
72 {
73  // Load the theme for this screen
74  bool foundtheme = LoadWindowFromXML("controls-ui.xml", "controls", this);
75  if (!foundtheme)
76  return false;
77 
78  m_description = dynamic_cast<MythUIText *>(GetChild("description"));
79  m_leftList = dynamic_cast<MythUIButtonList *>(GetChild("leftlist"));
80  m_rightList = dynamic_cast<MythUIButtonList *>(GetChild("rightlist"));
81  m_leftDescription = dynamic_cast<MythUIText *>(GetChild("leftdesc"));
82  m_rightDescription = dynamic_cast<MythUIText *>(GetChild("rightdesc"));
83 
84  if (!m_description || !m_leftList || !m_rightList ||
86  {
87  LOG(VB_GENERAL, LOG_ERR, "Theme is missing critical theme elements.");
88  return false;
89  }
90 
91  connect(m_leftList, SIGNAL(itemSelected(MythUIButtonListItem*)),
93  connect(m_leftList, SIGNAL(itemClicked(MythUIButtonListItem*)),
95 
96  connect(m_rightList, SIGNAL(itemSelected(MythUIButtonListItem*)),
98  connect(m_rightList, SIGNAL(itemClicked(MythUIButtonListItem*)),
100  connect(m_rightList, SIGNAL(TakingFocus()),
101  SLOT(RefreshKeyInformation()));
102 
103  for (uint i = 0; i < Action::kMaximumNumberOfBindings; i++)
104  {
105  MythUIButton *button = dynamic_cast<MythUIButton *>
106  (GetChild(QString("action_%1").arg(i)));
107 
108  if (!button)
109  {
110  LOG(VB_GENERAL, LOG_ERR, LOC +
111  QString("Unable to load action button action_%1").arg(i));
112 
113  return false;
114  }
115 
116  connect(button, SIGNAL(Clicked()), SLOT(ActionButtonPressed()));
117 
118  m_actionButtons.append(button);
119  }
120 
121  BuildFocusList();
122 
124 
125  /* start off with the actions by contexts view */
128  UpdateRightList();
129 
130  return true;
131 }
132 
139 {
141  return;
142 
143  if (direction == 0)
145 }
146 
151 {
152  (void) item;
153  NextPrevWidgetFocus(true);
154 }
155 
160 {
161  (void) item;
164 }
165 
170 {
171  QString key = GetCurrentKey();
172  if (!key.isEmpty())
173  {
174  QString label = tr("Modify Action");
175 
176  MythScreenStack *popupStack =
177  GetMythMainWindow()->GetStack("popup stack");
178 
179  m_menuPopup =
180  new MythDialogBox(label, popupStack, "actionmenu");
181 
182  if (m_menuPopup->Create())
183  popupStack->AddScreen(m_menuPopup);
184 
185  m_menuPopup->SetReturnEvent(this, "action");
186 
187  m_menuPopup->AddButton(tr("Set Binding"));
188  m_menuPopup->AddButton(tr("Remove Binding"));
189  }
190  else // for blank keys, no reason to ask what to do
191  GrabKey();
192 }
193 
198 {
199  QString label = tr("Change View");
200 
201  MythScreenStack *popupStack =
202  GetMythMainWindow()->GetStack("popup stack");
203 
204  m_menuPopup =
205  new MythDialogBox(label, popupStack, "mcviewmenu");
206 
207  if (m_menuPopup->Create())
208  popupStack->AddScreen(m_menuPopup);
209 
210  m_menuPopup->SetReturnEvent(this, "view");
211 
212  m_menuPopup->AddButton(tr("Actions By Context"));
213  m_menuPopup->AddButton(tr("Contexts By Key"));
214  m_menuPopup->AddButton(tr("Keys By Context"));
215 
216 }
217 
219 {
220  QString label = tr("Options");
221 
222  MythScreenStack *popupStack =
223  GetMythMainWindow()->GetStack("popup stack");
224 
225  m_menuPopup =
226  new MythDialogBox(label, popupStack, "optionmenu");
227 
228  if (m_menuPopup->Create())
229  popupStack->AddScreen(m_menuPopup);
230 
231  m_menuPopup->SetReturnEvent(this, "option");
232 
233  m_menuPopup->AddButton(tr("Save"));
234  m_menuPopup->AddButton(tr("Change View"));
235  m_menuPopup->AddButton(tr("Reset All Keys to Defaults"));
236 }
237 
239 {
240  if (m_bindings && m_bindings->HasChanges())
241  {
242  /* prompt user to save changes */
243  QString label = tr("Save changes?");
244 
245  MythScreenStack *popupStack =
246  GetMythMainWindow()->GetStack("popup stack");
247 
248  auto *confirmPopup = new MythConfirmationDialog(popupStack, label, true);
249 
250  if (confirmPopup->Create())
251  popupStack->AddScreen(confirmPopup);
252 
253  confirmPopup->SetReturnEvent(this, "exit");
254  }
255  else
257 }
258 
264 {
265  UpdateRightList();
266 }
267 
273 {
275 }
276 
277 
285  MythUIButtonList *uilist, const QStringList &contents, bool arrows)
286 {
287  // remove all strings from the current list
288  uilist->Reset();
289 
290  // add each new string
291  foreach (const auto & content, contents)
292  {
293  auto *item = new MythUIButtonListItem(uilist, content);
294  item->setDrawArrow(arrows);
295  }
296 }
297 
302 {
303  // get the selected item in the right list.
305 
306  if (!item)
307  return;
308 
309  QString rtstr = item->GetText();
310 
311  switch(m_currentView)
312  {
313  case kActionsByContext:
315  break;
316  case kKeysByContext:
318  break;
319  case kContextsByKey:
321  break;
322  }
323 }
324 
330 {
331  for (uint i = 0; i < Action::kMaximumNumberOfBindings; i++)
332  m_actionButtons.at(i)->SetText("");
333 
334  if (GetFocusWidget() == m_leftList)
335  {
336  m_description->Reset();
337  return;
338  }
339 
340  const QString context = GetCurrentContext();
341  const QString action = GetCurrentAction();
342 
343  QString desc = m_bindings->GetActionDescription(context, action);
344  m_description->SetText(tr(desc.toLatin1().constData()));
345 
346  QStringList keys = m_bindings->GetActionKeys(context, action);
347  for (int i = 0; (i < keys.count()) &&
348  (i < (int)Action::kMaximumNumberOfBindings); i++)
349  {
350  m_actionButtons.at(i)->SetText(keys[i]);
351  }
352 }
353 
354 
363 {
365  return m_leftList->GetItemCurrent()->GetText();
366 
367  if (GetFocusWidget() == m_leftList)
368  return QString();
369 
370  QString desc = m_rightList->GetItemCurrent()->GetText();
371  int loc = desc.indexOf(" => ");
372  if (loc == -1)
373  return QString(); // Should not happen
374 
376  return desc.left(loc);
377 
378  return desc.mid(loc + 4);
379 }
380 
389 {
391  {
393  {
394  return m_leftList->GetItemCurrent()->GetText();
395  }
396  return QString();
397  }
398 
399  if (GetFocusWidget() == m_leftList)
400  return QString();
401 
403  return QString();
404 
405  QString desc = m_rightList->GetItemCurrent()->GetText();
406  if (kContextList == m_leftListType &&
408  {
409  return desc;
410  }
411 
412  int loc = desc.indexOf(" => ");
413  if (loc == -1)
414  return QString(); // should not happen..
415 
417  return desc.left(loc);
418 
419  QString rv = desc.mid(loc+4);
420  if (rv == "<none>")
421  return QString();
422 
423  return rv;
424 }
425 
431 {
432  for (uint i = 0; i < Action::kMaximumNumberOfBindings; i++)
433  {
434  MythUIButton *button = m_actionButtons.at(i);
435  MythUIType *uitype = GetFocusWidget();
436  if (uitype == button)
437  return i;
438  }
439 
441 }
442 
451 {
452  MythUIButtonListItem* currentButton = nullptr;
453  if (m_leftListType == kKeyList &&
454  (currentButton = m_leftList->GetItemCurrent()))
455  {
456  return currentButton->GetText();
457  }
458 
459  if (GetFocusWidget() == m_leftList)
460  return QString();
461 
463  {
464  QString context = GetCurrentContext();
465  QString action = GetCurrentAction();
466  uint b = GetCurrentButton();
467  QStringList keys = m_bindings->GetActionKeys(context, action);
468 
469  if (b < (uint)keys.count())
470  return keys[b];
471 
472  return QString();
473  }
474 
475  currentButton = m_rightList->GetItemCurrent();
476  QString desc;
477  if (currentButton)
478  desc = currentButton->GetText();
479 
480  int loc = desc.indexOf(" => ");
481  if (loc == -1)
482  return QString(); // Should not happen
483 
484 
485  if (m_rightListType == kKeyList)
486  return desc.left(loc);
487 
488  return desc.mid(loc + 4);
489 }
490 
495 void MythControls::LoadData(const QString &hostname)
496 {
497  /* create the key bindings and the tree */
500 
501  /* Alphabetic order, but jump and global at the top */
502  m_sortedContexts.sort();
505  m_sortedContexts.insert(m_sortedContexts.begin(),
507  m_sortedContexts.insert(m_sortedContexts.begin(),
509 
510  foreach (const auto & ctx_name, m_sortedContexts)
511  {
512  QStringList actions = m_bindings->GetActions(ctx_name);
513  actions.sort();
514  m_contexts.insert(ctx_name, actions);
515  }
516 }
517 
525 {
526  QString context = GetCurrentContext();
527  QString key = GetCurrentKey();
528  QString action = GetCurrentAction();
529 
530  if (context.isEmpty() || key.isEmpty() || action.isEmpty())
531  {
532  LOG(VB_GENERAL, LOG_ERR,
533  "Unable to delete binding, missing information");
534  return;
535  }
536 
537  if (m_bindings->RemoveActionKey(context, action, key))
538  {
540  return;
541  }
542 
543  QString label = tr("This action is mandatory and needs at least one key "
544  "bound to it. Instead, try rebinding with another key.");
545 
546  MythScreenStack *popupStack =
547  GetMythMainWindow()->GetStack("popup stack");
548 
549  auto *confirmPopup = new MythConfirmationDialog(popupStack, label, false);
550 
551  if (confirmPopup->Create())
552  {
553  confirmPopup->SetReturnEvent(this, "mandatorydelete");
554  popupStack->AddScreen(confirmPopup);
555  }
556  else
557  delete confirmPopup;
558 }
559 
564 void MythControls::ResolveConflict(ActionID *conflict, int error_level,
565  const QString &key)
566 {
567  if (!conflict)
568  return;
569 
570  QString label;
571 
572  bool error = (KeyBindings::kKeyBindingError == error_level);
573 
574  if (error)
575  {
576  label = tr("This key binding conflicts with %1 in the %2 context. "
577  "Unable to bind key.")
578  .arg(conflict->GetAction()).arg(conflict->GetContext());
579  }
580  else
581  {
582  label = tr("This key binding conflicts with %1 in the %2 context. "
583  "Do you want to bind it anyway?")
584  .arg(conflict->GetAction()).arg(conflict->GetContext());
585  }
586 
587  MythScreenStack *popupStack =
588  GetMythMainWindow()->GetStack("popup stack");
589 
590  auto *confirmPopup = new MythConfirmationDialog(popupStack, label, !error);
591 
592  if (!error)
593  {
594  confirmPopup->SetData(QVariant::fromValue(key));
595  confirmPopup->SetReturnEvent(this, "conflict");
596  }
597 
598  if (confirmPopup->Create())
599  popupStack->AddScreen(confirmPopup);
600 
601  delete conflict;
602 }
603 
605 {
606  /* grab a key from the user */
607  MythScreenStack *popupStack =
608  GetMythMainWindow()->GetStack("popup stack");
609 
610  auto *keyGrabPopup = new KeyGrabPopupBox(popupStack);
611 
612  if (keyGrabPopup->Create())
613  popupStack->AddScreen(keyGrabPopup, false);
614 
615  connect(keyGrabPopup, SIGNAL(HaveResult(QString)),
616  SLOT(AddKeyToAction(QString)), Qt::QueuedConnection);
617 }
618 
627 void MythControls::AddKeyToAction(const QString& key, bool ignoreconflict)
628 {
629  QString action = GetCurrentAction();
630  QString context = GetCurrentContext();
631  QStringList keys = m_bindings->GetActionKeys(context, action);
632 
633  // Don't recreating an existing binding...
634  int binding_index = GetCurrentButton();
635  if ((binding_index >= (int)Action::kMaximumNumberOfBindings) ||
636  ((binding_index < keys.size()) && (keys[binding_index] == key)))
637  {
638  return;
639  }
640 
641  if (!ignoreconflict)
642  {
643  // Check for first of the potential conflicts.
644  int err_level = 0;
645  ActionID *conflict = m_bindings->GetConflict(context, key, err_level);
646  if (conflict)
647  {
648  ResolveConflict(conflict, err_level, key);
649 
650  return;
651  }
652  }
653 
654  if (binding_index < keys.count())
655  {
656  m_bindings->ReplaceActionKey(context, action, key,
657  keys[binding_index]);
658  }
659  else
660  {
661  m_bindings->AddActionKey(context, action, key);
662  }
663 
665 }
666 
667 void MythControls::customEvent(QEvent *event)
668 {
669  if (event->type() == DialogCompletionEvent::kEventType)
670  {
671  auto *dce = (DialogCompletionEvent*)(event);
672 
673  QString resultid = dce->GetId();
674  int buttonnum = dce->GetResult();
675 
676  if (resultid == "action")
677  {
678  if (buttonnum == 0)
679  GrabKey();
680  else if (buttonnum == 1)
681  DeleteKey();
682  }
683  else if (resultid == "option")
684  {
685  if (buttonnum == 0)
686  Save();
687  else if (buttonnum == 1)
688  ChangeView();
689  else if (buttonnum == 2)
690  GetMythMainWindow()->JumpTo("Reset All Keys");
691  }
692  else if (resultid == "exit")
693  {
694  if (buttonnum == 1)
695  Save();
696  else
697  Teardown();
698 
699  Close();
700  }
701  else if (resultid == "view")
702  {
703  QStringList contents;
704  QString leftcaption;
705  QString rightcaption;
706 
707  if (buttonnum == 0)
708  {
709  leftcaption = tr("Contexts");
710  rightcaption = tr("Actions");
712  contents = m_bindings->GetContexts();
713  }
714  else if (buttonnum == 1)
715  {
716  leftcaption = tr("Contexts");
717  rightcaption = tr("Keys");
719  contents = m_bindings->GetContexts();
720  }
721  else if (buttonnum == 2)
722  {
723  leftcaption = tr("Keys");
724  rightcaption = tr("Contexts");
726  contents = m_bindings->GetKeys();
727  }
728  else
729  return;
730 
731  m_leftDescription->SetText(leftcaption);
732  m_rightDescription->SetText(rightcaption);
733 
734  SetListContents(m_leftList, contents, true);
736  UpdateRightList();
737 
738  if (GetFocusWidget() != m_leftList)
740  }
741  else if (resultid == "conflict")
742  {
743  if (buttonnum == 1)
744  {
745  QString key = dce->GetData().toString();
746  AddKeyToAction(key, true);
747  }
748  }
749 
750  if (m_menuPopup)
751  m_menuPopup = nullptr;
752  }
753 
754 }
755 
756 /* vim: set expandtab tabstop=4 shiftwidth=4: */
static const QString kJumpContext
The statically assigned context for jump point actions.
Definition: actionset.h:80
Main header for mythcontrols.
Dialog asking for user confirmation.
void Save(void)
Definition: mythcontrols.h:93
void GrabKey(void)
bool HasChanges(void) const
Definition: keybindings.h:71
QString GetActionDescription(const QString &context_name, const QString &action_name) const
Get an action's description.
static void error(const char *str,...)
Definition: vbi.c:42
void RefreshKeyInformation(void)
Updates the list of keys that are shown and the description of the action.
void RightSelected(MythUIButtonListItem *item)
Refreshes key information when an item in the right list is selected.
Basic menu dialog, message and a list of options.
virtual void SetText(const QString &text)
Definition: mythuitext.cpp:135
Encapsulates information about the current keybindings.
Definition: keybindings.h:36
MythScreenStack * GetStack(const QString &stackname)
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
void ReplaceActionKey(const QString &context_name, const QString &action_name, const QString &newkey, const QString &oldkey)
Replace a key in an action.
MythUIButtonList * m_rightList
Definition: mythcontrols.h:118
#define LOC
A class that uniquely identifies an action.
Definition: action.h:84
The base class on which all widgets and screens are based.
Definition: mythuitype.h:63
void BuildFocusList(void)
static Type kEventType
Definition: mythdialogbox.h:57
QStringList GetKeyContexts(const QString &key) const
Get the context names in which a key is bound.
KeyBindings * m_bindings
Definition: mythcontrols.h:125
ViewType m_currentView
Definition: mythcontrols.h:116
virtual void Close()
void AddButton(const QString &title, QVariant data=0, bool newMenu=false, bool setCurrent=false)
void customEvent(QEvent *event) override
QStringList GetActions(const QString &context) const
Get a list of the actions in a context.
Definition: keybindings.cpp:71
void LeftSelected(MythUIButtonListItem *item)
Refreshes the right list when an item in the left list is selected.
void Close(void) override
void AddKeyToAction(const QString &key, bool ignoreconflict=false)
Add a key to the currently selected action.
virtual void AddScreen(MythScreenType *screen, bool allowFade=true)
MythUIButtonList * m_leftList
Definition: mythcontrols.h:117
MythUIText * m_description
Definition: mythcontrols.h:119
void TakingFocus()
void ChangeView(void)
Change the view.
Captures a key.
Definition: keygrabber.h:15
void ActionButtonPressed()
Slot handling a button being pressed in the left list.
MythUIText * m_rightDescription
Definition: mythcontrols.h:121
QList< MythUIButton * > m_actionButtons
Definition: mythcontrols.h:122
ListType m_rightListType
Definition: mythcontrols.h:130
void ShowMenu(void) override
virtual bool NextPrevWidgetFocus(bool up_or_down)
QHash< QString, QStringList > m_contexts
actions for a given context
Definition: mythcontrols.h:128
void Reset(void) override
Reset the widget to it's original state, should not reset changes made by the theme.
Definition: mythuitext.cpp:83
string hostname
Definition: caa.py:17
QStringList GetContextKeys(const QString &context) const
Get the keys within a context.
unsigned int uint
Definition: compat.h:140
void JumpTo(const QString &destination, bool pop=true)
List widget, displays list items in a variety of themeable arrangements and can trigger signals when ...
bool AddActionKey(const QString &context_name, const QString &action_name, const QString &key)
Add a key to an action.
void LeftPressed(MythUIButtonListItem *item)
Slot handling a button being pressed in the left list.
uint GetCurrentButton(void)
Returns the focused button, or Action::kMaximumNumberOfBindings if no buttons are focued.
void Teardown(void)
bool Create(void) override
Loads UI elements from theme.
static bool LoadWindowFromXML(const QString &xmlfile, const QString &windowname, MythUIType *parent)
MythUIType * GetFocusWidget(void) const
A single button widget.
Definition: mythuibutton.h:21
void Reset() override
Reset the widget to it's original state, should not reset changes made by the theme.
MythDialogBox * m_menuPopup
Definition: mythcontrols.h:123
MythMainWindow * GetMythMainWindow(void)
QString GetCurrentContext(void)
Get the currently selected context string.
QString GetText(const QString &name="") const
QStringList GetContexts(void) const
Returns a list of the context names.
Definition: keybindings.cpp:57
void ResolveConflict(ActionID *conflict, int error_level, const QString &key)
Resolve a potential conflict.
ActionID * GetConflict(const QString &context_name, const QString &key, int &level) const
Determine if adding a key would cause a conflict.
QString GetAction(void) const
Returns the action name.
Definition: action.h:103
static const QString kGlobalContext
The name of global actions.
Definition: actionset.h:82
QString GetCurrentKey(void)
Get the currently selected key string.
void DeleteKey(void)
Delete the currently active key to action mapping.
void SetReturnEvent(QObject *retobject, const QString &resultid)
void ChangeButtonFocus(int direction)
Change button focus in a particular direction.
void RightPressed(MythUIButtonListItem *item)
Slot handling a button being pressed in the left list.
ListType m_leftListType
Definition: mythcontrols.h:129
QString GetContext(void) const
Returns the context name.
Definition: action.h:100
static void SetListContents(MythUIButtonList *uilist, const QStringList &contents, bool arrows=false)
Set the contents of a list.
bool SetFocusWidget(MythUIType *widget=nullptr)
bool RemoveActionKey(const QString &context_name, const QString &action_name, const QString &key)
Unbind a key from an action.
QString GetCurrentAction(void)
Get the currently selected action string.
void LoadData(const QString &hostname)
Load the settings for a particular host.
QStringList GetKeys(void) const
Returns a list of all keys bound to an action.
Definition: keybindings.cpp:48
void UpdateRightList(void)
Update the right list.
QString GetHostName(void)
Event dispatched from MythUI modal dialogs to a listening class containing a result of some form.
Definition: mythdialogbox.h:41
static const unsigned int kMaximumNumberOfBindings
The maximum number of keys that can be bound to an action.
Definition: action.h:71
MythUIType * GetChild(const QString &name) const
Get a named child of this UIType.
Definition: mythuitype.cpp:130
~MythControls() override
MythUIText * m_leftDescription
Definition: mythcontrols.h:120
MythUIButtonListItem * GetItemCurrent() const
bool Create(void) override
QStringList GetActionKeys(const QString &context_name, const QString &action_name) const
Get an action's keys.
Definition: keybindings.cpp:94
QStringList m_sortedContexts
sorted list of contexts
Definition: mythcontrols.h:126
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:41