Removed debug messages.
[kgcqgv.git] / mainwindow.cpp
blob0c0523ab5985e918affac71e3b950cb04b23714b
1 #include "mainwindow.h"
2 #include "element.h"
3 #include <math.h>
4 #include <KDebug>
5 #include <QMouseEvent>
6 #include <QResizeEvent>
8 MainWindow::MainWindow(QWidget* parent)
9 : CANVAS(parent) {
10 #ifndef USE_KGC
11 m_scene = new QGraphicsScene(this);
12 setScene(m_scene);
14 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
15 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
17 setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
18 setOptimizationFlags(
19 QGraphicsView::DontClipPainter |
20 QGraphicsView::DontSavePainterState);
21 #endif
23 connect(&m_timer, SIGNAL(timeout()), this, SLOT(tick()));
25 m_time.start();
26 m_last_time = 0;
27 m_timer.start(20);
29 m_size = QSize(600, 600);
32 void MainWindow::resizeEvent(QResizeEvent*) {
33 #ifndef USE_KGC
34 m_scene->setSceneRect(rect());
35 #endif
38 void MainWindow::addElement(const QPoint& p) {
39 SCENE* scene =
40 #ifdef USE_KGC
41 this;
42 #else
43 m_scene;
44 #endif
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) {
83 addElement(e->pos());