-
Notifications
You must be signed in to change notification settings - Fork 0
/
funcData_Button.h
307 lines (248 loc) · 7.35 KB
/
funcData_Button.h
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
#ifndef FUNCDATA_BUTTON_H
#define FUNCDATA_BUTTON_H
#include <QTableWidget>
#include <QJsonObject>
#include <QJsonDocument>
#include <QJsonArray>
#include <QString>
#include <QDebug>
#include <QPushButton>
#include <QMouseEvent>
#include <utility>
#include <string>
#include "DynamicLib.h"
bool check_funcList(const QString& func);
QString getRefType(const QString &type);
class func_Data
{
private:
QString declare, name, typeRef;
QStringList paramList;
QVariantList argsList, fileList;
void *ptr{};
public:
func_Data(QString func_declare, const QJsonValue &func_argsList);
const QString &getDeclare() const
{
return declare;
}
const QString &getName() const
{
return name;
}
const QVariantList &getArgsList() const
{
return argsList;
}
const QVariantList &getfileList() const
{
return fileList;
}
const QStringList &getParamList() const
{
return paramList;
}
const QString &getTypeRef() const
{
return typeRef;
}
void display2table(QTableWidget *table) const;
static void display2table(const func_Data *func, QTableWidget *table)
{
func->display2table(table);
}
static QString getTableName(QTableWidget *table)
{
return table->horizontalHeaderItem(0)->text();
}
void setPtr(void *func_ptr)
{
this->ptr = func_ptr;
}
QVariantList loadFile() const;
QString call(bool isGBK = false) const;
void loadArgs(QTableWidget *table);
};
QT_BEGIN_NAMESPACE
class funcData_Button;
QT_END_NAMESPACE
class funcData_Button : public QPushButton
{
Q_OBJECT
func_Data *func;
public:
explicit funcData_Button(QWidget *parent = nullptr) : QPushButton(parent), func(nullptr) {}
// 这两个构造必须初始化func,不然析构时会崩溃
explicit funcData_Button(const QString &text, QWidget *parent = nullptr) : QPushButton(text, parent), func(nullptr) {}
// 这个构造里的init_func已初始化func
explicit funcData_Button(const QString &func_declare, const QJsonValue &func_argsList, QWidget *parent = nullptr)
: QPushButton(init_func(func_declare, func_argsList), parent) {}
~funcData_Button() override
{
delete func;
}
func_Data *func_data() const
{
return func;
}
int getTextWidth()
{
// 使用 QFontMetrics 计算文本的宽度
QFontMetrics fontMetrics(this->font());
return fontMetrics.width(this->text());
}
void setMaximum_Width_Height(int width, int height)
{
this->setMaximumWidth(width);
this->setMaximumHeight(height);
}
void autoResize()
{
// this->setMaximum_Width_Height(this->getTextWidth() + 20, 31);
this->setSizeIncrement(this->sizeHint());
}
static void autoResize(QPushButton *pb)
{
QFontMetrics fontMetrics(pb->font());
int width = fontMetrics.width(pb->text());
pb->setMaximumWidth(width);
}
const QString &init_func(const QString &func_declare, const QJsonValue &func_argsList)
{
func = new func_Data(func_declare, func_argsList);
return func->getName();
}
void init_func(func_Data *funcData)
{
this->func = funcData;
}
bool func_loadLib(DynamicLib *lib)
{
QString name = func->getName();
void *ptr = lib->loadFunc<void *>(name.toStdString().c_str());
if (ptr)
{
func->setPtr(ptr);
return true;
}
else
return false;
}
std::tuple<QString, QJsonObject> getJsonObj()
{
QJsonObject args;
args["args"] = QJsonArray::fromVariantList(func->getArgsList());
if (!func->getfileList().isEmpty())
args["file"] = QJsonArray::fromVariantList(func->getfileList());
return std::make_tuple(func->getDeclare(), args);
}
QString getDefParsing() const
{
/*
static QStringList i_F_pc_pc_pc_i_i_(void *func_ptr, const QVariantList &args)
{
if (args.size() != 5)
throw std::runtime_error("args.size error");
using i_F_pc_pc_pc_i_i = int (*)(char *, char *, char *, int, int); def
char *arg1 = tran(args[0]);
char *arg2 = tran(args[1]);
char *arg3 = tran(args[2]); parsing
int arg4 = args[3].toInt();
int arg5 = args[4].toInt();
int retCode = i_F_pc_pc_pc_i_i(func_ptr)(arg1, arg2, arg3, arg4, arg5); call
QString tmp1{arg1};
delete[] arg1;
QString tmp2{arg2}; save&clear
delete[] arg2;
QString tmp3{arg3};
delete[] arg3;
return {QString::number(retCode), tmp1, tmp2, tmp3}; ret
}
*/
QString func_def{"static QStringList "};
auto typeRef = func->getTypeRef();
func_def += typeRef + "_(void *func_ptr, const QVariantList &args)\n{\n\t";
QString def{"using "};
def += typeRef + " = ";
auto retType = getRefType(QString(typeRef[0]));
def += retType + " (*)(";
QString call, ret{"return {"};
if (typeRef[0] == 'i')
{
call = "int retCode = ";
ret += "QString::number(retCode), ";
}
call += "(" + typeRef + "(func_ptr))(";
QString parsing, saveClear;
int saveNums{0};
auto paramType = typeRef.mid(typeRef.indexOf('F') + 2).split('_');
for (int i{0}; i < paramType.length(); i++)
{
auto refType{getRefType(paramType[i])};
def += refType + ", ";
parsing += refType + " arg" + QString::number(i+1) + " = ";
if (paramType[i] == "i")
parsing += "args[" + QString::number(i) + "].toInt();";
else if (paramType[i] == "pi")
parsing += "new int(args[" + QString::number(i) + "].toInt());";
else if (paramType[i] == "d")
parsing += "args[" + QString::number(i) + "].toDouble();";
else if (paramType[i] == "pd")
parsing += "new double(args[" + QString::number(i) + "].toDouble());";
else if (paramType[i] == "c")
parsing += "args[" + QString::number(i) + "].toChar();";
else if (paramType[i] == "pc" || paramType[i] == "str")
parsing += "tran(args[" + QString::number(i) + "]);";
else if (paramType[i] == "uc")
parsing += "static_cast<unsigned char>(args[" + QString::number(i) + "].toChar());";
else if (paramType[i] == "puc" || paramType[i] == "ustr")
parsing += "reinterpret_cast<unsigned char *>(tran(args[" + QString::number(i) + "]));";
parsing += "\n\t";
call += "arg" + QString::number(i+1) + ", ";
if (refType.contains("*")) // 有指针情况
{
if (!refType.contains("const"))
{
saveNums++;
if (paramType[i].contains("u"))
saveClear += "QString tmp" + QString::number(saveNums) + "{(char*)arg" + QString::number(i + 1) + "};\n\t";
else if (paramType[i].contains("c"))
saveClear += "QString tmp" + QString::number(saveNums) + "{arg" + QString::number(i + 1) + "};\n\t";
else
saveClear += "QString tmp" + QString::number(saveNums) + "{QString::number(*arg" + QString::number(i + 1) + ")};\n\t";
ret += "tmp" + QString::number(saveNums) + ", ";
}
if (refType.contains("char"))
saveClear += "delete[] arg" + QString::number(i+1) + ";\n\t";
else
saveClear += "delete arg" + QString::number(i+1) + ";\n\t";
}
}
def.replace(def.length() - 2, 2, ");\n\n\t");
call.replace(call.length() - 2, 2, ");\n\n\t");
ret.replace(ret.length() - 2, 2, "};\n}\n");
QString check{"if (args.size() != " + QString::number(paramType.length()) + ")\n\t\t"};
check += "throw std::runtime_error(\"args.size error\");\n\t";
return {func_def + check + def + parsing + call + saveClear + ret};
}
signals:
void leftClicked(func_Data *);
void rightClicked(func_Data *);
protected:
void mousePressEvent(QMouseEvent *event) override
{
if (event->button() == Qt::RightButton)
{
emit rightClicked(this->func);
}
else if (event->button() == Qt::LeftButton)
{
emit leftClicked(this->func);
}
else
{
QPushButton::mousePressEvent(event);
}
}
};
#endif // FUNCDATA_BUTTON_H