kwave  18.07.70
Kwave::MenuManager Class Reference

Manager class for access to Kwave's menu subsystem. More...

#include <MenuManager.h>

Inheritance diagram for Kwave::MenuManager:
Inheritance graph
Collaboration diagram for Kwave::MenuManager:
Collaboration graph

Public Slots

void setItemChecked (const QString &uid, bool check)
 
void setItemEnabled (const QString &uid, bool enable)
 

Signals

void sigMenuCommand (const QString &command)
 

Public Member Functions

 MenuManager (QWidget *parent, QMenuBar &bar)
 
virtual ~MenuManager ()
 
int executeCommand (const QString &command)
 
void clearNumberedMenu (const QString &uid)
 
void addNumberedMenuEntry (const QString &uid, const QString &entry, const QString &param)
 
void selectItem (const QString &group, const QString &uid)
 
void setItemText (const QString &uid, const QString &text)
 
void setItemVisible (const QString &uid, bool show)
 

Private Attributes

Kwave::MenuRootm_menu_root
 

Static Private Attributes

static QMap< QString, QKeySequence > m_standard_keys
 

Detailed Description

Manager class for access to Kwave's menu subsystem.

Note
All commands must be emitted synchronously during X11 event processing instead of immediately through normal signal handling. This avoids trouble when a signal handler within the MenuNode class causes an action that deletes that menu node. It took me one week to find that bug!

Definition at line 46 of file MenuManager.h.

Constructor & Destructor Documentation

◆ MenuManager()

Kwave::MenuManager::MenuManager ( QWidget *  parent,
QMenuBar &  bar 
)

Constructor.

Parameters
parentthe menu's parent widget
barreference to the menu bar

Definition at line 50 of file MenuManager.cpp.

References Kwave::connect(), INS, m_menu_root, m_standard_keys, and sigMenuCommand().

51  :QObject(parent)
52 {
53  m_menu_root = new(std::nothrow) Kwave::MenuRoot(bar);
54  Q_ASSERT(m_menu_root);
55  if (m_menu_root) {
56  connect(
57  m_menu_root, SIGNAL(sigCommand(QString)),
58  this, SIGNAL(sigMenuCommand(QString)),
59  Qt::QueuedConnection
60  );
61  }
62 
63  // fill the list of standard keys if it is empty.
64  //
65  // HINT: I have gerated the list below through copy&paste of the
66  // description of "enum QKeySequence::StandardKey" into
67  // list.txt and then:
68  //
69  // cat list.txt | sed s/^\ \ //g | cut -d \ -f 1 | cut -d \: -f 3 |
70  // awk '{ print "\tINS(\""$0"\", QKeySequence::"$0");"}'
71 
72  if (m_standard_keys.isEmpty()) {
73  INS("UnknownKey", QKeySequence::UnknownKey);
74  INS("HelpContents", QKeySequence::HelpContents);
75  INS("WhatsThis", QKeySequence::WhatsThis);
76  INS("Open", QKeySequence::Open);
77  INS("Close", QKeySequence::Close);
78  INS("Save", QKeySequence::Save);
79  INS("New", QKeySequence::New);
80  INS("Delete", QKeySequence::Delete);
81  INS("Cut", QKeySequence::Cut);
82  INS("Copy", QKeySequence::Copy);
83  INS("Paste", QKeySequence::Paste);
84  INS("Undo", QKeySequence::Undo);
85  INS("Redo", QKeySequence::Redo);
86  INS("Back", QKeySequence::Back);
87  INS("Forward", QKeySequence::Forward);
88  INS("Refresh", QKeySequence::Refresh);
89  INS("ZoomIn", QKeySequence::ZoomIn);
90  INS("ZoomOut", QKeySequence::ZoomOut);
91  INS("Print", QKeySequence::Print);
92  INS("AddTab", QKeySequence::AddTab);
93  INS("NextChild", QKeySequence::NextChild);
94  INS("PreviousChild", QKeySequence::PreviousChild);
95  INS("Find", QKeySequence::Find);
96  INS("FindNext", QKeySequence::FindNext);
97  INS("FindPrevious", QKeySequence::FindPrevious);
98  INS("Replace", QKeySequence::Replace);
99  INS("SelectAll", QKeySequence::SelectAll);
100  INS("Bold", QKeySequence::Bold);
101  INS("Italic", QKeySequence::Italic);
102  INS("Underline", QKeySequence::Underline);
103  INS("MoveToNextChar", QKeySequence::MoveToNextChar);
104  INS("MoveToPreviousChar", QKeySequence::MoveToPreviousChar);
105  INS("MoveToNextWord", QKeySequence::MoveToNextWord);
106  INS("MoveToPreviousWord", QKeySequence::MoveToPreviousWord);
107  INS("MoveToNextLine", QKeySequence::MoveToNextLine);
108  INS("MoveToPreviousLine", QKeySequence::MoveToPreviousLine);
109  INS("MoveToNextPage", QKeySequence::MoveToNextPage);
110  INS("MoveToPreviousPage", QKeySequence::MoveToPreviousPage);
111  INS("MoveToStartOfLine", QKeySequence::MoveToStartOfLine);
112  INS("MoveToEndOfLine", QKeySequence::MoveToEndOfLine);
113  INS("MoveToStartOfBlock", QKeySequence::MoveToStartOfBlock);
114  INS("MoveToEndOfBlock", QKeySequence::MoveToEndOfBlock);
115  INS("MoveToStartOfDocument", QKeySequence::MoveToStartOfDocument);
116  INS("MoveToEndOfDocument", QKeySequence::MoveToEndOfDocument);
117  INS("SelectNextChar", QKeySequence::SelectNextChar);
118  INS("SelectPreviousChar", QKeySequence::SelectPreviousChar);
119  INS("SelectNextWord", QKeySequence::SelectNextWord);
120  INS("SelectPreviousWord", QKeySequence::SelectPreviousWord);
121  INS("SelectNextLine", QKeySequence::SelectNextLine);
122  INS("SelectPreviousLine", QKeySequence::SelectPreviousLine);
123  INS("SelectNextPage", QKeySequence::SelectNextPage);
124  INS("SelectPreviousPage", QKeySequence::SelectPreviousPage);
125  INS("SelectStartOfLine", QKeySequence::SelectStartOfLine);
126  INS("SelectEndOfLine", QKeySequence::SelectEndOfLine);
127  INS("SelectStartOfBlock", QKeySequence::SelectStartOfBlock);
128  INS("SelectEndOfBlock", QKeySequence::SelectEndOfBlock);
129  INS("SelectStartOfDocument", QKeySequence::SelectStartOfDocument);
130  INS("SelectEndOfDocument", QKeySequence::SelectEndOfDocument);
131  INS("DeleteStartOfWord", QKeySequence::DeleteStartOfWord);
132  INS("DeleteEndOfWord", QKeySequence::DeleteEndOfWord);
133  INS("DeleteEndOfLine", QKeySequence::DeleteEndOfLine);
134  }
135 }
bool connect(Kwave::StreamObject &source, const char *output, Kwave::StreamObject &sink, const char *input)
Definition: Connect.cpp:48
static QMap< QString, QKeySequence > m_standard_keys
Definition: MenuManager.h:140
Kwave::MenuRoot * m_menu_root
Definition: MenuManager.h:137
void sigMenuCommand(const QString &command)
Here is the call graph for this function:

◆ ~MenuManager()

Kwave::MenuManager::~MenuManager ( )
virtual

Destructor.

Definition at line 337 of file MenuManager.cpp.

References m_menu_root.

338 {
339  delete m_menu_root;
340  m_menu_root = Q_NULLPTR;
341 }
Kwave::MenuRoot * m_menu_root
Definition: MenuManager.h:137

Member Function Documentation

◆ addNumberedMenuEntry()

void Kwave::MenuManager::addNumberedMenuEntry ( const QString &  uid,
const QString &  entry,
const QString &  param 
)

Add an entry to a numbered menu

Parameters
uidunique id string of the numbered menu
entryname of the new entry (non-localized)
parama parameter to use when the entry is activated (optional, pass QString() when using the text of the menu entry as parameter)

Definition at line 225 of file MenuManager.cpp.

References _, Kwave::MenuNode::command(), DBG, Kwave::Parser::escape(), Kwave::MenuNode::findUID(), Kwave::MenuNode::insertLeaf(), m_menu_root, and Kwave::MenuNode::parentNode().

Referenced by Kwave::TopWidget::updateMenu(), and Kwave::TopWidget::updateRecentFiles().

228 {
229  Q_ASSERT(entry.length());
230  if (!entry.length()) return;
231 
232  Q_ASSERT(m_menu_root);
233  Kwave::MenuNode *node = (m_menu_root) ? m_menu_root->findUID(uid) : Q_NULLPTR;
234  Kwave::MenuNode *parent = (node) ? node->parentNode() : Q_NULLPTR;
235  if (parent) {
236  QString cmd = node->command();
237  if (cmd.contains(_("%1"))) {
238  QString p = (param.length()) ? param : entry;
239  cmd = cmd.arg(Kwave::Parser::escape(p));
240  }
241  node->insertLeaf(entry, cmd, 0, uid);
242  } else
243  qWarning("MenuManager: could not find numbered Menu '%s'", DBG(uid));
244 
245 }
virtual Kwave::MenuNode * parentNode() const
Definition: MenuNode.cpp:103
Kwave::MenuNode * findUID(const QString &uid)
Definition: MenuNode.cpp:198
const QString & command() const
Definition: MenuNode.h:77
#define _(m)
Definition: memcpy.c:66
Kwave::MenuRoot * m_menu_root
Definition: MenuManager.h:137
#define DBG(qs)
Definition: String.h:55
virtual Kwave::MenuNode * insertLeaf(const QString &name, const QString &command, const QKeySequence &shortcut, const QString &uid)
Definition: MenuNode.cpp:243
static QString escape(const QString &text)
Definition: Parser.cpp:277
Here is the call graph for this function:
Here is the caller graph for this function:

◆ clearNumberedMenu()

void Kwave::MenuManager::clearNumberedMenu ( const QString &  uid)

Deletes all entries of a numbered menu

Parameters
uidunique id string of the numbered menu

Definition at line 217 of file MenuManager.cpp.

References Kwave::MenuNode::clear(), Kwave::MenuNode::findUID(), and m_menu_root.

Referenced by Kwave::TopWidget::updateMenu(), and Kwave::TopWidget::updateRecentFiles().

218 {
219  Kwave::MenuNode *node = (m_menu_root) ?
220  m_menu_root->findUID(uid) : Q_NULLPTR;
221  if (node) node->clear();
222 }
Kwave::MenuNode * findUID(const QString &uid)
Definition: MenuNode.cpp:198
virtual void clear()
Definition: MenuNode.cpp:93
Kwave::MenuRoot * m_menu_root
Definition: MenuManager.h:137
Here is the call graph for this function:
Here is the caller graph for this function:

◆ executeCommand()

int Kwave::MenuManager::executeCommand ( const QString &  command)

Executes menu commands.

Parameters
commandstring with the command
Return values
0if succeeded
-EINVALif failed

Definition at line 138 of file MenuManager.cpp.

References _, DBG, Kwave::Parser::firstParam(), Kwave::MenuRoot::insertNode(), m_menu_root, m_standard_keys, and Kwave::Parser::nextParam().

Referenced by Kwave::TopWidget::executeCommand().

139 {
140 
141  Q_ASSERT(command.length());
142  if (!m_menu_root) return -EINVAL; // makes no sense if no menu root
143 
144  Kwave::Parser parser(command);
145 
146  QKeySequence shortcut; // keyboard shortcut (optional)
147  QString id ; // string id (optional)
148  QString param;
149 
150  // --- 1st parameter: command to be sent when selected ---
151  QString com = parser.firstParam();
152 
153  // --- 2nd parameter: position in the menu structure ---
154  QString pos = parser.nextParam();
155 
156  // bail out if no menu position is found
157  if (!pos.length()) {
158  qWarning("no position field !");
159  return -EINVAL;
160  }
161 
162  // --- 3rd parameter: bitmask for the key shortcut (optional) ---
163  param = parser.nextParam();
164  if (param.length()) {
165  // replace "::<StandardKeyName>" with the key sequence as string
166  QRegExp rx(_("::(\\w+)"), Qt::CaseInsensitive);
167  int p = 0;
168  while ((p = rx.indexIn(param, 0)) >= 0) {
169  QString stdname = rx.cap(1);
170  if (m_standard_keys.contains(stdname)) {
171  // translate into a key sequence
172  QKeySequence sequence = m_standard_keys[stdname];
173  QString expanded = sequence.toString();
174  param = param.replace(p, stdname.length() + 2, expanded);
175  } else {
176  // unknown standard key sequence name?
177  qWarning("MenuManager::executeCommand: pos=%d, stdname='%s' "
178  "-> UNKNOWN ???", p, DBG(stdname));
179  break;
180  }
181  }
182 
183  // default case: direct specification of a key sequence
184  shortcut = QKeySequence::fromString(i18n(param.toLatin1()));
185  }
186 
187  // --- 4rth parameter: parse the string id of the node (optional) ---
188  param = parser.nextParam();
189  if (param.length()) id = param;
190 
191 #ifdef DEBUG
192 // qDebug("MenuManager: insertNode('', '%s', '%s', %s, '%s')",
193 // DBG(pos), DBG(com), DBG(shortcut.toString()), DBG(id));
194  if (!shortcut.isEmpty()) {
195  static QMap<QString, QString> used_shortcuts;
196 
197  QString sc = shortcut.toString();
198  QString m = pos.left(pos.indexOf(_("/#")));
199 
200  if (used_shortcuts.contains(sc) && (used_shortcuts[sc] != m)) {
201  qWarning("MenuManager: insertNode('%s')", DBG(m));
202  qWarning(" shortcut %s already in use for '%s'",
203  DBG(sc), DBG(used_shortcuts[sc]));
204  } else {
205  used_shortcuts[sc] = m;
206  }
207  }
208 #endif /* DEBUG */
209 
210  // --- insert the new node into the menu structure ---
211  m_menu_root->insertNode(QString(), pos, com, shortcut, id);
212 
213  return 0;
214 }
virtual void insertNode(const QString &name, const QString &position, const QString &command, const QKeySequence &shortcut, const QString &uid) Q_DECL_OVERRIDE
Definition: MenuRoot.cpp:57
static QMap< QString, QKeySequence > m_standard_keys
Definition: MenuManager.h:140
#define _(m)
Definition: memcpy.c:66
Kwave::MenuRoot * m_menu_root
Definition: MenuManager.h:137
#define DBG(qs)
Definition: String.h:55
Here is the call graph for this function:
Here is the caller graph for this function:

◆ selectItem()

void Kwave::MenuManager::selectItem ( const QString &  group,
const QString &  uid 
)

Selects an menu item within a group of menu items. All other items will be deselected and the new one will become the only selected one. (exclusive one-of-n-selection)

Parameters
groupname of the menu group
uidunique id string specifying the new selection

Definition at line 248 of file MenuManager.cpp.

References DBG, Kwave::MenuRoot::groupList(), m_menu_root, and Kwave::MenuGroup::selectItem().

Referenced by Kwave::TopWidget::updateMenu().

249 {
250  if (!m_menu_root)
251  return;
252 
253  if (!group.length()) {
254  qWarning("MenuManager::selectItem('','%s'): no group!?", DBG(uid));
255  return;
256  }
257 
258  if (group[0] != QLatin1Char('@')) {
259  qWarning("MenuManager::selectItem('%s','%s'): "
260  "invalid group name, does not start with '@'!",
261  DBG(group), DBG(uid));
262  return;
263  }
264 
265  QHash<QString, Kwave::MenuGroup *> &groups = m_menu_root->groupList();
266  if (!groups.contains(group)) {
267  qWarning("MenuManager::selectItem(): group '%s' not found!",
268  DBG(group));
269  return;
270  }
271 
272  Kwave::MenuGroup *group_node = groups[group];
273  Q_ASSERT(group_node);
274  if (group_node) group_node->selectItem(uid);
275 }
virtual QHash< QString, Kwave::MenuGroup * > & groupList() Q_DECL_OVERRIDE
Definition: MenuRoot.cpp:51
virtual void selectItem(const QString &uid)
Definition: MenuGroup.cpp:90
Kwave::MenuRoot * m_menu_root
Definition: MenuManager.h:137
#define DBG(qs)
Definition: String.h:55
Here is the call graph for this function:
Here is the caller graph for this function:

◆ setItemChecked

void Kwave::MenuManager::setItemChecked ( const QString &  uid,
bool  check 
)
slot

Checks/unchecks a menu node.

Parameters
uidunique id string of the menu node
checktrue to set a checkmark, false to remove

Definition at line 278 of file MenuManager.cpp.

References Kwave::MenuNode::findUID(), m_menu_root, and Kwave::MenuNode::setChecked().

279 {
280  Kwave::MenuNode *node = (m_menu_root) ?
281  m_menu_root->findUID(uid) : Q_NULLPTR;
282  if (node) node->setChecked(check);
283 }
Kwave::MenuNode * findUID(const QString &uid)
Definition: MenuNode.cpp:198
virtual void setChecked(bool check)
Definition: MenuNode.cpp:169
Kwave::MenuRoot * m_menu_root
Definition: MenuManager.h:137
Here is the call graph for this function:

◆ setItemEnabled

void Kwave::MenuManager::setItemEnabled ( const QString &  uid,
bool  enable 
)
slot

Enables/disables a menu node.

Parameters
uidunique id string of the menu node
enabletrue to enable, false to disable

Definition at line 309 of file MenuManager.cpp.

References DBG, Kwave::MenuNode::findUID(), Kwave::MenuRoot::groupList(), m_menu_root, Kwave::MenuGroup::setEnabled(), and Kwave::MenuNode::setEnabled().

Referenced by Kwave::TopWidget::clipboardChanged(), Kwave::TopWidget::selectionChanged(), Kwave::TopWidget::setUndoRedoInfo(), Kwave::TopWidget::updateMenu(), Kwave::TopWidget::updateRecentFiles(), and Kwave::PlayerToolBar::updateState().

310 {
311  if (!m_menu_root) return;
312 
313  bool found = false;
314  Kwave::MenuNode *node = m_menu_root->findUID(uid);
315  if (node) {
316  /* enable/disable a single menu node */
317  node->setEnabled(enable);
318  found = true;
319  } else {
320  /* enable/disable a group */
321  QHash<QString, Kwave::MenuGroup *> &groups = m_menu_root->groupList();
322  if (groups.contains(uid)) {
323  Kwave::MenuGroup *group = groups[uid];
324  if (group) {
325  group->setEnabled(enable);
326  found = true;
327  }
328  }
329  }
330 
331  if (!found)
332  qWarning("MenuManager::setItemEnabled('%s', '%d'): uid not found!",
333  DBG(uid), enable);
334 }
virtual QHash< QString, Kwave::MenuGroup * > & groupList() Q_DECL_OVERRIDE
Definition: MenuRoot.cpp:51
Kwave::MenuNode * findUID(const QString &uid)
Definition: MenuNode.cpp:198
virtual void setEnabled(bool enable)
Definition: MenuNode.cpp:163
Kwave::MenuRoot * m_menu_root
Definition: MenuManager.h:137
#define DBG(qs)
Definition: String.h:55
virtual void setEnabled(bool enable)
Definition: MenuGroup.cpp:82
Here is the call graph for this function:
Here is the caller graph for this function:

◆ setItemText()

void Kwave::MenuManager::setItemText ( const QString &  uid,
const QString &  text 
)

Sets the text of a menu entry to a new value.

Parameters
uidunique id string of the menu node
textthe new text of the item

Definition at line 286 of file MenuManager.cpp.

References Kwave::MenuNode::findUID(), m_menu_root, and Kwave::MenuNode::setText().

Referenced by Kwave::TopWidget::setUndoRedoInfo().

287 {
288  Kwave::MenuNode *node = (m_menu_root) ?
289  m_menu_root->findUID(uid) : Q_NULLPTR;
290  if (node) node->setText(text);
291 }
Kwave::MenuNode * findUID(const QString &uid)
Definition: MenuNode.cpp:198
Kwave::MenuRoot * m_menu_root
Definition: MenuManager.h:137
virtual void setText(const QString &text)
Definition: MenuNode.cpp:175
Here is the call graph for this function:
Here is the caller graph for this function:

◆ setItemVisible()

void Kwave::MenuManager::setItemVisible ( const QString &  uid,
bool  show 
)

Shows/hides a menu entry identified by an ID. Groups are not supported.

Parameters
uidunique id string of the menu node
showtrue to show, false to hide

Definition at line 294 of file MenuManager.cpp.

References DBG, Kwave::MenuNode::findUID(), m_menu_root, and Kwave::MenuNode::setVisible().

Referenced by Kwave::TopWidget::updateMenu().

295 {
296  if (!m_menu_root) return;
297 
298  Kwave::MenuNode *node = m_menu_root->findUID(uid);
299  if (node) {
300  /* show/hide a single menu node */
301  node->setVisible(show);
302  } else {
303  qWarning("MenuManager::setItemVisible('%s', '%d'): uid not found!",
304  DBG(uid), show);
305  }
306 }
Kwave::MenuNode * findUID(const QString &uid)
Definition: MenuNode.cpp:198
virtual void setVisible(bool visible)
Definition: MenuNode.cpp:157
Kwave::MenuRoot * m_menu_root
Definition: MenuManager.h:137
#define DBG(qs)
Definition: String.h:55
Here is the call graph for this function:
Here is the caller graph for this function:

◆ sigMenuCommand

void Kwave::MenuManager::sigMenuCommand ( const QString &  command)
signal

Will be emitted if the command of a menu node should be executed.

See also
MenuNode.sigCommand()

Referenced by MenuManager().

Here is the caller graph for this function:

Member Data Documentation

◆ m_menu_root

Kwave::MenuRoot* Kwave::MenuManager::m_menu_root
private

◆ m_standard_keys

QMap< QString, QKeySequence > Kwave::MenuManager::m_standard_keys
staticprivate

map of standard key names / key sequences

static map with standard keys

Definition at line 140 of file MenuManager.h.

Referenced by executeCommand(), and MenuManager().


The documentation for this class was generated from the following files: