File indexing completed on 2024-04-28 08:44:01

0001 /*
0002     SPDX-FileCopyrightText: 2010 Pascal Fleury <fleury@users.sourceforge.net>
0003 
0004 SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005 */
0006 
0007 #include "jogshuttleconfig.h"
0008 
0009 #include <cstdio>
0010 #include <sstream>
0011 #include <string>
0012 #include <vector>
0013 
0014 #include <cstdlib>
0015 
0016 using std::string;
0017 using std::stringstream;
0018 using std::vector;
0019 
0020 // these 2 functions will convert the action maps to and from a string representation not unlike this:
0021 // button1=rewind_one_frame;button2=forward_one_frame;button15=play
0022 
0023 static const QChar DELIMITER = ';';
0024 static const QChar KEY_VALUE_SEP = '=';
0025 static const QLatin1String BUTTON_PREFIX("button");
0026 
0027 QStringList JogShuttleConfig::actionMap(const QString &actionsConfig)
0028 {
0029     QStringList actionMap;
0030     const QStringList mappings = actionsConfig.split(DELIMITER);
0031 
0032     for (const QString &mapping : mappings) {
0033         QStringList parts = mapping.split(KEY_VALUE_SEP);
0034         if (parts.size() != 2) {
0035             fprintf(stderr, "Invalid button configuration: %s", mapping.toLatin1().constData());
0036             continue;
0037         }
0038         // skip the 'button' prefix
0039 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
0040         int button_id = parts[0].midRef(BUTTON_PREFIX.size()).toInt();
0041 #else
0042         int button_id = QStringView(parts[0]).mid(BUTTON_PREFIX.size()).toInt();
0043 #endif
0044         // fprintf(stderr, " - Handling map key='%s' (ID=%d), value='%s'\n", parts[0].data().toLatin1(), button_id, parts[1].data().toLatin1()); // DBG
0045         while (actionMap.size() <= button_id) {
0046             actionMap << QString();
0047         }
0048         actionMap[button_id] = parts[1];
0049     }
0050 
0051     // for (int i = 0; i < actionMap.size(); ++i) fprintf(stderr, "button #%d -> action '%s'\n", i, actionMap[i].data().toLatin1());  //DBG
0052     return actionMap;
0053 }
0054 
0055 QString JogShuttleConfig::actionMap(const QStringList &actionMap)
0056 {
0057     QStringList mappings;
0058     for (int i = 0; i < actionMap.size(); ++i) {
0059         if (actionMap[i].isEmpty()) {
0060             continue;
0061         }
0062         mappings << QStringLiteral("%1%2%3%4").arg(BUTTON_PREFIX).arg(i).arg(KEY_VALUE_SEP).arg(actionMap[i]);
0063     }
0064 
0065     return mappings.join(DELIMITER);
0066 }