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

462 lines
15 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"
2019-09-08 18:06:43 +02:00
#include "widgets/dialogs/IrcConnectionEditor.hpp"
#include "widgets/dialogs/IrcConnectionPopup.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>
2019-09-08 18:06:43 +02:00
#include <QTableView>
#include "providers/irc/Irc2.hpp"
#include "widgets/helper/EditableModelView.hpp"
2018-04-18 09:12:29 +02:00
#define TAB_TWITCH 0
2019-09-09 22:26:56 +02:00
#define TAB_IRC 1
2018-04-18 09:12:29 +02:00
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
2019-09-08 18:06:43 +02:00
{
LayoutCreator<QWidget> obj(new QWidget());
auto outerBox = obj.setLayoutType<QVBoxLayout>();
// outerBox.emplace<QLabel>("Connection:");
2018-10-21 12:13:09 +02:00
{
2019-09-09 22:26:56 +02:00
auto view = this->ui_.irc.servers = new EditableModelView(
2019-09-08 18:06:43 +02:00
Irc::getInstance().newConnectionModel(this));
view->setTitles(
{"host", "port", "ssl", "user", "nick", "password"});
view->getTableView()->horizontalHeader()->resizeSection(0, 140);
view->getTableView()->horizontalHeader()->resizeSection(1, 30);
view->getTableView()->horizontalHeader()->resizeSection(2, 30);
view->addButtonPressed.connect([] {
Irc::getInstance().connections.appendItem(
IrcConnection_::unique());
});
outerBox->addWidget(view);
// auto box = outerBox.emplace<QHBoxLayout>().withoutMargin();
// auto conns = box.emplace<QListView>();
// conns->addActions({new QAction("hackint")});
// auto buttons = box.emplace<QVBoxLayout>().withoutMargin();
2018-10-21 12:13:09 +02:00
2019-09-08 18:06:43 +02:00
// buttons.emplace<QPushButton>("Add").onClick(this, [this]() {
// (new IrcConnectionPopup(this))
// ->show(); // XXX: don't show multiple
// });
2018-10-21 12:13:09 +02:00
2019-09-08 18:06:43 +02:00
// buttons.emplace<QPushButton>("Edit");
// buttons.emplace<QPushButton>("Remove");
// buttons->addStretch(1);
2018-10-21 12:13:09 +02:00
}
2019-09-08 18:06:43 +02:00
{
auto box = outerBox.emplace<QHBoxLayout>().withoutMargin();
box.emplace<QLabel>("Channel:");
2019-09-09 22:26:56 +02:00
this->ui_.irc.channel = box.emplace<QLineEdit>().getElement();
2019-09-08 18:06:43 +02:00
}
// auto vbox = obj.setLayoutType<QVBoxLayout>();
// auto form = vbox.emplace<QFormLayout>();
// auto servers = new QComboBox;
// auto accounts = new QComboBox;
//servers->setEditable(true);
//servers->addItems(
// {"irc://irc.hackint.org:6697", "irc://irc.somethingelse.com:6667"});
// form->addRow("Server:", servers);
// form->addRow("Account:", accounts);
// form->addRow("Channel:", new QLineEdit());
// form->addRow("User name:", new QLineEdit());
// form->addRow("First nick choice:", new QLineEdit());
// form->addRow("Second nick choice:", new QLineEdit());
// form->addRow("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
}
2019-09-08 18:06:43 +02:00
this->setMinimumSize(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;
2019-09-09 22:26:56 +02:00
case Channel::Type::Irc:
{
this->ui_.notebook->selectIndex(TAB_IRC);
this->ui_.irc.channel->setText(_channel.get()->getName());
if (auto ircChannel =
dynamic_cast<IrcChannel *>(_channel.get().get()))
{
if (auto server =
Irc::getInstance().getServerOfChannel(ircChannel))
{
int i = 0;
for (auto &&conn : Irc::getInstance().connections)
{
if (conn.id == server->getId())
{
this->ui_.irc.servers->getTableView()->selectRow(i);
break;
}
}
}
}
}
break;
2018-10-21 13:43:02 +02:00
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
}
}
2019-09-09 22:26:56 +02:00
break;
case TAB_IRC:
{
int row = this->ui_.irc.servers->getTableView()
->selectionModel()
->currentIndex()
.row();
auto &&vector = Irc::getInstance().connections.getVector();
if (row >= 0 && row < int(vector.size()))
{
return Irc::getInstance().getOrAddChannel(
vector[row].id, this->ui_.irc.channel->text());
}
else
{
return Channel::getEmpty();
}
}
break;
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