mirror-chatterino2/src/widgets/dialogs/SelectChannelDialog.cpp

359 lines
12 KiB
C++
Raw Normal View History

2018-06-26 14:09:39 +02:00
#include "SelectChannelDialog.hpp"
2018-04-18 09:12:29 +02:00
2018-06-26 14:09:39 +02:00
#include "Application.hpp"
#include "providers/twitch/TwitchServer.hpp"
#include "singletons/Theme.hpp"
2018-06-26 14:09:39 +02:00
#include "util/LayoutCreator.hpp"
#include "widgets/Notebook.hpp"
#include "widgets/helper/NotebookTab.hpp"
2018-04-18 09:12:29 +02:00
#include <QDialogButtonBox>
2018-10-21 12:13:09 +02:00
#include <QFormLayout>
2018-04-18 09:12:29 +02:00
#include <QGroupBox>
#include <QLabel>
#include <QLineEdit>
#include <QVBoxLayout>
#define TAB_TWITCH 0
namespace chatterino {
2018-06-24 11:45:30 +02:00
SelectChannelDialog::SelectChannelDialog(QWidget *parent)
: BaseWindow(parent, BaseWindow::EnableCustomFrame)
2018-07-06 19:23:47 +02:00
, selectedChannel_(Channel::getEmpty())
2018-04-18 09:12:29 +02:00
{
2018-04-18 18:55:49 +02:00
this->setWindowTitle("Select a channel to join");
2018-07-06 19:23:47 +02:00
this->tabFilter_.dialog = this;
2018-04-18 09:12:29 +02:00
2018-06-26 17:06:17 +02:00
LayoutCreator<QWidget> layoutWidget(this->getLayoutContainer());
2018-04-18 09:12:29 +02:00
auto layout = layoutWidget.setLayoutType<QVBoxLayout>().withoutMargin();
2018-06-11 15:04:54 +02:00
auto notebook = layout.emplace<Notebook>(this).assign(&this->ui_.notebook);
2018-04-18 09:12:29 +02:00
// twitch
{
2018-06-26 17:06:17 +02:00
LayoutCreator<QWidget> obj(new QWidget());
2018-04-18 09:12:29 +02:00
auto vbox = obj.setLayoutType<QVBoxLayout>();
// channel_btn
2018-08-06 21:17:03 +02:00
auto channel_btn = vbox.emplace<QRadioButton>("Channel").assign(
&this->ui_.twitch.channel);
auto channel_lbl =
vbox.emplace<QLabel>("Join a twitch channel by its name.").hidden();
2018-04-18 09:12:29 +02:00
channel_lbl->setWordWrap(true);
2018-08-06 21:17:03 +02:00
auto channel_edit = vbox.emplace<QLineEdit>().hidden().assign(
&this->ui_.twitch.channelName);
2018-04-18 09:12:29 +02:00
QObject::connect(channel_btn.getElement(), &QRadioButton::toggled,
[=](bool enabled) mutable {
2018-10-21 13:43:02 +02:00
if (enabled)
{
channel_edit->setFocus();
2018-08-06 21:17:03 +02:00
channel_edit->setSelection(
0, channel_edit->text().length());
}
2018-04-18 09:12:29 +02:00
channel_edit->setVisible(enabled);
channel_lbl->setVisible(enabled);
});
2018-04-18 09:12:29 +02:00
2018-07-06 19:23:47 +02:00
channel_btn->installEventFilter(&this->tabFilter_);
channel_edit->installEventFilter(&this->tabFilter_);
2018-04-18 09:12:29 +02:00
// whispers_btn
2018-08-06 21:17:03 +02:00
auto whispers_btn = vbox.emplace<QRadioButton>("Whispers")
.assign(&this->ui_.twitch.whispers);
2018-04-18 09:12:29 +02:00
auto whispers_lbl =
2018-08-06 21:17:03 +02:00
vbox.emplace<QLabel>("Shows the whispers that you receive while "
"chatterino is running.")
2018-04-18 09:12:29 +02:00
.hidden();
whispers_lbl->setWordWrap(true);
2018-07-06 19:23:47 +02:00
whispers_btn->installEventFilter(&this->tabFilter_);
2018-04-18 09:12:29 +02:00
2018-08-06 21:17:03 +02:00
QObject::connect(
whispers_btn.getElement(), &QRadioButton::toggled,
[=](bool enabled) mutable { whispers_lbl->setVisible(enabled); });
2018-04-18 09:12:29 +02:00
// mentions_btn
2018-08-06 21:17:03 +02:00
auto mentions_btn = vbox.emplace<QRadioButton>("Mentions")
.assign(&this->ui_.twitch.mentions);
2018-04-18 09:12:29 +02:00
auto mentions_lbl =
2018-08-06 21:17:03 +02:00
vbox.emplace<QLabel>("Shows all the messages that highlight you "
"from any channel.")
2018-04-18 09:12:29 +02:00
.hidden();
mentions_lbl->setWordWrap(true);
2018-07-06 19:23:47 +02:00
mentions_btn->installEventFilter(&this->tabFilter_);
2018-04-18 09:12:29 +02:00
2018-08-06 21:17:03 +02:00
QObject::connect(
mentions_btn.getElement(), &QRadioButton::toggled,
[=](bool enabled) mutable { mentions_lbl->setVisible(enabled); });
2018-04-18 09:12:29 +02:00
// watching_btn
2018-08-06 21:17:03 +02:00
auto watching_btn = vbox.emplace<QRadioButton>("Watching")
.assign(&this->ui_.twitch.watching);
2018-04-18 09:12:29 +02:00
auto watching_lbl =
2018-08-06 21:17:03 +02:00
vbox.emplace<QLabel>("Requires the chatterino browser extension.")
.hidden();
2018-04-18 09:12:29 +02:00
watching_lbl->setWordWrap(true);
2018-07-06 19:23:47 +02:00
watching_btn->installEventFilter(&this->tabFilter_);
2018-04-18 09:12:29 +02:00
2018-08-06 21:17:03 +02:00
QObject::connect(
watching_btn.getElement(), &QRadioButton::toggled,
[=](bool enabled) mutable { watching_lbl->setVisible(enabled); });
2018-04-18 09:12:29 +02:00
vbox->addStretch(1);
// tabbing order
2018-08-06 21:17:03 +02:00
QWidget::setTabOrder(watching_btn.getElement(),
channel_btn.getElement());
QWidget::setTabOrder(channel_btn.getElement(),
whispers_btn.getElement());
QWidget::setTabOrder(whispers_btn.getElement(),
mentions_btn.getElement());
QWidget::setTabOrder(mentions_btn.getElement(),
watching_btn.getElement());
2018-04-18 09:12:29 +02:00
// tab
2018-10-21 12:13:09 +02:00
auto tab = notebook->addPage(obj.getElement());
2018-06-01 16:01:49 +02:00
tab->setCustomTitle("Twitch");
2018-04-18 09:12:29 +02:00
}
// irc
2018-10-21 12:13:09 +02:00
/*
{
LayoutCreator<QWidget> obj(new QWidget());
auto vbox = obj.setLayoutType<QVBoxLayout>();
auto form = vbox.emplace<QFormLayout>();
form->addRow(new QLabel("User name:"), new QLineEdit());
form->addRow(new QLabel("First nick choice:"), new QLineEdit());
form->addRow(new QLabel("Second nick choice:"), new QLineEdit());
form->addRow(new QLabel("Third nick choice:"), new QLineEdit());
auto tab = notebook->addPage(obj.getElement());
tab->setCustomTitle("Irc");
}
*/
2018-04-18 09:12:29 +02:00
layout->setStretchFactor(notebook.getElement(), 1);
2018-04-18 09:12:29 +02:00
2018-08-06 21:17:03 +02:00
auto buttons =
layout.emplace<QHBoxLayout>().emplace<QDialogButtonBox>(this);
2018-04-18 09:12:29 +02:00
{
auto *button_ok = buttons->addButton(QDialogButtonBox::Ok);
2018-08-06 21:17:03 +02:00
QObject::connect(button_ok, &QPushButton::clicked,
[=](bool) { this->ok(); });
2018-04-18 09:12:29 +02:00
auto *button_cancel = buttons->addButton(QDialogButtonBox::Cancel);
2018-08-06 21:17:03 +02:00
QObject::connect(button_cancel, &QAbstractButton::clicked,
[=](bool) { this->close(); });
2018-04-18 09:12:29 +02:00
}
2018-10-21 12:13:09 +02:00
this->setScaleIndependantSize(300, 310);
2018-06-11 15:31:27 +02:00
this->ui_.notebook->selectIndex(TAB_TWITCH);
this->ui_.twitch.channel->setFocus();
2018-04-18 09:12:29 +02:00
// Shortcuts
auto *shortcut_ok = new QShortcut(QKeySequence("Return"), this);
QObject::connect(shortcut_ok, &QShortcut::activated, [=] { this->ok(); });
auto *shortcut_cancel = new QShortcut(QKeySequence("Esc"), this);
2018-08-06 21:17:03 +02:00
QObject::connect(shortcut_cancel, &QShortcut::activated,
[=] { this->close(); });
2018-04-18 09:12:29 +02:00
}
void SelectChannelDialog::ok()
{
2018-07-06 19:23:47 +02:00
this->hasSelectedChannel_ = true;
2018-04-18 09:12:29 +02:00
this->close();
}
2018-04-20 19:54:45 +02:00
void SelectChannelDialog::setSelectedChannel(IndirectChannel _channel)
2018-04-18 09:12:29 +02:00
{
2018-04-20 19:54:45 +02:00
auto channel = _channel.get();
2018-04-18 09:12:29 +02:00
assert(channel);
2018-07-06 19:23:47 +02:00
this->selectedChannel_ = channel;
2018-04-18 09:12:29 +02:00
2018-10-21 13:43:02 +02:00
switch (_channel.getType())
{
case Channel::Type::Twitch:
{
2018-06-11 15:04:54 +02:00
this->ui_.notebook->selectIndex(TAB_TWITCH);
this->ui_.twitch.channel->setFocus();
2018-08-02 14:23:27 +02:00
this->ui_.twitch.channelName->setText(channel->getName());
2018-10-21 13:43:02 +02:00
}
break;
case Channel::Type::TwitchWatching:
{
2018-06-11 15:04:54 +02:00
this->ui_.notebook->selectIndex(TAB_TWITCH);
this->ui_.twitch.watching->setFocus();
2018-10-21 13:43:02 +02:00
}
break;
case Channel::Type::TwitchMentions:
{
2018-06-11 15:04:54 +02:00
this->ui_.notebook->selectIndex(TAB_TWITCH);
this->ui_.twitch.mentions->setFocus();
2018-10-21 13:43:02 +02:00
}
break;
case Channel::Type::TwitchWhispers:
{
2018-06-11 15:04:54 +02:00
this->ui_.notebook->selectIndex(TAB_TWITCH);
this->ui_.twitch.whispers->setFocus();
2018-10-21 13:43:02 +02:00
}
break;
default:
{
2018-06-11 15:04:54 +02:00
this->ui_.notebook->selectIndex(TAB_TWITCH);
this->ui_.twitch.channel->setFocus();
2018-04-18 09:12:29 +02:00
}
}
2018-07-06 19:23:47 +02:00
this->hasSelectedChannel_ = false;
2018-04-18 09:12:29 +02:00
}
2018-04-20 19:54:45 +02:00
IndirectChannel SelectChannelDialog::getSelectedChannel() const
2018-04-18 09:12:29 +02:00
{
2018-10-21 13:43:02 +02:00
if (!this->hasSelectedChannel_)
{
2018-07-06 19:23:47 +02:00
return this->selectedChannel_;
2018-04-18 09:12:29 +02:00
}
auto app = getApp();
2018-10-21 13:43:02 +02:00
switch (this->ui_.notebook->getSelectedIndex())
{
case TAB_TWITCH:
{
if (this->ui_.twitch.channel->isChecked())
{
2018-08-06 21:17:03 +02:00
return app->twitch.server->getOrAddChannel(
this->ui_.twitch.channelName->text().trimmed());
2018-10-21 13:43:02 +02:00
}
else if (this->ui_.twitch.watching->isChecked())
{
return app->twitch.server->watchingChannel;
2018-10-21 13:43:02 +02:00
}
else if (this->ui_.twitch.mentions->isChecked())
{
return app->twitch.server->mentionsChannel;
2018-10-21 13:43:02 +02:00
}
else if (this->ui_.twitch.whispers->isChecked())
{
return app->twitch.server->whispersChannel;
2018-04-18 09:12:29 +02:00
}
}
}
2018-07-06 19:23:47 +02:00
return this->selectedChannel_;
2018-04-18 09:12:29 +02:00
}
bool SelectChannelDialog::hasSeletedChannel() const
{
2018-07-06 19:23:47 +02:00
return this->hasSelectedChannel_;
2018-04-18 09:12:29 +02:00
}
2018-08-06 21:17:03 +02:00
bool SelectChannelDialog::EventFilter::eventFilter(QObject *watched,
QEvent *event)
2018-04-18 09:12:29 +02:00
{
auto *widget = (QWidget *)watched;
2018-10-21 13:43:02 +02:00
if (event->type() == QEvent::FocusIn)
{
2018-04-18 09:12:29 +02:00
widget->grabKeyboard();
auto *radio = dynamic_cast<QRadioButton *>(watched);
2018-10-21 13:43:02 +02:00
if (radio)
{
2018-04-18 09:12:29 +02:00
radio->setChecked(true);
}
return true;
2018-10-21 13:43:02 +02:00
}
else if (event->type() == QEvent::FocusOut)
{
2018-04-18 09:12:29 +02:00
widget->releaseKeyboard();
return false;
2018-10-21 13:43:02 +02:00
}
else if (event->type() == QEvent::KeyPress)
{
2018-04-18 09:12:29 +02:00
QKeyEvent *event_key = static_cast<QKeyEvent *>(event);
2018-08-06 21:17:03 +02:00
if ((event_key->key() == Qt::Key_Tab ||
event_key->key() == Qt::Key_Down) &&
2018-10-21 13:43:02 +02:00
event_key->modifiers() == Qt::NoModifier)
{
if (widget == this->dialog->ui_.twitch.channelName)
{
2018-06-11 15:04:54 +02:00
this->dialog->ui_.twitch.whispers->setFocus();
2018-04-18 09:12:29 +02:00
return true;
2018-10-21 13:43:02 +02:00
}
else
{
2018-04-18 09:12:29 +02:00
widget->nextInFocusChain()->setFocus();
}
return true;
2018-10-21 13:43:02 +02:00
}
else if (((event_key->key() == Qt::Key_Tab ||
event_key->key() == Qt::Key_Backtab) &&
event_key->modifiers() == Qt::ShiftModifier) ||
((event_key->key() == Qt::Key_Up) &&
event_key->modifiers() == Qt::NoModifier))
{
if (widget == this->dialog->ui_.twitch.channelName)
{
2018-06-11 15:04:54 +02:00
this->dialog->ui_.twitch.watching->setFocus();
2018-04-18 09:12:29 +02:00
return true;
2018-10-21 13:43:02 +02:00
}
else if (widget == this->dialog->ui_.twitch.whispers)
{
2018-06-11 15:04:54 +02:00
this->dialog->ui_.twitch.channel->setFocus();
2018-04-18 09:12:29 +02:00
return true;
}
widget->previousInFocusChain()->setFocus();
return true;
2018-10-21 13:43:02 +02:00
}
else
{
2018-04-18 09:12:29 +02:00
return false;
}
2018-10-21 13:43:02 +02:00
}
else if (event->type() == QEvent::KeyRelease)
{
2018-04-18 09:12:29 +02:00
QKeyEvent *event_key = static_cast<QKeyEvent *>(event);
2018-08-06 21:17:03 +02:00
if ((event_key->key() == Qt::Key_Backtab ||
event_key->key() == Qt::Key_Down) &&
2018-10-21 13:43:02 +02:00
event_key->modifiers() == Qt::NoModifier)
{
2018-04-18 09:12:29 +02:00
return true;
}
}
return false;
}
2018-04-18 09:33:05 +02:00
void SelectChannelDialog::closeEvent(QCloseEvent *)
2018-04-18 09:12:29 +02:00
{
2018-04-18 09:33:05 +02:00
this->closed.invoke();
2018-04-18 09:12:29 +02:00
}
2018-07-06 17:11:37 +02:00
void SelectChannelDialog::themeChangedEvent()
2018-04-18 09:12:29 +02:00
{
2018-07-06 17:11:37 +02:00
BaseWindow::themeChangedEvent();
2018-04-18 09:33:05 +02:00
2018-10-21 13:43:02 +02:00
if (this->theme->isLightTheme())
{
2018-08-06 21:17:03 +02:00
this->setStyleSheet(
"QRadioButton { color: #000 } QLabel { color: #000 }");
2018-10-21 13:43:02 +02:00
}
else
{
2018-08-06 21:17:03 +02:00
this->setStyleSheet(
"QRadioButton { color: #fff } QLabel { color: #fff }");
2018-04-18 09:33:05 +02:00
}
2018-04-18 09:12:29 +02:00
}
} // namespace chatterino