Rev 9 | Go to most recent revision | Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1 | muzer | 1 | #include "mainwindow.h" |
| 2 | #include "ui_mainwindow.h" |
||
| 3 | |||
| 4 | MainWindow::MainWindow(QWidget *parent) : |
||
| 5 | QMainWindow(parent), |
||
| 6 | ui(new Ui::MainWindow) |
||
| 7 | { |
||
| 8 | ui->setupUi(this); |
||
| 9 | |||
| 10 | this->setMinimumSize(800, 600); |
||
| 11 | this->resize(800, 600); |
||
| 12 | this->setMaximumSize(800, 600); |
||
| 13 | |||
| 14 | server = new QTcpServer(this); |
||
| 15 | connect(server, SIGNAL(newConnection()), this, SLOT(newConnection())); |
||
| 16 | server->listen(QHostAddress::Any, 4567); |
||
| 17 | |||
| 18 | timer = new QTimer(this); |
||
| 19 | connect(timer, SIGNAL(timeout()), this, SLOT(update())); |
||
| 20 | timer->start(50); |
||
| 21 | |||
| 22 | id = 0; |
||
| 23 | } |
||
| 24 | |||
| 25 | MainWindow::~MainWindow() |
||
| 26 | { |
||
| 27 | delete ui; |
||
| 28 | delete server; |
||
| 29 | } |
||
| 30 | |||
| 31 | void MainWindow::reset() |
||
| 32 | { |
||
| 33 | for (int i = 0; i < bikes.count(); i++) |
||
| 34 | { |
||
| 35 | bikes[i]->reset(); |
||
| 36 | } |
||
| 37 | } |
||
| 38 | |||
| 39 | void MainWindow::changeEvent(QEvent *e) |
||
| 40 | { |
||
| 41 | QMainWindow::changeEvent(e); |
||
| 42 | switch (e->type()) { |
||
| 43 | case QEvent::LanguageChange: |
||
| 44 | ui->retranslateUi(this); |
||
| 45 | break; |
||
| 46 | default: |
||
| 47 | break; |
||
| 48 | } |
||
| 49 | } |
||
| 50 | |||
| 51 | void MainWindow::paintEvent(QPaintEvent *e) |
||
| 52 | { |
||
| 53 | e->accept(); |
||
| 54 | QPainter painter(this); |
||
| 55 | |||
| 56 | painter.setRenderHint(QPainter::Antialiasing, true); |
||
| 57 | |||
| 58 | bool ready = true; |
||
| 59 | for (int i = 0; i < bikes.count(); i++) |
||
| 60 | { |
||
| 61 | if (!bikes[i]->isReady) |
||
| 62 | { |
||
| 63 | ready = false; |
||
| 64 | } |
||
| 65 | } |
||
| 66 | |||
| 67 | if (ready) |
||
| 68 | { |
||
| 69 | for (int i = 0; i < bikes.count(); i++) |
||
| 70 | { |
||
| 71 | if (bikes[i]->show) |
||
| 72 | { |
||
| 73 | bikes[i]->hasHadGo = false; |
||
| 74 | bikes[i]->run(bikes); |
||
| 75 | bikes[i]->draw(&painter); |
||
| 76 | } |
||
| 77 | } |
||
| 78 | Bike *bike; |
||
| 79 | foreach(bike,bikes){ |
||
| 80 | while(!bike->hasHadGo){ |
||
| 81 | } |
||
| 82 | } |
||
| 83 | } |
||
| 84 | |||
| 85 | checkClients(); |
||
| 86 | } |
||
| 87 | |||
| 88 | void MainWindow::newConnection() |
||
| 89 | { |
||
| 90 | while (server->hasPendingConnections()) |
||
| 91 | { |
||
| 92 | QTcpSocket *socket = server->nextPendingConnection(); |
||
| 93 | Bike *bike = new Bike(socket, id); |
||
| 94 | id += 1; |
||
| 95 | reset(); |
||
| 96 | |||
| 97 | bikes.append(bike); |
||
| 98 | cout << ":: New Bike: " << socket->peerAddress().toString().toStdString() << endl; |
||
| 99 | } |
||
| 100 | } |
||
| 101 | |||
| 102 | void MainWindow::checkClients() |
||
| 103 | { |
||
| 104 | for (int i = bikes.count() - 1; i >= 0; i--) |
||
| 105 | { |
||
| 106 | if (bikes[i]->isDisconnected) |
||
| 107 | { |
||
| 108 | delete bikes[i]; |
||
| 109 | bikes.removeAt(i); |
||
| 110 | cout << ":: Removed Bike: " << i << endl; |
||
| 111 | } |
||
| 112 | } |
||
| 113 | } |