mirror of
https://github.com/Chatterino/chatterino2.git
synced 2024-11-13 19:49:51 +01:00
52 lines
1.1 KiB
C++
52 lines
1.1 KiB
C++
#ifndef RESIZINGTEXTEDIT_H
|
|
#define RESIZINGTEXTEDIT_H
|
|
|
|
#include <QTextEdit>
|
|
|
|
class ResizingTextEdit : public QTextEdit
|
|
{
|
|
public:
|
|
ResizingTextEdit()
|
|
{
|
|
auto sizePolicy = this->sizePolicy();
|
|
sizePolicy.setHeightForWidth(true);
|
|
sizePolicy.setVerticalPolicy(QSizePolicy::Preferred);
|
|
this->setSizePolicy(sizePolicy);
|
|
|
|
QObject::connect(this, &QTextEdit::textChanged, this, &QWidget::updateGeometry);
|
|
}
|
|
|
|
QSize
|
|
sizeHint() const override
|
|
{
|
|
return QSize(this->width(), this->heightForWidth(this->width()));
|
|
}
|
|
|
|
bool
|
|
hasHeightForWidth() const override
|
|
{
|
|
return true;
|
|
}
|
|
|
|
protected:
|
|
int
|
|
heightForWidth(int w) const override
|
|
{
|
|
auto margins = this->contentsMargins();
|
|
|
|
int documentWidth;
|
|
|
|
if (w >= margins.left() + margins.right()) {
|
|
documentWidth = w - margins.left() - margins.right();
|
|
} else {
|
|
documentWidth = 0;
|
|
}
|
|
|
|
auto document = this->document()->clone();
|
|
|
|
return margins.top() + document->size().height() + margins.bottom() + 5;
|
|
}
|
|
};
|
|
|
|
#endif // RESIZINGTEXTEDIT_H
|