Debian Codegraph @

src/Gui/Dialogs/DlgAbout.cpp

raw
1 /***************************************************************************
2 * Copyright (c) 2004 Werner Mayer <wmayer[at]users.sourceforge.net> *
3 * *
4 * This file is part of the FreeCAD CAx development system. *
5 * *
6 * This library is free software; you can redistribute it and/or *
7 * modify it under the terms of the GNU Library General Public *
8 * License as published by the Free Software Foundation; either *
9 * version 2 of the License, or (at your option) any later version. *
10 * *
11 * This library is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU Library General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Library General Public *
17 * License along with this library; see the file COPYING.LIB. If not, *
18 * write to the Free Software Foundation, Inc., 59 Temple Place, *
19 * Suite 330, Boston, MA 02111-1307, USA *
20 * *
21 ***************************************************************************/
22
23 #include <cstdlib>
24 #include <QApplication>
25 #include <QClipboard>
26 #include <QDir>
27 #include <QFile>
28 #include <QFileInfo>
29 #include <QLocale>
30 #include <QProcessEnvironment>
31 #include <QRegularExpression>
32 #include <QRegularExpressionMatch>
33 #include <QScreen>
34 #include <QSettings>
35 #include <QSysInfo>
36 #include <QTextBrowser>
37 #include <QTextStream>
38 #include <QTimer>
39 #include <Inventor/C/basic.h>
40
41 #include <App/Application.h>
42 #include <App/Metadata.h>
43 #include <Base/Console.h>
44 #include <Base/Interpreter.h>
45 #include <CXX/WrapPython.h>
46 #include <Gui/Application.h>
47
48 #include <filesystem>
49 #include <LibraryVersions.h>
50 #include <zlib.h>
51
52 #include "BitmapFactory.h"
53 #include "Dialogs/DlgAbout.h"
54 #include "MainWindow.h"
55 #include "SplashScreen.h"
56 #include "ui_AboutApplication.h"
57
58 using namespace Gui;
59 using namespace Gui::Dialog;
60 namespace fs = std::filesystem;
61
62 // ------------------------------------------------------------------------------
63
64 AboutDialogFactory* AboutDialogFactory::factory = nullptr;
65
66 AboutDialogFactory::~AboutDialogFactory() = default;
67
68 QDialog* AboutDialogFactory::create(QWidget* parent) const
69 {
70 return new AboutDialog(parent);
71 }
72
73 const AboutDialogFactory* AboutDialogFactory::defaultFactory()
74 {
75 static const AboutDialogFactory this_factory;
76 if (factory) {
77 return factory;
78 }
79 return &this_factory;
80 }
81
82 void AboutDialogFactory::setDefaultFactory(AboutDialogFactory* f)
83 {
84 if (factory != f) {
85 delete factory;
86 }
87 factory = f;
88 }
89
90 // ------------------------------------------------------------------------------
91
92 /* TRANSLATOR Gui::Dialog::AboutDialog */
93
94 /**
95 * Constructs an AboutDialog which is a child of 'parent', with the
96 * name 'name' and widget flags set to 'WStyle_Customize|WStyle_NoBorder|WType_Modal'
97 *
98 * The dialog will be modal.
99 */
100 AboutDialog::AboutDialog(QWidget* parent)
101 : QDialog(parent)
102 , ui(new Ui_AboutApplication)
103 {
104 setModal(true);
105 ui->setupUi(this);
106 connect(ui->copyButton, &QPushButton::clicked, this, &AboutDialog::copyToClipboard);
107
108 // remove the automatic help button in dialog title since we don't use it
109 setWindowFlag(Qt::WindowContextHelpButtonHint, false);
110
111 layout()->setSizeConstraint(QLayout::SetFixedSize);
112 QRect rect = QApplication::primaryScreen()->availableGeometry();
113
114 // See if we have a custom About screen image set
115 QPixmap image = aboutImage();
116
117 // Fallback to the splashscreen image
118 if (image.isNull()) {
119 image = SplashScreen::splashImage();
120 }
121
122 // Make sure the image is not too big
123 int denom = 2;
124 if (image.height() > rect.height() / denom || image.width() > rect.width() / denom) {
125 float scale = static_cast<float>(image.width()) / static_cast<float>(image.height());
126 int width = std::min(image.width(), rect.width() / denom);
127 int height = std::min(image.height(), rect.height() / denom);
128 height = std::min(height, static_cast<int>(width / scale));
129 width = static_cast<int>(scale * height);
130
131 image = image.scaled(width, height);
132 }
133 ui->labelSplashPicture->setPixmap(image);
134 ui->tabWidget->setCurrentIndex(0); // always start on the About tab
135
136 setupLabels();
137 showCredits();
138 showLicenseInformation();
139 showLibraryInformation();
140 showCollectionInformation();
141 showPrivacyPolicy();
142 showOrHideImage(rect);
143 }
144
145 /**
146 * Destroys the object and frees any allocated resources
147 */
148 AboutDialog::~AboutDialog()
149 {
150 // no need to delete child widgets, Qt does it all for us
151 delete ui;
152 }
153
154 QPixmap AboutDialog::aboutImage() const
155 {
156 // See if we have a custom About screen image set
157 QPixmap about_image;
158 QFileInfo fi(QStringLiteral("images:about_image.png"));
159 if (fi.isFile() && fi.exists()) {
160 about_image.load(fi.filePath(), "PNG");
161 }
162
163 const std::string about_path = App::Application::Config()["AboutImage"];
164 if (!about_path.empty() && about_image.isNull()) {
165 QString path = QString::fromStdString(about_path);
166 if (QDir(path).isRelative()) {
167 QString home = QString::fromStdString(App::Application::getHomePath());
168 path = QFileInfo(QDir(home), path).absoluteFilePath();
169 }
170 about_image.load(path);
171
172 // Now try the icon paths
173 if (about_image.isNull()) {
174 about_image = Gui::BitmapFactory().pixmap(about_path.c_str());
175 }
176 }
177
178 return about_image;
179 }
180
181 void AboutDialog::showOrHideImage(const QRect& rect)
182 {
183 adjustSize();
184 if (height() > rect.height()) {
185 ui->labelSplashPicture->hide();
186 }
187 }
188
189 void AboutDialog::setupLabels()
190 {
191 // fonts are rendered smaller on Mac so point size can't be the same for all platforms
192 int fontSize = 8;
193 #ifdef Q_OS_MAC
194 fontSize = 11;
195 #endif
196 // avoid overriding user set style sheet
197 if (qApp->styleSheet().isEmpty()) {
198 setStyleSheet(
199 QStringLiteral("Gui--Dialog--AboutDialog QLabel {font-size: %1pt;}").arg(fontSize)
200 );
201 }
202
203 QString exeName = qApp->applicationName();
204 std::map<std::string, std::string>& config = App::Application::Config();
205 std::map<std::string, std::string>::iterator it;
206 QString banner = QString::fromStdString(config["CopyrightInfo"]);
207 banner = banner.left(banner.indexOf(QLatin1Char('\n')));
208 QString major = QString::fromStdString(config["BuildVersionMajor"]);
209 QString minor = QString::fromStdString(config["BuildVersionMinor"]);
210 QString point = QString::fromStdString(config["BuildVersionPoint"]);
211 QString suffix = QString::fromStdString(config["BuildVersionSuffix"]);
212 QString build = QString::fromStdString(config["BuildRevision"]);
213 QString disda = QString::fromStdString(config["BuildRevisionDate"]);
214 QString mturl = QString::fromStdString(config["MaintainerUrl"]);
215
216 // we use replace() to keep label formatting, so a label with text "<b>Unknown</b>"
217 // gets replaced to "<b>FreeCAD</b>", for example
218
219 QString author = ui->labelAuthor->text();
220 author.replace(QStringLiteral("Unknown Application"), exeName);
221 author.replace(QStringLiteral("(c) Unknown Author"), banner);
222 ui->labelAuthor->setText(author);
223 ui->labelAuthor->setUrl(mturl);
224
225 if (qApp->styleSheet().isEmpty()) {
226 ui->labelAuthor->setStyleSheet(QStringLiteral(
227 "Gui--UrlLabel {color: #0000FF;text-decoration: underline;font-weight: 600;}"
228 ));
229 }
230
231 QString version = ui->labelBuildVersion->text();
232 version.replace(
233 QStringLiteral("Unknown"),
234 QStringLiteral("%1.%2.%3%4").arg(major, minor, point, suffix)
235 );
236 ui->labelBuildVersion->setText(version);
237
238 QString revision = ui->labelBuildRevision->text();
239 revision.replace(QStringLiteral("Unknown"), build);
240 ui->labelBuildRevision->setText(revision);
241
242 QString date = ui->labelBuildDate->text();
243 date.replace(QStringLiteral("Unknown"), disda);
244 ui->labelBuildDate->setText(date);
245
246 QString os = ui->labelBuildOS->text();
247 os.replace(QStringLiteral("Unknown"), App::Application::prettyProductInfoWrapper());
248 ui->labelBuildOS->setText(os);
249
250 QString architecture = ui->labelBuildRunArchitecture->text();
251 if (QSysInfo::buildCpuArchitecture() == QSysInfo::currentCpuArchitecture()) {
252 architecture.replace(QStringLiteral("Unknown"), QSysInfo::buildCpuArchitecture());
253 }
254 else {
255 architecture.replace(
256 QStringLiteral("Unknown"),
257 QStringLiteral("%1 (running on: %2)")
258 .arg(QSysInfo::buildCpuArchitecture(), QSysInfo::currentCpuArchitecture())
259 );
260 }
261 ui->labelBuildRunArchitecture->setText(architecture);
262
263 // branch name
264 it = config.find("BuildRevisionBranch");
265 if (it != config.end()) {
266 QString branch = ui->labelBuildBranch->text();
267 branch.replace(QStringLiteral("Unknown"), QString::fromStdString(it->second));
268 ui->labelBuildBranch->setText(branch);
269 }
270 else {
271 ui->labelBranch->hide();
272 ui->labelBuildBranch->hide();
273 }
274
275 // hash id
276 it = config.find("BuildRevisionHash");
277 if (it != config.end()) {
278 QString hash = ui->labelBuildHash->text();
279 hash.replace(
280 QStringLiteral("Unknown"),
281 QString::fromStdString(it->second).left(7)
282 ); // Use the 7-char abbreviated hash
283 ui->labelBuildHash->setText(hash);
284 if (auto url_itr = config.find("BuildRepositoryURL"); url_itr != config.end()) {
285 auto url = QString::fromStdString(url_itr->second);
286
287 if (int space = url.indexOf(QChar::fromLatin1(' ')); space != -1) {
288 url = url.left(space); // Strip off the branch information to get just the repo
289 }
290
291 if (url == QStringLiteral("Unknown")) {
292 url = QStringLiteral("https://github.com/FreeCAD/FreeCAD"); // Just take a guess
293 }
294
295 // This may only create valid URLs for Github, but some other hosts use the same format
296 // so give it a shot...
297 auto https = url.replace(QStringLiteral("git://"), QStringLiteral("https://"));
298 https.replace(QStringLiteral(".git"), QStringLiteral(""));
299 ui->labelBuildHash->setUrl(
300 https + QStringLiteral("/commit/") + QString::fromStdString(it->second)
301 );
302 }
303 }
304 else {
305 ui->labelHash->hide();
306 ui->labelBuildHash->hide();
307 }
308 }
309
310 void AboutDialog::showCredits()
311 {
312 auto creditsFileURL = QLatin1String(":/doc/CONTRIBUTORS");
313 QFile creditsFile(creditsFileURL);
314
315 if (!creditsFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
316 return;
317 }
318
319 auto tab_credits = new QWidget();
320 tab_credits->setObjectName(QStringLiteral("tab_credits"));
321 ui->tabWidget->addTab(tab_credits, tr("Credits"));
322 auto hlayout = new QVBoxLayout(tab_credits);
323 auto textField = new QTextBrowser(tab_credits);
324 textField->setOpenLinks(false);
325 hlayout->addWidget(textField);
326
327 //: Header for the Credits tab of the About screen
328 QString creditsHTML
329 = QStringLiteral("<html><body><h1>%1</h1><p>%2</p><h2>%3</h2><ul>")
330 .arg(tr("Credits", "Header for the Credits tab of the About screen"))
331 .arg(tr("FreeCAD would not be possible without the contributions of:"))
332 .arg(tr("Individuals", "Header for the list of individual people in the Credits list."));
333
334 QTextStream stream(&creditsFile);
335 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
336 stream.setCodec("UTF-8");
337 #endif
338 QString line;
339 while (stream.readLineInto(&line)) {
340 if (!line.isEmpty()) {
341 if (line == QStringLiteral("Firms")) {
342 creditsHTML += QStringLiteral("</ul><h2>");
343 //: Header for the list of companies/organizations in the Credits list.
344 creditsHTML += tr("Organizations");
345 creditsHTML += QStringLiteral("</h2><ul>");
346 }
347 else {
348 creditsHTML += QStringLiteral("<li>") + line + QStringLiteral("</li>");
349 }
350 }
351 }
352 creditsHTML += QStringLiteral("</ul></body></html>");
353 textField->setHtml(creditsHTML);
354 }
355
356 void AboutDialog::showLicenseInformation()
357 {
358 QString licenseFileURL = QStringLiteral("%1/LICENSE.html")
359 .arg(QString::fromStdString(App::Application::getHelpDir()));
360 QFile licenseFile(licenseFileURL);
361
362 if (licenseFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
363 QString licenseHTML = QString::fromUtf8(licenseFile.readAll());
364 const auto placeholder = QStringLiteral(
365 "<!--PLACEHOLDER_FOR_ADDITIONAL_LICENSE_INFORMATION-->"
366 );
367 licenseHTML.replace(placeholder, getAdditionalLicenseInformation());
368
369 ui->tabWidget->removeTab(1); // Hide the license placeholder widget
370
371 auto tab_license = new QWidget();
372 tab_license->setObjectName(QStringLiteral("tab_license"));
373 ui->tabWidget->addTab(tab_license, tr("License"));
374 auto hlayout = new QVBoxLayout(tab_license);
375 auto textField = new QTextBrowser(tab_license);
376 textField->setOpenExternalLinks(true);
377 hlayout->addWidget(textField);
378
379 textField->setHtml(licenseHTML);
380 }
381 else {
382 QString info(QLatin1String("SUCH DAMAGES.<hr/>"));
383 info += getAdditionalLicenseInformation();
384 QString lictext = ui->textBrowserLicense->toHtml();
385 lictext.replace(QStringLiteral("SUCH DAMAGES.<hr/>"), info);
386 ui->textBrowserLicense->setHtml(lictext);
387 }
388 }
389
390 QString AboutDialog::getAdditionalLicenseInformation() const
391 {
392 // Any additional piece of text to be added after the main license text goes below.
393 // Please set title in <h2> tags, license text in <p> tags
394 // and add an <hr/> tag at the end to nicely separate license blocks
395 QString info;
396 #ifdef _USE_3DCONNEXION_SDK
397 info += QStringLiteral(
398 "<h2>3D Mouse Support</h2>"
399 "<p>Development tools and related technology provided under license from 3Dconnexion.<br/>"
400 "Copyright &#169; 1992&ndash;2012 3Dconnexion. All rights reserved.</p>"
401 "<hr/>"
402 );
403 #endif
404 return info;
405 }
406
407 void AboutDialog::showLibraryInformation()
408 {
409 auto tab_library = new QWidget();
410 tab_library->setObjectName(QStringLiteral("tab_library"));
411 ui->tabWidget->addTab(tab_library, tr("Libraries"));
412 auto hlayout = new QVBoxLayout(tab_library);
413 auto textField = new QTextBrowser(tab_library);
414 textField->setOpenExternalLinks(true);
415 hlayout->addWidget(textField);
416
417 QString baseurl = QStringLiteral("file:///%1/ThirdPartyLibraries.html")
418 .arg(QString::fromStdString(App::Application::getHelpDir()));
419
420 textField->setSource(QUrl(baseurl));
421 }
422
423 void AboutDialog::showCollectionInformation()
424 {
425 QString doc = QString::fromStdString(App::Application::getHelpDir());
426 QString path = doc + QLatin1String("Collection.html");
427 if (!QFile::exists(path)) {
428 return;
429 }
430
431 auto tab_collection = new QWidget();
432 tab_collection->setObjectName(QStringLiteral("tab_collection"));
433 ui->tabWidget->addTab(tab_collection, tr("Collection"));
434 auto hlayout = new QVBoxLayout(tab_collection);
435 auto textField = new QTextBrowser(tab_collection);
436 textField->setOpenExternalLinks(true);
437 hlayout->addWidget(textField);
438 textField->setSource(path);
439 }
440
441 void AboutDialog::showPrivacyPolicy()
442 {
443 auto policyFileURL = QLatin1String(":/doc/PRIVACY_POLICY");
444 QFile policyFile(policyFileURL);
445
446 if (!policyFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
447 return;
448 }
449 auto text = QString::fromUtf8(policyFile.readAll());
450 auto tabPrivacyPolicy = new QWidget();
451 tabPrivacyPolicy->setObjectName(QStringLiteral("tabPrivacyPolicy"));
452 ui->tabWidget->addTab(tabPrivacyPolicy, tr("Privacy Policy"));
453 auto hLayout = new QVBoxLayout(tabPrivacyPolicy);
454 auto textField = new QTextBrowser(tabPrivacyPolicy);
455 textField->setOpenExternalLinks(true);
456 hLayout->addWidget(textField);
457 textField->setMarkdown(text);
458 }
459
460 void AboutDialog::linkActivated(const QUrl& link)
461 {
462 auto licenseView = new LicenseView();
463 licenseView->setAttribute(Qt::WA_DeleteOnClose);
464 licenseView->show();
465 QString title = tr("License");
466 QString fragment = link.fragment();
467 if (fragment.startsWith(QLatin1String("_Toc"))) {
468 QString prefix = fragment.mid(4);
469 title = QStringLiteral("%1 %2").arg(prefix, title);
470 }
471 licenseView->setWindowTitle(title);
472 getMainWindow()->addWindow(licenseView);
473 licenseView->setSource(link);
474 }
475
476 void AboutDialog::copyToClipboard()
477 {
478 QString data;
479 QTextStream str(&data);
480 std::map<std::string, std::string>& config = App::Application::Config();
481 App::Application::getVerboseCommonInfo(str, config);
482 Gui::Application::getVerboseDPIStyleInfo(str);
483 App::Application::getVerboseAddOnsInfo(str, config);
484
485 QClipboard* cb = QApplication::clipboard();
486 cb->setText(data);
487
488 auto copytext = ui->copyButton->text();
489 ui->copyButton->setText(tr("Copied!"));
490 const int timeout = 2000;
491 QTimer::singleShot(timeout, this, [this, copytext]() { ui->copyButton->setText(copytext); });
492 }
493
494 // ----------------------------------------------------------------------------
495
496 /* TRANSLATOR Gui::LicenseView */
497
498 LicenseView::LicenseView(QWidget* parent)
499 : MDIView(nullptr, parent, Qt::WindowFlags())
500 {
501 browser = new QTextBrowser(this);
502 browser->setOpenExternalLinks(true);
503 browser->setOpenLinks(true);
504 setCentralWidget(browser);
505 }
506
507 LicenseView::~LicenseView() = default;
508
509 void LicenseView::setSource(const QUrl& url)
510 {
511 browser->setSource(url);
512 }
513
514 #include "moc_DlgAbout.cpp"