File indexing completed on 2025-01-05 04:47:43

0001 /*
0002   SPDX-FileCopyrightText: 2004 Reinhold Kainhofer <reinhold@kainhofer.com>
0003 
0004   SPDX-License-Identifier: GPL-2.0-or-later WITH Qt-Commercial-exception-1.0
0005 */
0006 
0007 #include "yearprint.h"
0008 
0009 #include "calendarsupport_debug.h"
0010 #include <KConfigGroup>
0011 #include <KLocalizedString>
0012 using namespace CalendarSupport;
0013 
0014 /**************************************************************
0015  *           Print Year
0016  **************************************************************/
0017 
0018 QWidget *CalPrintYear::createConfigWidget(QWidget *w)
0019 {
0020     return new CalPrintYearConfig(w);
0021 }
0022 
0023 void CalPrintYear::readSettingsWidget()
0024 {
0025     auto cfg = dynamic_cast<CalPrintYearConfig *>((QWidget *)mConfigWidget);
0026     if (cfg) {
0027         mPrintFooter = cfg->mPrintFooter->isChecked();
0028         mYear = cfg->mYear->value();
0029         mPages = cfg->mPages->currentText().toInt();
0030         mSubDaysEvents = (cfg->mSubDays->currentIndex() == 0) ? Text : TimeBoxes;
0031         mHolidaysEvents = (cfg->mHolidays->currentIndex() == 0) ? Text : TimeBoxes;
0032         mExcludeConfidential = cfg->mExcludeConfidential->isChecked();
0033         mExcludePrivate = cfg->mExcludePrivate->isChecked();
0034     }
0035 }
0036 
0037 void CalPrintYear::setSettingsWidget()
0038 {
0039     auto cfg = dynamic_cast<CalPrintYearConfig *>((QWidget *)mConfigWidget);
0040     if (cfg) {
0041         QDate start(mYear, 1, 1);
0042         const int months = 12;
0043         int prevPages = 0;
0044         for (int i = 1; i <= months; ++i) {
0045             const int pages = (months - 1) / i + 1;
0046             if (pages != prevPages) {
0047                 prevPages = pages;
0048                 cfg->mPages->addItem(QString::number(pages), pages);
0049             }
0050         }
0051 
0052         cfg->mPrintFooter->setChecked(mPrintFooter);
0053         cfg->mYear->setValue(mYear);
0054         cfg->mPages->setCurrentIndex(cfg->mPages->findData(mPages));
0055 
0056         cfg->mSubDays->setCurrentIndex((mSubDaysEvents == Text) ? 0 : 1);
0057         cfg->mHolidays->setCurrentIndex((mHolidaysEvents == Text) ? 0 : 1);
0058         cfg->mExcludeConfidential->setChecked(mExcludeConfidential);
0059         cfg->mExcludePrivate->setChecked(mExcludePrivate);
0060     }
0061 }
0062 
0063 void CalPrintYear::doLoadConfig()
0064 {
0065     CalPrintPluginBase::doLoadConfig();
0066     if (mConfig) {
0067         KConfigGroup config(mConfig, QStringLiteral("Yearprint"));
0068         mYear = config.readEntry("Year", QDate::currentDate().year());
0069         mPages = config.readEntry("Pages", 1);
0070         mSubDaysEvents = config.readEntry("ShowSubDayEventsAs", static_cast<int>(TimeBoxes));
0071         mHolidaysEvents = config.readEntry("ShowHolidaysAs", static_cast<int>(Text));
0072     }
0073     setSettingsWidget();
0074 }
0075 
0076 void CalPrintYear::doSaveConfig()
0077 {
0078     qCDebug(CALENDARSUPPORT_LOG);
0079 
0080     readSettingsWidget();
0081     if (mConfig) {
0082         KConfigGroup config(mConfig, QStringLiteral("Yearprint"));
0083         config.writeEntry("Year", mYear);
0084         config.writeEntry("Pages", mPages);
0085         config.writeEntry("Pages", mPages);
0086         config.writeEntry("ShowSubDayEventsAs", mSubDaysEvents);
0087         config.writeEntry("ShowHolidaysAs", mHolidaysEvents);
0088     }
0089     CalPrintPluginBase::doSaveConfig();
0090 }
0091 
0092 QPageLayout::Orientation CalPrintYear::defaultOrientation() const
0093 {
0094     return (mPages == 1) ? QPageLayout::Landscape : QPageLayout::Portrait;
0095 }
0096 
0097 void CalPrintYear::setDateRange(const QDate &from, const QDate &to)
0098 {
0099     CalPrintPluginBase::setDateRange(from, to);
0100     auto cfg = dynamic_cast<CalPrintYearConfig *>((QWidget *)mConfigWidget);
0101     if (cfg) {
0102         cfg->mYear->setValue(from.year());
0103     }
0104 }
0105 
0106 void CalPrintYear::print(QPainter &p, int width, int height)
0107 {
0108     auto locale = QLocale::system();
0109 
0110     QRect headerBox(0, 0, width, headerHeight());
0111     QRect footerBox(0, height - footerHeight(), width, footerHeight());
0112     height -= footerHeight();
0113 
0114     QDate start(mYear, 1, 1);
0115 
0116     // Determine the nr of months and the max nr of days per month (dependent on
0117     // calendar system!!!!)
0118     QDate temp(start);
0119     const int months = 12;
0120     int maxdays = 1;
0121     for (int i = 1; i < months; ++i) {
0122         maxdays = qMax(maxdays, temp.daysInMonth());
0123         temp = temp.addMonths(1);
0124     }
0125 
0126     // Now determine the months per page so that the printout fits on
0127     // exactly mPages pages
0128     int monthsPerPage = (months - 1) / mPages + 1;
0129     int pages = (months - 1) / monthsPerPage + 1;
0130     int thismonth = 0;
0131     temp = start;
0132     for (int page = 0; page < pages; ++page) {
0133         if (page > 0) {
0134             mPrinter->newPage();
0135         }
0136         QDate end = start.addMonths(monthsPerPage);
0137         end = end.addDays(-1);
0138         QString stdate = locale.toString(start, QLocale::ShortFormat);
0139         QString endate = locale.toString(end, QLocale::ShortFormat);
0140         QString title = i18nc("date from-to", "%1\u2013%2", stdate, endate);
0141         drawHeader(p, title, start.addMonths(-1), start.addMonths(monthsPerPage), headerBox);
0142 
0143         QRect monthesBox(headerBox);
0144         monthesBox.setTop(monthesBox.bottom() + padding());
0145         monthesBox.setBottom(height);
0146 
0147         drawBox(p, BOX_BORDER_WIDTH, monthesBox);
0148         float monthwidth = float(monthesBox.width()) / float(monthsPerPage);
0149 
0150         for (int j = 0; j < monthsPerPage; ++j) {
0151             if (++thismonth > months) {
0152                 break;
0153             }
0154             int xstart = static_cast<int>(j * monthwidth + 0.5);
0155             int xend = static_cast<int>((j + 1) * monthwidth + 0.5);
0156             QRect monthBox(xstart, monthesBox.top(), xend - xstart, monthesBox.height());
0157             drawMonth(p, temp, monthBox, maxdays, mSubDaysEvents, mHolidaysEvents);
0158 
0159             temp = temp.addMonths(1);
0160         }
0161 
0162         drawFooter(p, footerBox);
0163         start = start.addMonths(monthsPerPage);
0164     }
0165 }