feat: implement AI schedule app with multi-device sync
- Qt 6 desktop client with day/month/year views, AI scheduling, and TOML settings\n- Go sync server with SQLite change stream, WebSocket notifications, and Docker deployment\n- Client outbox/LWW sync, offline retry, and dual-client integration tests\n- Linux fcitx5 setup, validation reports, and Windows MinGW preset
This commit is contained in:
@@ -0,0 +1,290 @@
|
||||
#include "SettingsDialog.h"
|
||||
|
||||
#include <QFormLayout>
|
||||
#include <QFrame>
|
||||
#include <QHBoxLayout>
|
||||
#include <QLineEdit>
|
||||
#include <QVBoxLayout>
|
||||
|
||||
#include "ElaComboBox.h"
|
||||
#include "ElaLineEdit.h"
|
||||
#include "ElaPushButton.h"
|
||||
#include "ElaText.h"
|
||||
#include "ElaToggleSwitch.h"
|
||||
#include "ElaToolButton.h"
|
||||
#include "ui/theme/ThemeManager.h"
|
||||
#include "ui/theme/WindowEffects.h"
|
||||
#include "utils/AppSettings.h"
|
||||
|
||||
SettingsDialog::SettingsDialog(ThemeManager& themeManager, AppSettings& settings, QWidget* parent)
|
||||
: ElaDialog(parent)
|
||||
, m_themeManager(themeManager)
|
||||
, m_settings(settings)
|
||||
{
|
||||
setWindowTitle(QStringLiteral("设置"));
|
||||
setWindowButtonFlags(ElaAppBarType::CloseButtonHint);
|
||||
resize(560, 720);
|
||||
setMinimumWidth(520);
|
||||
|
||||
auto* root = new QVBoxLayout(this);
|
||||
root->setContentsMargins(24, 8, 24, 18);
|
||||
root->setSpacing(10);
|
||||
|
||||
const auto addSectionTitle = [this, root](const QString& text) {
|
||||
auto* title = new ElaText(text, 14, this);
|
||||
QFont titleFont = title->font();
|
||||
titleFont.setWeight(QFont::DemiBold);
|
||||
title->setFont(titleFont);
|
||||
root->addWidget(title);
|
||||
};
|
||||
const auto addDivider = [this, root]() {
|
||||
auto* divider = new QFrame(this);
|
||||
divider->setFrameShape(QFrame::HLine);
|
||||
divider->setFrameShadow(QFrame::Plain);
|
||||
root->addWidget(divider);
|
||||
};
|
||||
|
||||
addSectionTitle(QStringLiteral("外观"));
|
||||
|
||||
auto* themeRow = new QHBoxLayout();
|
||||
themeRow->setSpacing(16);
|
||||
auto* themeLabel = new ElaText(QStringLiteral("主题"), 13, this);
|
||||
m_themeCombo = new ElaComboBox(this);
|
||||
m_themeCombo->addItems({QStringLiteral("跟随系统"), QStringLiteral("浅色"), QStringLiteral("深色")});
|
||||
m_themeCombo->setFixedSize(150, 32);
|
||||
switch (m_themeManager.preference())
|
||||
{
|
||||
case ThemeManager::Preference::Light:
|
||||
m_themeCombo->setCurrentIndex(1);
|
||||
break;
|
||||
case ThemeManager::Preference::Dark:
|
||||
m_themeCombo->setCurrentIndex(2);
|
||||
break;
|
||||
case ThemeManager::Preference::System:
|
||||
default:
|
||||
m_themeCombo->setCurrentIndex(0);
|
||||
break;
|
||||
}
|
||||
themeRow->addWidget(themeLabel, 0, Qt::AlignVCenter);
|
||||
themeRow->addWidget(m_themeCombo, 0, Qt::AlignVCenter);
|
||||
themeRow->addStretch(1);
|
||||
root->addLayout(themeRow);
|
||||
|
||||
auto* micaRow = new QHBoxLayout();
|
||||
micaRow->setSpacing(16);
|
||||
auto* micaTextLayout = new QVBoxLayout();
|
||||
micaTextLayout->setSpacing(2);
|
||||
auto* micaTitle = new ElaText(QStringLiteral("窗口云母效果"), 13, this);
|
||||
auto* micaDescription = new ElaText(
|
||||
QStringLiteral("使用 ElaWidgetTools 的 ElaMica 背景(跨平台模拟;Windows 可扩展系统 Mica)。"),
|
||||
11,
|
||||
this);
|
||||
micaDescription->setTextStyle(ElaTextType::Caption);
|
||||
micaDescription->setWordWrap(true);
|
||||
micaDescription->setMaximumWidth(350);
|
||||
micaTextLayout->addWidget(micaTitle);
|
||||
micaTextLayout->addWidget(micaDescription);
|
||||
|
||||
m_micaSwitch = new ElaToggleSwitch(this);
|
||||
m_micaSwitch->setIsToggled(m_settings.micaEnabled());
|
||||
micaRow->addLayout(micaTextLayout, 1);
|
||||
micaRow->addWidget(m_micaSwitch, 0, Qt::AlignVCenter);
|
||||
root->addLayout(micaRow);
|
||||
|
||||
addDivider();
|
||||
addSectionTitle(QStringLiteral("AI 服务"));
|
||||
|
||||
auto* aiDescription = new ElaText(
|
||||
QStringLiteral("配置 OpenAI 兼容接口(云端或本地服务)。AI 排程与对话会使用此处配置。"),
|
||||
11,
|
||||
this);
|
||||
aiDescription->setTextStyle(ElaTextType::Caption);
|
||||
aiDescription->setWordWrap(true);
|
||||
root->addWidget(aiDescription);
|
||||
|
||||
const AiConfig aiConfig = m_settings.aiConfig();
|
||||
auto* aiForm = new QFormLayout();
|
||||
aiForm->setHorizontalSpacing(14);
|
||||
aiForm->setVerticalSpacing(10);
|
||||
aiForm->setFieldGrowthPolicy(QFormLayout::ExpandingFieldsGrow);
|
||||
aiForm->setLabelAlignment(Qt::AlignLeft | Qt::AlignVCenter);
|
||||
|
||||
auto* baseUrlLabel = new ElaText(QStringLiteral("接口地址"), 13, this);
|
||||
m_baseUrlEdit = new ElaLineEdit(this);
|
||||
m_baseUrlEdit->setPlaceholderText(QStringLiteral("https://api.openai.com/v1"));
|
||||
m_baseUrlEdit->setText(aiConfig.baseUrl);
|
||||
m_baseUrlEdit->setClearButtonEnabled(true);
|
||||
m_baseUrlEdit->setMinimumHeight(34);
|
||||
|
||||
auto* modelLabel = new ElaText(QStringLiteral("模型"), 13, this);
|
||||
m_modelEdit = new ElaLineEdit(this);
|
||||
m_modelEdit->setPlaceholderText(QStringLiteral("gpt-4o-mini"));
|
||||
m_modelEdit->setText(aiConfig.model);
|
||||
m_modelEdit->setClearButtonEnabled(true);
|
||||
m_modelEdit->setMinimumHeight(34);
|
||||
|
||||
auto* apiKeyLabel = new ElaText(QStringLiteral("API Key"), 13, this);
|
||||
auto* keyRow = new QHBoxLayout();
|
||||
keyRow->setSpacing(6);
|
||||
m_apiKeyEdit = new ElaLineEdit(this);
|
||||
m_apiKeyEdit->setPlaceholderText(QStringLiteral("sk-..."));
|
||||
m_apiKeyEdit->setText(aiConfig.apiKey);
|
||||
m_apiKeyEdit->setEchoMode(QLineEdit::Password);
|
||||
m_apiKeyEdit->setClearButtonEnabled(true);
|
||||
m_apiKeyEdit->setMinimumHeight(34);
|
||||
m_keyVisibilityButton = new ElaToolButton(this);
|
||||
m_keyVisibilityButton->setIsTransparent(true);
|
||||
m_keyVisibilityButton->setElaIcon(ElaIconType::Eye);
|
||||
m_keyVisibilityButton->setFixedSize(30, 30);
|
||||
m_keyVisibilityButton->setToolTip(QStringLiteral("显示 API Key"));
|
||||
keyRow->addWidget(m_apiKeyEdit, 1);
|
||||
keyRow->addWidget(m_keyVisibilityButton, 0, Qt::AlignVCenter);
|
||||
|
||||
aiForm->addRow(baseUrlLabel, m_baseUrlEdit);
|
||||
aiForm->addRow(modelLabel, m_modelEdit);
|
||||
aiForm->addRow(apiKeyLabel, keyRow);
|
||||
root->addLayout(aiForm);
|
||||
|
||||
auto* keyNote = new ElaText(
|
||||
QStringLiteral("API Key 仅保存在本地配置文件,文件权限设为 0600,不会写入日志。"),
|
||||
11,
|
||||
this);
|
||||
keyNote->setTextStyle(ElaTextType::Caption);
|
||||
keyNote->setWordWrap(true);
|
||||
root->addWidget(keyNote);
|
||||
|
||||
addDivider();
|
||||
addSectionTitle(QStringLiteral("同步服务"));
|
||||
|
||||
auto* syncDescription = new ElaText(
|
||||
QStringLiteral("自部署 Go 同步服务端。首次启用会自动注册设备并保存访问令牌。"),
|
||||
11,
|
||||
this);
|
||||
syncDescription->setTextStyle(ElaTextType::Caption);
|
||||
syncDescription->setWordWrap(true);
|
||||
root->addWidget(syncDescription);
|
||||
|
||||
const SyncConfig initialSync = m_settings.syncConfig();
|
||||
auto* syncUrlRow = new QHBoxLayout();
|
||||
syncUrlRow->setSpacing(12);
|
||||
auto* syncUrlLabel = new ElaText(QStringLiteral("服务地址"), 13, this);
|
||||
m_syncUrlEdit = new ElaLineEdit(this);
|
||||
m_syncUrlEdit->setPlaceholderText(QStringLiteral("http://127.0.0.1:8787"));
|
||||
m_syncUrlEdit->setText(initialSync.serverUrl);
|
||||
m_syncUrlEdit->setClearButtonEnabled(true);
|
||||
m_syncUrlEdit->setMinimumHeight(34);
|
||||
syncUrlRow->addWidget(syncUrlLabel, 0, Qt::AlignVCenter);
|
||||
syncUrlRow->addWidget(m_syncUrlEdit, 1);
|
||||
root->addLayout(syncUrlRow);
|
||||
|
||||
auto* syncToggleRow = new QHBoxLayout();
|
||||
syncToggleRow->setSpacing(16);
|
||||
auto* syncToggleText = new QVBoxLayout();
|
||||
syncToggleText->setSpacing(2);
|
||||
auto* syncToggleTitle = new ElaText(QStringLiteral("启用多设备同步"), 13, this);
|
||||
auto* syncToggleDescription = new ElaText(
|
||||
initialSync.token.isEmpty() ? QStringLiteral("当前尚未注册设备;启用后自动注册。")
|
||||
: QStringLiteral("设备已注册;修改地址或开关后立即重连。"),
|
||||
11,
|
||||
this);
|
||||
syncToggleDescription->setTextStyle(ElaTextType::Caption);
|
||||
syncToggleText->addWidget(syncToggleTitle);
|
||||
syncToggleText->addWidget(syncToggleDescription);
|
||||
m_syncEnabledSwitch = new ElaToggleSwitch(this);
|
||||
m_syncEnabledSwitch->setIsToggled(initialSync.enabled);
|
||||
syncToggleRow->addLayout(syncToggleText, 1);
|
||||
syncToggleRow->addWidget(m_syncEnabledSwitch, 0, Qt::AlignVCenter);
|
||||
root->addLayout(syncToggleRow);
|
||||
|
||||
auto* pathNote = new ElaText(QStringLiteral("配置文件:%1").arg(m_settings.configPath()), 11, this);
|
||||
pathNote->setTextStyle(ElaTextType::Caption);
|
||||
pathNote->setWordWrap(true);
|
||||
pathNote->setTextInteractionFlags(Qt::TextSelectableByMouse);
|
||||
root->addWidget(pathNote);
|
||||
|
||||
root->addStretch(1);
|
||||
|
||||
auto* buttonRow = new QHBoxLayout();
|
||||
buttonRow->addStretch(1);
|
||||
auto* doneButton = new ElaPushButton(QStringLiteral("完成"), this);
|
||||
doneButton->setFixedSize(88, 34);
|
||||
doneButton->setDefault(true);
|
||||
buttonRow->addWidget(doneButton);
|
||||
root->addLayout(buttonRow);
|
||||
|
||||
connect(m_themeCombo, &ElaComboBox::currentIndexChanged, this, [this](int index) {
|
||||
switch (index)
|
||||
{
|
||||
case 1:
|
||||
m_themeManager.setPreference(ThemeManager::Preference::Light);
|
||||
break;
|
||||
case 2:
|
||||
m_themeManager.setPreference(ThemeManager::Preference::Dark);
|
||||
break;
|
||||
case 0:
|
||||
default:
|
||||
m_themeManager.setPreference(ThemeManager::Preference::System);
|
||||
break;
|
||||
}
|
||||
});
|
||||
connect(m_micaSwitch, &ElaToggleSwitch::toggled, this, [this](bool enabled) {
|
||||
m_settings.setMicaEnabled(enabled);
|
||||
WindowEffects::applyMica(enabled);
|
||||
Q_EMIT micaChanged(enabled);
|
||||
});
|
||||
connect(m_baseUrlEdit, &ElaLineEdit::editingFinished, this, &SettingsDialog::saveAiConfig);
|
||||
connect(m_modelEdit, &ElaLineEdit::editingFinished, this, &SettingsDialog::saveAiConfig);
|
||||
connect(m_apiKeyEdit, &ElaLineEdit::editingFinished, this, &SettingsDialog::saveAiConfig);
|
||||
connect(m_keyVisibilityButton, &ElaToolButton::clicked, this, &SettingsDialog::toggleKeyVisibility);
|
||||
connect(m_syncUrlEdit, &ElaLineEdit::editingFinished, this, &SettingsDialog::saveSyncConfig);
|
||||
connect(m_syncEnabledSwitch, &ElaToggleSwitch::toggled, this, [this](bool) { saveSyncConfig(); });
|
||||
connect(doneButton, &ElaPushButton::clicked, this, [this]() {
|
||||
saveAiConfig();
|
||||
saveSyncConfig();
|
||||
accept();
|
||||
});
|
||||
connect(this, &QDialog::finished, this, [this]() {
|
||||
saveAiConfig();
|
||||
saveSyncConfig();
|
||||
});
|
||||
}
|
||||
|
||||
void SettingsDialog::saveAiConfig()
|
||||
{
|
||||
if (!m_baseUrlEdit || !m_modelEdit || !m_apiKeyEdit)
|
||||
{
|
||||
return;
|
||||
}
|
||||
AiConfig config = m_settings.aiConfig();
|
||||
config.baseUrl = m_baseUrlEdit->text().trimmed();
|
||||
config.model = m_modelEdit->text().trimmed();
|
||||
config.apiKey = m_apiKeyEdit->text().trimmed();
|
||||
m_settings.setAiConfig(config);
|
||||
}
|
||||
|
||||
void SettingsDialog::saveSyncConfig()
|
||||
{
|
||||
if (!m_syncUrlEdit || !m_syncEnabledSwitch)
|
||||
{
|
||||
return;
|
||||
}
|
||||
SyncConfig config = m_settings.syncConfig();
|
||||
const QString url = m_syncUrlEdit->text().trimmed();
|
||||
const bool enabled = m_syncEnabledSwitch->getIsToggled();
|
||||
if (config.serverUrl == url && config.enabled == enabled)
|
||||
{
|
||||
return;
|
||||
}
|
||||
config.serverUrl = url;
|
||||
config.enabled = enabled;
|
||||
m_settings.setSyncConfig(config);
|
||||
Q_EMIT syncSettingsChanged();
|
||||
}
|
||||
|
||||
void SettingsDialog::toggleKeyVisibility()
|
||||
{
|
||||
const bool hidden = m_apiKeyEdit->echoMode() == QLineEdit::Password;
|
||||
m_apiKeyEdit->setEchoMode(hidden ? QLineEdit::Normal : QLineEdit::Password);
|
||||
m_keyVisibilityButton->setElaIcon(hidden ? ElaIconType::EyeSlash : ElaIconType::Eye);
|
||||
m_keyVisibilityButton->setToolTip(hidden ? QStringLiteral("隐藏 API Key") : QStringLiteral("显示 API Key"));
|
||||
}
|
||||
Reference in New Issue
Block a user