# HG changeset patch # User Matti Hamalainen # Date 1525774469 -10800 # Node ID 43a5e09bb832d9c2362ea6a06c3bcbddbcff40d6 # Parent f1b41bdabe1277d10128aafd148a4f2bbd553423 Split some utility functions to util.{h,cpp} diff -r f1b41bdabe12 -r 43a5e09bb832 Makefile.gen --- a/Makefile.gen Tue May 08 00:44:17 2018 +0300 +++ b/Makefile.gen Tue May 08 13:14:29 2018 +0300 @@ -29,6 +29,7 @@ APP_HDRS=\ src/main.h \ + src/util.h \ src/ui_viewtransactions.h \ src/ui_mainwindow.h \ src/ui_editperson.h \ @@ -36,6 +37,7 @@ APP_OBJS=\ main.o \ + util.o \ editperson.o \ viewtransactions.o \ aboutwindow.o \ diff -r f1b41bdabe12 -r 43a5e09bb832 src/editperson.cpp --- a/src/editperson.cpp Tue May 08 00:44:17 2018 +0300 +++ b/src/editperson.cpp Tue May 08 13:14:29 2018 +0300 @@ -7,6 +7,7 @@ // included file "COPYING" for exact terms. // #include "main.h" +#include "util.h" #include "ui_editperson.h" diff -r f1b41bdabe12 -r 43a5e09bb832 src/main.cpp --- a/src/main.cpp Tue May 08 00:44:17 2018 +0300 +++ b/src/main.cpp Tue May 08 13:14:29 2018 +0300 @@ -14,7 +14,7 @@ #include "ui_mainwindow.h" #include "ui_aboutwindow.h" #include "runguard.h" - +#include "util.h" // @@ -56,103 +56,6 @@ // -// Convert QString to a double value, replacing comma -// -double slMoneyStrToValue(const QString &str) -{ - QString str2 = str; - return str2.replace(",", ".").toDouble(); -} - - -// -// Convert double value to formatted QString -// -QString slMoneyValueToStr(double val) -{ - return QStringLiteral("%1").arg(val, 1, 'f', 2); -} - - -QString slMoneyValueToStrSign(double val) -{ - return QStringLiteral("%1%2"). - arg(val > 0 ? "+" : ""). - arg(val, 1, 'f', 2); -} - - -// -// Trim and cleanup given QString (removing double whitespace etc.) -// -QString slCleanupStr(const QString &str) -{ - return str.simplified().trimmed(); -} - - -// -// Manipulate given QDateTime value to get desired -// correct timestamp. -// -const QDateTime slDateTimeToLocal(const QDateTime &val) -{ - QDateTime tmp = val; - tmp.setOffsetFromUtc(0); - return tmp.toLocalTime(); -} - - -// -// Return a string representation of given QDateTime -// converted to local time. -// -const QString slDateTimeToStr(const QDateTime &val) -{ - return slDateTimeToLocal(val).toString(QStringLiteral("yyyy-MM-dd hh:mm")); -} - - -// -// Error logging -// -void slLog(const QString &mtype, const QString &msg) -{ - QString filename = settings.dataPath + QDir::separator() + APP_LOG_FILE; - QFile fh(filename); - if (fh.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text)) - { - QTextStream out(&fh); - out << - slDateTimeToLocal(QDateTime::currentDateTimeUtc()). - toString(QStringLiteral("yyyy-MM-dd hh:mm:ss")) - << " [" << mtype << "]: " << msg << "\n"; - fh.close(); - } -} - - -// -// Display an error dialog with given title and message -// -int slErrorMsg(const QString &title, const QString &msg) -{ - QMessageBox dlg; - - slLog("ERROR", msg); - - dlg.setText(title); - dlg.setInformativeText(msg); - dlg.setTextFormat(Qt::RichText); - dlg.setIcon(QMessageBox::Critical); - dlg.setStandardButtons(QMessageBox::Ok); - dlg.setDefaultButton(QMessageBox::Ok); - - return dlg.exec(); -} - - -// // Check if an SQL error has occured (for given QSqlError) and // report it to stdout if so. Return "false" if error has occured, // true otherwise. diff -r f1b41bdabe12 -r 43a5e09bb832 src/main.h --- a/src/main.h Tue May 08 00:44:17 2018 +0300 +++ b/src/main.h Tue May 08 13:14:29 2018 +0300 @@ -113,17 +113,6 @@ // // Global functions // -double slMoneyStrToValue(const QString &str); -QString slMoneyValueToStr(double val); -QString slMoneyValueToStrSign(double val); -QString slCleanupStr(const QString &str); -const QDateTime slDateTimeToLocal(const QDateTime &val); -const QString slDateTimeToStr(const QDateTime &val); - -void slLog(const QString &mtype, const QString &msg); -int slErrorMsg(const QString &title, const QString &msg); -bool slCheckAndReportSQLError(const QString where, const QSqlError &err, bool report = false); - void slGetPersonInfoRec(QSqlQuery &query, SLPersonInfo &info); bool slGetPersonInfo(qint64 id, SLPersonInfo &info); void slSetCommonStyleSheet(QWidget *widget); diff -r f1b41bdabe12 -r 43a5e09bb832 src/printing.cpp --- a/src/printing.cpp Tue May 08 00:44:17 2018 +0300 +++ b/src/printing.cpp Tue May 08 13:14:29 2018 +0300 @@ -9,6 +9,8 @@ #include #include #include "main.h" +#include "util.h" + #define APP_PRINT_WIDTH 1000 #define APP_PRINT_HEIGHT 1300 diff -r f1b41bdabe12 -r 43a5e09bb832 src/sqlmodels.cpp --- a/src/sqlmodels.cpp Tue May 08 00:44:17 2018 +0300 +++ b/src/sqlmodels.cpp Tue May 08 13:14:29 2018 +0300 @@ -7,6 +7,7 @@ // included file "COPYING" for exact terms. // #include "main.h" +#include "util.h" SLPersonSQLModel::SLPersonSQLModel(QObject *parent) : QSqlQueryModel(parent) diff -r f1b41bdabe12 -r 43a5e09bb832 src/util.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/util.cpp Tue May 08 13:14:29 2018 +0300 @@ -0,0 +1,111 @@ +// +// Syntilista - debt list/management database program +// Programmed and designed by Matti Hämäläinen +// (C) Copyright 2017-2018 Tecnic Software productions (TNSP) +// +// Distributed under 3-clause BSD style license, refer to +// included file "COPYING" for exact terms. +// +#include "main.h" +#include "util.h" +#include + + +// +// Convert QString to a double value, replacing comma +// +double slMoneyStrToValue(const QString &str) +{ + QString str2 = str; + return str2.replace(",", ".").toDouble(); +} + + +// +// Convert double value to formatted QString +// +QString slMoneyValueToStr(double val) +{ + return QStringLiteral("%1").arg(val, 1, 'f', 2); +} + + +QString slMoneyValueToStrSign(double val) +{ + return QStringLiteral("%1%2"). + arg(val > 0 ? "+" : ""). + arg(val, 1, 'f', 2); +} + + +// +// Trim and cleanup given QString (removing double whitespace etc.) +// +QString slCleanupStr(const QString &str) +{ + return str.simplified().trimmed(); +} + + +// +// Manipulate given QDateTime value to get desired +// correct timestamp. +// +const QDateTime slDateTimeToLocal(const QDateTime &val) +{ + QDateTime tmp = val; + tmp.setOffsetFromUtc(0); + return tmp.toLocalTime(); +} + + +// +// Return a string representation of given QDateTime +// converted to local time. +// +const QString slDateTimeToStr(const QDateTime &val) +{ +// return slDateTimeToLocal(val).toString(QStringLiteral("yyyy-MM-dd hh:mm")); + return slDateTimeToLocal(val).toString(QStringLiteral("dd.MM.yyyy hh:mm")); +} + + +// +// Error logging +// +void slLog(const QString &mtype, const QString &msg) +{ + QString filename = settings.dataPath + QDir::separator() + APP_LOG_FILE; + QFile fh(filename); + if (fh.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text)) + { + QTextStream out(&fh); + out << + slDateTimeToLocal(QDateTime::currentDateTimeUtc()). + toString(QStringLiteral("yyyy-MM-dd hh:mm:ss")) + << " [" << mtype << "]: " << msg << "\n"; + fh.close(); + } +} + + +// +// Display an error dialog with given title and message +// +int slErrorMsg(const QString &title, const QString &msg) +{ + QMessageBox dlg; + + slLog("ERROR", msg); + + dlg.setText(title); + dlg.setInformativeText(msg); + dlg.setTextFormat(Qt::RichText); + dlg.setIcon(QMessageBox::Critical); + dlg.setStandardButtons(QMessageBox::Ok); + dlg.setDefaultButton(QMessageBox::Ok); + + return dlg.exec(); +} + + diff -r f1b41bdabe12 -r 43a5e09bb832 src/util.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/util.h Tue May 08 13:14:29 2018 +0300 @@ -0,0 +1,24 @@ +// +// Syntilista - debt list/management database program +// Programmed and designed by Matti Hämäläinen +// (C) Copyright 2017-2018 Tecnic Software productions (TNSP) +// +// Distributed under 3-clause BSD style license, refer to +// included file "COPYING" for exact terms. +// +#ifndef SL_UTIL_H +#define SL_UTIL_H + +double slMoneyStrToValue(const QString &str); +QString slMoneyValueToStr(double val); +QString slMoneyValueToStrSign(double val); +QString slCleanupStr(const QString &str); +const QDateTime slDateTimeToLocal(const QDateTime &val); +const QString slDateTimeToStr(const QDateTime &val); + +void slLog(const QString &mtype, const QString &msg); +int slErrorMsg(const QString &title, const QString &msg); +bool slCheckAndReportSQLError(const QString where, const QSqlError &err, bool report = false); + + +#endif // SL_UTIL_H diff -r f1b41bdabe12 -r 43a5e09bb832 src/viewtransactions.cpp --- a/src/viewtransactions.cpp Tue May 08 00:44:17 2018 +0300 +++ b/src/viewtransactions.cpp Tue May 08 13:14:29 2018 +0300 @@ -7,6 +7,7 @@ // included file "COPYING" for exact terms. // #include "main.h" +#include "util.h" #include "ui_viewtransactions.h"