printingwizard.cpp
00001 /* 00002 This file is part of KAddressBook. 00003 Copyright (c) 1996-2002 Mirko Boehm <mirko@kde.org> 00004 Tobias Koenig <tokoe@kde.org> 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 #include <tqcombobox.h> 00026 #include <tqheader.h> 00027 #include <tqlabel.h> 00028 #include <tqlayout.h> 00029 #include <tqlistview.h> 00030 #include <tqpixmap.h> 00031 #include <tqpushbutton.h> 00032 #include <tqradiobutton.h> 00033 00034 #include <kabc/addresseelist.h> 00035 #include <kapplication.h> 00036 #include <kdebug.h> 00037 #include <kdialog.h> 00038 #include <kdialogbase.h> 00039 #include <klocale.h> 00040 #include <kprinter.h> 00041 00042 // including the styles 00043 #include "detailledstyle.h" 00044 #include "mikesstyle.h" 00045 00046 #include "kabprefs.h" 00047 #include "printprogress.h" 00048 #include "printstyle.h" 00049 #include "printsortmode.h" 00050 00051 #include "printingwizard.h" 00052 00053 using namespace KABPrinting; 00054 00055 PrintingWizard::PrintingWizard( KPrinter *printer, KABC::AddressBook* ab, 00056 const TQStringList& selection, TQWidget *parent, 00057 const char* name ) 00058 : KWizard( parent, name ), mPrinter( printer ), mAddressBook( ab ), 00059 mSelection( selection ), mStyle( 0 ) 00060 { 00061 mSelectionPage = new SelectionPage( this ); 00062 mSelectionPage->setUseSelection( !selection.isEmpty() ); 00063 insertPage( mSelectionPage, i18n("Choose Contacts to Print"), -1 ); 00064 00065 mFilters = Filter::restore( kapp->config(), "Filter" ); 00066 TQStringList filters; 00067 for ( Filter::List::ConstIterator it = mFilters.begin(); it != mFilters.end(); ++it ) 00068 filters.append( (*it).name() ); 00069 00070 mSelectionPage->setFilters( filters ); 00071 00072 mSelectionPage->setCategories( KABPrefs::instance()->customCategories() ); 00073 00074 setAppropriate( mSelectionPage, true ); 00075 00076 00077 mStylePage = new StylePage( mAddressBook, this ); 00078 connect( mStylePage, TQT_SIGNAL( styleChanged(int) ), TQT_SLOT( slotStyleSelected(int) ) ); 00079 insertPage( mStylePage, i18n("Choose Printing Style"), -1 ); 00080 00081 registerStyles(); 00082 00083 if ( mStyleFactories.count() > 0 ) 00084 slotStyleSelected( 0 ); 00085 } 00086 00087 PrintingWizard::~PrintingWizard() 00088 { 00089 } 00090 00091 void PrintingWizard::accept() 00092 { 00093 print(); 00094 close(); 00095 } 00096 00097 void PrintingWizard::registerStyles() 00098 { 00099 mStyleFactories.append( new DetailledPrintStyleFactory( this ) ); 00100 mStyleFactories.append( new MikesStyleFactory( this ) ); 00101 00102 mStylePage->clearStyleNames(); 00103 for ( uint i = 0; i < mStyleFactories.count(); ++i ) 00104 mStylePage->addStyleName( mStyleFactories.at( i )->description() ); 00105 } 00106 00107 void PrintingWizard::slotStyleSelected( int index ) 00108 { 00109 if ( index < 0 || (uint)index >= mStyleFactories.count() ) 00110 return; 00111 00112 setFinishEnabled( mStylePage, false ); 00113 00114 if ( mStyle ) 00115 mStyle->hidePages(); 00116 00117 if ( mStyleList.at( index ) != 0 ) 00118 mStyle = mStyleList.at( index ); 00119 else { 00120 PrintStyleFactory *factory = mStyleFactories.at( index ); 00121 kdDebug(5720) << "PrintingWizardImpl::slotStyleSelected: " 00122 << "creating print style " 00123 << factory->description() << endl; 00124 mStyle = factory->create(); 00125 mStyleList.insert( index, mStyle ); 00126 } 00127 00128 mStyle->showPages(); 00129 00130 mStylePage->setPreview( mStyle->preview() ); 00131 00132 setFinishEnabled( page( pageCount() - 1 ), true ); 00133 00134 if ( mStyle->preferredSortField() != 0 ) { 00135 mStylePage->setSortField( mStyle->preferredSortField() ); 00136 mStylePage->setSortAscending( mStyle->preferredSortType() ); 00137 } 00138 } 00139 00140 KABC::AddressBook* PrintingWizard::addressBook() 00141 { 00142 return mAddressBook; 00143 } 00144 00145 KPrinter* PrintingWizard::printer() 00146 { 00147 return mPrinter; 00148 } 00149 00150 void PrintingWizard::print() 00151 { 00152 // create and show print progress widget: 00153 PrintProgress *progress = new PrintProgress( this ); 00154 insertPage( progress, i18n( "Print Progress" ), -1 ); 00155 showPage( progress ); 00156 kapp->processEvents(); 00157 00158 // prepare list of contacts to print: 00159 00160 KABC::AddresseeList list; 00161 if ( mStyle != 0 ) { 00162 if ( mSelectionPage->useSelection() ) { 00163 TQStringList::ConstIterator it; 00164 for ( it = mSelection.begin(); it != mSelection.end(); ++it ) { 00165 KABC::Addressee addr = addressBook()->findByUid( *it ); 00166 if ( !addr.isEmpty() ) 00167 list.append( addr ); 00168 } 00169 } else if ( mSelectionPage->useFilters() ) { 00170 // find contacts that can pass selected filter 00171 Filter::List::ConstIterator filterIt; 00172 for ( filterIt = mFilters.begin(); filterIt != mFilters.end(); ++filterIt ) 00173 if ( (*filterIt).name() == mSelectionPage->filter() ) 00174 break; 00175 00176 KABC::AddressBook::ConstIterator it; 00177 for ( it = addressBook()->begin(); it != addressBook()->end(); ++it ) { 00178 if ( (*filterIt).filterAddressee( *it ) ) 00179 list.append( *it ); 00180 } 00181 00182 } else if ( mSelectionPage->useCategories() ) { 00183 TQStringList categories = mSelectionPage->categories(); 00184 KABC::AddressBook::ConstIterator it; 00185 for ( it = addressBook()->begin(); it != addressBook()->end(); ++it ) { 00186 const TQStringList tmp( (*it).categories() ); 00187 TQStringList::ConstIterator tmpIt; 00188 for ( tmpIt = tmp.begin(); tmpIt != tmp.end(); ++tmpIt ) 00189 if ( categories.contains( *tmpIt ) ) { 00190 list.append( *it ); 00191 break; 00192 } 00193 } 00194 } else { 00195 // create a string list of all entries: 00196 KABC::AddressBook::ConstIterator it; 00197 for ( it = addressBook()->begin(); it != addressBook()->end(); ++it ) 00198 list.append( *it ); 00199 } 00200 00201 list.setReverseSorting( !mStylePage->sortAscending() ); 00202 00203 #if KDE_IS_VERSION(3,3,91) 00204 PrintSortMode sortMode( mStylePage->sortField() ); 00205 list.sortByMode( &sortMode ); 00206 #else 00207 list.sortByField( mStylePage->sortField() ); 00208 #endif 00209 } 00210 00211 kdDebug(5720) << "PrintingWizardImpl::print: printing " 00212 << list.count() << " contacts." << endl; 00213 00214 // ... print: 00215 setBackEnabled( progress, false ); 00216 cancelButton()->setEnabled( false ); 00217 mStyle->print( list, progress ); 00218 } 00219 00220 #include "printingwizard.moc"