summaryrefslogtreecommitdiffstats
path: root/examples/designer/taskmenuextension/tictactoe.cpp
diff options
context:
space:
mode:
authorFriedemann Kleint <Friedemann.Kleint@qt.io>2021-01-27 10:49:35 +0100
committerFriedemann Kleint <Friedemann.Kleint@qt.io>2021-02-03 11:40:00 +0100
commit0d36e0d0e1374517bd196c74b0f148abfaf1a58a (patch)
tree8b5d1bbd840b7b3c6b1b2f606fb9926d65df4062 /examples/designer/taskmenuextension/tictactoe.cpp
parent4a49f40ad8bd56962cc9efea6439a4f9b6ffb9f7 (diff)
Brush up the taskmenuextension example
When porting the code to Python, some issues showed up. - Rewrite cellRect() to take the position (reason being that porting the existing code to Python creates problems with implicit conversion to float in the division). - Use plain assignment by operator[] to replace the characters in the string - Avoid repeating return types. - Streamline code. - Initialize myState member variable. - Indicate C++ in the tool tip Task-number: PYSIDE-1455 Change-Id: I62fa61eebaa2eefe8db0842d6a3789bd7cd4257e Reviewed-by: Jarek Kobus <jaroslaw.kobus@qt.io>
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};
}