comparison src/main.cpp @ 246:43a5e09bb832

Split some utility functions to util.{h,cpp}
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 08 May 2018 13:14:29 +0300
parents 3d3ba5759cac
children 4f947840c806
comparison
equal deleted inserted replaced
245:f1b41bdabe12 246:43a5e09bb832
12 #include <QStandardPaths> 12 #include <QStandardPaths>
13 #include "main.h" 13 #include "main.h"
14 #include "ui_mainwindow.h" 14 #include "ui_mainwindow.h"
15 #include "ui_aboutwindow.h" 15 #include "ui_aboutwindow.h"
16 #include "runguard.h" 16 #include "runguard.h"
17 17 #include "util.h"
18 18
19 19
20 // 20 //
21 // Global struct for settings 21 // Global struct for settings
22 // 22 //
51 ) 51 )
52 }, 52 },
53 }; 53 };
54 54
55 static const int nslSQLSchemaData = sizeof(slSQLSchemaData) / sizeof(slSQLSchemaData[0]); 55 static const int nslSQLSchemaData = sizeof(slSQLSchemaData) / sizeof(slSQLSchemaData[0]);
56
57
58 //
59 // Convert QString to a double value, replacing comma
60 //
61 double slMoneyStrToValue(const QString &str)
62 {
63 QString str2 = str;
64 return str2.replace(",", ".").toDouble();
65 }
66
67
68 //
69 // Convert double value to formatted QString
70 //
71 QString slMoneyValueToStr(double val)
72 {
73 return QStringLiteral("%1").arg(val, 1, 'f', 2);
74 }
75
76
77 QString slMoneyValueToStrSign(double val)
78 {
79 return QStringLiteral("%1%2").
80 arg(val > 0 ? "+" : "").
81 arg(val, 1, 'f', 2);
82 }
83
84
85 //
86 // Trim and cleanup given QString (removing double whitespace etc.)
87 //
88 QString slCleanupStr(const QString &str)
89 {
90 return str.simplified().trimmed();
91 }
92
93
94 //
95 // Manipulate given QDateTime value to get desired
96 // correct timestamp.
97 //
98 const QDateTime slDateTimeToLocal(const QDateTime &val)
99 {
100 QDateTime tmp = val;
101 tmp.setOffsetFromUtc(0);
102 return tmp.toLocalTime();
103 }
104
105
106 //
107 // Return a string representation of given QDateTime
108 // converted to local time.
109 //
110 const QString slDateTimeToStr(const QDateTime &val)
111 {
112 return slDateTimeToLocal(val).toString(QStringLiteral("yyyy-MM-dd hh:mm"));
113 }
114
115
116 //
117 // Error logging
118 //
119 void slLog(const QString &mtype, const QString &msg)
120 {
121 QString filename = settings.dataPath + QDir::separator() + APP_LOG_FILE;
122 QFile fh(filename);
123 if (fh.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text))
124 {
125 QTextStream out(&fh);
126 out <<
127 slDateTimeToLocal(QDateTime::currentDateTimeUtc()).
128 toString(QStringLiteral("yyyy-MM-dd hh:mm:ss"))
129 << " [" << mtype << "]: " << msg << "\n";
130 fh.close();
131 }
132 }
133
134
135 //
136 // Display an error dialog with given title and message
137 //
138 int slErrorMsg(const QString &title, const QString &msg)
139 {
140 QMessageBox dlg;
141
142 slLog("ERROR", msg);
143
144 dlg.setText(title);
145 dlg.setInformativeText(msg);
146 dlg.setTextFormat(Qt::RichText);
147 dlg.setIcon(QMessageBox::Critical);
148 dlg.setStandardButtons(QMessageBox::Ok);
149 dlg.setDefaultButton(QMessageBox::Ok);
150
151 return dlg.exec();
152 }
153 56
154 57
155 // 58 //
156 // Check if an SQL error has occured (for given QSqlError) and 59 // Check if an SQL error has occured (for given QSqlError) and
157 // report it to stdout if so. Return "false" if error has occured, 60 // report it to stdout if so. Return "false" if error has occured,