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