Run command settings: add import and export buttons

BUG: 409388
This commit is contained in:
Albert Vaca Cintora 2023-03-30 01:08:03 +02:00
parent 1e0ab4569d
commit d925254e3a
2 changed files with 74 additions and 0 deletions

View file

@ -10,6 +10,8 @@
#include <QHBoxLayout>
#include <QHeaderView>
#include <QJsonDocument>
#include <QJsonArray>
#include <QFileDialog>
#include <QMenu>
#include <QPushButton>
#include <QStandardItemModel>
@ -74,6 +76,15 @@ RunCommandConfig::RunCommandConfig(QWidget *parent, const QVariantList &args)
button->setMenu(defaultMenu);
layout->addWidget(button);
QHBoxLayout *importExportLayout = new QHBoxLayout(this);
QPushButton *exportButton = new QPushButton(i18n("Export"), this);
importExportLayout->addWidget(exportButton);
connect(exportButton, &QPushButton::clicked, this, &RunCommandConfig::exportCommands);
QPushButton *importButton = new QPushButton(i18n("Import"), this);
importExportLayout->addWidget(importButton);
connect(importButton, &QPushButton::clicked, this, &RunCommandConfig::importCommands);
layout->addLayout(importExportLayout);
setLayout(layout);
m_entriesModel = new QStandardItemModel(this);
@ -86,6 +97,67 @@ RunCommandConfig::~RunCommandConfig()
{
}
void RunCommandConfig::exportCommands()
{
QString filePath = QFileDialog::getSaveFileName(this, i18n("Export Commands"), QDir::homePath(), QStringLiteral("JSON (*.json)"));
if (filePath.isEmpty())
return;
QFile file(filePath);
if (!file.open(QFile::WriteOnly | QFile::Text)) {
qWarning() << "Could not write to file:" << filePath;
return;
}
QJsonArray jsonArray;
for (int i = 0; i < m_entriesModel->rowCount(); i++) {
QJsonObject jsonObj;
jsonObj[QStringLiteral("name")] = m_entriesModel->index(i, 0).data().toString();
jsonObj[QStringLiteral("command")] = m_entriesModel->index(i, 1).data().toString();
jsonArray.append(jsonObj);
}
QJsonDocument jsonDocument(jsonArray);
file.write(jsonDocument.toJson());
file.close();
}
void RunCommandConfig::importCommands()
{
QString filePath = QFileDialog::getOpenFileName(this, i18n("Import Commands"), QDir::homePath(), QStringLiteral("JSON (*.json)"));
if (filePath.isEmpty())
return;
QFile file(filePath);
if (!file.open(QFile::ReadOnly | QFile::Text)) {
qWarning() << "Could not read file:" << filePath;
return;
}
QByteArray jsonData = file.readAll();
file.close();
QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonData);
if (jsonDoc.isNull() || !jsonDoc.isArray()) {
qWarning() << "Invalid JSON format.";
return;
}
// Clear the current command list
m_entriesModel->removeRows(0, m_entriesModel->rowCount());
// Populate the model with the imported commands
QJsonArray jsonArray = jsonDoc.array();
for (const QJsonValue &jsonValue : jsonArray) {
QJsonObject jsonObj = jsonValue.toObject();
QString name = jsonObj.value(QStringLiteral("name")).toString();
QString command = jsonObj.value(QStringLiteral("command")).toString();
insertRow(m_entriesModel->rowCount(), name, command);
}
Q_EMIT changed(true);
}
void RunCommandConfig::addSuggestedCommand(QMenu *menu, const QString &name, const QString &command)
{
auto action = new QAction(name);

View file

@ -26,6 +26,8 @@ public Q_SLOTS:
private Q_SLOTS:
void onDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight);
void exportCommands();
void importCommands();
private:
void addSuggestedCommand(QMenu *menu, const QString &name, const QString &command);