1
0

DeclarativeInputEngine.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. //============================================================================
  2. // INCLUDES
  3. //============================================================================
  4. #include "DeclarativeInputEngine.h"
  5. #include <QInputMethodEvent>
  6. #include <QCoreApplication>
  7. #include <QGuiApplication>
  8. #include <QDebug>
  9. #include <QQmlEngine>
  10. #include <QJSEngine>
  11. #include <QtQml>
  12. #include <QTimer>
  13. /**
  14. * Private data class
  15. */
  16. struct DeclarativeInputEnginePrivate
  17. {
  18. DeclarativeInputEngine* _this;
  19. bool Animating;
  20. QTimer* AnimatingFinishedTimer;//< triggers position adjustment of focused QML item is covered by keybaord rectangle
  21. int InputMode;
  22. QRect KeyboardRectangle;
  23. /**
  24. * Private data constructor
  25. */
  26. DeclarativeInputEnginePrivate(DeclarativeInputEngine* _public);
  27. }; // struct DeclarativeInputEnginePrivate
  28. //==============================================================================
  29. DeclarativeInputEnginePrivate::DeclarativeInputEnginePrivate(DeclarativeInputEngine* _public)
  30. : _this(_public),
  31. Animating(false),
  32. InputMode(DeclarativeInputEngine::Latin)
  33. {
  34. }
  35. //==============================================================================
  36. DeclarativeInputEngine::DeclarativeInputEngine(QObject *parent) :
  37. QObject(parent),
  38. d(new DeclarativeInputEnginePrivate(this))
  39. {
  40. d->AnimatingFinishedTimer = new QTimer(this);
  41. d->AnimatingFinishedTimer->setSingleShot(true);
  42. d->AnimatingFinishedTimer->setInterval(100);
  43. connect(d->AnimatingFinishedTimer, SIGNAL(timeout()), this, SLOT(animatingFinished()));
  44. }
  45. //==============================================================================
  46. DeclarativeInputEngine::~DeclarativeInputEngine()
  47. {
  48. delete d;
  49. }
  50. //==============================================================================
  51. void DeclarativeInputEngine::virtualKeyCancel()
  52. {
  53. qDebug() << "VT KeyCancel";
  54. }
  55. //==============================================================================
  56. bool DeclarativeInputEngine::virtualKeyClick(Qt::Key key, const QString & text, Qt::KeyboardModifiers modifiers)
  57. {
  58. Q_UNUSED(key)
  59. Q_UNUSED(modifiers)
  60. QInputMethodEvent ev;
  61. if (text == QString("\x7F"))
  62. {
  63. //delete one char
  64. ev.setCommitString("",-1,1);
  65. } else
  66. {
  67. //add some text
  68. ev.setCommitString(text);
  69. }
  70. QCoreApplication::sendEvent(QGuiApplication::focusObject(),&ev);
  71. return true;
  72. }
  73. //==============================================================================
  74. void DeclarativeInputEngine::sendKeyToFocusItem(const QString& text)
  75. {
  76. qDebug() << "CDeclarativeInputEngine::sendKeyToFocusItem " << text;
  77. QInputMethodEvent ev;
  78. if (text == QString("\x7F"))
  79. {
  80. // Backspace
  81. // ev.setCommitString("",-1,1);
  82. QKeyEvent ev(QEvent::KeyPress, Qt::Key_Backspace, Qt::NoModifier);
  83. QCoreApplication::sendEvent(QGuiApplication::focusObject(),&ev);
  84. } else if (text == "\n") {
  85. // Qt::Key_Enter
  86. QKeyEvent evEnterP(QEvent::KeyPress, Qt::Key_Enter, Qt::NoModifier);
  87. QCoreApplication::sendEvent(QGuiApplication::focusObject(),&evEnterP);
  88. } else if (text == "\uf060") {
  89. // move left
  90. QKeyEvent ev(QEvent::KeyPress, Qt::Key_Left, Qt::NoModifier);
  91. QCoreApplication::sendEvent(QGuiApplication::focusObject(),&ev);
  92. } else if (text == "\uf061") {
  93. // move right
  94. QKeyEvent ev(QEvent::KeyPress, Qt::Key_Right, Qt::NoModifier);
  95. QCoreApplication::sendEvent(QGuiApplication::focusObject(),&ev);
  96. } else {
  97. //add some text
  98. ev.setCommitString(text);
  99. QCoreApplication::sendEvent(QGuiApplication::focusObject(),&ev);
  100. }
  101. }
  102. //==============================================================================
  103. bool DeclarativeInputEngine::virtualKeyPress(Qt::Key key, const QString & text, Qt::KeyboardModifiers modifiers, bool repeat)
  104. {
  105. Q_UNUSED(key)
  106. Q_UNUSED(text)
  107. Q_UNUSED(modifiers)
  108. Q_UNUSED(repeat)
  109. // not implemented yet
  110. return true;
  111. }
  112. //==============================================================================
  113. bool DeclarativeInputEngine::virtualKeyRelease(Qt::Key key, const QString & text, Qt::KeyboardModifiers modifiers)
  114. {
  115. Q_UNUSED(key)
  116. Q_UNUSED(text)
  117. Q_UNUSED(modifiers)
  118. // not implemented yet
  119. return true;
  120. }
  121. //==============================================================================
  122. QRect DeclarativeInputEngine::keyboardRectangle() const
  123. {
  124. return d->KeyboardRectangle;
  125. }
  126. //==============================================================================
  127. void DeclarativeInputEngine::setKeyboardRectangle(const QRect& Rect)
  128. {
  129. setAnimating(true);
  130. d->AnimatingFinishedTimer->start(100);
  131. d->KeyboardRectangle = Rect;
  132. emit keyboardRectangleChanged();
  133. }
  134. //==============================================================================
  135. bool DeclarativeInputEngine::isAnimating() const
  136. {
  137. return d->Animating;
  138. }
  139. //==============================================================================
  140. void DeclarativeInputEngine::setAnimating(bool Animating)
  141. {
  142. if (d->Animating != Animating)
  143. {
  144. d->Animating = Animating;
  145. emit animatingChanged();
  146. }
  147. }
  148. //==============================================================================
  149. void DeclarativeInputEngine::animatingFinished()
  150. {
  151. setAnimating(false);
  152. }
  153. //==============================================================================
  154. int DeclarativeInputEngine::inputMode() const
  155. {
  156. return d->InputMode;
  157. }
  158. //==============================================================================
  159. void DeclarativeInputEngine::setInputMode(int Mode)
  160. {
  161. qDebug() << "CDeclarativeInputEngine::setInputMode " << Mode;
  162. if (Mode != d->InputMode)
  163. {
  164. d->InputMode = Mode;
  165. emit inputModeChanged();
  166. }
  167. }
  168. //------------------------------------------------------------------------------
  169. // EOF DeclarativeInputEngine.cpp