| 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 "imgviewdelegate.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 "imgviewlistview.h" |
| 42 |
#include "imagedataservice.h" |
| 43 |
namespace { |
| 44 |
const QString IMAGE_DEFAULTTYPE = "All pics"; |
| 45 |
} |
| 46 |
|
| 47 |
const int NotSupportedOrDamagedWidth = 40; //损坏图片宽度 |
| 48 |
const int NotSupportedOrDamagedHeigh = 40; |
| 49 |
const QString LOCMAP_SELECTED_DARK = ":/resources/dark/images/58 drak.svg"; |
| 50 |
const QString LOCMAP_NOT_SELECTED_DARK = ":/resources/dark/images/imagewithbg-dark.svg"; |
| 51 |
const QString LOCMAP_SELECTED_LIGHT = ":/resources/light/images/58.svg"; |
| 52 |
const QString LOCMAP_NOT_SELECTED_LIGHT = ":/resources/light/images/imagewithbg.svg"; |
| 53 |
|
| 54 |
const int NORMAL_ITEM_PAINT_OFFSET = 10;//绘制时普通项向下偏移大小 |
| 55 |
const int SELECT_ITEM_PAINT_OFFSET = 5;//绘制时选中项向下偏移大小 |
| 56 |
|
| 57 |
ImgViewDelegate::ImgViewDelegate(QObject *parent) |
| 58 |
: QStyledItemDelegate(parent) |
| 59 |
{ |
| 60 |
m_default = utils::base::renderSVG(":/icons/deepin/builtin/icons/light/picture_default_light.svg", QSize(60, 45)); |
| 61 |
m_videoDefault = utils::base::renderSVG(":/icons/deepin/builtin/icons/light/video_default_light.svg", QSize(60, 45)); |
| 62 |
m_damagePixmap = utils::image::getDamagePixmap(DApplicationHelper::instance()->themeType() == DApplicationHelper::LightType); |
| 63 |
connect(DGuiApplicationHelper::instance(), &DGuiApplicationHelper::themeTypeChanged, this, |
| 64 |
&ImgViewDelegate::onThemeTypeChanged); |
| 65 |
} |
| 66 |
|
| 67 |
void ImgViewDelegate::setItemSize(QSize size) |
| 68 |
{ |
| 69 |
// m_size = size; |
| 70 |
} |
| 71 |
|
| 72 |
void ImgViewDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const |
| 73 |
{ |
| 74 |
painter->save(); |
| 75 |
QString pixmapstring; |
| 76 |
QPixmap _pixmap; |
| 77 |
const DBImgInfo data = itemData(index); |
| 78 |
if (data.itemType == ItemTypeBlank) { |
| 79 |
painter->restore(); |
| 80 |
return; |
| 81 |
} |
| 82 |
|
| 83 |
if (!ImageDataService::instance()->imageIsLoaded(data.filePath)) { |
| 84 |
_pixmap = m_default; |
| 85 |
} else { |
| 86 |
QImage img = ImageDataService::instance()->getThumnailImageByPath(data.filePath); |
| 87 |
if (img.isNull()) { |
| 88 |
if (data.itemType == ItemTypeVideo) { |
| 89 |
_pixmap = m_videoDefault; |
| 90 |
} else { |
| 91 |
_pixmap = m_damagePixmap; |
| 92 |
} |
| 93 |
} else { |
| 94 |
_pixmap = QPixmap::fromImage(img); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
bool selected = data.isSelected; |
| 100 |
if (/*(option.state & QStyle::State_MouseOver) &&*/ |
| 101 |
(option.state & QStyle::State_Selected) != 0) { |
| 102 |
selected = true; |
| 103 |
} |
| 104 |
painter->setRenderHints(QPainter::HighQualityAntialiasing | |
| 105 |
QPainter::SmoothPixmapTransform | |
| 106 |
QPainter::Antialiasing); |
| 107 |
QRect backgroundRect = option.rect; |
| 108 |
if (backgroundRect.width() != ImgViewListView::ITEM_CURRENT_WH) { |
| 109 |
backgroundRect.setTopLeft(QPoint(backgroundRect.topLeft() + QPoint(0, NORMAL_ITEM_PAINT_OFFSET))); |
| 110 |
backgroundRect.setBottomRight(QPoint(backgroundRect.bottomRight() + QPoint(0, NORMAL_ITEM_PAINT_OFFSET))); |
| 111 |
} else { |
| 112 |
backgroundRect.setTopLeft(QPoint(backgroundRect.topLeft() + QPoint(0, SELECT_ITEM_PAINT_OFFSET))); |
| 113 |
backgroundRect.setBottomRight(QPoint(backgroundRect.bottomRight() + QPoint(0, SELECT_ITEM_PAINT_OFFSET))); |
| 114 |
} |
| 115 |
QRect pixmapRect; |
| 116 |
QBrush backbrush; |
| 117 |
//当前显示项 |
| 118 |
if (backgroundRect.width() == ImgViewListView::ITEM_CURRENT_WH) { |
| 119 |
QPainterPath backgroundBp; |
| 120 |
backgroundBp.addRoundedRect(backgroundRect, 8, 8); |
| 121 |
painter->setClipPath(backgroundBp); |
| 122 |
|
| 123 |
backgroundRect.setX(backgroundRect.x() + 1); |
| 124 |
backgroundRect.setWidth(backgroundRect.width() - 1); |
| 125 |
painter->fillRect(backgroundRect, QBrush(DGuiApplicationHelper::instance()->applicationPalette().highlight().color())); |
| 126 |
|
| 127 |
pixmapstring = ""; |
| 128 |
DGuiApplicationHelper::ColorType themeType = DGuiApplicationHelper::instance()->themeType(); |
| 129 |
if (themeType == DGuiApplicationHelper::DarkType) { |
| 130 |
pixmapstring = LOCMAP_SELECTED_DARK; |
| 131 |
backbrush = QBrush(utils::common::DARK_BACKGROUND_COLOR); |
| 132 |
} else { |
| 133 |
pixmapstring = LOCMAP_SELECTED_LIGHT; |
| 134 |
backbrush = QBrush(utils::common::LIGHT_BACKGROUND_COLOR); |
| 135 |
} |
| 136 |
|
| 137 |
//绘制默认选中背景 |
| 138 |
QRect backRect(backgroundRect.x() + 4, backgroundRect.y() + 4, backgroundRect.width() - 8, backgroundRect.height() - 8); |
| 139 |
QPainterPath backBp; |
| 140 |
backBp.addRoundedRect(backRect, 4, 4); |
| 141 |
painter->setClipPath(backBp); |
| 142 |
painter->fillRect(backRect, backbrush); |
| 143 |
|
| 144 |
QPainterPath bg; |
| 145 |
bg.addRoundedRect(pixmapRect, 4, 4); |
| 146 |
if (!_pixmap.isNull()) { |
| 147 |
pixmapRect.setX(backgroundRect.x() + 4); |
| 148 |
pixmapRect.setY(backgroundRect.y() + 4); |
| 149 |
pixmapRect.setWidth(backgroundRect.width() - 8); |
| 150 |
pixmapRect.setHeight(backgroundRect.height() - 8); |
| 151 |
bg.addRoundedRect(pixmapRect, 4, 4); |
| 152 |
painter->setClipPath(bg); |
| 153 |
} |
| 154 |
} else { |
| 155 |
pixmapRect.setX(backgroundRect.x() + 1); |
| 156 |
pixmapRect.setY(backgroundRect.y() + 0); |
| 157 |
pixmapRect.setWidth(backgroundRect.width() - 2); |
| 158 |
pixmapRect.setHeight(backgroundRect.height() - 0); |
| 159 |
|
| 160 |
pixmapstring = ""; |
| 161 |
DGuiApplicationHelper::ColorType themeType = DGuiApplicationHelper::instance()->themeType(); |
| 162 |
if (themeType == DGuiApplicationHelper::DarkType) { |
| 163 |
pixmapstring = LOCMAP_NOT_SELECTED_DARK; |
| 164 |
} else { |
| 165 |
pixmapstring = LOCMAP_NOT_SELECTED_LIGHT; |
| 166 |
} |
| 167 |
|
| 168 |
QPixmap pixmap = utils::base::renderSVG(pixmapstring, QSize(32, 40)); |
| 169 |
QPainterPath bg; |
| 170 |
bg.addRoundedRect(pixmapRect, 4, 4); |
| 171 |
painter->setClipPath(bg); |
| 172 |
} |
| 173 |
|
| 174 |
QPainterPath bp1; |
| 175 |
bp1.addRoundedRect(pixmapRect, 4, 4); |
| 176 |
painter->setClipPath(bp1); |
| 177 |
painter->drawPixmap(pixmapRect, _pixmap); |
| 178 |
|
| 179 |
painter->restore(); |
| 180 |
} |
| 181 |
|
| 182 |
QSize ImgViewDelegate::sizeHint(const QStyleOptionViewItem &option, |
| 183 |
const QModelIndex &index) const |
| 184 |
{ |
| 185 |
Q_UNUSED(option) |
| 186 |
return index.data(Qt::SizeHintRole).value<QSize>(); |
| 187 |
} |
| 188 |
|
| 189 |
DBImgInfo ImgViewDelegate::itemData(const QModelIndex &index) const |
| 190 |
{ |
| 191 |
DBImgInfo data = index.data(Qt::DisplayRole).value<DBImgInfo>(); |
| 192 |
data.isSelected = index.data(Qt::UserRole).toBool(); |
| 193 |
return data; |
| 194 |
} |
| 195 |
|
| 196 |
bool ImgViewDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index) |
| 197 |
{ |
| 198 |
Q_UNUSED(model); |
| 199 |
|
| 200 |
return false; |
| 201 |
} |
| 202 |
|
| 203 |
void ImgViewDelegate::onThemeTypeChanged(int themeType) |
| 204 |
{ |
| 205 |
Q_UNUSED(themeType) |
| 206 |
m_damagePixmap = utils::image::getDamagePixmap(DApplicationHelper::instance()->themeType() == DApplicationHelper::LightType); |
| 207 |
|
| 208 |
if (DApplicationHelper::instance()->themeType() == DApplicationHelper::LightType) { |
| 209 |
m_default = utils::base::renderSVG(":/icons/deepin/builtin/icons/light/picture_default_light.svg", QSize(60, 45)); |
| 210 |
m_videoDefault = utils::base::renderSVG(":/icons/deepin/builtin/icons/light/video_default_light.svg", QSize(60, 45)); |
| 211 |
} else { |
| 212 |
m_default = utils::base::renderSVG("::/icons/deepin/builtin/icons/dark/picture_default_dark.svg", QSize(60, 45)); |
| 213 |
m_videoDefault = utils::base::renderSVG(":/icons/deepin/builtin/icons/dark/video_default_dark.svg", QSize(60, 45)); |
| 214 |
} |
| 215 |
} |