keyboard: port to QRegularExpression

This commit is contained in:
Adriaan de Groot 2023-09-05 15:18:02 +02:00
parent 22bd80daac
commit 427311f2c3
3 changed files with 50 additions and 29 deletions

View File

@ -23,8 +23,10 @@
#include "utils/Variant.h" #include "utils/Variant.h"
#include <QApplication> #include <QApplication>
#include <QFile>
#include <QGuiApplication> #include <QGuiApplication>
#include <QProcess> #include <QProcess>
#include <QRegularExpression>
#include <QTimer> #include <QTimer>
#include <QDBusConnection> #include <QDBusConnection>
@ -113,7 +115,7 @@ xkbmap_query_grp_option()
} }
//it's either in the end of line or before the other option so \s or , //it's either in the end of line or before the other option so \s or ,
int lastIndex = outputLine.indexOf( QRegExp( "[\\s,]" ), index ); int lastIndex = outputLine.indexOf( QRegularExpression( "[\\s,]" ), index );
return outputLine.mid( index, lastIndex - index ); return outputLine.mid( index, lastIndex - index );
} }
@ -349,7 +351,9 @@ Config::getCurrentKeyboardLayoutXkb( QString& currentLayout, QString& currentVar
symbols = true; symbols = true;
} }
else if ( !line.trimmed().startsWith( "xkb_geometry" ) ) else if ( !line.trimmed().startsWith( "xkb_geometry" ) )
{
continue; continue;
}
int firstQuote = line.indexOf( '"' ); int firstQuote = line.indexOf( '"' );
int lastQuote = line.lastIndexOf( '"' ); int lastQuote = line.lastIndexOf( '"' );

View File

@ -19,6 +19,10 @@
#include "utils/Logger.h" #include "utils/Logger.h"
#include <QByteArray>
#include <QFile>
#include <QRegularExpression>
#ifdef Q_OS_FREEBSD #ifdef Q_OS_FREEBSD
static const char XKB_FILE[] = "/usr/local/share/X11/xkb/rules/base.lst"; static const char XKB_FILE[] = "/usr/local/share/X11/xkb/rules/base.lst";
#else #else
@ -75,16 +79,22 @@ parseKeyboardModels( const char* filepath )
break; break;
} }
// here we are in the model section, otherwise we would continue or break // Here we are in the model section, otherwise we would continue or break.
QRegExp rx; // Sample model lines:
rx.setPattern( "^\\s+(\\S+)\\s+(\\w.*)\n$" ); //
// ! model
// pc86 Generic 86-key PC
// pc101 Generic 101-key PC
//
QRegularExpression rx( "^\\s+(\\S+)\\s+(\\w.*)\n$" );
QRegularExpressionMatch m;
// insert into the model map // insert into the model map
if ( rx.indexIn( line ) != -1 ) if ( QString( line ).indexOf( rx, 0, &m ) != -1 )
{ {
QString modelDesc = rx.cap( 2 ); const QString modelDescription = m.captured( 2 );
QString model = rx.cap( 1 ); const QString model = m.captured( 1 );
models.insert( modelDesc, model ); models.insert( modelDescription, model );
} }
} }
@ -119,16 +129,21 @@ parseKeyboardLayouts( const char* filepath )
break; break;
} }
QRegExp rx; // Sample layout lines:
rx.setPattern( "^\\s+(\\S+)\\s+(\\w.*)\n$" ); //
// ! layout
// us English (US)
// af Afghani
QRegularExpression rx( "^\\s+(\\S+)\\s+(\\w.*)\n$" );
QRegularExpressionMatch m;
// insert into the layout map // insert into the layout map
if ( rx.indexIn( line ) != -1 ) if ( QString( line ).indexOf( rx, 0, &m ) != -1 )
{ {
KeyboardGlobal::KeyboardInfo info; KeyboardGlobal::KeyboardInfo info;
info.description = rx.cap( 2 ); info.description = m.captured( 2 );
info.variants.insert( QObject::tr( "Default" ), "" ); info.variants.insert( QObject::tr( "Default" ), "" );
layouts.insert( rx.cap( 1 ), info ); layouts.insert( m.captured( 1 ), info );
} }
} }
@ -148,25 +163,35 @@ parseKeyboardLayouts( const char* filepath )
break; break;
} }
QRegExp rx; // Sample variant lines:
rx.setPattern( "^\\s+(\\S+)\\s+(\\S+): (\\w.*)\n$" ); //
// ! variant
// chr us: Cherokee
// haw us: Hawaiian
// ps af: Pashto
// uz af: Uzbek (Afghanistan)
QRegularExpression rx( "^\\s+(\\S+)\\s+(\\S+): (\\w.*)\n$" );
QRegularExpressionMatch m;
// insert into the variants multimap, if the pattern matches // insert into the variants multimap, if the pattern matches
if ( rx.indexIn( line ) != -1 ) if ( QString( line ).indexOf( rx, 0, &m ) != -1 )
{ {
if ( layouts.find( rx.cap( 2 ) ) != layouts.end() ) const QString variantKey = m.captured( 1 );
const QString baseLayout = m.captured( 2 );
const QString description = m.captured( 3 );
if ( layouts.find( baseLayout ) != layouts.end() )
{ {
// in this case we found an entry in the multimap, and add the values to the multimap // in this case we found an entry in the multimap, and add the values to the multimap
layouts.find( rx.cap( 2 ) ).value().variants.insert( rx.cap( 3 ), rx.cap( 1 ) ); layouts.find( baseLayout ).value().variants.insert( description, variantKey );
} }
else else
{ {
// create a new map in the multimap - the value was not found. // create a new map in the multimap - the value was not found.
KeyboardGlobal::KeyboardInfo info; KeyboardGlobal::KeyboardInfo info;
info.description = rx.cap( 2 ); info.description = baseLayout;
info.variants.insert( QObject::tr( "Default" ), "" ); info.variants.insert( QObject::tr( "Default" ), "" );
info.variants.insert( rx.cap( 3 ), rx.cap( 1 ) ); info.variants.insert( description, variantKey );
layouts.insert( rx.cap( 2 ), info ); layouts.insert( baseLayout, info );
} }
} }
} }

View File

@ -16,16 +16,8 @@
#ifndef KEYBOARDGLOBAL_H #ifndef KEYBOARDGLOBAL_H
#define KEYBOARDGLOBAL_H #define KEYBOARDGLOBAL_H
#include <QDebug>
#include <QDir>
#include <QFile>
#include <QHash>
#include <QList>
#include <QMap> #include <QMap>
#include <QRegExp>
#include <QString> #include <QString>
#include <QStringList>
#include <QTextStream>
class KeyboardGlobal class KeyboardGlobal
{ {