mirror-chatterino2/src/messages/layouts/MessageLayout.hpp

92 lines
2.2 KiB
C++
Raw Normal View History

#pragma once
2018-08-16 00:16:33 +02:00
#include "common/Common.hpp"
2018-06-26 17:20:03 +02:00
#include "common/FlagsEnum.hpp"
#include <QPixmap>
#include <boost/noncopyable.hpp>
#include <cinttypes>
#include <memory>
namespace chatterino {
2018-04-01 16:43:30 +02:00
struct Message;
using MessagePtr = std::shared_ptr<const Message>;
struct Selection;
struct MessageLayoutContainer;
class MessageLayoutElement;
enum class MessageElementFlag;
using MessageElementFlags = FlagsEnum<MessageElementFlag>;
2018-08-07 07:55:31 +02:00
enum class MessageLayoutFlag : uint8_t {
RequiresBufferUpdate = 1 << 1,
RequiresLayout = 1 << 2,
AlternateBackground = 1 << 3,
Collapsed = 1 << 4,
Expanded = 1 << 5,
2018-10-21 13:29:52 +02:00
IgnoreHighlights = 1 << 6,
2018-08-07 07:55:31 +02:00
};
using MessageLayoutFlags = FlagsEnum<MessageLayoutFlag>;
2018-01-28 16:29:47 +01:00
class MessageLayout : boost::noncopyable
{
public:
MessageLayout(MessagePtr message_);
2018-04-06 16:37:30 +02:00
~MessageLayout();
2018-08-07 01:35:24 +02:00
const Message *getMessage();
int getHeight() const;
2018-08-07 07:55:31 +02:00
MessageLayoutFlags flags;
2018-08-07 07:55:31 +02:00
bool layout(int width, float scale_, MessageElementFlags flags);
// Painting
2018-08-06 21:17:03 +02:00
void paint(QPainter &painter, int width, int y, int messageIndex,
Selection &selection, bool isLastReadMessage,
bool isWindowFocused);
void invalidateBuffer();
void deleteBuffer();
2018-04-18 09:12:29 +02:00
void deleteCache();
// Elements
const MessageLayoutElement *getElementAt(QPoint point);
int getLastCharacterIndex() const;
int getFirstMessageCharacterIndex() const;
int getSelectionIndex(QPoint position);
2018-08-16 00:16:33 +02:00
void addSelectionText(QString &str, int from = 0, int to = INT_MAX,
CopyMode copymode = CopyMode::Everything);
// Misc
bool isDisabled() const;
private:
// variables
MessagePtr message_;
std::shared_ptr<MessageLayoutContainer> container_;
2018-09-20 13:09:37 +02:00
std::shared_ptr<QPixmap> buffer_{};
bool bufferValid_ = false;
int height_ = 0;
int currentLayoutWidth_ = -1;
int layoutState_ = -1;
float scale_ = -1;
unsigned int bufferUpdatedCount_ = 0;
2018-08-07 07:55:31 +02:00
MessageElementFlags currentWordFlags_;
int collapsedHeight_ = 32;
// methods
2018-08-07 07:55:31 +02:00
void actuallyLayout(int width, MessageElementFlags flags);
void updateBuffer(QPixmap *pixmap, int messageIndex, Selection &selection);
};
2018-04-01 16:43:30 +02:00
using MessageLayoutPtr = std::shared_ptr<MessageLayout>;
} // namespace chatterino