1 #include "mainwindow.h"
6 #include <QResizeEvent>
8 MainWindow::MainWindow(QWidget
* parent
)
11 m_scene
= new QGraphicsScene(this);
14 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
15 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
17 setViewportUpdateMode(QGraphicsView::FullViewportUpdate
);
19 QGraphicsView::DontClipPainter
|
20 QGraphicsView::DontSavePainterState
);
23 connect(&m_timer
, SIGNAL(timeout()), this, SLOT(tick()));
29 m_size
= QSize(600, 600);
32 void MainWindow::resizeEvent(QResizeEvent
*) {
34 m_scene
->setSceneRect(rect());
38 void MainWindow::addElement(const QPoint
& p
) {
45 Element
* element
= new Element(scene
, p
);
46 m_elements
.push_back(element
);
48 kDebug() << m_elements
.size() << endl
;
51 void MainWindow::tick() {
52 int elapsed
= m_time
.elapsed();
53 int delta
= elapsed
- m_last_time
;
54 m_last_time
= elapsed
;
56 foreach (Element
* item
, m_elements
) {
57 QPointF new_pos
= item
->position() + item
->velocity() * delta
;
58 QPointF new_vel
= item
->velocity();
60 if (new_pos
.x() <= item
->size().x() / 2) {
61 new_pos
.setX(item
->size().x() - new_pos
.x());
62 new_vel
.setX(fabs(new_vel
.x()));
64 if (new_pos
.x() >= m_size
.width() - item
->size().x() / 2) {
65 new_pos
.setX(2 * m_size
.width() - item
->size().x() - new_pos
.x());
66 new_vel
.setX(-fabs(new_vel
.x()));
68 if (new_pos
.y() <= item
->size().y() / 2) {
69 new_pos
.setY(item
->size().y() - new_pos
.y());
70 new_vel
.setY(fabs(new_vel
.y()));
72 if (new_pos
.y() >= m_size
.height() - item
->size().y() / 2) {
73 new_pos
.setY(2 * m_size
.height() - item
->size().y() - new_pos
.y());
74 new_vel
.setY(-fabs(new_vel
.y()));
77 item
->setPosition(new_pos
);
78 item
->setVelocity(new_vel
);
82 void MainWindow::mousePressEvent(QMouseEvent
* e
) {