1
0

DeclarativeInputEngine.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. }
  54. //==============================================================================
  55. bool DeclarativeInputEngine::virtualKeyClick(Qt::Key key, const QString & text, Qt::KeyboardModifiers modifiers)
  56. {
  57. Q_UNUSED(key)
  58. Q_UNUSED(modifiers)
  59. QInputMethodEvent ev;
  60. if (text == QString("\x7F"))
  61. {
  62. //delete one char
  63. ev.setCommitString("",-1,1);
  64. } else
  65. {
  66. //add some text
  67. ev.setCommitString(text);
  68. }
  69. QCoreApplication::sendEvent(QGuiApplication::focusObject(),&ev);
  70. return true;
  71. }
  72. //==============================================================================
  73. void DeclarativeInputEngine::sendKeyToFocusItem(const QString& text)
  74. {
  75. qDebug() << "CDeclarativeInputEngine::sendKeyToFocusItem " << text;
  76. QInputMethodEvent ev;
  77. if (text == QString("\x7F"))
  78. {
  79. // Backspace
  80. // ev.setCommitString("",-1,1);
  81. QKeyEvent ev(QEvent::KeyPress, Qt::Key_Backspace, Qt::NoModifier);
  82. QCoreApplication::sendEvent(QGuiApplication::focusObject(),&ev);
  83. } else if (text == "\n") {
  84. // Qt::Key_Enter
  85. QKeyEvent evEnterP(QEvent::KeyPress, Qt::Key_Enter, Qt::NoModifier);
  86. QCoreApplication::sendEvent(QGuiApplication::focusObject(),&evEnterP);
  87. } else if (text == "\uf060") {
  88. // move left
  89. QKeyEvent ev(QEvent::KeyPress, Qt::Key_Left, Qt::NoModifier);
  90. QCoreApplication::sendEvent(QGuiApplication::focusObject(),&ev);
  91. } else if (text == "\uf061") {
  92. // move right
  93. QKeyEvent ev(QEvent::KeyPress, Qt::Key_Right, Qt::NoModifier);
  94. QCoreApplication::sendEvent(QGuiApplication::focusObject(),&ev);
  95. } else {
  96. //add some text
  97. ev.setCommitString(text);
  98. QCoreApplication::sendEvent(QGuiApplication::focusObject(),&ev);
  99. }
  100. }
  101. //==============================================================================
  102. bool DeclarativeInputEngine::virtualKeyPress(Qt::Key key, const QString & text, Qt::KeyboardModifiers modifiers, bool repeat)
  103. {
  104. Q_UNUSED(key)
  105. Q_UNUSED(text)
  106. Q_UNUSED(modifiers)
  107. Q_UNUSED(repeat)
  108. // not implemented yet
  109. return true;
  110. }
  111. //==============================================================================
  112. bool DeclarativeInputEngine::virtualKeyRelease(Qt::Key key, const QString & text, Qt::KeyboardModifiers modifiers)
  113. {
  114. Q_UNUSED(key)
  115. Q_UNUSED(text)
  116. Q_UNUSED(modifiers)
  117. // not implemented yet
  118. return true;
  119. }
  120. //==============================================================================
  121. QRect DeclarativeInputEngine::keyboardRectangle() const
  122. {
  123. return d->KeyboardRectangle;
  124. }
  125. //==============================================================================
  126. void DeclarativeInputEngine::setKeyboardRectangle(const QRect& Rect)
  127. {
  128. setAnimating(true);
  129. d->AnimatingFinishedTimer->start(100);
  130. d->KeyboardRectangle = Rect;
  131. emit keyboardRectangleChanged();
  132. }
  133. //==============================================================================
  134. bool DeclarativeInputEngine::isAnimating() const
  135. {
  136. return d->Animating;
  137. }
  138. //==============================================================================
  139. void DeclarativeInputEngine::setAnimating(bool Animating)
  140. {
  141. if (d->Animating != Animating)
  142. {
  143. d->Animating = Animating;
  144. emit animatingChanged();
  145. }
  146. }
  147. //==============================================================================
  148. void DeclarativeInputEngine::animatingFinished()
  149. {
  150. setAnimating(false);
  151. }
  152. //==============================================================================
  153. int DeclarativeInputEngine::inputMode() const
  154. {
  155. return d->InputMode;
  156. }
  157. //==============================================================================
  158. void DeclarativeInputEngine::setInputMode(int Mode)
  159. {
  160. qDebug() << "CDeclarativeInputEngine::setInputMode " << Mode;
  161. if (Mode != d->InputMode)
  162. {
  163. d->InputMode = Mode;
  164. emit inputModeChanged();
  165. }
  166. }
  167. //------------------------------------------------------------------------------
  168. // EOF DeclarativeInputEngine.cpp