File: MenuRoot.cpp

package info (click to toggle)
kwave 25.04.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 23,272 kB
  • sloc: cpp: 56,173; xml: 817; perl: 688; sh: 57; makefile: 11
file content (183 lines) | stat: -rw-r--r-- 6,248 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/***************************************************************************
                          MenuRoot.cpp  -  root node of a menu structure
                             -------------------
    begin                : Mon Jan 10 2000
    copyright            : (C) 2000 by Thomas Eschenbacher
    email                : Thomas.Eschenbacher@gmx.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/


#include <new>

#include <QListIterator>

#include <KLocalizedString>
#include <QMenuBar>

#include "libkwave/String.h"

#include "libgui/MenuItem.h"
#include "libgui/MenuRoot.h"
#include "libgui/MenuSub.h"

//***************************************************************************
/** garbage collector for menu nodes */
QList<Kwave::MenuNode *> Kwave::MenuRoot::m_garbage;

//***************************************************************************
Kwave::MenuRoot::MenuRoot(QMenuBar &bar)
    :Kwave::MenuNode(nullptr, _("(root)"), QString(), 0, QString()),
     m_menu_bar(bar), m_group_list()
{
}

//***************************************************************************
Kwave::MenuRoot::~MenuRoot()
{
    clear();
}

//***************************************************************************
QHash<QString, Kwave::MenuGroup *> &Kwave::MenuRoot::groupList()
{
    return m_group_list;
}

//*****************************************************************************
void Kwave::MenuRoot::insertNode(const QString &name,
                                 const QString &position,
                                 const QString &command,
                                 const QKeySequence &shortcut,
                                 const QString &uid)
{
    Kwave::MenuNode::insertNode(name, position, command, shortcut, uid);

    // now delete all leafs that have been converted to branches
    while (!m_garbage.isEmpty()) {
        delete m_garbage.takeFirst();
    }
}

//***************************************************************************
Kwave::MenuSub *Kwave::MenuRoot::insertBranch(const QString &name,
                                              const QString &command,
                                              const QKeySequence &shortcut,
                                              const QString &uid)
{
    QMenu *menu = m_menu_bar.addMenu(name);
    Q_ASSERT(menu);
    if (!menu) return nullptr;

    Kwave::MenuSub *sub = new(std::nothrow)
        Kwave::MenuSub(this, menu, name, command, shortcut, uid);
    Q_ASSERT(sub);
    if (!sub) return nullptr;

    insertChild(sub, nullptr);

    return sub;
}

//***************************************************************************
Kwave::MenuNode *Kwave::MenuRoot::insertLeaf(const QString &name,
                                             const QString &command,
                                             const QKeySequence &shortcut,
                                             const QString &uid)
{
    Kwave::MenuItem *item = new(std::nothrow)
        Kwave::MenuItem(this, name, command, shortcut, uid);
    Q_ASSERT(item);
    if (!item) return nullptr;

    insertChild(item, nullptr);
    m_menu_bar.addAction(item->action());

    return item;
}

//***************************************************************************
void Kwave::MenuRoot::hideChild(Kwave::MenuSub *child)
{
    Q_ASSERT(child);
    if (!child) return;
    if (!m_children.contains(child)) return;
    if (groupList().contains(child->name())) return;

    QAction *action = child->action();
    if (action) m_menu_bar.removeAction(action);
}

//***************************************************************************
void Kwave::MenuRoot::showChild(Kwave::MenuSub *child)
{
    Q_ASSERT(child);
    if (!child) return;
    if (!m_children.contains(child)) return;
    if (groupList().contains(child->name())) return;

    // find the menu bar entry after which we can insert
    QAction *action_before = nullptr;
    QListIterator<Kwave::MenuNode *> it(m_children);
    it.toBack();
    while (it.hasPrevious()) {
        Kwave::MenuNode *c = it.previous();
        if (c == child) break;
        if (c) action_before = c->action();
    }

    if (action_before)
        m_menu_bar.insertMenu(action_before, child->menu());
    else
        m_menu_bar.addMenu(child->menu());
}

//***************************************************************************
void Kwave::MenuRoot::removeChild(Kwave::MenuNode *child)
{
    Q_ASSERT(child);
    if (!child) return;
    if (!m_children.contains(child)) return;

    QHash<QString, Kwave::MenuGroup *> &group_list = groupList();
    if (!group_list.contains(child->name())) {
        // only remove what has been added to the menu bar,
        // but not menu groups
        QAction *action = child->action();
        if (action) m_menu_bar.removeAction(action);
    }
    Kwave::MenuNode::removeChild(child);
}

//***************************************************************************
bool Kwave::MenuRoot::specialCommand(const QString &command)
{
    Q_ASSERT(command.length());
    if (!command.length()) return false;

    if (command == _("#separator")) {
        m_menu_bar.addSeparator();
        return true;
    }

    return Kwave::MenuNode::specialCommand(command);
}

//***************************************************************************
void Kwave::MenuRoot::deleteLater(Kwave::MenuNode *node)
{
    if (node) m_garbage.append(node);
}

//***************************************************************************
//***************************************************************************

#include "moc_MenuRoot.cpp"