-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.cpp
More file actions
319 lines (269 loc) · 8.37 KB
/
Utils.cpp
File metadata and controls
319 lines (269 loc) · 8.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
* Utils.cpp
*
* Created on: 27 Jan 2012
* Author: Max Foster
*/
#include <iostream>
#include <fstream>
#include <stdexcept>
#include <algorithm>
#include <ctime>
#include <cmath>
using namespace std;
#include <QString>
#include <QDateTime>
#include <QMessageBox>
#include <QFile>
#include <QDir>
#include "Utils.h"
#include "dialogs/utils/PendingDialog.h"
#include "dialogs/utils/MoveFileExistsDialog.h"
#include "dialogs/utils/DatePickerDialog.h"
Date::Date(const time_t seconds_)
{
tm *time = gmtime(&seconds_);
seconds = time->tm_sec;
minute = time->tm_min;
hour = time->tm_hour;
day = time->tm_mday;
month = time->tm_mon + 1;
year = time->tm_year + 1900;
}
Date::Date(unsigned minute, unsigned hour, unsigned day, unsigned month, unsigned year)
: seconds(0), minute(minute), hour(hour), day(day), month(month), year(year) {}
Date::Date(const QDateTime &qDateTime)
: seconds(0), minute(qDateTime.time().minute()), hour(qDateTime.time().hour()),
day(qDateTime.date().day()), month(qDateTime.date().month()), year(qDateTime.date().year()) {}
Date::Date(const QDate &qDate)
: seconds(0), minute(0), hour(0), day(qDate.day()), month(qDate.month()), year(qDate.year()) {}
Date::operator std::string() const
{
stringstream stream;
stream << *this;
return stream.str();
}
Date::operator QString() const
{
stringstream stream;
stream << *this;
return stream.str().c_str();
}
Date::operator QDateTime() const
{
return QDateTime(QDate(year, month, day), QTime(hour, minute), Qt::UTC);
}
Date::operator QDate() const
{
return QDate(year, month, day);
}
Date::operator time_t() const
{
time_t t = 0;
tm *time = gmtime(&t);
time->tm_sec = seconds;
time->tm_min = minute;
time->tm_hour = hour;
time->tm_mday = day;
time->tm_mon = month - 1;
time->tm_year = year - 1900;
return mktime(time);
}
QString Date::toQStringWithoutTime() const
{
stringstream stream;
stream << day << "/" << month << "/" << year;
return stream.str().c_str();
}
string to2Digits(const unsigned num)
{
if (num < 10) return '0' + toString(num);
return toString(num);
}
ostream & operator <<(ostream &stream, const Date &date)
{
stream << date.day << "/" << date.month << "/" << date.year
<< " - " << to2Digits(date.hour) << ":" << to2Digits(date.minute);
return stream;
}
string lowerCase(const string &str)
{
string returnStr = str;
transform(returnStr.begin(), returnStr.end(), returnStr.begin(), ::tolower);
return returnStr;
}
void replaceChars(string &str, const char searchChar, const char newChar)
{
size_t spacePos;
while ((spacePos = str.find(searchChar)) != str.npos) str[spacePos] = newChar;
}
const char * limitLength(const char *str, unsigned maxLength)
{
static const unsigned stringSize = 1024, elipsisLength = 3;
static char string[stringSize + 1];
if (maxLength > stringSize) maxLength = stringSize;
strncpy(string, str, maxLength - elipsisLength);
if (strlen(str) > maxLength - elipsisLength) strcat(string, "...");
string[maxLength] = '\0';
return string;
}
void showInfoDialog(const char *message)
{
QMessageBox(QMessageBox::Information, " ", message).exec();
}
void showErrorDialog(const char *message)
{
QMessageBox(QMessageBox::Warning, "Error", message).exec();
}
void showErrorDialog(const vector<string> &errors)
{
string errorMessage;
errorMessage.reserve(1024);
for (unsigned i = 0; i < errors.size(); ++i) errorMessage += errors[i];
showErrorDialog(errorMessage.c_str());
}
void showFatalDialog(const char *message)
{
QMessageBox(QMessageBox::Critical, "Fatal Error", message).exec();
}
bool showYesNoDialog(const char *question)
{
QMessageBox messageBox(QMessageBox::Question, " ", question, QMessageBox::Yes | QMessageBox::No);
messageBox.setDefaultButton(QMessageBox::No);
switch (messageBox.exec())
{
case QMessageBox::Yes: return true;
default: return false;
}
}
void showPendingDialog(const char *message, int (*percentCompleteCheckFunction)(void))
{
PendingDialog(message, percentCompleteCheckFunction).exec();
}
bool showDatePickerDialog(QDate &dateToModify)
{
if (DatePickerDialog(dateToModify).exec() == DatePickerDialog::Accepted) return true;
return false;
}
const char * createFullName(const char *forename, const char *surname)
{
static char name[1024];
strcpy(name, forename);
strcat(name, " ");
strcat(name, surname);
return name;
}
void addError(vector<string> &errors, string error)
{
error += '\n';
// Add the error to the list if it isn't already found in the list
bool alreadyAdded = false;
for (unsigned i = 0; i < errors.size(); ++i)
{
if (errors[i] == error)
{
alreadyAdded = true;
break;
}
}
if (!alreadyAdded) errors.push_back(error);
}
bool validateLengthOf(const char *value, const int min, const int max, const string &valueName, string &errorMessage)
{
const int length = strlen(value);
const bool isValid = (length >= min) && (length <= max);
if (!isValid)
errorMessage = valueName + " must be between " + toString(min) + " and " + toString(max) + " characters";
return isValid;
}
bool validateLengthOf(const char *value, const int max, const string &valueName, string &errorMessage)
{
const int length = strlen(value);
const bool isValid = (length <= max);
if (!isValid) errorMessage = valueName + " cannot be longer than " + toString(max) + " characters";
return isValid;
}
void copyFile(const char *sourceFilename, const char *destinationFilename)
{
if (QFile::exists(destinationFilename)) QFile::remove(destinationFilename);
QFile::copy(sourceFilename, destinationFilename);
}
void moveFile(const char *sourceFilename, const char *destinationFilename)
{
if (QFile::exists(destinationFilename))
{
switch (MoveFileExistsDialog(sourceFilename, destinationFilename).exec())
{
case MoveFileExistsDialog::MOVE_AND_REPLACE:
QFile::remove(destinationFilename);
QFile::copy(sourceFilename, destinationFilename);
remove(sourceFilename);
break;
case MoveFileExistsDialog::DELETE_AND_KEEP:
remove(sourceFilename);
break;
default: break;
}
}
else
{
QFile::copy(sourceFilename, destinationFilename);
remove(sourceFilename);
}
}
void moveDirectory(const char *sourceDirectoryPath, const char *destinationDirectoryPath)
{
#ifdef _WIN32
const char slashChar = '\\';
#else
const char slashChar = '/';
#endif
QDir dir, sourceDirectory(sourceDirectoryPath);
if (!sourceDirectory.exists()) return;
dir.mkpath(destinationDirectoryPath);
QStringList entryList = sourceDirectory.entryList();
for (int i = 0; i < entryList.size(); ++i)
{
QString &entry = entryList[i];
if ((entry.length() == 0) || (entry[0] == QChar('.'))) continue;
string source = string(sourceDirectoryPath) + slashChar + entry.toStdString(),
destination = string(destinationDirectoryPath) + slashChar + entry.toStdString();
if (QDir(source.c_str()).exists()) moveDirectory(source.c_str(), destination.c_str());
else
{
copyFile(source.c_str(), destination.c_str());
remove(source.c_str());
}
}
dir.rmdir(sourceDirectoryPath);
}
QString to2Dp(const QString &str)
{
const int pointIndex = str.lastIndexOf('.');
if (pointIndex < 0) return str + ".00";
switch (str.length() - pointIndex)
{
case 1: return str + "00";
case 2: return str + "0";
case 3: return str;
default:
{
QString decimals = '0' + str.mid(pointIndex, str.length() - pointIndex);
const double roundedDecimals = round(strtod(decimals.toStdString().c_str(), NULL) * 100.0) / 100.0;
QString decimalString = toString(roundedDecimals).c_str();
switch (decimalString.length())
{
case 0: decimalString += "0.00"; break;
case 1: decimalString += ".00"; break;
case 2: decimalString += "00"; break;
case 3: decimalString += "0"; break;
default: break;
}
return str.left(pointIndex) + decimalString.right(3);
}
}
}
double doubleTo2Dp(const double number)
{
return strtod(to2Dp(toString(number).c_str()).toStdString().c_str(), NULL);
}