Add helper function for escaping strings which are copied into clipboard

Introduce a helper function for escaping special characters and for
adding quotes when copying data into the system clipboard.
This commit is contained in:
Martin Kleusberg
2017-12-04 13:03:56 +01:00
parent 316860a709
commit ebd96c6a03
2 changed files with 24 additions and 18 deletions

View File

@@ -275,11 +275,8 @@ void ExtendedTableWidget::copy(const bool withHeaders)
result.append(fieldSepText);
htmlResult.append("</th><th>");
}
// SQLite allows tabs in column and table names. TODO: should we escape double quotes too?
if (headerText.contains('\n') || headerText.contains('\t'))
result.append(QString("\"%1\"").arg(headerText));
else
result.append(headerText);
result.append(escapeCopiedData(headerText));
htmlResult.append(headerText);
}
result.append(rowSepText);
@@ -299,26 +296,15 @@ void ExtendedTableWidget::copy(const bool withHeaders)
htmlResult.append(fieldSepHtml);
}
currentRow = index.row();
QByteArray data = index.data(Qt::EditRole).toByteArray();
QString text = index.data(Qt::EditRole).toByteArray();
// Table cell data
QString text = data;
if (text.contains('\n') || text.contains('\t'))
htmlResult.append("<pre>" + text.toHtmlEscaped() + "</pre>");
else
htmlResult.append(text.toHtmlEscaped());
// Empty string is enquoted in plain text format, whilst NULL isn't
// We also quote the data when there are line breaks in the text, again for spreadsheet compatability.
// We also need to quote when there are tabs in the string (another option would be to replace the tabs by spaces, that's what
// LibreOffice seems to be doing here).
if (!data.isNull()) {
if (text.isEmpty() || text.contains('\n') || text.contains('\t') || text.contains('"')) {
text.replace("\"", "\"\"");
result.append(QString("\"%1\"").arg(text));
} else
result.append(text);
}
result.append(escapeCopiedData(text));
}
QMimeData *mimeData = new QMimeData;
@@ -327,6 +313,25 @@ void ExtendedTableWidget::copy(const bool withHeaders)
qApp->clipboard()->setMimeData(mimeData);
}
QString ExtendedTableWidget::escapeCopiedData(QString data) const
{
// Empty string is enquoted in plain text format, whilst NULL isn't
// We also quote the data when there are line breaks in the text, again for spreadsheet compatability.
// We also need to quote when there are tabs in the string (another option would be to replace the tabs by spaces, that's what
// LibreOffice seems to be doing here).
if(data.isNull())
return data;
if(data.isEmpty() || data.contains('\n') || data.contains('\t') || data.contains('"'))
{
data.replace("\"", "\"\"");
return QString("\"%1\"").arg(data);
} else {
return data;
}
}
void ExtendedTableWidget::paste()
{
// Get list of selected items

View File

@@ -35,6 +35,7 @@ private:
void copy(const bool withHeaders = false);
void paste();
void setPasteData(const QModelIndex& idx, const QString& data);
QString escapeCopiedData(QString data) const;
void useAsFilter();