Cocoa: Implement QPlatformInputContext::locale().

Listen to NSTextInputContextKeyboardSelectionDidChangeNotification.

Task-number: QTBUG-48772
Change-Id: Icea4ef61fd184edbe65a7f195318832a22c312ab
Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@theqtcompany.com>
bb10
Liang Qi 2015-10-22 12:54:18 +02:00
parent 129d268246
commit 7df107f026
4 changed files with 40 additions and 0 deletions

View File

@ -35,6 +35,7 @@
#define QCOCOAINPUTCONTEXT_H
#include <qpa/qplatforminputcontext.h>
#include <QtCore/QLocale>
#include <QtCore/QPointer>
QT_BEGIN_NAMESPACE
@ -50,12 +51,16 @@ public:
void reset() Q_DECL_OVERRIDE;
QLocale locale() const Q_DECL_OVERRIDE { return m_locale; }
void updateLocale();
private Q_SLOTS:
void connectSignals();
void focusObjectChanged(QObject *focusObject);
private:
QPointer<QWindow> mWindow;
QLocale m_locale;
};
QT_END_NAMESPACE

View File

@ -36,6 +36,8 @@
#include "qcocoanativeinterface.h"
#include "qcocoawindow.h"
#include <Carbon/Carbon.h>
#include <QtCore/QRect>
#include <QtGui/QGuiApplication>
#include <QtGui/QWindow>
@ -76,6 +78,7 @@ QCocoaInputContext::QCocoaInputContext()
, mWindow(QGuiApplication::focusWindow())
{
QMetaObject::invokeMethod(this, "connectSignals", Qt::QueuedConnection);
updateLocale();
}
QCocoaInputContext::~QCocoaInputContext()
@ -116,4 +119,20 @@ void QCocoaInputContext::focusObjectChanged(QObject *focusObject)
mWindow = QGuiApplication::focusWindow();
}
void QCocoaInputContext::updateLocale()
{
TISInputSourceRef source = TISCopyCurrentKeyboardInputSource();
CFArrayRef languages = (CFArrayRef) TISGetInputSourceProperty(source, kTISPropertyInputSourceLanguages);
if (CFArrayGetCount(languages) > 0) {
CFStringRef langRef = (CFStringRef)CFArrayGetValueAtIndex(languages, 0);
QString name = QCFString::toQString(langRef);
QLocale locale(name);
if (m_locale != locale) {
m_locale = locale;
emitLocaleChanged();
}
CFRelease(langRef);
}
}
QT_END_NAMESPACE

View File

@ -95,6 +95,7 @@ Q_FORWARD_DECLARE_OBJC_CLASS(QT_MANGLE_NAMESPACE(QNSViewMouseMoveHelper));
- (void)notifyWindowStateChanged:(Qt::WindowState)newState;
- (void)windowNotification : (NSNotification *) windowNotification;
- (void)notifyWindowWillZoom:(BOOL)willZoom;
- (void)textInputContextKeyboardSelectionDidChangeNotification : (NSNotification *) textInputContextKeyboardSelectionDidChangeNotification;
- (void)viewDidHide;
- (void)viewDidUnhide;

View File

@ -41,6 +41,7 @@
#include "qcocoahelpers.h"
#include "qmultitouch_mac_p.h"
#include "qcocoadrag.h"
#include "qcocoainputcontext.h"
#include <qpa/qplatformintegration.h>
#include <qpa/qwindowsysteminterface.h>
@ -215,6 +216,11 @@ static NSString *_q_NSWindowDidChangeOcclusionStateNotification = nil;
name:NSViewFrameDidChangeNotification
object:self];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(textInputContextKeyboardSelectionDidChangeNotification:)
name:NSTextInputContextKeyboardSelectionDidChangeNotification
object:nil];
return self;
}
@ -464,6 +470,15 @@ QT_WARNING_POP
}
}
- (void)textInputContextKeyboardSelectionDidChangeNotification : (NSNotification *) textInputContextKeyboardSelectionDidChangeNotification
{
Q_UNUSED(textInputContextKeyboardSelectionDidChangeNotification)
if (([NSApp keyWindow] == [self window]) && [[self window] firstResponder] == self) {
QCocoaInputContext *ic = qobject_cast<QCocoaInputContext *>(QCocoaIntegration::instance()->inputContext());
ic->updateLocale();
}
}
- (void)notifyWindowWillZoom:(BOOL)willZoom
{
Qt::WindowState newState = willZoom ? Qt::WindowMaximized : Qt::WindowNoState;