summaryrefslogtreecommitdiffstats
path: root/kbabel/addons/preview/pothumbcreator.cpp
blob: f067948a0987c401f276d8e86a86ed3492863004 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*  
  This file is part of KBabel
  Copyright (C) 2001 Matthias Kiefer <kiefer@kde.org>

  Text painting code is based on the text file preview textthumbnail by
  Copyright (C) 2000 Carsten Pfeiffer <pfeiffer@kde.org> 
                2000 Malte Starostik <malte@kde.org> 

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program 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 General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

  In addition, as a special exception, the copyright holders give
  permission to link the code of this program with any edition of
  the Qt library by Trolltech AS, Norway (or with modified versions
  of Qt that use the same license as Qt), and distribute linked
  combinations including the two.  You must obey the GNU General
  Public License in all respects for all of the code used other than
  Qt. If you modify this file, you may extend this exception to
  your version of the file, but you are not obligated to do so.  If
  you do not wish to do so, delete this exception statement from
  your version.
  
*/

#include <qpainter.h>
#include <qpixmap.h>
#include <qimage.h>

#include <kapplication.h>
#include <kglobal.h>
#include <kiconloader.h>
#include <kstandarddirs.h>
#include <kpixmapsplitter.h>

#include "pothumbcreator.h"
#include "poinfo.h"

using namespace KBabel;

extern "C"
{
    KDE_EXPORT ThumbCreator *new_creator()
    {
        return new PoThumbCreator;
    }
}

PoThumbCreator::PoThumbCreator()
    : m_splitter(0)
{
}

PoThumbCreator::~PoThumbCreator()
{
    if(m_splitter)
        delete m_splitter;
}

bool PoThumbCreator::create(const QString &path, int width, int height, QImage &img)
{
    if ( !m_splitter )
    {
        m_splitter = new KPixmapSplitter;
        QString pixmap = locate( "data", "konqueror/pics/thumbnailfont_7x4.png" );
        if ( !pixmap.isEmpty() )
        {
            m_splitter->setPixmap( QPixmap( pixmap ));
            m_splitter->setItemSize( QSize( 4, 7 ));
        }
    }

    bool ok = false;
    PoInfo poInfo;
    QStringList wordList;

    // We do not call msgfmt, as a thumbnail must be created fast.
    if( PoInfo::info( path, poInfo, wordList, false, true, false ) == OK )
    {
       ok = true;
       
       QPixmap pix;
       if (height * 3 > width * 4)
           pix.resize(width, width * 4 / 3);
       else
           pix.resize(height * 3 / 4, height);

       pix.fill( QColor( 245, 245, 245 ) ); // light-grey background

      
       
       // one pixel for the rectangle, the rest. whitespace
       int xBorder = 1 + pix.width()/16;    // minimum x-border
       int yBorder = 1 + pix.height()/16; // minimum y-border

       int circle = 16*360;
       int fuzzyAngle = poInfo.fuzzy*circle/poInfo.total;
       int untransAngle = poInfo.untranslated*circle/poInfo.total;
       
       int w = pix.width()-2*xBorder;
       int h = pix.height()*2/3-2*yBorder;

       int d = QMIN(w,h);
       xBorder = (pix.width()-d)/2;
       yBorder = (pix.height()*2/3-d)/2;
       
       QPainter p(&pix);
       
       if(fuzzyAngle>0)
       {
           p.setBrush(Qt::blue);
           
           if(poInfo.total == poInfo.fuzzy)
           {
               p.drawEllipse(xBorder,yBorder,d,d);
           }
           else
           {
               p.drawPie(xBorder,yBorder, d, d, 0, -fuzzyAngle);
           }
       }
       
       if(untransAngle>0)
       {
           p.setBrush(Qt::red);

           if(poInfo.untranslated == poInfo.total)
           {
               p.drawEllipse(xBorder,yBorder,d,d);
           }
           else
           {
               p.drawPie(xBorder,yBorder, d, d, -fuzzyAngle, -untransAngle);
           }
       }
       
       if(circle - fuzzyAngle - untransAngle>0)
       {
           p.setBrush(Qt::darkGreen);

           if(poInfo.fuzzy==0 && poInfo.untranslated==0)
           {
               p.drawEllipse(xBorder,yBorder,d,d);
           }
           else
           {
               p.drawPie(xBorder,yBorder, d, d, -fuzzyAngle-untransAngle
               , -(circle-fuzzyAngle-untransAngle) );
           }
       }
       
       p.end();
       
       

       QRect rect;

       QSize chSize = m_splitter->itemSize(); // the size of one char
       int xOffset = chSize.width();
       int yOffset = chSize.height();

       // one pixel for the rectangle, the rest. whitespace
       xBorder = 1 + pix.width()/16;    // minimum x-border
       yBorder = 1 + pix.height()/16; // minimum y-border

       // calculate a better border so that the text is centered
       int canvasWidth = pix.width() - 2*xBorder;
       int canvasHeight = pix.height()/3 -  2*yBorder;
       int numCharsPerLine = (int) (canvasWidth / chSize.width());
       int numLines = (int) (canvasHeight / chSize.height());

        int rest = pix.width() - (numCharsPerLine * chSize.width());
        xBorder = QMAX( xBorder, rest/2); // center horizontally
        rest = pix.height()/3 - (numLines * chSize.height());
        yBorder = QMAX( yBorder, rest/2); // center vertically
        // end centering


        QString text;
        
        if(numCharsPerLine < 30)
        {
            if(!poInfo.revision.isEmpty())
                text += poInfo.revision+'\n';
            if(!poInfo.lastTranslator.isEmpty())
                text += poInfo.lastTranslator+'\n';
            if(!poInfo.languageTeam.isEmpty())
                text += poInfo.languageTeam+'\n';
            if(!poInfo.contentType.isEmpty())
                text += poInfo.contentType+'\n';
            if(!poInfo.creation.isEmpty())
                text += poInfo.creation+'\n';
            if(!poInfo.project.isEmpty())
                text += poInfo.project+'\n';
            if(!poInfo.mimeVersion.isEmpty())
                text += "MIME-Version: "+poInfo.mimeVersion+'\n';
            if(!poInfo.encoding.isEmpty())
                text += poInfo.encoding+'\n';
 
            int lines = text.contains('\n')+1;
            if(lines < numLines)
            {
                text = poInfo.headerComment+'\n'+text;
                if(!poInfo.others.isEmpty())
                    text += poInfo.others+'\n';
            }

            if(text.at(text.length()-1) == '\n')
                text.truncate(text.length()-1);
        }
        else
        {   
            if(!poInfo.headerComment.isEmpty())
                text += poInfo.headerComment+'\n';
            if(!poInfo.project.isEmpty())
                text += "Project-Id-Version: "+poInfo.project+'\n';
            if(!poInfo.creation.isEmpty())
                text += "POT-Creation-Date: "+poInfo.creation+'\n';
            if(!poInfo.revision.isEmpty())
                text += "PO-Revision-Date: "+ poInfo.revision+'\n';
            if(!poInfo.lastTranslator.isEmpty())
                text += "Last-Translator: "+poInfo.lastTranslator+'\n';
            if(!poInfo.languageTeam.isEmpty())
                text += "Language-Team: "+poInfo.languageTeam+'\n';
            if(!poInfo.contentType.isEmpty())
                text += "Content-Type: "+poInfo.contentType+'\n';
            if(!poInfo.mimeVersion.isEmpty())
                text += "MIME-Version: "+poInfo.mimeVersion+'\n';
            if(!poInfo.encoding.isEmpty())
                text += "Content-Transfer-Encoding: "+poInfo.encoding+'\n';
            if(!poInfo.others.isEmpty())
                text += poInfo.others;


            if(text.at(text.length()-1) == '\n')
                text.truncate(text.length()-1);
        }
               /*
            double p = ((double)(poInfo.total-poInfo.fuzzy-poInfo.untranslated))*100/poInfo.total;
            text = locale->formatNumber(p,0)+'%';
        */
        // find the maximum string length to center the text
        QStringList lineList=QStringList::split('\n',text);
        uint max=0;
        for( QStringList::Iterator it = lineList.begin(); it != lineList.end()
                ; ++it )
        {
            if((*it).length() > max)
                max = (*it).length();
        }        
        rest = pix.width() - (max * chSize.width());
        xBorder = QMAX( xBorder, rest/2); // center horizontally
        rest = pix.height()/3 - (lineList.count() * chSize.height());
        yBorder = QMAX( yBorder, rest/2); // center vertically

        // where to paint the characters
        int x = xBorder, y = pix.height()*2/3; 
        int posNewLine  = pix.width() - (chSize.width() + xBorder);
        int posLastLine = pix.height() - (chSize.height() + yBorder);
        bool newLine = false;
        Q_ASSERT( posNewLine > 0 );
        const QPixmap *fontPixmap = &(m_splitter->pixmap());


        for ( uint i = 0; i < text.length(); i++ )
        {
            if ( x > posNewLine || newLine ) // start a new line?
            {
                x = xBorder;
                y += yOffset;

                if ( y > posLastLine ) // more text than space
                    break;

                // after starting a new line, we also jump to the next
                // physical newline in the file if we don't come from one
                if ( !newLine )
                {
                    int pos = text.find( '\n', i );
                    if ( pos > (int) i )
                        i = pos +1;
                }

                newLine = false;
           }

           // check for newlines in the text (unix,dos)
           QChar ch = text.at( i );
           if ( ch == '\n' )
           {
               newLine = true;
               continue;
           }
           else if ( ch == '\r' && text.at(i+1) == '\n' )
           {
               newLine = true;
               i++; // skip the next character (\n) as well
               continue;
           }
                
           rect = m_splitter->coordinates( ch );
           if ( !rect.isEmpty() )
           {
               bitBlt( &pix, QPoint(x,y), fontPixmap, rect, Qt::CopyROP );
           }

           x += xOffset; // next character
       }

        // very very seldom a babelfish lives in po files and even
        // in this seldom cases they are usually hidden ;-)
        if(pix.width() > 40 && KApplication::random()%2000 == 0)
        {
           QPixmap kbabelPix; 
           if(pix.width() < 80)
           {
               kbabelPix = KGlobal::iconLoader()->loadIcon("kbabel"
                       ,KIcon::Small,16,KIcon::DefaultState,0,true);
           }
           else if(pix.width() < 150)
           {
               kbabelPix = KGlobal::iconLoader()->loadIcon("kbabel"
                       ,KIcon::Desktop,32,KIcon::DefaultState,0,true);
           }
           else
           {
               kbabelPix = KGlobal::iconLoader()->loadIcon("kbabel"
                       ,KIcon::Desktop,48,KIcon::DefaultState,0,true);
           }
           
           int x = pix.width()-kbabelPix.width()-4;
           x = QMAX(x,0);
           if(!kbabelPix.isNull())
           {
               bitBlt(&pix, QPoint(x,4), &kbabelPix, kbabelPix.rect()
                       , Qt::CopyROP);
           }
        }

       img = pix.convertToImage();
    }

    return ok;
}

ThumbCreator::Flags PoThumbCreator::flags() const
{
    return (Flags)(DrawFrame);
}