1 // Copyright (C) 2020 by Yuri Victorovich. All rights reserved.
2 
3 #pragma once
4 
5 #include <string>
6 #include <QString>
7 #include <QPoint>
8 #include <QPixmap>
9 #include <QStringList>
10 #include <QImage>
11 #include <QByteArray>
12 #include <QSize>
13 #include <QPainter>
14 class QComboBox;
15 
16 #include <string>
17 #include <vector>
18 #include <ostream>
19 #include <sstream>
20 #include <tuple>
21 #include <algorithm>
22 
23 class QWidget;
24 
25 namespace Util {
26 
27 std::string QStringToStlString(const QString &qs);
28 bool warningOk(QWidget *parent, const QString &msg);
29 float getScreenDPI();
30 QPoint getGlobalMousePos();
31 std::string formatUIntHumanReadable(size_t u);
32 std::string formatUIntHumanReadableSuffixed(size_t u);
33 std::string formatFlops(size_t flops);
34 template<typename T>
arrayMinMax(const T * arr,size_t len)35 std::tuple<T,T> arrayMinMax(const T *arr, size_t len) {
36 	T amin = std::numeric_limits<T>::max();
37 	T amax = std::numeric_limits<T>::lowest();
38 	for (const T *ae = arr + len; arr < ae; arr++) {
39 		if (*arr < amin)
40 			amin = *arr;
41 		if (*arr > amax)
42 			amax = *arr;
43 	}
44 	return std::tuple<T,T>(amin, amax);
45 }
46 
47 float* copyFpArray(const float *a, size_t sz);
48 size_t getFileSize(const QString &fileName);
49 QPixmap getScreenshot(bool hideOurWindows);
50 unsigned char* convertArrayFloatToUInt8(const float *a, size_t size);
51 bool doesFileExist(const char *filePath);
52 QStringList readListFromFile(const char *fileName);
53 std::string getMyOwnExecutablePath();
54 QImage svgToImage(const QByteArray& svgContent, const QSize& size, QPainter::CompositionMode mode);
55 void selectComboBoxItemWithItemData(QComboBox &comboBox, int value);
56 void setWidgetColor(QWidget *widget, const char *color);
57 std::string charToSubscript(char ch);
58 std::string stringToSubscript(const std::string &str);
59 
60 template<typename T>
isValueIn(const std::vector<T> & v,T val)61 bool isValueIn(const std::vector<T> &v, T val) {
62 	return std::find(v.begin(), v.end(), val) != v.end();
63 }
64 
65 inline void splitString(const std::string& str, std::vector<std::string> &cont, char delim = ' ') {
66 	std::stringstream ss(str);
67 	std::string token;
68 	while (std::getline(ss, token, delim))
69 		cont.push_back(token);
70 }
71 
72 template <class Container, class Conv>
73 inline void splitString(const std::string& str, Container &cont, char delim = ' ') {
74 	std::stringstream ss(str);
75 	std::string token;
76 	while (std::getline(ss, token, delim))
77 		cont.push_back(Conv::conv(token));
78 }
79 
80 };
81 
82 #define Q2S(qs) Util::QStringToStlString(qs)
83 #define S2Q(ss) QString(ss.c_str())
84 
85 // general templates
86 
87 template<typename T, unsigned long N>
88 inline std::ostream& operator<<(std::ostream &os, const std::array<T,N> &c) {
89 	os << "[";
90 	unsigned idx = 0;
91 	for (auto &e : c) {
92 		if (idx++ != 0)
93 			os << ',';
94 		os << e;
95 	}
96 	os << "]";
97 	return os;
98 }
99 
100 template<typename T>
101 inline std::ostream& operator<<(std::ostream &os, const std::vector<T> &c) {
102 	os << "[";
103 	unsigned idx = 0;
104 	for (auto &e : c) {
105 		if (idx++ != 0)
106 			os << ',';
107 		os << e;
108 	}
109 	os << "]";
110 	return os;
111 }
112 
113 // silence unwanted warnings about unused variables
114 #define UNUSED(expr) do { (void)(expr); } while (0);
115