mirror of
https://github.com/fralx/LimeReport.git
synced 2024-12-27 01:48:09 +03:00
127 lines
4.0 KiB
C++
127 lines
4.0 KiB
C++
#include <QApplication>
|
|
#include <QUuid>
|
|
#include <LimeReport>
|
|
#include <iostream>
|
|
#include <QDebug>
|
|
#include <QDir>
|
|
#include <QFile>
|
|
#include <QCommandLineParser>
|
|
#include <QPrinterInfo>
|
|
|
|
#ifdef _WIN32
|
|
#include <io.h>
|
|
#include <fcntl.h>
|
|
#endif
|
|
|
|
enum PrintType{ Printer, PDF, Display};
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
QApplication a(argc, argv);
|
|
QApplication::setApplicationVersion(LIMEREPORT_VERSION_STR);
|
|
QStringList vars;
|
|
|
|
#if QT_VERSION > QT_VERSION_CHECK(5, 2, 0)
|
|
QCommandLineParser parser;
|
|
parser.addHelpOption();
|
|
parser.addVersionOption();
|
|
QCommandLineOption sourceOption(QStringList() << "s" << "source",
|
|
QCoreApplication::translate("main", "Limereport pattern file name"),
|
|
QCoreApplication::translate("main", "source"));
|
|
parser.addOption(sourceOption);
|
|
QCommandLineOption destinationOption(QStringList() << "d" << "destination",
|
|
QCoreApplication::translate("main", "Output file name"),
|
|
QCoreApplication::translate("main", "destination"));
|
|
parser.addOption(destinationOption);
|
|
QCommandLineOption variablesOption(QStringList() << "p" << "param",
|
|
QCoreApplication::translate("main", "Report parameter (can be more than one)"),
|
|
QCoreApplication::translate("main", "param_name=param_value"));
|
|
parser.addOption(variablesOption);
|
|
QCommandLineOption typeOption(QStringList() << "t" << "type",
|
|
QCoreApplication::translate("main", "Output type (Printer | PDF)"),
|
|
QCoreApplication::translate("main", "type"));
|
|
parser.addOption(typeOption);
|
|
parser.process(a);
|
|
|
|
LimeReport::ReportEngine report;
|
|
|
|
if (parser.value(sourceOption).isEmpty()){
|
|
std::cerr<<"Error! Report file is not specified !! \n";
|
|
return 1;
|
|
}
|
|
|
|
if (!report.loadFromFile(parser.value(sourceOption))){
|
|
std::cerr<<"Error! Report file \""+parser.value(sourceOption).toStdString()+"\" not found \n";
|
|
return 1;
|
|
}
|
|
|
|
if (!parser.values(variablesOption).isEmpty()){
|
|
foreach(QString var, parser.values(variablesOption)){
|
|
QStringList varItem = var.split("=");
|
|
if (varItem.size() == 2)
|
|
report.dataManager()->setReportVariable(varItem.at(0),varItem.at(1));
|
|
}
|
|
}
|
|
|
|
|
|
PrintType printType = PDF;
|
|
|
|
if (!parser.value(typeOption).isEmpty()){
|
|
if (parser.value(typeOption).compare("Printer",Qt::CaseInsensitive) == 0)
|
|
printType = Printer;
|
|
if (parser.value(typeOption).compare("Display",Qt::CaseInsensitive) == 0)
|
|
printType = Display;
|
|
}
|
|
|
|
|
|
QPrinterInfo pi;
|
|
QPrinter printer;
|
|
|
|
switch(printType){
|
|
case PDF:
|
|
if (parser.value(destinationOption).isEmpty()){
|
|
report.printToPDF(QFileInfo(parser.value(sourceOption)).baseName());
|
|
} else {
|
|
report.printToPDF(parser.value(destinationOption));
|
|
}
|
|
break;
|
|
case Printer:
|
|
if (parser.value(destinationOption).isEmpty()){
|
|
if (!pi.defaultPrinterName().isEmpty()){
|
|
printer.setPrinterName(pi.defaultPrinterName());
|
|
report.printReport(&printer);
|
|
} else {
|
|
std::cerr<<"Error! Default printer not found on system";
|
|
}
|
|
} else {
|
|
printer.setPrinterName(parser.value(destinationOption));
|
|
report.printReport(&printer);
|
|
}
|
|
break;
|
|
case Display:
|
|
report.previewReport();
|
|
break;
|
|
}
|
|
|
|
#else
|
|
std::cerr<<"This demo intended for Qt 5.2 and higher\n";
|
|
#endif
|
|
// QUuid uid = QUuid::createUuid();
|
|
// QString uidStr = uid.toString()+".pdf";
|
|
// report.printToPDF(uidStr);
|
|
// QFile in(uidStr);
|
|
// QFile out;
|
|
// out.open(stdout, QFile::WriteOnly);
|
|
// in.open(QIODevice::ReadOnly);
|
|
//#ifdef _WIN32
|
|
// _setmode(fileno(stdout),O_BINARY);
|
|
//#endif
|
|
// QByteArray buffer = in.readAll();
|
|
// fwrite(buffer,1,buffer.size(),stdout);
|
|
// in.close();
|
|
// in.remove();
|
|
|
|
return 0;
|
|
//return a.exec();
|
|
}
|