00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef KORG_NOPRINTER
00026
00027 #include "calprintyearconfig_base.h"
00028 #include "yearprint.h"
00029
00030 #include <libkcal/calendar.h>
00031
00032 #include <kconfig.h>
00033 #include <kdebug.h>
00034 #include <kcalendarsystem.h>
00035 #include <klocale.h>
00036
00037 #include <qcheckbox.h>
00038 #include <qspinbox.h>
00039 #include <qcombobox.h>
00040 #include <qpainter.h>
00041
00042 class YearPrintFactory : public KOrg::PrintPluginFactory {
00043 public:
00044 KOrg::PrintPlugin *create() { return new CalPrintYear; }
00045 };
00046
00047 K_EXPORT_COMPONENT_FACTORY( libkorg_yearlyprint, YearPrintFactory )
00048
00049
00050
00051
00052
00053
00054 QWidget *CalPrintYear::createConfigWidget( QWidget *w )
00055 {
00056 return new CalPrintYearConfig_Base( w );
00057 }
00058
00059 void CalPrintYear::readSettingsWidget()
00060 {
00061 CalPrintYearConfig_Base *cfg =
00062 dynamic_cast<CalPrintYearConfig_Base*>( mConfigWidget );
00063 if ( cfg ) {
00064 mYear = cfg->mYear->value();
00065 mPages = cfg->mPages->currentText().toInt();
00066 mSubDaysEvents = (cfg->mSubDays->currentItem()==0)?Text:TimeBoxes;
00067 mHolidaysEvents = (cfg->mHolidays->currentItem()==0)?Text:TimeBoxes;
00068 }
00069 }
00070
00071 void CalPrintYear::setSettingsWidget()
00072 {
00073 CalPrintYearConfig_Base *cfg =
00074 dynamic_cast<CalPrintYearConfig_Base*>( mConfigWidget );
00075 if ( cfg ) {
00076 const KCalendarSystem *calsys = calendarSystem();
00077 QDate start;
00078 calsys->setYMD( start, mYear, 1, 1 );
00079 int months = calsys->monthsInYear( start );
00080 int pages=0, prevPages=0;
00081 for ( int i=1; i<= months; ++i ) {
00082 pages = (months-1)/i + 1;
00083 if ( pages != prevPages ) {
00084 prevPages = pages;
00085 cfg->mPages->insertItem( QString::number( pages ), 0 );
00086 }
00087 }
00088
00089 cfg->mYear->setValue( mYear );
00090 cfg->mPages->setCurrentText( QString::number( mPages ) );
00091
00092 cfg->mSubDays->setCurrentItem( (mSubDaysEvents==Text)?0:1 );
00093 cfg->mHolidays->setCurrentItem( (mHolidaysEvents==Text)?0:1 );
00094 }
00095 }
00096
00097 void CalPrintYear::loadConfig()
00098 {
00099 if ( mConfig ) {
00100 mYear = mConfig->readNumEntry( "Year", 2007 );
00101 mPages = mConfig->readNumEntry( "Pages", 1 );
00102 mSubDaysEvents = mConfig->readNumEntry( "ShowSubDayEventsAs", TimeBoxes );
00103 mHolidaysEvents = mConfig->readNumEntry( "ShowHolidaysAs", Text );
00104 }
00105 setSettingsWidget();
00106 }
00107
00108 void CalPrintYear::saveConfig()
00109 {
00110 kdDebug(5850) << "CalPrintYear::saveConfig()" << endl;
00111
00112 readSettingsWidget();
00113 if ( mConfig ) {
00114 mConfig->writeEntry( "Year", mYear );
00115 mConfig->writeEntry( "Pages", mPages );
00116 mConfig->writeEntry( "Pages", mPages );
00117 mConfig->writeEntry( "ShowSubDayEventsAs", mSubDaysEvents );
00118 mConfig->writeEntry( "ShowHolidaysAs", mHolidaysEvents );
00119 }
00120 }
00121
00122 KPrinter::Orientation CalPrintYear::defaultOrientation()
00123 {
00124 return ( mPages == 1 )?(KPrinter::Landscape):(KPrinter::Portrait);
00125 }
00126
00127
00128 void CalPrintYear::setDateRange( const QDate& from, const QDate& to )
00129 {
00130 CalPrintPluginBase::setDateRange( from, to );
00131 CalPrintYearConfig_Base *cfg =
00132 dynamic_cast<CalPrintYearConfig_Base*>( mConfigWidget );
00133 if ( cfg ) {
00134 cfg->mYear->setValue( from.year() );
00135 }
00136 }
00137
00138 void CalPrintYear::print( QPainter &p, int width, int height )
00139 {
00140 kdDebug()<<"CalPrintYear::print, width: "<<width<<", height: "<<height<<endl;
00141 QRect headerBox( 0, 0, width, headerHeight() );
00142 kdDebug()<<"headerBox: "<<headerBox<<endl;
00143 const KCalendarSystem *calsys = calendarSystem();
00144 KLocale *locale = KGlobal::locale();
00145 if ( !calsys || !locale ) return;
00146
00147 QDate start;
00148 calsys->setYMD( start, mYear, 1, 1 );
00149
00150
00151
00152 QDate temp( start );
00153 int months = calsys->monthsInYear( start );
00154 int maxdays = 1;
00155 for ( int i = 1; i< months; ++i ) {
00156 maxdays = QMAX( maxdays, temp.daysInMonth() );
00157 temp = calsys->addMonths( temp, 1 );
00158 }
00159
00160
00161
00162 int monthsPerPage = (months-1) / mPages + 1;
00163 int pages = (months-1) / monthsPerPage + 1;
00164 int thismonth = 0;
00165 temp = start;
00166 for ( int page = 0; page < pages; ++page ) {
00167 if ( page > 0 ) {
00168 mPrinter->newPage();
00169 }
00170 QDate end( calsys->addMonths( start, monthsPerPage ) );
00171 end = calsys->addDays( end, -1 );
00172 QString title;
00173 if ( orientation() == KPrinter::Landscape ) {
00174 title = i18n("date from - to", "%1 - %2");
00175 } else {
00176 title = i18n("date from -\nto", "%1 -\n%2");
00177 }
00178 drawHeader( p, title
00179 .arg( locale->formatDate( start ) )
00180 .arg( locale->formatDate( end ) ),
00181 calsys->addMonths( start, -1), calsys->addMonths( start, monthsPerPage ),
00182 headerBox );
00183
00184 QRect monthesBox( headerBox );
00185 monthesBox.setTop( monthesBox.bottom() + padding() );
00186 monthesBox.setBottom( height );
00187
00188 drawBox( p, BOX_BORDER_WIDTH, monthesBox );
00189 float monthwidth = float(monthesBox.width()) / float( monthsPerPage );
00190
00191 for ( int j=0; j<monthsPerPage; ++j ) {
00192 if ( ++thismonth > months ) break;
00193 int xstart = int(j*monthwidth + 0.5);
00194 int xend = int((j+1)*monthwidth + 0.5);
00195 QRect monthBox( xstart, monthesBox.top(), xend-xstart, monthesBox.height() );
00196 drawMonth( p, temp, monthBox, maxdays, mSubDaysEvents, mHolidaysEvents );
00197
00198 temp = calsys->addMonths( temp, 1 );
00199 }
00200 start = calsys->addMonths( start, monthsPerPage );
00201 }
00202 }
00203
00204 #endif