mirror-chatterino2/src/widgets/splits/Split.cpp

723 lines
20 KiB
C++
Raw Normal View History

#include "widgets/splits/Split.hpp"
2018-06-26 15:33:51 +02:00
#include "common/Common.hpp"
2018-07-15 14:11:46 +02:00
#include "common/NetworkRequest.hpp"
2018-11-14 17:26:08 +01:00
#include "controllers/accounts/AccountController.hpp"
#include "debug/Log.hpp"
2018-06-26 14:09:39 +02:00
#include "providers/twitch/EmoteValue.hpp"
#include "providers/twitch/TwitchChannel.hpp"
#include "providers/twitch/TwitchMessageBuilder.hpp"
#include "providers/twitch/TwitchServer.hpp"
2018-06-28 19:46:45 +02:00
#include "singletons/Settings.hpp"
2018-06-28 20:03:04 +02:00
#include "singletons/Theme.hpp"
2018-06-26 14:09:39 +02:00
#include "singletons/WindowManager.hpp"
2018-12-02 17:49:15 +01:00
#include "util/Shortcut.hpp"
2018-06-26 14:09:39 +02:00
#include "util/StreamLink.hpp"
#include "widgets/Notebook.hpp"
2018-06-26 17:20:03 +02:00
#include "widgets/Window.hpp"
2018-06-26 15:11:45 +02:00
#include "widgets/dialogs/QualityPopup.hpp"
#include "widgets/dialogs/SelectChannelDialog.hpp"
#include "widgets/dialogs/TextInputDialog.hpp"
#include "widgets/dialogs/UserInfoPopup.hpp"
#include "widgets/helper/ChannelView.hpp"
2018-06-26 17:20:03 +02:00
#include "widgets/helper/DebugPopup.hpp"
2018-10-09 19:43:29 +02:00
#include "widgets/helper/NotebookTab.hpp"
#include "widgets/helper/ResizingTextEdit.hpp"
2018-06-26 17:20:03 +02:00
#include "widgets/helper/SearchPopup.hpp"
2018-10-09 19:43:29 +02:00
#include "widgets/splits/ClosedSplits.hpp"
2018-06-26 17:20:03 +02:00
#include "widgets/splits/SplitContainer.hpp"
#include "widgets/splits/SplitHeader.hpp"
#include "widgets/splits/SplitInput.hpp"
2018-06-26 17:20:03 +02:00
#include "widgets/splits/SplitOverlay.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>
2018-07-15 14:11:46 +02:00
#include <QJsonArray>
#include <QLabel>
#include <QListWidget>
#include <QMimeData>
#include <QMovie>
2017-01-17 00:15:44 +01:00
#include <QPainter>
#include <QVBoxLayout>
2016-12-29 17:31:07 +01:00
#include <functional>
#include <random>
2017-04-14 17:52:22 +02:00
namespace chatterino {
namespace {
void showTutorialVideo(QWidget *parent, const QString &source,
const QString &title, const QString &description)
{
auto window =
2019-09-08 21:45:46 +02:00
new BaseWindow(BaseWindow::Flags::EnableCustomFrame, parent);
window->setWindowTitle("Chatterino - " + title);
window->setAttribute(Qt::WA_DeleteOnClose);
auto layout = new QVBoxLayout();
layout->addWidget(new QLabel(description));
auto label = new QLabel(window);
layout->addWidget(label);
auto movie = new QMovie(label);
movie->setFileName(source);
label->setMovie(movie);
movie->start();
window->getLayoutContainer()->setLayout(layout);
window->show();
}
} // namespace
2018-05-10 23:58:07 +02:00
pajlada::Signals::Signal<Qt::KeyboardModifiers> Split::modifierStatusChanged;
Qt::KeyboardModifiers Split::modifierStatus = Qt::NoModifier;
2018-04-28 22:00:05 +02:00
Split::Split(SplitContainer *parent)
2018-06-24 11:45:30 +02:00
: Split(static_cast<QWidget *>(parent))
2018-04-28 22:00:05 +02:00
{
2018-07-06 19:23:47 +02:00
this->container_ = parent;
2018-04-28 22:00:05 +02:00
}
Split::Split(QWidget *parent)
: BaseWidget(parent)
2018-07-06 19:23:47 +02:00
, container_(nullptr)
, channel_(Channel::getEmpty())
, vbox_(new QVBoxLayout(this))
, header_(new SplitHeader(this))
, view_(new ChannelView(this))
, input_(new SplitInput(this))
2018-07-06 19:23:47 +02:00
, overlay_(new SplitOverlay(this))
2016-12-29 17:31:07 +01:00
{
this->setMouseTracking(true);
this->view_->setPausable(true);
this->view_->setFocusPolicy(Qt::FocusPolicy::NoFocus);
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
2018-08-08 15:35:54 +02:00
createShortcut(this, "CTRL+W", &Split::deleteFromContainer);
// CTRL+R: Change Channel
2018-08-08 15:35:54 +02:00
createShortcut(this, "CTRL+R", &Split::changeChannel);
2017-11-12 17:21:50 +01:00
2018-01-05 13:42:23 +01:00
// CTRL+F: Search
2018-08-08 15:35:54 +02:00
createShortcut(this, "CTRL+F", &Split::showSearch);
2018-01-05 13:42:23 +01:00
2018-11-14 17:26:08 +01:00
// F5: reload emotes
createShortcut(this, "F5", &Split::reloadChannelAndSubscriberEmotes);
2018-12-09 12:44:37 +01:00
// F10
2018-07-06 17:02:26 +02:00
createShortcut(this, "F10", [] {
2018-04-06 16:37:30 +02:00
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);
this->input_->ui_.textEdit->installEventFilter(parent);
this->view_->mouseDown.connect([this](QMouseEvent *) { //
2018-06-04 21:44:03 +02:00
this->giveFocus(Qt::MouseFocusReason);
});
this->view_->selectionChanged.connect([this]() {
2018-10-21 13:43:02 +02:00
if (view_->hasSelection())
{
this->input_->clearSelection();
}
});
this->view_->joinToChannel.connect([this](QString twitchChannel) {
this->container_->appendNewSplit(false)->setChannel(
2018-09-30 18:55:41 +02:00
getApp()->twitch.server->getOrAddChannel(twitchChannel));
});
this->input_->textChanged.connect([=](const QString &newText) {
2018-10-21 13:43:02 +02:00
if (getSettings()->showEmptyInput)
{
return;
}
2018-10-21 13:43:02 +02:00
if (newText.length() == 0)
{
this->input_->hide();
2018-10-21 13:43:02 +02:00
}
else if (this->input_->isHidden())
{
this->input_->show();
}
});
2018-08-08 15:35:54 +02:00
getSettings()->showEmptyInput.connect(
2018-07-05 18:17:12 +02:00
[this](const bool &showEmptyInput, auto) {
2018-10-21 13:43:02 +02:00
if (!showEmptyInput && this->input_->getInputText().length() == 0)
{
this->input_->hide();
2018-10-21 13:43:02 +02:00
}
else
{
this->input_->show();
2018-01-02 02:15:11 +01:00
}
},
2018-07-06 19:23:47 +02:00
this->managedConnections_);
2018-01-17 16:52:51 +01:00
this->header_->updateModerationModeIcon();
2018-07-06 19:23:47 +02:00
this->overlay_->hide();
2018-08-06 21:17:03 +02:00
this->setSizePolicy(QSizePolicy::MinimumExpanding,
QSizePolicy::MinimumExpanding);
2019-08-11 21:08:18 +02:00
this->managedConnect(modifierStatusChanged, [this](Qt::KeyboardModifiers
status) {
if ((status ==
showSplitOverlayModifiers /*|| status == showAddSplitRegions*/) &&
2018-10-21 13:43:02 +02:00
this->isMouseOver_)
2019-08-11 21:08:18 +02:00
{
this->overlay_->show();
}
else
{
this->overlay_->hide();
}
2019-09-16 10:40:02 +02:00
if (getSettings()->pauseChatModifier.getEnum() != Qt::NoModifier &&
status == getSettings()->pauseChatModifier.getEnum())
{
this->view_->pause(PauseReason::KeyboardModifier);
}
else
{
this->view_->unpause(PauseReason::KeyboardModifier);
}
2019-08-11 21:08:18 +02:00
});
2018-05-25 14:57:17 +02:00
this->input_->ui_.textEdit->focused.connect(
2018-08-06 21:17:03 +02:00
[this] { this->focused.invoke(); });
this->input_->ui_.textEdit->focusLost.connect(
2018-08-06 21:17:03 +02:00
[this] { this->focusLost.invoke(); });
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-07-06 19:23:47 +02:00
this->usermodeChangedConnection_.disconnect();
this->roomModeChangedConnection_.disconnect();
this->channelIDChangedConnection_.disconnect();
this->indirectChannelChangedConnection_.disconnect();
2016-12-29 17:31:07 +01:00
}
2018-05-10 19:50:31 +02:00
ChannelView &Split::getChannelView()
{
return *this->view_;
2018-05-10 19:50:31 +02:00
}
SplitContainer *Split::getContainer()
{
2018-07-06 19:23:47 +02:00
return this->container_;
2018-05-10 19:50:31 +02:00
}
bool Split::isInContainer() const
{
2018-07-06 19:23:47 +02:00
return this->container_ != nullptr;
}
2018-07-06 19:23:47 +02:00
void Split::setContainer(SplitContainer *container)
2018-05-10 23:58:07 +02:00
{
2018-07-06 19:23:47 +02:00
this->container_ = container;
2018-05-10 23:58:07 +02:00
}
2018-04-20 19:54:45 +02:00
IndirectChannel Split::getIndirectChannel()
2017-04-12 17:46:44 +02:00
{
2018-07-06 19:23:47 +02:00
return this->channel_;
2017-04-12 17:46:44 +02:00
}
2018-04-20 19:54:45 +02:00
ChannelPtr Split::getChannel()
2017-04-12 17:46:44 +02:00
{
2018-07-06 19:23:47 +02:00
return this->channel_.get();
2018-04-20 19:54:45 +02:00
}
void Split::setChannel(IndirectChannel newChannel)
{
2018-07-06 19:23:47 +02:00
this->channel_ = newChannel;
this->view_->setChannel(newChannel.get());
2018-07-06 19:23:47 +02:00
this->usermodeChangedConnection_.disconnect();
this->roomModeChangedConnection_.disconnect();
this->indirectChannelChangedConnection_.disconnect();
2018-01-17 18:36:12 +01:00
2018-04-20 19:54:45 +02:00
TwitchChannel *tc = dynamic_cast<TwitchChannel *>(newChannel.get().get());
2018-01-17 18:36:12 +01:00
2018-10-21 13:43:02 +02:00
if (tc != nullptr)
{
2018-07-06 19:23:47 +02:00
this->usermodeChangedConnection_ = tc->userStateChanged.connect([this] {
this->header_->updateModerationModeIcon();
this->header_->updateRoomModes();
});
2018-05-24 08:58:34 +02:00
2018-08-06 21:17:03 +02:00
this->roomModeChangedConnection_ = tc->roomModesChanged.connect(
[this] { this->header_->updateRoomModes(); });
2018-01-17 18:36:12 +01:00
}
2018-08-06 21:17:03 +02:00
this->indirectChannelChangedConnection_ =
newChannel.getChannelChanged().connect([this] { //
QTimer::singleShot(0, [this] { this->setChannel(this->channel_); });
});
2018-04-20 19:54:45 +02:00
this->header_->updateModerationModeIcon();
this->header_->updateChannelText();
this->header_->updateRoomModes();
2018-01-17 18:36:12 +01:00
this->channelChanged.invoke();
// Queue up save because: Split channel changed
getApp()->windows->queueSave();
2017-04-12 17:46:44 +02:00
}
2018-01-17 16:52:51 +01:00
void Split::setModerationMode(bool value)
{
this->moderationMode_ = value;
this->header_->updateModerationModeIcon();
2018-11-03 21:26:57 +01:00
this->view_->queueLayout();
2018-01-17 16:52:51 +01:00
}
bool Split::getModerationMode() const
{
2018-07-06 19:23:47 +02:00
return this->moderationMode_;
2018-01-17 16:52:51 +01:00
}
void Split::insertTextToInput(const QString &text)
{
this->input_->insertText(text);
}
2018-04-18 09:12:29 +02:00
void Split::showChangeChannelPopup(const char *dialogTitle, bool empty,
std::function<void(bool)> callback)
2017-01-17 00:15:44 +01:00
{
2018-10-21 13:43:02 +02:00
if (this->selectChannelDialog_.hasElement())
{
2018-07-06 19:23:47 +02:00
this->selectChannelDialog_->raise();
2018-06-24 11:45:30 +02:00
return;
}
2018-12-02 17:49:15 +01:00
auto dialog = new SelectChannelDialog(this);
2018-10-21 13:43:02 +02:00
if (!empty)
{
dialog->setSelectedChannel(this->getIndirectChannel());
2017-01-17 00:15:44 +01:00
}
2018-04-18 09:12:29 +02:00
dialog->setAttribute(Qt::WA_DeleteOnClose);
dialog->show();
dialog->closed.connect([=] {
2018-10-21 13:43:02 +02:00
if (dialog->hasSeletedChannel())
{
2018-04-18 09:12:29 +02:00
this->setChannel(dialog->getSelectedChannel());
2018-10-21 13:43:02 +02:00
if (this->isInContainer())
{
2018-10-13 14:20:06 +02:00
this->container_->refreshTab();
}
2018-04-18 09:12:29 +02:00
}
2018-04-18 09:12:29 +02:00
callback(dialog->hasSeletedChannel());
2018-07-06 19:23:47 +02:00
this->selectChannelDialog_ = nullptr;
2018-04-18 09:12:29 +02:00
});
2018-07-06 19:23:47 +02:00
this->selectChannelDialog_ = dialog;
2017-01-17 00:15:44 +01: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();
2018-01-23 22:48:33 +01:00
}
2017-11-12 17:21:50 +01:00
void Split::giveFocus(Qt::FocusReason reason)
{
this->input_->ui_.textEdit->setFocus(reason);
}
2017-11-12 17:21:50 +01:00
bool Split::hasFocus() const
{
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-07-06 17:11:37 +02:00
painter.fillRect(this->rect(), this->theme->splits.background);
2016-12-29 17:31:07 +01:00
}
2017-01-29 11:38:00 +01:00
void Split::mouseMoveEvent(QMouseEvent *event)
{
2018-05-31 16:02:20 +02:00
this->handleModifiers(QGuiApplication::queryKeyboardModifiers());
}
void Split::keyPressEvent(QKeyEvent *event)
{
this->view_->unsetCursor();
2018-05-31 16:02:20 +02:00
this->handleModifiers(QGuiApplication::queryKeyboardModifiers());
}
void Split::keyReleaseEvent(QKeyEvent *event)
{
this->view_->unsetCursor();
2018-05-31 16:02:20 +02:00
this->handleModifiers(QGuiApplication::queryKeyboardModifiers());
}
void Split::resizeEvent(QResizeEvent *event)
{
// Queue up save because: Split resized
getApp()->windows->queueSave();
BaseWidget::resizeEvent(event);
2018-07-06 19:23:47 +02:00
this->overlay_->setGeometry(this->rect());
}
void Split::enterEvent(QEvent *event)
{
2018-07-06 19:23:47 +02:00
this->isMouseOver_ = true;
2018-05-10 23:58:07 +02:00
2018-05-31 16:02:20 +02:00
this->handleModifiers(QGuiApplication::queryKeyboardModifiers());
2018-05-10 23:58:07 +02:00
2018-10-21 13:43:02 +02:00
if (modifierStatus ==
showSplitOverlayModifiers /*|| modifierStatus == showAddSplitRegions*/)
{
2018-07-06 19:23:47 +02:00
this->overlay_->show();
}
2018-10-21 13:43:02 +02:00
if (this->container_ != nullptr)
{
2018-07-06 19:23:47 +02:00
this->container_->resetMouseStatus();
}
}
void Split::leaveEvent(QEvent *event)
{
2018-07-06 19:23:47 +02:00
this->isMouseOver_ = false;
2018-07-06 19:23:47 +02:00
this->overlay_->hide();
2018-05-31 16:02:20 +02:00
this->handleModifiers(QGuiApplication::queryKeyboardModifiers());
}
2018-05-31 16:02:20 +02:00
void Split::focusInEvent(QFocusEvent *event)
{
this->giveFocus(event->reason());
}
void Split::handleModifiers(Qt::KeyboardModifiers modifiers)
{
2018-10-21 13:43:02 +02:00
if (modifierStatus != modifiers)
{
2018-05-10 23:58:07 +02:00
modifierStatus = modifiers;
modifierStatusChanged.invoke(modifiers);
}
2018-05-10 19:50:31 +02:00
}
2018-09-04 21:39:54 +02:00
void Split::setIsTopRightSplit(bool value)
{
this->isTopRightSplit_ = value;
this->header_->setAddButtonVisible(value);
}
/// Slots
2018-08-08 15:35:54 +02:00
void Split::addSibling()
{
2018-10-21 13:43:02 +02:00
if (this->container_)
{
2018-07-06 19:23:47 +02:00
this->container_->appendNewSplit(true);
}
}
2018-08-08 15:35:54 +02:00
void Split::deleteFromContainer()
{
2018-10-21 13:43:02 +02:00
if (this->container_)
{
2018-07-06 19:23:47 +02:00
this->container_->deleteSplit(this);
2018-10-09 19:43:29 +02:00
auto *tab = this->getContainer()->getTab();
tab->connect(tab, &QWidget::destroyed,
[tab]() mutable { ClosedSplits::invalidateTab(tab); });
ClosedSplits::push({this->getChannel()->getName(), tab});
}
}
2018-08-08 15:35:54 +02:00
void Split::changeChannel()
{
2018-04-18 09:12:29 +02:00
this->showChangeChannelPopup("Change channel", false, [](bool) {});
2018-06-24 11:45:30 +02:00
2017-09-15 17:23:49 +02:00
auto popup = this->findChildren<QDockWidget *>();
2018-10-21 13:43:02 +02:00
if (popup.size() && popup.at(0)->isVisible() && !popup.at(0)->isFloating())
{
2017-09-12 22:10:30 +02:00
popup.at(0)->hide();
2018-08-02 14:23:27 +02:00
showViewerList();
2017-09-12 22:10:30 +02:00
}
}
void Split::explainMoving()
{
showTutorialVideo(this, ":/examples/moving.gif", "Moving",
"Hold <Ctrl+Alt> to move splits.\n\nExample:");
}
void Split::explainSplitting()
{
showTutorialVideo(this, ":/examples/splitting.gif", "Splitting",
"Hold <Ctrl+Alt> to add new splits.\n\nExample:");
}
2018-08-08 15:35:54 +02:00
void Split::popup()
2017-06-11 09:11:55 +02:00
{
auto app = getApp();
Window &window = app->windows->createWindow(WindowType::Popup);
2017-11-12 17:21:50 +01:00
2018-08-06 21:17:03 +02:00
Split *split = new Split(static_cast<SplitContainer *>(
window.getNotebook().getOrAddSelectedPage()));
2017-11-12 17:21:50 +01:00
split->setChannel(this->getIndirectChannel());
2018-05-10 19:50:31 +02:00
window.getNotebook().getOrAddSelectedPage()->appendSplit(split);
2017-11-12 17:21:50 +01:00
window.show();
2017-06-11 09:11:55 +02:00
}
2018-08-08 15:35:54 +02:00
void Split::clear()
2017-06-11 09:11:55 +02:00
{
this->view_->clearMessages();
2017-06-11 09:11:55 +02:00
}
2018-08-02 14:23:27 +02:00
void Split::openInBrowser()
2017-06-11 09:11:55 +02:00
{
2018-08-02 14:23:27 +02:00
auto channel = this->getChannel();
2018-01-17 03:18:47 +01:00
2018-10-21 13:43:02 +02:00
if (auto twitchChannel = dynamic_cast<TwitchChannel *>(channel.get()))
{
2018-08-06 21:17:03 +02:00
QDesktopServices::openUrl("https://twitch.tv/" +
twitchChannel->getName());
2018-01-17 03:18:47 +01:00
}
2017-06-11 09:11:55 +02:00
}
2018-08-08 15:35:54 +02:00
void Split::openBrowserPlayer()
2017-06-11 09:11:55 +02:00
{
2018-08-02 14:23:27 +02:00
ChannelPtr channel = this->getChannel();
2018-10-21 13:43:02 +02:00
if (auto twitchChannel = dynamic_cast<TwitchChannel *>(channel.get()))
{
2018-08-06 21:17:03 +02:00
QDesktopServices::openUrl("https://player.twitch.tv/?channel=" +
twitchChannel->getName());
2018-01-17 03:18:47 +01:00
}
2017-06-11 09:11:55 +02:00
}
2018-08-02 14:23:27 +02:00
void Split::openInStreamlink()
{
2018-10-21 13:43:02 +02:00
try
{
2018-08-02 14:23:27 +02:00
openStreamlinkForChannel(this->getChannel()->getName());
2018-10-21 13:43:02 +02:00
}
catch (const Exception &ex)
{
2018-08-11 14:20:53 +02:00
log("Error in doOpenStreamlink: {}", ex.what());
}
}
2018-08-02 14:23:27 +02:00
void Split::showViewerList()
{
2017-09-15 17:23:49 +02:00
auto viewerDock = new QDockWidget("Viewer List", this);
viewerDock->setAllowedAreas(Qt::LeftDockWidgetArea);
viewerDock->setFeatures(QDockWidget::DockWidgetVerticalTitleBar |
2018-08-06 21:17:03 +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());
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 = {"Broadcaster", "VIPs", "Moderators",
"Staff", "Admins", "Global Moderators",
"Viewers"};
static QStringList jsonLabels = {"broadcaster", "vips", "moderators",
"staff", "admins", "global_mods",
"viewers"};
2017-09-15 17:23:49 +02:00
QList<QListWidgetItem *> labelList;
2018-10-21 13:43:02 +02:00
for (auto &x : labels)
{
auto label = new QListWidgetItem(x);
2018-07-06 17:11:37 +02:00
label->setBackgroundColor(this->theme->splits.header.background);
labelList.append(label);
}
auto loadingLabel = new QLabel("Loading...");
2019-08-20 21:50:36 +02:00
NetworkRequest::twitchRequest("https://tmi.twitch.tv/group/user/" +
this->getChannel()->getName() + "/chatters")
.caller(this)
.onSuccess([=](auto result) -> Outcome {
auto obj = result.parseJson();
QJsonObject chattersObj = obj.value("chatters").toObject();
loadingLabel->hide();
for (int i = 0; i < jsonLabels.size(); i++)
{
auto currentCategory =
chattersObj.value(jsonLabels.at(i)).toArray();
// If current category of chatters is empty, dont show this
// category.
if (currentCategory.empty())
continue;
chattersList->addItem(labelList.at(i));
foreach (const QJsonValue &v, currentCategory)
chattersList->addItem(v.toString());
}
2019-08-20 21:50:36 +02:00
return Success;
})
.execute();
searchBar->setPlaceholderText("Search User...");
2017-09-15 17:23:49 +02:00
QObject::connect(searchBar, &QLineEdit::textEdited, this, [=]() {
auto query = searchBar->text();
2018-10-21 13:43:02 +02:00
if (!query.isEmpty())
{
2017-09-15 17:23:49 +02:00
auto results = chattersList->findItems(query, Qt::MatchStartsWith);
chattersList->hide();
resultList->clear();
2018-10-21 13:43:02 +02:00
for (auto &item : results)
{
2018-08-06 21:17:03 +02:00
if (!labels.contains(item->text()))
resultList->addItem(item->text());
}
resultList->show();
2018-10-21 13:43:02 +02:00
}
else
{
resultList->hide();
chattersList->show();
}
});
2017-09-15 17:23:49 +02:00
QObject::connect(viewerDock, &QDockWidget::topLevelChanged, this,
[=]() { viewerDock->setMinimumWidth(300); });
auto listDoubleClick = [=](QString userName) {
2018-10-21 13:43:02 +02:00
if (!labels.contains(userName))
{
this->view_->showUserInfoPopup(userName);
2017-09-12 22:10:30 +02:00
}
};
QObject::connect(chattersList, &QListWidget::doubleClicked, this, [=]() {
listDoubleClick(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, [=]() {
listDoubleClick(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-07-06 17:11:37 +02:00
multiWidget->setStyleSheet(this->theme->splits.input.styleSheet);
multiWidget->setLayout(dockVbox);
viewerDock->setWidget(multiWidget);
viewerDock->setFloating(true);
viewerDock->show();
viewerDock->activateWindow();
}
void Split::openSubPage()
{
ChannelPtr channel = this->getChannel();
if (auto twitchChannel = dynamic_cast<TwitchChannel *>(channel.get()))
{
QDesktopServices::openUrl(twitchChannel->subscriptionUrl());
}
}
2018-08-02 14:23:27 +02:00
void Split::copyToClipboard()
2017-09-12 19:06:16 +02:00
{
QApplication::clipboard()->setText(this->view_->getSelectedText());
2017-09-12 19:06:16 +02:00
}
2018-08-08 15:35:54 +02:00
void Split::showSearch()
2018-01-05 13:42:23 +01:00
{
SearchPopup *popup = new SearchPopup();
popup->setChannel(this->getChannel());
popup->show();
}
void Split::reloadChannelAndSubscriberEmotes()
{
getApp()->accounts->twitch.getCurrent()->loadEmotes();
auto channel = this->getChannel();
if (auto twitchChannel = dynamic_cast<TwitchChannel *>(channel.get()))
{
twitchChannel->refreshBTTVChannelEmotes();
twitchChannel->refreshFFZChannelEmotes();
}
}
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);
}
void Split::drag()
{
2018-10-21 13:43:02 +02:00
if (auto container = dynamic_cast<SplitContainer *>(this->parentWidget()))
{
SplitContainer::isDraggingSplit = true;
SplitContainer::draggingSplit = this;
2018-05-10 19:50:31 +02:00
auto originalLocation = container->releaseSplit(this);
2018-08-02 14:23:27 +02:00
auto drag = new QDrag(this);
auto mimeData = new QMimeData;
mimeData->setData("chatterino/split", "xD");
drag->setMimeData(mimeData);
2018-10-21 13:43:02 +02:00
if (drag->exec(Qt::MoveAction) == Qt::IgnoreAction)
{
2018-08-02 14:23:27 +02:00
container->insertSplit(this, originalLocation);
}
SplitContainer::isDraggingSplit = false;
}
}
2017-04-14 17:52:22 +02:00
} // namespace chatterino