1
0

DeclarativeInputEngine.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 == "\u2190") {
  89. // move left
  90. QKeyEvent ev(QEvent::KeyPress, Qt::Key_Left, Qt::NoModifier);
  91. QCoreApplication::sendEvent(QGuiApplication::focusObject(),&ev);
  92. } else if (text == "\u2192") {
  93. // move right
  94. QKeyEvent ev(QEvent::KeyPress, Qt::Key_Right, Qt::NoModifier);
  95. QCoreApplication::sendEvent(QGuiApplication::focusObject(),&ev);
  96. } else if (text == "\u2191") {
  97. // move up
  98. QKeyEvent ev(QEvent::KeyPress, Qt::Key_Up, Qt::NoModifier);
  99. QCoreApplication::sendEvent(QGuiApplication::focusObject(),&ev);
  100. } else if (text == "\u2193") {
  101. // move down
  102. QKeyEvent ev(QEvent::KeyPress, Qt::Key_Down, Qt::NoModifier);
  103. QCoreApplication::sendEvent(QGuiApplication::focusObject(),&ev);
  104. } else if (text == "\u21a6") {
  105. // tabulator
  106. QKeyEvent ev(QEvent::KeyPress, Qt::Key_Tab, Qt::NoModifier);
  107. QCoreApplication::sendEvent(QGuiApplication::focusObject(),&ev);
  108. } else if (text == "\u21a4") {
  109. // backtab
  110. QKeyEvent ev(QEvent::KeyPress, Qt::Key_Backtab, Qt::NoModifier);
  111. QCoreApplication::sendEvent(QGuiApplication::focusObject(),&ev);
  112. } else {
  113. //add some text
  114. ev.setCommitString(text);
  115. QCoreApplication::sendEvent(QGuiApplication::focusObject(),&ev);
  116. }
  117. }
  118. //==============================================================================
  119. bool DeclarativeInputEngine::virtualKeyPress(Qt::Key key, const QString & text, Qt::KeyboardModifiers modifiers, bool repeat)
  120. {
  121. Q_UNUSED(key)
  122. Q_UNUSED(text)
  123. Q_UNUSED(modifiers)
  124. Q_UNUSED(repeat)
  125. // not implemented yet
  126. return true;
  127. }
  128. //==============================================================================
  129. bool DeclarativeInputEngine::virtualKeyRelease(Qt::Key key, const QString & text, Qt::KeyboardModifiers modifiers)
  130. {
  131. Q_UNUSED(key)
  132. Q_UNUSED(text)
  133. Q_UNUSED(modifiers)
  134. // not implemented yet
  135. return true;
  136. }
  137. //==============================================================================
  138. QRect DeclarativeInputEngine::keyboardRectangle() const
  139. {
  140. return d->KeyboardRectangle;
  141. }
  142. //==============================================================================
  143. void DeclarativeInputEngine::setKeyboardRectangle(const QRect& Rect)
  144. {
  145. setAnimating(true);
  146. d->AnimatingFinishedTimer->start(100);
  147. d->KeyboardRectangle = Rect;
  148. emit keyboardRectangleChanged();
  149. }
  150. //==============================================================================
  151. bool DeclarativeInputEngine::isAnimating() const
  152. {
  153. return d->Animating;
  154. }
  155. //==============================================================================
  156. void DeclarativeInputEngine::setAnimating(bool Animating)
  157. {
  158. if (d->Animating != Animating)
  159. {
  160. d->Animating = Animating;
  161. emit animatingChanged();
  162. }
  163. }
  164. //==============================================================================
  165. void DeclarativeInputEngine::animatingFinished()
  166. {
  167. setAnimating(false);
  168. }
  169. //==============================================================================
  170. int DeclarativeInputEngine::inputMode() const
  171. {
  172. return d->InputMode;
  173. }
  174. //==============================================================================
  175. void DeclarativeInputEngine::setInputMode(int Mode)
  176. {
  177. qDebug() << "CDeclarativeInputEngine::setInputMode " << Mode;
  178. if (Mode != d->InputMode)
  179. {
  180. d->InputMode = Mode;
  181. emit inputModeChanged();
  182. }
  183. }
  184. //------------------------------------------------------------------------------
  185. // EOF DeclarativeInputEngine.cpp