source: wkhtmltox/trunk/fuentes/src/shared/progressfeedback.cc @ 51

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

wip

File size: 3.1 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, 2011 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#include "progressfeedback.hh"
22#include <cstdio>
23namespace wkhtmltopdf {
24/*!
25  \file progressfeedback.hh
26  \brief Define the ProgressFeedback class
27*/
28
29/*!
30  \class ProgressFeedback
31  \brief Produce progress feedback on the terminal
32*/
33
34#define S(t) ((t).toLocal8Bit().constData())
35
36/*!
37  \brief Write out a warning message
38  \param message The warning message
39*/
40void ProgressFeedback::warning(const QString &message) {
41        if (quiet) return;
42        fprintf(stderr, "Warning: %s",S(message));
43        for (int l = 9 + message.size(); l < lw; ++l)
44                fprintf(stderr, " ");
45        fprintf(stderr, "\n");
46        lw = 0;
47}
48
49/*!
50  \brief Write out an error message
51  \param message The error message
52*/
53void ProgressFeedback::error(const QString &message) {
54        fprintf(stderr, "Error: %s",S(message));
55        for (int l = 7 + message.size(); l < lw; ++l)
56                fprintf(stderr, " ");
57        fprintf(stderr, "\n");
58        lw = 0;
59}
60
61/*!
62  \brief Write out the name of the next phase
63*/
64void ProgressFeedback::phaseChanged() {
65        if (quiet) return;
66        QString desc=converter.phaseDescription();
67        fprintf(stderr, "%s", S(desc));
68
69        int l = desc.size();
70        if (converter.currentPhase() < converter.phaseCount() -1)
71                l += fprintf(stderr," (%d/%d)",converter.currentPhase()+1,converter.phaseCount()-1);
72        for (; l < lw; ++l)
73                fprintf(stderr, " ");
74        fprintf(stderr, "\n");
75        lw = 0;
76}
77
78/*!
79  \brief Update progress bar
80*/
81void ProgressFeedback::progressChanged(int progress) {
82        if (quiet) return;
83        fprintf(stderr, "[");
84        int w=60;
85        progress *= w;
86        progress /= 100;
87        for (int i=0; i < w; ++i) {
88                if (i < progress) fprintf(stderr, "=");
89                else if (i == progress) fprintf(stderr, ">");
90                else fprintf(stderr, " ");
91        }
92        fprintf(stderr, "]");
93        fprintf(stderr, " %s", S(converter.progressString()));
94        int l=1+w+2+converter.progressString().size();
95        for (int i=l; i < lw; ++i) fprintf(stderr, " ");
96        lw = l;
97        fprintf(stderr, "\r");
98}
99
100ProgressFeedback::ProgressFeedback(bool q, Converter & _):
101    quiet(q), converter(_), lw(0) {
102    connect(&converter, SIGNAL(warning(const QString &)), this, SLOT(warning(const QString &)));
103        connect(&converter, SIGNAL(error(const QString &)), this, SLOT(error(const QString &)));
104        connect(&converter, SIGNAL(phaseChanged()), this, SLOT(phaseChanged()));
105        connect(&converter, SIGNAL(progressChanged(int)), this, SLOT(progressChanged(int)));
106}
107
108}
Note: See TracBrowser for help on using the repository browser.