| 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 "thumbnaildelegate.h" |
| 22 |
#include "utils/imageutils.h" |
| 23 |
#include "utils/baseutils.h" |
| 24 |
#include "application.h" |
| 25 |
#include <QDateTime> |
| 26 |
|
| 27 |
#include <QHBoxLayout> |
| 28 |
#include <QLineEdit> |
| 29 |
#include <QPainter> |
| 30 |
#include <QPixmapCache> |
| 31 |
#include <QStandardItemModel> |
| 32 |
#include <QThread> |
| 33 |
#include <QTimer> |
| 34 |
#include <QPainterPath> |
| 35 |
#include <QMouseEvent> |
| 36 |
#include <QImageReader> |
| 37 |
#include <QApplication> |
| 38 |
#include <DFontSizeManager> |
| 39 |
#include <DGuiApplicationHelper> |
| 40 |
|
| 41 |
#include "imageengineapi.h" |
| 42 |
#include "imagedataservice.h" |
| 43 |
#include "signalmanager.h" |
| 44 |
namespace { |
| 45 |
const QString IMAGE_DEFAULTTYPE = "All pics"; |
| 46 |
} |
| 47 |
|
| 48 |
const int NotSupportedOrDamagedWidth = 40; //损坏图片宽度 |
| 49 |
const int NotSupportedOrDamagedHeigh = 40; |
| 50 |
|
| 51 |
ThumbnailDelegate::ThumbnailDelegate(DelegateType type, QObject *parent) |
| 52 |
: QStyledItemDelegate(parent) |
| 53 |
, m_imageTypeStr(IMAGE_DEFAULTTYPE) |
| 54 |
, selectedPixmapLight(utils::base::renderSVG(":/resources/images/other/select_active.svg", QSize(28, 28))) |
| 55 |
, selectedPixmapDark(utils::base::renderSVG(":/images/logo/resources/images/other/select_active_dark.svg", QSize(28, 28))) |
| 56 |
, m_delegatetype(type) |
| 57 |
{ |
| 58 |
m_default = utils::base::renderSVG(":/icons/deepin/builtin/icons/light/picture_default_light.svg", QSize(60, 45)); |
| 59 |
m_videoDefault = utils::base::renderSVG(":/icons/deepin/builtin/icons/light/video_default_light.svg", QSize(60, 45)); |
| 60 |
m_damagePixmap = utils::image::getDamagePixmap(DApplicationHelper::instance()->themeType() == DApplicationHelper::LightType); |
| 61 |
connect(DGuiApplicationHelper::instance(), &DGuiApplicationHelper::themeTypeChanged, this, |
| 62 |
&ThumbnailDelegate::onThemeTypeChanged); |
| 63 |
} |
| 64 |
|
| 65 |
void ThumbnailDelegate::setItemSize(QSize size) |
| 66 |
{ |
| 67 |
m_size = size; |
| 68 |
} |
| 69 |
|
| 70 |
void ThumbnailDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const |
| 71 |
{ |
| 72 |
if (!bneedpaint) { |
| 73 |
return; |
| 74 |
} |
| 75 |
const DBImgInfo data = itemData(index); |
| 76 |
switch (data.itemType) { |
| 77 |
case ItemType::ItemTypeBlank: { |
| 78 |
//空白项,什么都不绘制 |
| 79 |
break; |
| 80 |
} |
| 81 |
case ItemType::ItemTypeTimeLineTitle: { |
| 82 |
break; |
| 83 |
} |
| 84 |
case ItemType::ItemTypeImportTimeLineTitle: { |
| 85 |
break; |
| 86 |
} |
| 87 |
case ItemType::ItemTypePic: |
| 88 |
case ItemType::ItemTypeVideo: { |
| 89 |
drawImgAndVideo(painter, option, index); |
| 90 |
break; |
| 91 |
} |
| 92 |
default: |
| 93 |
drawImgAndVideo(painter, option, index); |
| 94 |
break; |
| 95 |
} |
| 96 |
return; |
| 97 |
} |
| 98 |
|
| 99 |
void ThumbnailDelegate::drawImgAndVideo(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const |
| 100 |
{ |
| 101 |
painter->save(); |
| 102 |
const DBImgInfo data = itemData(index); |
| 103 |
bool selected = false; |
| 104 |
if (/*(option.state & QStyle::State_MouseOver) &&*/ |
| 105 |
(option.state & QStyle::State_Selected) != 0) { |
| 106 |
selected = true; |
| 107 |
} |
| 108 |
painter->setRenderHints(QPainter::HighQualityAntialiasing | |
| 109 |
QPainter::SmoothPixmapTransform | |
| 110 |
QPainter::Antialiasing); |
| 111 |
QRect backgroundRect = option.rect; |
| 112 |
//选中阴影框 |
| 113 |
if (selected) { |
| 114 |
QPainterPath backgroundBp; |
| 115 |
backgroundBp.addRoundedRect(backgroundRect, utils::common::SHADOW_BORDER_RADIUS, utils::common::SHADOW_BORDER_RADIUS); |
| 116 |
painter->setClipPath(backgroundBp); |
| 117 |
|
| 118 |
QBrush shadowbrush; |
| 119 |
DGuiApplicationHelper::ColorType themeType = DGuiApplicationHelper::instance()->themeType(); |
| 120 |
if (themeType == DGuiApplicationHelper::LightType) { |
| 121 |
shadowbrush = QBrush(QColor("#DEDEDE")); |
| 122 |
} |
| 123 |
if (themeType == DGuiApplicationHelper::DarkType) { |
| 124 |
shadowbrush = QBrush(QColor("#4F4F4F")); |
| 125 |
} |
| 126 |
painter->fillRect(backgroundRect, shadowbrush); |
| 127 |
|
| 128 |
//绘制选中默认背景 |
| 129 |
QRect backRect(backgroundRect.x() + 8, backgroundRect.y() + 8, backgroundRect.width() - 16, backgroundRect.height() - 16); |
| 130 |
QPainterPath backBp; |
| 131 |
backBp.addRoundedRect(backRect, utils::common::BORDER_RADIUS, utils::common::BORDER_RADIUS); |
| 132 |
painter->setClipPath(backBp); |
| 133 |
painter->fillRect(backRect, shadowbrush); |
| 134 |
} |
| 135 |
|
| 136 |
QRect pixmapRect; |
| 137 |
QImage img = ImageDataService::instance()->getThumnailImageByPath(data.filePath); |
| 138 |
if (img.isNull()) { |
| 139 |
pixmapRect.setX(backgroundRect.x() + backgroundRect.width() / 2 - NotSupportedOrDamagedWidth / 2); |
| 140 |
pixmapRect.setY(backgroundRect.y() + backgroundRect.height() / 2 - NotSupportedOrDamagedHeigh / 2); |
| 141 |
pixmapRect.setWidth(NotSupportedOrDamagedWidth); |
| 142 |
pixmapRect.setHeight(NotSupportedOrDamagedHeigh); |
| 143 |
} else { |
| 144 |
pixmapRect.setX(backgroundRect.x() + 8); |
| 145 |
pixmapRect.setY(backgroundRect.y() + 8);; |
| 146 |
pixmapRect.setWidth(backgroundRect.width() - 16); |
| 147 |
pixmapRect.setHeight(backgroundRect.height() - 16); |
| 148 |
} |
| 149 |
//2020/6/9 DJH UI 透明图片背景 |
| 150 |
QBrush transparentbrush; |
| 151 |
DGuiApplicationHelper::ColorType themeType1 = DGuiApplicationHelper::instance()->themeType(); |
| 152 |
if (themeType1 == DGuiApplicationHelper::LightType) { |
| 153 |
transparentbrush = QBrush(QColor("#FFFFFF")); |
| 154 |
} else if (themeType1 == DGuiApplicationHelper::DarkType) { //#BUG77517,去除下方的虚化代码,改为直接填充黑色 |
| 155 |
transparentbrush = QBrush(QColor("#000000")); |
| 156 |
} |
| 157 |
QRect transparentRect(backgroundRect.x() + 8, backgroundRect.y() + 8, backgroundRect.width() - 16, backgroundRect.height() - 16); |
| 158 |
QPainterPath transparentBp; |
| 159 |
transparentBp.addRoundedRect(transparentRect, utils::common::BORDER_RADIUS, utils::common::BORDER_RADIUS); |
| 160 |
painter->setClipPath(transparentBp); |
| 161 |
painter->fillRect(transparentRect, transparentbrush); |
| 162 |
|
| 163 |
QPainterPath bp1; |
| 164 |
bp1.addRoundedRect(pixmapRect, utils::common::BORDER_RADIUS, utils::common::BORDER_RADIUS); |
| 165 |
painter->setClipPath(bp1); |
| 166 |
|
| 167 |
if (!ImageDataService::instance()->imageIsLoaded(data.filePath)) { |
| 168 |
painter->drawPixmap(pixmapRect, m_default); |
| 169 |
} else { |
| 170 |
QImage img = ImageDataService::instance()->getThumnailImageByPath(data.filePath); |
| 171 |
if (img.isNull()) { |
| 172 |
if (data.itemType == ItemTypeVideo) { |
| 173 |
painter->drawPixmap(pixmapRect, m_videoDefault); |
| 174 |
} else { |
| 175 |
painter->drawPixmap(pixmapRect, m_damagePixmap); |
| 176 |
} |
| 177 |
} else { |
| 178 |
painter->drawPixmap(pixmapRect, QPixmap::fromImage(img)); |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
//绘制选中图标 |
| 183 |
if (selected) { |
| 184 |
QPixmap selectedPixmap; |
| 185 |
DGuiApplicationHelper::ColorType themeType3 = DGuiApplicationHelper::instance()->themeType(); |
| 186 |
if (themeType3 == DGuiApplicationHelper::LightType) { |
| 187 |
selectedPixmap = selectedPixmapLight; |
| 188 |
} |
| 189 |
if (themeType3 == DGuiApplicationHelper::DarkType) { |
| 190 |
selectedPixmap = selectedPixmapDark; |
| 191 |
} |
| 192 |
// QRect selectedRect(backgroundRect.x() + backgroundRect.width() - 28, backgroundRect.y(), 28, 28); |
| 193 |
QRect selectedRect(backgroundRect.x() + backgroundRect.width() - 30, backgroundRect.y() + 4, 28, 28); |
| 194 |
QPainterPath selectedBp; |
| 195 |
selectedBp.addRoundedRect(selectedRect, utils::common::BORDER_RADIUS, utils::common::BORDER_RADIUS); |
| 196 |
painter->setClipPath(selectedBp); |
| 197 |
painter->drawPixmap(selectedRect, selectedPixmap); |
| 198 |
} |
| 199 |
|
| 200 |
//绘制剩余天数 |
| 201 |
if (COMMON_STR_TRASH == m_imageTypeStr) { |
| 202 |
QPainterPath bp; |
| 203 |
bp.addRoundedRect(backgroundRect, utils::common::BORDER_RADIUS, utils::common::BORDER_RADIUS); |
| 204 |
painter->setClipPath(bp); |
| 205 |
painter->setPen(QColor(85, 85, 85, 170)); //边框颜色:灰色 |
| 206 |
//字符串的像素宽度 |
| 207 |
const int m_Width = painter->fontMetrics().width(data.remainDays); |
| 208 |
painter->setBrush(QBrush(QColor(85, 85, 85, 170)));//填充颜色:灰色 |
| 209 |
QString str(data.remainDays); |
| 210 |
QFontMetrics Text(str); |
| 211 |
|
| 212 |
//2020/3/13-xiaolong |
| 213 |
int textwidth = m_Width + 6; //阴影图框:6:左3像素+右3像素 |
| 214 |
int textheight = DFontSizeManager::instance()->fontPixelSize(painter->font()); |
| 215 |
int rectwidth = backgroundRect.width() - 8; //缩略图宽度:总宽度减去选中框宽度8 |
| 216 |
if (textwidth > rectwidth) { //容纳文字像素的宽度大于缩略图宽度 |
| 217 |
textwidth = rectwidth - 4;//减少距离:左右各2 |
| 218 |
} |
| 219 |
int tempcha = (rectwidth - textwidth > 4) ? (rectwidth - textwidth - 4) : 4; |
| 220 |
int posx = backgroundRect.x() + tempcha; //剩余天数起始坐标 |
| 221 |
//文字背景圆角矩形框弧度,与字号相关 |
| 222 |
int radious = 6; |
| 223 |
if (textheight < 14) { |
| 224 |
radious = 4; |
| 225 |
} |
| 226 |
|
| 227 |
painter->drawRoundedRect(posx, backgroundRect.y() + backgroundRect.height() - textheight - 14, |
| 228 |
textwidth, textheight + 2, radious, radious); //Y参数:backgroundRect宽度-文字宽度-14边距 |
| 229 |
|
| 230 |
painter->setPen(QColor(255, 255, 255)); |
| 231 |
if (m_Width - textwidth > 0) { |
| 232 |
str = Text.elidedText(str, Qt::ElideRight, textwidth); |
| 233 |
} |
| 234 |
painter->drawText(posx + 3, backgroundRect.y() + backgroundRect.height() - 15, str);//在框中绘制文字,起点位置离最下方15像素 |
| 235 |
} |
| 236 |
//绘制视频时间 |
| 237 |
if (data.itemType == ItemTypeVideo) { |
| 238 |
QString str(ImageDataService::instance()->getMovieDurationStrByPath(data.filePath)); |
| 239 |
//TODO 暂时屏蔽最近删除界面的视频时间显示,待需求明确显示位置后修改并解除屏蔽 |
| 240 |
if (dApp->signalM->getSliderValue() > 2 && !str.isEmpty() && m_delegatetype != AlbumViewTrashType) { |
| 241 |
QPainterPath bp; |
| 242 |
bp.addRoundedRect(backgroundRect, utils::common::BORDER_RADIUS, utils::common::BORDER_RADIUS); |
| 243 |
painter->setClipPath(bp); |
| 244 |
painter->setPen(QColor(85, 85, 85, 170)); //边框颜色:灰色 |
| 245 |
//字符串的像素宽度 |
| 246 |
painter->setBrush(QBrush(QColor(85, 85, 85, 170)));//填充颜色:灰色 |
| 247 |
|
| 248 |
const int m_Width = painter->fontMetrics().width(str); |
| 249 |
QFontMetrics Text(str); |
| 250 |
|
| 251 |
//2020/3/13-xiaolong |
| 252 |
int textwidth = m_Width + 6; //阴影图框:6:左3像素+右3像素 |
| 253 |
int textheight = DFontSizeManager::instance()->fontPixelSize(painter->font()); |
| 254 |
int rectwidth = backgroundRect.width() - 8; //缩略图宽度:总宽度减去选中框宽度8 |
| 255 |
if (textwidth > rectwidth) { //容纳文字像素的宽度大于缩略图宽度 |
| 256 |
textwidth = rectwidth - 4;//减少距离:左右各2 |
| 257 |
} |
| 258 |
int posx = backgroundRect.x() + backgroundRect.width() - m_Width - 16; //时长起始坐标 |
| 259 |
//文字背景圆角矩形框弧度,与字号相关 |
| 260 |
int radious = 6; |
| 261 |
if (textheight < 14) { |
| 262 |
radious = 4; |
| 263 |
} |
| 264 |
|
| 265 |
painter->drawRoundedRect(posx, backgroundRect.y() + backgroundRect.height() - textheight - 14, |
| 266 |
textwidth, textheight + 2, radious, radious); //Y参数:backgroundRect宽度-文字宽度-14边距 |
| 267 |
|
| 268 |
painter->setPen(QColor(255, 255, 255)); |
| 269 |
if (m_Width - textwidth > 0) { |
| 270 |
str = Text.elidedText(str, Qt::ElideRight, textwidth); |
| 271 |
} |
| 272 |
painter->drawText(posx + 3, backgroundRect.y() + backgroundRect.height() - 15, str); |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
//绘制心形图标 |
| 277 |
if (COMMON_STR_FAVORITES == m_imageTypeStr) { |
| 278 |
QPainterPath bp; |
| 279 |
bp.addRoundedRect(backgroundRect, utils::common::BORDER_RADIUS, utils::common::BORDER_RADIUS); |
| 280 |
painter->setClipPath(bp); |
| 281 |
|
| 282 |
QPixmap favPixmap = utils::base::renderSVG(":/resources/images/other/fav_icon .svg", QSize(20, 20)); |
| 283 |
painter->drawPixmap(backgroundRect.x() + 20, backgroundRect.y() + backgroundRect.height() - 30, favPixmap); |
| 284 |
} |
| 285 |
|
| 286 |
painter->restore(); |
| 287 |
} |
| 288 |
|
| 289 |
void ThumbnailDelegate::onThemeTypeChanged(int themeType) |
| 290 |
{ |
| 291 |
Q_UNUSED(themeType) |
| 292 |
m_damagePixmap = utils::image::getDamagePixmap(DApplicationHelper::instance()->themeType() == DApplicationHelper::LightType); |
| 293 |
|
| 294 |
if (DApplicationHelper::instance()->themeType() == DApplicationHelper::LightType) { |
| 295 |
m_default = utils::base::renderSVG(":/icons/deepin/builtin/icons/light/picture_default_light.svg", QSize(60, 45)); |
| 296 |
m_videoDefault = utils::base::renderSVG(":/icons/deepin/builtin/icons/light/video_default_light.svg", QSize(60, 45)); |
| 297 |
} else { |
| 298 |
m_default = utils::base::renderSVG("::/icons/deepin/builtin/icons/dark/picture_default_dark.svg", QSize(60, 45)); |
| 299 |
m_videoDefault = utils::base::renderSVG(":/icons/deepin/builtin/icons/dark/video_default_dark.svg", QSize(60, 45)); |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
QSize ThumbnailDelegate::sizeHint(const QStyleOptionViewItem &option, |
| 304 |
const QModelIndex &index) const |
| 305 |
{ |
| 306 |
Q_UNUSED(option) |
| 307 |
DBImgInfo data = index.data(Qt::DisplayRole).value<DBImgInfo>(); |
| 308 |
if (data.itemType == ItemType::ItemTypeBlank |
| 309 |
|| data.itemType == ItemType::ItemTypeTimeLineTitle |
| 310 |
|| data.itemType == ItemType::ItemTypeImportTimeLineTitle) { |
| 311 |
QSize size(data.imgWidth, data.imgHeight); |
| 312 |
return size; |
| 313 |
} else { |
| 314 |
return m_size; |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
DBImgInfo ThumbnailDelegate::itemData(const QModelIndex &index) const |
| 319 |
{ |
| 320 |
DBImgInfo data = index.data(Qt::DisplayRole).value<DBImgInfo>(); |
| 321 |
data.isSelected = index.data(Qt::UserRole).toBool(); |
| 322 |
return data; |
| 323 |
} |
| 324 |
|
| 325 |
bool ThumbnailDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index) |
| 326 |
{ |
| 327 |
Q_UNUSED(model); |
| 328 |
if (!index.isValid()) |
| 329 |
return false; |
| 330 |
//跟随收藏按钮变化位置 |
| 331 |
QRect rect = QRect(option.rect.x() + 20, option.rect.y() + option.rect.height() - 30, 20, 20); |
| 332 |
QMouseEvent *pMouseEvent = static_cast<QMouseEvent *>(event); |
| 333 |
if (COMMON_STR_FAVORITES == m_imageTypeStr) { |
| 334 |
if (event->type() == QEvent::MouseButtonPress) { |
| 335 |
const DBImgInfo data = itemData(index); |
| 336 |
bool blast = false; |
| 337 |
if (!blast && rect.contains(pMouseEvent->pos())) { |
| 338 |
emit sigCancelFavorite(index); |
| 339 |
} else if (blast && event->type() == QEvent::MouseButtonPress && rect.contains(pMouseEvent->x(), pMouseEvent->y() + 27)) { |
| 340 |
emit sigCancelFavorite(index); |
| 341 |
} |
| 342 |
} |
| 343 |
} |
| 344 |
return false; |
| 345 |
} |