feat(frontend):适配主题窗口圆角配置

This commit is contained in:
iaom 2023-11-21 15:50:30 +08:00
parent bd9de3c541
commit da7cdb1551
8 changed files with 98 additions and 40 deletions

View File

@ -31,14 +31,14 @@ using namespace UkuiSearch;
/** /**
* @brief UKuiSearchLineEdit * @brief UKuiSearchLineEdit
*/ */
SearchLineEdit::SearchLineEdit(QWidget *parent) : QLineEdit(parent) { SearchLineEdit::SearchLineEdit(int radius, QWidget *parent) : QLineEdit(parent), m_radius(radius)
setStyle(new LineEditStyle()); {
setStyle(new LineEditStyle(m_radius));
this->setFocusPolicy(Qt::StrongFocus); this->setFocusPolicy(Qt::StrongFocus);
this->setFixedSize(680, 50); this->setFixedSize(680, 50);
this->setTextMargins(35, 0, 0, 0); this->setTextMargins(35, 0, 0, 0);
this->setAttribute(Qt::WA_TranslucentBackground); this->setAttribute(Qt::WA_TranslucentBackground);
this->setDragEnabled(true); this->setDragEnabled(true);
m_queryIcon = new QLabel; m_queryIcon = new QLabel;
QPixmap pixmap = QPixmap(IconLoader::loadIconQt("system-search-symbolic", QIcon(":/res/icons/system-search.symbolic.png")).pixmap(QSize(18, 18))); QPixmap pixmap = QPixmap(IconLoader::loadIconQt("system-search-symbolic", QIcon(":/res/icons/system-search.symbolic.png")).pixmap(QSize(18, 18)));
m_queryIcon->setProperty("useIconHighlightEffect", 0x02); m_queryIcon->setProperty("useIconHighlightEffect", 0x02);
@ -90,7 +90,7 @@ void SearchLineEdit::paintEvent(QPaintEvent *e)
p.setBrush(palette().base()); p.setBrush(palette().base());
p.setOpacity(GlobalSettings::getInstance().getValue(TRANSPARENCY_KEY).toDouble()); p.setOpacity(GlobalSettings::getInstance().getValue(TRANSPARENCY_KEY).toDouble());
p.setPen(Qt::NoPen); p.setPen(Qt::NoPen);
p.drawRoundedRect(this->rect(), 12, 12); p.drawRoundedRect(this->rect(), m_radius, m_radius);
return QLineEdit::paintEvent(e); return QLineEdit::paintEvent(e);
} }
@ -100,42 +100,62 @@ void SearchLineEdit::focusOutEvent(QFocusEvent *e)
this->setFocus(); this->setFocus();
} }
SeachBarWidget::SeachBarWidget(QWidget *parent): QWidget(parent) { void SearchLineEdit::setRadius(int radius)
{
m_radius = radius;
auto style = dynamic_cast<LineEditStyle *>(this->style());
if(style) {
style->setRadius(m_radius);
}
update();
}
SearchBarWidget::SearchBarWidget(QWidget *parent): QWidget(parent)
{
m_ly = new QHBoxLayout(this); 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); this->setFixedSize(m_searchLineEdit->width()+20, m_searchLineEdit->height()+20);
m_ly->setContentsMargins(0,0,0,0); m_ly->setContentsMargins(0,0,0,0);
m_ly->addWidget(m_searchLineEdit); m_ly->addWidget(m_searchLineEdit);
this->setFocusProxy(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(); m_searchLineEdit->clear();
} }
void SeachBarWidget::reSearch() void SearchBarWidget::reSearch()
{ {
Q_EMIT this->m_searchLineEdit->requestSearchKeyword(m_searchLineEdit->text()); Q_EMIT this->m_searchLineEdit->requestSearchKeyword(m_searchLineEdit->text());
} }
void SeachBarWidget::setText(QString keyword) void SearchBarWidget::setText(QString keyword)
{ {
m_searchLineEdit->setText(keyword); m_searchLineEdit->setText(keyword);
} }
void SeachBarWidget::paintEvent(QPaintEvent *e) void SearchBarWidget::paintEvent(QPaintEvent *e)
{ {
Q_UNUSED(e) Q_UNUSED(e)
QPainter p(this); QPainter p(this);
p.setRenderHint(QPainter::Antialiasing); p.setRenderHint(QPainter::Antialiasing);
QPainterPath rectPath; 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->setPen(Qt::NoPen);
painter->setBrush(f->palette.brush(QPalette::Active, QPalette::Button)); painter->setBrush(f->palette.brush(QPalette::Active, QPalette::Button));
painter->setRenderHint(QPainter::Antialiasing, true); painter->setRenderHint(QPainter::Antialiasing, true);
painter->drawRoundedRect(option->rect, 12, 12); painter->drawRoundedRect(option->rect, m_radius, m_radius);
painter->restore(); painter->restore();
return; return;
} }
@ -214,3 +234,13 @@ void LineEditStyle::drawPrimitive(QStyle::PrimitiveElement element, const QStyle
// return QProxyStyle::drawPrimitive(element, option, painter, widget); // return QProxyStyle::drawPrimitive(element, option, painter, widget);
} }
} }
LineEditStyle::LineEditStyle(int radius)
{
m_radius = radius;
}
void LineEditStyle::setRadius(int radius)
{
m_radius = radius;
}

View File

@ -41,12 +41,13 @@ namespace UkuiSearch {
class SearchLineEdit : public QLineEdit { class SearchLineEdit : public QLineEdit {
Q_OBJECT Q_OBJECT
public: public:
SearchLineEdit(QWidget *parent = nullptr); explicit SearchLineEdit(int radius = 12, QWidget *parent = nullptr);
// void record(); // void record();
~SearchLineEdit(); ~SearchLineEdit();
void setRadius(int radius);
protected: protected:
void paintEvent(QPaintEvent *); void paintEvent(QPaintEvent *) override;
void focusOutEvent(QFocusEvent *); void focusOutEvent(QFocusEvent *) override;
Q_SIGNALS: Q_SIGNALS:
void requestSearchKeyword(QString text); void requestSearchKeyword(QString text);
@ -55,19 +56,20 @@ private:
QLabel *m_queryIcon; QLabel *m_queryIcon;
QTimer *m_timer; QTimer *m_timer;
bool m_isEmpty = true; bool m_isEmpty = true;
int m_radius = 12;
}; };
class SeachBarWidget: public QWidget { class SearchBarWidget: public QWidget {
Q_OBJECT Q_OBJECT
public: public:
SeachBarWidget(QWidget *parent = nullptr); SearchBarWidget(QWidget *parent = nullptr);
~SeachBarWidget(); ~SearchBarWidget();
void clear(); void clear();
void reSearch(); void reSearch();
protected: protected:
void paintEvent(QPaintEvent *e); void paintEvent(QPaintEvent *e) override;
Q_SIGNALS: Q_SIGNALS:
void requestSearchKeyword(QString text); void requestSearchKeyword(QString text);
@ -78,6 +80,7 @@ public Q_SLOTS:
private: private:
SearchLineEdit *m_searchLineEdit; SearchLineEdit *m_searchLineEdit;
QHBoxLayout *m_ly; QHBoxLayout *m_ly;
int m_radius = 12;
}; };
@ -85,8 +88,11 @@ private:
class LineEditStyle : public QProxyStyle class LineEditStyle : public QProxyStyle
{ {
public: public:
LineEditStyle() {} explicit LineEditStyle(int radius = 12);
void drawPrimitive(PrimitiveElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget = nullptr) const override; void drawPrimitive(PrimitiveElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget = nullptr) const override;
void setRadius(int radius);
private:
int m_radius = 12;
}; };
} }

View File

@ -131,10 +131,10 @@ void SearchResultPage::paintEvent(QPaintEvent *event)
p.setBrush(palette().base()); p.setBrush(palette().base());
p.setOpacity(GlobalSettings::getInstance().getValue(TRANSPARENCY_KEY).toDouble()); p.setOpacity(GlobalSettings::getInstance().getValue(TRANSPARENCY_KEY).toDouble());
p.setPen(Qt::NoPen); 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; 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()); QPixmap pixmap(this->rect().size());
@ -170,6 +170,12 @@ void SearchResultPage::paintEvent(QPaintEvent *event)
void SearchResultPage::initUi() 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); this->setFixedSize(700,552);
m_hlayout = new QHBoxLayout(this); m_hlayout = new QHBoxLayout(this);
m_hlayout->setContentsMargins(18 ,18, 10, 18); m_hlayout->setContentsMargins(18 ,18, 10, 18);

View File

@ -51,6 +51,7 @@ private:
QHBoxLayout * m_hlayout = nullptr; QHBoxLayout * m_hlayout = nullptr;
ResultArea * m_resultArea = nullptr; ResultArea * m_resultArea = nullptr;
DetailArea * m_detailArea = nullptr; DetailArea * m_detailArea = nullptr;
int m_radius = 12;
Q_SIGNALS: Q_SIGNALS:
void startSearch(const QString &); void startSearch(const QString &);

View File

@ -123,10 +123,17 @@ MainWindow::~MainWindow() {
/** /**
* @brief initUi ui控件 * @brief initUi ui控件
*/ */
void MainWindow::initUi() { void MainWindow::initUi()
this->setFixedSize(WINDOW_WIDTH, 68); {
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->move(this->rect().topLeft());
m_searchBarWidget->show(); m_searchBarWidget->show();
m_searchResultPage = new SearchResultPage(this); m_searchResultPage = new SearchResultPage(this);
@ -153,10 +160,10 @@ void MainWindow::initConnections()
// connect(qApp, &QApplication::paletteChanged, this, [ = ]() { // connect(qApp, &QApplication::paletteChanged, this, [ = ]() {
// m_iconLabel->setPixmap(QIcon::fromTheme("kylin-search").pixmap(QSize(WINDOW_ICON_SIZE, WINDOW_ICON_SIZE))); // 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_stackedWidget, &StackedWidget::effectiveSearch, m_searchLayout, &SearchBarHLayout::effectiveSearchRecord);
//connect(m_searchResultPage, &SearchResultPage::resizeHeight, this, &MainWindow::resizeHeight); //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(); m_researchTimer->stop();
}); });
connect(m_searchBarWidget, &SeachBarWidget::requestSearchKeyword, this, [ = ](QString text) { connect(m_searchBarWidget, &SearchBarWidget::requestSearchKeyword, this, [ = ](QString text) {
if(text == "" || text.isEmpty()) { if(text == "" || text.isEmpty()) {
m_askTimer->stop(); m_askTimer->stop();
} else { } else {
@ -437,12 +444,9 @@ void MainWindow::paintEvent(QPaintEvent *event)
{ {
Q_UNUSED(event) Q_UNUSED(event)
QPainterPath path; QPainterPath path;
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_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, m_radius, m_radius);
path.addRoundedRect(m_searchResultPage->x()+10, m_searchResultPage->y()+10, m_searchResultPage->width()-20, m_searchResultPage->height()-20, 12, 12); KWindowEffects::enableBlurBehind(this->windowHandle(), true, QRegion(path.toFillPolygon().toPolygon()));
KWindowEffects::enableBlurBehind(this->winId(), true, QRegion(path.toFillPolygon().toPolygon()));
} }
bool MainWindow::eventFilter(QObject *watched, QEvent *event) bool MainWindow::eventFilter(QObject *watched, QEvent *event)

View File

@ -73,8 +73,8 @@ public:
void initSettings(); void initSettings();
protected: protected:
void paintEvent(QPaintEvent *); void paintEvent(QPaintEvent *) override;
void keyPressEvent(QKeyEvent *event); void keyPressEvent(QKeyEvent *event) override;
void initUi(); void initUi();
void initConnections(); void initConnections();
@ -96,7 +96,7 @@ Q_SIGNALS:
void setText(QString keyword); void setText(QString keyword);
private: private:
void setSearchMethod(const bool isIndexSearch); void setSearchMethod(bool isIndexSearch);
double getTransparentData(); double getTransparentData();
void initTimer(); void initTimer();
bool tryHideMainwindow(); bool tryHideMainwindow();
@ -104,7 +104,7 @@ private:
QMenu *m_menu = nullptr; QMenu *m_menu = nullptr;
SeachBarWidget *m_searchBarWidget; SearchBarWidget *m_searchBarWidget;
SearchResultPage *m_searchResultPage; SearchResultPage *m_searchResultPage;
QSystemTrayIcon *m_sys_tray_icon = nullptr; QSystemTrayIcon *m_sys_tray_icon = nullptr;
@ -119,6 +119,7 @@ private:
AppWidgetPlugin *m_appWidgetPlugin = nullptr; AppWidgetPlugin *m_appWidgetPlugin = nullptr;
bool m_isIndexSearch = false; bool m_isIndexSearch = false;
bool m_releaseFreeMemoryTimerWorking = false; bool m_releaseFreeMemoryTimerWorking = false;
int m_radius = 12;
}; };
} }

View File

@ -54,6 +54,7 @@ GlobalSettingsPrivate::GlobalSettingsPrivate(QObject *parent) : QObject(parent)
//主题,字体大小 //主题,字体大小
setValue(STYLE_NAME_KEY, "ukui-light"); setValue(STYLE_NAME_KEY, "ukui-light");
setValue(FONT_SIZE_KEY, 11); setValue(FONT_SIZE_KEY, 11);
setValue(WINDOW_RADIUS_KEY, 12);
if(QGSettings::isSchemaInstalled(THEME_GSETTINGS_ID)) { if(QGSettings::isSchemaInstalled(THEME_GSETTINGS_ID)) {
m_themeGsettings = new QGSettings(THEME_GSETTINGS_ID, QByteArray(), this); m_themeGsettings = new QGSettings(THEME_GSETTINGS_ID, QByteArray(), this);
connect(m_themeGsettings, &QGSettings::changed, this, [ = ](const QString & key) { connect(m_themeGsettings, &QGSettings::changed, this, [ = ](const QString & key) {
@ -67,6 +68,11 @@ GlobalSettingsPrivate::GlobalSettingsPrivate(QObject *parent) : QObject(parent)
qApp->paletteChanged(qApp->palette()); qApp->paletteChanged(qApp->palette());
} else if (key == ICON_THEME_KEY) { } else if (key == ICON_THEME_KEY) {
qApp->paletteChanged(qApp->palette()); 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)) { 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)) { if(m_themeGsettings->keys().contains(FONT_SIZE_KEY)) {
setValue(FONT_SIZE_KEY, m_themeGsettings->get(FONT_SIZE_KEY).toDouble()); 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());
}
} }
} }

View File

@ -32,6 +32,7 @@ const static QString TRANSPARENCY_KEY = "transparency";
const static QString STYLE_NAME_KEY = "styleName"; const static QString STYLE_NAME_KEY = "styleName";
const static QString FONT_SIZE_KEY = "systemFontSize"; const static QString FONT_SIZE_KEY = "systemFontSize";
const static QString ICON_THEME_KEY = "iconThemeName"; const static QString ICON_THEME_KEY = "iconThemeName";
const static QString WINDOW_RADIUS_KEY = "windowRadius";
class GlobalSettingsPrivate; class GlobalSettingsPrivate;
/** /**