mirror-chatterino2/src/widgets/chatwidgetheader.cpp

224 lines
7.1 KiB
C++
Raw Normal View History

2017-06-11 09:31:45 +02:00
#include "widgets/chatwidgetheader.hpp"
#include "colorscheme.hpp"
#include "twitch/twitchchannel.hpp"
2017-09-15 17:23:49 +02:00
#include "util/urlfetch.hpp"
2017-06-11 09:31:45 +02:00
#include "widgets/chatwidget.hpp"
#include "widgets/notebookpage.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
{
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, &RippleEffectLabel::clicked, this,
2017-06-11 09:11:55 +02:00
&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");
this->initializeChannelSignals();
this->chatWidget->channelChanged.connect([this]() {
this->initializeChannelSignals(); //
});
}
void ChatWidgetHeader::initializeChannelSignals()
{
// Disconnect any previous signal first
this->onlineStatusChangedConnection.disconnect();
auto channel = this->chatWidget->getChannel();
twitch::TwitchChannel *twitchChannel = dynamic_cast<twitch::TwitchChannel *>(channel.get());
if (twitchChannel) {
twitchChannel->onlineStatusChanged.connect([this]() {
this->updateChannelText(); //
});
}
2017-01-15 16:38:30 +01:00
}
2017-09-22 00:50:43 +02:00
void ChatWidgetHeader::resizeEvent(QResizeEvent *event)
{
this->setFixedHeight(static_cast<float>(32 * getDpiMultiplier()));
}
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 {
auto channel = this->chatWidget->getChannel();
twitch::TwitchChannel *twitchChannel = dynamic_cast<twitch::TwitchChannel *>(channel.get());
2017-09-22 00:50:43 +02:00
if (twitchChannel != nullptr && twitchChannel->isLive) {
this->channelNameLabel.setText(QString::fromStdString(channelName) + " (live)");
this->setToolTip("<style>.center { text-align: center; }</style>"
"<p class = \"center\">" +
2017-09-22 00:50:43 +02:00
twitchChannel->streamStatus + "<br><br>" + twitchChannel->streamGame +
"<br>"
"Live for " +
twitchChannel->streamUptime + " with " +
twitchChannel->streamViewerCount +
" viewers"
"</p>");
2017-09-15 17:23:49 +02:00
} 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) {
2017-09-12 22:10:30 +02:00
this->chatWidget->doChangeChannel();
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
{
QTimer::singleShot(80, [&] {
2017-09-15 17:23:49 +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
{
}
} // namespace widgets
} // namespace chatterino