source: wkhtmltox/trunk/fuentes/src/lib/utilities.cc @ 51

Last change on this file since 51 was 51, checked in by mabarracus, 4 years ago

wip

File size: 7.4 KB
Line 
1// -*- mode: c++; tab-width: 4; indent-tabs-mode: t; eval: (progn (c-set-style "stroustrup") (c-set-offset 'innamespace 0)); -*-
2// vi:set ts=4 sts=4 sw=4 noet :
3//
4// Copyright 2010 wkhtmltopdf authors
5//
6// This file is part of wkhtmltopdf.
7//
8// wkhtmltopdf is free software: you can redistribute it and/or modify
9// it under the terms of the GNU Lesser General Public License as published by
10// the Free Software Foundation, either version 3 of the License, or
11// (at your option) any later version.
12//
13// wkhtmltopdf is distributed in the hope that it will be useful,
14// but WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16// GNU General Public License for more details.
17//
18// You should have received a copy of the GNU Lesser General Public License
19// along with wkhtmltopdf.  If not, see <http://www.gnu.org/licenses/>.
20
21
22#include "utilities.hh"
23#include <QDebug>
24#include <QTextStream>
25#include <QMetaEnum>
26#include <QNetworkReply>
27
28void loadSvg(QSvgRenderer * & ptr, const QString & path, const char * def, int w, int h) {
29         delete ptr;
30         ptr = 0;
31         if (path != "") {
32                ptr = new QSvgRenderer(path);
33                if (ptr->isValid()) return;
34                qWarning() << "Failed to load " << path;
35                delete ptr;
36         }
37
38         QByteArray a;
39         QTextStream s(&a, QIODevice::WriteOnly );
40         s << "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n"
41           << "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n"
42           << "<svg xmlns=\"http://www.w3.org/2000/svg\"\n"
43           << "  xmlns:xlink=\"http://www.w3.org/1999/xlink\"\n"
44           << "  version=\"1.1\"\n"
45           << "  baseProfile=\"full\"\n"
46           << "  width=\"" << w << "px\"\n"
47           << "  height=\"" << h << "px\"\n"
48           << "  viewBox=\"0 0 " << w << "px " << h << "px\">\n"
49           << def
50           << "</svg>\n";
51         s.flush();
52         ptr = new QSvgRenderer(a);
53
54         if (ptr->isValid()) return;
55         delete ptr;
56         ptr = 0;
57}
58
59#define RB \
60"<path d=\"M0,0 h-1 a1,1 0 0,0 2,0 z\" fill=\"#808080\" stroke=\"\" stroke-width=\"0\" transform=\"translate(5.5,5.5)rotate(-225)scale(5.5,5.5)\"/>\n" \
61"<path d=\"M0,0 h-1 a1,1 0 0,0 2,0 z\" fill=\"#ffffff\" stroke=\"\" stroke-width=\"0\" transform=\"translate(5.5,5.5)rotate(-45)scale(5.5,5.5)\"/>\n" \
62"<path d=\"M0,0 h-1 a1,1 0 0,0 2,0 z\" fill=\"#404040\" stroke=\"\" stroke-width=\"0\" transform=\"translate(5.5,5.5)rotate(-225)scale(4.5,4.5)\"/>\n" \
63 "<path d=\"M0,0 h-1 a1,1 0 0,0 2,0 z\" fill=\"#d4d0c8\" stroke=\"\" stroke-width=\"0\" transform=\"translate(5.5,5.5)rotate(-45)scale(4.5,4.5)\"/>\n" \
64"<circle id=\"c1\" cx=\"5.5\" cy=\"5.5\" r=\"3.5\" fill=\"white\" stroke=\"\" stroke-width=\"0\"/>\n"
65
66#define CB \
67"<rect x=\"0\" y=\"0\" width=\"1\" height=\"12\" fill=\"#808080\" stroke=\"\" stroke-width=\"0\" />\n" \
68"<rect x=\"1\" y=\"0\" width=\"11\" height=\"1\" fill=\"#808080\" stroke=\"\" stroke-width=\"0\" />\n" \
69"<rect x=\"1\" y=\"11\" width=\"11\" height=\"1\" fill=\"#d4d0c8\" stroke=\"\" stroke-width=\"0\" />\n" \
70"<rect x=\"11\" y=\"1\" width=\"1\" height=\"11\" fill=\"#d4d0c8\" stroke=\"\" stroke-width=\"0\" />\n" \
71"<rect x=\"1\" y=\"1\" width=\"1\" height=\"10\" fill=\"#404040\" stroke=\"\" stroke-width=\"0\" />\n" \
72"<rect x=\"2\" y=\"1\" width=\"9\" height=\"1\" fill=\"#404040\" stroke=\"\" stroke-width=\"0\" />\n" \
73"<rect x=\"2\" y=\"10\" width=\"9\" height=\"1\" fill=\"#ffffff\" stroke=\"\" stroke-width=\"0\" />\n" \
74"<rect x=\"10\" y=\"2\" width=\"1\" height=\"9\" fill=\"#ffffff\" stroke=\"\" stroke-width=\"0\" />\n" \
75"<rect x=\"2\" y=\"2\" width=\"8\" height=\"8\" fill=\"#ffffff\" stroke=\"\" stroke-width=\"0\" />\n"
76
77void MyLooksStyle::setCheckboxSvg(const QString & path) {
78        loadSvg(checkbox, path,
79                        CB, 12, 12);
80}
81void MyLooksStyle::setCheckboxCheckedSvg(const QString & path) {
82        loadSvg(checkbox_checked, path,
83                        CB
84                        "<path d=\"M 3 5.5 L 3 8 L 5.5 10.5 L 10 5.5 L 10 2.5 L 5.5 7.5\" fill=\"black\" />\n",
85                        12, 12);
86}
87void MyLooksStyle::setRadioButtonSvg(const QString & path) {
88        loadSvg(radiobutton, path, RB, 11, 11);
89}
90void MyLooksStyle::setRadioButtonCheckedSvg(const QString & path) {
91        loadSvg(radiobutton_checked, path,
92                        RB
93                        "<circle id=\"c2\" cx=\"5.5\" cy=\"5.5\" r=\"1.5\" fill=\"black\" stroke=\"\" stroke-width=\"0\"/>\n", 11, 11);
94}
95
96MyLooksStyle::MyLooksStyle(): weAreDrawingForms(false) {
97}
98
99void MyLooksStyle::producingForms(bool f) {weAreDrawingForms=f;}
100
101void MyLooksStyle::drawPrimitive( PrimitiveElement element, const QStyleOption * option, QPainter * painter, const QWidget * widget) const {
102        painter->setBrush(Qt::white);
103        painter->setPen(QPen(Qt::black, 0.7));
104        painter->setBackground(Qt::NoBrush);
105        painter->setBackgroundMode(Qt::TransparentMode);
106        QRect r = option->rect;
107        if (element == QStyle::PE_PanelLineEdit) {
108                painter->drawRect(r);
109        } else if (element == QStyle::PE_IndicatorCheckBox) {
110                if (weAreDrawingForms || ((option->state & QStyle::State_On)? !checkbox_checked: !checkbox)) {
111                        painter->drawRect(r);
112                        if (!weAreDrawingForms && (option->state & QStyle::State_On)) {
113                                r.translate(int(r.width()*0.075), int(r.width()*0.075));
114                                painter->drawLine(r.topLeft(), r.bottomRight());
115                                painter->drawLine(r.topRight(), r.bottomLeft());
116                        }
117                } else if (option->state & QStyle::State_On)
118                        checkbox_checked->render(painter, r);
119                else
120                        checkbox->render(painter, r);
121        } else if (element == QStyle::PE_IndicatorRadioButton) {
122                if (weAreDrawingForms || ((option->state & QStyle::State_On)? !radiobutton_checked: !radiobutton)) {
123                        painter->drawEllipse(r);
124                        if (!weAreDrawingForms && (option->state & QStyle::State_On)) {
125                                r.translate(int(r.width()*0.20), int(r.width()*0.20));
126                                r.setWidth(int(r.width()*0.70));
127                                r.setHeight(int(r.height()*0.70));
128                                painter->setBrush(Qt::black);
129                                painter->drawEllipse(r);
130                        }
131                } else if (option->state & QStyle::State_On)
132                        radiobutton_checked->render(painter, r);
133                else
134                        radiobutton->render(painter, r);
135        } else {
136                parent_t::drawPrimitive(element, option, painter, widget);
137        }
138}
139
140int handleError(bool success, int errorCode) {
141        QHash<int, const char *> cm;
142        cm[400] = "Bad Request";
143        cm[401] = "Unauthorized";
144        cm[402] = "Payment Required";
145        cm[403] = "Forbidden";
146        cm[404] = "Page not found";
147        cm[405] = "Method Not Allowed";
148        cm[500] = "Internal Server Error";
149        cm[501] = "Not Implemented";
150        cm[503] = "Service Unavailable";
151        cm[505] = "HTTP Version Not Supported";
152        QHash<int, int> ce;
153        ce[404] = 2;
154        ce[401] = 3;
155        if (errorCode) {
156                int c = EXIT_FAILURE;
157                if (ce.contains(errorCode)) c = ce[errorCode];
158                const char * m = "";
159                if (cm.contains(errorCode)) m = cm[errorCode];
160                if (errorCode < 1000) {
161                        fprintf(stderr, "Exit with code %d due to http error: %d %s\n", c, errorCode, m);
162                } else {
163                        QNetworkReply::NetworkError error = (QNetworkReply::NetworkError)(errorCode - 1000);
164                        QString errorValue;
165                        QMetaObject meta = QNetworkReply::staticMetaObject;
166                        for (int i=0; i < meta.enumeratorCount(); ++i) {
167                                QMetaEnum m = meta.enumerator(i);
168                                if (m.name() == QLatin1String("NetworkError")) {
169                                        errorValue = QLatin1String(m.valueToKey(error));
170                                        break;
171                                }
172                        }
173                        fprintf(stderr, "Exit with code %d due to network error: %s\n", c, errorValue.toLocal8Bit().data());
174                }
175                return c;
176        } else if (!success) {
177                fprintf(stderr, "Exit with code %d, due to unknown error.\n", EXIT_FAILURE);
178        }
179        return success?EXIT_SUCCESS:EXIT_FAILURE;
180}
181
182QSvgRenderer * MyLooksStyle::checkbox = 0;
183QSvgRenderer * MyLooksStyle::checkbox_checked = 0;
184QSvgRenderer * MyLooksStyle::radiobutton = 0;
185QSvgRenderer * MyLooksStyle::radiobutton_checked = 0;
Note: See TracBrowser for help on using the repository browser.