-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwidget.cpp
303 lines (214 loc) · 11.9 KB
/
widget.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
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
#include "widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
setWindowTitle("Simple QuakeEngine HTML generator");
resize(700, 340);
QPushButton *generateButton = new QPushButton("Generate");
QPushButton *aboutButton = new QPushButton("About");
resultText = new QPlainTextEdit();
QLabel *label_1 = new QLabel("Source Repo URL:");
QLabel *label_2 = new QLabel("Target Repo URL:");
QLabel *label_3 = new QLabel("Official Website:");
QLabel *label_4 = new QLabel("Official Discord URL:");
QLabel *label_5 = new QLabel("Official Discord Name:");
QLabel *label_6 = new QLabel("Repo Description");
QLabel *label_7 = new QLabel("Resulting HTML Code:");
QLabel *label_8 = new QLabel(" Target organization:");
lineEdit_1 = new QLineEdit();
lineEdit_2 = new QLineEdit();
lineEdit_3 = new QLineEdit();
lineEdit_4 = new QLineEdit();
lineEdit_5 = new QLineEdit();
lineEdit_6 = new QLineEdit();
orgList = new QComboBox();
orgList->addItem("QuakeEngines");
orgList->addItem("QuakeMods");
orgList->addItem("QuakeTools");
orgList->addItem("HLSources");
orgList->addItem("SourceSources");
lineEdit_2->setPlaceholderText("Leave blank if repo name remains unchanged"); // for Target Repo URL
lineEdit_3->setPlaceholderText("If exists"); // for Official Website
lineEdit_4->setPlaceholderText("If exists"); // for Official Discord URL
QGridLayout *layout = new QGridLayout;
layout->addWidget(label_1, 0, 0);
layout->addWidget(label_2, 1, 0);// Target Repo URL (one one row, 0th col)
layout->addWidget(label_8, 1, 2); // Target org. (same row , 2nd col)
layout->addWidget(label_3, 2, 0);
layout->addWidget(label_4, 3, 0); // Discord URL (on one row , 0th col)
layout->addWidget(label_5, 3, 2); // Discord Name (same row , 2nd col)
layout->addWidget(label_6, 4, 0);
layout->addWidget(label_7, 5, 0);
layout->addWidget(aboutButton, 6, 0); //
layout->addWidget(lineEdit_1, 0, 1, 1 ,3);
layout->addWidget(lineEdit_2, 1, 1, 1 ,1); // Target Repo field (one one row, 1st col)
layout->addWidget(orgList, 1, 3, 1 ,1); // Target org. field (same row , 3rd col)
layout->addWidget(lineEdit_3, 2, 1, 1 ,3);
layout->addWidget(lineEdit_4, 3, 1, 1, 1); // Discord URL field (on one row, 1st col)
layout->addWidget(lineEdit_5, 3, 3, 1, 1); // Discord URL (same row, 3rd col)
layout->addWidget(lineEdit_6, 4, 1, 1 ,3);
layout->addWidget(resultText, 5, 1, 1 ,3);
layout->addWidget(generateButton, 6, 1, 1, 3);
setLayout(layout);
// make enter key work within input fields
connect(lineEdit_1, SIGNAL(returnPressed()), generateButton, SIGNAL(clicked()));
connect(lineEdit_2, SIGNAL(returnPressed()), generateButton, SIGNAL(clicked()));
connect(lineEdit_3, SIGNAL(returnPressed()), generateButton, SIGNAL(clicked()));
connect(lineEdit_4, SIGNAL(returnPressed()), generateButton, SIGNAL(clicked()));
connect(lineEdit_5, SIGNAL(returnPressed()), generateButton, SIGNAL(clicked()));
connect(lineEdit_6, SIGNAL(returnPressed()), generateButton, SIGNAL(clicked()));
connect(generateButton, SIGNAL(clicked()), this, SLOT(generateHTML()));
connect(aboutButton, SIGNAL(clicked()), this, SLOT(showAboutDialog()));
}
Widget::~Widget()
{
}
void Widget::generateHTML()
{
QString githubURL = "https://github.com/";
QString sourceforgeURL = "https://sourceforge.net/p/";
QString gitlabURL = "https://gitlab.com/";
QString sourceRepo = lineEdit_1->text();
QString targetRepo = lineEdit_2->text();
QString officialSite = lineEdit_3->text();
QString discordURL = lineEdit_4->text();
QString discordName = lineEdit_5->text();
QString descriptionText = lineEdit_6->text();
QString orgName = orgList->currentText();
QString shortSourceRepo = sourceRepo; // same by default
QString shortTargetRepo = targetRepo;
QString shortOfficialSite = officialSite;
QString repoName, newRepoName, gitTags, sourceforgeTag, gitlabTag, resultHTML;
QRegExp rx1(".*/(.*)$"); // regexp for getting repo name from relative path
QString HTMLBeginning = "<div class=\"item\">\n<h3><a href=\"@01\">@02</a> @03[@04]</h3>\n";
QString HTMLGithub = "<span class=\"subtext\">Mirrors:\n<a href=\"@05\">@06</a>,\n"
"<a href=\"https://techgaun.github.io/active-forks/index.html#@04\">Other forks</a>\n</span><br>\n";
QString HTMLOtherGit = "<span class=\"subtext\">Unofficial mirror on github: \n<a href=\"@05\">@06</a></span><br>\n";
QString HTMLOfficialSite = "<span class=\"subtext\">Official website: <a href=\"@07\">@08</a>";
QString HTMLOfficialDiscord = "Official Discord server: <a href=\"@09\">@10</a></span><br>\n";
QString HTMLEnd = "<p>@11</p>\n</div>";
// ****************************************************************************
// Source Repo field
if (sourceRepo.startsWith("http://") || sourceRepo.startsWith("https://")) { // if its full URL, not relative path
if (sourceRepo.startsWith(githubURL) || sourceRepo.startsWith(gitlabURL)) { // if its github or gitlab
if (sourceRepo.startsWith(githubURL)) { // for github
shortSourceRepo.replace(githubURL, "");
} else { // for gitlab
shortSourceRepo.replace(gitlabURL, "");
gitlabTag = "@gitlab "; // adding gitlab tag
}
if (rx1.indexIn(shortSourceRepo) != -1) { // if relative short path (shortSourceRepo) is correct
repoName = rx1.cap(1); // getting result of regexp capture
} else { // relative short path (shortSourceRepo) is not correct
resultHTML = "ERROR Please enter full Source repo URL or relative path for github in this format: user/repository\n"
"Parent directories like gitlab.com/username is not supported";
}
} else if(sourceRepo.startsWith(sourceforgeURL)) { // if it's sourceforge
QRegExp rx2("/code/.*");
shortSourceRepo.replace(sourceforgeURL, ""); // cut URL prefix
shortSourceRepo.replace(rx2, ""); // cut everything after repo name
repoName = shortSourceRepo; // for sourceforge - name is same as short relative path
sourceforgeTag = "@sourceforge ";
} else if (sourceRepo.startsWith("https://sourceforge.net/")) { // sourceforge without /p/ in URL means it's not direct link to repo code
resultHTML = "ERROR Please enter direct link to repository code, not to main page. Example:\n"
"https://sourceforge.net/p/quark/code/HEAD/tree/\n"
"Instead of https://sourceforge.net/projects/quark/ ";
} else {
resultHTML = "ERROR Unrecognized git service. Only github, sourceforge and gitlab are supported. For other fit services - do it manually";
}
} else if (!sourceRepo.isEmpty()) { // if this field is not empty
if (rx1.indexIn(shortSourceRepo) != -1) { // if relative short path (shortSourceRepo) is correct
repoName = rx1.cap(1); // getting result of regexp capture
sourceRepo = githubURL + sourceRepo; // make full URL
} else {
resultHTML = "ERROR Please enter full source URL or relative path for github in this format: user/repository";
}
} else { // if this field is empty
resultHTML = "Please enter something for source repo URL";
}
// ****************************************************************************
// Target Repo field
if (!targetRepo.isEmpty()) { // if this field not empty
if (!targetRepo.startsWith(githubURL)) { // if its relative path (not full URL)
if (rx1.indexIn(shortTargetRepo) != -1) { // if relative short path (shortTargetRepo) is correct
newRepoName = rx1.cap(1);
} else { // otherwise - assume its just name of repo
newRepoName = targetRepo;
}
shortTargetRepo = orgName+ "/" + newRepoName;
targetRepo = githubURL + shortTargetRepo; // make full URL
} else { // for full URL
shortTargetRepo = shortTargetRepo.replace(githubURL, "");
}
} else { // if this field is empty
if (sourceRepo.startsWith(githubURL)) { // for github
shortTargetRepo = orgName; // don't show repo name in relative path (shortTargetRepo) because it's same as original
targetRepo = githubURL + orgName + "/" + repoName;
} else { // for other git services - show repo name just for clarity
shortTargetRepo = orgName + "/" + repoName;
targetRepo = githubURL + shortTargetRepo;
}
}
// ****************************************************************************
// Official Website field
if (!officialSite.isEmpty()) {
if (officialSite.startsWith("https://") || officialSite.startsWith("http://")) {
shortOfficialSite.replace(QRegExp("^https?://(www.)?") , ""); // remove http(s) and www for shortOfficialSite
} else {
officialSite = "http://" + officialSite; // add http:// if not exist
}
shortOfficialSite.replace(QRegExp("/$"), ""); // remove last slash in URL for shortOfficialSite
if (!discordURL.isEmpty()) { // if Official Discord also exist
HTMLOfficialSite += " /\n"; // add slash and newline symbol in the end of HTML
} else {
HTMLOfficialSite += "\n</span><br>\n"; // if not - close span tag
}
} else {
HTMLOfficialSite = ""; // if officialSite field is empty - make HTMLOfficialSite blank too
}
// ****************************************************************************
// Official Discord field
if (!discordURL.isEmpty()) {
if (officialSite.isEmpty()) { // if Official Website field is empty -
HTMLOfficialDiscord = "<span class=\"subtext\">" + HTMLOfficialDiscord; // add opening span tag in the beginning of HTML
}
} else {
HTMLOfficialDiscord = ""; // if officialDiscord field is empty - make HTMLOfficialDiscord blank too
}
if (!discordURL.isEmpty() && discordName.isEmpty()) { // if discord URL is not empty, but name field is
resultHTML = "ERROR Please enter name of the Official Discord server";
}
// ****************************************************************************
// Description field
descriptionText.replace(QRegExp(" +"), " "); // remove double spaces
// ****************************************************************************
// Construct resulting HTML code
if (resultHTML.isEmpty()) { // if resultHTML it not containing some error message
if (sourceRepo.startsWith(githubURL)) { // if source repo is on github
resultHTML = HTMLBeginning + HTMLGithub + HTMLOfficialSite + HTMLOfficialDiscord + HTMLEnd;
} else {
resultHTML = HTMLBeginning + HTMLOtherGit + HTMLOfficialSite + HTMLOfficialDiscord + HTMLEnd;
}
}
// ****************************************************************************
// Replacing placeholders in template with real values
gitTags = sourceforgeTag + gitlabTag; // one of the tag wouldn't be blank (both are blank if its github)
resultHTML.replace("@01", sourceRepo);
resultHTML.replace("@02", repoName);
resultHTML.replace("@03", gitTags);
resultHTML.replace("@04", shortSourceRepo);
resultHTML.replace("@05", targetRepo);
resultHTML.replace("@06", shortTargetRepo);
resultHTML.replace("@07", officialSite);
resultHTML.replace("@08", shortOfficialSite);
resultHTML.replace("@09", discordURL);
resultHTML.replace("@10", discordName);
resultHTML.replace("@11", descriptionText);
resultText->setPlainText(resultHTML);
}
void Widget::showAboutDialog()
{
AboutDialog *aboutDialog = new AboutDialog(this);
aboutDialog->resize(180, 100);
aboutDialog->exec();
}