/* * Copyright (C) 2020 ~ 2021 Uniontech Software Technology Co., Ltd. * * Author: ZhangYong * * Maintainer: ZhangYong * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #include "albumimagebutton.h" #include #include #include #include DGUI_USE_NAMESPACE AlbumImageButton::AlbumImageButton(QWidget *parent) : DPushButton(parent), status(0), autoChecked(false) , transparent(true) { } AlbumImageButton::AlbumImageButton(const QString &normalPic, const QString &hoverPic, const QString &pressPic, const QString &checkedPic, QWidget *parent) : DPushButton(parent), status(0), autoChecked(false) , transparent(true) { defaultPicPath.normalPicPath = normalPic; defaultPicPath.hoverPicPath = hoverPic; defaultPicPath.pressPicPath = pressPic; defaultPicPath.checkedPicPath = checkedPic; auto pl = this->palette(); QColor sbcolor("#000000"); sbcolor.setAlphaF(0.05); pl.setColor(DPalette::Shadow, sbcolor); setPalette(pl); } void AlbumImageButton::setPropertyPic(const QString &propertyName, const QVariant &value, const QString &normalPic, const QString &hoverPic, const QString &pressPic, const QString &checkedPic) { MusicPicPathInfo curPicPath; curPicPath.normalPicPath = normalPic; curPicPath.hoverPicPath = hoverPic; curPicPath.pressPicPath = pressPic; curPicPath.checkedPicPath = checkedPic; if (propertyPicPaths.first == propertyName) { propertyPicPaths.second[value] = curPicPath; } else { QMap curPicPathInfo; curPicPathInfo.insert(value, curPicPath); propertyPicPaths.first = propertyName; propertyPicPaths.second = curPicPathInfo; } } void AlbumImageButton::setPropertyPic(const QString &normalPic, const QString &hoverPic, const QString &pressPic, const QString &checkedPic) { defaultPicPath.normalPicPath = normalPic; defaultPicPath.hoverPicPath = hoverPic; defaultPicPath.pressPicPath = pressPic; defaultPicPath.checkedPicPath = checkedPic; } void AlbumImageButton::paintEvent(QPaintEvent *event) { if (!transparent) { DPushButton::paintEvent(event); } QString curPicPath = defaultPicPath.normalPicPath; if (propertyPicPaths.first.isEmpty() || !propertyPicPaths.second.contains(property(propertyPicPaths.first.toStdString().data()))) { QString curPropertyPicPathStr; if (isChecked() && !defaultPicPath.checkedPicPath.isEmpty()) { curPropertyPicPathStr = defaultPicPath.checkedPicPath; } else { if (status == 1 && !defaultPicPath.hoverPicPath.isEmpty()) { curPropertyPicPathStr = defaultPicPath.hoverPicPath; } else if (status == 2 && !defaultPicPath.pressPicPath.isEmpty()) { curPropertyPicPathStr = defaultPicPath.pressPicPath; } } if (!curPropertyPicPathStr.isEmpty()) { curPicPath = curPropertyPicPathStr; } } else { QVariant value = property(propertyPicPaths.first.toStdString().data()); MusicPicPathInfo curPropertyPicPath = propertyPicPaths.second[value]; QString curPropertyPicPathStr; if (isChecked() && !defaultPicPath.checkedPicPath.isEmpty()) { curPropertyPicPathStr = curPropertyPicPath.checkedPicPath; } else { if (status == 1 && !defaultPicPath.hoverPicPath.isEmpty()) { curPropertyPicPathStr = curPropertyPicPath.hoverPicPath; } else if (status == 2 && !defaultPicPath.pressPicPath.isEmpty()) { curPropertyPicPathStr = curPropertyPicPath.pressPicPath; } else { curPropertyPicPathStr = curPropertyPicPath.normalPicPath; } } if (!curPropertyPicPathStr.isEmpty()) { curPicPath = curPropertyPicPathStr; } } QPixmap pixmap = DHiDPIHelper::loadNxPixmap(curPicPath); if (pixmap.isNull()) { pixmap = DHiDPIHelper::loadNxPixmap(defaultPicPath.normalPicPath); } pixmap.setDevicePixelRatio(devicePixelRatioF()); QPainter painter(this); painter.save(); painter.setRenderHint(QPainter::Antialiasing); painter.setRenderHint(QPainter::HighQualityAntialiasing); painter.setRenderHint(QPainter::SmoothPixmapTransform); int pixmapWidth = pixmap.rect().width() - 3;//按钮配置大小比资源图片小3像素 int pixmapHeight = pixmap.rect().height() - 3;//按钮配置大小比资源图片小3像素 QRect pixmapRect((rect().width() - pixmapWidth) / 2, (rect().height() - pixmapHeight) / 2 + 2, pixmapWidth, pixmapHeight);//按钮向下移动2px pixmapRect = pixmapRect.intersected(rect()); painter.drawPixmap(pixmapRect, pixmap, QRect(0, 0, pixmapWidth, pixmapHeight)); painter.restore(); } void AlbumImageButton::enterEvent(QEvent *event) { status = 1; DPushButton::enterEvent(event); if (autoChecked) { setChecked(true); } } void AlbumImageButton::leaveEvent(QEvent *event) { status = 0; DPushButton::leaveEvent(event); if (autoChecked) { setChecked(false); } } void AlbumImageButton::mousePressEvent(QMouseEvent *event) { status = 2; DPushButton::mousePressEvent(event); } void AlbumImageButton::mouseReleaseEvent(QMouseEvent *event) { status = 0; DPushButton::mouseReleaseEvent(event); }