mirror-chatterino2/chatwidgetview.cpp

149 lines
3.7 KiB
C++
Raw Normal View History

2017-01-01 02:30:42 +01:00
#include "chatwidgetview.h"
#include "channels.h"
2017-01-17 00:15:44 +01:00
#include "chatwidget.h"
2017-01-18 01:04:54 +01:00
#include "colorscheme.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"
2017-01-18 14:48:42 +01:00
#include <math.h>
2017-01-11 01:08:20 +01:00
#include <QPainter>
2017-01-11 18:52:09 +01:00
#include <QScroller>
2017-01-18 01:04:54 +01:00
#include <functional>
2017-01-01 02:30:42 +01:00
2017-01-17 00:15:44 +01:00
ChatWidgetView::ChatWidgetView(ChatWidget *parent)
2017-01-11 18:52:09 +01:00
: QWidget()
2017-01-18 04:33:30 +01:00
, chatWidget(parent)
, scrollbar(this)
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));
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-18 04:33:30 +01:00
auto c = this->chatWidget->getChannel();
2017-01-11 01:08:20 +01:00
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 *)
{
2017-01-18 04:33:30 +01:00
this->scrollbar.resize(this->scrollbar.width(), height());
this->scrollbar.move(width() - this->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-18 04:33:30 +01:00
auto c = this->chatWidget->getChannel();
2017-01-05 16:07:20 +01:00
2017-01-18 01:04:54 +01:00
QColor color;
ColorScheme &scheme = ColorScheme::instance();
// code for tesing colors
/*
static ConcurrentMap<qreal, QImage *> imgCache;
std::function<QImage *(qreal)> getImg = [&scheme](qreal light) {
return imgCache.getOrAdd(light, [&scheme, &light] {
QImage *img = new QImage(150, 50, QImage::Format_RGB32);
QColor color;
for (int j = 0; j < 50; j++) {
for (qreal i = 0; i < 150; i++) {
color = QColor::fromHslF(i / 150.0, light, j / 50.0);
scheme.normalizeColor(color);
img->setPixelColor(i, j, color);
}
}
return img;
});
};
for (qreal k = 0; k < 4.8; k++) {
auto img = getImg(k / 5);
painter.drawImage(QRect(k * 150, 0, 150, 150), *img);
}
painter.fillRect(QRect(0, 9, 500, 2), QColor(0, 0, 0));*/
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-18 04:33:30 +01:00
int start = this->scrollbar.getValue();
2017-01-11 01:08:20 +01:00
2017-01-18 04:33:30 +01:00
if (start >= messages.length()) {
return;
}
int y = -(messages[start].get()->getHeight() *
2017-01-18 14:48:42 +01:00
(fmod(this->scrollbar.getValue(), 1)));
2017-01-18 04:33:30 +01:00
for (int i = start; i < messages.size(); ++i) {
Message *message = messages[i].get();
for (WordPart const &wordPart : message->getWordParts()) {
2017-01-13 18:59:11 +01:00
painter.setPen(QColor(255, 0, 0));
2017-01-18 04:33:30 +01:00
painter.drawRect(wordPart.getX(), wordPart.getY() + y,
wordPart.getWidth(), wordPart.getHeight());
2017-01-13 18:59:11 +01:00
2017-01-11 01:08:20 +01:00
// image
2017-01-18 04:33:30 +01:00
if (wordPart.getWord().isImage()) {
LazyLoadedImage &lli = wordPart.getWord().getImage();
2017-01-11 01:08:20 +01:00
2017-01-18 04:33:30 +01:00
const QPixmap *image = lli.getPixmap();
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-18 04:33:30 +01:00
QRect(wordPart.getX(), wordPart.getY() + y,
wordPart.getWidth(), wordPart.getHeight()),
2017-01-11 18:52:09 +01:00
*image);
2017-01-11 01:08:20 +01:00
}
}
// text
2017-01-11 18:52:09 +01:00
else {
2017-01-18 04:33:30 +01:00
QColor color = wordPart.getWord().getColor();
2017-01-18 01:04:54 +01:00
painter.setPen(color);
2017-01-18 04:33:30 +01:00
painter.setFont(wordPart.getWord().getFont());
2017-01-11 18:52:09 +01:00
2017-01-13 18:59:11 +01:00
painter.drawText(
2017-01-18 04:33:30 +01:00
QRectF(wordPart.getX(), wordPart.getY() + y, 10000, 10000),
wordPart.getText(),
QTextOption(Qt::AlignLeft | Qt::AlignTop));
2017-01-11 01:08:20 +01:00
}
}
2017-01-07 20:43:55 +01:00
2017-01-18 04:33:30 +01:00
y += message->getHeight();
2017-01-11 01:08:20 +01:00
}
2017-01-05 16:07:20 +01:00
}