korganizer

calprinter.cpp
1 /*
2  This file is part of KOrganizer.
3 
4  Copyright (c) 1998 Preston Brown <pbrown@kde.org>
5  Copyright (C) 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com>
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 
21  As a special exception, permission is given to link this program
22  with any edition of TQt, and distribute the resulting executable,
23  without including the source code for TQt in the source distribution.
24 */
25 
26 #include <tqvbuttongroup.h>
27 #include <tqwidgetstack.h>
28 #include <tqradiobutton.h>
29 #include <tqlayout.h>
30 #include <tqpushbutton.h>
31 #include <tqcombobox.h>
32 #include <tqlabel.h>
33 #include <tqvbox.h>
34 #include <tqsplitter.h>
35 
36 #include <kprinter.h>
37 #include <ksimpleconfig.h>
38 #include <kdebug.h>
39 
40 #include "korganizer/corehelper.h"
41 
42 #include "calprinter.h"
43 #ifndef KORG_NOPRINTER
44 #include "calprinter.moc"
45 
46 #include "calprintdefaultplugins.h"
47 
48 CalPrinter::CalPrinter( TQWidget *parent, Calendar *calendar, KOrg::CoreHelper *helper )
49  : TQObject( parent, "CalPrinter" )
50 {
51  mParent = parent;
52  mConfig = new KSimpleConfig( "korganizer_printing.rc" );
53  mCoreHelper = helper;
54 
55  init( calendar );
56 }
57 
58 CalPrinter::~CalPrinter()
59 {
60  kdDebug(5850) << "~CalPrinter()" << endl;
61 
62  mPrintPlugins.clear();
63 
64  delete mConfig;
65 }
66 
67 void CalPrinter::init( Calendar *calendar )
68 {
69  mCalendar = calendar;
70 
71  mPrintPlugins.clear();
72  mPrintPlugins.setAutoDelete( true );
73 
74  mPrintPlugins = mCoreHelper->loadPrintPlugins();
75  mPrintPlugins.prepend( new CalPrintTodos() );
76  mPrintPlugins.prepend( new CalPrintMonth() );
77  mPrintPlugins.prepend( new CalPrintWeek() );
78  mPrintPlugins.prepend( new CalPrintDay() );
79  mPrintPlugins.prepend( new CalPrintIncidence() );
80 
81  KOrg::PrintPlugin::List::Iterator it = mPrintPlugins.begin();
82  for ( ; it != mPrintPlugins.end(); ++it ) {
83  if ( *it ) {
84  (*it)->setConfig( mConfig );
85  (*it)->setCalendar( mCalendar );
86  (*it)->setKOrgCoreHelper( mCoreHelper );
87  (*it)->doLoadConfig();
88  }
89  }
90 }
91 
92 void CalPrinter::setDateRange( const TQDate &fd, const TQDate &td )
93 {
94  KOrg::PrintPlugin::List::Iterator it = mPrintPlugins.begin();
95  for ( ; it != mPrintPlugins.end(); ++it ) {
96  (*it)->setDateRange( fd, td );
97  }
98 }
99 
100 void CalPrinter::print( int type, const TQDate &fd, const TQDate &td,
101  Incidence::List selectedIncidences, bool preview )
102 {
103  KOrg::PrintPlugin::List::Iterator it = mPrintPlugins.begin();
104  for ( it = mPrintPlugins.begin(); it != mPrintPlugins.end(); ++it ) {
105  (*it)->setSelectedIncidences( selectedIncidences );
106  }
107  CalPrintDialog printDialog( mPrintPlugins, mParent );
108  printDialog.setOrientation( CalPrinter::ePrintOrientation( mConfig->readNumEntry("Orientation", 1 ) ) );
109  printDialog.setPreview( preview );
110  printDialog.setPrintType( type );
111  setDateRange( fd, td );
112 
113  if ( printDialog.exec() == TQDialog::Accepted ) {
114  mConfig->writeEntry( "Orientation", printDialog.orientation() );
115 
116  // Save all changes in the dialog
117  for ( it = mPrintPlugins.begin(); it != mPrintPlugins.end(); ++it ) {
118  (*it)->doSaveConfig();
119  }
120  doPrint( printDialog.selectedPlugin(), printDialog.orientation(), preview );
121  }
122  for ( it = mPrintPlugins.begin(); it != mPrintPlugins.end(); ++it ) {
123  (*it)->setSelectedIncidences( Incidence::List() );
124  }
125 }
126 
127 void CalPrinter::doPrint( KOrg::PrintPlugin *selectedStyle,
128  CalPrinter::ePrintOrientation dlgorientation, bool preview )
129 {
130  if ( !selectedStyle ) {
131  KMessageBox::error( mParent,
132  i18n("Unable to print, no valid print style was returned."),
133  i18n("Printing error") );
134  return;
135  }
136  KPrinter printer;
137 
138  printer.setPreviewOnly( preview );
139  switch ( dlgorientation ) {
140  case eOrientPlugin:
141  printer.setOrientation( selectedStyle->defaultOrientation() );
142  break;
143  case eOrientPortrait:
144  printer.setOrientation( KPrinter::Portrait );
145  break;
146  case eOrientLandscape:
147  printer.setOrientation( KPrinter::Landscape );
148  break;
149  case eOrientPrinter:
150  default:
151  break;
152  }
153 
154  if ( preview || printer.setup( mParent, i18n("Print Calendar") ) ) {
155  selectedStyle->doPrint( &printer );
156  }
157 }
158 
160 
161 void CalPrinter::updateConfig()
162 {
163 }
164 
165 
166 
167 /****************************************************************************/
168 
169 CalPrintDialog::CalPrintDialog( KOrg::PrintPlugin::List plugins,
170  TQWidget *parent, const char *name )
171  : KDialogBase( parent, name, /*modal*/true, i18n("Print"), Ok | Cancel )
172 {
173  TQVBox *page = makeVBoxMainWidget();
174 
175  TQSplitter *splitter = new TQSplitter( page );
176  splitter->setOrientation( TQt::Horizontal );
177 
178  mTypeGroup = new TQVButtonGroup( i18n("Print Style"), splitter, "buttonGroup" );
179  // use the minimal width possible = max width of the radio buttons, not extensible
180 /* mTypeGroup->setSizePolicy( TQSizePolicy( (TQSizePolicy::SizeType)4,
181  (TQSizePolicy::SizeType)5, 0, 0,
182  mTypeGroup->sizePolicy().hasHeightForWidth() ) );*/
183 
184  TQWidget *splitterRight = new TQWidget( splitter, "splitterRight" );
185  TQGridLayout *splitterRightLayout = new TQGridLayout( splitterRight );
186  splitterRightLayout->setMargin( marginHint() );
187  splitterRightLayout->setSpacing( spacingHint() );
188 
189  mConfigArea = new TQWidgetStack( splitterRight, "configWidgetStack" );
190  splitterRightLayout->addMultiCellWidget( mConfigArea, 0,0, 0,1 );
191 
192  TQLabel *orientationLabel = new TQLabel( i18n("Page &orientation:"),
193  splitterRight, "orientationLabel" );
194  splitterRightLayout->addWidget( orientationLabel, 1, 0 );
195 
196  mOrientationSelection = new TQComboBox( splitterRight, "orientationCombo" );
197  mOrientationSelection->insertItem( i18n("Use Default Orientation of Selected Style") );
198  mOrientationSelection->insertItem( i18n("Use Printer Default") );
199  mOrientationSelection->insertItem( i18n("Portrait") );
200  mOrientationSelection->insertItem( i18n("Landscape") );
201  splitterRightLayout->addWidget( mOrientationSelection, 1, 1 );
202 
203  // signals and slots connections
204  connect( mTypeGroup, TQ_SIGNAL( clicked( int ) ), TQ_SLOT( setPrintType( int ) ) );
205  orientationLabel->setBuddy( mOrientationSelection );
206 
207  // First insert the config widgets into the widget stack. This possibly assigns
208  // proper ids (when two plugins have the same sortID), so store them in a map
209  // and use these new IDs to later sort the plugins for the type selection.
210  for ( KOrg::PrintPlugin::List::Iterator it = plugins.begin();
211  it != plugins.end(); ++it ) {
212  int newid = mConfigArea->addWidget( (*it)->configWidget( mConfigArea ), (*it)->sortID() );
213  mPluginIDs[newid] = (*it);
214  }
215  // Insert all plugins with in sorted order; plugins with clashing IDs will be first...
216  TQMap<int, KOrg::PrintPlugin*>::ConstIterator mapit;
217  for ( mapit = mPluginIDs.begin(); mapit != mPluginIDs.end(); ++mapit ) {
218  KOrg::PrintPlugin *p = mapit.data();
219  TQRadioButton *radioButton = new TQRadioButton( p->description(), mTypeGroup );
220  radioButton->setEnabled( p->enabled() );
221  mTypeGroup->insert( radioButton, mapit.key() );
222 // radioButton->setMinimumHeight( radioButton->sizeHint().height() - 5 );
223  }
224 
225  setMinimumSize( minimumSizeHint() );
226  resize( minimumSizeHint() );
227 }
228 
229 CalPrintDialog::~CalPrintDialog()
230 {
231 }
232 
233 void CalPrintDialog::setPreview(bool preview)
234 {
235  setButtonOK( preview ? i18n("&Preview") : KStdGuiItem::print() );
236 }
237 
238 void CalPrintDialog::setPrintType( int i )
239 {
240  mTypeGroup->setButton( i );
241  mConfigArea->raiseWidget( i );
242 }
243 
244 void CalPrintDialog::setOrientation( CalPrinter::ePrintOrientation orientation )
245 {
246  mOrientation = orientation;
247  mOrientationSelection->setCurrentItem( mOrientation );
248 }
249 
250 KOrg::PrintPlugin *CalPrintDialog::selectedPlugin()
251 {
252  int id = mTypeGroup->selectedId();
253  if ( mPluginIDs.contains( id ) ) {
254  return mPluginIDs[id];
255  } else {
256  return 0;
257  }
258 }
259 
260 void CalPrintDialog::slotOk()
261 {
262  mOrientation = (CalPrinter::ePrintOrientation)mOrientationSelection->currentItem();
263 
264  TQMap<int, KOrg::PrintPlugin*>::Iterator it = mPluginIDs.begin();
265  for ( ; it != mPluginIDs.end(); ++it ) {
266  if ( it.data() )
267  it.data()->readSettingsWidget();
268  }
269 
270  KDialogBase::slotOk();
271 }
272 
273 #endif
CalPrinter(TQWidget *par, Calendar *cal, KOrg::CoreHelper *helper)
Definition: calprinter.cpp:48
void setDateRange(const TQDate &start, const TQDate &end)
Set date range to be printed.
Definition: calprinter.cpp:92
Base class for KOrganizer printing classes.
Definition: printplugin.h:52
virtual void doPrint(KPrinter *printer)=0
Actually do the printing.
virtual TQString description()=0
Returns short description of print format.
virtual KPrinter::Orientation defaultOrientation()
Orientation of printout.
Definition: printplugin.h:116
virtual bool enabled()
Returns true if the plugin should be enabled; false otherwise.
Definition: printplugin.h:91
virtual void readSettingsWidget()
Read settings from configuration widget and apply them to current object.
Definition: printplugin.h:132