xcursor-viewer/dialog.cpp

345 lines
11 KiB
C++
Raw Permalink Normal View History

2019-05-19 20:23:18 +08:00
/*
* Copyright (C) 2019 Ivan Romanov <drizt72@zoho.eu>
*
* 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>
2019-05-26 01:45:35 +08:00
#include <QFileInfo>
#include <QShortcut>
2023-02-03 18:45:11 +08:00
#include <QMessageBox>
2019-05-26 01:45:35 +08:00
#define QS(str) QStringLiteral(str)
#define QSU(str) QString::fromUtf8(str)
#define QSL(str) QString::fromLatin1(str)
2019-05-19 20:23:18 +08:00
Dialog::Dialog(const QString& path, QWidget *parent)
2019-05-19 20:23:18 +08:00
: QDialog(parent)
, ui(new Ui::Dialog)
{
ui->setupUi(this);
2019-05-26 01:45:35 +08:00
connect(ui->twCursors, &QTreeWidget::currentItemChanged, this, &Dialog::showCursor);
2019-05-19 20:23:18 +08:00
connect(ui->pbOpenFolder, &QPushButton::clicked, this, &Dialog::openFolder);
connect(new QShortcut(QKeySequence("Ctrl+O"), ui->pbOpenFolder), &QShortcut::activated,
ui->pbOpenFolder, &QPushButton::click);
2023-02-03 18:45:11 +08:00
connect(new QShortcut(QKeySequence("Ctrl+E"), ui->pbExport), &QShortcut::activated,
ui->pbExport, &QPushButton::click);
if (!path.isEmpty()) {
openFolderPath(path);
}
2019-05-19 20:23:18 +08:00
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::openFolder()
{
2019-05-26 01:45:35 +08:00
QString path = QFileDialog::getExistingDirectory(this);
2019-05-19 20:23:18 +08:00
2019-05-26 01:45:35 +08:00
if (path.isEmpty()) {
2019-05-19 20:23:18 +08:00
return;
}
2019-05-26 01:45:35 +08:00
openFolderPath(path);
}
void Dialog::openFolderPath(QString path)
2019-05-26 01:45:35 +08:00
{
const QFileInfo pathInfo(path);
QString fileToSelect;
if (!pathInfo.isDir()) {
path = pathInfo.absoluteDir().absolutePath();
fileToSelect = pathInfo.fileName();
}
2019-05-26 01:45:35 +08:00
QDir dir(path);
2019-05-19 20:23:18 +08:00
2019-05-26 01:45:35 +08:00
QFileInfoList fileList = dir.entryInfoList(QDir::Filter::Files);
2019-05-19 20:23:18 +08:00
2019-05-26 01:45:35 +08:00
_cursorFileMap.clear();
2019-05-19 20:23:18 +08:00
2019-05-26 01:45:35 +08:00
for (const QFileInfo &fileInfo: fileList) {
QFile file(fileInfo.absoluteFilePath());
2019-05-19 20:23:18 +08:00
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;
2019-05-26 01:45:35 +08:00
cursorFile.name = fileInfo.fileName();
QFileInfo fi = fileInfo;
while (fi.isSymLink()) {
fi = QFileInfo(fi.symLinkTarget());
}
cursorFile.realName = fi.fileName();
2019-05-19 20:23:18 +08:00
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
2019-05-19 20:23:18 +08:00
>> 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));
2019-05-20 01:29:12 +08:00
cursor.size = imgSubtype;
2019-05-19 20:23:18 +08:00
2019-05-26 01:45:35 +08:00
QString key = QS("%1").arg(static_cast<int>(subtype), 3, 10, QLatin1Char('0'));
2021-10-06 05:08:29 +08:00
cursorFile.cursorMap.insert(key, cursor);
2019-05-19 20:23:18 +08:00
2019-05-20 01:29:12 +08:00
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()) {
2019-05-26 01:45:35 +08:00
cursorFile.copyright += QS("\n");
2019-05-20 01:29:12 +08:00
}
2019-05-26 01:45:35 +08:00
cursorFile.copyright += QSU(commData);
2019-05-20 01:29:12 +08:00
break;
case 2:
if (!cursorFile.license.isEmpty()) {
2019-05-26 01:45:35 +08:00
cursorFile.license += QS("\n");
2019-05-20 01:29:12 +08:00
}
2019-05-26 01:45:35 +08:00
cursorFile.license += QSU(commData);
2019-05-20 01:29:12 +08:00
break;
case 3:
if (!cursorFile.other.isEmpty()) {
2019-05-26 01:45:35 +08:00
cursorFile.other += QS("\n");
2019-05-20 01:29:12 +08:00
}
2019-05-26 01:45:35 +08:00
cursorFile.other += QSU(commData);
2019-05-20 01:29:12 +08:00
break;
default:
break;
}
2019-05-19 20:23:18 +08:00
file.seek(tocPos);
}
}
2019-05-26 01:45:35 +08:00
_cursorFileMap.insert(cursorFile.name, cursorFile);
2019-05-19 20:23:18 +08:00
}
2019-05-26 01:45:35 +08:00
ui->twCursors->clear();
2019-05-19 20:23:18 +08:00
2019-05-26 01:45:35 +08:00
QList<QTreeWidgetItem*> topLevelItems;
QTreeWidgetItem *itemToSelect = nullptr;
2019-05-19 20:23:18 +08:00
QStringList nameList;
2019-05-26 01:45:35 +08:00
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;
}
2019-05-26 01:45:35 +08:00
}
}
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;
}
2019-05-26 01:45:35 +08:00
}
}
}
2019-05-19 20:23:18 +08:00
}
2019-05-26 01:45:35 +08:00
ui->twCursors->addTopLevelItems(topLevelItems);
if (itemToSelect) {
ui->twCursors->setCurrentItem(itemToSelect);
}
2023-02-03 18:45:11 +08:00
ui->pbExport->setEnabled(!_cursorFileMap.isEmpty());
2019-05-19 20:23:18 +08:00
}
2019-05-26 01:45:35 +08:00
void Dialog::showCursor(QTreeWidgetItem *current, QTreeWidgetItem *previous)
2019-05-19 20:23:18 +08:00
{
2019-05-26 01:45:35 +08:00
if (!current) {
ui->teCursorInfo->clear();
return;
2019-05-19 20:23:18 +08:00
}
2019-05-26 01:45:35 +08:00
CursorFile currentCursorFile = _cursorFileMap.value(current->text(0));
CursorFile prevCursorFile = previous ? _cursorFileMap.value(previous->text(0)) : CursorFile();
if(currentCursorFile.name.isEmpty()) {
2019-05-19 20:23:18 +08:00
ui->teCursorInfo->clear();
return;
}
2019-05-26 01:45:35 +08:00
if (prevCursorFile.realName == currentCursorFile.realName) {
return;
}
2019-05-19 20:23:18 +08:00
2019-05-26 01:45:35 +08:00
if (currentCursorFile.cachedCursors.isEmpty()) {
currentCursorFile.cachedCursors = "<html><body>";
2019-05-19 20:23:18 +08:00
2019-05-26 01:45:35 +08:00
QStringList keys = currentCursorFile.cursorMap.keys();
keys.removeDuplicates();
keys.sort();
2019-05-20 01:29:12 +08:00
2019-05-26 01:45:35 +08:00
if (!currentCursorFile.copyright.isEmpty()) {
currentCursorFile.cachedCursors += QS("Copyright: %1<br/>").arg(currentCursorFile.copyright);
}
2019-05-20 01:29:12 +08:00
2019-05-26 01:45:35 +08:00
if (!currentCursorFile.license.isEmpty()) {
currentCursorFile.cachedCursors += QS("License: %1<br/>").arg(currentCursorFile.license);
}
2019-05-20 01:29:12 +08:00
2019-05-26 01:45:35 +08:00
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>";
2019-05-19 20:23:18 +08:00
}
2019-05-26 01:45:35 +08:00
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;
}
}
}
2019-05-19 20:23:18 +08:00
2019-05-26 01:45:35 +08:00
ui->teCursorInfo->setHtml(currentCursorFile.cachedCursors);
2019-05-19 20:23:18 +08:00
}
2023-02-03 18:45:11 +08:00
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!"));
}