5be7f7f04b
- 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
411 lines
12 KiB
C++
411 lines
12 KiB
C++
#include "YearView.h"
|
|
|
|
#include <QFontMetrics>
|
|
#include <QMouseEvent>
|
|
#include <QPainter>
|
|
#include <QPainterPath>
|
|
#include <QWheelEvent>
|
|
|
|
namespace
|
|
{
|
|
constexpr int kColumns = 4;
|
|
constexpr int kRows = 3;
|
|
constexpr qreal kMargin = 10.0;
|
|
constexpr qreal kGap = 10.0;
|
|
constexpr qreal kTitleHeight = 24.0;
|
|
constexpr qreal kWeekdayHeight = 13.0;
|
|
constexpr qreal kInnerPadding = 8.0;
|
|
constexpr qreal kGridGap = 2.0;
|
|
constexpr int kMiniRows = 6;
|
|
constexpr int kMiniColumns = 7;
|
|
} // namespace
|
|
|
|
YearView::YearView(QWidget* parent)
|
|
: QWidget(parent)
|
|
, m_year(QDate::currentDate().year())
|
|
, m_selectedDate(QDate::currentDate())
|
|
{
|
|
setMouseTracking(true);
|
|
setMinimumSize(680, 480);
|
|
}
|
|
|
|
void YearView::setYear(int year)
|
|
{
|
|
if (year < 1900 || year > 9999 || m_year == year)
|
|
{
|
|
return;
|
|
}
|
|
m_year = year;
|
|
update();
|
|
}
|
|
|
|
void YearView::setItems(const QVector<ScheduleItem>& items)
|
|
{
|
|
m_summary.clear();
|
|
const QDateTime now = QDateTime::currentDateTime();
|
|
for (const ScheduleItem& item : items)
|
|
{
|
|
const QDate date = item.block.startAt.date();
|
|
DaySummary& summary = m_summary[date];
|
|
++summary.total;
|
|
if (item.block.status == QStringLiteral("done"))
|
|
{
|
|
++summary.done;
|
|
}
|
|
else if (item.block.status == QStringLiteral("scheduled") ||
|
|
item.block.status == QStringLiteral("in_progress"))
|
|
{
|
|
if (item.block.endAt < now)
|
|
{
|
|
summary.overdue = true;
|
|
}
|
|
}
|
|
}
|
|
update();
|
|
}
|
|
|
|
void YearView::setSelectedDate(const QDate& date)
|
|
{
|
|
if (m_selectedDate == date)
|
|
{
|
|
return;
|
|
}
|
|
m_selectedDate = date;
|
|
update();
|
|
}
|
|
|
|
void YearView::setTokens(const ThemeTokens& tokens)
|
|
{
|
|
m_tokens = tokens;
|
|
update();
|
|
}
|
|
|
|
int YearView::year() const
|
|
{
|
|
return m_year;
|
|
}
|
|
|
|
QDate YearView::selectedDate() const
|
|
{
|
|
return m_selectedDate;
|
|
}
|
|
|
|
QRectF YearView::cardRect(int monthIndex) const
|
|
{
|
|
const qreal availableWidth = width() - kMargin * 2 - kGap * (kColumns - 1);
|
|
const qreal availableHeight = height() - kMargin * 2 - kGap * (kRows - 1);
|
|
const qreal cardWidth = availableWidth / kColumns;
|
|
const qreal cardHeight = availableHeight / kRows;
|
|
const int column = monthIndex % kColumns;
|
|
const int row = monthIndex / kColumns;
|
|
return QRectF(kMargin + column * (cardWidth + kGap),
|
|
kMargin + row * (cardHeight + kGap),
|
|
cardWidth,
|
|
cardHeight);
|
|
}
|
|
|
|
QRectF YearView::titleRect(int monthIndex) const
|
|
{
|
|
const QRectF card = cardRect(monthIndex);
|
|
return QRectF(card.left() + kInnerPadding,
|
|
card.top() + 4,
|
|
card.width() - kInnerPadding * 2,
|
|
kTitleHeight - 4);
|
|
}
|
|
|
|
QRectF YearView::gridRect(int monthIndex) const
|
|
{
|
|
const QRectF card = cardRect(monthIndex);
|
|
return QRectF(card.left() + kInnerPadding,
|
|
card.top() + kTitleHeight + kWeekdayHeight,
|
|
card.width() - kInnerPadding * 2,
|
|
card.height() - kTitleHeight - kWeekdayHeight - kInnerPadding);
|
|
}
|
|
|
|
QRectF YearView::miniCellRect(int monthIndex, int cellIndex) const
|
|
{
|
|
const QRectF grid = gridRect(monthIndex);
|
|
const qreal cellWidth = (grid.width() - kGridGap * (kMiniColumns - 1)) / kMiniColumns;
|
|
const qreal cellHeight = (grid.height() - kGridGap * (kMiniRows - 1)) / kMiniRows;
|
|
const int row = cellIndex / kMiniColumns;
|
|
const int column = cellIndex % kMiniColumns;
|
|
return QRectF(grid.left() + column * (cellWidth + kGridGap),
|
|
grid.top() + row * (cellHeight + kGridGap),
|
|
cellWidth,
|
|
cellHeight);
|
|
}
|
|
|
|
int YearView::monthAt(const QPointF& pos) const
|
|
{
|
|
for (int month = 0; month < 12; ++month)
|
|
{
|
|
if (cardRect(month).contains(pos))
|
|
{
|
|
return month;
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
QDate YearView::dateAt(const QPointF& pos) const
|
|
{
|
|
const int month = monthAt(pos);
|
|
if (month < 0)
|
|
{
|
|
return {};
|
|
}
|
|
const QRectF grid = gridRect(month);
|
|
if (!grid.contains(pos))
|
|
{
|
|
return {};
|
|
}
|
|
const qreal cellWidth = (grid.width() - kGridGap * (kMiniColumns - 1)) / kMiniColumns;
|
|
const qreal cellHeight = (grid.height() - kGridGap * (kMiniRows - 1)) / kMiniRows;
|
|
if (cellWidth <= 0 || cellHeight <= 0)
|
|
{
|
|
return {};
|
|
}
|
|
const int column = qBound(0, static_cast<int>((pos.x() - grid.left()) / (cellWidth + kGridGap)), kMiniColumns - 1);
|
|
const int row = qBound(0, static_cast<int>((pos.y() - grid.top()) / (cellHeight + kGridGap)), kMiniRows - 1);
|
|
const QDate firstOfMonth(m_year, month + 1, 1);
|
|
const QDate start = firstOfMonth.addDays(-(firstOfMonth.dayOfWeek() - 1));
|
|
const QDate date = start.addDays(row * kMiniColumns + column);
|
|
if (date.month() != month + 1 || date.year() != m_year)
|
|
{
|
|
return {};
|
|
}
|
|
return date;
|
|
}
|
|
|
|
void YearView::paintEvent(QPaintEvent* event)
|
|
{
|
|
Q_UNUSED(event)
|
|
|
|
QPainter painter(this);
|
|
painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
|
|
painter.fillRect(rect(), m_tokens.windowBackground);
|
|
|
|
const QDate today = QDate::currentDate();
|
|
|
|
for (int month = 0; month < 12; ++month)
|
|
{
|
|
const QRectF card = cardRect(month);
|
|
QPainterPath cardPath;
|
|
cardPath.addRoundedRect(card, 10, 10);
|
|
painter.fillPath(cardPath, m_tokens.cardBackground);
|
|
painter.setPen(QPen(m_tokens.cardBorder, 1.0));
|
|
painter.setBrush(Qt::NoBrush);
|
|
painter.drawPath(cardPath);
|
|
|
|
QFont titleFont = painter.font();
|
|
titleFont.setPixelSize(13);
|
|
titleFont.setWeight(QFont::DemiBold);
|
|
painter.setFont(titleFont);
|
|
painter.setPen(m_tokens.textPrimary);
|
|
painter.drawText(titleRect(month), Qt::AlignLeft | Qt::AlignVCenter, QStringLiteral("%1月").arg(month + 1));
|
|
|
|
int monthTotal = 0;
|
|
for (int day = 1; day <= QDate(m_year, month + 1, 1).daysInMonth(); ++day)
|
|
{
|
|
monthTotal += m_summary.value(QDate(m_year, month + 1, day)).total;
|
|
}
|
|
QFont countFont = painter.font();
|
|
countFont.setPixelSize(10);
|
|
countFont.setWeight(QFont::Normal);
|
|
painter.setFont(countFont);
|
|
painter.setPen(m_tokens.textMuted);
|
|
painter.drawText(titleRect(month),
|
|
Qt::AlignRight | Qt::AlignVCenter,
|
|
monthTotal > 0 ? QStringLiteral("%1 项").arg(monthTotal) : QStringLiteral("—"));
|
|
|
|
const QRectF grid = gridRect(month);
|
|
const qreal cellWidth = (grid.width() - kGridGap * (kMiniColumns - 1)) / kMiniColumns;
|
|
const qreal cellHeight = (grid.height() - kGridGap * (kMiniRows - 1)) / kMiniRows;
|
|
|
|
QFont weekdayFont = painter.font();
|
|
weekdayFont.setPixelSize(8);
|
|
painter.setFont(weekdayFont);
|
|
painter.setPen(m_tokens.textMuted);
|
|
static const char* weekdays[] = {"一", "二", "三", "四", "五", "六", "日"};
|
|
for (int column = 0; column < kMiniColumns; ++column)
|
|
{
|
|
const QRectF cell(grid.left() + column * (cellWidth + kGridGap),
|
|
grid.top() - kWeekdayHeight,
|
|
cellWidth,
|
|
kWeekdayHeight - 2);
|
|
painter.drawText(cell, Qt::AlignCenter, QString::fromUtf8(weekdays[column]));
|
|
}
|
|
|
|
const QDate firstOfMonth(m_year, month + 1, 1);
|
|
const QDate start = firstOfMonth.addDays(-(firstOfMonth.dayOfWeek() - 1));
|
|
|
|
QFont dayFont = painter.font();
|
|
dayFont.setPixelSize(qBound(7, static_cast<int>(cellHeight * 0.42), 10));
|
|
dayFont.setWeight(QFont::Normal);
|
|
|
|
for (int cellIndex = 0; cellIndex < kMiniRows * kMiniColumns; ++cellIndex)
|
|
{
|
|
const QDate date = start.addDays(cellIndex);
|
|
const bool inMonth = date.month() == month + 1 && date.year() == m_year;
|
|
if (!inMonth)
|
|
{
|
|
continue;
|
|
}
|
|
QRectF cell = miniCellRect(month, cellIndex);
|
|
cell.adjust(0.5, 0.5, -0.5, -0.5);
|
|
|
|
const auto it = m_summary.constFind(date);
|
|
const bool hasTasks = it != m_summary.constEnd() && it->total > 0;
|
|
const bool selected = date == m_selectedDate;
|
|
const bool hovered = date == m_hoverDate;
|
|
const bool isToday = date == today;
|
|
|
|
QColor fill = Qt::transparent;
|
|
QColor border = Qt::transparent;
|
|
if (hasTasks)
|
|
{
|
|
if (it->overdue)
|
|
{
|
|
fill = m_tokens.blockOverdueFill;
|
|
border = m_tokens.blockOverdueBorder;
|
|
}
|
|
else if (it->done == it->total)
|
|
{
|
|
fill = m_tokens.blockDoneFill;
|
|
border = m_tokens.blockDoneBorder;
|
|
}
|
|
else
|
|
{
|
|
fill = m_tokens.blockFill;
|
|
border = m_tokens.blockBorder;
|
|
}
|
|
fill.setAlpha(m_tokens.dark ? 120 : 150);
|
|
border.setAlpha(160);
|
|
}
|
|
if (hovered)
|
|
{
|
|
fill = m_tokens.cardHoverBackground;
|
|
border = m_tokens.accent;
|
|
}
|
|
if (selected)
|
|
{
|
|
fill = m_tokens.accent;
|
|
fill.setAlpha(m_tokens.dark ? 190 : 220);
|
|
border = m_tokens.accent;
|
|
}
|
|
|
|
if (fill.alpha() > 0)
|
|
{
|
|
QPainterPath cellPath;
|
|
cellPath.addRoundedRect(cell, 3, 3);
|
|
painter.fillPath(cellPath, fill);
|
|
}
|
|
if (border.alpha() > 0)
|
|
{
|
|
painter.setPen(QPen(border, selected || isToday ? 1.2 : 0.7));
|
|
painter.setBrush(Qt::NoBrush);
|
|
painter.drawRoundedRect(cell, 3, 3);
|
|
}
|
|
else if (isToday)
|
|
{
|
|
painter.setPen(QPen(m_tokens.accent, 1.0));
|
|
painter.setBrush(Qt::NoBrush);
|
|
painter.drawRoundedRect(cell, 3, 3);
|
|
}
|
|
|
|
painter.setFont(dayFont);
|
|
if (selected)
|
|
{
|
|
painter.setPen(m_tokens.onAccent);
|
|
}
|
|
else if (hasTasks)
|
|
{
|
|
painter.setPen(m_tokens.textPrimary);
|
|
}
|
|
else
|
|
{
|
|
painter.setPen(m_tokens.textMuted);
|
|
}
|
|
painter.drawText(cell, Qt::AlignCenter, QString::number(date.day()));
|
|
}
|
|
}
|
|
}
|
|
|
|
void YearView::updateHover(const QPointF& pos)
|
|
{
|
|
const QDate date = dateAt(pos);
|
|
if (date == m_hoverDate)
|
|
{
|
|
return;
|
|
}
|
|
m_hoverDate = date;
|
|
setCursor(date.isValid() ? Qt::PointingHandCursor : Qt::ArrowCursor);
|
|
update();
|
|
}
|
|
|
|
void YearView::mousePressEvent(QMouseEvent* event)
|
|
{
|
|
if (event->button() != Qt::LeftButton)
|
|
{
|
|
QWidget::mousePressEvent(event);
|
|
return;
|
|
}
|
|
const int month = monthAt(event->position());
|
|
if (month >= 0 && titleRect(month).contains(event->position()))
|
|
{
|
|
const QDate firstOfMonth(m_year, month + 1, 1);
|
|
Q_EMIT monthActivated(firstOfMonth);
|
|
event->accept();
|
|
return;
|
|
}
|
|
const QDate date = dateAt(event->position());
|
|
if (date.isValid())
|
|
{
|
|
m_selectedDate = date;
|
|
update();
|
|
Q_EMIT dateSelected(date);
|
|
}
|
|
event->accept();
|
|
}
|
|
|
|
void YearView::mouseDoubleClickEvent(QMouseEvent* event)
|
|
{
|
|
if (event->button() != Qt::LeftButton)
|
|
{
|
|
QWidget::mouseDoubleClickEvent(event);
|
|
return;
|
|
}
|
|
const QDate date = dateAt(event->position());
|
|
if (date.isValid())
|
|
{
|
|
Q_EMIT dateActivated(date);
|
|
}
|
|
event->accept();
|
|
}
|
|
|
|
void YearView::mouseMoveEvent(QMouseEvent* event)
|
|
{
|
|
updateHover(event->position());
|
|
QWidget::mouseMoveEvent(event);
|
|
}
|
|
|
|
void YearView::leaveEvent(QEvent* event)
|
|
{
|
|
m_hoverDate = {};
|
|
setCursor(Qt::ArrowCursor);
|
|
update();
|
|
QWidget::leaveEvent(event);
|
|
}
|
|
|
|
void YearView::wheelEvent(QWheelEvent* event)
|
|
{
|
|
const int delta = event->angleDelta().y();
|
|
if (delta != 0)
|
|
{
|
|
Q_EMIT yearStepRequested(delta > 0 ? -1 : 1);
|
|
event->accept();
|
|
return;
|
|
}
|
|
QWidget::wheelEvent(event);
|
|
}
|