QImage ImageViewerTool::loadImage(const QString &fileName)
{
QImageReader reader(fileName);
reader.setAutoTransform(true);
qDebug() << "loading" << QDir::toNativeSeparators(fileName);
if (!reader.canRead()) {
qDebug() << QDir::toNativeSeparators(fileName) << ": can't load image";
return QImage();
}
QImage image;
if (!reader.read(&image)) {
qDebug() << QDir::toNativeSeparators(fileName) << ": corrupted image: " << reader.errorString();
return QImage();
}
const QSize maximumSize(2000, 2000);
if (image.size().width() > maximumSize.width() || image.height() > maximumSize.height())
image = image.scaled(maximumSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
return image;
}
void ImageViewerTool::wheelEvent(QWheelEvent *event)
{
BasicWindow::wheelEvent(event);
int value = event->delta();
double scaleRatio = value * 1.0 / (8 * 360);
m_scaleFactor += scaleRatio;
if (m_scaleFactor >= 5){
m_scaleFactor = 5;
}
else if (m_scaleFactor <= 0.1){
m_scaleFactor = 0.1;
}
if (m_scaleFactor > 1){
m_miniLabel->setVisible(true);
}
else{
m_viewPos = QPointF(0, 0);
m_miniLabel->setVisible(false);
}
m_miniLabel->setViewScaleFactor(m_scaleFactor);
}
void ImageViewerTool::paintEvent(QPaintEvent* event)
{
BasicWindow::paintEvent(event);
QPainter painter(this);
painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
painter.setPen(Qt::NoPen);
QPainterPath clippath;
clippath.addRoundedRect(this->rect(), 4.5, 4.5);
painter.setClipPath(clippath);
//drawback
drawBack(&painter);
//drawImage
const qreal iw = m_currentImage.width();
const qreal ih = m_currentImage.height();
const qreal wh = height();
const qreal ww = width();
painter.save();
qreal scaleX = m_imageScaleSize.width() * m_scaleFactor / iw;
qreal scaleY = m_imageScaleSize.height() * m_scaleFactor / ih;
if (m_scaleFactor > 1){
painter.translate(m_viewPos.x() * m_scaleFactor - (ww - iw * scaleX) / 2, m_viewPos.y() * m_scaleFactor - (wh - ih * scaleY) / 2);
}
painter.translate(ww / 2, wh / 2);
painter.rotate(m_rotationAngle);
painter.scale(scaleX, scaleY);
painter.translate(-iw / 2, -ih / 2);
painter.drawImage(0, 0, m_currentImage);
painter.restore();
if (m_isShowFactor != m_scaleFactor){
m_isShowFactor = m_scaleFactor;
m_factorTimer->start(1000);
int number = m_scaleFactor * 100;
QPoint point = QPoint(this->width() / 2 - 50, this->height() / 2 - 25);
painter.save();
QRect scaleRect = QRect(point, QSize(100, 50));
m_spliterImage->draw(&painter, scaleRect, 255);
QFont font = painter.font();
font.setFamily("Microsoft YaHei");
font.setPixelSize(14);
painter.setFont(font);
painter.setPen(QColor(255, 255, 255, 200));
painter.setBrush(Qt::NoBrush);
painter.drawText(scaleRect, Qt::AlignCenter, QString("%1%").arg(number));
painter.restore();
painter.drawPixmap(point, QPixmap());
}
}
void ImageViewerTool::setRotationImage(int rotationAngle)
{
m_rotationAngle = rotationAngle;
update();
}
void ImageViewerTool::setImageView(QPointF pos, QSizeF size)
{
m_viewPos = -pos;
update();
}
void ViewImageMiniLabel::paintEvent(QPaintEvent* event)
{
QLabel::paintEvent(event);
QPainter painter(this);
painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
painter.setPen(Qt::NoPen);
QPainterPath clippath;
clippath.addRoundedRect(this->rect(), 4.5, 4.5);
painter.setClipPath(clippath);
//drawback
painter.save();
painter.setBrush(QColor(52, 52, 52, 220));
painter.drawRect(this->rect());
painter.restore();
//drawImage
const qreal iw = m_image.width();
const qreal ih = m_image.height();
const qreal wh = height();
const qreal ww = width();
painter.save();
painter.translate(ww / 2, wh / 2);
painter.rotate(m_rotationAngle);
painter.scale(m_imageScaleSize.width() * 1.0 / iw, m_imageScaleSize.height() * 1.0 / ih);
painter.translate(-iw / 2, -ih / 2);
painter.drawImage(0, 0, m_image);
painter.restore();
if (m_viewSize.width() < this->size().width() || m_viewSize.height() < this->size().height()){
painter.save();
painter.setPen(Qt::red);
painter.setBrush(Qt::NoBrush);
painter.drawRoundedRect(QRectF(m_viewPos, m_viewSize), 4.5, 4.5);
painter.restore();
}
}
void ViewImageMiniLabel::mouseReleaseEvent(QMouseEvent *event)
{
setCursor(Qt::PointingHandCursor);
m_mousepressPos = event->globalPos();
m_leftButtonPress = false;
}
void ViewImageMiniLabel::mouseMoveEvent(QMouseEvent *event)
{
if (m_leftButtonPress) {
QPoint p = event->globalPos();
int offsetx = p.x() - m_mousepressPos.x();
int offsety = p.y() - m_mousepressPos.y();
if ((offsetx > -1 && offsetx < 1) && (offsety > -1 && offsety < 1))
return;
m_mousepressPos = p;
qreal x = m_viewPos.x() + offsetx;
qreal y = m_viewPos.y() + offsety;
if (x + m_viewSize.width() > this->width()){
x = this->width() - m_viewSize.width();
}
else if (x < 0){
x = 0;
}
if (y + m_viewSize.height() > this->height()){
y = this->height() - m_viewSize.height();
}
else if (y < 0){
y = 0;
}
m_viewPos.setX(x);
m_viewPos.setY(y);
setViewImageUpdate(m_viewPos);
}
}
void ViewImageMiniLabel::setViewImageUpdate(QPointF ponit)
{
QPointF newpoint;
qreal scaleX = m_imageScaleSize.width() * 1.0 / m_image.width();
qreal scaleY = m_imageScaleSize.height() * 1.0 / m_image.height();
newpoint.rx() = ponit.x() - (this->width() - m_image.width() * scaleX) / 2;
newpoint.ry() = ponit.y() - (this->height() - m_image.height() * scaleY) / 2;
m_imageView->setImageView(newpoint * 4.0, m_viewSize * 4.0);
update();
}
void ViewImageMiniLabel::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton) {
setCursor(Qt::ClosedHandCursor);
m_leftButtonPress = true;
}
m_mousepressPos = event->globalPos();
}
void ViewImageMiniLabel::setViewScaleFactor(qreal factor)
{
m_viewSize = this->size() * 1.0 / factor;
if (m_viewSize.width() > this->size().width()){
m_viewSize.rwidth() = this->size().width();
}
else if (m_viewSize.height() > this->size().height()){
m_viewSize.rheight() = this->size().height();
}
if (m_viewPos.x() + m_viewSize.width() > this->width()){
m_viewPos.rx() = this->width() - m_viewSize.width();
}
else if (m_viewPos.x() < 0){
m_viewPos.rx() = 0;
}
if (m_viewPos.y() + m_viewSize.height() > this->height()){
m_viewPos.ry() = this->height() - m_viewSize.height();
}
else if (m_viewPos.y() < 0){
m_viewPos.ry() = 0;
}
setViewImageUpdate(m_viewPos);
}
void ViewImageMiniLabel::mouseDoubleClickEvent(QMouseEvent *event)
{
if (m_animationRotation == nullptr){
m_animationRotation = new QPropertyAnimation(this, "");
connect(m_animationRotation, &QVariantAnimation::valueChanged, [=](const QVariant &value){
m_rotationAngle = value.toInt();
m_imageView->setRotationImage(m_rotationAngle);
update();
});
}
int startAngle = m_rotationAngle;
int endAngle = startAngle + 90;
m_animationRotation->setDuration(300);
m_animationRotation->setStartValue(startAngle);
m_animationRotation->setEndValue(endAngle);
m_animationRotation->start();
}