File: MenuManager.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 (348 lines) | stat: -rw-r--r-- 13,852 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/***************************************************************************
        MenuManager.cpp  -  manager class for Kwave's menu structure
                             -------------------
    begin                : Sun Jun 4 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 <errno.h>
#include <stdlib.h>

#include <new>

#include <QKeySequence>
#include <QLatin1Char>
#include <QRegularExpression>
#include <QString>
#include <QWidget>

#include <KLocalizedString>

#include "libkwave/Parser.h"
#include "libkwave/String.h"

#include "libgui/MenuGroup.h"
#include "libgui/MenuManager.h"
#include "libgui/MenuNode.h"
#include "libgui/MenuRoot.h"

//***************************************************************************
/** static map with standard keys */
QMap<QString, QKeySequence> Kwave::MenuManager::m_standard_keys;

/** helper macro for inserting an entry into the m_standard_keys map */
#define INS(n,v) m_standard_keys.insert(_(n), v)

//***************************************************************************
Kwave::MenuManager::MenuManager(QWidget *parent, QMenuBar &bar)
    :QObject(parent)
{
    m_menu_root = new(std::nothrow) Kwave::MenuRoot(bar);
    Q_ASSERT(m_menu_root);
    if (m_menu_root) {
        connect(
            m_menu_root, SIGNAL(sigCommand(QString)),
            this, SIGNAL(sigMenuCommand(QString)),
            Qt::QueuedConnection
        );
    }

    // fill the list of standard keys if it is empty.
    //
    // HINT: I have gerated the list below through copy&paste of the
    //       description of "enum QKeySequence::StandardKey" into
    //       list.txt and then:
    //
    // cat list.txt | sed s/^\ \ //g | cut -d \  -f 1 | cut -d \: -f 3 |
    // awk '{ print "\tINS(\""$0"\", QKeySequence::"$0");"}'

    if (m_standard_keys.isEmpty()) {
        INS("UnknownKey",            QKeySequence::UnknownKey);
        INS("HelpContents",          QKeySequence::HelpContents);
        INS("WhatsThis",             QKeySequence::WhatsThis);
        INS("Open",                  QKeySequence::Open);
        INS("Close",                 QKeySequence::Close);
        INS("Save",                  QKeySequence::Save);
        INS("New",                   QKeySequence::New);
        INS("Delete",                QKeySequence::Delete);
        INS("Cut",                   QKeySequence::Cut);
        INS("Copy",                  QKeySequence::Copy);
        INS("Paste",                 QKeySequence::Paste);
        INS("Undo",                  QKeySequence::Undo);
        INS("Redo",                  QKeySequence::Redo);
        INS("Back",                  QKeySequence::Back);
        INS("Forward",               QKeySequence::Forward);
        INS("Refresh",               QKeySequence::Refresh);
        INS("ZoomIn",                QKeySequence::ZoomIn);
        INS("ZoomOut",               QKeySequence::ZoomOut);
        INS("Print",                 QKeySequence::Print);
        INS("AddTab",                QKeySequence::AddTab);
        INS("NextChild",             QKeySequence::NextChild);
        INS("PreviousChild",         QKeySequence::PreviousChild);
        INS("Find",                  QKeySequence::Find);
        INS("FindNext",              QKeySequence::FindNext);
        INS("FindPrevious",          QKeySequence::FindPrevious);
        INS("Replace",               QKeySequence::Replace);
        INS("SelectAll",             QKeySequence::SelectAll);
        INS("Bold",                  QKeySequence::Bold);
        INS("Italic",                QKeySequence::Italic);
        INS("Underline",             QKeySequence::Underline);
        INS("MoveToNextChar",        QKeySequence::MoveToNextChar);
        INS("MoveToPreviousChar",    QKeySequence::MoveToPreviousChar);
        INS("MoveToNextWord",        QKeySequence::MoveToNextWord);
        INS("MoveToPreviousWord",    QKeySequence::MoveToPreviousWord);
        INS("MoveToNextLine",        QKeySequence::MoveToNextLine);
        INS("MoveToPreviousLine",    QKeySequence::MoveToPreviousLine);
        INS("MoveToNextPage",        QKeySequence::MoveToNextPage);
        INS("MoveToPreviousPage",    QKeySequence::MoveToPreviousPage);
        INS("MoveToStartOfLine",     QKeySequence::MoveToStartOfLine);
        INS("MoveToEndOfLine",       QKeySequence::MoveToEndOfLine);
        INS("MoveToStartOfBlock",    QKeySequence::MoveToStartOfBlock);
        INS("MoveToEndOfBlock",      QKeySequence::MoveToEndOfBlock);
        INS("MoveToStartOfDocument", QKeySequence::MoveToStartOfDocument);
        INS("MoveToEndOfDocument",   QKeySequence::MoveToEndOfDocument);
        INS("SelectNextChar",        QKeySequence::SelectNextChar);
        INS("SelectPreviousChar",    QKeySequence::SelectPreviousChar);
        INS("SelectNextWord",        QKeySequence::SelectNextWord);
        INS("SelectPreviousWord",    QKeySequence::SelectPreviousWord);
        INS("SelectNextLine",        QKeySequence::SelectNextLine);
        INS("SelectPreviousLine",    QKeySequence::SelectPreviousLine);
        INS("SelectNextPage",        QKeySequence::SelectNextPage);
        INS("SelectPreviousPage",    QKeySequence::SelectPreviousPage);
        INS("SelectStartOfLine",     QKeySequence::SelectStartOfLine);
        INS("SelectEndOfLine",       QKeySequence::SelectEndOfLine);
        INS("SelectStartOfBlock",    QKeySequence::SelectStartOfBlock);
        INS("SelectEndOfBlock",      QKeySequence::SelectEndOfBlock);
        INS("SelectStartOfDocument", QKeySequence::SelectStartOfDocument);
        INS("SelectEndOfDocument",   QKeySequence::SelectEndOfDocument);
        INS("DeleteStartOfWord",     QKeySequence::DeleteStartOfWord);
        INS("DeleteEndOfWord",       QKeySequence::DeleteEndOfWord);
        INS("DeleteEndOfLine",       QKeySequence::DeleteEndOfLine);
    }
}

//***************************************************************************
int Kwave::MenuManager::executeCommand(const QString &command)
{

    Q_ASSERT(command.length());
    if (!m_menu_root) return -EINVAL; // makes no sense if no menu root

    Kwave::Parser parser(command);

    QKeySequence shortcut;  // keyboard shortcut (optional)
    QString id ;            // string id (optional)
    QString param;

    // --- 1st parameter: command to be sent when selected ---
    QString com = parser.firstParam();

    // --- 2nd parameter: position in the menu structure ---
    QString pos = parser.nextParam();

    // bail out if no menu position is found
    if (!pos.length()) {
        qWarning("no position field !");
        return -EINVAL;
    }

    // --- 3rd parameter: bitmask for the key shortcut (optional) ---
    param = parser.nextParam();
    if (param.length()) {
        // replace "::<StandardKeyName>" with the key sequence as string
        QRegularExpression rx{_("::(\\w+)"),
            QRegularExpression::CaseInsensitiveOption};
        QRegularExpressionMatch match;
        qsizetype p = 0;
        while ((p = param.indexOf(rx, 0, &match)) >= 0) {
            QString stdname = match.captured(1);
            if (m_standard_keys.contains(stdname)) {
                // translate into a key sequence
                QKeySequence sequence = m_standard_keys[stdname];
                QString expanded = sequence.toString();
                param = param.replace(p, stdname.length() + 2, expanded);
            } else {
                // unknown standard key sequence name?
                qWarning("MenuManager::executeCommand: pos=%d, stdname='%s' "
                         "-> UNKNOWN ???",
                         static_cast<int>(p), DBG(stdname));
                break;
            }
        }

        // default case: direct specification of a key sequence
        shortcut = QKeySequence::fromString(param);
    }

    // --- 4rth parameter: parse the string id of the node (optional) ---
    param = parser.nextParam();
    if (param.length()) id = param;

#ifdef DEBUG
//     qDebug("MenuManager: insertNode('', '%s', '%s', %s, '%s')",
//         DBG(pos), DBG(com), DBG(shortcut.toString()), DBG(id));
    if (!shortcut.isEmpty()) {
        static QMap<QString, QString> used_shortcuts;

        QString sc = shortcut.toString();
        QString m  = pos.left(pos.indexOf(_("/#")));

        if (used_shortcuts.contains(sc) && (used_shortcuts[sc] != m)) {
            qWarning("MenuManager: insertNode('%s')", DBG(m));
            qWarning("    shortcut %s already in use for '%s'",
                     DBG(sc), DBG(used_shortcuts[sc]));
        } else {
            used_shortcuts[sc] = m;
        }
    }
#endif /* DEBUG */

    // --- insert the new node into the menu structure ---
    m_menu_root->insertNode(QString(), pos, com, shortcut, id);

    return 0;
}

//***************************************************************************
void Kwave::MenuManager::clearNumberedMenu(const QString &uid)
{
    Kwave::MenuNode *node = (m_menu_root) ?
        m_menu_root->findUID(uid) : nullptr;
    if (node) node->clear();
}

//***************************************************************************
void Kwave::MenuManager::addNumberedMenuEntry(const QString &uid,
                                              const QString &entry,
                                              const QString &param)
{
    Q_ASSERT(entry.length());
    if (!entry.length()) return;

    Q_ASSERT(m_menu_root);
    Kwave::MenuNode *node = (m_menu_root) ? m_menu_root->findUID(uid) :
                                            nullptr;
    Kwave::MenuNode *parent = (node) ? node->parentNode() : nullptr;
    if (parent) {
        QString cmd = node->command();
        if (cmd.contains(_("%1"))) {
            QString p = (param.length()) ? param : entry;
            cmd = cmd.arg(Kwave::Parser::escape(p));
        }
        node->insertLeaf(entry, cmd, 0, uid);
    } else
        qWarning("MenuManager: could not find numbered Menu '%s'", DBG(uid));

}

//***************************************************************************
void Kwave::MenuManager::selectItem(const QString &group, const QString &uid)
{
    if (!m_menu_root)
        return;

    if (!group.length()) {
        qWarning("MenuManager::selectItem('','%s'): no group!?", DBG(uid));
        return;
    }

    if (group[0] != QLatin1Char('@')) {
        qWarning("MenuManager::selectItem('%s','%s'): "
                "invalid group name, does not start with '@'!",
                DBG(group), DBG(uid));
        return;
    }

    QHash<QString, Kwave::MenuGroup *> &groups = m_menu_root->groupList();
    if (!groups.contains(group)) {
        qWarning("MenuManager::selectItem(): group '%s' not found!",
                 DBG(group));
        return;
    }

    Kwave::MenuGroup *group_node = groups[group];
    Q_ASSERT(group_node);
    if (group_node) group_node->selectItem(uid);
}

//***************************************************************************
void Kwave::MenuManager::setItemChecked(const QString &uid, bool check)
{
    Kwave::MenuNode *node = (m_menu_root) ?
        m_menu_root->findUID(uid) : nullptr;
    if (node) node->setChecked(check);
}

//***************************************************************************
void Kwave::MenuManager::setItemText(const QString &uid, const QString &text)
{
    Kwave::MenuNode *node = (m_menu_root) ?
        m_menu_root->findUID(uid) : nullptr;
    if (node) node->setText(text);
}

//***************************************************************************
void Kwave::MenuManager::setItemVisible(const QString &uid, bool show)
{
    if (!m_menu_root) return;

    Kwave::MenuNode *node = m_menu_root->findUID(uid);
    if (node) {
        /* show/hide a single menu node */
        node->setVisible(show);
    } else {
        qWarning("MenuManager::setItemVisible('%s', '%d'): uid not found!",
                 DBG(uid), show);
    }
}

//***************************************************************************
void Kwave::MenuManager::setItemEnabled(const QString &uid, bool enable)
{
    if (!m_menu_root) return;

    bool found = false;
    Kwave::MenuNode *node = m_menu_root->findUID(uid);
    if (node) {
        /* enable/disable a single menu node */
        node->setEnabled(enable);
        found = true;
    } else {
        /* enable/disable a group */
        QHash<QString, Kwave::MenuGroup *> &groups = m_menu_root->groupList();
        if (groups.contains(uid)) {
            Kwave::MenuGroup *group = groups[uid];
            if (group) {
                group->setEnabled(enable);
                found = true;
            }
        }
    }

    if (!found)
        qWarning("MenuManager::setItemEnabled('%s', '%d'): uid not found!",
                 DBG(uid), enable);
}

//***************************************************************************
Kwave::MenuManager::~MenuManager()
{
    delete m_menu_root;
    m_menu_root = nullptr;
}

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

#include "moc_MenuManager.cpp"