// -*- mode: c++; tab-width: 4; indent-tabs-mode: t; eval: (progn (c-set-style "stroustrup") (c-set-offset 'innamespace 0)); -*-
// vi:set ts=4 sts=4 sw=4 noet :
//
// Copyright 2010 wkhtmltopdf authors
//
// This file is part of wkhtmltopdf.
//
// wkhtmltopdf is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// wkhtmltopdf is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with wkhtmltopdf. If not, see .
#include "utilities.hh"
#include
#include
#include
#include
void loadSvg(QSvgRenderer * & ptr, const QString & path, const char * def, int w, int h) {
delete ptr;
ptr = 0;
if (path != "") {
ptr = new QSvgRenderer(path);
if (ptr->isValid()) return;
qWarning() << "Failed to load " << path;
delete ptr;
}
QByteArray a;
QTextStream s(&a, QIODevice::WriteOnly );
s << "\n"
<< "\n"
<< "\n";
s.flush();
ptr = new QSvgRenderer(a);
if (ptr->isValid()) return;
delete ptr;
ptr = 0;
}
#define RB \
"\n" \
"\n" \
"\n" \
"\n" \
"\n"
#define CB \
"\n" \
"\n" \
"\n" \
"\n" \
"\n" \
"\n" \
"\n" \
"\n" \
"\n"
void MyLooksStyle::setCheckboxSvg(const QString & path) {
loadSvg(checkbox, path,
CB, 12, 12);
}
void MyLooksStyle::setCheckboxCheckedSvg(const QString & path) {
loadSvg(checkbox_checked, path,
CB
"\n",
12, 12);
}
void MyLooksStyle::setRadioButtonSvg(const QString & path) {
loadSvg(radiobutton, path, RB, 11, 11);
}
void MyLooksStyle::setRadioButtonCheckedSvg(const QString & path) {
loadSvg(radiobutton_checked, path,
RB
"\n", 11, 11);
}
MyLooksStyle::MyLooksStyle(): weAreDrawingForms(false) {
}
void MyLooksStyle::producingForms(bool f) {weAreDrawingForms=f;}
void MyLooksStyle::drawPrimitive( PrimitiveElement element, const QStyleOption * option, QPainter * painter, const QWidget * widget) const {
painter->setBrush(Qt::white);
painter->setPen(QPen(Qt::black, 0.7));
painter->setBackground(Qt::NoBrush);
painter->setBackgroundMode(Qt::TransparentMode);
QRect r = option->rect;
if (element == QStyle::PE_PanelLineEdit) {
painter->drawRect(r);
} else if (element == QStyle::PE_IndicatorCheckBox) {
if (weAreDrawingForms || ((option->state & QStyle::State_On)? !checkbox_checked: !checkbox)) {
painter->drawRect(r);
if (!weAreDrawingForms && (option->state & QStyle::State_On)) {
r.translate(int(r.width()*0.075), int(r.width()*0.075));
painter->drawLine(r.topLeft(), r.bottomRight());
painter->drawLine(r.topRight(), r.bottomLeft());
}
} else if (option->state & QStyle::State_On)
checkbox_checked->render(painter, r);
else
checkbox->render(painter, r);
} else if (element == QStyle::PE_IndicatorRadioButton) {
if (weAreDrawingForms || ((option->state & QStyle::State_On)? !radiobutton_checked: !radiobutton)) {
painter->drawEllipse(r);
if (!weAreDrawingForms && (option->state & QStyle::State_On)) {
r.translate(int(r.width()*0.20), int(r.width()*0.20));
r.setWidth(int(r.width()*0.70));
r.setHeight(int(r.height()*0.70));
painter->setBrush(Qt::black);
painter->drawEllipse(r);
}
} else if (option->state & QStyle::State_On)
radiobutton_checked->render(painter, r);
else
radiobutton->render(painter, r);
} else {
parent_t::drawPrimitive(element, option, painter, widget);
}
}
int handleError(bool success, int errorCode) {
QHash cm;
cm[400] = "Bad Request";
cm[401] = "Unauthorized";
cm[402] = "Payment Required";
cm[403] = "Forbidden";
cm[404] = "Page not found";
cm[405] = "Method Not Allowed";
cm[500] = "Internal Server Error";
cm[501] = "Not Implemented";
cm[503] = "Service Unavailable";
cm[505] = "HTTP Version Not Supported";
QHash ce;
ce[404] = 2;
ce[401] = 3;
if (errorCode) {
int c = EXIT_FAILURE;
if (ce.contains(errorCode)) c = ce[errorCode];
const char * m = "";
if (cm.contains(errorCode)) m = cm[errorCode];
if (errorCode < 1000) {
fprintf(stderr, "Exit with code %d due to http error: %d %s\n", c, errorCode, m);
} else {
QNetworkReply::NetworkError error = (QNetworkReply::NetworkError)(errorCode - 1000);
QString errorValue;
QMetaObject meta = QNetworkReply::staticMetaObject;
for (int i=0; i < meta.enumeratorCount(); ++i) {
QMetaEnum m = meta.enumerator(i);
if (m.name() == QLatin1String("NetworkError")) {
errorValue = QLatin1String(m.valueToKey(error));
break;
}
}
fprintf(stderr, "Exit with code %d due to network error: %s\n", c, errorValue.toLocal8Bit().data());
}
return c;
} else if (!success) {
fprintf(stderr, "Exit with code %d, due to unknown error.\n", EXIT_FAILURE);
}
return success?EXIT_SUCCESS:EXIT_FAILURE;
}
QSvgRenderer * MyLooksStyle::checkbox = 0;
QSvgRenderer * MyLooksStyle::checkbox_checked = 0;
QSvgRenderer * MyLooksStyle::radiobutton = 0;
QSvgRenderer * MyLooksStyle::radiobutton_checked = 0;