PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/examples/nfc/annotatedurl/mainwindow.cpp

https://gitlab.com/f3822/qtconnectivity
C++ | 162 lines | 92 code | 21 blank | 49 comment | 9 complexity | c3c9cf2d022b0ed5fdf87af64dc1deee MD5 | raw file
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2021 The Qt Company Ltd.
  4. ** Contact: https://www.qt.io/licensing/
  5. **
  6. ** This file is part of the QtNfc module.
  7. **
  8. ** $QT_BEGIN_LICENSE:BSD$
  9. ** Commercial License Usage
  10. ** Licensees holding valid commercial Qt licenses may use this file in
  11. ** accordance with the commercial license agreement provided with the
  12. ** Software or, alternatively, in accordance with the terms contained in
  13. ** a written agreement between you and The Qt Company. For licensing terms
  14. ** and conditions see https://www.qt.io/terms-conditions. For further
  15. ** information use the contact form at https://www.qt.io/contact-us.
  16. **
  17. ** BSD License Usage
  18. ** Alternatively, you may use this file under the terms of the BSD license
  19. ** as follows:
  20. **
  21. ** "Redistribution and use in source and binary forms, with or without
  22. ** modification, are permitted provided that the following conditions are
  23. ** met:
  24. ** * Redistributions of source code must retain the above copyright
  25. ** notice, this list of conditions and the following disclaimer.
  26. ** * Redistributions in binary form must reproduce the above copyright
  27. ** notice, this list of conditions and the following disclaimer in
  28. ** the documentation and/or other materials provided with the
  29. ** distribution.
  30. ** * Neither the name of The Qt Company Ltd nor the names of its
  31. ** contributors may be used to endorse or promote products derived
  32. ** from this software without specific prior written permission.
  33. **
  34. **
  35. ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  36. ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  37. ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  38. ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  39. ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  42. ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  43. ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  44. ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  45. ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
  46. **
  47. ** $QT_END_LICENSE$
  48. **
  49. ****************************************************************************/
  50. #include "mainwindow.h"
  51. #include <QUrl>
  52. #include <QDesktopServices>
  53. #include <QLabel>
  54. #include <QLayout>
  55. #include <QScreen>
  56. MainWindow::MainWindow(QWidget *parent)
  57. : QWidget(parent),
  58. m_titleLabel(new QLabel(this)),
  59. m_infoLabel(new QLabel(tr("Enable NFC"), this)),
  60. m_uriLabel(new QLabel(this)),
  61. m_landscapeIconLabel(new QLabel(this)),
  62. m_portraitIconLabel(new QLabel(this))
  63. {
  64. m_titleLabel->setAlignment(Qt::AlignCenter);
  65. m_infoLabel->setAlignment(Qt::AlignCenter);
  66. m_uriLabel->setAlignment(Qt::AlignCenter);
  67. m_landscapeIconLabel->setAlignment(Qt::AlignCenter);
  68. m_portraitIconLabel->setAlignment(Qt::AlignCenter);
  69. QFont f = m_titleLabel->font();
  70. f.setBold(true);
  71. m_titleLabel->setFont(f);
  72. QVBoxLayout *verticalLayout = new QVBoxLayout;
  73. verticalLayout->addWidget(m_titleLabel);
  74. verticalLayout->addWidget(m_infoLabel);
  75. verticalLayout->addWidget(m_portraitIconLabel);
  76. verticalLayout->addWidget(m_uriLabel);
  77. QHBoxLayout *horizontalLayout = new QHBoxLayout;
  78. horizontalLayout->addWidget(m_landscapeIconLabel);
  79. horizontalLayout->addLayout(verticalLayout);
  80. horizontalLayout->setSpacing(0);
  81. setLayout(horizontalLayout);
  82. handleScreenChange();
  83. }
  84. MainWindow::~MainWindow()
  85. {
  86. }
  87. void MainWindow::displayAnnotatedUrl(const QUrl &url, const QString &title, const QPixmap &pixmap)
  88. {
  89. m_infoLabel->setHidden(true);
  90. m_uriLabel->setText(url.toString());
  91. m_titleLabel->setText(title);
  92. m_pixmap = pixmap;
  93. updateWidgetLayout(m_screen->orientation());
  94. }
  95. void MainWindow::nfcStateChanged(bool enabled)
  96. {
  97. const QString text = enabled ? "Touch a tag" : "Enable NFC";
  98. m_infoLabel->setText(text);
  99. }
  100. void MainWindow::showTagError(const QString &message)
  101. {
  102. m_uriLabel->clear();
  103. m_titleLabel->clear();
  104. m_pixmap = QPixmap();
  105. m_landscapeIconLabel->setVisible(false);
  106. m_portraitIconLabel->setVisible(false);
  107. m_infoLabel->setHidden(false);
  108. m_infoLabel->setText(message);
  109. }
  110. void MainWindow::mouseReleaseEvent(QMouseEvent * /*event*/)
  111. {
  112. QDesktopServices::openUrl(QUrl(m_uriLabel->text()));
  113. }
  114. void MainWindow::moveEvent(QMoveEvent * /*event*/)
  115. {
  116. if (screen() != m_screen)
  117. handleScreenChange();
  118. }
  119. void MainWindow::updateWidgetLayout(Qt::ScreenOrientation orientation)
  120. {
  121. m_landscapeIconLabel->setVisible(false);
  122. m_portraitIconLabel->setVisible(false);
  123. if (!m_pixmap.isNull()) {
  124. const bool landscapeMode = (orientation == Qt::LandscapeOrientation)
  125. || (orientation == Qt::InvertedLandscapeOrientation);
  126. QLabel *imageLabel = landscapeMode ? m_landscapeIconLabel : m_portraitIconLabel;
  127. imageLabel->setVisible(true);
  128. if (m_pixmap.width() > imageLabel->width() || m_pixmap.height() > imageLabel->height())
  129. imageLabel->setPixmap(m_pixmap.scaled(imageLabel->size(), Qt::KeepAspectRatio));
  130. else
  131. imageLabel->setPixmap(m_pixmap);
  132. }
  133. }
  134. void MainWindow::handleScreenChange()
  135. {
  136. if (m_screen)
  137. disconnect(m_screen, &QScreen::orientationChanged, this, &MainWindow::updateWidgetLayout);
  138. m_screen = screen();
  139. connect(m_screen, &QScreen::orientationChanged, this, &MainWindow::updateWidgetLayout);
  140. updateWidgetLayout(m_screen->orientation());
  141. }