File: MenuSub.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 (189 lines) | stat: -rw-r--r-- 6,376 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
184
185
186
187
188
189
/***************************************************************************
                          MenuSub.cpp  -  submenu
                             -------------------
    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 <QPixmap>

#include <KLocalizedString>

#include "libkwave/String.h"

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

//***************************************************************************
Kwave::MenuSub::MenuSub(Kwave::MenuNode *parent,
                        QMenu *menu,
                        const QString &name,
                        const QString &command,
                        const QKeySequence &shortcut,
                        const QString &uid)
    :Kwave::MenuNode(parent, name, command, shortcut, uid), m_menu(menu)
{
    QAction *act = action();
    Q_ASSERT(act);
    if (act) act->setText(i18nc(UTF8(_("menu: ") + path()), UTF8(name)));
}

//***************************************************************************
Kwave::MenuSub::~MenuSub()
{
}

//*****************************************************************************
void Kwave::MenuSub::setVisible(bool visible)
{
    if (!m_menu) return;

    Kwave::MenuRoot *root = qobject_cast<Kwave::MenuRoot *>(rootNode());
    Q_ASSERT(root);
    if (root && (parentNode() == root)) {
        // special case: entries of the main menu can only be made
        // visible/invisible by adding to/removing from the main menu
        if (visible) {
            root->showChild(this);
        }
        else
            root->hideChild(this);
    } else {
        // normal menu entry
        m_menu->setVisible(visible);
    }
}

//*****************************************************************************
bool Kwave::MenuSub::isEnabled()
{
    if (m_menu && !m_menu->isEnabled()) return false;
    return Kwave::MenuNode::isEnabled();
}

//*****************************************************************************
void Kwave::MenuSub::setEnabled(bool enable)
{
    if (m_menu) m_menu->setEnabled(enable);
}

//*****************************************************************************
const QIcon Kwave::MenuSub::icon()
{
    return (m_menu) ? m_menu->icon() : QIcon();
}

//*****************************************************************************
void Kwave::MenuSub::setIcon(const QIcon &icon)
{
    if (m_menu) m_menu->setIcon(icon);
}

//***************************************************************************
Kwave::MenuSub *Kwave::MenuSub::insertBranch(const QString &name,
                                             const QString &command,
                                             const QKeySequence &shortcut,
                                             const QString &uid)
{
    QMenu *menu = (m_menu) ? m_menu->addMenu(name) : nullptr;
    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::MenuSub::insertLeaf(const QString &name,
                                            const QString &command,
                                            const QKeySequence &shortcut,
                                            const QString &uid)
{
    Q_ASSERT(name.length());
    Q_ASSERT(m_menu);
    if (!name.length() || !m_menu) return nullptr;

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

    /*
     * find out where to insert the leaf: if there is a placeholder
     * with the matching uid, insert after that one, otherwise append
     * to the end
     */
    bool found = false;
    Kwave::MenuNode *child_after = nullptr;
    QListIterator<Kwave::MenuNode *> it(m_children);
    it.toBack();
    while (!found && it.hasPrevious()) {
        Kwave::MenuNode *child = it.previous();
        if (!child) continue;
        if (uid.length() && ((uid == child->uid()) || (uid == child->name())))
            found = true;
        else if (child->action())
            child_after = child;
    }

    insertChild(item, (found) ? child_after : nullptr);

    QAction *action_after = (found && child_after) ?
        child_after->action() : nullptr;
    if (action_after)
        m_menu->insertAction(action_after, item->action());
    else
        m_menu->addAction(item->action());

    return item;
}

//***************************************************************************
void Kwave::MenuSub::removeChild(Kwave::MenuNode *child)
{
    QAction *act = (child) ? child->action() : nullptr;
    if (act && m_menu) m_menu->removeAction(act);

    Kwave::MenuNode::removeChild(child);
}

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

    if (command.startsWith(_("#exclusive"))) {
        return true;
    } else if (command.startsWith(_("#separator"))) {
        if (m_menu) m_menu->addSeparator();
        return true;
    }

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

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

#include "moc_MenuSub.cpp"