Introduce function for standard button texts to QPlatformTheme.

Change-Id: I91eec04a95b5047d893490a70152237b2991f662
Reviewed-by: Shawn Rutledge <shawn.rutledge@digia.com>
This commit is contained in:
Friedemann Kleint 2014-01-08 15:12:43 +01:00 committed by The Qt Project
parent d5912b2a47
commit ab2c90cc52
13 changed files with 133 additions and 133 deletions

View File

@ -51,6 +51,7 @@
#include <private/qiconloader_p.h> #include <private/qiconloader_p.h>
#include <private/qguiapplication_p.h> #include <private/qguiapplication_p.h>
#include <qpa/qplatformintegration.h> #include <qpa/qplatformintegration.h>
#include <qpa/qplatformdialoghelper.h>
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
@ -627,6 +628,63 @@ QList<QKeySequence> QPlatformTheme::keyBindings(QKeySequence::StandardKey key) c
return list; return list;
} }
/*!
Returns the text of a standard \a button.
\since 5.3
\sa QMessageDialogOptions::StandardButton
*/
QString QPlatformTheme::standardButtonText(int button) const
{
return QPlatformTheme::defaultStandardButtonText(button);
}
QString QPlatformTheme::defaultStandardButtonText(int button)
{
switch (button) {
case QMessageDialogOptions::Ok:
return QCoreApplication::translate("QPlatformTheme", "OK");
case QMessageDialogOptions::Save:
return QCoreApplication::translate("QPlatformTheme", "Save");
case QMessageDialogOptions::SaveAll:
return QCoreApplication::translate("QPlatformTheme", "Save All");
case QMessageDialogOptions::Open:
return QCoreApplication::translate("QPlatformTheme", "Open");
case QMessageDialogOptions::Yes:
return QCoreApplication::translate("QPlatformTheme", "&Yes");
case QMessageDialogOptions::YesToAll:
return QCoreApplication::translate("QPlatformTheme", "Yes to &All");
case QMessageDialogOptions::No:
return QCoreApplication::translate("QPlatformTheme", "&No");
case QMessageDialogOptions::NoToAll:
return QCoreApplication::translate("QPlatformTheme", "N&o to All");
case QMessageDialogOptions::Abort:
return QCoreApplication::translate("QPlatformTheme", "Abort");
case QMessageDialogOptions::Retry:
return QCoreApplication::translate("QPlatformTheme", "Retry");
case QMessageDialogOptions::Ignore:
return QCoreApplication::translate("QPlatformTheme", "Ignore");
case QMessageDialogOptions::Close:
return QCoreApplication::translate("QPlatformTheme", "Close");
case QMessageDialogOptions::Cancel:
return QCoreApplication::translate("QPlatformTheme", "Cancel");
case QMessageDialogOptions::Discard:
return QCoreApplication::translate("QPlatformTheme", "Discard");
case QMessageDialogOptions::Help:
return QCoreApplication::translate("QPlatformTheme", "Help");
case QMessageDialogOptions::Apply:
return QCoreApplication::translate("QPlatformTheme", "Apply");
case QMessageDialogOptions::Reset:
return QCoreApplication::translate("QPlatformTheme", "Reset");
case QMessageDialogOptions::RestoreDefaults:
return QCoreApplication::translate("QPlatformTheme", "Restore Defaults");
default:
break;
}
return QString();
}
unsigned QPlatformThemePrivate::currentKeyPlatforms() unsigned QPlatformThemePrivate::currentKeyPlatforms()
{ {
const uint keyboardScheme = QGuiApplicationPrivate::platformTheme()->themeHint(QPlatformTheme::KeyboardScheme).toInt(); const uint keyboardScheme = QGuiApplicationPrivate::platformTheme()->themeHint(QPlatformTheme::KeyboardScheme).toInt();

View File

@ -296,7 +296,10 @@ public:
virtual QList<QKeySequence> keyBindings(QKeySequence::StandardKey key) const; virtual QList<QKeySequence> keyBindings(QKeySequence::StandardKey key) const;
virtual QString standardButtonText(int button) const;
static QVariant defaultThemeHint(ThemeHint hint); static QVariant defaultThemeHint(ThemeHint hint);
static QString defaultStandardButtonText(int button);
protected: protected:
explicit QPlatformTheme(QPlatformThemePrivate *priv); explicit QPlatformTheme(QPlatformThemePrivate *priv);

View File

@ -57,6 +57,7 @@
#include <private/qguiapplication_p.h> #include <private/qguiapplication_p.h>
#include <qpa/qplatformintegration.h> #include <qpa/qplatformintegration.h>
#include <qpa/qplatformservices.h> #include <qpa/qplatformservices.h>
#include <qpa/qplatformdialoghelper.h>
#include <algorithm> #include <algorithm>
@ -533,6 +534,25 @@ const QFont *QGnomeTheme::font(Font type) const
} }
} }
QString QGnomeTheme::standardButtonText(int button) const
{
switch (button) {
case QMessageDialogOptions::Ok:
return QCoreApplication::translate("QGnomeTheme", "&OK");
case QMessageDialogOptions::Save:
return QCoreApplication::translate("QGnomeTheme", "&Save");
case QMessageDialogOptions::Cancel:
return QCoreApplication::translate("QGnomeTheme", "&Cancel");
case QMessageDialogOptions::QMessageDialogOptions::Close:
return QCoreApplication::translate("QGnomeTheme", "&Close");
case QMessageDialogOptions::Discard:
return QCoreApplication::translate("QGnomeTheme", "Close without Saving");
default:
break;
}
return QPlatformTheme::standardButtonText(button);
}
/*! /*!
\brief Creates a UNIX theme according to the detected desktop environment. \brief Creates a UNIX theme according to the detected desktop environment.
*/ */

View File

@ -109,6 +109,7 @@ public:
QGnomeTheme(); QGnomeTheme();
virtual QVariant themeHint(ThemeHint hint) const; virtual QVariant themeHint(ThemeHint hint) const;
virtual const QFont *font(Font type) const; virtual const QFont *font(Font type) const;
QString standardButtonText(int button) const Q_DECL_OVERRIDE;
static const char *name; static const char *name;
}; };

View File

@ -43,6 +43,8 @@
#include <QtWidgets/QStyle> #include <QtWidgets/QStyle>
#include "qandroidplatformdialoghelpers.h" #include "qandroidplatformdialoghelpers.h"
#include "androidjnimain.h" #include "androidjnimain.h"
#include <private/qguiapplication_p.h>
#include <qpa/qplatformtheme.h>
namespace QtAndroidDialogHelpers { namespace QtAndroidDialogHelpers {
static jclass g_messageDialogHelperClass = 0; static jclass g_messageDialogHelperClass = 0;
@ -61,49 +63,6 @@ void QAndroidPlatformMessageDialogHelper::exec()
m_loop.exec(); m_loop.exec();
} }
static QString standardButtonText(int sbutton)
{
switch (sbutton) {
case QMessageDialogOptions::Ok:
return QAndroidPlatformMessageDialogHelper::tr("OK");
case QMessageDialogOptions::Save:
return QAndroidPlatformMessageDialogHelper::tr("Save");
case QMessageDialogOptions::Open:
return QAndroidPlatformMessageDialogHelper::tr("Open");
case QMessageDialogOptions::Cancel:
return QAndroidPlatformMessageDialogHelper::tr("Cancel");
case QMessageDialogOptions::Close:
return QAndroidPlatformMessageDialogHelper::tr("Close");
case QMessageDialogOptions::Apply:
return QAndroidPlatformMessageDialogHelper::tr("Apply");
case QMessageDialogOptions::Reset:
return QAndroidPlatformMessageDialogHelper::tr("Reset");
case QMessageDialogOptions::Help:
return QAndroidPlatformMessageDialogHelper::tr("Help");
case QMessageDialogOptions::Discard:
return QAndroidPlatformMessageDialogHelper::tr("Discard");
case QMessageDialogOptions::Yes:
return QAndroidPlatformMessageDialogHelper::tr("Yes");
case QMessageDialogOptions::YesToAll:
return QAndroidPlatformMessageDialogHelper::tr("Yes to All");
case QMessageDialogOptions::No:
return QAndroidPlatformMessageDialogHelper::tr("No");
case QMessageDialogOptions::NoToAll:
return QAndroidPlatformMessageDialogHelper::tr("No to All");
case QMessageDialogOptions::SaveAll:
return QAndroidPlatformMessageDialogHelper::tr("Save All");
case QMessageDialogOptions::Abort:
return QAndroidPlatformMessageDialogHelper::tr("Abort");
case QMessageDialogOptions::Retry:
return QAndroidPlatformMessageDialogHelper::tr("Retry");
case QMessageDialogOptions::Ignore:
return QAndroidPlatformMessageDialogHelper::tr("Ignore");
case QMessageDialogOptions::RestoreDefaults:
return QAndroidPlatformMessageDialogHelper::tr("Restore Defaults");
} // switch
return QString();
}
bool QAndroidPlatformMessageDialogHelper::show(Qt::WindowFlags windowFlags bool QAndroidPlatformMessageDialogHelper::show(Qt::WindowFlags windowFlags
, Qt::WindowModality windowModality , Qt::WindowModality windowModality
, QWindow *parent) , QWindow *parent)
@ -134,8 +93,10 @@ bool QAndroidPlatformMessageDialogHelper::show(Qt::WindowFlags windowFlags
m_javaMessageDialog.callMethod<void>("setDetailedText", "(Ljava/lang/String;)V", QJNIObjectPrivate::fromString(str).object()); m_javaMessageDialog.callMethod<void>("setDetailedText", "(Ljava/lang/String;)V", QJNIObjectPrivate::fromString(str).object());
for (int i = QMessageDialogOptions::FirstButton; i < QMessageDialogOptions::LastButton; i<<=1) { for (int i = QMessageDialogOptions::FirstButton; i < QMessageDialogOptions::LastButton; i<<=1) {
if ( opt->standardButtons() & i ) if ( opt->standardButtons() & i ) {
m_javaMessageDialog.callMethod<void>("addButton", "(ILjava/lang/String;)V", i, QJNIObjectPrivate::fromString(standardButtonText(i)).object()); const QString text = QGuiApplicationPrivate::platformTheme()->standardButtonText(i);
m_javaMessageDialog.callMethod<void>("addButton", "(ILjava/lang/String;)V", i, QJNIObjectPrivate::fromString(text).object());
}
} }
m_javaMessageDialog.callMethod<void>("show", "(J)V", jlong(static_cast<QObject*>(this))); m_javaMessageDialog.callMethod<void>("show", "(J)V", jlong(static_cast<QObject*>(this)));

View File

@ -46,6 +46,7 @@
#include "qandroidplatformdialoghelpers.h" #include "qandroidplatformdialoghelpers.h"
#include <QVariant> #include <QVariant>
#include <QFileInfo> #include <QFileInfo>
#include <QCoreApplication>
#include <qandroidplatformintegration.h> #include <qandroidplatformintegration.h>
QAndroidPlatformTheme::QAndroidPlatformTheme(QAndroidPlatformNativeInterface *androidPlatformNativeInterface) QAndroidPlatformTheme::QAndroidPlatformTheme(QAndroidPlatformNativeInterface *androidPlatformNativeInterface)
@ -152,6 +153,21 @@ QVariant QAndroidPlatformTheme::themeHint(ThemeHint hint) const
} }
} }
QString QAndroidPlatformTheme::standardButtonText(int button) const
{
switch (button) {
case QMessageDialogOptions::Yes:
return QCoreApplication::translate("QAndroidPlatformTheme", "Yes");
case QMessageDialogOptions::YesToAll:
return QCoreApplication::translate("QAndroidPlatformTheme", "Yes to All");
case QMessageDialogOptions::No:
return QCoreApplication::translate("QAndroidPlatformTheme", "No");
case QMessageDialogOptions::NoToAll:
return QCoreApplication::translate("QAndroidPlatformTheme", "No to All");
}
return QPlatformTheme::standardButtonText(button);
}
bool QAndroidPlatformTheme::usePlatformNativeDialog(QPlatformTheme::DialogType type) const bool QAndroidPlatformTheme::usePlatformNativeDialog(QPlatformTheme::DialogType type) const
{ {
if (type == MessageDialog) if (type == MessageDialog)

View File

@ -54,6 +54,7 @@ public:
virtual const QPalette *palette(Palette type = SystemPalette) const; virtual const QPalette *palette(Palette type = SystemPalette) const;
virtual const QFont *font(Font type = SystemFont) const; virtual const QFont *font(Font type = SystemFont) const;
virtual QVariant themeHint(ThemeHint hint) const; virtual QVariant themeHint(ThemeHint hint) const;
QString standardButtonText(int button) const Q_DECL_OVERRIDE;
virtual bool usePlatformNativeDialog(DialogType type) const; virtual bool usePlatformNativeDialog(DialogType type) const;
virtual QPlatformDialogHelper *createPlatformDialogHelper(DialogType type) const; virtual QPlatformDialogHelper *createPlatformDialogHelper(DialogType type) const;

View File

@ -93,4 +93,9 @@ QPlatformMenuItem::MenuRole detectMenuRole(const QString &caption)
return QPlatformMenuItem::NoRole; return QPlatformMenuItem::NoRole;
} }
QString msgDialogButtonDiscard()
{
return QCoreApplication::translate("QCocoaTheme", "Don't Save");
}
QT_END_NAMESPACE QT_END_NAMESPACE

View File

@ -53,6 +53,8 @@ QString qt_mac_applicationmenu_string(int type);
QPlatformMenuItem::MenuRole detectMenuRole(const QString &caption); QPlatformMenuItem::MenuRole detectMenuRole(const QString &caption);
QString msgDialogButtonDiscard();
QT_END_NAMESPACE QT_END_NAMESPACE
#endif // MESSAGES_H #endif // MESSAGES_H

View File

@ -73,6 +73,7 @@ public:
QPlatformTheme::IconOptions options = 0) const; QPlatformTheme::IconOptions options = 0) const;
QVariant themeHint(ThemeHint hint) const; QVariant themeHint(ThemeHint hint) const;
QString standardButtonText(int button) const Q_DECL_OVERRIDE;
static const char *name; static const char *name;

View File

@ -42,6 +42,7 @@
#import <Cocoa/Cocoa.h> #import <Cocoa/Cocoa.h>
#include "qcocoatheme.h" #include "qcocoatheme.h"
#include "messages.h"
#include <QtCore/QVariant> #include <QtCore/QVariant>
@ -292,6 +293,11 @@ QVariant QCocoaTheme::themeHint(ThemeHint hint) const
return QPlatformTheme::themeHint(hint); return QPlatformTheme::themeHint(hint);
} }
QString QCocoaTheme::standardButtonText(int button) const
{
return button == QMessageDialogOptions::Discard ? msgDialogButtonDiscard() : QPlatformTheme::standardButtonText(button);
}
QPlatformMenuItem *QCocoaTheme::createPlatformMenuItem() const QPlatformMenuItem *QCocoaTheme::createPlatformMenuItem() const
{ {
return new QCocoaMenuItem(); return new QCocoaMenuItem();

View File

@ -46,7 +46,9 @@
#include <QtWidgets/qdialog.h> #include <QtWidgets/qdialog.h>
#include <QtWidgets/qapplication.h> #include <QtWidgets/qapplication.h>
#include <private/qwidget_p.h> #include <private/qwidget_p.h>
#include <private/qguiapplication_p.h>
#include <QtGui/qpa/qplatformdialoghelper.h> #include <QtGui/qpa/qplatformdialoghelper.h>
#include <QtGui/qpa/qplatformtheme.h>
#include <QtWidgets/qaction.h> #include <QtWidgets/qaction.h>
#include "qdialogbuttonbox.h" #include "qdialogbuttonbox.h"
@ -246,7 +248,6 @@ public:
void _q_handleButtonClicked(); void _q_handleButtonClicked();
void addButtonsToLayout(const QList<QAbstractButton *> &buttonList, bool reverse); void addButtonsToLayout(const QList<QAbstractButton *> &buttonList, bool reverse);
void retranslateStrings(); void retranslateStrings();
const char *standardButtonText(QDialogButtonBox::StandardButton sbutton) const;
}; };
QDialogButtonBoxPrivate::QDialogButtonBoxPrivate(Qt::Orientation orient) QDialogButtonBoxPrivate::QDialogButtonBoxPrivate(Qt::Orientation orient)
@ -428,7 +429,6 @@ QPushButton *QDialogButtonBoxPrivate::createButton(QDialogButtonBox::StandardBut
bool doLayout) bool doLayout)
{ {
Q_Q(QDialogButtonBox); Q_Q(QDialogButtonBox);
const char *buttonText = 0;
int icon = 0; int icon = 0;
switch (sbutton) { switch (sbutton) {
@ -477,9 +477,7 @@ QPushButton *QDialogButtonBoxPrivate::createButton(QDialogButtonBox::StandardBut
return 0; return 0;
; ;
} }
buttonText = standardButtonText(sbutton); QPushButton *button = new QPushButton(QGuiApplicationPrivate::platformTheme()->standardButtonText(sbutton), q);
QPushButton *button = new QPushButton(QDialogButtonBox::tr(buttonText), q);
QStyle *style = q->style(); QStyle *style = q->style();
if (style->styleHint(QStyle::SH_DialogButtonBox_ButtonsHaveIcons, 0, q) && icon != 0) if (style->styleHint(QStyle::SH_DialogButtonBox_ButtonsHaveIcons, 0, q) && icon != 0)
button->setIcon(style->standardIcon(QStyle::StandardPixmap(icon), 0, q)); button->setIcon(style->standardIcon(QStyle::StandardPixmap(icon), 0, q));
@ -525,87 +523,15 @@ void QDialogButtonBoxPrivate::createStandardButtons(QDialogButtonBox::StandardBu
layoutButtons(); layoutButtons();
} }
const char *QDialogButtonBoxPrivate::standardButtonText(QDialogButtonBox::StandardButton sbutton) const
{
const char *buttonText = 0;
bool gnomeLayout = (layoutPolicy == QDialogButtonBox::GnomeLayout);
switch (sbutton) {
case QDialogButtonBox::Ok:
buttonText = gnomeLayout ? QT_TRANSLATE_NOOP("QDialogButtonBox", "&OK") : QT_TRANSLATE_NOOP("QDialogButtonBox", "OK");
break;
case QDialogButtonBox::Save:
buttonText = gnomeLayout ? QT_TRANSLATE_NOOP("QDialogButtonBox", "&Save") : QT_TRANSLATE_NOOP("QDialogButtonBox", "Save");
break;
case QDialogButtonBox::Open:
buttonText = QT_TRANSLATE_NOOP("QDialogButtonBox", "Open");
break;
case QDialogButtonBox::Cancel:
buttonText = gnomeLayout ? QT_TRANSLATE_NOOP("QDialogButtonBox", "&Cancel") : QT_TRANSLATE_NOOP("QDialogButtonBox", "Cancel");
break;
case QDialogButtonBox::Close:
buttonText = gnomeLayout ? QT_TRANSLATE_NOOP("QDialogButtonBox", "&Close") : QT_TRANSLATE_NOOP("QDialogButtonBox", "Close");
break;
case QDialogButtonBox::Apply:
buttonText = QT_TRANSLATE_NOOP("QDialogButtonBox", "Apply");
break;
case QDialogButtonBox::Reset:
buttonText = QT_TRANSLATE_NOOP("QDialogButtonBox", "Reset");
break;
case QDialogButtonBox::Help:
buttonText = QT_TRANSLATE_NOOP("QDialogButtonBox", "Help");
break;
case QDialogButtonBox::Discard:
if (layoutPolicy == QDialogButtonBox::MacLayout)
buttonText = QT_TRANSLATE_NOOP("QDialogButtonBox", "Don't Save");
else if (layoutPolicy == QDialogButtonBox::GnomeLayout)
buttonText = QT_TRANSLATE_NOOP("QDialogButtonBox", "Close without Saving");
else
buttonText = QT_TRANSLATE_NOOP("QDialogButtonBox", "Discard");
break;
case QDialogButtonBox::Yes:
buttonText = QT_TRANSLATE_NOOP("QDialogButtonBox", "&Yes");
break;
case QDialogButtonBox::YesToAll:
buttonText = QT_TRANSLATE_NOOP("QDialogButtonBox", "Yes to &All");
break;
case QDialogButtonBox::No:
buttonText = QT_TRANSLATE_NOOP("QDialogButtonBox", "&No");
break;
case QDialogButtonBox::NoToAll:
buttonText = QT_TRANSLATE_NOOP("QDialogButtonBox", "N&o to All");
break;
case QDialogButtonBox::SaveAll:
buttonText = QT_TRANSLATE_NOOP("QDialogButtonBox", "Save All");
break;
case QDialogButtonBox::Abort:
buttonText = QT_TRANSLATE_NOOP("QDialogButtonBox", "Abort");
break;
case QDialogButtonBox::Retry:
buttonText = QT_TRANSLATE_NOOP("QDialogButtonBox", "Retry");
break;
case QDialogButtonBox::Ignore:
buttonText = QT_TRANSLATE_NOOP("QDialogButtonBox", "Ignore");
break;
case QDialogButtonBox::RestoreDefaults:
buttonText = QT_TRANSLATE_NOOP("QDialogButtonBox", "Restore Defaults");
break;
case QDialogButtonBox::NoButton:
;
} // switch
return buttonText;
}
void QDialogButtonBoxPrivate::retranslateStrings() void QDialogButtonBoxPrivate::retranslateStrings()
{ {
const char *buttonText = 0; typedef QHash<QPushButton *, QDialogButtonBox::StandardButton>::iterator Iterator;
QHash<QPushButton *, QDialogButtonBox::StandardButton>::iterator it = standardButtonHash.begin();
while (it != standardButtonHash.end()) { const Iterator end = standardButtonHash.end();
buttonText = standardButtonText(it.value()); for (Iterator it = standardButtonHash.begin(); it != end; ++it) {
if (buttonText) { const QString text = QGuiApplicationPrivate::platformTheme()->standardButtonText(it.value());
QPushButton *button = it.key(); if (!text.isEmpty())
button->setText(QDialogButtonBox::tr(buttonText)); it.key()->setText(text);
}
++it;
} }
} }

View File

@ -188,11 +188,11 @@ void tst_languageChange::retranslatability_data()
//next we fill it with data //next we fill it with data
QTest::newRow( "QInputDialog" ) QTest::newRow( "QInputDialog" )
<< int(InputDialog) << (QSet<QByteArray>() << int(InputDialog) << (QSet<QByteArray>()
<< "QDialogButtonBox::Cancel"); << "QPlatformTheme::Cancel");
QTest::newRow( "QColorDialog" ) QTest::newRow( "QColorDialog" )
<< int(ColorDialog) << (QSet<QByteArray>() << int(ColorDialog) << (QSet<QByteArray>()
<< "QDialogButtonBox::Cancel" << "QPlatformTheme::Cancel"
<< "QColorDialog::&Sat:" << "QColorDialog::&Sat:"
<< "QColorDialog::&Add to Custom Colors" << "QColorDialog::&Add to Custom Colors"
<< "QColorDialog::&Green:" << "QColorDialog::&Green:"
@ -237,8 +237,8 @@ void tst_languageChange::retranslatability_data()
<< "QFileSystemModel::Type::All other platforms" << "QFileSystemModel::Type::All other platforms"
#endif #endif
// << "QFileSystemModel::%1 KB" // << "QFileSystemModel::%1 KB"
<< "QDialogButtonBox::Cancel" << "QPlatformTheme::Cancel"
<< "QDialogButtonBox::Open" << "QPlatformTheme::Open"
<< "QFileDialog::File &name:"); << "QFileDialog::File &name:");
} }