Signals | Public Member Functions | Public Attributes

ExportTableThread Class Reference

#include <exporttablethread.h>

List of all members.

Signals

void rowCount (int)
void finishedRows (int)
void completed (bool)

Public Member Functions

 ExportTableThread (QObject *parent=0)
void run (QSqlDatabase db, QString dbName, QString tableName, QString fileName, QString fieldDeliminator, QString fieldEncloser, QString lineDeliminator, bool addColumnsOnTop, bool CSV, bool HTML, bool XML, bool Excel)

Public Attributes

QString strQuery
QStringList columnList

Detailed Description

Definition at line 7 of file exporttablethread.h.


Constructor & Destructor Documentation

ExportTableThread::ExportTableThread ( QObject *  parent = 0 ) [explicit]

Definition at line 3 of file exporttablethread.cpp.

                                                    :
        QThread(parent)
{
}

Member Function Documentation

void ExportTableThread::completed ( bool   ) [signal]

Referenced by run().

void ExportTableThread::finishedRows ( int   ) [signal]

Referenced by run().

void ExportTableThread::rowCount ( int   ) [signal]

Referenced by run().

void ExportTableThread::run ( QSqlDatabase  db,
QString  dbName,
QString  tableName,
QString  fileName,
QString  fieldDeliminator,
QString  fieldEncloser,
QString  lineDeliminator,
bool  addColumnsOnTop,
bool  CSV,
bool  HTML,
bool  XML,
bool  Excel 
)

Definition at line 8 of file exporttablethread.cpp.

References columnList, completed(), finishedRows(), rowCount(), and strQuery.

Referenced by ExportTable::startExport().

                                                  {
    db.open();
    if (db.isOpen()) {

        QString textToExport;
        QString lineTerminator = lineDeliminator;
        QSqlQuery query = QSqlQuery(db);
        query.exec(tr("use `%1`").arg(dbName));
        query.clear();
        if (query.exec(strQuery)) {
            emit rowCount(query.size());
            int fin = 0;
            if (CSV) {
                if (addColumnsOnTop) {
                    QSqlQuery query1 = QSqlQuery(db);
                    if (query1.exec(strQuery)) {
                        QString rowText = "";
                        query1.first();
                        QSqlRecord headerRec = query1.record();
                        for (int irec = 0; irec < headerRec.count(); irec++) {
                            if (columnList.contains(headerRec.fieldName(irec))) {
                                rowText.append(fieldEncloser);
                                rowText.append(headerRec.fieldName(irec));
                                rowText.append(fieldEncloser);
                                rowText.append(fieldDeliminator);
                            }
                        }
                        rowText = rowText.left(rowText.length() -1);
                        textToExport.append(rowText);
                        textToExport.append(lineTerminator);
                        query1.clear();
                    }
                }
                while (query.next()) {
                    fin = fin + 1;
                    QSqlRecord rec = query.record();
                    QString rowText = "";
                    for (int col = 0; col < rec.count(); col++) {
                        if (columnList.contains(rec.fieldName(col))) {
                            rowText.append(fieldEncloser);
                            rowText.append(rec.value(col).toString());
                            rowText.append(fieldEncloser);
                            rowText.append(fieldDeliminator);
                        }
                    }
                    rowText = rowText.left(rowText.length() -1);
                    textToExport.append(rowText);
                    textToExport.append(lineTerminator);
                    emit finishedRows(fin);
                }
            } else if (HTML) {
                QString table;
                table = "<html>";
                table.append("\n<head>");
                table.append("\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">");
                table.append(tr("<title>Output for %1</title>").arg(tableName));
                table.append(tr("\n"
                                "<style type=\"text/css\" <!--\n"
                                ".normal { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; color: #000000}\n"
                                ".medium { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 15px; font-weight: bold; color #000000; text-decoration: none}\n"
                                "--></style>"
                                "\n"));
                table.append("\n</head>");
                table.append("\n<body>");
                table.append(tr("\n<h3>%1</h3><table border=1>\n<tr>").arg(tableName));
                textToExport = table;
                QSqlQuery query1 = QSqlQuery(db);
                if (query1.exec(strQuery)) {
                    QString rowText = "";
                    query1.first();
                    QSqlRecord headerRec = query1.record();
                    for (int irec = 0; irec < headerRec.count(); irec++) {
                        if (columnList.contains(headerRec.fieldName(irec))) {
                            rowText.append("<td bgcolor=silver class='medium'>");
                            rowText.append(headerRec.fieldName(irec));
                            rowText.append("</td>");
                            rowText.append("\n");
                        }
                    }
                    rowText.append("\n</tr>");
                    textToExport.append(rowText);
                    textToExport.append(lineTerminator);
                    query1.clear();
                }
                while (query.next()) {
                    fin = fin + 1;
                    QSqlRecord rec = query.record();
                    QString rowText = "\n<tr>";
                    for (int col = 0; col < rec.count(); col++) {
                        if (columnList.contains(rec.fieldName(col))) {
                            rowText.append("<td class='normal' valign='top'>");
                            rowText.append(rec.value(col).toString());
                            rowText.append("</td>");
                            rowText.append("\n");
                        }
                    }
                    rowText.append("</tr>");
                    textToExport.append(rowText);
                    emit finishedRows(fin);
                }
                textToExport.append("\n</table></body></html>");
            } else if (XML) {
                QString header = "<data>";
                textToExport = header;
                while (query.next()) {
                    fin = fin + 1;
                    QSqlRecord rec = query.record();
                    QString rowText = "\n<row>";
                    for (int col = 0; col < rec.count(); col++) {
                        if (columnList.contains(rec.fieldName(col))) {
                            rowText.append(tr("<%1>").arg(rec.fieldName(col)));
                            rowText.append(rec.value(col).toString());
                            rowText.append(tr("</%1>").arg(rec.fieldName(col)));
                        }
                    }
                    rowText.append("</row>");
                    textToExport.append(rowText);
                    emit finishedRows(fin);
                }
                textToExport.append("\n</data>");

            } else if (Excel) {

                QString header = "";
                header.append(tr(""
                                 "<?xml version\"1.0\"?>"
                                 "\n<?mso-application progid=\"Excel.Sheet\"?>"
                                 "\n<Workbook"
                                 "\n   xmlns:x=\"urn:schemas-microsoft-com:office:excel\""
                                 "\n   xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\""
                                 "\n   xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\">"
                                 "\n<styles>"
                                 "\n<style ss:ID=\"Default\" ss:\"Normal\">"
                                 "\n<Alignment ss:Vertical=\"Bottom\"/>"
                                 "\n<Borders/>"
                                 "\n<Font/>"
                                 "\n<Interior/>"
                                 "\n<NumberFormat/>"
                                 "\n<Protection/>"
                                 "\n</style>"
                                 "\n</styles>"
                                 "\n"));
                header.append("<Worksheet ss:Name=\"Sheet1\">\n<ss:Table>");
                textToExport = header;
                while (query.next()) {
                    fin = fin + 1;
                    QSqlRecord rec = query.record();
                    QString rowText = "\n<ss:Row>\n";
                    for (int col = 0; col < rec.count(); col++) {
                        if (columnList.contains(rec.fieldName(col))) {
                            //
                            rowText.append(tr("<ss:Cell><Data ss:Type=\"String\">%1</Data></ss:Cell>\n").arg(rec.value(col).toString()));
                            //
                        }
                    }
                    rowText.append("</ss:Row>");
                    textToExport.append(rowText);
                    emit finishedRows(fin);
                }
                textToExport.append("\n</ss:Table>\n</Worksheet>\n</Workbook>");
            }

            QFile file(fileName);
            if (!file.open(QFile::WriteOnly | QFile::Text)) {
                return;
            }
            QTextStream out(&file);
            out << textToExport;
            emit completed(true);
        } else {
            emit completed(false);
        }
    }
}

Member Data Documentation

Definition at line 16 of file exporttablethread.h.

Referenced by run(), and ExportTable::startExport().

Definition at line 12 of file exporttablethread.h.

Referenced by run(), and ExportTable::startExport().


The documentation for this class was generated from the following files: