1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
|
// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#include "mainwindow.h"
#include <QUrl>
#include <QDesktopServices>
#include <QScreen>
#include <QLabel>
#include <QLayout>
MainWindow::MainWindow(QWidget *parent)
: 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))
{
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()
{
}
void MainWindow::displayAnnotatedUrl(const QUrl &url, const QString &title, const QPixmap &pixmap)
{
m_infoLabel->setHidden(true);
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";
m_infoLabel->setText(text);
}
void MainWindow::showTagError(const QString &message)
{
m_uriLabel->clear();
m_titleLabel->clear();
m_pixmap = QPixmap();
m_landscapeIconLabel->setVisible(false);
m_portraitIconLabel->setVisible(false);
m_infoLabel->setHidden(false);
m_infoLabel->setText(message);
}
void MainWindow::mouseReleaseEvent(QMouseEvent * /*event*/)
{
QDesktopServices::openUrl(QUrl(m_uriLabel->text()));
}
void MainWindow::moveEvent(QMoveEvent * /*event*/)
{
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());
}
|