PageRenderTime 47ms CodeModel.GetById 16ms app.highlight 26ms RepoModel.GetById 1ms app.codeStats 0ms

/safeFatPrinter/trunk/src/dcontroller.cpp

http://cupsfilter.googlecode.com/
C++ | 418 lines | 394 code | 13 blank | 11 comment | 9 complexity | b9438c77d92bb15e7bca1fa2118be1b7 MD5 | raw file
  1#include "dController.h"
  2#include "SimpleItem.h"
  3
  4
  5dController::dController(QObject *parent)
  6{
  7    QUuid uSID=QUuid::createUuid () ;  //generate SID
  8    SID=uSID.toString().replace("{","").replace("}","");
  9    read_settings();
 10    conn_state=false;
 11
 12    connect2Server(serverHostName,serverPort);
 13    createDocModel();
 14}
 15QStandardItemModel *dController::document_model () const
 16{
 17    return doc_model;
 18}
 19void dController::createDocModel()
 20{
 21  doc_model = new QStandardItemModel();
 22
 23  header << QObject::trUtf8("????? ???-??")
 24	  << QObject::trUtf8("???????? ???-??")
 25	  << QObject::trUtf8("????")
 26	  << QObject::trUtf8("????? ???????")
 27	  << QObject::trUtf8("????? ?????")
 28	  << QObject::trUtf8("???-?? ??????")
 29	  << QObject::trUtf8("???????????")
 30	  << QObject::trUtf8("?????????")
 31	  << QObject::trUtf8("???????")
 32	  << QObject::trUtf8("???. ?")
 33	  << QObject::trUtf8("???? ??????????")
 34	  << QObject::trUtf8("?????????? ?1")
 35	  << QObject::trUtf8("?????????? ?2")
 36	  << QObject::trUtf8("?????????? ?3")
 37	  << QObject::trUtf8("?????????? ?4")
 38	  << QObject::trUtf8("?????????? ?5")
 39	  << QObject::trUtf8("?????? ?????????")
 40	  << QObject::trUtf8("????? ????????? ???.")
 41	  << QObject::trUtf8("?????? ????????");
 42  doc_model->setHorizontalHeaderLabels(header);
 43  this->insertDocToModel();
 44}
 45void dController::insertDocToModel()
 46{
 47    // add empty row to model
 48    doc_model->insertRow(doc_model->rowCount());
 49}
 50
 51
 52void dController::insertDocToModel(QString &item)
 53{
 54     QStringList itemList= item.split("~");
 55     QList<QStandardItem *> cells;
 56     int Pos;
 57
 58     for (int i = 0; i <itemList.size() ; ++i) {
 59	 QStandardItem * cell_item= new QStandardItem();
 60	 cells.append(cell_item);
 61     }
 62
 63     for (int i = 0; i <itemList.size() ; ++i) {
 64	 QStringList list_2 = itemList.at(i).split("=", QString::SkipEmptyParts);
 65	 Pos =header.indexOf(list_2.at(0)); // ???? ??????
 66	 QStandardItem *cell_item =cells.at(Pos);
 67	 cell_item->setData(QVariant(list_2.at(1)),Qt::EditRole);
 68     }
 69     doc_model->appendRow (cells);
 70}
 71
 72void dController::setPrinter(QString selected_printer)
 73 {
 74    qDebug() << Q_FUNC_INFO<<selected_printer;
 75    curPrinter=selected_printer;
 76 }
 77void dController::savebase(QString wide_msg)
 78{
 79     currentDoc = wide_msg.split("~");
 80}
 81
 82void dController::checkMB(QString mb)
 83{
 84    //TODO ask server mb
 85    // ?????? ???????? ?????????? ?? ? ???? ???????? ? ????? ?? ?? ???
 86    // ???? ???? ?? ?? ?????????? ?????? ?????????? ??? ??? ??????
 87    QDate dt_end;
 88    dt_end=QDate::currentDate (); // ??????? ????
 89    QDate dt_begin (dt_end.year (), 1,1); // ?????? ??????
 90    QString m=QString(QObject::trUtf8("%1~%2~%3")).arg(mb).arg(dt_begin.toString(),dt_end.toString());
 91    emit sendServerCmd(IS_MB_EXIST,m);
 92}
 93void dController::connect2Server(QString &host,int port)
 94{
 95    netClient  = new netAgent();
 96    netClient->on_login(host,port,this->SID);
 97    connect (netClient,SIGNAL(error_signal(int,QString)),this,SIGNAL(error(int,QString)));
 98    connect (netClient,SIGNAL(takeServerResponce(QString &)),this,SLOT(parseNetwork(QString &)));
 99    connect (this,SIGNAL(sendServerCmd(commands_t ,QString &)), netClient,SLOT(send2Server(commands_t ,QString &)) );
100}
101
102void dController::parseNetwork(QString &message )
103{
104    qDebug() << Q_FUNC_INFO << message;
105    // ?????? ?????? /answer:???? ??????
106    QRegExp rx("^/answer:(.+):(.+)$");
107    if(rx.indexIn(message) != -1){
108	QString ans =rx.cap(1);
109	QString body =rx.cap(2);
110	qDebug() << ans << body <<"\n";
111	switch (ans.toInt()) {
112
113	    case MB_LIST_ANS:
114		  // ??????? ? ??????
115		  this->insertDocToModel(body);
116		break;
117	    case IS_MB_NOT_EXIST_ANS:
118		  // ?????? ????????? ???
119		 emit mbNumberChecked(body);
120		break;
121	    case IS_MB_EXIST_AND_BRAK_ANS:
122		  // ???????? ???? ? ?? ??????????? ????? ????????????
123		//doc_model->clear();// ??????? ?????? ???
124		this->insertDocToModel(body);
125		 emit mbNumberExist(doc_model->rowCount());
126	       break;
127	       case IS_MB_EXIST_AND_NOT_BRAK_ANS:
128		  // ???????? ???? ? ?? ?? ???????????  ?????? ????????????
129		  emit  mbNumberExist(-1);
130		   break;
131	    case REGISTER_ANS:
132		    emit connect2Demon();
133		break;
134	}
135    }
136}
137void dController::parseCnvThread(int Code,QString output )
138{
139    QString msg;
140	if (Code != QProcess::NormalExit) {
141	    msg =QObject::trUtf8("?????? ???????????? ??????????.\n%1:\n??? %2").arg(output).arg(Code,0,10);
142	    emit error(Code,msg);
143	}else{
144	    emit  inputFileConverted();
145	    qDebug() << QObject::tr("??????? ????????? ??????????? ????????? ????? ? pdf.");
146	}
147}
148void dController::parseMergeThread(int Code,QString output )
149{
150    QString msg;
151	if (Code != QProcess::NormalExit) {
152	    msg =QObject::trUtf8("?????? ???????????? ??????????.\n%1:\n??? %2").arg(output).arg(Code,0,10);
153	    emit error(Code,msg);
154	}else{
155	    emit  inputFileMerged();
156	    qDebug() << QObject::tr("??????? ????????? ?????????? ???????.");
157	}
158}
159void dController::read_settings()
160{
161  // TODO ???? ???????? ??????????? ?????? ????????? ??? ?? ??????? ??????????? ????? ini ????
162  qDebug() << Q_FUNC_INFO;
163	QSettings settings(QSettings::IniFormat, QSettings::UserScope,"??????????","?????????? ???????");
164	settings.setIniCodec("UTF-8");
165	settings.beginGroup("SERVICE");
166	serverHostName = settings.value("server","127.0.0.1").toString();
167	serverPort = settings.value("port",4242).toInt();
168	timeout_connect =settings.value("timeout_connect",5000).toInt();
169	timeout_read=settings.value("timeout_read",15000).toInt();
170	timeout_write=settings.value("timeout_write",15000).toInt();
171	settings.endGroup();
172#if defined(Q_OS_UNIX)
173	settings.beginGroup("POSTSCRIPT");
174	gsBin = settings.value("gs_bin","/usr/local/bin/gs").toString();
175
176	spoolDIR = settings.value("spool_dir","//var/log/spool/cups/tmp//").toString();
177	settings.endGroup();
178	settings.beginGroup("PDF");
179	pdfTk = settings.value("pdfTK","/usr/local/bin/pdftk").toString();
180	settings.endGroup();
181#elif defined(Q_OS_WIN)
182	settings.beginGroup("POSTSCRIPT");
183	gsBin = settings.value("gs_bin","C:\\Program Files\\gs\\gs8.70\\bin\\gswin32c.exe").toString();
184	spoolDIR = settings.value("spool_dir","c:\\spool\\").toString();
185	settings.endGroup();
186	settings.beginGroup("PDF");
187	pdftkBin = settings.value("pdfTK","c:\\Tools\\pdftk.exe").toString();
188	settings.endGroup();
189#endif
190	mainPDF.append(spoolDIR).append("%1.pdf");
191	outPDF.append(spoolDIR). append("%1_out.pdf");
192}
193
194
195
196
197
198
199void dController::write_settings()
200{
201    
202    QSettings settings(QSettings::IniFormat, QSettings::UserScope,"??????????","?????????? ???????");
203    settings.setIniCodec("UTF-8");
204
205    settings.beginGroup("SERVICE" );
206    settings.setValue("server",serverHostName);
207    settings.setValue("port",serverPort);
208    settings.setValue("timeout_connect",timeout_connect);
209    settings.setValue("timeout_read",timeout_read);
210    settings.setValue("timeout_write",timeout_write);
211    settings.endGroup();
212
213    settings.beginGroup("POSTSCRIPT" );
214    settings.setValue("gs_bin",gsBin);
215    //settings.setValue("output_fg_PDF",mainPDF);
216    //settings.value("output_PDF",outPDF);
217    settings.setValue("spool_dir",spoolDIR);
218    settings.endGroup();
219
220    settings.beginGroup("PDF");
221    settings.setValue("pdfTK",pdftkBin);
222    settings.endGroup();
223
224}
225void dController::convert2PDF(QString &in_file)
226{
227	ProcessT *proc = new ProcessT();
228	// ?????????? ????????? ????????? ??????
229	connect(proc,SIGNAL(commandOutput(int,QString)),this,SLOT(parseCnvThread(int,QString)));
230
231    	args.clear();
232	args.append( "-q" );
233	args.append( "-dQUIET" );
234	args.append( "-dNOPAUSE" );
235	args.append( "-dPARANOIDSAFER" );
236	args.append( "-dBATCH" );
237	args.append("-r300");
238	args.append( "-sDEVICE=pdfwrite" );
239	mainPDF=mainPDF.arg(SID);
240	outPDF=outPDF.arg(SID);
241	qDebug() << mainPDF;
242	args.append(QString("-sOutputFile=%1").arg(mainPDF));
243	args.append("-c");
244	args.append(".setpdfwrite");
245	args.append("-f");
246	args.append(in_file);
247	proc->execute(gsBin, args,QProcess::MergedChannels);
248	qDebug() <<Q_FUNC_INFO << "convert input file to pdf";
249}
250void dController::mergeTwoPDF(QString &fg_file,QString &bg_file,QString &out_file)
251{
252	ProcessT *proc = new ProcessT();
253	// ?????????? ????????? ????????? ??????
254	connect(proc,
255			SIGNAL(commandOutput(int,QString)),
256		    this,
257			SLOT(parseMergeThread(int,QString))
258			);
259	args.clear();
260	//pdftk in.pdf background back.pdf output out.pdf
261	//pdftk in.pdf stamp back.pdf output out.pdf
262	args.append(fg_file);
263	args.append("stamp");
264	args.append(bg_file);
265	args.append("output");
266	args.append(out_file);
267	proc->execute(pdftkBin, args,QProcess::MergedChannels);
268	qDebug() << Q_FUNC_INFO <<"pdftk " << fg_file <<" stamp " <<bg_file <<" output " <<out_file;
269}
270dController::~dController()
271{
272    qDebug() << Q_FUNC_INFO;
273    write_settings();
274}
275void dController::printWithTemplate (int RowId)
276{
277    /*
278    qDebug()<<Q_FUNC_INFO;
279    //TODO ??? ?????? ???? ? ????????? ?????? !!!
280    scene = new QGraphicsScene();
281
282    page_orient= false;
283    margin_top = MM_TO_POINT(15);
284    margin_bottom=MM_TO_POINT(15);
285    margin_left=MM_TO_POINT(35);
286    margin_right=MM_TO_POINT(15);
287    page_width=210;
288    page_height=297;
289    scene->clear();
290    scene->setSceneRect(0, 0, MM_TO_POINT(page_width),MM_TO_POINT(page_height));
291    QGraphicsRectItem *border_rect = new QGraphicsRectItem (QRectF(margin_left, margin_top, MM_TO_POINT(page_width)-margin_left-margin_right,MM_TO_POINT(page_height)-margin_top-margin_bottom));
292    qDebug() <<"margin_rect"<< margin_left << margin_top << margin_left<<margin_right;
293    border_rect->setPen(QPen(Qt::black,2,Qt::DotLine));
294    border_rect->setBrush(QBrush(Qt::white));
295    border_rect->setOpacity(0.5);
296    border_rect->setZValue(0);
297    border_rect->setData(ObjectName, "Border");
298    scene->addItem(border_rect);
299
300
301	    SimpleItem* Gritem = new SimpleItem;
302	    Gritem->setPos(MM_TO_POINT(180), MM_TO_POINT(0));
303	    Gritem->setPrintFrame(false);
304	    Gritem->setText(QStringList()<<QString("????????")<<QString("????? 12"));
305
306	    Gritem->setFlag(QGraphicsItem::ItemIsMovable);
307
308	    Gritem->setParentItem(border_rect);
309
310	    SimpleItem* MBitem = new SimpleItem;
311	    MBitem->setPos(MM_TO_POINT(5), MM_TO_POINT(280));
312	    MBitem->setPrintFrame(false);
313	    MBitem->setText(QStringList()<<QString("?? ?-%1").arg(templ));
314	    MBitem->setFlag(QGraphicsItem::ItemIsMovable);
315
316	    MBitem->setParentItem(border_rect);
317
318	scene->update();
319
320    QPrinter printer;
321    QString with_t;
322    with_t.append(spoolDIR).append("compl_teml2.pdf");
323
324     printer.setOutputFormat(QPrinter::PdfFormat);
325     printer.setOutputFileName(with_t);
326     printer.setPageSize(QPrinter::A4);
327     QPainter painter(&printer);
328     scene->render(&painter);
329
330     //QString in_file =mainPDF;
331     //in_file.arg(QString(SID));
332     qDebug() <<Q_FUNC_INFO<< mainPDF << outPDF;
333     this->mergeTwoPDF(mainPDF,with_t,outPDF);
334     // ?????? ????????? ?????????
335     scene->clear();
336    border_rect = new QGraphicsRectItem (QRectF(margin_left, margin_top, MM_TO_POINT(page_width)-margin_left-margin_right,MM_TO_POINT(page_height)-margin_top-margin_bottom));
337    qDebug() <<"margin_rect"<< margin_left << margin_top << margin_left<<margin_right;
338    border_rect->setPen(QPen(Qt::black,2,Qt::DotLine));
339    border_rect->setBrush(QBrush(Qt::white));
340    border_rect->setOpacity(0.5);
341    border_rect->setZValue(0);
342    border_rect->setData(ObjectName, "Border");
343    scene->addItem(border_rect);
344
345
346	    SimpleItem* Lastitem = new SimpleItem;
347	    Lastitem->setPos(MM_TO_POINT(40), MM_TO_POINT(200));
348	    Lastitem->setPrintFrame(true);
349	    Lastitem->setText(QStringList()<<QString("????????")
350							 <<QString("????? 12")
351							 <<QString("???????????:??? ???????????")
352							 <<QString("?????????:??? ??? ???????")
353							 <<QString("???????:2-63-15")
354							 <<QString("???.? 12/13")
355							 <<QString("????: 09.09.09'")
356							 <<QString("?? ?-%1").arg(templ)
357							);
358
359	    Lastitem->setFlag(QGraphicsItem::ItemIsMovable);
360
361	    Lastitem->setParentItem(border_rect);
362     QPrinter printer2;
363
364     printer2.setOutputFormat(QPrinter::PdfFormat);
365     QString last_p;
366     last_p.append(spoolDIR).append("last_page.pdf");
367     printer2.setOutputFileName(last_p);
368     printer2.setPageSize(QPrinter::A4);
369     QPainter painter2(&printer2);
370     scene->render(&painter2);
371     */
372     emit sayMeGood();
373}
374void dController::printOverSide(int RowId)
375{
376    /*
377    //TODO ??? ?????? ???? ? ????????? ?????? !!!
378    scene = new QGraphicsScene();
379
380    page_orient= false;
381    margin_top = MM_TO_POINT(15);
382    margin_bottom=MM_TO_POINT(15);
383    margin_left=MM_TO_POINT(35);
384    margin_right=MM_TO_POINT(15);
385    page_width=210;
386    page_height=297;
387    scene->clear();
388    scene->setSceneRect(0, 0, MM_TO_POINT(page_width),MM_TO_POINT(page_height));
389
390    QGraphicsRectItem *border_rect = new QGraphicsRectItem (QRectF(margin_left, margin_top, MM_TO_POINT(page_width)-margin_left-margin_right,MM_TO_POINT(page_height)-margin_top-margin_bottom));
391    qDebug() <<"margin_rect"<< margin_left << margin_top << margin_left<<margin_right;
392    border_rect->setPen(QPen(Qt::black,2,Qt::DotLine));
393    border_rect->setBrush(QBrush(Qt::white));
394    border_rect->setOpacity(0.5);
395    border_rect->setZValue(0);
396    border_rect->setData(ObjectName, "Border");
397    scene->addItem(border_rect);
398
399	    SimpleItem* MBitem = new SimpleItem;
400	    MBitem->setPos(MM_TO_POINT(5), MM_TO_POINT(280));
401	    MBitem->setPrintFrame(false);
402	    MBitem->setText(QStringList()<<QString("?? ?-%1").arg(mb));
403	    MBitem->setFlag(QGraphicsItem::ItemIsMovable);
404
405	    MBitem->setParentItem(border_rect);
406	    //scene->addItem(MBtem);
407	scene->update();
408
409    QPrinter printer;
410    printer.setPrinterName(curPrinter); //?????? ?? ???????? ???????
411     //printer.setOutputFormat(QPrinter::PdfFormat);
412     //printer.setOutputFileName("overside.pdf");
413     //printer.setPageSize(QPrinter::A4);
414     QPainter painter(&printer);
415     scene->render(&painter);
416     */
417     emit sayMeGood();
418}