| 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 <QVBoxLayout> |
| 22 |
#include <QDebug> |
| 23 |
#include <QPainter> |
| 24 |
|
| 25 |
#include <DSuggestButton> |
| 26 |
|
| 27 |
#include "thumbnailwidget.h" |
| 28 |
#include "application.h" |
| 29 |
#include "utils/baseutils.h" |
| 30 |
#include <QImageReader> |
| 31 |
#include <DGuiApplicationHelper> |
| 32 |
#include "controller/signalmanager.h" |
| 33 |
#include <DLabel> |
| 34 |
#include <DFontSizeManager> |
| 35 |
|
| 36 |
const QSize THUMBNAIL_BORDERSIZE = QSize(130, 130); |
| 37 |
const QSize Album_THUMBNAIL_SIZE = QSize(128, 128); |
| 38 |
const QString ICON_IMPORT_PHOTO_DARK = ":/resources/dark/images/icon_import_photo dark.svg"; |
| 39 |
const QString ICON_IMPORT_PHOTO_LIGHT = ":/resources/light/images/icon_import_photo.svg"; |
| 40 |
|
| 41 |
ThumbnailWidget::ThumbnailWidget(const QString &darkFile, |
| 42 |
const QString &lightFile, QWidget *parent) |
| 43 |
: ThemeWidget(darkFile, lightFile, parent) |
| 44 |
, m_picString("") |
| 45 |
{ |
| 46 |
|
| 47 |
// m_picString = ""; |
| 48 |
DGuiApplicationHelper::ColorType themeType1 = DGuiApplicationHelper::instance()->themeType(); |
| 49 |
if (themeType1 == DGuiApplicationHelper::DarkType) { |
| 50 |
m_picString = ICON_IMPORT_PHOTO_DARK; |
| 51 |
m_theme = true; |
| 52 |
} else { |
| 53 |
m_picString = ICON_IMPORT_PHOTO_LIGHT; |
| 54 |
m_theme = false; |
| 55 |
} |
| 56 |
|
| 57 |
QPixmap logo_pix1 = utils::base::renderSVG(m_picString, Album_THUMBNAIL_SIZE); |
| 58 |
m_logo = logo_pix1; |
| 59 |
|
| 60 |
QObject::connect(DGuiApplicationHelper::instance(), &DGuiApplicationHelper::themeTypeChanged, this, &ThumbnailWidget::onThemeTypeChanged); |
| 61 |
|
| 62 |
setMouseTracking(true); |
| 63 |
m_thumbnailLabel = new QLabel(this); |
| 64 |
m_thumbnailLabel->setFixedSize(THUMBNAIL_BORDERSIZE); |
| 65 |
onThemeChanged(dApp->viewerTheme->getCurrentTheme()); |
| 66 |
|
| 67 |
#ifndef LITE_DIV |
| 68 |
m_tips = new QLabel(this); |
| 69 |
m_tips->setObjectName("ThumbnailTips"); |
| 70 |
m_tips->setText(tr("No image files found")); |
| 71 |
#else |
| 72 |
|
| 73 |
DLabel *tips = new DLabel(this); |
| 74 |
tips->setText(tr("Photo not found")); |
| 75 |
DFontSizeManager::instance()->bind(tips, DFontSizeManager::T6); |
| 76 |
tips->setForegroundRole(DPalette::TextTips); |
| 77 |
tips->hide(); |
| 78 |
|
| 79 |
connect(dApp->signalM, &SignalManager::picNotExists, this, [ = ](bool visible) { |
| 80 |
if (visible) { |
| 81 |
tips->show(); |
| 82 |
} else { |
| 83 |
tips->hide(); |
| 84 |
} |
| 85 |
}); |
| 86 |
#endif |
| 87 |
QVBoxLayout *layout = new QVBoxLayout(this); |
| 88 |
layout->addStretch(); |
| 89 |
layout->addWidget(m_thumbnailLabel, 0, Qt::AlignCenter); |
| 90 |
layout->addSpacing(9); |
| 91 |
#ifndef LITE_DIV |
| 92 |
layout->addWidget(m_tips, 0, Qt::AlignCenter); |
| 93 |
#else |
| 94 |
layout->addWidget(tips, 0, Qt::AlignCenter); |
| 95 |
#endif |
| 96 |
layout->addStretch(); |
| 97 |
setLayout(layout); |
| 98 |
|
| 99 |
connect(dApp->viewerTheme, &ViewerThemeManager::viewerThemeChanged, this, &ThumbnailWidget::onThemeChanged); |
| 100 |
} |
| 101 |
|
| 102 |
void ThumbnailWidget::onThemeChanged(ViewerThemeManager::AppTheme theme) |
| 103 |
{ |
| 104 |
if (theme == ViewerThemeManager::Dark) { |
| 105 |
m_inBorderColor = utils::common::DARK_BORDER_COLOR; |
| 106 |
if (m_isDefaultThumbnail) |
| 107 |
m_defaultImage = m_logo; |
| 108 |
} else { |
| 109 |
m_inBorderColor = utils::common::LIGHT_BORDER_COLOR; |
| 110 |
if (m_isDefaultThumbnail) |
| 111 |
m_defaultImage = m_logo; |
| 112 |
} |
| 113 |
|
| 114 |
ThemeWidget::onThemeChanged(theme); |
| 115 |
update(); |
| 116 |
} |
| 117 |
|
| 118 |
void ThumbnailWidget::setThumbnailImage(const QPixmap thumbnail) |
| 119 |
{ |
| 120 |
if (thumbnail.isNull()) { |
| 121 |
m_defaultImage = m_logo; |
| 122 |
m_isDefaultThumbnail = true; |
| 123 |
} else { |
| 124 |
m_defaultImage = thumbnail; |
| 125 |
m_isDefaultThumbnail = false; |
| 126 |
} |
| 127 |
|
| 128 |
update(); |
| 129 |
} |
| 130 |
|
| 131 |
void ThumbnailWidget::onThemeTypeChanged() |
| 132 |
{ |
| 133 |
DGuiApplicationHelper::ColorType themeType = DGuiApplicationHelper::instance()->themeType(); |
| 134 |
// m_picString = ""; |
| 135 |
if (themeType == DGuiApplicationHelper::DarkType) { |
| 136 |
m_picString = ICON_IMPORT_PHOTO_DARK; |
| 137 |
m_theme = true; |
| 138 |
} else { |
| 139 |
m_picString = ICON_IMPORT_PHOTO_LIGHT; |
| 140 |
m_theme = false; |
| 141 |
} |
| 142 |
|
| 143 |
QPixmap logo_pix = utils::base::renderSVG(m_picString, Album_THUMBNAIL_SIZE); |
| 144 |
m_logo = logo_pix; |
| 145 |
if (m_isDefaultThumbnail) |
| 146 |
m_defaultImage = logo_pix; |
| 147 |
update(); |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
//bool ThumbnailWidget::isDefaultThumbnail() |
| 152 |
//{ |
| 153 |
// return m_isDefaultThumbnail; |
| 154 |
//} |
| 155 |
|
| 156 |
void ThumbnailWidget::paintEvent(QPaintEvent *event) |
| 157 |
{ |
| 158 |
Q_UNUSED(event); |
| 159 |
if (m_defaultImage.isNull()) { |
| 160 |
// if (m_theme) { |
| 161 |
// m_defaultImage = m_logo; |
| 162 |
// } else { |
| 163 |
// m_defaultImage = m_logo; |
| 164 |
// } |
| 165 |
m_defaultImage = m_logo; |
| 166 |
m_isDefaultThumbnail = true; |
| 167 |
} |
| 168 |
|
| 169 |
if (m_defaultImage.size() != Album_THUMBNAIL_SIZE) { |
| 170 |
m_defaultImage = m_defaultImage.scaled(Album_THUMBNAIL_SIZE, |
| 171 |
Qt::KeepAspectRatio, Qt::SmoothTransformation); |
| 172 |
} |
| 173 |
|
| 174 |
QPoint startPoint = mapToParent(QPoint(m_thumbnailLabel->x(), |
| 175 |
m_thumbnailLabel->y())); |
| 176 |
QPoint imgStartPoint = QPoint(startPoint.x() + (Album_THUMBNAIL_SIZE.width() - |
| 177 |
128) / 2 + 1, startPoint.y() + (Album_THUMBNAIL_SIZE.height() |
| 178 |
- 128) / 2 + 1); |
| 179 |
QRect imgRect = QRect(imgStartPoint.x(), imgStartPoint.y(), |
| 180 |
128, 128); |
| 181 |
|
| 182 |
QPainter painter(this); |
| 183 |
painter.setRenderHints(QPainter::HighQualityAntialiasing | |
| 184 |
QPainter::SmoothPixmapTransform); |
| 185 |
painter.drawPixmap(imgRect, m_defaultImage); |
| 186 |
} |
| 187 |
|
| 188 |
void ThumbnailWidget::mouseMoveEvent(QMouseEvent *event) |
| 189 |
{ |
| 190 |
QWidget::mouseMoveEvent(event); |
| 191 |
|
| 192 |
emit mouseHoverMoved(); |
| 193 |
} |
| 194 |
|
| 195 |
ThumbnailWidget::~ThumbnailWidget() |
| 196 |
{ |
| 197 |
} |