| 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 "imageview.h" |
| 22 |
|
| 23 |
#include <QDebug> |
| 24 |
#include <QFile> |
| 25 |
#include <QOpenGLWidget> |
| 26 |
#include <QWheelEvent> |
| 27 |
#include <QMouseEvent> |
| 28 |
#include <QMovie> |
| 29 |
#include <QGraphicsRectItem> |
| 30 |
#include <QGraphicsSvgItem> |
| 31 |
#include <QGraphicsPixmapItem> |
| 32 |
#include <QPaintEvent> |
| 33 |
#include <QtConcurrent> |
| 34 |
#include <QHBoxLayout> |
| 35 |
#include <qmath.h> |
| 36 |
#include <QScrollBar> |
| 37 |
#include <QGestureEvent> |
| 38 |
#include <QSvgRenderer> |
| 39 |
#include <QtGlobal> |
| 40 |
#include <QDesktopWidget> |
| 41 |
|
| 42 |
#include "graphicsitem.h" |
| 43 |
#include "utils/baseutils.h" |
| 44 |
#include "utils/imageutils.h" |
| 45 |
#include "utils/unionimage.h" |
| 46 |
#include "application.h" |
| 47 |
#include "ac-desktop-define.h" |
| 48 |
#include <DGuiApplicationHelper> |
| 49 |
#include "controller/signalmanager.h" |
| 50 |
#include "imagedataservice.h" |
| 51 |
|
| 52 |
#include <imageengine/imageengineapi.h> |
| 53 |
|
| 54 |
#ifndef QT_NO_OPENGL |
| 55 |
//#include <QGLWidget> |
| 56 |
#endif |
| 57 |
|
| 58 |
#include <sys/inotify.h> |
| 59 |
|
| 60 |
DWIDGET_USE_NAMESPACE |
| 61 |
|
| 62 |
namespace { |
| 63 |
|
| 64 |
const QColor LIGHT_CHECKER_COLOR = QColor("#FFFFFF"); |
| 65 |
const QColor DARK_CHECKER_COLOR = QColor("#CCCCCC"); |
| 66 |
|
| 67 |
#ifndef tablet_PC |
| 68 |
const qreal MAX_SCALE_FACTOR = 20.0; |
| 69 |
const qreal MIN_SCALE_FACTOR = 0.02; |
| 70 |
#else |
| 71 |
const qreal MAX_SCALE_FACTOR = 2.0; |
| 72 |
qreal MIN_SCALE_FACTOR = 0.0; |
| 73 |
#endif |
| 74 |
|
| 75 |
QVariantList cachePixmap(const QString &path) |
| 76 |
{ |
| 77 |
QImage tImg; |
| 78 |
QString errMsg; |
| 79 |
UnionImage_NameSpace::loadStaticImageFromFile(path, tImg, errMsg); |
| 80 |
QPixmap p = QPixmap::fromImage(tImg); |
| 81 |
if (QFileInfo(path).exists() && p.isNull()) { |
| 82 |
//判定为损坏图片 |
| 83 |
p = utils::image::getDamagePixmap(DApplicationHelper::instance()->themeType() == DApplicationHelper::LightType); |
| 84 |
qDebug() << errMsg; |
| 85 |
} |
| 86 |
QVariantList vl; |
| 87 |
vl << QVariant(path) << QVariant(p); |
| 88 |
return vl; |
| 89 |
} |
| 90 |
|
| 91 |
} // namespace |
| 92 |
ImageView::ImageView(QWidget *parent) |
| 93 |
: QGraphicsView(parent) |
| 94 |
, m_renderer(Native) |
| 95 |
, m_pool(new QThreadPool(this)) |
| 96 |
// , m_svgItem(nullptr) |
| 97 |
, m_movieItem(nullptr) |
| 98 |
, m_pixmapItem(nullptr) |
| 99 |
{ |
| 100 |
this->setObjectName("ImageView"); |
| 101 |
onThemeChanged(dApp->viewerTheme->getCurrentTheme()); |
| 102 |
setScene(new QGraphicsScene(this)); |
| 103 |
setMouseTracking(true); |
| 104 |
setTransformationAnchor(AnchorUnderMouse); |
| 105 |
setDragMode(ScrollHandDrag); |
| 106 |
setViewportUpdateMode(FullViewportUpdate); |
| 107 |
setAcceptDrops(false); |
| 108 |
setResizeAnchor(QGraphicsView::AnchorViewCenter); |
| 109 |
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); |
| 110 |
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); |
| 111 |
setFrameShape(QFrame::Shape::NoFrame); |
| 112 |
|
| 113 |
viewport()->setCursor(Qt::ArrowCursor); |
| 114 |
|
| 115 |
grabGesture(Qt::PinchGesture); |
| 116 |
grabGesture(Qt::SwipeGesture); |
| 117 |
|
| 118 |
connect(&m_watcher, &QFutureWatcherBase::finished, this, &ImageView::onCacheFinish); |
| 119 |
connect(dApp->viewerTheme, &ViewerThemeManager::viewerThemeChanged, this, &ImageView::onThemeChanged); |
| 120 |
m_pool->setMaxThreadCount(1); |
| 121 |
m_loadTimer = new QTimer(this); |
| 122 |
m_loadTimer->setSingleShot(true); |
| 123 |
m_loadTimer->setInterval(300); |
| 124 |
|
| 125 |
connect(m_loadTimer, &QTimer::timeout, this, &ImageView::onLoadTimerTimeout); |
| 126 |
QObject::connect(DGuiApplicationHelper::instance(), &DGuiApplicationHelper::themeTypeChanged, this, &ImageView::onThemeTypeChanged); |
| 127 |
m_imgFileWatcher = new QFileSystemWatcher(this); |
| 128 |
connect(m_imgFileWatcher, &QFileSystemWatcher::fileChanged, this, &ImageView::onImgFileChanged); |
| 129 |
m_isChangedTimer = new QTimer(this); |
| 130 |
QObject::connect(m_isChangedTimer, &QTimer::timeout, this, &ImageView::onIsChangedTimerTimeout); |
| 131 |
} |
| 132 |
|
| 133 |
ImageView::~ImageView() |
| 134 |
{ |
| 135 |
if (m_imgFileWatcher) { |
| 136 |
// m_imgFileWatcher->clear(); |
| 137 |
// m_imgFileWatcher->quit(); |
| 138 |
// m_imgFileWatcher->terminate(); |
| 139 |
// m_imgFileWatcher->wait(); |
| 140 |
m_imgFileWatcher->deleteLater(); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
void ImageView::clear() |
| 145 |
{ |
| 146 |
if (m_pixmapItem != nullptr) { |
| 147 |
delete m_pixmapItem; |
| 148 |
m_pixmapItem = nullptr; |
| 149 |
} |
| 150 |
m_movieItem = nullptr; |
| 151 |
scene()->clear(); |
| 152 |
} |
| 153 |
|
| 154 |
void ImageView::setImage(const QString &path, const QImage &image) |
| 155 |
{ |
| 156 |
m_loadPath = path; |
| 157 |
// Empty path will cause crash in release-build mode |
| 158 |
if (path.isEmpty()) { |
| 159 |
return; |
| 160 |
} |
| 161 |
m_imgFileWatcher->removePath(m_path); |
| 162 |
m_path = path; |
| 163 |
m_imgFileWatcher->addPath(m_path); |
| 164 |
QString strfixL = QFileInfo(path).suffix().toUpper(); |
| 165 |
QGraphicsScene *s = scene(); |
| 166 |
QFileInfo fi(path); |
| 167 |
QStringList fList = UnionImage_NameSpace::supportMovieFormat(); //"gif","mng" |
| 168 |
//QMovie can't read frameCount of "mng" correctly,so change |
| 169 |
//the judge way to solve the problem |
| 170 |
if (fList.contains(strfixL)) { |
| 171 |
if (m_pixmapItem != nullptr) { |
| 172 |
delete m_pixmapItem; |
| 173 |
m_pixmapItem = nullptr; |
| 174 |
} |
| 175 |
s->clear(); |
| 176 |
resetTransform(); |
| 177 |
m_movieItem = new GraphicsMovieItem(path, strfixL); |
| 178 |
// m_movieItem->start(); |
| 179 |
// Make sure item show in center of view after reload |
| 180 |
setSceneRect(m_movieItem->boundingRect()); |
| 181 |
qDebug() << "m_movieItem->boundingRect() = " << m_movieItem->boundingRect(); |
| 182 |
s->addItem(m_movieItem); |
| 183 |
emit imageChanged(path); |
| 184 |
QMetaObject::invokeMethod(this, [ = ]() { |
| 185 |
resetTransform(); |
| 186 |
autoFit(); |
| 187 |
}, Qt::QueuedConnection); |
| 188 |
} else { |
| 189 |
//当传入的image无效时,需要重新读取数据 |
| 190 |
m_movieItem = nullptr; |
| 191 |
qDebug() << "Start cache pixmap: " << path; |
| 192 |
if (image.isNull()) { |
| 193 |
QImageReader imagreader(path); //取原图的分辨率 |
| 194 |
int w = imagreader.size().width(); |
| 195 |
int h = imagreader.size().height(); |
| 196 |
scene()->clear(); |
| 197 |
resetTransform(); |
| 198 |
QImage image = ImageDataService::instance()->getThumnailImageByPath(path); //内存中的数据 |
| 199 |
int wScale = 0; |
| 200 |
int hScale = 0; |
| 201 |
int wWindow = 0; |
| 202 |
int hWindow = 0; |
| 203 |
if (QApplication::activeWindow()) { |
| 204 |
wWindow = QApplication::activeWindow()->width(); |
| 205 |
hWindow = QApplication::activeWindow()->height(); |
| 206 |
} else { |
| 207 |
wWindow = 1300; |
| 208 |
hWindow = 848; |
| 209 |
} |
| 210 |
|
| 211 |
if (w >= wWindow) { |
| 212 |
wScale = wWindow; |
| 213 |
hScale = wScale * h / w; |
| 214 |
if (hScale > hWindow) { |
| 215 |
hScale = hWindow; |
| 216 |
wScale = hScale * w / h; |
| 217 |
} |
| 218 |
} else if (h >= hWindow) { |
| 219 |
hScale = hWindow; |
| 220 |
wScale = hScale * w / h; |
| 221 |
if (wScale >= wWindow) { |
| 222 |
wScale = wWindow; |
| 223 |
hScale = wScale * h / w; |
| 224 |
} |
| 225 |
} else { |
| 226 |
wScale = w; |
| 227 |
hScale = h; |
| 228 |
} |
| 229 |
if (wScale == 0 || wScale == -1) { |
| 230 |
wScale = wWindow; |
| 231 |
hScale = hWindow; |
| 232 |
} |
| 233 |
|
| 234 |
QPixmap pix = QPixmap::fromImage(image.scaled(wScale, hScale, Qt::KeepAspectRatio));//缩放到原图大小 |
| 235 |
m_pixmapItem = new GraphicsPixmapItem(pix); |
| 236 |
m_pixmapItem->setTransformationMode(Qt::SmoothTransformation); |
| 237 |
// Make sure item show in center of view after reload |
| 238 |
m_blurEffect = new QGraphicsBlurEffect; |
| 239 |
m_blurEffect->setBlurRadius(5); |
| 240 |
m_blurEffect->setBlurHints(QGraphicsBlurEffect::PerformanceHint); |
| 241 |
m_pixmapItem->setGraphicsEffect(m_blurEffect); |
| 242 |
m_loadTimer->start(); |
| 243 |
setSceneRect(m_pixmapItem->boundingRect()); |
| 244 |
scene()->addItem(m_pixmapItem); |
| 245 |
emit imageChanged(path); |
| 246 |
QMetaObject::invokeMethod(this, [ = ]() { |
| 247 |
resetTransform(); |
| 248 |
}, Qt::QueuedConnection); |
| 249 |
} else { |
| 250 |
//当传入的image有效时,直接刷入图像,不再重复读取 |
| 251 |
QPixmap pix = QPixmap::fromImage(image); |
| 252 |
pix.setDevicePixelRatio(devicePixelRatioF()); |
| 253 |
m_pixmapItem = new GraphicsPixmapItem(pix); |
| 254 |
m_pixmapItem->setTransformationMode(Qt::SmoothTransformation); |
| 255 |
setSceneRect(m_pixmapItem->boundingRect()); |
| 256 |
scene()->clear(); |
| 257 |
scene()->addItem(m_pixmapItem); |
| 258 |
autoFit(); |
| 259 |
emit imageChanged(path); |
| 260 |
this->update(); |
| 261 |
emit hideNavigation(); |
| 262 |
} |
| 263 |
} |
| 264 |
m_firstset = true; |
| 265 |
} |
| 266 |
|
| 267 |
void ImageView::setScaleValue(qreal v) |
| 268 |
{ |
| 269 |
scale(v, v); |
| 270 |
#ifdef tablet_PC |
| 271 |
//平板模式下限制缩放范围:初始值--200% |
| 272 |
if (m_firstset) { |
| 273 |
m_firstset = false; |
| 274 |
m_value = imageRelativeScale(); |
| 275 |
} |
| 276 |
const qreal irs = imageRelativeScale(); |
| 277 |
if ((v < 1 && irs <= m_value)) { |
| 278 |
const qreal minv = m_value / irs; |
| 279 |
scale(minv, minv); |
| 280 |
} else if (v > 1 && irs >= m_max_scale_factor) { |
| 281 |
const qreal maxv = m_max_scale_factor / irs; |
| 282 |
scale(maxv, maxv); |
| 283 |
} else { |
| 284 |
m_isFitImage = false; |
| 285 |
m_isFitWindow = false; |
| 286 |
} |
| 287 |
#else |
| 288 |
const qreal irs = imageRelativeScale(); |
| 289 |
// Rollback |
| 290 |
if ((v < 1 && irs <= MIN_SCALE_FACTOR)) { |
| 291 |
const qreal minv = MIN_SCALE_FACTOR / irs; |
| 292 |
scale(minv, minv); |
| 293 |
} else if (v > 1 && irs >= MAX_SCALE_FACTOR) { |
| 294 |
const qreal maxv = MAX_SCALE_FACTOR / irs; |
| 295 |
scale(maxv, maxv); |
| 296 |
} else { |
| 297 |
m_isFitImage = false; |
| 298 |
m_isFitWindow = false; |
| 299 |
} |
| 300 |
#endif |
| 301 |
|
| 302 |
qreal rescale = imageRelativeScale(); |
| 303 |
if (rescale - 1 > -0.01 && |
| 304 |
rescale - 1 < 0.01) { |
| 305 |
emit checkAdaptImageBtn(); |
| 306 |
} else { |
| 307 |
emit disCheckAdaptImageBtn(); |
| 308 |
} |
| 309 |
|
| 310 |
qreal wrs = windowRelativeScale(); |
| 311 |
if (rescale - wrs > -0.01 && |
| 312 |
rescale - wrs < 0.01) { |
| 313 |
emit checkAdaptScreenBtn(); |
| 314 |
} else { |
| 315 |
emit disCheckAdaptScreenBtn(); |
| 316 |
} |
| 317 |
emit scaled(imageRelativeScale() * 100); |
| 318 |
emit showScaleLabel(); |
| 319 |
emit transformChanged(); |
| 320 |
} |
| 321 |
|
| 322 |
void ImageView::autoFit() |
| 323 |
{ |
| 324 |
//确认场景加载出来后,才能调用场景内的item |
| 325 |
// if (!scene()->isActive()) |
| 326 |
// return; |
| 327 |
if (image().isNull()) |
| 328 |
return; |
| 329 |
QSize image_size = image().size(); |
| 330 |
if ((image_size.width() >= width() || |
| 331 |
image_size.height() >= height()) && |
| 332 |
width() > 0 && height() > 0) { |
| 333 |
fitWindow(); |
| 334 |
} else { |
| 335 |
fitImage(); |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
const QImage ImageView::image() |
| 340 |
{ |
| 341 |
if (m_movieItem) { // bit-map |
| 342 |
return m_movieItem->pixmap().toImage(); |
| 343 |
} else if (m_pixmapItem) { |
| 344 |
//FIXME: access to m_pixmapItem will crash |
| 345 |
if (nullptr == m_pixmapItem) { //add to slove crash by shui |
| 346 |
return QImage(); |
| 347 |
} |
| 348 |
return m_pixmapItem->pixmap().toImage(); |
| 349 |
} else { |
| 350 |
return QImage(); |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
void ImageView::fitWindow() |
| 355 |
{ |
| 356 |
qreal wrs = windowRelativeScale(); |
| 357 |
resetTransform(); |
| 358 |
scale(wrs, wrs); |
| 359 |
emit checkAdaptScreenBtn(); |
| 360 |
if (wrs - 1 > -0.01 && |
| 361 |
wrs - 1 < 0.01) { |
| 362 |
emit checkAdaptImageBtn(); |
| 363 |
} else { |
| 364 |
emit disCheckAdaptImageBtn(); |
| 365 |
} |
| 366 |
m_isFitImage = false; |
| 367 |
m_isFitWindow = true; |
| 368 |
scaled(imageRelativeScale() * 100); |
| 369 |
emit transformChanged(); |
| 370 |
} |
| 371 |
|
| 372 |
void ImageView::fitImage() |
| 373 |
{ |
| 374 |
qreal wrs = windowRelativeScale(); |
| 375 |
resetTransform(); |
| 376 |
scale(1, 1); |
| 377 |
emit checkAdaptImageBtn(); |
| 378 |
if (wrs - 1 > -0.01 && |
| 379 |
wrs - 1 < 0.01) { |
| 380 |
emit checkAdaptScreenBtn(); |
| 381 |
} else { |
| 382 |
emit disCheckAdaptScreenBtn(); |
| 383 |
} |
| 384 |
m_isFitImage = true; |
| 385 |
m_isFitWindow = false; |
| 386 |
scaled(imageRelativeScale() * 100); |
| 387 |
emit transformChanged(); |
| 388 |
} |
| 389 |
|
| 390 |
void ImageView::rotateClockWise() |
| 391 |
{ |
| 392 |
QString errMsg; |
| 393 |
QImage rotateResult; |
| 394 |
if (!UnionImage_NameSpace::rotateImageFileWithImage(90, rotateResult, m_path, errMsg)) { |
| 395 |
qDebug() << errMsg; |
| 396 |
return; |
| 397 |
} |
| 398 |
dApp->m_imageloader->updateImageLoader(QStringList(m_path), QList<QImage>({rotateResult})); |
| 399 |
setImage(m_path, rotateResult); |
| 400 |
} |
| 401 |
|
| 402 |
void ImageView::rotateCounterclockwise() |
| 403 |
{ |
| 404 |
QString errMsg; |
| 405 |
QImage rotateResult; |
| 406 |
if (!UnionImage_NameSpace::rotateImageFileWithImage(-90, rotateResult, m_path, errMsg)) { |
| 407 |
qDebug() << errMsg; |
| 408 |
return; |
| 409 |
} |
| 410 |
dApp->m_imageloader->updateImageLoader(QStringList(m_path), QList<QImage>({rotateResult})); |
| 411 |
setImage(m_path, rotateResult); |
| 412 |
} |
| 413 |
|
| 414 |
void ImageView::centerOn(qreal x, qreal y) |
| 415 |
{ |
| 416 |
QGraphicsView::centerOn(x, y); |
| 417 |
emit transformChanged(); |
| 418 |
} |
| 419 |
|
| 420 |
qreal ImageView::imageRelativeScale() const |
| 421 |
{ |
| 422 |
// vertical scale factor are equal to the horizontal one |
| 423 |
return transform().m11(); |
| 424 |
} |
| 425 |
|
| 426 |
qreal ImageView::windowRelativeScale() const |
| 427 |
{ |
| 428 |
QRectF bf = sceneRect(); |
| 429 |
if (1.0 * width() / height() > 1.0 * bf.width() / bf.height()) { |
| 430 |
return 1.0 * height() / bf.height(); |
| 431 |
} else { |
| 432 |
return 1.0 * width() / bf.width(); |
| 433 |
} |
| 434 |
} |
| 435 |
|
| 436 |
const QString ImageView::path() const |
| 437 |
{ |
| 438 |
return m_path; |
| 439 |
} |
| 440 |
|
| 441 |
QPoint ImageView::mapToImage(const QPoint &p) const |
| 442 |
{ |
| 443 |
return viewportTransform().inverted().map(p); |
| 444 |
} |
| 445 |
|
| 446 |
QRect ImageView::mapToImage(const QRect &r) const |
| 447 |
{ |
| 448 |
return viewportTransform().inverted().mapRect(r); |
| 449 |
} |
| 450 |
|
| 451 |
QRect ImageView::visibleImageRect() const |
| 452 |
{ |
| 453 |
return mapToImage(rect()) & QRect(0, 0, static_cast<int>(sceneRect().width()), static_cast<int>(sceneRect().height())); |
| 454 |
} |
| 455 |
|
| 456 |
bool ImageView::isWholeImageVisible() const |
| 457 |
{ |
| 458 |
const QRect &r = visibleImageRect(); |
| 459 |
const QRectF &sr = sceneRect(); |
| 460 |
|
| 461 |
return r.width() >= sr.width() && r.height() >= sr.height(); |
| 462 |
} |
| 463 |
|
| 464 |
bool ImageView::isFitImage() const |
| 465 |
{ |
| 466 |
return m_isFitImage; |
| 467 |
} |
| 468 |
|
| 469 |
bool ImageView::isFitWindow() const |
| 470 |
{ |
| 471 |
return m_isFitWindow; |
| 472 |
} |
| 473 |
|
| 474 |
void ImageView::onImgFileChanged(const QString &ddfFile) |
| 475 |
{ |
| 476 |
Q_UNUSED(ddfFile) |
| 477 |
m_isChangedTimer->start(200); |
| 478 |
} |
| 479 |
|
| 480 |
void ImageView::onLoadTimerTimeout() |
| 481 |
{ |
| 482 |
QFuture<QVariantList> f = QtConcurrent::run(m_pool, cachePixmap, m_loadPath); |
| 483 |
if (m_watcher.isRunning()) { |
| 484 |
m_watcher.cancel(); |
| 485 |
m_watcher.waitForFinished(); |
| 486 |
} |
| 487 |
m_watcher.setFuture(f); |
| 488 |
emit hideNavigation(); |
| 489 |
} |
| 490 |
|
| 491 |
void ImageView::onThemeTypeChanged() |
| 492 |
{ |
| 493 |
DGuiApplicationHelper::ColorType themeType = DGuiApplicationHelper::instance()->themeType(); |
| 494 |
if (themeType == DGuiApplicationHelper::DarkType) { |
| 495 |
m_backgroundColor = utils::common::DARK_BACKGROUND_COLOR; |
| 496 |
} else { |
| 497 |
m_backgroundColor = utils::common::LIGHT_BACKGROUND_COLOR; |
| 498 |
} |
| 499 |
update(); |
| 500 |
} |
| 501 |
|
| 502 |
void ImageView::onIsChangedTimerTimeout() |
| 503 |
{ |
| 504 |
QFileInfo file(m_path); |
| 505 |
if (file.exists()) { |
| 506 |
//重新加载图片,刷新缓存 |
| 507 |
emit ImageEngineApi::instance()->sigRotateImageFile(0, m_path); |
| 508 |
setImage(m_path); |
| 509 |
m_isChangedTimer->stop(); |
| 510 |
} else { |
| 511 |
emit sigFileDelete(); |
| 512 |
m_isChangedTimer->stop(); |
| 513 |
} |
| 514 |
} |
| 515 |
|
| 516 |
void ImageView::mouseDoubleClickEvent(QMouseEvent *e) |
| 517 |
{ |
| 518 |
emit doubleClicked(); |
| 519 |
QGraphicsView::mouseDoubleClickEvent(e); |
| 520 |
} |
| 521 |
|
| 522 |
void ImageView::mouseReleaseEvent(QMouseEvent *e) |
| 523 |
{ |
| 524 |
QGraphicsView::mouseReleaseEvent(e); |
| 525 |
|
| 526 |
viewport()->setCursor(Qt::ArrowCursor); |
| 527 |
if (e->source() == Qt::MouseEventSynthesizedByQt && m_maxTouchPoints == 1) { |
| 528 |
const QRect &r = visibleImageRect(); |
| 529 |
//double left=r.width()+r.x(); |
| 530 |
const QRectF &sr = sceneRect(); |
| 531 |
//fix 42660 2020/08/14 单指时间在QEvent处理,双指手势通过手势处理。为了解决图片放大后单指滑动手势冲突的问题 |
| 532 |
if ((r.width() >= sr.width() && r.height() >= sr.height())) { |
| 533 |
int xpos = e->pos().x() - m_startpointx; |
| 534 |
if (abs(xpos) > 200 && m_startpointx != 0) { |
| 535 |
if (xpos > 0) { |
| 536 |
emit previousRequested(); |
| 537 |
} else { |
| 538 |
emit nextRequested(); |
| 539 |
} |
| 540 |
} |
| 541 |
} |
| 542 |
} |
| 543 |
m_startpointx = 0; |
| 544 |
m_maxTouchPoints = 0; |
| 545 |
#ifdef tablet_PC |
| 546 |
if (m_press) { |
| 547 |
emit clicked(); |
| 548 |
} |
| 549 |
#endif |
| 550 |
} |
| 551 |
|
| 552 |
void ImageView::mousePressEvent(QMouseEvent *e) |
| 553 |
{ |
| 554 |
#ifdef tablet_PC |
| 555 |
m_press = true; |
| 556 |
#endif |
| 557 |
QGraphicsView::mousePressEvent(e); |
| 558 |
|
| 559 |
viewport()->unsetCursor(); |
| 560 |
viewport()->setCursor(Qt::ArrowCursor); |
| 561 |
|
| 562 |
#ifndef tablet_PC |
| 563 |
emit clicked(); |
| 564 |
#endif |
| 565 |
m_startpointx = e->pos().x(); |
| 566 |
} |
| 567 |
|
| 568 |
void ImageView::mouseMoveEvent(QMouseEvent *e) |
| 569 |
{ |
| 570 |
m_press = false; |
| 571 |
if (!(e->buttons() | Qt::NoButton)) { |
| 572 |
viewport()->setCursor(Qt::ArrowCursor); |
| 573 |
|
| 574 |
emit mouseHoverMoved(); |
| 575 |
} else { |
| 576 |
QGraphicsView::mouseMoveEvent(e); |
| 577 |
viewport()->setCursor(Qt::ClosedHandCursor); |
| 578 |
|
| 579 |
emit transformChanged(); |
| 580 |
} |
| 581 |
#ifndef tablet_PC |
| 582 |
emit dApp->signalM->sigMouseMove(false); |
| 583 |
#endif |
| 584 |
} |
| 585 |
|
| 586 |
void ImageView::leaveEvent(QEvent *e) |
| 587 |
{ |
| 588 |
dApp->getDAppNew()->restoreOverrideCursor(); |
| 589 |
|
| 590 |
QGraphicsView::leaveEvent(e); |
| 591 |
} |
| 592 |
|
| 593 |
//void ImageView::resizeEvent(QResizeEvent *event) |
| 594 |
//{ |
| 595 |
// QGraphicsView::resizeEvent(event); |
| 596 |
// m_toast->move(width() / 2 - m_toast->width() / 2, |
| 597 |
// height() - 80 - m_toast->height() / 2 - 11); |
| 598 |
//} |
| 599 |
|
| 600 |
void ImageView::paintEvent(QPaintEvent *event) |
| 601 |
{ |
| 602 |
QGraphicsView::paintEvent(event); |
| 603 |
} |
| 604 |
|
| 605 |
void ImageView::dragEnterEvent(QDragEnterEvent *e) |
| 606 |
{ |
| 607 |
if (!utils::base::checkMimeUrls(e->mimeData()->urls())) { |
| 608 |
return; |
| 609 |
} |
| 610 |
e->accept(); |
| 611 |
} |
| 612 |
|
| 613 |
void ImageView::drawBackground(QPainter *painter, const QRectF &rect) |
| 614 |
{ |
| 615 |
painter->save(); |
| 616 |
painter->fillRect(rect, m_backgroundColor); |
| 617 |
painter->restore(); |
| 618 |
} |
| 619 |
int static count = 0; |
| 620 |
bool ImageView::event(QEvent *event) |
| 621 |
{ |
| 622 |
QEvent::Type evType = event->type(); |
| 623 |
if (evType == QEvent::TouchBegin || evType == QEvent::TouchUpdate || |
| 624 |
evType == QEvent::TouchEnd) { |
| 625 |
if (evType == QEvent::TouchBegin) { |
| 626 |
count = 0; |
| 627 |
m_maxTouchPoints = 1; |
| 628 |
} |
| 629 |
if (evType == QEvent::TouchUpdate) { |
| 630 |
QTouchEvent *touchEvent = dynamic_cast<QTouchEvent *>(event); |
| 631 |
QList<QTouchEvent::TouchPoint> touchPoints = touchEvent->touchPoints(); |
| 632 |
if (touchPoints.size() > count) { |
| 633 |
count = touchPoints.size(); |
| 634 |
} |
| 635 |
} |
| 636 |
if (evType == QEvent::TouchEnd) { |
| 637 |
QTouchEvent *touchEvent = dynamic_cast<QTouchEvent *>(event); |
| 638 |
QList<QTouchEvent::TouchPoint> touchPoints = touchEvent->touchPoints(); |
| 639 |
|
| 640 |
if (touchPoints.size() == 1 && count <= 1) { |
| 641 |
//QPointF centerPointOffset = gesture->centerPoint(); |
| 642 |
qreal offset = touchPoints.at(0).lastPos().x() - touchPoints.at(0).startPos().x(); |
| 643 |
if (qAbs(offset) > 200) { |
| 644 |
if (offset > 0) { |
| 645 |
emit previousRequested(); |
| 646 |
} else { |
| 647 |
emit nextRequested(); |
| 648 |
} |
| 649 |
} |
| 650 |
} |
| 651 |
} |
| 652 |
} else if (evType == QEvent::Gesture) |
| 653 |
handleGestureEvent(static_cast<QGestureEvent *>(event)); |
| 654 |
|
| 655 |
return QGraphicsView::event(event); |
| 656 |
} |
| 657 |
|
| 658 |
void ImageView::onCacheFinish() |
| 659 |
{ |
| 660 |
QVariantList vl = m_watcher.result(); |
| 661 |
if (vl.length() == 2) { |
| 662 |
const QString path = vl.first().toString(); |
| 663 |
QPixmap pixmap = vl.last().value<QPixmap>(); |
| 664 |
pixmap.setDevicePixelRatio(devicePixelRatioF()); |
| 665 |
if (path == m_path) { |
| 666 |
if (!m_pixmapItem) |
| 667 |
return; |
| 668 |
m_pixmapItem->setGraphicsEffect(nullptr); |
| 669 |
m_pixmapItem->setPixmap(pixmap); |
| 670 |
setSceneRect(m_pixmapItem->boundingRect()); |
| 671 |
autoFit(); |
| 672 |
emit imageChanged(path); |
| 673 |
this->update(); |
| 674 |
} |
| 675 |
} |
| 676 |
} |
| 677 |
|
| 678 |
void ImageView::onThemeChanged(ViewerThemeManager::AppTheme theme) |
| 679 |
{ |
| 680 |
if (theme == ViewerThemeManager::Dark) { |
| 681 |
m_backgroundColor = utils::common::DARK_BACKGROUND_COLOR; |
| 682 |
m_loadingIconPath = utils::view::DARK_LOADINGICON; |
| 683 |
} else { |
| 684 |
m_backgroundColor = utils::common::LIGHT_BACKGROUND_COLOR; |
| 685 |
m_loadingIconPath = utils::view::LIGHT_LOADINGICON; |
| 686 |
} |
| 687 |
update(); |
| 688 |
} |
| 689 |
|
| 690 |
void ImageView::scaleAtPoint(QPoint pos, qreal factor) |
| 691 |
{ |
| 692 |
// Remember zoom anchor point. |
| 693 |
const QPointF targetPos = pos; |
| 694 |
const QPointF targetScenePos = mapToScene(targetPos.toPoint()); |
| 695 |
|
| 696 |
// Do the scaling. |
| 697 |
setScaleValue(factor); |
| 698 |
|
| 699 |
// Restore the zoom anchor point. |
| 700 |
// |
| 701 |
// The Basic idea here is we don't care how the scene is scaled or transformed, |
| 702 |
// we just want to restore the anchor point to the target position we've |
| 703 |
// remembered, in the coordinate of the view/viewport. |
| 704 |
const QPointF curPos = mapFromScene(targetScenePos); |
| 705 |
const QPointF centerPos = QPointF(width() / 2.0, height() / 2.0) + (curPos - targetPos); |
| 706 |
const QPointF centerScenePos = mapToScene(centerPos.toPoint()); |
| 707 |
centerOn(centerScenePos.x(), centerScenePos.y()); |
| 708 |
} |
| 709 |
|
| 710 |
void ImageView::handleGestureEvent(QGestureEvent *gesture) |
| 711 |
{ |
| 712 |
if (QGesture *pinch = gesture->gesture(Qt::PinchGesture)) |
| 713 |
pinchTriggered(static_cast<QPinchGesture *>(pinch)); |
| 714 |
} |
| 715 |
|
| 716 |
void ImageView::pinchTriggered(QPinchGesture *gesture) |
| 717 |
{ |
| 718 |
m_maxTouchPoints = 2; |
| 719 |
QPinchGesture::ChangeFlags changeFlags = gesture->changeFlags(); |
| 720 |
if (changeFlags & QPinchGesture::ScaleFactorChanged) { |
| 721 |
QPoint pos = mapFromGlobal(gesture->centerPoint().toPoint()); |
| 722 |
scaleAtPoint(pos, gesture->scaleFactor()); |
| 723 |
} |
| 724 |
|
| 725 |
if (changeFlags & QPinchGesture::CenterPointChanged) { |
| 726 |
if (!m_isFirstPinch) { |
| 727 |
m_centerPoint = gesture->centerPoint(); |
| 728 |
m_isFirstPinch = true; |
| 729 |
} |
| 730 |
} |
| 731 |
if (gesture->state() == Qt::GestureFinished) { |
| 732 |
#ifndef tablet_PC |
| 733 |
QPointF centerPointOffset = gesture->centerPoint(); |
| 734 |
qreal offset = centerPointOffset.x() - m_centerPoint.x(); |
| 735 |
if (qAbs(offset) > 200) { |
| 736 |
if (offset > 0) { |
| 737 |
emit previousRequested(); |
| 738 |
} else { |
| 739 |
emit nextRequested(); |
| 740 |
} |
| 741 |
} |
| 742 |
#endif |
| 743 |
m_isFirstPinch = false; |
| 744 |
} |
| 745 |
} |
| 746 |
|
| 747 |
void ImageView::wheelEvent(QWheelEvent *event) |
| 748 |
{ |
| 749 |
QFileInfo file(m_path); |
| 750 |
if (!file.exists()) { |
| 751 |
event->accept(); |
| 752 |
} else { |
| 753 |
qreal factor = qPow(1.2, event->delta() / 240.0); |
| 754 |
scaleAtPoint(event->pos(), factor); |
| 755 |
|
| 756 |
event->accept(); |
| 757 |
} |
| 758 |
} |