Esempio n. 1
0
void PrintWrapper::slotPrint()
{
    KCmdLineArgs *args = KCmdLineArgs::parsedArgs();

#if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
    struct sigaction action;
#endif /* HAVE_SIGACTION && !HAVE_SIGSET*/

    // read variables from command line
    QString printer = args->getOption("d");
    QString title = args->getOption("t");
    int ncopies = QString(args->getOption("n")).toInt();
    QString job_mode = args->getOption("j");
    QString system = args->getOption("system");
    KStringList optlist = args->getOptionList("o");
    QMap< QString, QString > opts;
    KURL::List files;
    QStringList filestoprint;
    force_stdin = args->isSet("stdin");
    docopy = args->isSet("c");
    bool nodialog = !(args->isSet("dialog"));

    if(isatty(0))
    {
        kdDebug(500) << "stdin is a terminal, disabling it" << endl;
        check_stdin = false;
    }

    // parse options
    for(KStringList::ConstIterator it = optlist.begin(); it != optlist.end(); ++it)
    {
        QStringList l = QStringList::split('=', QString(*it), false);
        if(l.count() >= 1)
            opts[l[0]] = (l.count() == 2 ? l[1] : QString::null);
    }

    // read file list
    for(int i = 0; i < args->count(); i++)
        files.append(args->url(i));

    // some clean-up
    args->clear();

    // set default values if necessary
    if(job_mode == "console")
        job_output = 1;
    else if(job_mode == "none")
        job_output = 2;
    else
        job_output = 0;

    // some checking
    if(files.count() > 0)
    {
        check_stdin = false;

        if(force_stdin)
        {
            showmsg(i18n("A file has been specified on the command line. Printing from STDIN will be disabled."), 1);
            force_stdin = false;
        }
    }
    if(nodialog && files.count() == 0 && !force_stdin && !check_stdin)
    {
        errormsg(i18n("When using '--nodialog', you must at least specify one file to print or use the '--stdin' flag."));
    }

    if(check_stdin)
    { // check if there's any input on stdin
        fd_set in;
        struct timeval tm;
        tm.tv_sec = 0;
        tm.tv_usec = 0;
        FD_ZERO(&in);
        FD_SET(0, &in);
        if(select(1, &in, NULL, NULL, &tm))
        { // we have data on stdin
            if(read(0, &readchar, 1) > 0)
            {
                force_stdin = true;
                check_stdin = false;
                dataread = true;
                kdDebug(500) << "input detected on stdin" << endl;
            }
            else
            {
                force_stdin = check_stdin = false;
                kdDebug(500) << "stdin closed and empty" << endl;
            }
        }
        else
            kdDebug(500) << "no input on stdin at startup" << endl;
    }

    // force_stdin ? or also check_stdin ?
    KPrinter::ApplicationType dialog_mode = (force_stdin || nodialog ? KPrinter::StandAlone : KPrinter::StandAlonePersistent);
    KPrinter::setApplicationType(dialog_mode);
    if(!force_stdin)
        KPrinter::addStandardPage(KPrinter::FilesPage);

    KPrinter kprinter;
    if(nodialog)
    {
        KMPrinter *prt(0);
        KMManager *mgr = KMManager::self();

        mgr->printerList(false);
        if(!printer.isEmpty())
            prt = mgr->findPrinter(printer);
        else
            prt = mgr->defaultPrinter();

        if(prt == 0)
            errormsg(i18n("The specified printer or the default printer could not be found."));
        else if(!prt->autoConfigure(&kprinter))
            errormsg(i18n("Operation aborted."));
    }
    else if(!printer.isEmpty())
        kprinter.setSearchName(printer);
    kprinter.setDocName(title);
    kprinter.initOptions(opts);
    kprinter.setOption("kde-filelist", files.toStringList().join("@@"));
    kdDebug(500) << kprinter.option("kde-filelist") << endl;
    if(ncopies > 0)
        kprinter.setNumCopies(ncopies);

    if(nodialog)
        slotPrintRequested(&kprinter);
    else
    {
        dlg = KPrintDialog::printerDialog(&kprinter, 0);
        if(dlg)
        {
            connect(dlg, SIGNAL(printRequested(KPrinter *)), SLOT(slotPrintRequested(KPrinter *)));
            if(check_stdin)
            {
                notif = new QSocketNotifier(0, QSocketNotifier::Read, this);
                connect(notif, SIGNAL(activated(int)), this, SLOT(slotGotStdin()));
                kdDebug(500) << "waiting for input on stdin" << endl;
            }
            dlg->exec();
            delete dlg;
        }
        else
            errormsg(i18n("Unable to construct the print dialog."));
    }
Esempio n. 2
0
void TopLevel::printIt( KPrinter &printer, QPainter &painter )
{
  QPaintDeviceMetrics dm(painter.device());

  QApplication::setOverrideCursor( waitCursor );
  kapp->processEvents();

  const bool fullpage = printer.option(APP_KFAX_SCALE_FULLPAGE) == "true";
  const bool center_h = printer.option(APP_KFAX_CENTER_HORZ) == "true";
  const bool center_v = printer.option(APP_KFAX_CENTER_VERT) == "true";

  int currentpage = 0;
  bool first_page_printed = false;
  struct pagenode *pn;
  for(pn = firstpage; pn; pn = pn->next) {

    ++currentpage;
    // should this page be printed ?
    if (printer.pageList().findIndex(currentpage) < 0)
	continue;

    XImage *Image = Pimage(pn);
    if (!Image)
	continue;

    // byte-swapping the image
    QByteArray bytes( Image->height*Image->bytes_per_line );
    for (int y=Image->height-1; y>=0; --y) {
      Q_UINT32 offset  = y*Image->bytes_per_line;
      Q_UINT32 *source = (Q_UINT32 *) (Image->data + offset);
      Q_UINT32 *dest   = (Q_UINT32 *) (bytes.data() + offset);
      for (int x=(Image->bytes_per_line/4)-1; x>=0; --x) {
 	Q_UINT32 dv = 0, sv = *source;
	for (int bit=32; bit>0; --bit) {
		dv <<= 1;
		dv |= sv&1;
		sv >>= 1;
	}
        *dest = dv;
	++dest;
	++source;
      }
    }

    QImage image( (uchar *)bytes.data(), Image->bytes_per_line*8, Image->height, 1, NULL, 2, QImage::LittleEndian);

    if (first_page_printed)
	printer.newPage();
    first_page_printed = true;

    const QSize printersize( dm.width(), dm.height() );
    kdDebug() << "Printersize = " << printersize << endl;
    // print Image in original size if possible, else scale it.

    const QSize size(  // logical size of the image
			Image->width  * dm.logicalDpiX() / pn->dpiX,
			Image->height * dm.logicalDpiY() / pn->dpiY
		);

    kdDebug()	<< "Org image size = " << Image->width << "x" << Image->height 
		<< " logical picture res = " << pn->dpiX << "x" << pn->dpiY << endl;
    kdDebug()	<< "New image size = " << size 
		<< " logical printer res = " << dm.logicalDpiX() << "x" << dm.logicalDpiY() << endl;

    uint top, left, bottom, right;
    if (fullpage)
       top = left = bottom = right = 0;
    else
       printer.margins( &top, &left, &bottom, &right );
    kdDebug() << "Margins = " << top << " " << left << " " << bottom << " " << right << endl;

    const QSize maxSize( printersize.width()-left-right, printersize.height()-top-bottom );
    QSize scaledImageSize = size;
    if (size.width() > maxSize.width() || size.height() > maxSize.height() ) {
	// Image does not fit - scale it and print centered
	scaledImageSize.scale( maxSize, QSize::ScaleMin );
	kdDebug() << "Image does not fit - scaling to " << maxSize << endl;
    } else {
	// Image does fit - print it in original size, but centered
	scaledImageSize.scale( size, QSize::ScaleMin );
	kdDebug() << "Image does fit - scaling to " << size << endl;
    }
    kdDebug() << "Final image size " << scaledImageSize << endl;
    int x,y;
    if (center_h)
	x = (maxSize.width()-scaledImageSize.width())/2 + left;
    else
	x = left;
    if (center_v)
	y = (maxSize.height()-scaledImageSize.height())/2 + top;
    else
	y = top;
    painter.drawImage( QRect(x,y,scaledImageSize.width(), scaledImageSize.height()), image );

  }

  QApplication::restoreOverrideCursor();
}