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:
Misaki
2026-10-26 13:58:45 +08:00
commit 5be7f7f04b
151 changed files with 38485 additions and 0 deletions
+946
View File
@@ -0,0 +1,946 @@
#include "MainWindow.h"
#include <QApplication>
#include <QCloseEvent>
#include <QComboBox>
#include <QCoreApplication>
#include <QDateTime>
#include <QDialog>
#include <QFrame>
#include <QHBoxLayout>
#include <QPainter>
#include <QKeySequence>
#include <QLabel>
#include <QShortcut>
#include <QShowEvent>
#include <QSignalBlocker>
#include <QSplitter>
#include <QStackedWidget>
#include <QTime>
#include <QTimer>
#include <QVBoxLayout>
#include <algorithm>
#include "ElaComboBox.h"
#include "ElaLineEdit.h"
#include "ElaMessageBar.h"
#include "ElaPivot.h"
#include "ElaPushButton.h"
#include "ElaText.h"
#include "ElaToolButton.h"
#include "core/DateTimeUtils.h"
#include "services/NotificationService.h"
#include "services/ScheduleService.h"
#include "sync/SyncService.h"
#include "ui/dialogs/BlockEditDialog.h"
#include "ui/dialogs/SettingsDialog.h"
#include "ui/panels/TodayPanel.h"
#include "ui/theme/ThemeManager.h"
#include "ui/theme/WindowEffects.h"
#include "ui/views/MonthView.h"
#include "ui/views/ScheduleGridView.h"
#include "ui/views/YearView.h"
#include "utils/AppSettings.h"
namespace
{
QString weekdayLabel(const QDate& date)
{
static const char* labels[] = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
return QString::fromUtf8(labels[date.dayOfWeek() % 7]);
}
} // namespace
MainWindow::MainWindow(ScheduleService& scheduleService,
NotificationService& notificationService,
ThemeManager& themeManager,
AppSettings& settings,
SyncService* syncService,
QWidget* parent)
: ElaWindow(parent)
, m_scheduleService(scheduleService)
, m_notificationService(notificationService)
, m_themeManager(themeManager)
, m_settings(settings)
, m_syncService(syncService)
, m_currentDate(QDate::currentDate())
{
setWindowTitle(QStringLiteral("详细日程"));
setAppBarHeight(44);
setIsNavigationBarEnable(false);
setWindowButtonFlag(ElaAppBarType::RouteBackButtonHint, false);
setWindowButtonFlag(ElaAppBarType::RouteForwardButtonHint, false);
setWindowButtonFlag(ElaAppBarType::ThemeChangeButtonHint, false);
setWindowButtonFlag(ElaAppBarType::NavigationButtonHint, false);
setMinimumSize(1080, 680);
resize(1280, 820);
buildUi();
applyTheme(m_themeManager.tokens());
connectSignals();
restoreWindowState();
setView(View::Day);
updateOverdueBanner(m_notificationService.overdueItems());
connect(qApp, &QCoreApplication::aboutToQuit, this, &MainWindow::saveWindowState);
}
MainWindow::~MainWindow() = default;
void MainWindow::buildUi()
{
auto* root = new QWidget(this);
auto* rootLayout = new QVBoxLayout(root);
rootLayout->setContentsMargins(10, 6, 10, 10);
rootLayout->setSpacing(8);
m_toolbar = new QFrame(root);
m_toolbar->setObjectName(QStringLiteral("Toolbar"));
auto* toolbarLayout = new QHBoxLayout(m_toolbar);
toolbarLayout->setContentsMargins(10, 6, 10, 6);
toolbarLayout->setSpacing(6);
m_compactButton = new ElaToolButton(m_toolbar);
m_compactButton->setIsTransparent(true);
m_compactButton->setElaIcon(ElaIconType::House);
m_compactButton->setFixedSize(30, 30);
m_compactButton->setToolTip(QStringLiteral("返回紧凑视图 (Ctrl+W)"));
m_todayButton = new ElaPushButton(QStringLiteral("今天"), m_toolbar);
m_todayButton->setFixedSize(58, 30);
m_todayButton->setToolTip(QStringLiteral("回到今天 (Ctrl+T)"));
m_prevButton = new ElaToolButton(m_toolbar);
m_prevButton->setIsTransparent(true);
m_prevButton->setElaIcon(ElaIconType::ArrowLeft);
m_prevButton->setFixedSize(30, 30);
m_prevButton->setToolTip(QStringLiteral("上一段 (Alt+←)"));
m_nextButton = new ElaToolButton(m_toolbar);
m_nextButton->setIsTransparent(true);
m_nextButton->setElaIcon(ElaIconType::ArrowRight);
m_nextButton->setFixedSize(30, 30);
m_nextButton->setToolTip(QStringLiteral("下一段 (Alt+→)"));
m_dateLabel = new ElaText(m_toolbar);
m_dateLabel->setTextPixelSize(15);
QFont dateFont = m_dateLabel->font();
dateFont.setWeight(QFont::DemiBold);
m_dateLabel->setFont(dateFont);
m_viewPivot = new ElaPivot(m_toolbar);
m_viewPivot->setTextPixelSize(14);
m_viewPivot->setPivotSpacing(18);
m_viewPivot->setFixedSize(174, 30);
m_viewPivot->appendPivot(QStringLiteral("日"));
m_viewPivot->appendPivot(QStringLiteral("月"));
m_viewPivot->appendPivot(QStringLiteral("年"));
m_viewPivot->setCurrentIndex(0);
m_viewPivot->setToolTip(QStringLiteral("切换日 / 月 / 年视图 (Ctrl+1/2/3)"));
m_newButton = new ElaPushButton(QStringLiteral("新建日程"), m_toolbar);
m_newButton->setFixedSize(96, 32);
m_newButton->setToolTip(QStringLiteral("新建日程 (Ctrl+N)"));
m_searchEdit = new ElaLineEdit(m_toolbar);
m_searchEdit->setPlaceholderText(QStringLiteral("搜索(阶段 4)"));
m_searchEdit->setFixedSize(140, 30);
m_searchEdit->setEnabled(false);
m_searchEdit->setToolTip(QStringLiteral("搜索将在阶段 4 接入"));
m_snapCombo = new ElaComboBox(m_toolbar);
m_snapCombo->addItems({QStringLiteral("吸附 15 分"),
QStringLiteral("吸附 30 分"),
QStringLiteral("吸附 60 分")});
m_snapCombo->setFixedSize(100, 30);
m_snapCombo->setToolTip(QStringLiteral("时间网格吸附粒度"));
m_themeButton = new ElaToolButton(m_toolbar);
m_themeButton->setIsTransparent(true);
m_themeButton->setElaIcon(ElaIconType::CircleHalfStroke);
m_themeButton->setFixedSize(30, 30);
m_settingsButton = new ElaToolButton(m_toolbar);
m_settingsButton->setIsTransparent(true);
m_settingsButton->setElaIcon(ElaIconType::Gear);
m_settingsButton->setFixedSize(30, 30);
m_settingsButton->setToolTip(QStringLiteral("设置 (Ctrl+,)"));
m_syncLabel = new QLabel(QStringLiteral("● 本地"), m_toolbar);
m_syncLabel->setFixedHeight(24);
m_syncLabel->setToolTip(QStringLiteral("本地模式"));
toolbarLayout->addWidget(m_compactButton);
toolbarLayout->addWidget(m_todayButton);
toolbarLayout->addWidget(m_prevButton);
toolbarLayout->addWidget(m_nextButton);
toolbarLayout->addWidget(m_viewPivot);
toolbarLayout->addWidget(m_dateLabel);
toolbarLayout->addSpacing(6);
toolbarLayout->addWidget(m_newButton);
toolbarLayout->addStretch(1);
toolbarLayout->addWidget(m_searchEdit);
toolbarLayout->addWidget(m_snapCombo);
toolbarLayout->addWidget(m_themeButton);
toolbarLayout->addWidget(m_settingsButton);
toolbarLayout->addWidget(m_syncLabel);
rootLayout->addWidget(m_toolbar);
m_mainSplitter = new QSplitter(Qt::Horizontal, root);
m_mainSplitter->setChildrenCollapsible(false);
m_mainSplitter->setHandleWidth(6);
auto* calendarContainer = new QWidget(m_mainSplitter);
auto* calendarLayout = new QVBoxLayout(calendarContainer);
calendarLayout->setContentsMargins(0, 0, 0, 0);
calendarLayout->setSpacing(6);
m_overdueBanner = new QFrame(calendarContainer);
m_overdueBanner->setObjectName(QStringLiteral("OverdueBanner"));
auto* bannerLayout = new QHBoxLayout(m_overdueBanner);
bannerLayout->setContentsMargins(12, 6, 8, 6);
bannerLayout->setSpacing(10);
m_overdueLabel = new QLabel(m_overdueBanner);
auto* handleButton = new ElaPushButton(QStringLiteral("处理"), m_overdueBanner);
handleButton->setFixedSize(64, 26);
bannerLayout->addWidget(m_overdueLabel, 1);
bannerLayout->addWidget(handleButton, 0, Qt::AlignVCenter);
m_overdueBanner->setVisible(false);
m_viewStack = new QStackedWidget(calendarContainer);
m_grid = new ScheduleGridView(m_viewStack);
m_monthView = new MonthView(m_viewStack);
m_yearView = new YearView(m_viewStack);
m_viewStack->addWidget(m_grid);
m_viewStack->addWidget(m_monthView);
m_viewStack->addWidget(m_yearView);
m_hintLabel = new ElaText(QString(), 11, calendarContainer);
m_hintLabel->setTextStyle(ElaTextType::Caption);
m_hintLabel->setContentsMargins(4, 0, 0, 0);
calendarLayout->addWidget(m_overdueBanner);
calendarLayout->addWidget(m_viewStack, 1);
calendarLayout->addWidget(m_hintLabel);
m_rightDock = new QFrame(m_mainSplitter);
m_rightDock->setObjectName(QStringLiteral("RightDock"));
auto* rightLayout = new QVBoxLayout(m_rightDock);
rightLayout->setContentsMargins(6, 6, 6, 6);
rightLayout->setSpacing(0);
m_todayPanel = new TodayPanel(m_rightDock);
rightLayout->addWidget(m_todayPanel);
m_mainSplitter->addWidget(calendarContainer);
m_mainSplitter->addWidget(m_rightDock);
m_mainSplitter->setStretchFactor(0, 1);
m_mainSplitter->setStretchFactor(1, 0);
m_mainSplitter->setSizes({900, 320});
rootLayout->addWidget(m_mainSplitter, 1);
addCentralWidget(root);
setStackSwitchMode(ElaWindowType::StackSwitchMode::None);
setCurrentStackIndex(1);
connect(m_compactButton, &ElaToolButton::clicked, this, &MainWindow::close);
connect(m_todayButton, &ElaPushButton::clicked, this, [this]() { navigateToDate(QDate::currentDate()); });
connect(m_prevButton, &ElaToolButton::clicked, this, [this]() { stepDate(-1); });
connect(m_nextButton, &ElaToolButton::clicked, this, [this]() { stepDate(1); });
connect(m_newButton, &ElaPushButton::clicked, this, &MainWindow::openCreateDialogForNew);
connect(m_themeButton, &ElaToolButton::clicked, this, [this]() { m_themeManager.cyclePreference(); });
connect(m_settingsButton, &ElaToolButton::clicked, this, &MainWindow::openSettings);
if (m_syncService)
{
connect(m_syncService, &SyncService::statusChanged, this, &MainWindow::updateSyncStatus);
updateSyncStatus(m_syncService->statusText());
}
else
{
updateSyncStatus(QStringLiteral("本地"));
}
connect(m_snapCombo, &ElaComboBox::currentIndexChanged, this, [this](int index) {
static const int values[] = {15, 30, 60};
if (index >= 0 && index < 3 && m_grid)
{
m_grid->setSnapMinutes(values[index]);
m_settings.setSnapMinutes(values[index]);
}
});
connect(m_viewPivot, &ElaPivot::pivotClicked, this, [this](int index) {
if (index >= 0 && index <= 2)
{
setView(static_cast<View>(index));
}
});
connect(handleButton, &ElaPushButton::clicked, this, [this]() {
const QVector<ScheduleItem> items = m_notificationService.overdueItems();
if (items.isEmpty())
{
return;
}
const ScheduleItem& first = items.first();
m_currentDate = first.block.startAt.date();
setView(View::Day);
m_grid->scrollToBlock(first.block.id);
m_grid->setFocus(Qt::OtherFocusReason);
});
auto* newShortcut = new QShortcut(QKeySequence::New, this);
connect(newShortcut, &QShortcut::activated, this, &MainWindow::openCreateDialogForNew);
auto* prevShortcut = new QShortcut(QKeySequence(QStringLiteral("Alt+Left")), this);
connect(prevShortcut, &QShortcut::activated, this, [this]() { stepDate(-1); });
auto* nextShortcut = new QShortcut(QKeySequence(QStringLiteral("Alt+Right")), this);
connect(nextShortcut, &QShortcut::activated, this, [this]() { stepDate(1); });
auto* todayShortcut = new QShortcut(QKeySequence(QStringLiteral("Ctrl+T")), this);
connect(todayShortcut, &QShortcut::activated, this, [this]() { navigateToDate(QDate::currentDate()); });
auto* dayShortcut = new QShortcut(QKeySequence(QStringLiteral("Ctrl+1")), this);
connect(dayShortcut, &QShortcut::activated, this, [this]() { setView(View::Day); });
auto* monthShortcut = new QShortcut(QKeySequence(QStringLiteral("Ctrl+2")), this);
connect(monthShortcut, &QShortcut::activated, this, [this]() { setView(View::Month); });
auto* yearShortcut = new QShortcut(QKeySequence(QStringLiteral("Ctrl+3")), this);
connect(yearShortcut, &QShortcut::activated, this, [this]() { setView(View::Year); });
auto* settingsShortcut = new QShortcut(QKeySequence(QStringLiteral("Ctrl+,")), this);
connect(settingsShortcut, &QShortcut::activated, this, &MainWindow::openSettings);
auto* compactShortcut = new QShortcut(QKeySequence(QStringLiteral("Ctrl+W")), this);
connect(compactShortcut, &QShortcut::activated, this, &MainWindow::close);
}
void MainWindow::connectSignals()
{
connect(&m_scheduleService, &ScheduleService::dataChanged, this, &MainWindow::reloadCurrentView);
connect(m_grid, &ScheduleGridView::createRequested, this, &MainWindow::openCreateDialog);
connect(m_grid, &ScheduleGridView::editRequested, this, &MainWindow::openEditDialog);
connect(m_grid, &ScheduleGridView::moveRequested, this, [this](const QString& id,
const QDateTime& start,
const QDateTime& end) {
QString error;
if (!m_scheduleService.moveBlock(id, start, end, &error))
{
showError(QStringLiteral("移动日程失败"), error);
}
});
connect(m_grid, &ScheduleGridView::statusChangeRequested, this, [this](const QString& id,
const QString& status) {
QString error;
if (!m_scheduleService.setBlockStatus(id, status, &error))
{
showError(QStringLiteral("更新状态失败"), error);
}
});
connect(m_grid, &ScheduleGridView::postponeRequested, this, [this](const QString& id, int minutes) {
QString error;
if (!m_scheduleService.postponeBlock(id, minutes, &error))
{
showError(QStringLiteral("延后日程失败"), error);
}
});
connect(m_grid, &ScheduleGridView::deleteRequested, this, [this](const QString& id) {
QString error;
if (!m_scheduleService.deleteBlock(id, &error))
{
showError(QStringLiteral("删除日程失败"), error);
}
});
connect(m_monthView, &MonthView::dateSelected, this, &MainWindow::navigateToDate);
connect(m_monthView, &MonthView::dateActivated, this, [this](const QDate& date) {
m_currentDate = date;
setView(View::Day);
});
connect(m_monthView, &MonthView::createRequested, this, [this](const QDate& date) {
navigateToDate(date);
openCreateDialogForNew();
});
connect(m_monthView, &MonthView::monthStepRequested, this, [this](int direction) { stepDate(direction); });
connect(m_yearView, &YearView::dateSelected, this, &MainWindow::navigateToDate);
connect(m_yearView, &YearView::dateActivated, this, [this](const QDate& date) {
m_currentDate = date;
setView(View::Day);
});
connect(m_yearView, &YearView::monthActivated, this, [this](const QDate& month) {
m_currentDate = month;
setView(View::Month);
});
connect(m_yearView, &YearView::yearStepRequested, this, [this](int direction) { stepDate(direction); });
connect(m_todayPanel, &TodayPanel::editRequested, this, &MainWindow::openEditDialog);
connect(m_todayPanel, &TodayPanel::statusChangeRequested, this, [this](const QString& id,
const QString& status) {
QString error;
if (!m_scheduleService.setBlockStatus(id, status, &error))
{
showError(QStringLiteral("更新状态失败"), error);
}
});
connect(m_todayPanel, &TodayPanel::postponeRequested, this, [this](const QString& id, int minutes) {
QString error;
if (!m_scheduleService.postponeBlock(id, minutes, &error))
{
showError(QStringLiteral("延后日程失败"), error);
}
});
connect(m_todayPanel, &TodayPanel::deleteRequested, this, [this](const QString& id) {
QString error;
if (!m_scheduleService.deleteBlock(id, &error))
{
showError(QStringLiteral("删除日程失败"), error);
}
});
connect(m_todayPanel, &TodayPanel::focusRequested, this, [this](const QString& id) {
if (m_currentView != View::Day)
{
setView(View::Day);
}
m_grid->scrollToBlock(id);
m_grid->setFocus(Qt::OtherFocusReason);
});
connect(&m_notificationService,
&NotificationService::overdueListChanged,
this,
[this](const QVector<ScheduleItem>& items) {
updateOverdueBanner(items);
reloadCurrentView();
});
connect(&m_themeManager, &ThemeManager::themeChanged, this, &MainWindow::applyTheme);
connect(&m_themeManager, &ThemeManager::preferenceChanged, this, [this](ThemeManager::Preference) {
applyTheme(m_themeManager.tokens());
});
}
void MainWindow::applyTheme(const ThemeTokens& tokens)
{
m_tokens = tokens;
if (m_grid)
{
m_grid->setTokens(tokens);
}
if (m_monthView)
{
m_monthView->setTokens(tokens);
}
if (m_yearView)
{
m_yearView->setTokens(tokens);
}
if (m_todayPanel)
{
m_todayPanel->setTokens(tokens);
}
if (m_toolbar)
{
m_toolbar->setStyleSheet(QStringLiteral(
"QFrame#Toolbar{background:%1;border:1px solid %2;border-radius:10px;}")
.arg(tokens.toolbarBackground.name(), tokens.cardBorder.name()));
}
if (m_rightDock)
{
m_rightDock->setStyleSheet(QStringLiteral(
"QFrame#RightDock{background:%1;border:1px solid %2;border-radius:10px;}")
.arg(tokens.panelBackground.name(), tokens.cardBorder.name()));
}
if (m_overdueBanner)
{
m_overdueBanner->setStyleSheet(
QStringLiteral("QFrame#OverdueBanner{background:%1;border:1px solid %2;border-radius:8px;}")
.arg(tokens.blockOverdueFill.name(), tokens.blockOverdueBorder.name()));
}
if (m_overdueLabel)
{
m_overdueLabel->setStyleSheet(
QStringLiteral("background:transparent;color:%1;font-size:12px;")
.arg(tokens.blockOverdueBorder.name()));
}
if (m_syncLabel)
{
m_syncLabel->setStyleSheet(
QStringLiteral("background:transparent;color:%1;font-size:11px;").arg(tokens.textMuted.name()));
}
if (m_themeButton)
{
m_themeButton->setToolTip(QStringLiteral("主题:%1(点击切换)")
.arg(ThemeManager::preferenceLabel(m_themeManager.preference())));
}
}
void MainWindow::restoreWindowState()
{
const QByteArray geometry = m_settings.scheduleGeometry();
if (!geometry.isEmpty())
{
restoreGeometry(geometry);
}
else
{
moveToCenter();
}
const QByteArray mainState = m_settings.scheduleSplitter();
if (!mainState.isEmpty() && m_mainSplitter)
{
m_mainSplitter->restoreState(mainState);
}
int snapMinutes = m_settings.snapMinutes();
if (snapMinutes != 15 && snapMinutes != 30 && snapMinutes != 60)
{
snapMinutes = 15;
}
const int snapIndex = snapMinutes == 15 ? 0 : (snapMinutes == 30 ? 1 : 2);
{
const QSignalBlocker blocker(m_snapCombo);
m_snapCombo->setCurrentIndex(snapIndex);
}
m_grid->setSnapMinutes(snapMinutes);
}
void MainWindow::saveWindowState()
{
m_settings.setScheduleGeometry(saveGeometry());
if (m_mainSplitter)
{
m_settings.setScheduleSplitter(m_mainSplitter->saveState());
}
if (m_grid)
{
m_settings.setSnapMinutes(m_grid->snapMinutes());
}
}
void MainWindow::setView(View view)
{
m_currentView = view;
const int index = static_cast<int>(view);
if (m_viewStack)
{
m_viewStack->setCurrentIndex(index);
}
if (m_viewPivot)
{
m_viewPivot->setCurrentIndex(index);
}
updateDateLabel();
updateHint();
reloadCurrentView();
if (view == View::Day && m_grid)
{
m_grid->scrollToCurrentTime();
}
}
MainWindow::View MainWindow::view() const
{
return m_currentView;
}
QDate MainWindow::currentDate() const
{
return m_currentDate;
}
void MainWindow::reloadCurrentView()
{
if (!m_grid || !m_monthView || !m_yearView || !m_todayPanel)
{
return;
}
const QDateTime now = QDateTime::currentDateTime();
QVector<ScheduleItem> agendaItems;
switch (m_currentView)
{
case View::Day:
{
m_grid->setDate(m_currentDate);
agendaItems = m_scheduleService.itemsForDay(m_currentDate);
m_grid->setBlocks(agendaItems);
break;
}
case View::Month:
{
const QDate first(m_currentDate.year(), m_currentDate.month(), 1);
const QDate last = first.addMonths(1).addDays(-1);
m_monthView->setMonth(first);
m_monthView->setItems(m_scheduleService.itemsForRange(first, last));
m_monthView->setSelectedDate(m_currentDate);
agendaItems = m_scheduleService.itemsForDay(m_currentDate);
break;
}
case View::Year:
{
const int year = m_currentDate.year();
m_yearView->setYear(year);
m_yearView->setItems(m_scheduleService.itemsForRange(QDate(year, 1, 1), QDate(year, 12, 31)));
m_yearView->setSelectedDate(m_currentDate);
agendaItems = m_scheduleService.itemsForDay(m_currentDate);
break;
}
}
m_todayPanel->setDate(m_currentDate);
m_todayPanel->setItems(agendaItems, now);
}
void MainWindow::navigateToDate(const QDate& date)
{
if (!date.isValid())
{
return;
}
m_currentDate = date;
updateDateLabel();
reloadCurrentView();
if (m_currentView == View::Day && m_grid)
{
m_grid->scrollToCurrentTime();
}
}
void MainWindow::stepDate(int direction)
{
if (direction == 0)
{
return;
}
switch (m_currentView)
{
case View::Day:
navigateToDate(m_currentDate.addDays(direction));
break;
case View::Month:
navigateToDate(m_currentDate.addMonths(direction));
break;
case View::Year:
{
const int year = m_currentDate.year() + direction;
if (year < 1900 || year > 9999)
{
return;
}
const QDate first(year, m_currentDate.month(), 1);
if (!first.isValid())
{
return;
}
navigateToDate(QDate(year, m_currentDate.month(), qMin(m_currentDate.day(), first.daysInMonth())));
break;
}
}
}
void MainWindow::updateDateLabel()
{
if (!m_dateLabel)
{
return;
}
const QDate today = QDate::currentDate();
QString text;
switch (m_currentView)
{
case View::Day:
{
text = QStringLiteral("%1年%2月%3日 %4")
.arg(m_currentDate.year())
.arg(m_currentDate.month())
.arg(m_currentDate.day())
.arg(weekdayLabel(m_currentDate));
if (m_currentDate == today)
{
text.prepend(QStringLiteral("今天 · "));
}
break;
}
case View::Month:
text = QStringLiteral("%1年%2月").arg(m_currentDate.year()).arg(m_currentDate.month());
if (m_currentDate.year() == today.year() && m_currentDate.month() == today.month())
{
text.prepend(QStringLiteral("本月 · "));
}
break;
case View::Year:
text = QStringLiteral("%1年").arg(m_currentDate.year());
if (m_currentDate.year() == today.year())
{
text.prepend(QStringLiteral("今年 · "));
}
break;
}
m_dateLabel->setText(text);
}
void MainWindow::updateHint()
{
if (!m_hintLabel)
{
return;
}
switch (m_currentView)
{
case View::Day:
m_hintLabel->setText(
QStringLiteral("双击空白新建 · 拖动块移动 · 上下边缘改时长 · Ctrl+滚轮缩放 · Delete 删除"));
break;
case View::Month:
m_hintLabel->setText(
QStringLiteral("单击选择日期 · 双击打开日视图 · 滚轮切换月份 · 右键新建日程"));
break;
case View::Year:
m_hintLabel->setText(
QStringLiteral("单击选择日期 · 双击打开日视图 · 点击月份标题进入月视图 · 滚轮切换年份"));
break;
}
}
void MainWindow::showAndRaise()
{
if (isMinimized())
{
showNormal();
}
else
{
show();
}
raise();
activateWindow();
}
void MainWindow::openNewBlock()
{
openCreateDialogForNew();
}
void MainWindow::focusBlock(const QDate& date, const QString& blockId)
{
if (!date.isValid())
{
return;
}
m_currentDate = date;
setView(View::Day);
showAndRaise();
QTimer::singleShot(0, this, [this, blockId]() {
m_grid->scrollToBlock(blockId);
m_grid->setFocus(Qt::OtherFocusReason);
});
}
void MainWindow::openCreateDialog(const QDateTime& start, const QDateTime& end)
{
BlockEditDialog dialog(m_tokens, this);
dialog.setCreateMode(true);
dialog.setValues(QString(), start, end);
dialog.moveToCenter();
if (dialog.exec() != QDialog::Accepted)
{
return;
}
QString error;
if (!m_scheduleService.createBlock(dialog.titleValue(), dialog.startValue(), dialog.endValue(), &error))
{
showError(QStringLiteral("创建日程失败"), error);
return;
}
if (dialog.startValue().date() != m_currentDate)
{
navigateToDate(dialog.startValue().date());
}
}
void MainWindow::openCreateDialogForNew()
{
const QDateTime start = defaultNewStart();
openCreateDialog(start, start.addSecs(60 * 60));
}
void MainWindow::openEditDialog(const QString& blockId)
{
const QVector<ScheduleItem> items = m_scheduleService.itemsForDay(m_currentDate);
const auto it = std::find_if(items.cbegin(), items.cend(), [&blockId](const ScheduleItem& item) {
return item.block.id == blockId;
});
if (it == items.cend())
{
showError(QStringLiteral("编辑日程失败"), QStringLiteral("当前日期找不到该日程,请刷新后重试"));
return;
}
BlockEditDialog dialog(m_tokens, this);
dialog.setCreateMode(false);
dialog.setValues(it->task.title, it->block.startAt, it->block.endAt);
dialog.moveToCenter();
if (dialog.exec() != QDialog::Accepted)
{
return;
}
QString error;
if (!m_scheduleService.updateBlock(blockId,
dialog.titleValue(),
dialog.startValue(),
dialog.endValue(),
&error))
{
showError(QStringLiteral("保存日程失败"), error);
return;
}
if (dialog.startValue().date() != m_currentDate)
{
navigateToDate(dialog.startValue().date());
}
}
void MainWindow::openSettings()
{
SettingsDialog dialog(m_themeManager, m_settings, this);
if (m_syncService)
{
connect(&dialog, &SettingsDialog::syncSettingsChanged, m_syncService, &SyncService::reloadConfig);
}
dialog.moveToCenter();
dialog.exec();
}
void MainWindow::updateSyncStatus(const QString& status)
{
if (!m_syncLabel)
{
return;
}
QString shortStatus = status;
if (status.contains(QStringLiteral("失败")) || status.contains(QStringLiteral("错误")) ||
status.contains(QStringLiteral("未配置")) || status.contains(QStringLiteral("无效")))
{
shortStatus = QStringLiteral("同步错误");
}
else if (status.contains(QStringLiteral("离线")) || status.contains(QStringLiteral("等待")))
{
shortStatus = QStringLiteral("离线");
}
else if (status.contains(QStringLiteral("注册")) || status.contains(QStringLiteral("同步中")) ||
status.contains(QStringLiteral("WebSocket")))
{
shortStatus = QStringLiteral("同步中");
}
else if (status.contains(QStringLiteral("已同步")) || status.contains(QStringLiteral("已连接")))
{
shortStatus = QStringLiteral("已同步");
}
else
{
shortStatus = QStringLiteral("本地");
}
m_syncLabel->setText(QStringLiteral("● ") + shortStatus);
m_syncLabel->setToolTip(status);
}
void MainWindow::updateOverdueBanner(const QVector<ScheduleItem>& items)
{
if (!m_overdueBanner || !m_overdueLabel)
{
return;
}
if (items.isEmpty())
{
m_overdueBanner->setVisible(false);
return;
}
const ScheduleItem& first = items.first();
m_overdueLabel->setText(QStringLiteral("有 %1 项日程已超时,最早:%2(%3)")
.arg(items.size())
.arg(first.task.title,
DateTimeUtils::formatTimeRange(first.block.startAt, first.block.endAt)));
m_overdueBanner->setVisible(true);
}
void MainWindow::showError(const QString& title, const QString& message)
{
ElaMessageBar::error(ElaMessageBarType::TopRight,
title,
message.isEmpty() ? QStringLiteral("未知错误") : message,
4500,
this);
}
QDateTime MainWindow::defaultNewStart() const
{
const QDateTime now = QDateTime::currentDateTime();
QDateTime start;
if (m_currentDate == now.date())
{
start = DateTimeUtils::snapTo(now.addSecs(15 * 60), 15);
}
else
{
start = QDateTime(m_currentDate, QTime(9, 0), Qt::LocalTime);
}
const QDateTime rangeEnd = QDateTime(m_currentDate.addDays(1), QTime(0, 0), Qt::LocalTime);
if (start.addSecs(60 * 60) > rangeEnd)
{
start = rangeEnd.addSecs(-60 * 60);
}
const QDateTime rangeStart = QDateTime(m_currentDate, QTime(0, 0), Qt::LocalTime);
if (start < rangeStart)
{
start = rangeStart;
}
return start;
}
void MainWindow::closeEvent(QCloseEvent* event)
{
saveWindowState();
hide();
Q_EMIT closeRequested();
event->ignore();
}
void MainWindow::paintEvent(QPaintEvent* event)
{
ElaWindow::paintEvent(event);
if (!WindowEffects::isMicaActive())
{
return;
}
QPainter painter(this);
QColor tint = m_tokens.windowBackground;
tint.setAlpha(m_tokens.dark ? 92 : 84);
painter.fillRect(rect(), tint);
QColor bar = m_tokens.toolbarBackground;
bar.setAlpha(m_tokens.dark ? 178 : 168);
painter.fillRect(QRect(0, 0, width(), 44), bar);
}
void MainWindow::showEvent(QShowEvent* event)
{
ElaWindow::showEvent(event);
if (!m_firstShowHandled)
{
m_firstShowHandled = true;
QTimer::singleShot(0, this, [this]() {
if (m_grid && m_currentView == View::Day)
{
m_grid->scrollToCurrentTime();
}
});
}
}