UI: HomeWindow: Add SideButton

This commit is contained in:
Nodir Temirkhodjaev 2023-05-14 19:55:47 +03:00
parent 4c7960902d
commit a029a38b17
6 changed files with 81 additions and 25 deletions

View File

@ -47,6 +47,7 @@ SOURCES += \
form/controls/listview.cpp \ form/controls/listview.cpp \
form/controls/mainwindow.cpp \ form/controls/mainwindow.cpp \
form/controls/plaintextedit.cpp \ form/controls/plaintextedit.cpp \
form/controls/sidebutton.cpp \
form/controls/spinbox.cpp \ form/controls/spinbox.cpp \
form/controls/spincombo.cpp \ form/controls/spincombo.cpp \
form/controls/tabbar.cpp \ form/controls/tabbar.cpp \
@ -233,6 +234,7 @@ HEADERS += \
form/controls/listview.h \ form/controls/listview.h \
form/controls/mainwindow.h \ form/controls/mainwindow.h \
form/controls/plaintextedit.h \ form/controls/plaintextedit.h \
form/controls/sidebutton.h \
form/controls/spinbox.h \ form/controls/spinbox.h \
form/controls/spincombo.h \ form/controls/spincombo.h \
form/controls/tabbar.h \ form/controls/tabbar.h \

View File

@ -17,6 +17,7 @@
#include "labeldoublespin.h" #include "labeldoublespin.h"
#include "labelspin.h" #include "labelspin.h"
#include "labelspincombo.h" #include "labelspincombo.h"
#include "sidebutton.h"
#include "spinbox.h" #include "spinbox.h"
QCheckBox *ControlUtil::createCheckBox( QCheckBox *ControlUtil::createCheckBox(
@ -70,6 +71,25 @@ QPushButton *ControlUtil::createButton(
return c; return c;
} }
QToolButton *ControlUtil::createSideButton(
const QString &iconPath, const std::function<void()> &onClicked)
{
auto c = new SideButton();
c->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
c->setAutoRaise(true);
c->setAutoExclusive(true);
c->setCheckable(true);
c->setIcon(IconCache::icon(iconPath));
c->setIconSize(QSize(24, 24));
c->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred);
c->connect(c, &QToolButton::clicked, onClicked);
return c;
}
QToolButton *ControlUtil::createToolButton( QToolButton *ControlUtil::createToolButton(
const QString &iconPath, const std::function<void()> &onClicked) const QString &iconPath, const std::function<void()> &onClicked)
{ {

View File

@ -39,6 +39,8 @@ public:
static QPushButton *createButton( static QPushButton *createButton(
const QString &iconPath, const std::function<void()> &onClicked); const QString &iconPath, const std::function<void()> &onClicked);
static QToolButton *createSideButton(
const QString &iconPath, const std::function<void()> &onClicked);
static QToolButton *createToolButton( static QToolButton *createToolButton(
const QString &iconPath, const std::function<void()> &onClicked); const QString &iconPath, const std::function<void()> &onClicked);
static QToolButton *createFlatToolButton( static QToolButton *createFlatToolButton(

View File

@ -0,0 +1,33 @@
#include "sidebutton.h"
#include <QStylePainter>
namespace {
constexpr int indicatorWidth = 4;
}
SideButton::SideButton(QWidget *parent) : QToolButton(parent) { }
QSize SideButton::sizeHint() const
{
QSize sh = QToolButton::sizeHint();
sh.setWidth(sh.width() + indicatorWidth);
return sh;
}
void SideButton::paintEvent(QPaintEvent *event)
{
QToolButton::paintEvent(event);
if (!isChecked())
return;
QRect r = rect();
r.setX(r.width() - indicatorWidth);
r.setWidth(indicatorWidth);
QPainter p(this);
p.fillRect(r, QColor(0x3a, 0xd2, 0x4c));
}

View File

@ -0,0 +1,19 @@
#ifndef SIDEBUTTON_H
#define SIDEBUTTON_H
#include <QToolButton>
class SideButton : public QToolButton
{
Q_OBJECT
public:
explicit SideButton(QWidget *parent = nullptr);
QSize sizeHint() const override;
protected:
void paintEvent(QPaintEvent *event) override;
};
#endif // SIDEBUTTON_H

View File

@ -15,23 +15,6 @@
#include "aboutpage.h" #include "aboutpage.h"
#include "updatespage.h" #include "updatespage.h"
namespace {
QToolButton *createToolButton(const QString &iconPath, const std::function<void()> &onClicked)
{
auto c = ControlUtil::createFlatToolButton(iconPath, onClicked);
c->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
c->setAutoExclusive(true);
c->setCheckable(true);
c->setIconSize(QSize(24, 24));
c->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred);
return c;
}
}
HomeMainPage::HomeMainPage(HomeController *ctrl, QWidget *parent) : HomeBasePage(ctrl, parent) HomeMainPage::HomeMainPage(HomeController *ctrl, QWidget *parent) : HomeBasePage(ctrl, parent)
{ {
setupUi(); setupUi();
@ -47,6 +30,7 @@ void HomeMainPage::setupUi()
{ {
auto layout = new QHBoxLayout(); auto layout = new QHBoxLayout();
layout->setContentsMargins(6, 6, 6, 6); layout->setContentsMargins(6, 6, 6, 6);
layout->setSpacing(0);
// Side Bar // Side Bar
auto sideBar = setupSideBar(); auto sideBar = setupSideBar();
@ -55,7 +39,6 @@ void HomeMainPage::setupUi()
setupStackedLayout(); setupStackedLayout();
layout->addLayout(sideBar); layout->addLayout(sideBar);
layout->addWidget(ControlUtil::createSeparator(Qt::Vertical));
layout->addLayout(m_stackedLayout, 1); layout->addLayout(m_stackedLayout, 1);
this->setLayout(layout); this->setLayout(layout);
@ -78,15 +61,12 @@ QLayout *HomeMainPage::setupSideBar()
void HomeMainPage::setupSideBarButtons() void HomeMainPage::setupSideBarButtons()
{ {
QPalette palette; m_btUpdates = ControlUtil::createSideButton(
palette.setColor(QPalette::Highlight, QColor(0x26, 0x26, 0x26)); ":/icons/arrow_refresh_small.png", [&] { setCurrentIndex(0); });
m_btUpdates = createToolButton(":/icons/arrow_refresh_small.png", [&] { setCurrentIndex(0); });
m_btUpdates->setPalette(palette);
m_btUpdates->setChecked(true); m_btUpdates->setChecked(true);
m_btAbout = createToolButton(":/icons/information.png", [&] { setCurrentIndex(1); }); m_btAbout =
m_btAbout->setPalette(palette); ControlUtil::createSideButton(":/icons/information.png", [&] { setCurrentIndex(1); });
} }
void HomeMainPage::setupStackedLayout() void HomeMainPage::setupStackedLayout()