lineedit-autoresize.cpp 2.4 KB

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