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