| 1 | #include "./aboutdialog.h" |
| 2 | #include "../misc/dialogutils.h" |
| 3 | |
| 4 | #include "ui_aboutdialog.h" |
| 5 | |
| 6 | #include <c++utilities/application/argumentparser.h> |
| 7 | |
| 8 | #include <QApplication> |
| 9 | #include <QGraphicsPixmapItem> |
| 10 | #include <QMessageBox> |
| 11 | #include <QStringBuilder> |
| 12 | #include <QStyle> |
| 13 | |
| 14 | /*! |
| 15 | * \brief The QtUtilities namespace contains all utilities provided by the qtutilities library. |
| 16 | */ |
| 17 | namespace QtUtilities { |
| 18 | |
| 19 | /*! |
| 20 | * \class AboutDialog |
| 21 | * \brief The AboutDialog class provides a simple about dialog. |
| 22 | */ |
| 23 | |
| 24 | /*! |
| 25 | * \brief Constructs an about dialog with the provided information. |
| 26 | * \param parent Specifies the parent widget. |
| 27 | * \param applicationName Specifies the name of the application. If empty, |
| 28 | * QApplication::applicationName() will be used. |
| 29 | * \param creator Specifies the creator of the application. If empty, |
| 30 | * QApplication::organizationName() will be used. |
| 31 | * \param version Specifies the version of the application. If empty, |
| 32 | * QApplication::applicationVersion() will be used. |
| 33 | * \param dependencyVersions Specifies the dependency versions which were present at link-time. If empty, |
| 34 | * the application info from c++utilities is used. |
| 35 | * \param description Specifies a short description about the application. |
| 36 | * \param website Specifies the URL to the website of the application. |
| 37 | * \param image Specifies the application icon. If the image is null, the |
| 38 | * standard information icon will be used. |
| 39 | */ |
| 40 | AboutDialog::AboutDialog(QWidget *parent, const QString &applicationName, const QString &creator, const QString &version, |
| 41 | const std::vector<const char *> &dependencyVersions, const QString &website, const QString &description, const QImage &image) |
| 42 | : QDialog(parent) |
| 43 | , m_ui(new Ui::AboutDialog) |
| 44 | { |
| 45 | m_ui->setupUi(this); |
| 46 | makeHeading(m_ui->productNameLabel); |
| 47 | setStyleSheet(dialogStyleForPalette(palette())); |
| 48 | setWindowFlags((windowFlags()) & ~(Qt::WindowMinMaxButtonsHint | Qt::WindowContextHelpButtonHint | Qt::WindowFullscreenButtonHint)); |
| 49 | if (!applicationName.isEmpty()) { |
| 50 | m_ui->productNameLabel->setText(applicationName); |
| 51 | } else if (!QApplication::applicationDisplayName().isEmpty()) { |
| 52 | m_ui->productNameLabel->setText(QApplication::applicationDisplayName()); |
| 53 | } else { |
| 54 | m_ui->productNameLabel->setText(QApplication::applicationName()); |
| 55 | } |
| 56 | if (creator.startsWith(QLatin1Char('<'))) { |
| 57 | // assign rich text as-is |
| 58 | m_ui->creatorLabel->setText(creator); |
| 59 | } else { |
| 60 | // add "developed by " before creator name |
| 61 | auto setCreator = [this, creator = std::move(creator)] { |
| 62 | m_ui->creatorLabel->setText(tr("developed by %1").arg(creator.isEmpty() ? QApplication::organizationName() : creator)); |
| 63 | }; |
| 64 | setCreator(); |
| 65 | connect(this, &AboutDialog::retranslationRequired, this, std::move(setCreator)); |
| 66 | } |
| 67 | m_ui->versionLabel->setText(version.isEmpty() ? QApplication::applicationVersion() : version); |
| 68 | const auto &deps(dependencyVersions.size() ? dependencyVersions : CppUtilities::applicationInfo.dependencyVersions); |
| 69 | if (!deps.empty()) { |
| 70 | QStringList linkedAgainst; |
| 71 | linkedAgainst.reserve(static_cast<int>(deps.size())); |
| 72 | for (const auto &dependencyVersion : deps) { |
| 73 | linkedAgainst << QString::fromUtf8(dependencyVersion); |
| 74 | } |
| 75 | m_ui->versionLabel->setToolTip(QStringLiteral("<p>") % tr("Linked against:") % QStringLiteral("</p><ul><li>") |
| 76 | % linkedAgainst.join(QStringLiteral("</li><li>")) % QStringLiteral("</li></ul>")); |
| 77 | } |
| 78 | if (!website.isEmpty() || CppUtilities::applicationInfo.url) { |
| 79 | auto setWebsite = [this, website = std::move(website)] { |
| 80 | m_ui->websiteLabel->setText(tr("For updates and bug reports visit the <a href=\"%1\" " |
| 81 | "style=\"text-decoration: underline; color: palette(link);\">project " |
| 82 | "website</a>.") |
| 83 | .arg(!website.isEmpty() ? website : QString::fromUtf8(CppUtilities::applicationInfo.url))); |
| 84 | }; |
| 85 | setWebsite(); |
| 86 | connect(this, &AboutDialog::retranslationRequired, this, std::move(setWebsite)); |
| 87 | } else { |
| 88 | m_ui->websiteLabel->hide(); |
| 89 | } |
| 90 | m_ui->descLabel->setText(description.isEmpty() && CppUtilities::applicationInfo.description |
| 91 | ? QString::fromUtf8(CppUtilities::applicationInfo.description) |
| 92 | : description); |
| 93 | m_iconScene = new QGraphicsScene(this); |
| 94 | auto *item = image.isNull() |
| 95 | ? new QGraphicsPixmapItem(QApplication::style()->standardIcon(QStyle::SP_MessageBoxInformation, nullptr, this).pixmap(128)) |
| 96 | : new QGraphicsPixmapItem(QPixmap::fromImage(image)); |
| 97 | m_iconScene->addItem(item); |
| 98 | m_ui->graphicsView->setScene(m_iconScene); |
| 99 | auto setQtVersion = [this] { m_ui->qtVersionLabel->setText(tr("Using <a href=\"qtversion\">Qt %1</a>").arg(QString::fromUtf8(qVersion()))); }; |
| 100 | setQtVersion(); |
| 101 | connect(this, &AboutDialog::retranslationRequired, this, std::move(setQtVersion)); |
| 102 | connect(m_ui->qtVersionLabel, &QLabel::linkActivated, this, &AboutDialog::linkActivated); |
| 103 | centerWidget(this, parentWidget()); |
| 104 | } |
| 105 | |
| 106 | /*! |
| 107 | * \brief Constructs an about dialog with the specified information. |
| 108 | */ |
| 109 | AboutDialog::AboutDialog(QWidget *parent, const QString &applicationName, const QString &creator, const QString &version, const QString &website, |
| 110 | const QString &description, const QImage &image) |
| 111 | : AboutDialog(parent, applicationName, creator, version, {}, website, description, image) |
| 112 | { |
| 113 | } |
| 114 | |
| 115 | /*! |
| 116 | * \brief Constructs an about dialog with the specified \a parent, \a |
| 117 | * description and \a image. |
| 118 | */ |
| 119 | AboutDialog::AboutDialog(QWidget *parent, const QString &website, const QString &description, const QImage &image) |
| 120 | : AboutDialog(parent, QString(), QString(), QString(), website, description, image) |
| 121 | { |
| 122 | } |
| 123 | |
| 124 | /*! |
| 125 | * \brief Destroys the about dialog. |
| 126 | */ |
| 127 | AboutDialog::~AboutDialog() |
| 128 | { |
| 129 | } |
| 130 | |
| 131 | bool AboutDialog::event(QEvent *event) |
| 132 | { |
| 133 | const auto res = QDialog::event(event); |
| 134 | switch (event->type()) { |
| 135 | case QEvent::PaletteChange: |
| 136 | setStyleSheet(dialogStyleForPalette(palette())); |
| 137 | break; |
| 138 | case QEvent::LanguageChange: |
| 139 | setWindowTitle(QCoreApplication::translate("QtUtilities::AboutDialog", "About", nullptr)); |
| 140 | emit retranslationRequired(); |
| 141 | break; |
| 142 | default:; |
| 143 | } |
| 144 | return res; |
| 145 | } |
| 146 | |
| 147 | void AboutDialog::linkActivated(const QString &link) |
| 148 | { |
| 149 | if (link == QLatin1String("qtversion")) { |
| 150 | QMessageBox::aboutQt(nullptr); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | } // namespace QtUtilities |