Skip to content

Commit 5ba7d7e

Browse files
committedAug 19, 2019
dialog return codes, cleaning comments, readme
1 parent 609aa4b commit 5ba7d7e

File tree

5 files changed

+68
-42
lines changed

5 files changed

+68
-42
lines changed
 

‎README.md

+10-3
Original file line numberDiff line numberDiff line change
@@ -211,12 +211,19 @@ The launch instruction is the script that gets called when the user hits Ok (aft
211211

212212
Your app should then read the resultant settings.json file and use a json parser to extract the settings.
213213

214-
## Questions or Feature Requests?
214+
## Return Codes
215+
216+
Fig gives different return codes based on how the dialog was closed.
217+
218+
- 0: User pressed "Ok" and settings were saved
219+
- 1: Dialog window closed or "Cancel" button hit
220+
- 2: Error: Most likely fig failed to load schema
221+
222+
## Questions, Bugs, Feature Requests?
215223

216224
If you have any questions or want to discuss this project, I'm always open for communication.
217225

218-
If you have a feature or pull request, let me know and I may have some tips on how to go
219-
about implementing it, even if I don't have the time to do so myself.
226+
Post bugs and feature requests to github issues. This is a new project so you might run into limitations or issues.
220227

221228
If you find this useful, give it a star on github. Thanks!
222229

‎src/FigApp.cpp

+32-16
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ FigApp :: FigApp(int& argc, char* argv[]):
2323
m_pSettings = make_shared<Meta>(m_SettingsFn);
2424
}catch(Error& e){
2525
QMessageBox::critical(m_pWindow.get(), "Error", "Could not load settings file.");
26-
quit();
26+
fail();
2727
goto return_ctor;
2828
}
2929

@@ -32,16 +32,21 @@ FigApp :: FigApp(int& argc, char* argv[]):
3232
//m_pSchema = make_shared<Schema>(make_shared<Meta>(m_SchemaFn));
3333
}catch(Error& e){
3434
QMessageBox::critical(m_pWindow.get(), "Error", "Could not load schema file.");
35-
quit();
35+
fail();
3636
goto return_ctor;
3737
}
3838

3939
m_Title = m_Args.value_or("title", "Settings");
4040
if(m_pSchema->has(".title"))
4141
m_Title = m_pSchema->at<string>(".title");
4242

43-
init();
44-
m_Success = true;
43+
if(!init()){
44+
QMessageBox::critical(m_pWindow.get(), "Error", "Failed to initialize schema.");
45+
fail();
46+
goto return_ctor;
47+
}
48+
49+
m_ReturnCode = RC_SUCCESS; // ctor passed, tell main() we didn't fail
4550

4651
return_ctor: {}
4752
}
@@ -93,19 +98,12 @@ string FigApp :: as_string(const MetaElement& me)
9398

9499
}
95100

96-
void FigApp :: init()
101+
bool FigApp :: init()
97102
{
98103
m_pWindow = make_unique<FigWindow>(this);
99104

100-
//QPushButton *button1 = new QPushButton("One");
101-
//QPushButton *button2 = new QPushButton("Two");
102105
QVBoxLayout* layout = new QVBoxLayout;
103106
//layout->setObjectName("layout");
104-
//auto widget = new QWidget();
105-
//layout->addWidget(button1);
106-
//layout->addWidget(button2);
107-
//m_pWindow->setLayout(layout);
108-
//m_pWindow->setCentralWidget(widget);
109107

110108
if(m_pSchema->has(".icon")){
111109
auto icon = m_pSchema->at<string>(".icon");
@@ -225,8 +223,7 @@ void FigApp :: init()
225223
else if(vals)
226224
{
227225
auto box = new QComboBox;
228-
box->setObjectName("box");
229-
// if values exist, use a combobox
226+
//box->setObjectName("box");
230227
int j=0;
231228
for(auto&& val: *vals){
232229
string v = as_string(val);
@@ -329,9 +326,12 @@ void FigApp :: init()
329326
m_pWindow->setLayout(layout);
330327
m_pWindow->setWindowTitle(m_Title.c_str());
331328

332-
load();
329+
if(!load())
330+
return false;
333331

334332
m_pWindow->show();
333+
334+
return true;
335335
}
336336

337337
bool FigApp :: save()
@@ -456,7 +456,7 @@ bool FigApp :: save()
456456
return not fail;
457457
}
458458

459-
void FigApp :: load()
459+
bool FigApp :: load()
460460
{
461461
for(auto&& category: *m_pSettings)
462462
{
@@ -540,6 +540,8 @@ void FigApp :: load()
540540
}
541541
}
542542
}
543+
544+
return true;
543545
}
544546

545547
template<class T>
@@ -652,19 +654,33 @@ void FigApp :: restore_defaults()
652654
void FigApp :: save_and_quit()
653655
{
654656
if(save()){
657+
m_ReturnCode = RC_SUCCESS;
655658
if(m_pSchema->has(".launch")){
656659
auto launch = m_pSchema->at<string>(".launch");
657660
if(!QProcess::startDetached(launch.c_str())){
658661
string e = string("Unable to launch \"") + launch + "\".";
659662
QMessageBox::critical(m_pWindow.get(), "Error", e.c_str());
663+
m_ReturnCode = RC_ERROR;
660664
}
661665
}
662666
QApplication::quit();
663667
}
668+
else{
669+
// failed save() calls show dialog
670+
m_ReturnCode = RC_ERROR;
671+
QApplication::quit();
672+
}
664673
}
665674

666675
void FigApp :: quit()
667676
{
677+
m_ReturnCode = RC_CLOSE;
678+
QApplication::quit();
679+
}
680+
681+
void FigApp :: fail()
682+
{
683+
m_ReturnCode = RC_ERROR;
668684
QApplication::quit();
669685
}
670686

‎src/FigApp.h

+15-4
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,26 @@ class FigApp:
2828
virtual ~FigApp();
2929

3030
bool event(QEvent* event) override;
31-
bool failed() const { return not m_Success; }
32-
void init();
31+
void return_code(int r) { m_ReturnCode=r; } // called from FigApp on window close
32+
int return_code() const { return m_ReturnCode; }
33+
bool failed() const { return m_ReturnCode != 0; }
34+
bool init();
35+
void fail();
3336

3437
static std::string as_string(std::shared_ptr<Meta> m, std::string key);
3538
static std::string as_string(const MetaElement& me);
3639
bool save();
37-
void load();
40+
bool load();
3841

3942
template<class T>
4043
int index_of_meta(std::shared_ptr<Meta>& m, T v);
4144

45+
enum eReturnCodes {
46+
RC_SUCCESS = 0,
47+
RC_CLOSE = 1,
48+
RC_ERROR = 2
49+
};
50+
4251
private Q_SLOTS:
4352

4453
void quit();
@@ -62,7 +71,9 @@ class FigApp:
6271

6372
std::unique_ptr<FigWindow> m_pWindow;
6473

65-
bool m_Success = false;
74+
// this return code is both the ctor result (checked in main)
75+
// and the final app return code
76+
int m_ReturnCode = RC_ERROR; // start on error, since ctor can fail
6677
};
6778

6879
#endif

‎src/FigWindow.cpp

+2-16
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,18 @@
11
#include "FigWindow.h"
2+
#include "FigApp.h"
23
using namespace std;
34

45
FigWindow :: FigWindow(FigApp* app):
56
m_pApp(app)
67
{
7-
//m_UI.setupUi(this);
8-
//m_UI.taskList->setShowGrid(false);
9-
//m_UI.taskList->setContextMenuPolicy(Qt::CustomContextMenu);
10-
//connect(
11-
// m_UI.taskList,
12-
// SIGNAL(customContextMenuRequested(const QPoint&)),
13-
// app,
14-
// SLOT(ctxMenu(const QPoint&))
15-
//);
16-
//show();
178
}
189

1910
FigWindow :: ~FigWindow()
2011
{
21-
2212
}
2313

2414
void FigWindow :: closeEvent(QCloseEvent* ev)
2515
{
26-
//if(m_pApp->tray()->isVisible())
27-
//{
28-
// hide();
29-
// ev->ignore();
30-
//}
16+
m_pApp->return_code(FigApp::RC_CLOSE);
3117
}
3218

‎src/Main.cpp

+9-3
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,16 @@ using namespace std;
66
int main(int argc, char **argv)
77
{
88
FigApp app(argc, argv);
9-
9+
1010
if(app.failed())
11-
return 1;
11+
return FigApp::RC_ERROR;
1212

13-
return app.exec();
13+
int r = app.exec();
14+
15+
int rc = app.return_code();
16+
if(rc != 0)
17+
return rc;
18+
19+
return r;
1420
}
1521

0 commit comments

Comments
 (0)
Please sign in to comment.