Files
Schedule/client/src/ui/dialogs/SettingsDialog.cpp
T
Misaki 795f9730f8 feat: add desktop start and native reminders
- Notify at schedule start and optionally N minutes before\n- Add reminder lead setting and TOML persistence\n- Use Linux DBus native notifications with action handling\n- Focus the matching block when a notification is clicked\n- Keep overdue reminders and update validation docs/screenshots
2026-10-26 21:16:07 +08:00

335 lines
13 KiB
C++

#include "SettingsDialog.h"
#include <QFormLayout>
#include <QFrame>
#include <QHBoxLayout>
#include <QLineEdit>
#include <QVBoxLayout>
#include <QTimer>
#include "ElaComboBox.h"
#include "ElaLineEdit.h"
#include "ElaPushButton.h"
#include "ElaText.h"
#include "ElaToggleSwitch.h"
#include "ElaToolButton.h"
#include "ui/theme/AnimationUtils.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, 820);
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* reminderDescription = new ElaText(
QStringLiteral("日程开始时发送系统通知;可额外提前 N 分钟再提醒一次。"),
11,
this);
reminderDescription->setTextStyle(ElaTextType::Caption);
reminderDescription->setWordWrap(true);
root->addWidget(reminderDescription);
auto* reminderRow = new QHBoxLayout();
reminderRow->setSpacing(16);
auto* reminderLabel = new ElaText(QStringLiteral("提前提醒"), 13, this);
m_reminderLeadCombo = new ElaComboBox(this);
const QList<int> leadValues = {0, 5, 10, 15, 30, 60};
for (int minutes : leadValues)
{
m_reminderLeadCombo->addItem(minutes == 0 ? QStringLiteral("仅开始时")
: QStringLiteral("提前 %1 分钟").arg(minutes));
}
int leadIndex = leadValues.indexOf(m_settings.reminderLeadMinutes());
if (leadIndex < 0)
{
leadIndex = 2;
}
m_reminderLeadCombo->setCurrentIndex(leadIndex);
m_reminderLeadCombo->setFixedSize(180, 32);
reminderRow->addWidget(reminderLabel, 0, Qt::AlignVCenter);
reminderRow->addWidget(m_reminderLeadCombo, 0, Qt::AlignVCenter);
reminderRow->addStretch(1);
root->addLayout(reminderRow);
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_reminderLeadCombo, &ElaComboBox::currentIndexChanged, this, [this](int index) {
static const int values[] = {0, 5, 10, 15, 30, 60};
if (index >= 0 && index < 6)
{
m_settings.setReminderLeadMinutes(values[index]);
Q_EMIT reminderSettingsChanged();
}
});
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();
});
QTimer::singleShot(0, this, [this]() { AnimationUtils::fadeWindow(this); });
}
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"));
}