From 79415b76442ac6f8307ec0e10accec32383fabb9 Mon Sep 17 00:00:00 2001 From: Stefan Suhren Date: Mon, 4 May 2015 00:33:50 +0200 Subject: Add translations that are changeable during runtime --- GUI_SS2015.pro | 15 +++++++-- german.qm | Bin 0 -> 801 bytes languagedialog.cpp | 52 ++++++++++++++++++++++++++++++++ languagedialog.h | 38 +++++++++++++++++++++++ languagedialog.ui | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++++ main.cpp | 7 +++++ mainwindow.cpp | 61 +++++++++++++++++++++++++++++++++++-- mainwindow.h | 13 ++++++-- mainwindow.ui | 6 ++++ translations.qrc | 5 +++ 10 files changed, 275 insertions(+), 9 deletions(-) create mode 100644 german.qm create mode 100644 languagedialog.cpp create mode 100644 languagedialog.h create mode 100644 languagedialog.ui create mode 100644 translations.qrc diff --git a/GUI_SS2015.pro b/GUI_SS2015.pro index f6527fc..263d369 100644 --- a/GUI_SS2015.pro +++ b/GUI_SS2015.pro @@ -15,11 +15,20 @@ TEMPLATE = app SOURCES += main.cpp\ mainwindow.cpp \ dlgpreferences.cpp \ - interactioncanvas.cpp + interactioncanvas.cpp \ + languagedialog.cpp HEADERS += mainwindow.h \ dlgpreferences.h \ - interactioncanvas.h + interactioncanvas.h \ + languagedialog.h FORMS += mainwindow.ui \ - dlgpreferences.ui + dlgpreferences.ui \ + languagedialog.ui + +TRANSLATIONS += \ + german.ts + +RESOURCES += \ + translations.qrc diff --git a/german.qm b/german.qm new file mode 100644 index 0000000..ed7e949 Binary files /dev/null and b/german.qm differ diff --git a/languagedialog.cpp b/languagedialog.cpp new file mode 100644 index 0000000..c470a30 --- /dev/null +++ b/languagedialog.cpp @@ -0,0 +1,52 @@ +#include "languagedialog.h" +#include "ui_languagedialog.h" + +LanguageDialog::LanguageDialog(QWidget *parent) : + QDialog(parent), + ui(new Ui::LanguageDialog) +{ + ui->setupUi(this); +} + +LanguageDialog::~LanguageDialog() +{ + delete ui; +} + +int LanguageDialog::language() const +{ + return m_language; +} + +void LanguageDialog::setLanguage(int arg) +{ + if (m_language == arg) + { + return; + } + + switch (arg) + { + case 0: + ui->radioButtonEnglish->setChecked(true); + break; + + case 1: + ui->radioButtonGerman->setChecked(true); + break; + } + + m_language = arg; + emit languageChanged(arg); +} + +void LanguageDialog::on_radioButtonEnglish_clicked() +{ + m_language = 0; +} + + +void LanguageDialog::on_radioButtonGerman_clicked() +{ + m_language = 1; +} diff --git a/languagedialog.h b/languagedialog.h new file mode 100644 index 0000000..4dd0c61 --- /dev/null +++ b/languagedialog.h @@ -0,0 +1,38 @@ +#ifndef LANGUAGEDIALOG_H +#define LANGUAGEDIALOG_H + +#include +#include + +namespace Ui { +class LanguageDialog; +} + +class LanguageDialog : public QDialog +{ + Q_OBJECT + Q_PROPERTY(int language READ language WRITE setLanguage NOTIFY languageChanged) + +public: + explicit LanguageDialog(QWidget *parent = 0); + ~LanguageDialog(); + + int language() const; + +public slots: + void setLanguage(int arg); + +signals: + void languageChanged(int arg); + +private slots: + void on_radioButtonEnglish_clicked(); + + void on_radioButtonGerman_clicked(); + +private: + Ui::LanguageDialog *ui; + int m_language; +}; + +#endif // LANGUAGEDIALOG_H diff --git a/languagedialog.ui b/languagedialog.ui new file mode 100644 index 0000000..366541b --- /dev/null +++ b/languagedialog.ui @@ -0,0 +1,87 @@ + + + LanguageDialog + + + + 0 + 0 + 400 + 300 + + + + Dialog + + + + + + Language + + + + + + Eng&lish + + + + + + + Deuts&ch + + + + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + + + + buttonBox + accepted() + LanguageDialog + accept() + + + 248 + 254 + + + 157 + 274 + + + + + buttonBox + rejected() + LanguageDialog + reject() + + + 316 + 260 + + + 286 + 274 + + + + + diff --git a/main.cpp b/main.cpp index b48f94e..0649a69 100644 --- a/main.cpp +++ b/main.cpp @@ -1,9 +1,16 @@ #include "mainwindow.h" #include +#include +#include int main(int argc, char *argv[]) { QApplication a(argc, argv); + +// QTranslator translator; +// translator.load(":/translations.pm"); +// a.installTranslator(&translator); + MainWindow w; w.show(); diff --git a/mainwindow.cpp b/mainwindow.cpp index 5a548db..8bc0a22 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -26,17 +26,17 @@ void MainWindow::on_actionPreferences_triggered() tmp->setHeight(size().height()); tmp->setWidth(size().width()); - if(tmp->exec()) + if (tmp->exec()) { qDebug() << "DlgPreferences okay clicked."; int width = tmp->Width(); int height = tmp->Height(); - if(width >= 100 && width <= 800 && height >= 100 && height <= 800) + if (width >= 100 && width <= 800 && height >= 100 && height <= 800) { qDebug() << "Resizing QMainWindow."; - resize(width,height); + resize(width, height); } } @@ -44,7 +44,62 @@ void MainWindow::on_actionPreferences_triggered() } +void MainWindow::on_actionLanguage_triggered() +{ + LanguageDialog *tmp = new LanguageDialog(this); + + if (QLocale::system().language() == QLocale::German) + { + tmp->setLanguage(1); + } + else + { + tmp->setLanguage(0); + } + + if (tmp->exec()) + { + switch (tmp->language()) + { + case 0: + qDebug() << "English"; + loadLanguage(":/english.qm"); + + break; + + case 1: + qDebug() << "German"; + loadLanguage(":/german.qm"); + + break; + } + } +} + + void MainWindow::mouseMoveEvent(QMouseEvent *mouseEvent) { qDebug() << "MainWindow:" << mouseEvent->pos(); } + +void MainWindow::loadLanguage(const QString& rLanguageFile) +{ + // remove the old translator + qApp->removeTranslator(&m_translator); + + // load the new translator + if (m_translator.load(rLanguageFile)) + { + qApp->installTranslator(&m_translator); + } +} + +void MainWindow::changeEvent(QEvent *event) +{ + if (event != 0 && event->type() == QEvent::LanguageChange) + { + ui->retranslateUi(this); + } + + QMainWindow::changeEvent(event); +} diff --git a/mainwindow.h b/mainwindow.h index 410878f..10976c4 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -6,6 +6,7 @@ #include #include "dlgpreferences.h" #include "interactioncanvas.h" +#include "languagedialog.h" namespace Ui { class MainWindow; @@ -22,14 +23,20 @@ public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); + void loadLanguage(const QString &rLanguageFile); + +protected: + void mouseMoveEvent(QMouseEvent *mouseEvent); + private slots: void on_actionPreferences_triggered(); + void on_actionLanguage_triggered(); + + void changeEvent(QEvent *event); private: Ui::MainWindow *ui; + QTranslator m_translator; // contains the translations for this application - // QWidget interface -protected: - void mouseMoveEvent(QMouseEvent *mouseEvent); }; #endif // MAINWINDOW_H diff --git a/mainwindow.ui b/mainwindow.ui index 8e92663..209a4b9 100644 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -37,6 +37,7 @@ Edi&t + @@ -66,6 +67,11 @@ &About + + + &Language + + diff --git a/translations.qrc b/translations.qrc new file mode 100644 index 0000000..dabca70 --- /dev/null +++ b/translations.qrc @@ -0,0 +1,5 @@ + + + german.qm + + -- cgit v1.2.3-70-g09d2