#include "MonthView.h" #include #include #include #include #include #include #include #include #include "ElaMenu.h" #include "core/DateTimeUtils.h" namespace { constexpr qreal kHeaderHeight = 30.0; constexpr int kRows = 6; constexpr int kColumns = 7; QColor statusColor(const ScheduleItem& item, const ThemeTokens& tokens, const QDateTime& now) { if (item.block.status == QStringLiteral("done")) { return tokens.success; } if (item.block.status == QStringLiteral("dropped") || item.block.status == QStringLiteral("missed")) { return tokens.textMuted; } if (item.block.endAt < now) { return tokens.blockOverdueBorder; } if (item.block.status == QStringLiteral("in_progress")) { return tokens.warning; } return tokens.accent; } } // namespace MonthView::MonthView(QWidget* parent) : QWidget(parent) , m_month(QDate::currentDate().addDays(1 - QDate::currentDate().day())) , m_selectedDate(QDate::currentDate()) { setMouseTracking(true); setMinimumSize(560, 420); } void MonthView::setMonth(const QDate& monthAnchor) { if (!monthAnchor.isValid()) { return; } const QDate normalized(monthAnchor.year(), monthAnchor.month(), 1); if (normalized == m_month) { return; } m_month = normalized; update(); } void MonthView::setItems(const QVector& items) { m_itemsByDate.clear(); for (const ScheduleItem& item : items) { m_itemsByDate[item.block.startAt.date()].append(item); } for (auto it = m_itemsByDate.begin(); it != m_itemsByDate.end(); ++it) { std::sort(it->begin(), it->end(), [](const ScheduleItem& a, const ScheduleItem& b) { return a.block.startAt < b.block.startAt; }); } update(); } void MonthView::setSelectedDate(const QDate& date) { if (m_selectedDate == date) { return; } m_selectedDate = date; update(); } void MonthView::setTokens(const ThemeTokens& tokens) { m_tokens = tokens; update(); } QDate MonthView::month() const { return m_month; } QDate MonthView::selectedDate() const { return m_selectedDate; } QDate MonthView::visibleStart() const { return m_month.addDays(-(m_month.dayOfWeek() - 1)); } QRectF MonthView::cellRect(int row, int column) const { const qreal left = width() * column / static_cast(kColumns); const qreal top = kHeaderHeight + (height() - kHeaderHeight) * row / static_cast(kRows); const qreal cellWidth = width() / static_cast(kColumns); const qreal cellHeight = (height() - kHeaderHeight) / static_cast(kRows); return QRectF(left + 3, top + 3, cellWidth - 6, cellHeight - 6); } QRectF MonthView::cellRectForDate(const QDate& date) const { const int offset = visibleStart().daysTo(date); if (offset < 0 || offset >= kRows * kColumns) { return {}; } return cellRect(offset / kColumns, offset % kColumns); } QDate MonthView::dateAt(const QPointF& pos) const { if (pos.y() < kHeaderHeight || pos.x() < 0 || pos.x() >= width()) { return {}; } const int column = qBound(0, static_cast(pos.x() / (width() / static_cast(kColumns))), kColumns - 1); const qreal rowHeight = (height() - kHeaderHeight) / static_cast(kRows); if (rowHeight <= 0) { return {}; } const int row = qBound(0, static_cast((pos.y() - kHeaderHeight) / rowHeight), kRows - 1); return visibleStart().addDays(row * kColumns + column); } void MonthView::paintEvent(QPaintEvent* event) { Q_UNUSED(event) QPainter painter(this); painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing); painter.fillRect(rect(), m_tokens.windowBackground); QFont headerFont = painter.font(); headerFont.setPixelSize(11); headerFont.setWeight(QFont::DemiBold); painter.setFont(headerFont); painter.setPen(m_tokens.textSecondary); static const char* weekdays[] = {"一", "二", "三", "四", "五", "六", "日"}; for (int column = 0; column < kColumns; ++column) { const QRectF cell = cellRect(0, column); const QRectF headerRect(cell.left(), 2, cell.width(), kHeaderHeight - 6); painter.drawText(headerRect, Qt::AlignCenter, QString::fromUtf8(weekdays[column])); } const QDateTime now = QDateTime::currentDateTime(); const QDate today = now.date(); const QDate start = visibleStart(); QFont dayFont = painter.font(); dayFont.setPixelSize(12); dayFont.setWeight(QFont::DemiBold); QFont taskFont = painter.font(); taskFont.setPixelSize(10); taskFont.setWeight(QFont::Normal); for (int index = 0; index < kRows * kColumns; ++index) { const int row = index / kColumns; const int column = index % kColumns; const QDate date = start.addDays(index); const QRectF rect = cellRect(row, column); const bool inMonth = date.month() == m_month.month() && date.year() == m_month.year(); const bool selected = date == m_selectedDate; const bool hovered = date == m_hoverDate; const bool isToday = date == today; QColor background = m_tokens.cardBackground; if (!inMonth) { background = m_tokens.windowBackground; } else if (hovered) { background = m_tokens.cardHoverBackground; } if (selected) { background = m_tokens.accent; background.setAlpha(m_tokens.dark ? 70 : 34); } QPainterPath path; path.addRoundedRect(rect, 8, 8); painter.fillPath(path, background); QColor border = m_tokens.cardBorder; qreal borderWidth = 1.0; if (selected) { border = m_tokens.accent; borderWidth = 2.0; } else if (isToday) { border = m_tokens.accent; borderWidth = 1.3; } painter.setPen(QPen(border, borderWidth)); painter.setBrush(Qt::NoBrush); painter.drawPath(path); painter.setFont(dayFont); painter.setPen(selected ? m_tokens.onAccent : (inMonth ? m_tokens.textPrimary : m_tokens.textMuted)); if (selected) { const QRectF badge(rect.left() + 5, rect.top() + 4, 24, 20); QPainterPath badgePath; badgePath.addRoundedRect(badge, 6, 6); painter.fillPath(badgePath, m_tokens.accent); painter.setPen(m_tokens.onAccent); painter.drawText(badge, Qt::AlignCenter, QString::number(date.day())); } else { painter.drawText(QRectF(rect.left() + 7, rect.top() + 4, 30, 18), Qt::AlignLeft | Qt::AlignVCenter, QString::number(date.day())); } const auto it = m_itemsByDate.constFind(date); if (it == m_itemsByDate.constEnd() || it->isEmpty()) { continue; } painter.setFont(taskFont); const QFontMetrics metrics(taskFont); const qreal rowHeight = 15.0; const qreal startY = rect.top() + 26.0; const int maxRows = qMax(0, static_cast((rect.height() - 30.0) / rowHeight)); const int count = qMin(maxRows, static_cast(it->size())); for (int i = 0; i < count; ++i) { const ScheduleItem& item = it->at(i); const qreal y = startY + i * rowHeight; painter.setPen(Qt::NoPen); painter.setBrush(statusColor(item, m_tokens, now)); painter.drawEllipse(QPointF(rect.left() + 9, y + 6), 2.6, 2.6); QColor textColor = inMonth ? m_tokens.textSecondary : m_tokens.textMuted; if (item.block.status == QStringLiteral("done") || item.block.status == QStringLiteral("dropped")) { textColor.setAlpha(160); } painter.setPen(textColor); const QString title = metrics.elidedText(item.task.title, Qt::ElideRight, static_cast(rect.width() - 26)); painter.drawText(QRectF(rect.left() + 15, y, rect.width() - 22, rowHeight), Qt::AlignLeft | Qt::AlignVCenter, title); } if (static_cast(it->size()) > count) { const qreal y = startY + count * rowHeight; painter.setPen(m_tokens.textMuted); painter.drawText(QRectF(rect.left() + 15, y, rect.width() - 22, rowHeight), Qt::AlignLeft | Qt::AlignVCenter, QStringLiteral("+%1").arg(it->size() - count)); } } } void MonthView::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 MonthView::mousePressEvent(QMouseEvent* event) { if (event->button() != Qt::LeftButton) { QWidget::mousePressEvent(event); return; } const QDate date = dateAt(event->position()); if (date.isValid()) { m_selectedDate = date; update(); Q_EMIT dateSelected(date); } event->accept(); } void MonthView::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 MonthView::mouseMoveEvent(QMouseEvent* event) { updateHover(event->position()); QWidget::mouseMoveEvent(event); } void MonthView::leaveEvent(QEvent* event) { m_hoverDate = {}; setCursor(Qt::ArrowCursor); update(); QWidget::leaveEvent(event); } void MonthView::contextMenuEvent(QContextMenuEvent* event) { const QDate date = dateAt(event->pos()); if (!date.isValid()) { QWidget::contextMenuEvent(event); return; } m_selectedDate = date; update(); Q_EMIT dateSelected(date); ElaMenu menu(this); QAction* createAction = menu.addAction(QStringLiteral("在此日新建日程")); connect(createAction, &QAction::triggered, this, [this, date]() { Q_EMIT createRequested(date); }); QAction* openAction = menu.addAction(QStringLiteral("打开当日视图")); connect(openAction, &QAction::triggered, this, [this, date]() { Q_EMIT dateActivated(date); }); menu.exec(event->globalPos()); event->accept(); } void MonthView::wheelEvent(QWheelEvent* event) { const int delta = event->angleDelta().y(); if (delta != 0) { Q_EMIT monthStepRequested(delta > 0 ? -1 : 1); event->accept(); return; } QWidget::wheelEvent(event); }