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 "journalprint.h"
0008 #include "calendarsupport_debug.h"
0009 #include "utils.h"
0010 #include <KConfigGroup>
0011 
0012 using namespace CalendarSupport;
0013 
0014 /**************************************************************
0015  *           Print Journal
0016  **************************************************************/
0017 
0018 QWidget *CalPrintJournal::createConfigWidget(QWidget *w)
0019 {
0020     return new CalPrintJournalConfig(w);
0021 }
0022 
0023 void CalPrintJournal::readSettingsWidget()
0024 {
0025     auto cfg = dynamic_cast<CalPrintJournalConfig *>((QWidget *)mConfigWidget);
0026     if (cfg) {
0027         mPrintFooter = cfg->mPrintFooter->isChecked();
0028         mFromDate = cfg->mFromDate->date();
0029         mToDate = cfg->mToDate->date();
0030         mUseDateRange = cfg->mRangeJournals->isChecked();
0031         mExcludeConfidential = cfg->mExcludeConfidential->isChecked();
0032         mExcludePrivate = cfg->mExcludePrivate->isChecked();
0033     }
0034 }
0035 
0036 void CalPrintJournal::setSettingsWidget()
0037 {
0038     auto cfg = dynamic_cast<CalPrintJournalConfig *>((QWidget *)mConfigWidget);
0039     if (cfg) {
0040         cfg->mPrintFooter->setChecked(mPrintFooter);
0041         cfg->mFromDate->setDate(mFromDate);
0042         cfg->mToDate->setDate(mToDate);
0043         cfg->mExcludeConfidential->setChecked(mExcludeConfidential);
0044         cfg->mExcludePrivate->setChecked(mExcludePrivate);
0045 
0046         if (mUseDateRange) {
0047             cfg->mRangeJournals->setChecked(true);
0048             cfg->mFromDateLabel->setEnabled(true);
0049             cfg->mFromDate->setEnabled(true);
0050             cfg->mToDateLabel->setEnabled(true);
0051             cfg->mToDate->setEnabled(true);
0052         } else {
0053             cfg->mAllJournals->setChecked(true);
0054             cfg->mFromDateLabel->setEnabled(false);
0055             cfg->mFromDate->setEnabled(false);
0056             cfg->mToDateLabel->setEnabled(false);
0057             cfg->mToDate->setEnabled(false);
0058         }
0059     }
0060 }
0061 
0062 void CalPrintJournal::doLoadConfig()
0063 {
0064     CalPrintPluginBase::doLoadConfig();
0065     if (mConfig) {
0066         KConfigGroup config(mConfig, QStringLiteral("Journalprint"));
0067         mUseDateRange = config.readEntry("JournalsInRange", false);
0068     }
0069     setSettingsWidget();
0070 }
0071 
0072 void CalPrintJournal::doSaveConfig()
0073 {
0074     qCDebug(CALENDARSUPPORT_LOG);
0075 
0076     readSettingsWidget();
0077     if (mConfig) {
0078         KConfigGroup config(mConfig, QStringLiteral("Journalprint"));
0079         config.writeEntry("JournalsInRange", mUseDateRange);
0080     }
0081     CalPrintPluginBase::doSaveConfig();
0082 }
0083 
0084 void CalPrintJournal::setDateRange(const QDate &from, const QDate &to)
0085 {
0086     CalPrintPluginBase::setDateRange(from, to);
0087     auto cfg = dynamic_cast<CalPrintJournalConfig *>((QWidget *)mConfigWidget);
0088     if (cfg) {
0089         cfg->mFromDate->setDate(from);
0090         cfg->mToDate->setDate(to);
0091     }
0092 }
0093 
0094 void CalPrintJournal::drawJournal(const KCalendarCore::Journal::Ptr &journal, QPainter &p, int x, int &y, int width, int pageHeight)
0095 {
0096     QFont oldFont(p.font());
0097     p.setFont(QFont(QStringLiteral("sans-serif"), 15));
0098     QString headerText;
0099     QString dateText(QLocale::system().toString(journal->dtStart().toLocalTime().date(), QLocale::LongFormat));
0100 
0101     if (journal->summary().isEmpty()) {
0102         headerText = dateText;
0103     } else {
0104         headerText = i18nc("Description - date", "%1 - %2", journal->summary(), dateText);
0105     }
0106 
0107     QRect rect(p.boundingRect(x, y, width, -1, Qt::TextWordWrap, headerText));
0108     if (rect.bottom() > pageHeight) {
0109         if (mPrintFooter) {
0110             drawFooter(p, {0, pageHeight, width, footerHeight()});
0111         }
0112         // Start new page...
0113         y = 0;
0114         mPrinter->newPage();
0115         rect = p.boundingRect(x, y, width, -1, Qt::TextWordWrap, headerText);
0116     }
0117     QRect newrect;
0118     p.drawText(rect, Qt::TextWordWrap, headerText, &newrect);
0119     p.setFont(oldFont);
0120 
0121     y = newrect.bottom() + 4;
0122 
0123     p.drawLine(x + 3, y, x + width - 6, y);
0124     y += 5;
0125     if (!(journal->organizer().fullName().isEmpty())) {
0126         drawTextLines(p, i18n("Person: %1", journal->organizer().fullName()), x, y, width, pageHeight, false);
0127         y += 7;
0128     }
0129     if (!(journal->description().isEmpty())) {
0130         drawTextLines(p, journal->description(), x, y, width, pageHeight, journal->descriptionIsRich());
0131         y += 7;
0132     }
0133     y += 10;
0134 }
0135 
0136 void CalPrintJournal::print(QPainter &p, int width, int height)
0137 {
0138     int x = 0;
0139     int y = 0;
0140     KCalendarCore::Journal::List journals(mCalendar->journals(KCalendarCore::JournalSortDate, KCalendarCore::SortDirectionAscending));
0141     if (mUseDateRange) {
0142         const KCalendarCore::Journal::List allJournals = journals;
0143         journals.clear();
0144         for (const KCalendarCore::Journal::Ptr &j : allJournals) {
0145             const QDate dt = j->dtStart().date();
0146             if (mFromDate <= dt && dt <= mToDate) {
0147                 journals.append(j);
0148             }
0149         }
0150     }
0151 
0152     QRect headerBox(0, 0, width, headerHeight());
0153     QRect footerBox(0, height - footerHeight(), width, footerHeight());
0154     height -= footerHeight();
0155 
0156     drawHeader(p, i18n("Journal entries"), QDate(), QDate(), headerBox);
0157     y = headerHeight() + 15;
0158 
0159     for (const KCalendarCore::Journal::Ptr &j : std::as_const(journals)) {
0160         Q_ASSERT(j);
0161         if (j && (!mExcludeConfidential || j->secrecy() != KCalendarCore::Incidence::SecrecyConfidential)
0162             && (!mExcludePrivate || j->secrecy() != KCalendarCore::Incidence::SecrecyPrivate)) {
0163             drawJournal(j, p, x, y, width, height);
0164         }
0165     }
0166 
0167     if (mPrintFooter) {
0168         drawFooter(p, footerBox);
0169     }
0170 }