CalendarSupport

yearprint.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 "yearprint.h"
8
9#include "calendarsupport_debug.h"
10#include <KConfigGroup>
11#include <KLocalizedString>
12using namespace CalendarSupport;
13
14/**************************************************************
15 * Print Year
16 **************************************************************/
17
18QWidget *CalPrintYear::createConfigWidget(QWidget *w)
19{
20 return new CalPrintYearConfig(w);
21}
22
23void CalPrintYear::readSettingsWidget()
24{
25 auto cfg = dynamic_cast<CalPrintYearConfig *>((QWidget *)mConfigWidget);
26 if (cfg) {
27 mPrintFooter = cfg->mPrintFooter->isChecked();
28 mYear = cfg->mYear->value();
29 mPages = cfg->mPages->currentText().toInt();
30 mSubDaysEvents = (cfg->mSubDays->currentIndex() == 0) ? Text : TimeBoxes;
31 mHolidaysEvents = (cfg->mHolidays->currentIndex() == 0) ? Text : TimeBoxes;
32 mExcludeConfidential = cfg->mExcludeConfidential->isChecked();
33 mExcludePrivate = cfg->mExcludePrivate->isChecked();
34 }
35}
36
37void CalPrintYear::setSettingsWidget()
38{
39 auto cfg = dynamic_cast<CalPrintYearConfig *>((QWidget *)mConfigWidget);
40 if (cfg) {
41 QDate start(mYear, 1, 1);
42 const int months = 12;
43 int prevPages = 0;
44 for (int i = 1; i <= months; ++i) {
45 const int pages = (months - 1) / i + 1;
46 if (pages != prevPages) {
47 prevPages = pages;
48 cfg->mPages->addItem(QString::number(pages), pages);
49 }
50 }
51
52 cfg->mPrintFooter->setChecked(mPrintFooter);
53 cfg->mYear->setValue(mYear);
54 cfg->mPages->setCurrentIndex(cfg->mPages->findData(mPages));
55
56 cfg->mSubDays->setCurrentIndex((mSubDaysEvents == Text) ? 0 : 1);
57 cfg->mHolidays->setCurrentIndex((mHolidaysEvents == Text) ? 0 : 1);
58 cfg->mExcludeConfidential->setChecked(mExcludeConfidential);
59 cfg->mExcludePrivate->setChecked(mExcludePrivate);
60 }
61}
62
63void CalPrintYear::doLoadConfig()
64{
66 if (mConfig) {
67 KConfigGroup config(mConfig, QStringLiteral("Yearprint"));
68 mYear = config.readEntry("Year", QDate::currentDate().year());
69 mPages = config.readEntry("Pages", 1);
70 mSubDaysEvents = config.readEntry("ShowSubDayEventsAs", static_cast<int>(TimeBoxes));
71 mHolidaysEvents = config.readEntry("ShowHolidaysAs", static_cast<int>(Text));
72 }
73 setSettingsWidget();
74}
75
76void CalPrintYear::doSaveConfig()
77{
78 qCDebug(CALENDARSUPPORT_LOG);
79
80 readSettingsWidget();
81 if (mConfig) {
82 KConfigGroup config(mConfig, QStringLiteral("Yearprint"));
83 config.writeEntry("Year", mYear);
84 config.writeEntry("Pages", mPages);
85 config.writeEntry("Pages", mPages);
86 config.writeEntry("ShowSubDayEventsAs", mSubDaysEvents);
87 config.writeEntry("ShowHolidaysAs", mHolidaysEvents);
88 }
90}
91
92QPageLayout::Orientation CalPrintYear::defaultOrientation() const
93{
94 return (mPages == 1) ? QPageLayout::Landscape : QPageLayout::Portrait;
95}
96
97void CalPrintYear::setDateRange(const QDate &from, const QDate &to)
98{
100 auto cfg = dynamic_cast<CalPrintYearConfig *>((QWidget *)mConfigWidget);
101 if (cfg) {
102 cfg->mYear->setValue(from.year());
103 }
104}
105
106void CalPrintYear::print(QPainter &p, int width, int height)
107{
108 auto locale = QLocale::system();
109
110 QRect headerBox(0, 0, width, headerHeight());
111 QRect footerBox(0, height - footerHeight(), width, footerHeight());
112 height -= footerHeight();
113
114 QDate start(mYear, 1, 1);
115
116 // Determine the nr of months and the max nr of days per month (dependent on
117 // calendar system!!!!)
118 QDate temp(start);
119 const int months = 12;
120 int maxdays = 1;
121 for (int i = 1; i < months; ++i) {
122 maxdays = qMax(maxdays, temp.daysInMonth());
123 temp = temp.addMonths(1);
124 }
125
126 // Now determine the months per page so that the printout fits on
127 // exactly mPages pages
128 int monthsPerPage = (months - 1) / mPages + 1;
129 int pages = (months - 1) / monthsPerPage + 1;
130 int thismonth = 0;
131 temp = start;
132 for (int page = 0; page < pages; ++page) {
133 if (page > 0) {
134 mPrinter->newPage();
135 }
136 QDate end = start.addMonths(monthsPerPage);
137 end = end.addDays(-1);
138 QString stdate = locale.toString(start, QLocale::ShortFormat);
139 QString endate = locale.toString(end, QLocale::ShortFormat);
140 QString title = i18nc("date from-to", "%1\u2013%2", stdate, endate);
141 drawHeader(p, title, start.addMonths(-1), start.addMonths(monthsPerPage), headerBox);
142
143 QRect monthesBox(headerBox);
144 monthesBox.setTop(monthesBox.bottom() + padding());
145 monthesBox.setBottom(height);
146
147 drawBox(p, BOX_BORDER_WIDTH, monthesBox);
148 float monthwidth = float(monthesBox.width()) / float(monthsPerPage);
149
150 for (int j = 0; j < monthsPerPage; ++j) {
151 if (++thismonth > months) {
152 break;
153 }
154 int xstart = static_cast<int>(j * monthwidth + 0.5);
155 int xend = static_cast<int>((j + 1) * monthwidth + 0.5);
156 QRect monthBox(xstart, monthesBox.top(), xend - xstart, monthesBox.height());
157 drawMonth(p, temp, monthBox, maxdays, mSubDaysEvents, mHolidaysEvents);
158
159 temp = temp.addMonths(1);
160 }
161
162 drawFooter(p, footerBox);
163 start = start.addMonths(monthsPerPage);
164 }
165}
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".
static void drawBox(QPainter &p, int linewidth, QRect rect)
Draw a box with given width at the given coordinates.
int headerHeight() const
Returns the height of the page header.
void drawMonth(QPainter &p, QDate dt, QRect box, int maxdays=-1, int subDailyFlags=TimeBoxes, int holidaysFlags=Text)
Draw a vertical representation of the month containing the date dt.
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.
Q_SCRIPTABLE Q_NOREPLY void start()
QString i18nc(const char *context, const char *text, const TYPE &arg...)
const QList< QKeySequence > & end()
QDate currentDate()
int year() const const
QLocale system()
virtual bool newPage() override
QString number(double n, char format, int precision)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:13:02 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.