mirror-chatterino2/src/widgets/helper/ResizingTextEdit.hpp

76 lines
2 KiB
C++
Raw Normal View History

#pragma once
2017-01-21 05:14:27 +01:00
#include <pajlada/signals/signal.hpp>
2017-07-09 00:09:02 +02:00
#include <QCompleter>
2017-01-29 13:23:22 +01:00
#include <QKeyEvent>
2017-01-21 05:14:27 +01:00
#include <QTextEdit>
2018-06-26 16:37:59 +02:00
namespace chatterino {
2017-01-21 05:14:27 +01:00
class ResizingTextEdit : public QTextEdit
{
public:
2017-07-09 00:09:02 +02:00
ResizingTextEdit();
2017-01-21 05:14:27 +01:00
2017-07-09 00:09:02 +02:00
QSize sizeHint() const override;
2017-01-29 13:23:22 +01:00
2017-07-09 00:09:02 +02:00
bool hasHeightForWidth() const override;
bool isFirstWord() const;
2017-01-21 05:14:27 +01:00
pajlada::Signals::Signal<QKeyEvent *> keyPressed;
2018-05-25 14:57:17 +02:00
pajlada::Signals::NoArgSignal focused;
pajlada::Signals::NoArgSignal focusLost;
pajlada::Signals::Signal<const QMimeData *> imagePasted;
2017-01-29 13:23:22 +01:00
2017-07-09 00:09:02 +02:00
void setCompleter(QCompleter *c);
QCompleter *getCompleter() const;
/**
* Resets a completion for this text if one was is progress.
* See `completionInProgress_`.
*/
void resetCompletion();
2017-07-09 00:09:02 +02:00
protected:
int heightForWidth(int) const override;
void keyPressEvent(QKeyEvent *event) override;
2017-01-29 13:23:22 +01:00
2018-05-25 14:57:17 +02:00
void focusInEvent(QFocusEvent *event) override;
void focusOutEvent(QFocusEvent *event) override;
2018-05-25 14:57:17 +02:00
bool canInsertFromMimeData(const QMimeData *source) const override;
void insertFromMimeData(const QMimeData *source) override;
2017-07-09 00:09:02 +02:00
private:
2018-08-06 21:17:03 +02:00
// hadSpace is set to true in case the "textUnderCursor" word was after a
// space
QString textUnderCursor(bool *hadSpace = nullptr) const;
2018-07-06 19:23:47 +02:00
QCompleter *completer_ = nullptr;
/**
* This is true if a completion was done but the user didn't type yet,
* and might want to press `Tab` again to get the next completion
* on the original text.
*
* For example:
*
* input: "pog"
* `Tab` pressed:
* - complete to "PogBones"
* - retain "pog" for next completion
* - set `completionInProgress_ = true`
* `Tab` pressed again:
* - complete ["pog"] to "PogChamp"
*
* [other key] pressed - updating the input text:
* - set `completionInProgress_ = false`
*/
2018-07-06 19:23:47 +02:00
bool completionInProgress_ = false;
bool eventFilter(QObject *obj, QEvent *event) override;
2017-07-09 00:09:02 +02:00
private slots:
void insertCompletion(const QString &completion);
2017-01-21 05:14:27 +01:00
};
2018-06-26 16:37:59 +02:00
2018-06-26 17:20:03 +02:00
} // namespace chatterino