Commit ae2b2892 authored by Laurent Montel's avatar Laurent Montel 😁
Browse files

Move code in cpp file. Initialize value/pointer

parent 6ccd6aba
......@@ -22,7 +22,7 @@
QHash<KMinesState::BorderElement, QString> BorderItem::s_elementNames;
BorderItem::BorderItem( KGameRenderer* renderer, QGraphicsItem* parent )
: KGameRenderedItem(renderer, QLatin1String( "" ), parent), m_element(KMinesState::BorderEast),
: KGameRenderedItem(renderer, QString(), parent), m_element(KMinesState::BorderEast),
m_row(-1), m_col(-1)
{
if(s_elementNames.isEmpty())
......@@ -30,11 +30,38 @@ BorderItem::BorderItem( KGameRenderer* renderer, QGraphicsItem* parent )
setShapeMode(BoundingRectShape);
}
void BorderItem::setBorderType(KMinesState::BorderElement e)
{
m_element = e;
updatePixmap();
}
void BorderItem::setRowCol(int row, int col)
{
m_row = row;
m_col = col;
}
int BorderItem::row() const
{
return m_row;
}
int BorderItem::col() const
{
return m_col;
}
void BorderItem::updatePixmap()
{
setSpriteKey(s_elementNames[m_element]);
}
int BorderItem::type() const
{
return Type;
}
void BorderItem::fillNameHash()
{
s_elementNames[KMinesState::BorderNorth] = QStringLiteral( "border.edge.north" );
......
......@@ -31,22 +31,22 @@ class BorderItem : public KGameRenderedItem
{
public:
BorderItem( KGameRenderer* renderer, QGraphicsItem* parent );
void setBorderType( KMinesState::BorderElement e ) { m_element = e; updatePixmap(); }
void setRowCol( int row, int col ) { m_row = row; m_col = col; }
int row() const { return m_row; }
int col() const { return m_col; }
void setBorderType( KMinesState::BorderElement e );
void setRowCol( int row, int col );
Q_REQUIRED_RESULT int row() const;
Q_REQUIRED_RESULT int col() const;
void updatePixmap();
// enable use of qgraphicsitem_cast
enum { Type = UserType + 1 };
int type() const override { return Type; }
Q_REQUIRED_RESULT int type() const override;
private:
static QHash<KMinesState::BorderElement, QString> s_elementNames;
static void fillNameHash();
KMinesState::BorderElement m_element;
int m_row;
int m_col;
int m_row = -1;
int m_col = -1;
};
#endif
......@@ -25,7 +25,7 @@ QHash<int, QString> CellItem::s_digitNames;
QHash<KMinesState::CellState, QList<QString> > CellItem::s_stateNames;
CellItem::CellItem(KGameRenderer* renderer, QGraphicsItem* parent)
: KGameRenderedItem(renderer, QLatin1String( "" ), parent)
: KGameRenderedItem(renderer, QString(), parent)
{
if(s_digitNames.isEmpty())
fillNameHashes();
......@@ -44,6 +44,27 @@ void CellItem::unexplode()
m_exploded = false;
}
bool CellItem::isRevealed() const
{
return ( m_state == KMinesState::Revealed || m_state == KMinesState::Error);
}
bool CellItem::isFlagged() const
{
return m_state == KMinesState::Flagged;
}
bool CellItem::isQuestioned() const
{
return m_state == KMinesState::Questioned;
}
bool CellItem::isExploded() const
{
return m_exploded;
}
void CellItem::reset()
{
m_state = KMinesState::Released;
......@@ -85,6 +106,27 @@ void CellItem::setRenderSize(const QSize &renderSize)
}
}
void CellItem::setHasMine(bool hasMine)
{
m_hasMine = hasMine;
}
bool CellItem::hasMine() const
{
return m_hasMine;
}
void CellItem::setDigit(int digit)
{
m_digit = digit;
updatePixmap();
}
int CellItem::digit() const
{
return m_digit;
}
void CellItem::press()
{
if(m_state == KMinesState::Released)
......@@ -133,6 +175,11 @@ void CellItem::mark()
updatePixmap();
}
int CellItem::type() const
{
return Type;
}
void CellItem::reveal()
{
if(isRevealed())
......@@ -145,6 +192,12 @@ void CellItem::reveal()
updatePixmap();
}
void CellItem::unreveal()
{
m_state = KMinesState::Released;
updatePixmap();
}
void CellItem::undoPress()
{
if(m_state == KMinesState::Pressed)
......
......@@ -47,21 +47,21 @@ public:
/**
* Sets whether this item holds mine or not
*/
void setHasMine(bool hasMine) { m_hasMine = hasMine; }
void setHasMine(bool hasMine);
/**
* @return whether this item holds mine
*/
bool hasMine() const { return m_hasMine; }
bool hasMine() const;
/**
* Sets this item so it holds a digit
*
* @param digit digit number (1 to 8)
*/
void setDigit(int digit) { m_digit = digit; updatePixmap(); }
void setDigit(int digit);
/**
* @return digit this item holds or 0 if none
*/
int digit() const { return m_digit; }
int digit() const;
/**
* Shows what this item hides :)
* Can be a bomb, a digit, an empty square
......@@ -71,7 +71,7 @@ public:
* Hides what this item shows ;).
* I.e. resets revealed state
*/
void unreveal() { m_state = KMinesState::Released; updatePixmap(); }
void unreveal();
/**
* Removes the flag
*/
......@@ -83,19 +83,19 @@ public:
/**
* @return whether this cell is revealed
*/
bool isRevealed() const { return ( m_state == KMinesState::Revealed || m_state == KMinesState::Error); }
bool isRevealed() const;
/**
* @return whether this cell is marked with flag
*/
bool isFlagged() const { return m_state == KMinesState::Flagged; }
bool isFlagged() const;
/**
* @return whether this cell is marked with question
*/
bool isQuestioned() const { return m_state == KMinesState::Questioned; }
bool isQuestioned() const;
/**
* @return whether this cell is exploded
*/
bool isExploded() const { return m_exploded; }
bool isExploded() const;
/**
* Resets all properties & state of an item to default ones
*/
......@@ -107,7 +107,7 @@ public:
void mark();
// enable use of qgraphicsitem_cast
enum { Type = UserType + 1 };
int type() const override { return Type; }
int type() const override;
Q_SIGNALS:
/**
* Emitted when this item is revealed with mouse click
......
......@@ -190,7 +190,7 @@ void KMinesMainWindow::onGameOver(bool won)
m_gameClock->pause();
m_actionPause->setEnabled(false);
Kg::difficulty()->setGameRunning(false);
if(won && m_scene->canScore)
if(won && m_scene->canScore())
{
QPointer<KScoreDialog> scoreDialog = new KScoreDialog(KScoreDialog::Name | KScoreDialog::Time, this);
scoreDialog->initFromDifficulty(Kg::difficulty());
......@@ -214,7 +214,7 @@ void KMinesMainWindow::onGameOver(bool won)
m_scene->reset();
m_gameClock->restart();
m_actionPause->setEnabled(true);
m_scene->canScore = !Settings::disableScoreOnReset();
m_scene->setCanScore(!Settings::disableScoreOnReset());
}
}
}
......
......@@ -45,10 +45,10 @@ private Q_SLOTS:
void loadSettings();
private:
void setupActions();
KMinesScene* m_scene;
KMinesView* m_view;
KGameClock* m_gameClock;
KToggleAction* m_actionPause;
KMinesScene* m_scene = nullptr;
KMinesView* m_view = nullptr;
KGameClock* m_gameClock = nullptr;
KToggleAction* m_actionPause = nullptr;
QPointer<QLabel> mineLabel = new QLabel;
QPointer<QLabel> timeLabel = new QLabel;
......
......@@ -219,6 +219,21 @@ QRectF MineFieldItem::boundingRect() const
return QRectF(0, 0, m_cellSize*(m_numCols+2), m_cellSize*(m_numRows+2));
}
int MineFieldItem::rowCount() const
{
return m_numRows;
}
int MineFieldItem::columnCount() const
{
return m_numCols;
}
int MineFieldItem::minesCount() const
{
return m_minesCount;
}
void MineFieldItem::paint( QPainter * painter, const QStyleOptionGraphicsItem* opt, QWidget* w)
{
Q_UNUSED(painter);
......
......@@ -69,15 +69,15 @@ public:
/**
* @return num rows in field
*/
int rowCount() const { return m_numRows; }
int rowCount() const;
/**
* @return num columns in field
*/
int columnCount() const { return m_numCols; }
int columnCount() const;
/**
* @return num mines in field
*/
int minesCount() const { return m_minesCount; }
int minesCount() const;
/**
* Minimal number of free positions on a field
......
......@@ -81,6 +81,16 @@ void KMinesScene::reset()
m_messageItem->forceHide();
}
bool KMinesScene::canScore() const
{
return m_canScore;
}
void KMinesScene::setCanScore(bool value)
{
m_canScore = value;
}
void KMinesScene::resizeScene(int width, int height)
{
setSceneRect(0, 0, width, height);
......
......@@ -57,12 +57,14 @@ public:
* Resets the scene
*/
void reset();
KGameRenderer& renderer() {return m_renderer;}
/**
* Represents if the scores should be considered for the highscores
*/
bool canScore;
bool canScore() const;
void setCanScore(bool value);
KGameRenderer& renderer() {return m_renderer;}
Q_SIGNALS:
void minesCountChanged(int);
void gameOver(bool);
......@@ -70,13 +72,14 @@ Q_SIGNALS:
private Q_SLOTS:
void onGameOver(bool);
private:
bool m_canScore;
KGameRenderer m_renderer;
/**
* Game field graphics item
*/
MineFieldItem* m_fieldItem;
KGamePopupItem* m_messageItem;
KGamePopupItem* m_gamePausedMessageItem;
MineFieldItem* m_fieldItem = nullptr;
KGamePopupItem* m_messageItem = nullptr;
KGamePopupItem* m_gamePausedMessageItem = nullptr;
};
class QResizeEvent;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment