Utils/ImageView/Inc/ImageView.h

75 lines
2.0 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#pragma once
#include <QGraphicsView>
#include <QImage>
#include <QPixmap>
#include <Qt>
class QLabel;
class QGraphicsPixmapItem;
class QGraphicsScene;
class QMouseEvent;
class QWheelEvent;
// 可缩放、可平移的图片显示控件。
class ImageView : public QGraphicsView
{
Q_OBJECT
public:
explicit ImageView(QWidget* parent = nullptr);
~ImageView() override;
void setImage(const QImage& image);
void setImage(const QPixmap& pixmap);
bool loadImage(const QString& filePath);
void clearImage();
bool hasImage() const;
QImage image() const;
// 比例以原始图片大小为基准,1.0 表示 100%。
double zoomRatio() const;
void setZoomRatio(double ratio);
void zoomIn(double factor = 1.25);
void zoomOut(double factor = 1.25);
void resetView();
void setZoomRange(double minimum, double maximum);
double minimumZoomRatio() const;
double maximumZoomRatio() const;
// 图片未铺满视口时的停靠方向,默认居中。
void setImageAlignment(Qt::Alignment alignment);
Qt::Alignment imageAlignment() const;
signals:
void imageChanged();
void zoomRatioChanged(double ratio);
protected:
void resizeEvent(QResizeEvent* event) override;
void wheelEvent(QWheelEvent* event) override;
void mousePressEvent(QMouseEvent* event) override;
void mouseMoveEvent(QMouseEvent* event) override;
void mouseReleaseEvent(QMouseEvent* event) override;
void mouseDoubleClickEvent(QMouseEvent* event) override;
private:
void updateZoomLabel();
void updateZoomRatio(double ratio, bool keepCenter);
void repositionZoomLabel();
QGraphicsScene* m_scene;
QGraphicsPixmapItem* m_pixmapItem;
QLabel* m_zoomLabel;
QImage m_image;
double m_zoomRatio;
double m_minimumZoomRatio;
double m_maximumZoomRatio;
Qt::Alignment m_imageAlignment;
bool m_panning;
bool m_needsInitialFit;
QPoint m_lastMousePosition;
};