diff options
author | Ivan Solovev <ivan.solovev@qt.io> | 2021-06-08 18:30:15 +0200 |
---|---|---|
committer | Ivan Solovev <ivan.solovev@qt.io> | 2021-06-11 09:22:58 +0200 |
commit | 6d4d0ca9ea79ae64ab1bea33e9b99a76430ffc3c (patch) | |
tree | d48a0a397fff140ebbd3e7575cb39cc59982f184 /examples | |
parent | 661da19ebb43498c56a1185b933cdd64d79d9645 (diff) |
AnnotatedUrl: refactor UI
This patch refactors the UI layout, so that it handles screen rotation
correctly. The *.ui files are removed. The layout is created directly
from the source code instead.
The application now has two different labels to show the icon.
Depending on the actual screen orientation, one of them shows the icon,
and the other is hidden.
The icon is properly scaled before it's shown, so that the layout does
not expand beyond the visible screen rectangle.
Also this patch introduces a code that handles screen orientation
change, and the screen change (in case the application is dragged to
the other monitor). The latter is not really applicable to mobile
devices though.
Task-number: QTBUG-94033
Pick-to: 6.2
Change-Id: I0d7d781bd2b97e16f7fba530eeffd407c7eaff61
Reviewed-by: Alex Blasche <alexander.blasche@qt.io>
Diffstat (limited to 'examples')
-rw-r--r-- | examples/nfc/annotatedurl/CMakeLists.txt | 2 | ||||
-rw-r--r-- | examples/nfc/annotatedurl/annotatedurl.pro | 2 | ||||
-rw-r--r-- | examples/nfc/annotatedurl/mainwindow.cpp | 94 | ||||
-rw-r--r-- | examples/nfc/annotatedurl/mainwindow.h | 31 | ||||
-rw-r--r-- | examples/nfc/annotatedurl/mainwindow.ui | 61 |
5 files changed, 98 insertions, 92 deletions
diff --git a/examples/nfc/annotatedurl/CMakeLists.txt b/examples/nfc/annotatedurl/CMakeLists.txt index 7e3e54f2..82d24318 100644 --- a/examples/nfc/annotatedurl/CMakeLists.txt +++ b/examples/nfc/annotatedurl/CMakeLists.txt @@ -22,7 +22,7 @@ qt_add_executable(annotatedurl MANUAL_FINALIZATION annotatedurl.cpp annotatedurl.h main.cpp - mainwindow.cpp mainwindow.h mainwindow.ui + mainwindow.cpp mainwindow.h ) set_target_properties(annotatedurl PROPERTIES WIN32_EXECUTABLE TRUE diff --git a/examples/nfc/annotatedurl/annotatedurl.pro b/examples/nfc/annotatedurl/annotatedurl.pro index c13ce0c6..8988c526 100644 --- a/examples/nfc/annotatedurl/annotatedurl.pro +++ b/examples/nfc/annotatedurl/annotatedurl.pro @@ -11,8 +11,6 @@ SOURCES += main.cpp \ HEADERS += mainwindow.h \ annotatedurl.h -FORMS += mainwindow.ui - android { ANDROID_PACKAGE_SOURCE_DIR = $$PWD/android diff --git a/examples/nfc/annotatedurl/mainwindow.cpp b/examples/nfc/annotatedurl/mainwindow.cpp index 03f4ee09..e9863d03 100644 --- a/examples/nfc/annotatedurl/mainwindow.cpp +++ b/examples/nfc/annotatedurl/mainwindow.cpp @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2017 The Qt Company Ltd. +** Copyright (C) 2021 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtNfc module. @@ -49,40 +49,102 @@ ****************************************************************************/ #include "mainwindow.h" -#include "ui_mainwindow.h" -#include <QtGui/QDesktopServices> -#include <QtGui/QMouseEvent> -#include <QtCore/QUrl> +#include <QUrl> +#include <QDesktopServices> +#include <QLabel> +#include <QLayout> +#include <QScreen> MainWindow::MainWindow(QWidget *parent) -: QMainWindow(parent), ui(new Ui::MainWindow) + : QWidget(parent), + m_titleLabel(new QLabel(this)), + m_infoLabel(new QLabel(tr("Enable NFC"), this)), + m_uriLabel(new QLabel(this)), + m_landscapeIconLabel(new QLabel(this)), + m_portraitIconLabel(new QLabel(this)) { - ui->setupUi(this); + m_titleLabel->setAlignment(Qt::AlignCenter); + m_infoLabel->setAlignment(Qt::AlignCenter); + m_uriLabel->setAlignment(Qt::AlignCenter); + m_landscapeIconLabel->setAlignment(Qt::AlignCenter); + m_portraitIconLabel->setAlignment(Qt::AlignCenter); + + QFont f = m_titleLabel->font(); + f.setBold(true); + m_titleLabel->setFont(f); + + QVBoxLayout *verticalLayout = new QVBoxLayout; + verticalLayout->addWidget(m_titleLabel); + verticalLayout->addWidget(m_infoLabel); + verticalLayout->addWidget(m_portraitIconLabel); + verticalLayout->addWidget(m_uriLabel); + + QHBoxLayout *horizontalLayout = new QHBoxLayout; + horizontalLayout->addWidget(m_landscapeIconLabel); + horizontalLayout->addLayout(verticalLayout); + horizontalLayout->setSpacing(0); + + setLayout(horizontalLayout); + + handleScreenChange(); } MainWindow::~MainWindow() { - delete ui; } void MainWindow::displayAnnotatedUrl(const QUrl &url, const QString &title, const QPixmap &pixmap) { - ui->m_help->setHidden(true); + m_infoLabel->setHidden(true); - ui->m_url->setText(url.toString()); - ui->m_title->setText(title); - ui->m_image->setPixmap(pixmap); + m_uriLabel->setText(url.toString()); + m_titleLabel->setText(title); + + m_pixmap = pixmap; + updateWidgetLayout(m_screen->orientation()); } void MainWindow::nfcStateChanged(bool enabled) { const QString text = enabled ? "Touch a tag" : "Enable NFC"; - ui->m_help->setText(text); + m_infoLabel->setText(text); +} + +void MainWindow::mouseReleaseEvent(QMouseEvent * /*event*/) +{ + QDesktopServices::openUrl(QUrl(m_uriLabel->text())); } -void MainWindow::mouseReleaseEvent(QMouseEvent *event) +void MainWindow::moveEvent(QMoveEvent * /*event*/) { - if (ui->centralWidget->rect().contains(event->pos())) - QDesktopServices::openUrl(QUrl(ui->m_url->text())); + if (screen() != m_screen) + handleScreenChange(); +} + +void MainWindow::updateWidgetLayout(Qt::ScreenOrientation orientation) +{ + m_landscapeIconLabel->setVisible(false); + m_portraitIconLabel->setVisible(false); + if (!m_pixmap.isNull()) { + const bool landscapeMode = (orientation == Qt::LandscapeOrientation) + || (orientation == Qt::InvertedLandscapeOrientation); + QLabel *imageLabel = landscapeMode ? m_landscapeIconLabel : m_portraitIconLabel; + imageLabel->setVisible(true); + if (m_pixmap.width() > imageLabel->width() || m_pixmap.height() > imageLabel->height()) + imageLabel->setPixmap(m_pixmap.scaled(imageLabel->size(), Qt::KeepAspectRatio)); + else + imageLabel->setPixmap(m_pixmap); + } +} + +void MainWindow::handleScreenChange() +{ + if (m_screen) + disconnect(m_screen, &QScreen::orientationChanged, this, &MainWindow::updateWidgetLayout); + + m_screen = screen(); + connect(m_screen, &QScreen::orientationChanged, this, &MainWindow::updateWidgetLayout); + + updateWidgetLayout(m_screen->orientation()); } diff --git a/examples/nfc/annotatedurl/mainwindow.h b/examples/nfc/annotatedurl/mainwindow.h index 28962977..1770c39f 100644 --- a/examples/nfc/annotatedurl/mainwindow.h +++ b/examples/nfc/annotatedurl/mainwindow.h @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2017 The Qt Company Ltd. +** Copyright (C) 2021 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtNfc module. @@ -51,21 +51,16 @@ #ifndef MAINWINDOW_H #define MAINWINDOW_H -#include <QtWidgets/QMainWindow> +#include <QWidget> +#include <QPixmap> QT_FORWARD_DECLARE_CLASS(QMouseEvent) QT_FORWARD_DECLARE_CLASS(QUrl) -QT_FORWARD_DECLARE_CLASS(QPixmap) QT_FORWARD_DECLARE_CLASS(QString) -QT_FORWARD_DECLARE_CLASS(QWidget) +QT_FORWARD_DECLARE_CLASS(QLabel) +QT_FORWARD_DECLARE_CLASS(QScreen) -QT_BEGIN_NAMESPACE -namespace Ui { - class MainWindow; -} -QT_END_NAMESPACE - -class MainWindow : public QMainWindow +class MainWindow : public QWidget { Q_OBJECT @@ -79,9 +74,21 @@ public slots: protected: void mouseReleaseEvent(QMouseEvent *event) override; + void moveEvent(QMoveEvent *event) override; + +private slots: + void updateWidgetLayout(Qt::ScreenOrientation orientation); private: - Ui::MainWindow *ui; + void handleScreenChange(); + + QLabel *m_titleLabel; + QLabel *m_infoLabel; + QLabel *m_uriLabel; + QLabel *m_landscapeIconLabel; + QLabel *m_portraitIconLabel; + QScreen *m_screen = nullptr; + QPixmap m_pixmap; }; #endif // MAINWINDOW_H diff --git a/examples/nfc/annotatedurl/mainwindow.ui b/examples/nfc/annotatedurl/mainwindow.ui deleted file mode 100644 index 26e3b302..00000000 --- a/examples/nfc/annotatedurl/mainwindow.ui +++ /dev/null @@ -1,61 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<ui version="4.0"> - <class>MainWindow</class> - <widget class="QMainWindow" name="MainWindow"> - <property name="geometry"> - <rect> - <x>0</x> - <y>0</y> - <width>590</width> - <height>420</height> - </rect> - </property> - <property name="windowTitle"> - <string>MainWindow</string> - </property> - <widget class="QWidget" name="centralWidget"> - <layout class="QGridLayout" name="gridLayout" rowstretch="1,0,1" columnstretch="0,0,0"> - <item row="0" column="1" colspan="2"> - <widget class="QLabel" name="m_title"> - <property name="font"> - <font> - <weight>75</weight> - <bold>true</bold> - </font> - </property> - <property name="text"> - <string/> - </property> - </widget> - </item> - <item row="0" column="0" rowspan="3"> - <widget class="QLabel" name="m_image"> - <property name="text"> - <string/> - </property> - </widget> - </item> - <item row="1" column="1"> - <widget class="QLabel" name="m_help"> - <property name="text"> - <string>Touch a tag</string> - </property> - <property name="alignment"> - <set>Qt::AlignCenter</set> - </property> - </widget> - </item> - <item row="2" column="1" colspan="2"> - <widget class="QLabel" name="m_url"> - <property name="text"> - <string/> - </property> - </widget> - </item> - </layout> - </widget> - </widget> - <layoutdefault spacing="6" margin="11"/> - <resources/> - <connections/> -</ui> |