Refactor NameDialog

This commit is contained in:
WarmUpTill 2024-04-19 23:59:40 +02:00 committed by WarmUpTill
parent e080d2de9b
commit 2bc89364b2
7 changed files with 44 additions and 43 deletions

View File

@ -141,7 +141,7 @@ void AdvSceneSwitcher::on_sceneGroupAdd_clicked()
placeHolderText = format.arg(++i);
}
bool accepted = AdvSSNameDialog::AskForName(
bool accepted = NameDialog::AskForName(
this, obs_module_text("AdvSceneSwitcher.sceneGroupTab.add"),
obs_module_text("AdvSceneSwitcher.sceneGroupTab.add"), name,
placeHolderText);

View File

@ -67,7 +67,7 @@ bool AdvSceneSwitcher::AddNewMacro(std::shared_ptr<Macro> &res,
placeHolderText = fmt.arg(++i);
}
bool accepted = AdvSSNameDialog::AskForName(
bool accepted = NameDialog::AskForName(
this, obs_module_text("AdvSceneSwitcher.macroTab.add"),
obs_module_text("AdvSceneSwitcher.macroTab.name"), name,
placeHolderText);
@ -199,7 +199,7 @@ void AdvSceneSwitcher::RenameSelectedMacro()
std::string oldName = macro->Name();
std::string name;
if (!AdvSSNameDialog::AskForName(
if (!NameDialog::AskForName(
this, obs_module_text("AdvSceneSwitcher.windowTitle"),
obs_module_text("AdvSceneSwitcher.item.newName"), name,
QString::fromStdString(oldName))) {
@ -370,7 +370,7 @@ bool AdvSceneSwitcher::ResolveMacroImportNameConflict(
}
std::string newName;
bool accepted = AdvSSNameDialog::AskForName(
bool accepted = NameDialog::AskForName(
this,
obs_module_text(macro->IsGroup()
? "AdvSceneSwitcher.macroTab.addGroup"
@ -956,7 +956,7 @@ static void handleCustomLabelChange(MacroSegmentEdit *segmentEdit,
}
std::string label;
bool accepted = AdvSSNameDialog::AskForName(
bool accepted = NameDialog::AskForName(
GetSettingsWindow(),
obs_module_text(
"AdvSceneSwitcher.macroTab.segment.setCustomLabel"),

View File

@ -177,7 +177,7 @@ void ItemSelection::RenameItem()
Item *item = variant.value<Item *>();
std::string name;
bool accepted = AdvSSNameDialog::AskForName(
bool accepted = NameDialog::AskForName(
this, obs_module_text("AdvSceneSwitcher.windowTitle"),
obs_module_text("AdvSceneSwitcher.item.newName"), name,
QString::fromStdString(item->Name()));

View File

@ -5,22 +5,22 @@
namespace advss {
AdvSSNameDialog::AdvSSNameDialog(QWidget *parent) : QDialog(parent)
NameDialog::NameDialog(QWidget *parent)
: QDialog(parent),
_label(new QLabel(this)),
_userText(new QLineEdit(this))
{
setModal(true);
setWindowModality(Qt::WindowModality::WindowModal);
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
setFixedWidth(555);
setMinimumHeight(100);
QVBoxLayout *layout = new QVBoxLayout;
auto layout = new QVBoxLayout;
setLayout(layout);
label = new QLabel(this);
layout->addWidget(label);
label->setText("Set Text");
userText = new QLineEdit(this);
layout->addWidget(userText);
layout->addWidget(_label);
layout->addWidget(_userText);
QDialogButtonBox *buttonbox = new QDialogButtonBox(
QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
@ -30,43 +30,44 @@ AdvSSNameDialog::AdvSSNameDialog(QWidget *parent) : QDialog(parent)
connect(buttonbox, &QDialogButtonBox::rejected, this, &QDialog::reject);
}
static bool IsWhitespace(char ch)
static bool isWhitespace(char ch)
{
return ch == ' ' || ch == '\t';
}
static void CleanWhitespace(std::string &str)
static void cleanWhitespace(std::string &str)
{
while (str.size() && IsWhitespace(str.back()))
while (str.size() && isWhitespace(str.back())) {
str.erase(str.end() - 1);
while (str.size() && IsWhitespace(str.front()))
}
while (str.size() && isWhitespace(str.front())) {
str.erase(str.begin());
}
}
bool AdvSSNameDialog::AskForName(QWidget *parent, const QString &title,
const QString &text,
std::string &userTextInput,
const QString &placeHolder, int maxSize,
bool clean)
bool NameDialog::AskForName(QWidget *parent, const QString &title,
const QString &prompt, std::string &userTextInput,
const QString &placeHolder, int maxSize, bool clean)
{
if (maxSize <= 0 || maxSize > 32767) {
maxSize = 170;
}
AdvSSNameDialog dialog(parent);
NameDialog dialog(parent);
dialog.setWindowTitle(title);
dialog.label->setText(text);
dialog.userText->setMaxLength(maxSize);
dialog.userText->setText(placeHolder);
dialog.userText->selectAll();
dialog._label->setVisible(!prompt.isEmpty());
dialog._label->setText(prompt);
dialog._userText->setMaxLength(maxSize);
dialog._userText->setText(placeHolder);
dialog._userText->selectAll();
if (dialog.exec() != DialogCode::Accepted) {
return false;
}
userTextInput = dialog.userText->text().toUtf8().constData();
userTextInput = dialog._userText->text().toUtf8().constData();
if (clean) {
CleanWhitespace(userTextInput);
cleanWhitespace(userTextInput);
}
return true;
}

View File

@ -8,23 +8,23 @@
namespace advss {
// Based on OBS's NameDialog
class AdvSSNameDialog : public QDialog {
class NameDialog : public QDialog {
Q_OBJECT
public:
AdvSSNameDialog(QWidget *parent);
NameDialog(QWidget *parent);
// Returns true if user clicks OK, false otherwise
// userTextInput returns string that user typed into dialog
EXPORT static bool AskForName(QWidget *parent, const QString &title,
const QString &text,
const QString &prompt,
std::string &userTextInput,
const QString &placeHolder = QString(""),
int maxSize = 170, bool clean = true);
private:
QLabel *label;
QLineEdit *userText;
QLabel *_label;
QLineEdit *_userText;
};
} // namespace advss

View File

@ -137,9 +137,9 @@ void StringListEdit::showEvent(QShowEvent *e)
void StringListEdit::Add()
{
std::string name;
bool accepted = AdvSSNameDialog::AskForName(this, _addString,
_addStringDescription, name,
"", _maxStringSize, false);
bool accepted = NameDialog::AskForName(this, _addString,
_addStringDescription, name, "",
_maxStringSize, false);
if (!accepted || (!_allowEmpty && name.empty())) {
return;
@ -205,10 +205,10 @@ void StringListEdit::Down()
void StringListEdit::Clicked(QListWidgetItem *item)
{
std::string name;
bool accepted = AdvSSNameDialog::AskForName(this, _addString,
_addStringDescription, name,
item->text(),
_maxStringSize, false);
bool accepted = NameDialog::AskForName(this, _addString,
_addStringDescription, name,
item->text(), _maxStringSize,
false);
if (!accepted || (!_allowEmpty && name.empty())) {
return;

View File

@ -314,7 +314,7 @@ void TwitchCategorySearchButton::SetToken(
void TwitchCategorySearchButton::StartManualCategorySearch()
{
std::string category;
bool accepted = AdvSSNameDialog::AskForName(
bool accepted = NameDialog::AskForName(
this,
obs_module_text("AdvSceneSwitcher.twitchCategories.search"),
obs_module_text("AdvSceneSwitcher.twitchCategories.name"),