RabbitCommon v2.3.4
Loading...
Searching...
No Matches
DlgAbout.cpp
1/*++
2Copyright (c) Kang Lin studio, All Rights Reserved
3
4Author:
5 Kang Lin <kl222@126.com>
6
7Module Name:
8
9 DlgAbout.cpp
10
11Abstract:
12
13 This file contains about dialog implement.
14 */
15
16#include <QFile>
17#include <QDir>
18#include <QMenu>
19#include <QStandardPaths>
20#include <QSslError>
21#include <QDateTime>
22#include <QCursor>
23#include <QLoggingCategory>
24#include <QApplication>
25#ifdef HAVE_WebEngineWidgets
26#include <QWebEngineView>
27#endif
28
29#include "DlgAbout.h"
30#include "ui_DlgAbout.h"
31#include "RabbitCommonDir.h"
32#include "RabbitCommonTools.h"
33#include "Download.h"
34#include "Information.h"
35
36static Q_LOGGING_CATEGORY(log, "RabbitCommon.DlgAbout")
37
38CDlgAbout::CDlgAbout(QWidget *parent) :
39 QDialog(parent),
40 ui(new Ui::CDlgAbout),
41 m_pLicense(nullptr),
42 m_pChangeLog(nullptr),
43 m_pThanks(nullptr)
44{
45 ui->setupUi(this);
46
47 setAttribute(Qt::WA_QuitOnClose, true);
48
49 m_szAppName = qApp->applicationDisplayName();
50
51 m_szVersion = qApp->applicationVersion();
52#ifdef RabbitCommon_VERSION
53 if(m_szVersion.isEmpty())
54 {
55 qWarning(log) << "The m_szVersion is not set. Please use qApp->setApplicationVersion() or m_szVersion";
56 m_szVersion = RabbitCommon_VERSION;
57 }
58#endif
59
60 m_szArch = QSysInfo::buildCpuArchitecture();
61
62 m_szAuthor = tr("KangLin");
63 m_szEmail = "kl222@126.com";
64 m_szHomePage = "https://github.com/KangLin/" + qApp->applicationName();
65 m_szCopyrightOwner = tr("Kang Lin Studio");
66 m_szCopyrightStartTime = "2019";
67 m_AppIcon = QImage(":/icon/RabbitCommon/App");
68 m_CopyrightIcon = QImage(":/icon/RabbitCommon/CopyRight");
69
70 QString szLangUage = RabbitCommon::CTools::Instance()->GetLanguage();
71 QVector<QUrl> urls;
72 if("zh_CN" == szLangUage) {
73 m_DonationIcon = QImage(":/icon/RabbitCommon/Contribute_zh_CN");
74 urls << QUrl("https://github.com/KangLin/RabbitCommon/raw/master/Src/Resource/image/Contribute_zh_CN.png")
75 << QUrl("https://gitee.com/kl222/RabbitCommon/raw/master/Src/Resource/image/Contribute_zh_CN.png")
76 << QUrl("https://gitlab.com/kl222/RabbitCommon/-/raw/master/Src/Resource/image/Contribute_zh_CN.png");
77 } else {
78 m_DonationIcon = QImage(":/icon/RabbitCommon/Contribute_en");
79 urls << QUrl("https://github.com/KangLin/RabbitCommon/raw/master/Src/Resource/image/Contribute_en.png")
80 << QUrl("https://gitee.com/kl222/RabbitCommon/raw/master/Src/Resource/image/Contribute_en.png")
81 << QUrl("https://gitlab.com/kl222/RabbitCommon/-/raw/master/Src/Resource/image/Contribute_en.png");
82 }
83 m_Download = QSharedPointer<RabbitCommon::CDownload>(
84 new RabbitCommon::CDownload(), &QObject::deleteLater);
85 bool check = connect(m_Download.data(), SIGNAL(sigFinished(const QString)),
86 this, SLOT(slotDownloadFileFinished(const QString)));
87 Q_ASSERT(check);
88 check = connect(m_Download.data(), SIGNAL(sigError(int, const QString)),
89 this, SLOT(slotDownloadError(int, const QString)));
90 Q_ASSERT(check);
91 m_Download->Start(urls);
92
93 m_pLicense = new QTextBrowser(ui->tabWidget);
94 m_pChangeLog = new QTextBrowser(ui->tabWidget);
95 m_pThanks = new QTextBrowser(ui->tabWidget);
96
97 AppendFile(m_pChangeLog, "ChangeLog", tr("Change log"));
98 AppendFile(m_pLicense, "License", tr("License"));
99 AppendFile(m_pThanks, "Authors", tr("Thanks"));
100
101#if defined (Q_OS_ANDROID)
102 ui->lbDonation->installEventFilter(this);
103#else
104 ui->lbDonation->setContextMenuPolicy(Qt::CustomContextMenu);
105 check = connect(ui->lbDonation, SIGNAL(customContextMenuRequested(const QPoint &)),
106 this, SLOT(slotDonation(const QPoint &)));
107 Q_ASSERT(check);
108#endif
109
110}
111
112void CDlgAbout::showEvent(QShowEvent *event)
113{
114 Q_UNUSED(event)
115
116 setWindowIcon(QIcon(QPixmap::fromImage(m_AppIcon)));
117 ui->lblLogo->setPixmap(QPixmap::fromImage(m_AppIcon));
118 ui->lbCopyrightIcon->setPixmap(QPixmap::fromImage(m_CopyrightIcon));
119 ui->lbDonation->setPixmap(QPixmap::fromImage(m_DonationIcon));
120 ui->lblName->setText(m_szAppName);
121 ui->lbVersion->setText(Version());
122 ui->lbVersion->setOpenExternalLinks(true);
123 ui->lbAuthor->setText(tr("Author: ") + m_szAuthor
124 + tr(" Email: ") + "<a href=\"" + m_szEmail + "\">"
125 + m_szEmail + "</a>");
126 ui->lbAuthor->setOpenExternalLinks(true);
127 ui->lbHome->setOpenExternalLinks(true);
128 ui->lbHome->setText(tr("Home page: ") + "<a href=\"" + m_szHomePage + "\">"
129 + m_szHomePage + "</a>");
130 if(m_szCopyright.isEmpty())
131 {
132 if(m_szCopyrightTime.isEmpty())
133 m_szCopyrightTime = tr("%1 - %2").arg(m_szCopyrightStartTime,
134 QString::number(QDate::currentDate().year()));
135 m_szCopyright = tr("Copyright (C)") + " " + m_szCopyrightTime + " " + m_szCopyrightOwner;
136 }
137 ui->lbCopyright->setText(m_szCopyright);
138
139 adjustSize();
140}
141
142CDlgAbout::~CDlgAbout()
143{
144 delete ui;
145}
146
147int CDlgAbout::AppendFile(QTextBrowser *pEdit, const QString &szFile, const QString &szTitle)
148{
149 QDir d;
150 QString szFileLocation;
151 if(!pEdit) return -1;
152 szFileLocation = RabbitCommon::CDir::Instance()->GetDirDocument(
153 QApplication::applicationName(), true)
154 + QDir::separator()
155 + szFile + "_" + RabbitCommon::CTools::Instance()->GetLanguage() + ".md";
156 if(!d.exists(szFileLocation))
157 szFileLocation = RabbitCommon::CDir::Instance()->GetDirDocument(
158 QApplication::applicationName(), true)
159 + QDir::separator()
160 + szFile + ".md";
161 //TODO: be will remove in 3.0
162 if(!d.exists(szFileLocation))
163 szFileLocation = RabbitCommon::CDir::Instance()->GetDirApplicationInstallRoot() + QDir::separator()
164 + szFile + "_" + RabbitCommon::CTools::Instance()->GetLanguage() + ".md";
165 if(!d.exists(szFileLocation))
166 szFileLocation = RabbitCommon::CDir::Instance()->GetDirApplicationInstallRoot()
167 + QDir::separator() + szFile + ".md";
168
169 QFile readme(szFileLocation);
170 if(readme.open(QFile::ReadOnly))
171 {
172 QByteArray text;
173 text = readme.readAll();
174 QString szHtml;
175 #if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
176 szHtml = RabbitCommon::CTools::MarkDownToHtml(text);
177 #endif
178 if(szHtml.isEmpty())
179 szHtml = text;
180 if(pEdit) {
181 pEdit->setOpenExternalLinks(true);
182 pEdit->setOpenLinks(true);
183 pEdit->setReadOnly(true);
184 #if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
185 pEdit->setMarkdown(text);
186 #else
187 pEdit->setHtml(szHtml);
188 #endif
189 //把光标移动文档开始处
190 QTextCursor cursor = pEdit->textCursor();
191 cursor.movePosition(QTextCursor::Start);
192 pEdit->setTextCursor(cursor);
193 pEdit->show();
194 }
195 readme.close();
196 if(pEdit)
197 ui->tabWidget->addTab(pEdit, szTitle);
198 }
199 return 0;
200}
201
202void CDlgAbout::on_pbOK_clicked()
203{
204 close();
205}
206
207void CDlgAbout::slotDonation(const QPoint &pos)
208{
209 Q_UNUSED(pos)
210 QMenu m;
211 m.addAction(QIcon::fromTheme("document-save"), tr("Save"),
212 this, SLOT(slotSaveDonation()));
213 m.exec(QCursor::pos());
214}
215
216void CDlgAbout::slotSaveDonation()
217{
218 QString szDir = RabbitCommon::CDir::Instance()->GetDirUserImage()
219 + QDir::separator() + "donation.png";
220 QString szFile = QFileDialog::getSaveFileName(this,
221 tr("Save donation picture"),
222 szDir,
223 tr("Images (*.png *.xpm *.jpg)"));
224 QFileInfo fi(szFile);
225 if(fi.suffix().isEmpty())
226 szFile += ".png";
227 if(!szFile.isEmpty())
228 m_DonationIcon.save(szFile);
229}
230
231#if defined (Q_OS_ANDROID)
232bool CDlgAbout::eventFilter(QObject *watched, QEvent *event)
233{
234 qDebug() << event->type();
235 if(event->type() == QEvent::MouseButtonRelease)
236 slotSaveDonation();
237 return QDialog::eventFilter(watched, event);
238}
239#endif
240
241int CDlgAbout::SetDonationIcon(const QImage &img)
242{
243 m_Download.clear();
244 m_DonationIcon = img;
245 return 0;
246}
247
248QString CDlgAbout::BuildTime()
249{
250 if(m_szBuildTime.isEmpty())
251 return QString("%1/%2").arg(__DATE__, __TIME__);
252 return m_szBuildTime;
253}
254
255QString CDlgAbout::Version()
256{
257 QString szVersion;
258 if(m_szVersionRevision.isEmpty())
259 {
260 szVersion = tr("Version: ") + m_szVersion + " " + tr("Arch: ") + m_szArch;
261 } else {
262 if(m_szVersionRevisionUrl.isEmpty())
263 {
264 m_szVersionRevisionUrl =
265 m_szHomePage + "/tree/" + m_szVersionRevision;
266 }
267 szVersion = tr("Version: ") + m_szVersion + tr(" (From revision: ")
268#if (defined(HAVE_CMARK) || defined(HAVE_CMARK_GFM))
269 + "<a href=\"" + m_szVersionRevisionUrl + "\">"
270 + m_szVersionRevision + "</a>"
271#else
272 + m_szVersionRevision
273#endif
274 + ") " + tr("Arch: ") + m_szArch;
275 }
276 return szVersion;
277}
278
279void CDlgAbout::slotDownloadError(int nErr, const QString szError)
280{
281 QString szMsg;
282 szMsg = tr("Failed:") + tr("Download file is Failed.");
283 if(!szError.isEmpty())
284 szMsg += "(" + szError + ")";
285 qCritical(log) << szMsg << nErr;
286}
287
288void CDlgAbout::slotDownloadFileFinished(const QString szFile)
289{
290 qDebug(log) << "slotDownloadFileFinished:" << szFile;
291 m_DonationIcon.load(szFile);
292 ui->lbDonation->setPixmap(QPixmap::fromImage(m_DonationIcon));
293}
294
295void CDlgAbout::on_pbDetails_clicked()
296{
297 QString szApp;
298
299 szApp = tr("### ") + QApplication::applicationDisplayName() + "\n";
300 szApp += "- " + Version() + "\n";
301 szApp += "- " + tr("Build Date/Time: ") + BuildTime() + "\n";
302 szApp += "- " + tr("File Path: ") + QApplication::applicationFilePath() + "\n";
303 szApp += "- " + tr("Arguments: ") + qApp->arguments().join(' ') + "\n";
304
305 QCursor cursor = this->cursor();
306 setCursor(Qt::WaitCursor);
307 CInformation info(szApp, m_szDetails, this);
308#if defined (Q_OS_ANDROID)
309 info.showMaximized();
310#endif
311 RC_SHOW_WINDOW(&info);
312 setCursor(cursor);
313}
About dialog.
Definition DlgAbout.h:72
QString m_szVersion
The application version, it is different RabbitCommon::CTools::Version()
Definition DlgAbout.h:83
QString GetDirDocument(QString szProjectName=QString(), bool bReadOnly=false)
GetDirDocument.
Download the same file from multiple URLs.
Definition Download.h:48
static QString GetLanguage()
Get language Acquisition order: