Commit b8dc45d1 authored by Glen Ditchfield's avatar Glen Ditchfield 🐛
Browse files

Simplify drawBackground()

parent 1ec7281d
Pipeline #86450 skipped
......@@ -194,7 +194,7 @@ void MonthGraphicsView::drawBackground(QPainter *p, const QRectF &rect)
const QDate end = mMonthView->actualEndDateTime().date();
for (QDate d = start; d <= start.addDays(6); d = d.addDays(1)) {
MonthCell *cell = mScene->mMonthCellMap.value(d);
const MonthCell *const cell = mScene->mMonthCellMap.value(d);
if (!cell) {
// This means drawBackground() is being called before reloadIncidences(). Can happen with some
......@@ -212,48 +212,42 @@ void MonthGraphicsView::drawBackground(QPainter *p, const QRectF &rect)
*/
int columnWidth = mScene->columnWidth();
int rowHeight = mScene->rowHeight();
QDate todayDate {QDate::currentDate()};
const QList<QDate> workDays = CalendarSupport::workDays(mMonthView->actualStartDateTime().date(), mMonthView->actualEndDateTime().date());
QRect todayRect;
QRect selectedRect;
QColor holidayBg;
QColor workdayBg;
if (mMonthView->preferences()->useSystemColor()) {
workdayBg = palette().color(QPalette::Base);
holidayBg = palette().color(QPalette::AlternateBase);
} else {
workdayBg = mMonthView->preferences()->monthGridWorkHoursBackgroundColor();
holidayBg = mMonthView->preferences()->monthGridBackgroundColor();
}
for (QDate d = start; d <= end; d = d.addDays(1)) {
if (!mScene->mMonthCellMap.contains(d)) {
const MonthCell *const cell = mScene->mMonthCellMap.value(d);
if (!cell) {
// This means drawBackground() is being called before reloadIncidences(). Can happen with some
// themes. Bug #190191
return;
}
MonthCell *cell = mScene->mMonthCellMap[d];
const QRect cellRect(mScene->cellHorizontalPos(cell), mScene->cellVerticalPos(cell), columnWidth, rowHeight);
QColor color;
if (!mMonthView->preferences()->useSystemColor()) {
if (workDays.contains(d)) {
color = mMonthView->preferences()->monthGridWorkHoursBackgroundColor();
} else {
color = mMonthView->preferences()->monthGridBackgroundColor();
}
} else {
if (workDays.contains(d)) {
color = palette().color(QPalette::Base);
} else {
color = palette().color(QPalette::AlternateBase);
}
}
if (cell == mScene->selectedCell()) {
selectedRect = cellRect;
}
if (cell->date() == QDate::currentDate()) {
if (cell->date() == todayDate) {
todayRect = cellRect;
}
// Draw cell
p->setPen(mMonthView->preferences()->monthGridBackgroundColor().darker(150));
p->setBrush(color);
p->setBrush(workDays.contains(d) ? workdayBg : holidayBg);
p->drawRect(cellRect);
if (mMonthView->isBusyDay(d)) {
QColor busyColor = mMonthView->preferences()->viewBgBusyColor();
busyColor.setAlpha(EventViews::BUSY_BACKGROUND_ALPHA);
......@@ -276,40 +270,36 @@ void MonthGraphicsView::drawBackground(QPainter *p, const QRectF &rect)
p->drawRect(selectedRect);
}
/*
* Draw Dates
*/
font = mMonthView->preferences()->monthViewFont();
font.setPixelSize(MonthCell::topMargin() - 4);
p->setFont(font);
QPen oldPen;
if (!mMonthView->preferences()->useSystemColor()) {
oldPen = mMonthView->preferences()->monthGridBackgroundColor().darker(150);
} else {
if (mMonthView->preferences()->useSystemColor()) {
oldPen = palette().color(QPalette::WindowText).darker(150);
} else {
oldPen = mMonthView->preferences()->monthGridBackgroundColor().darker(150);
}
// Draw dates
for (QDate d = mMonthView->actualStartDateTime().date(); d <= mMonthView->actualEndDateTime().date(); d = d.addDays(1)) {
MonthCell *cell = mScene->mMonthCellMap.value(d);
MonthCell *const cell = mScene->mMonthCellMap.value(d);
// Draw cell header
int cellHeaderX = mScene->cellHorizontalPos(cell) + 1;
int cellHeaderY = mScene->cellVerticalPos(cell) + 1;
int cellHeaderWidth = columnWidth - 2;
int cellHeaderHeight = cell->topMargin() - 2;
QLinearGradient bgGradient(QPointF(cellHeaderX, cellHeaderY), QPointF(cellHeaderX + cellHeaderWidth, cellHeaderY + cellHeaderHeight));
const auto brush = KColorScheme(QPalette::Normal, KColorScheme::ColorSet::Header).background(KColorScheme::BackgroundRole::NormalBackground);
p->setBrush(brush);
bgGradient.setColorAt(1, brush.color());
p->setPen(Qt::NoPen);
p->drawRect(QRect(cellHeaderX, cellHeaderY, cellHeaderWidth, cellHeaderHeight));
QFont font = p->font();
if (cell->date() == QDate::currentDate()) {
font.setBold(true);
} else {
font.setBold(false);
}
font.setBold(cell->date() == todayDate);
p->setFont(font);
if (d.month() == mMonthView->currentMonth()) {
......@@ -318,6 +308,21 @@ void MonthGraphicsView::drawBackground(QPainter *p, const QRectF &rect)
p->setPen(oldPen);
}
QString dayText;
// Prepend month name if d is the first or last day of month
if (d.day() == 1 || // d is the first day of month
d.addDays(1).day() == 1) { // d is the last day of month
dayText = i18nc("'Month day' for month view cells", "%1 %2", QLocale::system().monthName(d.month(), QLocale::ShortFormat), d.day());
} else {
dayText = QString::number(d.day());
}
p->drawText(QRect(mScene->cellHorizontalPos(cell), // top right
mScene->cellVerticalPos(cell), // of the cell
mScene->columnWidth() - 2,
cell->topMargin()),
Qt::AlignRight,
dayText);
/*
Draw arrows if all items won't fit
*/
......@@ -339,25 +344,7 @@ void MonthGraphicsView::drawBackground(QPainter *p, const QRectF &rect)
} else {
cell->downArrow()->hide();
}
QString dayText;
// Prepend month name if d is the first or last day of month
if (d.day() == 1 || // d is the first day of month
d.addDays(1).day() == 1) { // d is the last day of month
dayText = i18nc("'Month day' for month view cells", "%1 %2", QLocale::system().monthName(d.month(), QLocale::ShortFormat), d.day());
} else {
dayText = QString::number(d.day());
}
p->drawText(QRect(mScene->cellHorizontalPos(cell), // top right
mScene->cellVerticalPos(cell), // of the cell
mScene->columnWidth() - 2,
cell->topMargin()),
Qt::AlignRight,
dayText);
}
// ...
}
void MonthScene::resetAll()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment