MythTV  master
keybindings.cpp
Go to the documentation of this file.
1 /* -*- myth -*- */
28 // MythTV headers
29 #include "mythdb.h"
30 #include "mythmainwindow.h"
31 
32 // MythControls headers
33 #include "keybindings.h"
34 
40  : m_hostname(hostname)
41 {
43  LoadContexts();
45 }
46 
48 QStringList KeyBindings::GetKeys(void) const
49 {
50  return m_actionSet.GetAllKeys();
51 }
52 
57 QStringList KeyBindings::GetContexts(void) const
58 {
59  QStringList ctxts = m_actionSet.GetContextStrings();
60  ctxts.sort();
61  return ctxts;
62 }
63 
71 QStringList KeyBindings::GetActions(const QString &context) const
72 {
73  return m_actionSet.GetActionStrings(context);
74 }
75 
83 void KeyBindings::GetKeyActions(const QString &key, ActionList &list) const
84 {
85  list = m_actionSet.GetActions(key);
86 }
87 
94 QStringList KeyBindings::GetActionKeys(const QString &context_name,
95  const QString &action_name) const
96 {
97  return m_actionSet.GetKeys(ActionID(context_name, action_name));
98 }
99 
105 QStringList KeyBindings::GetContextKeys(const QString &context) const
106 {
107  return m_actionSet.GetContextKeys(context);
108 }
109 
114 QStringList KeyBindings::GetKeyContexts(const QString &key) const
115 {
116  ActionList actions = m_actionSet.GetActions(key);
117  QStringList contexts;
118 
119  for (int i = 0; i < actions.size(); i++)
120  {
121  QString context = actions[i].GetContext();
122  if (!contexts.contains(context))
123  contexts.push_back(context);
124  }
125 
126  return contexts;
127 }
128 
135 QString KeyBindings::GetActionDescription(const QString &context_name,
136  const QString &action_name) const
137 {
138  ActionID id(context_name, action_name);
139  return m_actionSet.GetDescription(id);
140 }
141 
151 bool KeyBindings::AddActionKey(const QString &context_name,
152  const QString &action_name,
153  const QString &key)
154 {
155  return m_actionSet.Add(ActionID(context_name, action_name), key);
156 }
157 
179  const QString &context_name, const QString &key, int &level) const
180 {
181  const ActionList ids = m_actionSet.GetActions(key);
182 
183  // trying to bind a jumppoint to an already bound key
184  if ((context_name == ActionSet::kJumpContext) && (ids.count() > 0))
185  return new ActionID(ids[0]);
186 
187  // check the contexts of the other bindings
188  for (int i = 0; i < ids.count(); i++)
189  {
190  // jump points, then global, then the same context
191  if (ids[i].GetContext() == ActionSet::kJumpContext)
192  {
194  return new ActionID(ids[i]);
195  }
196  else if (ids[i].GetContext() == context_name)
197  {
199  return new ActionID(ids[i]);
200  }
201  else if (ids[i].GetContext() == ActionSet::kGlobalContext)
202  {
204  return new ActionID(ids[i]);
205  }
206  }
207 
208  return nullptr; // no conflicts
209 }
210 
221 void KeyBindings::ReplaceActionKey(const QString &context_name,
222  const QString &action_name,
223  const QString &newkey,
224  const QString &oldkey)
225 {
226  m_actionSet.Replace(ActionID(context_name, action_name), newkey, oldkey);
227 }
228 
241 bool KeyBindings::RemoveActionKey(const QString &context_name,
242  const QString &action_name,
243  const QString &key)
244 {
245  ActionID id(context_name, action_name);
246 
247  // Don't remove the last mandatory binding
248  if (m_mandatoryBindings.contains(id) &&
249  (m_actionSet.GetKeys(id).count() < 2))
250  {
251  return false;
252  }
253 
254  return m_actionSet.Remove(id, key);
255 }
256 
257 
262 {
263  MSqlQuery query(MSqlQuery::InitCon());
264  query.prepare(
265  "UPDATE keybindings "
266  "SET keylist = :KEYLIST "
267  "WHERE hostname = :HOSTNAME AND "
268  " action = :ACTION AND "
269  " context = :CONTEXT");
270 
271  QString keys = m_actionSet.GetKeyString(id);
272  query.bindValue(":KEYLIST", keys);
273  query.bindValue(":HOSTNAME", m_hostname);
274  query.bindValue(":CONTEXT", id.GetContext());
275  query.bindValue(":ACTION", id.GetAction());
276 
277  if (!query.exec() || !query.isActive())
278  {
279  MythDB::DBError("KeyBindings::CommitAction", query);
280  return;
281  }
282 
283  GetMythMainWindow()->ClearKey(id.GetContext(), id.GetAction());
284  GetMythMainWindow()->BindKey(id.GetContext(), id.GetAction(), keys);
285 }
286 
293 {
294  MSqlQuery query(MSqlQuery::InitCon());
295  query.prepare(
296  "UPDATE jumppoints "
297  "SET keylist = :KEYLIST "
298  "WHERE hostname = :HOSTNAME AND"
299  " destination = :DESTINATION");
300 
301  QString keys = m_actionSet.GetKeyString(id);
302  query.bindValue(":KEYLIST", keys);
303  query.bindValue(":HOSTNAME", m_hostname);
304  query.bindValue(":DESTINATION", id.GetAction());
305 
306  if (!query.exec() || !query.isActive())
307  {
308  MythDB::DBError("KeyBindings::CommitJumppoint", query);
309  return;
310  }
311 
312  GetMythMainWindow()->ClearJump(id.GetAction());
313  GetMythMainWindow()->BindJump(id.GetAction(), keys);
314 }
315 
324 {
326 
327  while (modified.size() > 0)
328  {
329  ActionID id = modified.front();
330 
331  // commit either a jumppoint or an action
332  if (id.GetContext() == ActionSet::kJumpContext)
333  CommitJumppoint(id);
334  else
335  CommitAction(id);
336 
337  // tell the action set that the action is no longer modified
338  m_actionSet.SetModifiedFlag(id, false);
339 
340  modified.pop_front();
341  }
342 }
343 
350 {
351  MSqlQuery query(MSqlQuery::InitCon());
352  query.prepare(
353  "SELECT destination, description, keylist "
354  "FROM jumppoints "
355  "WHERE hostname = :HOSTNAME "
356  "ORDER BY destination");
357  query.bindValue(":HOSTNAME", m_hostname);
358 
359  if (!query.exec() || !query.isActive())
360  {
361  MythDB::DBError("KeyBindings::LoadJumppoint", query);
362  return;
363  }
364 
365  while (query.next())
366  {
367  ActionID id(ActionSet::kJumpContext, query.value(0).toString());
368 
369  if (query.value(1).toString().isEmpty())
370  {
371  m_actionSet.AddAction(id, query.value(0).toString(),
372  query.value(2).toString());
373  }
374  else
375  {
376  m_actionSet.AddAction(id, query.value(1).toString(),
377  query.value(2).toString());
378  }
379  }
380 }
381 
389 {
390  MSqlQuery query(MSqlQuery::InitCon());
391  query.prepare(
392  "SELECT context, action, description, keylist "
393  "FROM keybindings "
394  "WHERE hostname = :HOSTNAME "
395  "ORDER BY context, action");
396  query.bindValue(":HOSTNAME", m_hostname);
397 
398  if (!query.exec() || !query.isActive())
399  {
400  MythDB::DBError("KeyBindings::LoadContexts", query);
401  return;
402  }
403 
404  while (query.next())
405  {
406  ActionID id(query.value(0).toString(), query.value(1).toString());
407  m_actionSet.AddAction(id, query.value(2).toString(),
408  query.value(3).toString());
409  }
410 }
411 
416 {
417  if (!m_mandatoryBindings.empty())
418  return;
419 
421  m_defaultKeys.append("Down");
422 
424  m_defaultKeys.append("Up");
425 
427  m_defaultKeys.append("Left");
428 
430  m_defaultKeys.append("Right");
431 
433  m_defaultKeys.append("Esc");
434 
436  m_defaultKeys.append("Return, Enter, Space");
437 }
438 
443 {
444  const ActionList &manlist = m_mandatoryBindings;
445  for (int i = 0; i < manlist.count(); i++)
446  {
447  if (m_actionSet.GetKeys(manlist[i]).isEmpty())
448  return false;
449  }
450 
451  return true; // none are empty...
452 }
bool next(void)
Wrap QSqlQuery::next() so we can display the query results.
Definition: mythdbcon.cpp:794
void bindValue(const QString &placeholder, const QVariant &val)
Definition: mythdbcon.cpp:875
QStringList m_defaultKeys
Definition: keybindings.h:84
static const QString kJumpContext
The statically assigned context for jump point actions.
Definition: actionset.h:80
QString GetActionDescription(const QString &context_name, const QString &action_name) const
Get an action's description.
QStringList GetKeys(const ActionID &id) const
Get the keys bound to an action by its identifier.
Definition: actionset.cpp:239
bool Replace(const ActionID &id, const QString &newkey, const QString &oldkey)
Replace a specific key in a specific action.
Definition: actionset.cpp:120
void ClearJump(const QString &destination)
void CommitChanges(void)
Commit all changes made to the keybindings.
QSqlQuery wrapper that fetches a DB connection from the connection pool.
Definition: mythdbcon.h:125
bool HasMandatoryBindings(void) const
Returns true iff all mandatory bindings are satisfied.
void ReplaceActionKey(const QString &context_name, const QString &action_name, const QString &newkey, const QString &oldkey)
Replace a key in an action.
void LoadContexts(void)
Load the keybindings from the database.
A class that uniquely identifies an action.
Definition: action.h:82
void BindKey(const QString &context, const QString &action, const QString &key)
QStringList GetKeyContexts(const QString &key) const
Get the context names in which a key is bound.
QList< ActionID > ActionList
Definition: action.h:120
QStringList GetActionStrings(const QString &context_name) const
Returns a list of all action in the action set.
Definition: actionset.cpp:173
QVariant value(int i) const
Definition: mythdbcon.h:182
QStringList GetActions(const QString &context) const
Get a list of the actions in a context.
Definition: keybindings.cpp:71
void LoadMandatoryBindings(void)
Load the mandatory bindings.
bool Add(const ActionID &id, const QString &key)
Add a binding.
Definition: actionset.cpp:57
QStringList GetContextStrings(void) const
Returns a list of all contexts in the action set.
Definition: actionset.cpp:160
void CommitAction(const ActionID &id)
Commit an action to the database, and reload its keybindings.
bool isActive(void) const
Definition: mythdbcon.h:188
QStringList GetContextKeys(const QString &context_name) const
Definition: actionset.cpp:253
QString GetKeyString(const ActionID &id) const
Returns a string containing all the keys in bound to an action by its identifier.
Definition: actionset.cpp:223
void BindJump(const QString &destination, const QString &key)
string hostname
Definition: caa.py:17
QStringList GetContextKeys(const QString &context) const
Get the keys within a context.
static MSqlQueryInfo InitCon(ConnectionReuse=kNormalConnection)
Only use this in combination with MSqlQuery constructor.
Definition: mythdbcon.cpp:547
bool AddActionKey(const QString &context_name, const QString &action_name, const QString &key)
Add a key to an action.
MythMainWindow * GetMythMainWindow(void)
QString m_hostname
Definition: keybindings.h:82
QStringList GetContexts(void) const
Returns a list of the context names.
Definition: keybindings.cpp:57
Main header for keybinding classes.
ActionSet m_actionSet
Definition: keybindings.h:85
ActionID * GetConflict(const QString &context_name, const QString &key, int &level) const
Determine if adding a key would cause a conflict.
bool prepare(const QString &query)
QSqlQuery::prepare() is not thread safe in Qt <= 3.3.2.
Definition: mythdbcon.cpp:819
static const QString kGlobalContext
The name of global actions.
Definition: actionset.h:82
QString GetDescription(const ActionID &id) const
Returns the description of an action by its identifier.
Definition: actionset.cpp:284
static bool modified(uint64_t sig)
Definition: eitcache.cpp:93
QStringList GetAllKeys(void) const
Get all keys (from every context) to which an action is bound.
Definition: actionset.cpp:269
ActionList GetModified(void) const
Returns the appropriate container of modified actions.
Definition: actionset.h:68
void GetKeyActions(const QString &key, ActionList &list) const
Get a list of the actions in a context.
Definition: keybindings.cpp:83
bool RemoveActionKey(const QString &context_name, const QString &action_name, const QString &key)
Unbind a key from an action.
bool exec(void)
Wrap QSqlQuery::exec() so we can display SQL.
Definition: mythdbcon.cpp:615
QStringList GetKeys(void) const
Returns a list of all keys bound to an action.
Definition: keybindings.cpp:48
static void DBError(const QString &where, const MSqlQuery &query)
Definition: mythdb.cpp:179
bool Remove(const ActionID &id, const QString &key)
Remove a key from an action identifier.
Definition: actionset.cpp:89
ActionList GetActions(const QString &key) const
Returns the actions bound to the specified key.
Definition: actionset.h:65
void ClearKey(const QString &context, const QString &action)
KeyBindings(const QString &hostname)
Create a new KeyBindings instance.
Definition: keybindings.cpp:39
void LoadJumppoints(void)
Load the jumppoints from the database.
bool AddAction(const ActionID &id, const QString &description, const QString &keys)
Add an action.
Definition: actionset.cpp:198
bool SetModifiedFlag(const ActionID &id, bool modified)
Mark an action as modified or unmodified by its identifier.
Definition: actionset.cpp:143
void CommitJumppoint(const ActionID &id)
Commit a jumppoint to the database.
ActionList m_mandatoryBindings
Definition: keybindings.h:83
QStringList GetActionKeys(const QString &context_name, const QString &action_name) const
Get an action's keys.
Definition: keybindings.cpp:94