Add methods to convert lists of QKeySequence to/from strings
QKeySequence provides conversion to and from strings. But a similar convenience was missing for QList<QKeySequence>. It would come in handy when you want for instance to save/restore the shortcuts of a QAction. Change-Id: I9e4f2001c58a595392a5019a57c564992c39bf88 Reviewed-by: Olivier Goffart <ogoffart@woboq.com> Reviewed-by: Friedemann Kleint <Friedemann.Kleint@digia.com> Reviewed-by: David Faure (KDE) <faure@kde.org>bb10
parent
6d0a685fc6
commit
2d1f3d7c51
|
|
@ -1681,6 +1681,47 @@ QKeySequence QKeySequence::fromString(const QString &str, SequenceFormat format)
|
|||
return QKeySequence(str, format);
|
||||
}
|
||||
|
||||
/*!
|
||||
\since 5.1
|
||||
|
||||
Return a list of QKeySequence from the string \a str based on \a format.
|
||||
|
||||
\sa fromString()
|
||||
\sa listToString()
|
||||
*/
|
||||
QList<QKeySequence> QKeySequence::listFromString(const QString &str, SequenceFormat format)
|
||||
{
|
||||
QList<QKeySequence> result;
|
||||
|
||||
QStringList strings = str.split(QLatin1String("; "));
|
||||
foreach (const QString &string, strings) {
|
||||
result << fromString(string, format);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/*!
|
||||
\since 5.1
|
||||
|
||||
Return a string representation of \a list based on \a format.
|
||||
|
||||
\sa toString()
|
||||
\sa listFromString()
|
||||
*/
|
||||
QString QKeySequence::listToString(const QList<QKeySequence> &list, SequenceFormat format)
|
||||
{
|
||||
QString result;
|
||||
|
||||
foreach (const QKeySequence &sequence, list) {
|
||||
result += sequence.toString(format);
|
||||
result += QLatin1String("; ");
|
||||
}
|
||||
result.truncate(result.length() - 2);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/*****************************************************************************
|
||||
QKeySequence stream functions
|
||||
*****************************************************************************/
|
||||
|
|
|
|||
|
|
@ -163,6 +163,9 @@ public:
|
|||
QString toString(SequenceFormat format = PortableText) const;
|
||||
static QKeySequence fromString(const QString &str, SequenceFormat format = PortableText);
|
||||
|
||||
static QList<QKeySequence> listFromString(const QString &str, SequenceFormat format = PortableText);
|
||||
static QString listToString(const QList<QKeySequence> &list, SequenceFormat format = PortableText);
|
||||
|
||||
SequenceMatch matches(const QKeySequence &seq) const;
|
||||
static QKeySequence mnemonic(const QString &text);
|
||||
static QList<QKeySequence> keyBindings(StandardKey key);
|
||||
|
|
|
|||
|
|
@ -133,6 +133,10 @@ private slots:
|
|||
void parseString();
|
||||
void fromString_data();
|
||||
void fromString();
|
||||
void listToString_data();
|
||||
void listToString();
|
||||
void listFromString_data();
|
||||
void listFromString();
|
||||
#ifdef QT_BUILD_INTERNAL
|
||||
void ensureSorted();
|
||||
#endif
|
||||
|
|
@ -631,6 +635,104 @@ void tst_QKeySequence::fromString()
|
|||
QCOMPARE(ks4, ks1);
|
||||
}
|
||||
|
||||
void tst_QKeySequence::listToString_data()
|
||||
{
|
||||
QTest::addColumn<QString>("strSequences");
|
||||
QTest::addColumn<QList<QKeySequence> >("sequences");
|
||||
|
||||
QList<QKeySequence> sequences;
|
||||
|
||||
sequences << QKeySequence(Qt::CTRL + Qt::Key_Left)
|
||||
<< QKeySequence(Qt::META + Qt::Key_A);
|
||||
QTest::newRow("Ctrl+Left; Meta+A") << "Ctrl+Left; Meta+A" << sequences;
|
||||
|
||||
sequences.clear();
|
||||
sequences << QKeySequence(Qt::CTRL + Qt::Key_Semicolon)
|
||||
<< QKeySequence(Qt::META + Qt::Key_A);
|
||||
QTest::newRow("Ctrl+;; Meta+A") << "Ctrl+;; Meta+A" << sequences;
|
||||
|
||||
sequences.clear();
|
||||
sequences << QKeySequence(Qt::Key_Semicolon)
|
||||
<< QKeySequence(Qt::META + Qt::Key_A);
|
||||
QTest::newRow(";; Meta+A") << ";; Meta+A" << sequences;
|
||||
|
||||
sequences.clear();
|
||||
sequences << QKeySequence(Qt::CTRL + Qt::Key_Left)
|
||||
<< QKeySequence(Qt::META + Qt::Key_Semicolon);
|
||||
QTest::newRow("Ctrl+Left; Meta+;") << "Ctrl+Left; Meta+;" << sequences;
|
||||
|
||||
sequences.clear();
|
||||
sequences << QKeySequence(Qt::CTRL + Qt::Key_Left)
|
||||
<< QKeySequence();
|
||||
QTest::newRow("Ctrl+Left; ") << "Ctrl+Left; " << sequences;
|
||||
|
||||
sequences.clear();
|
||||
sequences << QKeySequence(Qt::CTRL + Qt::Key_Left)
|
||||
<< QKeySequence()
|
||||
<< QKeySequence(Qt::META + Qt::Key_A);
|
||||
QTest::newRow("Ctrl+Left; ; Meta+A") << "Ctrl+Left; ; Meta+A" << sequences;
|
||||
}
|
||||
|
||||
void tst_QKeySequence::listToString()
|
||||
{
|
||||
QFETCH(QList<QKeySequence>, sequences);
|
||||
QFETCH(QString, strSequences);
|
||||
|
||||
QCOMPARE(QKeySequence::listToString(sequences), strSequences);
|
||||
}
|
||||
|
||||
void tst_QKeySequence::listFromString_data()
|
||||
{
|
||||
QTest::addColumn<QString>("strSequences");
|
||||
QTest::addColumn<QList<QKeySequence> >("sequences");
|
||||
|
||||
QList<QKeySequence> sequences;
|
||||
|
||||
sequences << QKeySequence(Qt::CTRL + Qt::Key_Left)
|
||||
<< QKeySequence(Qt::META + Qt::Key_A);
|
||||
QTest::newRow("Ctrl+Left; Meta+A") << "Ctrl+Left; Meta+A" << sequences;
|
||||
|
||||
sequences.clear();
|
||||
sequences << QKeySequence(Qt::CTRL + Qt::Key_Semicolon)
|
||||
<< QKeySequence(Qt::META + Qt::Key_A);
|
||||
QTest::newRow("Ctrl+;; Meta+A") << "Ctrl+;; Meta+A" << sequences;
|
||||
|
||||
sequences.clear();
|
||||
sequences << QKeySequence(Qt::Key_Semicolon)
|
||||
<< QKeySequence(Qt::META + Qt::Key_A);
|
||||
QTest::newRow(";; Meta+A") << ";; Meta+A" << sequences;
|
||||
|
||||
sequences.clear();
|
||||
sequences << QKeySequence(Qt::CTRL + Qt::Key_Left)
|
||||
<< QKeySequence(Qt::META + Qt::Key_Semicolon);
|
||||
QTest::newRow("Ctrl+Left; Meta+;") << "Ctrl+Left; Meta+;" << sequences;
|
||||
|
||||
sequences.clear();
|
||||
sequences << QKeySequence(Qt::CTRL + Qt::Key_Left)
|
||||
<< QKeySequence();
|
||||
QTest::newRow("Ctrl+Left; ") << "Ctrl+Left; " << sequences;
|
||||
|
||||
sequences.clear();
|
||||
sequences << QKeySequence(Qt::CTRL + Qt::Key_Left)
|
||||
<< QKeySequence()
|
||||
<< QKeySequence(Qt::META + Qt::Key_A);
|
||||
QTest::newRow("Ctrl+Left; ; Meta+A") << "Ctrl+Left; ; Meta+A" << sequences;
|
||||
|
||||
sequences.clear();
|
||||
sequences << QKeySequence(Qt::CTRL + Qt::Key_Left)
|
||||
<< QKeySequence(Qt::Key_unknown)
|
||||
<< QKeySequence(Qt::META + Qt::Key_A);
|
||||
QTest::newRow("Ctrl+Left; 4+3=2; Meta+A") << "Ctrl+Left; 4+3=2; Meta+A" << sequences;
|
||||
}
|
||||
|
||||
void tst_QKeySequence::listFromString()
|
||||
{
|
||||
QFETCH(QList<QKeySequence>, sequences);
|
||||
QFETCH(QString, strSequences);
|
||||
|
||||
QCOMPARE(QKeySequence::listFromString(strSequences), sequences);
|
||||
}
|
||||
|
||||
#if !defined (Q_OS_MAC) && !defined (Q_OS_WINCE)
|
||||
void tst_QKeySequence::translated_data()
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in New Issue