korganizer

calprinter.cpp

00001 /*
00002     This file is part of KOrganizer.
00003 
00004     Copyright (c) 1998 Preston Brown <pbrown@kde.org>
00005     Copyright (C) 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com>
00006 
00007     This program is free software; you can redistribute it and/or modify
00008     it under the terms of the GNU General Public License as published by
00009     the Free Software Foundation; either version 2 of the License, or
00010     (at your option) any later version.
00011 
00012     This program is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00015     GNU General Public License for more details.
00016 
00017     You should have received a copy of the GNU General Public License
00018     along with this program; if not, write to the Free Software
00019     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020 
00021     As a special exception, permission is given to link this program
00022     with any edition of Qt, and distribute the resulting executable,
00023     without including the source code for Qt in the source distribution.
00024 */
00025 
00026 #include <qvbuttongroup.h>
00027 #include <qwidgetstack.h>
00028 #include <qradiobutton.h>
00029 #include <qlayout.h>
00030 #include <qpushbutton.h>
00031 #include <qcombobox.h>
00032 #include <qlabel.h>
00033 #include <qvbox.h>
00034 #include <qsplitter.h>
00035 
00036 #include <kprinter.h>
00037 #include <ksimpleconfig.h>
00038 #include <kdebug.h>
00039 #include <kdeversion.h>
00040 
00041 #include "korganizer/corehelper.h"
00042 
00043 #include "calprinter.h"
00044 #ifndef KORG_NOPRINTER
00045 #include "calprinter.moc"
00046 
00047 #include "calprintdefaultplugins.h"
00048 
00049 CalPrinter::CalPrinter( QWidget *parent, Calendar *calendar, KOrg::CoreHelper *helper )
00050   : QObject( parent, "CalPrinter" )
00051 {
00052   mParent = parent;
00053   mConfig = new KSimpleConfig( "korganizer_printing.rc" );
00054   mCoreHelper = helper;
00055 
00056   init( calendar );
00057 }
00058 
00059 CalPrinter::~CalPrinter()
00060 {
00061   kdDebug(5850) << "~CalPrinter()" << endl;
00062 
00063   mPrintPlugins.clear();
00064 
00065   delete mConfig;
00066 }
00067 
00068 void CalPrinter::init( Calendar *calendar )
00069 {
00070   mCalendar = calendar;
00071 
00072   mPrintPlugins.clear();
00073   mPrintPlugins.setAutoDelete( true );
00074 
00075   mPrintPlugins = mCoreHelper->loadPrintPlugins();
00076   mPrintPlugins.prepend( new CalPrintTodos() );
00077   mPrintPlugins.prepend( new CalPrintMonth() );
00078   mPrintPlugins.prepend( new CalPrintWeek() );
00079   mPrintPlugins.prepend( new CalPrintDay() );
00080   mPrintPlugins.prepend( new CalPrintIncidence() );
00081 
00082   KOrg::PrintPlugin::List::Iterator it = mPrintPlugins.begin();
00083   for ( ; it != mPrintPlugins.end(); ++it ) {
00084     if ( *it ) {
00085       (*it)->setConfig( mConfig );
00086       (*it)->setCalendar( mCalendar );
00087       (*it)->setKOrgCoreHelper( mCoreHelper );
00088       (*it)->doLoadConfig();
00089     }
00090   }
00091 }
00092 
00093 void CalPrinter::setDateRange( const QDate &fd, const QDate &td )
00094 {
00095   KOrg::PrintPlugin::List::Iterator it = mPrintPlugins.begin();
00096   for ( ; it != mPrintPlugins.end(); ++it ) {
00097     (*it)->setDateRange( fd, td );
00098   }
00099 }
00100 
00101 void CalPrinter::print( int type, const QDate &fd, const QDate &td,
00102                         Incidence::List selectedIncidences, bool preview )
00103 {
00104   KOrg::PrintPlugin::List::Iterator it = mPrintPlugins.begin();
00105   for ( it = mPrintPlugins.begin(); it != mPrintPlugins.end(); ++it ) {
00106     (*it)->setSelectedIncidences( selectedIncidences );
00107   }
00108   CalPrintDialog printDialog( mPrintPlugins, mParent );
00109   printDialog.setOrientation( CalPrinter::ePrintOrientation( mConfig->readNumEntry("Orientation", 1 ) ) );
00110   printDialog.setPreview( preview );
00111   printDialog.setPrintType( type );
00112   setDateRange( fd, td );
00113 
00114   if ( printDialog.exec() == QDialog::Accepted ) {
00115     mConfig->writeEntry( "Orientation", printDialog.orientation() );
00116 
00117     // Save all changes in the dialog
00118     for ( it = mPrintPlugins.begin(); it != mPrintPlugins.end(); ++it ) {
00119       (*it)->doSaveConfig();
00120     }
00121     doPrint( printDialog.selectedPlugin(), printDialog.orientation(), preview );
00122   }
00123   for ( it = mPrintPlugins.begin(); it != mPrintPlugins.end(); ++it ) {
00124     (*it)->setSelectedIncidences( Incidence::List() );
00125   }
00126 }
00127 
00128 void CalPrinter::doPrint( KOrg::PrintPlugin *selectedStyle,
00129                           CalPrinter::ePrintOrientation dlgorientation, bool preview )
00130 {
00131   if ( !selectedStyle ) {
00132     KMessageBox::error( mParent,
00133                  i18n("Unable to print, no valid print style was returned."),
00134                  i18n("Printing error") );
00135     return;
00136   }
00137   KPrinter printer;
00138 
00139   printer.setPreviewOnly( preview );
00140   switch ( dlgorientation ) {
00141     case eOrientPlugin:
00142       printer.setOrientation( selectedStyle->defaultOrientation() );
00143       break;
00144     case eOrientPortrait:
00145       printer.setOrientation( KPrinter::Portrait );
00146       break;
00147     case eOrientLandscape:
00148       printer.setOrientation( KPrinter::Landscape );
00149       break;
00150     case eOrientPrinter:
00151     default:
00152       break;
00153   }
00154 
00155   if ( preview || printer.setup( mParent, i18n("Print Calendar") ) ) {
00156     selectedStyle->doPrint( &printer );
00157   }
00158 }
00159 
00161 
00162 void CalPrinter::updateConfig()
00163 {
00164 }
00165 
00166 
00167 
00168 /****************************************************************************/
00169 
00170 CalPrintDialog::CalPrintDialog( KOrg::PrintPlugin::List plugins,
00171                                 QWidget *parent, const char *name )
00172   : KDialogBase( parent, name, /*modal*/true, i18n("Print"), Ok | Cancel )
00173 {
00174   QVBox *page = makeVBoxMainWidget();
00175 
00176   QSplitter *splitter = new QSplitter( page );
00177   splitter->setOrientation( QSplitter::Horizontal );
00178 
00179   mTypeGroup = new QVButtonGroup( i18n("Print Style"), splitter, "buttonGroup" );
00180   // use the minimal width possible = max width of the radio buttons, not extensible
00181 /*  mTypeGroup->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)4,
00182     (QSizePolicy::SizeType)5, 0, 0,
00183       mTypeGroup->sizePolicy().hasHeightForWidth() ) );*/
00184 
00185   QWidget *splitterRight = new QWidget( splitter, "splitterRight" );
00186   QGridLayout *splitterRightLayout = new QGridLayout( splitterRight );
00187   splitterRightLayout->setMargin( marginHint() );
00188   splitterRightLayout->setSpacing( spacingHint() );
00189 
00190   mConfigArea = new QWidgetStack( splitterRight, "configWidgetStack" );
00191   splitterRightLayout->addMultiCellWidget( mConfigArea, 0,0, 0,1 );
00192 
00193   QLabel *orientationLabel = new QLabel( i18n("Page &orientation:"),
00194                                          splitterRight, "orientationLabel" );
00195   splitterRightLayout->addWidget( orientationLabel, 1, 0 );
00196 
00197   mOrientationSelection = new QComboBox( splitterRight, "orientationCombo" );
00198   mOrientationSelection->insertItem( i18n("Use Default Orientation of Selected Style") );
00199   mOrientationSelection->insertItem( i18n("Use Printer Default") );
00200   mOrientationSelection->insertItem( i18n("Portrait") );
00201   mOrientationSelection->insertItem( i18n("Landscape") );
00202   splitterRightLayout->addWidget( mOrientationSelection, 1, 1 );
00203 
00204   // signals and slots connections
00205   connect( mTypeGroup, SIGNAL( clicked( int ) ), SLOT( setPrintType( int ) ) );
00206   orientationLabel->setBuddy( mOrientationSelection );
00207 
00208   // First insert the config widgets into the widget stack. This possibly assigns
00209   // proper ids (when two plugins have the same sortID), so store them in a map
00210   // and use these new IDs to later sort the plugins for the type selection.
00211   for ( KOrg::PrintPlugin::List::Iterator it = plugins.begin();
00212         it != plugins.end(); ++it ) {
00213     int newid = mConfigArea->addWidget( (*it)->configWidget( mConfigArea ), (*it)->sortID() );
00214     mPluginIDs[newid] = (*it);
00215   }
00216   // Insert all plugins with in sorted order; plugins with clashing IDs will be first...
00217   QMap<int, KOrg::PrintPlugin*>::ConstIterator mapit;
00218   for ( mapit = mPluginIDs.begin(); mapit != mPluginIDs.end(); ++mapit ) {
00219     KOrg::PrintPlugin *p = mapit.data();
00220     QRadioButton *radioButton = new QRadioButton( p->description(), mTypeGroup );
00221     radioButton->setEnabled( p->enabled() );
00222     mTypeGroup->insert( radioButton, mapit.key() );
00223 //     radioButton->setMinimumHeight( radioButton->sizeHint().height() - 5 );
00224   }
00225 
00226   setMinimumSize( minimumSizeHint() );
00227   resize( minimumSizeHint() );
00228 }
00229 
00230 CalPrintDialog::~CalPrintDialog()
00231 {
00232 }
00233 
00234 void CalPrintDialog::setPreview(bool preview)
00235 {
00236 #if KDE_IS_VERSION( 3, 1, 93 )
00237   setButtonOK( preview ? i18n("&Preview") : KStdGuiItem::print() );
00238 #else
00239   setButtonOKText( preview ? i18n("&Preview") : i18n("&Print...") );
00240 #endif
00241 }
00242 
00243 void CalPrintDialog::setPrintType( int i )
00244 {
00245   mTypeGroup->setButton( i );
00246   mConfigArea->raiseWidget( i );
00247 }
00248 
00249 void CalPrintDialog::setOrientation( CalPrinter::ePrintOrientation orientation )
00250 {
00251   mOrientation = orientation;
00252   mOrientationSelection->setCurrentItem( mOrientation );
00253 }
00254 
00255 KOrg::PrintPlugin *CalPrintDialog::selectedPlugin()
00256 {
00257   int id = mTypeGroup->selectedId();
00258   if ( mPluginIDs.contains( id ) ) {
00259     return mPluginIDs[id];
00260   } else {
00261     return 0;
00262   }
00263 }
00264 
00265 void CalPrintDialog::slotOk()
00266 {
00267   mOrientation = (CalPrinter::ePrintOrientation)mOrientationSelection->currentItem();
00268 
00269   QMap<int, KOrg::PrintPlugin*>::Iterator it = mPluginIDs.begin();
00270   for ( ; it != mPluginIDs.end(); ++it ) {
00271     if ( it.data() )
00272       it.data()->readSettingsWidget();
00273   }
00274 
00275   KDialogBase::slotOk();
00276 }
00277 
00278 #endif
KDE Home | KDE Accessibility Home | Description of Access Keys