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

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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Qt 4.0: gameboard.cpp Example File (tutorial/t14/gameboard.cpp)</title>
    <style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }
a:link { color: #004faf; text-decoration: none }
a:visited { color: #672967; text-decoration: none }
td.postheader { font-family: sans-serif }
tr.address { font-family: sans-serif }
body { background: #ffffff; color: black; }</style>
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td align="left" valign="top" width="32"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></td>
<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a>&nbsp;&middot; <a href="annotated.html"><font color="#004faf">Annotated</font></a>&nbsp;&middot; <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a>&nbsp;&middot; <a href="functions.html"><font color="#004faf">Functions</font></a></td>
<td align="right" valign="top" width="230"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></td></tr></table><h1 align="center">gameboard.cpp Example File<br /><small><small>tutorial/t14/gameboard.cpp</small></small></h1>
<pre>&nbsp;   /****************************************************************************
    **
    ** Copyright (C) 2005-2005 Trolltech AS. All rights reserved.
    **
    ** This file is part of the documentation of the Qt Toolkit.
    **
    ** This file may be distributed under the terms of the Q Public License
** as defined by Trolltech AS of Norway and appearing in the file
** LICENSE.QPL included in the packaging of this file.
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
**   information about Qt Commercial License Agreements.
** See http://www.trolltech.com/qpl/ for QPL licensing information.
** See http://www.trolltech.com/gpl/ for GPL licensing information.
**
** Contact info@trolltech.com if any conditions of this licensing are
** not clear to you.
    **
    ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
    ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
    **
    ****************************************************************************/

    /****************************************************************
    **
    ** Implementation of GameBoard class, Qt tutorial 14
    **
    ****************************************************************/

    #include &lt;QApplication&gt;
    #include &lt;QFont&gt;
    #include &lt;QGridLayout&gt;
    #include &lt;QHBoxLayout&gt;
    #include &lt;QLCDNumber&gt;
    #include &lt;QLabel&gt;
    #include &lt;QPushButton&gt;
    #include &lt;QShortcut&gt;
    #include &lt;QVBoxLayout&gt;
    #include &lt;QWidget&gt;

    #include &quot;cannonfield.h&quot;
    #include &quot;gameboard.h&quot;
    #include &quot;lcdrange.h&quot;

    GameBoard::GameBoard(QWidget *parent)
        : QWidget(parent)
    {
        QPushButton *quit = new QPushButton(&quot;&amp;Quit&quot;);
        quit-&gt;setFont(QFont(&quot;Times&quot;, 18, QFont::Bold));

        connect(quit, SIGNAL(clicked()), qApp, SLOT(quit()));

        LCDRange *angle = new LCDRange(&quot;ANGLE&quot;);
        angle-&gt;setRange(5, 70);

        LCDRange *force = new LCDRange(&quot;FORCE&quot;);
        force-&gt;setRange(10, 50);

        QFrame *cannonBox = new QFrame;
        cannonBox-&gt;setFrameStyle(QFrame::WinPanel | QFrame::Sunken);

        cannonField = new CannonField;

        connect(angle, SIGNAL(valueChanged(int)),
                cannonField, SLOT(setAngle(int)));
        connect(cannonField, SIGNAL(angleChanged(int)),
                angle, SLOT(setValue(int)));

        connect(force, SIGNAL(valueChanged(int)),
                cannonField, SLOT(setForce(int)));
        connect(cannonField, SIGNAL(forceChanged(int)),
                force, SLOT(setValue(int)));

        connect(cannonField, SIGNAL(hit()),
                this, SLOT(hit()));
        connect(cannonField, SIGNAL(missed()),
                this, SLOT(missed()));

        QPushButton *shoot = new QPushButton(&quot;&amp;Shoot&quot;);
        shoot-&gt;setFont(QFont(&quot;Times&quot;, 18, QFont::Bold));

        connect(shoot, SIGNAL(clicked()),
                this, SLOT(fire()));
        connect(cannonField, SIGNAL(canShoot(bool)),
                shoot, SLOT(setEnabled(bool)));

        QPushButton *restart = new QPushButton(&quot;&amp;New Game&quot;);
        restart-&gt;setFont(QFont(&quot;Times&quot;, 18, QFont::Bold));

        connect(restart, SIGNAL(clicked()), this, SLOT(newGame()));

        hits = new QLCDNumber(2);
        shotsLeft = new QLCDNumber(2);
        QLabel *hitsLabel = new QLabel(&quot;HITS&quot;);
        QLabel *shotsLeftLabel = new QLabel(&quot;SHOTS LEFT&quot;);

        (void) new QShortcut(Qt::Key_Enter, this, SLOT(fire()));
        (void) new QShortcut(Qt::Key_Return, this, SLOT(fire()));
        (void) new QShortcut(Qt::CTRL + Qt::Key_Q, this, SLOT(close()));

        QHBoxLayout *topLayout = new QHBoxLayout;
        topLayout-&gt;addWidget(shoot);
        topLayout-&gt;addWidget(hits);
        topLayout-&gt;addWidget(hitsLabel);
        topLayout-&gt;addWidget(shotsLeft);
        topLayout-&gt;addWidget(shotsLeftLabel);
        topLayout-&gt;addStretch(1);
        topLayout-&gt;addWidget(restart);

        QVBoxLayout *leftLayout = new QVBoxLayout;
        leftLayout-&gt;addWidget(angle);
        leftLayout-&gt;addWidget(force);

        QVBoxLayout *cannonLayout = new QVBoxLayout;
        cannonLayout-&gt;addWidget(cannonField);
        cannonBox-&gt;setLayout(cannonLayout);

        QGridLayout *gridLayout = new QGridLayout;
        gridLayout-&gt;addWidget(quit, 0, 0);
        gridLayout-&gt;addLayout(topLayout, 0, 1);
        gridLayout-&gt;addLayout(leftLayout, 1, 0);
        gridLayout-&gt;addWidget(cannonBox, 1, 1, 2, 1);
        gridLayout-&gt;setColumnStretch(1, 10);
        setLayout(gridLayout);

        angle-&gt;setValue(60);
        force-&gt;setValue(25);
        angle-&gt;setFocus();

        newGame();
    }

    void GameBoard::fire()
    {
        if (cannonField-&gt;gameOver() || cannonField-&gt;isShooting())
            return;
        shotsLeft-&gt;display(shotsLeft-&gt;intValue() - 1);
        cannonField-&gt;shoot();
    }

    void GameBoard::hit()
    {
        hits-&gt;display(hits-&gt;intValue() + 1);
        if (shotsLeft-&gt;intValue() == 0)
            cannonField-&gt;setGameOver();
        else
            cannonField-&gt;newTarget();
    }

    void GameBoard::missed()
    {
        if (shotsLeft-&gt;intValue() == 0)
            cannonField-&gt;setGameOver();
    }

    void GameBoard::newGame()
    {
        shotsLeft-&gt;display(15);
        hits-&gt;display(0);
        cannonField-&gt;restartGame();
        cannonField-&gt;newTarget();
    }</pre>
<p /><address><hr /><div align="center">
<table width="100%" cellspacing="0" border="0"><tr class="address">
<td width="30%">Copyright &copy; 2005 <a href="trolltech.html">Trolltech</a></td>
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
<td width="30%" align="right"><div align="right">Qt 4.0.0</div></td>
</tr></table></div></address></body>
</html>