File: resizing-text-edit.cpp

package info (click to toggle)
obs-advanced-scene-switcher 1.32.8-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 43,492 kB
  • sloc: xml: 297,593; cpp: 147,875; python: 387; sh: 280; ansic: 170; makefile: 33
file content (101 lines) | stat: -rw-r--r-- 2,472 bytes parent folder | download | duplicates (2)
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
96
97
98
99
100
101
#include "resizing-text-edit.hpp"
#include "obs-module-helper.hpp"
#include "ui-helpers.hpp"

#include <QScrollBar>

namespace advss {

ResizingPlainTextEdit::ResizingPlainTextEdit(QWidget *parent,
					     const int scrollAt,
					     const int minLines,
					     const int paddingLines)
	: QPlainTextEdit(parent),
	  _scrollAt(scrollAt),
	  _minLines(minLines),
	  _paddingLines(paddingLines)
{
	setWordWrapMode(QTextOption::NoWrap);
	QWidget::connect(this, SIGNAL(textChanged()), this,
			 SLOT(ResizeTexteditArea()));
	QWidget::connect(this, SIGNAL(textChanged()), this,
			 SLOT(PreventExceedingMaxLength()));
	verticalScrollBar()->installEventFilter(this);
}

int ResizingPlainTextEdit::maxLength()
{
	return _maxLength;
}

void ResizingPlainTextEdit::setMaxLength(int maxLength)
{
	_maxLength = maxLength;
}

bool ResizingPlainTextEdit::eventFilter(QObject *obj, QEvent *event)
{
	if (obj == horizontalScrollBar()) {
		if (event->type() == QEvent::Show) {
			AddHeightForScrollBar(true);
		} else if (event->type() == QEvent::Hide) {
			AddHeightForScrollBar(false);
		}
	}

	return QPlainTextEdit::eventFilter(obj, event);
}

void ResizingPlainTextEdit::ResizeTexteditArea()
{
	QFontMetrics f(font());
	int rowHeight = f.lineSpacing();
	int numLines = document()->blockCount();

	int height = 0;
	if (numLines + _paddingLines < _minLines) {
		height = _minLines * rowHeight;
	} else if (numLines + _paddingLines < _scrollAt) {
		height = (numLines + _paddingLines) * rowHeight;
	} else {
		height = _scrollAt * rowHeight;
	}

	setFixedHeight(height + _hScrollBarAddedHeight);
	adjustSize();
	updateGeometry();
}

void ResizingPlainTextEdit::PreventExceedingMaxLength()
{
	auto plainText = QPlainTextEdit::toPlainText();

	if (_maxLength > -1 && plainText.length() > _maxLength) {
		QPlainTextEdit::setPlainText(plainText.left(_maxLength));
		QPlainTextEdit::moveCursor(QTextCursor::End);

		DisplayMessage(
			QString(obs_module_text(
					"AdvSceneSwitcher.validation.text.maxLength"))
				.arg(QString::number(_maxLength)));
	}
}

void ResizingPlainTextEdit::AddHeightForScrollBar(bool addHeight)
{
	if (!addHeight) {
		setFixedHeight(height() - _hScrollBarAddedHeight);
		_hScrollBarAddedHeight = 0;
		return;
	}

	// Check if we have already added space for the scroll bar
	if (_hScrollBarAddedHeight > 0) {
		return;
	}

	_hScrollBarAddedHeight = horizontalScrollBar()->height();
	setFixedHeight(height() + _hScrollBarAddedHeight);
}

} // namespace advss