1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the Qt Assistant of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
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** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29#include "aboutdialog.h"
30
31#include "helpviewer.h"
32#include "tracer.h"
33
34#include <QtCore/QBuffer>
35
36#include <QtWidgets/QLabel>
37#include <QtWidgets/QPushButton>
38#include <QtWidgets/QLayout>
39#include <QtWidgets/QApplication>
40#include <QtWidgets/QMessageBox>
41#include <QtGui/QDesktopServices>
42#include <QtGui/QScreen>
43
44QT_BEGIN_NAMESPACE
45
46AboutLabel::AboutLabel(QWidget *parent)
47 : QTextBrowser(parent)
48{
49 TRACE_OBJ
50 setFrameStyle(QFrame::NoFrame);
51 QPalette p;
52 p.setColor(acr: QPalette::Base, acolor: p.color(cr: QPalette::Window));
53 setPalette(p);
54}
55
56void AboutLabel::setText(const QString &text, const QByteArray &resources)
57{
58 TRACE_OBJ
59 QDataStream in(resources);
60 in >> m_resourceMap;
61
62 QTextBrowser::setText(text);
63}
64
65QSize AboutLabel::minimumSizeHint() const
66{
67 TRACE_OBJ
68 QTextDocument *doc = document();
69 doc->adjustSize();
70 return QSize(int(doc->size().width()), int(doc->size().height()));
71}
72
73QVariant AboutLabel::loadResource(int type, const QUrl &name)
74{
75 TRACE_OBJ
76 if (type == 2 || type == 3) {
77 if (m_resourceMap.contains(akey: name.toString())) {
78 return m_resourceMap.value(akey: name.toString());
79 }
80 }
81 return QVariant();
82}
83
84void AboutLabel::setSource(const QUrl &url)
85{
86 TRACE_OBJ
87 if (url.isValid() && (!HelpViewer::isLocalUrl(url)
88 || !HelpViewer::canOpenPage(url: url.path()))) {
89 if (!QDesktopServices::openUrl(url)) {
90 QMessageBox::warning(parent: this, title: tr(s: "Warning"),
91 text: tr(s: "Unable to launch external application."), button0Text: tr(s: "OK"));
92 }
93 }
94}
95
96AboutDialog::AboutDialog(QWidget *parent)
97 : QDialog(parent, Qt::MSWindowsFixedSizeDialogHint |
98 Qt::WindowTitleHint|Qt::WindowSystemMenuHint)
99{
100 TRACE_OBJ
101 m_pixmapLabel = nullptr;
102 m_aboutLabel = new AboutLabel();
103
104 m_closeButton = new QPushButton();
105 m_closeButton->setText(tr(s: "&Close"));
106 connect(sender: m_closeButton, signal: &QAbstractButton::clicked, receiver: this, slot: &QWidget::close);
107
108 m_layout = new QGridLayout(this);
109 m_layout->addWidget(m_aboutLabel, row: 1, column: 0, rowSpan: 1, columnSpan: -1);
110 m_layout->addItem(item: new QSpacerItem(20, 10, QSizePolicy::Minimum,
111 QSizePolicy::Fixed), row: 2, column: 1, rowSpan: 1, columnSpan: 1);
112 m_layout->addItem(item: new QSpacerItem(20, 20, QSizePolicy::Expanding), row: 3, column: 0, rowSpan: 1, columnSpan: 1);
113 m_layout->addWidget(m_closeButton, row: 3, column: 1, rowSpan: 1, columnSpan: 1);
114 m_layout->addItem(item: new QSpacerItem(20, 20, QSizePolicy::Expanding), row: 3, column: 2, rowSpan: 1, columnSpan: 1);
115}
116
117void AboutDialog::setText(const QString &text, const QByteArray &resources)
118{
119 TRACE_OBJ
120 m_aboutLabel->setText(text, resources);
121 updateSize();
122}
123
124void AboutDialog::setPixmap(const QPixmap &pixmap)
125{
126 TRACE_OBJ
127 if (!m_pixmapLabel) {
128 m_pixmapLabel = new QLabel();
129 m_layout->addWidget(m_pixmapLabel, row: 0, column: 0, rowSpan: 1, columnSpan: -1, Qt::AlignCenter);
130 }
131 m_pixmapLabel->setPixmap(pixmap);
132 updateSize();
133}
134
135QString AboutDialog::documentTitle() const
136{
137 TRACE_OBJ
138 return m_aboutLabel->documentTitle();
139}
140
141void AboutDialog::updateSize()
142{
143 TRACE_OBJ
144 auto screen = QGuiApplication::screenAt(point: QCursor::pos());
145 if (!screen)
146 screen = QGuiApplication::primaryScreen();
147 const QSize screenSize = screen->availableSize();
148 int limit = qMin(a: screenSize.width()/2, b: 500);
149
150#ifdef Q_OS_MAC
151 limit = qMin(screenSize.width()/2, 420);
152#endif
153
154 layout()->activate();
155 int width = layout()->totalMinimumSize().width();
156
157 if (width > limit)
158 width = limit;
159
160 QFontMetrics fm(qApp->font(className: "QWorkspaceTitleBar"));
161 int windowTitleWidth = qMin(a: fm.horizontalAdvance(windowTitle()) + 50, b: limit);
162 if (windowTitleWidth > width)
163 width = windowTitleWidth;
164
165 layout()->activate();
166 int height = (layout()->hasHeightForWidth())
167 ? layout()->totalHeightForWidth(w: width)
168 : layout()->totalMinimumSize().height();
169 setFixedSize(w: width, h: height);
170 QCoreApplication::removePostedEvents(receiver: this, eventType: QEvent::LayoutRequest);
171}
172
173QT_END_NAMESPACE
174

source code of qttools/src/assistant/assistant/aboutdialog.cpp