Skip to content

Commit 8935e22

Browse files
committed
Updated for keyboard inputs + stop run button in menu.
1 parent 970a004 commit 8935e22

File tree

2 files changed

+192
-39
lines changed

2 files changed

+192
-39
lines changed

src/Editor/Editor.cpp

Lines changed: 171 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "Editor.h"
22

3-
void Editor::actualiserTextEditor(QString path) {
3+
void Editor::actualiserTextEditor(const QString& path) {
44
QFile file(path);
55
QString fileContent;
66

@@ -65,15 +65,80 @@ Editor::Editor(const QString &title, const QString &filePath) {
6565
"}"
6666
;
6767

68+
QString TerminalScrollBar_StyleSheet = "QScrollBar:vertical {"
69+
" background-color: rgb(51,54,56);"
70+
" width: 16px;"
71+
" margin: 16px 0 16px 0;"
72+
"}"
73+
"QScrollBar::handle:vertical {"
74+
" background-color: rgb(70,70,70);"
75+
" min-height: 20px;"
76+
"}"
77+
"QScrollBar::add-line:vertical {"
78+
" border: none;"
79+
" background: none;"
80+
"}"
81+
"QScrollBar::sub-line:vertical {"
82+
" border: none;"
83+
" background: none;"
84+
"}"
85+
"QScrollBar::add-page:vertical, QScrollBar::sub-page:vertical {"
86+
" background: none;"
87+
"}"
88+
;
89+
90+
QString Title_Sec_StyleSheet = ""
91+
"QLabel {"
92+
" border: none;"
93+
" color: rgb(190,190,190);"
94+
" font-size: 13px;"
95+
" text-align: center;"
96+
" padding: 10px 0;"
97+
"}"
98+
;
99+
100+
QString input_zone_StyleSheet = ""
101+
"QLineEdit {"
102+
" background: rgb(30,30,30);"
103+
" color: rgb(230,230,230);"
104+
" font-size: 12px;"
105+
" border: 0.5px solid rgb(100,100,100);"
106+
"}"
107+
;
108+
109+
QString input_btn_StyleSheet = ""
110+
"QPushButton {"
111+
" border: none;"
112+
" background: rgb(20,20,20);"
113+
" color: rgb(200,200,200);"
114+
" font-size: 13px;"
115+
" text-align: center;"
116+
" border-radius: 5px;"
117+
" padding: 5px 7px;"
118+
"}"
119+
"QPushButton:hover {"
120+
" color: white;"
121+
" border-radius: 10px;"
122+
"}"
123+
"QPushButton:pressed {"
124+
" color: #0061D1;"
125+
"}"
126+
;
127+
128+
129+
// Window :
130+
68131
this->setWindowTitle(title);
69-
this->resize(300, 500);
132+
this->resize(600, 500);
70133
this->setStyleSheet(Window_StyleSheet);
71134
this->setWindowIcon(QIcon("images/icon.png"));
72135

73136
this->titleApp = title;
74137
this->filePath = filePath;
75138
this->possiblePathInterpreter = "N/A";
76139

140+
141+
77142
// Interface :
78143

79144
this->editor_layout = new VerticalLayout(this, Qt::AlignCenter);
@@ -82,9 +147,32 @@ Editor::Editor(const QString &title, const QString &filePath) {
82147
this->editor_text->setPlaceholderText("Ecrivez votre code ici.");
83148
this->editor_text->setStyleSheet(Editor_StyleSheet);
84149
this->editor_terminal->setStyleSheet(Terminal_StyleSheet);
150+
QScrollBar* scrollBar = this->editor_terminal->verticalScrollBar();
151+
scrollBar->setStyleSheet(TerminalScrollBar_StyleSheet);
152+
153+
this->editor_inputsLayout = new HorizontalLayout(this, Qt::AlignCenter);
154+
this->editor_inputsTitle = new TextLabel(this, "Entrées claviers: ", Qt::AlignAbsolute);
155+
this->editor_inputsBtn = new TextButton(this, "Envoyer au programme.");
156+
this->editor_inputs = new QLineEdit(this);
157+
this->editor_inputsTitle->setStyleSheet(Title_Sec_StyleSheet);
158+
this->editor_inputs->setStyleSheet(input_zone_StyleSheet);
159+
this->editor_inputsBtn->setStyleSheet(input_btn_StyleSheet);
160+
this->editor_inputsLayout->addWidget(this->editor_inputsTitle);
161+
this->editor_inputsLayout->addItem(new QSpacerItem(5, 0, QSizePolicy::Minimum, QSizePolicy::Minimum));
162+
this->editor_inputsLayout->addWidget(this->editor_inputs);
163+
this->editor_inputsLayout->addItem(new QSpacerItem(15, 0, QSizePolicy::Minimum, QSizePolicy::Minimum));
164+
this->editor_inputsLayout->addWidget(this->editor_inputsBtn);
165+
this->editor_inputsLayout->addItem(new QSpacerItem(15, 0, QSizePolicy::Minimum, QSizePolicy::Minimum));
85166

86167
this->editor_layout->addWidget(this->editor_text);
87168
this->editor_layout->addWidget(this->editor_terminal);
169+
this->editor_layout->addItem(this->editor_inputsLayout);
170+
171+
connect(this->editor_inputsBtn, &QPushButton::clicked, this, &Editor::editor_inputs_btn);
172+
173+
this->process = new QProcess(this);
174+
this->editor_terminal->setReadOnly(true);
175+
88176

89177

90178
// Menu :
@@ -97,12 +185,21 @@ Editor::Editor(const QString &title, const QString &filePath) {
97185
this->menuEditor_files_save = new QAction(tr("Sauvegarder"), this);
98186
this->menuEditor_files_saveas = new QAction(tr("Sauvegarder sous"), this);
99187
this->menuEditor_run_run = new QAction(tr("Executer..."), this);
188+
this->menuEditor_run_stop = new QAction(tr("Arrêter..."), this);
100189
this->menuEditor_run_settings = new QAction(tr("Paramètres"), this);
190+
this->menuEditor_files_new->setShortcut(QKeySequence::New);
191+
this->menuEditor_files_open->setShortcut(QKeySequence::Open);
192+
this->menuEditor_files_save->setShortcut(QKeySequence::Save);
193+
this->menuEditor_files_saveas->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_A));
194+
this->menuEditor_run_run->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_F5));
195+
this->menuEditor_run_stop->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_F6));
196+
this->menuEditor_run_settings->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_F7));
101197
this->menuEditor_files->addAction(this->menuEditor_files_new);
102198
this->menuEditor_files->addAction(this->menuEditor_files_open);
103199
this->menuEditor_files->addAction(this->menuEditor_files_save);
104200
this->menuEditor_files->addAction(this->menuEditor_files_saveas);
105201
this->menuEditor_run->addAction(this->menuEditor_run_run);
202+
this->menuEditor_run->addAction(this->menuEditor_run_stop);
106203
this->menuEditor_run->addAction(this->menuEditor_run_settings);
107204
this->editor_menu->addMenu(this->menuEditor_files);
108205
this->editor_menu->addMenu(this->menuEditor_run);
@@ -129,10 +226,16 @@ Editor::Editor(const QString &title, const QString &filePath) {
129226
}
130227

131228

229+
230+
// Connections globales :
231+
132232
connect(this->menuEditor_files_new, &QAction::triggered, this, &Editor::menu_file_new);
133233
connect(this->menuEditor_files_open, &QAction::triggered, this, &Editor::menu_file_open);
234+
connect(this->menuEditor_files_save, &QAction::triggered, this, &Editor::menu_file_save);
235+
connect(this->menuEditor_files_saveas, &QAction::triggered, this, &Editor::menu_file_saveas);
134236

135237
connect(this->menuEditor_run_run, &QAction::triggered, this, &Editor::menu_run_run);
238+
connect(this->menuEditor_run_stop, &QAction::triggered, this, &Editor::menu_run_stop);
136239
connect(this->menuEditor_run_settings, &QAction::triggered, this, &Editor::menu_run_settings);
137240
}
138241

@@ -147,10 +250,8 @@ void Editor::menu_file_open()
147250

148251
if (!fileName.isEmpty()) {
149252
actualiserTextEditor(fileName);
253+
this->currentEditFile_Path = fileName;
150254
}
151-
152-
actualiserTextEditor(fileName);
153-
this->currentEditFile_Path = fileName;
154255
}
155256

156257
void Editor::menu_file_new() {
@@ -165,41 +266,41 @@ void Editor::menu_file_new() {
165266
}
166267

167268
void Editor::menu_run_run() {
168-
auto *process = new QProcess(this);
169-
if (this->currentEditFile_Path != "N/A") {
170-
QString program;
171-
QStringList arguments;
172-
if (this->possiblePathInterpreter == "N/A") {
173-
program = "interpreters/V2-FPL.exe";
174-
arguments << this->currentEditFile_Path;
175-
} else {
176-
program = this->possiblePathInterpreter;
269+
if (this->process->state() == QProcess::NotRunning) {
270+
this->editor_terminal->clear();
271+
this->editor_terminal->setPlainText("Interprétation en cours... \n");
272+
273+
if (this->currentEditFile_Path != "N/A") {
274+
QString program;
275+
QStringList arguments;
276+
if (this->possiblePathInterpreter == "N/A") {
277+
program = "interpreters\\V2-FPL.exe";
278+
} else {
279+
program = this->possiblePathInterpreter;
280+
}
177281
arguments << this->currentEditFile_Path;
178-
}
179-
180-
process->setProgram(program);
181-
process->setArguments(arguments);
182-
process->setProcessChannelMode(QProcess::MergedChannels);
183282

184-
connect(process, &QProcess::readyReadStandardOutput, [this, process](){
185-
QByteArray output = process->readAllStandardOutput();
186-
this->editor_terminal->appendPlainText(QString(output));
187-
});
283+
this->process->setProgram(program);
284+
this->process->setArguments(arguments);
285+
this->process->setProcessChannelMode(QProcess::MergedChannels);
188286

189-
process->start();
287+
this->process->start();
190288

191-
if (!process->waitForStarted()) {
192-
qDebug() << "Process failed to start";
193-
}
289+
connect(this->process, &QProcess::readyReadStandardOutput, this, [=, this](){
290+
QByteArray output = this->process->readAllStandardOutput();
291+
this->editor_terminal->appendPlainText(QString(output));
292+
});
194293

195-
if (!process->waitForFinished()) {
196-
qDebug() << "Process execution failed";
294+
connect(this->process, &QProcess::readyReadStandardError, this, [=, this](){
295+
QByteArray error = this->process->readAllStandardError();
296+
this->editor_terminal->appendPlainText(QString(error));
297+
});
197298
}
299+
} else {
300+
this->editor_terminal->setPlainText("Veuillez stopper le programme.");
198301
}
199302
}
200303

201-
202-
203304
void Editor::menu_run_settings() {
204305
QString Window_StyleSheet = ""
205306
"QWidget {"
@@ -297,5 +398,44 @@ void Editor::menu_run_settings() {
297398
});
298399
}
299400

401+
void Editor::menu_file_save() {
402+
QFile file {this->currentEditFile_Path};
403+
if (file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
404+
QTextStream out(&file);
405+
out << this->editor_text->toPlainText();
406+
file.close();
407+
}
408+
}
409+
410+
void Editor::menu_file_saveas() {
411+
QString fileName = QFileDialog::getSaveFileName(this,
412+
tr("Enregistrer sous"), QDir::currentPath(), tr("Fichiers texte (*.txt);;Tous les fichiers (*.*)"));
413+
if (!fileName.isEmpty()) {
414+
std::ofstream newF {fileName.toStdString()};
415+
if (newF.is_open()) {
416+
newF << this->editor_text->toPlainText().toStdString() << std::endl;
417+
newF.close();
418+
}
419+
420+
actualiserTextEditor(fileName);
421+
this->currentEditFile_Path = fileName;
422+
}
423+
}
424+
425+
void Editor::editor_inputs_btn() {
426+
if (this->process != nullptr && this->process->state() == QProcess::Running && this->editor_inputs != nullptr && !this->editor_inputs->text().isEmpty()) {
427+
std::string content = this->editor_inputs->text().toStdString() + "\n";
428+
this->process->write(content.c_str());
429+
}
430+
}
431+
432+
void Editor::menu_run_stop() {
433+
if (this->process->state() == QProcess::Running) {
434+
this->process->close();
435+
}
436+
this->editor_terminal->clear();
437+
this->editor_terminal->setPlainText("Interpréteur éteint.");
438+
}
439+
300440

301441
Editor::~Editor() = default;

src/Editor/Editor.h

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#include <QMenuBar>
1919
#include <QTextStream>
2020
#include <QProcess>
21+
#include <QScrollBar>
22+
#include <QThread>
2123

2224
#include "../TextButton/TextButton.h"
2325
#include "../TextLabel/TextLabel.h"
@@ -35,15 +37,20 @@ Q_OBJECT
3537
QString currentEditFile_Path = "N/A";
3638

3739
private slots:
40+
void editor_inputs_btn();
3841
void menu_file_new();
3942
void menu_file_open();
43+
void menu_file_save();
44+
void menu_file_saveas();
4045
void menu_run_run();
46+
void menu_run_stop();
4147
void menu_run_settings();
4248

4349
private:
4450
// Global :
45-
void actualiserTextEditor(QString path);
51+
void actualiserTextEditor(const QString& path);
4652
QString titleApp = "App";
53+
QProcess* process{};
4754

4855
// Editeur :
4956
VerticalLayout* editor_layout;
@@ -57,16 +64,22 @@ private slots:
5764
QAction* menuEditor_files_save;
5865
QAction* menuEditor_files_saveas;
5966
QAction* menuEditor_run_run;
67+
QAction* menuEditor_run_stop;
6068
QAction* menuEditor_run_settings;
69+
// Pour les inputs:
70+
HorizontalLayout* editor_inputsLayout{};
71+
TextLabel* editor_inputsTitle{};
72+
QLineEdit* editor_inputs{};
73+
TextButton* editor_inputsBtn{};
6174

6275
// Paramètres (Widget pour les paramètres) :
63-
QWidget* settings_W;
64-
VerticalLayout* layout_settings;
65-
TextLabel* title_settings;
66-
TextLabel* title_pathInter;
67-
QLineEdit* pathLineEdit;
68-
TextButton* selectFileButton;
69-
HorizontalLayout* layout_selectfile;
76+
QWidget* settings_W{};
77+
VerticalLayout* layout_settings{};
78+
TextLabel* title_settings{};
79+
TextLabel* title_pathInter{};
80+
QLineEdit* pathLineEdit{};
81+
TextButton* selectFileButton{};
82+
HorizontalLayout* layout_selectfile{};
7083
QString possiblePathInterpreter;
7184
};
7285

0 commit comments

Comments
 (0)