#ifndef MYLABEL_H
#define MYLABEL_H
#include <QObject>
#include <QLabel>
#include <QMutex>
class MyLabel : public QLabel
{
public:
explicit MyLabel(QWidget *parent = 0);
void SetImage(QImage image);
QImage GetImage();
void clear();
protected:
void paintEvent(QPaintEvent *event);
void wheelEvent(QWheelEvent *event) override;
void mousePressEvent(QMouseEvent *event) override;
void mouseDoubleClickEvent(QMouseEvent *event) override;
void mouseMoveEvent(QMouseEvent *event) override;
void mouseReleaseEvent(QMouseEvent *event) override;
private:
void OnZoomInImage();
void OnZoomOutImage();
void OnPresetImage();
private:
QMutex m_Mutex;
QImage m_Image;
qreal ZoomValue = 1.0;
int XPtInterval = 0;
int YPtInterval = 0;
QPoint OldPos;
bool Pressed = false;
};
#endif
#include "mylabel.h"
#include <QPainter>
#include <QDebug>
#include <QWheelEvent>
#include <QMenu>
#include <QFileDialog>
MyLabel::MyLabel(QWidget *parent) :QLabel(parent)
{
}
void MyLabel::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
m_Mutex.lock();
int width = qMin(m_Image.width(), this->width());
int height = width * 1.0 / (m_Image.width() * 1.0 / m_Image.height());
height = qMin(height, this->height());
width = height * 1.0 * (m_Image.width() * 1.0 / m_Image.height());
painter.translate(this->width() / 2 + XPtInterval, this->height() / 2 + YPtInterval);
painter.scale(ZoomValue, ZoomValue);
QRect picRect(-width / 2, -height / 2, width, height);
painter.drawImage(picRect, m_Image);
m_Mutex.unlock();
}
void MyLabel::wheelEvent(QWheelEvent *event)
{
int value = event->delta();
if (value > 0)
OnZoomInImage();
else
OnZoomOutImage();
update();
}
void MyLabel::mousePressEvent(QMouseEvent *event)
{
OldPos = event->pos();
Pressed = true;
}
void MyLabel::mouseDoubleClickEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton)
{
ZoomValue = 1.0;
XPtInterval = 0;
YPtInterval = 0;
update();
}
}
void MyLabel::mouseMoveEvent(QMouseEvent *event)
{
if (!Pressed)
return QWidget::mouseMoveEvent(event);
this->setCursor(Qt::SizeAllCursor);
QPoint pos = event->pos();
int xPtInterval = pos.x() - OldPos.x();
int yPtInterval = pos.y() - OldPos.y();
XPtInterval += xPtInterval;
YPtInterval += yPtInterval;
OldPos = pos;
update();
}
void MyLabel::mouseReleaseEvent(QMouseEvent *event)
{
Pressed = false;
setCursor(Qt::ArrowCursor);
}
void MyLabel::OnZoomInImage()
{
ZoomValue += 1;
if (ZoomValue > 35)
{
ZoomValue = 35;
}
update();
}
void MyLabel::OnZoomOutImage()
{
ZoomValue -= 1;
if (ZoomValue <= 0)
{
ZoomValue = 0.5;
return;
}
update();
}
void MyLabel::OnPresetImage()
{
ZoomValue = 1.0;
XPtInterval = 0;
YPtInterval = 0;
update();
}
void MyLabel::SetImage(QImage image)
{
m_Mutex.lock();
m_Image = image.copy(0,0,image.width(),image.height());
m_Mutex.unlock();
update();
}
QImage MyLabel::GetImage()
{
return m_Image;
}
void MyLabel::clear()
{
m_Mutex.lock();
QPixmap pixmap = QPixmap("");
m_Image = pixmap.toImage();
m_Mutex.unlock();
update();
}
文章评论