-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmainwindow.cpp
137 lines (118 loc) · 5.56 KB
/
mainwindow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include "mainwindow.h"
#include "extractwindow.h"
#include "license.h"
#include "ui_mainwindow.h"
#include <QTranslator>
#include <QLibraryInfo>
#include <QDebug>
#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow),
compressWindow(new CompressWindow(this)),
extractWindow(new ExtractWindow(this)) {
ui->setupUi(this);
if (QFile(QStringLiteral("%1/settings.ini").arg(QCoreApplication::applicationDirPath())).exists())
m_settings = new QSettings(QStringLiteral("%1/settings.ini").arg(QCoreApplication::applicationDirPath()), QSettings::IniFormat);
else
m_settings = new QSettings(QSettings::NativeFormat, QSettings::UserScope, QStringLiteral("berv-uni"), QStringLiteral("frogarchiver"));
detectTranslations();
QSize iconSize = QSize(fontMetrics().height(), fontMetrics().height());
ui->buttonCompress->setIconSize(iconSize);
ui->buttonExtract->setIconSize(iconSize);
ui->mainToolBar->addAction(ui->actionCompress);
connect(ui->actionCompress, &QAction::triggered, this, &MainWindow::Compress);
connect(ui->buttonCompress, &QPushButton::pressed, this,
&MainWindow::Compress);
ui->mainToolBar->addAction(ui->actionExtract);
connect(ui->actionExtract, &QAction::triggered, this, &MainWindow::Extract);
connect(ui->buttonExtract, &QPushButton::pressed, this, &MainWindow::Extract);
connect(ui->actionExit, &QAction::triggered, this, &QApplication::quit);
connect(ui->actionAbout, &QAction::triggered, this, &MainWindow::showAbout);
connect(ui->actionLicense, &QAction::triggered, this, &MainWindow::showLicense);
connect(ui->menuLanguage, &QMenu::triggered, this, &MainWindow::changeLanguage);
}
MainWindow::~MainWindow() {
delete compressWindow;
delete extractWindow;
delete ui;
}
void MainWindow::showAbout() {
About Ab1;
Ab1.setModal(true);
Ab1.exec();
}
void MainWindow::Extract() { extractWindow->show(); }
void MainWindow::Compress() { compressWindow->show(); }
void MainWindow::showLicense() {
License license;
license.setModal(true);
license.exec();
}
void MainWindow::detectTranslations()
{
ui->menuLanguage->clear();
m_translations.clear();
const QStringList nameFilter{QStringLiteral("FrogArchiver_*.qm")};
const QString qt_ = QStringLiteral("qt_");
QDir i18nDir(QStringLiteral("%1/%2").arg(QCoreApplication::applicationDirPath(), QStringLiteral("i18n")));
if (i18nDir.entryList(nameFilter, QDir::Files, QDir::Name).isEmpty()) {
i18nDir.setPath(QStringLiteral("%1/../i18n").arg(QCoreApplication::applicationDirPath()));
if (i18nDir.entryList(nameFilter, QDir::Files, QDir::Name).isEmpty()) {
i18nDir.setPath(QStringLiteral("%1/../share/frogarchiver/i18n").arg(QCoreApplication::applicationDirPath()));
if (i18nDir.entryList(nameFilter, QDir::Files, QDir::Name).isEmpty()) {
i18nDir.setPath(QStringLiteral("/usr/share/frogarchiver/i18n"));
}
}
}
const QStringList langList = i18nDir.entryList(nameFilter,QDir::Files, QDir::Name);
for (const QString &translation : langList) {
auto *appTranslator = new QTranslator;
if (appTranslator->load(translation, i18nDir.absolutePath())) {
QString langCode = appTranslator->language().mid(0, 2);
auto *qtTranslator = new QTranslator;
QLocale locale = QLocale(appTranslator->language());
if (!langCode.startsWith(QStringLiteral("en")) && !qtTranslator->load(locale, qt_, QString(), i18nDir.absolutePath())) {
if (!qtTranslator->load(locale, qt_, QString(), QLibraryInfo::location(QLibraryInfo::TranslationsPath))) {
qWarning() << "Can't find qt ts";
}
}
m_translations.insert(langCode, {qtTranslator, appTranslator});
if (m_settings->value("language", QStringLiteral("en")).toString() == langCode) {
QApplication::installTranslator(qtTranslator);
QApplication::installTranslator(appTranslator);
}
}
}
for (const QString &translation : langList) {
QTranslator translator;
translator.load(translation, i18nDir.absolutePath());
QString lang = translation.mid(13,2);
auto langAction = ui->menuLanguage->addAction(translator.translate("MainWindow","TRANSLATE TO YOUR LANGUAGE NAME"));
langAction->setData(lang);
langAction->setCheckable(true);
langAction->setChecked(m_settings->value("language").toString() == lang);
}
}
void MainWindow::changeLanguage(QAction *action)
{
QVariant data = action->data();
if(!m_translations.contains(data.toString()))
detectTranslations();
const QList<QAction*> allActions = ui->menuLanguage->actions();
for (QAction *other : allActions)
other->setChecked(false);
for(auto translation : m_translations.value(m_settings->value("language").toString()))
QApplication::removeTranslator(translation);
m_settings->setValue("language", data);
for(auto translation : m_translations.value(data.toString()))
QApplication::installTranslator(translation);
action->setChecked(true);
QTranslator *translator = new QTranslator(this);
translator->load(QString("FrogArchiver_%1.qm").arg(m_settings->value("language").toString()), QString("%1/i18n").arg(QCoreApplication::applicationDirPath()));
qApp->installTranslator(translator);
}
void MainWindow::changeEvent(QEvent *e)
{
if (e->type() == QEvent::LanguageChange)
ui->retranslateUi(this);
}