-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPotaUtils.cpp
173 lines (154 loc) · 4.2 KB
/
PotaUtils.cpp
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
#include "qapplication.h"
#include "qcolor.h"
#include "qlabel.h"
#include "qsqlerror.h"
#include "qsqlquery.h"
#include "PotaUtils.h"
bool PotaQuery::ExecShowErr(QString query)
{
exec(query);
if (lastError().type() != QSqlError::NoError)
{
if (lErr!=nullptr)
SetColoredText(lErr,lastError().text(),"Err");
qDebug() << lastError().text();
return false;
}
return true;
}
bool PotaQuery::ExecMultiShowErr(QString querys, QString spliter)
{
QStringList QueryList = querys.split(spliter);
QString s;
s=lErr->text();
if ((s.length()>11)and(s.last(11)==" statements"))
s=s.first(s.length()-11)+"+"+str(QueryList.count())+" statements";
else
s=str(QueryList.count())+" statements";
SetColoredText(lErr,s,"Info");
for (int i=0;i<QueryList.count();i++)
{
if (QueryList[i].trimmed().isEmpty())
continue;
QString sQuery=RemoveComment(QueryList[i].trimmed(),"--");
ExecShowErr(sQuery);
if (lastError().type() != QSqlError::NoError)
{
if (lErr!=nullptr)
SetColoredText(lErr,QueryList[i].trimmed()+"\n"+lastError().text()+"\n"+
DBInfo(),"Err");
return false;
}
}
return true;
}
QVariant PotaQuery::Selec0ShowErr(QString query)
{
clear();
QVariant vNull;
if (ExecShowErr(query))
{
next();
return value(0);
}
else
return vNull;
}
QString DataType(QString TableName, QString FieldName){
QSqlQuery query("PRAGMA table_xinfo("+TableName+")");
while (query.next()){
if (query.value(1).toString()==FieldName)
return query.value(2).toString();
}
return "";
}
// QString SQLiteDate() {
// return QDate::currentDate().toString("yyyy-MM-dd");
// }
QString DBInfo()
{
QSqlDatabase db = QSqlDatabase::database();
QString sResult;
sResult=db.databaseName()+" - "+db.driverName()+" - "+db.connectOptions()+" : ";
sResult.append(iif(db.isValid(),"VALID DRIVER, ","").toString());
sResult.append(iif(db.isOpen(),"DB IS OPEN, ","").toString());
sResult.append(iif(db.isOpenError(),"OPENERROR, ","").toString());
sResult.truncate(sResult.length()-2);
return sResult;
}
QVariant iif(bool bCond,QVariant Var1,QVariant Var2)
{
if (bCond)
return Var1;
else
return Var2;
}
bool isDarkTheme() {
QColor backgroundColor = QApplication::palette().color(QPalette::Window);
return backgroundColor.lightness() < 128; // Valeur arbitraire pour détecter un thème sombre
}
QString RemoveComment(QString sCde, QString sCommentMarker)
{
QStringList LinesList = sCde.split("\n");
QString s;
QString sResult="";
int index;
for (int i=0;i<LinesList.count();i++)
{
s = LinesList[i];
index = s.indexOf(sCommentMarker);
if (index!=-1)
sResult += s.first(index).trimmed()+iif(s.first(index).trimmed().isEmpty(),""," ").toString();
else
sResult += s.trimmed()+iif(s.trimmed().isEmpty(),""," ").toString();
}
return sResult;
}
void SetColoredText(QLabel *l, QString text, QString type)
{
QString s=text;
if (text.length()>400)
s=text.first(200)+"...\n..."+text.last(400);
l->setText(s);
QPalette p = l->palette();
if (type=="Err")
p.setColor(QPalette::WindowText, "red");
else if (type=="Ok")
p.setColor(QPalette::WindowText, "green");
else if (type=="Info")
p.setColor(QPalette::WindowText, QColor("#9e5000"));
else
p.setColor(QPalette::WindowText, "white");
l->setPalette(p);
}
void SetButtonSize(QToolButton *b)
{
b->setFixedSize(24,24);
b->setIconSize(QSize(24,24));
}
QString str(int i)
{
QString s;
s.setNum(i);
return s;
}
QString str(float i)
{
QString s;
s.setNum(i);
return s;
}
QString str(qsizetype i)
{
QString s;
s.setNum(i);
return s;
}
QString StrReplace(QString s, const QString sTarg, const QString sRepl) {
int index = 0;
while ((index = s.indexOf(sTarg, index)) != -1) {
s.replace(index, sTarg.length(), sRepl);
index += sRepl.length(); // To avoid infinite loop
}
return s;
}