ippreportdlg.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., 51 Franklin Street, Fifth Floor,
00017  *  Boston, MA 02110-1301, USA.
00018  **/
00019 
00020 #include "ippreportdlg.h"
00021 #include "ipprequest.h"
00022 #include "kprinter.h"
00023 
00024 #include <klocale.h>
00025 #include <kguiitem.h>
00026 #include <kmessagebox.h>
00027 #include <kdebug.h>
00028 #include <ktextedit.h>
00029 #include <qsimplerichtext.h>
00030 #include <qpainter.h>
00031 #include <qpaintdevicemetrics.h>
00032 
00033 IppReportDlg::IppReportDlg(QWidget *parent, const char *name)
00034 : KDialogBase(parent, name, true, i18n("IPP Report"), Close|User1, Close, false, KGuiItem(i18n("&Print"), "fileprint"))
00035 {
00036     m_edit = new KTextEdit(this);
00037     m_edit->setReadOnly(true);
00038     setMainWidget(m_edit);
00039     resize(540, 500);
00040     setFocusProxy(m_edit);
00041     setButtonGuiItem(User1, KGuiItem(i18n("&Print"),"fileprint"));
00042 }
00043 
00044 void IppReportDlg::slotUser1()
00045 {
00046     KPrinter    printer;
00047     printer.setFullPage(true);
00048     printer.setDocName(caption());
00049     if (printer.setup(this))
00050     {
00051         QPainter    painter(&printer);
00052         QPaintDeviceMetrics metrics(&printer);
00053 
00054         // report is printed using QSimpleRichText
00055         QSimpleRichText rich(m_edit->text(), font());
00056         rich.setWidth(&painter, metrics.width());
00057         int margin = (int)(1.5 / 2.54 * metrics.logicalDpiY()); // 1.5 cm
00058         QRect   r(margin, margin, metrics.width()-2*margin, metrics.height()-2*margin);
00059         int hh = rich.height(), page(1);
00060         while (1)
00061         {
00062             rich.draw(&painter, margin, margin, r, colorGroup());
00063             QString s = caption() + ": " + QString::number(page);
00064             QRect   br = painter.fontMetrics().boundingRect(s);
00065             painter.drawText(r.right()-br.width()-5, r.top()-br.height()-4, br.width()+5, br.height()+4, Qt::AlignRight|Qt::AlignTop, s);
00066             r.moveBy(0, r.height()-10);
00067             painter.translate(0, -(r.height()-10));
00068             if (r.top() < hh)
00069             {
00070                 printer.newPage();
00071                 page++;
00072             }
00073             else
00074                 break;
00075         }
00076     }
00077 }
00078 
00079 void IppReportDlg::report(IppRequest *req, int group, const QString& caption)
00080 {
00081     QString str_report;
00082     QTextStream t(&str_report, IO_WriteOnly);
00083 
00084     if (req->htmlReport(group, t))
00085     {
00086         IppReportDlg    dlg;
00087         if (!caption.isEmpty())
00088             dlg.setCaption(caption);
00089         dlg.m_edit->setText(str_report);
00090         dlg.exec();
00091     }
00092     else
00093         KMessageBox::error(0, i18n("Internal error: unable to generate HTML report."));
00094 }
00095 
00096 #include "ippreportdlg.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys