From 52150469a837a1eca7e31b8aaea077127db0432f Mon Sep 17 00:00:00 2001 From: Paul Olav Tvete Date: Tue, 7 Feb 2023 09:51:10 +0100 Subject: [PATCH] Resurrect wiggly example as a manual test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In addition to being an iconic part of Qt's history, wiggly is also widely used for verifying on embedded hardware. Change-Id: Ica86626d98ade2570eebe787860293380c21f96a Reviewed-by: Friedemann Kleint Reviewed-by: Jan Arve Sæther --- .../examples/widgets/wiggly/CMakeLists.txt | 38 +++++++++++ .../manual/examples/widgets/wiggly/dialog.cpp | 27 ++++++++ tests/manual/examples/widgets/wiggly/dialog.h | 19 ++++++ tests/manual/examples/widgets/wiggly/main.cpp | 16 +++++ .../manual/examples/widgets/wiggly/wiggly.pro | 11 ++++ .../examples/widgets/wiggly/wigglywidget.cpp | 65 +++++++++++++++++++ .../examples/widgets/wiggly/wigglywidget.h | 32 +++++++++ 7 files changed, 208 insertions(+) create mode 100644 tests/manual/examples/widgets/wiggly/CMakeLists.txt create mode 100644 tests/manual/examples/widgets/wiggly/dialog.cpp create mode 100644 tests/manual/examples/widgets/wiggly/dialog.h create mode 100644 tests/manual/examples/widgets/wiggly/main.cpp create mode 100644 tests/manual/examples/widgets/wiggly/wiggly.pro create mode 100644 tests/manual/examples/widgets/wiggly/wigglywidget.cpp create mode 100644 tests/manual/examples/widgets/wiggly/wigglywidget.h diff --git a/tests/manual/examples/widgets/wiggly/CMakeLists.txt b/tests/manual/examples/widgets/wiggly/CMakeLists.txt new file mode 100644 index 0000000000..c529c20bfa --- /dev/null +++ b/tests/manual/examples/widgets/wiggly/CMakeLists.txt @@ -0,0 +1,38 @@ +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause + +cmake_minimum_required(VERSION 3.16) +project(wiggly LANGUAGES CXX) + +if(NOT DEFINED INSTALL_EXAMPLESDIR) + set(INSTALL_EXAMPLESDIR "examples") +endif() + +set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/widgets/widgets/wiggly") + +find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets) + +qt_standard_project_setup() + +qt_add_executable(wiggly + dialog.cpp dialog.h + main.cpp + wigglywidget.cpp wigglywidget.h +) + +set_target_properties(wiggly PROPERTIES + WIN32_EXECUTABLE TRUE + MACOSX_BUNDLE TRUE +) + +target_link_libraries(wiggly PRIVATE + Qt6::Core + Qt6::Gui + Qt6::Widgets +) + +install(TARGETS wiggly + RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}" + BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}" + LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}" +) diff --git a/tests/manual/examples/widgets/wiggly/dialog.cpp b/tests/manual/examples/widgets/wiggly/dialog.cpp new file mode 100644 index 0000000000..0f3aa84fd8 --- /dev/null +++ b/tests/manual/examples/widgets/wiggly/dialog.cpp @@ -0,0 +1,27 @@ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +#include "dialog.h" +#include "wigglywidget.h" + +#include +#include + +//! [0] +Dialog::Dialog(QWidget *parent) + : QDialog(parent) +{ + WigglyWidget *wigglyWidget = new WigglyWidget; + QLineEdit *lineEdit = new QLineEdit; + + QVBoxLayout *layout = new QVBoxLayout(this); + layout->addWidget(wigglyWidget); + layout->addWidget(lineEdit); + + connect(lineEdit, &QLineEdit::textChanged, wigglyWidget, &WigglyWidget::setText); + lineEdit->setText(u8"🖖 " + tr("Hello world!")); + + setWindowTitle(tr("Wiggly")); + resize(360, 145); +} +//! [0] diff --git a/tests/manual/examples/widgets/wiggly/dialog.h b/tests/manual/examples/widgets/wiggly/dialog.h new file mode 100644 index 0000000000..1f27668f4a --- /dev/null +++ b/tests/manual/examples/widgets/wiggly/dialog.h @@ -0,0 +1,19 @@ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +#ifndef DIALOG_H +#define DIALOG_H + +#include + +//! [0] +class Dialog : public QDialog +{ + Q_OBJECT + +public: + explicit Dialog(QWidget *parent = nullptr); +}; +//! [0] + +#endif diff --git a/tests/manual/examples/widgets/wiggly/main.cpp b/tests/manual/examples/widgets/wiggly/main.cpp new file mode 100644 index 0000000000..e7df19b6a0 --- /dev/null +++ b/tests/manual/examples/widgets/wiggly/main.cpp @@ -0,0 +1,16 @@ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +#include "dialog.h" + +#include + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + + Dialog dialog; + dialog.show(); + + return app.exec(); +} diff --git a/tests/manual/examples/widgets/wiggly/wiggly.pro b/tests/manual/examples/widgets/wiggly/wiggly.pro new file mode 100644 index 0000000000..0e39f18add --- /dev/null +++ b/tests/manual/examples/widgets/wiggly/wiggly.pro @@ -0,0 +1,11 @@ +QT += widgets + +HEADERS = wigglywidget.h \ + dialog.h +SOURCES = wigglywidget.cpp \ + dialog.cpp \ + main.cpp + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/widgets/widgets/wiggly +INSTALLS += target diff --git a/tests/manual/examples/widgets/wiggly/wigglywidget.cpp b/tests/manual/examples/widgets/wiggly/wigglywidget.cpp new file mode 100644 index 0000000000..3e686a67cf --- /dev/null +++ b/tests/manual/examples/widgets/wiggly/wigglywidget.cpp @@ -0,0 +1,65 @@ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +#include "wigglywidget.h" + +#include +#include +#include + +//! [0] +WigglyWidget::WigglyWidget(QWidget *parent) + : QWidget(parent), step(0) +{ + setBackgroundRole(QPalette::Midlight); + setAutoFillBackground(true); + + QFont newFont = font(); + newFont.setPointSize(newFont.pointSize() + 20); + setFont(newFont); + + timer.start(60, this); +} +//! [0] + +//! [1] +void WigglyWidget::paintEvent(QPaintEvent * /* event */) +//! [1] //! [2] +{ + static constexpr int sineTable[16] = { + 0, 38, 71, 92, 100, 92, 71, 38, 0, -38, -71, -92, -100, -92, -71, -38 + }; + + QFontMetrics metrics(font()); + int x = (width() - metrics.horizontalAdvance(text)) / 2; + int y = (height() + metrics.ascent() - metrics.descent()) / 2; + QColor color; +//! [2] + +//! [3] + QPainter painter(this); +//! [3] //! [4] + int offset = 0; + for (char32_t codePoint : text.toUcs4()) { + int index = (step + offset++) % 16; + color.setHsv((15 - index) * 16, 255, 191); + painter.setPen(color); + QString symbol = QString::fromUcs4(&codePoint, 1); + painter.drawText(x, y - ((sineTable[index] * metrics.height()) / 400), symbol); + x += metrics.horizontalAdvance(symbol); + } +} +//! [4] + +//! [5] +void WigglyWidget::timerEvent(QTimerEvent *event) +//! [5] //! [6] +{ + if (event->timerId() == timer.timerId()) { + ++step; + update(); + } else { + QWidget::timerEvent(event); + } +//! [6] +} diff --git a/tests/manual/examples/widgets/wiggly/wigglywidget.h b/tests/manual/examples/widgets/wiggly/wigglywidget.h new file mode 100644 index 0000000000..1c21e52234 --- /dev/null +++ b/tests/manual/examples/widgets/wiggly/wigglywidget.h @@ -0,0 +1,32 @@ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +#ifndef WIGGLYWIDGET_H +#define WIGGLYWIDGET_H + +#include +#include + +//! [0] +class WigglyWidget : public QWidget +{ + Q_OBJECT + +public: + WigglyWidget(QWidget *parent = nullptr); + +public slots: + void setText(const QString &newText) { text = newText; } + +protected: + void paintEvent(QPaintEvent *event) override; + void timerEvent(QTimerEvent *event) override; + +private: + QBasicTimer timer; + QString text; + int step; +}; +//! [0] + +#endif