kaddressbook

printingwizard.cpp
1 /*
2  This file is part of KAddressBook.
3  Copyright (c) 1996-2002 Mirko Boehm <mirko@kde.org>
4  Tobias Koenig <tokoe@kde.org>
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 #include <tqcombobox.h>
26 #include <tqheader.h>
27 #include <tqlabel.h>
28 #include <tqlayout.h>
29 #include <tqlistview.h>
30 #include <tqpixmap.h>
31 #include <tqpushbutton.h>
32 #include <tqradiobutton.h>
33 
34 #include <tdeabc/addresseelist.h>
35 #include <tdeapplication.h>
36 #include <kdebug.h>
37 #include <kdialog.h>
38 #include <kdialogbase.h>
39 #include <tdelocale.h>
40 #include <kprinter.h>
41 
42 // including the styles
43 #include "detailledstyle.h"
44 #include "mikesstyle.h"
45 
46 #include "kabprefs.h"
47 #include "printprogress.h"
48 #include "printstyle.h"
49 #include "printsortmode.h"
50 
51 #include "printingwizard.h"
52 
53 using namespace KABPrinting;
54 
55 PrintingWizard::PrintingWizard( KPrinter *printer, TDEABC::AddressBook* ab,
56  const TQStringList& selection, TQWidget *parent,
57  const char* name )
58  : KWizard( parent, name ), mPrinter( printer ), mAddressBook( ab ),
59  mSelection( selection ), mStyle( 0 )
60 {
61  mSelectionPage = new SelectionPage( this );
62  mSelectionPage->setUseSelection( !selection.isEmpty() );
63  insertPage( mSelectionPage, i18n("Choose Contacts to Print"), -1 );
64 
65  mFilters = Filter::restore( kapp->config(), "Filter" );
66  TQStringList filters;
67  for ( Filter::List::ConstIterator it = mFilters.begin(); it != mFilters.end(); ++it )
68  filters.append( (*it).name() );
69 
70  mSelectionPage->setFilters( filters );
71 
72  mSelectionPage->setCategories( KABPrefs::instance()->customCategories() );
73 
74  setAppropriate( mSelectionPage, true );
75 
76 
77  mStylePage = new StylePage( mAddressBook, this );
78  connect( mStylePage, TQ_SIGNAL( styleChanged(int) ), TQ_SLOT( slotStyleSelected(int) ) );
79  insertPage( mStylePage, i18n("Choose Printing Style"), -1 );
80 
82 
83  if ( mStyleFactories.count() > 0 )
84  slotStyleSelected( 0 );
85 }
86 
87 PrintingWizard::~PrintingWizard()
88 {
89 }
90 
92 {
93  print();
94  close();
95 }
96 
98 {
99  mStyleFactories.append( new DetailledPrintStyleFactory( this ) );
100  mStyleFactories.append( new MikesStyleFactory( this ) );
101 
102  mStylePage->clearStyleNames();
103  for ( uint i = 0; i < mStyleFactories.count(); ++i )
104  mStylePage->addStyleName( mStyleFactories.at( i )->description() );
105 }
106 
108 {
109  if ( index < 0 || (uint)index >= mStyleFactories.count() )
110  return;
111 
112  setFinishEnabled( mStylePage, false );
113 
114  if ( mStyle )
115  mStyle->hidePages();
116 
117  if ( mStyleList.at( index ) != 0 )
118  mStyle = mStyleList.at( index );
119  else {
120  PrintStyleFactory *factory = mStyleFactories.at( index );
121  kdDebug(5720) << "PrintingWizardImpl::slotStyleSelected: "
122  << "creating print style "
123  << factory->description() << endl;
124  mStyle = factory->create();
125  mStyleList.insert( index, mStyle );
126  }
127 
128  mStyle->showPages();
129 
130  mStylePage->setPreview( mStyle->preview() );
131 
132  setFinishEnabled( page( pageCount() - 1 ), true );
133 
134  if ( mStyle->preferredSortField() != 0 ) {
135  mStylePage->setSortField( mStyle->preferredSortField() );
136  mStylePage->setSortAscending( mStyle->preferredSortType() );
137  }
138 }
139 
140 TDEABC::AddressBook* PrintingWizard::addressBook()
141 {
142  return mAddressBook;
143 }
144 
146 {
147  return mPrinter;
148 }
149 
151 {
152  // create and show print progress widget:
153  PrintProgress *progress = new PrintProgress( this );
154  insertPage( progress, i18n( "Print Progress" ), -1 );
155  showPage( progress );
156  kapp->processEvents();
157 
158  // prepare list of contacts to print:
159 
160  TDEABC::AddresseeList list;
161  if ( mStyle != 0 ) {
162  if ( mSelectionPage->useSelection() ) {
163  TQStringList::ConstIterator it;
164  for ( it = mSelection.begin(); it != mSelection.end(); ++it ) {
165  TDEABC::Addressee addr = addressBook()->findByUid( *it );
166  if ( !addr.isEmpty() )
167  list.append( addr );
168  }
169  } else if ( mSelectionPage->useFilters() ) {
170  // find contacts that can pass selected filter
171  Filter::List::ConstIterator filterIt;
172  for ( filterIt = mFilters.begin(); filterIt != mFilters.end(); ++filterIt )
173  if ( (*filterIt).name() == mSelectionPage->filter() )
174  break;
175 
176  TDEABC::AddressBook::ConstIterator it;
177  for ( it = addressBook()->begin(); it != addressBook()->end(); ++it ) {
178  if ( (*filterIt).filterAddressee( *it ) )
179  list.append( *it );
180  }
181 
182  } else if ( mSelectionPage->useCategories() ) {
183  TQStringList categories = mSelectionPage->categories();
184  TDEABC::AddressBook::ConstIterator it;
185  for ( it = addressBook()->begin(); it != addressBook()->end(); ++it ) {
186  const TQStringList tmp( (*it).categories() );
187  TQStringList::ConstIterator tmpIt;
188  for ( tmpIt = tmp.begin(); tmpIt != tmp.end(); ++tmpIt )
189  if ( categories.contains( *tmpIt ) ) {
190  list.append( *it );
191  break;
192  }
193  }
194  } else {
195  // create a string list of all entries:
196  TDEABC::AddressBook::ConstIterator it;
197  for ( it = addressBook()->begin(); it != addressBook()->end(); ++it )
198  list.append( *it );
199  }
200 
201  list.setReverseSorting( !mStylePage->sortAscending() );
202 
203 #if KDE_IS_VERSION(3,3,91)
204  PrintSortMode sortMode( mStylePage->sortField() );
205  list.sortByMode( &sortMode );
206 #else
207  list.sortByField( mStylePage->sortField() );
208 #endif
209  }
210 
211  kdDebug(5720) << "PrintingWizardImpl::print: printing "
212  << list.count() << " contacts." << endl;
213 
214  // ... print:
215  setBackEnabled( progress, false );
216  cancelButton()->setEnabled( false );
217  mStyle->print( list, progress );
218 }
219 
220 #include "printingwizard.moc"
void restore(TDEConfig *config)
Loads the filter from the config file.
Definition: filter.cpp:132
This defines a simple widget to display print progress information.
Definition: printprogress.h:41
The factories are used to have all object of the respective print style created in one place.
Definition: printstyle.h:152
virtual TQString description() const =0
Overload this method to provide a one-liner description for your print style.
bool preferredSortType()
Returns the preferred sort type.
Definition: printstyle.cpp:116
TDEABC::Field * preferredSortField()
Returns the preferred sort criterion field.
Definition: printstyle.cpp:111
const TQPixmap & preview()
Reimplement this method to provide a preview of what will be printed.
Definition: printstyle.cpp:44
void showPages()
Show all style specific pages in the wizard.
Definition: printstyle.cpp:85
void hidePages()
Hide all style specific pages in the wizard.
Definition: printstyle.cpp:99
virtual void print(const TDEABC::Addressee::List &contacts, PrintProgress *)=0
Reimplement this method to actually print.
TDEABC::AddressBook * addressBook()
Retrieve the document object.
void print()
Perform the actual printing.
void accept()
Overloaded accept slot.
void registerStyles()
Modify this method to add a new PrintStyle.
KPrinter * printer()
Retrieve the printer to be used.
PrintingWizard(KPrinter *printer, TDEABC::AddressBook *ab, const TQStringList &selection, TQWidget *parent=0, const char *name=0)
Construct a printing wizard.
void slotStyleSelected(int)
A print style has been selected.