| 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 "application.h" |
| 22 |
#include "controller/configsetter.h" |
| 23 |
#include "navigationwidget.h" |
| 24 |
#include "widgets/imagebutton.h" |
| 25 |
#include "utils/baseutils.h" |
| 26 |
|
| 27 |
#include <QPainter> |
| 28 |
#include <dwindowclosebutton.h> |
| 29 |
#include <QMouseEvent> |
| 30 |
#include <QGraphicsDropShadowEffect> |
| 31 |
#include <QtDebug> |
| 32 |
#include <DDialogCloseButton> |
| 33 |
#include <DGuiApplicationHelper> |
| 34 |
|
| 35 |
namespace { |
| 36 |
|
| 37 |
const QString SETTINGS_GROUP = "ALBUM-VIEWPANEL"; |
| 38 |
const QString SETTINGS_ALWAYSHIDDEN_KEY = "NavigationAlwaysHidden"; |
| 39 |
const int IMAGE_MARGIN = 5; |
| 40 |
const int IMAGE_MARGIN_BOTTOM = 5; |
| 41 |
const QString ICON_CLOSE_NORMAL_LIGHT = ":/resources/light/button_tab_close_normal 2.svg"; |
| 42 |
const QString ICON_CLOSE_HOVER_LIGHT = ":/resources/light/button_tab_close_hover 2.svg"; |
| 43 |
const QString ICON_CLOSE_PRESS_LIGHT = ":/resources/light/button_tab_close_press 2.svg"; |
| 44 |
const QString ICON_CLOSE_NORMAL_DARK = ":/resources/dark/button_tab_close_normal 3.svg"; |
| 45 |
const QString ICON_CLOSE_HOVER_DARK = ":/resources/dark/button_tab_close_hover 3.svg"; |
| 46 |
const QString ICON_CLOSE_PRESS_DARK = ":/resources/dark/button_tab_close_press 3.svg"; |
| 47 |
|
| 48 |
} // namespace |
| 49 |
|
| 50 |
using namespace Dtk::Widget; |
| 51 |
|
| 52 |
NavigationWidget::NavigationWidget(QWidget *parent) |
| 53 |
: QWidget(parent) |
| 54 |
{ |
| 55 |
hide(); |
| 56 |
resize(150, 112); |
| 57 |
onThemeChanged(dApp->viewerTheme->getCurrentTheme()); |
| 58 |
|
| 59 |
ImageButton *closeBtn_light = new ImageButton(ICON_CLOSE_NORMAL_LIGHT, ICON_CLOSE_HOVER_LIGHT, ICON_CLOSE_PRESS_LIGHT, " ", this); |
| 60 |
closeBtn_light->setTooltipVisible(true); |
| 61 |
closeBtn_light->setFixedSize(32, 32); |
| 62 |
closeBtn_light->move(QPoint(this->x() + this->width() - 27 - 6, |
| 63 |
rect().topRight().y() + 4 - 6)); |
| 64 |
DPalette palette1 ; |
| 65 |
palette1.setColor(DPalette::Background, QColor(0, 0, 0, 1)); |
| 66 |
closeBtn_light->setPalette(palette1); |
| 67 |
closeBtn_light->hide(); |
| 68 |
connect(closeBtn_light, &ImageButton::clicked, [this]() { |
| 69 |
setAlwaysHidden(true); |
| 70 |
}); |
| 71 |
|
| 72 |
ImageButton *closeBtn_dark = new ImageButton(ICON_CLOSE_NORMAL_DARK, ICON_CLOSE_HOVER_DARK, ICON_CLOSE_PRESS_DARK, " ", this); |
| 73 |
closeBtn_dark->setTooltipVisible(true); |
| 74 |
closeBtn_dark->setFixedSize(32, 32); |
| 75 |
closeBtn_dark->move(QPoint(this->x() + this->width() - 27 - 6, |
| 76 |
rect().topRight().y() + 4 - 6)); |
| 77 |
DPalette palette2 ; |
| 78 |
palette2.setColor(DPalette::Background, QColor(0, 0, 0, 1)); |
| 79 |
closeBtn_dark->setPalette(palette2); |
| 80 |
closeBtn_dark->hide(); |
| 81 |
connect(closeBtn_dark, &ImageButton::clicked, [this]() { |
| 82 |
setAlwaysHidden(true); |
| 83 |
}); |
| 84 |
|
| 85 |
DGuiApplicationHelper::ColorType themeType1 = DGuiApplicationHelper::instance()->themeType(); |
| 86 |
if (themeType1 == DGuiApplicationHelper::DarkType) { |
| 87 |
closeBtn_light->hide(); |
| 88 |
closeBtn_dark->show(); |
| 89 |
} else { |
| 90 |
closeBtn_dark->hide(); |
| 91 |
closeBtn_light->show(); |
| 92 |
} |
| 93 |
|
| 94 |
QObject::connect(DGuiApplicationHelper::instance(), &DGuiApplicationHelper::themeTypeChanged, this, [ = ]() { |
| 95 |
DGuiApplicationHelper::ColorType themeType = DGuiApplicationHelper::instance()->themeType(); |
| 96 |
if (themeType == DGuiApplicationHelper::DarkType) { |
| 97 |
closeBtn_light->hide(); |
| 98 |
closeBtn_dark->show(); |
| 99 |
m_bgImgUrl = utils::view::naviwindow::DARK_BG_IMG ; |
| 100 |
m_BgColor = utils::view::naviwindow::DARK_BG_COLOR; |
| 101 |
m_mrBgColor = utils::view::naviwindow::DARK_MR_BG_COLOR; |
| 102 |
m_mrBorderColor = utils::view::naviwindow::DARK_MR_BORDER_Color; |
| 103 |
m_imgRBorderColor = utils::view::naviwindow:: DARK_IMG_R_BORDER_COLOR; |
| 104 |
} else { |
| 105 |
closeBtn_dark->hide(); |
| 106 |
closeBtn_light->show(); |
| 107 |
m_bgImgUrl = utils::view::naviwindow::LIGHT_BG_IMG ; |
| 108 |
m_BgColor = utils::view::naviwindow::LIGHT_BG_COLOR; |
| 109 |
m_mrBgColor = utils::view::naviwindow::LIGHT_MR_BG_COLOR; |
| 110 |
m_mrBorderColor = utils::view::naviwindow::LIGHT_MR_BORDER_Color; |
| 111 |
m_imgRBorderColor = utils::view::naviwindow::LIGHT_IMG_R_BORDER_COLOR; |
| 112 |
} |
| 113 |
}); |
| 114 |
|
| 115 |
m_mainRect = QRect(rect().x() + IMAGE_MARGIN, |
| 116 |
rect().y() + IMAGE_MARGIN_BOTTOM, |
| 117 |
rect().width() - IMAGE_MARGIN * 2, |
| 118 |
rect().height() - IMAGE_MARGIN_BOTTOM * 2); |
| 119 |
|
| 120 |
|
| 121 |
connect(dApp->viewerTheme, &ViewerThemeManager::viewerThemeChanged, this, &NavigationWidget::onThemeChanged); |
| 122 |
} |
| 123 |
|
| 124 |
void NavigationWidget::setAlwaysHidden(bool value) |
| 125 |
{ |
| 126 |
dApp->setter->setValue(SETTINGS_GROUP, SETTINGS_ALWAYSHIDDEN_KEY, |
| 127 |
QVariant(value)); |
| 128 |
if (isAlwaysHidden()) |
| 129 |
hide(); |
| 130 |
else |
| 131 |
show(); |
| 132 |
} |
| 133 |
|
| 134 |
bool NavigationWidget::isAlwaysHidden() const |
| 135 |
{ |
| 136 |
return dApp->setter->value(SETTINGS_GROUP, SETTINGS_ALWAYSHIDDEN_KEY, |
| 137 |
QVariant(false)).toBool(); |
| 138 |
} |
| 139 |
|
| 140 |
QPoint NavigationWidget::transImagePos(QPoint pos) |
| 141 |
{ |
| 142 |
return pos - QPoint(imageDrawRect.x(), imageDrawRect.y()); |
| 143 |
} |
| 144 |
|
| 145 |
void NavigationWidget::setImage(const QImage &img) |
| 146 |
{ |
| 147 |
const qreal ratio = devicePixelRatioF(); |
| 148 |
|
| 149 |
QRect tmpImageRect = QRect(m_mainRect.x(), m_mainRect.y(), |
| 150 |
qRound(m_mainRect.width() * ratio), |
| 151 |
qRound(m_mainRect.height() * ratio)); |
| 152 |
// QRect tmpImageRect = m_mainRect; |
| 153 |
|
| 154 |
m_originRect = img.rect(); |
| 155 |
|
| 156 |
// 只在图片比可显示区域大时才缩放 |
| 157 |
if (tmpImageRect.width() < m_originRect.width() || tmpImageRect.height() < m_originRect.height()) { |
| 158 |
m_img = img.scaled(tmpImageRect.size(), Qt::KeepAspectRatio, Qt::SmoothTransformation); |
| 159 |
} else { |
| 160 |
m_img = img; |
| 161 |
} |
| 162 |
|
| 163 |
m_pix = QPixmap::fromImage(m_img); |
| 164 |
m_pix.setDevicePixelRatio(ratio); |
| 165 |
m_imageScale = qMax(1.0, qMax(qreal(img.width()) / qreal(m_img.width()), qreal(img.height()) / qreal(m_img.height()))); |
| 166 |
// m_r = QRectF(0, 0, m_img.width() / ratio, m_img.height() / ratio); |
| 167 |
// m_widthScale = img.width() / m_img.width(); |
| 168 |
// m_heightScale = img.height() / m_img.height(); |
| 169 |
|
| 170 |
m_r = QRectF(0, 0, m_img.width(), m_img.height()); |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
imageDrawRect = QRect(static_cast<int>((m_mainRect.width() - m_img.width() / ratio) / 2 + IMAGE_MARGIN), |
| 175 |
static_cast<int>((m_mainRect.height() - m_img.height() / ratio) / 2 + utils::common::BORDER_WIDTH), |
| 176 |
static_cast<int>(m_img.width() / ratio), |
| 177 |
static_cast<int>(m_img.height() / ratio)); |
| 178 |
|
| 179 |
update(); |
| 180 |
} |
| 181 |
|
| 182 |
void NavigationWidget::setRectInImage(const QRect &r) |
| 183 |
{ |
| 184 |
if (m_img.isNull()) |
| 185 |
return; |
| 186 |
m_r.setX(qreal(r.x()) / m_imageScale); |
| 187 |
m_r.setY(qreal(r.y()) / m_imageScale); |
| 188 |
m_r.setWidth(qreal(r.width()) / m_imageScale); |
| 189 |
m_r.setHeight(qreal(r.height()) / m_imageScale); |
| 190 |
|
| 191 |
update(); |
| 192 |
} |
| 193 |
|
| 194 |
void NavigationWidget::mousePressEvent(QMouseEvent *e) |
| 195 |
{ |
| 196 |
if (e->button() == Qt::LeftButton) |
| 197 |
tryMoveRect(transImagePos(e->pos())); |
| 198 |
} |
| 199 |
|
| 200 |
void NavigationWidget::mouseMoveEvent(QMouseEvent *e) |
| 201 |
{ |
| 202 |
tryMoveRect(transImagePos(e->pos())); |
| 203 |
} |
| 204 |
|
| 205 |
void NavigationWidget::tryMoveRect(const QPoint &p) |
| 206 |
{ |
| 207 |
// int x0 = (m_mainRect.width() - m_img.width()) / 2 / devicePixelRatioF(); |
| 208 |
// int y0 = (m_mainRect.height() - m_img.height()) / 2 / devicePixelRatioF(); |
| 209 |
// const QRect imageRect(x0, y0, m_img.width(), m_img.height()); |
| 210 |
// if (! imageRect.contains(p)) |
| 211 |
// return; |
| 212 |
|
| 213 |
// if (!m_mainRect.contains(p)) |
| 214 |
// return; |
| 215 |
|
| 216 |
// const qreal x = 1.0 * (p.x() / devicePixelRatioF() - x0) / m_img.width() * m_originRect.width(); |
| 217 |
// const qreal y = 1.0 * (p.y() / devicePixelRatioF() - y0) / m_img.height() * m_originRect.height(); |
| 218 |
qreal x = p.x() * m_imageScale; |
| 219 |
qreal y = p.y() * m_imageScale; |
| 220 |
|
| 221 |
// qDebug() << p ; |
| 222 |
// qDebug() << x << y; |
| 223 |
// qDebug() << m_r; |
| 224 |
|
| 225 |
|
| 226 |
Q_EMIT requestMove(static_cast<int>(x), static_cast<int>(y)); |
| 227 |
} |
| 228 |
|
| 229 |
void NavigationWidget::paintEvent(QPaintEvent *) |
| 230 |
{ |
| 231 |
QImage img(m_img); |
| 232 |
if (m_img.isNull()) { |
| 233 |
QPainter p(this); |
| 234 |
p.fillRect(m_r, m_BgColor); |
| 235 |
return; |
| 236 |
} |
| 237 |
|
| 238 |
// const qreal ratio = devicePixelRatioF(); |
| 239 |
|
| 240 |
QPainter p(&img); |
| 241 |
p.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform); |
| 242 |
p.fillRect(m_r, m_mrBgColor); |
| 243 |
// p.setPen(m_mrBorderColor); |
| 244 |
p.setPen(QColor(0, 0, 0, 0)); |
| 245 |
// p.setPen(QPen(Qt::green)); |
| 246 |
|
| 247 |
|
| 248 |
p.drawRect(m_r); |
| 249 |
p.end(); |
| 250 |
p.begin(this); |
| 251 |
|
| 252 |
QImage background(m_bgImgUrl); |
| 253 |
|
| 254 |
p.drawImage(this->rect(), background); |
| 255 |
|
| 256 |
//**draw transparent background |
| 257 |
// QPixmap pm(12, 12); |
| 258 |
// QPainter pmp(&pm); |
| 259 |
// //TODO: the transparent box |
| 260 |
// //should not be scaled with the image |
| 261 |
// pmp.fillRect(0, 0, 6, 6, LIGHT_CHECKER_COLOR); |
| 262 |
// pmp.fillRect(6, 6, 6, 6, LIGHT_CHECKER_COLOR); |
| 263 |
// pmp.fillRect(0, 6, 6, 6, DARK_CHECKER_COLOR); |
| 264 |
// pmp.fillRect(6, 0, 6, 6, DARK_CHECKER_COLOR); |
| 265 |
// pmp.end(); |
| 266 |
|
| 267 |
// p.fillRect(imageDrawRect, QBrush(pm)); |
| 268 |
p.drawImage(imageDrawRect, img); |
| 269 |
QRect borderRect = QRect(imageDrawRect.x(), imageDrawRect.y() + 1, imageDrawRect.width(), imageDrawRect.height() + 1); |
| 270 |
// p.setPen(m_imgRBorderColor); |
| 271 |
p.setPen(QColor(0, 0, 0, 0)); |
| 272 |
// p.setPen(QPen(Qt::red)); |
| 273 |
p.drawRect(borderRect); |
| 274 |
p.end(); |
| 275 |
} |
| 276 |
|
| 277 |
void NavigationWidget::onThemeChanged(ViewerThemeManager::AppTheme theme) |
| 278 |
{ |
| 279 |
if (theme == ViewerThemeManager::Dark) { |
| 280 |
m_bgImgUrl = utils::view::naviwindow::DARK_BG_IMG ; |
| 281 |
m_BgColor = utils::view::naviwindow::DARK_BG_COLOR; |
| 282 |
m_mrBgColor = utils::view::naviwindow::DARK_MR_BG_COLOR; |
| 283 |
m_mrBorderColor = utils::view::naviwindow::DARK_MR_BORDER_Color; |
| 284 |
m_imgRBorderColor = utils::view::naviwindow:: DARK_IMG_R_BORDER_COLOR; |
| 285 |
} else { |
| 286 |
m_bgImgUrl = utils::view::naviwindow::LIGHT_BG_IMG ; |
| 287 |
m_BgColor = utils::view::naviwindow::LIGHT_BG_COLOR; |
| 288 |
m_mrBgColor = utils::view::naviwindow::LIGHT_MR_BG_COLOR; |
| 289 |
m_mrBorderColor = utils::view::naviwindow::LIGHT_MR_BORDER_Color; |
| 290 |
m_imgRBorderColor = utils::view::naviwindow::LIGHT_IMG_R_BORDER_COLOR; |
| 291 |
} |
| 292 |
update(); |
| 293 |
} |