Skip to content

Commit

Permalink
feat: 控制中心支持用户头像个性化配置
Browse files Browse the repository at this point in the history
使用新的账户头像资源,支持用户头像个性化配置

Log: 实现控制中心个性化头像定制功能
Resolve: linuxdeepin/developer-center#3796
Influence: 控制中心账户头像设置
  • Loading branch information
dengbo11 committed Apr 3, 2023
1 parent 06ca46c commit e92fc69
Show file tree
Hide file tree
Showing 18 changed files with 1,864 additions and 553 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ endif ()
set (MODULE_INSTALL_DIR "${CMAKE_INSTALL_FULL_LIBDIR}/dde-control-center/modules" CACHE STRING "Install dir for dde-control-center modules")

add_definitions(-DDefaultModuleDirectory="${MODULE_INSTALL_DIR}")

add_definitions(-DVarDirectory="${CMAKE_INSTALL_LOCALSTATEDIR}")
# Find the library
find_package(PkgConfig REQUIRED)
find_package(DtkCore REQUIRED)
Expand Down
3 changes: 2 additions & 1 deletion archlinux/PKGBUILD
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ build() {
-DDISABLE_UPDATE=ON \
-DDISABLE_AUTHENTICATION=ON \
-DDISABLE_LANGUAGE=ON \
-DCMAKE_INSTALL_LIBDIR=/usr/lib
-DCMAKE_INSTALL_LIBDIR=/usr/lib \
-DCMAKE_INSTALL_LOCALSTATEDIR=/var
ninja
}

Expand Down
3 changes: 2 additions & 1 deletion debian/rules
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ PACK_VER = $(shell echo $(VERSION) | awk -F'[+_~-]' '{print $$1}')
%:
dh $@ --parallel
override_dh_auto_configure:
dh_auto_configure -- -DCVERSION=$(DEB_VERSION_UPSTREAM) -DDVERSION=$(PACK_VER)
dh_auto_configure -- -DCVERSION=$(DEB_VERSION_UPSTREAM) -DDVERSION=$(PACK_VER) -DCMAKE_INSTALL_LOCALSTATEDIR=/var

30 changes: 17 additions & 13 deletions src/plugin-accounts/window/accountsmodule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "accountsmodule.h"
#include "avatarwidget.h"
#include "createaccountpage.h"
#include "dwidgetutil.h"
#include "groupitem.h"
#include "modifypasswdpage.h"
#include "removeuserdialog.h"
Expand Down Expand Up @@ -39,7 +40,7 @@
#include <DSysInfo>
#include <DDesktopServices>
#include <DFloatingButton>

#include <DBlurEffectWidget>

#include <polkit-qt5-1/PolkitQt1/Authority>

Expand Down Expand Up @@ -570,16 +571,16 @@ void AccountsModule::onModifyIcon()
QWidget *w = qobject_cast<QWidget *>(sender());
if (!w)
return;
AvatarListDialog *avatarListDialog = new AvatarListDialog(m_curUser, w);
avatarListDialog->deleteLater();
if (avatarListDialog->exec() == QDialog::Accepted) {
QString avatarpath = avatarListDialog->getAvatarPath();
if (!m_curUser) {
return;
}
if (!avatarpath.isEmpty() && avatarpath != m_curUser->currentAvatar())
m_worker->setAvatar(m_curUser, avatarpath);
}

AvatarListDialog *avatarListDialog = new AvatarListDialog(m_curUser);
avatarListDialog->show();

// 将窗口移动到屏幕中心位置
Dtk::Widget::moveToCenter(avatarListDialog);

connect(avatarListDialog, &AvatarListDialog::requestSaveAvatar, this, [this](const QString &path){
m_worker->setAvatar(m_curUser, path);
});
}

void AccountsModule::setCurrentUser(User *user)
Expand Down Expand Up @@ -658,8 +659,11 @@ void AccountsModule::changeUserGroup(const QStringList &groups)
int row_count = m_groupItemModel->rowCount();
for (int i = 0; i < row_count; ++i) {
QStandardItem *item = m_groupItemModel->item(i, 0);
item->setCheckState(item && groups.contains(item->text()) ? Qt::Checked : Qt::Unchecked);
item->setEnabled(item->text() != m_groupName);
if (item) {
item->setCheckState(groups.contains(item->text()) ? Qt::Checked
: Qt::Unchecked);
item->setEnabled(item->text() != m_groupName);
}
}
m_groupItemModel->sort(0);
}
Expand Down
49 changes: 49 additions & 0 deletions src/plugin-accounts/window/avatarcropbox.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// SPDX-FileCopyrightText: 2018 - 2023 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

#include "avatarcropbox.h"

#include <QPainter>

#define CropBoxSize 120

using namespace DCC_NAMESPACE;

AvatarCropBox::AvatarCropBox(QWidget *parent)
: QWidget(parent)
, m_backgroundColor(parent->palette().color(QPalette::Window))
{
setFixedSize(190, 190);
}

AvatarCropBox::~AvatarCropBox() { }

void AvatarCropBox::setBackgroundColor(QColor color)
{
m_backgroundColor = color;

repaint();
}

void AvatarCropBox::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event);

QPainter p(this);
p.setRenderHint(QPainter::Antialiasing);

auto rect = parentWidget()->rect();

QPainterPath border, cropBox;

border.setFillRule(Qt::WindingFill);
border.addRect(rect);

cropBox.setFillRule(Qt::WindingFill);
auto adjustSize = (rect.width() - CropBoxSize) / 2;
cropBox.addRoundedRect(rect.adjusted(adjustSize, adjustSize, -adjustSize, -adjustSize), 10, 10);

QPainterPath endPath = border.subtracted(cropBox);
p.fillPath(endPath, m_backgroundColor);
}
30 changes: 30 additions & 0 deletions src/plugin-accounts/window/avatarcropbox.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// SPDX-FileCopyrightText: 2018 - 2023 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once

#include "interface/namespace.h"

#include <QColor>
#include <QPainterPath>
#include <QWidget>

namespace DCC_NAMESPACE {

class AvatarCropBox : public QWidget
{
Q_OBJECT
public:
AvatarCropBox(QWidget *parent = nullptr);
~AvatarCropBox();

void setBackgroundColor(QColor color);

protected:
void paintEvent(QPaintEvent *event) override;

private:
QColor m_backgroundColor;
};

} // namespace DCC_NAMESPACE
49 changes: 29 additions & 20 deletions src/plugin-accounts/window/avataritemdelegate.cpp
Original file line number Diff line number Diff line change
@@ -1,51 +1,58 @@
//SPDX-FileCopyrightText: 2018 - 2023 UnionTech Software Technology Co., Ltd.
// SPDX-FileCopyrightText: 2018 - 2023 UnionTech Software Technology Co., Ltd.
//
//SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-License-Identifier: GPL-3.0-or-later
#include "avataritemdelegate.h"

#include "avatarlistwidget.h"

#include <DStyle>

#include <QObject>
#include <QStyledItemDelegate>
#include <QStyleOptionViewItem>
#include <QDebug>
#include <QModelIndex>
#include <QObject>
#include <QPainter>
#include <QPixmap>
#include <QPainterPath>
#include <QPen>
#include <QSize>
#include <QPixmap>
#include <QRect>
#include <QDebug>
#include <QPainterPath>
#include <QSize>
#include <QStyleOptionViewItem>
#include <QStyledItemDelegate>

DWIDGET_USE_NAMESPACE
using namespace DCC_NAMESPACE;

#define CustomAvatarRole 4

AvatarItemDelegate::AvatarItemDelegate(QObject *parent)
: QStyledItemDelegate(parent)
{
}

void AvatarItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
void AvatarItemDelegate::paint(QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
painter->setRenderHints(painter->renderHints()
| QPainter::Antialiasing
painter->setRenderHints(painter->renderHints() | QPainter::Antialiasing
| QPainter::SmoothPixmapTransform);

if (!index.isValid())
return;

QStyleOptionViewItem opt(option);
initStyleOption(&opt, index);
auto style = opt.widget->style();
opt.rect = opt.rect.adjusted(8, 8, -8, -8);
QStyle *style = option.widget ? option.widget->style() : QApplication::style();

opt.rect = opt.rect.adjusted(4, 4, -4, -4);

auto pm = static_cast<QStyle::PixelMetric>(DStyle::PM_FocusBorderWidth);
int borderWidth = style->pixelMetric(pm, &opt, nullptr);
pm = static_cast<QStyle::PixelMetric>(DStyle::PM_FocusBorderSpacing);
int borderSpacing = style->pixelMetric(pm, &opt, nullptr);
const QMargins margins(borderWidth + borderSpacing, borderWidth + borderSpacing,
borderWidth + borderSpacing, borderWidth + borderSpacing);
const QMargins margins(borderWidth + borderSpacing,
borderWidth + borderSpacing,
borderWidth + borderSpacing,
borderWidth + borderSpacing);
QPixmap pixmap = index.data(Qt::DecorationRole).value<QPixmap>();
QPainterPath path;
path.addRoundedRect(opt.rect.marginsRemoved(margins), 8, 8);
Expand All @@ -58,15 +65,16 @@ void AvatarItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &op
qreal tw = opt.rect.width() / 3.0;
qreal th = opt.rect.height() / 3.0;

//绘制背景
// 绘制背景
DStyleHelper dh(style);
QRectF rect(tw + opt.rect.x(), th + opt.rect.y(), tw, th);
rect.moveCenter(QRect(opt.rect).center());
painter->setPen(Qt::NoPen);
painter->setBrush(dh.getColor(&opt, QPalette::Button));

painter->drawRoundedRect(opt.rect.marginsRemoved(margins), 8, 8);

//画+号
// 画+号
qreal x1 = opt.rect.x() + tw;
qreal y1 = opt.rect.y() + opt.rect.height() / 2.0 - 0.5;
qreal x2 = opt.rect.x() + opt.rect.width() / 2.0 - 0.5;
Expand All @@ -81,7 +89,7 @@ void AvatarItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &op
painter->setBrush(Qt::NoBrush);
painter->drawRoundedRect(opt.rect.adjusted(1, 1, -1, -1), 8, 8);

//在中间绘制选中小图标
// 在中间绘制选中小图标
int radius = 8;
int cx = opt.rect.marginsRemoved(margins).right();
int cy = opt.rect.marginsRemoved(margins).top();
Expand All @@ -95,7 +103,8 @@ void AvatarItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &op
// draw + in the end
}

QSize AvatarItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
QSize AvatarItemDelegate::sizeHint(const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
Q_UNUSED(option)
Q_UNUSED(index)
Expand Down
15 changes: 9 additions & 6 deletions src/plugin-accounts/window/avataritemdelegate.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
//SPDX-FileCopyrightText: 2018 - 2023 UnionTech Software Technology Co., Ltd.
// SPDX-FileCopyrightText: 2018 - 2023 UnionTech Software Technology Co., Ltd.
//
//SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once

#include "interface/namespace.h"

#include <QStyledItemDelegate>
#include <QMetaType>
#include <QStyledItemDelegate>

struct LastItemData {
struct LastItemData
{
bool isDrawLast;
QString iconPath;
};
Expand All @@ -32,9 +33,11 @@ class AvatarItemDelegate : public QStyledItemDelegate
explicit AvatarItemDelegate(QObject *parent = nullptr);

// painting
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
void paint(QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index) const;

// set item size
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;
};
} // DCC_NAMESPACE
} // namespace DCC_NAMESPACE
Loading

0 comments on commit e92fc69

Please sign in to comment.