mirror-chatterino2/src/widgets/split.cpp

521 lines
14 KiB
C++
Raw Normal View History

2017-11-12 17:21:50 +01:00
#include "widgets/split.hpp"
2018-02-05 15:11:50 +01:00
#include "providers/twitch/emotevalue.hpp"
#include "providers/twitch/twitchchannel.hpp"
#include "providers/twitch/twitchmessagebuilder.hpp"
#include "providers/twitch/twitchserver.hpp"
2017-12-31 00:50:07 +01:00
#include "singletons/settingsmanager.hpp"
#include "singletons/thememanager.hpp"
#include "singletons/windowmanager.hpp"
#include "util/streamlink.hpp"
#include "util/urlfetch.hpp"
2018-04-06 16:37:30 +02:00
#include "widgets/helper/debugpopup.hpp"
2018-01-05 13:42:23 +01:00
#include "widgets/helper/searchpopup.hpp"
#include "widgets/helper/shortcut.hpp"
#include "widgets/qualitypopup.hpp"
2017-11-12 17:21:50 +01:00
#include "widgets/splitcontainer.hpp"
2017-09-15 17:23:49 +02:00
#include "widgets/textinputdialog.hpp"
2017-11-12 17:21:50 +01:00
#include "widgets/window.hpp"
2017-01-17 00:15:44 +01:00
2017-09-12 19:06:16 +02:00
#include <QApplication>
#include <QClipboard>
2018-01-17 03:18:47 +01:00
#include <QDesktopServices>
#include <QDockWidget>
#include <QDrag>
#include <QListWidget>
#include <QMimeData>
2017-01-17 00:15:44 +01:00
#include <QPainter>
#include <QVBoxLayout>
2016-12-29 17:31:07 +01:00
#include <functional>
#include <random>
2018-02-05 15:11:50 +01:00
using namespace chatterino::providers::twitch;
2017-04-14 17:52:22 +02:00
using namespace chatterino::messages;
2017-04-12 17:46:44 +02:00
2017-04-14 17:52:22 +02:00
namespace chatterino {
namespace widgets {
2017-01-18 21:30:23 +01:00
Split::Split(SplitContainer *parent)
: BaseWidget(parent)
, parentPage(*parent)
2018-02-05 15:11:50 +01:00
, channel(Channel::getEmpty())
, vbox(this)
, header(this)
2017-12-17 02:18:13 +01:00
, view(this)
, input(this)
2017-11-12 17:21:50 +01:00
, flexSizeX(1)
, flexSizeY(1)
2018-01-17 16:52:51 +01:00
, moderationMode(false)
2016-12-29 17:31:07 +01:00
{
this->setMouseTracking(true);
this->vbox.setSpacing(0);
this->vbox.setMargin(1);
2017-01-01 02:30:42 +01:00
this->vbox.addWidget(&this->header);
this->vbox.addWidget(&this->view, 1);
this->vbox.addWidget(&this->input);
// Initialize chat widget-wide hotkeys
// CTRL+W: Close Split
CreateShortcut(this, "CTRL+W", &Split::doCloseSplit);
// CTRL+R: Change Channel
CreateShortcut(this, "CTRL+R", &Split::doChangeChannel);
2017-11-12 17:21:50 +01:00
2018-01-05 13:42:23 +01:00
// CTRL+F: Search
CreateShortcut(this, "CTRL+F", &Split::doSearch);
2018-01-05 13:42:23 +01:00
2018-04-06 16:37:30 +02:00
// F12
CreateShortcut(this, "F10", [] {
auto *popup = new DebugPopup;
popup->setAttribute(Qt::WA_DeleteOnClose);
popup->show();
});
2017-11-12 17:21:50 +01:00
// xd
// CreateShortcut(this, "ALT+SHIFT+RIGHT", &Split::doIncFlexX);
// CreateShortcut(this, "ALT+SHIFT+LEFT", &Split::doDecFlexX);
// CreateShortcut(this, "ALT+SHIFT+UP", &Split::doIncFlexY);
// CreateShortcut(this, "ALT+SHIFT+DOWN", &Split::doDecFlexY);
2018-01-25 20:49:49 +01:00
this->input.ui.textEdit->installEventFilter(parent);
2017-09-16 16:49:52 +02:00
this->view.mouseDown.connect([this](QMouseEvent *) { this->giveFocus(Qt::MouseFocusReason); });
2017-09-21 02:20:02 +02:00
this->view.selectionChanged.connect([this]() {
if (view.hasSelection()) {
this->input.clearSelection();
}
});
this->input.textChanged.connect([this](const QString &newText) {
2017-12-31 22:58:35 +01:00
if (!singletons::SettingManager::getInstance().hideEmptyInput) {
return;
}
if (newText.length() == 0) {
this->input.hide();
} else if (this->input.isHidden()) {
this->input.show();
}
});
2018-01-02 02:15:11 +01:00
singletons::SettingManager::getInstance().hideEmptyInput.connect(
[this](const bool &hideEmptyInput, auto) {
if (hideEmptyInput && this->input.getInputText().length() == 0) {
this->input.hide();
} else {
this->input.show();
}
});
2018-01-17 16:52:51 +01:00
this->header.updateModerationModeIcon();
2017-01-01 02:30:42 +01:00
}
2017-11-12 17:21:50 +01:00
Split::~Split()
2017-01-01 02:30:42 +01:00
{
2018-01-17 18:36:12 +01:00
this->usermodeChangedConnection.disconnect();
this->channelIDChangedConnection.disconnect();
2016-12-29 17:31:07 +01:00
}
ChannelPtr Split::getChannel() const
2017-04-12 17:46:44 +02:00
{
return this->channel;
2017-04-12 17:46:44 +02:00
}
void Split::setChannel(ChannelPtr _newChannel)
2017-04-12 17:46:44 +02:00
{
this->view.setChannel(_newChannel);
2018-01-17 18:36:12 +01:00
this->usermodeChangedConnection.disconnect();
this->channel = _newChannel;
2018-02-05 15:11:50 +01:00
TwitchChannel *tc = dynamic_cast<TwitchChannel *>(_newChannel.get());
2018-01-17 18:36:12 +01:00
if (tc != nullptr) {
this->usermodeChangedConnection =
tc->userStateChanged.connect([this] { this->header.updateModerationModeIcon(); });
}
this->header.updateModerationModeIcon();
this->header.updateChannelText();
2018-01-17 18:36:12 +01:00
this->channelChanged.invoke();
2017-04-12 17:46:44 +02:00
}
2017-11-12 17:21:50 +01:00
void Split::setFlexSizeX(double x)
{
this->flexSizeX = x;
this->parentPage.updateFlexValues();
}
double Split::getFlexSizeX()
{
return this->flexSizeX;
}
void Split::setFlexSizeY(double y)
{
this->flexSizeY = y;
this->parentPage.updateFlexValues();
}
double Split::getFlexSizeY()
{
return this->flexSizeY;
}
2018-01-17 16:52:51 +01:00
void Split::setModerationMode(bool value)
{
if (value != this->moderationMode) {
this->moderationMode = value;
this->header.updateModerationModeIcon();
this->view.layoutMessages();
2018-01-17 16:52:51 +01:00
}
}
bool Split::getModerationMode() const
{
return this->moderationMode;
}
2017-11-12 17:21:50 +01:00
bool Split::showChangeChannelPopup(const char *dialogTitle, bool empty)
2017-01-17 00:15:44 +01:00
{
2017-04-12 17:46:44 +02:00
// create new input dialog and execute it
2017-01-17 00:15:44 +01:00
TextInputDialog dialog(this);
dialog.setWindowTitle(dialogTitle);
if (!empty) {
dialog.setText(this->channel->name);
dialog.highlightText();
}
2017-01-17 00:15:44 +01:00
if (dialog.exec() == QDialog::Accepted) {
QString newChannelName = dialog.getText().trimmed();
this->setChannel(providers::twitch::TwitchServer::getInstance().addChannel(newChannelName));
this->parentPage.refreshTitle();
return true;
2017-01-17 00:15:44 +01:00
}
return false;
2017-01-17 00:15:44 +01:00
}
2017-11-12 17:21:50 +01:00
void Split::layoutMessages()
2017-04-12 17:46:44 +02:00
{
this->view.layoutMessages();
2017-04-12 17:46:44 +02:00
}
2017-11-12 17:21:50 +01:00
void Split::updateGifEmotes()
2017-04-12 17:46:44 +02:00
{
this->view.queueUpdate();
2017-04-12 17:46:44 +02:00
}
2018-01-23 22:48:33 +01:00
void Split::updateLastReadMessage()
{
this->view.updateLastReadMessage();
}
2017-11-12 17:21:50 +01:00
void Split::giveFocus(Qt::FocusReason reason)
{
2018-01-25 20:49:49 +01:00
this->input.ui.textEdit->setFocus(reason);
}
2017-11-12 17:21:50 +01:00
bool Split::hasFocus() const
{
2018-01-25 20:49:49 +01:00
return this->input.ui.textEdit->hasFocus();
}
2017-11-12 17:21:50 +01:00
void Split::paintEvent(QPaintEvent *)
2016-12-29 17:31:07 +01:00
{
2017-04-12 17:46:44 +02:00
// color the background of the chat
2017-01-11 18:52:09 +01:00
QPainter painter(this);
2016-12-29 17:31:07 +01:00
2018-01-02 02:15:11 +01:00
painter.fillRect(this->rect(), this->themeManager.splits.background);
2016-12-29 17:31:07 +01:00
}
2017-01-29 11:38:00 +01:00
void Split::mouseMoveEvent(QMouseEvent *event)
{
this->handleModifiers(event, event->modifiers());
}
void Split::mousePressEvent(QMouseEvent *event)
{
if (event->buttons() == Qt::LeftButton && event->modifiers() & Qt::AltModifier) {
this->drag();
}
}
void Split::keyPressEvent(QKeyEvent *event)
{
this->view.unsetCursor();
this->handleModifiers(event, event->modifiers());
}
void Split::keyReleaseEvent(QKeyEvent *event)
{
this->view.unsetCursor();
this->handleModifiers(event, event->modifiers());
}
void Split::handleModifiers(QEvent *event, Qt::KeyboardModifiers modifiers)
{
if (modifiers == Qt::AltModifier) {
this->setCursor(Qt::SizeAllCursor);
event->accept();
// } else if (modifiers == Qt::ControlModifier) {
// this->setCursor(Qt::SplitHCursor);
// event->accept();
} else {
this->setCursor(Qt::ArrowCursor);
}
}
/// Slots
2017-11-12 17:21:50 +01:00
void Split::doAddSplit()
{
2017-11-12 17:21:50 +01:00
SplitContainer *page = static_cast<SplitContainer *>(this->parentWidget());
page->addChat(true);
}
2017-11-12 17:21:50 +01:00
void Split::doCloseSplit()
{
2017-11-12 17:21:50 +01:00
SplitContainer *page = static_cast<SplitContainer *>(this->parentWidget());
page->removeFromLayout(this);
deleteLater();
}
2017-11-12 17:21:50 +01:00
void Split::doChangeChannel()
{
this->showChangeChannelPopup("Change channel");
2017-09-15 17:23:49 +02:00
auto popup = this->findChildren<QDockWidget *>();
if (popup.size() && popup.at(0)->isVisible() && !popup.at(0)->isFloating()) {
2017-09-12 22:10:30 +02:00
popup.at(0)->hide();
doOpenViewerList();
}
}
2017-11-12 17:21:50 +01:00
void Split::doPopup()
2017-06-11 09:11:55 +02:00
{
Window &window = singletons::WindowManager::getInstance().createWindow(Window::Popup);
2017-11-12 17:21:50 +01:00
Split *split =
new Split(static_cast<SplitContainer *>(window.getNotebook().getOrAddSelectedPage()));
2017-11-12 17:21:50 +01:00
split->setChannel(this->getChannel());
window.getNotebook().getOrAddSelectedPage()->addToLayout(split);
2017-11-12 17:21:50 +01:00
window.show();
2017-06-11 09:11:55 +02:00
}
2017-11-12 17:21:50 +01:00
void Split::doClearChat()
2017-06-11 09:11:55 +02:00
{
this->view.clearMessages();
2017-06-11 09:11:55 +02:00
}
2017-11-12 17:21:50 +01:00
void Split::doOpenChannel()
2017-06-11 09:11:55 +02:00
{
ChannelPtr _channel = this->channel;
2018-02-05 15:11:50 +01:00
TwitchChannel *tc = dynamic_cast<TwitchChannel *>(_channel.get());
2018-01-17 03:18:47 +01:00
if (tc != nullptr) {
QDesktopServices::openUrl("https://twitch.tv/" + tc->name);
}
2017-06-11 09:11:55 +02:00
}
2017-11-12 17:21:50 +01:00
void Split::doOpenPopupPlayer()
2017-06-11 09:11:55 +02:00
{
ChannelPtr _channel = this->channel;
2018-02-05 15:11:50 +01:00
TwitchChannel *tc = dynamic_cast<TwitchChannel *>(_channel.get());
2018-01-17 03:18:47 +01:00
if (tc != nullptr) {
QDesktopServices::openUrl("https://player.twitch.tv/?channel=" + tc->name);
}
2017-06-11 09:11:55 +02:00
}
2017-11-12 17:21:50 +01:00
void Split::doOpenStreamlink()
{
try {
streamlink::Start(this->channel->name);
} catch (const streamlink::Exception &ex) {
debug::Log("Error in doOpenStreamlink: {}", ex.what());
}
}
2017-11-12 17:21:50 +01:00
void Split::doOpenViewerList()
{
2017-09-15 17:23:49 +02:00
auto viewerDock = new QDockWidget("Viewer List", this);
viewerDock->setAllowedAreas(Qt::LeftDockWidgetArea);
viewerDock->setFeatures(QDockWidget::DockWidgetVerticalTitleBar |
2017-09-15 17:23:49 +02:00
QDockWidget::DockWidgetClosable | QDockWidget::DockWidgetFloatable);
viewerDock->resize(0.5 * this->width(),
this->height() - this->header.height() - this->input.height());
viewerDock->move(0, this->header.height());
2017-09-12 22:10:30 +02:00
auto accountPopup = new AccountPopupWidget(this->channel);
accountPopup->setAttribute(Qt::WA_DeleteOnClose);
auto multiWidget = new QWidget(viewerDock);
auto dockVbox = new QVBoxLayout(viewerDock);
auto searchBar = new QLineEdit(viewerDock);
auto chattersList = new QListWidget();
auto resultList = new QListWidget();
static QStringList labels = {"Moderators", "Staff", "Admins", "Global Moderators", "Viewers"};
static QStringList jsonLabels = {"moderators", "staff", "admins", "global_mods", "viewers"};
2017-09-15 17:23:49 +02:00
QList<QListWidgetItem *> labelList;
for (auto &x : labels) {
auto label = new QListWidgetItem(x);
2018-01-02 02:15:11 +01:00
label->setBackgroundColor(this->themeManager.splits.header.background);
labelList.append(label);
}
auto loadingLabel = new QLabel("Loading...");
util::twitch::get("https://tmi.twitch.tv/group/user/" + channel->name + "/chatters", this,
[=](QJsonObject obj) {
QJsonObject chattersObj = obj.value("chatters").toObject();
loadingLabel->hide();
for (int i = 0; i < jsonLabels.size(); i++) {
chattersList->addItem(labelList.at(i));
foreach (const QJsonValue &v,
chattersObj.value(jsonLabels.at(i)).toArray())
chattersList->addItem(v.toString());
}
});
searchBar->setPlaceholderText("Search User...");
2017-09-15 17:23:49 +02:00
QObject::connect(searchBar, &QLineEdit::textEdited, this, [=]() {
auto query = searchBar->text();
2017-09-15 17:23:49 +02:00
if (!query.isEmpty()) {
auto results = chattersList->findItems(query, Qt::MatchStartsWith);
chattersList->hide();
resultList->clear();
2017-09-15 17:23:49 +02:00
for (auto &item : results) {
if (!labels.contains(item->text()))
resultList->addItem(item->text());
}
resultList->show();
2017-09-15 17:23:49 +02:00
} else {
resultList->hide();
chattersList->show();
}
});
2017-09-15 17:23:49 +02:00
QObject::connect(viewerDock, &QDockWidget::topLevelChanged, this,
[=]() { viewerDock->setMinimumWidth(300); });
2017-09-15 17:23:49 +02:00
QObject::connect(chattersList, &QListWidget::doubleClicked, this, [=]() {
if (!labels.contains(chattersList->currentItem()->text())) {
doOpenAccountPopupWidget(accountPopup, chattersList->currentItem()->text());
2017-09-12 22:10:30 +02:00
}
});
2017-09-15 17:23:49 +02:00
QObject::connect(resultList, &QListWidget::doubleClicked, this, [=]() {
if (!labels.contains(resultList->currentItem()->text())) {
doOpenAccountPopupWidget(accountPopup, resultList->currentItem()->text());
2017-09-12 22:10:30 +02:00
}
});
dockVbox->addWidget(searchBar);
dockVbox->addWidget(loadingLabel);
dockVbox->addWidget(chattersList);
dockVbox->addWidget(resultList);
resultList->hide();
2018-01-02 02:15:11 +01:00
multiWidget->setStyleSheet(this->themeManager.splits.input.styleSheet);
multiWidget->setLayout(dockVbox);
viewerDock->setWidget(multiWidget);
viewerDock->show();
}
2017-11-12 17:21:50 +01:00
void Split::doOpenAccountPopupWidget(AccountPopupWidget *widget, QString user)
2017-09-12 22:10:30 +02:00
{
widget->setName(user);
widget->show();
widget->setFocus();
widget->moveTo(this, QCursor::pos());
2017-09-12 22:10:30 +02:00
}
2017-11-12 17:21:50 +01:00
void Split::doCopy()
2017-09-12 19:06:16 +02:00
{
QApplication::clipboard()->setText(this->view.getSelectedText());
}
2018-01-05 13:42:23 +01:00
void Split::doSearch()
{
SearchPopup *popup = new SearchPopup();
popup->setChannel(this->getChannel());
popup->show();
}
template <typename Iter, typename RandomGenerator>
static Iter select_randomly(Iter start, Iter end, RandomGenerator &g)
{
std::uniform_int_distribution<> dis(0, std::distance(start, end) - 1);
std::advance(start, dis(g));
return start;
}
template <typename Iter>
static Iter select_randomly(Iter start, Iter end)
{
static std::random_device rd;
static std::mt19937 gen(rd());
return select_randomly(start, end, gen);
}
2017-11-12 17:21:50 +01:00
void Split::doIncFlexX()
{
this->setFlexSizeX(this->getFlexSizeX() * 1.2);
}
void Split::doDecFlexX()
{
this->setFlexSizeX(this->getFlexSizeX() * (1 / 1.2));
}
void Split::doIncFlexY()
{
this->setFlexSizeY(this->getFlexSizeY() * 1.2);
}
void Split::doDecFlexY()
{
this->setFlexSizeY(this->getFlexSizeY() * (1 / 1.2));
}
void Split::drag()
{
auto container = dynamic_cast<SplitContainer *>(this->parentWidget());
if (container != nullptr) {
SplitContainer::isDraggingSplit = true;
SplitContainer::draggingSplit = this;
auto originalLocation = container->removeFromLayout(this);
QDrag *drag = new QDrag(this);
QMimeData *mimeData = new QMimeData;
mimeData->setData("chatterino/split", "xD");
drag->setMimeData(mimeData);
Qt::DropAction dropAction = drag->exec(Qt::MoveAction);
if (dropAction == Qt::IgnoreAction) {
container->addToLayout(this, originalLocation);
}
SplitContainer::isDraggingSplit = false;
}
}
2017-04-14 17:52:22 +02:00
} // namespace widgets
} // namespace chatterino