mirror-chatterino2/src/widgets/helper/splitheader.cpp

268 lines
8.7 KiB
C++
Raw Normal View History

2017-11-12 17:21:50 +01:00
#include "widgets/helper/splitheader.hpp"
2017-12-31 00:50:07 +01:00
#include "singletons/thememanager.hpp"
#include "twitch/twitchchannel.hpp"
2018-01-13 04:05:38 +01:00
#include "util/layoutcreator.hpp"
2017-09-15 17:23:49 +02:00
#include "util/urlfetch.hpp"
2017-11-12 17:21:50 +01:00
#include "widgets/split.hpp"
#include "widgets/splitcontainer.hpp"
#include "widgets/tooltipwidget.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
2018-01-13 04:05:38 +01:00
SplitHeader::SplitHeader(Split *_split)
: BaseWidget(_split)
, split(_split)
2017-01-01 02:30:42 +01:00
{
this->setMouseTracking(true);
2018-01-13 04:05:38 +01:00
util::LayoutCreator<SplitHeader> layoutCreator(this);
auto layout = layoutCreator.emplace<QHBoxLayout>().withoutMargin();
{
// dropdown label
auto dropdown = layout.emplace<RippleEffectLabel>(this).assign(&this->dropdownLabel);
dropdown->getLabel().setTextFormat(Qt::RichText);
dropdown->getLabel().setText("<img src=':/images/tool_moreCollapser_off16.png' />");
dropdown->getLabel().setScaledContents(true);
dropdown->setMouseTracking(true);
this->addDropdownItems(dropdown.getElement());
QObject::connect(dropdown.getElement(), &RippleEffectLabel::clicked, this, [this] {
QTimer::singleShot(80, [&] {
this->dropdownMenu.move(
this->dropdownLabel->mapToGlobal(QPoint(0, this->dropdownLabel->height())));
this->dropdownMenu.show();
});
});
// channel name label
auto title = layout.emplace<SignalLabel>().assign(&this->titleLabel);
title->setMouseTracking(true);
QSizePolicy policy;
policy.setHorizontalStretch(1);
policy.setHorizontalPolicy(QSizePolicy::Policy::MinimumExpanding);
2018-01-13 04:05:38 +01:00
title->setSizePolicy(policy);
// title->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
// layout->setStretch(1, 1000);
QObject::connect(this->titleLabel, &SignalLabel::mouseDoubleClick, this,
&SplitHeader::mouseDoubleClickEvent);
// moderation mode
auto moderation = layout.emplace<RippleEffectLabel>(this).assign(&this->moderationLabel);
moderation->setMouseTracking(true);
moderation->getLabel().setScaledContents(true);
moderation->getLabel().setTextFormat(Qt::RichText);
moderation->getLabel().setText("<img src=':/images/moderator_bg.png' />");
2018-01-13 04:05:38 +01:00
}
// ---- misc
this->layout()->setMargin(0);
this->refreshTheme();
2017-06-11 09:11:55 +02:00
this->updateChannelText();
2017-01-15 16:38:30 +01:00
2018-01-13 04:05:38 +01:00
// this->titleLabel.setAlignment(Qt::AlignCenter);
this->initializeChannelSignals();
2018-01-13 04:05:38 +01:00
this->split->channelChanged.connect([this]() {
this->initializeChannelSignals(); //
});
}
2018-01-13 04:05:38 +01:00
SplitHeader::~SplitHeader()
{
this->onlineStatusChangedConnection.disconnect();
}
void SplitHeader::addDropdownItems(RippleEffectLabel *label)
{
connect(this->dropdownLabel, &RippleEffectLabel::clicked, this,
&SplitHeader::leftButtonClicked);
// clang-format off
this->dropdownMenu.addAction("Add new split", this->split, &Split::doAddSplit, QKeySequence(tr("Ctrl+T")));
this->dropdownMenu.addAction("Close split", this->split, &Split::doCloseSplit, QKeySequence(tr("Ctrl+W")));
this->dropdownMenu.addAction("Move split", this, SLOT(menuMoveSplit()));
this->dropdownMenu.addAction("Popup", this->split, &Split::doPopup);
this->dropdownMenu.addAction("Open viewer list", this->split, &Split::doOpenViewerList);
this->dropdownMenu.addSeparator();
this->dropdownMenu.addAction("Change channel", this->split, &Split::doChangeChannel, QKeySequence(tr("Ctrl+R")));
this->dropdownMenu.addAction("Clear chat", this->split, &Split::doClearChat);
this->dropdownMenu.addAction("Open channel", this->split, &Split::doOpenChannel);
this->dropdownMenu.addAction("Open popup player", this->split, &Split::doOpenPopupPlayer);
this->dropdownMenu.addAction("Open in Streamlink", this->split, &Split::doOpenStreamlink);
this->dropdownMenu.addSeparator();
this->dropdownMenu.addAction("Reload channel emotes", this, SLOT(menuReloadChannelEmotes()));
this->dropdownMenu.addAction("Manual reconnect", this, SLOT(menuManualReconnect()));
this->dropdownMenu.addSeparator();
this->dropdownMenu.addAction("Show changelog", this, SLOT(menuShowChangelog()));
// clang-format on
}
2017-11-12 17:21:50 +01:00
void SplitHeader::initializeChannelSignals()
{
// Disconnect any previous signal first
this->onlineStatusChangedConnection.disconnect();
2018-01-13 04:05:38 +01:00
auto channel = this->split->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-11-12 17:21:50 +01:00
void SplitHeader::resizeEvent(QResizeEvent *event)
2017-09-22 00:50:43 +02:00
{
2018-01-13 04:05:38 +01:00
int w = 28 * getDpiMultiplier();
this->setFixedHeight(w);
this->dropdownLabel->setFixedWidth(w);
this->moderationLabel->setFixedWidth(w);
2017-09-22 00:50:43 +02:00
}
2017-11-12 17:21:50 +01:00
void SplitHeader::updateChannelText()
2017-01-17 00:15:44 +01:00
{
2018-01-13 04:05:38 +01:00
const std::string channelName = this->split->channelName;
if (channelName.empty()) {
2018-01-13 04:05:38 +01:00
this->titleLabel->setText("<no channel>");
} else {
2018-01-13 04:05:38 +01:00
auto channel = this->split->getChannel();
twitch::TwitchChannel *twitchChannel = dynamic_cast<twitch::TwitchChannel *>(channel.get());
2017-09-22 00:50:43 +02:00
if (twitchChannel != nullptr && twitchChannel->isLive) {
this->isLive = true;
this->tooltip = "<style>.center { text-align: center; }</style>"
"<p class = \"center\">" +
twitchChannel->streamStatus + "<br><br>" + twitchChannel->streamGame +
"<br>"
"Live for " +
twitchChannel->streamUptime + " with " +
2018-01-13 04:05:38 +01:00
twitchChannel->streamViewerCount +
" viewers"
"</p>";
this->titleLabel->setText(QString::fromStdString(channelName) + " (live)");
2017-09-15 17:23:49 +02:00
} else {
this->isLive = false;
2018-01-13 04:05:38 +01:00
this->titleLabel->setText(QString::fromStdString(channelName));
this->tooltip = "";
}
}
2017-01-17 00:15:44 +01:00
}
2017-11-12 17:21:50 +01:00
void SplitHeader::paintEvent(QPaintEvent *)
2017-01-01 02:30:42 +01:00
{
QPainter painter(this);
2018-01-02 02:15:11 +01:00
painter.fillRect(rect(), this->themeManager.splits.header.background);
painter.setPen(this->themeManager.splits.header.border);
2017-01-01 02:30:42 +01:00
painter.drawRect(0, 0, width() - 1, height() - 1);
}
2017-11-12 17:21:50 +01:00
void SplitHeader::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-11-12 17:21:50 +01:00
void SplitHeader::mouseMoveEvent(QMouseEvent *event)
2017-01-01 02:30:42 +01:00
{
if (!this->dragging && this->isLive) {
auto tooltipWidget = TooltipWidget::getInstance();
tooltipWidget->moveTo(event->globalPos());
tooltipWidget->setText(tooltip);
tooltipWidget->show();
}
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) {
2018-01-13 04:05:38 +01:00
auto page = static_cast<SplitContainer *>(this->split->parentWidget());
2017-01-01 02:30:42 +01:00
2017-06-10 22:48:28 +02:00
if (page != nullptr) {
2017-11-12 17:21:50 +01:00
SplitContainer::isDraggingSplit = true;
2018-01-13 04:05:38 +01:00
SplitContainer::draggingSplit = this->split;
2017-01-01 02:30:42 +01:00
2018-01-13 04:05:38 +01:00
auto originalLocation = page->removeFromLayout(this->split);
2017-01-01 02:30:42 +01:00
2017-01-26 07:10:46 +01:00
// page->update();
2018-01-13 04:05:38 +01:00
QDrag *drag = new QDrag(this->split);
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) {
2018-01-13 04:05:38 +01:00
page->addToLayout(this->split, originalLocation);
2017-01-01 02:30:42 +01:00
}
2017-11-12 17:21:50 +01:00
SplitContainer::isDraggingSplit = false;
this->dragging = false;
2017-01-01 02:30:42 +01:00
}
}
}
}
void SplitHeader::leaveEvent(QEvent *event)
{
TooltipWidget::getInstance()->hide();
BaseWidget::leaveEvent(event);
}
2017-11-12 17:21:50 +01:00
void SplitHeader::mouseDoubleClickEvent(QMouseEvent *event)
2017-01-17 00:15:44 +01:00
{
if (event->button() == Qt::LeftButton) {
2018-01-13 04:05:38 +01:00
this->split->doChangeChannel();
2017-01-17 00:15:44 +01:00
}
}
2017-11-12 17:21:50 +01:00
void SplitHeader::leftButtonClicked()
2017-01-15 16:38:30 +01:00
{
}
2017-11-12 17:21:50 +01:00
void SplitHeader::rightButtonClicked()
2017-01-15 16:38:30 +01:00
{
}
2017-11-12 17:21:50 +01:00
void SplitHeader::refreshTheme()
{
QPalette palette;
2018-01-02 02:15:11 +01:00
palette.setColor(QPalette::Foreground, this->themeManager.splits.header.text);
2018-01-13 04:05:38 +01:00
this->dropdownLabel->setPalette(palette);
this->titleLabel->setPalette(palette);
this->moderationLabel->setPalette(palette);
}
2017-11-12 17:21:50 +01:00
void SplitHeader::menuMoveSplit()
2017-01-15 16:38:30 +01:00
{
}
2017-11-12 17:21:50 +01:00
void SplitHeader::menuReloadChannelEmotes()
2017-01-15 16:38:30 +01:00
{
}
2017-11-12 17:21:50 +01:00
void SplitHeader::menuManualReconnect()
2017-01-15 16:38:30 +01:00
{
}
2017-11-12 17:21:50 +01:00
void SplitHeader::menuShowChangelog()
2017-01-15 16:38:30 +01:00
{
}
} // namespace widgets
} // namespace chatterino