lineedit-autoresize.cpp 2.5 KB

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