From 7dee6f62c05e9ba87a5c5a27e6ac0417e0104c6d Mon Sep 17 00:00:00 2001
From: Petr Mironychev <9195189+Palm1r@users.noreply.github.com>
Date: Sat, 21 Dec 2024 14:11:45 +0100
Subject: [PATCH] feat: Add project settings panel
---
QodeAssistClient.cpp | 7 ++-
qodeassist.cpp | 3 +
settings/CMakeLists.txt | 2 +
settings/ProjectSettings.cpp | 72 ++++++++++++++++++++++++
settings/ProjectSettings.hpp | 43 +++++++++++++++
settings/ProjectSettingsPanel.cpp | 91 +++++++++++++++++++++++++++++++
settings/ProjectSettingsPanel.hpp | 26 +++++++++
settings/SettingsConstants.hpp | 6 ++
8 files changed, 249 insertions(+), 1 deletion(-)
create mode 100644 settings/ProjectSettings.cpp
create mode 100644 settings/ProjectSettings.hpp
create mode 100644 settings/ProjectSettingsPanel.cpp
create mode 100644 settings/ProjectSettingsPanel.hpp
diff --git a/QodeAssistClient.cpp b/QodeAssistClient.cpp
index 50dc552..9d17ea9 100644
--- a/QodeAssistClient.cpp
+++ b/QodeAssistClient.cpp
@@ -34,6 +34,7 @@
#include "core/ChangesManager.h"
#include "settings/CodeCompletionSettings.hpp"
#include "settings/GeneralSettings.hpp"
+#include "settings/ProjectSettings.hpp"
using namespace LanguageServerProtocol;
using namespace TextEditor;
@@ -237,7 +238,11 @@ void QodeAssistClient::cancelRunningRequest(TextEditor::TextEditorWidget *editor
bool QodeAssistClient::isEnabled(ProjectExplorer::Project *project) const
{
- return Settings::generalSettings().enableQodeAssist();
+ if (!project)
+ return Settings::generalSettings().enableQodeAssist();
+
+ Settings::ProjectSettings settings(project);
+ return settings.isEnabled();
}
void QodeAssistClient::setupConnections()
diff --git a/qodeassist.cpp b/qodeassist.cpp
index 9c8d5e8..213d7e5 100644
--- a/qodeassist.cpp
+++ b/qodeassist.cpp
@@ -43,6 +43,7 @@
#include "QodeAssistClient.hpp"
#include "chat/ChatOutputPane.h"
#include "chat/NavigationPanel.hpp"
+#include "settings/ProjectSettingsPanel.hpp"
#include "providers/Providers.hpp"
#include "templates/Templates.hpp"
@@ -102,6 +103,8 @@ public:
m_chatOutputPane = new Chat::ChatOutputPane(this);
m_navigationPanel = new Chat::NavigationPanel();
+ Settings::setupProjectPanel();
+
ConfigurationManager::instance().init();
}
diff --git a/settings/CMakeLists.txt b/settings/CMakeLists.txt
index d1be5a4..7161e84 100644
--- a/settings/CMakeLists.txt
+++ b/settings/CMakeLists.txt
@@ -8,6 +8,8 @@ add_library(QodeAssistSettings STATIC
CodeCompletionSettings.hpp CodeCompletionSettings.cpp
ChatAssistantSettings.hpp ChatAssistantSettings.cpp
SettingsDialog.hpp SettingsDialog.cpp
+ ProjectSettings.hpp ProjectSettings.cpp
+ ProjectSettingsPanel.hpp ProjectSettingsPanel.cpp
)
target_link_libraries(QodeAssistSettings
diff --git a/settings/ProjectSettings.cpp b/settings/ProjectSettings.cpp
new file mode 100644
index 0000000..7fb7cc4
--- /dev/null
+++ b/settings/ProjectSettings.cpp
@@ -0,0 +1,72 @@
+/*
+ * Copyright (C) 2024 Petr Mironychev
+ *
+ * This file is part of QodeAssist.
+ *
+ * QodeAssist 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.
+ *
+ * QodeAssist 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 QodeAssist. If not, see .
+ */
+
+#include "ProjectSettings.hpp"
+
+#include "GeneralSettings.hpp"
+#include "SettingsConstants.hpp"
+#include "SettingsTr.hpp"
+#include
+
+namespace QodeAssist::Settings {
+
+ProjectSettings::ProjectSettings(ProjectExplorer::Project *project)
+{
+ setAutoApply(true);
+
+ useGlobalSettings.setSettingsKey(Constants::QODE_ASSIST_USE_GLOBAL_SETTINGS);
+ useGlobalSettings.setDefaultValue(true);
+
+ enableQodeAssist.setSettingsKey(Constants::QODE_ASSIST_ENABLE_IN_PROJECT);
+ enableQodeAssist.setDisplayName(Tr::tr("Enable Qode Assist"));
+ enableQodeAssist.setLabelText(Tr::tr("Enable Qode Assist"));
+ enableQodeAssist.setDefaultValue(false);
+
+ Utils::Store map = Utils::storeFromVariant(
+ project->namedSettings(Constants::QODE_ASSIST_PROJECT_SETTINGS_ID));
+ fromMap(map);
+
+ enableQodeAssist.addOnChanged(this, [this, project] { save(project); });
+ useGlobalSettings.addOnChanged(this, [this, project] { save(project); });
+}
+
+void ProjectSettings::setUseGlobalSettings(bool useGlobal)
+{
+ useGlobalSettings.setValue(useGlobal);
+}
+
+bool ProjectSettings::isEnabled() const
+{
+ if (useGlobalSettings())
+ return generalSettings().enableQodeAssist();
+ return enableQodeAssist();
+}
+
+void ProjectSettings::save(ProjectExplorer::Project *project)
+{
+ Utils::Store map;
+ toMap(map);
+ project
+ ->setNamedSettings(Constants::QODE_ASSIST_PROJECT_SETTINGS_ID, Utils::variantFromStore(map));
+
+ // This triggers a restart
+ generalSettings().apply();
+}
+
+} // namespace QodeAssist::Settings
diff --git a/settings/ProjectSettings.hpp b/settings/ProjectSettings.hpp
new file mode 100644
index 0000000..9fd35f2
--- /dev/null
+++ b/settings/ProjectSettings.hpp
@@ -0,0 +1,43 @@
+/*
+ * Copyright (C) 2024 Petr Mironychev
+ *
+ * This file is part of QodeAssist.
+ *
+ * QodeAssist 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.
+ *
+ * QodeAssist 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 QodeAssist. If not, see .
+ */
+
+#pragma once
+
+#include
+
+namespace ProjectExplorer {
+class Project;
+}
+
+namespace QodeAssist::Settings {
+
+class ProjectSettings : public Utils::AspectContainer
+{
+public:
+ explicit ProjectSettings(ProjectExplorer::Project *project);
+ void save(ProjectExplorer::Project *project);
+
+ void setUseGlobalSettings(bool useGlobalSettings);
+ bool isEnabled() const;
+
+ Utils::BoolAspect enableQodeAssist{this};
+ Utils::BoolAspect useGlobalSettings{this};
+};
+
+} // namespace QodeAssist::Settings
diff --git a/settings/ProjectSettingsPanel.cpp b/settings/ProjectSettingsPanel.cpp
new file mode 100644
index 0000000..8bfa3d1
--- /dev/null
+++ b/settings/ProjectSettingsPanel.cpp
@@ -0,0 +1,91 @@
+/*
+ * Copyright (C) 2024 Petr Mironychev
+ *
+ * This file is part of QodeAssist.
+ *
+ * QodeAssist 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.
+ *
+ * QodeAssist 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 QodeAssist. If not, see .
+ */
+
+#include "ProjectSettingsPanel.hpp"
+
+#include
+#include
+#include
+#include
+
+#include "ProjectSettings.hpp"
+#include "SettingsConstants.hpp"
+#include "SettingsTr.hpp"
+
+using namespace ProjectExplorer;
+
+namespace QodeAssist::Settings {
+
+class ProjectSettingsWidget final : public ProjectExplorer::ProjectSettingsWidget
+{
+public:
+ ProjectSettingsWidget()
+ {
+ setGlobalSettingsId(Constants::QODE_ASSIST_GENERAL_OPTIONS_ID);
+ setUseGlobalSettingsCheckBoxVisible(true);
+ }
+};
+
+static ProjectSettingsWidget *createProjectPanel(Project *project)
+{
+ using namespace Layouting;
+
+ auto widget = new ProjectSettingsWidget;
+ auto settings = new ProjectSettings(project);
+ settings->setParent(widget);
+
+ QObject::connect(
+ widget,
+ &ProjectSettingsWidget::useGlobalSettingsChanged,
+ settings,
+ &ProjectSettings::setUseGlobalSettings);
+
+ widget->setUseGlobalSettings(settings->useGlobalSettings());
+ widget->setEnabled(!settings->useGlobalSettings());
+
+ QObject::connect(
+ widget, &ProjectSettingsWidget::useGlobalSettingsChanged, widget, [widget](bool useGlobal) {
+ widget->setEnabled(!useGlobal);
+ });
+
+ Column{
+ settings->enableQodeAssist,
+ }
+ .attachTo(widget);
+
+ return widget;
+}
+
+class ProjectPanelFactory final : public ProjectExplorer::ProjectPanelFactory
+{
+public:
+ ProjectPanelFactory()
+ {
+ setPriority(1000);
+ setDisplayName(Tr::tr("Qode Assist"));
+ setCreateWidgetFunction(&createProjectPanel);
+ }
+};
+
+void setupProjectPanel()
+{
+ static ProjectPanelFactory theProjectPanelFactory;
+}
+
+} // namespace QodeAssist::Settings
diff --git a/settings/ProjectSettingsPanel.hpp b/settings/ProjectSettingsPanel.hpp
new file mode 100644
index 0000000..a6bf4d4
--- /dev/null
+++ b/settings/ProjectSettingsPanel.hpp
@@ -0,0 +1,26 @@
+/*
+ * Copyright (C) 2024 Petr Mironychev
+ *
+ * This file is part of QodeAssist.
+ *
+ * QodeAssist 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.
+ *
+ * QodeAssist 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 QodeAssist. If not, see .
+ */
+
+#pragma once
+
+namespace QodeAssist::Settings {
+
+void setupProjectPanel();
+
+} // namespace QodeAssist::Settings
diff --git a/settings/SettingsConstants.hpp b/settings/SettingsConstants.hpp
index 8eaf21a..6a83675 100644
--- a/settings/SettingsConstants.hpp
+++ b/settings/SettingsConstants.hpp
@@ -24,6 +24,12 @@ namespace QodeAssist::Constants {
const char ACTION_ID[] = "QodeAssist.Action";
const char MENU_ID[] = "QodeAssist.Menu";
+// project settings
+
+const char QODE_ASSIST_PROJECT_SETTINGS_ID[] = "QodeAssist.ProjectSettings";
+const char QODE_ASSIST_USE_GLOBAL_SETTINGS[] = "QodeAssist.UseGlobalSettings";
+const char QODE_ASSIST_ENABLE_IN_PROJECT[] = "QodeAssist.EnableInProject";
+
// new settings
const char CC_PROVIDER[] = "QodeAssist.ccProvider";
const char CC_MODEL[] = "QodeAssist.ccModel";