mirror-chatterino2/src/widgets/splits/SplitInput.hpp

166 lines
4.6 KiB
C++
Raw Normal View History

#pragma once
2017-01-01 02:30:42 +01:00
2019-08-13 16:39:22 +02:00
#include "util/QObjectRef.hpp"
2018-06-26 14:09:39 +02:00
#include "widgets/BaseWidget.hpp"
2019-08-13 18:48:22 +02:00
#include "widgets/dialogs/EmotePopup.hpp"
2017-01-21 05:14:27 +01:00
#include <QHBoxLayout>
#include <QLabel>
2017-01-21 05:14:27 +01:00
#include <QLineEdit>
2017-01-18 04:52:47 +01:00
#include <QPaintEvent>
2017-01-20 06:10:28 +01:00
#include <QTextEdit>
2017-01-21 05:14:27 +01:00
#include <QVBoxLayout>
2017-01-18 04:52:47 +01:00
#include <QWidget>
#include <memory>
2017-01-01 02:30:42 +01:00
2017-01-18 21:30:23 +01:00
namespace chatterino {
2017-11-12 17:21:50 +01:00
class Split;
class EmotePopup;
class InputCompletionPopup;
class EffectLabel;
class MessageThread;
class ResizingTextEdit;
2017-01-29 13:23:22 +01:00
// MessageOverflow is used for controlling how to guide the user into not
// sending a message that will be discarded by Twitch
enum MessageOverflow {
// Allow overflowing characters to be inserted into the input box, but highlight them in red
Highlight,
// Prevent more characters from being inserted into the input box
Prevent,
// Do nothing
Allow,
};
2017-11-12 17:21:50 +01:00
class SplitInput : public BaseWidget
2017-01-01 02:30:42 +01:00
{
Q_OBJECT
public:
SplitInput(Split *_chatWidget, bool enableInlineReplying = true);
SplitInput(QWidget *parent, Split *_chatWidget,
bool enableInlineReplying = true);
2017-01-01 02:30:42 +01:00
bool hasSelection() const;
void clearSelection() const;
bool isEditFirstWord() const;
QString getInputText() const;
2018-01-24 20:58:53 +01:00
void insertText(const QString &text);
void setReply(std::shared_ptr<MessageThread> reply,
bool showInlineReplying = true);
void setPlaceholderText(const QString &text);
/**
* @brief Hide the widget
*
* This is a no-op if the SplitInput is already hidden
**/
void hide();
/**
* @brief Show the widget
*
* This is a no-op if the SplitInput is already shown
**/
void show();
/**
* @brief Returns the hidden or shown state of the SplitInput
*
* Hidden in this context means "has 0 height", meaning it won't be visible
* but Qt still treats the widget as "technically visible" so we receive events
* as if the widget is visible
**/
bool isHidden() const;
pajlada::Signals::Signal<const QString &> textChanged;
2017-09-21 02:20:02 +02:00
2017-01-01 02:30:42 +01:00
protected:
void scaleChangedEvent(float scale_) override;
void themeChangedEvent() override;
void paintEvent(QPaintEvent * /*event*/) override;
void resizeEvent(QResizeEvent * /*event*/) override;
2017-07-31 01:26:20 +02:00
void mousePressEvent(QMouseEvent *event) override;
virtual void giveFocus(Qt::FocusReason reason);
QString handleSendMessage(std::vector<QString> &arguments);
void postMessageSend(const QString &message,
const std::vector<QString> &arguments);
/// Clears the input box, clears reply thread if inline replies are enabled
void clearInput();
protected:
void addShortcuts() override;
2018-07-06 19:23:47 +02:00
void initLayout();
bool eventFilter(QObject *obj, QEvent *event) override;
2018-07-06 19:23:47 +02:00
void installKeyPressedEvent();
void onCursorPositionChanged();
void onTextChanged();
2018-07-06 19:23:47 +02:00
void updateEmoteButton();
void updateCompletionPopup();
void showCompletionPopup(const QString &text, bool emoteCompletion);
void hideCompletionPopup();
void insertCompletionText(const QString &text);
2018-08-27 14:36:01 +02:00
void openEmotePopup();
2018-07-06 19:23:47 +02:00
void updateCancelReplyButton();
// scaledMaxHeight returns the height in pixels that this widget can grow to
// This does not take hidden into account, so callers must take hidden into account themselves
int scaledMaxHeight() const;
// Returns true if the channel this input is connected to is a Twitch channel,
// the user's setting is set to Prevent, and the given text goes beyond the Twitch message length limit
bool shouldPreventInput(const QString &text) const;
2018-06-06 18:57:22 +02:00
Split *const split_;
2019-08-13 16:39:22 +02:00
QObjectRef<EmotePopup> emotePopup_;
QObjectRef<InputCompletionPopup> inputCompletionPopup_;
2017-01-29 13:23:22 +01:00
2018-01-25 20:49:49 +01:00
struct {
ResizingTextEdit *textEdit;
QLabel *textEditLength;
2018-08-08 15:35:54 +02:00
EffectLabel *emoteButton;
2018-01-25 20:49:49 +01:00
QHBoxLayout *hbox;
QVBoxLayout *vbox;
QWidget *replyWrapper;
QHBoxLayout *replyHbox;
QLabel *replyLabel;
EffectLabel *cancelReplyButton;
2018-06-06 18:57:22 +02:00
} ui_;
2018-01-25 20:49:49 +01:00
std::shared_ptr<MessageThread> replyThread_ = nullptr;
bool enableInlineReplying_;
pajlada::Signals::SignalHolder managedConnections_;
2018-06-06 18:57:22 +02:00
QStringList prevMsg_;
QString currMsg_;
int prevIndex_ = 0;
2018-01-25 20:49:49 +01:00
// Hidden denotes whether this split input should be hidden or not
// This is used instead of the regular QWidget::hide/show because
// focus events don't work as expected, so instead we use this bool and
// set the height of the split input to 0 if we're supposed to be hidden instead
bool hidden{false};
private slots:
2017-01-29 13:23:22 +01:00
void editTextChanged();
2017-11-12 17:21:50 +01:00
friend class Split;
friend class ReplyThreadPopup;
2017-01-01 02:30:42 +01:00
};
2017-04-14 17:52:22 +02:00
} // namespace chatterino