~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to examples/tutorial/t14/gameboard.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2005-2005 Trolltech AS. All rights reserved.
 
4
**
 
5
** This file is part of the example classes of the Qt Toolkit.
 
6
**
 
7
** This file may be distributed under the terms of the Q Public License
 
8
** as defined by Trolltech AS of Norway and appearing in the file
 
9
** LICENSE.QPL included in the packaging of this file.
 
10
**
 
11
** This file may be distributed and/or modified under the terms of the
 
12
** GNU General Public License version 2 as published by the Free Software
 
13
** Foundation and appearing in the file LICENSE.GPL included in the
 
14
** packaging of this file.
 
15
**
 
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
 
17
**   information about Qt Commercial License Agreements.
 
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
 
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
 
20
**
 
21
** Contact info@trolltech.com if any conditions of this licensing are
 
22
** not clear to you.
 
23
**
 
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 
26
**
 
27
****************************************************************************/
 
28
 
 
29
/****************************************************************
 
30
**
 
31
** Implementation of GameBoard class, Qt tutorial 14
 
32
**
 
33
****************************************************************/
 
34
 
 
35
#include <QApplication>
 
36
#include <QFont>
 
37
#include <QGridLayout>
 
38
#include <QHBoxLayout>
 
39
#include <QLCDNumber>
 
40
#include <QLabel>
 
41
#include <QPushButton>
 
42
#include <QShortcut>
 
43
#include <QVBoxLayout>
 
44
#include <QWidget>
 
45
 
 
46
#include "cannonfield.h"
 
47
#include "gameboard.h"
 
48
#include "lcdrange.h"
 
49
 
 
50
GameBoard::GameBoard(QWidget *parent)
 
51
    : QWidget(parent)
 
52
{
 
53
    QPushButton *quit = new QPushButton("&Quit");
 
54
    quit->setFont(QFont("Times", 18, QFont::Bold));
 
55
 
 
56
    connect(quit, SIGNAL(clicked()), qApp, SLOT(quit()));
 
57
 
 
58
    LCDRange *angle = new LCDRange("ANGLE");
 
59
    angle->setRange(5, 70);
 
60
 
 
61
    LCDRange *force = new LCDRange("FORCE");
 
62
    force->setRange(10, 50);
 
63
 
 
64
    QFrame *cannonBox = new QFrame;
 
65
    cannonBox->setFrameStyle(QFrame::WinPanel | QFrame::Sunken);
 
66
 
 
67
    cannonField = new CannonField;
 
68
 
 
69
    connect(angle, SIGNAL(valueChanged(int)),
 
70
            cannonField, SLOT(setAngle(int)));
 
71
    connect(cannonField, SIGNAL(angleChanged(int)),
 
72
            angle, SLOT(setValue(int)));
 
73
 
 
74
    connect(force, SIGNAL(valueChanged(int)),
 
75
            cannonField, SLOT(setForce(int)));
 
76
    connect(cannonField, SIGNAL(forceChanged(int)),
 
77
            force, SLOT(setValue(int)));
 
78
 
 
79
    connect(cannonField, SIGNAL(hit()),
 
80
            this, SLOT(hit()));
 
81
    connect(cannonField, SIGNAL(missed()),
 
82
            this, SLOT(missed()));
 
83
 
 
84
    QPushButton *shoot = new QPushButton("&Shoot");
 
85
    shoot->setFont(QFont("Times", 18, QFont::Bold));
 
86
 
 
87
    connect(shoot, SIGNAL(clicked()),
 
88
            this, SLOT(fire()));
 
89
    connect(cannonField, SIGNAL(canShoot(bool)),
 
90
            shoot, SLOT(setEnabled(bool)));
 
91
 
 
92
    QPushButton *restart = new QPushButton("&New Game");
 
93
    restart->setFont(QFont("Times", 18, QFont::Bold));
 
94
 
 
95
    connect(restart, SIGNAL(clicked()), this, SLOT(newGame()));
 
96
 
 
97
    hits = new QLCDNumber(2);
 
98
    shotsLeft = new QLCDNumber(2);
 
99
    QLabel *hitsLabel = new QLabel("HITS");
 
100
    QLabel *shotsLeftLabel = new QLabel("SHOTS LEFT");
 
101
 
 
102
    (void) new QShortcut(Qt::Key_Enter, this, SLOT(fire()));
 
103
    (void) new QShortcut(Qt::Key_Return, this, SLOT(fire()));
 
104
    (void) new QShortcut(Qt::CTRL + Qt::Key_Q, this, SLOT(close()));
 
105
 
 
106
    QHBoxLayout *topLayout = new QHBoxLayout;
 
107
    topLayout->addWidget(shoot);
 
108
    topLayout->addWidget(hits);
 
109
    topLayout->addWidget(hitsLabel);
 
110
    topLayout->addWidget(shotsLeft);
 
111
    topLayout->addWidget(shotsLeftLabel);
 
112
    topLayout->addStretch(1);
 
113
    topLayout->addWidget(restart);
 
114
 
 
115
    QVBoxLayout *leftLayout = new QVBoxLayout;
 
116
    leftLayout->addWidget(angle);
 
117
    leftLayout->addWidget(force);
 
118
 
 
119
    QVBoxLayout *cannonLayout = new QVBoxLayout;
 
120
    cannonLayout->addWidget(cannonField);
 
121
    cannonBox->setLayout(cannonLayout);
 
122
 
 
123
    QGridLayout *gridLayout = new QGridLayout;
 
124
    gridLayout->addWidget(quit, 0, 0);
 
125
    gridLayout->addLayout(topLayout, 0, 1);
 
126
    gridLayout->addLayout(leftLayout, 1, 0);
 
127
    gridLayout->addWidget(cannonBox, 1, 1, 2, 1);
 
128
    gridLayout->setColumnStretch(1, 10);
 
129
    setLayout(gridLayout);
 
130
 
 
131
    angle->setValue(60);
 
132
    force->setValue(25);
 
133
    angle->setFocus();
 
134
 
 
135
    newGame();
 
136
}
 
137
 
 
138
void GameBoard::fire()
 
139
{
 
140
    if (cannonField->gameOver() || cannonField->isShooting())
 
141
        return;
 
142
    shotsLeft->display(shotsLeft->intValue() - 1);
 
143
    cannonField->shoot();
 
144
}
 
145
 
 
146
void GameBoard::hit()
 
147
{
 
148
    hits->display(hits->intValue() + 1);
 
149
    if (shotsLeft->intValue() == 0)
 
150
        cannonField->setGameOver();
 
151
    else
 
152
        cannonField->newTarget();
 
153
}
 
154
 
 
155
void GameBoard::missed()
 
156
{
 
157
    if (shotsLeft->intValue() == 0)
 
158
        cannonField->setGameOver();
 
159
}
 
160
 
 
161
void GameBoard::newGame()
 
162
{
 
163
    shotsLeft->display(15);
 
164
    hits->display(0);
 
165
    cannonField->restartGame();
 
166
    cannonField->newTarget();
 
167
}