2022-06-19 16:17:38 +08:00
|
|
|
// SPDX-FileCopyrightText: 2022 Gary Wang <wzc782970009@gmail.com>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
2019-09-28 01:18:08 +08:00
|
|
|
#ifndef GRAPHICSVIEW_H
|
|
|
|
#define GRAPHICSVIEW_H
|
|
|
|
|
|
|
|
#include <QGraphicsView>
|
2019-10-01 10:37:14 +08:00
|
|
|
#include <QUrl>
|
2019-09-28 01:18:08 +08:00
|
|
|
|
2019-09-29 15:52:35 +08:00
|
|
|
class GraphicsScene;
|
2019-09-28 01:18:08 +08:00
|
|
|
class GraphicsView : public QGraphicsView
|
|
|
|
{
|
2019-10-03 17:57:14 +08:00
|
|
|
Q_OBJECT
|
2019-09-28 01:18:08 +08:00
|
|
|
public:
|
|
|
|
GraphicsView(QWidget *parent = nullptr);
|
|
|
|
|
2024-07-30 22:54:43 +08:00
|
|
|
void showFileFromPath(const QString &filePath);
|
2019-10-01 10:37:14 +08:00
|
|
|
|
2019-09-29 15:52:35 +08:00
|
|
|
void showImage(const QPixmap &pixmap);
|
2020-01-01 14:51:46 +08:00
|
|
|
void showImage(const QImage &image);
|
2019-09-29 15:52:35 +08:00
|
|
|
void showText(const QString &text);
|
2019-09-29 23:53:29 +08:00
|
|
|
void showSvg(const QString &filepath);
|
2021-04-15 00:00:18 +08:00
|
|
|
void showAnimated(const QString &filepath);
|
2019-09-29 15:52:35 +08:00
|
|
|
|
|
|
|
GraphicsScene * scene() const;
|
|
|
|
void setScene(GraphicsScene *scene);
|
|
|
|
|
2019-10-02 16:04:50 +08:00
|
|
|
qreal scaleFactor() const;
|
|
|
|
|
|
|
|
void resetTransform();
|
|
|
|
void zoomView(qreal scaleFactor);
|
2021-03-29 22:43:37 +08:00
|
|
|
void rotateView(bool clockwise = true);
|
2021-03-19 23:11:06 +08:00
|
|
|
void flipView(bool horizontal = true);
|
2019-10-02 16:04:50 +08:00
|
|
|
void resetScale();
|
|
|
|
void fitInView(const QRectF &rect, Qt::AspectRatioMode aspectRadioMode = Qt::IgnoreAspectRatio);
|
2021-05-14 00:00:03 +08:00
|
|
|
void fitByOrientation(Qt::Orientation ori = Qt::Horizontal, bool scaleDownOnly = false);
|
2019-10-02 16:04:50 +08:00
|
|
|
|
2021-06-07 14:06:57 +08:00
|
|
|
void displayScene();
|
|
|
|
bool isSceneBiggerThanView() const;
|
2021-06-04 13:53:47 +08:00
|
|
|
void setEnableAutoFitInView(bool enable = true);
|
2019-10-02 14:31:24 +08:00
|
|
|
|
2023-07-09 15:39:59 +08:00
|
|
|
bool avoidResetTransform() const;
|
|
|
|
void setAvoidResetTransform(bool avoidReset);
|
|
|
|
|
2021-03-25 23:05:50 +08:00
|
|
|
static QTransform resetScale(const QTransform & orig);
|
|
|
|
|
2019-10-03 17:57:14 +08:00
|
|
|
signals:
|
2021-03-25 23:05:50 +08:00
|
|
|
void navigatorViewRequired(bool required, QTransform transform);
|
2019-10-04 09:54:13 +08:00
|
|
|
void viewportRectChanged();
|
2019-10-03 17:57:14 +08:00
|
|
|
|
2019-09-30 23:02:44 +08:00
|
|
|
public slots:
|
2021-03-01 00:44:50 +08:00
|
|
|
void toggleCheckerboard(bool invertCheckerboardColor = false);
|
2019-09-30 23:02:44 +08:00
|
|
|
|
2019-09-28 01:18:08 +08:00
|
|
|
private:
|
|
|
|
void mousePressEvent(QMouseEvent * event) override;
|
|
|
|
void mouseMoveEvent(QMouseEvent * event) override;
|
|
|
|
void mouseReleaseEvent(QMouseEvent * event) override;
|
|
|
|
void wheelEvent(QWheelEvent *event) override;
|
2019-09-29 21:22:20 +08:00
|
|
|
void resizeEvent(QResizeEvent *event) override;
|
2019-09-29 15:52:35 +08:00
|
|
|
|
2019-09-29 21:22:20 +08:00
|
|
|
bool isThingSmallerThanWindowWith(const QTransform &transform) const;
|
|
|
|
bool shouldIgnoreMousePressMoveEvent(const QMouseEvent *event) const;
|
2021-03-01 00:44:50 +08:00
|
|
|
void setCheckerboardEnabled(bool enabled, bool invertColor = false);
|
2020-02-13 19:45:14 +08:00
|
|
|
void applyTransformationModeByScaleFactor();
|
2019-09-29 21:22:20 +08:00
|
|
|
|
2021-06-07 14:06:57 +08:00
|
|
|
// Consider switch to 3 state for "no fit", "always fit" and "fit when view is smaller"?
|
|
|
|
// ... or even more? e.g. "fit/snap width" things...
|
|
|
|
// Currently it's "no fit" when it's false and "fit when view is smaller" when it's true.
|
2019-09-29 21:22:20 +08:00
|
|
|
bool m_enableFitInView = false;
|
2023-07-09 15:39:59 +08:00
|
|
|
bool m_avoidResetTransform = false;
|
2019-09-30 23:02:44 +08:00
|
|
|
bool m_checkerboardEnabled = false;
|
2024-10-10 22:58:11 +08:00
|
|
|
bool m_useLightCheckerboard = false;
|
2019-09-28 01:18:08 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // GRAPHICSVIEW_H
|