mirror-chatterino2/chatwidgetview.cpp

99 lines
2.3 KiB
C++
Raw Normal View History

2017-01-01 02:30:42 +01:00
#include "chatwidgetview.h"
#include "channels.h"
2017-01-11 18:52:09 +01:00
#include "message.h"
2017-01-11 01:08:20 +01:00
#include "word.h"
#include "wordpart.h"
#include <QPainter>
2017-01-11 18:52:09 +01:00
#include <QScroller>
2017-01-01 02:30:42 +01:00
ChatWidgetView::ChatWidgetView()
2017-01-11 18:52:09 +01:00
: QWidget()
, scrollbar(this)
, m_channel(NULL)
2017-01-01 02:30:42 +01:00
{
2017-01-05 16:07:20 +01:00
auto scroll = QScroller::scroller(this);
2017-01-01 02:30:42 +01:00
2017-01-05 16:07:20 +01:00
scroll->scrollTo(QPointF(0, 100));
m_channel = Channels::getChannel("fourtf");
2017-01-01 02:30:42 +01:00
}
2017-01-03 21:19:33 +01:00
bool
ChatWidgetView::layoutMessages()
2017-01-03 21:19:33 +01:00
{
2017-01-11 01:08:20 +01:00
auto c = channel();
2017-01-11 18:52:09 +01:00
if (c == NULL)
return false;
2017-01-11 01:08:20 +01:00
auto messages = c->getMessagesClone();
bool redraw = false;
2017-01-11 18:52:09 +01:00
for (std::shared_ptr<Message> &message : messages) {
redraw |= message.get()->layout(this->width(), true);
2017-01-11 01:08:20 +01:00
}
return redraw;
}
void
ChatWidgetView::resizeEvent(QResizeEvent *)
{
scrollbar.resize(scrollbar.width(), height());
scrollbar.move(width() - scrollbar.width(), 0);
layoutMessages();
2017-01-03 21:19:33 +01:00
}
2017-01-05 16:07:20 +01:00
2017-01-11 18:52:09 +01:00
void
ChatWidgetView::paintEvent(QPaintEvent *)
2017-01-05 16:07:20 +01:00
{
QPainter painter(this);
2017-01-15 16:38:30 +01:00
painter.setRenderHint(QPainter::SmoothPixmapTransform);
2017-01-05 16:07:20 +01:00
auto c = channel();
2017-01-11 18:52:09 +01:00
if (c == NULL)
return;
2017-01-05 16:07:20 +01:00
2017-01-11 01:08:20 +01:00
auto messages = c->getMessagesClone();
2017-01-15 16:38:30 +01:00
int y = 0;
2017-01-11 01:08:20 +01:00
2017-01-11 18:52:09 +01:00
for (std::shared_ptr<Message> const &message : messages) {
for (WordPart const &wordPart : message.get()->wordParts()) {
2017-01-13 18:59:11 +01:00
painter.setPen(QColor(255, 0, 0));
painter.drawRect(wordPart.x(), wordPart.y() + y, wordPart.width(),
wordPart.height());
2017-01-11 01:08:20 +01:00
// image
2017-01-11 18:52:09 +01:00
if (wordPart.word().isImage()) {
LazyLoadedImage &lli = wordPart.word().getImage();
2017-01-11 01:08:20 +01:00
2017-01-13 18:59:11 +01:00
const QPixmap *image = lli.pixmap();
2017-01-11 01:08:20 +01:00
2017-01-11 18:52:09 +01:00
if (image != NULL) {
2017-01-13 18:59:11 +01:00
painter.drawPixmap(
2017-01-11 18:52:09 +01:00
QRect(wordPart.x(), wordPart.y() + y, wordPart.width(),
wordPart.height()),
*image);
2017-01-11 01:08:20 +01:00
}
}
// text
2017-01-11 18:52:09 +01:00
else {
painter.setPen(wordPart.word().color());
painter.setFont(wordPart.word().getFont());
2017-01-13 18:59:11 +01:00
painter.drawText(
QRectF(wordPart.x(), wordPart.y() + y, 10000, 10000),
2017-01-15 16:38:30 +01:00
wordPart.text(), QTextOption(Qt::AlignLeft | Qt::AlignTop));
2017-01-11 01:08:20 +01:00
}
}
2017-01-07 20:43:55 +01:00
2017-01-11 01:08:20 +01:00
y += message.get()->height();
}
2017-01-05 16:07:20 +01:00
}