OS X: Remove mnemonics in parentheses

In some language, mnemonics put after label text within parentheses.
e.g. "&Open" is translated to "開く(&O)" in Japanese.

OS X doesn't use mnemonics and '&' in label text is removed.
Mnemonics in parentheses (and spaces before them) also should be removed.

Change-Id: I88c0a1f60af7e148b3cf24a4e215ce807d62bce3
Reviewed-by: Tasuku Suzuki <stasuku@gmail.com>
Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@digia.com>
This commit is contained in:
Takumi Asaki 2014-04-25 17:14:31 +09:00 committed by Oswald Buddenhagen
parent bb760d9514
commit 8e3aacf61b
7 changed files with 51 additions and 25 deletions

View File

@ -7438,6 +7438,7 @@ start_lengthVariant:
underlinePositions.resize(maxUnderlines + 1);
QChar *cout = text.data() + old_offset;
QChar *cout0 = cout;
QChar *cin = cout;
int l = length;
while (l) {
@ -7448,7 +7449,18 @@ start_lengthVariant:
if (!l)
break;
if (*cin != QLatin1Char('&') && !hidemnmemonic)
underlinePositions[numUnderlines++] = cout - text.data() - old_offset;
underlinePositions[numUnderlines++] = cout - cout0;
} else if (hidemnmemonic && *cin == QLatin1Char('(') && l >= 4 &&
cin[1] == QLatin1Char('&') && cin[2] != QLatin1Char('&') &&
cin[3] == QLatin1Char(')')) {
int n = 0;
while ((cout - n) > cout0 && (cout - n - 1)->isSpace())
++n;
cout -= n;
cin += 4;
length -= n + 4;
l -= 4;
continue;
}
*cout = *cin;
++cout;

View File

@ -59,8 +59,8 @@ static NSButton *macCreateButton(const char *text, NSView *superview)
NSButton *button = [[NSButton alloc] initWithFrame:buttonFrameRect];
[button setButtonType:NSMomentaryLightButton];
[button setBezelStyle:NSRoundedBezelStyle];
[button setTitle:(NSString*)(CFStringRef)QCFString(QCoreApplication::translate("QDialogButtonBox", text)
.remove(QLatin1Char('&')))];
[button setTitle:(NSString*)(CFStringRef)QCFString(
qt_mac_removeMnemonics(QCoreApplication::translate("QDialogButtonBox", text)))];
[[button cell] setFont:[NSFont systemFontOfSize:
[NSFont systemFontSizeForControlSize:NSRegularControlSize]]];
[superview addSubview:button];

View File

@ -191,16 +191,7 @@ QT_NAMESPACE_ALIAS_OBJC_CLASS(QNSOpenSavePanelDelegate);
static QString strippedText(QString s)
{
s.remove( QString::fromLatin1("...") );
int i = 0;
while (i < s.size()) {
++i;
if (s.at(i-1) != QLatin1Char('&'))
continue;
if (i < s.size() && s.at(i) == QLatin1Char('&'))
++i;
s.remove(i-1,1);
}
return s.trimmed();
return qt_mac_removeMnemonics(s).trimmed();
}
- (NSString *)strip:(const QString &)label

View File

@ -79,8 +79,8 @@ static NSButton *macCreateButton(const char *text, NSView *superview)
NSButton *button = [[NSButton alloc] initWithFrame:buttonFrameRect];
[button setButtonType:NSMomentaryLightButton];
[button setBezelStyle:NSRoundedBezelStyle];
[button setTitle:(NSString*)(CFStringRef)QCFString(QCoreApplication::translate("QDialogButtonBox", text)
.remove(QLatin1Char('&')))];
[button setTitle:(NSString*)(CFStringRef)QCFString(
qt_mac_removeMnemonics(QCoreApplication::translate("QDialogButtonBox", text)))];
[[button cell] setFont:[NSFont systemFontOfSize:
[NSFont systemFontSizeForControlSize:NSRegularControlSize]]];
[superview addSubview:button];

View File

@ -503,6 +503,18 @@ QString qt_mac_removeMnemonics(const QString &original)
--l;
if (l == 0)
break;
} else if (original.at(currPos) == QLatin1Char('(') && l >= 4 &&
original.at(currPos + 1) == QLatin1Char('&') &&
original.at(currPos + 2) != QLatin1Char('&') &&
original.at(currPos + 3) == QLatin1Char(')')) {
/* remove mnemonics its format is "\s*(&X)" */
int n = 0;
while (finalDest > n && returnText.at(finalDest - n - 1).isSpace())
++n;
finalDest -= n;
currPos += 4;
l -= 4;
continue;
}
returnText[finalDest] = original.at(currPos);
++currPos;
@ -761,16 +773,7 @@ bool qt_mac_execute_apple_script(const QString &script, AEDesc *ret)
QString qt_mac_removeAmpersandEscapes(QString s)
{
int i = 0;
while (i < s.size()) {
++i;
if (s.at(i-1) != QLatin1Char('&'))
continue;
if (i < s.size() && s.at(i) == QLatin1Char('&'))
++i;
s.remove(i-1,1);
}
return s.trimmed();
return qt_mac_removeMnemonics(s).trimmed();
}
/*! \internal

View File

@ -494,6 +494,18 @@ static QString qt_mac_removeMnemonics(const QString &original)
--l;
if (l == 0)
break;
} else if (original.at(currPos) == QLatin1Char('(') && l >= 4 &&
original.at(currPos + 1) == QLatin1Char('&') &&
original.at(currPos + 2) != QLatin1Char('&') &&
original.at(currPos + 3) == QLatin1Char(')')) {
/* remove mnemonics its format is "\s*(&X)" */
int n = 0;
while (finalDest > n && returnText.at(finalDest - n - 1).isSpace())
++n;
finalDest -= n;
currPos += 4;
l -= 4;
continue;
}
returnText[finalDest] = original.at(currPos);
++currPos;

View File

@ -1047,6 +1047,14 @@ void tst_QMenu::QTBUG_37933_ampersands_data()
QTest::newRow("simple") << QString("Test") << QString("Test");
QTest::newRow("ampersand") << QString("&Test") << QString("Test");
QTest::newRow("double_ampersand") << QString("&Test && more") << QString("Test & more");
QTest::newRow("ampersand_in_parentheses") << QString("Test(&T) (&&) more") << QString("Test (&) more");
QTest::newRow("ampersand_in_parentheses_after_space") << QString("Test (&T)") << QString("Test");
QTest::newRow("ampersand_in_parentheses_after_spaces") << QString("Test (&T)") << QString("Test");
QTest::newRow("ampersand_in_parentheses_before_space") << QString("Test(&T) ") << QString("Test ");
QTest::newRow("only_ampersand_in_parentheses") << QString("(&T)") << QString("");
QTest::newRow("only_ampersand_in_parentheses_after_space") << QString(" (&T)") << QString("");
QTest::newRow("parentheses_after_space") << QString(" (Dummy)") << QString(" (Dummy)");
QTest::newRow("ampersand_after_space") << QString("About &Qt Project") << QString("About Qt Project");
}
void tst_qmenu_QTBUG_37933_ampersands();