Skip to content
Snippets Groups Projects
Commit a8a17fc9 authored by Paul Tvete's avatar Paul Tvete
Browse files

Use layouts instead of hardcoded sizes

parent 2f4c634c
Branches master
No related tags found
No related merge requests found
......@@ -20,6 +20,7 @@ public:
int force() const { return f; }
bool gameOver() const { return gameEnded; }
bool isShooting() const { return shooting; }
QSize sizeHint() const { return QSize(400, 300); }
public slots:
void setAngle( int degrees );
void setForce( int newton );
......
......@@ -13,14 +13,15 @@
#include <qpushbutton.h>
#include <qlcdnumber.h>
#include <QBoxLayout>
#include <QGridLayout>
#include "lcdrange.h"
#include "cannon.h"
GameBoard::GameBoard(QWidget *parent)
: QWidget( parent )
{
setMinimumSize( 500, 355 );
quit = new QPushButton( "Quit", this );
quit->setFont( QFont( "Times", 18, QFont::Bold ) );
......@@ -35,7 +36,7 @@ GameBoard::GameBoard(QWidget *parent)
frame = new QFrame( this );
frame->setFrameStyle( QFrame::WinPanel | QFrame::Sunken );
cannonField = new CannonField( this );
cannonField = new CannonField( frame );
cannonField->setPalette( QPalette(QColor(250, 250, 200)) );
cannonField->setAutoFillBackground(true);
......@@ -72,28 +73,36 @@ GameBoard::GameBoard(QWidget *parent)
QShortcut *quitShortcut = new QShortcut(Qt::Key_Q, this);
connect(quitShortcut, SIGNAL(activated()), qApp, SLOT(quit()));
quit->setGeometry( 10, 10, 75, 30 );
angle->setGeometry( 10, quit->y() + quit->height() + 10, 75, 130 );
force->setGeometry( 10, angle->y() + angle->height() + 10, 75, 130 );
frame->move( angle->x() + angle->width() + 10, angle->y() );
cannonField->move( frame->x() + 2, frame->y() + 2 );
shoot->setGeometry( 10, 315, 75, 30 );
restart->setGeometry( 380, 10, 110, 30 );
hits->setGeometry( 130, 10, 40, 30 );
hitsL->setGeometry( hits->x() + hits->width() + 5, 10, 60, 30 );
shotsLeft->setGeometry( 240, 10, 40, 30 );
shotsLeftL->setGeometry( shotsLeft->x()+shotsLeft->width()+5, 10, 60, 30 );
QGridLayout *topLayout = new QGridLayout(this);
topLayout->addWidget(quit, 0, 0);
QVBoxLayout *sideColumn = new QVBoxLayout;
topLayout->addLayout(sideColumn, 1, 0);
sideColumn->addWidget(angle);
sideColumn->addWidget(force);
sideColumn->addWidget(shoot);
QHBoxLayout *topRow = new QHBoxLayout;
topLayout->addLayout(topRow, 0, 1);
topRow->addStretch();
topRow->addWidget(hits);
topRow->addWidget(hitsL);
topRow->addStretch();
topRow->addWidget(shotsLeft);
topRow->addWidget(shotsLeftL);
topRow->addStretch();
topRow->addWidget(restart);
topLayout->addWidget(frame, 1, 1);
QVBoxLayout *frameLayout = new QVBoxLayout(frame);
frameLayout->addWidget(cannonField);
int margin = frame->frameWidth();
frameLayout->setContentsMargins(margin, margin, margin, margin);
newGame();
}
void GameBoard::resizeEvent( QResizeEvent * )
{
frame->resize( width() - frame->x() - 10,
height() - frame->y() - 10 );
cannonField->resize( frame->width() - 4, frame->height() - 4 );
}
void GameBoard::fire()
{
if ( cannonField->gameOver() || cannonField->isShooting() )
......
......@@ -24,8 +24,6 @@ class GameBoard : public QWidget
Q_OBJECT
public:
GameBoard( QWidget *parent=0 );
protected:
void resizeEvent( QResizeEvent * );
protected slots:
void fire();
void hit();
......
......@@ -14,7 +14,6 @@ int main( int argc, char **argv )
QApplication a( argc, argv );
GameBoard gb;
gb.setGeometry( 100, 100, 500, 355 );
gb.show();
return a.exec();
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment