summaryrefslogtreecommitdiff
path: root/examples/designer/taskmenuextension/tictactoe.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'examples/designer/taskmenuextension/tictactoe.cpp')
-rw-r--r--examples/designer/taskmenuextension/tictactoe.cpp57
1 files changed, 28 insertions, 29 deletions
diff --git a/examples/designer/taskmenuextension/tictactoe.cpp b/examples/designer/taskmenuextension/tictactoe.cpp
index c6370a2cb..f96c5af2b 100644
--- a/examples/designer/taskmenuextension/tictactoe.cpp
+++ b/examples/designer/taskmenuextension/tictactoe.cpp
@@ -56,32 +56,31 @@
static inline QString defaultState() { return QStringLiteral("---------"); }
TicTacToe::TicTacToe(QWidget *parent)
- : QWidget(parent)
+ : QWidget(parent), myState(defaultState())
{
}
QSize TicTacToe::minimumSizeHint() const
{
- return QSize(200, 200);
+ return {200, 200};
}
QSize TicTacToe::sizeHint() const
{
- return QSize(200, 200);
+ return {200, 200};
}
void TicTacToe::setState(const QString &newState)
{
turnNumber = 0;
myState = defaultState();
- int position = 0;
- while (position < 9 && position < newState.length()) {
- QChar mark = newState.at(position);
+ const int count = qMin(9, int(newState.length()));
+ for (int position = 0; position < count; ++position) {
+ const QChar mark = newState.at(position);
if (mark == Cross || mark == Nought) {
++turnNumber;
- myState.replace(position, 1, mark);
+ myState[position] = mark;
}
- position++;
}
update();
}
@@ -102,19 +101,16 @@ void TicTacToe::mousePressEvent(QMouseEvent *event)
{
if (turnNumber == 9) {
clearBoard();
- update();
- } else {
- for (int position = 0; position < 9; ++position) {
- QRect cell = cellRect(position / 3, position % 3);
- if (cell.contains(event->pos())) {
- if (myState.at(position) == Empty) {
- if (turnNumber % 2 == 0)
- myState.replace(position, 1, Cross);
- else
- myState.replace(position, 1, Nought);
- ++turnNumber;
- update();
- }
+ return;
+ }
+ for (int position = 0; position < 9; ++position) {
+ const QRect &cell = cellRect(position);
+ if (cell.contains(event->position().toPoint())) {
+ if (myState.at(position) == Empty) {
+ myState[position] = turnNumber % 2 == 0
+ ? Cross : Nought;
+ ++turnNumber;
+ update();
}
}
}
@@ -134,7 +130,7 @@ void TicTacToe::paintEvent(QPaintEvent * /* event */)
painter.setPen(QPen(Qt::darkBlue, 2));
for (int position = 0; position < 9; ++position) {
- QRect cell = cellRect(position / 3, position % 3);
+ const QRect &cell = cellRect(position);
if (myState.at(position) == Cross) {
painter.drawLine(cell.topLeft(), cell.bottomRight());
@@ -150,7 +146,7 @@ void TicTacToe::paintEvent(QPaintEvent * /* event */)
if (myState.at(position) != Empty
&& myState.at(position + 1) == myState.at(position)
&& myState.at(position + 2) == myState.at(position)) {
- int y = cellRect((position / 3), 0).center().y();
+ const int y = cellRect(position).center().y();
painter.drawLine(0, y, width(), y);
turnNumber = 9;
}
@@ -160,7 +156,7 @@ void TicTacToe::paintEvent(QPaintEvent * /* event */)
if (myState.at(position) != Empty
&& myState.at(position + 3) == myState.at(position)
&& myState.at(position + 6) == myState.at(position)) {
- int x = cellRect(0, position).center().x();
+ const int x = cellRect(position).center().x();
painter.drawLine(x, 0, x, height());
turnNumber = 9;
}
@@ -177,12 +173,15 @@ void TicTacToe::paintEvent(QPaintEvent * /* event */)
}
}
-QRect TicTacToe::cellRect(int row, int column) const
+QRect TicTacToe::cellRect(int position) const
{
const int HMargin = width() / 30;
const int VMargin = height() / 30;
- return QRect(column * cellWidth() + HMargin,
- row * cellHeight() + VMargin,
- cellWidth() - 2 * HMargin,
- cellHeight() - 2 * VMargin);
+ const int row = position / 3;
+ const int column = position % 3;
+ const QPoint pos{column * cellWidth() + HMargin,
+ row * cellHeight() + VMargin};
+ const QSize size{cellWidth() - 2 * HMargin,
+ cellHeight() - 2 * VMargin};
+ return {pos, size};
}