~ubuntu-branches/ubuntu/vivid/kdesdk/vivid

« back to all changes in this revision

Viewing changes to thumbnailers/po/pocreator.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2012-06-06 11:49:54 UTC
  • mfrom: (0.4.21)
  • Revision ID: package-import@ubuntu.com-20120606114954-rdls73fzlpzxglbx
Tags: 4:4.8.80-0ubuntu1
* New uptream beta release
* Update dont_export_private_classes.diff

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  This file is part of kde-thumbnailer-po
 
3
 *  Copyright (C) 2011-2012 Ni Hui <shuizhuyuanluo@126.com>
 
4
 *
 
5
 *  This program is free software; you can redistribute it and/or
 
6
 *  modify it under the terms of the GNU General Public License as
 
7
 *  published by the Free Software Foundation; either version 2 of
 
8
 *  the License or (at your option) version 3 or any later version
 
9
 *  accepted by the membership of KDE e.V. (or its successor approved
 
10
 *  by the membership of KDE e.V.), which shall act as a proxy
 
11
 *  defined in Section 14 of version 3 of the license.
 
12
 *
 
13
 *  This program 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 General Public License
 
19
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
20
 */
 
21
 
 
22
#include "pocreator.h"
 
23
 
 
24
#include <gettext-po.h>
 
25
#include <QColor>
 
26
#include <QImage>
 
27
#include <QLatin1String>
 
28
#include <QPainter>
 
29
#include <QWidget>
 
30
#include <kdemacros.h>
 
31
#include <KLocale>
 
32
 
 
33
#include "pocreatorsettings.h"
 
34
#include "ui_pocreatorform.h"
 
35
 
 
36
extern "C" {
 
37
    KDE_EXPORT ThumbCreator* new_creator() {
 
38
        KGlobal::locale()->insertCatalog( QLatin1String( "pothumbnail" ) );
 
39
        return new PoCreator;
 
40
    }
 
41
}
 
42
 
 
43
static bool readerror = false;
 
44
 
 
45
static void xerror( int severity,
 
46
                    po_message_t /*message*/,
 
47
                    const char */*filename*/, size_t /*lineno*/, size_t /*column*/,
 
48
                    int /*multiline_p*/, const char */*message_text*/ )
 
49
{
 
50
    if ( severity == PO_SEVERITY_ERROR || severity == PO_SEVERITY_FATAL_ERROR )
 
51
        readerror = true;
 
52
}
 
53
 
 
54
static void xerror2( int severity,
 
55
                     po_message_t /*message1*/,
 
56
                     const char */*filename1*/, size_t /*lineno1*/, size_t /*column1*/,
 
57
                     int /*multiline_p1*/, const char */*message_text1*/,
 
58
                     po_message_t /*message2*/,
 
59
                     const char */*filename2*/, size_t /*lineno2*/, size_t /*column2*/,
 
60
                     int /*multiline_p2*/, const char */*message_text2*/ )
 
61
{
 
62
    if ( severity == PO_SEVERITY_ERROR || severity == PO_SEVERITY_FATAL_ERROR )
 
63
        readerror = true;
 
64
}
 
65
 
 
66
static bool get_po_info( const char* filepath, int& translate, int& untranslate, int& fuzzy, int& obsolete )
 
67
{
 
68
    po_file_t pofile;
 
69
    const struct po_xerror_handler handler = { xerror, xerror2 };
 
70
 
 
71
    pofile = po_file_read( filepath, &handler );
 
72
    if ( pofile == NULL || readerror )
 
73
        return false;
 
74
 
 
75
    po_message_iterator_t it;
 
76
    it = po_message_iterator( pofile, NULL );
 
77
    po_message_t msg;
 
78
    const char* msgstr;
 
79
    while ( ( msg = po_next_message( it ) ) != NULL ) {
 
80
        if ( po_message_is_obsolete( msg ) )
 
81
            ++obsolete;
 
82
        else if ( po_message_is_fuzzy( msg ) )
 
83
            ++fuzzy;
 
84
        else {
 
85
            msgstr = po_message_msgstr( msg );
 
86
            if ( msgstr[0] == '\0' )
 
87
                ++untranslate;
 
88
            else
 
89
                ++translate;
 
90
        }
 
91
    }
 
92
    po_message_iterator_free( it );
 
93
 
 
94
    /// do not count domain header as translated message
 
95
    const char* header = po_file_domain_header( pofile, NULL );
 
96
    if ( header != NULL )
 
97
        --translate;
 
98
 
 
99
    po_file_free( pofile );
 
100
 
 
101
    return true;
 
102
}
 
103
 
 
104
PoCreator::PoCreator()
 
105
{
 
106
}
 
107
 
 
108
PoCreator::~PoCreator()
 
109
{
 
110
}
 
111
 
 
112
bool PoCreator::create( const QString& path, int width, int height, QImage& img )
 
113
{
 
114
    int translate = 0;
 
115
    int untranslate = 0;
 
116
    int fuzzy = 0;
 
117
    int obsolete = 0;
 
118
 
 
119
    if ( !get_po_info( path.toLocal8Bit(), translate, untranslate, fuzzy, obsolete ) )
 
120
        return false;
 
121
 
 
122
    int total = translate + untranslate + fuzzy + obsolete;
 
123
 
 
124
    int d = ( width < height ) ? width - 2 : height - 2;
 
125
 
 
126
    QImage pix( d + 2, d + 2, QImage::Format_ARGB32_Premultiplied );
 
127
    pix.fill( Qt::transparent ); /// transparent background
 
128
 
 
129
    int circle = 16 * 360;
 
130
    int untranslateAngle = untranslate * circle / total;
 
131
    int fuzzyAngle = fuzzy * circle / total;
 
132
    int obsoleteAngle = obsolete * circle / total;
 
133
    int translateAngle = circle - untranslateAngle - fuzzyAngle - obsoleteAngle;
 
134
 
 
135
    QPainter p( &pix );
 
136
    p.setRenderHint( QPainter::Antialiasing );
 
137
 
 
138
    if ( fuzzyAngle > 0 ) {
 
139
        p.setBrush( PoCreatorSettings::self()->fuzzyColor() );
 
140
        if ( fuzzy == total )
 
141
            p.drawEllipse( 1, 1, d, d );
 
142
        else
 
143
            p.drawPie( 1, 1, d, d, 0, -fuzzyAngle );
 
144
    }
 
145
    if ( untranslateAngle > 0 ) {
 
146
        p.setBrush( PoCreatorSettings::self()->untranslatedColor() );
 
147
        if ( untranslate == total )
 
148
            p.drawEllipse( 1, 1, d, d );
 
149
        else
 
150
            p.drawPie( 1, 1, d, d, -fuzzyAngle, -untranslateAngle );
 
151
    }
 
152
    if ( obsoleteAngle > 0 ) {
 
153
        p.setBrush( PoCreatorSettings::self()->obsoletedColor() );
 
154
        if ( obsolete == total )
 
155
            p.drawEllipse( 1, 1, d, d );
 
156
        else
 
157
            p.drawPie( 1, 1, d, d, -fuzzyAngle-untranslateAngle, -obsoleteAngle );
 
158
    }
 
159
    if ( translateAngle > 0 ) {
 
160
        p.setBrush( PoCreatorSettings::self()->translatedColor() );
 
161
        if ( translate == total )
 
162
            p.drawEllipse( 1, 1, d, d );
 
163
        else
 
164
            p.drawPie( 1, 1, d, d, -fuzzyAngle-untranslateAngle-obsoleteAngle, -translateAngle );
 
165
    }
 
166
 
 
167
    img = pix;
 
168
 
 
169
    return true;
 
170
}
 
171
 
 
172
class PoCreatorFormWidget : public QWidget, public Ui::PoCreatorForm
 
173
{
 
174
public:
 
175
    PoCreatorFormWidget() { setupUi( this ); }
 
176
};
 
177
 
 
178
QWidget* PoCreator::createConfigurationWidget()
 
179
{
 
180
    PoCreatorFormWidget* cw = new PoCreatorFormWidget;
 
181
    cw->translatedButton->setColor( PoCreatorSettings::self()->translatedColor() );
 
182
    cw->fuzzyButton->setColor( PoCreatorSettings::self()->fuzzyColor() );
 
183
    cw->untranslatedButton->setColor( PoCreatorSettings::self()->untranslatedColor() );
 
184
    cw->obsoletedButton->setColor( PoCreatorSettings::self()->obsoletedColor() );
 
185
    return cw;
 
186
}
 
187
 
 
188
void PoCreator::writeConfiguration( const QWidget* configurationWidget )
 
189
{
 
190
    const PoCreatorFormWidget* cw = static_cast<const PoCreatorFormWidget*>(configurationWidget);
 
191
    PoCreatorSettings::self()->setTranslatedColor( cw->translatedButton->color() );
 
192
    PoCreatorSettings::self()->setFuzzyColor( cw->fuzzyButton->color() );
 
193
    PoCreatorSettings::self()->setUntranslatedColor( cw->untranslatedButton->color() );
 
194
    PoCreatorSettings::self()->setObsoletedColor( cw->obsoletedButton->color() );
 
195
    PoCreatorSettings::self()->writeConfig();
 
196
}