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

224 lines
6.7 KiB
C++
Raw Normal View History

2017-06-06 17:18:23 +02:00
#pragma once
2017-01-01 02:30:42 +01:00
#include "common/FlagsEnum.hpp"
2019-08-20 21:50:36 +02:00
#include "messages/Image.hpp"
#include "messages/LimitedQueue.hpp"
2018-06-26 14:09:39 +02:00
#include "messages/LimitedQueueSnapshot.hpp"
#include "messages/Selection.hpp"
#include "widgets/BaseWidget.hpp"
2017-01-01 02:30:42 +01:00
2017-01-18 04:33:30 +01:00
#include <QPaintEvent>
2017-01-26 04:26:40 +01:00
#include <QScroller>
#include <QTimer>
2017-01-26 04:26:40 +01:00
#include <QWheelEvent>
2017-01-18 04:33:30 +01:00
#include <QWidget>
#include <pajlada/signals/signal.hpp>
2018-11-03 21:26:57 +01:00
#include <unordered_map>
#include <unordered_set>
2017-01-18 21:30:23 +01:00
namespace chatterino {
enum class HighlightState;
class Channel;
using ChannelPtr = std::shared_ptr<Channel>;
2018-11-03 21:26:57 +01:00
struct Message;
using MessagePtr = std::shared_ptr<const Message>;
2019-03-20 20:46:20 +01:00
enum class MessageFlag : uint32_t;
2018-11-03 21:26:57 +01:00
using MessageFlags = FlagsEnum<MessageFlag>;
class MessageLayout;
using MessageLayoutPtr = std::shared_ptr<MessageLayout>;
enum class MessageElementFlag;
using MessageElementFlags = FlagsEnum<MessageElementFlag>;
class Scrollbar;
class EffectLabel;
struct Link;
class MessageLayoutElement;
2018-11-03 21:26:57 +01:00
enum class PauseReason {
Mouse,
Selection,
DoubleClick,
2019-09-16 10:40:02 +02:00
KeyboardModifier,
2018-11-03 21:26:57 +01:00
};
using SteadyClock = std::chrono::steady_clock;
class ChannelView final : public BaseWidget
2017-01-01 02:30:42 +01:00
{
Q_OBJECT
2017-09-12 19:06:16 +02:00
2017-01-01 02:30:42 +01:00
public:
explicit ChannelView(BaseWidget *parent = nullptr);
void queueUpdate();
2018-01-06 03:48:56 +01:00
Scrollbar &getScrollBar();
QString getSelectedText();
2017-09-21 02:20:02 +02:00
bool hasSelection();
void clearSelection();
void setEnableScrollingToBottom(bool);
bool getEnableScrollingToBottom() const;
2018-08-07 07:55:31 +02:00
void setOverrideFlags(boost::optional<MessageElementFlags> value);
const boost::optional<MessageElementFlags> &getOverrideFlags() const;
2018-01-23 22:48:33 +01:00
void updateLastReadMessage();
2018-11-14 17:26:08 +01:00
/// Pausing
bool pausable() const;
void setPausable(bool value);
2018-11-03 21:26:57 +01:00
bool paused() const;
void pause(PauseReason reason, boost::optional<uint> msecs = boost::none);
void unpause(PauseReason reason);
2018-11-14 17:26:08 +01:00
ChannelPtr channel();
2018-06-13 13:27:10 +02:00
void setChannel(ChannelPtr channel_);
2018-11-14 17:26:08 +01:00
LimitedQueueSnapshot<MessageLayoutPtr> getMessagesSnapshot();
2018-11-03 21:26:57 +01:00
void queueLayout();
void clearMessages();
void showUserInfoPopup(const QString &userName);
2017-02-07 00:03:15 +01:00
pajlada::Signals::Signal<QMouseEvent *> mouseDown;
pajlada::Signals::NoArgSignal selectionChanged;
pajlada::Signals::Signal<HighlightState> tabHighlightRequested;
2018-10-13 14:20:06 +02:00
pajlada::Signals::NoArgSignal liveStatusChanged;
pajlada::Signals::Signal<const Link &> linkClicked;
pajlada::Signals::Signal<QString> joinToChannel;
2017-09-16 16:49:52 +02:00
2017-01-03 21:19:33 +01:00
protected:
2018-07-06 17:11:37 +02:00
void themeChangedEvent() override;
2018-12-02 18:26:21 +01:00
void scaleChangedEvent(float scale) override;
2018-01-24 20:35:26 +01:00
void resizeEvent(QResizeEvent *) override;
2017-01-03 21:19:33 +01:00
void paintEvent(QPaintEvent *) override;
void wheelEvent(QWheelEvent *event) override;
2017-01-26 04:26:40 +01:00
void enterEvent(QEvent *) override;
void leaveEvent(QEvent *) override;
void mouseMoveEvent(QMouseEvent *event) override;
void mousePressEvent(QMouseEvent *event) override;
void mouseReleaseEvent(QMouseEvent *event) override;
void mouseDoubleClickEvent(QMouseEvent *event) override;
2018-01-24 21:44:31 +01:00
2018-04-18 09:12:29 +02:00
void hideEvent(QHideEvent *) override;
2018-08-06 21:17:03 +02:00
void handleLinkClick(QMouseEvent *event, const Link &link,
MessageLayout *layout);
2017-02-17 23:51:35 +01:00
2018-08-06 21:17:03 +02:00
bool tryGetMessageAt(QPoint p, std::shared_ptr<MessageLayout> &message,
QPoint &relativePos, int &index);
2017-02-17 23:51:35 +01:00
2017-01-18 04:33:30 +01:00
private:
2018-08-08 20:06:20 +02:00
void initializeLayout();
void initializeScrollbar();
void initializeSignals();
2018-11-03 21:26:57 +01:00
void messageAppended(MessagePtr &message,
boost::optional<MessageFlags> overridingFlags);
void messageAddedAtStart(std::vector<MessagePtr> &messages);
void messageRemoveFromStart(MessagePtr &message);
void messageReplaced(size_t index, MessagePtr &replacement);
2018-08-08 20:06:20 +02:00
2018-11-03 21:26:57 +01:00
void performLayout(bool causedByScollbar = false);
void layoutVisibleMessages(
LimitedQueueSnapshot<MessageLayoutPtr> &messages);
void updateScrollbar(LimitedQueueSnapshot<MessageLayoutPtr> &messages,
bool causedByScrollbar);
2018-07-06 19:23:47 +02:00
void drawMessages(QPainter &painter);
void setSelection(const SelectionItem &start, const SelectionItem &end);
2018-08-07 07:55:31 +02:00
MessageElementFlags getFlags() const;
void selectWholeMessage(MessageLayout *layout, int &messageIndex);
2018-10-06 13:43:21 +02:00
void getWordBounds(MessageLayout *layout,
const MessageLayoutElement *element,
const QPoint &relativePos, int &wordStart, int &wordEnd);
2018-07-06 19:23:47 +02:00
2018-08-06 21:17:03 +02:00
void handleMouseClick(QMouseEvent *event,
const MessageLayoutElement *hoverLayoutElement,
MessageLayout *layout);
2018-08-06 21:17:03 +02:00
void addContextMenuItems(const MessageLayoutElement *hoveredElement,
MessageLayout *layout);
2018-07-06 19:23:47 +02:00
int getLayoutWidth() const;
2018-11-03 21:26:57 +01:00
void updatePauseTimer();
2018-07-06 19:23:47 +02:00
2018-06-13 13:27:10 +02:00
QTimer *layoutCooldown_;
bool layoutQueued_;
2018-06-13 13:27:10 +02:00
QTimer updateTimer_;
bool updateQueued_ = false;
bool messageWasAdded_ = false;
bool lastMessageHasAlternateBackground_ = false;
2018-11-03 21:40:48 +01:00
bool lastMessageHasAlternateBackgroundReverse_ = true;
bool pausable_ = false;
2018-11-03 21:26:57 +01:00
QTimer pauseTimer_;
std::unordered_map<PauseReason, boost::optional<SteadyClock::time_point>>
pauses_;
boost::optional<SteadyClock::time_point> pauseEnd_;
int pauseScrollOffset_ = 0;
int pauseSelectionOffset_ = 0;
2018-08-07 07:55:31 +02:00
boost::optional<MessageElementFlags> overrideFlags_;
MessageLayoutPtr lastReadMessage_;
LimitedQueueSnapshot<MessageLayoutPtr> snapshot_;
2017-09-17 02:13:57 +02:00
2018-06-13 13:27:10 +02:00
ChannelPtr channel_;
2017-04-12 17:46:44 +02:00
Scrollbar *scrollBar_;
2018-08-08 15:35:54 +02:00
EffectLabel *goToBottom_;
2017-02-07 00:03:15 +01:00
2018-08-06 21:17:03 +02:00
// This variable can be used to decide whether or not we should render the
// "Show latest messages" button
2018-06-13 13:27:10 +02:00
bool showingLatestMessages_ = true;
bool enableScrollingToBottom_ = true;
2018-06-13 13:27:10 +02:00
bool onlyUpdateEmotes_ = false;
2017-01-05 16:07:20 +01:00
2017-04-12 17:46:44 +02:00
// Mouse event variables
2018-06-13 13:27:10 +02:00
bool isMouseDown_ = false;
bool isRightMouseDown_ = false;
2018-10-06 13:43:21 +02:00
bool isDoubleClick_ = false;
2018-11-14 17:26:08 +01:00
DoubleClickSelection doubleClickSelection_;
2018-06-13 13:27:10 +02:00
QPointF lastPressPosition_;
QPointF lastRightPressPosition_;
QPointF lastDClickPosition_;
QTimer *clickTimer_;
2017-01-22 12:46:35 +01:00
Selection selection_;
2018-06-13 13:27:10 +02:00
bool selecting_ = false;
2017-08-18 15:12:07 +02:00
2018-11-14 17:26:08 +01:00
LimitedQueue<MessageLayoutPtr> messages_;
2018-06-13 13:27:10 +02:00
pajlada::Signals::Connection messageAppendedConnection_;
pajlada::Signals::Connection messageAddedAtStartConnection_;
pajlada::Signals::Connection messageRemovedConnection_;
pajlada::Signals::Connection messageReplacedConnection_;
pajlada::Signals::Connection repaintGifsConnection_;
pajlada::Signals::Connection layoutConnection_;
2018-06-13 03:58:52 +02:00
std::vector<pajlada::Signals::ScopedConnection> connections_;
std::vector<pajlada::Signals::ScopedConnection> channelConnections_;
std::unordered_set<std::shared_ptr<MessageLayout>> messagesOnScreen_;
2018-12-02 17:49:15 +01:00
static constexpr int leftPadding = 8;
static constexpr int scrollbarPadding = 8;
2017-01-22 12:46:35 +01:00
private slots:
void wordFlagsChanged()
2017-01-22 12:46:35 +01:00
{
2018-11-03 21:26:57 +01:00
queueLayout();
2017-02-07 00:03:15 +01:00
update();
2017-01-22 12:46:35 +01:00
}
2017-01-01 02:30:42 +01:00
};
2017-06-06 17:18:23 +02:00
2017-04-14 17:52:22 +02:00
} // namespace chatterino