Skip to content

Commit

Permalink
chore: Tweak UI to adapt to sidebar blur
Browse files Browse the repository at this point in the history
tweak UI to adapt to sidebar blur

Log:
  • Loading branch information
wangfei authored and FeiWang1119 committed Feb 1, 2024
1 parent 28c8157 commit 60200fd
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 11 deletions.
27 changes: 20 additions & 7 deletions src/frame/listitemdelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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 {
Expand All @@ -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());
Expand All @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion src/frame/listview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,9 @@ ListView::ListView(QWidget *parent)
setAttribute(Qt::WA_MacShowFocusRect);
scheduleDelayedItemsLayout();
setMouseTracking(true);

viewport()->setAutoFillBackground(false);
setAutoFillBackground(false);
}

ListView::~ListView()
Expand Down Expand Up @@ -522,7 +525,6 @@ void ListView::paintEvent(QPaintEvent *e)
int previousRow = -2;

painter.setRenderHint(QPainter::Antialiasing);
painter.fillRect(e->rect(), palette().color(backgroundRole()));

QVector<QModelIndex>::const_iterator end = toBeRendered.constEnd();
for (QVector<QModelIndex>::const_iterator it = toBeRendered.constBegin(); it != end; ++it) {
Expand Down
4 changes: 2 additions & 2 deletions src/frame/mainmodule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion src/frame/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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"));
Expand Down

0 comments on commit 60200fd

Please sign in to comment.