lineedit-autoresize.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. QMargins margins = contentsMargins();
  71. setMaximumHeight(newSize.height() + margins.top() + margins.bottom());
  72. }
  73. QString LineEditAutoResize::text()
  74. {
  75. return toPlainText();
  76. }
  77. void LineEditAutoResize::setText(const QString &text)
  78. {
  79. QMetaObject::invokeMethod(this, "SetPlainText", Qt::QueuedConnection,
  80. Q_ARG(const QString &, text));
  81. }
  82. void LineEditAutoResize::SetPlainText(const QString &text)
  83. {
  84. setPlainText(text);
  85. }