QString toListViewFormatterText::getFormattedString(toExportSettings &settings, //const toResultModel *model); const QAbstractItemModel * model) { int columns = model->columnCount(); int rows = model->rowCount(); QString output; QVector<int> sizes(columns); QVector<int> rlist = selectedRows(settings.selected); QVector<int> clist = selectedColumns(settings.selected); // must get widest length for each column // zero array or (if writing headers, set their size) for (int i = 0; i < columns - 1; i++) { if (settings.columnsHeader) { if (settings.columnsExport == toExportSettings::ColumnsSelected && !clist.contains(i)) continue; sizes[i] = model->headerData( i, Qt::Horizontal, Qt::DisplayRole).toString().length(); } else sizes[i] = 0; } sizes[columns - 1] = 1; // loop through model and get column widths QModelIndex mi; for (int row = 0; row < rows; row++) { for (int column = 0; column < columns - 1; column++) { if (settings.columnsExport == toExportSettings::ColumnsSelected && !clist.contains(column)) continue; if (!settings.rowsHeader && column == 0) continue; mi = model->index(row, column); QVariant data = model->data(mi, Qt::EditRole); QString v; if (data.isNull()) v = "{null}"; else v = data.toString(); int len = v.length(); if (len > sizes[column]) sizes[column] = len; } } // write header data to fixed widths if (settings.columnsHeader) { for (int column = 0; column < columns; column++) { if (settings.columnsExport == toExportSettings::ColumnsSelected && !clist.contains(column)) continue; if (!settings.rowsHeader && column == 0) continue; QString value = model->headerData( column, Qt::Horizontal, Qt::DisplayRole).toString(); output += value; for (int left = value.length(); left <= sizes[column]; left++) output += ' '; } endLine(output); // write ==== border for (int column = 0; column < columns; column++) { if (settings.columnsExport == toExportSettings::ColumnsSelected && !clist.contains(column)) continue; if (!settings.rowsHeader && column == 0) continue; for (int left = 0; left < sizes[column]; left++) output += '='; output += ' '; } endLine(output); } // write data for (int row = 0; row < rows; row++) { if (settings.rowsExport == toExportSettings::RowsSelected && !rlist.contains(row)) continue; for (int column = 0; column < columns; column++) { if (settings.columnsExport == toExportSettings::ColumnsSelected && !clist.contains(column)) continue; if (!settings.rowsHeader && column == 0) continue; mi = model->index(row, column); QVariant data = model->data(mi, Qt::EditRole); QString value; if (data.isNull()) value = "{null}"; else value = data.toString(); output += value; for (int left = value.length(); left <= sizes[column]; left++) output += ' '; } endLine(output); } return output; }
QString toListViewFormatterXLSX::getFormattedString(toExportSettings &settings, const QAbstractItemModel * model) { // Thx to ClipView tool static QString const DOC_START( "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n" "<?mso-application progid=\"Excel.Sheet\"?>\r\n" "<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\"\r\n" " xmlns:o=\"urn:schemas-microsoft-com:office:office\"\r\n" " xmlns:x=\"urn:schemas-microsoft-com:office:excel\"\r\n" " xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\"\r\n" " xmlns:html=\"http://www.w3.org/TR/REC-html40\">\r\n" " <Worksheet ss:Name=\"Sheet1\">\r\n" " <Table ss:ExpandedColumnCount=\"%1\" ss:ExpandedRowCount=\"%2\">\r\n" ); static QString const ROW_START(" <Row>\r\n"); static QString const ROW_LINE (" <Cell><Data ss:Type=\"%1\">%2</Data></Cell>\r\n"); static QString const ROW_END (" </Row>\r\n"); static QString const DOC_END ( " </Table>\r\n" " </Worksheet>\r\n" "</Workbook>\r\n" ); int columns = model->columnCount(); int rows = model->rowCount(); QString output; QVector<int> rlist = selectedRows(settings.selected); QVector<int> clist = selectedColumns(settings.selected); // -1 for XLSX does not support row number int columnCount = settings.columnsExport == toExportSettings::ColumnsSelected ? clist.size()-1 : columns; int rowCount = settings.rowsExport == toExportSettings::RowsSelected ? rlist.size() : rows; output += DOC_START.arg(columnCount).arg(rowCount); // write data for (int row = 0; row < rows; row++) { if (settings.rowsExport == toExportSettings::RowsSelected && !rlist.contains(row)) continue; output += ROW_START; for (int column = 1; column < columns; column++) { if (settings.columnsExport == toExportSettings::ColumnsSelected && !clist.contains(column)) continue; QVariant data = model->data(model->index(row, column), Qt::EditRole); QVariant type = model->headerData(column, Qt::Horizontal, Qt::UserRole); QString value; if (data.isNull()) value = "{null}"; else value = TO_ESCAPE(data.toString()); output += ROW_LINE.arg("String").arg(value); } output += ROW_END; } output += DOC_END; return output; }
QString toListViewFormatterHTML::getFormattedString(toExportSettings &settings, //const toResultModel *model); const QAbstractItemModel * model) { int columns = model->columnCount(); int rows = model->rowCount(); QString output; QString indent; QVector<int> rlist = selectedRows(settings.selected); QVector<int> clist = selectedColumns(settings.selected); output = QString("<HTML><HEAD><TITLE>Export</TITLE></HEAD><BODY><TABLE>"); endLine(output); QString bgcolor; if (settings.columnsHeader) { output += QString("<TR>"); endLine(output); for (int j = 0; j < columns; j++) { if (settings.columnsExport == toExportSettings::ColumnsSelected && !clist.contains(j)) continue; output += QString("\t<TH>"); endLine(output); QString text(Qt::escape(model->headerData(j, Qt::Horizontal, Qt::DisplayRole).toString())); output += "\t\t" + text; endLine(output); output += QString("\t</TH>"); endLine(output); } output += "</TR>"; endLine(output); } QModelIndex mi; for (int row = 0; row < rows; row++) { if (settings.rowsExport == toExportSettings::RowsSelected && !rlist.contains(row)) continue; output += "<TR>"; endLine(output); for (int i = 0; i < columns; i++) { if (settings.columnsExport == toExportSettings::ColumnsSelected && !clist.contains(i)) continue; if (!settings.rowsHeader && i == 0) continue; output += QString("\t<TD>"); endLine(output); mi = model->index(row, i); QString text(Qt::escape(model->data(mi, Qt::EditRole).toString())); output += "\t\t" + text; endLine(output); output += QString("\t</TD>"); endLine(output); } output += "</TR>"; endLine(output); } output += "</TABLE></BODY></HTML>"; return output; }