File indexing completed on 2024-10-13 06:39:59

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2007 Urs Wolfer <uwolfer at kde.org>
0004     SPDX-FileCopyrightText: 2008 Friedrich W. H. Kossebau <kossebau@kde.org>
0005     SPDX-FileCopyrightText: 2010 Teo Mrnjavac <teo@kde.org>
0006 
0007     Parts of this class have been take from the KAboutApplication class, which was
0008     SPDX-FileCopyrightText: 2000 Waldo Bastian <bastian@kde.org>
0009     SPDX-FileCopyrightText: 2000 Espen Sand <espen@kde.org>
0010 
0011     SPDX-License-Identifier: LGPL-2.0-only
0012 */
0013 
0014 #include "klicensedialog_p.h"
0015 
0016 // KF
0017 #include <KAboutData>
0018 #include <KLocalizedString>
0019 // Qt
0020 #include <QDialogButtonBox>
0021 #include <QFontDatabase>
0022 #include <QScrollBar>
0023 #include <QStyle>
0024 #include <QTextBrowser>
0025 #include <QVBoxLayout>
0026 
0027 KLicenseDialog::KLicenseDialog(const KAboutLicense &license, QWidget *parent)
0028     : QDialog(parent)
0029 {
0030     setAttribute(Qt::WA_DeleteOnClose);
0031     QVBoxLayout *layout = new QVBoxLayout(this);
0032 
0033     setWindowTitle(i18nc("@title:window", "License Agreement"));
0034 
0035     const QFont font = QFontDatabase::systemFont(QFontDatabase::FixedFont);
0036 
0037     const QString licenseText = license.text();
0038 
0039     QTextBrowser *licenseBrowser = new QTextBrowser(this);
0040     licenseBrowser->setFont(font);
0041     licenseBrowser->setLineWrapMode(QTextEdit::NoWrap);
0042     licenseBrowser->setText(licenseText);
0043     layout->addWidget(licenseBrowser);
0044 
0045     QDialogButtonBox *buttonBox = new QDialogButtonBox(this);
0046     buttonBox->setStandardButtons(QDialogButtonBox::Close);
0047     connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
0048     connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
0049     layout->addWidget(buttonBox);
0050 
0051     // try to set up the dialog such that the full width of the
0052     // document is visible without horizontal scroll-bars being required
0053     auto *style = this->style();
0054     const int leftMarginHint = style->pixelMetric(QStyle::PM_LayoutLeftMargin);
0055     const int rightMarginHint = style->pixelMetric(QStyle::PM_LayoutRightMargin);
0056     const qreal idealWidth = licenseBrowser->document()->idealWidth() + leftMarginHint + rightMarginHint + licenseBrowser->verticalScrollBar()->width() * 2;
0057 
0058     // try to allow enough height for a reasonable number of lines to be shown
0059     QFontMetrics metrics(font);
0060     const int idealHeight = metrics.height() * 30;
0061 
0062     resize(sizeHint().expandedTo(QSize(qRound(idealWidth), idealHeight)));
0063 }
0064 
0065 KLicenseDialog::~KLicenseDialog() = default;
0066 
0067 #include "moc_klicensedialog_p.cpp"