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 
44 QT_BEGIN_NAMESPACE
45 
AboutLabel(QWidget * parent)46 AboutLabel::AboutLabel(QWidget *parent)
47     : QTextBrowser(parent)
48 {
49     TRACE_OBJ
50     setFrameStyle(QFrame::NoFrame);
51     QPalette p;
52     p.setColor(QPalette::Base, p.color(QPalette::Window));
53     setPalette(p);
54 }
55 
setText(const QString & text,const QByteArray & resources)56 void 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 
minimumSizeHint() const65 QSize 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 
loadResource(int type,const QUrl & name)73 QVariant AboutLabel::loadResource(int type, const QUrl &name)
74 {
75     TRACE_OBJ
76     if (type == 2 || type == 3) {
77         if (m_resourceMap.contains(name.toString())) {
78             return m_resourceMap.value(name.toString());
79         }
80     }
81     return QVariant();
82 }
83 
setSource(const QUrl & url)84 void AboutLabel::setSource(const QUrl &url)
85 {
86     TRACE_OBJ
87     if (url.isValid() && (!HelpViewer::isLocalUrl(url)
88     || !HelpViewer::canOpenPage(url.path()))) {
89         if (!QDesktopServices::openUrl(url)) {
90             QMessageBox::warning(this, tr("Warning"),
91                 tr("Unable to launch external application."), tr("OK"));
92         }
93     }
94 }
95 
AboutDialog(QWidget * parent)96 AboutDialog::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("&Close"));
106     connect(m_closeButton, &QAbstractButton::clicked, this, &QWidget::close);
107 
108     m_layout = new QGridLayout(this);
109     m_layout->addWidget(m_aboutLabel, 1, 0, 1, -1);
110     m_layout->addItem(new QSpacerItem(20, 10, QSizePolicy::Minimum,
111         QSizePolicy::Fixed), 2, 1, 1, 1);
112     m_layout->addItem(new QSpacerItem(20, 20, QSizePolicy::Expanding), 3, 0, 1, 1);
113     m_layout->addWidget(m_closeButton, 3, 1, 1, 1);
114     m_layout->addItem(new QSpacerItem(20, 20, QSizePolicy::Expanding), 3, 2, 1, 1);
115 }
116 
setText(const QString & text,const QByteArray & resources)117 void AboutDialog::setText(const QString &text, const QByteArray &resources)
118 {
119     TRACE_OBJ
120     m_aboutLabel->setText(text, resources);
121     updateSize();
122 }
123 
setPixmap(const QPixmap & pixmap)124 void AboutDialog::setPixmap(const QPixmap &pixmap)
125 {
126     TRACE_OBJ
127     if (!m_pixmapLabel) {
128         m_pixmapLabel = new QLabel();
129         m_layout->addWidget(m_pixmapLabel, 0, 0, 1, -1, Qt::AlignCenter);
130     }
131     m_pixmapLabel->setPixmap(pixmap);
132     updateSize();
133 }
134 
documentTitle() const135 QString AboutDialog::documentTitle() const
136 {
137     TRACE_OBJ
138     return m_aboutLabel->documentTitle();
139 }
140 
updateSize()141 void AboutDialog::updateSize()
142 {
143     TRACE_OBJ
144     auto screen = QGuiApplication::screenAt(QCursor::pos());
145     if (!screen)
146         screen = QGuiApplication::primaryScreen();
147     const QSize screenSize = screen->availableSize();
148     int limit = qMin(screenSize.width()/2, 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("QWorkspaceTitleBar"));
161     int windowTitleWidth = qMin(fm.horizontalAdvance(windowTitle()) + 50, limit);
162     if (windowTitleWidth > width)
163         width = windowTitleWidth;
164 
165     layout()->activate();
166     int height = (layout()->hasHeightForWidth())
167         ? layout()->totalHeightForWidth(width)
168         : layout()->totalMinimumSize().height();
169     setFixedSize(width, height);
170     QCoreApplication::removePostedEvents(this, QEvent::LayoutRequest);
171 }
172 
173 QT_END_NAMESPACE
174