Prevent user from entering incorrect characters in Live Notifications channels list (#3715)

Co-authored-by: Sidd <iProdigy@users.noreply.github.com>
Co-authored-by: Rasmus Karlsson <rasmus.karlsson@pajlada.com>
This commit is contained in:
kornes
2022-05-08 10:27:25 +00:00
committed by GitHub
parent fd44f30c7d
commit 07dd8c560b
11 changed files with 199 additions and 0 deletions
+2
View File
@@ -420,6 +420,8 @@ set(SOURCE_FILES
widgets/helper/NotebookTab.hpp
widgets/helper/QColorPicker.cpp
widgets/helper/QColorPicker.hpp
widgets/helper/RegExpItemDelegate.cpp
widgets/helper/RegExpItemDelegate.hpp
widgets/helper/ResizingTextEdit.cpp
widgets/helper/ResizingTextEdit.hpp
widgets/helper/ScrollbarHighlight.cpp
+22
View File
@@ -5,6 +5,12 @@
namespace chatterino {
namespace {
const auto TWITCH_USER_LOGIN_PATTERN = R"(^[a-z0-9]\w{0,24}$)";
} // namespace
void openTwitchUsercard(QString channel, QString username)
{
QDesktopServices::openUrl("https://www.twitch.tv/popout/" + channel +
@@ -35,4 +41,20 @@ void stripChannelName(QString &channelName)
}
}
QRegularExpression twitchUserNameRegexp()
{
static QRegularExpression re(
TWITCH_USER_LOGIN_PATTERN,
QRegularExpression::PatternOption::CaseInsensitiveOption);
return re;
}
QRegularExpression twitchUserLoginRegexp()
{
static QRegularExpression re(TWITCH_USER_LOGIN_PATTERN);
return re;
}
} // namespace chatterino
+13
View File
@@ -1,5 +1,6 @@
#pragma once
#include <QRegularExpression>
#include <QString>
namespace chatterino {
@@ -12,4 +13,16 @@ void stripUserName(QString &userName);
// stripChannelName removes any @ prefix or , suffix to make it more suitable for command use
void stripChannelName(QString &channelName);
// Matches a strict Twitch user login.
// May contain lowercase a-z, 0-9, and underscores
// Must contain between 1 and 25 characters
// Must not start with an underscore
QRegularExpression twitchUserLoginRegexp();
// Matches a loose Twitch user login name.
// May contain lowercase and uppercase a-z, 0-9, and underscores
// Must contain between 1 and 25 characters
// Must not start with an underscore
QRegularExpression twitchUserNameRegexp();
} // namespace chatterino
+5
View File
@@ -1,4 +1,5 @@
#include "EditableModelView.hpp"
#include "widgets/helper/RegExpItemDelegate.hpp"
#include <QAbstractItemView>
#include <QAbstractTableModel>
@@ -90,6 +91,10 @@ EditableModelView::EditableModelView(QAbstractTableModel *model, bool movable)
// finish button layout
buttons->addStretch(1);
}
void EditableModelView::setValidationRegexp(QRegularExpression regexp)
{
this->tableView_->setItemDelegate(new RegExpItemDelegate(this, regexp));
}
void EditableModelView::setTitles(std::initializer_list<QString> titles)
{
+1
View File
@@ -16,6 +16,7 @@ public:
EditableModelView(QAbstractTableModel *model, bool movable = true);
void setTitles(std::initializer_list<QString> titles);
void setValidationRegexp(QRegularExpression regexp);
QTableView *getTableView();
QAbstractTableModel *getModel();
+24
View File
@@ -0,0 +1,24 @@
#include "widgets/helper/RegExpItemDelegate.hpp"
#include <QLineEdit>
namespace chatterino {
RegExpItemDelegate::RegExpItemDelegate(QObject *parent,
QRegularExpression regexp)
: QStyledItemDelegate(parent)
, regexp_(regexp)
{
}
QWidget *RegExpItemDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
auto *editor = new QLineEdit(parent);
editor->setValidator(
new QRegularExpressionValidator(this->regexp_, editor));
return editor;
}
} // namespace chatterino
+22
View File
@@ -0,0 +1,22 @@
#pragma once
#include <QRegularExpression>
#include <QStyledItemDelegate>
namespace chatterino {
class RegExpItemDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
RegExpItemDelegate(QObject *parent, QRegularExpression regexp);
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
const QModelIndex &index) const override;
private:
const QRegularExpression regexp_;
};
} // namespace chatterino
@@ -6,6 +6,7 @@
#include "singletons/Settings.hpp"
#include "singletons/Toasts.hpp"
#include "util/LayoutCreator.hpp"
#include "util/Twitch.hpp"
#include "widgets/helper/EditableModelView.hpp"
#include <QCheckBox>
@@ -96,6 +97,7 @@ NotificationPage::NotificationPage()
nullptr, Platform::Twitch))
.getElement();
view->setTitles({"Twitch channels"});
view->setValidationRegexp(twitchUserNameRegexp());
view->getTableView()->horizontalHeader()->setSectionResizeMode(
QHeaderView::Fixed);