From 329e8de84fc8e0a2e4a04fbaf85ab0e1632c6084 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Fri, 1 Jun 2018 17:27:58 +0200 Subject: Only add profiles to profile manager at init --- .../ProfileEditor/forms/profilemanagerdialog.cpp | 20 +++++++--------- plugins/ProfileEditor/forms/profilemanagerdialog.h | 9 ++++--- plugins/ProfileEditor/forms/profileview.cpp | 2 +- plugins/ProfileEditor/profileeditorplugin.cpp | 22 +++++++++++------ plugins/ProfileEditor/profileeditorplugin.h | 7 +++++- plugins/interfaces.h | 3 ++- src/browser.cpp | 28 +++++++++------------- 7 files changed, 48 insertions(+), 43 deletions(-) diff --git a/plugins/ProfileEditor/forms/profilemanagerdialog.cpp b/plugins/ProfileEditor/forms/profilemanagerdialog.cpp index c5fec2b..fbf7ad4 100644 --- a/plugins/ProfileEditor/forms/profilemanagerdialog.cpp +++ b/plugins/ProfileEditor/forms/profilemanagerdialog.cpp @@ -1,17 +1,21 @@ #include "profilemanagerdialog.h" +#include "profileview.h" #include "ui_profilemanagerdialog.h" #include -#include -#include "profileview.h" -ProfileManagerDialog::ProfileManagerDialog(QWidget *parent) : - QDialog(parent), - ui(new Ui::ProfileManagerDialog) +ProfileManagerDialog::ProfileManagerDialog(QHash &profiles, QWidget *parent) + : QDialog(parent) + , ui(new Ui::ProfileManagerDialog) + , profiles(profiles) { ui->setupUi(this); connect(ui->listWidget, &QListWidget::itemPressed, this, &ProfileManagerDialog::showProfile); showProfile(nullptr); + + for(const QString &name : profiles.keys()) { + ui->listWidget->addItem(name); + } } ProfileManagerDialog::~ProfileManagerDialog() @@ -19,12 +23,6 @@ ProfileManagerDialog::~ProfileManagerDialog() delete ui; } -void ProfileManagerDialog::addProfile(const QString &name, QWebEngineProfile *profile) -{ - profiles.insert(name, profile); - ui->listWidget->addItem(name); -} - void ProfileManagerDialog::showProfile(QListWidgetItem *item) { // clear out groupbox layout diff --git a/plugins/ProfileEditor/forms/profilemanagerdialog.h b/plugins/ProfileEditor/forms/profilemanagerdialog.h index ed79824..9ce177d 100644 --- a/plugins/ProfileEditor/forms/profilemanagerdialog.h +++ b/plugins/ProfileEditor/forms/profilemanagerdialog.h @@ -4,7 +4,8 @@ #include #include -namespace Ui { +namespace Ui +{ class ProfileManagerDialog; } @@ -15,17 +16,15 @@ class ProfileManagerDialog : public QDialog Q_OBJECT public: - explicit ProfileManagerDialog(QWidget *parent = 0); + explicit ProfileManagerDialog(QHash &profiles, QWidget *parent = 0); ~ProfileManagerDialog(); - void addProfile(const QString &name, QWebEngineProfile *profile); - private slots: void showProfile(QListWidgetItem *item); private: Ui::ProfileManagerDialog *ui; - QHash profiles; + const QHash profiles; }; #endif // PROFILEMANAGERDIALOG_H diff --git a/plugins/ProfileEditor/forms/profileview.cpp b/plugins/ProfileEditor/forms/profileview.cpp index e073dbb..b53cc7d 100644 --- a/plugins/ProfileEditor/forms/profileview.cpp +++ b/plugins/ProfileEditor/forms/profileview.cpp @@ -12,7 +12,7 @@ inline void connectSetting(QCheckBox *checkBox, QWebEngineSettings *settings, QWebEngineSettings::WebAttribute attr) { - QObject::connect(checkBox, &QCheckBox::clicked, [settings, attr](bool checked){ + QObject::connect(checkBox, &QCheckBox::clicked, [settings, attr](bool checked) { settings->setAttribute(attr, checked); }); } diff --git a/plugins/ProfileEditor/profileeditorplugin.cpp b/plugins/ProfileEditor/profileeditorplugin.cpp index 041dbe7..d0341b9 100644 --- a/plugins/ProfileEditor/profileeditorplugin.cpp +++ b/plugins/ProfileEditor/profileeditorplugin.cpp @@ -7,26 +7,34 @@ */ #include "profileeditorplugin.h" -#include "forms/profileview.h" #include "forms/profilemanagerdialog.h" +#include "forms/profileview.h" #include -QHash > ProfileEditorPlugin::commands() +QHash> ProfileEditorPlugin::commands() { QHash> hash; hash.insert("profileEditor:about", []() -> int { qDebug("ProfileEditor for smolbote"); return 0; }); + + hash.insert("profileEditor:edit", [this]() -> int { + auto *dialog = createWidget(nullptr); + return dialog->exec(); + }); return hash; } -QDialog *ProfileEditorPlugin::createWidget(QHash profiles, QWidget *parent) +void ProfileEditorPlugin::addProfile(const QString &name, QWebEngineProfile *profile) +{ + Q_CHECK_PTR(profile); + profiles.insert(name, profile); +} + +QDialog *ProfileEditorPlugin::createWidget(QWidget *parent) { - auto *widget = new ProfileManagerDialog(parent); - for(const QString &name : profiles.keys()) { - widget->addProfile(name, profiles.value(name)); - } + auto *widget = new ProfileManagerDialog(profiles, parent); widget->setAttribute(Qt::WA_DeleteOnClose, true); return widget; } diff --git a/plugins/ProfileEditor/profileeditorplugin.h b/plugins/ProfileEditor/profileeditorplugin.h index e026bbe..7ae0a57 100644 --- a/plugins/ProfileEditor/profileeditorplugin.h +++ b/plugins/ProfileEditor/profileeditorplugin.h @@ -9,6 +9,7 @@ #ifndef PROFILEEDITOR_PLUGIN_H #define PROFILEEDITOR_PLUGIN_H +#include #include class QWebEngineProfile; @@ -24,7 +25,11 @@ public: QHash> commands() override; // ProfileInterface - QDialog *createWidget(QHash profiles, QWidget *parent) override; + void addProfile(const QString &name, QWebEngineProfile *profile) override; + QDialog *createWidget(QWidget *parent) override; + +private: + QHash profiles; }; #endif //PROFILEEDITOR_PLUGIN_H diff --git a/plugins/interfaces.h b/plugins/interfaces.h index 4e361ab..d6bff54 100644 --- a/plugins/interfaces.h +++ b/plugins/interfaces.h @@ -34,7 +34,8 @@ class ProfileInterface { public: virtual ~ProfileInterface() = default; - virtual QDialog *createWidget(QHash profiles, QWidget *parent) = 0; + virtual void addProfile(const QString &name, QWebEngineProfile *profile) = 0; + virtual QDialog *createWidget(QWidget *parent = nullptr) = 0; }; #define PluginInterfaceIid "net.iserlohn-fortress.smolbote.PluginInterface" diff --git a/src/browser.cpp b/src/browser.cpp index be9d6b5..3442d66 100644 --- a/src/browser.cpp +++ b/src/browser.cpp @@ -118,20 +118,19 @@ void Browser::setup(const QString &defaultProfile) // register commands for(Plugin p : m_plugins) { - auto *plugin = qobject_cast(p.instance.get()); - if(plugin) { - m_commands.unite(plugin->commands()); - } + if(p.instance->inherits("ProfileInterface")) { auto *profileEditor = qobject_cast(p.instance.get()); Q_ASSERT_X(profileEditor != nullptr, "Browser::setup", "profile interface cast failed"); - m_commands.insert("profileEditor:edit", [this, profileEditor]() -> int { - QHash p; - for(const QString &key : m_profiles.keys()) { - p.insert(key, m_profiles.value(key)); - } - return profileEditor->createWidget(p, nullptr)->exec(); - }); + + for(const QString &name : m_profiles.keys()) { + profileEditor->addProfile(name, qobject_cast(m_profiles.value(name))); + } + } + + auto *plugin = qobject_cast(p.instance.get()); + if(plugin) { + m_commands.unite(plugin->commands()); } } @@ -234,12 +233,7 @@ MainWindow *Browser::createWindow() if(profileEditor) { auto *profileAction = new QAction(tr("Profile"), window); connect(profileAction, &QAction::triggered, window, [this, profileEditor]() { - QHash p; - for(const QString &key : m_profiles.keys()) { - p.insert(key, m_profiles.value(key)); - } - profileEditor->createWidget(p, nullptr)->show(); - //profileEditor->createWidget(WebProfile::defaultProfile(), nullptr)->show(); + profileEditor->createWidget(nullptr)->show(); }); window->addAction(MainWindow::ToolsMenu, profileAction); } -- cgit v1.2.1