This repository has been archived by the owner on Apr 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
130 lines (99 loc) · 3.43 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
#include "mainwindow.h"
MainWindow::MainWindow(QApplication * _app, QWidget *parent)
: QMainWindow(parent) {
app = _app;
setWindowTitle("Squirrel Recipe");
setAcceptDrops(true);
setMinimumWidth(500);
setMinimumHeight(650);
dialog = new QFileDialog(this);
helpdialog = new HelpDialog(this);
rloader = new RecipeLoader();
rInfo = new RecipeInfo(this);
SetupFileMenu();
SetupLangMenu();
SetupHelpMenu();
setCentralWidget(rInfo);
UpdateLang();
connect(this, SIGNAL(recipeLoaded(Recipe*)), rInfo, SLOT(LoadRecipe(Recipe*)));
connect(this, SIGNAL(refreshLang()), rInfo, SLOT(UpdateLang()));
connect(this, SIGNAL(refreshLang()), this, SLOT(UpdateLang()));
connect(this, SIGNAL(refreshLang()), helpdialog, SLOT(UpdateLang()));
}
MainWindow::~MainWindow() {
}
void MainWindow::SetupFileMenu() {
fileMenu = new QMenu("", this);
menuBar()->addMenu(fileMenu);
fileMenu->addAction("", this, SLOT(LoadRecipe()), QKeySequence::New);
fileMenu->addAction("", this, SLOT(CloseSlot()), QKeySequence::Close);
}
void MainWindow::SetupLangMenu() {
langMenu = new QMenu("", this);
menuBar()->addMenu(langMenu);
langMenu->addAction(QIcon(QPixmap(":/images/flag_fr.png")), "", this, SLOT(ChangeLangFr()));
langMenu->addAction(QIcon(QPixmap(":/images/flag_en.png")), "", this, SLOT(ChangeLangEn()));
}
void MainWindow::SetupHelpMenu() {
helpMenu = new QMenu("", this);
menuBar()->addMenu(helpMenu);
helpMenu->addAction("", this, SLOT(OpenHelp()), QKeySequence::HelpContents);
}
void MainWindow::UpdateLang() {
langMenu->setTitle(tr("&Langue"));
langMenu->actions()[0]->setText(tr("&Français"));
langMenu->actions()[1]->setText(tr("&Anglais"));
fileMenu->setTitle(tr("&Fichier"));
fileMenu->actions()[0]->setText(tr("&Charger une recette"));
fileMenu->actions()[1]->setText(tr("&Quitter"));
helpMenu->setTitle(tr("&Aide"));
helpMenu->actions()[0]->setText(tr("&A propos"));
}
void MainWindow::ChangeLangEn() {
ChangeLang("en");
}
void MainWindow::ChangeLangFr() {
ChangeLang("fr");
}
void MainWindow::ChangeLang(QString lang) {
app->removeTranslator(&translator);
QString fileName(":/langs/lang_" + lang + ".qm");
translator.load(fileName);
app->installTranslator(&translator);
emit refreshLang();
}
void MainWindow::LoadRecipe() {
dialog->setWindowTitle(tr("Charger une recette"));
dialog->setFileMode(QFileDialog::AnyFile);
dialog->setNameFilter(tr("Fichier JSON (*.json)"));
dialog->setViewMode(QFileDialog::Detail);
QStringList fileNames;
if (dialog->exec()){
fileNames = dialog->selectedFiles();
LoadFile(fileNames[0]);
}
}
void MainWindow::CloseSlot() {
emit closeApp();
}
void MainWindow::dropEvent(QDropEvent *event) {
if(event->mimeData()->hasUrls()) {
LoadFile(event->mimeData()->urls().at(0).toLocalFile());
}
}
void MainWindow::dragEnterEvent(QDragEnterEvent* event)
{
event->acceptProposedAction();
}
void MainWindow::LoadFile(const QString &path) {
QFile f(path);
try{
rloader->LoadFromFile(&f);
emit recipeLoaded(rloader->GetRecipe());
}catch(std::runtime_error& e) {
QMessageBox::critical(0, tr("Erreur"), e.what());
}
}
void MainWindow::OpenHelp() {
helpdialog->show();
}