• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

PIMPrint Library

  • sources
  • kde-4.14
  • kdepim
  • pimprint
  • calendar
calprintmonth.cpp
Go to the documentation of this file.
1 /*
2  T his file is part of *the PimPrint library.
3 
4  Copyright (C) 2013 Allen Winter <winter@kde.org>
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Library General Public
8  License as published by the Free Software Foundation; either
9  version 2 of the License, or (at your option) any later version.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Library General Public License for more details.
15 
16  You should have received a copy of the GNU Library General Public License
17  along with this library; see the file COPYING.LIB. If not, write to the
18  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  Boston, MA 02110-1301, USA.
20  */
21 
22 #include "calprintmonth.h"
23 
24 #include <KCalendarSystem>
25 #include <KLocale>
26 
27 using namespace PimPrint::Calendar;
28 
29 //@cond PRIVATE
30 class PimPrint::Calendar::CalPrintMonth::Private
31 {
32 public:
33  Private()
34  {
35  }
36 
37  QDate mStartDate; // starting date of print (for month and year)
38  QDate mEndDate; // ending date of print (for month and year)
39 };
40 //@endcond
41 
42 CalPrintMonth::CalPrintMonth(QPrinter *printer)
43  : CalPrintBase(printer),
44  d(new PimPrint::Calendar::CalPrintMonth::Private)
45 {
46  //TODO:
47  // set the calendar and calendar system
48 
49  // Set default print style
50  setPrintStyle(CalPrintBase::MonthClassic);
51 
52  // Set default Info options
53  setInfoOptions(0);
54 
55  // Set default Type options
56  setTypeOptions(0);
57 
58  // Set default Range options
59  setRangeOptions(0);
60 
61  // Set default Extra options
62  setExtraOptions(0);
63 }
64 
65 CalPrintMonth::~CalPrintMonth()
66 {
67  delete d;
68 }
69 
70 void CalPrintMonth::print(QPainter &p)
71 {
72  QDate fromMonth = d->mStartDate.addDays(-(d->mStartDate.day() - 1));
73  QDate toMonth = d->mEndDate.addDays(d->mEndDate.daysInMonth() - d->mEndDate.day());
74 
75  QRect headerBox(0, 0, pageWidth(), headerHeight());
76  QRect footerBox(0, pageHeight() - footerHeight(), pageWidth(), footerHeight());
77 
78  QRect monthBox(0, 0, pageWidth(), pageHeight() - footerHeight());
79  monthBox.setTop(headerBox.bottom() + padding());
80 
81  QDate curMonth = fromMonth;
82  do {
83  QString title(i18nc("monthname year", "%1 <numid>%2</numid>",
84  calendarSystem()->monthName(curMonth),
85  curMonth.year()));
86  QDate tmp(fromMonth);
87  int weekdayCol = weekdayColumn(tmp.dayOfWeek());
88  tmp = tmp.addDays(-weekdayCol);
89 
90  drawHeader(p, headerBox, title, curMonth.addMonths(-1), curMonth.addMonths(1));
91 
92  drawMonthTable(p, monthBox, curMonth, QTime(), QTime());
93 
94  if (extraOptions().testFlag(CalPrintBase::ExtraFooter)) {
95  drawFooter(p, footerBox);
96  }
97 
98  curMonth = curMonth.addDays(curMonth.daysInMonth());
99  if (curMonth <= toMonth) {
100  thePrinter()->newPage();
101  }
102  } while (curMonth <= toMonth);
103 
104 }
105 
106 void CalPrintMonth::setStartDate(const QDate &date)
107 {
108  d->mStartDate = date;
109 }
110 
111 QDate CalPrintMonth::startDate() const
112 {
113  return d->mStartDate;
114 }
115 
116 void CalPrintMonth::setEndDate(const QDate &date)
117 {
118  d->mEndDate = date;
119 }
120 
121 QDate CalPrintMonth::endDate() const
122 {
123  return d->mEndDate;
124 }
125 
126 void CalPrintMonth::drawMonthTable(QPainter &p, const QRect &box,
127  const QDate &date,
128  const QTime &fromTime, const QTime &toTime) const
129 {
130  int yoffset = subHeaderHeight();
131  int xoffset = 0;
132  QDate monthDate(QDate(date.year(), date.month(), 1));
133  QDate monthFirst(monthDate);
134  QDate monthLast(monthDate.addMonths(1).addDays(-1));
135 
136  int weekdayCol = weekdayColumn(monthDate.dayOfWeek());
137  monthDate = monthDate.addDays(-weekdayCol);
138 
139  if (extraOptions().testFlag(CalPrintBase::ExtraWeekNumbers)) {
140  xoffset += 14;
141  }
142 
143  int rows = (weekdayCol + date.daysInMonth() - 1) / 7 + 1;
144  double cellHeight = (box.height() - yoffset) / (1. * rows);
145  double cellWidth = (box.width() - xoffset) / 7.;
146 
147  // Precalculate the grid...
148  // rows is at most 6, so using 8 entries in the array is fine, too!
149  int coledges[8], rowedges[8];
150  for (int i = 0; i <= 7; ++i) {
151  rowedges[i] = int(box.top() + yoffset + i * cellHeight);
152  coledges[i] = int(box.left() + xoffset + i * cellWidth);
153  }
154 
155  if (extraOptions().testFlag(CalPrintBase::ExtraWeekNumbers)) {
156  //TODO: make this into a protected method for CalPrintBase
157  QFont oldFont(p.font());
158  QFont newFont(p.font());
159  newFont.setPointSize(6);
160  p.setFont(newFont);
161  QDate weekDate(monthDate);
162  for (int row = 0; row < rows; ++row) {
163  int calWeek = weekDate.weekNumber();
164  QRect rc(box.left(), rowedges[row],
165  coledges[0] - 3 - box.left(), rowedges[row + 1] - rowedges[row]);
166  p.drawText(rc, Qt::AlignRight | Qt::AlignVCenter, QString::number(calWeek));
167  weekDate = weekDate.addDays(7);
168  }
169  p.setFont(oldFont);
170  }
171 
172  QRect daysOfWeekBox(box);
173  daysOfWeekBox.setHeight(subHeaderHeight());
174  daysOfWeekBox.setLeft(box.left() + xoffset);
175  drawDaysOfWeek(p, daysOfWeekBox, monthDate, monthDate.addDays(6));
176 
177  QColor back = p.background().color();
178  bool darkbg = false;
179  for (int row = 0; row < rows; ++row) {
180  for (int col = 0; col < 7; ++col) {
181  // show days from previous/next month with a grayed background
182  if ((monthDate < monthFirst) || (monthDate > monthLast)) {
183  p.setBackground(back.darker(120));
184  darkbg = true;
185  }
186  QRect dayBox(coledges[col], rowedges[row],
187  coledges[col + 1] - coledges[col], rowedges[row + 1] - rowedges[row]);
188  drawDayBox(p, dayBox, monthDate, fromTime, toTime);
189  if (darkbg) {
190  p.setBackground(back);
191  darkbg = false;
192  }
193  monthDate = monthDate.addDays(1);
194  }
195  }
196 }
PimPrint::Calendar::CalPrintBase::setPrintStyle
void setPrintStyle(const Style style)
Sets the printing Style.
Definition: calprintbase.cpp:193
QFont::setPointSize
void setPointSize(int pointSize)
PimPrint::Calendar::CalPrintMonth
Definition: calprintmonth.h:33
PimPrint::Calendar::CalPrintBase::setExtraOptions
void setExtraOptions(ExtraOptions flags)
Sets extra options for printing.
Definition: calprintbase.cpp:361
QPainter::background
const QBrush & background() const
PimPrint::Calendar::CalPrintMonth::~CalPrintMonth
virtual ~CalPrintMonth()
Definition: calprintmonth.cpp:65
PimPrint::Calendar::CalPrintBase::thePrinter
QPrinter * thePrinter() const
Returns a pointer to the currently set QPrinter.
Definition: calprintbase.cpp:168
QPainter::setBackground
void setBackground(const QBrush &brush)
QDate::daysInMonth
int daysInMonth() const
PimPrint::Calendar::CalPrintBase::calendarSystem
KCalendarSystem * calendarSystem() const
Returns a pointer to the currently set calendar system.
Definition: calprintbase.cpp:188
QPrinter
QPainter::font
const QFont & font() const
QFont
PimPrint::Calendar::CalPrintBase::extraOptions
ExtraOptions extraOptions() const
Returns the current extra option flags.
Definition: calprintbase.cpp:366
PimPrint::Calendar::CalPrintBase::pageWidth
int pageWidth() const
Returns the current printed page width.
QRect::height
int height() const
QDate::month
int month() const
QTime
PimPrint::Calendar::CalPrintBase::weekdayColumn
int weekdayColumn(int weekday) const
Determines the column of the given weekday (1=Monday, 7=Sunday), taking the start of the week setting...
Definition: calprintbase.cpp:1330
PimPrint::Calendar::CalPrintBase::MonthClassic
Definition: calprintbase.h:130
PimPrint::Calendar::CalPrintMonth::setEndDate
void setEndDate(const QDate &date)
Sets the printout ending date.
Definition: calprintmonth.cpp:116
PimPrint::Calendar::CalPrintMonth::endDate
QDate endDate() const
Returns the current print ending date.
PimPrint::Calendar::CalPrintMonth::startDate
QDate startDate() const
Returns the current print starting date.
QDate::dayOfWeek
int dayOfWeek() const
QBrush::color
const QColor & color() const
QRect
QPainter::setFont
void setFont(const QFont &font)
PimPrint::Calendar::CalPrintMonth::CalPrintMonth
CalPrintMonth(QPrinter *printer)
Definition: calprintmonth.cpp:42
QString::number
QString number(int n, int base)
QDate::addMonths
QDate addMonths(int nmonths) const
QRect::top
int top() const
QRect::setTop
void setTop(int y)
QRect::left
int left() const
QPainter
PimPrint::Calendar::CalPrintBase::ExtraFooter
Definition: calprintbase.h:309
PimPrint::Calendar::CalPrintBase::drawFooter
int drawFooter(QPainter &p, const QRect &box) const
Draws a page footer containing the printing date and possibly other things, like a page number...
Definition: calprintbase.cpp:568
PimPrint::Calendar::CalPrintBase
Definition: calprintbase.h:67
QPainter::drawText
void drawText(const QPointF &position, const QString &text)
PimPrint::Calendar::CalPrintBase::padding
int padding() const
Returns the current padding margin.
QDate
QDate::year
int year() const
QString
QColor
PimPrint::Calendar::CalPrintBase::setRangeOptions
void setRangeOptions(RangeOptions flags)
Sets time-range options for printing.
Definition: calprintbase.cpp:351
PimPrint::Calendar::CalPrintBase::headerHeight
int headerHeight() const
Returns the current height of the page header.
QPrinter::newPage
bool newPage()
PimPrint::Calendar::CalPrintBase::setInfoOptions
void setInfoOptions(InfoOptions flags)
Sets information options for printing.
Definition: calprintbase.cpp:331
PimPrint::Calendar::CalPrintBase::drawHeader
int drawHeader(QPainter &p, const QRect &box, const QString &title, const QDate &leftMonth=QDate(), const QDate &rightMonth=QDate(), const bool expand=false, const QColor &backColor=QColor()) const
Draws the gray header bar of the printout to the QPainter.
Definition: calprintbase.cpp:497
PimPrint::Calendar::CalPrintBase::footerHeight
int footerHeight() const
Returns the current height of the page footer.
PimPrint::Calendar::CalPrintBase::ExtraWeekNumbers
Definition: calprintbase.h:306
PimPrint::Calendar::CalPrintMonth::print
void print(QPainter &p)
Definition: calprintmonth.cpp:70
PimPrint::Calendar::CalPrintBase::drawDayBox
void drawDayBox(QPainter &p, const QRect &box, const QDate &date, const QTime &startTime, const QTime &endTime, bool fullDate=false) const
Draws the box containing a list of all events and to-dos of the given day.
Definition: calprintbase.cpp:1141
QRect::width
int width() const
PimPrint::Calendar::CalPrintBase::drawDaysOfWeek
void drawDaysOfWeek(QPainter &p, const QRect &box, const QDate &fromDate, const QDate &toDate) const
Draws a horizontal bar with the weekday names of the given date range in the given area of the painte...
Definition: calprintbase.cpp:926
calprintmonth.h
PimPrint::Calendar::CalPrintBase::setTypeOptions
void setTypeOptions(TypeOptions flags)
Sets type options for printing.
Definition: calprintbase.cpp:341
PimPrint::Calendar::CalPrintBase::subHeaderHeight
int subHeaderHeight() const
Returns the current height of the page sub-header.
Definition: calprintbase.cpp:276
QRect::bottom
int bottom() const
PimPrint::Calendar::CalPrintMonth::setStartDate
void setStartDate(const QDate &date)
Sets the printout starting date.
Definition: calprintmonth.cpp:106
PimPrint::Calendar::CalPrintBase::pageHeight
int pageHeight() const
Returns the current printed page height.
QDate::addDays
QDate addDays(int ndays) const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:32:09 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

PIMPrint Library

Skip menu "PIMPrint Library"
  • Main Page
  • Namespace List
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer
  • pimprint

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal