forked from LePeruvien/DigitalNews
/
view.cpp
75 lines (65 loc) · 2.37 KB
/
view.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
#include "view.hpp"
#include "button.hpp"
#include "topbar.hpp"
#include <QtCore/QPropertyAnimation>
#include <QtCore/QDebug>
View::View(QGraphicsScene *parent, const QSize &size) : QGraphicsView(parent) {
resize(size);
setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
_topBar = new TopBar(QSize(size.width(), size.height() / 15));
_topBar->setPos(0, 0);
scene()->addItem(_topBar);
_cptArticles = 0;
_actButton = NULL;
}
void View::addArticle(const Article &article) {
unsigned int w = size().width() / 3;
unsigned int h = (size().height() - _topBar->size().height()) / 3;
Button *btn = new Button(article.draw(QRect(0, 0, w, h)));
connect(btn, SIGNAL(pressed()), this, SLOT(articleClicked()));
btn->resize(w, h);
btn->setPos((_cptArticles % 3) * (w), (_cptArticles / 3) * h + _topBar->size().height());
scene()->addItem(btn);
++_cptArticles;
}
void View::resizeEvent(QResizeEvent *event) {
QGraphicsView::resizeEvent(event);
fitInView(sceneRect(), Qt::KeepAspectRatio);
}
void View::articleClicked() {
Button *btn = dynamic_cast<Button*>(sender());
if (!btn)
return;
QPropertyAnimation *animation = new QPropertyAnimation(btn, "geometry");
animation->setDuration(750);
animation->setEasingCurve(QEasingCurve::OutExpo);
if (btn->isFront()) {
btn->setZValue(1);
animation->setStartValue(btn->geometry());
animation->setEndValue(btn->gridGeometry());
connect(animation, SIGNAL(finished()), btn, SLOT(setBack()));
animation->start();
//_actButton = NULL;
} else {
if (_actButton != NULL && _actButton != btn) {
delete animation;
return;
}
btn->setFront();
btn->setGridGeometry(btn->geometry());
animation->setStartValue(btn->geometry());
unsigned int w = size().width() / 17;
unsigned int h = (size().height() - _topBar->size().height()) / 15;
animation->setEndValue(QRectF(w, h + _topBar->size().height(), 15 * w, 13 * h));
animation->start();
_actButton = btn;
}
btn->setEnabled(false);
connect(animation, SIGNAL(finished()), animation, SLOT(deleteLater()));
connect(animation, SIGNAL(finished()), this, SLOT(animationFinished()));
}
void View::animationFinished() {
_actButton->setEnabled(true);
if (!_actButton->isFront())
_actButton = NULL;
}