Skip to content

Commit 5e38a72

Browse files
committed
Update Snippet, Confirmation Icon, Save and Open Dialogs
1 parent ab42d7b commit 5e38a72

6 files changed

Lines changed: 123 additions & 21 deletions

File tree

Features/snippets.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ void Snippets::LoadSnippets(bool& success)
2323
}
2424

2525
if (data.isEmpty()) {
26-
//TODO Add More Snippets : RegEx, PyLint
2726
data.insert(tr("API Help"), tr(
2827
"# Full API\n"
2928
"# ---------------------------\n"

Icons/Update.png

50 KB
Loading

PyRunResources.qrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@
1111
<file>Icons/PyRunImg.png</file>
1212
<file>startme.py</file>
1313
<file>About.htm</file>
14+
<file>Icons/Update.png</file>
1415
</qresource>
1516
</RCC>

UI/mainview.cpp

Lines changed: 59 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}
5264
void 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
}
100111
MainView::~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

237253
void 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

242260
void 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

247267
void 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

252274
void MainView::on_btnOutputOpen_clicked()
253275
{
276+
254277
BrowseAndLoadFile(ui->txtOutput);
255278
}
256279

@@ -261,7 +284,10 @@ void MainView::on_btnInputOpen_clicked()
261284

262285
void 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

267293
void MainView::on_btnOutputSave_clicked()
@@ -276,7 +302,7 @@ void MainView::on_btnInputSave_clicked()
276302

277303
void MainView::on_btnCodeSave_clicked()
278304
{
279-
SaveFile(ui->txtCode);
305+
SaveFile(ui->txtCode, true);
280306
}
281307

282308
void 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+
}

UI/mainview.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,11 @@ private slots:
5858
void StartPythonRun();
5959
void EndPythonRun();
6060

61+
void on_btnUpdateSnippet_clicked();
62+
6163
private:
64+
const QString FILETYPES_PYTHON = tr("Python Code (*.py);;All files (*.*)");
65+
const QString FILETYPES_OTHER = tr("Text files (*.txt);;All files (*.*)");
6266
QThread workerThread;
6367
Ui::MainView* ui;
6468
PythonSyntaxHighlighter* mHighlighter;
@@ -67,14 +71,15 @@ private slots:
6771
Snippets* mSnippets;
6872
void ChangeFontSize(QFont font, int size);
6973
void SetupHighlighter();
70-
void SaveFile(CodeEditor* codeEditor);
71-
void BrowseAndLoadFile(CodeEditor* codeEditor);
74+
void SaveFile(CodeEditor* codeEditor, const bool isPython = false);
75+
void BrowseAndLoadFile(CodeEditor* codeEditor, const bool isPython = false);
7276
QString LoadFile(const QString& fileName, bool& success);
7377
void LoadResources();
7478
void LoadSnippetsToCombo();
7579
void RunPythonCode(const QString& code);
7680
void LoadSettings();
7781
void SetupPython();
82+
bool Confirm(const QString& what);
7883
signals:
7984
void operate(const QString&, const QString&);
8085
};

UI/mainview.ui

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<rect>
77
<x>0</x>
88
<y>0</y>
9-
<width>753</width>
9+
<width>803</width>
1010
<height>620</height>
1111
</rect>
1212
</property>
@@ -20,7 +20,7 @@
2020
<widget class="QWidget" name="centralWidget">
2121
<layout class="QVBoxLayout" name="verticalLayout">
2222
<item>
23-
<layout class="QHBoxLayout" name="hlFont">
23+
<layout class="QHBoxLayout" name="horizontalLayout_3">
2424
<item>
2525
<widget class="QLabel" name="lblFonts">
2626
<property name="text">
@@ -163,6 +163,19 @@
163163
</item>
164164
</widget>
165165
</item>
166+
<item>
167+
<spacer name="horizontalSpacer">
168+
<property name="orientation">
169+
<enum>Qt::Horizontal</enum>
170+
</property>
171+
<property name="sizeHint" stdset="0">
172+
<size>
173+
<width>40</width>
174+
<height>20</height>
175+
</size>
176+
</property>
177+
</spacer>
178+
</item>
166179
<item>
167180
<widget class="QLabel" name="lblSnippets">
168181
<property name="text">
@@ -267,7 +280,7 @@
267280
</widget>
268281
</item>
269282
<item>
270-
<widget class="QPushButton" name="btnRunSnippet">
283+
<widget class="QPushButton" name="btnLoadSnippet">
271284
<property name="minimumSize">
272285
<size>
273286
<width>24</width>
@@ -281,14 +294,14 @@
281294
</size>
282295
</property>
283296
<property name="toolTip">
284-
<string>Run Selected Snippet</string>
297+
<string>Load To Code Area</string>
285298
</property>
286299
<property name="text">
287300
<string/>
288301
</property>
289302
<property name="icon">
290303
<iconset resource="../PyRunResources.qrc">
291-
<normaloff>:/data/Icons/Run.png</normaloff>:/data/Icons/Run.png</iconset>
304+
<normaloff>:/data/Icons/Load.png</normaloff>:/data/Icons/Load.png</iconset>
292305
</property>
293306
<property name="iconSize">
294307
<size>
@@ -299,7 +312,7 @@
299312
</widget>
300313
</item>
301314
<item>
302-
<widget class="QPushButton" name="btnLoadSnippet">
315+
<widget class="QPushButton" name="btnUpdateSnippet">
303316
<property name="minimumSize">
304317
<size>
305318
<width>24</width>
@@ -313,14 +326,14 @@
313326
</size>
314327
</property>
315328
<property name="toolTip">
316-
<string>Load To Code Area</string>
329+
<string>Update Current Snippet</string>
317330
</property>
318331
<property name="text">
319332
<string/>
320333
</property>
321334
<property name="icon">
322335
<iconset resource="../PyRunResources.qrc">
323-
<normaloff>:/data/Icons/Load.png</normaloff>:/data/Icons/Load.png</iconset>
336+
<normaloff>:/data/Icons/Update.png</normaloff>:/data/Icons/Update.png</iconset>
324337
</property>
325338
<property name="iconSize">
326339
<size>
@@ -349,11 +362,46 @@
349362
</property>
350363
</widget>
351364
</item>
365+
<item>
366+
<widget class="QPushButton" name="btnRunSnippet">
367+
<property name="minimumSize">
368+
<size>
369+
<width>24</width>
370+
<height>24</height>
371+
</size>
372+
</property>
373+
<property name="maximumSize">
374+
<size>
375+
<width>24</width>
376+
<height>24</height>
377+
</size>
378+
</property>
379+
<property name="toolTip">
380+
<string>Run Selected Snippet</string>
381+
</property>
382+
<property name="text">
383+
<string/>
384+
</property>
385+
<property name="icon">
386+
<iconset resource="../PyRunResources.qrc">
387+
<normaloff>:/data/Icons/Run.png</normaloff>:/data/Icons/Run.png</iconset>
388+
</property>
389+
<property name="iconSize">
390+
<size>
391+
<width>16</width>
392+
<height>16</height>
393+
</size>
394+
</property>
395+
</widget>
396+
</item>
352397
<item>
353398
<spacer name="hpFont">
354399
<property name="orientation">
355400
<enum>Qt::Horizontal</enum>
356401
</property>
402+
<property name="sizeType">
403+
<enum>QSizePolicy::Minimum</enum>
404+
</property>
357405
<property name="sizeHint" stdset="0">
358406
<size>
359407
<width>40</width>

0 commit comments

Comments
 (0)