korganizer

yearprint.cpp

00001 /*
00002     This file is part of KOrganizer.
00003 
00004     Copyright (c) 2004 Reinhold Kainhofer <reinhold@kainhofer.com>
00005 
00006     This program is free software; you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License as published by
00008     the Free Software Foundation; either version 2 of the License, or
00009     (at your option) any later version.
00010 
00011     This program is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00014     GNU General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License
00017     along with this program; if not, write to the Free Software
00018     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019 
00020     As a special exception, permission is given to link this program
00021     with any edition of TQt, and distribute the resulting executable,
00022     without including the source code for TQt in the source distribution.
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 <tdeconfig.h>
00033 #include <kdebug.h>
00034 #include <kcalendarsystem.h>
00035 #include <tdelocale.h>
00036 
00037 #include <tqcheckbox.h>
00038 #include <tqspinbox.h>
00039 #include <tqcombobox.h>
00040 #include <tqpainter.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  *           Print Year
00052  **************************************************************/
00053 
00054 TQWidget *CalPrintYear::createConfigWidget( TQWidget *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     TQDate 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( TQString::number( pages ), 0 );
00086       }
00087     }
00088 
00089     cfg->mYear->setValue( mYear );
00090     cfg->mPages->setCurrentText( TQString::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 TQDate& from, const TQDate& 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( TQPainter &p, int width, int height )
00139 {
00140   const KCalendarSystem *calsys = calendarSystem();
00141   TDELocale *locale = TDEGlobal::locale();
00142   if ( !calsys || !locale ) return;
00143 
00144   TQRect headerBox( 0, 0, width, headerHeight() );
00145   TQRect footerBox( 0, height - footerHeight(), width, footerHeight() );
00146   height -= footerHeight();
00147 
00148   TQDate start;
00149   calsys->setYMD( start, mYear, 1, 1 );
00150 
00151   // Determine the nr of months and the max nr of days per month (dependent on
00152   // calendar system!!!!)
00153   TQDate temp( start );
00154   int months = calsys->monthsInYear( start );
00155   int maxdays = 1;
00156   for ( int i = 1; i< months; ++i ) {
00157     maxdays = TQMAX( maxdays, temp.daysInMonth() );
00158     temp = calsys->addMonths( temp, 1 );
00159   }
00160 
00161   // Now determine the months per page so that the printout fits on
00162   // exactly mPages pages
00163   int monthsPerPage = (months-1) / mPages + 1;
00164   int pages = (months-1) / monthsPerPage + 1;
00165   int thismonth = 0;
00166   temp = start;
00167   for ( int page = 0; page < pages; ++page ) {
00168     if ( page > 0 ) {
00169       mPrinter->newPage();
00170     }
00171     TQDate end( calsys->addMonths( start, monthsPerPage ) );
00172     end = calsys->addDays( end, -1 );
00173     TQString title;
00174     if ( orientation() == KPrinter::Landscape ) {
00175       title = i18n("date from - to", "%1 - %2");
00176     } else {
00177       title = i18n("date from -\nto", "%1 -\n%2");
00178     }
00179     drawHeader( p, title
00180         .arg( locale->formatDate( start ) )
00181         .arg( locale->formatDate( end ) ),
00182       calsys->addMonths( start, -1), calsys->addMonths( start, monthsPerPage ),
00183       headerBox );
00184 
00185     TQRect monthesBox( headerBox );
00186     monthesBox.setTop( monthesBox.bottom() + padding() );
00187     monthesBox.setBottom( height );
00188 
00189     drawBox( p, BOX_BORDER_WIDTH, monthesBox );
00190     float monthwidth = float(monthesBox.width()) / float( monthsPerPage );
00191 
00192     for ( int j=0; j<monthsPerPage; ++j ) {
00193       if ( ++thismonth > months ) break;
00194       int xstart = int(j*monthwidth + 0.5);
00195       int xend = int((j+1)*monthwidth + 0.5);
00196       TQRect monthBox( xstart, monthesBox.top(), xend-xstart, monthesBox.height() );
00197       drawMonth( p, temp, monthBox, maxdays, mSubDaysEvents, mHolidaysEvents );
00198 
00199       temp = calsys->addMonths( temp, 1 );
00200     }
00201 
00202     drawFooter( p, footerBox );
00203     start = calsys->addMonths( start, monthsPerPage );
00204   }
00205 }
00206 
00207 #endif