summaryrefslogtreecommitdiffstats
path: root/kbarcode/label.cpp
blob: 2537fcbc8a3d1941cf22ce6597c36076cb536dc5 (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
/***************************************************************************
                          label.cpp  -  description
                             -------------------
    begin                : Mon Apr 29 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 "label.h"
#include "measurements.h"
#include "kbarcode.h"
#include "printersettings.h"
#include "sqltables.h"
#include "documentitem.h"
#include "barcodeitem.h"
#include "rectitem.h"
#include "lineitem.h"
#include "imageitem.h"
#include "textitem.h"

// Qt includes
#include <qimage.h>
#include <qiodevice.h>
#include <qpaintdevicemetrics.h>
#include <qpainter.h>
#include <qpen.h>
#include <qsqlcursor.h>
#include <qxml.h>

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

Label::Label( Definition* _def, QIODevice* device, QString labelname, QPaintDevice* _printer, QString customer_id, QString _article_no, QString _group )
    : TokenProvider( _printer )
{
    m_sequence = false;
    m_printer = _printer;
   
    setCustomerNo( customer_id );
    setArticleNo( _article_no );
    setGroup( _group );
    setLabelName( labelname.right( labelname.length() - labelname.findRev( "/" ) - 1 ) );

    d = _def;

    load( device );
}

Label::Label( Definition* _def, QIODevice* device, QString labelname, QPaintDevice* _printer )
    : TokenProvider( _printer )
{
    m_sequence = false;
    m_printer = _printer;
   
    setLabelName( labelname.right( labelname.length() - labelname.findRev( "/" ) - 1 ) );

    d = _def;

    load( device );
}

Label::~Label()
{
}

void Label::epcl( QTextStream* stream )
{
    *stream << EPCLUtils::header();
    
    DocumentItem* item;
    for( item = m_list.first();item;item=m_list.next())
	if( drawThisItem( item ) )
	    item->drawEPcl( stream );
    
    *stream << EPCLUtils::footer();
}

void Label::ipl( QTextStream* stream )
{
    IPLUtils utils;
    *stream << utils.header();
    
    DocumentItem* item;
    for( item = m_list.first();item;item=m_list.next())
	if( drawThisItem( item ) )
	    item->drawIpl( stream, &utils );
    
    *stream << utils.footer();
}


void Label::zpl( QTextStream* stream )
{
    *stream << ZPLUtils::header();
    
    DocumentItem* item;
    for( item = m_list.first();item;item=m_list.next())
	if( drawThisItem( item ) )
	    item->drawZpl( stream );
    
    *stream << ZPLUtils::footer();
}

void Label::InitBarcodes()
{
    bool firstbarcode = true;
    
    DocumentItem* item;
    for( item = m_list.first();item;item=m_list.next())
    {
        if( item->rtti() == eRtti_Barcode )
        {
	    /*
	      TODO: I am not sure wether this should be done
	    if( !drawThisItem( item ) )
		continue;
	    */

            BarcodeItem* bc = static_cast<BarcodeItem*>(item);
            bc->setIndex( index() );
            setBarcodeValue( bc );
            bc->updateBarcode();
            
            if( firstbarcode )
            {
                // set some values of the first barcode
                firstbarcode = false;
        
                m_sequence = bc->sequenceEnabled() || m_sequence;
                setBarcodeNo( bc->parsedValue() );
                setEncodingTypeName( bc->type() ); 
            }
        }
    }
    
    updateDone();    
}

void Label::draw( QPainter* painter, int x, int y )
{
    QSize label( (int)d->getMeasurements().width( painter->device() ),
		 (int)d->getMeasurements().height( painter->device() ) );

    InitBarcodes();

    DocumentItem* item;
    for( item = m_list.first();item;item=m_list.next())
	if( drawThisItem( item ) )
	{
	    // add x and y to clip coordinates
	    // as clip has its top left corner
	    // at (0,0)
	    QRect clip( item->boundingRect() );
	    if( x + clip.x() < x )
		clip.setX( 0 );

	    if( y + clip.y() < y )
		clip.setY( 0 );

	    if( clip.x() + clip.width() > label.width() )
		clip.setWidth( label.width() - clip.x() );

	    if( clip.y() + clip.height() > label.height() )
		clip.setHeight( label.height() - clip.y() );
	    
	    painter->save();
	    painter->translate( x,y );
	    painter->setClipRect( clip, QPainter::CoordPainter );
	    item->draw( painter );
	    painter->restore();
	}
}

void Label::setBarcodeValue( Barkode* barcode )
{
    // use the same i18n() for static as in BarcodeSettingsDlg
    if( barcode->databaseMode().lower() != "static" && barcode->databaseMode().lower() != i18n("Static") ) 
    {
        QString encoding_type = getTypeFromCaption( barcode->databaseMode() );
        QString mode = getModeFromCaption( barcode->databaseMode() );

        QSqlQuery query( "select barcode_no, encoding_type from " TABLE_BASIC 
                         " where article_no = '" + articleNo() + "'" ); 
        while ( query.next() ) 
        {
            barcode->setValue( query.value(0).toString() );
            barcode->setType( query.value(1).toString() );
        }

        if( mode.lower() != "main" ) 
        {
            QSqlQuery query1( "select barcode_no from " TABLE_CUSTOMER_TEXT " where customer_no ='"+ mode +
                             "' and article_no='" + articleNo() + "'" );
            while ( query1.next() ) 
            {
                if( !query1.value(0).toString().isEmpty() ) 
                {
                    barcode->setValue( query.value(0).toString() );
                    barcode->setType( encoding_type );
                }
            }
        }
    }
}

void Label::load( QIODevice* device )
{
    if( !device ) return;

    if( !device->isOpen() )
        device->open( IO_ReadOnly );

    QDomDocument doc( "KBarcodeLabel" );        
    doc.setContent( device );
    
    bool kbarcode18;
    QString description;
    Definition* definition = NULL;
    readXMLHeader( &doc, description, kbarcode18, &definition );
    delete definition;
    
    readDocumentItems( &m_list, &doc, NULL, kbarcode18 );
    m_list.setAutoDelete( true );
                
    // sort the list by z index
    m_list.sort();
    
    DocumentItem* item;
    for( item = m_list.first();item;item=m_list.next())
    {
        // set the paint device for all items
        item->setPaintDevice( m_printer );
        item->setTokenProvider( this );
    }
    
    device->close();    
}

void Label::getXLabel( double x, double y, double width, double height, QPainter* painter, int mode, QString value )
{
    painter->save();
    if( mode == LABEL_X ) {
        painter->setPen( QPen( Qt::black, 5) );
        painter->drawLine( (int)x, (int)y, int(x+width), int(y+height) );
        painter->drawLine( (int)x, int(y+height), int(x+width), (int)y );
    } else if( mode == ARTICLE_GROUP_NO ) {
        painter->setPen( QPen( QPen::black, 1 ) );
        QFont f( "helvetica", 15 );
        int w = 0;
        do {
            f.setPointSize( f.pointSize() - 1 );
            painter->setFont( f );
            w = painter->fontMetrics().width( value );
        } while( w > width && f.pointSize() > 0 );

        painter->drawText( int(x + (width-w)/2), int(y + (height-f.pointSize())/2), value);
    }

    painter->restore();
    return;
}

int Label::getId() const
{
    if( d )
        return d->getId();

    return -1;
};

bool Label::drawThisItem( const DocumentItem* item )
{
    QString script = item->visibilityScript();

    // make things go faster if the script is just "true"
    if( script.isEmpty() || script == "true" )
	return true;

    script = parse( script );
    
    return jsParseToBool( script );
}

bool Label::update() 
{
    DocumentItem* item;
    for( item = m_list.first();item;item=m_list.next())
	if( !item->visibilityScript().isEmpty() && item->visibilityScript() != "true" )
	    return true;
	    
    return TokenProvider::update();
}