-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdialog.cpp
344 lines (276 loc) · 11.2 KB
/
dialog.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*
* Copyright (C) 2019 Ivan Romanov <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "dialog.h"
#include <ui_dialog.h>
#include <QBuffer>
#include <QDataStream>
#include <QDebug>
#include <QDir>
#include <QFile>
#include <QFileDialog>
#include <QFileInfo>
#include <QShortcut>
#include <QMessageBox>
#define QS(str) QStringLiteral(str)
#define QSU(str) QString::fromUtf8(str)
#define QSL(str) QString::fromLatin1(str)
Dialog::Dialog(const QString& path, QWidget *parent)
: QDialog(parent)
, ui(new Ui::Dialog)
{
ui->setupUi(this);
connect(ui->twCursors, &QTreeWidget::currentItemChanged, this, &Dialog::showCursor);
connect(ui->pbOpenFolder, &QPushButton::clicked, this, &Dialog::openFolder);
connect(new QShortcut(QKeySequence("Ctrl+O"), ui->pbOpenFolder), &QShortcut::activated,
ui->pbOpenFolder, &QPushButton::click);
connect(new QShortcut(QKeySequence("Ctrl+E"), ui->pbExport), &QShortcut::activated,
ui->pbExport, &QPushButton::click);
if (!path.isEmpty()) {
openFolderPath(path);
}
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::openFolder()
{
QString path = QFileDialog::getExistingDirectory(this);
if (path.isEmpty()) {
return;
}
openFolderPath(path);
}
void Dialog::openFolderPath(QString path)
{
const QFileInfo pathInfo(path);
QString fileToSelect;
if (!pathInfo.isDir()) {
path = pathInfo.absoluteDir().absolutePath();
fileToSelect = pathInfo.fileName();
}
QDir dir(path);
QFileInfoList fileList = dir.entryInfoList(QDir::Filter::Files);
_cursorFileMap.clear();
for (const QFileInfo &fileInfo: fileList) {
QFile file(fileInfo.absoluteFilePath());
if (!file.open(QIODevice::OpenModeFlag::ReadOnly)) {
continue;
}
QDataStream stream(&file);
stream.setByteOrder(QDataStream::ByteOrder::LittleEndian);
quint32 magic;
quint32 header;
quint32 version;
quint32 ntoc;
stream >> magic >> header >> version >> ntoc;
if (magic != 0x72756358 /* Xcur */ && header != 16) {
continue;
}
CursorFile cursorFile;
cursorFile.name = fileInfo.fileName();
QFileInfo fi = fileInfo;
while (fi.isSymLink()) {
fi = QFileInfo(fi.symLinkTarget());
}
cursorFile.realName = fi.fileName();
for (quint32 i = 0; i < ntoc; ++i) {
quint32 type;
quint32 subtype;
quint32 position;
stream >> type >> subtype >> position;
qint64 tocPos = file.pos(); // position in table of contents entries
if (type == 0xfffd0002) {
file.seek(position);
quint32 imgHeader;
quint32 imgType;
quint32 imgSubtype;
quint32 imgVersion;
quint32 imgWidth;
quint32 imgHeight;
quint32 imgYhot;
quint32 imgXhot;
quint32 imgDelay;
stream >> imgHeader
>> imgType
>> imgSubtype
>> imgVersion
>> imgWidth
>> imgHeight
>> imgXhot
>> imgYhot
>> imgDelay;
if (imgHeader != 36 || imgType != type || imgSubtype != subtype || imgVersion != 1) {
continue;
}
QByteArray imgData = file.read((imgWidth * imgHeight) * 4);
Cursor cursor;
cursor.image = QImage(reinterpret_cast<uchar*>(imgData.data()), static_cast<int>(imgWidth), static_cast<int>(imgHeight), QImage::Format::Format_ARGB32).copy();
cursor.hotSpot = QPoint(static_cast<int>(imgXhot), static_cast<int>(imgYhot));
cursor.size = imgSubtype;
QString key = QS("%1").arg(static_cast<int>(subtype), 3, 10, QLatin1Char('0'));
cursorFile.cursorMap.insert(key, cursor);
file.seek(tocPos);
}
else if (type == 0xfffe0001) {
file.seek(position);
quint32 commHeader;
quint32 commType;
quint32 commSubtype;
quint32 commVersion;
quint32 commLength;
stream >> commHeader
>> commType
>> commSubtype
>> commVersion
>> commLength;
if (commHeader != 20 || commType != type || commSubtype != subtype || commVersion != 1) {
continue;
}
QByteArray commData;
commData.resize(static_cast<int>(commLength));
stream.readRawData(commData.data(), static_cast<int>(commLength));
switch (subtype) {
case 1:
if (!cursorFile.copyright.isEmpty()) {
cursorFile.copyright += QS("\n");
}
cursorFile.copyright += QSU(commData);
break;
case 2:
if (!cursorFile.license.isEmpty()) {
cursorFile.license += QS("\n");
}
cursorFile.license += QSU(commData);
break;
case 3:
if (!cursorFile.other.isEmpty()) {
cursorFile.other += QS("\n");
}
cursorFile.other += QSU(commData);
break;
default:
break;
}
file.seek(tocPos);
}
}
_cursorFileMap.insert(cursorFile.name, cursorFile);
}
ui->twCursors->clear();
QList<QTreeWidgetItem*> topLevelItems;
QTreeWidgetItem *itemToSelect = nullptr;
QStringList nameList;
for (const CursorFile &cursorFile: _cursorFileMap) {
if (cursorFile.realName == cursorFile.name) {
QTreeWidgetItem *item = new QTreeWidgetItem({cursorFile.name});
topLevelItems << item;
if (cursorFile.name == fileToSelect && !itemToSelect) {
itemToSelect = item;
}
}
}
for (const CursorFile &cursorFile: _cursorFileMap) {
if (cursorFile.realName != cursorFile.name) {
for (QTreeWidgetItem *topLevel: topLevelItems) {
if (topLevel->text(0) == cursorFile.realName) {
QTreeWidgetItem *item = new QTreeWidgetItem(topLevel, {cursorFile.name});
topLevelItems << item;
if (cursorFile.name == fileToSelect && !itemToSelect) {
itemToSelect = item;
}
}
}
}
}
ui->twCursors->addTopLevelItems(topLevelItems);
if (itemToSelect) {
ui->twCursors->setCurrentItem(itemToSelect);
}
ui->pbExport->setEnabled(!_cursorFileMap.isEmpty());
}
void Dialog::showCursor(QTreeWidgetItem *current, QTreeWidgetItem *previous)
{
if (!current) {
ui->teCursorInfo->clear();
return;
}
CursorFile currentCursorFile = _cursorFileMap.value(current->text(0));
CursorFile prevCursorFile = previous ? _cursorFileMap.value(previous->text(0)) : CursorFile();
if(currentCursorFile.name.isEmpty()) {
ui->teCursorInfo->clear();
return;
}
if (prevCursorFile.realName == currentCursorFile.realName) {
return;
}
if (currentCursorFile.cachedCursors.isEmpty()) {
currentCursorFile.cachedCursors = "<html><body>";
QStringList keys = currentCursorFile.cursorMap.keys();
keys.removeDuplicates();
keys.sort();
if (!currentCursorFile.copyright.isEmpty()) {
currentCursorFile.cachedCursors += QS("Copyright: %1<br/>").arg(currentCursorFile.copyright);
}
if (!currentCursorFile.license.isEmpty()) {
currentCursorFile.cachedCursors += QS("License: %1<br/>").arg(currentCursorFile.license);
}
if (!currentCursorFile.other.isEmpty()) {
currentCursorFile.cachedCursors += QS("Other: %1<br/>").arg(currentCursorFile.other);
}
for (const QString &key: keys) {
QList<Cursor> cursorList = currentCursorFile.cursorMap.values(key);
currentCursorFile.cachedCursors += "<p>";
Cursor firstCursor = cursorList.first();
currentCursorFile.cachedCursors += QS("Nominal size: %1. Image size: %2x%3. Hot spot: %4x%5<br/>").arg(QString::number(firstCursor.size),
QString::number(firstCursor.image.width()), QString::number(firstCursor.image.height()),
QString::number(firstCursor.hotSpot.x()), QString::number(firstCursor.hotSpot.y()));
for (const Cursor &cursor: cursorList) {
QByteArray imgBa;
QBuffer buffer(&imgBa);
cursor.image.save(&buffer, "PNG");
imgBa = imgBa.toBase64();
currentCursorFile.cachedCursors += "<img src=\"data:image/png;base64, " + QSL(imgBa) + "\"/>";
}
currentCursorFile.cachedCursors += "</p>";
}
currentCursorFile.cachedCursors += "</body></html>";
QMutableMapIterator<QString, CursorFile> it = _cursorFileMap;
QString realName = currentCursorFile.realName.isEmpty() ? currentCursorFile.name : currentCursorFile.realName;
while (it.hasNext()) {
CursorFile &cursorFile = it.next().value();
if (cursorFile.realName == currentCursorFile.realName) {
cursorFile.cachedCursors = currentCursorFile.cachedCursors;
}
}
}
ui->teCursorInfo->setHtml(currentCursorFile.cachedCursors);
}
void Dialog::on_pbExport_clicked() {
QString path = QFileDialog::getExistingDirectory(this);
for (const CursorFile &cursorFile: _cursorFileMap) {
for (const Cursor &cursor: cursorFile.cursorMap) {
QString fpath = QS("%1/%2_%3_%4.png").arg(path, cursorFile.name, QString::number(cursor.hotSpot.x()), QString::number(cursor.hotSpot.y()));
if(!cursor.image.save(fpath)) {
QMessageBox::critical(this, tr("Export Failed"), tr("Could not save file: <pre>%1</pre>").arg(fpath));
return;
}
}
}
QMessageBox::information(this, tr("Export Completed"), tr("The cursors have been exported successfully!"));
}