MythTV  master
actionset.cpp
Go to the documentation of this file.
1 /* -*- myth -*- */
23 // MythTV headers
24 #include "mythlogging.h"
25 
26 // MythContext headers
27 #include "action.h"
28 #include "actionset.h"
29 
30 const QString ActionSet::kJumpContext = "JumpPoints";
31 const QString ActionSet::kGlobalContext = "Global";
32 
34 {
35  while (!m_contexts.empty())
36  {
37  Context &ctx = *m_contexts.begin();
38  while (!ctx.empty())
39  {
40  delete *ctx.begin();
41  ctx.erase(ctx.begin());
42  }
43  m_contexts.erase(m_contexts.begin());
44  }
45 }
46 
57 bool ActionSet::Add(const ActionID &id, const QString &key)
58 {
59  Action *a = GetAction(id);
60 
61  if (!a)
62  return false;
63 
64  if (!a->AddKey(key))
65  {
66  LOG(VB_GENERAL, LOG_ERR, "ActionSet::AddKey() failed");
67  return false;
68  }
69 
70  ActionList &ids = m_keyToActionMap[key];
71  ids.push_back(id);
72  SetModifiedFlag(id, true);
73 
74  return true;
75 }
76 
89 bool ActionSet::Remove(const ActionID &id, const QString &key)
90 {
91  Action *a = GetAction(id);
92 
93  if (!a)
94  return false;
95 
96  if (!a->RemoveKey(key))
97  return false;
98 
99  m_keyToActionMap[key].removeAll(id);
100 
101  // remove the key if there isn't anything bound to it.
102  if (m_keyToActionMap[key].isEmpty())
103  m_keyToActionMap.remove(key);
104 
105  SetModifiedFlag(id, true);
106 
107  return true;
108 }
109 
121  const QString &newkey,
122  const QString &oldkey)
123 {
124  Action *a = GetAction(id);
125 
126  if (!a)
127  return false;
128 
129  if (!a->ReplaceKey(newkey, oldkey))
130  return false;
131 
132  m_keyToActionMap[oldkey].removeAll(id);
133  m_keyToActionMap[newkey].push_back(id);
134  SetModifiedFlag(id, true);
135 
136  return true;
137 }
138 
144 {
145  if (!modified)
146  return m_modified.removeAll(id) != 0;
147 
148  if (!IsModified(id))
149  {
150  m_modified.push_back(id);
151  return true;
152  }
153 
154  return false;
155 }
156 
160 QStringList ActionSet::GetContextStrings(void) const
161 {
162  QStringList context_strings;
163 
164  ContextMap::const_iterator it = m_contexts.begin();
165  for (; it != m_contexts.end(); ++it)
166  context_strings.append(it.key());
167  return context_strings;
168 }
169 
173 QStringList ActionSet::GetActionStrings(const QString &context_name) const
174 {
175  QStringList action_strings;
176 
177  ContextMap::const_iterator cit = m_contexts.find(context_name);
178  if (cit == m_contexts.end())
179  return action_strings;
180 
181  Context::const_iterator it = (*cit).begin();
182  for (; it != (*cit).end(); ++it)
183  action_strings.append(it.key());
184  return action_strings;
185 }
186 
199  const QString &description,
200  const QString &keys)
201 {
202  ContextMap::iterator cit = m_contexts.find(id.GetContext());
203  if (cit == m_contexts.end())
204  cit = m_contexts.insert(id.GetContext(), Context());
205  else if ((*cit).find(id.GetAction()) != (*cit).end())
206  return false;
207 
208  auto *a = new Action(description, keys);
209  (*cit).insert(id.GetAction(), a);
210 
211  const QStringList keylist = a->GetKeys();
212  foreach (const auto & key, keylist)
213  m_keyToActionMap[key].push_back(id);
214 
215  return true;
216 }
217 
222 QString ActionSet::GetKeyString(const ActionID &id) const
223 {
224  ContextMap::const_iterator cit = m_contexts.find(id.GetContext());
225  if (cit == m_contexts.end())
226  return QString();
227 
228  Context::const_iterator it = (*cit).find(id.GetAction());
229  if (it != (*cit).end())
230  return (*it)->GetKeyString();
231 
232  return QString();
233 }
234 
238 QStringList ActionSet::GetKeys(const ActionID &id) const
239 {
240  QStringList keys;
241 
242  ContextMap::const_iterator cit = m_contexts.find(id.GetContext());
243  if (cit == m_contexts.end())
244  return keys;
245 
246  Context::const_iterator it = (*cit).find(id.GetAction());
247  if (it != (*cit).end())
248  keys = (*it)->GetKeys();
249  return keys;
250 }
251 
252 QStringList ActionSet::GetContextKeys(const QString &context_name) const
253 {
254  QStringList keys;
255  ContextMap::const_iterator cit = m_contexts.find(context_name);
256  if (cit == m_contexts.end())
257  return keys;
258 
259  foreach (auto ctx, (*cit))
260  keys += ctx->GetKeys();
261  keys.sort();
262  return keys;
263 }
264 
267 QStringList ActionSet::GetAllKeys(void) const
268 {
269  QStringList keys;
270 
271  QMap<QString, ActionList>::ConstIterator it;
272 
273  for (it = m_keyToActionMap.begin(); it != m_keyToActionMap.end(); ++it)
274  keys.push_back(it.key());
275 
276  return keys;
277 }
278 
282 QString ActionSet::GetDescription(const ActionID &id) const
283 {
284  ContextMap::const_iterator cit = m_contexts.find(id.GetContext());
285  if (cit == m_contexts.end())
286  return QString();
287 
288  Context::const_iterator it = (*cit).find(id.GetAction());
289  if (it != (*cit).end())
290  return (*it)->GetDescription();
291 
292  return QString();
293 }
294 
300 {
301  ContextMap::iterator cit = m_contexts.find(id.GetContext());
302  if (cit == m_contexts.end())
303  {
304  LOG(VB_GENERAL, LOG_ERR,
305  QString("GetAction: Did not find context '%1'")
306  .arg(id.GetContext()));
307 
308  return nullptr;
309  }
310 
311  Context::iterator it = (*cit).find(id.GetAction());
312 
313  if (it == (*cit).end())
314  {
315  LOG(VB_GENERAL, LOG_ERR,
316  QString("GetAction: Did not find action '%1' in context '%1'")
317  .arg(id.GetAction()).arg(id.GetContext()));
318  return nullptr;
319  }
320 
321  return *it;
322 }
static const QString kJumpContext
The statically assigned context for jump point actions.
Definition: actionset.h:80
QHash< QString, Action * > Context
Definition: action.h:77
bool AddKey(const QString &key)
Add a key sequence to this action.
Definition: action.cpp:68
Main header for the action set class.
QStringList GetKeys(const ActionID &id) const
Get the keys bound to an action by its identifier.
Definition: actionset.cpp:238
bool Replace(const ActionID &id, const QString &newkey, const QString &oldkey)
Replace a specific key in a specific action.
Definition: actionset.cpp:120
bool ReplaceKey(const QString &newkey, const QString &oldkey)
Replace a key.
Definition: action.cpp:85
ContextMap m_contexts
Definition: actionset.h:87
A class that uniquely identifies an action.
Definition: action.h:84
An action (for this plugin) consists of a description, and a set of key sequences.
Definition: action.h:40
QStringList GetActionStrings(const QString &context_name) const
Returns a list of all action in the action set.
Definition: actionset.cpp:173
bool RemoveKey(const QString &key)
Remove a key from this action.
Definition: action.h:52
ActionList m_modified
Definition: actionset.h:88
Main header for the action class.
bool Add(const ActionID &id, const QString &key)
Add a binding.
Definition: actionset.cpp:57
Action * GetAction(const ActionID &id)
Returns a pointer to an action by its identifier.
Definition: actionset.cpp:299
QStringList GetContextStrings(void) const
Returns a list of all contexts in the action set.
Definition: actionset.cpp:160
QStringList GetContextKeys(const QString &context_name) const
Definition: actionset.cpp:252
bool IsModified(const ActionID &id) const
Returns true iff the action is modified.
Definition: actionset.h:72
QString GetKeyString(const ActionID &id) const
Returns a string containing all the keys in bound to an action by its identifier.
Definition: actionset.cpp:222
QMap< QString, ActionList > m_keyToActionMap
Definition: actionset.h:85
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: mythlogging.h:41
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:282
static bool modified(uint64_t sig)
Definition: eitcache.cpp:91
QList< ActionID > ActionList
Definition: action.h:115
QStringList GetAllKeys(void) const
Get all keys (from every context) to which an action is bound.
Definition: actionset.cpp:267
bool Remove(const ActionID &id, const QString &key)
Remove a key from an action identifier.
Definition: actionset.cpp:89
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