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

178 lines
4.5 KiB
C++
Raw Normal View History

2017-06-06 17:18:23 +02:00
#pragma once
2017-01-01 02:30:42 +01:00
2017-06-11 09:31:45 +02:00
#include "channel.hpp"
#include "messages/lazyloadedimage.hpp"
#include "messages/limitedqueuesnapshot.hpp"
2017-06-11 09:31:45 +02:00
#include "messages/messageref.hpp"
#include "messages/word.hpp"
#include "widgets/accountpopup.hpp"
#include "widgets/basewidget.hpp"
2017-11-12 17:21:50 +01:00
#include "widgets/helper/rippleeffectlabel.hpp"
2017-06-11 09:31:45 +02:00
#include "widgets/scrollbar.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 <boost/signals2.hpp>
#include <pajlada/signals/signal.hpp>
2017-01-18 21:30:23 +01:00
namespace chatterino {
namespace widgets {
2017-08-18 15:12:07 +02:00
struct SelectionItem {
int messageIndex;
int charIndex;
2017-09-12 19:06:16 +02:00
SelectionItem()
{
messageIndex = charIndex = 0;
}
2017-08-18 15:12:07 +02:00
SelectionItem(int _messageIndex, int _charIndex)
{
this->messageIndex = _messageIndex;
this->charIndex = _charIndex;
}
2017-09-12 19:06:16 +02:00
bool isSmallerThan(const SelectionItem &other) const
{
return this->messageIndex < other.messageIndex ||
(this->messageIndex == other.messageIndex && this->charIndex < other.charIndex);
}
bool equals(const SelectionItem &other) const
2017-08-18 15:12:07 +02:00
{
2017-09-12 19:06:16 +02:00
return this->messageIndex == other.messageIndex && this->charIndex == other.charIndex;
}
};
struct Selection {
SelectionItem start;
SelectionItem end;
SelectionItem min;
SelectionItem max;
Selection()
{
}
Selection(const SelectionItem &start, const SelectionItem &end)
: start(start)
, end(end)
, min(start)
, max(end)
{
if (max.isSmallerThan(min)) {
std::swap(this->min, this->max);
}
}
bool isEmpty() const
{
return this->start.equals(this->end);
}
bool isSingleMessage() const
{
return this->min.messageIndex == this->max.messageIndex;
2017-08-18 15:12:07 +02:00
}
};
class ChannelView : 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:
2017-12-17 02:18:13 +01:00
explicit ChannelView(BaseWidget *parent = 0);
~ChannelView();
2017-04-12 17:46:44 +02:00
void updateGifEmotes();
void queueUpdate();
ScrollBar &getScrollBar();
QString getSelectedText();
2017-09-21 02:20:02 +02:00
bool hasSelection();
void clearSelection();
void setChannel(std::shared_ptr<Channel> channel);
messages::LimitedQueueSnapshot<messages::SharedMessageRef> getMessagesSnapshot();
void layoutMessages();
void clearMessages();
2017-02-07 00:03:15 +01:00
2017-09-16 16:49:52 +02:00
boost::signals2::signal<void(QMouseEvent *)> mouseDown;
2017-09-21 02:20:02 +02:00
boost::signals2::signal<void()> selectionChanged;
pajlada::Signals::NoArgSignal highlightedMessageReceived;
2017-09-16 16:49:52 +02:00
2017-01-03 21:19:33 +01:00
protected:
virtual void resizeEvent(QResizeEvent *) override;
2017-01-03 21:19:33 +01:00
virtual void paintEvent(QPaintEvent *) override;
virtual void wheelEvent(QWheelEvent *event) override;
2017-01-26 04:26:40 +01:00
virtual void mouseMoveEvent(QMouseEvent *event) override;
virtual void mousePressEvent(QMouseEvent *event) override;
virtual void mouseReleaseEvent(QMouseEvent *event) override;
2017-02-17 23:51:35 +01:00
2017-04-12 17:46:44 +02:00
bool tryGetMessageAt(QPoint p, std::shared_ptr<messages::MessageRef> &message,
2017-08-18 15:12:07 +02:00
QPoint &relativePos, int &index);
2017-02-17 23:51:35 +01:00
2017-01-18 04:33:30 +01:00
private:
2017-02-07 00:03:15 +01:00
struct GifEmoteData {
messages::LazyLoadedImage *image;
QRect rect;
};
QTimer updateTimer;
bool updateQueued = false;
2017-09-17 02:13:57 +02:00
void detachChannel();
void actuallyLayoutMessages();
2017-08-18 15:12:07 +02:00
void drawMessages(QPainter &painter);
void updateMessageBuffer(messages::MessageRef *messageRef, QPixmap *buffer, int messageIndex);
2017-09-12 19:06:16 +02:00
void drawMessageSelection(QPainter &painter, messages::MessageRef *messageRef, int messageIndex,
int bufferHeight);
void setSelection(const SelectionItem &start, const SelectionItem &end);
2017-08-18 15:12:07 +02:00
std::shared_ptr<Channel> channel;
2017-04-12 17:46:44 +02:00
std::vector<GifEmoteData> gifEmotes;
2017-04-12 17:46:44 +02:00
ScrollBar scrollBar;
RippleEffectLabel *goToBottom;
2017-02-07 00:03:15 +01:00
// This variable can be used to decide whether or not we should render the "Show latest
// messages" button
bool showingLatestMessages = true;
AccountPopupWidget userPopupWidget;
bool onlyUpdateEmotes = false;
2017-01-05 16:07:20 +01:00
2017-04-12 17:46:44 +02:00
// Mouse event variables
bool isMouseDown = false;
QPointF lastPressPosition;
2017-01-22 12:46:35 +01:00
2017-09-12 19:06:16 +02:00
Selection selection;
2017-08-18 15:12:07 +02:00
bool selecting = false;
messages::LimitedQueue<messages::SharedMessageRef> messages;
boost::signals2::connection messageAppendedConnection;
boost::signals2::connection messageRemovedConnection;
2017-09-17 02:13:57 +02:00
boost::signals2::connection repaintGifsConnection;
boost::signals2::connection layoutConnection;
std::vector<pajlada::Signals::ScopedConnection> managedConnections;
2017-01-22 12:46:35 +01:00
private slots:
2017-04-12 17:46:44 +02:00
void wordTypeMaskChanged()
2017-01-22 12:46:35 +01:00
{
2017-02-17 23:51:35 +01:00
layoutMessages();
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 widgets
} // namespace chatterino