From 87e3340a56ea78a34e7e158445834d514fac178e Mon Sep 17 00:00:00 2001 From: Ivan Savenko Date: Tue, 17 Dec 2024 17:20:36 +0000 Subject: [PATCH] Track clipboard state and update UI accordingly --- launcher/startGame/StartGameTab.cpp | 35 +++++++++++++++++++++++++++++ launcher/startGame/StartGameTab.h | 7 +----- 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/launcher/startGame/StartGameTab.cpp b/launcher/startGame/StartGameTab.cpp index d532dccec..901dd4281 100644 --- a/launcher/startGame/StartGameTab.cpp +++ b/launcher/startGame/StartGameTab.cpp @@ -48,6 +48,37 @@ StartGameTab::StartGameTab(QWidget * parent) #ifndef ENABLE_EDITOR ui->buttonGameEditor->hide(); #endif + + auto clipboard = QGuiApplication::clipboard(); + + connect(clipboard, SIGNAL(dataChanged()), this, SLOT(clipboardDataChanged())); +} + +void StartGameTab::clipboardDataChanged() +{ + ui->buttonPresetExport->setIcon(QIcon{});// reset icon, if any + + auto clipboard = QGuiApplication::clipboard(); + QString clipboardText = clipboard->text().trimmed(); + + if (clipboardText.isEmpty()) + { + ui->buttonPresetImport->setEnabled(false); + } + else + { + // this *may* be json, try parsing it + if (clipboardText.startsWith('{')) + { + QByteArray presetBytes(clipboardText.toUtf8()); + const JsonNode presetJson(reinterpret_cast(presetBytes.data()), presetBytes.size(), "preset in clipboard"); + bool presetValid = !presetJson["name"].String().empty() && !presetJson["mods"].Vector().empty(); + + ui->buttonPresetImport->setEnabled(presetValid); + } + else + ui->buttonPresetImport->setEnabled(false); + } } StartGameTab::~StartGameTab() @@ -70,6 +101,8 @@ void StartGameTab::refreshState() refreshTranslation(getMainWindow()->getTranslationStatus()); refreshPresets(); refreshMods(); + + clipboardDataChanged(); } void StartGameTab::refreshPresets() @@ -361,6 +394,8 @@ void StartGameTab::on_buttonPresetExport_clicked() JsonNode presetJson = getMainWindow()->getModView()->exportCurrentPreset(); QString presetString = QString::fromStdString(presetJson.toCompactString()); QGuiApplication::clipboard()->setText(presetString); + + ui->buttonPresetExport->setIcon(QIcon{":/icons/mod-enabled.png"}); } void StartGameTab::on_buttonPresetImport_clicked() diff --git a/launcher/startGame/StartGameTab.h b/launcher/startGame/StartGameTab.h index d66e449b5..eb4cbb3e5 100644 --- a/launcher/startGame/StartGameTab.h +++ b/launcher/startGame/StartGameTab.h @@ -65,19 +65,14 @@ private slots: void on_buttonMissingVideoHelp_clicked(); void on_buttonMissingFilesHelp_clicked(); void on_buttonMissingCampaignsHelp_clicked(); - void on_buttonPresetExport_clicked(); - void on_buttonPresetImport_clicked(); - void on_buttonPresetNew_clicked(); - void on_buttonPresetDelete_clicked(); - void on_comboBoxModPresets_currentTextChanged(const QString &arg1); - void on_buttonPresetRename_clicked(); + void clipboardDataChanged(); private: Ui::StartGameTab * ui; };