lineedit-autoresize.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #include "lineedit-autoresize.hpp"
  2. LineEditAutoResize::LineEditAutoResize()
  3. {
  4. connect(this, SIGNAL(textChanged()), this, SLOT(checkTextLength()));
  5. connect(document()->documentLayout(),
  6. SIGNAL(documentSizeChanged(const QSizeF &)), this,
  7. SLOT(resizeVertically(const QSizeF &)));
  8. }
  9. void LineEditAutoResize::checkTextLength()
  10. {
  11. QString text = toPlainText();
  12. if (text.length() > m_maxLength) {
  13. setPlainText(text.left(m_maxLength));
  14. QTextCursor cursor = textCursor();
  15. cursor.setPosition(m_maxLength);
  16. setTextCursor(cursor);
  17. }
  18. }
  19. int LineEditAutoResize::maxLength()
  20. {
  21. return m_maxLength;
  22. }
  23. void LineEditAutoResize::setMaxLength(int length)
  24. {
  25. m_maxLength = length;
  26. }
  27. void LineEditAutoResize::keyPressEvent(QKeyEvent *event)
  28. {
  29. /* Always allow events with modifiers, for example to Copy content */
  30. Qt::KeyboardModifiers modifiers = event->modifiers();
  31. if (modifiers != Qt::NoModifier && modifiers != Qt::ShiftModifier) {
  32. QTextEdit::keyPressEvent(event);
  33. return;
  34. }
  35. switch (event->key()) {
  36. /* Always ignore the return key and send the signal instead */
  37. case Qt::Key_Return:
  38. event->ignore();
  39. emit returnPressed();
  40. break;
  41. /* Always allow navigation and deletion */
  42. case Qt::Key_Left:
  43. case Qt::Key_Right:
  44. case Qt::Key_Up:
  45. case Qt::Key_Down:
  46. case Qt::Key_PageUp:
  47. case Qt::Key_PageDown:
  48. case Qt::Key_Delete:
  49. case Qt::Key_Backspace:
  50. QTextEdit::keyPressEvent(event);
  51. break;
  52. /* Allow only if the content is not already at max length.
  53. * Some keys with modifiers should still be allowed though
  54. * (for example for Copy), and we don't want to make assumptions
  55. * about which modifiers are okay and which aren't, so let's
  56. * allow all. Actions that will still exceed the limit (like
  57. * Paste) can be caught in a later step. */
  58. default:
  59. if (toPlainText().length() >= m_maxLength &&
  60. event->modifiers() == Qt::NoModifier &&
  61. !textCursor().hasSelection())
  62. event->ignore();
  63. else
  64. QTextEdit::keyPressEvent(event);
  65. break;
  66. }
  67. }
  68. void LineEditAutoResize::resizeVertically(const QSizeF &newSize)
  69. {
  70. setMaximumHeight(newSize.height() + 7);
  71. }
  72. QString LineEditAutoResize::text()
  73. {
  74. return toPlainText();
  75. }
  76. void LineEditAutoResize::setText(const QString &text)
  77. {
  78. QMetaObject::invokeMethod(this, "SetPlainText", Qt::QueuedConnection,
  79. Q_ARG(const QString &, text));
  80. }
  81. void LineEditAutoResize::SetPlainText(const QString &text)
  82. {
  83. setPlainText(text);
  84. }