This repository has been archived by the owner on Jul 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
imagepreview.cpp
71 lines (59 loc) · 1.91 KB
/
imagepreview.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include "imagepreview.h"
#include <QScrollBar>
#include <QHBoxLayout>
#include <QImageReader>
ImagePreview::ImagePreview(QWidget *parent) : QWidget(parent)
{
imgLabel = new QLabel;
imgLabel->setScaledContents(true);
scrollArea = new QScrollArea;
scrollArea->setContentsMargins(0, 0, 0, 0);
scrollArea->setAlignment(Qt::AlignCenter);
scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
scrollArea->verticalScrollBar()->blockSignals(true);
scrollArea->horizontalScrollBar()->blockSignals(true);
scrollArea->setFrameStyle(0);
scrollArea->setWidget(imgLabel);
scrollArea->setWidgetResizable(true);
QHBoxLayout *layout = new QHBoxLayout();
layout->setContentsMargins(0, 0, 0, 0);
layout->setSpacing(0);
layout->addWidget(scrollArea);
this->setBgColor();
this->setLayout(layout);
}
QPixmap& ImagePreview::loadImage(QString fromFilePath)
{
QImageReader reader(fromFilePath);
if (reader.size().isValid()) {
QSize resize = reader.size();
resize.scale(QSize(imgLabel->width(), imgLabel->height()), Qt::KeepAspectRatio);
QImage previewImage;
reader.read(&previewImage);
prevPixmap = QPixmap::fromImage(previewImage);
}
imgLabel->setPixmap(prevPixmap);
this->resizeImagePreview();
return prevPixmap;
}
void ImagePreview::clear()
{
imgLabel->clear();
}
void ImagePreview::resizeImagePreview()
{
const QPixmap *pixmap = imgLabel->pixmap();
if (!pixmap)
return;
QSize prevSizePixmap = pixmap->size();
if (prevSizePixmap.width() > scrollArea->width() || prevSizePixmap.height() > scrollArea->height()) {
prevSizePixmap.scale(scrollArea->width(), scrollArea->height(), Qt::KeepAspectRatio);
}
imgLabel->setFixedSize(prevSizePixmap);
imgLabel->adjustSize();
}
void ImagePreview::setBgColor()
{
//
}