| 1 |
/* |
| 2 |
* Copyright (C) 2020 ~ 2021 Uniontech Software Technology Co., Ltd. |
| 3 |
* |
| 4 |
* Author: ZhangYong <[email protected]> |
| 5 |
* |
| 6 |
* Maintainer: ZhangYong <[email protected]> |
| 7 |
* |
| 8 |
* This program is free software: you can redistribute it and/or modify |
| 9 |
* it under the terms of the GNU General Public License as published by |
| 10 |
* the Free Software Foundation, either version 3 of the License, or |
| 11 |
* any later version. |
| 12 |
* |
| 13 |
* This program is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
* GNU General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU General Public License |
| 19 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 |
*/ |
| 21 |
#include "albumimagebutton.h" |
| 22 |
|
| 23 |
#include <QDebug> |
| 24 |
#include <QPainter> |
| 25 |
|
| 26 |
#include <DHiDPIHelper> |
| 27 |
#include <DPalette> |
| 28 |
|
| 29 |
DGUI_USE_NAMESPACE |
| 30 |
|
| 31 |
AlbumImageButton::AlbumImageButton(QWidget *parent) |
| 32 |
: DPushButton(parent), status(0), autoChecked(false) |
| 33 |
, transparent(true) |
| 34 |
{ |
| 35 |
} |
| 36 |
|
| 37 |
AlbumImageButton::AlbumImageButton(const QString &normalPic, const QString &hoverPic, |
| 38 |
const QString &pressPic, const QString &checkedPic, QWidget *parent) |
| 39 |
: DPushButton(parent), status(0), autoChecked(false) |
| 40 |
, transparent(true) |
| 41 |
{ |
| 42 |
defaultPicPath.normalPicPath = normalPic; |
| 43 |
defaultPicPath.hoverPicPath = hoverPic; |
| 44 |
defaultPicPath.pressPicPath = pressPic; |
| 45 |
defaultPicPath.checkedPicPath = checkedPic; |
| 46 |
|
| 47 |
auto pl = this->palette(); |
| 48 |
QColor sbcolor("#000000"); |
| 49 |
sbcolor.setAlphaF(0.05); |
| 50 |
pl.setColor(DPalette::Shadow, sbcolor); |
| 51 |
setPalette(pl); |
| 52 |
} |
| 53 |
|
| 54 |
void AlbumImageButton::setPropertyPic(const QString &propertyName, const QVariant &value, |
| 55 |
const QString &normalPic, const QString &hoverPic, const QString &pressPic, const QString &checkedPic) |
| 56 |
{ |
| 57 |
MusicPicPathInfo curPicPath; |
| 58 |
curPicPath.normalPicPath = normalPic; |
| 59 |
curPicPath.hoverPicPath = hoverPic; |
| 60 |
curPicPath.pressPicPath = pressPic; |
| 61 |
curPicPath.checkedPicPath = checkedPic; |
| 62 |
|
| 63 |
if (propertyPicPaths.first == propertyName) { |
| 64 |
propertyPicPaths.second[value] = curPicPath; |
| 65 |
} else { |
| 66 |
QMap<QVariant, MusicPicPathInfo> curPicPathInfo; |
| 67 |
curPicPathInfo.insert(value, curPicPath); |
| 68 |
propertyPicPaths.first = propertyName; |
| 69 |
propertyPicPaths.second = curPicPathInfo; |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
void AlbumImageButton::setPropertyPic(const QString &normalPic, const QString &hoverPic, const QString &pressPic, const QString &checkedPic) |
| 74 |
{ |
| 75 |
defaultPicPath.normalPicPath = normalPic; |
| 76 |
defaultPicPath.hoverPicPath = hoverPic; |
| 77 |
defaultPicPath.pressPicPath = pressPic; |
| 78 |
defaultPicPath.checkedPicPath = checkedPic; |
| 79 |
} |
| 80 |
|
| 81 |
void AlbumImageButton::paintEvent(QPaintEvent *event) |
| 82 |
{ |
| 83 |
if (!transparent) { |
| 84 |
DPushButton::paintEvent(event); |
| 85 |
} |
| 86 |
|
| 87 |
QString curPicPath = defaultPicPath.normalPicPath; |
| 88 |
if (propertyPicPaths.first.isEmpty() || !propertyPicPaths.second.contains(property(propertyPicPaths.first.toStdString().data()))) { |
| 89 |
QString curPropertyPicPathStr; |
| 90 |
if (isChecked() && !defaultPicPath.checkedPicPath.isEmpty()) { |
| 91 |
curPropertyPicPathStr = defaultPicPath.checkedPicPath; |
| 92 |
} else { |
| 93 |
if (status == 1 && !defaultPicPath.hoverPicPath.isEmpty()) { |
| 94 |
curPropertyPicPathStr = defaultPicPath.hoverPicPath; |
| 95 |
} else if (status == 2 && !defaultPicPath.pressPicPath.isEmpty()) { |
| 96 |
curPropertyPicPathStr = defaultPicPath.pressPicPath; |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
if (!curPropertyPicPathStr.isEmpty()) { |
| 101 |
curPicPath = curPropertyPicPathStr; |
| 102 |
} |
| 103 |
} else { |
| 104 |
QVariant value = property(propertyPicPaths.first.toStdString().data()); |
| 105 |
MusicPicPathInfo curPropertyPicPath = propertyPicPaths.second[value]; |
| 106 |
QString curPropertyPicPathStr; |
| 107 |
if (isChecked() && !defaultPicPath.checkedPicPath.isEmpty()) { |
| 108 |
curPropertyPicPathStr = curPropertyPicPath.checkedPicPath; |
| 109 |
} else { |
| 110 |
if (status == 1 && !defaultPicPath.hoverPicPath.isEmpty()) { |
| 111 |
curPropertyPicPathStr = curPropertyPicPath.hoverPicPath; |
| 112 |
} else if (status == 2 && !defaultPicPath.pressPicPath.isEmpty()) { |
| 113 |
curPropertyPicPathStr = curPropertyPicPath.pressPicPath; |
| 114 |
} else { |
| 115 |
curPropertyPicPathStr = curPropertyPicPath.normalPicPath; |
| 116 |
} |
| 117 |
} |
| 118 |
if (!curPropertyPicPathStr.isEmpty()) { |
| 119 |
curPicPath = curPropertyPicPathStr; |
| 120 |
} |
| 121 |
} |
| 122 |
QPixmap pixmap = DHiDPIHelper::loadNxPixmap(curPicPath); |
| 123 |
if (pixmap.isNull()) { |
| 124 |
pixmap = DHiDPIHelper::loadNxPixmap(defaultPicPath.normalPicPath); |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
pixmap.setDevicePixelRatio(devicePixelRatioF()); |
| 129 |
QPainter painter(this); |
| 130 |
painter.save(); |
| 131 |
painter.setRenderHint(QPainter::Antialiasing); |
| 132 |
painter.setRenderHint(QPainter::HighQualityAntialiasing); |
| 133 |
painter.setRenderHint(QPainter::SmoothPixmapTransform); |
| 134 |
|
| 135 |
int pixmapWidth = pixmap.rect().width() - 3;//按钮配置大小比资源图片小3像素 |
| 136 |
int pixmapHeight = pixmap.rect().height() - 3;//按钮配置大小比资源图片小3像素 |
| 137 |
QRect pixmapRect((rect().width() - pixmapWidth) / 2, (rect().height() - pixmapHeight) / 2 + 2, pixmapWidth, pixmapHeight);//按钮向下移动2px |
| 138 |
pixmapRect = pixmapRect.intersected(rect()); |
| 139 |
painter.drawPixmap(pixmapRect, pixmap, QRect(0, 0, pixmapWidth, pixmapHeight)); |
| 140 |
|
| 141 |
painter.restore(); |
| 142 |
} |
| 143 |
|
| 144 |
void AlbumImageButton::enterEvent(QEvent *event) |
| 145 |
{ |
| 146 |
status = 1; |
| 147 |
DPushButton::enterEvent(event); |
| 148 |
if (autoChecked) { |
| 149 |
setChecked(true); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
void AlbumImageButton::leaveEvent(QEvent *event) |
| 154 |
{ |
| 155 |
status = 0; |
| 156 |
DPushButton::leaveEvent(event); |
| 157 |
if (autoChecked) { |
| 158 |
setChecked(false); |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
void AlbumImageButton::mousePressEvent(QMouseEvent *event) |
| 163 |
{ |
| 164 |
status = 2; |
| 165 |
DPushButton::mousePressEvent(event); |
| 166 |
} |
| 167 |
|
| 168 |
void AlbumImageButton::mouseReleaseEvent(QMouseEvent *event) |
| 169 |
{ |
| 170 |
status = 0; |
| 171 |
DPushButton::mouseReleaseEvent(event); |
| 172 |
} |