From da7cdb15516717d3721dd2c118febbc0baff9315 Mon Sep 17 00:00:00 2001 From: iaom Date: Tue, 21 Nov 2023 15:50:30 +0800 Subject: [PATCH] =?UTF-8?q?feat(frontend):=E9=80=82=E9=85=8D=E4=B8=BB?= =?UTF-8?q?=E9=A2=98=E7=AA=97=E5=8F=A3=E5=9C=86=E8=A7=92=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/control/search-line-edit.cpp | 58 ++++++++++++++----- frontend/control/search-line-edit.h | 22 ++++--- .../stack-pages/search-result-page.cpp | 10 +++- .../control/stack-pages/search-result-page.h | 1 + frontend/mainwindow.cpp | 28 +++++---- frontend/mainwindow.h | 9 +-- libsearch/global-settings.cpp | 9 +++ libsearch/global-settings.h | 1 + 8 files changed, 98 insertions(+), 40 deletions(-) diff --git a/frontend/control/search-line-edit.cpp b/frontend/control/search-line-edit.cpp index 54a4628..1a8c365 100644 --- a/frontend/control/search-line-edit.cpp +++ b/frontend/control/search-line-edit.cpp @@ -31,14 +31,14 @@ using namespace UkuiSearch; /** * @brief UKuiSearchLineEdit 全局搜索的输入框 */ -SearchLineEdit::SearchLineEdit(QWidget *parent) : QLineEdit(parent) { - setStyle(new LineEditStyle()); +SearchLineEdit::SearchLineEdit(int radius, QWidget *parent) : QLineEdit(parent), m_radius(radius) +{ + setStyle(new LineEditStyle(m_radius)); this->setFocusPolicy(Qt::StrongFocus); this->setFixedSize(680, 50); this->setTextMargins(35, 0, 0, 0); this->setAttribute(Qt::WA_TranslucentBackground); this->setDragEnabled(true); - m_queryIcon = new QLabel; QPixmap pixmap = QPixmap(IconLoader::loadIconQt("system-search-symbolic", QIcon(":/res/icons/system-search.symbolic.png")).pixmap(QSize(18, 18))); m_queryIcon->setProperty("useIconHighlightEffect", 0x02); @@ -90,7 +90,7 @@ void SearchLineEdit::paintEvent(QPaintEvent *e) p.setBrush(palette().base()); p.setOpacity(GlobalSettings::getInstance().getValue(TRANSPARENCY_KEY).toDouble()); p.setPen(Qt::NoPen); - p.drawRoundedRect(this->rect(), 12, 12); + p.drawRoundedRect(this->rect(), m_radius, m_radius); return QLineEdit::paintEvent(e); } @@ -100,42 +100,62 @@ void SearchLineEdit::focusOutEvent(QFocusEvent *e) this->setFocus(); } -SeachBarWidget::SeachBarWidget(QWidget *parent): QWidget(parent) { +void SearchLineEdit::setRadius(int radius) +{ + m_radius = radius; + auto style = dynamic_cast(this->style()); + if(style) { + style->setRadius(m_radius); + } + update(); +} + +SearchBarWidget::SearchBarWidget(QWidget *parent): QWidget(parent) +{ + m_ly = new QHBoxLayout(this); - m_searchLineEdit = new SearchLineEdit(this); + m_radius = GlobalSettings::getInstance().getValue(WINDOW_RADIUS_KEY).toInt(); + m_searchLineEdit = new SearchLineEdit(m_radius, this); + connect(&GlobalSettings::getInstance(), &GlobalSettings::valueChanged, this, [&](const QString& key, const QVariant& value){ + if(key == WINDOW_RADIUS_KEY) { + m_radius = value.toInt(); + m_searchLineEdit->setRadius(m_radius); + } + }); this->setFixedSize(m_searchLineEdit->width()+20, m_searchLineEdit->height()+20); m_ly->setContentsMargins(0,0,0,0); m_ly->addWidget(m_searchLineEdit); this->setFocusProxy(m_searchLineEdit); - connect(m_searchLineEdit, &SearchLineEdit::requestSearchKeyword, this, &SeachBarWidget::requestSearchKeyword); + connect(m_searchLineEdit, &SearchLineEdit::requestSearchKeyword, this, &SearchBarWidget::requestSearchKeyword); + } -SeachBarWidget::~SeachBarWidget() { +SearchBarWidget::~SearchBarWidget() { } -void SeachBarWidget::clear() +void SearchBarWidget::clear() { m_searchLineEdit->clear(); } -void SeachBarWidget::reSearch() +void SearchBarWidget::reSearch() { Q_EMIT this->m_searchLineEdit->requestSearchKeyword(m_searchLineEdit->text()); } -void SeachBarWidget::setText(QString keyword) +void SearchBarWidget::setText(QString keyword) { m_searchLineEdit->setText(keyword); } -void SeachBarWidget::paintEvent(QPaintEvent *e) +void SearchBarWidget::paintEvent(QPaintEvent *e) { Q_UNUSED(e) QPainter p(this); p.setRenderHint(QPainter::Antialiasing); QPainterPath rectPath; - rectPath.addRoundedRect(this->rect().adjusted(10, 10, -10, -10), 12, 12); + rectPath.addRoundedRect(this->rect().adjusted(10, 10, -10, -10), m_radius, m_radius); // 画一个黑底 @@ -194,7 +214,7 @@ void LineEditStyle::drawPrimitive(QStyle::PrimitiveElement element, const QStyle painter->setPen(Qt::NoPen); painter->setBrush(f->palette.brush(QPalette::Active, QPalette::Button)); painter->setRenderHint(QPainter::Antialiasing, true); - painter->drawRoundedRect(option->rect, 12, 12); + painter->drawRoundedRect(option->rect, m_radius, m_radius); painter->restore(); return; } @@ -214,3 +234,13 @@ void LineEditStyle::drawPrimitive(QStyle::PrimitiveElement element, const QStyle // return QProxyStyle::drawPrimitive(element, option, painter, widget); } } + +LineEditStyle::LineEditStyle(int radius) +{ + m_radius = radius; +} + +void LineEditStyle::setRadius(int radius) +{ + m_radius = radius; +} diff --git a/frontend/control/search-line-edit.h b/frontend/control/search-line-edit.h index a6c4768..c9f833d 100644 --- a/frontend/control/search-line-edit.h +++ b/frontend/control/search-line-edit.h @@ -41,12 +41,13 @@ namespace UkuiSearch { class SearchLineEdit : public QLineEdit { Q_OBJECT public: - SearchLineEdit(QWidget *parent = nullptr); + explicit SearchLineEdit(int radius = 12, QWidget *parent = nullptr); // void record(); ~SearchLineEdit(); + void setRadius(int radius); protected: - void paintEvent(QPaintEvent *); - void focusOutEvent(QFocusEvent *); + void paintEvent(QPaintEvent *) override; + void focusOutEvent(QFocusEvent *) override; Q_SIGNALS: void requestSearchKeyword(QString text); @@ -55,19 +56,20 @@ private: QLabel *m_queryIcon; QTimer *m_timer; bool m_isEmpty = true; + int m_radius = 12; }; -class SeachBarWidget: public QWidget { +class SearchBarWidget: public QWidget { Q_OBJECT public: - SeachBarWidget(QWidget *parent = nullptr); - ~SeachBarWidget(); + SearchBarWidget(QWidget *parent = nullptr); + ~SearchBarWidget(); void clear(); void reSearch(); protected: - void paintEvent(QPaintEvent *e); + void paintEvent(QPaintEvent *e) override; Q_SIGNALS: void requestSearchKeyword(QString text); @@ -78,6 +80,7 @@ public Q_SLOTS: private: SearchLineEdit *m_searchLineEdit; QHBoxLayout *m_ly; + int m_radius = 12; }; @@ -85,8 +88,11 @@ private: class LineEditStyle : public QProxyStyle { public: - LineEditStyle() {} + explicit LineEditStyle(int radius = 12); void drawPrimitive(PrimitiveElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget = nullptr) const override; + void setRadius(int radius); +private: + int m_radius = 12; }; } diff --git a/frontend/control/stack-pages/search-result-page.cpp b/frontend/control/stack-pages/search-result-page.cpp index 6c7fe1f..08ec5d1 100644 --- a/frontend/control/stack-pages/search-result-page.cpp +++ b/frontend/control/stack-pages/search-result-page.cpp @@ -131,10 +131,10 @@ void SearchResultPage::paintEvent(QPaintEvent *event) p.setBrush(palette().base()); p.setOpacity(GlobalSettings::getInstance().getValue(TRANSPARENCY_KEY).toDouble()); p.setPen(Qt::NoPen); - p.drawRoundedRect(this->rect().adjusted(10,10,-10,-10), 12, 12); + p.drawRoundedRect(this->rect().adjusted(10,10,-10,-10), m_radius, m_radius); QPainterPath rectPath; - rectPath.addRoundedRect(this->rect().adjusted(10, 10, -10, -10), 12, 12); + rectPath.addRoundedRect(this->rect().adjusted(10, 10, -10, -10), m_radius, m_radius); // 画一个黑底 QPixmap pixmap(this->rect().size()); @@ -170,6 +170,12 @@ void SearchResultPage::paintEvent(QPaintEvent *event) void SearchResultPage::initUi() { + m_radius = GlobalSettings::getInstance().getValue(WINDOW_RADIUS_KEY).toInt(); + connect(&GlobalSettings::getInstance(), &GlobalSettings::valueChanged, this, [&](const QString& key, const QVariant& value){ + if(key == WINDOW_RADIUS_KEY) { + m_radius = value.toInt(); + } + }); this->setFixedSize(700,552); m_hlayout = new QHBoxLayout(this); m_hlayout->setContentsMargins(18 ,18, 10, 18); diff --git a/frontend/control/stack-pages/search-result-page.h b/frontend/control/stack-pages/search-result-page.h index ad42cc5..f036738 100644 --- a/frontend/control/stack-pages/search-result-page.h +++ b/frontend/control/stack-pages/search-result-page.h @@ -51,6 +51,7 @@ private: QHBoxLayout * m_hlayout = nullptr; ResultArea * m_resultArea = nullptr; DetailArea * m_detailArea = nullptr; + int m_radius = 12; Q_SIGNALS: void startSearch(const QString &); diff --git a/frontend/mainwindow.cpp b/frontend/mainwindow.cpp index 4bf4d81..f9c980d 100644 --- a/frontend/mainwindow.cpp +++ b/frontend/mainwindow.cpp @@ -123,10 +123,17 @@ MainWindow::~MainWindow() { /** * @brief initUi 初始化主界面主要ui控件 */ -void MainWindow::initUi() { - this->setFixedSize(WINDOW_WIDTH, 68); +void MainWindow::initUi() +{ + m_radius = GlobalSettings::getInstance().getValue(WINDOW_RADIUS_KEY).toInt(); + connect(&GlobalSettings::getInstance(), &GlobalSettings::valueChanged, this, [&](const QString& key, const QVariant& value){ + if(key == WINDOW_RADIUS_KEY) { + m_radius = value.toInt(); + } + }); - m_searchBarWidget = new SeachBarWidget(this); + this->setFixedSize(WINDOW_WIDTH, 68); + m_searchBarWidget = new SearchBarWidget(this); m_searchBarWidget->move(this->rect().topLeft()); m_searchBarWidget->show(); m_searchResultPage = new SearchResultPage(this); @@ -153,10 +160,10 @@ void MainWindow::initConnections() // connect(qApp, &QApplication::paletteChanged, this, [ = ]() { // m_iconLabel->setPixmap(QIcon::fromTheme("kylin-search").pixmap(QSize(WINDOW_ICON_SIZE, WINDOW_ICON_SIZE))); // }); - connect(m_searchBarWidget, &SeachBarWidget::requestSearchKeyword, this, &MainWindow::searchKeywordSlot); + connect(m_searchBarWidget, &SearchBarWidget::requestSearchKeyword, this, &MainWindow::searchKeywordSlot); // connect(m_stackedWidget, &StackedWidget::effectiveSearch, m_searchLayout, &SearchBarHLayout::effectiveSearchRecord); //connect(m_searchResultPage, &SearchResultPage::resizeHeight, this, &MainWindow::resizeHeight); - connect(this,&MainWindow::setText,m_searchBarWidget,&SeachBarWidget::setText); + connect(this,&MainWindow::setText,m_searchBarWidget,&SearchBarWidget::setText); } /** @@ -353,7 +360,7 @@ void MainWindow::initTimer() { } m_researchTimer->stop(); }); - connect(m_searchBarWidget, &SeachBarWidget::requestSearchKeyword, this, [ = ](QString text) { + connect(m_searchBarWidget, &SearchBarWidget::requestSearchKeyword, this, [ = ](QString text) { if(text == "" || text.isEmpty()) { m_askTimer->stop(); } else { @@ -437,12 +444,9 @@ void MainWindow::paintEvent(QPaintEvent *event) { Q_UNUSED(event) QPainterPath path; - - path.addRoundedRect(m_searchBarWidget->x()+10, m_searchBarWidget->y()+10, m_searchBarWidget->width()-20, m_searchBarWidget->height()-20, 12, 12); - path.addRoundedRect(m_searchResultPage->x()+10, m_searchResultPage->y()+10, m_searchResultPage->width()-20, m_searchResultPage->height()-20, 12, 12); - - KWindowEffects::enableBlurBehind(this->winId(), true, QRegion(path.toFillPolygon().toPolygon())); - + path.addRoundedRect(m_searchBarWidget->x()+10, m_searchBarWidget->y()+10, m_searchBarWidget->width()-20, m_searchBarWidget->height()-20, m_radius, m_radius); + path.addRoundedRect(m_searchResultPage->x()+10, m_searchResultPage->y()+10, m_searchResultPage->width()-20, m_searchResultPage->height()-20, m_radius, m_radius); + KWindowEffects::enableBlurBehind(this->windowHandle(), true, QRegion(path.toFillPolygon().toPolygon())); } bool MainWindow::eventFilter(QObject *watched, QEvent *event) diff --git a/frontend/mainwindow.h b/frontend/mainwindow.h index fe2a1ce..6fa541f 100644 --- a/frontend/mainwindow.h +++ b/frontend/mainwindow.h @@ -73,8 +73,8 @@ public: void initSettings(); protected: - void paintEvent(QPaintEvent *); - void keyPressEvent(QKeyEvent *event); + void paintEvent(QPaintEvent *) override; + void keyPressEvent(QKeyEvent *event) override; void initUi(); void initConnections(); @@ -96,7 +96,7 @@ Q_SIGNALS: void setText(QString keyword); private: - void setSearchMethod(const bool isIndexSearch); + void setSearchMethod(bool isIndexSearch); double getTransparentData(); void initTimer(); bool tryHideMainwindow(); @@ -104,7 +104,7 @@ private: QMenu *m_menu = nullptr; - SeachBarWidget *m_searchBarWidget; + SearchBarWidget *m_searchBarWidget; SearchResultPage *m_searchResultPage; QSystemTrayIcon *m_sys_tray_icon = nullptr; @@ -119,6 +119,7 @@ private: AppWidgetPlugin *m_appWidgetPlugin = nullptr; bool m_isIndexSearch = false; bool m_releaseFreeMemoryTimerWorking = false; + int m_radius = 12; }; } diff --git a/libsearch/global-settings.cpp b/libsearch/global-settings.cpp index c60710f..7cf8ebc 100644 --- a/libsearch/global-settings.cpp +++ b/libsearch/global-settings.cpp @@ -54,6 +54,7 @@ GlobalSettingsPrivate::GlobalSettingsPrivate(QObject *parent) : QObject(parent) //主题,字体大小 setValue(STYLE_NAME_KEY, "ukui-light"); setValue(FONT_SIZE_KEY, 11); + setValue(WINDOW_RADIUS_KEY, 12); if(QGSettings::isSchemaInstalled(THEME_GSETTINGS_ID)) { m_themeGsettings = new QGSettings(THEME_GSETTINGS_ID, QByteArray(), this); connect(m_themeGsettings, &QGSettings::changed, this, [ = ](const QString & key) { @@ -67,6 +68,11 @@ GlobalSettingsPrivate::GlobalSettingsPrivate(QObject *parent) : QObject(parent) qApp->paletteChanged(qApp->palette()); } else if (key == ICON_THEME_KEY) { qApp->paletteChanged(qApp->palette()); + } else if (key == WINDOW_RADIUS_KEY) { + qDebug() << WINDOW_RADIUS_KEY << m_themeGsettings->get(WINDOW_RADIUS_KEY).toInt(); + setValue(WINDOW_RADIUS_KEY, m_themeGsettings->get(WINDOW_RADIUS_KEY).toInt()); + qApp->paletteChanged(qApp->palette()); + Q_EMIT this->valueChanged(WINDOW_RADIUS_KEY, m_themeGsettings->get(WINDOW_RADIUS_KEY).toInt()); } }); if(m_themeGsettings->keys().contains(STYLE_NAME_KEY)) { @@ -75,6 +81,9 @@ GlobalSettingsPrivate::GlobalSettingsPrivate(QObject *parent) : QObject(parent) if(m_themeGsettings->keys().contains(FONT_SIZE_KEY)) { setValue(FONT_SIZE_KEY, m_themeGsettings->get(FONT_SIZE_KEY).toDouble()); } + if(m_themeGsettings->keys().contains(WINDOW_RADIUS_KEY)) { + setValue(WINDOW_RADIUS_KEY, m_themeGsettings->get(WINDOW_RADIUS_KEY).toInt()); + } } } diff --git a/libsearch/global-settings.h b/libsearch/global-settings.h index 17bea52..547f944 100644 --- a/libsearch/global-settings.h +++ b/libsearch/global-settings.h @@ -32,6 +32,7 @@ const static QString TRANSPARENCY_KEY = "transparency"; const static QString STYLE_NAME_KEY = "styleName"; const static QString FONT_SIZE_KEY = "systemFontSize"; const static QString ICON_THEME_KEY = "iconThemeName"; +const static QString WINDOW_RADIUS_KEY = "windowRadius"; class GlobalSettingsPrivate; /**