yearprint.cpp
1/*
2 This file is part of KOrganizer.
3
4 Copyright (c) 2004 Reinhold Kainhofer <reinhold@kainhofer.com>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program 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 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
20 As a special exception, permission is given to link this program
21 with any edition of TQt, and distribute the resulting executable,
22 without including the source code for TQt in the source distribution.
23*/
24
25#ifndef KORG_NOPRINTER
26
27#include "calprintyearconfig_base.h"
28#include "yearprint.h"
29
30#include <libkcal/calendar.h>
31
32#include <tdeconfig.h>
33#include <kdebug.h>
34#include <kcalendarsystem.h>
35#include <tdelocale.h>
36
37#include <tqcheckbox.h>
38#include <tqspinbox.h>
39#include <tqcombobox.h>
40#include <tqpainter.h>
41
42class YearPrintFactory : public KOrg::PrintPluginFactory {
43 public:
44 KOrg::PrintPlugin *create() { return new CalPrintYear; }
45};
46
47K_EXPORT_COMPONENT_FACTORY( libkorg_yearlyprint, YearPrintFactory )
48
49
50/**************************************************************
51 * Print Year
52 **************************************************************/
53
54TQWidget *CalPrintYear::createConfigWidget( TQWidget *w )
55{
56 return new CalPrintYearConfig_Base( w );
57}
58
59void CalPrintYear::readSettingsWidget()
60{
61 CalPrintYearConfig_Base *cfg =
62 dynamic_cast<CalPrintYearConfig_Base*>( mConfigWidget );
63 if ( cfg ) {
64 mYear = cfg->mYear->value();
65 mPages = cfg->mPages->currentText().toInt();
66 mSubDaysEvents = (cfg->mSubDays->currentItem()==0)?Text:TimeBoxes;
67 mHolidaysEvents = (cfg->mHolidays->currentItem()==0)?Text:TimeBoxes;
68 }
69}
70
71void CalPrintYear::setSettingsWidget()
72{
73 CalPrintYearConfig_Base *cfg =
74 dynamic_cast<CalPrintYearConfig_Base*>( mConfigWidget );
75 if ( cfg ) {
76 const KCalendarSystem *calsys = calendarSystem();
77 TQDate start;
78 calsys->setYMD( start, mYear, 1, 1 );
79 int months = calsys->monthsInYear( start );
80 int pages=0, prevPages=0;
81 for ( int i=1; i<= months; ++i ) {
82 pages = (months-1)/i + 1;
83 if ( pages != prevPages ) {
84 prevPages = pages;
85 cfg->mPages->insertItem( TQString::number( pages ), 0 );
86 }
87 }
88
89 cfg->mYear->setValue( mYear );
90 cfg->mPages->setCurrentText( TQString::number( mPages ) );
91
92 cfg->mSubDays->setCurrentItem( (mSubDaysEvents==Text)?0:1 );
93 cfg->mHolidays->setCurrentItem( (mHolidaysEvents==Text)?0:1 );
94 }
95}
96
97void CalPrintYear::loadConfig()
98{
99 if ( mConfig ) {
100 mYear = mConfig->readNumEntry( "Year", 2007 );
101 mPages = mConfig->readNumEntry( "Pages", 1 );
102 mSubDaysEvents = mConfig->readNumEntry( "ShowSubDayEventsAs", TimeBoxes );
103 mHolidaysEvents = mConfig->readNumEntry( "ShowHolidaysAs", Text );
104 }
105 setSettingsWidget();
106}
107
108void CalPrintYear::saveConfig()
109{
110 kdDebug(5850) << "CalPrintYear::saveConfig()" << endl;
111
112 readSettingsWidget();
113 if ( mConfig ) {
114 mConfig->writeEntry( "Year", mYear );
115 mConfig->writeEntry( "Pages", mPages );
116 mConfig->writeEntry( "Pages", mPages );
117 mConfig->writeEntry( "ShowSubDayEventsAs", mSubDaysEvents );
118 mConfig->writeEntry( "ShowHolidaysAs", mHolidaysEvents );
119 }
120}
121
122KPrinter::Orientation CalPrintYear::defaultOrientation()
123{
124 return ( mPages == 1 )?(KPrinter::Landscape):(KPrinter::Portrait);
125}
126
127
128void CalPrintYear::setDateRange( const TQDate& from, const TQDate& to )
129{
131 CalPrintYearConfig_Base *cfg =
132 dynamic_cast<CalPrintYearConfig_Base*>( mConfigWidget );
133 if ( cfg ) {
134 cfg->mYear->setValue( from.year() );
135 }
136}
137
138void CalPrintYear::print( TQPainter &p, int width, int height )
139{
140 const KCalendarSystem *calsys = calendarSystem();
141 TDELocale *locale = TDEGlobal::locale();
142 if ( !calsys || !locale ) return;
143
144 TQRect headerBox( 0, 0, width, headerHeight() );
145 TQRect footerBox( 0, height - footerHeight(), width, footerHeight() );
146 height -= footerHeight();
147
148 TQDate start;
149 calsys->setYMD( start, mYear, 1, 1 );
150
151 // Determine the nr of months and the max nr of days per month (dependent on
152 // calendar system!!!!)
153 TQDate temp( start );
154 int months = calsys->monthsInYear( start );
155 int maxdays = 1;
156 for ( int i = 1; i< months; ++i ) {
157 maxdays = TQMAX( maxdays, temp.daysInMonth() );
158 temp = calsys->addMonths( temp, 1 );
159 }
160
161 // Now determine the months per page so that the printout fits on
162 // exactly mPages pages
163 int monthsPerPage = (months-1) / mPages + 1;
164 int pages = (months-1) / monthsPerPage + 1;
165 int thismonth = 0;
166 temp = start;
167 for ( int page = 0; page < pages; ++page ) {
168 if ( page > 0 ) {
169 mPrinter->newPage();
170 }
171 TQDate end( calsys->addMonths( start, monthsPerPage ) );
172 end = calsys->addDays( end, -1 );
173 TQString title;
174 if ( orientation() == KPrinter::Landscape ) {
175 title = i18n("date from - to", "%1 - %2");
176 } else {
177 title = i18n("date from -\nto", "%1 -\n%2");
178 }
179 drawHeader( p, title
180 .arg( locale->formatDate( start ) )
181 .arg( locale->formatDate( end ) ),
182 calsys->addMonths( start, -1), calsys->addMonths( start, monthsPerPage ),
183 headerBox );
184
185 TQRect monthesBox( headerBox );
186 monthesBox.setTop( monthesBox.bottom() + padding() );
187 monthesBox.setBottom( height );
188
189 drawBox( p, BOX_BORDER_WIDTH, monthesBox );
190 float monthwidth = float(monthesBox.width()) / float( monthsPerPage );
191
192 for ( int j=0; j<monthsPerPage; ++j ) {
193 if ( ++thismonth > months ) break;
194 int xstart = int(j*monthwidth + 0.5);
195 int xend = int((j+1)*monthwidth + 0.5);
196 TQRect monthBox( xstart, monthesBox.top(), xend-xstart, monthesBox.height() );
197 drawMonth( p, temp, monthBox, maxdays, mSubDaysEvents, mHolidaysEvents );
198
199 temp = calsys->addMonths( temp, 1 );
200 }
201
202 drawFooter( p, footerBox );
203 start = calsys->addMonths( start, monthsPerPage );
204 }
205}
206
207#endif
Base class for KOrganizer printing classes.
Definition: printplugin.h:52
virtual void setDateRange(const TQDate &from, const TQDate &to)
Set date range which should be printed.
Definition: printplugin.h:141