Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
aboutdialog.cpp
Go to the documentation of this file.
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
20
23{
25 setFrameStyle(QFrame::NoFrame);
26 QPalette p;
27 p.setColor(QPalette::Base, p.color(QPalette::Window));
28 setPalette(p);
29}
30
31void AboutLabel::setText(const QString &text, const QByteArray &resources)
32{
34 QDataStream in(resources);
35 in >> m_resourceMap;
36
37 QTextBrowser::setText(text);
38}
39
41{
43 QTextDocument *doc = document();
44 doc->adjustSize();
45 return QSize(int(doc->size().width()), int(doc->size().height()));
46}
47
48QVariant AboutLabel::loadResource(int type, const QUrl &name)
49{
51 if (type == 2 || type == 3) {
52 if (m_resourceMap.contains(name.toString())) {
53 return m_resourceMap.value(name.toString());
54 }
55 }
56 return QVariant();
57}
58
59void AboutLabel::doSetSource(const QUrl &url, QTextDocument::ResourceType type)
60{
62 Q_UNUSED(type);
63 if (url.isValid() && (!HelpViewer::isLocalUrl(url)
64 || !HelpViewer::canOpenPage(url.path()))) {
65 if (!QDesktopServices::openUrl(url)) {
66 QMessageBox::warning(this, tr("Warning"),
67 tr("Unable to launch external application."), QMessageBox::Close);
68 }
69 }
70}
71
72AboutDialog::AboutDialog(QWidget *parent)
73 : QDialog(parent, Qt::MSWindowsFixedSizeDialogHint |
74 Qt::WindowTitleHint|Qt::WindowSystemMenuHint)
75{
77 m_pixmapLabel = nullptr;
78 m_aboutLabel = new AboutLabel();
79
80 m_closeButton = new QPushButton();
81 m_closeButton->setText(tr("&Close"));
82 connect(m_closeButton, &QAbstractButton::clicked, this, &QWidget::close);
83
84 m_layout = new QGridLayout(this);
85 m_layout->addWidget(m_aboutLabel, 1, 0, 1, -1);
86 m_layout->addItem(new QSpacerItem(20, 10, QSizePolicy::Minimum,
87 QSizePolicy::Fixed), 2, 1, 1, 1);
88 m_layout->addItem(new QSpacerItem(20, 20, QSizePolicy::Expanding), 3, 0, 1, 1);
89 m_layout->addWidget(m_closeButton, 3, 1, 1, 1);
90 m_layout->addItem(new QSpacerItem(20, 20, QSizePolicy::Expanding), 3, 2, 1, 1);
91}
92
93void AboutDialog::setText(const QString &text, const QByteArray &resources)
94{
96 m_aboutLabel->setText(text, resources);
97 updateSize();
98}
99
100void AboutDialog::setPixmap(const QPixmap &pixmap)
101{
103 if (!m_pixmapLabel) {
104 m_pixmapLabel = new QLabel();
105 m_layout->addWidget(m_pixmapLabel, 0, 0, 1, -1, Qt::AlignCenter);
106 }
107 m_pixmapLabel->setPixmap(pixmap);
108 updateSize();
109}
110
112{
114 return m_aboutLabel->documentTitle();
115}
116
117void AboutDialog::updateSize()
118{
120 auto screen = QGuiApplication::screenAt(QCursor::pos());
121 if (!screen)
122 screen = QGuiApplication::primaryScreen();
123 const QSize screenSize = screen->availableSize();
124 int limit = qMin(screenSize.width()/2, 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("QWorkspaceTitleBar"));
137 int windowTitleWidth = qMin(fm.horizontalAdvance(windowTitle()) + 50, limit);
138 if (windowTitleWidth > width)
139 width = windowTitleWidth;
140
141 layout()->activate();
142 int height = (layout()->hasHeightForWidth())
143 ? layout()->totalHeightForWidth(width)
144 : layout()->totalMinimumSize().height();
145 setFixedSize(width, height);
146 QCoreApplication::removePostedEvents(this, QEvent::LayoutRequest);
147}
148
149QT_END_NAMESPACE
QString documentTitle() const
void setText(const QString &text, const QByteArray &resources)
void setPixmap(const QPixmap &pixmap)
QSize minimumSizeHint() const override
QVariant loadResource(int type, const QUrl &name) override
This function is called when the document is loaded and for each image in the document.
void doSetSource(const QUrl &name, QTextDocument::ResourceType type) override
Attempts to load the document at the given url with the specified type.
void setText(const QString &text, const QByteArray &resources)
QObject * parent
Definition qobject.h:73
The QWidget class is the base class of all user interface objects.
Definition qwidget.h:99
Combined button and popup list for selecting options.
#define TRACE_OBJ
Definition tracer.h:34