kdeprint Library API Documentation

kprinter.cpp

00001 /* 00002 * This file is part of the KDE libraries 00003 * Copyright (c) 2001 Michael Goffioul <kdeprint@swing.be> 00004 * 00005 * This library is free software; you can redistribute it and/or 00006 * modify it under the terms of the GNU Library General Public 00007 * License version 2 as published by the Free Software Foundation. 00008 * 00009 * This library is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 * Library General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU Library General Public License 00015 * along with this library; see the file COPYING.LIB. If not, write to 00016 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 00017 * Boston, MA 02111-1307, USA. 00018 **/ 00019 00020 #include <config.h> 00021 00022 #include "kprinter.h" 00023 #include "kprinterimpl.h" 00024 #include "kprintdialog.h" 00025 #include "kprintpreview.h" 00026 #include "kmfactory.h" 00027 #include "kmuimanager.h" 00028 #include "kmmanager.h" 00029 #include "driver.h" 00030 00031 #include <qpaintdevicemetrics.h> 00032 #include <qfile.h> 00033 #include <qtl.h> 00034 #include <qdir.h> 00035 #include <qguardedptr.h> 00036 #include <kapplication.h> 00037 #include <kstandarddirs.h> 00038 #include <kglobal.h> 00039 #include <kconfig.h> 00040 #include <krun.h> 00041 #include <knotifyclient.h> 00042 #include <kdebug.h> 00043 #include <klocale.h> 00044 #include <kprocess.h> 00045 #include <klibloader.h> 00046 #include <kmessagebox.h> 00047 00048 static void dumpOptions(const QMap<QString,QString>& opts); 00049 static void reportError(KPrinter*); 00050 00051 //************************************************************************************** 00052 // KPrinterWrapper class 00053 //************************************************************************************** 00054 00055 class KPrinterWrapper : public QPrinter 00056 { 00057 friend class KPrinter; 00058 public: 00059 KPrinterWrapper(KPrinter*, PrinterMode m = ScreenResolution); 00060 ~KPrinterWrapper(); 00061 protected: 00062 virtual bool cmd(int, QPainter*, QPDevCmdParam*); 00063 virtual int metric(int) const; 00064 int qprinterMetric(int) const; 00065 private: 00066 KPrinter *m_printer; 00067 }; 00068 00069 KPrinterWrapper::KPrinterWrapper(KPrinter *prt, QPrinter::PrinterMode m) 00070 : QPrinter(m), m_printer(prt) 00071 { 00072 } 00073 00074 KPrinterWrapper::~KPrinterWrapper() 00075 { 00076 } 00077 00078 bool KPrinterWrapper::cmd(int c, QPainter *painter, QPDevCmdParam *p) 00079 { 00080 return QPrinter::cmd(c,painter,p); 00081 } 00082 00083 int KPrinterWrapper::metric(int m) const 00084 { 00085 return m_printer->metric(m); 00086 } 00087 00088 int KPrinterWrapper::qprinterMetric(int m) const 00089 { 00090 return QPrinter::metric(m); 00091 } 00092 00093 //************************************************************************************** 00094 // KPrinterPrivate class 00095 //************************************************************************************** 00096 00097 class KPrinterPrivate 00098 { 00099 public: 00100 QGuardedPtr<KPrinterImpl> m_impl; 00101 bool m_restore; 00102 bool m_previewonly; 00103 WId m_parentId; 00104 QString m_docfilename; 00105 QString m_docdirectory; 00106 KPrinterWrapper *m_wrapper; 00107 QMap<QString,QString> m_options; 00108 QString m_tmpbuffer; 00109 QString m_printername; 00110 QString m_searchname; 00111 QString m_errormsg; 00112 bool m_ready; 00113 int m_pagenumber; 00114 DrPageSize *m_pagesize; 00115 bool m_useprinterres; 00116 int m_defaultres; 00117 }; 00118 00119 //************************************************************************************** 00120 // KPrinter class 00121 //************************************************************************************** 00122 00123 KPrinter::KPrinter(bool restore, QPrinter::PrinterMode m) 00124 : QPaintDevice(QInternal::Printer|QInternal::ExternalDevice) 00125 { 00126 init(restore, m); 00127 } 00128 00129 KPrinter::~KPrinter() 00130 { 00131 // delete Wrapper object 00132 delete d->m_wrapper; 00133 00134 // save current options 00135 if (d->m_restore) 00136 saveSettings(); 00137 00138 // delete private data (along any data allocated internally) 00139 delete d->m_pagesize; 00140 delete d; 00141 } 00142 00143 void KPrinter::init(bool restore, QPrinter::PrinterMode m) 00144 { 00145 // Private data initialization 00146 d = new KPrinterPrivate; 00147 d->m_impl = KMFactory::self()->printerImplementation(); 00148 d->m_restore = restore; 00149 d->m_previewonly = false; 00150 d->m_parentId = 0; 00151 d->m_pagesize = 0; 00152 00153 // initialize QPrinter wrapper 00154 d->m_wrapper = new KPrinterWrapper(this, m); 00155 00156 // other initialization 00157 d->m_tmpbuffer = d->m_impl->tempFile(); 00158 d->m_ready = false; 00159 d->m_defaultres = d->m_wrapper->resolution(); 00160 d->m_useprinterres = false; 00161 00162 // reload options from implementation (static object) 00163 if (d->m_restore) 00164 loadSettings(); 00165 } 00166 00167 void KPrinter::loadSettings() 00168 { 00169 d->m_options = d->m_impl->loadOptions(); 00170 00171 // load the last printer used in the current process (if any) 00172 // and remove the corresponding entry in the option map, as it 00173 // is not needed anymore 00174 setSearchName(option("kde-searchname")); 00175 d->m_options.remove("kde-searchname"); 00176 00177 KConfig *conf = KGlobal::config(), *pconf = KMFactory::self()->printConfig(); 00178 conf->setGroup("KPrinter Settings"); 00179 pconf->setGroup("General"); 00180 00181 // load latest used printer from config file, if required in the options 00182 if (searchName().isEmpty() && pconf->readBoolEntry("UseLast", true)) 00183 setSearchName(conf->readEntry("Printer")); 00184 00185 // latest used print command 00186 setOption("kde-printcommand",conf->readPathEntry("PrintCommand")); 00187 00188 // latest used document directory 00189 setDocDirectory( conf->readPathEntry( "DocDirectory" ) ); 00190 setDocFileName( "print" ); 00191 } 00192 00193 void KPrinter::saveSettings() 00194 { 00195 if (d->m_impl) 00196 { 00197 setOption("kde-searchname", searchName()); 00198 d->m_impl->saveOptions(d->m_options); 00199 } 00200 00201 // save latest used printer to config file 00202 KConfig *conf = KGlobal::config(); 00203 conf->setGroup("KPrinter Settings"); 00204 conf->writeEntry("Printer",searchName()); 00205 // latest used print command 00206 conf->writePathEntry("PrintCommand",option("kde-printcommand")); 00207 00208 // latest used document directory 00209 if ( d->m_docdirectory.isEmpty() ) 00210 { 00211 KURL url( outputFileName() ); 00212 if ( url.isValid() ) 00213 conf->writePathEntry( "DocDirectory", url.directory() ); 00214 } 00215 else 00216 conf->writePathEntry( "DocDirectory", d->m_docdirectory ); 00217 } 00218 00219 bool KPrinter::setup(QWidget *parent, const QString& caption, bool forceExpand) 00220 { 00221 if (!kapp->authorize("print/dialog")) 00222 { 00223 autoConfigure(QString::null, parent); 00224 return true; // Just print it 00225 } 00226 00227 if (parent) 00228 d->m_parentId = parent->winId(); 00229 00230 KPrintDialog *dlg = KPrintDialog::printerDialog(this, parent, caption, forceExpand); 00231 bool state = false; 00232 if (dlg) 00233 { 00234 state = dlg->exec(); 00235 delete dlg; 00236 } 00237 return state; 00238 } 00239 00240 void KPrinter::addStandardPage(int p) 00241 { 00242 KMFactory::self()->settings()->standardDialogPages |= p; 00243 } 00244 00245 void KPrinter::removeStandardPage(int p) 00246 { 00247 KMFactory::self()->settings()->standardDialogPages &= (~p); 00248 } 00249 00250 void KPrinter::addDialogPage(KPrintDialogPage *page) 00251 { 00252 KMFactory::self()->uiManager()->addPrintDialogPage(page); 00253 } 00254 00255 void KPrinter::setPageSelection(PageSelectionType t) 00256 { 00257 KMFactory::self()->settings()->pageSelection = t; 00258 } 00259 00260 KPrinter::PageSelectionType KPrinter::pageSelection() 00261 { 00262 return (PageSelectionType)KMFactory::self()->settings()->pageSelection; 00263 } 00264 00265 void KPrinter::setApplicationType(ApplicationType t) 00266 { 00267 KMFactory::self()->settings()->application = t; 00268 } 00269 00270 KPrinter::ApplicationType KPrinter::applicationType() 00271 { 00272 return (ApplicationType)KMFactory::self()->settings()->application; 00273 } 00274 00275 bool KPrinter::cmd(int c, QPainter *painter, QPDevCmdParam *p) 00276 { 00277 bool value(true); 00278 if (c == QPaintDevice::PdcBegin) 00279 { 00280 d->m_impl->statusMessage(i18n("Initialization..."), this); 00281 d->m_pagenumber = 1; 00282 preparePrinting(); 00283 d->m_impl->statusMessage(i18n("Generating print data: page %1").arg(d->m_pagenumber), this); 00284 } 00285 value = d->m_wrapper->cmd(c,painter,p); 00286 if (c == QPaintDevice::PdcEnd) 00287 { 00288 // this call should take care of everything (preview, output-to-file, filtering, ...) 00289 value = value && printFiles(QStringList(d->m_wrapper->outputFileName()),true); 00290 // reset "ready" state 00291 finishPrinting(); 00292 } 00293 return value; 00294 } 00295 00296 void KPrinter::translateQtOptions() 00297 { 00298 d->m_wrapper->setCreator(creator()); 00299 d->m_wrapper->setDocName(docName()); 00300 d->m_wrapper->setFullPage(fullPage()); 00301 d->m_wrapper->setColorMode((QPrinter::ColorMode)colorMode()); 00302 d->m_wrapper->setOrientation((QPrinter::Orientation)orientation()); 00303 if ( !option( "kde-printsize" ).isEmpty() ) 00304 d->m_wrapper->setPageSize( ( QPrinter::PageSize )option( "kde-printsize" ).toInt() ); 00305 else 00306 d->m_wrapper->setPageSize((QPrinter::PageSize)pageSize()); 00307 d->m_wrapper->setOutputToFile(true); 00308 d->m_wrapper->setOutputFileName(d->m_tmpbuffer); 00309 d->m_wrapper->setNumCopies(option("kde-qtcopies").isEmpty() ? 1 : option("kde-qtcopies").toInt()); 00310 if (!option("kde-margin-top").isEmpty()) 00311 { 00318 int res = resolution(); 00319 d->m_wrapper->setMargins( 00320 ( int )( ( option("kde-margin-top").toFloat() * res + 71 ) / 72 ), 00321 ( int )( ( option("kde-margin-left").toFloat() * res + 71 ) / 72 ), 00322 ( int )( ( option("kde-margin-bottom").toFloat() * res + 71 ) / 72 ), 00323 ( int )( ( option("kde-margin-right").toFloat() * res + 71 ) / 72 ) ); 00324 } 00325 else if ( d->m_pagesize != NULL ) 00326 { 00327 int res = resolution(); 00328 DrPageSize *ps = d->m_pagesize; 00329 d->m_wrapper->setMargins( 00330 ( int )( ( ps->topMargin() * res + 71 ) / 72 ), 00331 ( int )( ( ps->leftMargin() * res + 71 ) / 72 ), 00332 ( int )( ( ps->bottomMargin() * res + 71 ) / 72 ), 00333 ( int )( ( ps->rightMargin() * res + 71 ) / 72 ) ); 00334 } 00335 /*else 00336 { 00337 int res = d->m_wrapper->resolution(); 00338 d->m_wrapper->setMargins( res/3, res/2, res/3, res/2 ); 00339 }*/ 00340 // for special printers, copies are handled by Qt 00341 if (option("kde-isspecial") == "1") 00342 d->m_wrapper->setNumCopies(numCopies()); 00343 } 00344 00345 bool KPrinter::printFiles(const QStringList& l, bool flag, bool startviewer) 00346 { 00347 QStringList files(l); 00348 bool status(true); 00349 00350 // First apply possible filters, and update "remove" flag if filters has 00351 // been applied (result == 0, means nothing happened). 00352 int fresult = d->m_impl->filterFiles(this, files, flag); 00353 if (fresult == -1) 00354 { 00355 reportError(this); 00356 status = false; 00357 } 00358 else if (fresult == 1) 00359 flag = true; 00360 00361 if (status) 00362 { 00363 // Automatic conversion to format supported by print system 00364 fresult = d->m_impl->autoConvertFiles(this, files, flag); 00365 if (fresult == -1) 00366 { 00367 reportError(this); 00368 status = false; 00369 } 00370 else if (fresult == 1) 00371 flag = true; 00372 } 00373 00374 // Continue if status is OK (filtering succeeded) and no output-to-file 00375 if (status && files.count() > 0) 00376 { 00377 // Show preview if needed (only possible for a single file !), and stop 00378 // if the user requested it. Force preview if preview-only mode has been set: it 00379 // then use by default the first file in the list. 00380 if (((files.count() != 1 || option("kde-preview") != "1") && !d->m_previewonly) || doPreview(files[0])) 00381 { 00382 // check if printing has been prepared (it may be not prepared if the KPrinter object is not 00383 // use as a QPaintDevice object) 00384 preparePrinting(); 00385 00386 if (!d->m_impl->printFiles(this, files, flag)) 00387 { 00388 reportError(this); 00389 status = false; 00390 } 00391 else 00392 { 00393 if (/* !outputToFile() && */ startviewer) 00394 { 00395 QStringList args; 00396 args << "-d"; 00397 args << printerName(); 00398 args << "--noshow"; 00399 kapp->kdeinitExec("kjobviewer", args); 00400 } 00401 } 00402 } 00403 else if (flag) 00404 // situation: only one file, it has been previewed and printing has been canceled, then 00405 // we should remove the file ourself 00406 { 00407 QFile::remove(files[0]); 00408 } 00409 } 00410 finishPrinting(); 00411 return status; 00412 } 00413 00414 bool KPrinter::doPreview(const QString& file) 00415 { 00416 d->m_impl->statusMessage(i18n("Previewing..."), this); 00417 d->m_impl->statusMessage(QString::null, this); 00418 return KPrintPreview::preview(file, d->m_previewonly, d->m_parentId); 00419 } 00420 00421 void KPrinter::preparePrinting() 00422 { 00423 // check if already prepared (-> do nothing) 00424 if (d->m_ready) return; 00425 00426 // re-initialize error 00427 setErrorMessage(QString::null); 00428 00429 // re-initialize margins and page size (by default, use Qt mechanism) 00430 setRealPageSize(NULL); 00431 00432 // print-system-specific setup, only if not printing to file 00433 if (option("kde-isspecial") != "1") 00434 d->m_impl->preparePrinting(this); 00435 00436 // set the correct resolution, if needed (or reset it) 00437 int res = option( "kde-resolution" ).toInt(); 00438 if ( d->m_useprinterres && res > 0 ) 00439 d->m_wrapper->setResolution( res ); 00440 else 00441 d->m_wrapper->setResolution( d->m_defaultres ); 00442 00443 // standard Qt settings 00444 translateQtOptions(); 00445 00446 d->m_ready = true; 00447 dumpOptions(d->m_options); 00448 } 00449 00450 void KPrinter::finishPrinting() 00451 { 00452 d->m_ready = false; 00453 // close the status window 00454 d->m_impl->statusMessage(QString::null, this); 00455 } 00456 00457 QValueList<int> KPrinter::pageList() const 00458 { 00459 QValueList<int> list; 00460 int mp(minPage()), MP(maxPage()); 00461 if (mp > 0 && MP > 0 && MP >= mp) 00462 { // do something only if bounds specified 00463 if (option("kde-current") == "1") 00464 { // print only current page 00465 int pp = currentPage(); 00466 if (pp >= mp && pp <= MP) list.append(pp); 00467 } 00468 else 00469 { 00470 // process range specification 00471 if (!option("kde-range").isEmpty()) 00472 { 00473 QStringList ranges = QStringList::split(',',option("kde-range"),false); 00474 // use a temporary map to insure uniqueness of pages. 00475 QMap<int,int> pages; 00476 for (QStringList::ConstIterator it=ranges.begin();it!=ranges.end();++it) 00477 { 00478 int p = (*it).find('-'); 00479 bool ok; 00480 if (p == -1) 00481 { 00482 int pp = (*it).toInt(&ok); 00483 if (ok && pp >= mp && pp <= MP) 00484 pages[pp] = 1; 00485 } 00486 else 00487 { 00488 int p1(0), p2(0); 00489 p1 = (*it).left(p).toInt(&ok); 00490 if (ok) p2 = (*it).right((*it).length()-p-1).toInt(&ok); 00491 if (ok && p1 <= p2) 00492 { 00493 // clip to min/max 00494 p1 = QMAX(mp,p1); 00495 p2 = QMIN(MP,p2); 00496 for (int i=p1;i<=p2;i++) 00497 pages[i] = 1; 00498 } 00499 } 00500 } 00501 // translate the map into the list 00502 for (QMap<int,int>::ConstIterator it=pages.begin();it!=pages.end();++it) 00503 list.append(it.key()); 00504 // sort the list 00505 qHeapSort(list); 00506 } 00507 else 00508 { // add all pages between min and max 00509 for (int i=mp;i<=MP;i++) list.append(i); 00510 } 00511 00512 // revert the list if needed 00513 if (pageOrder() == LastPageFirst) 00514 { 00515 for (uint i=0;i<(list.count()/2);i++) 00516 qSwap(list[i],list[list.count()-1-i]); 00517 } 00518 00519 // select page set if needed 00520 if (pageSet() != AllPages) 00521 { 00522 bool keepEven = (pageSet() == EvenPages); 00523 for (QValueList<int>::Iterator it=list.begin();it!=list.end();) 00524 if ((((*it) % 2) != 0 && keepEven) || 00525 (((*it) % 2) == 0 && !keepEven)) it = list.remove(it); 00526 else ++it; 00527 } 00528 } 00529 } 00530 return list; 00531 } 00532 00533 //************************************************************************************** 00534 // QPrinter interface 00535 //************************************************************************************** 00536 00537 int KPrinter::numCopies() const 00538 { 00539 bool ok; 00540 int p = option("kde-copies").toInt(&ok); 00541 return (ok ? p : 1); 00542 } 00543 00544 QSize KPrinter::margins() const 00545 { 00546 return d->m_wrapper->margins(); 00547 } 00548 00549 void KPrinter::margins( uint *top, uint *left, uint *bottom, uint *right ) const 00550 { 00551 d->m_wrapper->margins( top, left, bottom, right ); 00552 } 00553 00554 int KPrinter::metric(int m) const 00555 { 00556 if (d->m_pagesize == NULL || !option( "kde-printsize" ).isEmpty()) 00557 return d->m_wrapper->qprinterMetric(m); 00558 00559 int val(0); 00560 bool land = (orientation() == KPrinter::Landscape); 00561 uint res(d->m_wrapper->resolution()), top = res/2, left = res/2, bottom = res/3, right = res/2; 00562 margins( &top, &left, &bottom, &right ); 00563 switch ( m ) 00564 { 00565 case QPaintDeviceMetrics::PdmWidth: 00566 val = (land ? ( int )d->m_pagesize->pageHeight() : ( int )d->m_pagesize->pageWidth()); 00567 if ( res != 72 ) 00568 val = (val * res + 36) / 72; 00569 if ( !fullPage() ) 00570 val -= ( left + right ); 00571 break; 00572 case QPaintDeviceMetrics::PdmHeight: 00573 val = (land ? ( int )d->m_pagesize->pageWidth() : ( int )d->m_pagesize->pageHeight()); 00574 if ( res != 72 ) 00575 val = (val * res + 36) / 72; 00576 if ( !fullPage() ) 00577 val -= ( top + bottom ); 00578 break; 00579 case QPaintDeviceMetrics::PdmWidthMM: 00580 val = metric( QPaintDeviceMetrics::PdmWidth ); 00581 val = (val * 254 + 5*res) / (10*res); // +360 to get the right rounding 00582 break; 00583 case QPaintDeviceMetrics::PdmHeightMM: 00584 val = metric( QPaintDeviceMetrics::PdmHeight ); 00585 val = (val * 254 + 5*res) / (10*res); 00586 break; 00587 default: 00588 val = d->m_wrapper->qprinterMetric(m); 00589 break; 00590 } 00591 return val; 00592 } 00593 00594 void KPrinter::setOrientation(Orientation o) 00595 { 00596 KMFactory::self()->settings()->orientation = o; 00597 setOption("kde-orientation",(o == Landscape ? "Landscape" : "Portrait")); 00598 d->m_impl->broadcastOption("kde-orientation",(o == Landscape ? "Landscape" : "Portrait")); 00599 d->m_impl->broadcastOption( "kde-orientation-fixed", "1" ); 00600 } 00601 00602 void KPrinter::setOption( const QString& key, const QString& value, bool broadcast ) 00603 { 00604 setOption( key, value ); 00605 if ( broadcast ) 00606 d->m_impl->broadcastOption( key, value ); 00607 } 00608 00609 void KPrinter::setPageSize(PageSize s) 00610 { 00611 KMFactory::self()->settings()->pageSize = s; 00612 setOption("kde-pagesize",QString::number((int)s),true); 00613 d->m_impl->broadcastOption( "kde-pagesize-fixed", "1" ); 00614 } 00615 00616 void KPrinter::setOptions(const QMap<QString,QString>& opts) 00617 { // This functions remove all options except those with "kde-..." 00618 // which correspond to externally-sets options (use the value 00619 // from "opts" if specified 00620 QMap<QString,QString> tmpset = d->m_options; 00621 d->m_options = opts; 00622 // remove some problematic options that may not be overwritten (ugly hack). 00623 // Default values will be used instead, except if the dialog has set new ones. 00624 tmpset.remove("kde-pagesize"); 00625 tmpset.remove( "kde-printsize" ); 00626 tmpset.remove("kde-orientation"); 00627 tmpset.remove("kde-colormode"); 00628 tmpset.remove("kde-margin-top"); 00629 tmpset.remove("kde-margin-left"); 00630 tmpset.remove("kde-margin-bottom"); 00631 tmpset.remove("kde-margin-right"); 00632 tmpset.remove( "kde-resolution" ); 00633 tmpset.remove( "kde-fonts" ); 00634 for (QMap<QString,QString>::ConstIterator it=tmpset.begin();it!=tmpset.end();++it) 00635 if (it.key().left(4) == "kde-" && !(d->m_options.contains(it.key()))) 00636 d->m_options[it.key()] = it.data(); 00637 } 00638 00639 void KPrinter::initOptions(const QMap<QString,QString>& opts) 00640 { // This function can be used to initialize the KPrinter object just after 00641 // creation to set some options. Non global options will be propagated to 00642 // all listed printers (non-global => start with "kde-...") 00643 for (QMap<QString,QString>::ConstIterator it=opts.begin(); it!=opts.end(); ++it) 00644 { 00645 setOption(it.key(), it.data()); 00646 if (it.key().left(4) != "kde-") 00647 d->m_impl->broadcastOption(it.key(),it.data()); 00648 } 00649 } 00650 00651 void KPrinter::reload() 00652 { 00653 d->m_impl = KMFactory::self()->printerImplementation(); 00654 int global = KMFactory::self()->settings()->orientation; 00655 if (global != -1) setOrientation((KPrinter::Orientation)global); 00656 global = KMFactory::self()->settings()->pageSize; 00657 if (global != -1) setPageSize((KPrinter::PageSize)global); 00658 //initOptions(d->m_options); 00659 } 00660 00661 bool KPrinter::autoConfigure(const QString& prname, QWidget *parent) 00662 { 00663 KMManager *mgr = KMManager::self(); 00664 KMPrinter *mprt(0); 00665 00666 mgr->printerList(false); 00667 if (prname.isEmpty()) 00668 mprt = mgr->defaultPrinter(); 00669 else 00670 mprt = mgr->findPrinter(prname); 00671 00672 if (mprt) 00673 return mprt->autoConfigure(this, parent); 00674 else 00675 return false; 00676 } 00677 00678 //************************************************************************************** 00679 // Util functions 00680 //************************************************************************************** 00681 00682 void reportError(KPrinter *p) 00683 { 00684 if (!KNotifyClient::event("printerror",i18n("<p><nobr>A print error occurred. Error message received from system:</nobr></p><br>%1").arg(p->errorMessage()))) 00685 kdDebug(500) << "could not send notify event" << endl; 00686 } 00687 00688 KPrinter::PageSize pageNameToPageSize(const QString& _name) 00689 { 00690 QString name = _name.upper(); 00691 if (name == "LETTER") return KPrinter::Letter; 00692 else if (name == "LEGAL") return KPrinter::Legal; 00693 else if (name == "A4") return KPrinter::A4; 00694 else if (name == "A3") return KPrinter::A3; 00695 else if (name == "EXECUTIVE") return KPrinter::Executive; 00696 else if (name == "LEDGER") return KPrinter::Ledger; 00697 else if (name == "TABLOID") return KPrinter::Tabloid; 00698 else if (name == "FOLIO") return KPrinter::Folio; 00699 else if (name == "A5") return KPrinter::A5; 00700 else if (name == "A6") return KPrinter::A6; 00701 else if (name == "A7") return KPrinter::A7; 00702 else if (name == "A8") return KPrinter::A8; 00703 else if (name == "A9") return KPrinter::A9; 00704 else if (name == "A2") return KPrinter::A2; 00705 else if (name == "A1") return KPrinter::A1; 00706 else if (name == "A0") return KPrinter::A0; 00707 else if (name == "B0" || name == "B0ISO") return KPrinter::B0; 00708 else if (name == "B1" || name == "B1ISO") return KPrinter::B1; 00709 else if (name == "B2" || name == "B2ISO") return KPrinter::B2; 00710 else if (name == "B3" || name == "B3ISO") return KPrinter::B3; 00711 else if (name == "B4" || name == "B4ISO") return KPrinter::B4; 00712 else if (name == "B5" || name == "B5ISO") return KPrinter::B5; 00713 else if (name == "B6" || name == "B6ISO") return KPrinter::B6; 00714 else if (name == "B7" || name == "B7ISO") return KPrinter::B7; 00715 else if (name == "B8" || name == "B8ISO") return KPrinter::B8; 00716 else if (name == "B9" || name == "B9ISO") return KPrinter::B9; 00717 else if (name == "B10" || name == "B10ISO") return KPrinter::B10; 00718 else if (name == "C5" || name == "C5E" || name == "ENVC5") return KPrinter::C5E; 00719 else if (name == "DL" || name == "DLE" || name == "ENVDL") return KPrinter::DLE; 00720 else if (name == "COMM10" || name == "COM10" || name == "ENV10") return KPrinter::Comm10E; 00721 else return KPrinter::A4; 00722 } 00723 00724 const char* pageSizeToPageName(KPrinter::PageSize s) 00725 { 00726 switch(s) 00727 { 00728 case KPrinter::Letter: return "Letter"; 00729 case KPrinter::Legal: return "Legal"; 00730 case KPrinter::A4: return "A4"; 00731 case KPrinter::A3: return "A3"; 00732 case KPrinter::Executive: return "Executive"; 00733 case KPrinter::Ledger: return "Ledger"; 00734 case KPrinter::Tabloid: return "Tabloid"; 00735 case KPrinter::Folio: return "Folio"; 00736 case KPrinter::A5: return "A5"; 00737 case KPrinter::A6: return "A6"; 00738 case KPrinter::A7: return "A7"; 00739 case KPrinter::A8: return "A8"; 00740 case KPrinter::A9: return "A9"; 00741 case KPrinter::A2: return "A2"; 00742 case KPrinter::A1: return "A1"; 00743 case KPrinter::A0: return "A0"; 00744 case KPrinter::B0: return "B0"; 00745 case KPrinter::B1: return "B1"; 00746 case KPrinter::B2: return "B2"; 00747 case KPrinter::B3: return "B3"; 00748 case KPrinter::B4: return "B4"; 00749 case KPrinter::B5: return "B5"; 00750 case KPrinter::B6: return "B6"; 00751 case KPrinter::B7: return "B7"; 00752 case KPrinter::B8: return "B8"; 00753 case KPrinter::B9: return "B9"; 00754 case KPrinter::B10: return "B10"; 00755 case KPrinter::C5E: return "C5"; 00756 case KPrinter::DLE: return "DL"; 00757 case KPrinter::Comm10E: return "Comm10"; 00758 default: return "A4"; 00759 } 00760 } 00761 00762 // FIXME: remove for 4.0 00763 QSize rangeToSize( const QString& ) 00764 { 00765 kdWarning( 500 ) << "rangeToSize(QString) is obsolete, do not use (no effect)" << endl; 00766 return QSize(); 00767 } 00768 00769 static void dumpOptions(const QMap<QString,QString>& opts) 00770 { 00771 kdDebug(500) << "********************" << endl; 00772 for (QMap<QString,QString>::ConstIterator it=opts.begin(); it!=opts.end(); ++it) 00773 kdDebug(500) << it.key() << " = " << it.data() << endl; 00774 } 00775 00776 KPrinterImpl* KPrinter::implementation() const 00777 { return d->m_impl; } 00778 00779 const QString& KPrinter::option(const QString& key) const 00780 { return ((const KPrinterPrivate*)(d))->m_options[key]; } 00781 00782 void KPrinter::setOption(const QString& key, const QString& value) 00783 { d->m_options[key] = value; } 00784 00785 QString KPrinter::docName() const 00786 { return option("kde-docname"); } 00787 00788 void KPrinter::setDocName(const QString& d) 00789 { setOption("kde-docname",d); } 00790 00791 QString KPrinter::creator() const 00792 { return option("kde-creator"); } 00793 00794 void KPrinter::setCreator(const QString& d) 00795 { setOption("kde-creator",d); } 00796 00797 bool KPrinter::fullPage() const 00798 { return (option("kde-fullpage") == "1"); } 00799 00800 void KPrinter::setFullPage(bool on) 00801 { setOption("kde-fullpage",(on ? "1" : "0")); } 00802 00803 KPrinter::ColorMode KPrinter::colorMode() const 00804 { return (KPrinter::ColorMode)(option("kde-colormode") == "GrayScale" ? GrayScale : Color); } 00805 00806 void KPrinter::setColorMode(ColorMode m) 00807 { setOption("kde-colormode",(m == Color ? "Color" : "GrayScale")); } 00808 00809 void KPrinter::setNumCopies(int n) 00810 { setOption("kde-copies",QString::number(n)); } 00811 00812 KPrinter::Orientation KPrinter::orientation() const 00813 { return (option("kde-orientation") == "Landscape" ? Landscape : Portrait); } 00814 00815 KPrinter::PageOrder KPrinter::pageOrder() const 00816 { return (option("kde-pageorder") == "Reverse" ? LastPageFirst : FirstPageFirst); } 00817 00818 void KPrinter::setPageOrder(PageOrder o) 00819 { setOption("kde-pageorder",(o == LastPageFirst ? "Reverse" : "Forward")); } 00820 00821 KPrinter::CollateType KPrinter::collate() const 00822 { return (option("kde-collate") == "Collate" ? Collate : Uncollate); } 00823 00824 void KPrinter::setCollate(CollateType c) 00825 { setOption("kde-collate",(c == Collate ? "Collate" : "Uncollate")); } 00826 00827 int KPrinter::minPage() const 00828 { return (option("kde-minpage").isEmpty() ? 0 : option("kde-minpage").toInt()); } 00829 00830 int KPrinter::maxPage() const 00831 { return (option("kde-maxpage").isEmpty() ? 0 : option("kde-maxpage").toInt()); } 00832 00833 void KPrinter::setMinMax(int m, int M) 00834 { setOption("kde-minpage",QString::number(m)); setOption("kde-maxpage",QString::number(M)); } 00835 00836 int KPrinter::fromPage() const 00837 { return (option("kde-frompage").isEmpty() ? 0 : option("kde-frompage").toInt()); } 00838 00839 int KPrinter::toPage() const 00840 { return (option("kde-topage").isEmpty() ? 0 : option("kde-topage").toInt()); } 00841 00842 void KPrinter::setFromTo(int m, int M) 00843 { setOption("kde-frompage",QString::number(m)); setOption("kde-topage",QString::number(M)); setOption("kde-range",(m>0 && M>0 ? QString("%1-%2").arg(m).arg(M) : QString::fromLatin1(""))); } 00844 00845 // if no page size defined, use the localized one 00846 KPrinter::PageSize KPrinter::pageSize() const 00847 { return (option("kde-pagesize").isEmpty() ? (PageSize)KGlobal::locale()->pageSize() : (PageSize)option("kde-pagesize").toInt()); } 00848 00849 KPrinter::PageSetType KPrinter::pageSet() const 00850 { return (option("kde-pageset").isEmpty() ? AllPages : (PageSetType)(option("kde-pageset").toInt())); } 00851 00852 int KPrinter::currentPage() const 00853 { return (option("kde-currentpage").isEmpty() ? 0 : option("kde-currentpage").toInt()); } 00854 00855 void KPrinter::setCurrentPage(int p) 00856 { setOption("kde-currentpage",QString::number(p)); } 00857 00858 QString KPrinter::printerName() const 00859 { return d->m_printername; } 00860 00861 void KPrinter::setPrinterName(const QString& s) 00862 { d->m_printername = s; } 00863 00864 QString KPrinter::printProgram() const 00865 { return (option("kde-isspecial") == "1" ? option("kde-special-command") : QString::null); } 00866 00867 void KPrinter::setPrintProgram(const QString& prg) 00868 { 00869 if (prg.isNull()) 00870 { 00871 setOption("kde-isspecial", "0"); 00872 d->m_options.remove("kde-special-command"); 00873 } 00874 else 00875 { 00876 QString s(prg); 00877 if (s.find("%in") == -1) 00878 s.append(" %in"); 00879 setOutputToFile( s.find( "%out" ) != -1 ); 00880 setOption("kde-isspecial", "1"); 00881 setOption("kde-special-command", s); 00882 } 00883 } 00884 00885 QString KPrinter::printerSelectionOption() const 00886 { return QString::fromLatin1(""); } 00887 00888 void KPrinter::setPrinterSelectionOption(const QString&) 00889 {} 00890 00891 const QMap<QString,QString>& KPrinter::options() const 00892 { return d->m_options; } 00893 00894 QString KPrinter::searchName() const 00895 { return d->m_searchname; } 00896 00897 void KPrinter::setSearchName(const QString& s) 00898 { d->m_searchname = s; } 00899 00900 bool KPrinter::newPage() 00901 { 00902 d->m_pagenumber++; 00903 d->m_impl->statusMessage(i18n("Generating print data: page %1").arg(d->m_pagenumber), this); 00904 return d->m_wrapper->newPage(); 00905 } 00906 00907 QString KPrinter::outputFileName() const 00908 { return option("kde-outputfilename"); } 00909 00910 void KPrinter::setOutputFileName(const QString& f) 00911 { setOption("kde-outputfilename",f); setOutputToFile(!f.isEmpty()); } 00912 00913 bool KPrinter::outputToFile() const 00914 { return (option("kde-outputtofile") == "1" || (option("kde-isspecial") == "1" && option("kde-special-command").isEmpty())); } 00915 00916 void KPrinter::setOutputToFile(bool on) 00917 { 00918 setOption("kde-outputtofile",(on ? "1" : "0")); 00919 if (on) 00920 { 00921 setOption("kde-special-command",QString::null); 00922 setOption("kde-isspecial","1"); 00923 } 00924 } 00925 00926 bool KPrinter::abort() 00927 { return d->m_wrapper->abort(); } 00928 00929 bool KPrinter::aborted() const 00930 { return d->m_wrapper->aborted(); } 00931 00932 void KPrinter::setMargins(QSize m) 00933 { 00934 setMargins( m.height(), m.width(), m.height(), m.width() ); 00935 } 00936 00937 void KPrinter::setMargins( uint top, uint left, uint bottom, uint right ) 00938 { 00939 d->m_wrapper->setMargins( top, left, bottom, right ); 00940 setOption( "kde-margin-top", QString::number( top ), true ); 00941 setOption( "kde-margin-left", QString::number( left ), true ); 00942 setOption( "kde-margin-bottom", QString::number( bottom ), true ); 00943 setOption( "kde-margin-right", QString::number( right ), true ); 00944 } 00945 00946 // FIXME: remove for 4.0 00947 QSize KPrinter::realPageSize() const 00948 { 00949 kdWarning( 500 ) << "KPrinter::realPageSize() is obsolete, do not use" << endl; 00950 if ( d->m_pagesize ) 00951 return d->m_pagesize->pageSize(); 00952 else 00953 return QSize(); 00954 } 00955 00956 void KPrinter::setRealPageSize(DrPageSize *p) 00957 { 00958 if ( p ) 00959 { 00960 kdDebug( 500 ) << "Page size: width =" << p->pageWidth() << endl; 00961 kdDebug( 500 ) << "Page size: height =" << p->pageHeight() << endl; 00962 kdDebug( 500 ) << "Page size: left =" << p->leftMargin() << endl; 00963 kdDebug( 500 ) << "Page size: top =" << p->topMargin() << endl; 00964 kdDebug( 500 ) << "Page size: right =" << p->rightMargin() << endl; 00965 kdDebug( 500 ) << "Page size: bottom =" << p->bottomMargin() << endl; 00966 } 00967 else 00968 kdDebug( 500 ) << "Resetting page size" << endl; 00969 00970 /* we copy the page size structure internally 00971 * as the original object is owned by the driver 00972 * that control its destrution */ 00973 delete d->m_pagesize; 00974 d->m_pagesize = 0; 00975 if ( p ) 00976 d->m_pagesize = new DrPageSize( *p ); 00977 } 00978 00979 // FIXME: remove for 4.0 00980 void KPrinter::setRealPageSize( QSize ) 00981 { 00982 kdWarning( 500 ) << "KPrinter::setRealPageSize(QSize) is obsolete, do not use (no effect)" << endl; 00983 } 00984 00985 // FIXME: remove for 4.0 00986 void KPrinter::setRealDrawableArea( const QRect& ) 00987 { 00988 kdWarning( 500 ) << "KPrinter::setRealDrawableArea(QRect) is obsolete, do not use (no effect)" << endl; 00989 } 00990 00991 // FIXME: remove for 4.0 00992 QRect KPrinter::realDrawableArea() const 00993 { 00994 kdWarning( 500 ) << "KPrinter::realDrawableArea() is obsolete, do not use" << endl; 00995 if ( d->m_pagesize ) 00996 return d->m_pagesize->pageRect(); 00997 else 00998 return QRect(); 00999 } 01000 01001 QString KPrinter::errorMessage() const 01002 { return d->m_errormsg; } 01003 01004 void KPrinter::setErrorMessage(const QString& msg) 01005 { d->m_errormsg = msg; } 01006 01007 /* we're using a builtin member to store this state because we don't 01008 * want to keep it from object to object. So there's no need to use 01009 * the QMap structure to store this 01010 */ 01011 void KPrinter::setPreviewOnly(bool on) 01012 { d->m_previewonly = on; } 01013 01014 bool KPrinter::previewOnly() const 01015 { return d->m_previewonly; } 01016 01017 void KPrinter::setDocFileName(const QString& s) 01018 { d->m_docfilename = s; } 01019 01020 QString KPrinter::docFileName() const 01021 { return d->m_docfilename; } 01022 01023 void KPrinter::setDocDirectory( const QString& s ) 01024 { d->m_docdirectory = s; } 01025 01026 QString KPrinter::docDirectory() const 01027 { return ( d->m_docdirectory.isEmpty() ? QDir::homeDirPath() : d->m_docdirectory ); } 01028 01029 void KPrinter::setResolution(int dpi) 01030 { 01031 d->m_wrapper->setResolution(dpi); 01032 d->m_defaultres = dpi; 01033 } 01034 01035 int KPrinter::resolution() const 01036 { return d->m_wrapper->resolution(); } 01037 01038 void KPrinter::setUsePrinterResolution( bool on ) 01039 { d->m_useprinterres = on; }
KDE Logo
This file is part of the documentation for kdeprint Library Version 3.2.3.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sun Oct 10 18:55:55 2004 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003