CalendarSupport

journalprint.cpp
1/*
2 SPDX-FileCopyrightText: 2004 Reinhold Kainhofer <reinhold@kainhofer.com>
3
4 SPDX-License-Identifier: GPL-2.0-or-later WITH Qt-Commercial-exception-1.0
5*/
6
7#include "journalprint.h"
8#include "calendarsupport_debug.h"
9#include "utils.h"
10#include <KConfigGroup>
11
12using namespace CalendarSupport;
13
14/**************************************************************
15 * Print Journal
16 **************************************************************/
17
18QWidget *CalPrintJournal::createConfigWidget(QWidget *w)
19{
20 return new CalPrintJournalConfig(w);
21}
22
23void CalPrintJournal::readSettingsWidget()
24{
25 auto cfg = dynamic_cast<CalPrintJournalConfig *>((QWidget *)mConfigWidget);
26 if (cfg) {
27 mPrintFooter = cfg->mPrintFooter->isChecked();
28 mFromDate = cfg->mFromDate->date();
29 mToDate = cfg->mToDate->date();
30 mUseDateRange = cfg->mRangeJournals->isChecked();
31 mExcludeConfidential = cfg->mExcludeConfidential->isChecked();
32 mExcludePrivate = cfg->mExcludePrivate->isChecked();
33 }
34}
35
36void CalPrintJournal::setSettingsWidget()
37{
38 auto cfg = dynamic_cast<CalPrintJournalConfig *>((QWidget *)mConfigWidget);
39 if (cfg) {
40 cfg->mPrintFooter->setChecked(mPrintFooter);
41 cfg->mFromDate->setDate(mFromDate);
42 cfg->mToDate->setDate(mToDate);
43 cfg->mExcludeConfidential->setChecked(mExcludeConfidential);
44 cfg->mExcludePrivate->setChecked(mExcludePrivate);
45
46 if (mUseDateRange) {
47 cfg->mRangeJournals->setChecked(true);
48 cfg->mFromDateLabel->setEnabled(true);
49 cfg->mFromDate->setEnabled(true);
50 cfg->mToDateLabel->setEnabled(true);
51 cfg->mToDate->setEnabled(true);
52 } else {
53 cfg->mAllJournals->setChecked(true);
54 cfg->mFromDateLabel->setEnabled(false);
55 cfg->mFromDate->setEnabled(false);
56 cfg->mToDateLabel->setEnabled(false);
57 cfg->mToDate->setEnabled(false);
58 }
59 }
60}
61
62void CalPrintJournal::doLoadConfig()
63{
65 if (mConfig) {
66 KConfigGroup config(mConfig, QStringLiteral("Journalprint"));
67 mUseDateRange = config.readEntry("JournalsInRange", false);
68 }
69 setSettingsWidget();
70}
71
72void CalPrintJournal::doSaveConfig()
73{
74 qCDebug(CALENDARSUPPORT_LOG);
75
76 readSettingsWidget();
77 if (mConfig) {
78 KConfigGroup config(mConfig, QStringLiteral("Journalprint"));
79 config.writeEntry("JournalsInRange", mUseDateRange);
80 }
82}
83
84void CalPrintJournal::setDateRange(const QDate &from, const QDate &to)
85{
87 auto cfg = dynamic_cast<CalPrintJournalConfig *>((QWidget *)mConfigWidget);
88 if (cfg) {
89 cfg->mFromDate->setDate(from);
90 cfg->mToDate->setDate(to);
91 }
92}
93
94void CalPrintJournal::drawJournal(const KCalendarCore::Journal::Ptr &journal, QPainter &p, int x, int &y, int width, int pageHeight)
95{
96 QFont oldFont(p.font());
97 p.setFont(QFont(QStringLiteral("sans-serif"), 15));
98 QString headerText;
99 QString dateText(QLocale::system().toString(journal->dtStart().toLocalTime().date(), QLocale::LongFormat));
100
101 if (journal->summary().isEmpty()) {
102 headerText = dateText;
103 } else {
104 headerText = i18nc("Description - date", "%1 - %2", journal->summary(), dateText);
105 }
106
107 QRect rect(p.boundingRect(x, y, width, -1, Qt::TextWordWrap, headerText));
108 if (rect.bottom() > pageHeight) {
109 if (mPrintFooter) {
110 drawFooter(p, {0, pageHeight, width, footerHeight()});
111 }
112 // Start new page...
113 y = 0;
114 mPrinter->newPage();
115 rect = p.boundingRect(x, y, width, -1, Qt::TextWordWrap, headerText);
116 }
117 QRect newrect;
118 p.drawText(rect, Qt::TextWordWrap, headerText, &newrect);
119 p.setFont(oldFont);
120
121 y = newrect.bottom() + 4;
122
123 p.drawLine(x + 3, y, x + width - 6, y);
124 y += 5;
125 if (!(journal->organizer().fullName().isEmpty())) {
126 drawTextLines(p, i18n("Person: %1", journal->organizer().fullName()), x, y, width, pageHeight, false);
127 y += 7;
128 }
129 if (!(journal->description().isEmpty())) {
130 drawTextLines(p, journal->description(), x, y, width, pageHeight, journal->descriptionIsRich());
131 y += 7;
132 }
133 y += 10;
134}
135
136void CalPrintJournal::print(QPainter &p, int width, int height)
137{
138 int x = 0;
139 int y = 0;
141 if (mUseDateRange) {
142 const KCalendarCore::Journal::List allJournals = journals;
143 journals.clear();
144 for (const KCalendarCore::Journal::Ptr &j : allJournals) {
145 const QDate dt = j->dtStart().date();
146 if (mFromDate <= dt && dt <= mToDate) {
147 journals.append(j);
148 }
149 }
150 }
151
152 QRect headerBox(0, 0, width, headerHeight());
153 QRect footerBox(0, height - footerHeight(), width, footerHeight());
154 height -= footerHeight();
155
156 drawHeader(p, i18n("Journal entries"), QDate(), QDate(), headerBox);
157 y = headerHeight() + 15;
158
159 for (const KCalendarCore::Journal::Ptr &j : std::as_const(journals)) {
160 Q_ASSERT(j);
163 drawJournal(j, p, x, y, width, height);
164 }
165 }
166
167 if (mPrintFooter) {
168 drawFooter(p, footerBox);
169 }
170}
int drawHeader(QPainter &p, const QString &title, QDate month1, QDate month2, QRect box, bool expand=false, QColor backColor=QColor())
Draw the gray header bar of the printout to the QPainter.
int footerHeight() const
Returns the height of the page footer.
bool mExcludePrivate
Whether or not to print incidences with secrecy "private".
void drawTextLines(QPainter &p, const QString &entry, int x, int &y, int width, int pageHeight, bool richTextEntry)
Draws text lines splitting on page boundaries.
int headerHeight() const
Returns the height of the page header.
int drawFooter(QPainter &p, QRect box)
Draw a page footer containing the printing date and possibly other things, like a page number.
bool mExcludeConfidential
Whether or not to print incidences with secrecy "confidential".
void doSaveConfig() override
Save complete configuration.
void doLoadConfig() override
Load complete configuration.
bool mPrintFooter
Whether or not to print a footer at the bottoms of pages.
virtual void setDateRange(const QDate &from, const QDate &to)
Set date range which should be printed.
QPrinter * mPrinter
The printer object.
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
AKONADI_CALENDAR_EXPORT KCalendarCore::Journal::Ptr journal(const Akonadi::Item &item)
char * toString(const EngineQuery &query)
void clear()
QLocale system()
QRect boundingRect(const QRect &rectangle, int flags, const QString &text)
void drawLine(const QLine &line)
void drawText(const QPoint &position, const QString &text)
const QFont & font() const const
void setFont(const QFont &font)
virtual bool newPage() override
int bottom() const const
TextWordWrap
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:05:28 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.