From 60200fd928c916818021c7a47479b1d50bf1c405 Mon Sep 17 00:00:00 2001 From: wangfei Date: Tue, 30 Jan 2024 17:06:28 +0800 Subject: [PATCH] chore: Tweak UI to adapt to sidebar blur tweak UI to adapt to sidebar blur Log: --- src/frame/listitemdelegate.cpp | 27 ++++++++++++++++++++------- src/frame/listview.cpp | 4 +++- src/frame/mainmodule.cpp | 4 ++-- src/frame/mainwindow.cpp | 5 ++++- 4 files changed, 29 insertions(+), 11 deletions(-) diff --git a/src/frame/listitemdelegate.cpp b/src/frame/listitemdelegate.cpp index 40b17744f3..e724832c39 100644 --- a/src/frame/listitemdelegate.cpp +++ b/src/frame/listitemdelegate.cpp @@ -45,8 +45,11 @@ void ListItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &opti bool isIconMode = option.decorationAlignment == Qt::AlignCenter; bool isBeginning = (opt.viewItemPosition == QStyleOptionViewItem::ViewItemPosition::Beginning) || (opt.viewItemPosition == QStyleOptionViewItem::ViewItemPosition::OnlyOne); + bool isSelected = opt.state & QStyle::State_Selected; + bool isHover = opt.state & QStyle::State_MouseOver; + // 选择高亮背景 - if (opt.state & QStyle::State_Selected) { + if (isSelected) { QPalette::ColorGroup cg = (option.state & QStyle::State_Enabled) ? QPalette::Normal : QPalette::Disabled; @@ -56,9 +59,11 @@ void ListItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &opti QVariant value; QRect decorationRect; + int fontHeight = opt.widget->fontMetrics().height(); + const int firstItemHeight = (opt.rect.height() - (fontHeight * 2 + opt.decorationSize.height())) / 2; if (index.data(Qt::DecorationRole).isValid()) { if (isBeginning && isIconMode) { - decorationRect = QRect(opt.rect.topLeft() + QPoint((opt.rect.width() - opt.decorationSize.width()) / 2, 15), opt.decorationSize); + decorationRect = QRect(opt.rect.topLeft() + QPoint((opt.rect.width() - opt.decorationSize.width()) / 2, firstItemHeight), opt.decorationSize); opt.displayAlignment = Qt::AlignCenter; } else if (isBeginning) { opt.decorationSize += QSize(4, 4); @@ -71,13 +76,12 @@ void ListItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &opti } else { decorationRect = QRect(); } - int fontHeight = opt.widget->fontMetrics().height(); QString text; QRect displayRect; value = index.data(Qt::DisplayRole); if (value.isValid() && !value.isNull()) { if (isBeginning && isIconMode) { - displayRect = QRect(opt.rect.topLeft() + QPoint(0, opt.decorationSize.height() + 40), QSize(opt.rect.width(), -1)); + displayRect = QRect(opt.rect.topLeft() + QPoint(0, opt.decorationSize.height() + firstItemHeight + 10), QSize(opt.rect.width(), -1)); } else if (isBeginning || isIconMode) { displayRect = QRect(opt.rect.topLeft() + QPoint(decorationRect.width() + 18, (opt.rect.height() / 2 - fontHeight + 5)), QSize(opt.rect.width() - opt.decorationSize.width() - 30, -1)); } else { @@ -89,21 +93,27 @@ void ListItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &opti QStyle *style = option.widget ? option.widget->style() : QApplication::style(); // draw the item - drawBackground(style, painter, opt); + if (isIconMode || isSelected || isHover) { + drawBackground(style, painter, opt); + } // 图标的绘制用也可能会使用这些颜色 QPalette::ColorGroup cg = (opt.state & QStyle::State_Enabled) ? QPalette::Normal : QPalette::Disabled; - painter->setPen(opt.palette.color(cg, (opt.state & QStyle::State_Selected) ? QPalette::HighlightedText : QPalette::Text)); + auto role = (opt.state & QStyle::State_Selected) ? QPalette::HighlightedText : QPalette::Text; + painter->setPen(opt.palette.color(cg, role)); drawDecoration(painter, opt, decorationRect); drawDisplay(style, painter, opt, displayRect); if (isIconMode || isBeginning) { - painter->setPen(opt.palette.color(QPalette::Disabled, QPalette::Text)); + painter->save(); + painter->setPen(opt.palette.color(cg, role)); + painter->setOpacity(0.7); opt.text = index.data(Qt::StatusTipRole).toString(); int fontSize = opt.font.pointSize(); opt.font.setPointSize(fontSize - 3); drawDisplay(style, painter, opt, tipRect); + painter->restore(); } drawEllipse(painter, opt, index.data(Dtk::RightActionListRole).toInt()); @@ -118,6 +128,9 @@ void ListItemDelegate::drawBackground(const QStyle *style, QPainter *painter, co boption.init(option.widget); boption.QStyleOption::operator=(option); boption.position = DStyleOptionBackgroundGroup::ItemBackgroundPosition(option.viewItemPosition); + bool isIconMode = option.decorationAlignment == Qt::AlignCenter; + if (isIconMode) + boption.dpalette.setBrush(DPalette::ItemBackground, option.palette.color(QPalette::Base)); if (option.backgroundBrush.style() != Qt::NoBrush) { boption.dpalette.setBrush(DPalette::ItemBackground, option.backgroundBrush); diff --git a/src/frame/listview.cpp b/src/frame/listview.cpp index 68302f75f8..020491fc3f 100644 --- a/src/frame/listview.cpp +++ b/src/frame/listview.cpp @@ -225,6 +225,9 @@ ListView::ListView(QWidget *parent) setAttribute(Qt::WA_MacShowFocusRect); scheduleDelayedItemsLayout(); setMouseTracking(true); + + viewport()->setAutoFillBackground(false); + setAutoFillBackground(false); } ListView::~ListView() @@ -522,7 +525,6 @@ void ListView::paintEvent(QPaintEvent *e) int previousRow = -2; painter.setRenderHint(QPainter::Antialiasing); - painter.fillRect(e->rect(), palette().color(backgroundRole())); QVector::const_iterator end = toBeRendered.constEnd(); for (QVector::const_iterator it = toBeRendered.constBegin(); it != end; ++it) { diff --git a/src/frame/mainmodule.cpp b/src/frame/mainmodule.cpp index a23bc11995..5fd4659683 100644 --- a/src/frame/mainmodule.cpp +++ b/src/frame/mainmodule.cpp @@ -25,10 +25,10 @@ using namespace DCC_NAMESPACE; constexpr int NavViewMaximumWidth = QWIDGETSIZE_MAX; constexpr int NavViewMinimumWidth = 160; -constexpr QSize ListViewItemIconSize_IconMode(84, 84); +constexpr QSize ListViewItemIconSize_IconMode(64, 64); constexpr QSize ListViewItemGridSize_IconMode(280, 84); constexpr QSize ListViewItemIconSize_ListMode(32, 32); -constexpr QSize ListViewItemGridSize_ListMode(168, 48); +constexpr QSize ListViewItemGridSize_ListMode(168, 36); constexpr int ListView_ListMode_MaxWidth = 400; constexpr int ListView_IconMode_MaxWidth = 500; // mainly about icon and margin diff --git a/src/frame/mainwindow.cpp b/src/frame/mainwindow.cpp index d85d5e7399..263e280aa3 100644 --- a/src/frame/mainwindow.cpp +++ b/src/frame/mainwindow.cpp @@ -194,7 +194,9 @@ bool MainWindow::eventFilter(QObject *watched, QEvent *event) void MainWindow::initUI() { setMinimumSize(MainWindowMininumSize); - setCentralWidget(m_rootModule->activePage()); + auto root = m_rootModule->activePage(); + root->setAutoFillBackground(true); + setCentralWidget(root); layout()->setMargin(0); layout()->setSpacing(0); @@ -215,6 +217,7 @@ void MainWindow::initUI() }); m_backwardBtn->setAccessibleName("backwardbtn"); + m_backwardBtn->setFlat(true); titlebar()->addWidget(m_backwardBtn, Qt::AlignLeft | Qt::AlignVCenter); titlebar()->setIcon(DIconTheme::findQIcon("preferences-system"));