From cefd30909751ab288d7d92e96ab32144dd8235c8 Mon Sep 17 00:00:00 2001 From: RigoLigoRLC Date: Sun, 5 Dec 2021 18:09:31 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9ESpkIconButton=E4=B8=BA?= =?UTF-8?q?=E4=B8=BB=E9=A2=98=E5=92=8C=E6=8C=89=E9=92=AE=E5=B7=A5=E4=BD=9C?= =?UTF-8?q?=E5=87=8F=E8=B4=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 1 + gui/page/spkpageuitest.cpp | 12 +++++ gui/spkiconbutton.cpp | 56 ++++++++++++++++++++++++ gui/spkmainwindow.cpp | 22 ++++------ gui/spktitlebar.cpp | 1 + gui/spkui_general.cpp | 19 +++++++- inc/page/spkpageuitest.h | 4 ++ inc/spkiconbutton.h | 30 +++++++++++++ inc/spkmainwindow.h | 5 ++- inc/spkui_general.h | 4 +- resource/stylesheets/mainwindow_dark.css | 33 ++++++++++++++ src/spkutils.cpp | 2 +- 12 files changed, 171 insertions(+), 18 deletions(-) create mode 100644 gui/spkiconbutton.cpp create mode 100644 inc/spkiconbutton.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 1d56ac0..de38de0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -60,6 +60,7 @@ set(SOURCE_FILES inc/spkwindow.h gui/spkwindow.cpp inc/spktitlebar.h gui/spktitlebar.cpp + inc/spkiconbutton.h gui/spkiconbutton.cpp inc/spkui_general.h gui/spkui_general.cpp inc/spkmainwindow.h gui/spkmainwindow.cpp inc/spkmsgbox.h gui/spkmsgbox.cpp diff --git a/gui/page/spkpageuitest.cpp b/gui/page/spkpageuitest.cpp index 6a7dc4c..6ab5c7a 100644 --- a/gui/page/spkpageuitest.cpp +++ b/gui/page/spkpageuitest.cpp @@ -44,6 +44,11 @@ SpkUi::SpkPageUiTest::SpkPageUiTest(QWidget *parent) : QSplitter(parent) Loading->setObjectName("spk_pg_qsstest_loading"); Loading->start(); + Prog = new QProgressBar(this); + Prog->setObjectName("spk_pg_qsstest_prog"); + Prog->setValue(65); + Prog->setRange(0, 100); + AppItem = new SpkAppItem(0, this); AppItem->setObjectName("spk_pg_qsstest_appitem"); AppItem->SetTitle("Lorem ipsum dolor sit amet, consectetur adipiscing elit."); @@ -87,11 +92,16 @@ SpkUi::SpkPageUiTest::SpkPageUiTest(QWidget *parent) : QSplitter(parent) SlideH->setMaximum(1000); SlideH->setMinimum(0); + IconBtn = new SpkIconButton(this); + IconBtn->setObjectName("spk_pg_qsstest_iconbtn"); + IconBtn->SetIcon(QIcon(":/icons/settings.svg"), QSize{ 16, 16 }); + VLayTestWidgets = new QVBoxLayout; VLayTestWidgets->setObjectName("spk_pg_qsstest_vlay_btn"); VLayTestWidgets->addWidget(Btn); VLayTestWidgets->addWidget(Chk); VLayTestWidgets->addWidget(Rad); + VLayTestWidgets->addWidget(IconBtn); VLayTestWidgets->addWidget(Loading); VLayTestWidgets->addWidget(PopupText); VLayTestWidgets->addWidget(ShowPopup); @@ -113,6 +123,7 @@ SpkUi::SpkPageUiTest::SpkPageUiTest(QWidget *parent) : QSplitter(parent) VLayWidgets->setObjectName("spk_pg_qsstest_widgetlay"); VLayWidgets->addWidget(Group); VLayWidgets->addWidget(SlideH); + VLayWidgets->addWidget(Prog); HLay4Slider = new QHBoxLayout; HLay4Slider->setObjectName("spk_pg_qsstest_hlay_for_slider"); @@ -122,6 +133,7 @@ SpkUi::SpkPageUiTest::SpkPageUiTest(QWidget *parent) : QSplitter(parent) WidL = new QWidget(this); WidL->setObjectName("spk_pg_qsstest_widleft"); WidL->setLayout(HLay4Slider); + WidL->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Preferred); WidR = new QWidget(this); WidR->setObjectName("spk_pg_qsstest_widright"); diff --git a/gui/spkiconbutton.cpp b/gui/spkiconbutton.cpp new file mode 100644 index 0000000..971260b --- /dev/null +++ b/gui/spkiconbutton.cpp @@ -0,0 +1,56 @@ + +#include +#include +#include +#include +#include "spkiconbutton.h" + +SpkIconButton::SpkIconButton(QWidget *parent) : + QPushButton(parent) +{ + +} + +void SpkIconButton::SetIcon(QIcon i, QSize s) +{ + mPmapPaintedIcon = i.pixmap(s); + + setFixedSize((mPmapSize = s.grownBy(QMargins(IconMargin, IconMargin, IconMargin, IconMargin)))); +} + +void SpkIconButton::SetIcon(QPixmap m) +{ + mPmapPaintedIcon = m; + + setFixedSize((mPmapSize = m.size().grownBy(QMargins(IconMargin, IconMargin, + IconMargin, IconMargin)))); +} + +void SpkIconButton::SetIconSize(QSize s) +{ + setFixedSize((mPmapSize = s.grownBy(QMargins(IconMargin, IconMargin, IconMargin, IconMargin)))); +} + +void SpkIconButton::paintEvent(QPaintEvent *e) +{ + QPushButton::paintEvent(e); + + // Paint the icon mask + QPainter p(this), p1(&mPmapPaintedIcon); + QBrush b(Qt::SolidPattern); + + p.drawPixmap(IconMargin, IconMargin, mPmapPaintedIcon); + if(isDown() || isChecked()) + { + b.setColor(SpkUi::ColorBtnMaskSelected); + } + else + { + b.setColor(SpkUi::ColorBtnMaskUnselected); + } + p1.setCompositionMode(QPainter::CompositionMode_SourceIn); + p1.fillRect(0, 0, mPmapSize.width(), mPmapSize.height(), b); + p1.end(); + p.drawPixmap(IconMargin, IconMargin, mPmapPaintedIcon); + p.end(); +} diff --git a/gui/spkmainwindow.cpp b/gui/spkmainwindow.cpp index a6923e2..06d9b82 100644 --- a/gui/spkmainwindow.cpp +++ b/gui/spkmainwindow.cpp @@ -319,7 +319,7 @@ void SpkMainWindow::PopulateAppDetails(QJsonObject appDetails) void SpkMainWindow::ReloadThemedUiIcons() { for(auto &i : mThemedUiIconReferences) - i.first->setIcon(SpkUi::GetThemedIcon(i.second)); + i.first->SetIcon(SpkUi::GetThemedIcon(i.second), QSize { 20, 20 }); } // ==================== Main Window Initialization ==================== @@ -338,7 +338,7 @@ void SpkMainWindow::Initialize() [=](){ emit SearchKeyword(ui->SearchEdit->text(), 1); }); connect(ui->PageAppList, &SpkUi::SpkPageAppList::ApplicationClicked, this, &SpkMainWindow::EnterAppDetails); - connect(ui->BtnDayNight, &QPushButton::pressed, + connect(ui->BtnDayNight, &QPushButton::clicked, this, &SpkMainWindow::SwitchDayNightTheme); if(SpkUi::States::IsUsingDtkPlugin) { @@ -347,7 +347,7 @@ void SpkMainWindow::Initialize() } // Register themed button icons - mThemedUiIconReferences.append({ ui->BtnSettings, "settings" }); +// mThemedUiIconReferences.append({ ui->BtnSettings, "settings" }); mThemedUiIconReferences.append({ ui->BtnDayNight, "daynight" }); } @@ -400,24 +400,20 @@ SpkUi::SpkMainWidget::SpkMainWidget(QWidget *parent) : QFrame(parent) SidebarMgr = new SpkSidebarSelector(this); SidebarMgr->setObjectName("spk_mw_sidebar_mgr"); - BtnSettings = new QPushButton(this); + BtnSettings = new SpkIconButton(this); BtnSettings->setObjectName("styPlainChkBtn"); BtnSettings->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); BtnSettings->setCheckable(true); - BtnSettings->setMaximumSize({ 40, 40 }); - BtnSettings->setMinimumSize({ 40, 40 }); - BtnSettings->setIconSize(QSize(20, 20)); - BtnSettings->setIcon(SpkUi::GetThemedIcon("settings")); + BtnSettings->setFixedSize({ 40, 40 }); + BtnSettings->SetIcon(QIcon(":/icons/settings.svg"), QSize(20, 20)); BtnSettings->setProperty("spk_pageno", 0); SidebarMgr->BindPageSwitcherButton(BtnSettings); - BtnDayNight = new QPushButton(this); + BtnDayNight = new SpkIconButton(this); BtnDayNight->setObjectName("styPlainChkBtn"); BtnDayNight->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); - BtnDayNight->setMaximumSize({ 40, 40 }); - BtnDayNight->setMinimumSize({ 40, 40 }); - BtnDayNight->setIconSize(QSize(20, 20)); - BtnDayNight->setIcon(SpkUi::GetThemedIcon("daynight")); + BtnDayNight->setFixedSize({ 40, 40 }); + BtnDayNight->SetIcon(QIcon(":/icons/daynight.svg"), QSize(20, 20)); HLaySideTop->addWidget(StoreIcon); HLaySideTop->addStretch(); diff --git a/gui/spktitlebar.cpp b/gui/spktitlebar.cpp index a789e43..b4775fe 100644 --- a/gui/spktitlebar.cpp +++ b/gui/spktitlebar.cpp @@ -102,6 +102,7 @@ SpkTitleBarDefaultButton::SpkTitleBarDefaultButton(QWidget* parent) : QPushButto setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding); setMaximumWidth(ButtonWidth); setMinimumWidth(ButtonWidth); + setFocusPolicy(Qt::NoFocus); } void SpkTitleBarDefaultButton::paintEvent(QPaintEvent *e) diff --git a/gui/spkui_general.cpp b/gui/spkui_general.cpp index f9bf479..6de45e2 100644 --- a/gui/spkui_general.cpp +++ b/gui/spkui_general.cpp @@ -27,7 +27,7 @@ namespace SpkUi UiMetaObject SpkUiMetaObject; SpkUiStyle CurrentStyle; QString StylesheetBase, CurrentStylesheet; - QColor ColorLine, ColorBack; + QColor ColorLine, ColorBack, ColorBtnMaskSelected, ColorBtnMaskUnselected; QSize PrimaryScreenSize; SpkDtkPlugin *DtkPlugin = nullptr; QStyle *OldSystemStyle = nullptr; @@ -46,6 +46,16 @@ namespace SpkUi bool CrashHandlerActivated; } + // ======================= Static Linkage Private Functions ======================== + + static void SetBtnMaskColor() + { + ColorBtnMaskUnselected = ColorTextOnBackground(CurrentColorSet[Qss::ControlsBgnd]); + ColorBtnMaskSelected = ColorTextOnBackground(CurrentColorSet[Qss::AccentColor]); + } + + // ======================== Public Functions ========================= + void Initialize() { // Obtain global stylesheets @@ -153,6 +163,7 @@ namespace SpkUi Qss::ColorSet tempset; switch(aStyle) { + case Invalid: case Light: tempset = Qss::LightColorSet; ColorLine = Qt::black; @@ -169,6 +180,7 @@ namespace SpkUi } CurrentColorSet = tempset; CurrentStylesheet = StylesheetFromColors(CurrentColorSet); + SetBtnMaskColor(); qApp->setStyleSheet(CurrentStylesheet); } @@ -262,11 +274,16 @@ namespace SpkUi return gray > 0.5 ? Qt::black : Qt::white; } + // =================== UiMetaObject ======================= + // UiMetaObject is the signal-slot receiver for DDE plugin, receiving the DDE system level + // notifications of UI theme changes + void UiMetaObject::SetAccentColor(QColor aColor) { CurrentColorSet[Qss::AccentColor] = aColor.lighter(90); CurrentColorSet[Qss::AccentColorHighlighted] = aColor.lighter(105); CurrentColorSet[Qss::TextOnAccentColor] = ColorTextOnBackground(aColor); + SetBtnMaskColor(); qApp->setStyleSheet(StylesheetFromColors(CurrentColorSet)); } diff --git a/inc/page/spkpageuitest.h b/inc/page/spkpageuitest.h index 06faee8..9f6ebc0 100644 --- a/inc/page/spkpageuitest.h +++ b/inc/page/spkpageuitest.h @@ -11,9 +11,11 @@ #include #include #include +#include #include "spkappitem.h" #include "spkstretchlayout.h" #include "page/spkpageappdetails.h" +#include "spkiconbutton.h" #include "spkloading.h" @@ -40,6 +42,8 @@ namespace SpkUi QGroupBox *Group; QSlider *SlideH; QSlider *SlideV; + SpkIconButton *IconBtn; + QProgressBar *Prog; SpkLoading *Loading; SpkAppItem *AppItem; SpkStretchLayout *DetailsLay; diff --git a/inc/spkiconbutton.h b/inc/spkiconbutton.h new file mode 100644 index 0000000..dfbfba3 --- /dev/null +++ b/inc/spkiconbutton.h @@ -0,0 +1,30 @@ +#ifndef SPKICONBUTTON_H +#define SPKICONBUTTON_H + +#include +#include "spkui_general.h" + +class SpkIconButton : public QPushButton +{ + Q_OBJECT + + public: + SpkIconButton(QWidget *parent = nullptr); + ~SpkIconButton() {}; + + void SetIcon(QIcon, QSize); + void SetIcon(QPixmap); + void SetIconSize(QSize); + + protected: + void paintEvent(QPaintEvent *) override; + + private: + static enum { Dark, Light } sCurrentTheme; + QPixmap mPmapPaintedIcon; + QSize mPmapSize; + + static constexpr int IconMargin = 8; // shall we make it changable? +}; + +#endif // SPKICONBUTTON_H diff --git a/inc/spkmainwindow.h b/inc/spkmainwindow.h index 36e349d..ba6edea 100644 --- a/inc/spkmainwindow.h +++ b/inc/spkmainwindow.h @@ -14,6 +14,7 @@ #include #include #include "spkfocuslineedit.h" +#include "spkiconbutton.h" #include "page/spkpageuitest.h" #include "page/spkpageapplist.h" #include "page/spkpageappdetails.h" @@ -144,7 +145,7 @@ namespace SpkUi QVBoxLayout *VLaySidebar; QHBoxLayout *HLaySideTop; QLabel *StoreIcon; - QPushButton *BtnSettings, *BtnFeedback, *BtnLogs, *BtnDayNight; + SpkIconButton *BtnSettings, *BtnFeedback, *BtnLogs, *BtnDayNight; SpkSidebarTree *CategoryWidget; QMap *CategoryItemMap; SpkSidebarSelector *SidebarMgr; @@ -184,7 +185,7 @@ class SpkMainWindow : public SpkWindow mCategoryAppListGetReply, mAppDetailsGetReply; SpkUi::SpkStackedPages mCurrentPage = SpkUi::PgInvalid; - QList> mThemedUiIconReferences; + QList> mThemedUiIconReferences; public slots: void ReloadThemedUiIcons(); diff --git a/inc/spkui_general.h b/inc/spkui_general.h index 3f7552a..e3f0cf2 100644 --- a/inc/spkui_general.h +++ b/inc/spkui_general.h @@ -39,7 +39,9 @@ namespace SpkUi extern UiMetaObject SpkUiMetaObject; extern SpkUiStyle CurrentStyle; extern QString StylesheetBase, CurrentStylesheet; - extern QColor ColorLine, ColorBack; + extern QColor ColorLine, ColorBack, + ColorBtnMaskUnselected, ///< SpkIconButton icon mask colors, unselected & selected + ColorBtnMaskSelected; extern QSize PrimaryScreenSize; extern SpkDtkPlugin *DtkPlugin; extern QStyle *OldSystemStyle; diff --git a/resource/stylesheets/mainwindow_dark.css b/resource/stylesheets/mainwindow_dark.css index 1aa6bfe..5675f89 100644 --- a/resource/stylesheets/mainwindow_dark.css +++ b/resource/stylesheets/mainwindow_dark.css @@ -98,6 +98,23 @@ QScrollBar::add-line, QScrollBar::sub-line height:0px } +QProgressBar +{ + border: none; + background-color: DCTL2; + height: 8px; + border-radius: 4px; + font-size: 1px; + color: transparent; +} + +QProgressBar::chunk +{ + border: none; + border-radius: 4px; + background-color: ACC_; +} + /* Custom widgets */ ElidedLabel @@ -182,6 +199,22 @@ SpkAppItem::hover background-color: ACC_ } +SpkIconButton +{ + border-width: 0px; + background-color: GBG_; +} + +SpkIconButton:pressed +{ + background-color: ACCH; +} + +SpkIconButton:checked +{ + background-color: ACC_; +} + SpkTitleBar { background-color: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 DCTL2, stop:1 DCTL1) diff --git a/src/spkutils.cpp b/src/spkutils.cpp index 060a159..8f7a693 100644 --- a/src/spkutils.cpp +++ b/src/spkutils.cpp @@ -23,7 +23,7 @@ bool SpkUtils::VerifyReplyJson(QNetworkReply *aReply, QJsonValue &aRetDoc) { QJsonParseError err; QByteArray rawjson = aReply->readAll(); - qDebug() << "Received:" << rawjson; +// qDebug() << "Received:" << rawjson; QJsonDocument ret = QJsonDocument::fromJson(rawjson, &err); QJsonObject replyObject; if(err.error != QJsonParseError::NoError)