From 14966625236a4a0d84cb9bf4c88c1ca2c24744ff Mon Sep 17 00:00:00 2001 From: zhangjiaping Date: Tue, 20 Apr 2021 20:43:14 +0800 Subject: [PATCH] Optimize code. --- src/mainwindow.cpp | 11 +++++++++++ src/mainwindow.h | 1 + src/settings-widget.cpp | 24 +++--------------------- src/settings-widget.h | 8 ++------ 4 files changed, 17 insertions(+), 27 deletions(-) diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 0b7466e..f8706a8 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -214,6 +214,16 @@ void MainWindow::initUi() return; } m_settingsWidget = new SettingsWidget(); + connect(this, &MainWindow::webEngineChanged, m_settingsWidget, [ = ]() { + m_settingsWidget->resetWebEngine(); + }); + connect(m_settingsWidget, &SettingsWidget::webEngineChanged, this, [ = ](const QString &engine) { + if (m_search_gsettings && m_search_gsettings->keys().contains(WEB_ENGINE_KEY)) { + m_search_gsettings->set(WEB_ENGINE_KEY, engine); + } else { + GlobalSettings::getInstance()->setValue(WEB_ENGINE, engine); + } + }); centerToScreen(m_settingsWidget); m_settingsWidget->show(); connect(m_settingsWidget, &SettingsWidget::settingWidgetClosed, this, [ = ]() { @@ -478,6 +488,7 @@ void MainWindow::initGsettings() } else if (key == WEB_ENGINE_KEY) { QString web_engine = m_search_gsettings->get(WEB_ENGINE_KEY).toString(); GlobalSettings::getInstance()->setValue(WEB_ENGINE, web_engine); + Q_EMIT this->webEngineChanged(); } }); if (m_search_gsettings->keys().contains(SEARCH_METHOD_KEY)) { diff --git a/src/mainwindow.h b/src/mainwindow.h index 8b98989..21074a8 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -131,6 +131,7 @@ protected: Q_SIGNALS: void searchMethodChanged(FileUtils::SearchMethod); + void webEngineChanged(); public Q_SLOTS: /** diff --git a/src/settings-widget.cpp b/src/settings-widget.cpp index 210d468..04fe3a1 100644 --- a/src/settings-widget.cpp +++ b/src/settings-widget.cpp @@ -42,16 +42,6 @@ SettingsWidget::SettingsWidget(QWidget *parent) : QWidget(parent) m_hints.decorations = MWM_DECOR_BORDER; XAtomHelper::getInstance()->setWindowMotifHint(winId(), m_hints); - const QByteArray id(UKUI_SEARCH_SCHEMAS); - if (QGSettings::isSchemaInstalled(id)) { - m_web_engine_gsettings = new QGSettings(id); - connect(m_web_engine_gsettings, &QGSettings::changed, this, [ = ](const QString &key) { - if (key == WEB_ENGINE_KEY) { - resetWebEngine(); - } - }); - } - initUi(); refreshIndexState(); setupBlackList(GlobalSettings::getInstance()->getBlockDirs()); @@ -329,12 +319,7 @@ void SettingsWidget::onBtnDelClicked(const QString& path) { */ void SettingsWidget::resetWebEngine() { - QString engine; - if (m_web_engine_gsettings && m_web_engine_gsettings->keys().contains(WEB_ENGINE_KEY)) { - engine = m_web_engine_gsettings->get(WEB_ENGINE_KEY).toString(); - } else { - engine = GlobalSettings::getInstance()->getValue(WEB_ENGINE).toString(); - } + QString engine = GlobalSettings::getInstance()->getValue(WEB_ENGINE).toString(); m_engineBtnGroup->blockSignals(true); if (!engine.isEmpty()) { if (engine == "360") { @@ -356,11 +341,8 @@ void SettingsWidget::resetWebEngine() */ void SettingsWidget::setWebEngine(const QString& engine) { - if (m_web_engine_gsettings && m_web_engine_gsettings->keys().contains(WEB_ENGINE_KEY)) { - m_web_engine_gsettings->set(WEB_ENGINE_KEY, engine); - } else { - GlobalSettings::getInstance()->setValue(WEB_ENGINE, engine); - } +// GlobalSettings::getInstance()->setValue(WEB_ENGINE, engine); + Q_EMIT this->webEngineChanged(engine); } /** diff --git a/src/settings-widget.h b/src/settings-widget.h index 39787ad..87ce459 100644 --- a/src/settings-widget.h +++ b/src/settings-widget.h @@ -35,9 +35,6 @@ #include #include "xatom-helper.h" -#define UKUI_SEARCH_SCHEMAS "org.ukui.search.settings" -#define WEB_ENGINE_KEY "webEngine" - class SettingsWidget : public QWidget { Q_OBJECT @@ -48,6 +45,7 @@ public: void setIndexState(bool); void setIndexNum(int); void showWidget(); + void resetWebEngine(); private: void initUi(); @@ -57,7 +55,6 @@ private: void paintEvent(QPaintEvent *); void resize(); void showWarningDialog(const int&); - void resetWebEngine(); MotifWmHints m_hints; @@ -110,10 +107,9 @@ private: int m_blockdirs = 0; //黑名单文件夹数量 - QGSettings * m_web_engine_gsettings = nullptr; - Q_SIGNALS: void settingWidgetClosed(); + void webEngineChanged(const QString&); private Q_SLOTS: // void onBtnConfirmClicked();