Added plantxt export

Signed-off-by: Trial97 <alexandru.tripon97@gmail.com>
This commit is contained in:
Trial97
2023-06-25 14:11:41 +03:00
parent ef0752972a
commit 84c63f4f01
14 changed files with 282 additions and 176 deletions

View File

@ -1,122 +0,0 @@
// SPDX-License-Identifier: GPL-3.0-only
/*
* Prism Launcher - Minecraft Launcher
* Copyright (c) 2023 Trial97 <alexandru.tripon97@gmail.com>
*
* 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, version 3.
*
* 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 <https://www.gnu.org/licenses/>.
*/
#include "ExportModsToStringDialog.h"
#include <QCheckBox>
#include <QComboBox>
#include <QTextEdit>
#include "minecraft/MinecraftInstance.h"
#include "minecraft/mod/ModFolderModel.h"
#include "modplatform/helpers/ExportModsToStringTask.h"
#include "ui_ExportModsToStringDialog.h"
#include <QFileDialog>
#include <QFileSystemModel>
#include <QJsonDocument>
#include <QMessageBox>
#include <QPushButton>
ExportModsToStringDialog::ExportModsToStringDialog(InstancePtr instance, QWidget* parent)
: QDialog(parent), m_template_selected(false), ui(new Ui::ExportModsToStringDialog)
{
ui->setupUi(this);
ui->templateGroup->setDisabled(true);
MinecraftInstance* mcInstance = dynamic_cast<MinecraftInstance*>(instance.get());
if (mcInstance) {
mcInstance->loaderModList()->update();
connect(mcInstance->loaderModList().get(), &ModFolderModel::updateFinished, this, [this, mcInstance]() {
m_allMods = mcInstance->loaderModList()->allMods();
triggerImp();
});
}
connect(ui->formatComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &ExportModsToStringDialog::formatChanged);
connect(ui->authorsCheckBox, &QCheckBox::stateChanged, this, &ExportModsToStringDialog::trigger);
connect(ui->versionCheckBox, &QCheckBox::stateChanged, this, &ExportModsToStringDialog::trigger);
connect(ui->urlCheckBox, &QCheckBox::stateChanged, this, &ExportModsToStringDialog::trigger);
connect(ui->templateText, &QTextEdit::textChanged, this, &ExportModsToStringDialog::triggerImp);
connect(ui->copyButton, &QPushButton::clicked, this, [this](bool) {
this->ui->finalText->selectAll();
this->ui->finalText->copy();
});
}
ExportModsToStringDialog::~ExportModsToStringDialog()
{
delete ui;
}
void ExportModsToStringDialog::formatChanged(int index)
{
switch (index) {
case 0: {
ui->templateGroup->setDisabled(true);
ui->optionsGroup->setDisabled(false);
break;
}
case 1: {
ui->templateGroup->setDisabled(true);
ui->optionsGroup->setDisabled(false);
break;
}
case 2: {
ui->templateGroup->setDisabled(false);
ui->optionsGroup->setDisabled(true);
break;
}
}
triggerImp();
}
void ExportModsToStringDialog::triggerImp()
{
auto format = ExportToString::HTML;
switch (ui->formatComboBox->currentIndex()) {
case 2: {
m_template_selected = true;
ui->finalText->setPlainText(ExportToString::ExportModsToStringTask(m_allMods, ui->templateText->toPlainText()));
return;
}
case 0: {
format = ExportToString::HTML;
break;
}
case 1: {
format = ExportToString::MARKDOWN;
break;
}
default: {
return;
}
}
auto opt = 0;
if (ui->authorsCheckBox->isChecked())
opt |= ExportToString::Authors;
if (ui->versionCheckBox->isChecked())
opt |= ExportToString::Version;
if (ui->urlCheckBox->isChecked())
opt |= ExportToString::Url;
ui->finalText->setPlainText(ExportToString::ExportModsToStringTask(m_allMods, format, static_cast<ExportToString::OptionalData>(opt)));
if (!m_template_selected) {
auto exampleLine = format == ExportToString::HTML ? "<ul><a href=\"{url}\">{name}</a>[{version}] by {authors}</ul>"
: "[{name}]({url})[{version}] by {authors}";
if (ui->templateText->toPlainText() != exampleLine)
ui->templateText->setPlainText(exampleLine);
}
}

View File

@ -0,0 +1,171 @@
// SPDX-License-Identifier: GPL-3.0-only
/*
* Prism Launcher - Minecraft Launcher
* Copyright (c) 2023 Trial97 <alexandru.tripon97@gmail.com>
*
* 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, version 3.
*
* 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 <https://www.gnu.org/licenses/>.
*/
#include "ExportToModListDialog.h"
#include <QCheckBox>
#include <QComboBox>
#include <QTextEdit>
#include "FileSystem.h"
#include "minecraft/MinecraftInstance.h"
#include "minecraft/mod/ModFolderModel.h"
#include "modplatform/helpers/ExportToModList.h"
#include "ui_ExportToModListDialog.h"
#include <QFileDialog>
#include <QFileSystemModel>
#include <QJsonDocument>
#include <QMessageBox>
#include <QPushButton>
ExportToModListDialog::ExportToModListDialog(InstancePtr instance, QWidget* parent)
: QDialog(parent), m_template_selected(false), name(instance->name()), ui(new Ui::ExportToModListDialog)
{
ui->setupUi(this);
ui->templateGroup->setDisabled(true);
MinecraftInstance* mcInstance = dynamic_cast<MinecraftInstance*>(instance.get());
if (mcInstance) {
mcInstance->loaderModList()->update();
connect(mcInstance->loaderModList().get(), &ModFolderModel::updateFinished, this, [this, mcInstance]() {
m_allMods = mcInstance->loaderModList()->allMods();
triggerImp();
});
}
connect(ui->formatComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &ExportToModListDialog::formatChanged);
connect(ui->authorsCheckBox, &QCheckBox::stateChanged, this, &ExportToModListDialog::trigger);
connect(ui->versionCheckBox, &QCheckBox::stateChanged, this, &ExportToModListDialog::trigger);
connect(ui->urlCheckBox, &QCheckBox::stateChanged, this, &ExportToModListDialog::trigger);
connect(ui->templateText, &QTextEdit::textChanged, this, &ExportToModListDialog::triggerImp);
connect(ui->copyButton, &QPushButton::clicked, this, [this](bool) {
this->ui->finalText->selectAll();
this->ui->finalText->copy();
});
}
ExportToModListDialog::~ExportToModListDialog()
{
delete ui;
}
void ExportToModListDialog::formatChanged(int index)
{
switch (index) {
case 0: {
ui->templateGroup->setDisabled(true);
ui->optionsGroup->setDisabled(false);
ui->resultText->show();
format = ExportToModList::HTML;
break;
}
case 1: {
ui->templateGroup->setDisabled(true);
ui->optionsGroup->setDisabled(false);
ui->resultText->show();
format = ExportToModList::MARKDOWN;
break;
}
case 2: {
ui->templateGroup->setDisabled(true);
ui->optionsGroup->setDisabled(false);
ui->resultText->hide();
format = ExportToModList::PLAINTXT;
break;
}
case 3: {
ui->templateGroup->setDisabled(false);
ui->optionsGroup->setDisabled(true);
ui->resultText->hide();
format = ExportToModList::CUSTOM;
break;
}
}
triggerImp();
}
void ExportToModListDialog::triggerImp()
{
if (format == ExportToModList::CUSTOM) {
m_template_selected = true;
ui->finalText->setPlainText(ExportToModList::ExportToModList(m_allMods, ui->templateText->toPlainText()));
return;
}
auto opt = 0;
if (ui->authorsCheckBox->isChecked())
opt |= ExportToModList::Authors;
if (ui->versionCheckBox->isChecked())
opt |= ExportToModList::Version;
if (ui->urlCheckBox->isChecked())
opt |= ExportToModList::Url;
auto txt = ExportToModList::ExportToModList(m_allMods, format, static_cast<ExportToModList::OptionalData>(opt));
ui->finalText->setPlainText(txt);
QString exampleLine;
switch (format) {
case ExportToModList::HTML: {
exampleLine = "<ul><a href=\"{url}\">{name}</a>[{version}] by {authors}</ul>";
ui->resultText->setHtml(txt);
break;
}
case ExportToModList::MARKDOWN: {
exampleLine = "[{name}]({url})[{version}] by {authors}";
ui->resultText->setMarkdown(txt);
break;
}
case ExportToModList::PLAINTXT: {
exampleLine = "name: {name}; url: {url}; version: {version}; authors: {authors};";
break;
}
case ExportToModList::CUSTOM:
return;
}
if (!m_template_selected) {
if (ui->templateText->toPlainText() != exampleLine)
ui->templateText->setPlainText(exampleLine);
}
}
void ExportToModListDialog::done(int result)
{
if (result == Accepted) {
const QString filename = FS::RemoveInvalidFilenameChars(name);
const QString output =
QFileDialog::getSaveFileName(this, tr("Export %1").arg(name), FS::PathCombine(QDir::homePath(), filename + extension()),
"File (*.txt *.html *.md)", nullptr);
if (output.isEmpty())
return;
FS::write(output, ui->finalText->toPlainText().toUtf8());
}
QDialog::done(result);
}
QString ExportToModListDialog::extension()
{
switch (format) {
case ExportToModList::HTML:
return ".html";
case ExportToModList::MARKDOWN:
return ".md";
case ExportToModList::PLAINTXT:
return ".txt";
case ExportToModList::CUSTOM:
return ".txt";
}
return ".txt";
}

View File

@ -22,17 +22,20 @@
#include <QList>
#include "BaseInstance.h"
#include "minecraft/mod/Mod.h"
#include "modplatform/helpers/ExportToModList.h"
namespace Ui {
class ExportModsToStringDialog;
class ExportToModListDialog;
}
class ExportModsToStringDialog : public QDialog {
class ExportToModListDialog : public QDialog {
Q_OBJECT
public:
explicit ExportModsToStringDialog(InstancePtr instance, QWidget* parent = nullptr);
~ExportModsToStringDialog();
explicit ExportToModListDialog(InstancePtr instance, QWidget* parent = nullptr);
~ExportToModListDialog();
void done(int result) override;
protected slots:
void formatChanged(int index);
@ -40,7 +43,10 @@ class ExportModsToStringDialog : public QDialog {
void trigger(int) { triggerImp(); };
private:
QString extension();
QList<Mod*> m_allMods;
bool m_template_selected;
Ui::ExportModsToStringDialog* ui;
QString name;
ExportToModList::Formats format = ExportToModList::Formats::HTML;
Ui::ExportToModListDialog* ui;
};

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>ExportModsToStringDialog</class>
<widget class="QDialog" name="ExportModsToStringDialog">
<class>ExportToModListDialog</class>
<widget class="QDialog" name="ExportToModListDialog">
<property name="geometry">
<rect>
<x>0</x>
@ -11,7 +11,7 @@
</rect>
</property>
<property name="windowTitle">
<string>Export Modrinth Pack</string>
<string>Export Pack to ModList</string>
</property>
<property name="sizeGripEnabled">
<bool>true</bool>
@ -53,6 +53,11 @@
<string>Markdown</string>
</property>
</item>
<item>
<property name="text">
<string>Plaintext</string>
</property>
</item>
<item>
<property name="text">
<string>Custom</string>
@ -124,6 +129,13 @@
</property>
</widget>
</item>
<item>
<widget class="QTextBrowser" name="resultText">
<property name="openExternalLinks">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</widget>
</item>
@ -141,7 +153,7 @@
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="standardButtons">
<set>QDialogButtonBox::Ok</set>
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Save</set>
</property>
</widget>
</item>
@ -154,7 +166,7 @@
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>ExportModsToStringDialog</receiver>
<receiver>ExportToModListDialog</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
@ -167,5 +179,21 @@
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>ExportToModListDialog</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>324</x>
<y>390</y>
</hint>
<hint type="destinationlabel">
<x>324</x>
<y>206</y>
</hint>
</hints>
</connection>
</connections>
</ui>