src/mainwindow.cpp

changeset 6
73e448b2943d
parent 5
593a658cba8e
child 7
68443f5be176
equal deleted inserted replaced
5:593a658cba8e 6:73e448b2943d
7 MainWindow::MainWindow(QWidget *parent) : 7 MainWindow::MainWindow(QWidget *parent) :
8 QMainWindow{parent}, 8 QMainWindow{parent},
9 ui{std::make_unique<Ui_MainWindow>()}, 9 ui{std::make_unique<Ui_MainWindow>()},
10 documents{this} 10 documents{this}
11 { 11 {
12 ui->setupUi(this); 12 this->ui->setupUi(this);
13 connect(ui->actionNew, &QAction::triggered, this, &MainWindow::newModel); 13 connect(ui->actionNew, &QAction::triggered, this, &MainWindow::newModel);
14 connect(ui->actionOpen, &QAction::triggered, this, &MainWindow::openModel);
14 connect(ui->actionQuit, &QAction::triggered, this, &QMainWindow::close); 15 connect(ui->actionQuit, &QAction::triggered, this, &QMainWindow::close);
15 updateTitle(); 16 this->updateTitle();
17 this->loadLocales();
18 changeLanguage(QLocale::system().name());
16 } 19 }
17 20
18 MainWindow::~MainWindow() 21 MainWindow::~MainWindow()
19 { 22 {
20 } 23 }
28 #include <QFileDialog> 31 #include <QFileDialog>
29 #include <QMessageBox> 32 #include <QMessageBox>
30 void MainWindow::openModel() 33 void MainWindow::openModel()
31 { 34 {
32 const QString path = QFileDialog::getOpenFileName( 35 const QString path = QFileDialog::getOpenFileName(
33 this, "Open model", "", "LDraw models (*.ldr *.dat)"); 36 this, tr("Open model"), "", tr("LDraw models (*.ldr *.dat)"));
34 if (not path.isEmpty()) 37 if (not path.isEmpty())
35 { 38 {
36 QFile file{path}; 39 QFile file{path};
37 const bool open_result = file.open(QIODevice::ReadOnly); 40 const bool open_result = file.open(QIODevice::ReadOnly);
38 if (open_result) 41 if (open_result)
39 { 42 {
40 43
41 } 44 }
42 else 45 else
43 { 46 {
44 QMessageBox::critical(this, "Problem opening file", 47 QMessageBox::critical(this, tr("Problem opening file"),
45 QString{"Could not open %1: %2"} 48 tr("Could not open %1: %2")
46 .arg(path) 49 .arg(path)
47 .arg(file.errorString())); 50 .arg(file.errorString()));
48 } 51 }
49 } 52 }
53 }
54
55 /**
56 * @brief Changes the application language to the specified language
57 * @param localeCode Code of the locale to translate to
58 */
59 void MainWindow::changeLanguage(const QString& localeCode)
60 {
61 if (not localeCode.isEmpty() and localeCode != this->currentLanguage)
62 {
63 this->currentLanguage = localeCode;
64 QLocale::setDefault({localeCode});
65 qApp->removeTranslator(&this->translator);
66 const bool loadSuccessful = this->translator.load(pathToTranslation(localeCode));
67 if (loadSuccessful)
68 {
69 qApp->installTranslator(&this->translator);
70 }
71 }
72 }
73
74 void MainWindow::languageChangeRequested()
75 {
76 QAction* const senderAction = qobject_cast<QAction*>(sender());
77 if (senderAction != nullptr)
78 {
79 const QString localeCode = senderAction->data().toString();
80 this->changeLanguage(localeCode);
81 }
82 }
83
84 void MainWindow::changeEvent(QEvent* event)
85 {
86 if (event != nullptr)
87 {
88 switch (event->type())
89 {
90 case QEvent::LanguageChange:
91 this->ui->retranslateUi(this);
92 break;
93 default:
94 break;
95 }
96 }
97 QMainWindow::changeEvent(event);
50 } 98 }
51 99
52 /** 100 /**
53 * @brief Creates a new tab widget for the specified model. 101 * @brief Creates a new tab widget for the specified model.
54 * @param model Model to get a new tab widget for 102 * @param model Model to get a new tab widget for
103 QString title = ::appName; 151 QString title = ::appName;
104 title += " "; 152 title += " ";
105 title += fullVersionString(); 153 title += fullVersionString();
106 setWindowTitle(title); 154 setWindowTitle(title);
107 } 155 }
156
157 QString MainWindow::pathToTranslation(const QString& localeCode)
158 {
159 QDir dir {":/locale"};
160 return dir.filePath(localeCode + ".qm");
161 }
162
163 void MainWindow::loadLocales()
164 {
165 QDir dir {":/locale"};
166 this->ui->menuLanguage->clear();
167 QVector<QString> localeCodes = {"en"};
168 for (const QFileInfo& file : dir.entryInfoList(QDir::Files))
169 {
170 localeCodes.append(file.baseName());
171 }
172 for (const QString& localeCode : localeCodes)
173 {
174 const QLocale locale{localeCode};
175 const QString languageName = QLocale::languageToString(locale.language());
176 const QIcon flag{":/flags/" + localeCode + ".png"};
177 QAction* action = new QAction{languageName, this};
178 action->setData(localeCode);
179 action->setIcon(flag);
180 this->ui->menuLanguage->addAction(action);
181 connect(action, &QAction::triggered, this, &MainWindow::languageChangeRequested);
182 }
183 }

mercurial