Files
Schedule/client/src/ui/panels/TodayPanel.cpp
T
Misaki c749d642cf feat: add search, recurring schedules, and Windows packaging
- Add title/description search with Ctrl+F and a result dialog\n- Support daily/weekly/monthly recurring blocks with conflict skipping\n- Add CPack NSIS packaging, MinGW release preset, and deploy script\n- Add window/view/list/dialog fade animations\n- Document multi-device conflict behavior and update validation screenshots
2026-10-26 19:53:56 +08:00

235 lines
7.2 KiB
C++

#include "TodayPanel.h"
#include <QFrame>
#include <QHBoxLayout>
#include <QLayoutItem>
#include <QScrollBar>
#include <QVBoxLayout>
#include <algorithm>
#include "ElaScrollArea.h"
#include "ElaText.h"
#include "ElaToolButton.h"
#include "TodayItemCard.h"
#include "ui/theme/AnimationUtils.h"
TodayPanel::TodayPanel(QWidget* parent)
: QWidget(parent)
{
setMinimumWidth(272);
setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding);
auto* root = new QVBoxLayout(this);
root->setContentsMargins(12, 10, 12, 12);
root->setSpacing(10);
auto* header = new QHBoxLayout();
header->setContentsMargins(2, 0, 2, 0);
header->setSpacing(6);
m_titleLabel = new ElaText(QStringLiteral("当日任务"), 15, this);
QFont titleFont = m_titleLabel->font();
titleFont.setWeight(QFont::DemiBold);
m_titleLabel->setFont(titleFont);
m_countLabel = new ElaText(this);
m_countLabel->setTextPixelSize(11);
m_reviewButton = new ElaToolButton(this);
m_reviewButton->setIsTransparent(true);
m_reviewButton->setElaIcon(ElaIconType::ClipboardCheck);
m_reviewButton->setFixedSize(26, 26);
m_reviewButton->setToolTip(QStringLiteral("今日复盘 (Ctrl+R)"));
connect(m_reviewButton, &ElaToolButton::clicked, this, &TodayPanel::reviewRequested);
header->addWidget(m_titleLabel);
header->addStretch(1);
header->addWidget(m_countLabel, 0, Qt::AlignVCenter);
header->addWidget(m_reviewButton, 0, Qt::AlignVCenter);
root->addLayout(header);
m_insightLabel = new ElaText(this);
m_insightLabel->setTextPixelSize(11);
m_insightLabel->setTextStyle(ElaTextType::Caption);
m_insightLabel->setWordWrap(true);
m_insightLabel->setVisible(false);
root->addWidget(m_insightLabel);
m_scrollArea = new ElaScrollArea(this);
m_scrollArea->setWidgetResizable(true);
m_scrollArea->setFrameShape(QFrame::NoFrame);
m_scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
m_scrollArea->setStyleSheet(QStringLiteral("QScrollArea{background:transparent;border:none;}"));
m_scrollArea->viewport()->setAutoFillBackground(false);
m_scrollArea->viewport()->setStyleSheet(QStringLiteral("background:transparent;"));
m_listWidget = new QWidget(m_scrollArea);
m_listWidget->setAutoFillBackground(false);
m_listWidget->setStyleSheet(QStringLiteral("background:transparent;"));
m_listWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
m_listLayout = new QVBoxLayout(m_listWidget);
m_listLayout->setContentsMargins(0, 0, 2, 0);
m_listLayout->setSpacing(8);
m_listLayout->addStretch(1);
m_scrollArea->setWidget(m_listWidget);
root->addWidget(m_scrollArea, 1);
}
void TodayPanel::setTokens(const ThemeTokens& tokens)
{
m_tokens = tokens;
if (m_insightLabel)
{
m_insightLabel->setStyleSheet(QStringLiteral("color:%1;").arg(tokens.accent.name()));
}
rebuild();
}
void TodayPanel::setInsightText(const QString& text)
{
if (!m_insightLabel)
{
return;
}
m_insightLabel->setText(text);
m_insightLabel->setVisible(!text.trimmed().isEmpty());
}
void TodayPanel::setDate(const QDate& date)
{
if (!m_titleLabel)
{
return;
}
if (!date.isValid() || date == QDate::currentDate())
{
m_titleLabel->setText(QStringLiteral("今日任务"));
return;
}
m_titleLabel->setText(QStringLiteral("%1月%2日任务").arg(date.month()).arg(date.day()));
}
void TodayPanel::setItems(const QVector<ScheduleItem>& items, const QDateTime& now)
{
m_items = items;
m_now = now;
rebuild();
}
void TodayPanel::clearLayout(QVBoxLayout* layout)
{
while (QLayoutItem* item = layout->takeAt(0))
{
if (QWidget* widget = item->widget())
{
delete widget;
}
// addLayout 时 QLayout 本身即 QLayoutItem,直接 delete 可避免二次释放。
delete item;
}
}
void TodayPanel::rebuild()
{
if (!m_listLayout)
{
return;
}
clearLayout(m_listLayout);
const QDateTime now = m_now.isValid() ? m_now : QDateTime::currentDateTime();
int activeCount = 0;
int overdueCount = 0;
for (const ScheduleItem& item : m_items)
{
if (item.block.status == QStringLiteral("scheduled") || item.block.status == QStringLiteral("in_progress"))
{
++activeCount;
if (item.block.endAt < now)
{
++overdueCount;
}
}
}
if (overdueCount > 0)
{
m_countLabel->setText(QStringLiteral("%1 项待办 · %2 项超时").arg(activeCount).arg(overdueCount));
}
else if (activeCount > 0)
{
m_countLabel->setText(QStringLiteral("%1 项待办").arg(activeCount));
}
else
{
m_countLabel->setText(QStringLiteral("全部完成"));
}
QVector<ScheduleItem> sorted = m_items;
std::sort(sorted.begin(), sorted.end(), [&now](const ScheduleItem& a, const ScheduleItem& b) {
const auto rank = [&now](const ScheduleItem& item) {
const QString& status = item.block.status;
if (status == QStringLiteral("dropped") || status == QStringLiteral("missed"))
{
return 4;
}
if (status == QStringLiteral("done"))
{
return 3;
}
if (item.block.endAt < now)
{
return 0;
}
if (status == QStringLiteral("in_progress"))
{
return 1;
}
return 2;
};
const int rankA = rank(a);
const int rankB = rank(b);
if (rankA != rankB)
{
return rankA < rankB;
}
if (a.block.startAt != b.block.startAt)
{
return a.block.startAt < b.block.startAt;
}
return a.block.id < b.block.id;
});
if (sorted.isEmpty())
{
auto* empty = new ElaText(QStringLiteral("今天还没有安排\n双击时间网格即可新建"), 13, m_listWidget);
empty->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
empty->setMinimumHeight(120);
empty->setTextStyle(ElaTextType::Caption);
m_listLayout->addWidget(empty);
m_listLayout->addStretch(1);
if (m_renderedCount != 0)
{
m_renderedCount = 0;
AnimationUtils::fadeIn(m_listWidget, 150);
}
return;
}
for (const ScheduleItem& item : sorted)
{
auto* card = new TodayItemCard(item, m_tokens, now, m_listWidget);
connect(card, &TodayItemCard::clicked, this, &TodayPanel::focusRequested);
connect(card, &TodayItemCard::editRequested, this, &TodayPanel::editRequested);
connect(card, &TodayItemCard::statusChangeRequested, this, &TodayPanel::statusChangeRequested);
connect(card, &TodayItemCard::postponeRequested, this, &TodayPanel::postponeRequested);
connect(card, &TodayItemCard::deleteRequested, this, &TodayPanel::deleteRequested);
m_listLayout->addWidget(card);
}
m_listLayout->addStretch(1);
if (m_renderedCount != sorted.size())
{
m_renderedCount = sorted.size();
AnimationUtils::fadeIn(m_listWidget, 150);
}
}