kwave  18.07.70
MenuSub.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  MenuSub.cpp - submenu
3  -------------------
4  begin : Mon Jan 10 2000
5  copyright : (C) 2000 by Thomas Eschenbacher
6  email : Thomas.Eschenbacher@gmx.de
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #include "config.h"
19 
20 #include <new>
21 
22 #include <QPixmap>
23 
24 #include <KLocalizedString>
25 
26 #include "libkwave/String.h"
27 
28 #include "libgui/MenuItem.h"
29 #include "libgui/MenuRoot.h"
30 #include "libgui/MenuSub.h"
31 
32 //***************************************************************************
34  QMenu *menu,
35  const QString &name,
36  const QString &command,
37  const QKeySequence &shortcut,
38  const QString &uid)
39  :Kwave::MenuNode(parent, name, command, shortcut, uid), m_menu(menu)
40 {
41  QAction *act = action();
42  Q_ASSERT(act);
43  if (act) act->setText(i18nc(UTF8(_("menu: ") + path()), UTF8(name)));
44 }
45 
46 //***************************************************************************
48 {
49 }
50 
51 //*****************************************************************************
52 void Kwave::MenuSub::setVisible(bool visible)
53 {
54  if (!m_menu) return;
55 
56  Kwave::MenuRoot *root = qobject_cast<Kwave::MenuRoot *>(rootNode());
57  Q_ASSERT(root);
58  if (root && (parentNode() == root)) {
59  // special case: entries of the main menu can only be made
60  // visible/invisible by adding to/removing from the main menu
61  if (visible) {
62  root->showChild(this);
63  }
64  else
65  root->hideChild(this);
66  } else {
67  // normal menu entry
68  m_menu->setVisible(visible);
69  }
70 }
71 
72 //*****************************************************************************
74 {
75  if (m_menu && !m_menu->isEnabled()) return false;
77 }
78 
79 //*****************************************************************************
80 void Kwave::MenuSub::setEnabled(bool enable)
81 {
82  if (m_menu) m_menu->setEnabled(enable);
83 }
84 
85 //*****************************************************************************
86 const QIcon Kwave::MenuSub::icon()
87 {
88  return (m_menu) ? m_menu->icon() : QIcon();
89 }
90 
91 //*****************************************************************************
92 void Kwave::MenuSub::setIcon(const QIcon &icon)
93 {
94  if (m_menu) m_menu->setIcon(icon);
95 }
96 
97 //***************************************************************************
99  const QString &command,
100  const QKeySequence &shortcut,
101  const QString &uid)
102 {
103  QMenu *menu = (m_menu) ? m_menu->addMenu(name) : Q_NULLPTR;
104  Q_ASSERT(menu);
105  if (!menu) return Q_NULLPTR;
106 
107  Kwave::MenuSub *sub = new(std::nothrow)
108  Kwave::MenuSub(this, menu, name, command, shortcut, uid);
109  Q_ASSERT(sub);
110  if (!sub) return Q_NULLPTR;
111 
112  insertChild(sub, Q_NULLPTR);
113 
114  return sub;
115 }
116 
117 //***************************************************************************
119  const QString &command,
120  const QKeySequence &shortcut,
121  const QString &uid)
122 {
123  Q_ASSERT(name.length());
124  Q_ASSERT(m_menu);
125  if (!name.length() || !m_menu) return Q_NULLPTR;
126 
127  Kwave::MenuItem *item = new(std::nothrow)
128  Kwave::MenuItem(this, name, command, shortcut, uid);
129  Q_ASSERT(item);
130  if (!item) return Q_NULLPTR;
131 
132  /*
133  * find out where to insert the leaf: if there is a placeholder
134  * with the matching uid, insert after that one, otherwise append
135  * to the end
136  */
137  bool found = false;
138  Kwave::MenuNode *child_after = Q_NULLPTR;
139  QListIterator<Kwave::MenuNode *> it(m_children);
140  it.toBack();
141  while (!found && it.hasPrevious()) {
142  Kwave::MenuNode *child = it.previous();
143  if (!child) continue;
144  if (uid.length() && ((uid == child->uid()) || (uid == child->name())))
145  found = true;
146  else if (child->action())
147  child_after = child;
148  }
149 
150  insertChild(item, (found) ? child_after : Q_NULLPTR);
151 
152  QAction *action_after = (found && child_after) ?
153  child_after->action() : Q_NULLPTR;
154  if (action_after)
155  m_menu->insertAction(action_after, item->action());
156  else
157  m_menu->addAction(item->action());
158 
159  return item;
160 }
161 
162 //***************************************************************************
164 {
165  QAction *act = (child) ? child->action() : Q_NULLPTR;
166  if (act && m_menu) m_menu->removeAction(act);
167 
169 }
170 
171 //***************************************************************************
173 {
174  Q_ASSERT(command.length());
175  if (!command.length()) return false;
176 
177  if (command.startsWith(_("#exclusive"))) {
178  return true;
179  } else if (command.startsWith(_("#separator"))) {
180  if (m_menu) m_menu->addSeparator();
181  return true;
182  }
183 
184  return Kwave::MenuNode::specialCommand(command);
185 }
186 
187 //***************************************************************************
188 //***************************************************************************
const QString path() const
Definition: MenuNode.cpp:63
virtual Kwave::MenuNode * parentNode() const
Definition: MenuNode.cpp:103
virtual const QIcon icon() Q_DECL_OVERRIDE
Definition: MenuSub.cpp:86
Definition: App.h:33
virtual ~MenuSub() Q_DECL_OVERRIDE
Definition: MenuSub.cpp:47
virtual Kwave::MenuNode * insertLeaf(const QString &name, const QString &command, const QKeySequence &shortcut, const QString &uid) Q_DECL_OVERRIDE
Definition: MenuSub.cpp:118
virtual bool specialCommand(const QString &command) Q_DECL_OVERRIDE
Definition: MenuSub.cpp:172
QMenu * m_menu
Definition: MenuSub.h:155
virtual bool isEnabled() Q_DECL_OVERRIDE
Definition: MenuSub.cpp:73
const QKeySequence & shortcut() const
Definition: MenuNode.h:95
virtual void setVisible(bool visible) Q_DECL_OVERRIDE
Definition: MenuSub.cpp:52
virtual void setIcon(const QIcon &icon) Q_DECL_OVERRIDE
Definition: MenuSub.cpp:92
virtual void removeChild(Kwave::MenuNode *child)
Definition: MenuNode.cpp:223
virtual void insertChild(Kwave::MenuNode *node, Kwave::MenuNode *before)
Definition: MenuNode.cpp:181
const char name[16]
Definition: memcpy.c:510
virtual bool specialCommand(const QString &command)
Definition: MenuNode.cpp:421
const QString & name() const
Definition: MenuNode.h:74
const QString & command() const
Definition: MenuNode.h:77
virtual QMenu * menu()
Definition: MenuSub.h:150
virtual bool isEnabled()
Definition: MenuNode.cpp:130
QList< Kwave::MenuNode * > m_children
Definition: MenuNode.h:317
virtual QAction * action() Q_DECL_OVERRIDE
Definition: MenuSub.h:102
virtual Kwave::MenuSub * insertBranch(const QString &name, const QString &command, const QKeySequence &shortcut, const QString &uid) Q_DECL_OVERRIDE
Definition: MenuSub.cpp:98
#define _(m)
Definition: memcpy.c:66
const QString & uid() const
Definition: MenuNode.h:85
virtual void removeChild(Kwave::MenuNode *child) Q_DECL_OVERRIDE
Definition: MenuSub.cpp:163
void hideChild(Kwave::MenuSub *child)
Definition: MenuRoot.cpp:110
MenuSub(Kwave::MenuNode *parent, QMenu *menu, const QString &name, const QString &command, const QKeySequence &shortcut, const QString &uid)
Definition: MenuSub.cpp:33
Kwave::MenuNode * rootNode()
Definition: MenuNode.cpp:109
virtual QAction * action() Q_DECL_OVERRIDE
Definition: MenuItem.h:122
#define UTF8(qs)
Definition: String.h:48
void showChild(Kwave::MenuSub *child)
Definition: MenuRoot.cpp:122
virtual void setEnabled(bool enable) Q_DECL_OVERRIDE
Definition: MenuSub.cpp:80
virtual QAction * action()
Definition: MenuNode.h:80