File: lineedit-autoresize.cpp

package info (click to toggle)
obs-studio 29.0.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 35,920 kB
  • sloc: ansic: 182,921; cpp: 98,959; sh: 1,591; python: 945; makefile: 858; javascript: 19
file content (95 lines) | stat: -rw-r--r-- 2,476 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include "lineedit-autoresize.hpp"

LineEditAutoResize::LineEditAutoResize()
{
	connect(this, SIGNAL(textChanged()), this, SLOT(checkTextLength()));
	connect(document()->documentLayout(),
		SIGNAL(documentSizeChanged(const QSizeF &)), this,
		SLOT(resizeVertically(const QSizeF &)));
}

void LineEditAutoResize::checkTextLength()
{
	QString text = toPlainText();
	if (text.length() > m_maxLength) {
		setPlainText(text.left(m_maxLength));
		QTextCursor cursor = textCursor();
		cursor.setPosition(m_maxLength);
		setTextCursor(cursor);
	}
}

int LineEditAutoResize::maxLength()
{
	return m_maxLength;
}

void LineEditAutoResize::setMaxLength(int length)
{
	m_maxLength = length;
}

void LineEditAutoResize::keyPressEvent(QKeyEvent *event)
{
	/* Always allow events with modifiers, for example to Copy content */
	Qt::KeyboardModifiers modifiers = event->modifiers();
	if (modifiers != Qt::NoModifier && modifiers != Qt::ShiftModifier) {
		QTextEdit::keyPressEvent(event);
		return;
	}

	switch (event->key()) {
	/* Always ignore the return key and send the signal instead */
	case Qt::Key_Return:
		event->ignore();
		emit returnPressed();
		break;
	/* Always allow navigation and deletion */
	case Qt::Key_Left:
	case Qt::Key_Right:
	case Qt::Key_Up:
	case Qt::Key_Down:
	case Qt::Key_PageUp:
	case Qt::Key_PageDown:
	case Qt::Key_Delete:
	case Qt::Key_Backspace:
		QTextEdit::keyPressEvent(event);
		break;
	/* Allow only if the content is not already at max length.
	 * Some keys with modifiers should still be allowed though
	 * (for example for Copy), and we don't want to make assumptions
	 * about which modifiers are okay and which aren't, so let's
	 * allow all. Actions that will still exceed the limit (like
	 * Paste) can be caught in a later step. */
	default:
		if (toPlainText().length() >= m_maxLength &&
		    event->modifiers() == Qt::NoModifier &&
		    !textCursor().hasSelection())
			event->ignore();
		else
			QTextEdit::keyPressEvent(event);
		break;
	}
}

void LineEditAutoResize::resizeVertically(const QSizeF &newSize)
{
	QMargins margins = contentsMargins();
	setMaximumHeight(newSize.height() + margins.top() + margins.bottom());
}

QString LineEditAutoResize::text()
{
	return toPlainText();
}

void LineEditAutoResize::setText(const QString &text)
{
	QMetaObject::invokeMethod(this, "SetPlainText", Qt::QueuedConnection,
				  Q_ARG(const QString &, text));
}

void LineEditAutoResize::SetPlainText(const QString &text)
{
	setPlainText(text);
}