mirror-chatterino2/widgets/resizingtextedit.h

55 lines
1.2 KiB
C
Raw Normal View History

2017-01-21 05:14:27 +01:00
#ifndef RESIZINGTEXTEDIT_H
#define RESIZINGTEXTEDIT_H
2017-01-29 13:23:22 +01:00
#include <QKeyEvent>
2017-01-21 05:14:27 +01:00
#include <QTextEdit>
2017-01-29 13:23:22 +01:00
#include <boost/signals2.hpp>
2017-01-21 05:14:27 +01:00
class ResizingTextEdit : public QTextEdit
{
public:
ResizingTextEdit()
2017-01-29 13:23:22 +01:00
: keyPressed()
2017-01-21 05:14:27 +01:00
{
auto sizePolicy = this->sizePolicy();
sizePolicy.setHeightForWidth(true);
sizePolicy.setVerticalPolicy(QSizePolicy::Preferred);
this->setSizePolicy(sizePolicy);
2017-04-12 17:46:44 +02:00
QObject::connect(this, &QTextEdit::textChanged, this, &QWidget::updateGeometry);
2017-01-21 05:14:27 +01:00
}
2017-04-12 17:46:44 +02:00
QSize sizeHint() const override
2017-01-21 05:14:27 +01:00
{
return QSize(this->width(), this->heightForWidth(this->width()));
}
2017-04-12 17:46:44 +02:00
bool hasHeightForWidth() const override
2017-01-21 05:14:27 +01:00
{
return true;
}
2017-01-29 13:23:22 +01:00
boost::signals2::signal<void(QKeyEvent *)> keyPressed;
2017-01-21 05:14:27 +01:00
protected:
2017-04-12 17:46:44 +02:00
int heightForWidth(int) const override
2017-01-21 05:14:27 +01:00
{
auto margins = this->contentsMargins();
2017-04-12 17:46:44 +02:00
return margins.top() + document()->size().height() + margins.bottom() + 5;
2017-01-21 05:14:27 +01:00
}
2017-01-29 13:23:22 +01:00
2017-04-12 17:46:44 +02:00
void keyPressEvent(QKeyEvent *event)
2017-01-29 13:23:22 +01:00
{
event->ignore();
keyPressed(event);
if (!event->isAccepted()) {
QTextEdit::keyPressEvent(event);
}
}
2017-01-21 05:14:27 +01:00
};
#endif // RESIZINGTEXTEDIT_H