summaryrefslogtreecommitdiffstats
path: root/kbarcode/kbarcode.cpp
blob: 32f35597dca1fc859b9819e17168e795e2485845 (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
/***************************************************************************
                          kbarcode.cpp  -  description
                             -------------------
    begin                : Mit Jan 15 2003
    copyright            : (C) 2003 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 "kbarcode.h"
#include "barkode.h"
#include "barcodedialog.h"
#include "batchwizard.h"
#include "labeleditor.h"
#include "databasebrowser.h"
#include "csvimportdlg.h"
#include "sqltables.h"
#include "kbarcodesettings.h"

// TQt includes
#include <tqgroupbox.h>
#include <tqlayout.h>

// KDE includes
#include <kaction.h>
#include <kapplication.h>
#include <kcmdlineargs.h>
#include <kiconloader.h>
#include <klocale.h>
#include <kpopupmenu.h>
#include <kpushbutton.h>
#include <kmessagebox.h>

KBarcode::KBarcode( TQWidget *parent, const char *name, WFlags f)
    : DCOPObject("KBarcode"),
      DSMainWindow( parent, name, f )
{
    TQGroupBox* w = new TQGroupBox( this );
    w->setColumnLayout(0, Qt::Vertical );
    TQVBoxLayout* tqlayout = new TQVBoxLayout( w->tqlayout() );
    setCentralWidget( w );

    buttonSingle = new KPushButton( i18n("Barcode &Generator..."), w );
    buttonSingle->setEnabled( Barkode::haveBarcode() );
    buttonEditor = new KPushButton( i18n("&Label Editor..."), w );
    buttonBatch = new KPushButton( i18n("&Batch Printing..."), w );
    buttonData = new KPushButton( i18n("Edit SQL &Tables..."), w );
    buttonData->setEnabled( false );
    
    buttonSingle->setIconSet( BarIconSet( "barcode" ) );
    buttonEditor->setIconSet( BarIconSet( "edit" ) );
    buttonBatch->setIconSet( BarIconSet( "fileprint" ) );

    tqlayout->addWidget( buttonSingle );
    tqlayout->addWidget( buttonEditor );
    tqlayout->addWidget( buttonBatch );
    tqlayout->addWidget( buttonData );

    connect( buttonSingle, TQT_SIGNAL( clicked() ), TQT_TQOBJECT(this),  TQT_SLOT( startBarcode() ) );
    connect( buttonEditor, TQT_SIGNAL( clicked() ), TQT_TQOBJECT(this),  TQT_SLOT( startLabelEditor() ) );
    connect( buttonBatch, TQT_SIGNAL( clicked() ), TQT_TQOBJECT(this),  TQT_SLOT( startBatchPrint() ) );
    connect( SqlTables::getInstance(), TQT_SIGNAL( connectedSQL() ), TQT_TQOBJECT(this),  TQT_SLOT( enableData() ) );
    
    setupActions();
    show();

    KAction* editLabelDefAct = new KAction(i18n("&Edit Label Definitions"), "",
                                0, TQT_TQOBJECT(this),  TQT_SLOT(editLabelDef()), actionCollection(), "design" );

    KAction* editArticleAct = new KAction(i18n("&Edit Articles"), "",
                                0, TQT_TQOBJECT(this),  TQT_SLOT(editArticles()), actionCollection(), "design" );

    KAction* editCustomerAct = new KAction(i18n("&Edit Customers"), "",
                                0, TQT_TQOBJECT(this),  TQT_SLOT(editCustomers()), actionCollection(), "design" );

    KAction* editCustomerTextAct = new KAction(i18n("&Edit Customer Text"), "",
                                0, TQT_TQOBJECT(this),  TQT_SLOT(editCustomerText()), actionCollection() );

    KAction* importCSVAct = new KAction(i18n("&Import CSV File..."), "",
                                0, TQT_TQOBJECT(this),  TQT_SLOT(importCSV()), actionCollection() );
                                
    KPopupMenu* data = new KPopupMenu( buttonData );
    editLabelDefAct->plug( data );
    editArticleAct->plug( data );
    editCustomerAct->plug( data );
    editCustomerTextAct->plug( data );
    buttonData->setPopup( data );
    data->insertSeparator();
    importCSVAct->plug( data );

    enableData();
}

KBarcode::~KBarcode()
{
    DSMainWindow::saveConfig();
}

void KBarcode::setupActions()
{
    DSMainWindow::setupActions();
}

void KBarcode::startBarcode()
{
    new BarCodeDialog();
}

void KBarcode::startLabelEditor()
{
    LabelEditor* ed = new LabelEditor( NULL, TQString(), "LabelEditorWindow" );
    ed->startupDlg( LabelEditor::eCreateNewLabel, TQString() );
}

void KBarcode::startBatchPrint()
{
    new BatchWizard( this );
}

void KBarcode::editArticles()
{
    new DatabaseBrowser( TABLE_BASIC, NULL, "basicbrowser" );
}

void KBarcode::editCustomers()
{
    new DatabaseBrowser( TABLE_CUSTOMER, NULL, "customerbrowser" );
}

void KBarcode::editCustomerText()
{
    new DatabaseBrowser( TABLE_CUSTOMER_TEXT, NULL, "customertextbrowser" );
}

void KBarcode::editLabelDef()
{
    new DatabaseBrowser( TABLE_LABEL_DEF, NULL, "defbrowser" );
}

void KBarcode::enableData()
{
    buttonData->setEnabled( SqlTables::getInstance()->isConnected() );
}

bool KBarcode::parseCmdLine()
{
    enum { BARCODE, LABEL, BATCH, NONE } mode = NONE;

    KCmdLineArgs* args = KCmdLineArgs::parsedArgs();

    BatchPrinter::EOutputFormat eFormat = BatchPrinter::POSTSCRIPT;
    LabelEditor* pEdit = NULL;
    TQString serial;
    TQString sqlquery   = TQString();
    TQString csvfile    = TQString();
    TQString printer    = TQString();

    bool bPrintNow     = args->isSet("print");
    int serialinc      = 0;
    int numlabels      = -1;

    TQString format = args->getOption("output");
    if( format.upper() == "BARCODE" )
	eFormat = BatchPrinter::BCP;
    else if( format.upper() == "IMAGE" )
	eFormat = BatchPrinter::IMAGE;
    else if( format.upper() == "POSTSCRIPT" )
	eFormat = BatchPrinter::POSTSCRIPT;
    else
    {
	KMessageBox::error( this, i18n("%1 is no valid output format for --output. Valid values are POSTSCRIPT, IMAGE and BARCODE.").tqarg( format ) );
    }

    if( args->isSet("barcode") )
    {
	mode = BARCODE;
	startBarcode();
    }

    if( args->isSet("batch") )
	mode = BATCH;
    
    if( args->isSet("label") )
	mode = LABEL;

    if( args->isSet("serialnumber") )
    {
	serial = args->getOption("serialnumber");
	if( args->isSet("serialinc") )
	    serialinc = args->getOption("serialinc").toInt();
    }

    if( args->isSet("numlabels") )
	numlabels = args->getOption("numlabels").toInt();

    if( args->isSet("importsql") )
	sqlquery = args->getOption("importsql");

    if( args->isSet("importcsv") ) 
	csvfile = args->getOption("importcsv");

    if( args->isSet("printer") )
	printer = args->getOption("printer");

    for( int i = 0; i < args->count(); i++) 
	if( mode == BATCH )
	{
	    BatchWizard* b = new BatchWizard();
	    b->setFilename( args->url( i ).path() );
	    b->setOutputFormat( eFormat );
	    b->setSerialNumber( serial, serialinc );
	    if( !sqlquery.isEmpty() )
		b->setImportSqlQuery( sqlquery );

	    if( !csvfile.isEmpty() )
		b->setImportCsvFile( csvfile );

	    if( numlabels != -1 )
		b->setNumLabels( numlabels );

	    if( bPrintNow )
            {
		b->printNow( printer, false );
                delete b;
            }
	}
	else
        {
	    pEdit = new LabelEditor( 0, args->url( i ).path() );
            if( bPrintNow )
                // TODO: use the printer set by the printer commandline option
                pEdit->print(); 
        }

    if( !args->count() && mode == LABEL )
    {
        pEdit = new LabelEditor();
        if( bPrintNow )
            // TODO: use the printer set by the printer commandline option
            pEdit->print(); 

    }

    int argc = args->count();
    args->clear();  // Free some memory

    // close after printing
    if( bPrintNow && argc )
    {
        // TODO: really close the whole application
        this->close();
        return true;
    }
    else
        return false;    
}

void KBarcode::importCSV()
{
    new CSVImportDlg( this, "csvimportdlg" );
}

bool KBarcode::isSTQLConnected() const
{
    return SqlTables::isConnected();
}

bool KBarcode::connectSQL()
{
    return SqlTables::getInstance()->connectMySQL();
}

void KBarcode::showWizard()
{
    DSMainWindow::wizard();
}

void KBarcode::showConfigure()
{
    KBarcodeSettings::getInstance()->configure();
}


#include "kbarcode.moc"