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  QStringList::const_iterator it = contents.begin();
292  for (; it != contents.end(); ++it)
293  {
294  auto *item = new MythUIButtonListItem(uilist, *it);
295  item->setDrawArrow(arrows);
296  }
297 }
298 
303 {
304  // get the selected item in the right list.
306 
307  if (!item)
308  return;
309 
310  QString rtstr = item->GetText();
311 
312  switch(m_currentView)
313  {
314  case kActionsByContext:
316  break;
317  case kKeysByContext:
319  break;
320  case kContextsByKey:
322  break;
323  }
324 }
325 
331 {
332  for (uint i = 0; i < Action::kMaximumNumberOfBindings; i++)
333  m_actionButtons.at(i)->SetText("");
334 
335  if (GetFocusWidget() == m_leftList)
336  {
337  m_description->Reset();
338  return;
339  }
340 
341  const QString context = GetCurrentContext();
342  const QString action = GetCurrentAction();
343 
344  QString desc = m_bindings->GetActionDescription(context, action);
345  m_description->SetText(tr(desc.toLatin1().constData()));
346 
347  QStringList keys = m_bindings->GetActionKeys(context, action);
348  for (int i = 0; (i < keys.count()) &&
349  (i < (int)Action::kMaximumNumberOfBindings); i++)
350  {
351  m_actionButtons.at(i)->SetText(keys[i]);
352  }
353 }
354 
355 
364 {
366  return m_leftList->GetItemCurrent()->GetText();
367 
368  if (GetFocusWidget() == m_leftList)
369  return QString();
370 
371  QString desc = m_rightList->GetItemCurrent()->GetText();
372  int loc = desc.indexOf(" => ");
373  if (loc == -1)
374  return QString(); // Should not happen
375 
377  return desc.left(loc);
378 
379  return desc.mid(loc + 4);
380 }
381 
390 {
392  {
394  {
395  return m_leftList->GetItemCurrent()->GetText();
396  }
397  return QString();
398  }
399 
400  if (GetFocusWidget() == m_leftList)
401  return QString();
402 
404  return QString();
405 
406  QString desc = m_rightList->GetItemCurrent()->GetText();
407  if (kContextList == m_leftListType &&
409  {
410  return desc;
411  }
412 
413  int loc = desc.indexOf(" => ");
414  if (loc == -1)
415  return QString(); // should not happen..
416 
418  return desc.left(loc);
419 
420  QString rv = desc.mid(loc+4);
421  if (rv == "<none>")
422  return QString();
423 
424  return rv;
425 }
426 
432 {
433  for (uint i = 0; i < Action::kMaximumNumberOfBindings; i++)
434  {
435  MythUIButton *button = m_actionButtons.at(i);
436  MythUIType *uitype = GetFocusWidget();
437  if (uitype == button)
438  return i;
439  }
440 
442 }
443 
452 {
453  MythUIButtonListItem* currentButton = nullptr;
454  if (m_leftListType == kKeyList &&
455  (currentButton = m_leftList->GetItemCurrent()))
456  {
457  return currentButton->GetText();
458  }
459 
460  if (GetFocusWidget() == m_leftList)
461  return QString();
462 
464  {
465  QString context = GetCurrentContext();
466  QString action = GetCurrentAction();
467  uint b = GetCurrentButton();
468  QStringList keys = m_bindings->GetActionKeys(context, action);
469 
470  if (b < (uint)keys.count())
471  return keys[b];
472 
473  return QString();
474  }
475 
476  currentButton = m_rightList->GetItemCurrent();
477  QString desc;
478  if (currentButton)
479  desc = currentButton->GetText();
480 
481  int loc = desc.indexOf(" => ");
482  if (loc == -1)
483  return QString(); // Should not happen
484 
485 
486  if (m_rightListType == kKeyList)
487  return desc.left(loc);
488 
489  return desc.mid(loc + 4);
490 }
491 
496 void MythControls::LoadData(const QString &hostname)
497 {
498  /* create the key bindings and the tree */
501 
502  /* Alphabetic order, but jump and global at the top */
503  m_sortedContexts.sort();
506  m_sortedContexts.insert(m_sortedContexts.begin(),
508  m_sortedContexts.insert(m_sortedContexts.begin(),
510 
511  QStringList::const_iterator it = m_sortedContexts.begin();
512  for (; it != m_sortedContexts.end(); ++it)
513  {
514  QString ctx_name = *it;
515  QStringList actions = m_bindings->GetActions(ctx_name);
516  actions.sort();
517  m_contexts.insert(ctx_name, actions);
518  }
519 }
520 
528 {
529  QString context = GetCurrentContext();
530  QString key = GetCurrentKey();
531  QString action = GetCurrentAction();
532 
533  if (context.isEmpty() || key.isEmpty() || action.isEmpty())
534  {
535  LOG(VB_GENERAL, LOG_ERR,
536  "Unable to delete binding, missing information");
537  return;
538  }
539 
540  if (m_bindings->RemoveActionKey(context, action, key))
541  {
543  return;
544  }
545 
546  QString label = tr("This action is mandatory and needs at least one key "
547  "bound to it. Instead, try rebinding with another key.");
548 
549  MythScreenStack *popupStack =
550  GetMythMainWindow()->GetStack("popup stack");
551 
552  auto *confirmPopup = new MythConfirmationDialog(popupStack, label, false);
553 
554  if (confirmPopup->Create())
555  {
556  confirmPopup->SetReturnEvent(this, "mandatorydelete");
557  popupStack->AddScreen(confirmPopup);
558  }
559  else
560  delete confirmPopup;
561 }
562 
567 void MythControls::ResolveConflict(ActionID *conflict, int error_level,
568  const QString &key)
569 {
570  if (!conflict)
571  return;
572 
573  QString label;
574 
575  bool error = (KeyBindings::kKeyBindingError == error_level);
576 
577  if (error)
578  label = tr("This key binding conflicts with %1 in the %2 context. "
579  "Unable to bind key.")
580  .arg(conflict->GetAction()).arg(conflict->GetContext());
581  else
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  MythScreenStack *popupStack =
587  GetMythMainWindow()->GetStack("popup stack");
588 
589  auto *confirmPopup = new MythConfirmationDialog(popupStack, label, !error);
590 
591  if (!error)
592  {
593  confirmPopup->SetData(qVariantFromValue(key));
594  confirmPopup->SetReturnEvent(this, "conflict");
595  }
596 
597  if (confirmPopup->Create())
598  popupStack->AddScreen(confirmPopup);
599 
600  delete conflict;
601 }
602 
604 {
605  /* grab a key from the user */
606  MythScreenStack *popupStack =
607  GetMythMainWindow()->GetStack("popup stack");
608 
609  auto *keyGrabPopup = new KeyGrabPopupBox(popupStack);
610 
611  if (keyGrabPopup->Create())
612  popupStack->AddScreen(keyGrabPopup, false);
613 
614  connect(keyGrabPopup, SIGNAL(HaveResult(QString)),
615  SLOT(AddKeyToAction(QString)), Qt::QueuedConnection);
616 }
617 
626 void MythControls::AddKeyToAction(const QString& key, bool ignoreconflict)
627 {
628  QString action = GetCurrentAction();
629  QString context = GetCurrentContext();
630  QStringList keys = m_bindings->GetActionKeys(context, action);
631 
632  // Don't recreating an existing binding...
633  int binding_index = GetCurrentButton();
634  if ((binding_index >= (int)Action::kMaximumNumberOfBindings) ||
635  ((binding_index < keys.size()) && (keys[binding_index] == key)))
636  {
637  return;
638  }
639 
640  if (!ignoreconflict)
641  {
642  // Check for first of the potential conflicts.
643  int err_level = 0;
644  ActionID *conflict = m_bindings->GetConflict(context, key, err_level);
645  if (conflict)
646  {
647  ResolveConflict(conflict, err_level, key);
648 
649  return;
650  }
651  }
652 
653  if (binding_index < keys.count())
654  m_bindings->ReplaceActionKey(context, action, key,
655  keys[binding_index]);
656  else
657  m_bindings->AddActionKey(context, action, key);
658 
660 }
661 
662 void MythControls::customEvent(QEvent *event)
663 {
664  if (event->type() == DialogCompletionEvent::kEventType)
665  {
666  auto *dce = (DialogCompletionEvent*)(event);
667 
668  QString resultid = dce->GetId();
669  int buttonnum = dce->GetResult();
670 
671  if (resultid == "action")
672  {
673  if (buttonnum == 0)
674  GrabKey();
675  else if (buttonnum == 1)
676  DeleteKey();
677  }
678  else if (resultid == "option")
679  {
680  if (buttonnum == 0)
681  Save();
682  else if (buttonnum == 1)
683  ChangeView();
684  else if (buttonnum == 2)
685  GetMythMainWindow()->JumpTo("Reset All Keys");
686  }
687  else if (resultid == "exit")
688  {
689  if (buttonnum == 1)
690  Save();
691  else
692  Teardown();
693 
694  Close();
695  }
696  else if (resultid == "view")
697  {
698  QStringList contents;
699  QString leftcaption, rightcaption;
700 
701  if (buttonnum == 0)
702  {
703  leftcaption = tr("Contexts");
704  rightcaption = tr("Actions");
706  contents = m_bindings->GetContexts();
707  }
708  else if (buttonnum == 1)
709  {
710  leftcaption = tr("Contexts");
711  rightcaption = tr("Keys");
713  contents = m_bindings->GetContexts();
714  }
715  else if (buttonnum == 2)
716  {
717  leftcaption = tr("Keys");
718  rightcaption = tr("Contexts");
720  contents = m_bindings->GetKeys();
721  }
722  else
723  return;
724 
725  m_leftDescription->SetText(leftcaption);
726  m_rightDescription->SetText(rightcaption);
727 
728  SetListContents(m_leftList, contents, true);
730  UpdateRightList();
731 
732  if (GetFocusWidget() != m_leftList)
734  }
735  else if (resultid == "conflict")
736  {
737  if (buttonnum == 1)
738  {
739  QString key = dce->GetData().toString();
740  AddKeyToAction(key, true);
741  }
742  }
743 
744  if (m_menuPopup)
745  m_menuPopup = nullptr;
746  }
747 
748 }
749 
750 /* 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.
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:82
The base class on which all widgets and screens are based.
Definition: mythuitype.h:63
void BuildFocusList(void)
static Type kEventType
Definition: mythdialogbox.h:50
void customEvent(QEvent *) override
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
void LeftPressed(MythUIButtonListItem *)
Slot handling a button being pressed in the left list.
virtual void Close()
void AddButton(const QString &title, QVariant data=0, bool newMenu=false, bool setCurrent=false)
QStringList GetActions(const QString &context) const
Get a list of the actions in a context.
Definition: keybindings.cpp:71
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
void RightPressed(MythUIButtonListItem *)
Slot handling a button being pressed in the left list.
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.
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.
void LeftSelected(MythUIButtonListItem *)
Refreshes the right list when an item in the left list is selected.
ActionID * GetConflict(const QString &context_name, const QString &key, int &level) const
Determine if adding a key would cause a conflict.
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: mythlogging.h:41
QString GetAction(void) const
Returns the action name.
Definition: action.h:108
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 RightSelected(MythUIButtonListItem *)
Refreshes key information when an item in the right list is selected.
ListType m_leftListType
Definition: mythcontrols.h:129
QString GetContext(void) const
Returns the context name.
Definition: action.h:105
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:37
static const unsigned int kMaximumNumberOfBindings
The maximum number of keys that can be bound to an action.
Definition: action.h:69
MythUIType * GetChild(const QString &name) const
Get a named child of this UIType.
Definition: mythuitype.cpp:132
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