Chatterino
MessageLayout.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include "common/Common.hpp"
4 #include "common/FlagsEnum.hpp"
5 
6 #include <QPixmap>
7 #include <boost/noncopyable.hpp>
8 #include <cinttypes>
9 #include <memory>
10 
11 namespace chatterino {
12 
13 struct Message;
14 using MessagePtr = std::shared_ptr<const Message>;
15 
16 struct Selection;
17 struct MessageLayoutContainer;
18 class MessageLayoutElement;
19 
20 enum class MessageElementFlag : int64_t;
21 using MessageElementFlags = FlagsEnum<MessageElementFlag>;
22 
23 enum class MessageLayoutFlag : uint8_t {
24  RequiresBufferUpdate = 1 << 1,
25  RequiresLayout = 1 << 2,
26  AlternateBackground = 1 << 3,
27  Collapsed = 1 << 4,
28  Expanded = 1 << 5,
29  IgnoreHighlights = 1 << 6,
30 };
32 
33 class MessageLayout : boost::noncopyable
34 {
35 public:
36  MessageLayout(MessagePtr message_);
37  ~MessageLayout();
38 
39  const Message *getMessage();
40  const MessagePtr &getMessagePtr() const;
41 
42  int getHeight() const;
43  int getWidth() const;
44 
46 
47  bool layout(int width, float scale_, MessageElementFlags flags);
48 
49  // Painting
50  void paint(QPainter &painter, int width, int y, int messageIndex,
51  Selection &selection, bool isLastReadMessage,
52  bool isWindowFocused, bool isMentions);
53  void invalidateBuffer();
54  void deleteBuffer();
55  void deleteCache();
56 
57  // Elements
58  const MessageLayoutElement *getElementAt(QPoint point);
59  int getLastCharacterIndex() const;
60  int getFirstMessageCharacterIndex() const;
61  int getSelectionIndex(QPoint position);
62  void addSelectionText(QString &str, uint32_t from = 0,
63  uint32_t to = UINT32_MAX,
64  CopyMode copymode = CopyMode::Everything);
65 
66  // Misc
67  bool isDisabled() const;
68  bool isReplyable() const;
69 
70 private:
71  // variables
72  MessagePtr message_;
73  std::shared_ptr<MessageLayoutContainer> container_;
74  std::shared_ptr<QPixmap> buffer_{};
75  bool bufferValid_ = false;
76 
77  int height_ = 0;
78 
79  int currentLayoutWidth_ = -1;
80  int layoutState_ = -1;
81  float scale_ = -1;
82  unsigned int layoutCount_ = 0;
83  unsigned int bufferUpdatedCount_ = 0;
84 
85  MessageElementFlags currentWordFlags_;
86 
87  int collapsedHeight_ = 32;
88 
89  // methods
90  void actuallyLayout(int width, MessageElementFlags flags);
91  void updateBuffer(QPixmap *pixmap, int messageIndex, Selection &selection);
92 };
93 
94 using MessageLayoutPtr = std::shared_ptr<MessageLayout>;
95 
96 } // namespace chatterino
int64_t FlagsEnum< MessageElementFlag > MessageElementFlags
Definition: MessageLayout.hpp:21
MessageLayoutFlag
Definition: MessageLayout.hpp:23
Definition: MessageLayoutElement.hpp:26
Definition: MessageLayout.hpp:33
Definition: Selection.hpp:44
Definition: Application.cpp:48
MessageElementFlag
Definition: MessageElement.hpp:29
MessageLayoutFlags flags
Definition: MessageLayout.hpp:45
Definition: FlagsEnum.hpp:9
CopyMode
Definition: Common.hpp:52
std::shared_ptr< const Message > MessagePtr
Definition: Channel.hpp:18
Definition: Message.hpp:54
std::shared_ptr< MessageLayout > MessageLayoutPtr
Definition: MessageLayout.hpp:94