File indexing completed on 2024-05-05 04:43:23

0001 /* This file is part of the KDE project
0002  * Copyright (C) 2001-2007 by OpenMFG, LLC (info@openmfg.com)
0003  * Copyright (C) 2007-2008 by Adam Pigg (adam@piggz.co.uk)
0004  *
0005  * This library is free software; you can redistribute it and/or
0006  * modify it under the terms of the GNU Lesser General Public
0007  * License as published by the Free Software Foundation; either
0008  * version 2.1 of the License, or (at your option) any later version.
0009  *
0010  * This library is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0013  * Lesser General Public License for more details.
0014  *
0015  * You should have received a copy of the GNU Lesser General Public
0016  * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
0017  */
0018 
0019 #include "KReportScreenRenderer_p.h"
0020 #include "KReportRenderObjects.h"
0021 #include "KReportUnit.h"
0022 #include "kreport_debug.h"
0023 
0024 namespace KReportPrivate {
0025 
0026 ScreenRenderer::ScreenRenderer()
0027 {
0028 
0029 }
0030 
0031 ScreenRenderer::~ScreenRenderer()
0032 {
0033 }
0034 
0035 bool ScreenRenderer::render(const KReportRendererContext& context, ORODocument *document, int page)
0036 {
0037     if (!document)
0038         return false;
0039 
0040     if (!context.painter())
0041         return false;
0042 
0043     OROPage *p = document->page(page);
0044 
0045     if (!p) {
0046         return false;
0047     }
0048 
0049     // Render Page Objects
0050     for (int i = 0; i < p->primitiveCount(); i++) {
0051         OROPrimitive *prim = p->primitive(i);
0052 
0053         if (OROTextBox *tb = dynamic_cast<OROTextBox*>(prim)) {
0054             QPointF ps = tb->position();
0055             QSizeF sz = tb->size();
0056             QRectF rc = QRectF(ps.x(), ps.y(), sz.width(), sz.height());
0057 
0058             context.painter()->save();
0059             //Background
0060 
0061             context.painter()->setBackgroundMode(Qt::TransparentMode);
0062 
0063             QColor bg = tb->textStyle().backgroundColor;
0064             bg.setAlphaF(0.01 * tb->textStyle().backgroundOpacity);
0065 
0066             context.painter()->fillRect(rc, bg);
0067 
0068             //Text
0069             context.painter()->setFont(tb->textStyle().font);
0070             context.painter()->setPen(tb->textStyle().foregroundColor);
0071             context.painter()->drawText(rc.adjusted(2, 2, 0, 0), tb->flags(), tb->text());
0072 
0073             //outer line
0074             context.painter()->setPen(QPen(tb->lineStyle().color(), tb->lineStyle().weight(), tb->lineStyle().penStyle()));
0075             context.painter()->drawRect(rc);
0076 
0077             //Reset back to defaults for next element
0078             context.painter()->restore();
0079         }
0080         else if (OROLine *ln = dynamic_cast<OROLine*>(prim)) {
0081             QPointF s = ln->startPoint();
0082             QPointF e = ln->endPoint();
0083             //QPen pen ( _painter->pen() );
0084             QPen pen(ln->lineStyle().color(), ln->lineStyle().weight(), ln->lineStyle().penStyle());
0085 
0086             context.painter()->save();
0087             context.painter()->setRenderHint(QPainter::Antialiasing, true);
0088             context.painter()->setPen(pen);
0089             context.painter()->drawLine(QLineF(s.x(), s.y(), e.x(), e.y()));
0090             context.painter()->setRenderHint(QPainter::Antialiasing, false);
0091             context.painter()->restore();
0092         }
0093         else if (ORORect *re = dynamic_cast<ORORect*>(prim)) {
0094             QPointF ps = re->position();
0095             QSizeF sz = re->size();
0096             QRectF rc = QRectF(ps.x(), ps.y(), sz.width(), sz.height());
0097 
0098             context.painter()->save();
0099             context.painter()->setPen(re->pen());
0100             context.painter()->setBrush(re->brush());
0101             context.painter()->drawRect(rc);
0102             context.painter()->restore();
0103         }
0104         else if (OROEllipse *re = dynamic_cast<OROEllipse*>(prim)) {
0105             QPointF ps = re->position();
0106             QSizeF sz = re->size();
0107             QRectF rc = QRectF(ps.x(), ps.y(), sz.width(), sz.height());
0108 
0109             context.painter()->save();
0110             context.painter()->setPen(re->pen());
0111             context.painter()->setBrush(re->brush());
0112             context.painter()->drawEllipse(rc);
0113             context.painter()->restore();
0114         }
0115         else if ( OROImage *im = dynamic_cast<OROImage*>(prim)) {
0116             QPointF ps = im->position();
0117             QSizeF sz = im->size();
0118             QRectF rc = QRectF(ps.x(), ps.y(), sz.width(), sz.height());
0119 
0120             QImage img = im->image();
0121             if (im->isScaled())
0122                 img = img.scaled(rc.size().toSize(), (Qt::AspectRatioMode) im->aspectRatioMode(),
0123                                  (Qt::TransformationMode) im->transformationMode());
0124 
0125             QRectF sr = QRectF(QPointF(0.0, 0.0), rc.size().boundedTo(img.size()));
0126             context.painter()->drawImage(rc.topLeft(), img, sr);
0127         }
0128         else if (OROPicture *im = dynamic_cast<OROPicture*>(prim)) {
0129             QPointF ps = im->position();
0130             QSizeF sz = im->size();
0131             QRectF rc = QRectF(ps.x(), ps.y(), sz.width(), sz.height());
0132             context.painter()->save();
0133             context.painter()->drawPicture(rc.topLeft(), *(im->picture()));
0134             context.painter()->restore();
0135         }
0136         else if (OROCheckBox *chk = dynamic_cast<OROCheckBox*>(prim)) {
0137             QPointF ps = chk->position();
0138             QSizeF sz = chk->size();
0139             QRectF rc = QRectF(ps.x(), ps.y(), sz.width(), sz.height());
0140 
0141             context.painter()->save();
0142 
0143             context.painter()->setBackgroundMode(Qt::OpaqueMode);
0144             context.painter()->setRenderHint(QPainter::Antialiasing);
0145 
0146             context.painter()->setPen(chk->foregroundColor());
0147 
0148             if (chk->lineStyle().penStyle() == Qt::NoPen || chk->lineStyle().weight() <= 0) {
0149                 context.painter()->setPen(QPen(Qt::lightGray));
0150             } else {
0151                 context.painter()->setPen(QPen(chk->lineStyle().color(), chk->lineStyle().weight(), chk->lineStyle().penStyle()));
0152             }
0153 
0154             qreal ox = sz.width() / 5;
0155             qreal oy = sz.height() / 5;
0156 
0157             //Checkbox Style
0158             if (chk->checkType() == OROCheckBox::Type::Cross) {
0159                 context.painter()->drawRoundedRect(rc, sz.width() / 10 , sz.height() / 10);
0160 
0161                 if (chk->value()) {
0162                     QPen lp;
0163                     lp.setColor(chk->foregroundColor());
0164                     lp.setWidth(ox > oy ? oy : ox);
0165                     context.painter()->setPen(lp);
0166                     context.painter()->drawLine(QPointF(ox, oy) + ps, QPointF(sz.width() - ox, sz.height() - oy) + ps);
0167                     context.painter()->drawLine(QPointF(ox, sz.height() - oy) + ps, QPoint(sz.width() - ox, oy) + ps);
0168                 }
0169             }
0170             else if (chk->checkType() == OROCheckBox::Type::Dot) {
0171                 //Radio Style
0172                 context.painter()->drawEllipse(rc);
0173 
0174                 if (chk->value()) {
0175                     QBrush lb(chk->foregroundColor());
0176                     context.painter()->setBrush(lb);
0177                     context.painter()->setPen(Qt::NoPen);
0178                     context.painter()->drawEllipse(rc.center(), sz.width() / 2 - ox, sz.height() / 2 - oy);
0179                 }
0180             }
0181             else {
0182                 //Tickbox Style
0183                 context.painter()->drawRoundedRect(rc, sz.width() / 10 , sz.height() / 10);
0184 
0185                 if (chk->value()) {
0186                     QPen lp;
0187                     lp.setColor(chk->foregroundColor());
0188                     lp.setWidth(ox > oy ? oy : ox);
0189                     context.painter()->setPen(lp);
0190                     context.painter()->drawLine(
0191                         QPointF(ox, sz.height() / 2) + ps,
0192                         QPointF(sz.width() / 2, sz.height() - oy) + ps);
0193                     context.painter()->drawLine(
0194                         QPointF(sz.width() / 2, sz.height() - oy) + ps,
0195                         QPointF(sz.width() - ox, oy) + ps);
0196                 }
0197             }
0198 
0199             context.painter()->restore();
0200         }
0201         else {
0202             kreportWarning() << "unrecognized primitive type";
0203         }
0204     }
0205 
0206     return true;
0207 }
0208 
0209 }