Mercurial > hg > syntilista
comparison src/util.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 | |
children | 4f947840c806 |
comparison
equal
deleted
inserted
replaced
245:f1b41bdabe12 | 246:43a5e09bb832 |
---|---|
1 // | |
2 // Syntilista - debt list/management database program | |
3 // Programmed and designed by Matti Hämäläinen <ccr@tnsp.org> | |
4 // (C) Copyright 2017-2018 Tecnic Software productions (TNSP) | |
5 // | |
6 // Distributed under 3-clause BSD style license, refer to | |
7 // included file "COPYING" for exact terms. | |
8 // | |
9 #include "main.h" | |
10 #include "util.h" | |
11 #include <QMessageBox> | |
12 | |
13 | |
14 // | |
15 // Convert QString to a double value, replacing comma | |
16 // | |
17 double slMoneyStrToValue(const QString &str) | |
18 { | |
19 QString str2 = str; | |
20 return str2.replace(",", ".").toDouble(); | |
21 } | |
22 | |
23 | |
24 // | |
25 // Convert double value to formatted QString | |
26 // | |
27 QString slMoneyValueToStr(double val) | |
28 { | |
29 return QStringLiteral("%1").arg(val, 1, 'f', 2); | |
30 } | |
31 | |
32 | |
33 QString slMoneyValueToStrSign(double val) | |
34 { | |
35 return QStringLiteral("%1%2"). | |
36 arg(val > 0 ? "+" : ""). | |
37 arg(val, 1, 'f', 2); | |
38 } | |
39 | |
40 | |
41 // | |
42 // Trim and cleanup given QString (removing double whitespace etc.) | |
43 // | |
44 QString slCleanupStr(const QString &str) | |
45 { | |
46 return str.simplified().trimmed(); | |
47 } | |
48 | |
49 | |
50 // | |
51 // Manipulate given QDateTime value to get desired | |
52 // correct timestamp. | |
53 // | |
54 const QDateTime slDateTimeToLocal(const QDateTime &val) | |
55 { | |
56 QDateTime tmp = val; | |
57 tmp.setOffsetFromUtc(0); | |
58 return tmp.toLocalTime(); | |
59 } | |
60 | |
61 | |
62 // | |
63 // Return a string representation of given QDateTime | |
64 // converted to local time. | |
65 // | |
66 const QString slDateTimeToStr(const QDateTime &val) | |
67 { | |
68 // return slDateTimeToLocal(val).toString(QStringLiteral("yyyy-MM-dd hh:mm")); | |
69 return slDateTimeToLocal(val).toString(QStringLiteral("dd.MM.yyyy hh:mm")); | |
70 } | |
71 | |
72 | |
73 // | |
74 // Error logging | |
75 // | |
76 void slLog(const QString &mtype, const QString &msg) | |
77 { | |
78 QString filename = settings.dataPath + QDir::separator() + APP_LOG_FILE; | |
79 QFile fh(filename); | |
80 if (fh.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text)) | |
81 { | |
82 QTextStream out(&fh); | |
83 out << | |
84 slDateTimeToLocal(QDateTime::currentDateTimeUtc()). | |
85 toString(QStringLiteral("yyyy-MM-dd hh:mm:ss")) | |
86 << " [" << mtype << "]: " << msg << "\n"; | |
87 fh.close(); | |
88 } | |
89 } | |
90 | |
91 | |
92 // | |
93 // Display an error dialog with given title and message | |
94 // | |
95 int slErrorMsg(const QString &title, const QString &msg) | |
96 { | |
97 QMessageBox dlg; | |
98 | |
99 slLog("ERROR", msg); | |
100 | |
101 dlg.setText(title); | |
102 dlg.setInformativeText(msg); | |
103 dlg.setTextFormat(Qt::RichText); | |
104 dlg.setIcon(QMessageBox::Critical); | |
105 dlg.setStandardButtons(QMessageBox::Ok); | |
106 dlg.setDefaultButton(QMessageBox::Ok); | |
107 | |
108 return dlg.exec(); | |
109 } | |
110 | |
111 |