-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPixmapLabel.cpp
More file actions
150 lines (133 loc) · 3.56 KB
/
PixmapLabel.cpp
File metadata and controls
150 lines (133 loc) · 3.56 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include "PixmapLabel.h"
#include <QDebug>
#include <QStylePainter>
QPixmap* PixmapLabel::backside = NULL;
QPixmap* PixmapLabel::emptypix = NULL;
PixmapLabel::PixmapLabel(QWidget *parent) :
QLabel(parent)
{
this->setMinimumSize(1,1);
setScaledContents(false);
setStyleSheet("QLabel{border-radius: 5px; background: transparent;}");
animator = new QPropertyAnimation(this);
animator->setTargetObject(this);
animator->setStartValue(0.0);
animator->setEndValue(1.0);
animator->setDuration(300);
loop = new QEventLoop(this);
connect(animator, SIGNAL(valueChanged(const QVariant&)), SLOT(update()));
connect(animator, SIGNAL(finished()), loop, SLOT(quit()));
connect(animator, SIGNAL(finished()), SLOT(onAnimatorFinished()));
connect(animator, SIGNAL(finished()), animator, SLOT(stop()));
}
PixmapLabel::~PixmapLabel()
{
delete animator;
delete loop;
}
int PixmapLabel::heightForWidth(int width) const
{
return actpix->isNull() ? this->height() : ((qreal)pix.height()*width)/pix.width();
}
QSize PixmapLabel::sizeHint() const
{
int w = this->width();
return QSize(w, heightForWidth(w));
}
QPixmap PixmapLabel::scaledPixmap() const
{
if (animator->state() == QAbstractAnimation::Running)
{
QRect rect = this->rect();
QPoint c = rect.center();
int w = rect.width();
w *= animator->currentValue().toFloat();
rect.setWidth(w);
rect.moveCenter(c);
return actpix->scaled(rect.size());
}
else
{
return actpix->scaled(this->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
}
}
void PixmapLabel::loadBackSide(QString fname)
{
backside = new QPixmap(fname);
emptypix = new QPixmap(backside->size());
emptypix->fill(QColor(0xffe9f3fb));
}
void PixmapLabel::flip()
{
if (animator->state() != QAbstractAnimation::Stopped)
loop->exec(QEventLoop::ExcludeUserInputEvents);
reanimate = true;
animator->setDirection(QAbstractAnimation::Backward);
animator->start();
}
void PixmapLabel::setPixmap(const QPixmap &p, int id)
{
pix = p;
this->id = id;
down = true;
actpix = backside;
QLabel::setPixmap(scaledPixmap());
}
void PixmapLabel::fill()
{
actpix = emptypix;
}
/*
* Ezt a fügvényt csak az újraosztás elején használjuk
*
*/
void PixmapLabel::showBackSide()
{
actpix = backside;
QLabel::setPixmap(scaledPixmap());
}
void PixmapLabel::resizeEvent(QResizeEvent *e)
{
Q_UNUSED(e)
if(!actpix->isNull())
QLabel::setPixmap(scaledPixmap());
}
/*
* Először leanimálja az aktuális pixmapet
*/
void PixmapLabel::mousePressEvent(QMouseEvent *event)
{
if (event->button() != Qt::LeftButton || !down || animator->state() != QAbstractAnimation::Stopped) return;
emit cardClicked(this);
}
/*
* flip után ez animálja fel az új oldalt
*/
void PixmapLabel::onAnimatorFinished()
{
if (reanimate)
{
reanimate = false;
if (down)
{
actpix = &pix;
down = false;
}
else
{
actpix = backside;
down = true;
}
animator->setDirection(QAbstractAnimation::Forward);
animator->start();
}
}
void PixmapLabel::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event)
QPixmap pix = scaledPixmap();
QSize size = this->size();
int x = (size.width()-pix.width())/2;
QPainter p(this);
p.drawPixmap(x, 0, pix);
}