summaryrefslogtreecommitdiffstats
path: root/kbarcode/labelutils.cpp
blob: 8bf88200547832efcf3508de51eacbb25fdc9115 (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
/***************************************************************************
                          labelutils.cpp  -  description
                             -------------------
    begin                : Sam Okt 26 2002
    copyright            : (C) 2002 by Dominik Seichter
    email                : domseichter@web.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#include "labelutils.h"
#include "dsrichtext.h" 

// TQt includes
#include <tqbitmap.h>
#include <tqimage.h>
#include <tqpaintdevicemetrics.h>
#include <tqpainter.h>
#include <tqsimplerichtext.h>

// KDE includes
#include <kapplication.h>
#include <kglobal.h>
#include <klocale.h>

// own includes
#include "printersettings.h"

#define CONVERSION_FACTOR 25.4000508001016

LabelUtils::LabelUtils()
{
}

LabelUtils::~LabelUtils()
{ }

double LabelUtils::pixelToMm( double pixel, const TQPaintDevice* device, int mode )
{
    TQPaintDeviceMetrics pdm( device ? device : TQT_TQPAINTDEVICE(TDEApplication::desktop()) );
    if( mode == DpiX )
	return (pixel * CONVERSION_FACTOR) / (double)pdm.logicalDpiX();
    else
	return (pixel * CONVERSION_FACTOR) / (double)pdm.logicalDpiY();
}

double LabelUtils::mmToPixel( double mm, const TQPaintDevice* device, int mode )
{
    if( !mm )
        return 0;

    // We don't get valid metrics from the printer - and we want a better resolution
    // anyway (it's the PS driver that takes care of the printer resolution).

    TQPaintDeviceMetrics pdm( device ? device : TQT_TQPAINTDEVICE(TDEApplication::desktop()) );
    
//    tqDebug("DpiX=%i", pdm.logicalDpiX());
//    tqDebug("DpiY=%i", pdm.logicalDpiY());
    if( mode == DpiX )
	return (mm / CONVERSION_FACTOR) * (double)pdm.logicalDpiX();
    else
	return (mm / CONVERSION_FACTOR) * (double)pdm.logicalDpiY();
}

double LabelUtils::pixelToPixelX( double unit, const TQPaintDevice* src, const TQPaintDevice* dest )
{
    TQPaintDeviceMetrics p1( src );
    TQPaintDeviceMetrics p2( dest );

    return ( unit * (double)p2.logicalDpiX() ) / (double)p1.logicalDpiX();
}

double LabelUtils::pixelToPixelY( double unit, const TQPaintDevice* src, const TQPaintDevice* dest )
{
    TQPaintDeviceMetrics p1( src );
    TQPaintDeviceMetrics p2( dest );

    //return pixelToPixelX( unit, src, dest );
    return ( unit * (double)p2.logicalDpiY() ) / (double)p1.logicalDpiY();
}

const TQString LabelUtils::getTypeFromCaption( const TQString & cap )
{
    // TODO: remove this function
    TQString search = cap.right( cap.length() - cap.find(":") - 1 ).lower().stripWhiteSpace();
    return search;
}

const TQString LabelUtils::getModeFromCaption( const TQString & cap )
{
    return cap.left( cap.find(":") ).lower().stripWhiteSpace();
}

TQSize LabelUtils::stringSize( const TQString & t )
{
    TQSimpleRichText srt( t, TDEApplication::font() );
    TQSize s;
    s.setWidth( srt.widthUsed() );
    s.setHeight( srt.height() );

    return s;
}

void LabelUtils::renderString( TQPainter* painter, const TQString & t, const TQRect & rect, double scalex, double scaley )
{
    // DSRichText cannot calculate the width on its own
    TQSimpleRichText srt( t, TDEApplication::font() );
    int width = (rect.width() > 0) ? rect.width() : srt.widthUsed();
    int height = (rect.height() > 0) ? rect.height() : srt.height();
    
    DSRichText r( t );
    r.setX( rect.x() );
    r.setY( rect.y() );
    r.setWidth( width );
    r.setHeight( height );
    r.setScale( scalex, scaley );
    r.draw( painter );    
}

TQPixmap* LabelUtils::drawString( const TQString & t, int w, int h, double rot )
{
    TQSimpleRichText srt( t, TDEApplication::font() );

    int width = (w > 0) ? w : srt.widthUsed();
    int height = (h > 0) ? h : srt.height();
    srt.setWidth( width );
    
    TQPixmap* pix;
    TQPainter painter;
    if( rot == 0.0 ) {
        TQBitmap bm( width, height );
        bm.fill( TQt::color0 ); //transparent
        painter.begin( &bm );
    
          painter.save();
            painter.setPen( TQt::color1 );
            TQColorGroup cg;
            cg.setColor( TQColorGroup::Foreground, TQt::color1 );
              cg.setColor( TQColorGroup::Text, TQt::color1 );
              cg.setColor( TQColorGroup::Base, TQt::color0 );
    
            srt.draw( &painter, 0, 0, bm.rect(), cg );
          painter.restore();
        painter.end();
        
        pix = new TQPixmap( width, height );
        pix->fill( TQt::white );
        pix->setMask( bm );
            
        if( !pix->isNull() ) {
            painter.begin( pix );
            painter.setPen( TQt::black );
            TQColorGroup cg;
            srt.draw( &painter, 0, 0, pix->rect(), cg );
            painter.end();
        } 
    } else {
        int w2 = (w > 0) ? w : srt.widthUsed();
        int h2 = (h > 0) ? h : srt.height();

        TQWMatrix wm;
        wm.rotate( rot );

        TQSize s = LabelUtils::stringSize( t );
        TQPixmap* tmp = LabelUtils::drawString( t, s.width(), s.height() );

        TQPixmap* p = new TQPixmap( w2, h2 );
        p->fill( TQt::white );
        painter.begin( p );
        painter.drawPixmap( 0, 0, tmp->xForm( wm ) );
        painter.end();

        p->setMask( p->createHeuristicMask() );

        pix = p;
        delete tmp;
    }
    
    return pix;
}