1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868
|
/* * This file is part of Maliit framework *
*
* Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
* Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
* Copyright (C) 2013 Jolla Ltd.
*
* All rights reserved.
*
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation
* and appearing in the file LICENSE.LGPL included in the packaging
* of this file.
*/
#include "minputcontext.h"
#include <QGuiApplication>
#include <QScreen>
#include <QKeyEvent>
#include <QTextFormat>
#include <QDebug>
#include <QLoggingCategory>
#include <QByteArray>
#include <QRectF>
#include <QScreen>
#include <QLocale>
#include <QWindow>
#include <QSharedDataPointer>
#include <QQuickItem>
// includes needed to load input context plugin
#include <qpa/qplatforminputcontextfactory_p.h>
#include <qpa/qplatforminputcontextplugin_p.h>
#include <qpa/qplatforminputcontext.h>
#include "private/qfactoryloader_p.h"
Q_GLOBAL_STATIC_WITH_ARGS(QFactoryLoader, loader,
(QPlatformInputContextFactoryInterface_iid, QLatin1String("/platforminputcontexts"),
Qt::CaseInsensitive))
namespace
{
const int SoftwareInputPanelHideTimer = 100;
const char * const InputContextName = "MInputContext";
QLoggingCategory lcMaliit("org.maliit.inputContext", QtWarningMsg);
int orientationAngle(Qt::ScreenOrientation orientation)
{
QScreen *screen = qGuiApp->primaryScreen();
return screen->angleBetween(orientation, screen->primaryOrientation());
}
}
MInputContext::MInputContext()
: imServer(0),
active(false),
inputPanelState(InputPanelHidden),
preeditCursorPos(-1),
redirectKeys(false),
currentFocusAcceptsInput(false),
composeInputContext(qLoadPlugin<QPlatformInputContext, QPlatformInputContextPlugin>
(loader(), "compose", QStringList()))
{
QByteArray debugEnvVar = qgetenv("MALIIT_DEBUG");
if (!debugEnvVar.isEmpty() && debugEnvVar != "0") {
lcMaliit.setEnabled(QtDebugMsg, true);
}
qCDebug(lcMaliit) << "Creating Maliit input context";
QSharedPointer<Maliit::InputContext::DBus::Address> address;
QByteArray maliitServerAddress = qgetenv("MALIIT_SERVER_ADDRESS");
if (!maliitServerAddress.isEmpty()) {
address.reset(new Maliit::InputContext::DBus::FixedAddress(maliitServerAddress.constData()));
} else {
address.reset(new Maliit::InputContext::DBus::DynamicAddress);
}
imServer = new DBusServerConnection(address);
sipHideTimer.setSingleShot(true);
sipHideTimer.setInterval(SoftwareInputPanelHideTimer);
connect(&sipHideTimer, SIGNAL(timeout()), SLOT(sendHideInputMethod()));
connectInputMethodServer();
}
MInputContext::~MInputContext()
{
delete imServer;
delete composeInputContext;
}
void MInputContext::connectInputMethodServer()
{
connect(imServer, SIGNAL(connected()), this, SLOT(onDBusConnection()));
connect(imServer, SIGNAL(disconnected()), this, SLOT(onDBusDisconnection()));
// Hook up incoming communication from input method server
connect(imServer, SIGNAL(activationLostEvent()), this, SLOT(activationLostEvent()));
connect(imServer, SIGNAL(imInitiatedHide()), this, SLOT(imInitiatedHide()));
connect(imServer, SIGNAL(commitString(QString,int,int,int)),
this, SLOT(commitString(QString,int,int,int)));
connect(imServer, SIGNAL(updatePreedit(QString,QList<Maliit::PreeditTextFormat>,int,int,int)),
this, SLOT(updatePreedit(QString,QList<Maliit::PreeditTextFormat>,int,int,int)));
connect(imServer, SIGNAL(keyEvent(int,int,int,QString,bool,int,Maliit::EventRequestType)),
this, SLOT(keyEvent(int,int,int,QString,bool,int,Maliit::EventRequestType)));
connect(imServer, SIGNAL(updateInputMethodArea(QRect)),
this, SLOT(updateInputMethodArea(QRect)));
connect(imServer, SIGNAL(setGlobalCorrectionEnabled(bool)),
this, SLOT(setGlobalCorrectionEnabled(bool)));
connect(imServer, SIGNAL(getPreeditRectangle(QRect&,bool&)),
this, SLOT(getPreeditRectangle(QRect&,bool&)));
connect(imServer, SIGNAL(invokeAction(QString,QKeySequence)), this, SLOT(onInvokeAction(QString,QKeySequence)));
connect(imServer, SIGNAL(setRedirectKeys(bool)), this, SLOT(setRedirectKeys(bool)));
connect(imServer, SIGNAL(setDetectableAutoRepeat(bool)),
this, SLOT(setDetectableAutoRepeat(bool)));
connect(imServer, SIGNAL(setSelection(int,int)),
this, SLOT(setSelection(int,int)));
connect(imServer, SIGNAL(getSelection(QString&,bool&)),
this, SLOT(getSelection(QString&, bool&)));
connect(imServer, SIGNAL(setLanguage(QString)),
this, SLOT(setLanguage(QString)));
}
bool MInputContext::isValid() const
{
return true;
}
void MInputContext::setLanguage(const QString &language)
{
QLocale newLocale(language);
Qt::LayoutDirection oldDirection = inputDirection();
if (newLocale != inputLocale) {
inputLocale = newLocale;
emitLocaleChanged();
}
Qt::LayoutDirection newDirection = inputDirection();
if (newDirection != oldDirection) {
emitInputDirectionChanged(newDirection);
}
}
void MInputContext::reset()
{
if (composeInputContext) composeInputContext->reset();
qCDebug(lcMaliit) << InputContextName << "in" << Q_FUNC_INFO;
const bool hadPreedit = !preedit.isEmpty();
preedit.clear();
preeditCursorPos = -1;
// reset input method server, preedit requires synchronization.
// rationale: input method might be autocommitting existing preedit without
// user interaction.
imServer->reset(hadPreedit);
}
void MInputContext::commit()
{
qCDebug(lcMaliit) << InputContextName << "in" << Q_FUNC_INFO;
const bool hadPreedit = !preedit.isEmpty();
if (hadPreedit) {
QList<QInputMethodEvent::Attribute> attributes;
if (preeditCursorPos >= 0) {
bool valid = false;
int start = cursorStartPosition(&valid);
if (valid) {
attributes << QInputMethodEvent::Attribute(QInputMethodEvent::Selection,
start + preeditCursorPos, 0, QVariant());
}
}
QInputMethodEvent event("", attributes);
event.setCommitString(preedit);
if (qGuiApp->focusObject()) {
QGuiApplication::sendEvent(qGuiApp->focusObject(), &event);
}
preedit.clear();
preeditCursorPos = -1;
Q_EMIT preeditChanged();
}
imServer->reset(hadPreedit);
}
void MInputContext::invokeAction(QInputMethod::Action action, int x)
{
qCDebug(lcMaliit) << InputContextName << "in" << Q_FUNC_INFO;
if (!inputMethodAccepted())
return;
if (action == QInputMethod::Click) {
if (x < 0 || x >= preedit.length()) {
reset();
return;
}
// To preserve the wire protocol, the "x" argument gets
// transferred in widget state instead of being an extra
// argument to mouseClickedOnPreedit().
QMap<QString, QVariant> stateInformation = getStateInformation();
stateInformation["preeditClickPos"] = x;
imServer->updateWidgetInformation(stateInformation, false);
// FIXME: proper positions and preeditClickPos
QRect preeditRect;
QPoint globalPos;
imServer->mouseClickedOnPreedit(globalPos, preeditRect);
} else {
QPlatformInputContext::invokeAction(action, x);
}
}
void MInputContext::update(Qt::InputMethodQueries queries)
{
if (composeInputContext) composeInputContext->update(queries);
qCDebug(lcMaliit) << InputContextName << "in" << Q_FUNC_INFO;
Q_UNUSED(queries) // fetching everything
if (queries & Qt::ImPlatformData) {
updateInputMethodExtensions();
}
bool effectiveFocusChange = false;
if (queries & Qt::ImEnabled) {
bool newAcceptance = inputMethodAccepted();
if (newAcceptance && !active) {
setFocusObject(QGuiApplication::focusObject());
return;
}
if (newAcceptance != currentFocusAcceptsInput) {
currentFocusAcceptsInput = newAcceptance;
effectiveFocusChange = true;
}
}
// get the state information of currently focused widget, and pass it to input method server
QMap<QString, QVariant> stateInformation = getStateInformation();
imServer->updateWidgetInformation(stateInformation, effectiveFocusChange);
}
void MInputContext::updateServerOrientation(Qt::ScreenOrientation orientation)
{
if (active) {
imServer->appOrientationChanged(orientationAngle(orientation));
}
}
void MInputContext::setFocusObject(QObject *focused)
{
if (composeInputContext) composeInputContext->setFocusObject(focused);
qCDebug(lcMaliit) << InputContextName << "in" << Q_FUNC_INFO << focused;
updateInputMethodExtensions();
QWindow *newFocusWindow = qGuiApp->focusWindow();
if (newFocusWindow != window.data()) {
if (window) {
disconnect(window.data(), SIGNAL(contentOrientationChanged(Qt::ScreenOrientation)),
this, SLOT(updateServerOrientation(Qt::ScreenOrientation)));
}
window = newFocusWindow;
if (window) {
connect(window.data(), SIGNAL(contentOrientationChanged(Qt::ScreenOrientation)),
this, SLOT(updateServerOrientation(Qt::ScreenOrientation)));
updateServerOrientation(window->contentOrientation());
}
}
bool oldAcceptInput = currentFocusAcceptsInput;
currentFocusAcceptsInput = inputMethodAccepted();
if (!active && currentFocusAcceptsInput) {
imServer->activateContext();
active = true;
}
if (newFocusWindow && currentFocusAcceptsInput) {
updateServerOrientation(newFocusWindow->contentOrientation());
}
if (active && (currentFocusAcceptsInput || oldAcceptInput)) {
const QMap<QString, QVariant> stateInformation = getStateInformation();
imServer->updateWidgetInformation(stateInformation, true);
}
if (inputPanelState == InputPanelShowPending && currentFocusAcceptsInput) {
sipHideTimer.stop();
imServer->showInputMethod();
inputPanelState = InputPanelShown;
}
}
QString MInputContext::preeditString()
{
return preedit;
}
bool MInputContext::filterEvent(const QEvent *event)
{
bool eaten = false;
bool eatenByCompose = false;
if (!redirectKeys && composeInputContext) {
eatenByCompose = composeInputContext->filterEvent(event);
}
switch (event->type()) {
case QEvent::KeyPress:
case QEvent::KeyRelease:
if (!inputMethodAccepted()) {
break;
}
if (redirectKeys) {
const QKeyEvent *key = static_cast<const QKeyEvent *>(event);
imServer->processKeyEvent(key->type(), static_cast<Qt::Key>(key->key()),
key->modifiers(), key->text(), key->isAutoRepeat(),
key->count(), key->nativeScanCode(),
key->nativeModifiers(), 0 /* currentKeyEventTime */);
eaten = true;
}
break;
default:
break;
}
return eaten || eatenByCompose;
}
QRectF MInputContext::keyboardRect() const
{
return keyboardRectangle;
}
bool MInputContext::isAnimating() const
{
return false; // don't know here when input method server is actually doing transitions
}
void MInputContext::showInputPanel()
{
qCDebug(lcMaliit) << Q_FUNC_INFO;
if (inputMethodAccepted()) {
sipHideTimer.stop();
}
if (!active || !inputMethodAccepted()) {
// in case SIP request comes without a properly focused widget, we
// don't ask input method server to be shown. It's done when the next widget
// is focused, so the widget state information can be set.
inputPanelState = InputPanelShowPending;
} else {
// note: could do this also if panel was hidden
imServer->showInputMethod();
inputPanelState = InputPanelShown;
}
}
void MInputContext::hideInputPanel()
{
qCDebug(lcMaliit) << Q_FUNC_INFO;
sipHideTimer.start();
}
bool MInputContext::isInputPanelVisible() const
{
return !keyboardRectangle.isEmpty();
}
QLocale MInputContext::locale() const
{
return inputLocale;
}
Qt::LayoutDirection MInputContext::inputDirection() const
{
return inputLocale.textDirection();
}
void MInputContext::sendHideInputMethod()
{
imServer->hideInputMethod();
inputPanelState = InputPanelHidden;
}
void MInputContext::activationLostEvent()
{
qCDebug(lcMaliit) << InputContextName << "in" << Q_FUNC_INFO;
// This method is called when activation was gracefully lost.
// There is similar cleaning up done in onDBusDisconnection.
active = false;
inputPanelState = InputPanelHidden;
updateInputMethodArea(QRect());
}
void MInputContext::imInitiatedHide()
{
qCDebug(lcMaliit) << InputContextName << "in" << Q_FUNC_INFO;
inputPanelState = InputPanelHidden;
// remove focus on QtQuick2
QQuickItem *inputItem = qobject_cast<QQuickItem*>(QGuiApplication::focusObject());
if (inputItem && inputItem->flags().testFlag(QQuickItem::ItemAcceptsInputMethod)) {
inputItem->setFocus(false);
}
}
void MInputContext::commitString(const QString &string, int replacementStart,
int replacementLength, int cursorPos)
{
qCDebug(lcMaliit) << InputContextName << "in" << Q_FUNC_INFO;
if (imServer->pendingResets()) {
return;
}
bool hadPreedit = !preedit.isEmpty();
preedit.clear();
preeditCursorPos = -1;
int start = -1;
if (cursorPos >= 0) {
bool valid = false;
int currentStart = cursorStartPosition(&valid);
if (valid) {
start = cursorPos + currentStart + replacementStart;
}
}
if (start >= 0) {
QList<QInputMethodEvent::Attribute> attributes;
attributes << QInputMethodEvent::Attribute(QInputMethodEvent::Selection, start, 0, QVariant());
QInputMethodEvent event("", attributes);
event.setCommitString(string, replacementStart, replacementLength);
if (qGuiApp->focusObject()) {
QGuiApplication::sendEvent(qGuiApp->focusObject(), &event);
}
} else {
QInputMethodEvent event;
event.setCommitString(string, replacementStart, replacementLength);
if (qGuiApp->focusObject()) {
QGuiApplication::sendEvent(qGuiApp->focusObject(), &event);
}
}
if (hadPreedit) {
Q_EMIT preeditChanged();
}
}
void MInputContext::updatePreedit(const QString &string,
const QList<Maliit::PreeditTextFormat> &preeditFormats,
int replacementStart, int replacementLength, int cursorPos)
{
qCDebug(lcMaliit) << InputContextName << "in" << Q_FUNC_INFO << "preedit:" << string
<< ", replacementStart:" << replacementStart
<< ", replacementLength:" << replacementLength
<< ", cursorPos:" << cursorPos;
if (imServer->pendingResets()) {
return;
}
updatePreeditInternally(string, preeditFormats, replacementStart, replacementLength, cursorPos);
}
void MInputContext::updatePreeditInternally(const QString &string,
const QList<Maliit::PreeditTextFormat> &preeditFormats,
int replacementStart, int replacementLength, int cursorPos)
{
preedit = string;
preeditCursorPos = cursorPos;
QList<QInputMethodEvent::Attribute> attributes;
Q_FOREACH (const Maliit::PreeditTextFormat &preeditFormat, preeditFormats) {
QTextCharFormat format;
// update style mode
switch (preeditFormat.preeditFace) {
case Maliit::PreeditNoCandidates:
format.setUnderlineStyle(QTextCharFormat::SpellCheckUnderline);
format.setUnderlineColor(Qt::red);
break;
case Maliit::PreeditUnconvertible:
format.setForeground(QBrush(QColor(128, 128, 128)));
break;
case Maliit::PreeditActive:
format.setForeground(QBrush(QColor(153, 50, 204)));
format.setFontWeight(QFont::Bold);
break;
case Maliit::PreeditKeyPress:
case Maliit::PreeditDefault:
format.setUnderlineStyle(QTextCharFormat::SingleUnderline);
format.setUnderlineColor(QColor(0, 0, 0));
break;
}
attributes << QInputMethodEvent::Attribute(QInputMethodEvent::TextFormat,
preeditFormat.start,
preeditFormat.length, format);
}
if (cursorPos >= 0) {
attributes << QInputMethodEvent::Attribute(QInputMethodEvent::Cursor, cursorPos, 1, QVariant());
}
QInputMethodEvent event(string, attributes);
if (replacementStart || replacementLength) {
event.setCommitString("", replacementStart, replacementLength);
}
if (qGuiApp->focusObject()) {
QGuiApplication::sendEvent(qGuiApp->focusObject(), &event);
} else {
qCDebug(lcMaliit) << Q_FUNC_INFO;
qCWarning(lcMaliit) << "No focused object, cannot update preedit."
<< "Wrong reset/preedit behaviour in active input method plugin?";
}
Q_EMIT preeditChanged();
}
void MInputContext::keyEvent(int type, int key, int modifiers, const QString &text,
bool autoRepeat, int count,
Maliit::EventRequestType requestType)
{
qCDebug(lcMaliit) << InputContextName << "in" << Q_FUNC_INFO;
if (qGuiApp->focusWindow() != 0 && requestType != Maliit::EventRequestSignalOnly) {
QEvent::Type eventType = static_cast<QEvent::Type>(type);
QKeyEvent event(eventType, key, static_cast<Qt::KeyboardModifiers>(modifiers), text, autoRepeat, count);
// need window instead of focus item so e.g. QQuickItem::keyPressEvent() gets properly called
QGuiApplication::sendEvent(qGuiApp->focusWindow(), &event);
}
}
void MInputContext::updateInputMethodArea(const QRect &rect)
{
bool wasVisible = isInputPanelVisible();
if (rect != keyboardRectangle) {
keyboardRectangle = rect;
emitKeyboardRectChanged();
if (wasVisible != isInputPanelVisible()) {
emitInputPanelVisibleChanged();
}
}
}
void MInputContext::setGlobalCorrectionEnabled(bool enabled)
{
Q_UNUSED(enabled)
// not handling preedit injections, ignored
}
void MInputContext::getPreeditRectangle(QRect &rectangle, bool &valid) const
{
// not supported
rectangle = QRect();
valid = false;
return;
}
void MInputContext::onInvokeAction(const QString &action, const QKeySequence &sequence)
{
qCDebug(lcMaliit) << InputContextName << Q_FUNC_INFO << "action" << action;
// NOTE: currently not trying to trigger action directly
static const Qt::KeyboardModifiers AllModifiers = Qt::ShiftModifier | Qt::ControlModifier | Qt::AltModifier
| Qt::MetaModifier | Qt::KeypadModifier;
for (int i = 0; i < sequence.count(); i++) {
const int key = sequence[i] & ~AllModifiers;
const int modifiers = sequence[i] & AllModifiers;
QString text("");
if (modifiers == Qt::NoModifier || modifiers == Qt::ShiftModifier) {
text = QString(key);
}
keyEvent(QEvent::KeyPress, key, modifiers, text, false, 1);
keyEvent(QEvent::KeyRelease, key, modifiers, text, false, 1);
}
}
void MInputContext::onDBusDisconnection()
{
qCDebug(lcMaliit) << Q_FUNC_INFO;
active = false;
redirectKeys = false;
updateInputMethodArea(QRect());
}
void MInputContext::onDBusConnection()
{
qCDebug(lcMaliit) << Q_FUNC_INFO;
// using one attribute extension for everything
imServer->registerAttributeExtension(0, QString());
// Force activation, since setFocusObject may have been called after
// onDBusDisconnection set active to false or before the dbus connection.
active = false;
if (inputMethodAccepted()) {
setFocusObject(QGuiApplication::focusObject());
if (inputPanelState != InputPanelHidden) {
imServer->showInputMethod();
inputPanelState = InputPanelShown;
}
}
}
void MInputContext::notifyOrientationAboutToChange(MInputContext::OrientationAngle angle)
{
// can get called from signal so cannot be sure we are really currently active
if (active) {
imServer->appOrientationAboutToChange(static_cast<int>(angle));
}
}
void MInputContext::notifyOrientationChanged(MInputContext::OrientationAngle angle)
{
// can get called from signal so cannot be sure we are really currently active
if (active) {
imServer->appOrientationChanged(static_cast<int>(angle));
}
}
Maliit::TextContentType MInputContext::contentType(Qt::InputMethodHints hints) const
{
Maliit::TextContentType type = Maliit::FreeTextContentType;
hints &= Qt::ImhExclusiveInputMask;
if (hints == Qt::ImhFormattedNumbersOnly || hints == Qt::ImhDigitsOnly) {
type = Maliit::NumberContentType;
} else if (hints == Qt::ImhDialableCharactersOnly) {
type = Maliit::PhoneNumberContentType;
} else if (hints == Qt::ImhEmailCharactersOnly) {
type = Maliit::EmailContentType;
} else if (hints == Qt::ImhUrlCharactersOnly) {
type = Maliit::UrlContentType;
}
return type;
}
void MInputContext::setRedirectKeys(bool enabled)
{
redirectKeys = enabled;
}
void MInputContext::setDetectableAutoRepeat(bool enabled)
{
Q_UNUSED(enabled);
qCDebug(lcMaliit) << "Detectable autorepeat not supported.";
}
QMap<QString, QVariant> MInputContext::getStateInformation() const
{
QMap<QString, QVariant> stateInformation;
stateInformation["focusState"] = inputMethodAccepted();
if (!inputMethodAccepted() || !qGuiApp->focusObject()) {
return stateInformation;
}
QInputMethodQueryEvent query(Qt::ImQueryAll);
QGuiApplication::sendEvent(qGuiApp->focusObject(), &query);
QVariant queryResult;
queryResult = query.value(Qt::ImSurroundingText);
if (queryResult.isValid()) {
stateInformation["surroundingText"] = queryResult.toString();
}
queryResult = query.value(Qt::ImCursorPosition);
if (queryResult.isValid()) {
stateInformation["cursorPosition"] = queryResult.toInt();
}
queryResult = query.value(Qt::ImAnchorPosition);
if (queryResult.isValid()) {
stateInformation["anchorPosition"] = queryResult.toInt();
}
queryResult = query.value(Qt::ImHints);
auto hints = queryResult.value<std::underlying_type<Qt::InputMethodHint>::type>();
// content type value
// Deprecated, replaced by just transmitting all hints (see below):
// FIXME: Remove once MAbstractInputMethod API for this got deprecated/removed.
stateInformation["contentType"] = contentType(static_cast<Qt::InputMethodHint>(hints));
stateInformation["autocapitalizationEnabled"] = !(hints & Qt::ImhNoAutoUppercase);
stateInformation["hiddenText"] = static_cast<bool>(hints & Qt::ImhHiddenText);
stateInformation["predictionEnabled"] = !(hints & Qt::ImhNoPredictiveText);
stateInformation["maliit-inputmethod-hints"] = hints;
queryResult = query.value(Qt::ImEnterKeyType);
stateInformation["enterKeyType"] = queryResult.value<std::underlying_type<Qt::EnterKeyType>::type>();
// is text selected
queryResult = query.value(Qt::ImCurrentSelection);
if (queryResult.isValid()) {
stateInformation["hasSelection"] = !(queryResult.toString().isEmpty());
}
QWindow *window = qGuiApp->focusWindow();
if (window) {
stateInformation["winId"] = static_cast<qulonglong>(window->winId());
}
queryResult = query.value(Qt::ImCursorRectangle);
if (queryResult.isValid()) {
QRect rect = queryResult.toRect();
rect = qGuiApp->inputMethod()->inputItemTransform().mapRect(rect);
if (window) {
stateInformation["cursorRectangle"] = QRect(window->mapToGlobal(rect.topLeft()), rect.size());
}
}
stateInformation["toolbarId"] = 0; // Global extension id. And bad state parameter name for it.
return stateInformation;
}
void MInputContext::setSelection(int start, int length)
{
if (!inputMethodAccepted())
return;
QList<QInputMethodEvent::Attribute> attributes;
attributes << QInputMethodEvent::Attribute(QInputMethodEvent::Selection, start,
length, QVariant());
QInputMethodEvent event("", attributes);
QGuiApplication::sendEvent(qGuiApp->focusObject(), &event);
}
void MInputContext::getSelection(QString &selection, bool &valid) const
{
selection.clear();
QString selectionText;
valid = false;
if (!inputMethodAccepted()) {
return;
}
QInputMethodQueryEvent query(Qt::ImCurrentSelection);
QGuiApplication::sendEvent(qGuiApp->focusObject(), &query);
QVariant queryResult = query.value(Qt::ImCurrentSelection);
valid = queryResult.isValid();
selectionText = queryResult.toString();
selection = selectionText;
}
int MInputContext::cursorStartPosition(bool *valid)
{
int start = -1;
if (valid) {
*valid = false;
}
if (!inputMethodAccepted()) {
return start;
}
QInputMethodQueryEvent query(Qt::ImCursorPosition | Qt::ImAnchorPosition);
QGuiApplication::sendEvent(qGuiApp->focusObject(), &query);
// obtain the cursor absolute position
QVariant queryResult = query.value(Qt::ImCursorPosition);
if (queryResult.isValid()) {
int absCursorPos = queryResult.toInt();
// Fetch anchor position too but don't require it.
queryResult = query.value(Qt::ImAnchorPosition);
int absAnchorPos = queryResult.isValid() ? queryResult.toInt() : absCursorPos;
// In case of selection, base cursorPos on start of it.
start = qMin<int>(absCursorPos, absAnchorPos);
*valid = true;
}
return start;
}
void MInputContext::updateInputMethodExtensions()
{
if (!inputMethodAccepted()) {
return;
}
if (!qGuiApp->focusObject()) {
return;
}
qCDebug(lcMaliit) << InputContextName << Q_FUNC_INFO;
QVariantMap extensions = qGuiApp->focusObject()->property("__inputMethodExtensions").toMap();
QVariant value;
value = extensions.value("enterKeyIconSource");
imServer->setExtendedAttribute(0, "/keys", "actionKey", "icon", QVariant(value.toUrl().toString()));
value = extensions.value("enterKeyText");
imServer->setExtendedAttribute(0, "/keys", "actionKey", "label", QVariant(value.toString()));
value = extensions.value("enterKeyEnabled");
imServer->setExtendedAttribute(0, "/keys", "actionKey", "enabled", value.isValid() ? value.toBool() : true);
value = extensions.value("enterKeyHighlighted");
imServer->setExtendedAttribute(0, "/keys", "actionKey", "highlighted", value.isValid() ? value.toBool() : false);
}
|