mirror-chatterino2/src/widgets/chatwidgetheader.cpp

225 lines
7.4 KiB
C++
Raw Normal View History

2017-06-11 09:31:45 +02:00
#include "widgets/chatwidgetheader.hpp"
#include "colorscheme.hpp"
#include "widgets/chatwidget.hpp"
#include "widgets/notebookpage.hpp"
#include "util/urlfetch.hpp"
2017-01-01 02:30:42 +01:00
2017-01-15 16:38:30 +01:00
#include <QByteArray>
#include <QDrag>
#include <QMimeData>
#include <QPainter>
2017-04-14 17:52:22 +02:00
namespace chatterino {
namespace widgets {
2017-01-18 21:30:23 +01:00
2017-06-10 22:48:28 +02:00
ChatWidgetHeader::ChatWidgetHeader(ChatWidget *_chatWidget)
: BaseWidget(_chatWidget)
2017-06-10 22:48:28 +02:00
, chatWidget(_chatWidget)
, leftLabel(this)
2017-06-11 09:11:55 +02:00
, leftMenu(this)
, rightLabel(this)
2017-06-11 09:11:55 +02:00
, rightMenu(this)
2017-01-01 02:30:42 +01:00
{
2017-06-11 09:11:55 +02:00
this->setFixedHeight(32);
2017-01-15 16:38:30 +01:00
this->refreshTheme();
2017-06-11 09:11:55 +02:00
this->updateChannelText();
2017-01-15 16:38:30 +01:00
2017-06-11 09:11:55 +02:00
this->setLayout(&this->hbox);
this->hbox.setMargin(0);
this->hbox.addWidget(&this->leftLabel);
this->hbox.addWidget(&this->channelNameLabel, 1);
this->hbox.addWidget(&this->rightLabel);
2017-01-15 16:38:30 +01:00
// left
2017-06-11 09:11:55 +02:00
this->leftLabel.getLabel().setTextFormat(Qt::RichText);
this->leftLabel.getLabel().setText("<img src=':/images/tool_moreCollapser_off16.png' />");
connect(&this->leftLabel, &ChatWidgetHeaderButton::clicked, this,
&ChatWidgetHeader::leftButtonClicked);
this->leftMenu.addAction("Add new split", this->chatWidget, &ChatWidget::doAddSplit,
QKeySequence(tr("Ctrl+T")));
this->leftMenu.addAction("Close split", this->chatWidget, &ChatWidget::doCloseSplit,
QKeySequence(tr("Ctrl+W")));
this->leftMenu.addAction("Move split", this, SLOT(menuMoveSplit()));
this->leftMenu.addAction("Popup", this->chatWidget, &ChatWidget::doPopup);
this->leftMenu.addAction("Open viewer list", this->chatWidget, &ChatWidget::doOpenViewerList);
2017-06-11 09:11:55 +02:00
this->leftMenu.addSeparator();
this->leftMenu.addAction("Change channel", this->chatWidget, &ChatWidget::doChangeChannel,
QKeySequence(tr("Ctrl+R")));
this->leftMenu.addAction("Clear chat", this->chatWidget, &ChatWidget::doClearChat);
this->leftMenu.addAction("Open channel", this->chatWidget, &ChatWidget::doOpenChannel);
this->leftMenu.addAction("Open popup player", this->chatWidget, &ChatWidget::doOpenPopupPlayer);
this->leftMenu.addAction("Open in Streamlink", this->chatWidget, &ChatWidget::doOpenStreamlink);
2017-06-11 09:11:55 +02:00
this->leftMenu.addSeparator();
this->leftMenu.addAction("Reload channel emotes", this, SLOT(menuReloadChannelEmotes()));
this->leftMenu.addAction("Manual reconnect", this, SLOT(menuManualReconnect()));
this->leftMenu.addSeparator();
this->leftMenu.addAction("Show changelog", this, SLOT(menuShowChangelog()));
2017-01-15 16:38:30 +01:00
// middle
2017-06-11 09:11:55 +02:00
this->channelNameLabel.setAlignment(Qt::AlignCenter);
2017-06-11 09:11:55 +02:00
connect(&this->channelNameLabel, &SignalLabel::mouseDoubleClick, this,
2017-01-18 21:30:23 +01:00
&ChatWidgetHeader::mouseDoubleClickEvent);
2017-01-15 16:38:30 +01:00
// right
2017-06-11 09:11:55 +02:00
this->rightLabel.setMinimumWidth(this->height());
this->rightLabel.getLabel().setTextFormat(Qt::RichText);
this->rightLabel.getLabel().setText("ayy");
QTimer *timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, checkLive);
timer->start(60000);
2017-01-15 16:38:30 +01:00
}
2017-04-12 17:46:44 +02:00
void ChatWidgetHeader::updateChannelText()
2017-01-17 00:15:44 +01:00
{
const std::string channelName = this->chatWidget->channelName;
if (channelName.empty()) {
this->channelNameLabel.setText("<no channel>");
} else {
if(this->chatWidget->getChannelRef()->isLive)
{
auto channel = this->chatWidget->getChannelRef();
this->channelNameLabel.setText(QString::fromStdString(channelName) + " (live)");
this->setToolTip("<style>.center { text-align: center; }</style>" \
"<p class = \"center\">" + \
channel->streamStatus + "<br><br>" + \
channel->streamGame + "<br>" \
"Live for " + channel->streamUptime + \
" with " + channel->streamViewerCount + " viewers" \
"</p>"
);
}
else
{
this->channelNameLabel.setText(QString::fromStdString(channelName));
this->setToolTip("");
}
}
2017-01-17 00:15:44 +01:00
}
2017-04-12 17:46:44 +02:00
void ChatWidgetHeader::paintEvent(QPaintEvent *)
2017-01-01 02:30:42 +01:00
{
QPainter painter(this);
painter.fillRect(rect(), this->colorScheme.ChatHeaderBackground);
painter.setPen(this->colorScheme.ChatHeaderBorder);
2017-01-01 02:30:42 +01:00
painter.drawRect(0, 0, width() - 1, height() - 1);
}
2017-04-12 17:46:44 +02:00
void ChatWidgetHeader::mousePressEvent(QMouseEvent *event)
2017-01-01 02:30:42 +01:00
{
2017-06-11 09:11:55 +02:00
this->dragging = true;
2017-01-01 02:30:42 +01:00
2017-06-11 09:11:55 +02:00
this->dragStart = event->pos();
2017-01-01 02:30:42 +01:00
}
2017-04-12 17:46:44 +02:00
void ChatWidgetHeader::mouseMoveEvent(QMouseEvent *event)
2017-01-01 02:30:42 +01:00
{
2017-06-11 09:11:55 +02:00
if (this->dragging) {
if (std::abs(this->dragStart.x() - event->pos().x()) > 12 ||
std::abs(this->dragStart.y() - event->pos().y()) > 12) {
2017-06-10 22:48:28 +02:00
auto page = static_cast<NotebookPage *>(this->chatWidget->parentWidget());
2017-01-01 02:30:42 +01:00
2017-06-10 22:48:28 +02:00
if (page != nullptr) {
2017-01-01 02:30:42 +01:00
NotebookPage::isDraggingSplit = true;
2017-06-10 22:48:28 +02:00
NotebookPage::draggingSplit = this->chatWidget;
2017-01-01 02:30:42 +01:00
2017-06-10 22:48:28 +02:00
auto originalLocation = page->removeFromLayout(this->chatWidget);
2017-01-01 02:30:42 +01:00
2017-01-26 07:10:46 +01:00
// page->update();
2017-06-10 22:48:28 +02:00
QDrag *drag = new QDrag(this->chatWidget);
2017-01-11 18:52:09 +01:00
QMimeData *mimeData = new QMimeData;
2017-01-01 02:30:42 +01:00
mimeData->setData("chatterino/split", "xD");
drag->setMimeData(mimeData);
Qt::DropAction dropAction = drag->exec(Qt::MoveAction);
2017-01-11 18:52:09 +01:00
if (dropAction == Qt::IgnoreAction) {
2017-06-10 22:48:28 +02:00
page->addToLayout(this->chatWidget, originalLocation);
2017-01-01 02:30:42 +01:00
}
NotebookPage::isDraggingSplit = false;
}
}
}
}
2017-04-12 17:46:44 +02:00
void ChatWidgetHeader::mouseDoubleClickEvent(QMouseEvent *event)
2017-01-17 00:15:44 +01:00
{
if (event->button() == Qt::LeftButton) {
this->chatWidget->showChangeChannelPopup("Change channel");
2017-01-17 00:15:44 +01:00
}
}
2017-04-12 17:46:44 +02:00
void ChatWidgetHeader::leftButtonClicked()
2017-01-15 16:38:30 +01:00
{
2017-06-11 09:11:55 +02:00
this->leftMenu.move(this->leftLabel.mapToGlobal(QPoint(0, this->leftLabel.height())));
this->leftMenu.show();
2017-01-15 16:38:30 +01:00
}
2017-04-12 17:46:44 +02:00
void ChatWidgetHeader::rightButtonClicked()
2017-01-15 16:38:30 +01:00
{
}
void ChatWidgetHeader::refreshTheme()
{
QPalette palette;
palette.setColor(QPalette::Foreground, this->colorScheme.Text);
this->leftLabel.setPalette(palette);
this->channelNameLabel.setPalette(palette);
this->rightLabel.setPalette(palette);
}
2017-04-12 17:46:44 +02:00
void ChatWidgetHeader::menuMoveSplit()
2017-01-15 16:38:30 +01:00
{
}
2017-04-12 17:46:44 +02:00
void ChatWidgetHeader::menuReloadChannelEmotes()
2017-01-15 16:38:30 +01:00
{
}
2017-04-12 17:46:44 +02:00
void ChatWidgetHeader::menuManualReconnect()
2017-01-15 16:38:30 +01:00
{
}
2017-04-12 17:46:44 +02:00
void ChatWidgetHeader::menuShowChangelog()
2017-01-15 16:38:30 +01:00
{
}
void ChatWidgetHeader::checkLive()
{
auto channel = this->chatWidget->getChannelRef();
auto id = QString::fromStdString(channel->roomID);
util::twitch::get("https://api.twitch.tv/kraken/streams/" + id,[=](QJsonObject obj){
if(obj.value("stream").isNull())
{
channel->isLive = false;
this->updateChannelText();
}
else
{
channel->isLive = true;
auto stream = obj.value("stream").toObject();
channel->streamViewerCount = QString::number(stream.value("viewers").toDouble());
channel->streamGame = stream.value("game").toString();
channel->streamStatus = stream.value("channel").toObject().value("status").toString();
QDateTime since = QDateTime::fromString(stream.value("created_at").toString(),Qt::ISODate);
auto diff = since.secsTo(QDateTime::currentDateTime());
channel->streamUptime = QString::number(diff/3600) + "h " + QString::number(diff % 3600 / 60) + "m";
this->updateChannelText();
}
});
}
} // namespace widgets
} // namespace chatterino