@@ -49,6 +49,18 @@ void MainView::EndPythonRun()
4949 ui->btnRun ->setEnabled (true );
5050 ui->btnRunSnippet ->setEnabled (true );
5151}
52+ bool MainView::Confirm (const QString& what)
53+ {
54+ QMessageBox msgBox (this );
55+ msgBox.setWindowTitle (tr (APP_NAME));
56+ msgBox.setText (what);
57+ msgBox.setStandardButtons (QMessageBox::Yes | QMessageBox::No);
58+ msgBox.setDefaultButton (QMessageBox::No);
59+ if (msgBox.exec () == QMessageBox::Yes) {
60+ return true ;
61+ }
62+ return false ;
63+ }
5264void MainView::LoadSettings ()
5365{
5466 QSettings settings;
@@ -95,7 +107,6 @@ void MainView::LoadResources()
95107 if (!success) {
96108 mAbout = tr (APP_NAME " Written by Bhathiya Perera" );
97109 }
98-
99110}
100111MainView::~MainView ()
101112{
@@ -138,9 +149,12 @@ QString MainView::LoadFile(const QString& fileName, bool& success)
138149 return text;
139150}
140151
141- void MainView::BrowseAndLoadFile (CodeEditor* codeEditor)
152+ void MainView::BrowseAndLoadFile (CodeEditor* codeEditor, const bool isPython )
142153{
143- QString fileName = QFileDialog::getOpenFileName (this );
154+
155+ QString fileName = QFileDialog::getOpenFileName (this , tr (" Open" ),
156+ QApplication::applicationDirPath (),
157+ ((isPython) ? FILETYPES_PYTHON : FILETYPES_OTHER));
144158 if (fileName.isEmpty ()) {
145159 return ;
146160 }
@@ -151,9 +165,11 @@ void MainView::BrowseAndLoadFile(CodeEditor* codeEditor)
151165 }
152166}
153167
154- void MainView::SaveFile (CodeEditor* codeEditor)
168+ void MainView::SaveFile (CodeEditor* codeEditor, const bool isPython )
155169{
156- QString fileName = QFileDialog::getSaveFileName (this );
170+ QString fileName = QFileDialog::getSaveFileName (this , tr (" Save" ),
171+ QApplication::applicationDirPath (),
172+ ((isPython) ? FILETYPES_PYTHON : FILETYPES_OTHER));
157173 if (fileName.isEmpty ()) {
158174 return ;
159175 }
@@ -236,21 +252,28 @@ void MainView::on_cmbFontSize_currentIndexChanged(const QString& fontSize)
236252
237253void MainView::on_btnCodeClear_clicked ()
238254{
239- ui->txtCode ->clear ();
255+ if (Confirm (tr (" Are you sure you want to clear code ?" ))) {
256+ ui->txtCode ->clear ();
257+ }
240258}
241259
242260void MainView::on_btnInputClear_clicked ()
243261{
244- ui->txtInput ->clear ();
262+ if (Confirm (tr (" Are you sure you want to clear input ?" ))) {
263+ ui->txtInput ->clear ();
264+ }
245265}
246266
247267void MainView::on_btnOutputClear_clicked ()
248268{
249- ui->txtOutput ->clear ();
269+ if (Confirm (tr (" Are you sure you want to clear output ?" ))) {
270+ ui->txtOutput ->clear ();
271+ }
250272}
251273
252274void MainView::on_btnOutputOpen_clicked ()
253275{
276+
254277 BrowseAndLoadFile (ui->txtOutput );
255278}
256279
@@ -261,7 +284,10 @@ void MainView::on_btnInputOpen_clicked()
261284
262285void MainView::on_btnCodeOpen_clicked ()
263286{
264- BrowseAndLoadFile (ui->txtCode );
287+ if (!ui->txtCode ->toPlainText ().isEmpty () && Confirm (tr (" Would you like to save code ?" ))) {
288+ on_btnCodeSave_clicked ();
289+ }
290+ BrowseAndLoadFile (ui->txtCode , true );
265291}
266292
267293void MainView::on_btnOutputSave_clicked ()
@@ -276,7 +302,7 @@ void MainView::on_btnInputSave_clicked()
276302
277303void MainView::on_btnCodeSave_clicked ()
278304{
279- SaveFile (ui->txtCode );
305+ SaveFile (ui->txtCode , true );
280306}
281307
282308void MainView::on_btnCodeDatabase_clicked ()
@@ -358,3 +384,26 @@ void MainView::LoadSnippetsToCombo()
358384 ui->cmbSnippets ->addItems (QStringList (keys));
359385 }
360386}
387+
388+ void MainView::on_btnUpdateSnippet_clicked ()
389+ {
390+ if (ui->txtCode ->toPlainText ().isEmpty ()) {
391+ return ;
392+ }
393+
394+ if (!Confirm (" Are you sure you want to overwrite selected snippet ?" )){
395+ return ;
396+ }
397+
398+ bool ok;
399+
400+
401+ mSnippets ->AddSnippet (ui->cmbSnippets ->currentText (), ui->txtCode ->toPlainText (), ok);
402+
403+ if (ok) {
404+ QMessageBox::information (this , tr (APP_NAME), tr (" Snippet updated." ));
405+ } else {
406+ QMessageBox::critical (this , tr (APP_NAME), tr (" Snippet updating failed." ));
407+ }
408+ LoadSnippetsToCombo ();
409+ }
0 commit comments