Commit a8a17fc9 authored by Paul Tvete's avatar Paul Tvete
Browse files

Use layouts instead of hardcoded sizes

parent 2f4c634c
Loading
Loading
Loading
Loading
+1 −0
Original line number Original line Diff line number Diff line
@@ -20,6 +20,7 @@ public:
    int   force()      const { return f; }
    int   force()      const { return f; }
    bool  gameOver()   const { return gameEnded; }
    bool  gameOver()   const { return gameEnded; }
    bool  isShooting() const { return shooting; }
    bool  isShooting() const { return shooting; }
    QSize sizeHint()   const { return QSize(400, 300); }
public slots:
public slots:
    void  setAngle( int degrees );
    void  setAngle( int degrees );
    void  setForce( int newton );
    void  setForce( int newton );
+30 −21
Original line number Original line Diff line number Diff line
@@ -13,14 +13,15 @@
#include <qpushbutton.h>
#include <qpushbutton.h>
#include <qlcdnumber.h>
#include <qlcdnumber.h>


#include <QBoxLayout>
#include <QGridLayout>

#include "lcdrange.h"
#include "lcdrange.h"
#include "cannon.h"
#include "cannon.h"


GameBoard::GameBoard(QWidget *parent)
GameBoard::GameBoard(QWidget *parent)
        : QWidget( parent )
        : QWidget( parent )
{
{
    setMinimumSize( 500, 355 );

    quit = new QPushButton( "Quit", this );
    quit = new QPushButton( "Quit", this );
    quit->setFont( QFont( "Times", 18, QFont::Bold ) );
    quit->setFont( QFont( "Times", 18, QFont::Bold ) );


@@ -35,7 +36,7 @@ GameBoard::GameBoard(QWidget *parent)
    frame = new QFrame( this );
    frame = new QFrame( this );
    frame->setFrameStyle( QFrame::WinPanel | QFrame::Sunken );
    frame->setFrameStyle( QFrame::WinPanel | QFrame::Sunken );


    cannonField = new CannonField( this );
    cannonField = new CannonField( frame );


    cannonField->setPalette( QPalette(QColor(250, 250, 200)) );
    cannonField->setPalette( QPalette(QColor(250, 250, 200)) );
    cannonField->setAutoFillBackground(true);
    cannonField->setAutoFillBackground(true);
@@ -72,28 +73,36 @@ GameBoard::GameBoard(QWidget *parent)
    QShortcut *quitShortcut = new QShortcut(Qt::Key_Q, this);
    QShortcut *quitShortcut = new QShortcut(Qt::Key_Q, this);
    connect(quitShortcut, SIGNAL(activated()), qApp, SLOT(quit()));
    connect(quitShortcut, SIGNAL(activated()), qApp, SLOT(quit()));


    quit->setGeometry( 10, 10, 75, 30 );
    QGridLayout *topLayout = new QGridLayout(this);
    angle->setGeometry( 10, quit->y() + quit->height() + 10, 75, 130 );
    topLayout->addWidget(quit, 0, 0);
    force->setGeometry( 10, angle->y() + angle->height() + 10, 75, 130 );

    frame->move( angle->x() + angle->width() + 10, angle->y() );
    QVBoxLayout *sideColumn = new QVBoxLayout;
    cannonField->move( frame->x() + 2, frame->y() + 2 );
    topLayout->addLayout(sideColumn, 1, 0);
    shoot->setGeometry( 10, 315, 75, 30 );
    sideColumn->addWidget(angle);
    restart->setGeometry( 380, 10, 110, 30 );
    sideColumn->addWidget(force);
    hits->setGeometry( 130, 10, 40, 30 );
    sideColumn->addWidget(shoot);
    hitsL->setGeometry( hits->x() + hits->width() + 5, 10, 60, 30 );

    shotsLeft->setGeometry( 240, 10, 40, 30 );
    QHBoxLayout *topRow = new QHBoxLayout;
    shotsLeftL->setGeometry( shotsLeft->x()+shotsLeft->width()+5, 10, 60, 30 );
    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();
    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()
void GameBoard::fire()
{
{
    if ( cannonField->gameOver() || cannonField->isShooting() )
    if ( cannonField->gameOver() || cannonField->isShooting() )
+0 −2
Original line number Original line Diff line number Diff line
@@ -24,8 +24,6 @@ class GameBoard : public QWidget
    Q_OBJECT
    Q_OBJECT
public:
public:
    GameBoard( QWidget *parent=0 );
    GameBoard( QWidget *parent=0 );
protected:
    void  resizeEvent( QResizeEvent * );
protected slots:
protected slots:
    void  fire();
    void  fire();
    void  hit();
    void  hit();
+0 −1
Original line number Original line Diff line number Diff line
@@ -14,7 +14,6 @@ int main( int argc, char **argv )
    QApplication a( argc, argv );
    QApplication a( argc, argv );


    GameBoard gb;
    GameBoard gb;
    gb.setGeometry( 100, 100, 500, 355 );
    gb.show();
    gb.show();
    return a.exec();
    return a.exec();
}
}