summaryrefslogtreecommitdiff
path: root/examples
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
parent4a49f40ad8bd56962cc9efea6439a4f9b6ffb9f7 (diff)
downloadqttools-0d36e0d0e1374517bd196c74b0f148abfaf1a58a.tar.gz
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')
-rw-r--r--examples/designer/taskmenuextension/tictactoe.cpp57
-rw-r--r--examples/designer/taskmenuextension/tictactoe.h2
-rw-r--r--examples/designer/taskmenuextension/tictactoedialog.cpp3
-rw-r--r--examples/designer/taskmenuextension/tictactoeplugin.cpp2
4 files changed, 31 insertions, 33 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};
}
diff --git a/examples/designer/taskmenuextension/tictactoe.h b/examples/designer/taskmenuextension/tictactoe.h
index 23784d750..5f081bbdc 100644
--- a/examples/designer/taskmenuextension/tictactoe.h
+++ b/examples/designer/taskmenuextension/tictactoe.h
@@ -82,7 +82,7 @@ private:
static constexpr char16_t Cross = 'X';
static constexpr char16_t Nought = 'O';
- QRect cellRect(int row, int col) const;
+ QRect cellRect(int position) const;
int cellWidth() const { return width() / 3; }
int cellHeight() const { return height() / 3; }
diff --git a/examples/designer/taskmenuextension/tictactoedialog.cpp b/examples/designer/taskmenuextension/tictactoedialog.cpp
index 8b62064f8..a13b093d2 100644
--- a/examples/designer/taskmenuextension/tictactoedialog.cpp
+++ b/examples/designer/taskmenuextension/tictactoedialog.cpp
@@ -74,11 +74,10 @@ TicTacToeDialog::TicTacToeDialog(TicTacToe *tic, QWidget *parent)
connect(buttonBox, &QDialogButtonBox::accepted, this, &TicTacToeDialog::saveState);
connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
- QVBoxLayout *mainLayout = new QVBoxLayout;
+ QVBoxLayout *mainLayout = new QVBoxLayout(this);
mainLayout->addWidget(editor);
mainLayout->addWidget(buttonBox);
- setLayout(mainLayout);
setWindowTitle(tr("Edit State"));
}
//! [0]
diff --git a/examples/designer/taskmenuextension/tictactoeplugin.cpp b/examples/designer/taskmenuextension/tictactoeplugin.cpp
index 45075fe62..5515526bc 100644
--- a/examples/designer/taskmenuextension/tictactoeplugin.cpp
+++ b/examples/designer/taskmenuextension/tictactoeplugin.cpp
@@ -74,7 +74,7 @@ QString TicTacToePlugin::group() const
QString TicTacToePlugin::toolTip() const
{
- return QString();
+ return QStringLiteral("Tic Tac Toe Example, demonstrating class QDesignerTaskMenuExtension (C++)");
}
QString TicTacToePlugin::whatsThis() const