-
Notifications
You must be signed in to change notification settings - Fork 0
/
imagelabel.cpp
47 lines (38 loc) · 848 Bytes
/
imagelabel.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
#include "imagelabel.h"
ImageLabel::ImageLabel(){
}
void ImageLabel::setPixmap(QPixmap pix){
original = pix;
//repaint();
update();
}
void ImageLabel::paintEvent(QPaintEvent *event){
QLabel::paintEvent(event);
if(original.isNull()) return;
float w,h; // widget size
w=width();
h=height();
float pw,ph; // picture size
pw = original.width();
ph = original.height();
float dw,dh; // difference ratio
dw = pw/w;
dh = ph/h;
int lw,lh; // location
if(pw<w && ph<h){
//no scaling - the picture is small enough to just be displayed
scaled = original;
lw=(w-pw)/2;
lh=(h-ph)/2;
}else if(dw > dh){
scaled = original.scaledToWidth(w);
lw = 0;
lh = (h-scaled.height())/2;
}else{
scaled = original.scaledToHeight(h);
lw = (w-scaled.width())/2;
lh = 0;
}
QPainter paint(this);
paint.drawPixmap(lw,lh,scaled);
}