|
| 1 | +#include "kdevcxx_with_ai_config_page.h" |
| 2 | +#include <klocalizedstring.h> |
| 3 | +#include <qboxlayout.h> |
| 4 | +#include <qformlayout.h> |
| 5 | +#include <qicon.h> |
| 6 | +#include <qtabwidget.h> |
| 7 | + |
| 8 | +#ifndef Q_MOC_RUN |
| 9 | +#include <aiprocess/app_settings.h> |
| 10 | +#include <aiprocess/spdlog.hpp> |
| 11 | +#include <simple_enum/enum_cast.hpp> |
| 12 | +#include <simple_enum/enum_index.hpp> |
| 13 | +#include <aiprocess/log.h> |
| 14 | +#endif |
| 15 | + |
| 16 | +template<typename T> |
| 17 | +concept callable_with_no_args_returning_void = requires(T t) { |
| 18 | + { t() } -> std::same_as<void>; |
| 19 | +}; |
| 20 | + |
| 21 | +template<typename WidgetType> |
| 22 | +struct config_widget_creator_t |
| 23 | + { |
| 24 | + QWidget * parent; |
| 25 | + QFormLayout * form_layout; |
| 26 | + |
| 27 | + template<callable_with_no_args_returning_void Callable> |
| 28 | + auto operator()(QString const & label, Callable emit_changed) const -> WidgetType * |
| 29 | + { |
| 30 | + auto * widget = new WidgetType(parent); |
| 31 | + QObject::connect(widget, &WidgetType::textChanged, parent, emit_changed); |
| 32 | + form_layout->addRow(label, widget); |
| 33 | + return widget; |
| 34 | + } |
| 35 | + |
| 36 | + template<callable_with_no_args_returning_void Callable> |
| 37 | + auto operator()(QString const & label, QStringList const & logLevels, Callable emit_changed) const -> WidgetType * |
| 38 | + { |
| 39 | + auto * widget = new WidgetType(parent); |
| 40 | + QObject::connect( |
| 41 | + widget, static_cast<void (WidgetType::*)(int)>(&QComboBox::currentIndexChanged), parent, emit_changed |
| 42 | + ); |
| 43 | + form_layout->addRow(label, widget); |
| 44 | + widget->addItems(logLevels); |
| 45 | + return widget; |
| 46 | + } |
| 47 | + }; |
| 48 | + |
| 49 | +kdevcxx_with_ai_config_page::kdevcxx_with_ai_config_page(KDevelop::IPlugin * plugin, QWidget * parent) : |
| 50 | + KDevelop::ConfigPage(plugin, nullptr, parent) |
| 51 | + { |
| 52 | + QVBoxLayout * main_layout = new QVBoxLayout(this); |
| 53 | + QTabWidget * ai_tab_widget = new QTabWidget(this); |
| 54 | + main_layout->addWidget(ai_tab_widget); |
| 55 | + |
| 56 | + auto emit_changed = [this]<typename... Args>(Args &&...) { emit changed(); }; |
| 57 | + // Create a widget to hold the form layout |
| 58 | + QWidget * ai_form_widget = new QWidget(); |
| 59 | + QFormLayout * ai_form_layout = new QFormLayout(ai_form_widget); |
| 60 | + |
| 61 | + openai_key = config_widget_creator_t<QLineEdit>{ai_form_widget, ai_form_layout}("OpenAI Key:", emit_changed); |
| 62 | + open_ai_model = config_widget_creator_t<QLineEdit>{ai_form_widget, ai_form_layout}("OpenAI Model:", emit_changed); |
| 63 | + language_rules |
| 64 | + = config_widget_creator_t<QPlainTextEdit>{ai_form_widget, ai_form_layout}("Language Rules:", emit_changed); |
| 65 | + |
| 66 | + ai_tab_widget->addTab(ai_form_widget, i18n("AI Configuration")); |
| 67 | + |
| 68 | + // second page |
| 69 | + |
| 70 | + QWidget * log_form_widget = new QWidget(); |
| 71 | + QFormLayout * log_form_layout = new QFormLayout(log_form_widget); |
| 72 | + |
| 73 | + { |
| 74 | + config_widget_creator_t<QLineEdit> creator{log_form_widget, log_form_layout}; |
| 75 | + log_path_edit = creator("Log Path:", emit_changed); |
| 76 | + console_log_pattern_edit = creator("Console Log Pattern:", emit_changed); |
| 77 | + general_log_pattern_edit = creator("General Log Pattern:", emit_changed); |
| 78 | + snippet_log_pattern_edit = creator("Snippet Log Pattern:", emit_changed); |
| 79 | + important_log_pattern_edit = creator("Important Log Pattern:", emit_changed); |
| 80 | + } |
| 81 | + |
| 82 | + { |
| 83 | + QStringList logLevels = {"trace", "debug", "info", "warn", "error", "critical"}; |
| 84 | + config_widget_creator_t<QComboBox> creator{log_form_widget, log_form_layout}; |
| 85 | + console_log_level_combo = creator("Console Log Pattern:", logLevels, emit_changed); |
| 86 | + general_log_level_combo = creator("General Log Level:", logLevels, emit_changed); |
| 87 | + snippet_log_level_combo = creator("Snippet Log Pattern:", logLevels, emit_changed); |
| 88 | + important_log_level_combo = creator("Important Log Pattern:", logLevels, emit_changed); |
| 89 | + } |
| 90 | + |
| 91 | + flush_every_spin = new QSpinBox(log_form_widget); |
| 92 | + flush_every_spin->setMinimum(1); |
| 93 | + flush_every_spin->setMaximum(60); |
| 94 | + flush_every_spin->setValue(3); |
| 95 | + log_form_layout->addRow(i18n("Flush Every (seconds):"), flush_every_spin); |
| 96 | + |
| 97 | + activation_keys_spin = new QSpinBox(log_form_widget); |
| 98 | + activation_keys_spin->setMinimum(0); |
| 99 | + activation_keys_spin->setMaximum(std::numeric_limits<int>::max()); |
| 100 | + log_form_layout->addRow(i18n("Activation Keys:"), activation_keys_spin); |
| 101 | + |
| 102 | + ai_tab_widget->addTab(log_form_widget, i18n("Logging")); |
| 103 | + reset(); |
| 104 | + } |
| 105 | + |
| 106 | +QString kdevcxx_with_ai_config_page::name() const { return i18n("OpenAI Configuration"); } |
| 107 | + |
| 108 | +QString kdevcxx_with_ai_config_page::fullName() const { return i18n("Configure Open AI Settings"); } |
| 109 | + |
| 110 | +QIcon kdevcxx_with_ai_config_page::icon() const { return QIcon::fromTheme("preferences-other"); } |
| 111 | + |
| 112 | +void kdevcxx_with_ai_config_page::apply() |
| 113 | + { |
| 114 | + aiprocess::debug("apply() called"); |
| 115 | + aiprocess::store_ai_settings(aiprocess::ai_settings_t{ |
| 116 | + .api_key = openai_key->text().toStdString(), |
| 117 | + .cxx_rules = language_rules->toPlainText().toStdString(), |
| 118 | + .gpt_model = open_ai_model->text().toStdString() |
| 119 | + }); |
| 120 | + using level_enum = aiprocess::app_settings_t::level_enum; |
| 121 | + aiprocess::store_app_settings(aiprocess::app_settings_t{ |
| 122 | + .log_path = log_path_edit->text().toStdString(), |
| 123 | + .console_log_pattern = console_log_pattern_edit->text().toStdString(), |
| 124 | + .general_log_pattern = general_log_pattern_edit->text().toStdString(), |
| 125 | + .snippet_log_pattern = snippet_log_pattern_edit->text().toStdString(), |
| 126 | + .important_log_pattern = important_log_pattern_edit->text().toStdString(), |
| 127 | + .console_log_level = level_enum(console_log_level_combo->currentIndex()), |
| 128 | + .general_log_level = level_enum(general_log_level_combo->currentIndex()), |
| 129 | + .snippet_log_level = level_enum(snippet_log_level_combo->currentIndex()), |
| 130 | + .important_log_level = level_enum(important_log_level_combo->currentIndex()), |
| 131 | + .activation_keys = activation_keys_spin->value() |
| 132 | + }); |
| 133 | + } |
| 134 | + |
| 135 | +void kdevcxx_with_ai_config_page::reset() |
| 136 | + { |
| 137 | + aiprocess::debug("reset() called"); |
| 138 | + { |
| 139 | + aiprocess::ai_settings_t settings = aiprocess::load_ai_settings(); |
| 140 | + language_rules->setPlainText(QString::fromStdString(settings.cxx_rules)); |
| 141 | + openai_key->setText(QString::fromStdString(settings.api_key)); |
| 142 | + open_ai_model->setText(QString::fromStdString(settings.gpt_model)); |
| 143 | + } |
| 144 | + |
| 145 | + // Second page settings |
| 146 | + |
| 147 | + { |
| 148 | + aiprocess::app_settings_t settings{aiprocess::load_app_settings()}; |
| 149 | + log_path_edit->setText(QString::fromStdString(settings.log_path)); |
| 150 | + console_log_pattern_edit->setText(QString::fromStdString(settings.console_log_pattern)); |
| 151 | + general_log_pattern_edit->setText(QString::fromStdString(settings.general_log_pattern)); |
| 152 | + snippet_log_pattern_edit->setText(QString::fromStdString(settings.snippet_log_pattern)); |
| 153 | + important_log_pattern_edit->setText(QString::fromStdString(settings.important_log_pattern)); |
| 154 | + |
| 155 | + auto set_combo_index = [](QComboBox * comboBox, auto enumValue) |
| 156 | + { |
| 157 | + auto ix{simple_enum::enum_index(enumValue)}; |
| 158 | + if(ix.has_value()) |
| 159 | + comboBox->setCurrentIndex(*ix); |
| 160 | + }; |
| 161 | + set_combo_index(console_log_level_combo, settings.console_log_level); |
| 162 | + set_combo_index(general_log_level_combo, settings.general_log_level); |
| 163 | + set_combo_index(snippet_log_level_combo, settings.snippet_log_level); |
| 164 | + set_combo_index(important_log_level_combo, settings.important_log_level); |
| 165 | + |
| 166 | + activation_keys_spin->setValue(settings.activation_keys); |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | +void kdevcxx_with_ai_config_page::defaults() |
| 171 | + { |
| 172 | + aiprocess::debug("defaults() called"); |
| 173 | + language_rules->setPlainText(QString::fromUtf8(aiprocess::default_ai_rules.data(), aiprocess::default_ai_rules.size()) |
| 174 | + ); |
| 175 | + open_ai_model->setText(QString::fromUtf8(aiprocess::default_gpt_model.data(), aiprocess::default_gpt_model.size())); |
| 176 | + } |
0 commit comments