From 3e76d380a12ad193954316df746ff879e6db449b Mon Sep 17 00:00:00 2001 From: TheLastRar Date: Sun, 22 May 2022 13:35:59 +0100 Subject: [PATCH] DEV9: Move helper Qt classes into a separate file --- pcsx2-qt/CMakeLists.txt | 2 + pcsx2-qt/Settings/DEV9SettingsWidget.cpp | 65 ------------------ pcsx2-qt/Settings/DEV9SettingsWidget.h | 37 ----------- pcsx2-qt/Settings/DEV9UiCommon.cpp | 84 ++++++++++++++++++++++++ pcsx2-qt/Settings/DEV9UiCommon.h | 56 ++++++++++++++++ pcsx2-qt/pcsx2-qt.vcxproj | 3 + pcsx2-qt/pcsx2-qt.vcxproj.filters | 9 +++ 7 files changed, 154 insertions(+), 102 deletions(-) create mode 100644 pcsx2-qt/Settings/DEV9UiCommon.cpp create mode 100644 pcsx2-qt/Settings/DEV9UiCommon.h diff --git a/pcsx2-qt/CMakeLists.txt b/pcsx2-qt/CMakeLists.txt index 56dee96bac..7bf685454d 100644 --- a/pcsx2-qt/CMakeLists.txt +++ b/pcsx2-qt/CMakeLists.txt @@ -94,6 +94,8 @@ target_sources(pcsx2-qt PRIVATE Settings/DEV9SettingsWidget.cpp Settings/DEV9SettingsWidget.h Settings/DEV9SettingsWidget.ui + Settings/DEV9UiCommon.cpp + Settings/DEV9UiCommon.h Settings/HddCreateQt.cpp Settings/HddCreateQt.h Settings/SettingsDialog.cpp diff --git a/pcsx2-qt/Settings/DEV9SettingsWidget.cpp b/pcsx2-qt/Settings/DEV9SettingsWidget.cpp index 5d9db43d91..10eb0f56a3 100644 --- a/pcsx2-qt/Settings/DEV9SettingsWidget.cpp +++ b/pcsx2-qt/Settings/DEV9SettingsWidget.cpp @@ -55,71 +55,6 @@ static const char* s_dns_name[] = { using PacketReader::IP::IP_Address; -#define IP_RANGE_INTER "([0-1]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5]|)" -#define IP_RANGE_FINAL "([0-1]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])" - -// clang-format off -const QRegularExpression IPValidator::intermediateRegex{QStringLiteral("^" IP_RANGE_INTER "\\." IP_RANGE_INTER "\\." IP_RANGE_INTER "\\." IP_RANGE_INTER "$")}; -const QRegularExpression IPValidator::finalRegex {QStringLiteral("^" IP_RANGE_FINAL "\\." IP_RANGE_FINAL "\\." IP_RANGE_FINAL "\\." IP_RANGE_FINAL "$")}; -// clang-format on - -IPValidator::IPValidator(QObject* parent, bool allowEmpty) - : QValidator(parent) - , m_allowEmpty{allowEmpty} -{ -} - -QValidator::State IPValidator::validate(QString& input, int& pos) const -{ - if (input.isEmpty()) - return m_allowEmpty ? Acceptable : Intermediate; - - QRegularExpressionMatch m = finalRegex.match(input, 0, QRegularExpression::NormalMatch); - if (m.hasMatch()) - return Acceptable; - - m = intermediateRegex.match(input, 0, QRegularExpression::PartialPreferCompleteMatch); - if (m.hasMatch() || m.hasPartialMatch()) - return Intermediate; - else - { - pos = input.size(); - return Invalid; - } -} - -IPItemDelegate::IPItemDelegate(QObject* parent) - : QItemDelegate(parent) -{ -} - -QWidget* IPItemDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const -{ - QLineEdit* editor = new QLineEdit(parent); - editor->setValidator(new IPValidator()); - return editor; -} - -void IPItemDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const -{ - QString value = index.model()->data(index, Qt::EditRole).toString(); - QLineEdit* line = static_cast(editor); - line->setText(value); -} - -void IPItemDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const -{ - QLineEdit* line = static_cast(editor); - QString value = line->text(); - model->setData(index, value); -} - -void IPItemDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const -{ - editor->setGeometry(option.rect); -} - - DEV9SettingsWidget::DEV9SettingsWidget(SettingsDialog* dialog, QWidget* parent) : QWidget(parent) , m_dialog{dialog} diff --git a/pcsx2-qt/Settings/DEV9SettingsWidget.h b/pcsx2-qt/Settings/DEV9SettingsWidget.h index 844b3549eb..69883231b6 100644 --- a/pcsx2-qt/Settings/DEV9SettingsWidget.h +++ b/pcsx2-qt/Settings/DEV9SettingsWidget.h @@ -16,7 +16,6 @@ #pragma once #include -#include #include #include "ui_DEV9SettingsWidget.h" @@ -25,35 +24,6 @@ class SettingsDialog; -class IPValidator : public QValidator -{ - Q_OBJECT - -public: - explicit IPValidator(QObject* parent = nullptr, bool allowEmpty = false); - virtual State validate(QString& input, int& pos) const override; - -private: - static const QRegularExpression intermediateRegex; - static const QRegularExpression finalRegex; - - bool m_allowEmpty; -}; - -class IPItemDelegate : public QItemDelegate -{ - Q_OBJECT - -public: - explicit IPItemDelegate(QObject* parent = nullptr); - -protected: - QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const; - void setEditorData(QWidget* editor, const QModelIndex& index) const; - void setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const; - void updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const; -}; - class DEV9SettingsWidget : public QWidget { Q_OBJECT @@ -86,13 +56,6 @@ protected: bool eventFilter(QObject* object, QEvent* event); private: - struct HostEntryUi - { - std::string Url; - std::string Desc; - std::string Address = "0.0.0.0"; - bool Enabled; - }; void AddAdapter(const AdapterEntry& adapter); void RefreshHostList(); diff --git a/pcsx2-qt/Settings/DEV9UiCommon.cpp b/pcsx2-qt/Settings/DEV9UiCommon.cpp new file mode 100644 index 0000000000..ceb07bf501 --- /dev/null +++ b/pcsx2-qt/Settings/DEV9UiCommon.cpp @@ -0,0 +1,84 @@ +/* PCSX2 - PS2 Emulator for PCs + * Copyright (C) 2002-2022 PCSX2 Dev Team + * + * PCSX2 is free software: you can redistribute it and/or modify it under the terms + * of the GNU Lesser General Public License as published by the Free Software Found- + * ation, either version 3 of the License, or (at your option) any later version. + * + * PCSX2 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with PCSX2. + * If not, see . + */ + +#include "PrecompiledHeader.h" + +#include + +#include "DEV9UiCommon.h" + +#define IP_RANGE_INTER "([0-1]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5]|)" +#define IP_RANGE_FINAL "([0-1]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])" + +// clang-format off +const QRegularExpression IPValidator::intermediateRegex{QStringLiteral("^" IP_RANGE_INTER "\\." IP_RANGE_INTER "\\." IP_RANGE_INTER "\\." IP_RANGE_INTER "$")}; +const QRegularExpression IPValidator::finalRegex {QStringLiteral("^" IP_RANGE_FINAL "\\." IP_RANGE_FINAL "\\." IP_RANGE_FINAL "\\." IP_RANGE_FINAL "$")}; +// clang-format on + +IPValidator::IPValidator(QObject* parent, bool allowEmpty) + : QValidator(parent) + , m_allowEmpty{allowEmpty} +{ +} + +QValidator::State IPValidator::validate(QString& input, int& pos) const +{ + if (input.isEmpty()) + return m_allowEmpty ? Acceptable : Intermediate; + + QRegularExpressionMatch m = finalRegex.match(input, 0, QRegularExpression::NormalMatch); + if (m.hasMatch()) + return Acceptable; + + m = intermediateRegex.match(input, 0, QRegularExpression::PartialPreferCompleteMatch); + if (m.hasMatch() || m.hasPartialMatch()) + return Intermediate; + else + { + pos = input.size(); + return Invalid; + } +} + +IPItemDelegate::IPItemDelegate(QObject* parent) + : QItemDelegate(parent) +{ +} + +QWidget* IPItemDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const +{ + QLineEdit* editor = new QLineEdit(parent); + editor->setValidator(new IPValidator()); + return editor; +} + +void IPItemDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const +{ + QString value = index.model()->data(index, Qt::EditRole).toString(); + QLineEdit* line = static_cast(editor); + line->setText(value); +} + +void IPItemDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const +{ + QLineEdit* line = static_cast(editor); + QString value = line->text(); + model->setData(index, value); +} + +void IPItemDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const +{ + editor->setGeometry(option.rect); +} diff --git a/pcsx2-qt/Settings/DEV9UiCommon.h b/pcsx2-qt/Settings/DEV9UiCommon.h new file mode 100644 index 0000000000..7d2f15d5b4 --- /dev/null +++ b/pcsx2-qt/Settings/DEV9UiCommon.h @@ -0,0 +1,56 @@ +/* PCSX2 - PS2 Emulator for PCs + * Copyright (C) 2002-2021 PCSX2 Dev Team + * + * PCSX2 is free software: you can redistribute it and/or modify it under the terms + * of the GNU Lesser General Public License as published by the Free Software Found- + * ation, either version 3 of the License, or (at your option) any later version. + * + * PCSX2 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with PCSX2. + * If not, see . + */ + +#pragma once + +#include +#include + +struct HostEntryUi +{ + std::string Url; + std::string Desc; + std::string Address = "0.0.0.0"; + bool Enabled; +}; + +class IPValidator : public QValidator +{ + Q_OBJECT + +public: + explicit IPValidator(QObject* parent = nullptr, bool allowEmpty = false); + virtual State validate(QString& input, int& pos) const override; + +private: + static const QRegularExpression intermediateRegex; + static const QRegularExpression finalRegex; + + bool m_allowEmpty; +}; + +class IPItemDelegate : public QItemDelegate +{ + Q_OBJECT + +public: + explicit IPItemDelegate(QObject* parent = nullptr); + +protected: + QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const; + void setEditorData(QWidget* editor, const QModelIndex& index) const; + void setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const; + void updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const; +}; diff --git a/pcsx2-qt/pcsx2-qt.vcxproj b/pcsx2-qt/pcsx2-qt.vcxproj index 37107c4487..4e6ee34696 100644 --- a/pcsx2-qt/pcsx2-qt.vcxproj +++ b/pcsx2-qt/pcsx2-qt.vcxproj @@ -155,6 +155,7 @@ + @@ -190,6 +191,7 @@ + @@ -234,6 +236,7 @@ + diff --git a/pcsx2-qt/pcsx2-qt.vcxproj.filters b/pcsx2-qt/pcsx2-qt.vcxproj.filters index 99627fdc11..bf637a1c21 100644 --- a/pcsx2-qt/pcsx2-qt.vcxproj.filters +++ b/pcsx2-qt/pcsx2-qt.vcxproj.filters @@ -173,6 +173,9 @@ moc + + moc + Settings @@ -191,6 +194,9 @@ Settings + + Settings + Settings @@ -294,6 +300,9 @@ Settings + + Settings + Tools\Input Recording