1 | // Copyright (C) 2016 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 |
3 | |
4 | #include "aboutdialog.h" |
5 | |
6 | #include "helpviewer.h" |
7 | #include "tracer.h" |
8 | |
9 | #include <QtCore/QBuffer> |
10 | |
11 | #include <QtWidgets/QLabel> |
12 | #include <QtWidgets/QPushButton> |
13 | #include <QtWidgets/QLayout> |
14 | #include <QtWidgets/QApplication> |
15 | #include <QtWidgets/QMessageBox> |
16 | #include <QtGui/QDesktopServices> |
17 | #include <QtGui/QScreen> |
18 | |
19 | QT_BEGIN_NAMESPACE |
20 | |
21 | AboutLabel::AboutLabel(QWidget *parent) |
22 | : QTextBrowser(parent) |
23 | { |
24 | TRACE_OBJ |
25 | setFrameStyle(QFrame::NoFrame); |
26 | QPalette p; |
27 | p.setColor(acr: QPalette::Base, acolor: p.color(cr: QPalette::Window)); |
28 | setPalette(p); |
29 | } |
30 | |
31 | void AboutLabel::setText(const QString &text, const QByteArray &resources) |
32 | { |
33 | TRACE_OBJ |
34 | QDataStream in(resources); |
35 | in >> m_resourceMap; |
36 | |
37 | QTextBrowser::setText(text); |
38 | } |
39 | |
40 | QSize AboutLabel::minimumSizeHint() const |
41 | { |
42 | TRACE_OBJ |
43 | QTextDocument *doc = document(); |
44 | doc->adjustSize(); |
45 | return QSize(int(doc->size().width()), int(doc->size().height())); |
46 | } |
47 | |
48 | QVariant AboutLabel::loadResource(int type, const QUrl &name) |
49 | { |
50 | TRACE_OBJ |
51 | if (type == 2 || type == 3) { |
52 | if (m_resourceMap.contains(key: name.toString())) { |
53 | return m_resourceMap.value(key: name.toString()); |
54 | } |
55 | } |
56 | return QVariant(); |
57 | } |
58 | |
59 | void AboutLabel::doSetSource(const QUrl &url, QTextDocument::ResourceType type) |
60 | { |
61 | TRACE_OBJ |
62 | Q_UNUSED(type); |
63 | if (url.isValid() && (!HelpViewer::isLocalUrl(url) |
64 | || !HelpViewer::canOpenPage(url: url.path()))) { |
65 | if (!QDesktopServices::openUrl(url)) { |
66 | QMessageBox::warning(parent: this, title: tr(s: "Warning" ), |
67 | text: tr(s: "Unable to launch external application." ), buttons: QMessageBox::Close); |
68 | } |
69 | } |
70 | } |
71 | |
72 | AboutDialog::AboutDialog(QWidget *parent) |
73 | : QDialog(parent, Qt::MSWindowsFixedSizeDialogHint | |
74 | Qt::WindowTitleHint|Qt::WindowSystemMenuHint) |
75 | { |
76 | TRACE_OBJ |
77 | m_pixmapLabel = nullptr; |
78 | m_aboutLabel = new AboutLabel(); |
79 | |
80 | m_closeButton = new QPushButton(); |
81 | m_closeButton->setText(tr(s: "&Close" )); |
82 | connect(sender: m_closeButton, signal: &QAbstractButton::clicked, context: this, slot: &QWidget::close); |
83 | |
84 | m_layout = new QGridLayout(this); |
85 | m_layout->addWidget(m_aboutLabel, row: 1, column: 0, rowSpan: 1, columnSpan: -1); |
86 | m_layout->addItem(item: new QSpacerItem(20, 10, QSizePolicy::Minimum, |
87 | QSizePolicy::Fixed), row: 2, column: 1, rowSpan: 1, columnSpan: 1); |
88 | m_layout->addItem(item: new QSpacerItem(20, 20, QSizePolicy::Expanding), row: 3, column: 0, rowSpan: 1, columnSpan: 1); |
89 | m_layout->addWidget(m_closeButton, row: 3, column: 1, rowSpan: 1, columnSpan: 1); |
90 | m_layout->addItem(item: new QSpacerItem(20, 20, QSizePolicy::Expanding), row: 3, column: 2, rowSpan: 1, columnSpan: 1); |
91 | } |
92 | |
93 | void AboutDialog::setText(const QString &text, const QByteArray &resources) |
94 | { |
95 | TRACE_OBJ |
96 | m_aboutLabel->setText(text, resources); |
97 | updateSize(); |
98 | } |
99 | |
100 | void AboutDialog::setPixmap(const QPixmap &pixmap) |
101 | { |
102 | TRACE_OBJ |
103 | if (!m_pixmapLabel) { |
104 | m_pixmapLabel = new QLabel(); |
105 | m_layout->addWidget(m_pixmapLabel, row: 0, column: 0, rowSpan: 1, columnSpan: -1, Qt::AlignCenter); |
106 | } |
107 | m_pixmapLabel->setPixmap(pixmap); |
108 | updateSize(); |
109 | } |
110 | |
111 | QString AboutDialog::documentTitle() const |
112 | { |
113 | TRACE_OBJ |
114 | return m_aboutLabel->documentTitle(); |
115 | } |
116 | |
117 | void AboutDialog::updateSize() |
118 | { |
119 | TRACE_OBJ |
120 | auto screen = QGuiApplication::screenAt(point: QCursor::pos()); |
121 | if (!screen) |
122 | screen = QGuiApplication::primaryScreen(); |
123 | const QSize screenSize = screen->availableSize(); |
124 | int limit = qMin(a: screenSize.width()/2, b: 500); |
125 | |
126 | #ifdef Q_OS_MAC |
127 | limit = qMin(screenSize.width()/2, 420); |
128 | #endif |
129 | |
130 | layout()->activate(); |
131 | int width = layout()->totalMinimumSize().width(); |
132 | |
133 | if (width > limit) |
134 | width = limit; |
135 | |
136 | QFontMetrics fm(qApp->font(className: "QWorkspaceTitleBar" )); |
137 | int windowTitleWidth = qMin(a: fm.horizontalAdvance(windowTitle()) + 50, b: limit); |
138 | if (windowTitleWidth > width) |
139 | width = windowTitleWidth; |
140 | |
141 | layout()->activate(); |
142 | int height = (layout()->hasHeightForWidth()) |
143 | ? layout()->totalHeightForWidth(w: width) |
144 | : layout()->totalMinimumSize().height(); |
145 | setFixedSize(w: width, h: height); |
146 | QCoreApplication::removePostedEvents(receiver: this, eventType: QEvent::LayoutRequest); |
147 | } |
148 | |
149 | QT_END_NAMESPACE |
150 | |