-
Notifications
You must be signed in to change notification settings - Fork 9
/
myphotoview.cpp
329 lines (259 loc) · 7.5 KB
/
myphotoview.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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#include "myphotoview.h"
PYNum::PYNum(QWidget* parent):QWidget(parent)
{
m_opacity = 0.0;
setWindowOpacity(PYN_FIN_OPACITY);
m_fadeTimer = new QTimer(this);
m_fadeTimer->setInterval(PYN_TIMER_INTERVAL);
connect(m_fadeTimer,SIGNAL(timeout()),this,SLOT(DoFading()));
m_hideTimer = new QTimer(this);
m_hideTimer->setInterval(PYN_TIMER_INTERVAL);
connect(m_hideTimer,SIGNAL(timeout()),this,SLOT(DoHiding()));
m_holdTimer = new QTimer(this);
m_holdTimer->setInterval(PYN_HOLD_DURATION);
m_holdTimer->setSingleShot(true);
connect(m_holdTimer,SIGNAL(timeout()),m_hideTimer,SLOT(start()));
m_nTotal = 0;
m_nValue = 0;
setFixedHeight(PYN_MAX_HEIGHT);
setWindowFlags(Qt::FramelessWindowHint);
setAttribute(Qt::WA_TranslucentBackground);
}
void PYNum::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.setRenderHints(QPainter::Antialiasing|QPainter::HighQualityAntialiasing);
drawBg(&painter);
drawText(&painter);
}
void PYNum::drawBg(QPainter *painter)
{
painter->save();
painter->setPen(Qt::NoPen);
QLinearGradient BgGradient(QPoint(0,0),QPoint(0,height()));
BgGradient.setColorAt(0.0,PYN_BG_START_COLOR);
BgGradient.setColorAt(1.0,PYN_BG_END_COLOR);
painter->setBrush(BgGradient);
painter->drawRoundedRect(rect(),PYN_RECT_RADIUS,PYN_RECT_RADIUS);
painter->restore();
}
void PYNum::drawText(QPainter *painter)
{
painter->save();
QString strText ;
strText = tr("%1/%2").arg(m_nValue).arg(m_nTotal);
qreal TextLength = fontMetrics().width(strText);
qreal ThisWidth = TextLength * PYN_WIDTH_FACTOR;
painter->setPen(PYN_TEXT_COLOR);
QFont TextFont;
TextFont.setBold(true);
painter->setFont(TextFont);
painter->drawText(rect(),strText,Qt::AlignVCenter|Qt::AlignHCenter);
setFixedWidth(ThisWidth);
painter->restore();
}
void PYNum::showEvent(QShowEvent *)
{
m_opacity = 0.0;
setWindowOpacity(m_opacity);
m_fadeTimer->start();
}
void PYNum::DoFading()
{
m_opacity += PYN_OPACITY_INCREMENT;
if(m_opacity > PYN_FIN_OPACITY)
{
m_opacity = PYN_FIN_OPACITY;
m_fadeTimer->stop();
m_holdTimer->start();
}
setWindowOpacity(m_opacity);
}
void PYNum::DoHiding()
{
m_opacity -= PYN_OPACITY_INCREMENT;
if(m_opacity < 0.0)
{
m_opacity = 0.0;
m_hideTimer->stop();
}
setWindowOpacity(m_opacity);
}
void PYNum::setTotal(int totalNum)
{
m_nTotal = totalNum;
update();
}
void PYNum::setValue(int value)
{
m_nValue = value;
update();
}
myPhotoView::myPhotoView(QWidget *parent) :
QWidget(parent)
{
this->initVariables();
}
void myPhotoView::initVariables()
{
m_currImage = 0;
m_nTotal = 0;
m_nCurrIndex = 0;
num = new PYNum(this);
connect(this,SIGNAL(sig_setTotal(int)),num,SLOT(setTotal(int)));
connect(this,SIGNAL(sig_setValue(int)),num,SLOT(setValue(int)));
prevButton = new QToolButton(this);
nextButton = new QToolButton(this);
prevButton->setIcon(QIcon(":/images/prev.png"));
nextButton->setIcon(QIcon(":/images/next.png"));
prevButton->setIconSize(PYP_BUTTON_SIZE);
nextButton->setIconSize(PYP_BUTTON_SIZE);
prevButton->setFixedSize(PYP_BUTTON_SIZE);
nextButton->setFixedSize(PYP_BUTTON_SIZE);
prevButton->setAutoRaise(true);
nextButton->setAutoRaise(true);
connect(prevButton,SIGNAL(clicked()),this,SLOT(showPrevious()));
connect(nextButton,SIGNAL(clicked()),this,SLOT(showNext()));
calcGeo();
#ifdef PYP_IMAGE_FADE_TIMER_INTERVAL
m_imageOpacity = 0.0;
m_imageFadeTimer = new QTimer(this);
m_imageFadeTimer->setInterval(PYP_IMAGE_FADE_TIMER_INTERVAL);
connect(m_imageFadeTimer,SIGNAL(timeout()),this,SLOT(DoImageFading()));
#endif
}
void myPhotoView::calcGeo()
{
/// RESIZE AND MOVE PREVBUTTON
QPoint PrevMovePot(PYP_LEFT_SPACE,height() - PYP_TOP_SPACE - PYP_BUTTON_SIZE.height());
prevButton->move(PrevMovePot);
prevButton->show();
/// RESIZE AND MOVE NEXTBUTTON
QPoint NextMovePot(width() - PYP_LEFT_SPACE - PYP_BUTTON_SIZE.width(),height() - PYP_TOP_SPACE -PYP_BUTTON_SIZE.height());
nextButton->move(NextMovePot);
nextButton->show();
/// RESIZE AND MOVE NUM
qreal NumX = (qreal)width()/2 - (qreal)num->width()/2;
qreal NumY = height() - PYP_TOP_SPACE - PYP_BUTTON_SIZE.height()/2 - num->height()/2;
QPointF NumMovePot(NumX,NumY);
num->move(NumMovePot.toPoint());
}
void myPhotoView::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.setRenderHints(QPainter::Antialiasing|QPainter::HighQualityAntialiasing|QPainter::SmoothPixmapTransform);
drawBg(&painter);
drawImage(&painter);
}
void myPhotoView::showEvent(QShowEvent *)
{
calcGeo();
}
void myPhotoView::resizeEvent(QResizeEvent *)
{
calcGeo();
}
void myPhotoView::drawBg(QPainter *painter)
{
painter->save();
painter->setPen(Qt::NoPen);
QLinearGradient BgGradient(QPoint(0,0),QPoint(0,height()));
BgGradient.setColorAt(0.0,PYP_BG_START_COLOR);
BgGradient.setColorAt(1.0,PYP_BG_END_COLOR);
painter->setBrush(BgGradient);
painter->drawRect(rect());
painter->restore();
}
void myPhotoView::drawImage(QPainter *painter)
{
painter->save();
#ifdef PYP_IMAGE_MOTION_PATTERN_FADE
painter->setOpacity(m_imageOpacity);
qreal imageWidth = m_currImage->width();
qreal imageHeight = m_currImage->height();
qreal ImageX = rect().center().x() - imageWidth/2;
qreal ImageY = rect().center().y() - imageHeight/2;
QPointF ImagePot(ImageX,ImageY);
painter->drawImage(ImagePot,*m_currImage);
#endif
painter->restore();
}
#ifdef PYP_IMAGE_FADE_TIMER_INTERVAL
void myPhotoView::DoImageFading()
{
m_imageOpacity += PYP_IMAGE_OPACITY_INCREMENT;
if(m_imageOpacity > 1.0)
{
m_imageOpacity = 1.0;
m_imageFadeTimer->stop();
}
update();
}
#endif
void myPhotoView::load(const QString &strFolder)
{
m_strFolder = strFolder;
QDir imageFolder(strFolder);
if (imageFolder.exists())
{
QStringList ImageList = imageFolder.entryList(PYP_IMAGE_SUFFIX);
if (ImageList.count()>0)
{
QString strFullPath ;
m_ImageVec.clear();
foreach(const QString strImage,ImageList)
{
strFullPath = tr("%1/%2").arg(strFolder).arg(strImage);
QImage* img = new QImage(strFullPath);
m_ImageVec.push_back(img);
}
if(m_ImageVec.size() > 0)
{
m_currImage = m_ImageVec.at(0);
m_nCurrIndex = 0;
}
emit sig_setTotal(ImageList.count());
#ifdef PYP_IMAGE_MOTION_PATTERN_FADE
m_imageFadeTimer->start();
#endif
}
}
}
void myPhotoView::showNext()
{
if(m_ImageVec.isEmpty())
{
return ;
}
m_nCurrIndex++;
if(m_nCurrIndex >= m_ImageVec.count())
{
m_nCurrIndex = m_ImageVec.count() -1;
}
m_currImage = m_ImageVec.at(m_nCurrIndex);
#ifdef PYP_IMAGE_MOTION_PATTERN_FADE
m_imageOpacity = 0.0;
m_imageFadeTimer->start();
#endif
emit sig_setValue(m_nCurrIndex + 1);
calcGeo();
}
void myPhotoView::showPrevious()
{
if(m_ImageVec.isEmpty())
{
return ;
}
m_nCurrIndex--;
if(m_nCurrIndex < 0)
{
m_nCurrIndex = 0;
}
m_currImage = m_ImageVec.at(m_nCurrIndex);
#ifdef PYP_IMAGE_MOTION_PATTERN_FADE
m_imageOpacity = 0.0;
m_imageFadeTimer->start();
#endif
emit sig_setValue(m_nCurrIndex + 1);
calcGeo();
}